MXVK Vulkan Framework 0.35.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) : mxvk::VK_Window("MXVK Stencil", width, height, fullscreen, MXVK_VALIDATION, enable_vsync), shader_root((path.empty() ? std::string(STENCIL_ASSET_DIR) : path) + "/data") { setClearColor(0.015f, 0.018f, 0.025f, 1.0f); }
17
18 ~StencilWindow() override {
19 if (device != VK_NULL_HANDLE) {
20 vkDeviceWaitIdle(device);
21 }
22 stencil.reset();
23 }
24
25 void onSwapchainAboutToRecreate() override { stencil.reset(); }
26
27 void onPrepareFrameRendering(VkCommandBuffer cmd, [[maybe_unused]] uint32_t image_index) override {
28 ensure_stencil();
29 if (stencil) {
30 stencil->prepare_for_rendering(cmd);
31 }
32 }
33
34 void onConfigureDepthStencilAttachments(VkRenderingAttachmentInfo &depth_attachment, VkRenderingAttachmentInfo &stencil_attachment, [[maybe_unused]] uint32_t image_index) override {
35 depth_attachment.imageView = VK_NULL_HANDLE;
36 if (stencil) {
37 stencil->configure_attachment(stencil_attachment);
38 }
39 }
40
41 void onRecordCustomRendering(VkCommandBuffer cmd, [[maybe_unused]] uint32_t image_index) override {
42 if (!stencil || !stencil->valid()) {
43 return;
44 }
45
46 const auto now = std::chrono::steady_clock::now();
47 const float elapsed = std::chrono::duration<float>(now - start_time).count();
48 const VkExtent2D current_extent = getSwapchainExtent();
49 const float aspect = current_extent.height > 0U ? static_cast<float>(current_extent.width) / static_cast<float>(current_extent.height) : 1.0f;
50
52 .time = elapsed,
53 .aspect = aspect,
54 .phase = elapsed * 0.35f,
55 .scale = 1.0f,
56 };
57 stencil->draw_mask(cmd, push);
58 stencil->draw_content(cmd, push);
59 }
60
61 private:
62 void ensure_stencil() {
63 const VkExtent2D current_extent = getSwapchainExtent();
64 if (current_extent.width == 0U || current_extent.height == 0U) {
65 return;
66 }
67
68 if (!stencil) {
69 stencil = std::make_unique<mxvk::VK_Stencil>();
70 stencil->initialize(
72 .device = getDevice(),
73 .physical_device = getPhysicalDevice(),
74 .graphics_queue = getGraphicsQueue(),
75 .command_pool = getCommandPool(),
76 },
77 current_extent,
79 VK_FORMAT_UNDEFINED,
81 shader_root + "/fullscreen.vert.spv",
82 shader_root + "/mandala_mask.frag.spv",
83 shader_root + "/content_fullscreen.vert.spv",
84 shader_root + "/stencil_fill.frag.spv");
85 } else {
86 stencil->resize(current_extent);
87 }
88 }
89
90 std::string shader_root;
91 std::chrono::steady_clock::time_point start_time{std::chrono::steady_clock::now()};
92 std::unique_ptr<mxvk::VK_Stencil> stencil{};
93 };
94} // namespace example
95
96int main(int argc, char **argv) {
97 try {
98 const Arguments args = proc_args(argc, argv);
99 example::StencilWindow window(args.path, args.width, args.height, args.fullscreen, args.enable_vsync);
100 window.loop();
101 } catch (const mxvk::Exception &e) {
102 std::cerr << std::format("mxvk: Exception: {}\n", e.text());
103 return EXIT_FAILURE;
104 } catch (const ArgException<std::string> &e) {
105 std::cerr << std::format("mxvk: Argument Exception: {}\n", e.text());
106 return EXIT_FAILURE;
107 }
108 return EXIT_SUCCESS;
109}
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:854
Exception thrown by Argz::proc() on unrecognised or malformed options.
Definition argz.hpp:169
void onPrepareFrameRendering(VkCommandBuffer cmd, uint32_t image_index) override
Record resource transitions that must happen before dynamic rendering begins.
Definition stencil.cpp:27
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:34
void onSwapchainAboutToRecreate() override
Called right before swapchain-dependent resources are recreated.
Definition stencil.cpp:25
void onRecordCustomRendering(VkCommandBuffer cmd, uint32_t image_index) override
Optional hook for derived classes to record extra draw commands.
Definition stencil.cpp:41
~StencilWindow() override
Definition stencil.cpp:18
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:39
VkExtent2D getSwapchainExtent() const noexcept
Get the current swapchain extent.
Definition mxvk.hpp:222
void loop()
Run the main event/render loop.
Definition mxvk.cpp:651
VkDevice getDevice() const noexcept
Get the Vulkan logical device handle.
Definition mxvk.hpp:198
VkDevice device
Definition mxvk.hpp:606
VkQueue getGraphicsQueue() const noexcept
Get the graphics queue handle.
Definition mxvk.hpp:204
VkCommandPool getCommandPool() const noexcept
Get the command pool used for graphics/upload work.
Definition mxvk.hpp:207
void setClearColor(float r, float g, float b, float a=1.0f)
Set the per-frame color attachment clear color.
Definition mxvk.cpp:637
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:210
VkPhysicalDevice getPhysicalDevice() const noexcept
Get the Vulkan physical device handle.
Definition mxvk.hpp:201
VkFormat getSwapchainFormat() const noexcept
Get the swapchain color format.
Definition mxvk.hpp:213
#define MXVK_VALIDATION
Definition mxvk.hpp:28
Reusable dynamic-rendering stencil image and fullscreen stencil pipelines.
int main()
Definition main.py:165
Utilities for loading and saving PNG images.
Definition mxvk.hpp:31
Plain data structure returned by proc_args() with all common libmx2 CLI options.
Definition argz.hpp:718
bool fullscreen
Whether fullscreen mode was requested.
Definition argz.hpp:724
bool enable_vsync
Enable FIFO present mode / v-sync (--enable-vsync).
Definition argz.hpp:738
int height
Viewport height in pixels (default: 720).
Definition argz.hpp:721
std::string path
Asset root; proc_args() defaults it to the executable directory.
Definition argz.hpp:723
int width
Viewport width in pixels (default: 1280).
Definition argz.hpp:720
Push-constant payload shared by the mask and content shaders.
Minimal Vulkan handles required by MXVK resource helpers.