[graphite] Avoid redundant fields in DepthStencilSettings Whether or not the tests are enabled is inferred from the write state and the comparison state. Cleans up the backend depth/stencil state objects as well. Bug: 500416105 Change-Id: I1252a26c848a0e3fe536688fec04b374774d89b6 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1373996 Reviewed-by: Nathan Sanchez <nathanasanchez@google.com> Commit-Queue: Michael Ludwig <michaelludwig@google.com>
diff --git a/src/gpu/graphite/Device.cpp b/src/gpu/graphite/Device.cpp index d981a78..31e8303 100644 --- a/src/gpu/graphite/Device.cpp +++ b/src/gpu/graphite/Device.cpp
@@ -1925,9 +1925,8 @@ SkASSERT(renderer); for (const RenderStep* step : renderer->steps()) { auto dss = step->depthStencilSettings(); - SkASSERT((!step->performsShading() || dss.fDepthTestEnabled) && - (!dss.fDepthTestEnabled || - dss.fDepthCompareOp == CompareOp::kLess || + SkASSERT((!step->performsShading() || dss.fDepthCompareOp != CompareOp::kAlways) || + (dss.fDepthCompareOp == CompareOp::kLess || dss.fDepthCompareOp == CompareOp::kLEqual)); } #endif
diff --git a/src/gpu/graphite/DrawTypes.h b/src/gpu/graphite/DrawTypes.h index 73fe3d7..6fa385e 100644 --- a/src/gpu/graphite/DrawTypes.h +++ b/src/gpu/graphite/DrawTypes.h
@@ -10,6 +10,7 @@ #include "include/private/SkAssert.h" #include "include/private/SkEnumBitMask.h" +#include "include/private/SkTo.h" #include <cstddef> #include <cstdint> @@ -207,30 +208,26 @@ constexpr Face(StencilOp stencilFail, StencilOp depthFail, StencilOp dsPass, - CompareOp compare, - uint32_t readMask, - uint32_t writeMask) + CompareOp compare) : fStencilFailOp(stencilFail) , fDepthFailOp(depthFail) , fDepthStencilPassOp(dsPass) - , fCompareOp(compare) - , fReadMask(readMask) - , fWriteMask(writeMask) {} + , fCompareOp(compare) {} StencilOp fStencilFailOp = StencilOp::kKeep; StencilOp fDepthFailOp = StencilOp::kKeep; StencilOp fDepthStencilPassOp = StencilOp::kKeep; CompareOp fCompareOp = CompareOp::kAlways; - uint32_t fReadMask = 0xffffffff; - uint32_t fWriteMask = 0xffffffff; + + // Return true if not the default stencil settings, i.e. does something to or with the + // stencil buffer. + constexpr explicit operator bool() const { return !(*this == Face()); } constexpr bool operator==(const Face& that) const { return this->fStencilFailOp == that.fStencilFailOp && this->fDepthFailOp == that.fDepthFailOp && this->fDepthStencilPassOp == that.fDepthStencilPassOp && - this->fCompareOp == that.fCompareOp && - this->fReadMask == that.fReadMask && - this->fWriteMask == that.fWriteMask; + this->fCompareOp == that.fCompareOp; } }; @@ -238,34 +235,53 @@ constexpr DepthStencilSettings(Face front, Face back, uint32_t stencilRef, - bool stencilTest, + uint32_t stencilReadMask, + uint32_t stencilWriteMask, CompareOp depthCompare, - bool depthTest, bool depthWrite) : fFrontStencil(front) , fBackStencil(back) , fStencilReferenceValue(stencilRef) + , fStencilReadMask(stencilReadMask) + , fStencilWriteMask(stencilWriteMask) , fDepthCompareOp(depthCompare) - , fStencilTestEnabled(stencilTest) - , fDepthTestEnabled(depthTest) - , fDepthWriteEnabled(depthWrite) {} + , fDepthWriteEnabled(depthWrite) { + // If there's no non-default stencil face state, the shared stencil state should be default + SkASSERT(this->stencilEnabled() || (stencilRef == 0 && + stencilReadMask == 0xffffffff && + stencilWriteMask == 0xffffffff)); + } + + // True means the render pass must have a stencil attachment + constexpr bool stencilEnabled() const { + return SkToBool(fFrontStencil) || SkToBool(fBackStencil); + } + // True means the render pass must have a depth attachment + constexpr bool depthEnabled() const { + return fDepthCompareOp != CompareOp::kAlways || fDepthWriteEnabled; + } + + constexpr explicit operator bool() const { + return this->stencilEnabled() || this->depthEnabled(); + } constexpr bool operator==(const DepthStencilSettings& that) const { return this->fFrontStencil == that.fFrontStencil && this->fBackStencil == that.fBackStencil && this->fStencilReferenceValue == that.fStencilReferenceValue && + this->fStencilReadMask == that.fStencilReadMask && + this->fStencilWriteMask == that.fStencilWriteMask && this->fDepthCompareOp == that.fDepthCompareOp && - this->fStencilTestEnabled == that.fStencilTestEnabled && - this->fDepthTestEnabled == that.fDepthTestEnabled && this->fDepthWriteEnabled == that.fDepthWriteEnabled; } Face fFrontStencil; Face fBackStencil; uint32_t fStencilReferenceValue = 0; + uint32_t fStencilReadMask = 0xffffffff; + uint32_t fStencilWriteMask = 0xffffffff; + CompareOp fDepthCompareOp = CompareOp::kAlways; - bool fStencilTestEnabled = false; - bool fDepthTestEnabled = false; bool fDepthWriteEnabled = false; };
diff --git a/src/gpu/graphite/Renderer.h b/src/gpu/graphite/Renderer.h index 8a6275d..f934e13 100644 --- a/src/gpu/graphite/Renderer.h +++ b/src/gpu/graphite/Renderer.h
@@ -32,8 +32,8 @@ namespace skgpu::graphite { -class DrawWriter; class DrawParams; +class DrawWriter; class PipelineDataGatherer; class Rect; class ResourceProvider; @@ -236,9 +236,9 @@ const DepthStencilSettings& depthStencilSettings() const { return fDepthStencilSettings; } SkEnumBitMask<DepthStencilFlags> depthStencilFlags() const { - return (fDepthStencilSettings.fStencilTestEnabled + return (fDepthStencilSettings.stencilEnabled() ? DepthStencilFlags::kStencil : DepthStencilFlags::kNone) | - (fDepthStencilSettings.fDepthTestEnabled || fDepthStencilSettings.fDepthWriteEnabled + (fDepthStencilSettings.depthEnabled() ? DepthStencilFlags::kDepth : DepthStencilFlags::kNone); } @@ -409,7 +409,7 @@ // A render step using non-AA inner fills with a second draw should not also be part of a // multi-step renderer (to keep reasoning simple) and must use the LESS depth test. SkASSERT(!this->useNonAAInnerFill() || - (fStepCount == 1 && fSteps[0]->depthStencilSettings().fDepthTestEnabled && + (fStepCount == 1 && fSteps[0]->depthStencilSettings().fDepthCompareOp == CompareOp::kLess)); }
diff --git a/src/gpu/graphite/dawn/DawnGraphicsPipeline.cpp b/src/gpu/graphite/dawn/DawnGraphicsPipeline.cpp index 5564a70..76c3fd5 100644 --- a/src/gpu/graphite/dawn/DawnGraphicsPipeline.cpp +++ b/src/gpu/graphite/dawn/DawnGraphicsPipeline.cpp
@@ -459,25 +459,28 @@ // Depth stencil state const auto& depthStencilSettings = step->depthStencilSettings(); - SkASSERT(depthStencilSettings.fDepthTestEnabled || - depthStencilSettings.fDepthCompareOp == CompareOp::kAlways); + TextureFormat dsFormat = renderPassDesc.fDepthStencilAttachment.fFormat; + SkASSERT(dsFormat != TextureFormat::kUnsupported || !depthStencilSettings); + wgpu::DepthStencilState depthStencil; if (dsFormat != TextureFormat::kUnsupported) { SkASSERT(TextureFormatIsDepthOrStencil(dsFormat)); depthStencil.format = TextureFormatToDawnFormat(dsFormat); - if (depthStencilSettings.fDepthTestEnabled) { - depthStencil.depthWriteEnabled = depthStencilSettings.fDepthWriteEnabled; - } - depthStencil.depthCompare = compare_op_to_dawn(depthStencilSettings.fDepthCompareOp); - // Dawn validation fails if the stencil state is non-default and the - // format doesn't have the stencil aspect. - if (TextureFormatHasStencil(dsFormat) && depthStencilSettings.fStencilTestEnabled) { + if (depthStencilSettings.depthEnabled()) { + SkASSERT(TextureFormatHasDepth(dsFormat)); + depthStencil.depthWriteEnabled = depthStencilSettings.fDepthWriteEnabled; + depthStencil.depthCompare = compare_op_to_dawn(depthStencilSettings.fDepthCompareOp); + } + + if (depthStencilSettings.stencilEnabled()) { + SkASSERT(TextureFormatHasStencil(dsFormat)); depthStencil.stencilFront = stencil_face_to_dawn(depthStencilSettings.fFrontStencil); depthStencil.stencilBack = stencil_face_to_dawn(depthStencilSettings.fBackStencil); - depthStencil.stencilReadMask = depthStencilSettings.fFrontStencil.fReadMask; - depthStencil.stencilWriteMask = depthStencilSettings.fFrontStencil.fWriteMask; + depthStencil.stencilReadMask = depthStencilSettings.fStencilReadMask; + depthStencil.stencilWriteMask = depthStencilSettings.fStencilWriteMask; + // NOTE: fStencilReferenceValue is dynamic state and set on the render encoder } descriptor.depthStencil = &depthStencil;
diff --git a/src/gpu/graphite/mtl/MtlSharedContext.h b/src/gpu/graphite/mtl/MtlSharedContext.h index aa173e4..31e7552 100644 --- a/src/gpu/graphite/mtl/MtlSharedContext.h +++ b/src/gpu/graphite/mtl/MtlSharedContext.h
@@ -23,8 +23,8 @@ } namespace skgpu::graphite { -struct ContextOptions; class MtlGraphicsPipeline; +struct ContextOptions; class MtlThreadSafeResourceProvider final : public ThreadSafeResourceProvider { public: @@ -60,7 +60,8 @@ SkExecutor*, SkSpan<sk_sp<SkRuntimeEffect>> userDefinedKnownRuntimeEffects); - void createCompatibleDepthStencilState(const DepthStencilSettings&, const char* label); + sk_cfp<id<MTLDepthStencilState>> createCompatibleDepthStencilState(const DepthStencilSettings&, + const char* label) const; sk_sp<GraphicsPipeline> createGraphicsPipeline( const RuntimeEffectDictionary*, @@ -79,7 +80,7 @@ // which case, this would be a reasonable place for this cache. // TODO(robertphillips): Come up with a scheme to map from DepthStencilSettings to tightly // packed ints and switch this to be a std::array. - skia_private::THashMap<DepthStencilSettings, sk_cfp<id<MTLDepthStencilState>>> + skia_private::TArray<std::pair<DepthStencilSettings, sk_cfp<id<MTLDepthStencilState>>>> fDepthStencilStates; };
diff --git a/src/gpu/graphite/mtl/MtlSharedContext.mm b/src/gpu/graphite/mtl/MtlSharedContext.mm index 7dbc125..199af64 100644 --- a/src/gpu/graphite/mtl/MtlSharedContext.mm +++ b/src/gpu/graphite/mtl/MtlSharedContext.mm
@@ -81,7 +81,8 @@ { kInverseCoverPass, "inverse-cover" }, { kIgnoreDSS, "ignore" }, }) { - this->createCompatibleDepthStencilState(dss.first, dss.second); + fDepthStencilStates.push_back( + {dss.first, this->createCompatibleDepthStencilState(dss.first, dss.second)}); } } @@ -150,11 +151,13 @@ } } -MTLStencilDescriptor* stencil_face_to_mtl(DepthStencilSettings::Face face) { +MTLStencilDescriptor* stencil_face_to_mtl(DepthStencilSettings::Face face, + uint32_t readMask, + uint32_t writeMask) { MTLStencilDescriptor* result = [[MTLStencilDescriptor alloc] init]; result.stencilCompareFunction = compare_op_to_mtl(face.fCompareOp); - result.readMask = face.fReadMask; - result.writeMask = face.fWriteMask; + result.readMask = readMask; + result.writeMask = writeMask; result.depthStencilPassOperation = stencil_op_to_mtl(face.fDepthStencilPassOp); result.depthFailureOperation = stencil_op_to_mtl(face.fDepthFailOp); result.stencilFailureOperation = stencil_op_to_mtl(face.fStencilFailOp); @@ -166,32 +169,41 @@ sk_cfp<id<MTLDepthStencilState>> MtlSharedContext::getCompatibleDepthStencilState( const DepthStencilSettings& depthStencilSettings) const { - sk_cfp<id<MTLDepthStencilState>>* depthStencilState; - depthStencilState = fDepthStencilStates.find(depthStencilSettings); + for (auto&& dssToMtl : fDepthStencilStates) { + if (dssToMtl.first == depthStencilSettings) { + return dssToMtl.second; + } + } // We've explicitly initialized fDepthStencilStates with all the common depth stencil settings // in the ctor - since there are so few of them. This frees us from concurrency concerns (i.e., // if we were to lazily create them and store them in a map). However, if a new one is // encountered we will need to either add its initialization to the ctor or reconsider this // approach. - SkAssertResult(depthStencilState); - return *depthStencilState; + SkASSERTF(false, "Unexpected depth stencil settings"); + return this->createCompatibleDepthStencilState(depthStencilSettings, + "Runtime DepthStencilSettings"); } -void MtlSharedContext::createCompatibleDepthStencilState( - const DepthStencilSettings& depthStencilSettings, - const char* label) { +sk_cfp<id<MTLDepthStencilState>> MtlSharedContext::createCompatibleDepthStencilState( + const DepthStencilSettings& depthStencilSettings, + const char* label) const { MTLDepthStencilDescriptor* desc = [[MTLDepthStencilDescriptor alloc] init]; - SkASSERT(depthStencilSettings.fDepthTestEnabled || - depthStencilSettings.fDepthCompareOp == CompareOp::kAlways); - desc.depthCompareFunction = compare_op_to_mtl(depthStencilSettings.fDepthCompareOp); - if (depthStencilSettings.fDepthTestEnabled) { + if (depthStencilSettings.depthEnabled()) { + desc.depthCompareFunction = compare_op_to_mtl(depthStencilSettings.fDepthCompareOp); desc.depthWriteEnabled = depthStencilSettings.fDepthWriteEnabled; } - if (depthStencilSettings.fStencilTestEnabled) { - desc.frontFaceStencil = stencil_face_to_mtl(depthStencilSettings.fFrontStencil); - desc.backFaceStencil = stencil_face_to_mtl(depthStencilSettings.fBackStencil); + + if (depthStencilSettings.stencilEnabled()) { + desc.frontFaceStencil = stencil_face_to_mtl(depthStencilSettings.fFrontStencil, + depthStencilSettings.fStencilReadMask, + depthStencilSettings.fStencilWriteMask); + desc.backFaceStencil = stencil_face_to_mtl(depthStencilSettings.fBackStencil, + depthStencilSettings.fStencilReadMask, + depthStencilSettings.fStencilWriteMask); + + // NOTE: The stencil reference value is dynamic state and set on the render encoder } desc.label = [NSString stringWithFormat:@"%@(write:%d)", [NSString stringWithUTF8String:label], @@ -199,8 +211,7 @@ sk_cfp<id<MTLDepthStencilState>> dss( [this->device() newDepthStencilStateWithDescriptor: desc]); - - fDepthStencilStates.set(depthStencilSettings, std::move(dss)); + return dss; } sk_sp<GraphicsPipeline> MtlSharedContext::createGraphicsPipeline(
diff --git a/src/gpu/graphite/render/CommonDepthStencilSettings.h b/src/gpu/graphite/render/CommonDepthStencilSettings.h index 562f01e..cfa67a2 100644 --- a/src/gpu/graphite/render/CommonDepthStencilSettings.h +++ b/src/gpu/graphite/render/CommonDepthStencilSettings.h
@@ -18,23 +18,23 @@ */ static constexpr DepthStencilSettings kDirectDepthLessPass = { - /*front=*/ {}, - /*back=*/ {}, - /*stencilRef=*/ 0, - /*stencilTest=*/ false, - /*depthCompare=*/CompareOp::kLess, - /*depthTest=*/ true, - /*depthWrite=*/ true + /*front=*/ {}, + /*back=*/ {}, + /*stencilRef=*/ 0, + /*stencilReadMask=*/ 0xffffffff, + /*stencilWriteMask=*/0xffffffff, + /*depthCompare=*/ CompareOp::kLess, + /*depthWrite=*/ true, }; static constexpr DepthStencilSettings kDirectDepthLEqualPass = { - /*front=*/ {}, - /*back=*/ {}, - /*stencilRef=*/ 0, - /*stencilTest=*/ false, - /*depthCompare=*/CompareOp::kLEqual, - /*depthTest=*/ true, - /*depthWrite=*/ true + /*front=*/ {}, + /*back=*/ {}, + /*stencilRef=*/ 0, + /*stencilReadMask=*/ 0xffffffff, + /*stencilWriteMask=*/0xffffffff, + /*depthCompare=*/ CompareOp::kLEqual, + /*depthWrite=*/ true, }; /** @@ -47,9 +47,7 @@ /*stencilFail=*/ StencilOp::kKeep, /*depthFail=*/ StencilOp::kKeep, /*dsPass=*/ StencilOp::kIncWrap, - /*compare=*/ CompareOp::kAlways, - /*readMask=*/ 0xffffffff, - /*writeMask=*/ 0xffffffff + /*compare=*/ CompareOp::kAlways }; // Decrements stencil value on counterclockwise triangles. Used for "winding" fill. @@ -57,9 +55,7 @@ /*stencilFail=*/ StencilOp::kKeep, /*depthFail=*/ StencilOp::kKeep, /*dsPass=*/ StencilOp::kDecWrap, - /*compare=*/ CompareOp::kAlways, - /*readMask=*/ 0xffffffff, - /*writeMask=*/ 0xffffffff + /*compare=*/ CompareOp::kAlways }; // Increments the stencil value whenever a triangle is encountered. Used for storing a stroked @@ -68,9 +64,7 @@ /*stencilFail=*/ StencilOp::kKeep, /*depthFail=*/ StencilOp::kKeep, /*dsPass=*/ StencilOp::kIncClamp, - /*compare=*/ CompareOp::kAlways, - /*readMask=*/ 0xffffffff, - /*writeMask=*/ 0xffffffff + /*compare=*/ CompareOp::kAlways }; // Toggles the bottom stencil bit. Used for "even-odd" fill. @@ -78,45 +72,43 @@ /*stencilFail=*/ StencilOp::kKeep, /*depthFail=*/ StencilOp::kKeep, /*dsPass=*/ StencilOp::kInvert, - /*compare=*/ CompareOp::kAlways, - /*readMask=*/ 0xffffffff, - /*writeMask=*/ 0x00000001 + /*compare=*/ CompareOp::kAlways }; // Stencil settings to use for a standard Redbook "stencil" pass corresponding to a "winding" // fill rule (regular or inverse is selected by a follow-up pass). constexpr DepthStencilSettings kWindingStencilPass = { - /*front=*/ kIncrementCW, - /*back=*/ kDecrementCCW, - /*stencilRef=*/ 0, - /*stencilTest=*/ true, - /*depthCompare=*/CompareOp::kLess, - /*depthTest=*/ true, - /*depthWrite=*/ false // The depth write will be handled by the covering pass + /*front=*/ kIncrementCW, + /*back=*/ kDecrementCCW, + /*stencilRef=*/ 0, + /*stencilReadMask=*/ 0xffffffff, + /*stencilWriteMask=*/0xffffffff, + /*depthCompare=*/ CompareOp::kLess, + /*depthWrite=*/ false // The depth write will be handled by the covering pass }; // Stencil settings to use for a standard Redbook "stencil" pass corresponding to an "even-odd" // fill rule (regular or inverse is selected by a follow-up pass). constexpr DepthStencilSettings kEvenOddStencilPass = { - /*front=*/ kToggle, - /*back=*/ kToggle, - /*stencilRef=*/ 0, - /*stencilTest=*/ true, - /*depthCompare=*/CompareOp::kLess, - /*depthTest=*/ true, - /*depthWrite=*/ false // The depth write will be handled by the covering pass + /*front=*/ kToggle, + /*back=*/ kToggle, + /*stencilRef=*/ 0, + /*stencilReadMask=*/ 0xffffffff, + /*stencilWriteMask=*/0x00000001, + /*depthCompare=*/ CompareOp::kLess, + /*depthWrite=*/ false // The depth write will be handled by the covering pass }; // Stencil settings to use for always stenciling out any triangle encountered, useful for stroked // paths. static constexpr DepthStencilSettings kIncrementStencilPass = { - /*front=*/ kIncrementAlways, - /*back=*/ kIncrementAlways, - /*stencilRef=*/ 0, - /*stencilTest=*/ true, - /*depthCompare=*/CompareOp::kLess, - /*depthTest=*/ true, - /*depthWrite=*/ false // The depth write will be handled by the covering pass + /*front=*/ kIncrementAlways, + /*back=*/ kIncrementAlways, + /*stencilRef=*/ 0, + /*stencilReadMask=*/ 0xffffffff, + /*stencilWriteMask=*/0xffffffff, + /*depthCompare=*/ CompareOp::kLess, + /*depthWrite=*/ false // The depth write will be handled by the covering pass }; /** @@ -132,9 +124,7 @@ /*stencilFail=*/ StencilOp::kKeep, /*depthFail=*/ StencilOp::kZero, /*dsPass=*/ StencilOp::kZero, - /*compare=*/ CompareOp::kNotEqual, - /*readMask=*/ 0xffffffff, - /*writeMask=*/ 0xffffffff + /*compare=*/ CompareOp::kNotEqual }; // Resets non-zero bits to 0, passes when zero. @@ -142,33 +132,31 @@ /*stencilFail=*/ StencilOp::kZero, /*depthFail=*/ StencilOp::kKeep, /*dsPass=*/ StencilOp::kKeep, - /*compare=*/ CompareOp::kEqual, - /*readMask=*/ 0xffffffff, - /*writeMask=*/ 0xffffffff + /*compare=*/ CompareOp::kEqual }; // Stencil settings to use for a standard Redbook "cover" pass for a regular fill, assuming that the // stencil buffer has been modified by either kWindingStencilPass or kEvenOddStencilPass. constexpr DepthStencilSettings kRegularCoverPass = { - /*front=*/ kPassNonZero, - /*back=*/ kPassNonZero, - /*stencilRef=*/ 0, - /*stencilTest=*/ true, - /*depthCompare=*/CompareOp::kLess, - /*depthTest=*/ true, - /*depthWrite=*/ true + /*front=*/ kPassNonZero, + /*back=*/ kPassNonZero, + /*stencilRef=*/ 0, + /*stencilReadMask=*/ 0xffffffff, + /*stencilWriteMask=*/0xffffffff, + /*depthCompare=*/ CompareOp::kLess, + /*depthWrite=*/ true }; // Stencil settings to use for a standard Redbook "cover" pass for inverse fills, assuming that the // stencil buffer has been modified by either kWindingStencilPass or kEvenOddStencilPass. constexpr DepthStencilSettings kInverseCoverPass = { - /*front=*/ kPassZero, - /*back=*/ kPassZero, - /*stencilRef=*/ 0, - /*stencilTest=*/ true, - /*depthCompare=*/CompareOp::kLess, - /*depthTest=*/ true, - /*depthWrite=*/ true + /*front=*/ kPassZero, + /*back=*/ kPassZero, + /*stencilRef=*/ 0, + /*stencilReadMask=*/ 0xffffffff, + /*stencilWriteMask=*/0xffffffff, + /*depthCompare=*/ CompareOp::kLess, + /*depthWrite=*/ true }; } // namespace skgpu::graphite
diff --git a/src/gpu/graphite/vk/VulkanGraphicsPipeline.cpp b/src/gpu/graphite/vk/VulkanGraphicsPipeline.cpp index f2d603b..85c24aa 100644 --- a/src/gpu/graphite/vk/VulkanGraphicsPipeline.cpp +++ b/src/gpu/graphite/vk/VulkanGraphicsPipeline.cpp
@@ -247,42 +247,45 @@ static void setup_stencil_op_state(VkStencilOpState* opState, const DepthStencilSettings::Face& face, - uint32_t referenceValue) { + uint32_t referenceValue, + uint32_t readMask, + uint32_t writeMask) { opState->failOp = stencil_op_to_vk_stencil_op(face.fStencilFailOp); opState->passOp = stencil_op_to_vk_stencil_op(face.fDepthStencilPassOp); opState->depthFailOp = stencil_op_to_vk_stencil_op(face.fDepthFailOp); opState->compareOp = compare_op_to_vk_compare_op(face.fCompareOp); - opState->compareMask = face.fReadMask; + opState->compareMask = readMask; // Note: On some old ARM driver versions, dynamic state for stencil write mask doesn't work // correctly in the presence of discard or alpha to coverage, if the static state provided // when creating the pipeline has a value of 0. However, both alphaToCoverageEnable and // rasterizerDiscardEnable are always false in Graphite, so no driver bug workaround is // necessary at the moment. - opState->writeMask = face.fWriteMask; + opState->writeMask = writeMask; opState->reference = referenceValue; } static void setup_depth_stencil_state(const Caps& caps, const DepthStencilSettings& stencilSettings, VkPipelineDepthStencilStateCreateInfo* stencilInfo) { - SkASSERT(stencilSettings.fDepthTestEnabled || - stencilSettings.fDepthCompareOp == CompareOp::kAlways); - *stencilInfo = {}; stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; if (!caps.useBasicDynamicState()) { - stencilInfo->depthTestEnable = stencilSettings.fDepthTestEnabled; + stencilInfo->depthTestEnable = stencilSettings.depthEnabled(); stencilInfo->depthWriteEnable = stencilSettings.fDepthWriteEnabled; stencilInfo->depthCompareOp = compare_op_to_vk_compare_op(stencilSettings.fDepthCompareOp); stencilInfo->depthBoundsTestEnable = VK_FALSE; - stencilInfo->stencilTestEnable = stencilSettings.fStencilTestEnabled; - if (stencilSettings.fStencilTestEnabled) { + stencilInfo->stencilTestEnable = stencilSettings.stencilEnabled(); + if (stencilInfo->stencilTestEnable) { setup_stencil_op_state(&stencilInfo->front, stencilSettings.fFrontStencil, - stencilSettings.fStencilReferenceValue); + stencilSettings.fStencilReferenceValue, + stencilSettings.fStencilReadMask, + stencilSettings.fStencilWriteMask); setup_stencil_op_state(&stencilInfo->back, stencilSettings.fBackStencil, - stencilSettings.fStencilReferenceValue); + stencilSettings.fStencilReferenceValue, + stencilSettings.fStencilReadMask, + stencilSettings.fStencilWriteMask); } stencilInfo->minDepthBounds = 0.0f; stencilInfo->maxDepthBounds = 1.0f; @@ -1355,54 +1358,20 @@ // the diff between pipelines and update the state as needed. This is not quite so efficient, // but will need awareness from the front-end to optimize. if (sharedContext->caps()->useBasicDynamicState()) { - const DepthStencilSettings& depthStencil = fDepthStencilSettings; - const DepthStencilSettings::Face& frontStencil = depthStencil.fFrontStencil; - const DepthStencilSettings::Face& backStencil = depthStencil.fBackStencil; + // Primitive type + const bool primitiveTypeDirty = + previous == nullptr || previous->fPrimitiveType != fPrimitiveType; + if (primitiveTypeDirty) { + VULKAN_CALL(sharedContext->interface(), + CmdSetPrimitiveTopology(commandBuffer, + primitive_type_to_vk_topology(fPrimitiveType))); + } + // Depth-stencil settings + const DepthStencilSettings& depthStencil = fDepthStencilSettings; const DepthStencilSettings* pastDepthStencil = previous ? &previous->fDepthStencilSettings : nullptr; - const DepthStencilSettings::Face* pastFrontStencil = - previous ? &pastDepthStencil->fFrontStencil : nullptr; - const DepthStencilSettings::Face* pastBackStencil = - previous ? &pastDepthStencil->fBackStencil : nullptr; - const bool frontStencilReadMaskDirty = - previous == nullptr || pastFrontStencil->fReadMask != frontStencil.fReadMask; - if (frontStencilReadMaskDirty) { - VULKAN_CALL(sharedContext->interface(), - CmdSetStencilCompareMask( - commandBuffer, VK_STENCIL_FACE_FRONT_BIT, frontStencil.fReadMask)); - } - const bool backStencilReadMaskDirty = - previous == nullptr || pastBackStencil->fReadMask != backStencil.fReadMask; - if (backStencilReadMaskDirty) { - VULKAN_CALL(sharedContext->interface(), - CmdSetStencilCompareMask( - commandBuffer, VK_STENCIL_FACE_BACK_BIT, backStencil.fReadMask)); - } - const bool frontStencilWriteMaskDirty = - previous == nullptr || pastFrontStencil->fWriteMask != frontStencil.fWriteMask; - if (frontStencilWriteMaskDirty) { - VULKAN_CALL(sharedContext->interface(), - CmdSetStencilWriteMask( - commandBuffer, VK_STENCIL_FACE_FRONT_BIT, frontStencil.fWriteMask)); - } - const bool backStencilWriteMaskDirty = - previous == nullptr || pastBackStencil->fWriteMask != backStencil.fWriteMask; - if (backStencilWriteMaskDirty) { - VULKAN_CALL(sharedContext->interface(), - CmdSetStencilWriteMask( - commandBuffer, VK_STENCIL_FACE_BACK_BIT, backStencil.fWriteMask)); - } - const bool stencilReferenceDirty = - previous == nullptr || - pastDepthStencil->fStencilReferenceValue != depthStencil.fStencilReferenceValue; - if (stencilReferenceDirty) { - VULKAN_CALL(sharedContext->interface(), - CmdSetStencilReference(commandBuffer, - VK_STENCIL_FACE_FRONT_AND_BACK, - depthStencil.fStencilReferenceValue)); - } const bool depthCompareOpDirty = previous == nullptr || pastDepthStencil->fDepthCompareOp != depthStencil.fDepthCompareOp; @@ -1414,10 +1383,10 @@ } const bool depthTestEnabledDirty = previous == nullptr || - pastDepthStencil->fDepthTestEnabled != depthStencil.fDepthTestEnabled; + pastDepthStencil->depthEnabled() != depthStencil.depthEnabled(); if (depthTestEnabledDirty) { VULKAN_CALL(sharedContext->interface(), - CmdSetDepthTestEnable(commandBuffer, depthStencil.fDepthTestEnabled)); + CmdSetDepthTestEnable(commandBuffer, depthStencil.depthEnabled())); } const bool depthWriteEnabledDirty = previous == nullptr || @@ -1428,59 +1397,82 @@ } const bool stencilTestEnabledDirty = previous == nullptr || - pastDepthStencil->fStencilTestEnabled != depthStencil.fStencilTestEnabled; + pastDepthStencil->stencilEnabled() != depthStencil.stencilEnabled(); if (stencilTestEnabledDirty) { VULKAN_CALL(sharedContext->interface(), - CmdSetStencilTestEnable(commandBuffer, depthStencil.fStencilTestEnabled)); + CmdSetStencilTestEnable(commandBuffer, depthStencil.stencilEnabled())); } - const bool frontStencilOpsDirty = - previous == nullptr || - pastFrontStencil->fDepthStencilPassOp != frontStencil.fDepthStencilPassOp || - pastFrontStencil->fStencilFailOp != frontStencil.fStencilFailOp || - pastFrontStencil->fDepthFailOp != frontStencil.fDepthFailOp || - pastFrontStencil->fCompareOp != frontStencil.fCompareOp; - if (frontStencilOpsDirty) { - VULKAN_CALL( - sharedContext->interface(), - CmdSetStencilOp(commandBuffer, - VK_STENCIL_FACE_FRONT_BIT, - /*failOp=*/ - stencil_op_to_vk_stencil_op(frontStencil.fStencilFailOp), - /*passOp=*/ - stencil_op_to_vk_stencil_op(frontStencil.fDepthStencilPassOp), - /*depthFailOp=*/ - stencil_op_to_vk_stencil_op(frontStencil.fDepthFailOp), - /*compareOp=*/ - compare_op_to_vk_compare_op(frontStencil.fCompareOp))); - } - const bool backStencilOpsDirty = - previous == nullptr || - pastBackStencil->fDepthStencilPassOp != backStencil.fDepthStencilPassOp || - pastBackStencil->fStencilFailOp != backStencil.fStencilFailOp || - pastBackStencil->fDepthFailOp != backStencil.fDepthFailOp || - pastBackStencil->fCompareOp != backStencil.fCompareOp; - if (backStencilOpsDirty) { - VULKAN_CALL( - sharedContext->interface(), - CmdSetStencilOp(commandBuffer, - VK_STENCIL_FACE_BACK_BIT, - /*failOp=*/ - stencil_op_to_vk_stencil_op(backStencil.fStencilFailOp), - /*passOp=*/ - stencil_op_to_vk_stencil_op(backStencil.fDepthStencilPassOp), - /*depthFailOp=*/ - stencil_op_to_vk_stencil_op(backStencil.fDepthFailOp), - /*compareOp=*/ - compare_op_to_vk_compare_op(backStencil.fCompareOp))); - } - const bool primitiveTypeDirty = - previous == nullptr || previous->fPrimitiveType != fPrimitiveType; - if (primitiveTypeDirty) { - VULKAN_CALL(sharedContext->interface(), - CmdSetPrimitiveTopology(commandBuffer, - primitive_type_to_vk_topology(fPrimitiveType))); + + // The rest of the stencil state only needs to be checked if we have non-default state or + // if the test state was dirty (in which case it needs to be set back to the default). + if (stencilTestEnabledDirty || depthStencil.stencilEnabled()) { + // FRONT_AND_BACK + const bool stencilReadMaskDirty = + previous == nullptr || + pastDepthStencil->fStencilReadMask != depthStencil.fStencilReadMask; + if (stencilReadMaskDirty) { + VULKAN_CALL(sharedContext->interface(), + CmdSetStencilCompareMask(commandBuffer, + VK_STENCIL_FACE_FRONT_AND_BACK, + depthStencil.fStencilReadMask)); + } + const bool stencilWriteMaskDirty = + previous == nullptr || + pastDepthStencil->fStencilWriteMask != depthStencil.fStencilWriteMask; + if (stencilWriteMaskDirty) { + VULKAN_CALL(sharedContext->interface(), + CmdSetStencilWriteMask(commandBuffer, + VK_STENCIL_FACE_FRONT_AND_BACK, + depthStencil.fStencilWriteMask)); + } + const bool stencilReferenceDirty = + previous == nullptr || + pastDepthStencil->fStencilReferenceValue != depthStencil.fStencilReferenceValue; + if (stencilReferenceDirty) { + VULKAN_CALL(sharedContext->interface(), + CmdSetStencilReference(commandBuffer, + VK_STENCIL_FACE_FRONT_AND_BACK, + depthStencil.fStencilReferenceValue)); + } + + // FRONT-only + const DepthStencilSettings::Face& front = depthStencil.fFrontStencil; + const bool frontStencilOpsDirty = !previous || pastDepthStencil->fFrontStencil != front; + if (frontStencilOpsDirty) { + VULKAN_CALL( + sharedContext->interface(), + CmdSetStencilOp(commandBuffer, + VK_STENCIL_FACE_FRONT_BIT, + /*failOp=*/ + stencil_op_to_vk_stencil_op(front.fStencilFailOp), + /*passOp=*/ + stencil_op_to_vk_stencil_op(front.fDepthStencilPassOp), + /*depthFailOp=*/ + stencil_op_to_vk_stencil_op(front.fDepthFailOp), + /*compareOp=*/ + compare_op_to_vk_compare_op(front.fCompareOp))); + } + + // BACK-only + const DepthStencilSettings::Face& back = depthStencil.fBackStencil; + const bool backStencilOpsDirty = !previous || pastDepthStencil->fBackStencil != back; + if (backStencilOpsDirty) { + VULKAN_CALL( + sharedContext->interface(), + CmdSetStencilOp(commandBuffer, + VK_STENCIL_FACE_BACK_BIT, + /*failOp=*/ + stencil_op_to_vk_stencil_op(back.fStencilFailOp), + /*passOp=*/ + stencil_op_to_vk_stencil_op(back.fDepthStencilPassOp), + /*depthFailOp=*/ + stencil_op_to_vk_stencil_op(back.fDepthFailOp), + /*compareOp=*/ + compare_op_to_vk_compare_op(back.fCompareOp))); + } } } + if (sharedContext->caps()->useVertexInputDynamicState()) { const bool vertexInputDirty = previous == nullptr ||