Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dynamic stencil reference to command list #42

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/nvrhi/nvrhi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,7 @@ namespace nvrhi
virtual void setPushConstants(const void* data, size_t byteSize) = 0;

virtual void setGraphicsState(const GraphicsState& state) = 0;
virtual void setStencilRefValue(uint8_t value) = 0;
virtual void draw(const DrawArguments& args) = 0;
virtual void drawIndexed(const DrawArguments& args) = 0;
virtual void drawIndirect(uint32_t offsetBytes, uint32_t drawCount = 1) = 0;
Expand Down
1 change: 1 addition & 0 deletions src/d3d11/d3d11-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ namespace nvrhi::d3d11
void setPushConstants(const void* data, size_t byteSize) override;

void setGraphicsState(const GraphicsState& state) override;
void setStencilRefValue(uint8_t value) override;
void draw(const DrawArguments& args) override;
void drawIndexed(const DrawArguments& args) override;
void drawIndirect(uint32_t offsetBytes, uint32_t drawCount) override;
Expand Down
6 changes: 6 additions & 0 deletions src/d3d11/d3d11-graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ namespace nvrhi::d3d11
}
}

void CommandList::setStencilRefValue(uint8_t value)
{
GraphicsPipeline* pipeline = checked_cast<GraphicsPipeline*>(m_CurrentGraphicsPipeline.Get());
m_Context.immediateContext->OMSetDepthStencilState(pipeline->pDepthStencilState, value);
}

void CommandList::draw(const DrawArguments& args)
{
m_Context.immediateContext->DrawInstanced(args.vertexCount, args.instanceCount, args.startVertexLocation, args.startInstanceLocation);
Expand Down
1 change: 1 addition & 0 deletions src/d3d12/d3d12-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ namespace nvrhi::d3d12
void setPushConstants(const void* data, size_t byteSize) override;

void setGraphicsState(const GraphicsState& state) override;
void setStencilRefValue(uint8_t value) override;
void draw(const DrawArguments& args) override;
void drawIndexed(const DrawArguments& args) override;
void drawIndirect(uint32_t offsetBytes, uint32_t drawCount) override;
Expand Down
5 changes: 5 additions & 0 deletions src/d3d12/d3d12-graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@ namespace nvrhi::d3d12
m_CurrentGraphicsState = state;
}

void CommandList::setStencilRefValue(uint8_t value)
{
m_ActiveCommandList->commandList->OMSetStencilRef(value);
}

void CommandList::unbindShadingRateState()
{
if (m_CurrentGraphicsStateValid && m_CurrentGraphicsState.shadingRateState.enabled)
Expand Down
1 change: 1 addition & 0 deletions src/validation/validation-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ namespace nvrhi::validation
void setPushConstants(const void* data, size_t byteSize) override;

void setGraphicsState(const GraphicsState& state) override;
void setStencilRefValue(uint8_t value) override;
void draw(const DrawArguments& args) override;
void drawIndexed(const DrawArguments& args) override;
void drawIndirect(uint32_t offsetBytes, uint32_t drawCount) override;
Expand Down
33 changes: 33 additions & 0 deletions src/validation/validation-commandlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,39 @@ namespace nvrhi::validation
m_CurrentGraphicsState = state;
}

void CommandListWrapper::setStencilRefValue(uint8_t value)
{
if (!requireOpenState())
return;

if (!requireType(CommandQueue::Graphics, "draw"))
return;

if (!m_GraphicsStateSet)
{
error("Graphics state is not set before a draw call.\n"
"Note that setting compute state invalidates the graphics state.");
return;
}

const auto& dsDesc = m_CurrentGraphicsState.pipeline->getDesc().renderState.depthStencilState;

if (!dsDesc.stencilEnable)
{
error("setStencilRefValue: stencil is not enabled");
return;
}

if (!dsDesc.stencilReadMask &&
!dsDesc.stencilWriteMask)
{
error("setStencilRefValue: stencil read and write masks are both 0");
return;
}

m_CommandList->setStencilRefValue(value);
}

void CommandListWrapper::draw(const DrawArguments& args)
{
if (!requireOpenState())
Expand Down
1 change: 1 addition & 0 deletions src/vulkan/vulkan-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ namespace nvrhi::vulkan
void setPushConstants(const void* data, size_t byteSize) override;

void setGraphicsState(const GraphicsState& state) override;
void setStencilRefValue(uint8_t value) override;
void draw(const DrawArguments& args) override;
void drawIndexed(const DrawArguments& args) override;
void drawIndirect(uint32_t offsetBytes, uint32_t drawCount) override;
Expand Down
9 changes: 7 additions & 2 deletions src/vulkan/vulkan-graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,12 @@ namespace nvrhi::vulkan
vk::DynamicState dynamicStates[4] = {
vk::DynamicState::eViewport,
vk::DynamicState::eScissor,
vk::DynamicState::eStencilReference,
vk::DynamicState::eBlendConstants,
vk::DynamicState::eFragmentShadingRateKHR
};

auto dynamicStateInfo = vk::PipelineDynamicStateCreateInfo()
.setDynamicStateCount(pso->usesBlendConstants ? 3 : 2)
.setDynamicStateCount(pso->usesBlendConstants ? 4 : 3)
.setPDynamicStates(dynamicStates);

auto pipelineInfo = vk::GraphicsPipelineCreateInfo()
Expand Down Expand Up @@ -786,6 +786,11 @@ namespace nvrhi::vulkan
m_AnyVolatileBufferWrites = false;
}

void CommandList::setStencilRefValue(uint8_t value)
{
m_CurrentCmdBuf->cmdBuf.setStencilReference(vk::StencilFaceFlagBits::eFrontAndBack, value);
}

void CommandList::updateGraphicsVolatileBuffers()
{
if (m_AnyVolatileBufferWrites && m_CurrentGraphicsState.pipeline)
Expand Down
5 changes: 3 additions & 2 deletions src/vulkan/vulkan-meshlets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,15 @@ namespace nvrhi::vulkan

pso->usesBlendConstants = blendState.usesConstantColor(uint32_t(fb->desc.colorAttachments.size()));

vk::DynamicState dynamicStates[3] = {
vk::DynamicState dynamicStates[4] = {
vk::DynamicState::eViewport,
vk::DynamicState::eScissor,
vk::DynamicState::eStencilReference,
vk::DynamicState::eBlendConstants
};

auto dynamicStateInfo = vk::PipelineDynamicStateCreateInfo()
.setDynamicStateCount(pso->usesBlendConstants ? 3 : 2)
.setDynamicStateCount(pso->usesBlendConstants ? 4 : 3)
.setPDynamicStates(dynamicStates);

auto pipelineInfo = vk::GraphicsPipelineCreateInfo()
Expand Down
Loading