From ea56333c1e64b0106d11c4c729cf46abbe88edfd Mon Sep 17 00:00:00 2001 From: Yunchao He Date: Fri, 4 Jan 2019 04:28:37 +0000 Subject: [PATCH] Replace DepthStencilState builder via DepthStencilState descriptor. This change also removes DepthStencilState object. Bug=dawn:31 Change-Id: I7bb54ef4da527184bb2726c77d93d411d44c3956 Reviewed-on: https://dawn-review.googlesource.com/c/3541 Reviewed-by: Kai Ninomiya Commit-Queue: Yunchao He --- BUILD.gn | 11 - dawn.json | 52 +--- examples/CHelloTriangle.cpp | 19 +- examples/CubeReflection.cpp | 42 +-- examples/glTFViewer/glTFViewer.cpp | 8 +- src/dawn_native/DepthStencilState.cpp | 135 --------- src/dawn_native/DepthStencilState.h | 78 ----- src/dawn_native/Device.cpp | 4 - src/dawn_native/Device.h | 3 - src/dawn_native/Forward.h | 2 - src/dawn_native/Pipeline.cpp | 1 - src/dawn_native/RenderPipeline.cpp | 40 ++- src/dawn_native/RenderPipeline.h | 6 +- src/dawn_native/ToBackend.h | 5 - .../d3d12/DepthStencilStateD3D12.cpp | 99 ------- .../d3d12/DepthStencilStateD3D12.h | 38 --- src/dawn_native/d3d12/DeviceD3D12.cpp | 4 - src/dawn_native/d3d12/DeviceD3D12.h | 1 - src/dawn_native/d3d12/Forward.h | 2 - src/dawn_native/d3d12/RenderPipelineD3D12.cpp | 85 +++++- src/dawn_native/metal/CommandBufferMTL.mm | 5 +- src/dawn_native/metal/DepthStencilStateMTL.h | 39 --- src/dawn_native/metal/DepthStencilStateMTL.mm | 120 -------- src/dawn_native/metal/DeviceMTL.h | 1 - src/dawn_native/metal/DeviceMTL.mm | 4 - src/dawn_native/metal/Forward.h | 2 - src/dawn_native/metal/RenderPipelineMTL.h | 3 + src/dawn_native/metal/RenderPipelineMTL.mm | 112 ++++++- src/dawn_native/null/NullBackend.cpp | 3 - src/dawn_native/null/NullBackend.h | 4 - .../opengl/DepthStencilStateGL.cpp | 116 -------- src/dawn_native/opengl/DepthStencilStateGL.h | 34 --- src/dawn_native/opengl/DeviceGL.cpp | 4 - src/dawn_native/opengl/DeviceGL.h | 1 - src/dawn_native/opengl/Forward.h | 2 - src/dawn_native/opengl/RenderPipelineGL.cpp | 91 +++++- .../vulkan/DepthStencilStateVk.cpp | 116 -------- src/dawn_native/vulkan/DepthStencilStateVk.h | 39 --- src/dawn_native/vulkan/DeviceVk.cpp | 4 - src/dawn_native/vulkan/DeviceVk.h | 1 - src/dawn_native/vulkan/Forward.h | 2 - src/dawn_native/vulkan/RenderPipelineVk.cpp | 97 ++++++- src/tests/end2end/DepthStencilStateTests.cpp | 273 ++++++++++++------ src/tests/unittests/WireTests.cpp | 23 +- .../DepthStencilStateValidationTests.cpp | 127 -------- src/utils/ComboRenderPipelineDescriptor.cpp | 54 ++-- src/utils/ComboRenderPipelineDescriptor.h | 1 + 47 files changed, 678 insertions(+), 1235 deletions(-) delete mode 100644 src/dawn_native/DepthStencilState.cpp delete mode 100644 src/dawn_native/DepthStencilState.h delete mode 100644 src/dawn_native/d3d12/DepthStencilStateD3D12.cpp delete mode 100644 src/dawn_native/d3d12/DepthStencilStateD3D12.h delete mode 100644 src/dawn_native/metal/DepthStencilStateMTL.h delete mode 100644 src/dawn_native/metal/DepthStencilStateMTL.mm delete mode 100644 src/dawn_native/opengl/DepthStencilStateGL.cpp delete mode 100644 src/dawn_native/opengl/DepthStencilStateGL.h delete mode 100644 src/dawn_native/vulkan/DepthStencilStateVk.cpp delete mode 100644 src/dawn_native/vulkan/DepthStencilStateVk.h delete mode 100644 src/tests/unittests/validation/DepthStencilStateValidationTests.cpp diff --git a/BUILD.gn b/BUILD.gn index 47349421ed..cd0ba6cb04 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -380,8 +380,6 @@ source_set("libdawn_native_sources") { "src/dawn_native/ComputePipeline.cpp", "src/dawn_native/ComputePipeline.h", "src/dawn_native/DawnNative.cpp", - "src/dawn_native/DepthStencilState.cpp", - "src/dawn_native/DepthStencilState.h", "src/dawn_native/Device.cpp", "src/dawn_native/Device.h", "src/dawn_native/Error.cpp", @@ -443,8 +441,6 @@ source_set("libdawn_native_sources") { "src/dawn_native/d3d12/CommandBufferD3D12.h", "src/dawn_native/d3d12/ComputePipelineD3D12.cpp", "src/dawn_native/d3d12/ComputePipelineD3D12.h", - "src/dawn_native/d3d12/DepthStencilStateD3D12.cpp", - "src/dawn_native/d3d12/DepthStencilStateD3D12.h", "src/dawn_native/d3d12/DescriptorHeapAllocator.cpp", "src/dawn_native/d3d12/DescriptorHeapAllocator.h", "src/dawn_native/d3d12/DeviceD3D12.cpp", @@ -495,8 +491,6 @@ source_set("libdawn_native_sources") { "src/dawn_native/metal/CommandBufferMTL.mm", "src/dawn_native/metal/ComputePipelineMTL.h", "src/dawn_native/metal/ComputePipelineMTL.mm", - "src/dawn_native/metal/DepthStencilStateMTL.h", - "src/dawn_native/metal/DepthStencilStateMTL.mm", "src/dawn_native/metal/DeviceMTL.h", "src/dawn_native/metal/DeviceMTL.mm", "src/dawn_native/metal/Forward.h", @@ -537,8 +531,6 @@ source_set("libdawn_native_sources") { "src/dawn_native/opengl/CommandBufferGL.h", "src/dawn_native/opengl/ComputePipelineGL.cpp", "src/dawn_native/opengl/ComputePipelineGL.h", - "src/dawn_native/opengl/DepthStencilStateGL.cpp", - "src/dawn_native/opengl/DepthStencilStateGL.h", "src/dawn_native/opengl/DeviceGL.cpp", "src/dawn_native/opengl/DeviceGL.h", "src/dawn_native/opengl/Forward.h", @@ -580,8 +572,6 @@ source_set("libdawn_native_sources") { "src/dawn_native/vulkan/CommandBufferVk.h", "src/dawn_native/vulkan/ComputePipelineVk.cpp", "src/dawn_native/vulkan/ComputePipelineVk.h", - "src/dawn_native/vulkan/DepthStencilStateVk.cpp", - "src/dawn_native/vulkan/DepthStencilStateVk.h", "src/dawn_native/vulkan/DeviceVk.cpp", "src/dawn_native/vulkan/DeviceVk.h", "src/dawn_native/vulkan/FencedDeleter.cpp", @@ -821,7 +811,6 @@ test("dawn_unittests") { "src/tests/unittests/validation/CommandBufferValidationTests.cpp", "src/tests/unittests/validation/ComputeValidationTests.cpp", "src/tests/unittests/validation/CopyCommandsValidationTests.cpp", - "src/tests/unittests/validation/DepthStencilStateValidationTests.cpp", "src/tests/unittests/validation/DynamicStateCommandValidationTests.cpp", "src/tests/unittests/validation/FenceValidationTests.cpp", "src/tests/unittests/validation/InputStateValidationTests.cpp", diff --git a/dawn.json b/dawn.json index 0fcc21d1ee..1d09f1aef5 100644 --- a/dawn.json +++ b/dawn.json @@ -421,10 +421,6 @@ "name": "create command buffer builder", "returns": "command buffer builder" }, - { - "name": "create depth stencil state builder", - "returns": "depth stencil state builder" - }, { "name": "create fence", "returns": "fence", @@ -502,42 +498,16 @@ } ] }, - "depth stencil state": { - "category": "object" - }, - "depth stencil state builder": { - "category": "object", - "methods": [ - { - "name": "get result", - "returns": "depth stencil state" - }, - { - "name": "set depth compare function", - "args": [ - {"name": "depth compare function", "type": "compare function"} - ] - }, - { - "name": "set depth write enabled", - "args" : [ - {"name": "enabled", "type": "bool"} - ] - }, - { - "name": "set stencil function", - "args": [ - {"name": "face", "type": "face"}, - {"name": "descriptor", "type": "stencil state face descriptor", "annotation": "const*"} - ] - }, - { - "name": "set stencil mask", - "args": [ - {"name": "read mask", "type": "uint32_t"}, - {"name": "write mask", "type": "uint32_t"} - ] - } + "depth stencil state descriptor": { + "category": "structure", + "extensible": true, + "members": [ + {"name": "depth write enabled", "type": "bool"}, + {"name": "depth compare", "type": "compare function"}, + {"name": "front", "type": "stencil state face descriptor"}, + {"name": "back", "type": "stencil state face descriptor"}, + {"name": "stencil read mask", "type": "uint32_t"}, + {"name": "stencil write mask", "type": "uint32_t"} ] }, "device error callback": { @@ -888,7 +858,7 @@ {"name": "primitive topology", "type": "primitive topology"}, {"name": "attachments state", "type": "attachments state descriptor", "annotation": "const*"}, {"name": "sample count", "type": "uint32_t"}, - {"name": "depth stencil state", "type": "depth stencil state"}, + {"name": "depth stencil state", "type": "depth stencil state descriptor", "annotation": "const*"}, {"name": "num blend states", "type": "uint32_t"}, {"name": "blend states", "type": "blend state descriptor", "annotation": "const*", "length": "num blend states"} ] diff --git a/examples/CHelloTriangle.cpp b/examples/CHelloTriangle.cpp index 741e52853d..287a8ac58c 100644 --- a/examples/CHelloTriangle.cpp +++ b/examples/CHelloTriangle.cpp @@ -111,13 +111,24 @@ void init() { descriptor.indexFormat = DAWN_INDEX_FORMAT_UINT32; descriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; - dawnDepthStencilStateBuilder depthStencilBuilder = dawnDeviceCreateDepthStencilStateBuilder(device); - descriptor.depthStencilState = dawnDepthStencilStateBuilderGetResult(depthStencilBuilder); - dawnDepthStencilStateBuilderRelease(depthStencilBuilder); + dawnStencilStateFaceDescriptor stencilFace; + stencilFace.compare = DAWN_COMPARE_FUNCTION_ALWAYS; + stencilFace.stencilFailOp = DAWN_STENCIL_OPERATION_KEEP; + stencilFace.depthFailOp = DAWN_STENCIL_OPERATION_KEEP; + stencilFace.passOp = DAWN_STENCIL_OPERATION_KEEP; + + dawnDepthStencilStateDescriptor depthStencilState; + depthStencilState.nextInChain = nullptr; + depthStencilState.depthWriteEnabled = false; + depthStencilState.depthCompare = DAWN_COMPARE_FUNCTION_ALWAYS; + depthStencilState.back = stencilFace; + depthStencilState.front = stencilFace; + depthStencilState.stencilReadMask = 0xff; + depthStencilState.stencilWriteMask = 0xff; + descriptor.depthStencilState = &depthStencilState; pipeline = dawnDeviceCreateRenderPipeline(device, &descriptor); - dawnDepthStencilStateRelease(descriptor.depthStencilState); dawnInputStateRelease(descriptor.inputState); } diff --git a/examples/CubeReflection.cpp b/examples/CubeReflection.cpp index 66ba8b07ed..1e70901eab 100644 --- a/examples/CubeReflection.cpp +++ b/examples/CubeReflection.cpp @@ -193,11 +193,6 @@ void init() { depthStencilView = CreateDefaultDepthStencilView(device); - auto depthStencilState = device.CreateDepthStencilStateBuilder() - .SetDepthCompareFunction(dawn::CompareFunction::Less) - .SetDepthWriteEnabled(true) - .GetResult(); - utils::ComboRenderPipelineDescriptor descriptor(device); descriptor.layout = pl; descriptor.cVertexStage.module = vsModule; @@ -207,21 +202,11 @@ void init() { descriptor.cDepthStencilAttachment.format = dawn::TextureFormat::D32FloatS8Uint; descriptor.cColorAttachments[0].format = GetPreferredSwapChainTextureFormat(); - descriptor.depthStencilState = depthStencilState; + descriptor.cDepthStencilState.depthWriteEnabled = true; + descriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less; pipeline = device.CreateRenderPipeline(&descriptor); - dawn::StencilStateFaceDescriptor planeStencilDescriptor; - planeStencilDescriptor.compare = dawn::CompareFunction::Always; - planeStencilDescriptor.stencilFailOp = dawn::StencilOperation::Keep; - planeStencilDescriptor.depthFailOp = dawn::StencilOperation::Keep; - planeStencilDescriptor.passOp = dawn::StencilOperation::Replace; - auto planeStencilState = device.CreateDepthStencilStateBuilder() - .SetDepthCompareFunction(dawn::CompareFunction::Less) - .SetDepthWriteEnabled(false) - .SetStencilFunction(dawn::Face::Both, &planeStencilDescriptor) - .GetResult(); - utils::ComboRenderPipelineDescriptor pDescriptor(device); pDescriptor.layout = pl; pDescriptor.cVertexStage.module = vsModule; @@ -231,22 +216,12 @@ void init() { pDescriptor.cDepthStencilAttachment.format = dawn::TextureFormat::D32FloatS8Uint; pDescriptor.cColorAttachments[0].format = GetPreferredSwapChainTextureFormat(); - pDescriptor.depthStencilState = planeStencilState; + pDescriptor.cDepthStencilState.front.passOp = dawn::StencilOperation::Replace; + pDescriptor.cDepthStencilState.back.passOp = dawn::StencilOperation::Replace; + pDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less; planePipeline = device.CreateRenderPipeline(&pDescriptor); - dawn::StencilStateFaceDescriptor reflectionStencilDescriptor; - reflectionStencilDescriptor.compare = dawn::CompareFunction::Equal; - reflectionStencilDescriptor.stencilFailOp = dawn::StencilOperation::Keep; - reflectionStencilDescriptor.depthFailOp = dawn::StencilOperation::Keep; - reflectionStencilDescriptor.passOp = dawn::StencilOperation::Replace; - auto reflectionStencilState = - device.CreateDepthStencilStateBuilder() - .SetDepthCompareFunction(dawn::CompareFunction::Less) - .SetDepthWriteEnabled(true) - .SetStencilFunction(dawn::Face::Both, &reflectionStencilDescriptor) - .GetResult(); - utils::ComboRenderPipelineDescriptor rfDescriptor(device); rfDescriptor.layout = pl; rfDescriptor.cVertexStage.module = vsModule; @@ -256,7 +231,12 @@ void init() { rfDescriptor.cDepthStencilAttachment.format = dawn::TextureFormat::D32FloatS8Uint; rfDescriptor.cColorAttachments[0].format = GetPreferredSwapChainTextureFormat(); - rfDescriptor.depthStencilState = reflectionStencilState; + pDescriptor.cDepthStencilState.front.compare = dawn::CompareFunction::Equal; + pDescriptor.cDepthStencilState.back.compare = dawn::CompareFunction::Equal; + pDescriptor.cDepthStencilState.front.passOp = dawn::StencilOperation::Replace; + pDescriptor.cDepthStencilState.back.passOp = dawn::StencilOperation::Replace; + rfDescriptor.cDepthStencilState.depthWriteEnabled = true; + rfDescriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less; reflectionPipeline = device.CreateRenderPipeline(&rfDescriptor); diff --git a/examples/glTFViewer/glTFViewer.cpp b/examples/glTFViewer/glTFViewer.cpp index 01f61a8dbf..4e2b3fe330 100644 --- a/examples/glTFViewer/glTFViewer.cpp +++ b/examples/glTFViewer/glTFViewer.cpp @@ -282,11 +282,6 @@ namespace { dawn::BindingType::SampledTexture}, }); - auto depthStencilState = device.CreateDepthStencilStateBuilder() - .SetDepthCompareFunction(dawn::CompareFunction::Less) - .SetDepthWriteEnabled(true) - .GetResult(); - auto pipelineLayout = utils::MakeBasicPipelineLayout(device, &bindGroupLayout); utils::ComboRenderPipelineDescriptor descriptor(device); @@ -299,7 +294,8 @@ namespace { descriptor.cDepthStencilAttachment.format = dawn::TextureFormat::D32FloatS8Uint; descriptor.cColorAttachments[0].format = GetPreferredSwapChainTextureFormat(); - descriptor.depthStencilState = depthStencilState; + descriptor.cDepthStencilState.depthWriteEnabled = true; + descriptor.cDepthStencilState.depthCompare = dawn::CompareFunction::Less; dawn::RenderPipeline pipeline = device.CreateRenderPipeline(&descriptor); diff --git a/src/dawn_native/DepthStencilState.cpp b/src/dawn_native/DepthStencilState.cpp deleted file mode 100644 index 2bf6d7e2d8..0000000000 --- a/src/dawn_native/DepthStencilState.cpp +++ /dev/null @@ -1,135 +0,0 @@ -// Copyright 2017 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "dawn_native/DepthStencilState.h" - -#include "dawn_native/Device.h" - -namespace dawn_native { - - // DepthStencilStateBase - - DepthStencilStateBase::DepthStencilStateBase(DepthStencilStateBuilder* builder) - : ObjectBase(builder->GetDevice()), - mDepthInfo(builder->mDepthInfo), - mStencilInfo(builder->mStencilInfo) { - } - - bool DepthStencilStateBase::StencilTestEnabled() const { - return mStencilInfo.back.compare != dawn::CompareFunction::Always || - mStencilInfo.back.stencilFailOp != dawn::StencilOperation::Keep || - mStencilInfo.back.depthFailOp != dawn::StencilOperation::Keep || - mStencilInfo.back.passOp != dawn::StencilOperation::Keep || - mStencilInfo.front.compare != dawn::CompareFunction::Always || - mStencilInfo.front.stencilFailOp != dawn::StencilOperation::Keep || - mStencilInfo.front.depthFailOp != dawn::StencilOperation::Keep || - mStencilInfo.front.passOp != dawn::StencilOperation::Keep; - } - - const DepthStencilStateBase::DepthInfo& DepthStencilStateBase::GetDepth() const { - return mDepthInfo; - } - - const DepthStencilStateBase::StencilInfo& DepthStencilStateBase::GetStencil() const { - return mStencilInfo; - } - - // DepthStencilStateBuilder - - enum DepthStencilStateSetProperties { - DEPTH_STENCIL_STATE_PROPERTY_DEPTH_COMPARE_FUNCTION = 0x1, - DEPTH_STENCIL_STATE_PROPERTY_DEPTH_WRITE_ENABLED = 0x2, - DEPTH_STENCIL_STATE_PROPERTY_STENCIL_BACK_FUNCTION = 0x4, - DEPTH_STENCIL_STATE_PROPERTY_STENCIL_FRONT_FUNCTION = 0x08, - DEPTH_STENCIL_STATE_PROPERTY_STENCIL_MASK = 0x10, - }; - - DepthStencilStateBuilder::DepthStencilStateBuilder(DeviceBase* device) : Builder(device) { - } - - DepthStencilStateBase* DepthStencilStateBuilder::GetResultImpl() { - return GetDevice()->CreateDepthStencilState(this); - } - - void DepthStencilStateBuilder::SetDepthCompareFunction( - dawn::CompareFunction depthCompareFunction) { - if ((mPropertiesSet & DEPTH_STENCIL_STATE_PROPERTY_DEPTH_COMPARE_FUNCTION) != 0) { - HandleError("Depth compare property set multiple times"); - return; - } - - mPropertiesSet |= DEPTH_STENCIL_STATE_PROPERTY_DEPTH_COMPARE_FUNCTION; - - mDepthInfo.compareFunction = depthCompareFunction; - } - - void DepthStencilStateBuilder::SetDepthWriteEnabled(bool enabled) { - if ((mPropertiesSet & DEPTH_STENCIL_STATE_PROPERTY_DEPTH_WRITE_ENABLED) != 0) { - HandleError("Depth write enabled property set multiple times"); - return; - } - - mPropertiesSet |= DEPTH_STENCIL_STATE_PROPERTY_DEPTH_WRITE_ENABLED; - - mDepthInfo.depthWriteEnabled = enabled; - } - - void DepthStencilStateBuilder::SetStencilFunction( - dawn::Face face, - const StencilStateFaceDescriptor* descriptor) { - if (face == dawn::Face::None) { - HandleError("Can't set stencil function of None face"); - return; - } - - if (face & dawn::Face::Back) { - if ((mPropertiesSet & DEPTH_STENCIL_STATE_PROPERTY_STENCIL_BACK_FUNCTION) != 0) { - HandleError("Stencil back function property set multiple times"); - return; - } - - mPropertiesSet |= DEPTH_STENCIL_STATE_PROPERTY_STENCIL_BACK_FUNCTION; - - mStencilInfo.back.compare = descriptor->compare; - mStencilInfo.back.stencilFailOp = descriptor->stencilFailOp; - mStencilInfo.back.depthFailOp = descriptor->depthFailOp; - mStencilInfo.back.passOp = descriptor->passOp; - } - if (face & dawn::Face::Front) { - if ((mPropertiesSet & DEPTH_STENCIL_STATE_PROPERTY_STENCIL_FRONT_FUNCTION) != 0) { - HandleError("Stencil front function property set multiple times"); - return; - } - - mPropertiesSet |= DEPTH_STENCIL_STATE_PROPERTY_STENCIL_FRONT_FUNCTION; - - mStencilInfo.front.compare = descriptor->compare; - mStencilInfo.front.stencilFailOp = descriptor->stencilFailOp; - mStencilInfo.front.depthFailOp = descriptor->depthFailOp; - mStencilInfo.front.passOp = descriptor->passOp; - } - } - - void DepthStencilStateBuilder::SetStencilMask(uint32_t readMask, uint32_t writeMask) { - if ((mPropertiesSet & DEPTH_STENCIL_STATE_PROPERTY_STENCIL_MASK) != 0) { - HandleError("Stencilmask property set multiple times"); - return; - } - - mPropertiesSet |= DEPTH_STENCIL_STATE_PROPERTY_STENCIL_MASK; - mStencilInfo.readMask = readMask; - mStencilInfo.writeMask = writeMask; - } - -} // namespace dawn_native diff --git a/src/dawn_native/DepthStencilState.h b/src/dawn_native/DepthStencilState.h deleted file mode 100644 index 46da7e9ab4..0000000000 --- a/src/dawn_native/DepthStencilState.h +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright 2017 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef DAWNNATIVE_DEPTHSTENCILSTATE_H_ -#define DAWNNATIVE_DEPTHSTENCILSTATE_H_ - -#include "dawn_native/Builder.h" -#include "dawn_native/Forward.h" -#include "dawn_native/ObjectBase.h" - -#include "dawn_native/dawn_platform.h" - -namespace dawn_native { - - class DepthStencilStateBase : public ObjectBase { - public: - DepthStencilStateBase(DepthStencilStateBuilder* builder); - - struct DepthInfo { - dawn::CompareFunction compareFunction = dawn::CompareFunction::Always; - bool depthWriteEnabled = false; - }; - - struct StencilInfo { - StencilStateFaceDescriptor back = { - dawn::CompareFunction::Always, dawn::StencilOperation::Keep, - dawn::StencilOperation::Keep, dawn::StencilOperation::Keep}; - StencilStateFaceDescriptor front = { - dawn::CompareFunction::Always, dawn::StencilOperation::Keep, - dawn::StencilOperation::Keep, dawn::StencilOperation::Keep}; - uint32_t readMask = 0xff; - uint32_t writeMask = 0xff; - }; - - bool StencilTestEnabled() const; - const DepthInfo& GetDepth() const; - const StencilInfo& GetStencil() const; - - private: - DepthInfo mDepthInfo; - StencilInfo mStencilInfo; - }; - - class DepthStencilStateBuilder : public Builder { - public: - DepthStencilStateBuilder(DeviceBase* device); - - // Dawn API - void SetDepthCompareFunction(dawn::CompareFunction depthCompareFunction); - void SetDepthWriteEnabled(bool enabled); - void SetStencilFunction(dawn::Face face, const StencilStateFaceDescriptor* descriptor); - void SetStencilMask(uint32_t readMask, uint32_t writeMask); - - private: - friend class DepthStencilStateBase; - - DepthStencilStateBase* GetResultImpl() override; - - int mPropertiesSet = 0; - - DepthStencilStateBase::DepthInfo mDepthInfo; - DepthStencilStateBase::StencilInfo mStencilInfo; - }; - -} // namespace dawn_native - -#endif // DAWNNATIVE_DEPTHSTENCILSTATE_H_ diff --git a/src/dawn_native/Device.cpp b/src/dawn_native/Device.cpp index c94dc11c4e..100b6079fe 100644 --- a/src/dawn_native/Device.cpp +++ b/src/dawn_native/Device.cpp @@ -19,7 +19,6 @@ #include "dawn_native/Buffer.h" #include "dawn_native/CommandBuffer.h" #include "dawn_native/ComputePipeline.h" -#include "dawn_native/DepthStencilState.h" #include "dawn_native/ErrorData.h" #include "dawn_native/Fence.h" #include "dawn_native/FenceSignalTracker.h" @@ -141,9 +140,6 @@ namespace dawn_native { return result; } - DepthStencilStateBuilder* DeviceBase::CreateDepthStencilStateBuilder() { - return new DepthStencilStateBuilder(this); - } FenceBase* DeviceBase::CreateFence(const FenceDescriptor* descriptor) { FenceBase* result = nullptr; diff --git a/src/dawn_native/Device.h b/src/dawn_native/Device.h index 012cb22b35..79e1a350e4 100644 --- a/src/dawn_native/Device.h +++ b/src/dawn_native/Device.h @@ -52,8 +52,6 @@ namespace dawn_native { FenceSignalTracker* GetFenceSignalTracker() const; virtual CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) = 0; - virtual DepthStencilStateBase* CreateDepthStencilState( - DepthStencilStateBuilder* builder) = 0; virtual InputStateBase* CreateInputState(InputStateBuilder* builder) = 0; virtual RenderPassDescriptorBase* CreateRenderPassDescriptor( RenderPassDescriptorBuilder* builder) = 0; @@ -87,7 +85,6 @@ namespace dawn_native { BufferBase* CreateBuffer(const BufferDescriptor* descriptor); CommandBufferBuilder* CreateCommandBufferBuilder(); ComputePipelineBase* CreateComputePipeline(const ComputePipelineDescriptor* descriptor); - DepthStencilStateBuilder* CreateDepthStencilStateBuilder(); FenceBase* CreateFence(const FenceDescriptor* descriptor); InputStateBuilder* CreateInputStateBuilder(); PipelineLayoutBase* CreatePipelineLayout(const PipelineLayoutDescriptor* descriptor); diff --git a/src/dawn_native/Forward.h b/src/dawn_native/Forward.h index 93e635266f..05b3f808ff 100644 --- a/src/dawn_native/Forward.h +++ b/src/dawn_native/Forward.h @@ -29,8 +29,6 @@ namespace dawn_native { class CommandBufferBase; class CommandBufferBuilder; class ComputePassEncoderBase; - class DepthStencilStateBase; - class DepthStencilStateBuilder; class FenceBase; class InputStateBase; class InputStateBuilder; diff --git a/src/dawn_native/Pipeline.cpp b/src/dawn_native/Pipeline.cpp index 175e864732..fb2a3112e6 100644 --- a/src/dawn_native/Pipeline.cpp +++ b/src/dawn_native/Pipeline.cpp @@ -14,7 +14,6 @@ #include "dawn_native/Pipeline.h" -#include "dawn_native/DepthStencilState.h" #include "dawn_native/Device.h" #include "dawn_native/InputState.h" #include "dawn_native/PipelineLayout.h" diff --git a/src/dawn_native/RenderPipeline.cpp b/src/dawn_native/RenderPipeline.cpp index 1fde80a7c9..fc25d37cdd 100644 --- a/src/dawn_native/RenderPipeline.cpp +++ b/src/dawn_native/RenderPipeline.cpp @@ -15,7 +15,6 @@ #include "dawn_native/RenderPipeline.h" #include "common/BitSetIterator.h" -#include "dawn_native/DepthStencilState.h" #include "dawn_native/Device.h" #include "dawn_native/InputState.h" #include "dawn_native/RenderPassDescriptor.h" @@ -86,7 +85,24 @@ namespace dawn_native { return {}; } - } // namespace + MaybeError ValidateDepthStencilStateDescriptor( + const DepthStencilStateDescriptor* descriptor) { + if (descriptor->nextInChain != nullptr) { + return DAWN_VALIDATION_ERROR("nextInChain must be nullptr"); + } + DAWN_TRY(ValidateCompareFunction(descriptor->depthCompare)); + DAWN_TRY(ValidateCompareFunction(descriptor->front.compare)); + DAWN_TRY(ValidateStencilOperation(descriptor->front.stencilFailOp)); + DAWN_TRY(ValidateStencilOperation(descriptor->front.depthFailOp)); + DAWN_TRY(ValidateStencilOperation(descriptor->front.passOp)); + DAWN_TRY(ValidateCompareFunction(descriptor->back.compare)); + DAWN_TRY(ValidateStencilOperation(descriptor->back.stencilFailOp)); + DAWN_TRY(ValidateStencilOperation(descriptor->back.depthFailOp)); + DAWN_TRY(ValidateStencilOperation(descriptor->back.passOp)); + return {}; + } + + } // anonymous namespace MaybeError ValidateRenderPipelineDescriptor(DeviceBase* device, const RenderPipelineDescriptor* descriptor) { @@ -137,10 +153,22 @@ namespace dawn_native { return DAWN_VALIDATION_ERROR("Each color attachment should have blend state"); } - // TODO: validate depth stencil state + DAWN_TRY(ValidateDepthStencilStateDescriptor(descriptor->depthStencilState)); + return {}; } + bool StencilTestEnabled(const DepthStencilStateDescriptor* mDepthStencilState) { + return mDepthStencilState->back.compare != dawn::CompareFunction::Always || + mDepthStencilState->back.stencilFailOp != dawn::StencilOperation::Keep || + mDepthStencilState->back.depthFailOp != dawn::StencilOperation::Keep || + mDepthStencilState->back.passOp != dawn::StencilOperation::Keep || + mDepthStencilState->front.compare != dawn::CompareFunction::Always || + mDepthStencilState->front.stencilFailOp != dawn::StencilOperation::Keep || + mDepthStencilState->front.depthFailOp != dawn::StencilOperation::Keep || + mDepthStencilState->front.passOp != dawn::StencilOperation::Keep; + } + // RenderPipelineBase RenderPipelineBase::RenderPipelineBase(DeviceBase* device, @@ -148,7 +176,7 @@ namespace dawn_native { : PipelineBase(device, descriptor->layout, dawn::ShaderStageBit::Vertex | dawn::ShaderStageBit::Fragment), - mDepthStencilState(descriptor->depthStencilState), + mDepthStencilState(*descriptor->depthStencilState), mIndexFormat(descriptor->indexFormat), mInputState(descriptor->inputState), mPrimitiveTopology(descriptor->primitiveTopology), @@ -175,8 +203,8 @@ namespace dawn_native { return &mBlendStates[attachmentSlot]; } - DepthStencilStateBase* RenderPipelineBase::GetDepthStencilState() { - return mDepthStencilState.Get(); + const DepthStencilStateDescriptor* RenderPipelineBase::GetDepthStencilStateDescriptor() { + return &mDepthStencilState; } dawn::IndexFormat RenderPipelineBase::GetIndexFormat() const { diff --git a/src/dawn_native/RenderPipeline.h b/src/dawn_native/RenderPipeline.h index 35cf013081..2ede129e25 100644 --- a/src/dawn_native/RenderPipeline.h +++ b/src/dawn_native/RenderPipeline.h @@ -15,7 +15,6 @@ #ifndef DAWNNATIVE_RENDERPIPELINE_H_ #define DAWNNATIVE_RENDERPIPELINE_H_ -#include "dawn_native/DepthStencilState.h" #include "dawn_native/InputState.h" #include "dawn_native/Pipeline.h" @@ -30,13 +29,14 @@ namespace dawn_native { MaybeError ValidateRenderPipelineDescriptor(DeviceBase* device, const RenderPipelineDescriptor* descriptor); + bool StencilTestEnabled(const DepthStencilStateDescriptor* mDepthStencilState); class RenderPipelineBase : public PipelineBase { public: RenderPipelineBase(DeviceBase* device, const RenderPipelineDescriptor* descriptor); const BlendStateDescriptor* GetBlendStateDescriptor(uint32_t attachmentSlot); - DepthStencilStateBase* GetDepthStencilState(); + const DepthStencilStateDescriptor* GetDepthStencilStateDescriptor(); dawn::IndexFormat GetIndexFormat() const; InputStateBase* GetInputState(); dawn::PrimitiveTopology GetPrimitiveTopology() const; @@ -51,7 +51,7 @@ namespace dawn_native { bool IsCompatibleWith(const RenderPassDescriptorBase* renderPass) const; private: - Ref mDepthStencilState; + DepthStencilStateDescriptor mDepthStencilState; dawn::IndexFormat mIndexFormat; Ref mInputState; dawn::PrimitiveTopology mPrimitiveTopology; diff --git a/src/dawn_native/ToBackend.h b/src/dawn_native/ToBackend.h index 34b7492ba9..94391f3115 100644 --- a/src/dawn_native/ToBackend.h +++ b/src/dawn_native/ToBackend.h @@ -48,11 +48,6 @@ namespace dawn_native { using BackendType = typename BackendTraits::ComputePipelineType; }; - template - struct ToBackendTraits { - using BackendType = typename BackendTraits::DepthStencilStateType; - }; - template struct ToBackendTraits { using BackendType = typename BackendTraits::DeviceType; diff --git a/src/dawn_native/d3d12/DepthStencilStateD3D12.cpp b/src/dawn_native/d3d12/DepthStencilStateD3D12.cpp deleted file mode 100644 index 05d04c4c37..0000000000 --- a/src/dawn_native/d3d12/DepthStencilStateD3D12.cpp +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright 2017 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "dawn_native/d3d12/DepthStencilStateD3D12.h" - -#include "common/BitSetIterator.h" - -namespace dawn_native { namespace d3d12 { - - namespace { - D3D12_STENCIL_OP StencilOp(dawn::StencilOperation op) { - switch (op) { - case dawn::StencilOperation::Keep: - return D3D12_STENCIL_OP_KEEP; - case dawn::StencilOperation::Zero: - return D3D12_STENCIL_OP_ZERO; - case dawn::StencilOperation::Replace: - return D3D12_STENCIL_OP_REPLACE; - case dawn::StencilOperation::IncrementClamp: - return D3D12_STENCIL_OP_INCR_SAT; - case dawn::StencilOperation::DecrementClamp: - return D3D12_STENCIL_OP_DECR_SAT; - case dawn::StencilOperation::Invert: - return D3D12_STENCIL_OP_INVERT; - case dawn::StencilOperation::IncrementWrap: - return D3D12_STENCIL_OP_INCR; - case dawn::StencilOperation::DecrementWrap: - return D3D12_STENCIL_OP_DECR; - default: - UNREACHABLE(); - } - } - - D3D12_COMPARISON_FUNC ComparisonFunc(dawn::CompareFunction func) { - switch (func) { - case dawn::CompareFunction::Always: - return D3D12_COMPARISON_FUNC_ALWAYS; - case dawn::CompareFunction::Equal: - return D3D12_COMPARISON_FUNC_EQUAL; - case dawn::CompareFunction::Greater: - return D3D12_COMPARISON_FUNC_GREATER; - case dawn::CompareFunction::GreaterEqual: - return D3D12_COMPARISON_FUNC_GREATER_EQUAL; - case dawn::CompareFunction::Less: - return D3D12_COMPARISON_FUNC_LESS; - case dawn::CompareFunction::LessEqual: - return D3D12_COMPARISON_FUNC_LESS_EQUAL; - case dawn::CompareFunction::Never: - return D3D12_COMPARISON_FUNC_NEVER; - case dawn::CompareFunction::NotEqual: - return D3D12_COMPARISON_FUNC_NOT_EQUAL; - default: - UNREACHABLE(); - } - } - - D3D12_DEPTH_STENCILOP_DESC StencilOpDesc(const StencilStateFaceDescriptor descriptor) { - D3D12_DEPTH_STENCILOP_DESC desc; - - desc.StencilFailOp = StencilOp(descriptor.stencilFailOp); - desc.StencilDepthFailOp = StencilOp(descriptor.depthFailOp); - desc.StencilPassOp = StencilOp(descriptor.passOp); - desc.StencilFunc = ComparisonFunc(descriptor.compare); - - return desc; - } - } // anonymous namespace - - DepthStencilState::DepthStencilState(DepthStencilStateBuilder* builder) - : DepthStencilStateBase(builder) { - mDepthStencilDescriptor.DepthEnable = TRUE; - mDepthStencilDescriptor.DepthWriteMask = - GetDepth().depthWriteEnabled ? D3D12_DEPTH_WRITE_MASK_ALL : D3D12_DEPTH_WRITE_MASK_ZERO; - mDepthStencilDescriptor.DepthFunc = ComparisonFunc(GetDepth().compareFunction); - - mDepthStencilDescriptor.StencilEnable = StencilTestEnabled() ? TRUE : FALSE; - mDepthStencilDescriptor.StencilReadMask = static_cast(GetStencil().readMask); - mDepthStencilDescriptor.StencilWriteMask = static_cast(GetStencil().writeMask); - - mDepthStencilDescriptor.FrontFace = StencilOpDesc(GetStencil().front); - mDepthStencilDescriptor.BackFace = StencilOpDesc(GetStencil().back); - } - - const D3D12_DEPTH_STENCIL_DESC& DepthStencilState::GetD3D12DepthStencilDescriptor() const { - return mDepthStencilDescriptor; - } - -}} // namespace dawn_native::d3d12 diff --git a/src/dawn_native/d3d12/DepthStencilStateD3D12.h b/src/dawn_native/d3d12/DepthStencilStateD3D12.h deleted file mode 100644 index e7041b7766..0000000000 --- a/src/dawn_native/d3d12/DepthStencilStateD3D12.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2017 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef DAWNNATIVE_D3D12_DEPTHSTENCILSTATED3D12_H_ -#define DAWNNATIVE_D3D12_DEPTHSTENCILSTATED3D12_H_ - -#include "dawn_native/DepthStencilState.h" - -#include "dawn_native/d3d12/d3d12_platform.h" - -namespace dawn_native { namespace d3d12 { - - class Device; - - class DepthStencilState : public DepthStencilStateBase { - public: - DepthStencilState(DepthStencilStateBuilder* builder); - - const D3D12_DEPTH_STENCIL_DESC& GetD3D12DepthStencilDescriptor() const; - - private: - D3D12_DEPTH_STENCIL_DESC mDepthStencilDescriptor; - }; - -}} // namespace dawn_native::d3d12 - -#endif // DAWNNATIVE_D3D12_DEPTHSTENCILSTATED3D12_H_ diff --git a/src/dawn_native/d3d12/DeviceD3D12.cpp b/src/dawn_native/d3d12/DeviceD3D12.cpp index 5bfc7cc138..2d9f6beb5b 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.cpp +++ b/src/dawn_native/d3d12/DeviceD3D12.cpp @@ -23,7 +23,6 @@ #include "dawn_native/d3d12/CommandAllocatorManager.h" #include "dawn_native/d3d12/CommandBufferD3D12.h" #include "dawn_native/d3d12/ComputePipelineD3D12.h" -#include "dawn_native/d3d12/DepthStencilStateD3D12.h" #include "dawn_native/d3d12/DescriptorHeapAllocator.h" #include "dawn_native/d3d12/InputStateD3D12.h" #include "dawn_native/d3d12/NativeSwapChainImplD3D12.h" @@ -303,9 +302,6 @@ namespace dawn_native { namespace d3d12 { const ComputePipelineDescriptor* descriptor) { return new ComputePipeline(this, descriptor); } - DepthStencilStateBase* Device::CreateDepthStencilState(DepthStencilStateBuilder* builder) { - return new DepthStencilState(builder); - } InputStateBase* Device::CreateInputState(InputStateBuilder* builder) { return new InputState(builder); } diff --git a/src/dawn_native/d3d12/DeviceD3D12.h b/src/dawn_native/d3d12/DeviceD3D12.h index 70341aeee9..8230227779 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.h +++ b/src/dawn_native/d3d12/DeviceD3D12.h @@ -42,7 +42,6 @@ namespace dawn_native { namespace d3d12 { ~Device(); CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override; - DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) override; InputStateBase* CreateInputState(InputStateBuilder* builder) override; RenderPassDescriptorBase* CreateRenderPassDescriptor( RenderPassDescriptorBuilder* builder) override; diff --git a/src/dawn_native/d3d12/Forward.h b/src/dawn_native/d3d12/Forward.h index d3b6b449c5..49d1375b28 100644 --- a/src/dawn_native/d3d12/Forward.h +++ b/src/dawn_native/d3d12/Forward.h @@ -24,7 +24,6 @@ namespace dawn_native { namespace d3d12 { class Buffer; class CommandBuffer; class ComputePipeline; - class DepthStencilState; class Device; class InputState; class PipelineLayout; @@ -43,7 +42,6 @@ namespace dawn_native { namespace d3d12 { using BufferType = Buffer; using CommandBufferType = CommandBuffer; using ComputePipelineType = ComputePipeline; - using DepthStencilStateType = DepthStencilState; using DeviceType = Device; using InputStateType = InputState; using PipelineLayoutType = PipelineLayout; diff --git a/src/dawn_native/d3d12/RenderPipelineD3D12.cpp b/src/dawn_native/d3d12/RenderPipelineD3D12.cpp index 2866485b94..ffd103de61 100644 --- a/src/dawn_native/d3d12/RenderPipelineD3D12.cpp +++ b/src/dawn_native/d3d12/RenderPipelineD3D12.cpp @@ -15,7 +15,6 @@ #include "dawn_native/d3d12/RenderPipelineD3D12.h" #include "common/Assert.h" -#include "dawn_native/d3d12/DepthStencilStateD3D12.h" #include "dawn_native/d3d12/DeviceD3D12.h" #include "dawn_native/d3d12/InputStateD3D12.h" #include "dawn_native/d3d12/PipelineLayoutD3D12.h" @@ -142,7 +141,85 @@ namespace dawn_native { namespace d3d12 { blendDesc.LogicOp = D3D12_LOGIC_OP_NOOP; return blendDesc; } - } // namespace + + D3D12_STENCIL_OP StencilOp(dawn::StencilOperation op) { + switch (op) { + case dawn::StencilOperation::Keep: + return D3D12_STENCIL_OP_KEEP; + case dawn::StencilOperation::Zero: + return D3D12_STENCIL_OP_ZERO; + case dawn::StencilOperation::Replace: + return D3D12_STENCIL_OP_REPLACE; + case dawn::StencilOperation::IncrementClamp: + return D3D12_STENCIL_OP_INCR_SAT; + case dawn::StencilOperation::DecrementClamp: + return D3D12_STENCIL_OP_DECR_SAT; + case dawn::StencilOperation::Invert: + return D3D12_STENCIL_OP_INVERT; + case dawn::StencilOperation::IncrementWrap: + return D3D12_STENCIL_OP_INCR; + case dawn::StencilOperation::DecrementWrap: + return D3D12_STENCIL_OP_DECR; + default: + UNREACHABLE(); + } + } + + D3D12_COMPARISON_FUNC ComparisonFunc(dawn::CompareFunction func) { + switch (func) { + case dawn::CompareFunction::Always: + return D3D12_COMPARISON_FUNC_ALWAYS; + case dawn::CompareFunction::Equal: + return D3D12_COMPARISON_FUNC_EQUAL; + case dawn::CompareFunction::Greater: + return D3D12_COMPARISON_FUNC_GREATER; + case dawn::CompareFunction::GreaterEqual: + return D3D12_COMPARISON_FUNC_GREATER_EQUAL; + case dawn::CompareFunction::Less: + return D3D12_COMPARISON_FUNC_LESS; + case dawn::CompareFunction::LessEqual: + return D3D12_COMPARISON_FUNC_LESS_EQUAL; + case dawn::CompareFunction::Never: + return D3D12_COMPARISON_FUNC_NEVER; + case dawn::CompareFunction::NotEqual: + return D3D12_COMPARISON_FUNC_NOT_EQUAL; + default: + UNREACHABLE(); + } + } + + D3D12_DEPTH_STENCILOP_DESC StencilOpDesc(const StencilStateFaceDescriptor descriptor) { + D3D12_DEPTH_STENCILOP_DESC desc; + + desc.StencilFailOp = StencilOp(descriptor.stencilFailOp); + desc.StencilDepthFailOp = StencilOp(descriptor.depthFailOp); + desc.StencilPassOp = StencilOp(descriptor.passOp); + desc.StencilFunc = ComparisonFunc(descriptor.compare); + + return desc; + } + + D3D12_DEPTH_STENCIL_DESC ComputeDepthStencilDesc( + const DepthStencilStateDescriptor* descriptor) { + D3D12_DEPTH_STENCIL_DESC mDepthStencilDescriptor; + mDepthStencilDescriptor.DepthEnable = TRUE; + mDepthStencilDescriptor.DepthWriteMask = descriptor->depthWriteEnabled + ? D3D12_DEPTH_WRITE_MASK_ALL + : D3D12_DEPTH_WRITE_MASK_ZERO; + mDepthStencilDescriptor.DepthFunc = ComparisonFunc(descriptor->depthCompare); + + mDepthStencilDescriptor.StencilEnable = StencilTestEnabled(descriptor) ? TRUE : FALSE; + mDepthStencilDescriptor.StencilReadMask = + static_cast(descriptor->stencilReadMask); + mDepthStencilDescriptor.StencilWriteMask = + static_cast(descriptor->stencilWriteMask); + + mDepthStencilDescriptor.FrontFace = StencilOpDesc(descriptor->front); + mDepthStencilDescriptor.BackFace = StencilOpDesc(descriptor->back); + return mDepthStencilDescriptor; + } + + } // anonymous namespace RenderPipeline::RenderPipeline(Device* device, const RenderPipelineDescriptor* descriptor) : RenderPipelineBase(device, descriptor), @@ -239,8 +316,8 @@ namespace dawn_native { namespace d3d12 { descriptorD3D12.BlendState.AlphaToCoverageEnable = FALSE; descriptorD3D12.BlendState.IndependentBlendEnable = TRUE; - DepthStencilState* depthStencilState = ToBackend(GetDepthStencilState()); - descriptorD3D12.DepthStencilState = depthStencilState->GetD3D12DepthStencilDescriptor(); + descriptorD3D12.DepthStencilState = + ComputeDepthStencilDesc(GetDepthStencilStateDescriptor()); descriptorD3D12.SampleMask = UINT_MAX; descriptorD3D12.PrimitiveTopologyType = D3D12PrimitiveTopologyType(GetPrimitiveTopology()); diff --git a/src/dawn_native/metal/CommandBufferMTL.mm b/src/dawn_native/metal/CommandBufferMTL.mm index cbd8262e9d..22f8241efa 100644 --- a/src/dawn_native/metal/CommandBufferMTL.mm +++ b/src/dawn_native/metal/CommandBufferMTL.mm @@ -18,7 +18,6 @@ #include "dawn_native/Commands.h" #include "dawn_native/metal/BufferMTL.h" #include "dawn_native/metal/ComputePipelineMTL.h" -#include "dawn_native/metal/DepthStencilStateMTL.h" #include "dawn_native/metal/DeviceMTL.h" #include "dawn_native/metal/InputStateMTL.h" #include "dawn_native/metal/PipelineLayoutMTL.h" @@ -435,9 +434,7 @@ namespace dawn_native { namespace metal { SetRenderPipelineCmd* cmd = mCommands.NextCommand(); lastPipeline = ToBackend(cmd->pipeline).Get(); - DepthStencilState* depthStencilState = - ToBackend(lastPipeline->GetDepthStencilState()); - [encoder setDepthStencilState:depthStencilState->GetMTLDepthStencilState()]; + [encoder setDepthStencilState:lastPipeline->GetMTLDepthStencilState()]; lastPipeline->Encode(encoder); } break; diff --git a/src/dawn_native/metal/DepthStencilStateMTL.h b/src/dawn_native/metal/DepthStencilStateMTL.h deleted file mode 100644 index c33fadfbd2..0000000000 --- a/src/dawn_native/metal/DepthStencilStateMTL.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2017 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef DAWNNATIVE_METAL_DEPTHSTENCILSTATEMTL_H_ -#define DAWNNATIVE_METAL_DEPTHSTENCILSTATEMTL_H_ - -#include "dawn_native/DepthStencilState.h" - -#import - -namespace dawn_native { namespace metal { - - class Device; - - class DepthStencilState : public DepthStencilStateBase { - public: - DepthStencilState(DepthStencilStateBuilder* builder); - ~DepthStencilState(); - - id GetMTLDepthStencilState(); - - private: - id mMtlDepthStencilState = nil; - }; - -}} // namespace dawn_native::metal - -#endif // DAWNNATIVE_METAL_DEPTHSTENCILSTATEMTL_H_ diff --git a/src/dawn_native/metal/DepthStencilStateMTL.mm b/src/dawn_native/metal/DepthStencilStateMTL.mm deleted file mode 100644 index 8ff2431a52..0000000000 --- a/src/dawn_native/metal/DepthStencilStateMTL.mm +++ /dev/null @@ -1,120 +0,0 @@ -// Copyright 2017 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "dawn_native/metal/DepthStencilStateMTL.h" - -#include "dawn_native/metal/DeviceMTL.h" - -namespace dawn_native { namespace metal { - - namespace { - MTLCompareFunction MetalDepthStencilCompareFunction(dawn::CompareFunction compareFunction) { - switch (compareFunction) { - case dawn::CompareFunction::Never: - return MTLCompareFunctionNever; - case dawn::CompareFunction::Less: - return MTLCompareFunctionLess; - case dawn::CompareFunction::LessEqual: - return MTLCompareFunctionLessEqual; - case dawn::CompareFunction::Greater: - return MTLCompareFunctionGreater; - case dawn::CompareFunction::GreaterEqual: - return MTLCompareFunctionGreaterEqual; - case dawn::CompareFunction::NotEqual: - return MTLCompareFunctionNotEqual; - case dawn::CompareFunction::Equal: - return MTLCompareFunctionEqual; - case dawn::CompareFunction::Always: - return MTLCompareFunctionAlways; - } - } - - MTLStencilOperation MetalStencilOperation(dawn::StencilOperation stencilOperation) { - switch (stencilOperation) { - case dawn::StencilOperation::Keep: - return MTLStencilOperationKeep; - case dawn::StencilOperation::Zero: - return MTLStencilOperationZero; - case dawn::StencilOperation::Replace: - return MTLStencilOperationReplace; - case dawn::StencilOperation::Invert: - return MTLStencilOperationInvert; - case dawn::StencilOperation::IncrementClamp: - return MTLStencilOperationIncrementClamp; - case dawn::StencilOperation::DecrementClamp: - return MTLStencilOperationDecrementClamp; - case dawn::StencilOperation::IncrementWrap: - return MTLStencilOperationIncrementWrap; - case dawn::StencilOperation::DecrementWrap: - return MTLStencilOperationDecrementWrap; - } - } - } - - DepthStencilState::DepthStencilState(DepthStencilStateBuilder* builder) - : DepthStencilStateBase(builder) { - MTLDepthStencilDescriptor* mtlDepthStencilDescriptor = [MTLDepthStencilDescriptor new]; - - auto& depth = GetDepth(); - mtlDepthStencilDescriptor.depthCompareFunction = - MetalDepthStencilCompareFunction(depth.compareFunction); - mtlDepthStencilDescriptor.depthWriteEnabled = depth.depthWriteEnabled; - - auto& stencil = GetStencil(); - if (StencilTestEnabled()) { - MTLStencilDescriptor* backFaceStencil = [MTLStencilDescriptor new]; - MTLStencilDescriptor* frontFaceStencil = [MTLStencilDescriptor new]; - - backFaceStencil.stencilCompareFunction = - MetalDepthStencilCompareFunction(stencil.back.compare); - backFaceStencil.stencilFailureOperation = - MetalStencilOperation(stencil.back.stencilFailOp); - backFaceStencil.depthFailureOperation = MetalStencilOperation(stencil.back.depthFailOp); - backFaceStencil.depthStencilPassOperation = MetalStencilOperation(stencil.back.passOp); - backFaceStencil.readMask = stencil.readMask; - backFaceStencil.writeMask = stencil.writeMask; - - frontFaceStencil.stencilCompareFunction = - MetalDepthStencilCompareFunction(stencil.front.compare); - frontFaceStencil.stencilFailureOperation = - MetalStencilOperation(stencil.front.stencilFailOp); - frontFaceStencil.depthFailureOperation = - MetalStencilOperation(stencil.front.depthFailOp); - frontFaceStencil.depthStencilPassOperation = - MetalStencilOperation(stencil.front.passOp); - frontFaceStencil.readMask = stencil.readMask; - frontFaceStencil.writeMask = stencil.writeMask; - - mtlDepthStencilDescriptor.backFaceStencil = backFaceStencil; - mtlDepthStencilDescriptor.frontFaceStencil = frontFaceStencil; - [backFaceStencil release]; - [frontFaceStencil release]; - } - - auto mtlDevice = ToBackend(builder->GetDevice())->GetMTLDevice(); - mMtlDepthStencilState = - [mtlDevice newDepthStencilStateWithDescriptor:mtlDepthStencilDescriptor]; - [mtlDepthStencilDescriptor release]; - } - - DepthStencilState::~DepthStencilState() { - [mMtlDepthStencilState release]; - mMtlDepthStencilState = nil; - } - - id DepthStencilState::GetMTLDepthStencilState() { - return mMtlDepthStencilState; - } - -}} // namespace dawn_native::metal diff --git a/src/dawn_native/metal/DeviceMTL.h b/src/dawn_native/metal/DeviceMTL.h index 9aeb4a7031..fa96d4e75a 100644 --- a/src/dawn_native/metal/DeviceMTL.h +++ b/src/dawn_native/metal/DeviceMTL.h @@ -38,7 +38,6 @@ namespace dawn_native { namespace metal { ~Device(); CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override; - DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) override; InputStateBase* CreateInputState(InputStateBuilder* builder) override; RenderPassDescriptorBase* CreateRenderPassDescriptor( RenderPassDescriptorBuilder* builder) override; diff --git a/src/dawn_native/metal/DeviceMTL.mm b/src/dawn_native/metal/DeviceMTL.mm index 8f888228b7..b11018a104 100644 --- a/src/dawn_native/metal/DeviceMTL.mm +++ b/src/dawn_native/metal/DeviceMTL.mm @@ -21,7 +21,6 @@ #include "dawn_native/metal/BufferMTL.h" #include "dawn_native/metal/CommandBufferMTL.h" #include "dawn_native/metal/ComputePipelineMTL.h" -#include "dawn_native/metal/DepthStencilStateMTL.h" #include "dawn_native/metal/InputStateMTL.h" #include "dawn_native/metal/PipelineLayoutMTL.h" #include "dawn_native/metal/QueueMTL.h" @@ -179,9 +178,6 @@ namespace dawn_native { namespace metal { const ComputePipelineDescriptor* descriptor) { return new ComputePipeline(this, descriptor); } - DepthStencilStateBase* Device::CreateDepthStencilState(DepthStencilStateBuilder* builder) { - return new DepthStencilState(builder); - } InputStateBase* Device::CreateInputState(InputStateBuilder* builder) { return new InputState(builder); } diff --git a/src/dawn_native/metal/Forward.h b/src/dawn_native/metal/Forward.h index 6e675580bf..599a797ac2 100644 --- a/src/dawn_native/metal/Forward.h +++ b/src/dawn_native/metal/Forward.h @@ -30,7 +30,6 @@ namespace dawn_native { namespace metal { class Buffer; class CommandBuffer; class ComputePipeline; - class DepthStencilState; class Device; class Framebuffer; class InputState; @@ -50,7 +49,6 @@ namespace dawn_native { namespace metal { using BufferType = Buffer; using CommandBufferType = CommandBuffer; using ComputePipelineType = ComputePipeline; - using DepthStencilStateType = DepthStencilState; using DeviceType = Device; using InputStateType = InputState; using PipelineLayoutType = PipelineLayout; diff --git a/src/dawn_native/metal/RenderPipelineMTL.h b/src/dawn_native/metal/RenderPipelineMTL.h index ff58cc9ee6..6da06e2f4b 100644 --- a/src/dawn_native/metal/RenderPipelineMTL.h +++ b/src/dawn_native/metal/RenderPipelineMTL.h @@ -33,10 +33,13 @@ namespace dawn_native { namespace metal { void Encode(id encoder); + id GetMTLDepthStencilState(); + private: MTLIndexType mMtlIndexType; MTLPrimitiveType mMtlPrimitiveTopology; id mMtlRenderPipelineState = nil; + id mMtlDepthStencilState = nil; }; }} // namespace dawn_native::metal diff --git a/src/dawn_native/metal/RenderPipelineMTL.mm b/src/dawn_native/metal/RenderPipelineMTL.mm index b8ba9542dd..15822b4482 100644 --- a/src/dawn_native/metal/RenderPipelineMTL.mm +++ b/src/dawn_native/metal/RenderPipelineMTL.mm @@ -14,7 +14,6 @@ #include "dawn_native/metal/RenderPipelineMTL.h" -#include "dawn_native/metal/DepthStencilStateMTL.h" #include "dawn_native/metal/DeviceMTL.h" #include "dawn_native/metal/InputStateMTL.h" #include "dawn_native/metal/PipelineLayoutMTL.h" @@ -143,6 +142,89 @@ namespace dawn_native { namespace metal { attachment.alphaBlendOperation = MetalBlendOperation(descriptor->alphaBlend.operation); attachment.writeMask = MetalColorWriteMask(descriptor->colorWriteMask); } + + MTLCompareFunction MetalDepthStencilCompareFunction(dawn::CompareFunction compareFunction) { + switch (compareFunction) { + case dawn::CompareFunction::Never: + return MTLCompareFunctionNever; + case dawn::CompareFunction::Less: + return MTLCompareFunctionLess; + case dawn::CompareFunction::LessEqual: + return MTLCompareFunctionLessEqual; + case dawn::CompareFunction::Greater: + return MTLCompareFunctionGreater; + case dawn::CompareFunction::GreaterEqual: + return MTLCompareFunctionGreaterEqual; + case dawn::CompareFunction::NotEqual: + return MTLCompareFunctionNotEqual; + case dawn::CompareFunction::Equal: + return MTLCompareFunctionEqual; + case dawn::CompareFunction::Always: + return MTLCompareFunctionAlways; + } + } + + MTLStencilOperation MetalStencilOperation(dawn::StencilOperation stencilOperation) { + switch (stencilOperation) { + case dawn::StencilOperation::Keep: + return MTLStencilOperationKeep; + case dawn::StencilOperation::Zero: + return MTLStencilOperationZero; + case dawn::StencilOperation::Replace: + return MTLStencilOperationReplace; + case dawn::StencilOperation::Invert: + return MTLStencilOperationInvert; + case dawn::StencilOperation::IncrementClamp: + return MTLStencilOperationIncrementClamp; + case dawn::StencilOperation::DecrementClamp: + return MTLStencilOperationDecrementClamp; + case dawn::StencilOperation::IncrementWrap: + return MTLStencilOperationIncrementWrap; + case dawn::StencilOperation::DecrementWrap: + return MTLStencilOperationDecrementWrap; + } + } + + MTLDepthStencilDescriptor* ComputeDepthStencilDesc( + const DepthStencilStateDescriptor* descriptor) { + MTLDepthStencilDescriptor* mtlDepthStencilDescriptor = + [[MTLDepthStencilDescriptor new] autorelease]; + mtlDepthStencilDescriptor.depthCompareFunction = + MetalDepthStencilCompareFunction(descriptor->depthCompare); + mtlDepthStencilDescriptor.depthWriteEnabled = descriptor->depthWriteEnabled; + + if (StencilTestEnabled(descriptor)) { + MTLStencilDescriptor* backFaceStencil = [[MTLStencilDescriptor new] autorelease]; + MTLStencilDescriptor* frontFaceStencil = [[MTLStencilDescriptor new] autorelease]; + + backFaceStencil.stencilCompareFunction = + MetalDepthStencilCompareFunction(descriptor->back.compare); + backFaceStencil.stencilFailureOperation = + MetalStencilOperation(descriptor->back.stencilFailOp); + backFaceStencil.depthFailureOperation = + MetalStencilOperation(descriptor->back.depthFailOp); + backFaceStencil.depthStencilPassOperation = + MetalStencilOperation(descriptor->back.passOp); + backFaceStencil.readMask = descriptor->stencilReadMask; + backFaceStencil.writeMask = descriptor->stencilWriteMask; + + frontFaceStencil.stencilCompareFunction = + MetalDepthStencilCompareFunction(descriptor->front.compare); + frontFaceStencil.stencilFailureOperation = + MetalStencilOperation(descriptor->front.stencilFailOp); + frontFaceStencil.depthFailureOperation = + MetalStencilOperation(descriptor->front.depthFailOp); + frontFaceStencil.depthStencilPassOperation = + MetalStencilOperation(descriptor->front.passOp); + frontFaceStencil.readMask = descriptor->stencilReadMask; + frontFaceStencil.writeMask = descriptor->stencilWriteMask; + + mtlDepthStencilDescriptor.backFaceStencil = backFaceStencil; + mtlDepthStencilDescriptor.frontFaceStencil = frontFaceStencil; + } + return mtlDepthStencilDescriptor; + } + } // anonymous namespace RenderPipeline::RenderPipeline(Device* device, const RenderPipelineDescriptor* descriptor) @@ -186,21 +268,29 @@ namespace dawn_native { namespace metal { // TODO(kainino@chromium.org): push constants, textures, samplers - NSError* error = nil; - mMtlRenderPipelineState = [mtlDevice newRenderPipelineStateWithDescriptor:descriptorMTL - error:&error]; - if (error != nil) { - NSLog(@" error => %@", error); - device->HandleError("Error creating rendering pipeline state"); + { + NSError* error = nil; + mMtlRenderPipelineState = [mtlDevice newRenderPipelineStateWithDescriptor:descriptorMTL + error:&error]; [descriptorMTL release]; - return; + if (error != nil) { + NSLog(@" error => %@", error); + device->HandleError("Error creating rendering pipeline state"); + return; + } } - [descriptorMTL release]; + // create depth stencil state and cache it, fetch the cached depth stencil state when we + // call setDepthStencilState() for a given render pipeline in CommandBuffer, in order to + // improve performance. + mMtlDepthStencilState = + [mtlDevice newDepthStencilStateWithDescriptor:ComputeDepthStencilDesc( + GetDepthStencilStateDescriptor())]; } RenderPipeline::~RenderPipeline() { [mMtlRenderPipelineState release]; + [mMtlDepthStencilState release]; } MTLIndexType RenderPipeline::GetMTLIndexType() const { @@ -215,4 +305,8 @@ namespace dawn_native { namespace metal { [encoder setRenderPipelineState:mMtlRenderPipelineState]; } + id RenderPipeline::GetMTLDepthStencilState() { + return mMtlDepthStencilState; + } + }} // namespace dawn_native::metal diff --git a/src/dawn_native/null/NullBackend.cpp b/src/dawn_native/null/NullBackend.cpp index 4c84785251..aa3ec7fb9a 100644 --- a/src/dawn_native/null/NullBackend.cpp +++ b/src/dawn_native/null/NullBackend.cpp @@ -52,9 +52,6 @@ namespace dawn_native { namespace null { const ComputePipelineDescriptor* descriptor) { return new ComputePipeline(this, descriptor); } - DepthStencilStateBase* Device::CreateDepthStencilState(DepthStencilStateBuilder* builder) { - return new DepthStencilState(builder); - } InputStateBase* Device::CreateInputState(InputStateBuilder* builder) { return new InputState(builder); } diff --git a/src/dawn_native/null/NullBackend.h b/src/dawn_native/null/NullBackend.h index aecb4dfc93..15a25f3b92 100644 --- a/src/dawn_native/null/NullBackend.h +++ b/src/dawn_native/null/NullBackend.h @@ -22,7 +22,6 @@ #include "dawn_native/Buffer.h" #include "dawn_native/CommandBuffer.h" #include "dawn_native/ComputePipeline.h" -#include "dawn_native/DepthStencilState.h" #include "dawn_native/Device.h" #include "dawn_native/InputState.h" #include "dawn_native/PipelineLayout.h" @@ -42,7 +41,6 @@ namespace dawn_native { namespace null { class Buffer; class CommandBuffer; using ComputePipeline = ComputePipelineBase; - using DepthStencilState = DepthStencilStateBase; class Device; using InputState = InputStateBase; using PipelineLayout = PipelineLayoutBase; @@ -61,7 +59,6 @@ namespace dawn_native { namespace null { using BufferType = Buffer; using CommandBufferType = CommandBuffer; using ComputePipelineType = ComputePipeline; - using DepthStencilStateType = DepthStencilState; using DeviceType = Device; using InputStateType = InputState; using PipelineLayoutType = PipelineLayout; @@ -91,7 +88,6 @@ namespace dawn_native { namespace null { ~Device(); CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override; - DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) override; InputStateBase* CreateInputState(InputStateBuilder* builder) override; RenderPassDescriptorBase* CreateRenderPassDescriptor( RenderPassDescriptorBuilder* builder) override; diff --git a/src/dawn_native/opengl/DepthStencilStateGL.cpp b/src/dawn_native/opengl/DepthStencilStateGL.cpp deleted file mode 100644 index b71aa7f50c..0000000000 --- a/src/dawn_native/opengl/DepthStencilStateGL.cpp +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright 2017 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "dawn_native/opengl/DepthStencilStateGL.h" - -#include "common/Assert.h" -#include "dawn_native/opengl/PersistentPipelineStateGL.h" - -namespace dawn_native { namespace opengl { - - namespace { - GLuint OpenGLCompareFunction(dawn::CompareFunction compareFunction) { - switch (compareFunction) { - case dawn::CompareFunction::Never: - return GL_NEVER; - case dawn::CompareFunction::Less: - return GL_LESS; - case dawn::CompareFunction::LessEqual: - return GL_LEQUAL; - case dawn::CompareFunction::Greater: - return GL_GREATER; - case dawn::CompareFunction::GreaterEqual: - return GL_GEQUAL; - case dawn::CompareFunction::NotEqual: - return GL_NOTEQUAL; - case dawn::CompareFunction::Equal: - return GL_EQUAL; - case dawn::CompareFunction::Always: - return GL_ALWAYS; - default: - UNREACHABLE(); - } - } - - GLuint OpenGLStencilOperation(dawn::StencilOperation stencilOperation) { - switch (stencilOperation) { - case dawn::StencilOperation::Keep: - return GL_KEEP; - case dawn::StencilOperation::Zero: - return GL_ZERO; - case dawn::StencilOperation::Replace: - return GL_REPLACE; - case dawn::StencilOperation::Invert: - return GL_INVERT; - case dawn::StencilOperation::IncrementClamp: - return GL_INCR; - case dawn::StencilOperation::DecrementClamp: - return GL_DECR; - case dawn::StencilOperation::IncrementWrap: - return GL_INCR_WRAP; - case dawn::StencilOperation::DecrementWrap: - return GL_DECR_WRAP; - default: - UNREACHABLE(); - } - } - } // namespace - - DepthStencilState::DepthStencilState(DepthStencilStateBuilder* builder) - : DepthStencilStateBase(builder) { - } - - void DepthStencilState::ApplyNow(PersistentPipelineState& persistentPipelineState) const { - auto& depthInfo = GetDepth(); - - // Depth writes only occur if depth is enabled - if (depthInfo.compareFunction == dawn::CompareFunction::Always && - !depthInfo.depthWriteEnabled) { - glDisable(GL_DEPTH_TEST); - } else { - glEnable(GL_DEPTH_TEST); - } - - if (depthInfo.depthWriteEnabled) { - glDepthMask(GL_TRUE); - } else { - glDepthMask(GL_FALSE); - } - - glDepthFunc(OpenGLCompareFunction(depthInfo.compareFunction)); - - if (StencilTestEnabled()) { - glEnable(GL_STENCIL_TEST); - } else { - glDisable(GL_STENCIL_TEST); - } - - auto& stencilInfo = GetStencil(); - - GLenum backCompareFunction = OpenGLCompareFunction(stencilInfo.back.compare); - GLenum frontCompareFunction = OpenGLCompareFunction(stencilInfo.front.compare); - persistentPipelineState.SetStencilFuncsAndMask(backCompareFunction, frontCompareFunction, - stencilInfo.readMask); - - glStencilOpSeparate(GL_BACK, OpenGLStencilOperation(stencilInfo.back.stencilFailOp), - OpenGLStencilOperation(stencilInfo.back.depthFailOp), - OpenGLStencilOperation(stencilInfo.back.passOp)); - glStencilOpSeparate(GL_FRONT, OpenGLStencilOperation(stencilInfo.front.stencilFailOp), - OpenGLStencilOperation(stencilInfo.front.depthFailOp), - OpenGLStencilOperation(stencilInfo.front.passOp)); - - glStencilMask(stencilInfo.writeMask); - } - -}} // namespace dawn_native::opengl diff --git a/src/dawn_native/opengl/DepthStencilStateGL.h b/src/dawn_native/opengl/DepthStencilStateGL.h deleted file mode 100644 index da06830db7..0000000000 --- a/src/dawn_native/opengl/DepthStencilStateGL.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2017 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef DAWNNATIVE_OPENGL_DEPTHSTENCILSTATEGL_H_ -#define DAWNNATIVE_OPENGL_DEPTHSTENCILSTATEGL_H_ - -#include "dawn_native/DepthStencilState.h" - -namespace dawn_native { namespace opengl { - - class Device; - class PersistentPipelineState; - - class DepthStencilState : public DepthStencilStateBase { - public: - DepthStencilState(DepthStencilStateBuilder* builder); - - void ApplyNow(PersistentPipelineState& persistentPipelineState) const; - }; - -}} // namespace dawn_native::opengl - -#endif // DAWNNATIVE_OPENGL_DEPTHSTENCILSTATEGL_H_ diff --git a/src/dawn_native/opengl/DeviceGL.cpp b/src/dawn_native/opengl/DeviceGL.cpp index fec30040c4..7afd520a82 100644 --- a/src/dawn_native/opengl/DeviceGL.cpp +++ b/src/dawn_native/opengl/DeviceGL.cpp @@ -21,7 +21,6 @@ #include "dawn_native/opengl/BufferGL.h" #include "dawn_native/opengl/CommandBufferGL.h" #include "dawn_native/opengl/ComputePipelineGL.h" -#include "dawn_native/opengl/DepthStencilStateGL.h" #include "dawn_native/opengl/InputStateGL.h" #include "dawn_native/opengl/PipelineLayoutGL.h" #include "dawn_native/opengl/QueueGL.h" @@ -78,9 +77,6 @@ namespace dawn_native { namespace opengl { const ComputePipelineDescriptor* descriptor) { return new ComputePipeline(this, descriptor); } - DepthStencilStateBase* Device::CreateDepthStencilState(DepthStencilStateBuilder* builder) { - return new DepthStencilState(builder); - } InputStateBase* Device::CreateInputState(InputStateBuilder* builder) { return new InputState(builder); } diff --git a/src/dawn_native/opengl/DeviceGL.h b/src/dawn_native/opengl/DeviceGL.h index 8216035542..b32d269e2c 100644 --- a/src/dawn_native/opengl/DeviceGL.h +++ b/src/dawn_native/opengl/DeviceGL.h @@ -41,7 +41,6 @@ namespace dawn_native { namespace opengl { // Dawn API CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override; - DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) override; InputStateBase* CreateInputState(InputStateBuilder* builder) override; RenderPassDescriptorBase* CreateRenderPassDescriptor( RenderPassDescriptorBuilder* builder) override; diff --git a/src/dawn_native/opengl/Forward.h b/src/dawn_native/opengl/Forward.h index 1c5942fcbc..49c381c698 100644 --- a/src/dawn_native/opengl/Forward.h +++ b/src/dawn_native/opengl/Forward.h @@ -30,7 +30,6 @@ namespace dawn_native { namespace opengl { class Buffer; class CommandBuffer; class ComputePipeline; - class DepthStencilState; class Device; class InputState; class PersistentPipelineState; @@ -50,7 +49,6 @@ namespace dawn_native { namespace opengl { using BufferType = Buffer; using CommandBufferType = CommandBuffer; using ComputePipelineType = ComputePipeline; - using DepthStencilStateType = DepthStencilState; using DeviceType = Device; using InputStateType = InputState; using PipelineLayoutType = PipelineLayout; diff --git a/src/dawn_native/opengl/RenderPipelineGL.cpp b/src/dawn_native/opengl/RenderPipelineGL.cpp index fec187afd7..9d14fc4574 100644 --- a/src/dawn_native/opengl/RenderPipelineGL.cpp +++ b/src/dawn_native/opengl/RenderPipelineGL.cpp @@ -14,7 +14,6 @@ #include "dawn_native/opengl/RenderPipelineGL.h" -#include "dawn_native/opengl/DepthStencilStateGL.h" #include "dawn_native/opengl/DeviceGL.h" #include "dawn_native/opengl/Forward.h" #include "dawn_native/opengl/InputStateGL.h" @@ -109,7 +108,92 @@ namespace dawn_native { namespace opengl { descriptor->colorWriteMask & dawn::ColorWriteMask::Alpha); } - } // namespace + GLuint OpenGLCompareFunction(dawn::CompareFunction compareFunction) { + switch (compareFunction) { + case dawn::CompareFunction::Never: + return GL_NEVER; + case dawn::CompareFunction::Less: + return GL_LESS; + case dawn::CompareFunction::LessEqual: + return GL_LEQUAL; + case dawn::CompareFunction::Greater: + return GL_GREATER; + case dawn::CompareFunction::GreaterEqual: + return GL_GEQUAL; + case dawn::CompareFunction::NotEqual: + return GL_NOTEQUAL; + case dawn::CompareFunction::Equal: + return GL_EQUAL; + case dawn::CompareFunction::Always: + return GL_ALWAYS; + default: + UNREACHABLE(); + } + } + + GLuint OpenGLStencilOperation(dawn::StencilOperation stencilOperation) { + switch (stencilOperation) { + case dawn::StencilOperation::Keep: + return GL_KEEP; + case dawn::StencilOperation::Zero: + return GL_ZERO; + case dawn::StencilOperation::Replace: + return GL_REPLACE; + case dawn::StencilOperation::Invert: + return GL_INVERT; + case dawn::StencilOperation::IncrementClamp: + return GL_INCR; + case dawn::StencilOperation::DecrementClamp: + return GL_DECR; + case dawn::StencilOperation::IncrementWrap: + return GL_INCR_WRAP; + case dawn::StencilOperation::DecrementWrap: + return GL_DECR_WRAP; + default: + UNREACHABLE(); + } + } + + void ApplyDepthStencilState(const DepthStencilStateDescriptor* descriptor, + PersistentPipelineState* persistentPipelineState) { + // Depth writes only occur if depth is enabled + if (descriptor->depthCompare == dawn::CompareFunction::Always && + !descriptor->depthWriteEnabled) { + glDisable(GL_DEPTH_TEST); + } else { + glEnable(GL_DEPTH_TEST); + } + + if (descriptor->depthWriteEnabled) { + glDepthMask(GL_TRUE); + } else { + glDepthMask(GL_FALSE); + } + + glDepthFunc(OpenGLCompareFunction(descriptor->depthCompare)); + + if (StencilTestEnabled(descriptor)) { + glEnable(GL_STENCIL_TEST); + } else { + glDisable(GL_STENCIL_TEST); + } + + GLenum backCompareFunction = OpenGLCompareFunction(descriptor->back.compare); + GLenum frontCompareFunction = OpenGLCompareFunction(descriptor->front.compare); + persistentPipelineState->SetStencilFuncsAndMask( + backCompareFunction, frontCompareFunction, descriptor->stencilReadMask); + + glStencilOpSeparate(GL_BACK, OpenGLStencilOperation(descriptor->back.stencilFailOp), + OpenGLStencilOperation(descriptor->back.depthFailOp), + OpenGLStencilOperation(descriptor->back.passOp)); + glStencilOpSeparate(GL_FRONT, OpenGLStencilOperation(descriptor->front.stencilFailOp), + OpenGLStencilOperation(descriptor->front.depthFailOp), + OpenGLStencilOperation(descriptor->front.passOp)); + + glStencilMask(descriptor->stencilWriteMask); + } + + } // anonymous namespace RenderPipeline::RenderPipeline(Device* device, const RenderPipelineDescriptor* descriptor) : RenderPipelineBase(device, descriptor), @@ -131,8 +215,7 @@ namespace dawn_native { namespace opengl { auto inputState = ToBackend(GetInputState()); glBindVertexArray(inputState->GetVAO()); - auto depthStencilState = ToBackend(GetDepthStencilState()); - depthStencilState->ApplyNow(persistentPipelineState); + ApplyDepthStencilState(GetDepthStencilStateDescriptor(), &persistentPipelineState); for (uint32_t attachmentSlot : IterateBitSet(GetColorAttachmentsMask())) { ApplyBlendState(attachmentSlot, GetBlendStateDescriptor(attachmentSlot)); diff --git a/src/dawn_native/vulkan/DepthStencilStateVk.cpp b/src/dawn_native/vulkan/DepthStencilStateVk.cpp deleted file mode 100644 index 2f6472c91c..0000000000 --- a/src/dawn_native/vulkan/DepthStencilStateVk.cpp +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright 2018 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "dawn_native/vulkan/DepthStencilStateVk.h" - -#include "common/Assert.h" - -namespace dawn_native { namespace vulkan { - - namespace { - VkCompareOp VulkanCompareOp(dawn::CompareFunction op) { - switch (op) { - case dawn::CompareFunction::Always: - return VK_COMPARE_OP_ALWAYS; - case dawn::CompareFunction::Equal: - return VK_COMPARE_OP_EQUAL; - case dawn::CompareFunction::Greater: - return VK_COMPARE_OP_GREATER; - case dawn::CompareFunction::GreaterEqual: - return VK_COMPARE_OP_GREATER_OR_EQUAL; - case dawn::CompareFunction::Less: - return VK_COMPARE_OP_LESS; - case dawn::CompareFunction::LessEqual: - return VK_COMPARE_OP_LESS_OR_EQUAL; - case dawn::CompareFunction::Never: - return VK_COMPARE_OP_NEVER; - case dawn::CompareFunction::NotEqual: - return VK_COMPARE_OP_NOT_EQUAL; - default: - UNREACHABLE(); - } - } - - VkStencilOp VulkanStencilOp(dawn::StencilOperation op) { - switch (op) { - case dawn::StencilOperation::Keep: - return VK_STENCIL_OP_KEEP; - case dawn::StencilOperation::Zero: - return VK_STENCIL_OP_ZERO; - case dawn::StencilOperation::Replace: - return VK_STENCIL_OP_REPLACE; - case dawn::StencilOperation::IncrementClamp: - return VK_STENCIL_OP_INCREMENT_AND_CLAMP; - case dawn::StencilOperation::DecrementClamp: - return VK_STENCIL_OP_DECREMENT_AND_CLAMP; - case dawn::StencilOperation::Invert: - return VK_STENCIL_OP_INVERT; - case dawn::StencilOperation::IncrementWrap: - return VK_STENCIL_OP_INCREMENT_AND_WRAP; - case dawn::StencilOperation::DecrementWrap: - return VK_STENCIL_OP_DECREMENT_AND_WRAP; - default: - UNREACHABLE(); - } - } - - } // anonymous namespace - - DepthStencilState::DepthStencilState(DepthStencilStateBuilder* builder) - : DepthStencilStateBase(builder) { - mCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; - mCreateInfo.pNext = nullptr; - mCreateInfo.flags = 0; - - const auto& depth = GetDepth(); - // Depth writes only occur if depth is enabled - mCreateInfo.depthTestEnable = - (depth.compareFunction == dawn::CompareFunction::Always && !depth.depthWriteEnabled) - ? VK_FALSE - : VK_TRUE; - mCreateInfo.depthWriteEnable = depth.depthWriteEnabled ? VK_TRUE : VK_FALSE; - mCreateInfo.depthCompareOp = VulkanCompareOp(depth.compareFunction); - mCreateInfo.depthBoundsTestEnable = false; - mCreateInfo.minDepthBounds = 0.0f; - mCreateInfo.maxDepthBounds = 1.0f; - - const auto& stencil = GetStencil(); - mCreateInfo.stencilTestEnable = StencilTestEnabled() ? VK_TRUE : VK_FALSE; - - mCreateInfo.front.failOp = VulkanStencilOp(stencil.front.stencilFailOp); - mCreateInfo.front.passOp = VulkanStencilOp(stencil.front.passOp); - mCreateInfo.front.depthFailOp = VulkanStencilOp(stencil.front.depthFailOp); - mCreateInfo.front.compareOp = VulkanCompareOp(stencil.front.compare); - - mCreateInfo.back.failOp = VulkanStencilOp(stencil.back.stencilFailOp); - mCreateInfo.back.passOp = VulkanStencilOp(stencil.back.passOp); - mCreateInfo.back.depthFailOp = VulkanStencilOp(stencil.back.depthFailOp); - mCreateInfo.back.compareOp = VulkanCompareOp(stencil.back.compare); - - // Dawn doesn't have separate front and back stencil masks. - mCreateInfo.front.compareMask = stencil.readMask; - mCreateInfo.back.compareMask = stencil.readMask; - mCreateInfo.front.writeMask = stencil.writeMask; - mCreateInfo.back.writeMask = stencil.writeMask; - - // The stencil reference is always dynamic - mCreateInfo.front.reference = 0; - mCreateInfo.back.reference = 0; - } - - const VkPipelineDepthStencilStateCreateInfo* DepthStencilState::GetCreateInfo() const { - return &mCreateInfo; - } - -}} // namespace dawn_native::vulkan diff --git a/src/dawn_native/vulkan/DepthStencilStateVk.h b/src/dawn_native/vulkan/DepthStencilStateVk.h deleted file mode 100644 index 1f9a2a5221..0000000000 --- a/src/dawn_native/vulkan/DepthStencilStateVk.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2018 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef DAWNNATIVE_VULKAN_DEPTHSTENCILSTATEVK_H_ -#define DAWNNATIVE_VULKAN_DEPTHSTENCILSTATEVK_H_ - -#include "dawn_native/DepthStencilState.h" - -#include "common/vulkan_platform.h" - -namespace dawn_native { namespace vulkan { - - class Device; - - // Pre-computes the depth-stencil configuration to give to a graphics pipeline create info. - class DepthStencilState : public DepthStencilStateBase { - public: - DepthStencilState(DepthStencilStateBuilder* builder); - - const VkPipelineDepthStencilStateCreateInfo* GetCreateInfo() const; - - private: - VkPipelineDepthStencilStateCreateInfo mCreateInfo; - }; - -}} // namespace dawn_native::vulkan - -#endif // DAWNNATIVE_VULKAN_DEPTHSTENCILSTATEVK_H_ diff --git a/src/dawn_native/vulkan/DeviceVk.cpp b/src/dawn_native/vulkan/DeviceVk.cpp index 3e78b5e4a5..1a33e612c2 100644 --- a/src/dawn_native/vulkan/DeviceVk.cpp +++ b/src/dawn_native/vulkan/DeviceVk.cpp @@ -25,7 +25,6 @@ #include "dawn_native/vulkan/BufferVk.h" #include "dawn_native/vulkan/CommandBufferVk.h" #include "dawn_native/vulkan/ComputePipelineVk.h" -#include "dawn_native/vulkan/DepthStencilStateVk.h" #include "dawn_native/vulkan/FencedDeleter.h" #include "dawn_native/vulkan/InputStateVk.h" #include "dawn_native/vulkan/NativeSwapChainImplVk.h" @@ -237,9 +236,6 @@ namespace dawn_native { namespace vulkan { const ComputePipelineDescriptor* descriptor) { return new ComputePipeline(this, descriptor); } - DepthStencilStateBase* Device::CreateDepthStencilState(DepthStencilStateBuilder* builder) { - return new DepthStencilState(builder); - } InputStateBase* Device::CreateInputState(InputStateBuilder* builder) { return new InputState(builder); } diff --git a/src/dawn_native/vulkan/DeviceVk.h b/src/dawn_native/vulkan/DeviceVk.h index 086b70642c..4ff27d41f9 100644 --- a/src/dawn_native/vulkan/DeviceVk.h +++ b/src/dawn_native/vulkan/DeviceVk.h @@ -64,7 +64,6 @@ namespace dawn_native { namespace vulkan { // Dawn API CommandBufferBase* CreateCommandBuffer(CommandBufferBuilder* builder) override; - DepthStencilStateBase* CreateDepthStencilState(DepthStencilStateBuilder* builder) override; InputStateBase* CreateInputState(InputStateBuilder* builder) override; RenderPassDescriptorBase* CreateRenderPassDescriptor( RenderPassDescriptorBuilder* builder) override; diff --git a/src/dawn_native/vulkan/Forward.h b/src/dawn_native/vulkan/Forward.h index 3e43e326d6..36b35cec5f 100644 --- a/src/dawn_native/vulkan/Forward.h +++ b/src/dawn_native/vulkan/Forward.h @@ -24,7 +24,6 @@ namespace dawn_native { namespace vulkan { class Buffer; class CommandBuffer; class ComputePipeline; - class DepthStencilState; class Device; class InputState; class PipelineLayout; @@ -43,7 +42,6 @@ namespace dawn_native { namespace vulkan { using BufferType = Buffer; using CommandBufferType = CommandBuffer; using ComputePipelineType = ComputePipeline; - using DepthStencilStateType = DepthStencilState; using DeviceType = Device; using InputStateType = InputState; using PipelineLayoutType = PipelineLayout; diff --git a/src/dawn_native/vulkan/RenderPipelineVk.cpp b/src/dawn_native/vulkan/RenderPipelineVk.cpp index a1ef01476d..481ab1b057 100644 --- a/src/dawn_native/vulkan/RenderPipelineVk.cpp +++ b/src/dawn_native/vulkan/RenderPipelineVk.cpp @@ -14,7 +14,6 @@ #include "dawn_native/vulkan/RenderPipelineVk.h" -#include "dawn_native/vulkan/DepthStencilStateVk.h" #include "dawn_native/vulkan/DeviceVk.h" #include "dawn_native/vulkan/FencedDeleter.h" #include "dawn_native/vulkan/InputStateVk.h" @@ -126,6 +125,97 @@ namespace dawn_native { namespace vulkan { return attachment; } + VkCompareOp VulkanCompareOp(dawn::CompareFunction op) { + switch (op) { + case dawn::CompareFunction::Always: + return VK_COMPARE_OP_ALWAYS; + case dawn::CompareFunction::Equal: + return VK_COMPARE_OP_EQUAL; + case dawn::CompareFunction::Greater: + return VK_COMPARE_OP_GREATER; + case dawn::CompareFunction::GreaterEqual: + return VK_COMPARE_OP_GREATER_OR_EQUAL; + case dawn::CompareFunction::Less: + return VK_COMPARE_OP_LESS; + case dawn::CompareFunction::LessEqual: + return VK_COMPARE_OP_LESS_OR_EQUAL; + case dawn::CompareFunction::Never: + return VK_COMPARE_OP_NEVER; + case dawn::CompareFunction::NotEqual: + return VK_COMPARE_OP_NOT_EQUAL; + default: + UNREACHABLE(); + } + } + + VkStencilOp VulkanStencilOp(dawn::StencilOperation op) { + switch (op) { + case dawn::StencilOperation::Keep: + return VK_STENCIL_OP_KEEP; + case dawn::StencilOperation::Zero: + return VK_STENCIL_OP_ZERO; + case dawn::StencilOperation::Replace: + return VK_STENCIL_OP_REPLACE; + case dawn::StencilOperation::IncrementClamp: + return VK_STENCIL_OP_INCREMENT_AND_CLAMP; + case dawn::StencilOperation::DecrementClamp: + return VK_STENCIL_OP_DECREMENT_AND_CLAMP; + case dawn::StencilOperation::Invert: + return VK_STENCIL_OP_INVERT; + case dawn::StencilOperation::IncrementWrap: + return VK_STENCIL_OP_INCREMENT_AND_WRAP; + case dawn::StencilOperation::DecrementWrap: + return VK_STENCIL_OP_DECREMENT_AND_WRAP; + default: + UNREACHABLE(); + } + } + + VkPipelineDepthStencilStateCreateInfo ComputeDepthStencilDesc( + const DepthStencilStateDescriptor* descriptor) { + VkPipelineDepthStencilStateCreateInfo depthStencilState; + depthStencilState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; + depthStencilState.pNext = nullptr; + depthStencilState.flags = 0; + + // Depth writes only occur if depth is enabled + depthStencilState.depthTestEnable = + (descriptor->depthCompare == dawn::CompareFunction::Always && + !descriptor->depthWriteEnabled) + ? VK_FALSE + : VK_TRUE; + depthStencilState.depthWriteEnable = descriptor->depthWriteEnabled ? VK_TRUE : VK_FALSE; + depthStencilState.depthCompareOp = VulkanCompareOp(descriptor->depthCompare); + depthStencilState.depthBoundsTestEnable = false; + depthStencilState.minDepthBounds = 0.0f; + depthStencilState.maxDepthBounds = 1.0f; + + depthStencilState.stencilTestEnable = + StencilTestEnabled(descriptor) ? VK_TRUE : VK_FALSE; + + depthStencilState.front.failOp = VulkanStencilOp(descriptor->front.stencilFailOp); + depthStencilState.front.passOp = VulkanStencilOp(descriptor->front.passOp); + depthStencilState.front.depthFailOp = VulkanStencilOp(descriptor->front.depthFailOp); + depthStencilState.front.compareOp = VulkanCompareOp(descriptor->front.compare); + + depthStencilState.back.failOp = VulkanStencilOp(descriptor->back.stencilFailOp); + depthStencilState.back.passOp = VulkanStencilOp(descriptor->back.passOp); + depthStencilState.back.depthFailOp = VulkanStencilOp(descriptor->back.depthFailOp); + depthStencilState.back.compareOp = VulkanCompareOp(descriptor->back.compare); + + // Dawn doesn't have separate front and back stencil masks. + depthStencilState.front.compareMask = descriptor->stencilReadMask; + depthStencilState.back.compareMask = descriptor->stencilReadMask; + depthStencilState.front.writeMask = descriptor->stencilWriteMask; + depthStencilState.back.writeMask = descriptor->stencilWriteMask; + + // The stencil reference is always dynamic + depthStencilState.front.reference = 0; + depthStencilState.back.reference = 0; + + return depthStencilState; + } + } // anonymous namespace RenderPipeline::RenderPipeline(Device* device, const RenderPipelineDescriptor* descriptor) @@ -210,6 +300,9 @@ namespace dawn_native { namespace vulkan { multisample.alphaToCoverageEnable = VK_FALSE; multisample.alphaToOneEnable = VK_FALSE; + VkPipelineDepthStencilStateCreateInfo depthStencilState = + ComputeDepthStencilDesc(GetDepthStencilStateDescriptor()); + // Initialize the "blend state info" that will be chained in the "create info" from the data // pre-computed in the BlendState std::array colorBlendAttachments; @@ -279,7 +372,7 @@ namespace dawn_native { namespace vulkan { createInfo.pViewportState = &viewport; createInfo.pRasterizationState = &rasterization; createInfo.pMultisampleState = &multisample; - createInfo.pDepthStencilState = ToBackend(GetDepthStencilState())->GetCreateInfo(); + createInfo.pDepthStencilState = &depthStencilState; createInfo.pColorBlendState = &colorBlend; createInfo.pDynamicState = &dynamic; createInfo.layout = ToBackend(GetLayout())->GetHandle(); diff --git a/src/tests/end2end/DepthStencilStateTests.cpp b/src/tests/end2end/DepthStencilStateTests.cpp index e8782be4d5..e68d447904 100644 --- a/src/tests/end2end/DepthStencilStateTests.cpp +++ b/src/tests/end2end/DepthStencilStateTests.cpp @@ -110,7 +110,7 @@ class DepthStencilStateTest : public DawnTest { } struct TestSpec { - const dawn::DepthStencilState& depthStencilState; + const dawn::DepthStencilStateDescriptor& depthStencilState; RGBA8 color; float depth; uint32_t stencil; @@ -119,15 +119,27 @@ class DepthStencilStateTest : public DawnTest { // Check whether a depth comparison function works as expected // The less, equal, greater booleans denote wether the respective triangle should be visible based on the comparison function void CheckDepthCompareFunction(dawn::CompareFunction compareFunction, bool less, bool equal, bool greater) { - dawn::DepthStencilState baseState = device.CreateDepthStencilStateBuilder() - .SetDepthCompareFunction(dawn::CompareFunction::Always) - .SetDepthWriteEnabled(true) - .GetResult(); + dawn::StencilStateFaceDescriptor stencilFace; + stencilFace.compare = dawn::CompareFunction::Always; + stencilFace.stencilFailOp = dawn::StencilOperation::Keep; + stencilFace.depthFailOp = dawn::StencilOperation::Keep; + stencilFace.passOp = dawn::StencilOperation::Keep; - dawn::DepthStencilState state = device.CreateDepthStencilStateBuilder() - .SetDepthCompareFunction(compareFunction) - .SetDepthWriteEnabled(true) - .GetResult(); + dawn::DepthStencilStateDescriptor baseState; + baseState.depthWriteEnabled = true; + baseState.depthCompare = dawn::CompareFunction::Always; + baseState.back = stencilFace; + baseState.front = stencilFace; + baseState.stencilReadMask = 0xff; + baseState.stencilWriteMask = 0xff; + + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = true; + state.depthCompare = compareFunction; + state.back = stencilFace; + state.front = stencilFace; + state.stencilReadMask = 0xff; + state.stencilWriteMask = 0xff; RGBA8 baseColor = RGBA8(255, 255, 255, 255); RGBA8 lessColor = RGBA8(255, 0, 0, 255); @@ -155,20 +167,26 @@ class DepthStencilStateTest : public DawnTest { baseStencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState baseState = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &baseStencilFaceDescriptor) - .GetResult(); + dawn::DepthStencilStateDescriptor baseState; + baseState.depthWriteEnabled = false; + baseState.depthCompare = dawn::CompareFunction::Always; + baseState.back = baseStencilFaceDescriptor; + baseState.front = baseStencilFaceDescriptor; + baseState.stencilReadMask = 0xff; + baseState.stencilWriteMask = 0xff; dawn::StencilStateFaceDescriptor stencilFaceDescriptor; stencilFaceDescriptor.compare = compareFunction; stencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.passOp = dawn::StencilOperation::Keep; - dawn::DepthStencilState state = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &stencilFaceDescriptor) - .GetResult(); + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = false; + state.depthCompare = dawn::CompareFunction::Always; + state.back = stencilFaceDescriptor; + state.front = stencilFaceDescriptor; + state.stencilReadMask = 0xff; + state.stencilWriteMask = 0xff; RGBA8 baseColor = RGBA8(255, 255, 255, 255); RGBA8 lessColor = RGBA8(255, 0, 0, 255); @@ -195,20 +213,26 @@ class DepthStencilStateTest : public DawnTest { baseStencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState baseState = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &baseStencilFaceDescriptor) - .GetResult(); + dawn::DepthStencilStateDescriptor baseState; + baseState.depthWriteEnabled = false; + baseState.depthCompare = dawn::CompareFunction::Always; + baseState.back = baseStencilFaceDescriptor; + baseState.front = baseStencilFaceDescriptor; + baseState.stencilReadMask = 0xff; + baseState.stencilWriteMask = 0xff; dawn::StencilStateFaceDescriptor stencilFaceDescriptor; stencilFaceDescriptor.compare = dawn::CompareFunction::Always; stencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.passOp = stencilOperation; - dawn::DepthStencilState state = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &stencilFaceDescriptor) - .GetResult(); + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = false; + state.depthCompare = dawn::CompareFunction::Always; + state.back = stencilFaceDescriptor; + state.front = stencilFaceDescriptor; + state.stencilReadMask = 0xff; + state.stencilWriteMask = 0xff; CheckStencil({ // Wipe the stencil buffer with the initialStencil value @@ -226,10 +250,13 @@ class DepthStencilStateTest : public DawnTest { stencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.passOp = dawn::StencilOperation::Keep; - dawn::DepthStencilState state = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &stencilFaceDescriptor) - .GetResult(); + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = false; + state.depthCompare = dawn::CompareFunction::Always; + state.back = stencilFaceDescriptor; + state.front = stencilFaceDescriptor; + state.stencilReadMask = 0xff; + state.stencilWriteMask = 0xff; testParams.push_back({ state, RGBA8(0, 255, 0, 255), 0, expectedStencil }); DoTest(testParams, RGBA8(0, 255, 0, 255)); @@ -268,7 +295,7 @@ class DepthStencilStateTest : public DawnTest { descriptor.cFragmentStage.module = fsModule; descriptor.cAttachmentsState.hasDepthStencilAttachment = true; descriptor.cDepthStencilAttachment.format = dawn::TextureFormat::D32FloatS8Uint; - descriptor.depthStencilState = test.depthStencilState; + descriptor.depthStencilState = &test.depthStencilState; dawn::RenderPipeline pipeline = device.CreateRenderPipeline(&descriptor); @@ -303,8 +330,19 @@ class DepthStencilStateTest : public DawnTest { // Test compilation and usage of the fixture TEST_P(DepthStencilStateTest, Basic) { - dawn::DepthStencilState state = device.CreateDepthStencilStateBuilder() - .GetResult(); + dawn::StencilStateFaceDescriptor stencilFace; + stencilFace.compare = dawn::CompareFunction::Always; + stencilFace.stencilFailOp = dawn::StencilOperation::Keep; + stencilFace.depthFailOp = dawn::StencilOperation::Keep; + stencilFace.passOp = dawn::StencilOperation::Keep; + + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = false; + state.depthCompare = dawn::CompareFunction::Always; + state.back = stencilFace; + state.front = stencilFace; + state.stencilReadMask = 0xff; + state.stencilWriteMask = 0xff; DoTest({ { state, RGBA8(0, 255, 0, 255), 0.5f, 0u }, @@ -313,8 +351,19 @@ TEST_P(DepthStencilStateTest, Basic) { // Test defaults: depth and stencil tests disabled TEST_P(DepthStencilStateTest, DepthStencilDisabled) { - dawn::DepthStencilState state = device.CreateDepthStencilStateBuilder() - .GetResult(); + dawn::StencilStateFaceDescriptor stencilFace; + stencilFace.compare = dawn::CompareFunction::Always; + stencilFace.stencilFailOp = dawn::StencilOperation::Keep; + stencilFace.depthFailOp = dawn::StencilOperation::Keep; + stencilFace.passOp = dawn::StencilOperation::Keep; + + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = false; + state.depthCompare = dawn::CompareFunction::Always; + state.back = stencilFace; + state.front = stencilFace; + state.stencilReadMask = 0xff; + state.stencilWriteMask = 0xff; TestSpec specs[3] = { { state, RGBA8(255, 0, 0, 255), 0.0f, 0u }, @@ -367,19 +416,35 @@ TEST_P(DepthStencilStateTest, DepthNotEqual) { // Test that disabling depth writes works and leaves the depth buffer unchanged TEST_P(DepthStencilStateTest, DepthWriteDisabled) { - dawn::DepthStencilState baseState = device.CreateDepthStencilStateBuilder() - .SetDepthCompareFunction(dawn::CompareFunction::Always) - .SetDepthWriteEnabled(true) - .GetResult(); + dawn::StencilStateFaceDescriptor stencilFace; + stencilFace.compare = dawn::CompareFunction::Always; + stencilFace.stencilFailOp = dawn::StencilOperation::Keep; + stencilFace.depthFailOp = dawn::StencilOperation::Keep; + stencilFace.passOp = dawn::StencilOperation::Keep; - dawn::DepthStencilState noDepthWrite = device.CreateDepthStencilStateBuilder() - .SetDepthCompareFunction(dawn::CompareFunction::Always) - .SetDepthWriteEnabled(false) - .GetResult(); + dawn::DepthStencilStateDescriptor baseState; + baseState.depthWriteEnabled = true; + baseState.depthCompare = dawn::CompareFunction::Always; + baseState.back = stencilFace; + baseState.front = stencilFace; + baseState.stencilReadMask = 0xff; + baseState.stencilWriteMask = 0xff; - dawn::DepthStencilState checkState = device.CreateDepthStencilStateBuilder() - .SetDepthCompareFunction(dawn::CompareFunction::Equal) - .GetResult(); + dawn::DepthStencilStateDescriptor noDepthWrite; + noDepthWrite.depthWriteEnabled = false; + noDepthWrite.depthCompare = dawn::CompareFunction::Always; + noDepthWrite.back = stencilFace; + noDepthWrite.front = stencilFace; + noDepthWrite.stencilReadMask = 0xff; + noDepthWrite.stencilWriteMask = 0xff; + + dawn::DepthStencilStateDescriptor checkState; + checkState.depthWriteEnabled = false; + checkState.depthCompare = dawn::CompareFunction::Equal; + checkState.back = stencilFace; + checkState.front = stencilFace; + checkState.stencilReadMask = 0xff; + checkState.stencilWriteMask = 0xff; DoTest({ { baseState, RGBA8(255, 255, 255, 255), 1.f, 0u }, // Draw a base triangle with depth enabled @@ -465,21 +530,26 @@ TEST_P(DepthStencilStateTest, StencilReadMask) { baseStencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState baseState = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &baseStencilFaceDescriptor) - .GetResult(); + dawn::DepthStencilStateDescriptor baseState; + baseState.depthWriteEnabled = false; + baseState.depthCompare = dawn::CompareFunction::Always; + baseState.back = baseStencilFaceDescriptor; + baseState.front = baseStencilFaceDescriptor; + baseState.stencilReadMask = 0xff; + baseState.stencilWriteMask = 0xff; dawn::StencilStateFaceDescriptor stencilFaceDescriptor; stencilFaceDescriptor.compare = dawn::CompareFunction::Equal; stencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.passOp = dawn::StencilOperation::Keep; - dawn::DepthStencilState state = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &stencilFaceDescriptor) - .SetStencilMask(0x2, 0xff) - .GetResult(); + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = false; + state.depthCompare = dawn::CompareFunction::Always; + state.back = stencilFaceDescriptor; + state.front = stencilFaceDescriptor; + state.stencilReadMask = 0x2; + state.stencilWriteMask = 0xff; RGBA8 baseColor = RGBA8(255, 255, 255, 255); RGBA8 red = RGBA8(255, 0, 0, 255); @@ -497,21 +567,26 @@ TEST_P(DepthStencilStateTest, StencilWriteMask) { baseStencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState baseState = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &baseStencilFaceDescriptor) - .SetStencilMask(0xff, 0x1) - .GetResult(); + dawn::DepthStencilStateDescriptor baseState; + baseState.depthWriteEnabled = false; + baseState.depthCompare = dawn::CompareFunction::Always; + baseState.back = baseStencilFaceDescriptor; + baseState.front = baseStencilFaceDescriptor; + baseState.stencilReadMask = 0xff; + baseState.stencilWriteMask = 0x1; dawn::StencilStateFaceDescriptor stencilFaceDescriptor; stencilFaceDescriptor.compare = dawn::CompareFunction::Equal; stencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.passOp = dawn::StencilOperation::Keep; - dawn::DepthStencilState state = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &stencilFaceDescriptor) - .GetResult(); + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = false; + state.depthCompare = dawn::CompareFunction::Always; + state.back = stencilFaceDescriptor; + state.front = stencilFaceDescriptor; + state.stencilReadMask = 0xff; + state.stencilWriteMask = 0xff; RGBA8 baseColor = RGBA8(255, 255, 255, 255); RGBA8 green = RGBA8(0, 255, 0, 255); @@ -528,20 +603,26 @@ TEST_P(DepthStencilStateTest, StencilFail) { baseStencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState baseState = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &baseStencilFaceDescriptor) - .GetResult(); + dawn::DepthStencilStateDescriptor baseState; + baseState.depthWriteEnabled = false; + baseState.depthCompare = dawn::CompareFunction::Always; + baseState.back = baseStencilFaceDescriptor; + baseState.front = baseStencilFaceDescriptor; + baseState.stencilReadMask = 0xff; + baseState.stencilWriteMask = 0xff; dawn::StencilStateFaceDescriptor stencilFaceDescriptor; stencilFaceDescriptor.compare = dawn::CompareFunction::Less; stencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Replace; stencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.passOp = dawn::StencilOperation::Keep; - dawn::DepthStencilState state = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &stencilFaceDescriptor) - .GetResult(); + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = false; + state.depthCompare = dawn::CompareFunction::Always; + state.back = stencilFaceDescriptor; + state.front = stencilFaceDescriptor; + state.stencilReadMask = 0xff; + state.stencilWriteMask = 0xff; CheckStencil({ { baseState, RGBA8(255, 255, 255, 255), 1.f, 1 }, // Triangle to set stencil value to 1 @@ -556,23 +637,26 @@ TEST_P(DepthStencilStateTest, StencilDepthFail) { baseStencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState baseState = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &baseStencilFaceDescriptor) - .SetDepthWriteEnabled(true) - .GetResult(); + dawn::DepthStencilStateDescriptor baseState; + baseState.depthWriteEnabled = true; + baseState.depthCompare = dawn::CompareFunction::Always; + baseState.back = baseStencilFaceDescriptor; + baseState.front = baseStencilFaceDescriptor; + baseState.stencilReadMask = 0xff; + baseState.stencilWriteMask = 0xff; dawn::StencilStateFaceDescriptor stencilFaceDescriptor; stencilFaceDescriptor.compare = dawn::CompareFunction::Greater; stencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Replace; stencilFaceDescriptor.passOp = dawn::StencilOperation::Keep; - dawn::DepthStencilState state = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &stencilFaceDescriptor) - .SetDepthWriteEnabled(true) - .SetDepthCompareFunction(dawn::CompareFunction::Less) - .GetResult(); + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = true; + state.depthCompare = dawn::CompareFunction::Less; + state.back = stencilFaceDescriptor; + state.front = stencilFaceDescriptor; + state.stencilReadMask = 0xff; + state.stencilWriteMask = 0xff; CheckStencil({ { baseState, RGBA8(255, 255, 255, 255), 0.f, 1 }, // Triangle to set stencil value to 1. Depth is 0 @@ -587,23 +671,26 @@ TEST_P(DepthStencilStateTest, StencilDepthPass) { baseStencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; baseStencilFaceDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState baseState = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &baseStencilFaceDescriptor) - .SetDepthWriteEnabled(true) - .GetResult(); + dawn::DepthStencilStateDescriptor baseState; + baseState.depthWriteEnabled = true; + baseState.depthCompare = dawn::CompareFunction::Always; + baseState.back = baseStencilFaceDescriptor; + baseState.front = baseStencilFaceDescriptor; + baseState.stencilReadMask = 0xff; + baseState.stencilWriteMask = 0xff; dawn::StencilStateFaceDescriptor stencilFaceDescriptor; stencilFaceDescriptor.compare = dawn::CompareFunction::Greater; stencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; stencilFaceDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState state = - device.CreateDepthStencilStateBuilder() - .SetStencilFunction(dawn::Face::Both, &stencilFaceDescriptor) - .SetDepthWriteEnabled(true) - .SetDepthCompareFunction(dawn::CompareFunction::Less) - .GetResult(); + dawn::DepthStencilStateDescriptor state; + state.depthWriteEnabled = true; + state.depthCompare = dawn::CompareFunction::Less; + state.back = stencilFaceDescriptor; + state.front = stencilFaceDescriptor; + state.stencilReadMask = 0xff; + state.stencilWriteMask = 0xff; CheckStencil({ { baseState, RGBA8(255, 255, 255, 255), 1.f, 1 }, // Triangle to set stencil value to 1. Depth is 0 diff --git a/src/tests/unittests/WireTests.cpp b/src/tests/unittests/WireTests.cpp index 68122711b4..aa08fea1eb 100644 --- a/src/tests/unittests/WireTests.cpp +++ b/src/tests/unittests/WireTests.cpp @@ -350,15 +350,20 @@ TEST_F(WireTests, CStringArgument) { .WillOnce(Return(apiInputState)); // Create the depth-stencil state - dawnDepthStencilStateBuilder depthStencilStateBuilder = dawnDeviceCreateDepthStencilStateBuilder(device); - dawnDepthStencilStateBuilder apiDepthStencilStateBuilder = api.GetNewDepthStencilStateBuilder(); - EXPECT_CALL(api, DeviceCreateDepthStencilStateBuilder(apiDevice)) - .WillOnce(Return(apiDepthStencilStateBuilder)); + dawnStencilStateFaceDescriptor stencilFace; + stencilFace.compare = DAWN_COMPARE_FUNCTION_ALWAYS; + stencilFace.stencilFailOp = DAWN_STENCIL_OPERATION_KEEP; + stencilFace.depthFailOp = DAWN_STENCIL_OPERATION_KEEP; + stencilFace.passOp = DAWN_STENCIL_OPERATION_KEEP; - dawnDepthStencilState depthStencilState = dawnDepthStencilStateBuilderGetResult(depthStencilStateBuilder); - dawnDepthStencilState apiDepthStencilState = api.GetNewDepthStencilState(); - EXPECT_CALL(api, DepthStencilStateBuilderGetResult(apiDepthStencilStateBuilder)) - .WillOnce(Return(apiDepthStencilState)); + dawnDepthStencilStateDescriptor depthStencilState; + depthStencilState.nextInChain = nullptr; + depthStencilState.depthWriteEnabled = false; + depthStencilState.depthCompare = DAWN_COMPARE_FUNCTION_ALWAYS; + depthStencilState.back = stencilFace; + depthStencilState.front = stencilFace; + depthStencilState.stencilReadMask = 0xff; + depthStencilState.stencilWriteMask = 0xff; // Create the pipeline layout dawnPipelineLayoutDescriptor layoutDescriptor; @@ -405,7 +410,7 @@ TEST_F(WireTests, CStringArgument) { pipelineDescriptor.inputState = inputState; pipelineDescriptor.indexFormat = DAWN_INDEX_FORMAT_UINT32; pipelineDescriptor.primitiveTopology = DAWN_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; - pipelineDescriptor.depthStencilState = depthStencilState; + pipelineDescriptor.depthStencilState = &depthStencilState; dawnDeviceCreateRenderPipeline(device, &pipelineDescriptor); EXPECT_CALL(api, DeviceCreateRenderPipeline(apiDevice, MatchesLambda([](const dawnRenderPipelineDescriptor* desc) -> bool { diff --git a/src/tests/unittests/validation/DepthStencilStateValidationTests.cpp b/src/tests/unittests/validation/DepthStencilStateValidationTests.cpp deleted file mode 100644 index f2639f9fc7..0000000000 --- a/src/tests/unittests/validation/DepthStencilStateValidationTests.cpp +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright 2017 The Dawn Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "tests/unittests/validation/ValidationTest.h" - -class DepthStencilStateValidationTest : public ValidationTest { -}; - -// Test cases where creation should succeed -TEST_F(DepthStencilStateValidationTest, CreationSuccess) { - // Success for setting all properties - { - dawn::StencilStateFaceDescriptor stencilFaceDescriptor; - stencilFaceDescriptor.compare = dawn::CompareFunction::Greater; - stencilFaceDescriptor.stencilFailOp = dawn::StencilOperation::Keep; - stencilFaceDescriptor.depthFailOp = dawn::StencilOperation::Keep; - stencilFaceDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState ds = - AssertWillBeSuccess(device.CreateDepthStencilStateBuilder()) - .SetDepthCompareFunction(dawn::CompareFunction::Less) - .SetDepthWriteEnabled(true) - .SetStencilFunction(dawn::Face::Both, &stencilFaceDescriptor) - .SetStencilMask(0x0, 0x1) - .GetResult(); - } - - // Success for empty builder - { - dawn::DepthStencilState ds = AssertWillBeSuccess(device.CreateDepthStencilStateBuilder()) - .GetResult(); - } - - // Test success when setting stencil function on separate faces - { - dawn::StencilStateFaceDescriptor stencilFrontDescriptor; - stencilFrontDescriptor.compare = dawn::CompareFunction::Less; - stencilFrontDescriptor.stencilFailOp = dawn::StencilOperation::Replace; - stencilFrontDescriptor.depthFailOp = dawn::StencilOperation::Replace; - stencilFrontDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::StencilStateFaceDescriptor stencilBackDescriptor; - stencilBackDescriptor.compare = dawn::CompareFunction::Greater; - stencilBackDescriptor.stencilFailOp = dawn::StencilOperation::Replace; - stencilBackDescriptor.depthFailOp = dawn::StencilOperation::Replace; - stencilBackDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState ds = - AssertWillBeSuccess(device.CreateDepthStencilStateBuilder()) - .SetStencilFunction(dawn::Face::Front, &stencilFrontDescriptor) - .SetStencilFunction(dawn::Face::Back, &stencilBackDescriptor) - .GetResult(); - } -} - -// Test creation failure when specifying properties multiple times -TEST_F(DepthStencilStateValidationTest, CreationDuplicates) { - // Test failure when specifying depth write enabled multiple times - { - dawn::DepthStencilState ds = AssertWillBeError(device.CreateDepthStencilStateBuilder()) - .SetDepthWriteEnabled(true) - .SetDepthWriteEnabled(false) - .GetResult(); - } - - // Test failure when specifying depth compare function multiple times - { - dawn::DepthStencilState ds = AssertWillBeError(device.CreateDepthStencilStateBuilder()) - .SetDepthCompareFunction(dawn::CompareFunction::Less) - .SetDepthCompareFunction(dawn::CompareFunction::Greater) - .GetResult(); - } - - // Test failure when setting stencil mask multiple times - { - dawn::DepthStencilState ds = AssertWillBeError(device.CreateDepthStencilStateBuilder()) - .SetStencilMask(0x00, 0x00) - .SetStencilMask(0xff, 0xff) - .GetResult(); - } - - // Test failure when directly setting stencil function on a face multiple times - { - dawn::StencilStateFaceDescriptor stencilBackDescriptor1; - stencilBackDescriptor1.compare = dawn::CompareFunction::Less; - stencilBackDescriptor1.stencilFailOp = dawn::StencilOperation::Replace; - stencilBackDescriptor1.depthFailOp = dawn::StencilOperation::Replace; - stencilBackDescriptor1.passOp = dawn::StencilOperation::Replace; - dawn::StencilStateFaceDescriptor stencilBackDescriptor2; - stencilBackDescriptor2.compare = dawn::CompareFunction::Greater; - stencilBackDescriptor2.stencilFailOp = dawn::StencilOperation::Replace; - stencilBackDescriptor2.depthFailOp = dawn::StencilOperation::Replace; - stencilBackDescriptor2.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState ds = - AssertWillBeError(device.CreateDepthStencilStateBuilder()) - .SetStencilFunction(dawn::Face::Back, &stencilBackDescriptor1) - .SetStencilFunction(dawn::Face::Back, &stencilBackDescriptor2) - .GetResult(); - } - - // Test failure when indirectly setting stencil function on a face multiple times - { - dawn::StencilStateFaceDescriptor stencilBothDescriptor; - stencilBothDescriptor.compare = dawn::CompareFunction::Less; - stencilBothDescriptor.stencilFailOp = dawn::StencilOperation::Replace; - stencilBothDescriptor.depthFailOp = dawn::StencilOperation::Replace; - stencilBothDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::StencilStateFaceDescriptor stencilBackDescriptor; - stencilBackDescriptor.compare = dawn::CompareFunction::Greater; - stencilBackDescriptor.stencilFailOp = dawn::StencilOperation::Replace; - stencilBackDescriptor.depthFailOp = dawn::StencilOperation::Replace; - stencilBackDescriptor.passOp = dawn::StencilOperation::Replace; - dawn::DepthStencilState ds = - AssertWillBeError(device.CreateDepthStencilStateBuilder()) - .SetStencilFunction(dawn::Face::Both, &stencilBothDescriptor) - .SetStencilFunction(dawn::Face::Back, &stencilBackDescriptor) - .GetResult(); - } -} diff --git a/src/utils/ComboRenderPipelineDescriptor.cpp b/src/utils/ComboRenderPipelineDescriptor.cpp index 9e9ff97b4a..edbe01f4aa 100644 --- a/src/utils/ComboRenderPipelineDescriptor.cpp +++ b/src/utils/ComboRenderPipelineDescriptor.cpp @@ -50,25 +50,45 @@ namespace utils { } } - descriptor->inputState = device.CreateInputStateBuilder().GetResult(); - descriptor->depthStencilState = device.CreateDepthStencilStateBuilder().GetResult(); - descriptor->layout = utils::MakeBasicPipelineLayout(device, nullptr); + // Set defaults for the blend state descriptors. + { + descriptor->numBlendStates = 1; + descriptor->blendStates = &cBlendStates[0]; - descriptor->numBlendStates = 1; - descriptor->blendStates = &cBlendStates[0]; - - dawn::BlendDescriptor blend; - blend.operation = dawn::BlendOperation::Add; - blend.srcFactor = dawn::BlendFactor::One; - blend.dstFactor = dawn::BlendFactor::Zero; - dawn::BlendStateDescriptor blendStateDescriptor; - blendStateDescriptor.blendEnabled = false; - blendStateDescriptor.alphaBlend = blend; - blendStateDescriptor.colorBlend = blend; - blendStateDescriptor.colorWriteMask = dawn::ColorWriteMask::All; - for (uint32_t i = 0; i < kMaxColorAttachments; ++i) { - cBlendStates[i] = blendStateDescriptor; + dawn::BlendDescriptor blend; + blend.operation = dawn::BlendOperation::Add; + blend.srcFactor = dawn::BlendFactor::One; + blend.dstFactor = dawn::BlendFactor::Zero; + dawn::BlendStateDescriptor blendStateDescriptor; + blendStateDescriptor.blendEnabled = false; + blendStateDescriptor.alphaBlend = blend; + blendStateDescriptor.colorBlend = blend; + blendStateDescriptor.colorWriteMask = dawn::ColorWriteMask::All; + for (uint32_t i = 0; i < kMaxColorAttachments; ++i) { + cBlendStates[i] = blendStateDescriptor; + } } + + // Set defaults for the depth stencil state descriptors. + { + dawn::StencilStateFaceDescriptor stencilFace; + stencilFace.compare = dawn::CompareFunction::Always; + stencilFace.stencilFailOp = dawn::StencilOperation::Keep; + stencilFace.depthFailOp = dawn::StencilOperation::Keep; + stencilFace.passOp = dawn::StencilOperation::Keep; + + // dawn::DepthStencilStateDescriptor depthStencilState; + cDepthStencilState.depthWriteEnabled = false; + cDepthStencilState.depthCompare = dawn::CompareFunction::Always; + cDepthStencilState.back = stencilFace; + cDepthStencilState.front = stencilFace; + cDepthStencilState.stencilReadMask = 0xff; + cDepthStencilState.stencilWriteMask = 0xff; + descriptor->depthStencilState = &cDepthStencilState; + } + + descriptor->inputState = device.CreateInputStateBuilder().GetResult(); + descriptor->layout = utils::MakeBasicPipelineLayout(device, nullptr); } } // namespace utils diff --git a/src/utils/ComboRenderPipelineDescriptor.h b/src/utils/ComboRenderPipelineDescriptor.h index f9c7ffa929..8acbdb57b6 100644 --- a/src/utils/ComboRenderPipelineDescriptor.h +++ b/src/utils/ComboRenderPipelineDescriptor.h @@ -34,6 +34,7 @@ namespace utils { std::array cColorAttachments; dawn::AttachmentDescriptor cDepthStencilAttachment; std::array cBlendStates; + dawn::DepthStencilStateDescriptor cDepthStencilState; }; } // namespace utils