Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1bd9e2f

Browse files
committed
Tidy stencil state handling
Bug: b/128715612 Change-Id: I1e9859d2cf001bfb341a49ad4f8fc9ef52c9fa5b Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/27508 Tested-by: Chris Forbes <[email protected]> Presubmit-Ready: Chris Forbes <[email protected]> Kokoro-Presubmit: kokoro <[email protected]> Reviewed-by: Nicolas Capens <[email protected]>
1 parent 1cef4e6 commit 1bd9e2f

File tree

8 files changed

+53
-117
lines changed

8 files changed

+53
-117
lines changed

src/Device/Context.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,9 @@ namespace sw
170170
stencilBuffer = nullptr;
171171

172172
stencilEnable = false;
173-
stencilCompareMode = VK_COMPARE_OP_ALWAYS;
174-
stencilReference = 0;
175-
stencilMask = 0xFFFFFFFF;
176-
stencilFailOperation = VK_STENCIL_OP_KEEP;
177-
stencilPassOperation = VK_STENCIL_OP_KEEP;
178-
stencilZFailOperation = VK_STENCIL_OP_KEEP;
179-
stencilWriteMask = 0xFFFFFFFF;
180-
181173
twoSidedStencil = false;
182-
stencilCompareModeCCW = VK_COMPARE_OP_ALWAYS;
183-
stencilReferenceCCW = 0;
184-
stencilMaskCCW = 0xFFFFFFFF;
185-
stencilFailOperationCCW = VK_STENCIL_OP_KEEP;
186-
stencilPassOperationCCW = VK_STENCIL_OP_KEEP;
187-
stencilZFailOperationCCW = VK_STENCIL_OP_KEEP;
188-
stencilWriteMaskCCW = 0xFFFFFFFF;
174+
frontStencil = {};
175+
backStencil = {};
189176

190177
rasterizerDiscard = false;
191178

src/Device/Context.hpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,9 @@ namespace sw
159159
DrawType drawType;
160160

161161
bool stencilEnable;
162-
VkCompareOp stencilCompareMode;
163-
int stencilReference;
164-
int stencilMask;
165-
VkStencilOp stencilFailOperation;
166-
VkStencilOp stencilPassOperation;
167-
VkStencilOp stencilZFailOperation;
168-
int stencilWriteMask;
169-
170162
bool twoSidedStencil;
171-
VkCompareOp stencilCompareModeCCW;
172-
int stencilReferenceCCW;
173-
int stencilMaskCCW;
174-
VkStencilOp stencilFailOperationCCW;
175-
VkStencilOp stencilPassOperationCCW;
176-
VkStencilOp stencilZFailOperationCCW;
177-
int stencilWriteMaskCCW;
163+
VkStencilOpState frontStencil;
164+
VkStencilOpState backStencil;
178165

179166
// Pixel processor states
180167
VkCullModeFlags cullMode;

src/Device/PixelProcessor.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,9 @@ namespace sw
292292
if(context->stencilActive())
293293
{
294294
state.stencilActive = true;
295-
state.stencilCompareMode = context->stencilCompareMode;
296-
state.stencilFailOperation = context->stencilFailOperation;
297-
state.stencilPassOperation = context->stencilPassOperation;
298-
state.stencilZFailOperation = context->stencilZFailOperation;
299-
state.noStencilMask = (context->stencilMask == 0xFF);
300-
state.noStencilWriteMask = (context->stencilWriteMask == 0xFF);
301-
state.stencilWriteMasked = (context->stencilWriteMask == 0x00);
302-
303295
state.twoSidedStencil = context->twoSidedStencil;
304-
state.stencilCompareModeCCW = context->twoSidedStencil ? context->stencilCompareModeCCW : state.stencilCompareMode;
305-
state.stencilFailOperationCCW = context->twoSidedStencil ? context->stencilFailOperationCCW : state.stencilFailOperation;
306-
state.stencilPassOperationCCW = context->twoSidedStencil ? context->stencilPassOperationCCW : state.stencilPassOperation;
307-
state.stencilZFailOperationCCW = context->twoSidedStencil ? context->stencilZFailOperationCCW : state.stencilZFailOperation;
308-
state.noStencilMaskCCW = context->twoSidedStencil ? (context->stencilMaskCCW == 0xFF) : state.noStencilMask;
309-
state.noStencilWriteMaskCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0xFF) : state.noStencilWriteMask;
310-
state.stencilWriteMaskedCCW = context->twoSidedStencil ? (context->stencilWriteMaskCCW == 0x00) : state.stencilWriteMasked;
296+
state.frontStencil = context->frontStencil;
297+
state.backStencil = context->backStencil;
311298
}
312299

313300
if(context->depthBufferActive())

src/Device/PixelProcessor.hpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,9 @@ namespace sw
3939
bool quadLayoutDepthBuffer;
4040

4141
bool stencilActive;
42-
VkCompareOp stencilCompareMode;
43-
VkStencilOp stencilFailOperation;
44-
VkStencilOp stencilPassOperation;
45-
VkStencilOp stencilZFailOperation;
46-
bool noStencilMask;
47-
bool noStencilWriteMask;
48-
bool stencilWriteMasked;
4942
bool twoSidedStencil;
50-
VkCompareOp stencilCompareModeCCW;
51-
VkStencilOp stencilFailOperationCCW;
52-
VkStencilOp stencilPassOperationCCW;
53-
VkStencilOp stencilZFailOperationCCW;
54-
bool noStencilMaskCCW;
55-
bool noStencilWriteMaskCCW;
56-
bool stencilWriteMaskedCCW;
43+
VkStencilOpState frontStencil;
44+
VkStencilOpState backStencil;
5745

5846
bool depthTestActive;
5947
bool occlusionEnabled;

src/Device/Renderer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ namespace sw
328328

329329
if(pixelState.stencilActive)
330330
{
331-
data->stencil[0].set(context->stencilReference, context->stencilMask, context->stencilWriteMask);
332-
data->stencil[1].set(context->stencilReferenceCCW, context->stencilMaskCCW, context->stencilWriteMaskCCW);
331+
data->stencil[0].set(context->frontStencil.reference, context->frontStencil.compareMask, context->frontStencil.writeMask);
332+
data->stencil[1].set(context->backStencil.reference, context->backStencil.compareMask, context->backStencil.writeMask);
333333
}
334334

335335
data->lineWidth = context->lineWidth;

src/Pipeline/PixelRoutine.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -290,33 +290,33 @@ namespace sw
290290
}
291291

292292
Byte8 value = *Pointer<Byte8>(buffer);
293-
Byte8 valueCCW = value;
293+
Byte8 valueBack = value;
294294

295-
if(!state.noStencilMask)
295+
if(state.frontStencil.compareMask != 0xff)
296296
{
297297
value &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[0].testMaskQ));
298298
}
299299

300-
stencilTest(value, state.stencilCompareMode, false);
300+
stencilTest(value, state.frontStencil.compareOp, false);
301301

302302
if(state.twoSidedStencil)
303303
{
304-
if(!state.noStencilMaskCCW)
304+
if(state.backStencil.compareMask != 0xff)
305305
{
306-
valueCCW &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].testMaskQ));
306+
valueBack &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].testMaskQ));
307307
}
308308

309-
stencilTest(valueCCW, state.stencilCompareModeCCW, true);
309+
stencilTest(valueBack, state.backStencil.compareOp, true);
310310

311311
value &= *Pointer<Byte8>(primitive + OFFSET(Primitive,clockwiseMask));
312-
valueCCW &= *Pointer<Byte8>(primitive + OFFSET(Primitive,invClockwiseMask));
313-
value |= valueCCW;
312+
valueBack &= *Pointer<Byte8>(primitive + OFFSET(Primitive,invClockwiseMask));
313+
value |= valueBack;
314314
}
315315

316316
sMask = SignMask(value) & cMask;
317317
}
318318

319-
void PixelRoutine::stencilTest(Byte8 &value, VkCompareOp stencilCompareMode, bool CCW)
319+
void PixelRoutine::stencilTest(Byte8 &value, VkCompareOp stencilCompareMode, bool isBack)
320320
{
321321
Byte8 equal;
322322

@@ -330,31 +330,31 @@ namespace sw
330330
break;
331331
case VK_COMPARE_OP_LESS: // a < b ~ b > a
332332
value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
333-
value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
333+
value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[isBack].referenceMaskedSignedQ)));
334334
break;
335335
case VK_COMPARE_OP_EQUAL:
336-
value = CmpEQ(value, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
336+
value = CmpEQ(value, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[isBack].referenceMaskedQ)));
337337
break;
338338
case VK_COMPARE_OP_NOT_EQUAL: // a != b ~ !(a == b)
339-
value = CmpEQ(value, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
339+
value = CmpEQ(value, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[isBack].referenceMaskedQ)));
340340
value ^= Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
341341
break;
342342
case VK_COMPARE_OP_LESS_OR_EQUAL: // a <= b ~ (b > a) || (a == b)
343343
equal = value;
344-
equal = CmpEQ(equal, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedQ)));
344+
equal = CmpEQ(equal, *Pointer<Byte8>(data + OFFSET(DrawData,stencil[isBack].referenceMaskedQ)));
345345
value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
346-
value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
346+
value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[isBack].referenceMaskedSignedQ)));
347347
value |= equal;
348348
break;
349349
case VK_COMPARE_OP_GREATER: // a > b
350-
equal = *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ));
350+
equal = *Pointer<Byte8>(data + OFFSET(DrawData,stencil[isBack].referenceMaskedSignedQ));
351351
value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
352352
equal = CmpGT(As<SByte8>(equal), As<SByte8>(value));
353353
value = equal;
354354
break;
355355
case VK_COMPARE_OP_GREATER_OR_EQUAL: // a >= b ~ !(a < b) ~ !(b > a)
356356
value += Byte8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);
357-
value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[CCW].referenceMaskedSignedQ)));
357+
value = CmpGT(As<SByte8>(value), *Pointer<SByte8>(data + OFFSET(DrawData,stencil[isBack].referenceMaskedSignedQ)));
358358
value ^= Byte8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
359359
break;
360360
default:
@@ -729,15 +729,15 @@ namespace sw
729729
return;
730730
}
731731

732-
if(state.stencilPassOperation == VK_STENCIL_OP_KEEP && state.stencilZFailOperation == VK_STENCIL_OP_KEEP && state.stencilFailOperation == VK_STENCIL_OP_KEEP)
732+
if(state.frontStencil.passOp == VK_STENCIL_OP_KEEP && state.frontStencil.depthFailOp == VK_STENCIL_OP_KEEP && state.frontStencil.failOp == VK_STENCIL_OP_KEEP)
733733
{
734-
if(!state.twoSidedStencil || (state.stencilPassOperationCCW == VK_STENCIL_OP_KEEP && state.stencilZFailOperationCCW == VK_STENCIL_OP_KEEP && state.stencilFailOperationCCW == VK_STENCIL_OP_KEEP))
734+
if(!state.twoSidedStencil || (state.backStencil.passOp == VK_STENCIL_OP_KEEP && state.backStencil.depthFailOp == VK_STENCIL_OP_KEEP && state.backStencil.failOp == VK_STENCIL_OP_KEEP))
735735
{
736736
return;
737737
}
738738
}
739739

740-
if(state.stencilWriteMasked && (!state.twoSidedStencil || state.stencilWriteMaskedCCW))
740+
if((state.frontStencil.writeMask == 0) && (!state.twoSidedStencil || (state.backStencil.writeMask == 0)))
741741
{
742742
return;
743743
}
@@ -752,9 +752,9 @@ namespace sw
752752
Byte8 bufferValue = *Pointer<Byte8>(buffer);
753753

754754
Byte8 newValue;
755-
stencilOperation(newValue, bufferValue, state.stencilPassOperation, state.stencilZFailOperation, state.stencilFailOperation, false, zMask, sMask);
755+
stencilOperation(newValue, bufferValue, state.frontStencil, false, zMask, sMask);
756756

757-
if(!state.noStencilWriteMask)
757+
if(state.frontStencil.writeMask != 0)
758758
{
759759
Byte8 maskedValue = bufferValue;
760760
newValue &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[0].writeMaskQ));
@@ -764,21 +764,21 @@ namespace sw
764764

765765
if(state.twoSidedStencil)
766766
{
767-
Byte8 newValueCCW;
767+
Byte8 newValueBack;
768768

769-
stencilOperation(newValueCCW, bufferValue, state.stencilPassOperationCCW, state.stencilZFailOperationCCW, state.stencilFailOperationCCW, true, zMask, sMask);
769+
stencilOperation(newValueBack, bufferValue, state.backStencil, true, zMask, sMask);
770770

771-
if(!state.noStencilWriteMaskCCW)
771+
if(state.backStencil.writeMask != 0)
772772
{
773773
Byte8 maskedValue = bufferValue;
774-
newValueCCW &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].writeMaskQ));
774+
newValueBack &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].writeMaskQ));
775775
maskedValue &= *Pointer<Byte8>(data + OFFSET(DrawData,stencil[1].invWriteMaskQ));
776-
newValueCCW |= maskedValue;
776+
newValueBack |= maskedValue;
777777
}
778778

779779
newValue &= *Pointer<Byte8>(primitive + OFFSET(Primitive,clockwiseMask));
780-
newValueCCW &= *Pointer<Byte8>(primitive + OFFSET(Primitive,invClockwiseMask));
781-
newValue |= newValueCCW;
780+
newValueBack &= *Pointer<Byte8>(primitive + OFFSET(Primitive,invClockwiseMask));
781+
newValue |= newValueBack;
782782
}
783783

784784
newValue &= *Pointer<Byte8>(constants + OFFSET(Constants,maskB4Q) + 8 * cMask);
@@ -788,27 +788,27 @@ namespace sw
788788
*Pointer<Byte4>(buffer) = Byte4(newValue);
789789
}
790790

791-
void PixelRoutine::stencilOperation(Byte8 &newValue, Byte8 &bufferValue, VkStencilOp stencilPassOperation, VkStencilOp stencilZFailOperation, VkStencilOp stencilFailOperation, bool CCW, Int &zMask, Int &sMask)
791+
void PixelRoutine::stencilOperation(Byte8 &newValue, Byte8 &bufferValue, VkStencilOpState const &ops, bool isBack, Int &zMask, Int &sMask)
792792
{
793793
Byte8 &pass = newValue;
794794
Byte8 fail;
795795
Byte8 zFail;
796796

797-
stencilOperation(pass, bufferValue, stencilPassOperation, CCW);
797+
stencilOperation(pass, bufferValue, ops.passOp, isBack);
798798

799-
if(stencilZFailOperation != stencilPassOperation)
799+
if(ops.depthFailOp != ops.passOp)
800800
{
801-
stencilOperation(zFail, bufferValue, stencilZFailOperation, CCW);
801+
stencilOperation(zFail, bufferValue, ops.depthFailOp, isBack);
802802
}
803803

804-
if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
804+
if(ops.failOp != ops.passOp || ops.failOp != ops.depthFailOp)
805805
{
806-
stencilOperation(fail, bufferValue, stencilFailOperation, CCW);
806+
stencilOperation(fail, bufferValue, ops.failOp, isBack);
807807
}
808808

809-
if(stencilFailOperation != stencilPassOperation || stencilFailOperation != stencilZFailOperation)
809+
if(ops.failOp != ops.passOp || ops.failOp != ops.depthFailOp)
810810
{
811-
if(state.depthTestActive && stencilZFailOperation != stencilPassOperation) // zMask valid and values not the same
811+
if(state.depthTestActive && ops.depthFailOp != ops.passOp) // zMask valid and values not the same
812812
{
813813
pass &= *Pointer<Byte8>(constants + OFFSET(Constants,maskB4Q) + 8 * zMask);
814814
zFail &= *Pointer<Byte8>(constants + OFFSET(Constants,invMaskB4Q) + 8 * zMask);
@@ -821,7 +821,7 @@ namespace sw
821821
}
822822
}
823823

824-
void PixelRoutine::stencilOperation(Byte8 &output, Byte8 &bufferValue, VkStencilOp operation, bool CCW)
824+
void PixelRoutine::stencilOperation(Byte8 &output, Byte8 &bufferValue, VkStencilOp operation, bool isBack)
825825
{
826826
switch(operation)
827827
{
@@ -832,7 +832,7 @@ namespace sw
832832
output = Byte8(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
833833
break;
834834
case VK_STENCIL_OP_REPLACE:
835-
output = *Pointer<Byte8>(data + OFFSET(DrawData,stencil[CCW].referenceQ));
835+
output = *Pointer<Byte8>(data + OFFSET(DrawData,stencil[isBack].referenceQ));
836836
break;
837837
case VK_STENCIL_OP_INCREMENT_AND_CLAMP:
838838
output = AddSat(bufferValue, Byte8(1, 1, 1, 1, 1, 1, 1, 1));

src/Pipeline/PixelRoutine.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ namespace sw
6464
private:
6565
Float4 interpolateCentroid(Float4 &x, Float4 &y, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective);
6666
void stencilTest(Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &cMask);
67-
void stencilTest(Byte8 &value, VkCompareOp stencilCompareMode, bool CCW);
68-
void stencilOperation(Byte8 &newValue, Byte8 &bufferValue, VkStencilOp stencilPassOperation, VkStencilOp stencilZFailOperation, VkStencilOp stencilFailOperation, bool CCW, Int &zMask, Int &sMask);
69-
void stencilOperation(Byte8 &output, Byte8 &bufferValue, VkStencilOp operation, bool CCW);
67+
void stencilTest(Byte8 &value, VkCompareOp stencilCompareMode, bool isBack);
68+
void stencilOperation(Byte8 &newValue, Byte8 &bufferValue, VkStencilOpState const &ops, bool isBack, Int &zMask, Int &sMask);
69+
void stencilOperation(Byte8 &output, Byte8 &bufferValue, VkStencilOp operation, bool isBack);
7070
Bool depthTest(Pointer<Byte> &zBuffer, int q, Int &x, Float4 &z, Int &sMask, Int &zMask, Int &cMask);
7171

7272
// Raster operations

src/Vulkan/VkPipeline.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -381,21 +381,8 @@ GraphicsPipeline::GraphicsPipeline(const VkGraphicsPipelineCreateInfo* pCreateIn
381381
context.stencilEnable = context.twoSidedStencil = depthStencilState->stencilTestEnable;
382382
if(context.stencilEnable)
383383
{
384-
context.stencilMask = depthStencilState->front.compareMask;
385-
context.stencilCompareMode = depthStencilState->front.compareOp;
386-
context.stencilZFailOperation = depthStencilState->front.depthFailOp;
387-
context.stencilFailOperation = depthStencilState->front.failOp;
388-
context.stencilPassOperation = depthStencilState->front.passOp;
389-
context.stencilReference = depthStencilState->front.reference;
390-
context.stencilWriteMask = depthStencilState->front.writeMask;
391-
392-
context.stencilMaskCCW = depthStencilState->back.compareMask;
393-
context.stencilCompareModeCCW = depthStencilState->back.compareOp;
394-
context.stencilZFailOperationCCW = depthStencilState->back.depthFailOp;
395-
context.stencilFailOperationCCW = depthStencilState->back.failOp;
396-
context.stencilPassOperationCCW = depthStencilState->back.passOp;
397-
context.stencilReferenceCCW = depthStencilState->back.reference;
398-
context.stencilWriteMaskCCW = depthStencilState->back.writeMask;
384+
context.frontStencil = depthStencilState->front;
385+
context.backStencil = depthStencilState->back;
399386
}
400387
}
401388

0 commit comments

Comments
 (0)