Use explicit front and back stencil state.

Previously stencil states were stored in array. This commit changes the
DepthStencilState to explicitly store a front and back stencil.
This commit is contained in:
Austin Eng 2017-05-31 09:29:05 -04:00 committed by Corentin Wallez
parent 376f1c6a8e
commit f51be34864
4 changed files with 37 additions and 43 deletions

View File

@ -22,8 +22,8 @@ namespace backend {
DepthStencilStateBase::DepthStencilStateBase(DepthStencilStateBuilder* builder) DepthStencilStateBase::DepthStencilStateBase(DepthStencilStateBuilder* builder)
: depthEnabled(builder->depthEnabled), stencilEnabled(builder->stencilEnabled), : depthEnabled(builder->depthEnabled), stencilEnabled(builder->stencilEnabled),
depthInfo(builder->depthInfo), depthInfo(builder->depthInfo), backStencilInfo(builder->backStencilInfo),
stencilInfos { builder->stencilInfos[0], builder->stencilInfos[1] } { frontStencilInfo(builder->frontStencilInfo) {
} }
bool DepthStencilStateBase::DepthIsEnabled() const { bool DepthStencilStateBase::DepthIsEnabled() const {
@ -38,15 +38,12 @@ namespace backend {
return depthInfo; return depthInfo;
} }
const DepthStencilStateBase::StencilInfo& DepthStencilStateBase::GetStencil(nxt::Face face) const { const DepthStencilStateBase::StencilInfo& DepthStencilStateBase::GetBackStencil() const {
switch (face) { return backStencilInfo;
case nxt::Face::Back: }
return stencilInfos[0];
case nxt::Face::Front: const DepthStencilStateBase::StencilInfo& DepthStencilStateBase::GetFrontStencil() const {
return stencilInfos[1]; return frontStencilInfo;
default:
ASSERT(false);
}
} }
@ -78,40 +75,34 @@ namespace backend {
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) {
auto& stencilInfo = stencilInfos[0]; backStencilInfo.stencilFail = stencilFail;
stencilInfo.stencilFail = stencilFail; backStencilInfo.depthFail = stencilFail;
stencilInfo.depthFail = stencilFail; backStencilInfo.stencilPass = stencilPass;
stencilInfo.stencilPass = stencilPass;
} }
if (face & nxt::Face::Front) { if (face & nxt::Face::Front) {
auto& stencilInfo = stencilInfos[1]; frontStencilInfo.stencilFail = stencilFail;
stencilInfo.stencilFail = stencilFail; frontStencilInfo.depthFail = stencilFail;
stencilInfo.depthFail = stencilFail; frontStencilInfo.stencilPass = stencilPass;
stencilInfo.stencilPass = stencilPass;
} }
} }
void DepthStencilStateBuilder::SetStencilCompareFunction(nxt::Face face, nxt::CompareFunction stencilCompareFunction) { void DepthStencilStateBuilder::SetStencilCompareFunction(nxt::Face face, nxt::CompareFunction stencilCompareFunction) {
if (face & nxt::Face::Back) { if (face & nxt::Face::Back) {
auto& stencilInfo = stencilInfos[0]; backStencilInfo.compareFunction = stencilCompareFunction;
stencilInfo.compareFunction = stencilCompareFunction;
} }
if (face & nxt::Face::Front) { if (face & nxt::Face::Front) {
auto& stencilInfo = stencilInfos[1]; frontStencilInfo.compareFunction = stencilCompareFunction;
stencilInfo.compareFunction = stencilCompareFunction;
} }
} }
void DepthStencilStateBuilder::SetStencilMask(nxt::Face face, uint32_t readMask, uint32_t writeMask) { void DepthStencilStateBuilder::SetStencilMask(nxt::Face face, uint32_t readMask, uint32_t writeMask) {
if (face & nxt::Face::Back) { if (face & nxt::Face::Back) {
auto& stencilInfo = stencilInfos[0]; backStencilInfo.readMask = readMask;
stencilInfo.readMask = readMask; backStencilInfo.writeMask = writeMask;
stencilInfo.writeMask = writeMask;
} }
if (face & nxt::Face::Front) { if (face & nxt::Face::Front) {
auto& stencilInfo = stencilInfos[1]; frontStencilInfo.readMask = readMask;
stencilInfo.readMask = readMask; frontStencilInfo.writeMask = writeMask;
stencilInfo.writeMask = writeMask;
} }
} }

View File

@ -45,13 +45,15 @@ namespace backend {
bool DepthIsEnabled() const; bool DepthIsEnabled() const;
bool StencilIsEnabled() const; bool StencilIsEnabled() const;
const DepthInfo& GetDepth() const; const DepthInfo& GetDepth() const;
const StencilInfo& GetStencil(nxt::Face face) const; const StencilInfo& GetBackStencil() const;
const StencilInfo& GetFrontStencil() const;
private: private:
bool depthEnabled = false; bool depthEnabled = false;
bool stencilEnabled = false; bool stencilEnabled = false;
DepthInfo depthInfo; DepthInfo depthInfo;
StencilInfo stencilInfos[2]; StencilInfo backStencilInfo;
StencilInfo frontStencilInfo;
}; };
class DepthStencilStateBuilder : public Builder<DepthStencilStateBase> { class DepthStencilStateBuilder : public Builder<DepthStencilStateBase> {
@ -63,7 +65,7 @@ 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 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);
@ -76,7 +78,8 @@ namespace backend {
bool depthEnabled; bool depthEnabled;
bool stencilEnabled; bool stencilEnabled;
DepthStencilStateBase::DepthInfo depthInfo; DepthStencilStateBase::DepthInfo depthInfo;
DepthStencilStateBase::StencilInfo stencilInfos[2]; DepthStencilStateBase::StencilInfo backStencilInfo;
DepthStencilStateBase::StencilInfo frontStencilInfo;
}; };
} }

View File

@ -478,7 +478,7 @@ namespace metal {
ASSERT(encoders.render); ASSERT(encoders.render);
[encoders.render [encoders.render
setStencilFrontReferenceValue:cmd->frontReference setStencilFrontReferenceValue:cmd->frontReference
backReferenceValue:cmd->backReference]; backReferenceValue:cmd->backReference];
} }
@ -706,7 +706,7 @@ namespace metal {
return MTLStencilOperationIncrementWrap; return MTLStencilOperationIncrementWrap;
case nxt::StencilOperation::DecrementWrap: case nxt::StencilOperation::DecrementWrap:
return MTLStencilOperationDecrementWrap; return MTLStencilOperationDecrementWrap;
default: default:
ASSERT(false); ASSERT(false);
} }
} }
@ -735,9 +735,9 @@ namespace metal {
MTLStencilDescriptor* backFaceStencil = [MTLStencilDescriptor new]; MTLStencilDescriptor* backFaceStencil = [MTLStencilDescriptor new];
MTLStencilDescriptor* frontFaceStencil = [MTLStencilDescriptor new]; MTLStencilDescriptor* frontFaceStencil = [MTLStencilDescriptor new];
auto& back = GetStencil(nxt::Face::Back); auto& back = GetBackStencil();
auto& front = GetStencil(nxt::Face::Front); auto& front = GetFrontStencil();
backFaceStencil.stencilCompareFunction = DepthStencilCompareFunction(back.compareFunction); backFaceStencil.stencilCompareFunction = DepthStencilCompareFunction(back.compareFunction);
backFaceStencil.stencilFailureOperation = StencilOperation(back.stencilFail); backFaceStencil.stencilFailureOperation = StencilOperation(back.stencilFail);
backFaceStencil.depthFailureOperation = StencilOperation(back.depthFail); backFaceStencil.depthFailureOperation = StencilOperation(back.depthFail);
@ -913,7 +913,7 @@ namespace metal {
builder->HandleError("Error creating pipeline state"); builder->HandleError("Error creating pipeline state");
return; return;
} }
DepthStencilState* depthStencilState = ToBackend(GetDepthStencilState()); DepthStencilState* depthStencilState = ToBackend(GetDepthStencilState());
MTLDepthStencilDescriptor* dsDesc = depthStencilState->GetMTLDepthStencilDescriptor(); MTLDepthStencilDescriptor* dsDesc = depthStencilState->GetMTLDepthStencilDescriptor();
mtlDepthStencilState = [device->GetMTLDevice() mtlDepthStencilState = [device->GetMTLDevice()

View File

@ -214,8 +214,8 @@ namespace opengl {
if (StencilIsEnabled()) { if (StencilIsEnabled()) {
glEnable(GL_STENCIL_TEST); glEnable(GL_STENCIL_TEST);
auto& back = GetStencil(nxt::Face::Back); auto& back = GetBackStencil();
auto& front = GetStencil(nxt::Face::Front); auto& front = GetFrontStencil();
glStencilOpSeparate(GL_BACK, glStencilOpSeparate(GL_BACK,
OpenGLStencilOperation(back.stencilFail), OpenGLStencilOperation(back.stencilFail),
@ -238,8 +238,8 @@ namespace opengl {
void DepthStencilState::ApplyStencilReferenceNow(uint32_t backReference, uint32_t frontReference) { void DepthStencilState::ApplyStencilReferenceNow(uint32_t backReference, uint32_t frontReference) {
if (StencilIsEnabled()) { if (StencilIsEnabled()) {
auto& back = GetStencil(nxt::Face::Back); auto& back = GetBackStencil();
auto& front = GetStencil(nxt::Face::Front); auto& front = GetFrontStencil();
glStencilFuncSeparate(GL_BACK, glStencilFuncSeparate(GL_BACK,
OpenGLCompareFunction(back.compareFunction), OpenGLCompareFunction(back.compareFunction),
backReference, backReference,