MXVK Vulkan Framework 0.33.1
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_Stenciloperator= (const VK_Stencil &)=delete
VK_Stenciloperator= (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 {
11 destroy();
12 }
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 90 of file mxvk_stencil.cpp.

90 {
91 if (!valid()) {
92 return;
93 }
94 attachment.imageView = view;
95 attachment.imageLayout = VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL;
96 attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
97 attachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
98 attachment.clearValue.depthStencil = {1.0f, 0U};
99 }
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 38 of file mxvk_stencil.cpp.

38 {
39 destroy_pipelines();
40 destroy_image();
41 vk_context = {};
42 stencil_extent = {};
43 render_color_format = VK_FORMAT_UNDEFINED;
44 render_depth_format = VK_FORMAT_UNDEFINED;
45 stencil_image_format = VK_FORMAT_UNDEFINED;
46 cache = VK_NULL_HANDLE;
47 mask_vertex_path.clear();
48 mask_fragment_path.clear();
49 content_vertex_path.clear();
50 content_fragment_path.clear();
51 }

◆ 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 110 of file mxvk_stencil.cpp.

110 {
111 if (content_pipeline == VK_NULL_HANDLE || content_layout == VK_NULL_HANDLE) {
112 return;
113 }
114 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, content_pipeline);
115 vkCmdPushConstants(cmd, content_layout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PushConstants), &push_constants);
116 vkCmdDraw(cmd, 3, 1, 0, 0);
117 }
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 101 of file mxvk_stencil.cpp.

101 {
102 if (mask_pipeline == VK_NULL_HANDLE || mask_layout == VK_NULL_HANDLE) {
103 return;
104 }
105 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, mask_pipeline);
106 vkCmdPushConstants(cmd, mask_layout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PushConstants), &push_constants);
107 vkCmdDraw(cmd, 3, 1, 0, 0);
108 }

◆ extent()

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

Return the current stencil attachment extent.

Definition at line 121 of file mxvk_stencil.hpp.

121{ 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 14 of file mxvk_stencil.cpp.

22 {
23 destroy();
24 vk_context = context;
25 stencil_extent = extent;
26 render_color_format = color_format;
27 render_depth_format = depth_format;
28 cache = pipeline_cache;
29 mask_vertex_path = mask_vertex_shader;
30 mask_fragment_path = mask_fragment_shader;
31 content_vertex_path = content_vertex_shader;
32 content_fragment_path = content_fragment_shader;
33 stencil_image_format = choose_stencil_format();
34 create_resources();
35 create_pipelines();
36 }
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 62 of file mxvk_stencil.cpp.

62 {
63 if (!valid()) {
64 return;
65 }
66
67 VkImageMemoryBarrier2 barrier{};
68 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
69 barrier.srcStageMask = image_initialized ? VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT : VK_PIPELINE_STAGE_2_NONE;
70 barrier.srcAccessMask = image_initialized ? VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT : VK_ACCESS_2_NONE;
71 barrier.dstStageMask = VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT;
72 barrier.dstAccessMask = VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
73 barrier.oldLayout = image_initialized ? VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_UNDEFINED;
74 barrier.newLayout = VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL;
75 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
76 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
77 barrier.image = image;
78 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
79 barrier.subresourceRange.levelCount = 1;
80 barrier.subresourceRange.layerCount = 1;
81
82 VkDependencyInfo dependency{};
83 dependency.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
84 dependency.imageMemoryBarrierCount = 1;
85 dependency.pImageMemoryBarriers = &barrier;
86 vkCmdPipelineBarrier2(cmd, &dependency);
87 image_initialized = true;
88 }

◆ 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 53 of file mxvk_stencil.cpp.

53 {
54 if (extent.width == stencil_extent.width && extent.height == stencil_extent.height) {
55 return;
56 }
57 stencil_extent = extent;
58 destroy_image();
59 create_resources();
60 }

◆ stencil_format()

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

Return the selected stencil-capable image format.

Definition at line 118 of file mxvk_stencil.hpp.

118{ return stencil_image_format; }

◆ valid()

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

Check whether image resources are ready for rendering.

Definition at line 119 of file mxvk_stencil.cpp.

119 {
120 return vk_context.device != VK_NULL_HANDLE && image != VK_NULL_HANDLE && view != VK_NULL_HANDLE &&
121 stencil_extent.width > 0U && stencil_extent.height > 0U;
122 }

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