-
Notifications
You must be signed in to change notification settings - Fork 109
/
main.cpp
374 lines (326 loc) · 18.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// MIT License
//
// Copyright( c ) 2017 Packt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files( the "Software" ), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Vulkan Cookbook
// ISBN: 9781786468154
// © Packt Publishing Limited
//
// Author: Pawel Lapinski
// LinkedIn: https://www.linkedin.com/in/pawel-lapinski-84522329
//
// 09-Using_Geometry_Shaders
#include "CookbookSampleFramework.h"
using namespace VulkanCookbook;
class Sample : public VulkanCookbookSample {
VkDestroyer(VkBuffer) VertexBuffer;
VkDestroyer(VkDeviceMemory) BufferMemory;
VkDestroyer(VkRenderPass) RenderPass;
VkDestroyer(VkPipelineLayout) PipelineLayout;
VkDestroyer(VkPipeline) GraphicsPipeline;
virtual bool Initialize( WindowParameters window_parameters ) override {
VkPhysicalDeviceFeatures device_features = {};
device_features.geometryShader = true;
if( !InitializeVulkan( window_parameters, &device_features, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, false ) ) {
return false;
}
// Render pass
std::vector<VkAttachmentDescription> attachment_descriptions = {
{
0, // VkAttachmentDescriptionFlags flags
Swapchain.Format, // VkFormat format
VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples
VK_ATTACHMENT_LOAD_OP_CLEAR, // VkAttachmentLoadOp loadOp
VK_ATTACHMENT_STORE_OP_STORE, // VkAttachmentStoreOp storeOp
VK_ATTACHMENT_LOAD_OP_DONT_CARE, // VkAttachmentLoadOp stencilLoadOp
VK_ATTACHMENT_STORE_OP_DONT_CARE, // VkAttachmentStoreOp stencilStoreOp
VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR // VkImageLayout finalLayout
}
};
std::vector<SubpassParameters> subpass_parameters = {
{
VK_PIPELINE_BIND_POINT_GRAPHICS, // VkPipelineBindPoint PipelineType
{}, // std::vector<VkAttachmentReference> InputAttachments
{
{ // std::vector<VkAttachmentReference> ColorAttachments
0, // uint32_t attachment
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout layout
}
},
{}, // std::vector<VkAttachmentReference> ResolveAttachments
nullptr, // VkAttachmentReference const * DepthStencilAttachment
{} // std::vector<uint32_t> PreserveAttachments
}
};
std::vector<VkSubpassDependency> subpass_dependencies = {
{
VK_SUBPASS_EXTERNAL, // uint32_t srcSubpass
0, // uint32_t dstSubpass
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // VkPipelineStageFlags srcStageMask
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, // VkPipelineStageFlags dstStageMask
VK_ACCESS_MEMORY_READ_BIT, // VkAccessFlags srcAccessMask
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, // VkAccessFlags dstAccessMask
VK_DEPENDENCY_BY_REGION_BIT // VkDependencyFlags dependencyFlags
},
{
0, // uint32_t srcSubpass
VK_SUBPASS_EXTERNAL, // uint32_t dstSubpass
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, // VkPipelineStageFlags srcStageMask
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // VkPipelineStageFlags dstStageMask
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, // VkAccessFlags srcAccessMask
VK_ACCESS_MEMORY_READ_BIT, // VkAccessFlags dstAccessMask
VK_DEPENDENCY_BY_REGION_BIT // VkDependencyFlags dependencyFlags
}
};
InitVkDestroyer( LogicalDevice, RenderPass );
if( !CreateRenderPass( *LogicalDevice, attachment_descriptions, subpass_parameters, subpass_dependencies, *RenderPass ) ) {
return false;
}
// Graphics pipeline
std::vector<unsigned char> vertex_shader_spirv;
if( !GetBinaryFileContents( "Data/Shaders/Other/09 Using Geometry Shaders/shader.vert.spv", vertex_shader_spirv ) ) {
return false;
}
VkDestroyer(VkShaderModule) vertex_shader_module;
InitVkDestroyer( LogicalDevice, vertex_shader_module );
if( !CreateShaderModule( *LogicalDevice, vertex_shader_spirv, *vertex_shader_module ) ) {
return false;
}
std::vector<unsigned char> geometry_shader_spirv;
if( !GetBinaryFileContents( "Data/Shaders/Other/09 Using Geometry Shaders/shader.geom.spv", geometry_shader_spirv ) ) {
return false;
}
VkDestroyer(VkShaderModule) geometry_shader_module;
InitVkDestroyer( LogicalDevice, geometry_shader_module );
if( !CreateShaderModule( *LogicalDevice, geometry_shader_spirv, *geometry_shader_module ) ) {
return false;
}
std::vector<unsigned char> fragment_shader_spirv;
if( !GetBinaryFileContents( "Data/Shaders/Other/09 Using Geometry Shaders/shader.frag.spv", fragment_shader_spirv ) ) {
return false;
}
VkDestroyer(VkShaderModule) fragment_shader_module;
InitVkDestroyer( LogicalDevice, fragment_shader_module );
if( !CreateShaderModule( *LogicalDevice, fragment_shader_spirv, *fragment_shader_module ) ) {
return false;
}
std::vector<ShaderStageParameters> shader_stage_params = {
{
VK_SHADER_STAGE_VERTEX_BIT, // VkShaderStageFlagBits ShaderStage
*vertex_shader_module, // VkShaderModule ShaderModule
"main", // char const * EntryPointName;
nullptr // VkSpecializationInfo const * SpecializationInfo;
},
{
VK_SHADER_STAGE_GEOMETRY_BIT, // VkShaderStageFlagBits ShaderStage
*geometry_shader_module, // VkShaderModule ShaderModule
"main", // char const * EntryPointName;
nullptr // VkSpecializationInfo const * SpecializationInfo;
},
{
VK_SHADER_STAGE_FRAGMENT_BIT, // VkShaderStageFlagBits ShaderStage
*fragment_shader_module, // VkShaderModule ShaderModule
"main", // char const * EntryPointName
nullptr // VkSpecializationInfo const * SpecializationInfo
}
};
std::vector<VkPipelineShaderStageCreateInfo> shader_stage_create_infos;
SpecifyPipelineShaderStages( shader_stage_params, shader_stage_create_infos );
std::vector<VkVertexInputBindingDescription> vertex_input_binding_descriptions = {
{
0, // uint32_t binding
3 * sizeof( float ), // uint32_t stride
VK_VERTEX_INPUT_RATE_VERTEX // VkVertexInputRate inputRate
}
};
std::vector<VkVertexInputAttributeDescription> vertex_attribute_descriptions = {
{
0, // uint32_t location
0, // uint32_t binding
VK_FORMAT_R32G32B32_SFLOAT, // VkFormat format
0 // uint32_t offset
}
};
VkPipelineVertexInputStateCreateInfo vertex_input_state_create_info;
SpecifyPipelineVertexInputState( vertex_input_binding_descriptions, vertex_attribute_descriptions, vertex_input_state_create_info );
VkPipelineInputAssemblyStateCreateInfo input_assembly_state_create_info;
SpecifyPipelineInputAssemblyState( VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, false, input_assembly_state_create_info );
ViewportInfo viewport_infos = {
{ // std::vector<VkViewport> Viewports
{
0.0f, // float x
0.0f, // float y
500.0f, // float width
500.0f, // float height
0.0f, // float minDepth
1.0f // float maxDepth
}
},
{ // std::vector<VkRect2D> Scissors
{
{ // VkOffset2D offset
0, // int32_t x
0 // int32_t y
},
{ // VkExtent2D extent
500, // uint32_t width
500 // uint32_t height
}
}
}
};
VkPipelineViewportStateCreateInfo viewport_state_create_info;
SpecifyPipelineViewportAndScissorTestState( viewport_infos, viewport_state_create_info );
VkPipelineRasterizationStateCreateInfo rasterization_state_create_info;
SpecifyPipelineRasterizationState( false, false, VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, false, 0.0f, 0.0f, 0.0f, 1.0f, rasterization_state_create_info );
VkPipelineMultisampleStateCreateInfo multisample_state_create_info;
SpecifyPipelineMultisampleState( VK_SAMPLE_COUNT_1_BIT, false, 0.0f, nullptr, false, false, multisample_state_create_info );
std::vector<VkPipelineColorBlendAttachmentState> attachment_blend_states = {
{
false, // VkBool32 blendEnable
VK_BLEND_FACTOR_ONE, // VkBlendFactor srcColorBlendFactor
VK_BLEND_FACTOR_ONE, // VkBlendFactor dstColorBlendFactor
VK_BLEND_OP_ADD, // VkBlendOp colorBlendOp
VK_BLEND_FACTOR_ONE, // VkBlendFactor srcAlphaBlendFactor
VK_BLEND_FACTOR_ONE, // VkBlendFactor dstAlphaBlendFactor
VK_BLEND_OP_ADD, // VkBlendOp alphaBlendOp
VK_COLOR_COMPONENT_R_BIT | // VkColorComponentFlags colorWriteMask
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT |
VK_COLOR_COMPONENT_A_BIT
}
};
VkPipelineColorBlendStateCreateInfo blend_state_create_info;
SpecifyPipelineBlendState( false, VK_LOGIC_OP_COPY, attachment_blend_states, { 1.0f, 1.0f, 1.0f, 1.0f }, blend_state_create_info );
std::vector<VkDynamicState> dynamic_states = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR
};
VkPipelineDynamicStateCreateInfo dynamic_state_create_info;
SpecifyPipelineDynamicStates( dynamic_states, dynamic_state_create_info );
InitVkDestroyer( LogicalDevice, PipelineLayout );
if( !CreatePipelineLayout( *LogicalDevice, {}, {}, *PipelineLayout ) ) {
return false;
}
VkGraphicsPipelineCreateInfo graphics_pipeline_create_info;
SpecifyGraphicsPipelineCreationParameters( 0, shader_stage_create_infos, vertex_input_state_create_info, input_assembly_state_create_info,
nullptr, &viewport_state_create_info, rasterization_state_create_info, &multisample_state_create_info, nullptr, &blend_state_create_info,
&dynamic_state_create_info, *PipelineLayout, *RenderPass, 0, VK_NULL_HANDLE, -1, graphics_pipeline_create_info );
std::vector<VkPipeline> graphics_pipeline;
if( !CreateGraphicsPipelines( *LogicalDevice, { graphics_pipeline_create_info }, VK_NULL_HANDLE, graphics_pipeline ) ) {
return false;
}
InitVkDestroyer( LogicalDevice, GraphicsPipeline );
*GraphicsPipeline = graphics_pipeline[0];
// Vertex data
std::vector<float> vertices = {
0.0f, -0.75f, 0.0f,
-0.75f, 0.75f, 0.0f,
0.75f, 0.75f, 0.0f
};
InitVkDestroyer( LogicalDevice, VertexBuffer );
if( !CreateBuffer( *LogicalDevice, sizeof(vertices[0]) * vertices.size(), VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, *VertexBuffer ) ) {
return false;
}
InitVkDestroyer( LogicalDevice, BufferMemory );
if( !AllocateAndBindMemoryObjectToBuffer( PhysicalDevice, *LogicalDevice, *VertexBuffer, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, *BufferMemory ) ) {
return false;
}
if( !UseStagingBufferToUpdateBufferWithDeviceLocalMemoryBound( PhysicalDevice, *LogicalDevice, sizeof( vertices[0] ) * vertices.size(), &vertices[0], *VertexBuffer, 0, 0,
VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, GraphicsQueue.Handle, FramesResources.front().CommandBuffer, {} ) ) {
return false;
}
return true;
}
virtual bool Draw() override {
auto prepare_frame = [&]( VkCommandBuffer command_buffer, uint32_t swapchain_image_index, VkFramebuffer framebuffer ) {
if( !BeginCommandBufferRecordingOperation( command_buffer, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, nullptr ) ) {
return false;
}
if( PresentQueue.FamilyIndex != GraphicsQueue.FamilyIndex ) {
ImageTransition image_transition_before_drawing = {
Swapchain.Images[swapchain_image_index], // VkImage Image
VK_ACCESS_MEMORY_READ_BIT, // VkAccessFlags CurrentAccess
VK_ACCESS_MEMORY_READ_BIT, // VkAccessFlags NewAccess
VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout CurrentLayout
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout NewLayout
PresentQueue.FamilyIndex, // uint32_t CurrentQueueFamily
GraphicsQueue.FamilyIndex, // uint32_t NewQueueFamily
VK_IMAGE_ASPECT_COLOR_BIT // VkImageAspectFlags Aspect
};
SetImageMemoryBarrier( command_buffer, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, { image_transition_before_drawing } );
}
// Drawing
BeginRenderPass( command_buffer, *RenderPass, framebuffer, { { 0, 0 }, Swapchain.Size }, { { 0.1f, 0.2f, 0.3f, 1.0f } }, VK_SUBPASS_CONTENTS_INLINE );
BindPipelineObject( command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *GraphicsPipeline );
VkViewport viewport = {
0.0f, // float x
0.0f, // float y
static_cast<float>(Swapchain.Size.width), // float width
static_cast<float>(Swapchain.Size.height), // float height
0.0f, // float minDepth
1.0f, // float maxDepth
};
SetViewportStateDynamically( command_buffer, 0, { viewport } );
VkRect2D scissor = {
{ // VkOffset2D offset
0, // int32_t x
0 // int32_t y
},
{ // VkExtent2D extent
Swapchain.Size.width, // uint32_t width
Swapchain.Size.height // uint32_t height
}
};
SetScissorStateDynamically( command_buffer, 0, { scissor } );
BindVertexBuffers( command_buffer, 0, { { *VertexBuffer, 0 } } );
DrawGeometry( command_buffer, 3, 1, 0, 0 );
EndRenderPass( command_buffer );
if( PresentQueue.FamilyIndex != GraphicsQueue.FamilyIndex ) {
ImageTransition image_transition_before_present = {
Swapchain.Images[swapchain_image_index], // VkImage Image
VK_ACCESS_MEMORY_READ_BIT, // VkAccessFlags CurrentAccess
VK_ACCESS_MEMORY_READ_BIT, // VkAccessFlags NewAccess
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // VkImageLayout CurrentLayout
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // VkImageLayout NewLayout
GraphicsQueue.FamilyIndex, // uint32_t CurrentQueueFamily
PresentQueue.FamilyIndex, // uint32_t NewQueueFamily
VK_IMAGE_ASPECT_COLOR_BIT // VkImageAspectFlags Aspect
};
SetImageMemoryBarrier( command_buffer, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, { image_transition_before_present } );
}
if( !EndCommandBufferRecordingOperation( command_buffer ) ) {
return false;
}
return true;
};
return IncreasePerformanceThroughIncreasingTheNumberOfSeparatelyRenderedFrames( *LogicalDevice, GraphicsQueue.Handle, PresentQueue.Handle,
*Swapchain.Handle, Swapchain.Size, Swapchain.ImageViewsRaw, *RenderPass, {}, prepare_frame, FramesResources );
}
virtual bool Resize() override {
if( !CreateSwapchain( VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, false ) ) {
return false;
}
return true;
}
};
VULKAN_COOKBOOK_SAMPLE_FRAMEWORK( "09 - Using Geometry Shaders", 50, 25, 1280, 800, Sample )