From 001c2ea98fa8bba333d8028131f9fd97e19c9c2b Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Mon, 5 Jun 2017 13:12:16 -0400 Subject: [PATCH] Fix some nits from depth-stencil PR --- examples/HelloDepthStencil.cpp | 10 ++--- examples/Utils.cpp | 6 +-- examples/Utils.h | 3 +- src/backend/common/DepthStencilState.cpp | 6 +-- src/backend/opengl/CommandBufferGL.cpp | 1 - src/backend/opengl/DepthStencilStateGL.cpp | 18 ++------- .../opengl/PersistentPipelineStateGL.cpp | 40 ++++++++++--------- .../opengl/PersistentPipelineStateGL.h | 13 +++--- 8 files changed, 41 insertions(+), 56 deletions(-) diff --git a/examples/HelloDepthStencil.cpp b/examples/HelloDepthStencil.cpp index 05afce5ada..6e70a1513e 100644 --- a/examples/HelloDepthStencil.cpp +++ b/examples/HelloDepthStencil.cpp @@ -58,7 +58,7 @@ void initBuffers() { 20, 21, 22, 20, 22, 23 }; - indexBuffer = CreateFrozenBufferFromData(device, (void*)indexData, sizeof(indexData), nxt::BufferUsageBit::Index); + indexBuffer = CreateFrozenBufferFromData(device, indexData, sizeof(indexData), nxt::BufferUsageBit::Index); static const float vertexData[6 * 4 * 6] = { -1.0, -1.0, 1.0, 1.0, 0.0, 0.0, @@ -91,7 +91,7 @@ void initBuffers() { -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0 }; - vertexBuffer = CreateFrozenBufferFromData(device, (void*)vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex); + vertexBuffer = CreateFrozenBufferFromData(device, vertexData, sizeof(vertexData), nxt::BufferUsageBit::Vertex); static const float planeData[6 * 4] = { -2.0, -1.0, -2.0, 0.5, 0.5, 0.5, @@ -99,7 +99,7 @@ void initBuffers() { 2.0, -1.0, 2.0, 0.5, 0.5, 0.5, -2.0, -1.0, 2.0, 0.5, 0.5, 0.5, }; - planeBuffer = CreateFrozenBufferFromData(device, (void*)planeData, sizeof(planeData), nxt::BufferUsageBit::Vertex); + planeBuffer = CreateFrozenBufferFromData(device, planeData, sizeof(planeData), nxt::BufferUsageBit::Vertex); } struct CameraData { @@ -171,10 +171,10 @@ void init() { .GetResult(); glm::mat4 transform(1.0); - transformBuffer[0] = CreateFrozenBufferFromData(device, (void*)&transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform); + transformBuffer[0] = CreateFrozenBufferFromData(device, &transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform); transform = glm::translate(transform, glm::vec3(0.f, -2.f, 0.f)); - transformBuffer[1] = CreateFrozenBufferFromData(device, (void*)&transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform); + transformBuffer[1] = CreateFrozenBufferFromData(device, &transform, sizeof(glm::mat4), nxt::BufferUsageBit::Uniform); nxt::BufferView cameraBufferView = cameraBuffer.CreateBufferViewBuilder() .SetExtent(0, sizeof(CameraData)) diff --git a/examples/Utils.cpp b/examples/Utils.cpp index 289bfc8d4c..9edab258a5 100644 --- a/examples/Utils.cpp +++ b/examples/Utils.cpp @@ -245,7 +245,7 @@ void CreateDefaultRenderPass(const nxt::Device& device, nxt::RenderPass* renderP .GetResult(); } -nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, void* data, uint32_t size, nxt::BufferUsageBit usage) { +nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* data, uint32_t size, nxt::BufferUsageBit usage) { nxt::Buffer buffer = device.CreateBufferBuilder() .SetAllowedUsage(nxt::BufferUsageBit::Mapped | usage) .SetInitialUsage(nxt::BufferUsageBit::Mapped) @@ -307,10 +307,6 @@ extern "C" { return CreateShaderModule(device, static_cast(stage), source).Release(); } - nxtBuffer CreateFrozenBufferFromData(nxtDevice device, void* data, uint32_t size, nxtBufferUsageBit usage) { - return CreateFrozenBufferFromData(device, data, size, usage); - } - void DoSwapBuffers() { if (cmdBufType == CmdBufType::Terrible) { c2sBuf->Flush(); diff --git a/examples/Utils.h b/examples/Utils.h index df4c183c45..e16883718e 100644 --- a/examples/Utils.h +++ b/examples/Utils.h @@ -34,9 +34,8 @@ extern "C" { nxt::Device CreateCppNXTDevice(); nxt::ShaderModule CreateShaderModule(const nxt::Device& device, nxt::ShaderStage stage, const char* source); void CreateDefaultRenderPass(const nxt::Device& device, nxt::RenderPass* renderPass, nxt::Framebuffer* framebuffer); - nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, void* data, uint32_t size, nxt::BufferUsageBit usage); + nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* data, uint32_t size, nxt::BufferUsageBit usage); #else nxtDevice CreateNXTDevice(); nxtShaderModule CreateShaderModule(nxtDevice device, nxtShaderStage stage, const char* source); - nxtBuffer CreateFrozenBufferFromData(nxtDevice device, void* data, uint32_t size, nxtBufferUsageBit usage); #endif diff --git a/src/backend/common/DepthStencilState.cpp b/src/backend/common/DepthStencilState.cpp index aad61e0739..86e5b2e016 100644 --- a/src/backend/common/DepthStencilState.cpp +++ b/src/backend/common/DepthStencilState.cpp @@ -29,16 +29,14 @@ namespace backend { } bool DepthStencilStateBase::StencilTestEnabled() const { - return ( - stencilInfo.back.compareFunction != nxt::CompareFunction::Always || + return stencilInfo.back.compareFunction != nxt::CompareFunction::Always || stencilInfo.back.stencilFail != nxt::StencilOperation::Keep || stencilInfo.back.depthFail != nxt::StencilOperation::Keep || stencilInfo.back.depthStencilPass != nxt::StencilOperation::Keep || stencilInfo.front.compareFunction != nxt::CompareFunction::Always || stencilInfo.front.stencilFail != nxt::StencilOperation::Keep || stencilInfo.front.depthFail != nxt::StencilOperation::Keep || - stencilInfo.front.depthStencilPass != nxt::StencilOperation::Keep - ); + stencilInfo.front.depthStencilPass != nxt::StencilOperation::Keep; } const DepthStencilStateBase::DepthInfo& DepthStencilStateBase::GetDepth() const { diff --git a/src/backend/opengl/CommandBufferGL.cpp b/src/backend/opengl/CommandBufferGL.cpp index 501db18eb6..0c808ae2b5 100644 --- a/src/backend/opengl/CommandBufferGL.cpp +++ b/src/backend/opengl/CommandBufferGL.cpp @@ -185,7 +185,6 @@ namespace opengl { case Command::SetStencilReference: { SetStencilReferenceCmd* cmd = commands.NextCommand(); - // DepthStencilState* depthStencilState = ToBackend(lastPipeline->GetDepthStencilState()); persistentPipelineState.SetStencilReference(cmd->reference); } break; diff --git a/src/backend/opengl/DepthStencilStateGL.cpp b/src/backend/opengl/DepthStencilStateGL.cpp index 97c95b0abd..a814c561e7 100644 --- a/src/backend/opengl/DepthStencilStateGL.cpp +++ b/src/backend/opengl/DepthStencilStateGL.cpp @@ -95,32 +95,20 @@ namespace opengl { GLenum backCompareFunction = OpenGLCompareFunction(stencilInfo.back.compareFunction); GLenum frontCompareFunction = OpenGLCompareFunction(stencilInfo.front.compareFunction); - - persistentPipelineState.CacheStencilFuncsAndMask(backCompareFunction, frontCompareFunction, stencilInfo.readMask); + persistentPipelineState.SetStencilFuncsAndMask(backCompareFunction, frontCompareFunction, stencilInfo.readMask); glStencilOpSeparate(GL_BACK, OpenGLStencilOperation(stencilInfo.back.stencilFail), OpenGLStencilOperation(stencilInfo.back.depthFail), OpenGLStencilOperation(stencilInfo.back.depthStencilPass) ); - glStencilFuncSeparate(GL_BACK, - backCompareFunction, - persistentPipelineState.GetCachedStencilReference(), - stencilInfo.readMask - ); - glStencilMaskSeparate(GL_BACK, stencilInfo.writeMask); - glStencilOpSeparate(GL_FRONT, OpenGLStencilOperation(stencilInfo.front.stencilFail), OpenGLStencilOperation(stencilInfo.front.depthFail), OpenGLStencilOperation(stencilInfo.front.depthStencilPass) ); - glStencilFuncSeparate(GL_FRONT, - frontCompareFunction, - persistentPipelineState.GetCachedStencilReference(), - stencilInfo.readMask - ); - glStencilMaskSeparate(GL_FRONT, stencilInfo.writeMask); + + glStencilMask(stencilInfo.writeMask); } diff --git a/src/backend/opengl/PersistentPipelineStateGL.cpp b/src/backend/opengl/PersistentPipelineStateGL.cpp index a6ee85e34c..480c521dae 100644 --- a/src/backend/opengl/PersistentPipelineStateGL.cpp +++ b/src/backend/opengl/PersistentPipelineStateGL.cpp @@ -20,36 +20,40 @@ namespace backend { namespace opengl { void PersistentPipelineState::SetDefaultState() { - stencilBackCompareFunction = GL_ALWAYS; - stencilFrontCompareFunction = GL_ALWAYS; - stencilReadMask = 0xff; - SetStencilReference(0); + CallGLStencilFunc(); } - void PersistentPipelineState::CacheStencilFuncsAndMask(GLenum stencilBackCompareFunction, GLenum stencilFrontCompareFunction, uint32_t stencilReadMask) { + void PersistentPipelineState::SetStencilFuncsAndMask(GLenum stencilBackCompareFunction, GLenum stencilFrontCompareFunction, uint32_t stencilReadMask) { + if (this->stencilBackCompareFunction == stencilBackCompareFunction && + this->stencilFrontCompareFunction == stencilFrontCompareFunction && + this->stencilReadMask == stencilReadMask) { + return; + } + this->stencilBackCompareFunction = stencilBackCompareFunction; this->stencilFrontCompareFunction = stencilFrontCompareFunction; this->stencilReadMask = stencilReadMask; + CallGLStencilFunc(); } void PersistentPipelineState::SetStencilReference(uint32_t stencilReference) { - if (this->stencilReference != stencilReference) { - this->stencilReference = stencilReference; - glStencilFuncSeparate(GL_BACK, + if (this->stencilReference == stencilReference) { + return; + } + + this->stencilReference = stencilReference; + CallGLStencilFunc(); + } + + void PersistentPipelineState::CallGLStencilFunc() { + glStencilFuncSeparate(GL_BACK, stencilBackCompareFunction, stencilReference, - stencilReadMask - ); - glStencilFuncSeparate(GL_FRONT, + stencilReadMask); + glStencilFuncSeparate(GL_FRONT, stencilFrontCompareFunction, stencilReference, - stencilReadMask - ); - } - } - - GLuint PersistentPipelineState::GetCachedStencilReference() const { - return stencilReference; + stencilReadMask); } } diff --git a/src/backend/opengl/PersistentPipelineStateGL.h b/src/backend/opengl/PersistentPipelineStateGL.h index 1381fdcfa3..c0b0a3077c 100644 --- a/src/backend/opengl/PersistentPipelineStateGL.h +++ b/src/backend/opengl/PersistentPipelineStateGL.h @@ -25,15 +25,16 @@ namespace opengl { class PersistentPipelineState { public: void SetDefaultState(); - void CacheStencilFuncsAndMask(GLenum stencilBackCompareFunction, GLenum stencilFrontCompareFunction, uint32_t stencilReadMask); + void SetStencilFuncsAndMask(GLenum stencilBackCompareFunction, GLenum stencilFrontCompareFunction, uint32_t stencilReadMask); void SetStencilReference(uint32_t stencilReference); - GLuint GetCachedStencilReference() const; private: - GLenum stencilBackCompareFunction; - GLenum stencilFrontCompareFunction; - GLuint stencilReadMask; - GLuint stencilReference; + void CallGLStencilFunc(); + + GLenum stencilBackCompareFunction = GL_ALWAYS; + GLenum stencilFrontCompareFunction = GL_ALWAYS; + GLuint stencilReadMask = 0xffffffff; + GLuint stencilReference = 0; }; }