Remove StencilReferenceMode. Add SetStencilReference.

This moves the application of the stencil reference from the
DepthStencilState to Command::SetStencilReference
This commit is contained in:
Austin Eng 2017-05-30 18:13:03 -04:00 committed by Corentin Wallez
parent 3efcf2172d
commit 4f5521e440
11 changed files with 89 additions and 76 deletions

View File

@ -170,10 +170,6 @@ void init() {
.SetInput(1, 6 * sizeof(float), nxt::InputStepMode::Vertex) .SetInput(1, 6 * sizeof(float), nxt::InputStepMode::Vertex)
.GetResult(); .GetResult();
auto depthStencilState = device.CreateDepthStencilStateBuilder()
.SetDepthEnabled(true)
.GetResult();
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder() nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
.SetBindingsType(nxt::ShaderStageBit::Vertex, nxt::BindingType::UniformBuffer, 0, 2) .SetBindingsType(nxt::ShaderStageBit::Vertex, nxt::BindingType::UniformBuffer, 0, 2)
.GetResult(); .GetResult();
@ -236,6 +232,12 @@ void init() {
.GetResult(); .GetResult();
CreateDefaultRenderPass(device, &renderpass, &framebuffer); CreateDefaultRenderPass(device, &renderpass, &framebuffer);
auto depthStencilState = device.CreateDepthStencilStateBuilder()
.SetDepthEnabled(true)
.SetStencilEnabled(false)
.GetResult();
pipeline = device.CreatePipelineBuilder() pipeline = device.CreatePipelineBuilder()
.SetSubpass(renderpass, 0) .SetSubpass(renderpass, 0)
.SetLayout(pl) .SetLayout(pl)
@ -249,8 +251,6 @@ void init() {
.SetDepthEnabled(true) .SetDepthEnabled(true)
.SetDepthWrite(nxt::DepthWriteMode::Disabled) .SetDepthWrite(nxt::DepthWriteMode::Disabled)
.SetStencilEnabled(true) .SetStencilEnabled(true)
.SetStencilReferenceMode(nxt::StencilReferenceMode::Static)
.SetStencilReference(nxt::Face::Both, 1)
.SetStencilCompareFunction(nxt::Face::Both, nxt::CompareFunction::Always) .SetStencilCompareFunction(nxt::Face::Both, nxt::CompareFunction::Always)
.SetStencilOperation(nxt::Face::Both, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace) .SetStencilOperation(nxt::Face::Both, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace)
.SetStencilMask(nxt::Face::Both, 0xff, 0xff) .SetStencilMask(nxt::Face::Both, 0xff, 0xff)
@ -269,8 +269,6 @@ void init() {
.SetDepthEnabled(true) .SetDepthEnabled(true)
.SetDepthWrite(nxt::DepthWriteMode::Enabled) .SetDepthWrite(nxt::DepthWriteMode::Enabled)
.SetStencilEnabled(true) .SetStencilEnabled(true)
.SetStencilReferenceMode(nxt::StencilReferenceMode::Static)
.SetStencilReference(nxt::Face::Both, 1)
.SetStencilCompareFunction(nxt::Face::Both, nxt::CompareFunction::Equal) .SetStencilCompareFunction(nxt::Face::Both, nxt::CompareFunction::Equal)
.SetStencilOperation(nxt::Face::Both, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace) .SetStencilOperation(nxt::Face::Both, nxt::StencilOperation::Keep, nxt::StencilOperation::Keep, nxt::StencilOperation::Replace)
.SetStencilMask(nxt::Face::Both, 0xff, 0x00) .SetStencilMask(nxt::Face::Both, 0xff, 0x00)
@ -314,10 +312,12 @@ void frame() {
.DrawElements(36, 1, 0, 0) .DrawElements(36, 1, 0, 0)
.SetPipeline(planePipeline) .SetPipeline(planePipeline)
.SetStencilReference(0x1, 0x1)
.SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets) .SetVertexBuffers(0, 1, &planeBuffer, vertexBufferOffsets)
.DrawElements(6, 1, 0, 0) .DrawElements(6, 1, 0, 0)
.SetPipeline(reflectionPipeline) .SetPipeline(reflectionPipeline)
.SetStencilReference(0x1, 0x1)
.SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets) .SetVertexBuffers(0, 1, &vertexBuffer, vertexBufferOffsets)
.SetBindGroup(0, bindGroup[1]) .SetBindGroup(0, bindGroup[1])
.DrawElements(36, 1, 0, 0) .DrawElements(36, 1, 0, 0)

View File

@ -279,6 +279,13 @@
{"name": "first instance", "type": "uint32_t"} {"name": "first instance", "type": "uint32_t"}
] ]
}, },
{
"name": "set stencil reference",
"args": [
{"name": "back reference", "type": "uint32_t"},
{"name": "front reference", "type": "uint32_t"}
]
},
{ {
"name": "set bind group", "name": "set bind group",
"args": [ "args": [
@ -465,12 +472,6 @@
{"name": "stencil enabled", "type": "bool"} {"name": "stencil enabled", "type": "bool"}
] ]
}, },
{
"name": "set stencil reference mode",
"args": [
{"name": "reference mode", "type": "stencil reference mode"}
]
},
{ {
"name": "set stencil operation", "name": "set stencil operation",
"args": [ "args": [
@ -494,13 +495,6 @@
{"name": "read mask", "type": "uint32_t"}, {"name": "read mask", "type": "uint32_t"},
{"name": "write mask", "type": "uint32_t"} {"name": "write mask", "type": "uint32_t"}
] ]
},
{
"name": "set stencil reference",
"args": [
{"name": "face", "type": "face"},
{"name": "reference", "type": "int"}
]
} }
] ]
}, },
@ -788,13 +782,6 @@
{"value": 4, "name": "compute"} {"value": 4, "name": "compute"}
] ]
}, },
"stencil reference mode": {
"category": "enum",
"values": [
{"value": 0, "name": "static"},
{"value": 1, "name": "dynamic"}
]
},
"stencil operation": { "stencil operation": {
"category": "enum", "category": "enum",
"values": [ "values": [
@ -928,8 +915,5 @@
}, },
"bool": { "bool": {
"category": "native" "category": "native"
},
"int": {
"category": "native"
} }
} }

View File

@ -110,6 +110,12 @@ namespace backend {
cmd->~SetPushConstantsCmd(); cmd->~SetPushConstantsCmd();
} }
break; break;
case Command::SetStencilReference:
{
SetStencilReferenceCmd* cmd = commands->NextCommand<SetStencilReferenceCmd>();
cmd->~SetStencilReferenceCmd();
}
break;
case Command::SetBindGroup: case Command::SetBindGroup:
{ {
SetBindGroupCmd* cmd = commands->NextCommand<SetBindGroupCmd>(); SetBindGroupCmd* cmd = commands->NextCommand<SetBindGroupCmd>();
@ -564,6 +570,12 @@ namespace backend {
} }
break; break;
case Command::SetStencilReference:
{
SetStencilReferenceCmd* cmd = iterator.NextCommand<SetStencilReferenceCmd>();
}
break;
case Command::SetBindGroup: case Command::SetBindGroup:
{ {
SetBindGroupCmd* cmd = iterator.NextCommand<SetBindGroupCmd>(); SetBindGroupCmd* cmd = iterator.NextCommand<SetBindGroupCmd>();
@ -754,6 +766,13 @@ namespace backend {
memcpy(values, data, count * sizeof(uint32_t)); memcpy(values, data, count * sizeof(uint32_t));
} }
void CommandBufferBuilder::SetStencilReference(uint32_t backReference, uint32_t frontReference) {
SetStencilReferenceCmd* cmd = allocator.Allocate<SetStencilReferenceCmd>(Command::SetStencilReference);
new(cmd) SetStencilReferenceCmd;
cmd->backReference = backReference;
cmd->frontReference = frontReference;
}
void CommandBufferBuilder::SetBindGroup(uint32_t groupIndex, BindGroupBase* group) { void CommandBufferBuilder::SetBindGroup(uint32_t groupIndex, BindGroupBase* group) {
if (groupIndex >= kMaxBindGroups) { if (groupIndex >= kMaxBindGroups) {
HandleError("Setting bind group over the max"); HandleError("Setting bind group over the max");

View File

@ -68,6 +68,7 @@ namespace backend {
void EndRenderPass(); void EndRenderPass();
void SetPushConstants(nxt::ShaderStageBit stage, uint32_t offset, uint32_t count, const void* data); void SetPushConstants(nxt::ShaderStageBit stage, uint32_t offset, uint32_t count, const void* data);
void SetPipeline(PipelineBase* pipeline); void SetPipeline(PipelineBase* pipeline);
void SetStencilReference(uint32_t backReference, uint32_t frontReference);
void SetBindGroup(uint32_t groupIndex, BindGroupBase* group); void SetBindGroup(uint32_t groupIndex, BindGroupBase* group);
void SetIndexBuffer(BufferBase* buffer, uint32_t offset, nxt::IndexFormat format); void SetIndexBuffer(BufferBase* buffer, uint32_t offset, nxt::IndexFormat format);

View File

@ -37,6 +37,7 @@ namespace backend {
EndRenderPass, EndRenderPass,
SetPipeline, SetPipeline,
SetPushConstants, SetPushConstants,
SetStencilReference,
SetBindGroup, SetBindGroup,
SetIndexBuffer, SetIndexBuffer,
SetVertexBuffers, SetVertexBuffers,
@ -94,6 +95,11 @@ namespace backend {
uint32_t count; uint32_t count;
}; };
struct SetStencilReferenceCmd {
uint32_t backReference;
uint32_t frontReference;
};
struct SetBindGroupCmd { struct SetBindGroupCmd {
uint32_t index; uint32_t index;
Ref<BindGroupBase> group; Ref<BindGroupBase> group;

View File

@ -34,10 +34,6 @@ namespace backend {
return stencilEnabled; return stencilEnabled;
} }
nxt::StencilReferenceMode DepthStencilStateBase::GetStencilReferenceMode() const {
return referenceMode;
}
const DepthStencilStateBase::DepthInfo& DepthStencilStateBase::GetDepth() const { const DepthStencilStateBase::DepthInfo& DepthStencilStateBase::GetDepth() const {
return depthInfo; return depthInfo;
} }
@ -79,10 +75,6 @@ namespace backend {
this->stencilEnabled = stencilEnabled; this->stencilEnabled = stencilEnabled;
} }
void DepthStencilStateBuilder::SetStencilReferenceMode(nxt::StencilReferenceMode referenceMode) {
this->referenceMode = referenceMode;
}
void DepthStencilStateBuilder::SetStencilOperation(nxt::Face face, nxt::StencilOperation stencilFail, void DepthStencilStateBuilder::SetStencilOperation(nxt::Face face, nxt::StencilOperation stencilFail,
nxt::StencilOperation depthFail, nxt::StencilOperation stencilPass) { nxt::StencilOperation depthFail, nxt::StencilOperation stencilPass) {
if (face & nxt::Face::Back) { if (face & nxt::Face::Back) {
@ -123,15 +115,4 @@ namespace backend {
} }
} }
void DepthStencilStateBuilder::SetStencilReference(nxt::Face face, int reference) {
if (face & nxt::Face::Back) {
auto& stencilInfo = stencilInfos[0];
stencilInfo.reference = reference;
}
if (face & nxt::Face::Front) {
auto& stencilInfo = stencilInfos[1];
stencilInfo.reference = reference;
}
}
} }

View File

@ -40,19 +40,16 @@ namespace backend {
nxt::StencilOperation stencilPass = nxt::StencilOperation::Keep; nxt::StencilOperation stencilPass = nxt::StencilOperation::Keep;
uint32_t readMask = 0xff; uint32_t readMask = 0xff;
uint32_t writeMask = 0xff; uint32_t writeMask = 0xff;
int reference = 0;
}; };
bool DepthIsEnabled() const; bool DepthIsEnabled() const;
bool StencilIsEnabled() const; bool StencilIsEnabled() const;
nxt::StencilReferenceMode GetStencilReferenceMode() const;
const DepthInfo& GetDepth() const; const DepthInfo& GetDepth() const;
const StencilInfo& GetStencil(nxt::Face face) const; const StencilInfo& GetStencil(nxt::Face face) const;
private: private:
bool depthEnabled = false; bool depthEnabled = false;
bool stencilEnabled = false; bool stencilEnabled = false;
nxt::StencilReferenceMode referenceMode = nxt::StencilReferenceMode::Static;
DepthInfo depthInfo; DepthInfo depthInfo;
StencilInfo stencilInfos[2]; StencilInfo stencilInfos[2];
}; };
@ -66,12 +63,10 @@ namespace backend {
void SetDepthCompareFunction(nxt::CompareFunction depthCompareFunction); void SetDepthCompareFunction(nxt::CompareFunction depthCompareFunction);
void SetDepthWrite(nxt::DepthWriteMode depthWriteMode); void SetDepthWrite(nxt::DepthWriteMode depthWriteMode);
void SetStencilEnabled(bool stencilEnabled); void SetStencilEnabled(bool stencilEnabled);
void SetStencilReferenceMode(nxt::StencilReferenceMode referenceMode);
void SetStencilOperation(nxt::Face face, nxt::StencilOperation stencilFail, void SetStencilOperation(nxt::Face face, nxt::StencilOperation stencilFail,
nxt::StencilOperation depthFail, nxt::StencilOperation stencilPass); nxt::StencilOperation depthFail, nxt::StencilOperation stencilPass);
void SetStencilCompareFunction(nxt::Face face, nxt::CompareFunction stencilCompareFunction); void SetStencilCompareFunction(nxt::Face face, nxt::CompareFunction stencilCompareFunction);
void SetStencilMask(nxt::Face face, uint32_t readMask, uint32_t writeMask); void SetStencilMask(nxt::Face face, uint32_t readMask, uint32_t writeMask);
void SetStencilReference(nxt::Face face, int reference);
private: private:
friend class DepthStencilStateBase; friend class DepthStencilStateBase;
@ -80,7 +75,6 @@ namespace backend {
bool depthEnabled; bool depthEnabled;
bool stencilEnabled; bool stencilEnabled;
nxt::StencilReferenceMode referenceMode;
DepthStencilStateBase::DepthInfo depthInfo; DepthStencilStateBase::DepthInfo depthInfo;
DepthStencilStateBase::StencilInfo stencilInfos[2]; DepthStencilStateBase::StencilInfo stencilInfos[2];
}; };

View File

@ -178,6 +178,14 @@ namespace opengl {
} }
break; break;
case Command::SetStencilReference:
{
SetStencilReferenceCmd* cmd = commands.NextCommand<SetStencilReferenceCmd>();
DepthStencilState* depthStencilState = ToBackend(lastPipeline->GetDepthStencilState());
depthStencilState->ApplyStencilReferenceNow(cmd->backReference, cmd->frontReference);
}
break;
case Command::SetBindGroup: case Command::SetBindGroup:
{ {
SetBindGroupCmd* cmd = commands.NextCommand<SetBindGroupCmd>(); SetBindGroupCmd* cmd = commands.NextCommand<SetBindGroupCmd>();

View File

@ -184,12 +184,14 @@ namespace opengl {
: BufferViewBase(builder), device(device) { : BufferViewBase(builder), device(device) {
} }
// DepthStencilState
DepthStencilState::DepthStencilState(Device* device, DepthStencilStateBuilder* builder) DepthStencilState::DepthStencilState(Device* device, DepthStencilStateBuilder* builder)
: DepthStencilStateBase(builder), device(device) { : DepthStencilStateBase(builder), device(device) {
} }
void DepthStencilState::Apply() { void DepthStencilState::ApplyNow() {
if (DepthIsEnabled()) { if (DepthIsEnabled()) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
auto& depth = GetDepth(); auto& depth = GetDepth();
@ -210,30 +212,47 @@ namespace opengl {
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
} }
static const GLuint GL_FACES[2] = { GL_BACK, GL_FRONT };
static const nxt::Face NXT_FACES[2] = { nxt::Face::Back, nxt::Face::Front };
if (StencilIsEnabled()) { if (StencilIsEnabled()) {
glEnable(GL_STENCIL_TEST); glEnable(GL_STENCIL_TEST);
for (uint32_t i = 0; i < 2; ++i) { auto& back = GetStencil(nxt::Face::Back);
auto& stencil = GetStencil(NXT_FACES[i]); auto& front = GetStencil(nxt::Face::Front);
glStencilFuncSeparate(GL_FACES[i],
OpenGLCompareFunction(stencil.compareFunction), glStencilOpSeparate(GL_BACK,
stencil.reference, OpenGLStencilOperation(back.stencilFail),
stencil.readMask OpenGLStencilOperation(back.depthFail),
); OpenGLStencilOperation(back.stencilPass)
glStencilOpSeparate(GL_FACES[i], );
OpenGLStencilOperation(stencil.stencilFail), glStencilOpSeparate(GL_FRONT,
OpenGLStencilOperation(stencil.depthFail), OpenGLStencilOperation(front.stencilFail),
OpenGLStencilOperation(stencil.stencilPass) OpenGLStencilOperation(front.depthFail),
); OpenGLStencilOperation(front.stencilPass)
glStencilMaskSeparate(GL_FACES[i], stencil.writeMask); );
}
glStencilMaskSeparate(GL_BACK, back.writeMask);
glStencilMaskSeparate(GL_FRONT, front.writeMask);
} }
else { else {
glDisable(GL_STENCIL_TEST); glDisable(GL_STENCIL_TEST);
} }
} }
void DepthStencilState::ApplyStencilReferenceNow(uint32_t backReference, uint32_t frontReference) {
if (StencilIsEnabled()) {
auto& back = GetStencil(nxt::Face::Back);
auto& front = GetStencil(nxt::Face::Front);
glStencilFuncSeparate(GL_BACK,
OpenGLCompareFunction(back.compareFunction),
backReference,
back.readMask
);
glStencilFuncSeparate(GL_FRONT,
OpenGLCompareFunction(front.compareFunction),
frontReference,
front.readMask
);
}
}
// InputState // InputState
InputState::InputState(Device* device, InputStateBuilder* builder) InputState::InputState(Device* device, InputStateBuilder* builder)

View File

@ -139,7 +139,8 @@ namespace opengl {
class DepthStencilState : public DepthStencilStateBase { class DepthStencilState : public DepthStencilStateBase {
public: public:
DepthStencilState(Device* device, DepthStencilStateBuilder* builder); DepthStencilState(Device* device, DepthStencilStateBuilder* builder);
void Apply(); void ApplyNow();
void ApplyStencilReferenceNow(uint32_t backReference, uint32_t frontReference);
private: private:
Device* device; Device* device;

View File

@ -208,7 +208,7 @@ namespace opengl {
auto inputState = ToBackend(GetInputState()); auto inputState = ToBackend(GetInputState());
glBindVertexArray(inputState->GetVAO()); glBindVertexArray(inputState->GetVAO());
auto depthStencilState = ToBackend(GetDepthStencilState()); auto depthStencilState = ToBackend(GetDepthStencilState());
depthStencilState->Apply(); depthStencilState->ApplyNow();
} }
} }