MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
stencil.cpp
Go to the documentation of this file.
1#include "mxvk/argz.hpp"
2#include "mxvk/mxvk.hpp"
5
6#include <chrono>
7#include <cstdlib>
8#include <format>
9#include <iostream>
10#include <memory>
11#include <string>
12
13namespace example {
15 public:
16 StencilWindow(const std::string &path, int width, int height, bool fullscreen, bool enable_vsync)
17 : mxvk::VK_Window("MXVK Stencil", width, height, fullscreen, MXVK_VALIDATION, enable_vsync),
18 shader_root((path.empty() ? std::string(STENCIL_ASSET_DIR) : path) + "/data") {
19 setClearColor(0.015f, 0.018f, 0.025f, 1.0f);
20 }
21
22 ~StencilWindow() override {
23 if (device != VK_NULL_HANDLE) {
24 vkDeviceWaitIdle(device);
25 }
26 stencil.reset();
27 }
28
30 stencil.reset();
31 }
32
33 void onPrepareFrameRendering(VkCommandBuffer cmd, [[maybe_unused]] uint32_t image_index) override {
34 ensure_stencil();
35 if (stencil) {
36 stencil->prepare_for_rendering(cmd);
37 }
38 }
39
40 void onConfigureDepthStencilAttachments(VkRenderingAttachmentInfo &depth_attachment,
41 VkRenderingAttachmentInfo &stencil_attachment,
42 [[maybe_unused]] uint32_t image_index) override {
43 depth_attachment.imageView = VK_NULL_HANDLE;
44 if (stencil) {
45 stencil->configure_attachment(stencil_attachment);
46 }
47 }
48
49 void onRecordCustomRendering(VkCommandBuffer cmd, [[maybe_unused]] uint32_t image_index) override {
50 if (!stencil || !stencil->valid()) {
51 return;
52 }
53
54 const auto now = std::chrono::steady_clock::now();
55 const float elapsed = std::chrono::duration<float>(now - start_time).count();
56 const VkExtent2D current_extent = getSwapchainExtent();
57 const float aspect = current_extent.height > 0U
58 ? static_cast<float>(current_extent.width) / static_cast<float>(current_extent.height)
59 : 1.0f;
60
62 .time = elapsed,
63 .aspect = aspect,
64 .phase = elapsed * 0.35f,
65 .scale = 1.0f,
66 };
67 stencil->draw_mask(cmd, push);
68 stencil->draw_content(cmd, push);
69 }
70
71 private:
72 void ensure_stencil() {
73 const VkExtent2D current_extent = getSwapchainExtent();
74 if (current_extent.width == 0U || current_extent.height == 0U) {
75 return;
76 }
77
78 if (!stencil) {
79 stencil = std::make_unique<mxvk::VK_Stencil>();
81 .device = getDevice(),
82 .physical_device = getPhysicalDevice(),
83 .graphics_queue = getGraphicsQueue(),
84 .command_pool = getCommandPool(),
85 },
86 current_extent, getSwapchainFormat(), VK_FORMAT_UNDEFINED, getPipelineCache(), shader_root + "/fullscreen.vert.spv", shader_root + "/mandala_mask.frag.spv", shader_root + "/content_fullscreen.vert.spv", shader_root + "/stencil_fill.frag.spv");
87 } else {
88 stencil->resize(current_extent);
89 }
90 }
91
92 std::string shader_root;
93 std::chrono::steady_clock::time_point start_time{std::chrono::steady_clock::now()};
94 std::unique_ptr<mxvk::VK_Stencil> stencil{};
95 };
96} // namespace example
97
98int main(int argc, char **argv) {
99 try {
100 const Arguments args = proc_args(argc, argv);
101 example::StencilWindow window(args.path, args.width, args.height, args.fullscreen, args.enable_vsync);
102 window.loop();
103 } catch (const mxvk::Exception &e) {
104 std::cerr << std::format("mxvk: Exception: {}\n", e.text());
105 return EXIT_FAILURE;
106 } catch (const ArgException<std::string> &e) {
107 std::cerr << std::format("mxvk: Argument Exception: {}\n", e.text());
108 return EXIT_FAILURE;
109 }
110 return EXIT_SUCCESS;
111}
Lightweight, header-only, template command-line argument parser.
Arguments proc_args(int &argc, char **argv)
Parse standard libmx2 command-line options from main()'s argv.
Definition argz.hpp:872
Exception thrown by Argz::proc() on unrecognised or malformed options.
Definition argz.hpp:178
void onPrepareFrameRendering(VkCommandBuffer cmd, uint32_t image_index) override
Record resource transitions that must happen before dynamic rendering begins.
Definition stencil.cpp:33
StencilWindow(const std::string &path, int width, int height, bool fullscreen, bool enable_vsync)
Definition stencil.cpp:16
void onConfigureDepthStencilAttachments(VkRenderingAttachmentInfo &depth_attachment, VkRenderingAttachmentInfo &stencil_attachment, uint32_t image_index) override
Allow derived classes to customize depth/stencil attachments for the main dynamic rendering pass.
Definition stencil.cpp:40
void onSwapchainAboutToRecreate() override
Called right before swapchain-dependent resources are recreated.
Definition stencil.cpp:29
void onRecordCustomRendering(VkCommandBuffer cmd, uint32_t image_index) override
Optional hook for derived classes to record extra draw commands.
Definition stencil.cpp:49
~StencilWindow() override
Definition stencil.cpp:22
std::string text() const
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.
Main Vulkan window wrapper for MXVK.
Definition mxvk.hpp:37
VkExtent2D getSwapchainExtent() const noexcept
Get the current swapchain extent.
Definition mxvk.hpp:186
void loop()
Run the main event/render loop.
Definition mxvk.cpp:600
VkDevice getDevice() const noexcept
Get the Vulkan logical device handle.
Definition mxvk.hpp:168
VkDevice device
Definition mxvk.hpp:485
VkQueue getGraphicsQueue() const noexcept
Get the graphics queue handle.
Definition mxvk.hpp:174
VkCommandPool getCommandPool() const noexcept
Get the command pool used for graphics/upload work.
Definition mxvk.hpp:177
void setClearColor(float r, float g, float b, float a=1.0f)
Set the per-frame color attachment clear color.
Definition mxvk.cpp:593
VK_Window()=default
Construct an empty window object.
VkPipelineCache getPipelineCache() const noexcept
Get the persistent Vulkan pipeline cache used by framework pipelines.
Definition mxvk.hpp:180
VkPhysicalDevice getPhysicalDevice() const noexcept
Get the Vulkan physical device handle.
Definition mxvk.hpp:171
VkFormat getSwapchainFormat() const noexcept
Get the swapchain color format.
Definition mxvk.hpp:183
int main(void)
Definition main.cpp:7
#define MXVK_VALIDATION
Definition mxvk.hpp:27
Reusable dynamic-rendering stencil image and fullscreen stencil pipelines.
Utilities for loading and saving PNG images.
Definition mxvk.hpp:30
Plain data structure returned by proc_args() with all common libmx2 CLI options.
Definition argz.hpp:730
bool fullscreen
Whether fullscreen mode was requested.
Definition argz.hpp:736
bool enable_vsync
Enable FIFO present mode / v-sync (--enable-vsync).
Definition argz.hpp:750
int height
Viewport height in pixels (default: 720).
Definition argz.hpp:733
std::string path
Asset root; proc_args() defaults it to the executable directory.
Definition argz.hpp:735
int width
Viewport width in pixels (default: 1280).
Definition argz.hpp:732
Push-constant payload shared by the mask and content shaders.
Minimal Vulkan handles required by MXVK resource helpers.