Implement colormask

This commit is contained in:
Henrik Rydgård 2016-12-26 18:32:52 +01:00
parent 2952588a54
commit cbf4e5f38c
7 changed files with 50 additions and 59 deletions

View file

@ -71,7 +71,7 @@ SoftGPU::SoftGPU(GraphicsContext *gfxCtx, Draw::DrawContext *_thin3D)
InputLayout *inputLayout = thin3d->CreateInputLayout(desc);
DepthStencilState *depth = thin3d->CreateDepthStencilState({ false, false, Comparison::LESS });
BlendState *blendstateOff = thin3d->CreateBlendState({ false });
BlendState *blendstateOff = thin3d->CreateBlendState({ false, 0xF });
RasterState *rasterNoCull = thin3d->CreateRasterState({});
samplerNearest = thin3d->CreateSamplerState({ TextureFilter::NEAREST, TextureFilter::NEAREST, TextureFilter::NEAREST });

View file

@ -592,7 +592,7 @@ void NativeInitGraphics(GraphicsContext *graphicsContext) {
uiContext->theme = &ui_theme;
Draw::InputLayout *inputLayout = ui_draw2d.CreateInputLayout(thin3d);
Draw::BlendState *blendNormal = thin3d->CreateBlendState({ true, BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA });
Draw::BlendState *blendNormal = thin3d->CreateBlendState({ true, 0xF, BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA });
Draw::DepthStencilState *depth = thin3d->CreateDepthStencilState({ false, false, Comparison::LESS });
Draw::RasterState *rasterNoCull = thin3d->CreateRasterState({});

View file

@ -9,6 +9,20 @@
namespace Draw {
bool RefCountedObject::Release() {
if (refcount_ > 0 && refcount_ < 10000) {
refcount_--;
if (refcount_ == 0) {
delete this;
return true;
}
}
else {
ELOG("Refcount (%d) invalid for object %p - corrupt?", refcount_, this);
}
return false;
}
// ================================== PIXEL/FRAGMENT SHADERS
// The Vulkan ones can be re-used with modern GL later if desired, as they're just GLSL.

View file

@ -237,20 +237,8 @@ public:
RefCountedObject() : refcount_(1) {}
virtual ~RefCountedObject() {}
// TODO: Reconsider this annoying ref counting stuff.
virtual void AddRef() { refcount_++; }
virtual bool Release() {
if (refcount_ > 0 && refcount_ < 10000) {
refcount_--;
if (refcount_ == 0) {
delete this;
return true;
}
} else {
ELOG("Refcount (%d) invalid for object %p - corrupt?", refcount_, this);
}
return false;
}
void AddRef() { refcount_++; }
bool Release();
private:
int refcount_;
@ -371,6 +359,7 @@ struct DepthStencilStateDesc {
struct BlendStateDesc {
bool enabled;
int colorMask;
BlendFactor srcCol;
BlendFactor dstCol;
BlendOp eqCol;
@ -379,7 +368,13 @@ struct BlendStateDesc {
BlendOp eqAlpha;
bool logicEnabled;
LogicOp logicOp;
// int colorMask;
};
enum {
COLOR_MASK_R = 1,
COLOR_MASK_G = 2,
COLOR_MASK_B = 4,
COLOR_MASK_A = 8,
};
enum BorderColor {

View file

@ -125,6 +125,7 @@ public:
D3DBLENDOP eqCol, eqAlpha;
D3DBLEND srcCol, srcAlpha, dstCol, dstAlpha;
uint32_t fixedColor;
uint32_t colorMask;
void Apply(LPDIRECT3DDEVICE9 device) {
device->SetRenderState(D3DRS_ALPHABLENDENABLE, (DWORD)enabled);
@ -134,6 +135,7 @@ public:
device->SetRenderState(D3DRS_DESTBLEND, dstCol);
device->SetRenderState(D3DRS_SRCBLENDALPHA, srcAlpha);
device->SetRenderState(D3DRS_DESTBLENDALPHA, dstAlpha);
device->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
// device->SetRenderState(, fixedColor);
}
};

View file

@ -109,7 +109,7 @@ public:
GLuint srcCol, srcAlpha, dstCol, dstAlpha;
bool logicEnabled;
GLuint logicOp;
// int maskBits;
int colorMask;
// uint32_t fixedColor;
void Apply() {
@ -120,10 +120,8 @@ public:
} else {
glDisable(GL_BLEND);
}
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
// glColorMask(maskBits & 1, (maskBits >> 1) & 1, (maskBits >> 2) & 1, (maskBits >> 3) & 1);
// glBlendColor(fixedColor);
glColorMask(colorMask & 1, (colorMask >> 1) & 1, (colorMask >> 2) & 1, (colorMask >> 3) & 1);
#if !defined(USING_GLES2)
if (logicEnabled) {
glEnable(GL_COLOR_LOGIC_OP);
@ -755,6 +753,7 @@ BlendState *OpenGLContext::CreateBlendState(const BlendStateDesc &desc) {
bs->logicEnabled = desc.logicEnabled;
bs->logicOp = logicOpToGL[(int)desc.logicOp];
#endif
bs->colorMask = desc.colorMask;
return bs;
}

View file

@ -58,7 +58,7 @@ static const VkCompareOp compToVK[] = {
};
// So can this.
static const VkBlendOp blendEqToGL[] = {
static const VkBlendOp blendEqToVk[] = {
VK_BLEND_OP_ADD,
VK_BLEND_OP_SUBTRACT,
VK_BLEND_OP_REVERSE_SUBTRACT,
@ -121,28 +121,8 @@ static inline void Uint8x4ToFloat4(uint32_t u, float f[4]) {
class VKBlendState : public BlendState {
public:
bool blendEnabled;
VkBlendOp eqCol, eqAlpha;
VkBlendFactor srcCol, srcAlpha, dstColor, dstAlpha;
bool logicEnabled;
VkLogicOp logicOp;
void ToVulkan(VkPipelineColorBlendStateCreateInfo *info, VkPipelineColorBlendAttachmentState *attachments) const {
memset(info, 0, sizeof(*info));
info->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
info->attachmentCount = 1;
info->logicOp = logicOp;
info->logicOpEnable = logicEnabled;
attachments[0].blendEnable = blendEnabled;
attachments[0].colorBlendOp = eqCol;
attachments[0].alphaBlendOp = eqAlpha;
attachments[0].colorWriteMask = 0xF;
attachments[0].dstAlphaBlendFactor = dstAlpha;
attachments[0].dstColorBlendFactor = dstColor;
attachments[0].srcAlphaBlendFactor = srcAlpha;
attachments[0].srcColorBlendFactor = srcCol;
info->pAttachments = attachments;
}
VkPipelineColorBlendStateCreateInfo info{};
std::vector<VkPipelineColorBlendAttachmentState> attachments;
};
class VKDepthStencilState : public DepthStencilState {
@ -839,10 +819,6 @@ Pipeline *VKContext::CreateGraphicsPipeline(const PipelineDesc &desc) {
stage.flags = 0;
}
VkPipelineColorBlendStateCreateInfo colorBlend;
VkPipelineColorBlendAttachmentState attachment0;
blend->ToVulkan(&colorBlend, &attachment0);
VkPipelineDepthStencilStateCreateInfo depthStencil;
depth->ToVulkan(&depthStencil);
@ -875,7 +851,7 @@ Pipeline *VKContext::CreateGraphicsPipeline(const PipelineDesc &desc) {
info.flags = 0;
info.stageCount = (uint32_t)stages.size();
info.pStages = stages.data();
info.pColorBlendState = &colorBlend;
info.pColorBlendState = &blend->info;
info.pDepthStencilState = &depthStencil;
info.pDynamicState = &dynamicInfo;
info.pInputAssemblyState = &inputAssembly;
@ -991,15 +967,20 @@ DepthStencilState *VKContext::CreateDepthStencilState(const DepthStencilStateDes
BlendState *VKContext::CreateBlendState(const BlendStateDesc &desc) {
VKBlendState *bs = new VKBlendState();
bs->blendEnabled = desc.enabled;
bs->eqCol = blendEqToGL[(int)desc.eqCol];
bs->srcCol = blendFactorToVk[(int)desc.srcCol];
bs->dstColor = blendFactorToVk[(int)desc.dstCol];
bs->eqAlpha = blendEqToGL[(int)desc.eqAlpha];
bs->srcAlpha = blendFactorToVk[(int)desc.srcAlpha];
bs->dstAlpha = blendFactorToVk[(int)desc.dstAlpha];
bs->logicEnabled = desc.logicEnabled;
bs->logicOp = logicOpToVK[(int)desc.logicOp];
bs->info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
bs->info.attachmentCount = 1;
bs->info.logicOp = logicOpToVK[(int)desc.logicOp];
bs->info.logicOpEnable = desc.logicEnabled;
bs->attachments.resize(1);
bs->attachments[0].blendEnable = desc.enabled;
bs->attachments[0].colorBlendOp = blendEqToVk[(int)desc.eqCol];
bs->attachments[0].alphaBlendOp = blendEqToVk[(int)desc.eqAlpha];
bs->attachments[0].colorWriteMask = desc.colorMask;
bs->attachments[0].dstAlphaBlendFactor = blendFactorToVk[(int)desc.dstAlpha];
bs->attachments[0].dstColorBlendFactor = blendFactorToVk[(int)desc.dstCol];
bs->attachments[0].srcAlphaBlendFactor = blendFactorToVk[(int)desc.srcAlpha];
bs->attachments[0].srcColorBlendFactor = blendFactorToVk[(int)desc.srcCol];
bs->info.pAttachments = bs->attachments.data();
return bs;
}