15 struct PushConstants {
23 StaticWindow(
const std::string path,
const std::string &text,
int width,
int height,
bool fullscreen,
bool enable_vsync)
25 shader_root((path.empty() ? std::string(static_example_ASSET_DIR) : path) +
"/data") {
30 if (
device != VK_NULL_HANDLE) {
33 destroyGraphicsPipeline();
36 void event(SDL_Event &e)
override {
37 if (e.type == SDL_EVENT_KEY_DOWN && e.key.key == SDLK_ESCAPE) {
43 destroyGraphicsPipeline();
47 if (!ensureGraphicsPipeline()) {
51 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, graphics_pipeline);
53 const auto now = std::chrono::steady_clock::now();
54 const float elapsed_seconds = std::chrono::duration<float>(now - start_time).count();
56 const float width =
static_cast<float>(extent.width);
57 const float height =
static_cast<float>(extent.height);
58 const PushConstants push_constants{elapsed_seconds, width, height,
static_cast<float>(frame_index++)};
63 VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
65 sizeof(PushConstants),
68 vkCmdDraw(cmd, 3, 1, 0, 0);
72 std::string shader_root;
73 std::chrono::steady_clock::time_point start_time{std::chrono::steady_clock::now()};
74 uint32_t frame_index = 0;
76 void destroyGraphicsPipeline() {
77 if (
device == VK_NULL_HANDLE) {
78 pipeline_layout = VK_NULL_HANDLE;
79 graphics_pipeline = VK_NULL_HANDLE;
83 if (graphics_pipeline != VK_NULL_HANDLE) {
84 vkDestroyPipeline(
device, graphics_pipeline,
nullptr);
85 graphics_pipeline = VK_NULL_HANDLE;
87 if (pipeline_layout != VK_NULL_HANDLE) {
88 vkDestroyPipelineLayout(
device, pipeline_layout,
nullptr);
89 pipeline_layout = VK_NULL_HANDLE;
93 bool ensureGraphicsPipeline() {
94 if (graphics_pipeline != VK_NULL_HANDLE && pipeline_layout != VK_NULL_HANDLE) {
98 createGraphicsPipeline();
99 return graphics_pipeline != VK_NULL_HANDLE && pipeline_layout != VK_NULL_HANDLE;
102 void createGraphicsPipeline() {
107 const std::string vert_path = shader_root +
"/triangle.vert.spv";
108 const std::string frag_path = shader_root +
"/triangle.frag.spv";
109 const std::vector<char> vert_bytes =
loadSpv(vert_path);
110 const std::vector<char> frag_bytes =
loadSpv(frag_path);
113 VkShaderModule frag_module = VK_NULL_HANDLE;
117 std::array<VkPipelineShaderStageCreateInfo, 2> shader_stages{};
118 shader_stages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
119 shader_stages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
120 shader_stages[0].module = vert_module;
121 shader_stages[0].pName =
"main";
122 shader_stages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
123 shader_stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
124 shader_stages[1].module = frag_module;
125 shader_stages[1].pName =
"main";
127 VkPipelineVertexInputStateCreateInfo vertex_input{};
128 vertex_input.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
130 VkPipelineInputAssemblyStateCreateInfo input_assembly{};
131 input_assembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
132 input_assembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
133 input_assembly.primitiveRestartEnable = VK_FALSE;
135 VkPipelineViewportStateCreateInfo viewport_state{};
136 viewport_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
137 viewport_state.viewportCount = 1;
138 viewport_state.scissorCount = 1;
140 const VkDynamicState dynamic_states[] = {
141 VK_DYNAMIC_STATE_VIEWPORT,
142 VK_DYNAMIC_STATE_SCISSOR,
144 VkPipelineDynamicStateCreateInfo dynamic_state{};
145 dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
146 dynamic_state.dynamicStateCount = 2;
147 dynamic_state.pDynamicStates = dynamic_states;
149 VkPipelineRasterizationStateCreateInfo rasterizer{};
150 rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
151 rasterizer.depthClampEnable = VK_FALSE;
152 rasterizer.rasterizerDiscardEnable = VK_FALSE;
153 rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
154 rasterizer.lineWidth = 1.0f;
155 rasterizer.cullMode = VK_CULL_MODE_NONE;
156 rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
157 rasterizer.depthBiasEnable = VK_FALSE;
159 VkPipelineMultisampleStateCreateInfo multisampling{};
160 multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
161 multisampling.sampleShadingEnable = VK_FALSE;
162 multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
164 VkPipelineDepthStencilStateCreateInfo depth_stencil{};
165 depth_stencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
166 depth_stencil.depthTestEnable = VK_FALSE;
167 depth_stencil.depthWriteEnable = VK_FALSE;
168 depth_stencil.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
169 depth_stencil.depthBoundsTestEnable = VK_FALSE;
170 depth_stencil.stencilTestEnable = VK_FALSE;
172 VkPipelineColorBlendAttachmentState color_blend_attachment{};
173 color_blend_attachment.colorWriteMask =
174 VK_COLOR_COMPONENT_R_BIT |
175 VK_COLOR_COMPONENT_G_BIT |
176 VK_COLOR_COMPONENT_B_BIT |
177 VK_COLOR_COMPONENT_A_BIT;
178 color_blend_attachment.blendEnable = VK_FALSE;
180 VkPipelineColorBlendStateCreateInfo color_blending{};
181 color_blending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
182 color_blending.logicOpEnable = VK_FALSE;
183 color_blending.attachmentCount = 1;
184 color_blending.pAttachments = &color_blend_attachment;
186 VkPushConstantRange push_constant_range{};
187 push_constant_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
188 push_constant_range.offset = 0;
189 push_constant_range.size =
sizeof(PushConstants);
191 VkPipelineLayoutCreateInfo pipeline_layout_info{};
192 pipeline_layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
193 pipeline_layout_info.pushConstantRangeCount = 1;
194 pipeline_layout_info.pPushConstantRanges = &push_constant_range;
195 if (vkCreatePipelineLayout(
device, &pipeline_layout_info,
nullptr, &pipeline_layout) != VK_SUCCESS) {
196 throw mxvk::Exception(
"Failed to create pipeline layout");
199 VkPipelineRenderingCreateInfo pipeline_rendering_info{};
200 pipeline_rendering_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO;
201 pipeline_rendering_info.viewMask = 0;
202 pipeline_rendering_info.colorAttachmentCount = 1;
206 pipeline_rendering_info.depthAttachmentFormat =
depth_format;
209 VkGraphicsPipelineCreateInfo pipeline_info{};
210 pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
211 pipeline_info.pNext = &pipeline_rendering_info;
212 pipeline_info.stageCount =
static_cast<uint32_t
>(shader_stages.size());
213 pipeline_info.pStages = shader_stages.data();
214 pipeline_info.pVertexInputState = &vertex_input;
215 pipeline_info.pInputAssemblyState = &input_assembly;
216 pipeline_info.pViewportState = &viewport_state;
217 pipeline_info.pRasterizationState = &rasterizer;
218 pipeline_info.pMultisampleState = &multisampling;
219 pipeline_info.pDepthStencilState = &depth_stencil;
220 pipeline_info.pColorBlendState = &color_blending;
221 pipeline_info.pDynamicState = &dynamic_state;
222 pipeline_info.layout = pipeline_layout;
223 pipeline_info.renderPass = VK_NULL_HANDLE;
224 pipeline_info.subpass = 0;
225 pipeline_info.basePipelineHandle = VK_NULL_HANDLE;
226 pipeline_info.basePipelineIndex = -1;
228 if (vkCreateGraphicsPipelines(
device, VK_NULL_HANDLE, 1, &pipeline_info,
nullptr, &graphics_pipeline) != VK_SUCCESS) {
229 throw mxvk::Exception(
"Failed to create graphics pipeline");
232 if (graphics_pipeline != VK_NULL_HANDLE) {
233 vkDestroyPipeline(
device, graphics_pipeline,
nullptr);
234 graphics_pipeline = VK_NULL_HANDLE;
236 if (pipeline_layout != VK_NULL_HANDLE) {
237 vkDestroyPipelineLayout(
device, pipeline_layout,
nullptr);
238 pipeline_layout = VK_NULL_HANDLE;
240 if (frag_module != VK_NULL_HANDLE) {
241 vkDestroyShaderModule(
device, frag_module,
nullptr);
243 vkDestroyShaderModule(
device, vert_module,
nullptr);
247 vkDestroyShaderModule(
device, frag_module,
nullptr);
248 vkDestroyShaderModule(
device, vert_module,
nullptr);
251 VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
252 VkPipeline graphics_pipeline = VK_NULL_HANDLE;