MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk::VK_Stencil Class Reference

Owns a stencil attachment and two fullscreen pipelines for stencil-masked rendering. More...

#include <mxvk/include/mxvk/mxvk_stencil.hpp>

Classes

struct  PushConstants
 Push-constant payload shared by the mask and content shaders. More...

Public Member Functions

void configure_attachment (VkRenderingAttachmentInfo &attachment) const
 Fill a VkRenderingAttachmentInfo for VK_Window dynamic rendering.
void destroy ()
 Release all Vulkan resources and reset the helper to an empty state.
void draw_content (VkCommandBuffer cmd, const PushConstants &push_constants) const
 Draw the fullscreen content pass clipped by stencil reference value 1.
void draw_mask (VkCommandBuffer cmd, const PushConstants &push_constants) const
 Draw the fullscreen stencil-writing mask pass.
VkExtent2D extent () const noexcept
 Return the current stencil attachment extent.
void initialize (const VulkanContext &context, VkExtent2D extent, VkFormat color_format, VkFormat depth_format, VkPipelineCache pipeline_cache, const std::string &mask_vertex_shader, const std::string &mask_fragment_shader, const std::string &content_vertex_shader, const std::string &content_fragment_shader)
 Create the stencil attachment and fullscreen mask/content pipelines.
VK_Stencil & operator= (const VK_Stencil &)=delete
VK_Stencil & operator= (VK_Stencil &&)=delete
void prepare_for_rendering (VkCommandBuffer cmd)
 Transition the stencil image for use as a dynamic-rendering stencil attachment.
void resize (VkExtent2D extent)
 Recreate the stencil image for a new render extent.
VkFormat stencil_format () const noexcept
 Return the selected stencil-capable image format.
bool valid () const noexcept
 Check whether image resources are ready for rendering.
 VK_Stencil ()=default
 Construct an empty stencil helper.
 VK_Stencil (const VK_Stencil &)=delete
 VK_Stencil (VK_Stencil &&)=delete
 ~VK_Stencil ()
 Destroy any live Vulkan resources owned by the helper.

Detailed Description

Owns a stencil attachment and two fullscreen pipelines for stencil-masked rendering.

VK_Stencil is intended for use from VK_Window subclasses. Call initialize() once the window has a valid device and swapchain extent, call prepare_for_rendering() before VK_Window begins dynamic rendering, pass configure_attachment() through VK_Window::onConfigureDepthStencilAttachments(), then call draw_mask() before draw_content() inside VK_Window::onRecordCustomRendering().

Definition at line 26 of file mxvk_stencil.hpp.

Constructor & Destructor Documentation

◆ VK_Stencil() [1/3]

mxvk::VK_Stencil::VK_Stencil ( )
default

Construct an empty stencil helper.

◆ ~VK_Stencil()

mxvk::VK_Stencil::~VK_Stencil ( )

Destroy any live Vulkan resources owned by the helper.

Definition at line 10 of file mxvk_stencil.cpp.

10{ destroy(); }
void destroy()
Release all Vulkan resources and reset the helper to an empty state.

◆ VK_Stencil() [2/3]

mxvk::VK_Stencil::VK_Stencil ( const VK_Stencil & )
delete

◆ VK_Stencil() [3/3]

mxvk::VK_Stencil::VK_Stencil ( VK_Stencil && )
delete

Member Function Documentation

◆ configure_attachment()

void mxvk::VK_Stencil::configure_attachment ( VkRenderingAttachmentInfo & attachment) const

Fill a VkRenderingAttachmentInfo for VK_Window dynamic rendering.

Parameters
attachmentAttachment info to populate when the helper is valid.

Definition at line 80 of file mxvk_stencil.cpp.

80 {
81 if (!valid()) {
82 return;
83 }
84 attachment.imageView = view;
85 attachment.imageLayout = VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL;
86 attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
87 attachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
88 attachment.clearValue.depthStencil = {1.0f, 0U};
89 }
bool valid() const noexcept
Check whether image resources are ready for rendering.

◆ destroy()

void mxvk::VK_Stencil::destroy ( )

Release all Vulkan resources and reset the helper to an empty state.

Definition at line 28 of file mxvk_stencil.cpp.

28 {
29 destroy_pipelines();
30 destroy_image();
31 vk_context = {};
32 stencil_extent = {};
33 render_color_format = VK_FORMAT_UNDEFINED;
34 render_depth_format = VK_FORMAT_UNDEFINED;
35 stencil_image_format = VK_FORMAT_UNDEFINED;
36 cache = VK_NULL_HANDLE;
37 mask_vertex_path.clear();
38 mask_fragment_path.clear();
39 content_vertex_path.clear();
40 content_fragment_path.clear();
41 }

◆ draw_content()

void mxvk::VK_Stencil::draw_content ( VkCommandBuffer cmd,
const PushConstants & push_constants ) const

Draw the fullscreen content pass clipped by stencil reference value 1.

Parameters
cmdCommand buffer recording inside the dynamic-rendering scope.
push_constantsValues passed to the content fragment shader.

Definition at line 100 of file mxvk_stencil.cpp.

100 {
101 if (content_pipeline == VK_NULL_HANDLE || content_layout == VK_NULL_HANDLE) {
102 return;
103 }
104 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, content_pipeline);
105 vkCmdPushConstants(cmd, content_layout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PushConstants), &push_constants);
106 vkCmdDraw(cmd, 3, 1, 0, 0);
107 }
Push-constant payload shared by the mask and content shaders.

◆ draw_mask()

void mxvk::VK_Stencil::draw_mask ( VkCommandBuffer cmd,
const PushConstants & push_constants ) const

Draw the fullscreen stencil-writing mask pass.

Parameters
cmdCommand buffer recording inside the dynamic-rendering scope.
push_constantsValues passed to the mask fragment shader.

Definition at line 91 of file mxvk_stencil.cpp.

91 {
92 if (mask_pipeline == VK_NULL_HANDLE || mask_layout == VK_NULL_HANDLE) {
93 return;
94 }
95 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, mask_pipeline);
96 vkCmdPushConstants(cmd, mask_layout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PushConstants), &push_constants);
97 vkCmdDraw(cmd, 3, 1, 0, 0);
98 }

◆ extent()

VkExtent2D mxvk::VK_Stencil::extent ( ) const
inlinenodiscardnoexcept

Return the current stencil attachment extent.

Definition at line 113 of file mxvk_stencil.hpp.

113{ return stencil_extent; }

◆ initialize()

void mxvk::VK_Stencil::initialize ( const VulkanContext & context,
VkExtent2D extent,
VkFormat color_format,
VkFormat depth_format,
VkPipelineCache pipeline_cache,
const std::string & mask_vertex_shader,
const std::string & mask_fragment_shader,
const std::string & content_vertex_shader,
const std::string & content_fragment_shader )

Create the stencil attachment and fullscreen mask/content pipelines.

Parameters
contextVulkan device, physical device, queue, and command pool handles.
extentSize of the stencil attachment in pixels.
color_formatColor attachment format used by the active dynamic rendering pass.
depth_formatDepth attachment format used by the active dynamic rendering pass.
pipeline_cacheOptional pipeline cache used when creating graphics pipelines.
mask_vertex_shaderSPIR-V vertex shader path for the stencil-writing pass.
mask_fragment_shaderSPIR-V fragment shader path for the stencil-writing pass.
content_vertex_shaderSPIR-V vertex shader path for the stencil-tested content pass.
content_fragment_shaderSPIR-V fragment shader path for the stencil-tested content pass.

Existing resources are destroyed before the new resources are created.

Definition at line 12 of file mxvk_stencil.cpp.

12 {
13 destroy();
14 vk_context = context;
15 stencil_extent = extent;
16 render_color_format = color_format;
17 render_depth_format = depth_format;
18 cache = pipeline_cache;
19 mask_vertex_path = mask_vertex_shader;
20 mask_fragment_path = mask_fragment_shader;
21 content_vertex_path = content_vertex_shader;
22 content_fragment_path = content_fragment_shader;
23 stencil_image_format = choose_stencil_format();
24 create_resources();
25 create_pipelines();
26 }
VkExtent2D extent() const noexcept
Return the current stencil attachment extent.

◆ operator=() [1/2]

VK_Stencil & mxvk::VK_Stencil::operator= ( const VK_Stencil & )
delete

◆ operator=() [2/2]

VK_Stencil & mxvk::VK_Stencil::operator= ( VK_Stencil && )
delete

◆ prepare_for_rendering()

void mxvk::VK_Stencil::prepare_for_rendering ( VkCommandBuffer cmd)

Transition the stencil image for use as a dynamic-rendering stencil attachment.

Parameters
cmdCommand buffer currently recording outside a rendering scope.

Definition at line 52 of file mxvk_stencil.cpp.

52 {
53 if (!valid()) {
54 return;
55 }
56
57 VkImageMemoryBarrier2 barrier{};
58 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
59 barrier.srcStageMask = image_initialized ? VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT : VK_PIPELINE_STAGE_2_NONE;
60 barrier.srcAccessMask = image_initialized ? VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT : VK_ACCESS_2_NONE;
61 barrier.dstStageMask = VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT;
62 barrier.dstAccessMask = VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
63 barrier.oldLayout = image_initialized ? VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_UNDEFINED;
64 barrier.newLayout = VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL;
65 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
66 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
67 barrier.image = image;
68 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
69 barrier.subresourceRange.levelCount = 1;
70 barrier.subresourceRange.layerCount = 1;
71
72 VkDependencyInfo dependency{};
73 dependency.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
74 dependency.imageMemoryBarrierCount = 1;
75 dependency.pImageMemoryBarriers = &barrier;
76 vkCmdPipelineBarrier2(cmd, &dependency);
77 image_initialized = true;
78 }

◆ resize()

void mxvk::VK_Stencil::resize ( VkExtent2D extent)

Recreate the stencil image for a new render extent.

Parameters
extentNew attachment size in pixels.

Pipelines are retained because they depend on formats, not image dimensions.

Definition at line 43 of file mxvk_stencil.cpp.

43 {
44 if (extent.width == stencil_extent.width && extent.height == stencil_extent.height) {
45 return;
46 }
47 stencil_extent = extent;
48 destroy_image();
49 create_resources();
50 }

◆ stencil_format()

VkFormat mxvk::VK_Stencil::stencil_format ( ) const
inlinenodiscardnoexcept

Return the selected stencil-capable image format.

Definition at line 110 of file mxvk_stencil.hpp.

110{ return stencil_image_format; }

◆ valid()

bool mxvk::VK_Stencil::valid ( ) const
nodiscardnoexcept

Check whether image resources are ready for rendering.

Definition at line 109 of file mxvk_stencil.cpp.

109{ return vk_context.device != VK_NULL_HANDLE && image != VK_NULL_HANDLE && view != VK_NULL_HANDLE && stencil_extent.width > 0U && stencil_extent.height > 0U; }

The documentation for this class was generated from the following files: