MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include "mxvk/argz.hpp"
2#include "mxvk/mxvk.hpp"
4
5#include <array>
6#include <chrono>
7#include <cstdlib>
8#include <format>
9#include <iostream>
10#include <string>
11#include <vector>
12
13namespace example {
15 struct PushConstants {
16 float time;
17 float width;
18 float height;
19 float frame;
20 };
21
22 public:
23 StaticWindow(const std::string path, const std::string &text, int width, int height, bool fullscreen, bool enable_vsync) : mxvk::VK_Window(text, width, height, fullscreen, MXVK_VALIDATION, enable_vsync), shader_root((path.empty() ? std::string(static_example_ASSET_DIR) : path) + "/data") { setClearColor(0.02f, 0.03f, 0.06f, 1.0f); }
24
25 ~StaticWindow() override {
26 if (device != VK_NULL_HANDLE) {
27 vkDeviceWaitIdle(device);
28 }
29 destroyGraphicsPipeline();
30 }
31
32 void event(SDL_Event &e) override {
33 if (e.type == SDL_EVENT_KEY_DOWN && e.key.key == SDLK_ESCAPE) {
34 exit();
35 }
36 }
37
38 void onSwapchainAboutToRecreate() override { destroyGraphicsPipeline(); }
39
40 void onRecordCustomRendering(VkCommandBuffer cmd, [[maybe_unused]] uint32_t image_index) override {
41 if (!ensureGraphicsPipeline()) {
42 return;
43 }
44
45 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, graphics_pipeline);
46
47 const auto now = std::chrono::steady_clock::now();
48 const float elapsed_seconds = std::chrono::duration<float>(now - start_time).count();
49 const VkExtent2D extent = getSwapchainExtent();
50 const float width = static_cast<float>(extent.width);
51 const float height = static_cast<float>(extent.height);
52 const PushConstants push_constants{elapsed_seconds, width, height, static_cast<float>(frame_index++)};
53
54 vkCmdPushConstants(cmd, pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PushConstants), &push_constants);
55
56 vkCmdDraw(cmd, 3, 1, 0, 0);
57 }
58
59 private:
60 std::string shader_root;
61 std::chrono::steady_clock::time_point start_time{std::chrono::steady_clock::now()};
62 uint32_t frame_index = 0;
63
64 void destroyGraphicsPipeline() {
65 if (device == VK_NULL_HANDLE) {
66 pipeline_layout = VK_NULL_HANDLE;
67 graphics_pipeline = VK_NULL_HANDLE;
68 return;
69 }
70
71 if (graphics_pipeline != VK_NULL_HANDLE) {
72 vkDestroyPipeline(device, graphics_pipeline, nullptr);
73 graphics_pipeline = VK_NULL_HANDLE;
74 }
75 if (pipeline_layout != VK_NULL_HANDLE) {
76 vkDestroyPipelineLayout(device, pipeline_layout, nullptr);
77 pipeline_layout = VK_NULL_HANDLE;
78 }
79 }
80
81 bool ensureGraphicsPipeline() {
82 if (graphics_pipeline != VK_NULL_HANDLE && pipeline_layout != VK_NULL_HANDLE) {
83 return true;
84 }
85
86 createGraphicsPipeline();
87 return graphics_pipeline != VK_NULL_HANDLE && pipeline_layout != VK_NULL_HANDLE;
88 }
89
90 void createGraphicsPipeline() {
91 if (device == VK_NULL_HANDLE || swapchain_format == VK_FORMAT_UNDEFINED) {
92 return;
93 }
94
95 const std::string vert_path = shader_root + "/triangle.vert.spv";
96 const std::string frag_path = shader_root + "/triangle.frag.spv";
97 const std::vector<char> vert_bytes = loadSpv(vert_path);
98 const std::vector<char> frag_bytes = loadSpv(frag_path);
99
100 const VkShaderModule vert_module = createShaderModule(device, vert_bytes);
101 VkShaderModule frag_module = VK_NULL_HANDLE;
102 try {
103 frag_module = createShaderModule(device, frag_bytes);
104
105 std::array<VkPipelineShaderStageCreateInfo, 2> shader_stages{};
106 shader_stages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
107 shader_stages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
108 shader_stages[0].module = vert_module;
109 shader_stages[0].pName = "main";
110 shader_stages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
111 shader_stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
112 shader_stages[1].module = frag_module;
113 shader_stages[1].pName = "main";
114
115 VkPipelineVertexInputStateCreateInfo vertex_input{};
116 vertex_input.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
117
118 VkPipelineInputAssemblyStateCreateInfo input_assembly{};
119 input_assembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
120 input_assembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
121 input_assembly.primitiveRestartEnable = VK_FALSE;
122
123 VkPipelineViewportStateCreateInfo viewport_state{};
124 viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
125 viewport_state.viewportCount = 1;
126 viewport_state.scissorCount = 1;
127
128 const VkDynamicState dynamic_states[] = {
129 VK_DYNAMIC_STATE_VIEWPORT,
130 VK_DYNAMIC_STATE_SCISSOR,
131 };
132 VkPipelineDynamicStateCreateInfo dynamic_state{};
133 dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
134 dynamic_state.dynamicStateCount = 2;
135 dynamic_state.pDynamicStates = dynamic_states;
136
137 VkPipelineRasterizationStateCreateInfo rasterizer{};
138 rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
139 rasterizer.depthClampEnable = VK_FALSE;
140 rasterizer.rasterizerDiscardEnable = VK_FALSE;
141 rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
142 rasterizer.lineWidth = 1.0f;
143 rasterizer.cullMode = VK_CULL_MODE_NONE;
144 rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
145 rasterizer.depthBiasEnable = VK_FALSE;
146
147 VkPipelineMultisampleStateCreateInfo multisampling{};
148 multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
149 multisampling.sampleShadingEnable = VK_FALSE;
150 multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
151
152 VkPipelineDepthStencilStateCreateInfo depth_stencil{};
153 depth_stencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
154 depth_stencil.depthTestEnable = VK_FALSE;
155 depth_stencil.depthWriteEnable = VK_FALSE;
156 depth_stencil.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
157 depth_stencil.depthBoundsTestEnable = VK_FALSE;
158 depth_stencil.stencilTestEnable = VK_FALSE;
159
160 VkPipelineColorBlendAttachmentState color_blend_attachment{};
161 color_blend_attachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
162 color_blend_attachment.blendEnable = VK_FALSE;
163
164 VkPipelineColorBlendStateCreateInfo color_blending{};
165 color_blending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
166 color_blending.logicOpEnable = VK_FALSE;
167 color_blending.attachmentCount = 1;
168 color_blending.pAttachments = &color_blend_attachment;
169
170 VkPushConstantRange push_constant_range{};
171 push_constant_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
172 push_constant_range.offset = 0;
173 push_constant_range.size = sizeof(PushConstants);
174
175 VkPipelineLayoutCreateInfo pipeline_layout_info{};
176 pipeline_layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
177 pipeline_layout_info.pushConstantRangeCount = 1;
178 pipeline_layout_info.pPushConstantRanges = &push_constant_range;
179 if (vkCreatePipelineLayout(device, &pipeline_layout_info, nullptr, &pipeline_layout) != VK_SUCCESS) {
180 throw mxvk::Exception("Failed to create pipeline layout");
181 }
182
183 VkPipelineRenderingCreateInfo pipeline_rendering_info{};
184 pipeline_rendering_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO;
185 pipeline_rendering_info.viewMask = 0;
186 pipeline_rendering_info.colorAttachmentCount = 1;
187 pipeline_rendering_info.pColorAttachmentFormats = &swapchain_format;
188 const VkFormat depth_format = getDepthFormat();
189 if (depth_format != VK_FORMAT_UNDEFINED) {
190 pipeline_rendering_info.depthAttachmentFormat = depth_format;
191 }
192
193 VkGraphicsPipelineCreateInfo pipeline_info{};
194 pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
195 pipeline_info.pNext = &pipeline_rendering_info;
196 pipeline_info.stageCount = static_cast<uint32_t>(shader_stages.size());
197 pipeline_info.pStages = shader_stages.data();
198 pipeline_info.pVertexInputState = &vertex_input;
199 pipeline_info.pInputAssemblyState = &input_assembly;
200 pipeline_info.pViewportState = &viewport_state;
201 pipeline_info.pRasterizationState = &rasterizer;
202 pipeline_info.pMultisampleState = &multisampling;
203 pipeline_info.pDepthStencilState = &depth_stencil;
204 pipeline_info.pColorBlendState = &color_blending;
205 pipeline_info.pDynamicState = &dynamic_state;
206 pipeline_info.layout = pipeline_layout;
207 pipeline_info.renderPass = VK_NULL_HANDLE;
208 pipeline_info.subpass = 0;
209 pipeline_info.basePipelineHandle = VK_NULL_HANDLE;
210 pipeline_info.basePipelineIndex = -1;
211
212 if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipeline_info, nullptr, &graphics_pipeline) != VK_SUCCESS) {
213 throw mxvk::Exception("Failed to create graphics pipeline");
214 }
215 } catch (...) {
216 if (graphics_pipeline != VK_NULL_HANDLE) {
217 vkDestroyPipeline(device, graphics_pipeline, nullptr);
218 graphics_pipeline = VK_NULL_HANDLE;
219 }
220 if (pipeline_layout != VK_NULL_HANDLE) {
221 vkDestroyPipelineLayout(device, pipeline_layout, nullptr);
222 pipeline_layout = VK_NULL_HANDLE;
223 }
224 if (frag_module != VK_NULL_HANDLE) {
225 vkDestroyShaderModule(device, frag_module, nullptr);
226 }
227 vkDestroyShaderModule(device, vert_module, nullptr);
228 throw;
229 }
230
231 vkDestroyShaderModule(device, frag_module, nullptr);
232 vkDestroyShaderModule(device, vert_module, nullptr);
233 }
234
235 VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
236 VkPipeline graphics_pipeline = VK_NULL_HANDLE;
237 };
238} // namespace example
239
240int main(int argc, char **argv) {
241 try {
242 const Arguments args = proc_args(argc, argv);
243 example::StaticWindow ex_window(args.path, "Static Noise Example", args.width, args.height, args.fullscreen, args.enable_vsync);
244 ex_window.loop();
245 } catch (mxvk::Exception &e) {
246 std::cerr << std::format("mxvk: Exception: {}\n", e.text());
247 return EXIT_FAILURE;
248 } catch (ArgException<std::string> &e) {
249 std::cerr << std::format("mxvk: Argument Exception: {}\n", e.text());
250 }
251 return EXIT_SUCCESS;
252}
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
StaticWindow(const std::string path, const std::string &text, int width, int height, bool fullscreen, bool enable_vsync)
Definition main.cpp:23
~StaticWindow() override
Definition main.cpp:25
void onRecordCustomRendering(VkCommandBuffer cmd, uint32_t image_index) override
Optional hook for derived classes to record extra draw commands.
Definition main.cpp:40
void event(SDL_Event &e) override
Handle one SDL event.
Definition main.cpp:32
void onSwapchainAboutToRecreate() override
Called right before swapchain-dependent resources are recreated.
Definition main.cpp:38
std::string text() const
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 device
Definition mxvk.hpp:606
VkFormat swapchain_format
Definition mxvk.hpp:614
VkFormat depth_format
Definition mxvk.hpp:615
static VkShaderModule createShaderModule(VkDevice device, const std::vector< char > &spv_bytes)
Create a shader module from SPIR-V bytecode.
Definition mxvk.cpp:154
void setClearColor(float r, float g, float b, float a=1.0f)
Set the per-frame color attachment clear color.
Definition mxvk.cpp:637
void exit()
Request loop termination.
Definition mxvk.cpp:1378
VK_Window()=default
Construct an empty window object.
VkFormat getDepthFormat() const noexcept
Get the depth format used for dynamic rendering attachments.
Definition mxvk.hpp:236
static std::vector< char > loadSpv(const std::string &path)
Load a SPIR-V file from disk.
Definition mxvk.cpp:152
#define MXVK_VALIDATION
Definition mxvk.hpp:28
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