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

View File

@ -279,6 +279,13 @@
{"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",
"args": [
@ -465,12 +472,6 @@
{"name": "stencil enabled", "type": "bool"}
]
},
{
"name": "set stencil reference mode",
"args": [
{"name": "reference mode", "type": "stencil reference mode"}
]
},
{
"name": "set stencil operation",
"args": [
@ -494,13 +495,6 @@
{"name": "read 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"}
]
},
"stencil reference mode": {
"category": "enum",
"values": [
{"value": 0, "name": "static"},
{"value": 1, "name": "dynamic"}
]
},
"stencil operation": {
"category": "enum",
"values": [
@ -928,8 +915,5 @@
},
"bool": {
"category": "native"
},
"int": {
"category": "native"
}
}

View File

@ -110,6 +110,12 @@ namespace backend {
cmd->~SetPushConstantsCmd();
}
break;
case Command::SetStencilReference:
{
SetStencilReferenceCmd* cmd = commands->NextCommand<SetStencilReferenceCmd>();
cmd->~SetStencilReferenceCmd();
}
break;
case Command::SetBindGroup:
{
SetBindGroupCmd* cmd = commands->NextCommand<SetBindGroupCmd>();
@ -563,6 +569,12 @@ namespace backend {
}
}
break;
case Command::SetStencilReference:
{
SetStencilReferenceCmd* cmd = iterator.NextCommand<SetStencilReferenceCmd>();
}
break;
case Command::SetBindGroup:
{
@ -754,6 +766,13 @@ namespace backend {
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) {
if (groupIndex >= kMaxBindGroups) {
HandleError("Setting bind group over the max");

View File

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

View File

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

View File

@ -34,10 +34,6 @@ namespace backend {
return stencilEnabled;
}
nxt::StencilReferenceMode DepthStencilStateBase::GetStencilReferenceMode() const {
return referenceMode;
}
const DepthStencilStateBase::DepthInfo& DepthStencilStateBase::GetDepth() const {
return depthInfo;
}
@ -79,10 +75,6 @@ namespace backend {
this->stencilEnabled = stencilEnabled;
}
void DepthStencilStateBuilder::SetStencilReferenceMode(nxt::StencilReferenceMode referenceMode) {
this->referenceMode = referenceMode;
}
void DepthStencilStateBuilder::SetStencilOperation(nxt::Face face, nxt::StencilOperation stencilFail,
nxt::StencilOperation depthFail, nxt::StencilOperation stencilPass) {
if (face & nxt::Face::Back) {
@ -122,16 +114,5 @@ namespace backend {
stencilInfo.writeMask = writeMask;
}
}
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;
uint32_t readMask = 0xff;
uint32_t writeMask = 0xff;
int reference = 0;
};
bool DepthIsEnabled() const;
bool StencilIsEnabled() const;
nxt::StencilReferenceMode GetStencilReferenceMode() const;
const DepthInfo& GetDepth() const;
const StencilInfo& GetStencil(nxt::Face face) const;
private:
bool depthEnabled = false;
bool stencilEnabled = false;
nxt::StencilReferenceMode referenceMode = nxt::StencilReferenceMode::Static;
DepthInfo depthInfo;
StencilInfo stencilInfos[2];
};
@ -66,12 +63,10 @@ namespace backend {
void SetDepthCompareFunction(nxt::CompareFunction depthCompareFunction);
void SetDepthWrite(nxt::DepthWriteMode depthWriteMode);
void SetStencilEnabled(bool stencilEnabled);
void SetStencilReferenceMode(nxt::StencilReferenceMode referenceMode);
void SetStencilOperation(nxt::Face face, nxt::StencilOperation stencilFail,
nxt::StencilOperation depthFail, nxt::StencilOperation stencilPass);
void SetStencilCompareFunction(nxt::Face face, nxt::CompareFunction stencilCompareFunction);
void SetStencilMask(nxt::Face face, uint32_t readMask, uint32_t writeMask);
void SetStencilReference(nxt::Face face, int reference);
private:
friend class DepthStencilStateBase;
@ -80,7 +75,6 @@ namespace backend {
bool depthEnabled;
bool stencilEnabled;
nxt::StencilReferenceMode referenceMode;
DepthStencilStateBase::DepthInfo depthInfo;
DepthStencilStateBase::StencilInfo stencilInfos[2];
};

View File

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

View File

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

View File

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

View File

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