forked from LunarG/VulkanSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_sets.cpp
More file actions
384 lines (334 loc) · 15 KB
/
multiple_sets.cpp
File metadata and controls
384 lines (334 loc) · 15 KB
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
375
376
377
378
379
380
381
382
383
384
/*
* Vulkan Samples
*
* Copyright (C) 2015-2016 Valve Corporation
* Copyright (C) 2015-2016 LunarG, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
VULKAN_SAMPLE_SHORT_DESCRIPTION
Use multiple descriptor sets to draw a textured cube.
*/
#include <util_init.hpp>
#include <assert.h>
#include <string.h>
#include <cstdlib>
#include "cube_data.h"
// This sample is based on drawtexturedcube, but rather than place both
// UBO and sampled image into a single set, we split it up to demonstrate
// how to create, submit, and reference multiple sets in a shader
/* For this sample, we'll start with GLSL so the shader function is plain */
/* and then use the glslang GLSLtoSPV utility to convert it to SPIR-V for */
/* the driver. We do this for clarity rather than using pre-compiled */
/* SPIR-V */
const char *vertShaderText =
"#version 400\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_ARB_shading_language_420pack : enable\n"
// Note, set = 0 here
"layout (std140, set = 0, binding = 0) uniform bufferVals {\n"
" mat4 mvp;\n"
"} myBufferVals;\n"
// And set = 1 here
"layout (set = 1, binding = 0) uniform sampler2D surface;\n"
"layout (location = 0) in vec4 pos;\n"
"layout (location = 1) in vec2 inTexCoords;\n"
"layout (location = 0) out vec4 outColor;\n"
"layout (location = 1) out vec2 outTexCoords;\n"
"out gl_PerVertex { \n"
" vec4 gl_Position;\n"
"};\n"
"void main() {\n"
" outColor = texture(surface, vec2(0.0));\n"
" outTexCoords = inTexCoords;\n"
" gl_Position = myBufferVals.mvp * pos;\n"
"}\n";
const char *fragShaderText =
"#version 400\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_ARB_shading_language_420pack : enable\n"
"layout (location = 0) in vec4 inColor;\n"
"layout (location = 1) in vec2 inTexCoords;\n"
"layout (location = 0) out vec4 outColor;\n"
"void main() {\n"
" vec4 resColor = inColor;\n"
// Create a border to see the cube more easily
" if (inTexCoords.x < 0.01 || inTexCoords.x > 0.99)\n"
" resColor *= vec4(0.1, 0.1, 0.1, 1.0);\n"
" if (inTexCoords.y < 0.01 || inTexCoords.y > 0.99)\n"
" resColor *= vec4(0.1, 0.1, 0.1, 1.0);\n"
" outColor = resColor;\n"
"}\n";
int sample_main(int argc, char *argv[]) {
VkResult U_ASSERT_ONLY res;
struct sample_info info = {};
char sample_title[] = "Multiple Descriptor Sets";
process_command_line_args(info, argc, argv);
init_global_layer_properties(info);
init_instance_extension_names(info);
init_device_extension_names(info);
init_instance(info, sample_title);
init_enumerate_device(info);
init_window_size(info, 500, 500);
init_connection(info);
init_window(info);
init_swapchain_extension(info);
init_device(info);
init_command_pool(info);
init_command_buffer(info);
execute_begin_command_buffer(info);
init_device_queue(info);
init_swap_chain(info);
init_depth_buffer(info);
// Sample from a green texture to easily see that we've pulled correct texel
// value
const char *textureName = "green.ppm";
init_texture(info, textureName);
init_uniform_buffer(info);
init_renderpass(info, true);
init_shaders(info, vertShaderText, fragShaderText);
init_framebuffers(info, true);
init_vertex_buffer(info, g_vb_texture_Data, sizeof(g_vb_texture_Data),
sizeof(g_vb_texture_Data[0]), true);
/* VULKAN_KEY_START */
// Set up two descriptor sets
static const unsigned descriptor_set_count = 2;
// Create first layout to contain uniform buffer data
VkDescriptorSetLayoutBinding uniform_binding[1] = {};
uniform_binding[0].binding = 0;
uniform_binding[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
uniform_binding[0].descriptorCount = 1;
uniform_binding[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
uniform_binding[0].pImmutableSamplers = NULL;
VkDescriptorSetLayoutCreateInfo uniform_layout_info[1] = {};
uniform_layout_info[0].sType =
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
uniform_layout_info[0].pNext = NULL;
uniform_layout_info[0].bindingCount = 1;
uniform_layout_info[0].pBindings = uniform_binding;
// Create second layout containing combined sampler/image data
VkDescriptorSetLayoutBinding sampler2D_binding[1] = {};
sampler2D_binding[0].binding = 0;
sampler2D_binding[0].descriptorType =
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
sampler2D_binding[0].descriptorCount = 1;
sampler2D_binding[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
sampler2D_binding[0].pImmutableSamplers = NULL;
VkDescriptorSetLayoutCreateInfo sampler2D_layout_info[1] = {};
sampler2D_layout_info[0].sType =
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
sampler2D_layout_info[0].pNext = NULL;
sampler2D_layout_info[0].bindingCount = 1;
sampler2D_layout_info[0].pBindings = sampler2D_binding;
// Create multiple sets, using each createInfo
static const unsigned uniform_set_index = 0;
static const unsigned sampler_set_index = 1;
VkDescriptorSetLayout descriptor_layouts[descriptor_set_count] = {};
res = vkCreateDescriptorSetLayout(info.device, uniform_layout_info, NULL,
&descriptor_layouts[uniform_set_index]);
assert(res == VK_SUCCESS);
res = vkCreateDescriptorSetLayout(info.device, sampler2D_layout_info, NULL,
&descriptor_layouts[sampler_set_index]);
assert(res == VK_SUCCESS);
// Create pipeline layout with multiple descriptor sets
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo[1] = {};
pipelineLayoutCreateInfo[0].sType =
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutCreateInfo[0].pNext = NULL;
pipelineLayoutCreateInfo[0].pushConstantRangeCount = 0;
pipelineLayoutCreateInfo[0].pPushConstantRanges = NULL;
pipelineLayoutCreateInfo[0].setLayoutCount = descriptor_set_count;
pipelineLayoutCreateInfo[0].pSetLayouts = descriptor_layouts;
res = vkCreatePipelineLayout(info.device, pipelineLayoutCreateInfo, NULL,
&info.pipeline_layout);
assert(res == VK_SUCCESS);
// Create a single pool to contain data for our two descriptor sets
VkDescriptorPoolSize type_count[2] = {};
type_count[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
type_count[0].descriptorCount = 1;
type_count[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
type_count[1].descriptorCount = 1;
VkDescriptorPoolCreateInfo pool_info[1] = {};
pool_info[0].sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
pool_info[0].pNext = NULL;
pool_info[0].maxSets = descriptor_set_count;
pool_info[0].poolSizeCount =
sizeof(type_count) / sizeof(VkDescriptorPoolSize);
pool_info[0].pPoolSizes = type_count;
VkDescriptorPool descriptor_pool[1] = {};
res = vkCreateDescriptorPool(info.device, pool_info, NULL, descriptor_pool);
assert(res == VK_SUCCESS);
VkDescriptorSetAllocateInfo alloc_info[1];
alloc_info[0].sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
alloc_info[0].pNext = NULL;
alloc_info[0].descriptorPool = descriptor_pool[0];
alloc_info[0].descriptorSetCount = descriptor_set_count;
alloc_info[0].pSetLayouts = descriptor_layouts;
// Populate descriptor sets
VkDescriptorSet descriptor_sets[descriptor_set_count] = {};
res = vkAllocateDescriptorSets(info.device, alloc_info, descriptor_sets);
assert(res == VK_SUCCESS);
// Using empty brace initializer on the next line triggers a bug in older
// versions of gcc, so memset instead
VkWriteDescriptorSet descriptor_writes[2];
memset(descriptor_writes, 0, sizeof(descriptor_writes));
// Populate with info about our uniform buffer
descriptor_writes[0] = {};
descriptor_writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptor_writes[0].pNext = NULL;
descriptor_writes[0].dstSet = descriptor_sets[uniform_set_index];
descriptor_writes[0].descriptorCount = 1;
descriptor_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptor_writes[0].pBufferInfo =
&info.uniform_data.buffer_info; // populated by init_uniform_buffer()
descriptor_writes[0].dstArrayElement = 0;
descriptor_writes[0].dstBinding = 0;
// Populate with info about our sampled image
descriptor_writes[1] = {};
descriptor_writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptor_writes[1].pNext = NULL;
descriptor_writes[1].dstSet = descriptor_sets[sampler_set_index];
descriptor_writes[1].descriptorCount = 1;
descriptor_writes[1].descriptorType =
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptor_writes[1].pImageInfo =
&info.texture_data.image_info; // populated by init_texture()
descriptor_writes[1].dstArrayElement = 0;
descriptor_writes[1].dstBinding = 0;
vkUpdateDescriptorSets(info.device, descriptor_set_count, descriptor_writes,
0, NULL);
/* VULKAN_KEY_END */
// Call remaining boilerplate utils
init_pipeline_cache(info);
init_pipeline(info, true);
// The remaining is identical to drawtexturedcube
VkClearValue clear_values[2];
clear_values[0].color.float32[0] = 0.2f;
clear_values[0].color.float32[1] = 0.2f;
clear_values[0].color.float32[2] = 0.2f;
clear_values[0].color.float32[3] = 0.2f;
clear_values[1].depthStencil.depth = 1.0f;
clear_values[1].depthStencil.stencil = 0;
VkSemaphore presentCompleteSemaphore;
VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo;
presentCompleteSemaphoreCreateInfo.sType =
VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
presentCompleteSemaphoreCreateInfo.pNext = NULL;
presentCompleteSemaphoreCreateInfo.flags = 0;
res = vkCreateSemaphore(info.device, &presentCompleteSemaphoreCreateInfo,
NULL, &presentCompleteSemaphore);
assert(res == VK_SUCCESS);
// Get the index of the next available swapchain image:
res = vkAcquireNextImageKHR(info.device, info.swap_chain, UINT64_MAX,
presentCompleteSemaphore, VK_NULL_HANDLE,
&info.current_buffer);
// TODO: Deal with the VK_SUBOPTIMAL_KHR and VK_ERROR_OUT_OF_DATE_KHR
// return codes
assert(res == VK_SUCCESS);
set_image_layout(info, info.buffers[info.current_buffer].image,
VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
VkRenderPassBeginInfo rp_begin;
rp_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
rp_begin.pNext = NULL;
rp_begin.renderPass = info.render_pass;
rp_begin.framebuffer = info.framebuffers[info.current_buffer];
rp_begin.renderArea.offset.x = 0;
rp_begin.renderArea.offset.y = 0;
rp_begin.renderArea.extent.width = info.width;
rp_begin.renderArea.extent.height = info.height;
rp_begin.clearValueCount = 2;
rp_begin.pClearValues = clear_values;
vkCmdBeginRenderPass(info.cmd, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(info.cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipeline);
vkCmdBindDescriptorSets(info.cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
info.pipeline_layout, 0, descriptor_set_count,
descriptor_sets, 0, NULL);
const VkDeviceSize offsets[1] = {0};
vkCmdBindVertexBuffers(info.cmd, 0, 1, &info.vertex_buffer.buf, offsets);
init_viewports(info);
init_scissors(info);
vkCmdDraw(info.cmd, 12 * 3, 1, 0, 0);
vkCmdEndRenderPass(info.cmd);
execute_pre_present_barrier(info);
res = vkEndCommandBuffer(info.cmd);
assert(res == VK_SUCCESS);
const VkCommandBuffer cmd_bufs[] = {info.cmd};
VkFenceCreateInfo fenceInfo;
VkFence drawFence;
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.pNext = NULL;
fenceInfo.flags = 0;
vkCreateFence(info.device, &fenceInfo, NULL, &drawFence);
VkPipelineStageFlags pipe_stage_flags =
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
VkSubmitInfo submit_info[1] = {};
submit_info[0].pNext = NULL;
submit_info[0].sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submit_info[0].waitSemaphoreCount = 1;
submit_info[0].pWaitSemaphores = &presentCompleteSemaphore;
submit_info[0].pWaitDstStageMask = &pipe_stage_flags;
submit_info[0].commandBufferCount = 1;
submit_info[0].pCommandBuffers = cmd_bufs;
submit_info[0].signalSemaphoreCount = 0;
submit_info[0].pSignalSemaphores = NULL;
/* Queue the command buffer for execution */
res = vkQueueSubmit(info.queue, 1, submit_info, drawFence);
assert(res == VK_SUCCESS);
/* Now present the image in the window */
VkPresentInfoKHR present;
present.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
present.pNext = NULL;
present.swapchainCount = 1;
present.pSwapchains = &info.swap_chain;
present.pImageIndices = &info.current_buffer;
present.pWaitSemaphores = NULL;
present.waitSemaphoreCount = 0;
present.pResults = NULL;
/* Make sure command buffer is finished before presenting */
do {
res =
vkWaitForFences(info.device, 1, &drawFence, VK_TRUE, FENCE_TIMEOUT);
} while (res == VK_TIMEOUT);
assert(res == VK_SUCCESS);
res = vkQueuePresentKHR(info.queue, &present);
assert(res == VK_SUCCESS);
wait_seconds(1);
if (info.save_images)
write_ppm(info, "multiple_sets");
vkDestroySemaphore(info.device, presentCompleteSemaphore, NULL);
vkDestroyFence(info.device, drawFence, NULL);
destroy_pipeline(info);
destroy_pipeline_cache(info);
destroy_textures(info);
// instead of destroy_descriptor_pool(info);
vkDestroyDescriptorPool(info.device, descriptor_pool[0], NULL);
destroy_vertex_buffer(info);
destroy_framebuffers(info);
destroy_shaders(info);
destroy_renderpass(info);
// instead of destroy_descriptor_and_pipeline_layouts(info);
for (int i = 0; i < descriptor_set_count; i++)
vkDestroyDescriptorSetLayout(info.device, descriptor_layouts[i], NULL);
vkDestroyPipelineLayout(info.device, info.pipeline_layout, NULL);
destroy_uniform_buffer(info);
destroy_depth_buffer(info);
destroy_swap_chain(info);
destroy_command_buffer(info);
destroy_command_pool(info);
destroy_device(info);
destroy_window(info);
destroy_instance(info);
return 0;
}