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

renderer_vulkan: Simplify depth pipeline state and move stencil to dynamic state. #1854

Merged
merged 2 commits into from
Dec 24, 2024
Merged
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
33 changes: 7 additions & 26 deletions src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ GraphicsPipeline::GraphicsPipeline(
.frontFace = key.front_face == Liverpool::FrontFace::Clockwise
? vk::FrontFace::eClockwise
: vk::FrontFace::eCounterClockwise,
.depthBiasEnable = bool(key.depth_bias_enable),
.depthBiasEnable = key.depth_bias_enable,
.lineWidth = 1.0f,
};

Expand Down Expand Up @@ -164,6 +164,7 @@ GraphicsPipeline::GraphicsPipeline(
vk::DynamicState::eBlendConstants, vk::DynamicState::eDepthBounds,
vk::DynamicState::eDepthBias, vk::DynamicState::eStencilReference,
vk::DynamicState::eStencilCompareMask, vk::DynamicState::eStencilWriteMask,
vk::DynamicState::eStencilOpEXT,
};

if (instance.IsColorWriteEnableSupported()) {
Expand All @@ -182,31 +183,11 @@ GraphicsPipeline::GraphicsPipeline(
};

const vk::PipelineDepthStencilStateCreateInfo depth_info = {
.depthTestEnable = key.depth_stencil.depth_enable,
.depthWriteEnable = key.depth_stencil.depth_write_enable,
.depthCompareOp = LiverpoolToVK::CompareOp(key.depth_stencil.depth_func),
.depthBoundsTestEnable = key.depth_stencil.depth_bounds_enable,
.stencilTestEnable = key.depth_stencil.stencil_enable,
.front{
.failOp = LiverpoolToVK::StencilOp(key.stencil.stencil_fail_front),
.passOp = LiverpoolToVK::StencilOp(key.stencil.stencil_zpass_front),
.depthFailOp = LiverpoolToVK::StencilOp(key.stencil.stencil_zfail_front),
.compareOp = LiverpoolToVK::CompareOp(key.depth_stencil.stencil_ref_func),
},
.back{
.failOp = LiverpoolToVK::StencilOp(key.depth_stencil.backface_enable
? key.stencil.stencil_fail_back.Value()
: key.stencil.stencil_fail_front.Value()),
.passOp = LiverpoolToVK::StencilOp(key.depth_stencil.backface_enable
? key.stencil.stencil_zpass_back.Value()
: key.stencil.stencil_zpass_front.Value()),
.depthFailOp = LiverpoolToVK::StencilOp(key.depth_stencil.backface_enable
? key.stencil.stencil_zfail_back.Value()
: key.stencil.stencil_zfail_front.Value()),
.compareOp = LiverpoolToVK::CompareOp(key.depth_stencil.backface_enable
? key.depth_stencil.stencil_bf_func.Value()
: key.depth_stencil.stencil_ref_func.Value()),
},
.depthTestEnable = key.depth_test_enable,
.depthWriteEnable = key.depth_write_enable,
.depthCompareOp = key.depth_compare_op,
.depthBoundsTestEnable = key.depth_bounds_test_enable,
.stencilTestEnable = key.stencil_test_enable,
};

boost::container::static_vector<vk::PipelineShaderStageCreateInfo, MaxShaderStages>
Expand Down
14 changes: 11 additions & 3 deletions src/video_core/renderer_vulkan/vk_graphics_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ struct GraphicsPipelineKey {
vk::Format depth_format;
vk::Format stencil_format;

Liverpool::DepthControl depth_stencil;
u32 depth_bias_enable;
struct {
bool depth_test_enable : 1;
bool depth_write_enable : 1;
bool depth_bounds_test_enable : 1;
bool depth_bias_enable : 1;
bool stencil_test_enable : 1;
// Must be named to be zero-initialized.
u8 _unused : 3;
};
vk::CompareOp depth_compare_op;

u32 num_samples;
u32 mrt_mask;
Liverpool::StencilControl stencil;
AmdGpu::PrimitiveType prim_type;
u32 enable_primitive_restart;
u32 primitive_restart_index;
Expand Down
14 changes: 8 additions & 6 deletions src/video_core/renderer_vulkan/vk_pipeline_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,13 @@ bool PipelineCache::RefreshGraphicsKey() {
auto& regs = liverpool->regs;
auto& key = graphics_key;

key.depth_stencil = regs.depth_control;
key.stencil = regs.stencil_control;
key.depth_stencil.depth_write_enable.Assign(regs.depth_control.depth_write_enable.Value() &&
!regs.depth_render_control.depth_clear_enable);
key.depth_test_enable = regs.depth_control.depth_enable;
key.depth_write_enable =
regs.depth_control.depth_write_enable && !regs.depth_render_control.depth_clear_enable;
key.depth_bounds_test_enable = regs.depth_control.depth_bounds_enable;
key.depth_bias_enable = regs.polygon_control.NeedsBias();
key.depth_compare_op = LiverpoolToVK::CompareOp(regs.depth_control.depth_func);
key.stencil_test_enable = regs.depth_control.stencil_enable;

const auto depth_format = instance.GetSupportedFormat(
LiverpoolToVK::DepthFormat(regs.depth_buffer.z_info.format,
Expand All @@ -272,13 +274,13 @@ bool PipelineCache::RefreshGraphicsKey() {
key.depth_format = depth_format;
} else {
key.depth_format = vk::Format::eUndefined;
key.depth_stencil.depth_enable.Assign(false);
key.depth_test_enable = false;
}
if (regs.depth_buffer.StencilValid()) {
key.stencil_format = depth_format;
} else {
key.stencil_format = vk::Format::eUndefined;
key.depth_stencil.stencil_enable.Assign(false);
key.stencil_test_enable = false;
}

key.prim_type = regs.primitive_type;
Expand Down
28 changes: 28 additions & 0 deletions src/video_core/renderer_vulkan/vk_rasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,33 @@ void Rasterizer::UpdateDynamicState(const GraphicsPipeline& pipeline) {
cmdbuf.setDepthBias(regs.poly_offset.back_offset, regs.poly_offset.depth_bias,
regs.poly_offset.back_scale / 16.f);
}

if (regs.depth_control.stencil_enable) {
const auto front_fail_op =
LiverpoolToVK::StencilOp(regs.stencil_control.stencil_fail_front);
const auto front_pass_op =
LiverpoolToVK::StencilOp(regs.stencil_control.stencil_zpass_front);
const auto front_depth_fail_op =
LiverpoolToVK::StencilOp(regs.stencil_control.stencil_zfail_front);
const auto front_compare_op = LiverpoolToVK::CompareOp(regs.depth_control.stencil_ref_func);
if (regs.depth_control.backface_enable) {
const auto back_fail_op =
LiverpoolToVK::StencilOp(regs.stencil_control.stencil_fail_back);
const auto back_pass_op =
LiverpoolToVK::StencilOp(regs.stencil_control.stencil_zpass_back);
const auto back_depth_fail_op =
LiverpoolToVK::StencilOp(regs.stencil_control.stencil_zfail_back);
const auto back_compare_op =
LiverpoolToVK::CompareOp(regs.depth_control.stencil_bf_func);
cmdbuf.setStencilOpEXT(vk::StencilFaceFlagBits::eFront, front_fail_op, front_pass_op,
front_depth_fail_op, front_compare_op);
cmdbuf.setStencilOpEXT(vk::StencilFaceFlagBits::eBack, back_fail_op, back_pass_op,
back_depth_fail_op, back_compare_op);
} else {
cmdbuf.setStencilOpEXT(vk::StencilFaceFlagBits::eFrontAndBack, front_fail_op,
front_pass_op, front_depth_fail_op, front_compare_op);
}

const auto front = regs.stencil_ref_front;
const auto back = regs.stencil_ref_back;
if (front.stencil_test_val == back.stencil_test_val) {
Expand All @@ -973,13 +999,15 @@ void Rasterizer::UpdateDynamicState(const GraphicsPipeline& pipeline) {
cmdbuf.setStencilReference(vk::StencilFaceFlagBits::eFront, front.stencil_test_val);
cmdbuf.setStencilReference(vk::StencilFaceFlagBits::eBack, back.stencil_test_val);
}

if (front.stencil_write_mask == back.stencil_write_mask) {
cmdbuf.setStencilWriteMask(vk::StencilFaceFlagBits::eFrontAndBack,
front.stencil_write_mask);
} else {
cmdbuf.setStencilWriteMask(vk::StencilFaceFlagBits::eFront, front.stencil_write_mask);
cmdbuf.setStencilWriteMask(vk::StencilFaceFlagBits::eBack, back.stencil_write_mask);
}

if (front.stencil_mask == back.stencil_mask) {
cmdbuf.setStencilCompareMask(vk::StencilFaceFlagBits::eFrontAndBack,
front.stencil_mask);
Expand Down
Loading