From d0d04be4e0047892c22152dbaea6fb044283a8c3 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Mon, 30 Nov 2020 17:30:53 +0800 Subject: [PATCH] fix cache reset on thread switch --- native/cocos/renderer/core/gfx/GFXDef.h | 2 +- .../renderer/gfx-gles2/GLES2Commands.cpp | 517 +++++++++--------- .../cocos/renderer/gfx-gles2/GLES2Context.cpp | 82 +-- .../renderer/gfx-gles2/GLES2GPUObjects.h | 8 +- .../renderer/gfx-gles3/GLES3Commands.cpp | 25 +- .../cocos/renderer/gfx-gles3/GLES3Context.cpp | 84 ++- .../renderer/gfx-gles3/GLES3GPUObjects.h | 8 +- 7 files changed, 369 insertions(+), 357 deletions(-) diff --git a/native/cocos/renderer/core/gfx/GFXDef.h b/native/cocos/renderer/core/gfx/GFXDef.h index 15f87cb242..aec6599f52 100644 --- a/native/cocos/renderer/core/gfx/GFXDef.h +++ b/native/cocos/renderer/core/gfx/GFXDef.h @@ -1020,7 +1020,7 @@ struct BlendState { uint32_t isA2C = 0; uint32_t isIndepend = 0; Color blendColor; - BlendTargetList targets; + BlendTargetList targets{1}; }; struct PipelineStateInfo { diff --git a/native/cocos/renderer/gfx-gles2/GLES2Commands.cpp b/native/cocos/renderer/gfx-gles2/GLES2Commands.cpp index 4f7fd67cbc..b668a55384 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Commands.cpp +++ b/native/cocos/renderer/gfx-gles2/GLES2Commands.cpp @@ -1409,15 +1409,18 @@ void GLES2CmdFuncBeginRenderPass(GLES2Device *device, GLES2GPURenderPass *gpuRen break; } - // restore states - if (glClears & GL_COLOR_BUFFER_BIT) { - ColorMask colorMask = cache->bs.targets[0].blendColorMask; - if (colorMask != ColorMask::ALL) { - glColorMask((GLboolean)(colorMask & ColorMask::R), - (GLboolean)(colorMask & ColorMask::G), - (GLboolean)(colorMask & ColorMask::B), - (GLboolean)(colorMask & ColorMask::A)); + if (gpuRenderPass->depthStencilAttachment.format != Format::UNKNOWN) { + bool hasDepth = GFX_FORMAT_INFOS[(int)gpuRenderPass->depthStencilAttachment.format].hasDepth; + if (hasDepth) { + switch (gpuRenderPass->depthStencilAttachment.depthLoadOp) { + case LoadOp::LOAD: break; // GL default behaviour + case LoadOp::CLEAR: { + if (!cache->dss.depthWrite) { + GL_CHECK(glDepthMask(true)); } + GL_CHECK(glClearDepthf(clearDepth)); + glClears |= GL_DEPTH_BUFFER_BIT; + break; } case LoadOp::DISCARD: { // invalidate fbo @@ -1471,7 +1474,7 @@ void GLES2CmdFuncBeginRenderPass(GLES2Device *device, GLES2GPURenderPass *gpuRen } } - if ((glClears & GL_COLOR_BUFFER_BIT) && !cache->dss.depthWrite) { + if ((glClears & GL_DEPTH_BUFFER_BIT) && !cache->dss.depthWrite) { GL_CHECK(glDepthMask(false)); } @@ -2007,269 +2010,269 @@ void GLES2CmdFuncBindState(GLES2Device *device, GLES2GPUPipelineState *gpuPipeli } } } - } // if + } + } + } // if - // bind vao - if (gpuInputAssembler && gpuPipelineState->gpuShader && - (isShaderChanged || gpuInputAssembler != gfxStateCache.gpuInputAssembler)) { - gfxStateCache.gpuInputAssembler = gpuInputAssembler; - if (device->useVAO()) { - GLuint hash = gpuPipelineState->gpuShader->glProgram ^device->getThreadID(); - GLuint glVAO = gpuInputAssembler->glVAOs[hash]; - if (!glVAO) { - GL_CHECK(glGenVertexArraysOES(1, &glVAO)); - gpuInputAssembler->glVAOs[hash] = glVAO; - GL_CHECK(glBindVertexArrayOES(glVAO)); - GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0)); - GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); + // bind vao + if (gpuInputAssembler && gpuPipelineState->gpuShader && + (isShaderChanged || gpuInputAssembler != gfxStateCache.gpuInputAssembler)) { + gfxStateCache.gpuInputAssembler = gpuInputAssembler; + if (device->useVAO()) { + GLuint hash = gpuPipelineState->gpuShader->glProgram ^device->getThreadID(); + GLuint glVAO = gpuInputAssembler->glVAOs[hash]; + if (!glVAO) { + GL_CHECK(glGenVertexArraysOES(1, &glVAO)); + gpuInputAssembler->glVAOs[hash] = glVAO; + GL_CHECK(glBindVertexArrayOES(glVAO)); + GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0)); + GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); - for (size_t j = 0; j < gpuPipelineState->gpuShader->glInputs.size(); ++j) { - const GLES2GPUInput &gpuInput = gpuPipelineState->gpuShader->glInputs[j]; - for (size_t a = 0; a < gpuInputAssembler->attributes.size(); ++a) { - const GLES2GPUAttribute &gpuAttribute = gpuInputAssembler->glAttribs[a]; - if (gpuAttribute.name == gpuInput.name) { - GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, gpuAttribute.glBuffer)); + for (size_t j = 0; j < gpuPipelineState->gpuShader->glInputs.size(); ++j) { + const GLES2GPUInput &gpuInput = gpuPipelineState->gpuShader->glInputs[j]; + for (size_t a = 0; a < gpuInputAssembler->attributes.size(); ++a) { + const GLES2GPUAttribute &gpuAttribute = gpuInputAssembler->glAttribs[a]; + if (gpuAttribute.name == gpuInput.name) { + GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, gpuAttribute.glBuffer)); - for (uint c = 0; c < gpuAttribute.componentCount; ++c) { - GLint glLoc = gpuInput.glLoc + c; - uint attribOffset = - gpuAttribute.offset + gpuAttribute.size * c; - GL_CHECK(glEnableVertexAttribArray(glLoc)); + for (uint c = 0; c < gpuAttribute.componentCount; ++c) { + GLint glLoc = gpuInput.glLoc + c; + uint attribOffset = + gpuAttribute.offset + gpuAttribute.size * c; + GL_CHECK(glEnableVertexAttribArray(glLoc)); - cache->glEnabledAttribLocs[glLoc] = true; - GL_CHECK(glVertexAttribPointer(glLoc, gpuAttribute.count, - gpuAttribute.glType, - gpuAttribute.isNormalized, - gpuAttribute.stride, - BUFFER_OFFSET( - attribOffset))); + cache->glEnabledAttribLocs[glLoc] = true; + GL_CHECK(glVertexAttribPointer(glLoc, gpuAttribute.count, + gpuAttribute.glType, + gpuAttribute.isNormalized, + gpuAttribute.stride, + BUFFER_OFFSET( + attribOffset))); - if (device->useInstancedArrays()) { - GL_CHECK(glVertexAttribDivisorEXT(glLoc, - gpuAttribute.isInstanced - ? 1 : 0)); - } - } - break; + if (device->useInstancedArrays()) { + GL_CHECK(glVertexAttribDivisorEXT(glLoc, + gpuAttribute.isInstanced + ? 1 : 0)); } - } // for - } // for - - if (gpuInputAssembler->gpuIndexBuffer) { - GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, - gpuInputAssembler->gpuIndexBuffer->glBuffer)); - } - - GL_CHECK(glBindVertexArrayOES(0)); - GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0)); - GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); - cache->glVAO = 0; - cache->glArrayBuffer = 0; - cache->glElementArrayBuffer = 0; - } - - if (cache->glVAO != glVAO) { - GL_CHECK(glBindVertexArrayOES(glVAO)); - cache->glVAO = glVAO; - } - } else { - for (uint a = 0; a < cache->glCurrentAttribLocs.size(); ++a) { - cache->glCurrentAttribLocs[a] = false; - } - - for (size_t j = 0; j < gpuPipelineState->gpuShader->glInputs.size(); ++j) { - const GLES2GPUInput &gpuInput = gpuPipelineState->gpuShader->glInputs[j]; - for (size_t a = 0; a < gpuInputAssembler->attributes.size(); ++a) { - const GLES2GPUAttribute &gpuAttribute = gpuInputAssembler->glAttribs[a]; - if (gpuAttribute.name == gpuInput.name) { - if (cache->glArrayBuffer != gpuAttribute.glBuffer) { - GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, gpuAttribute.glBuffer)); - cache->glArrayBuffer = gpuAttribute.glBuffer; - } - - for (uint c = 0; c < gpuAttribute.componentCount; ++c) { - GLint glLoc = gpuInput.glLoc + c; - uint attribOffset = gpuAttribute.offset + gpuAttribute.size * c; - GL_CHECK(glEnableVertexAttribArray(glLoc)); - cache->glEnabledAttribLocs[glLoc] = true; - cache->glCurrentAttribLocs[glLoc] = true; - GL_CHECK(glVertexAttribPointer(glLoc, gpuAttribute.count, - gpuAttribute.glType, - gpuAttribute.isNormalized, - gpuAttribute.stride, - BUFFER_OFFSET(attribOffset))); - - if (device->useInstancedArrays()) { - GL_CHECK(glVertexAttribDivisorEXT(glLoc, - gpuAttribute.isInstanced - ? 1 : 0)); - } - } - break; } - } // for - } // for - - if (gpuInputAssembler->gpuIndexBuffer) { - if (cache->glElementArrayBuffer != - gpuInputAssembler->gpuIndexBuffer->glBuffer) { - GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, - gpuInputAssembler->gpuIndexBuffer->glBuffer)); - cache->glElementArrayBuffer = gpuInputAssembler->gpuIndexBuffer->glBuffer; + break; } - } + } // for + } // for - for (uint a = 0; a < cache->glCurrentAttribLocs.size(); ++a) { - if (cache->glEnabledAttribLocs[a] != cache->glCurrentAttribLocs[a]) { - GL_CHECK(glDisableVertexAttribArray(a)); - cache->glEnabledAttribLocs[a] = false; - } - } - } // if + if (gpuInputAssembler->gpuIndexBuffer) { + GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, + gpuInputAssembler->gpuIndexBuffer->glBuffer)); + } + + GL_CHECK(glBindVertexArrayOES(0)); + GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0)); + GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)); + cache->glVAO = 0; + cache->glArrayBuffer = 0; + cache->glElementArrayBuffer = 0; } - if (gpuPipelineState && !gpuPipelineState->dynamicStates.empty()) { - for (DynamicStateFlagBit dynamicState : gpuPipelineState->dynamicStates) { - switch (dynamicState) { - case DynamicStateFlagBit::VIEWPORT: - if (cache->viewport != viewport) { - cache->viewport = viewport; - GL_CHECK(glViewport(viewport.left, viewport.top, viewport.width, - viewport.height)); - } - break; - case DynamicStateFlagBit::SCISSOR: - if (cache->scissor != scissor) { - cache->scissor = scissor; - GL_CHECK(glScissor(scissor.x, scissor.y, scissor.width, - scissor.height)); - } - break; - case DynamicStateFlagBit::LINE_WIDTH: - if (cache->rs.lineWidth != lineWidth) { - cache->rs.lineWidth = lineWidth; - GL_CHECK(glLineWidth(lineWidth)); - } - break; - case DynamicStateFlagBit::DEPTH_BIAS: - if ((bool) cache->rs.depthBiasEnabled != depthBiasEnabled) { - if (depthBiasEnabled) - GL_CHECK(glEnable(GL_POLYGON_OFFSET_FILL)); - else - GL_CHECK(glDisable(GL_POLYGON_OFFSET_FILL)); + if (cache->glVAO != glVAO) { + GL_CHECK(glBindVertexArrayOES(glVAO)); + cache->glVAO = glVAO; + } + } else { + for (uint a = 0; a < cache->glCurrentAttribLocs.size(); ++a) { + cache->glCurrentAttribLocs[a] = false; + } - cache->rs.depthBiasEnabled = depthBiasEnabled; + for (size_t j = 0; j < gpuPipelineState->gpuShader->glInputs.size(); ++j) { + const GLES2GPUInput &gpuInput = gpuPipelineState->gpuShader->glInputs[j]; + for (size_t a = 0; a < gpuInputAssembler->attributes.size(); ++a) { + const GLES2GPUAttribute &gpuAttribute = gpuInputAssembler->glAttribs[a]; + if (gpuAttribute.name == gpuInput.name) { + if (cache->glArrayBuffer != gpuAttribute.glBuffer) { + GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, gpuAttribute.glBuffer)); + cache->glArrayBuffer = gpuAttribute.glBuffer; + } + + for (uint c = 0; c < gpuAttribute.componentCount; ++c) { + GLint glLoc = gpuInput.glLoc + c; + uint attribOffset = gpuAttribute.offset + gpuAttribute.size * c; + GL_CHECK(glEnableVertexAttribArray(glLoc)); + cache->glEnabledAttribLocs[glLoc] = true; + cache->glCurrentAttribLocs[glLoc] = true; + GL_CHECK(glVertexAttribPointer(glLoc, gpuAttribute.count, + gpuAttribute.glType, + gpuAttribute.isNormalized, + gpuAttribute.stride, + BUFFER_OFFSET(attribOffset))); + + if (device->useInstancedArrays()) { + GL_CHECK(glVertexAttribDivisorEXT(glLoc, + gpuAttribute.isInstanced + ? 1 : 0)); } - if ((cache->rs.depthBias != depthBias.constant) || - (cache->rs.depthBiasSlop != depthBias.slope)) { - GL_CHECK(glPolygonOffset(depthBias.constant, depthBias.slope)); - cache->rs.depthBias = depthBias.constant; - cache->rs.depthBiasSlop = depthBias.slope; - } - break; - case DynamicStateFlagBit::BLEND_CONSTANTS: - if ((cache->bs.blendColor.x != gpuPipelineState->bs.blendColor.x) || - (cache->bs.blendColor.y != gpuPipelineState->bs.blendColor.y) || - (cache->bs.blendColor.z != gpuPipelineState->bs.blendColor.z) || - (cache->bs.blendColor.w != gpuPipelineState->bs.blendColor.w)) { - GL_CHECK(glBlendColor(gpuPipelineState->bs.blendColor.x, - gpuPipelineState->bs.blendColor.y, - gpuPipelineState->bs.blendColor.z, - gpuPipelineState->bs.blendColor.w)); - cache->bs.blendColor = gpuPipelineState->bs.blendColor; - } - break; - case DynamicStateFlagBit::STENCIL_WRITE_MASK: - switch (stencilWriteMask.face) { - case StencilFace::FRONT: - if (cache->dss.stencilWriteMaskFront != - stencilWriteMask.writeMask) { - GL_CHECK(glStencilMaskSeparate(GL_FRONT, - stencilWriteMask.writeMask)); - cache->dss.stencilWriteMaskFront = stencilWriteMask.writeMask; - } - break; - case StencilFace::BACK: - if (cache->dss.stencilWriteMaskBack != - stencilWriteMask.writeMask) { - GL_CHECK(glStencilMaskSeparate(GL_BACK, - stencilWriteMask.writeMask)); - cache->dss.stencilWriteMaskBack = stencilWriteMask.writeMask; - } - break; - case StencilFace::ALL: - if ((cache->dss.stencilWriteMaskFront != - stencilWriteMask.writeMask) || - (cache->dss.stencilWriteMaskBack != - stencilWriteMask.writeMask)) { - GL_CHECK(glStencilMask(stencilWriteMask.writeMask)); - cache->dss.stencilWriteMaskFront = stencilWriteMask.writeMask; - cache->dss.stencilWriteMaskBack = stencilWriteMask.writeMask; - } - break; - } - break; - case DynamicStateFlagBit::STENCIL_COMPARE_MASK: - switch (stencilCompareMask.face) { - case StencilFace::FRONT: - if ((cache->dss.stencilRefFront != - (uint) stencilCompareMask.refrence) || - (cache->dss.stencilReadMaskFront != - stencilCompareMask.compareMask)) { - GL_CHECK(glStencilFuncSeparate(GL_FRONT, - GLES2_CMP_FUNCS[(uint) cache->dss.stencilFuncFront], - stencilCompareMask.refrence, - stencilCompareMask.compareMask)); - cache->dss.stencilRefFront = stencilCompareMask.refrence; - cache->dss.stencilReadMaskFront = stencilCompareMask.compareMask; - } - break; - case StencilFace::BACK: - if ((cache->dss.stencilRefBack != - (uint) stencilCompareMask.refrence) || - (cache->dss.stencilReadMaskBack != - stencilCompareMask.compareMask)) { - GL_CHECK(glStencilFuncSeparate(GL_BACK, - GLES2_CMP_FUNCS[(uint) cache->dss.stencilFuncBack], - stencilCompareMask.refrence, - stencilCompareMask.compareMask)); - cache->dss.stencilRefBack = stencilCompareMask.refrence; - cache->dss.stencilReadMaskBack = stencilCompareMask.compareMask; - } - break; - case StencilFace::ALL: - if ((cache->dss.stencilRefFront != - (uint) stencilCompareMask.refrence) || - (cache->dss.stencilReadMaskFront != - stencilCompareMask.compareMask) || - (cache->dss.stencilRefBack != - (uint) stencilCompareMask.refrence) || - (cache->dss.stencilReadMaskBack != - stencilCompareMask.compareMask)) { - GL_CHECK(glStencilFuncSeparate(GL_FRONT, - GLES2_CMP_FUNCS[(uint) cache->dss.stencilFuncFront], - stencilCompareMask.refrence, - stencilCompareMask.compareMask)); - GL_CHECK(glStencilFuncSeparate(GL_BACK, - GLES2_CMP_FUNCS[(uint) cache->dss.stencilFuncBack], - stencilCompareMask.refrence, - stencilCompareMask.compareMask)); - cache->dss.stencilRefFront = stencilCompareMask.refrence; - cache->dss.stencilReadMaskFront = stencilCompareMask.compareMask; - cache->dss.stencilRefBack = stencilCompareMask.refrence; - cache->dss.stencilReadMaskBack = stencilCompareMask.compareMask; - } - break; - } - break; - default: - CC_LOG_ERROR("Invalid dynamic states."); - break; - } // switch + } + break; + } } // for - } // if + } // for + + if (gpuInputAssembler->gpuIndexBuffer) { + if (cache->glElementArrayBuffer != + gpuInputAssembler->gpuIndexBuffer->glBuffer) { + GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, + gpuInputAssembler->gpuIndexBuffer->glBuffer)); + cache->glElementArrayBuffer = gpuInputAssembler->gpuIndexBuffer->glBuffer; + } + } + + for (uint a = 0; a < cache->glCurrentAttribLocs.size(); ++a) { + if (cache->glEnabledAttribLocs[a] != cache->glCurrentAttribLocs[a]) { + GL_CHECK(glDisableVertexAttribArray(a)); + cache->glEnabledAttribLocs[a] = false; + } + } } - } + } // if + + if (gpuPipelineState && !gpuPipelineState->dynamicStates.empty()) { + for (DynamicStateFlagBit dynamicState : gpuPipelineState->dynamicStates) { + switch (dynamicState) { + case DynamicStateFlagBit::VIEWPORT: + if (cache->viewport != viewport) { + cache->viewport = viewport; + GL_CHECK(glViewport(viewport.left, viewport.top, viewport.width, + viewport.height)); + } + break; + case DynamicStateFlagBit::SCISSOR: + if (cache->scissor != scissor) { + cache->scissor = scissor; + GL_CHECK(glScissor(scissor.x, scissor.y, scissor.width, + scissor.height)); + } + break; + case DynamicStateFlagBit::LINE_WIDTH: + if (cache->rs.lineWidth != lineWidth) { + cache->rs.lineWidth = lineWidth; + GL_CHECK(glLineWidth(lineWidth)); + } + break; + case DynamicStateFlagBit::DEPTH_BIAS: + if ((bool) cache->rs.depthBiasEnabled != depthBiasEnabled) { + if (depthBiasEnabled) + GL_CHECK(glEnable(GL_POLYGON_OFFSET_FILL)); + else + GL_CHECK(glDisable(GL_POLYGON_OFFSET_FILL)); + + cache->rs.depthBiasEnabled = depthBiasEnabled; + } + if ((cache->rs.depthBias != depthBias.constant) || + (cache->rs.depthBiasSlop != depthBias.slope)) { + GL_CHECK(glPolygonOffset(depthBias.constant, depthBias.slope)); + cache->rs.depthBias = depthBias.constant; + cache->rs.depthBiasSlop = depthBias.slope; + } + break; + case DynamicStateFlagBit::BLEND_CONSTANTS: + if ((cache->bs.blendColor.x != gpuPipelineState->bs.blendColor.x) || + (cache->bs.blendColor.y != gpuPipelineState->bs.blendColor.y) || + (cache->bs.blendColor.z != gpuPipelineState->bs.blendColor.z) || + (cache->bs.blendColor.w != gpuPipelineState->bs.blendColor.w)) { + GL_CHECK(glBlendColor(gpuPipelineState->bs.blendColor.x, + gpuPipelineState->bs.blendColor.y, + gpuPipelineState->bs.blendColor.z, + gpuPipelineState->bs.blendColor.w)); + cache->bs.blendColor = gpuPipelineState->bs.blendColor; + } + break; + case DynamicStateFlagBit::STENCIL_WRITE_MASK: + switch (stencilWriteMask.face) { + case StencilFace::FRONT: + if (cache->dss.stencilWriteMaskFront != + stencilWriteMask.writeMask) { + GL_CHECK(glStencilMaskSeparate(GL_FRONT, + stencilWriteMask.writeMask)); + cache->dss.stencilWriteMaskFront = stencilWriteMask.writeMask; + } + break; + case StencilFace::BACK: + if (cache->dss.stencilWriteMaskBack != + stencilWriteMask.writeMask) { + GL_CHECK(glStencilMaskSeparate(GL_BACK, + stencilWriteMask.writeMask)); + cache->dss.stencilWriteMaskBack = stencilWriteMask.writeMask; + } + break; + case StencilFace::ALL: + if ((cache->dss.stencilWriteMaskFront != + stencilWriteMask.writeMask) || + (cache->dss.stencilWriteMaskBack != + stencilWriteMask.writeMask)) { + GL_CHECK(glStencilMask(stencilWriteMask.writeMask)); + cache->dss.stencilWriteMaskFront = stencilWriteMask.writeMask; + cache->dss.stencilWriteMaskBack = stencilWriteMask.writeMask; + } + break; + } + break; + case DynamicStateFlagBit::STENCIL_COMPARE_MASK: + switch (stencilCompareMask.face) { + case StencilFace::FRONT: + if ((cache->dss.stencilRefFront != + (uint) stencilCompareMask.refrence) || + (cache->dss.stencilReadMaskFront != + stencilCompareMask.compareMask)) { + GL_CHECK(glStencilFuncSeparate(GL_FRONT, + GLES2_CMP_FUNCS[(uint) cache->dss.stencilFuncFront], + stencilCompareMask.refrence, + stencilCompareMask.compareMask)); + cache->dss.stencilRefFront = stencilCompareMask.refrence; + cache->dss.stencilReadMaskFront = stencilCompareMask.compareMask; + } + break; + case StencilFace::BACK: + if ((cache->dss.stencilRefBack != + (uint) stencilCompareMask.refrence) || + (cache->dss.stencilReadMaskBack != + stencilCompareMask.compareMask)) { + GL_CHECK(glStencilFuncSeparate(GL_BACK, + GLES2_CMP_FUNCS[(uint) cache->dss.stencilFuncBack], + stencilCompareMask.refrence, + stencilCompareMask.compareMask)); + cache->dss.stencilRefBack = stencilCompareMask.refrence; + cache->dss.stencilReadMaskBack = stencilCompareMask.compareMask; + } + break; + case StencilFace::ALL: + if ((cache->dss.stencilRefFront != + (uint) stencilCompareMask.refrence) || + (cache->dss.stencilReadMaskFront != + stencilCompareMask.compareMask) || + (cache->dss.stencilRefBack != + (uint) stencilCompareMask.refrence) || + (cache->dss.stencilReadMaskBack != + stencilCompareMask.compareMask)) { + GL_CHECK(glStencilFuncSeparate(GL_FRONT, + GLES2_CMP_FUNCS[(uint) cache->dss.stencilFuncFront], + stencilCompareMask.refrence, + stencilCompareMask.compareMask)); + GL_CHECK(glStencilFuncSeparate(GL_BACK, + GLES2_CMP_FUNCS[(uint) cache->dss.stencilFuncBack], + stencilCompareMask.refrence, + stencilCompareMask.compareMask)); + cache->dss.stencilRefFront = stencilCompareMask.refrence; + cache->dss.stencilReadMaskFront = stencilCompareMask.compareMask; + cache->dss.stencilRefBack = stencilCompareMask.refrence; + cache->dss.stencilReadMaskBack = stencilCompareMask.compareMask; + } + break; + } + break; + default: + CC_LOG_ERROR("Invalid dynamic states."); + break; + } // switch + } // for + } // if } void GLES2CmdFuncDraw(GLES2Device *device, DrawInfo &drawInfo) { diff --git a/native/cocos/renderer/gfx-gles2/GLES2Context.cpp b/native/cocos/renderer/gfx-gles2/GLES2Context.cpp index df4c6ffe2b..a78143340b 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Context.cpp +++ b/native/cocos/renderer/gfx-gles2/GLES2Context.cpp @@ -421,49 +421,49 @@ bool GLES2Context::MakeCurrent(bool bound) { #endif _isInitialized = true; - - ////////////////////////////////////////////////////////////////////////// - - GL_CHECK(glPixelStorei(GL_PACK_ALIGNMENT, 1)); - GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); - GL_CHECK(glActiveTexture(GL_TEXTURE0)); - - ////////////////////////////////////////////////////////////////////////// - - GL_CHECK(glEnable(GL_SCISSOR_TEST)); - GL_CHECK(glEnable(GL_CULL_FACE)); - GL_CHECK(glCullFace(GL_BACK)); - - GL_CHECK(glFrontFace(GL_CCW)); - - //GL_CHECK(glDisable(GL_MULTISAMPLE)); - - ////////////////////////////////////////////////////////////////////////// - // DepthStencilState - GL_CHECK(glEnable(GL_DEPTH_TEST)); - GL_CHECK(glDepthMask(GL_TRUE)); - GL_CHECK(glDepthFunc(GL_LESS)); - - GL_CHECK(glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 1, 0xffffffff)); - GL_CHECK(glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_KEEP)); - GL_CHECK(glStencilMaskSeparate(GL_FRONT, 0xffffffff)); - GL_CHECK(glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 1, 0xffffffff)); - GL_CHECK(glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_KEEP)); - GL_CHECK(glStencilMaskSeparate(GL_BACK, 0xffffffff)); - - GL_CHECK(glDisable(GL_STENCIL_TEST)); - - ////////////////////////////////////////////////////////////////////////// - // BlendState - - GL_CHECK(glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE)); - GL_CHECK(glDisable(GL_BLEND)); - GL_CHECK(glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD)); - GL_CHECK(glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO)); - GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)); - GL_CHECK(glBlendColor((GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f)); } + ////////////////////////////////////////////////////////////////////////// + + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glActiveTexture(GL_TEXTURE0); + + ////////////////////////////////////////////////////////////////////////// + + glEnable(GL_SCISSOR_TEST); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + + glFrontFace(GL_CCW); + + //glDisable(GL_MULTISAMPLE); + + ////////////////////////////////////////////////////////////////////////// + // DepthStencilState + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); + glDepthFunc(GL_LESS); + + glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 1, 0xffffffff); + glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_KEEP); + glStencilMaskSeparate(GL_FRONT, 0xffffffff); + glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 1, 0xffffffff); + glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_KEEP); + glStencilMaskSeparate(GL_BACK, 0xffffffff); + + glDisable(GL_STENCIL_TEST); + + ////////////////////////////////////////////////////////////////////////// + // BlendState + + glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); + glDisable(GL_BLEND); + glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD); + glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO); + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + glBlendColor((GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f); + CC_LOG_DEBUG("eglMakeCurrent() - SUCCEEDED, Context: 0x%p", this); return true; } else { diff --git a/native/cocos/renderer/gfx-gles2/GLES2GPUObjects.h b/native/cocos/renderer/gfx-gles2/GLES2GPUObjects.h index 0c5407f33d..4862a1a431 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2GPUObjects.h +++ b/native/cocos/renderer/gfx-gles2/GLES2GPUObjects.h @@ -325,8 +325,6 @@ public: GLES2ObjectCache gfxStateCache; void initialize(size_t texUnits, size_t vertexAttributes) { - bt.resize(1); - bs.targets.push_back(bt[0]); glTextures.resize(texUnits, 0u); glEnabledAttribLocs.resize(vertexAttributes, false); glCurrentAttribLocs.resize(vertexAttributes, false); @@ -347,6 +345,12 @@ public: isCullFaceEnabled = true; isStencilTestEnabled = false; + viewport = Viewport(); + scissor = Rect(); + rs = RasterizerState(); + dss = DepthStencilState(); + bs = BlendState(); + gfxStateCache.gpuPipelineState = nullptr; gfxStateCache.numClearColors = 0u; gfxStateCache.gpuRenderPass = nullptr; diff --git a/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp b/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp index 247eb05ce8..8bab0eeafd 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp +++ b/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp @@ -1459,7 +1459,7 @@ void GLES3CmdFuncBeginRenderPass(GLES3Device *device, GLES3GPURenderPass *gpuRen GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, gpuFramebuffer->glFramebuffer)); cache->glFramebuffer = gpuFramebuffer->glFramebuffer; // render targets are drawn with flipped-Y - gfxStateCache.reverseCW = !!gpuFramebuffer->glFramebuffer; + gfxStateCache.reverseCW = gpuFramebuffer->isOffscreen; } if (cache->viewport.left != renderArea.x || @@ -1517,15 +1517,18 @@ void GLES3CmdFuncBeginRenderPass(GLES3Device *device, GLES3GPURenderPass *gpuRen break; } - // restore states - if (glClears & GL_COLOR_BUFFER_BIT) { - ColorMask colorMask = cache->bs.targets[0].blendColorMask; - if (colorMask != ColorMask::ALL) { - glColorMask((GLboolean)(colorMask & ColorMask::R), - (GLboolean)(colorMask & ColorMask::G), - (GLboolean)(colorMask & ColorMask::B), - (GLboolean)(colorMask & ColorMask::A)); + if (gpuRenderPass->depthStencilAttachment.format != Format::UNKNOWN) { + bool hasDepth = GFX_FORMAT_INFOS[(int)gpuRenderPass->depthStencilAttachment.format].hasDepth; + if (hasDepth) { + switch (gpuRenderPass->depthStencilAttachment.depthLoadOp) { + case LoadOp::LOAD: break; // GL default behaviour + case LoadOp::CLEAR: { + if (!cache->dss.depthWrite) { + GL_CHECK(glDepthMask(true)); } + GL_CHECK(glClearDepthf(clearDepth)); + glClears |= GL_DEPTH_BUFFER_BIT; + break; } case LoadOp::DISCARD: { // invalidate fbo @@ -1579,7 +1582,7 @@ void GLES3CmdFuncBeginRenderPass(GLES3Device *device, GLES3GPURenderPass *gpuRen } } - if ((glClears & GL_COLOR_BUFFER_BIT) && !cache->dss.depthWrite) { + if ((glClears & GL_DEPTH_BUFFER_BIT) && !cache->dss.depthWrite) { GL_CHECK(glDepthMask(false)); } @@ -1936,7 +1939,7 @@ void GLES3CmdFuncBindState(GLES3Device *device, GLES3GPUPipelineState *gpuPipeli } // if // bind vao - if (gpuInputAssembler && gpuPipelineState->gpuShader && + if (gpuInputAssembler && gpuPipelineState && gpuPipelineState->gpuShader && (isShaderChanged || gpuInputAssembler != gfxStateCache.gpuInputAssembler)) { gfxStateCache.gpuInputAssembler = gpuInputAssembler; if (USE_VAO) { diff --git a/native/cocos/renderer/gfx-gles3/GLES3Context.cpp b/native/cocos/renderer/gfx-gles3/GLES3Context.cpp index 567a9e8ae3..570817a23a 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Context.cpp +++ b/native/cocos/renderer/gfx-gles3/GLES3Context.cpp @@ -420,51 +420,49 @@ bool GLES3Context::MakeCurrent(bool bound) { #endif _isInitialized = true; - - ////////////////////////////////////////////////////////////////////////// - - GL_CHECK(glPixelStorei(GL_PACK_ALIGNMENT, 1)); - GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); - GL_CHECK(glActiveTexture(GL_TEXTURE0)); - - ////////////////////////////////////////////////////////////////////////// - - GL_CHECK(glEnable(GL_SCISSOR_TEST)); - GL_CHECK(glEnable(GL_CULL_FACE)); - GL_CHECK(glCullFace(GL_BACK)); - - GL_CHECK(glFrontFace(GL_CCW)); - - //GL_CHECK(glDisable(GL_MULTISAMPLE)); - - ////////////////////////////////////////////////////////////////////////// - // DepthStencilState - GL_CHECK(glEnable(GL_DEPTH_TEST)); - GL_CHECK(glDepthMask(GL_TRUE)); - GL_CHECK(glDepthFunc(GL_LESS)); - - GL_CHECK(glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 1, 0xffffffff)); - GL_CHECK(glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_KEEP)); - GL_CHECK(glStencilMaskSeparate(GL_FRONT, 0xffffffff)); - GL_CHECK(glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 1, 0xffffffff)); - GL_CHECK(glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_KEEP)); - GL_CHECK(glStencilMaskSeparate(GL_BACK, 0xffffffff)); - - GL_CHECK(glDisable(GL_STENCIL_TEST)); - - ////////////////////////////////////////////////////////////////////////// - // BlendState - - GL_CHECK(glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE)); - GL_CHECK(glDisable(GL_BLEND)); - GL_CHECK(glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD)); - GL_CHECK(glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO)); - GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)); - GL_CHECK(glBlendColor((GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f)); - - _isInitialized = true; } + ////////////////////////////////////////////////////////////////////////// + + GL_CHECK(glPixelStorei(GL_PACK_ALIGNMENT, 1)); + GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); + GL_CHECK(glActiveTexture(GL_TEXTURE0)); + + ////////////////////////////////////////////////////////////////////////// + + GL_CHECK(glEnable(GL_SCISSOR_TEST)); + GL_CHECK(glEnable(GL_CULL_FACE)); + GL_CHECK(glCullFace(GL_BACK)); + + GL_CHECK(glFrontFace(GL_CCW)); + + //GL_CHECK(glDisable(GL_MULTISAMPLE)); + + ////////////////////////////////////////////////////////////////////////// + // DepthStencilState + GL_CHECK(glEnable(GL_DEPTH_TEST)); + GL_CHECK(glDepthMask(GL_TRUE)); + GL_CHECK(glDepthFunc(GL_LESS)); + + GL_CHECK(glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 1, 0xffffffff)); + GL_CHECK(glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_KEEP)); + GL_CHECK(glStencilMaskSeparate(GL_FRONT, 0xffffffff)); + GL_CHECK(glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 1, 0xffffffff)); + GL_CHECK(glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_KEEP)); + GL_CHECK(glStencilMaskSeparate(GL_BACK, 0xffffffff)); + + GL_CHECK(glDisable(GL_STENCIL_TEST)); + + ////////////////////////////////////////////////////////////////////////// + // BlendState + + GL_CHECK(glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE)); + GL_CHECK(glDisable(GL_BLEND)); + GL_CHECK(glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD)); + GL_CHECK(glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO)); + GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)); + GL_CHECK(glBlendColor((GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f)); + CC_LOG_DEBUG("eglMakeCurrent() - SUCCEEDED, Context: 0x%p", this); return true; } else { diff --git a/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h b/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h index 1e43b74c71..519ac0dde9 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h +++ b/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h @@ -294,8 +294,6 @@ public: GLES3ObjectCache gfxStateCache; void initialize(size_t texUnits, size_t bufferBindings, size_t vertexAttributes) { - bt.resize(1); - bs.targets.push_back(bt[0]); glBindUBOs.resize(bufferBindings, 0u); glBindUBOOffsets.resize(bufferBindings, 0u); glTextures.resize(texUnits, 0u); @@ -321,6 +319,12 @@ public: glReadFBO = 0; isCullFaceEnabled = true; isStencilTestEnabled = false; + + viewport = Viewport(); + scissor = Rect(); + rs = RasterizerState(); + dss = DepthStencilState(); + bs = BlendState(); gfxStateCache.numClearColors = 0u; gfxStateCache.gpuRenderPass = nullptr;