Skip to content

Commit 9cf2375

Browse files
committed
Uniform Buffers
1 parent 27500be commit 9cf2375

3 files changed

Lines changed: 86 additions & 36 deletions

File tree

shaders/model_vert.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
layout(location = 0) in vec3 in_position;
44
layout(location = 1) in vec3 in_normal;
55

6-
layout(push_constant) uniform pushConstants {
6+
layout(set = 0, binding = 0) uniform transform {
77
mat4 modelViewProj;
8-
} u_pushConstants;
8+
} u_transforms;
99

1010
layout(location = 0) out vec3 out_normal;
1111

1212
void main() {
13-
mat4 modelViewProj = u_pushConstants.modelViewProj;
13+
mat4 modelViewProj = u_transforms.modelViewProj;
1414
gl_Position = modelViewProj * vec4(in_position, 1.0);
1515
out_normal = in_normal;
1616
}

shaders/model_vert.spv

28 Bytes
Binary file not shown.

src/main.cpp

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,27 @@ VulkanSwapchain swapchain;
2121
VkRenderPass renderPass;
2222
std::vector<VulkanImage> depthBuffers;
2323
std::vector<VkFramebuffer> framebuffers;
24-
VulkanPipeline spritePipeline;
25-
VulkanPipeline modelPipeline;
2624
VkCommandPool commandPools[FRAMES_IN_FLIGHT];
2725
VkCommandBuffer commandBuffers[FRAMES_IN_FLIGHT];
2826
VkFence fences[FRAMES_IN_FLIGHT];
2927
VkSemaphore acquireSemaphores[FRAMES_IN_FLIGHT];
3028
VkSemaphore releaseSemaphores[FRAMES_IN_FLIGHT];
31-
VulkanBuffer vertexBuffer;
32-
VulkanBuffer indexBuffer;
33-
VulkanImage image;
3429

30+
VulkanBuffer spriteVertexBuffer;
31+
VulkanBuffer spriteIndexBuffer;
32+
VulkanImage image;
3533
VkSampler sampler;
36-
VkDescriptorPool descriptorPool;
37-
VkDescriptorSet descriptorSet;
38-
VkDescriptorSetLayout descriptorLayout;
34+
VkDescriptorPool spriteDescriptorPool;
35+
VkDescriptorSet spriteDescriptorSet;
36+
VkDescriptorSetLayout spriteDescriptorLayout;
37+
VulkanPipeline spritePipeline;
3938

4039
Model model;
40+
VulkanPipeline modelPipeline;
41+
VkDescriptorSetLayout modelDescriptorSetLayout;
42+
VkDescriptorPool modelDescriptorPool;
43+
VkDescriptorSet modelDescriptorSets[FRAMES_IN_FLIGHT];
44+
VulkanBuffer modelUniformBuffers[FRAMES_IN_FLIGHT];
4145

4246
bool handleMessage() {
4347
SDL_Event event;
@@ -161,7 +165,7 @@ void initApplication(SDL_Window* window) {
161165
createInfo.maxSets = 1;
162166
createInfo.poolSizeCount = ARRAY_COUNT(poolSizes);
163167
createInfo.pPoolSizes = poolSizes;
164-
VKA(vkCreateDescriptorPool(context->device, &createInfo, 0, &descriptorPool));
168+
VKA(vkCreateDescriptorPool(context->device, &createInfo, 0, &spriteDescriptorPool));
165169
}
166170

167171
{
@@ -171,25 +175,66 @@ void initApplication(SDL_Window* window) {
171175
VkDescriptorSetLayoutCreateInfo createInfo = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
172176
createInfo.bindingCount = ARRAY_COUNT(bindings);
173177
createInfo.pBindings = bindings;
174-
VKA(vkCreateDescriptorSetLayout(context->device, &createInfo, 0, &descriptorLayout));
178+
VKA(vkCreateDescriptorSetLayout(context->device, &createInfo, 0, &spriteDescriptorLayout));
175179

176180
VkDescriptorSetAllocateInfo allocateInfo = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
177-
allocateInfo.descriptorPool = descriptorPool;
181+
allocateInfo.descriptorPool = spriteDescriptorPool;
178182
allocateInfo.descriptorSetCount = 1;
179-
allocateInfo.pSetLayouts = &descriptorLayout;
180-
VKA(vkAllocateDescriptorSets(context->device, &allocateInfo, &descriptorSet));
183+
allocateInfo.pSetLayouts = &spriteDescriptorLayout;
184+
VKA(vkAllocateDescriptorSets(context->device, &allocateInfo, &spriteDescriptorSet));
181185

182186
VkDescriptorImageInfo imageInfo = { sampler, image.view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL};
183187
VkWriteDescriptorSet descriptorWrites[1];
184188
descriptorWrites[0] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
185-
descriptorWrites[0].dstSet = descriptorSet;
189+
descriptorWrites[0].dstSet = spriteDescriptorSet;
186190
descriptorWrites[0].dstBinding = 0;
187191
descriptorWrites[0].descriptorCount = 1;
188192
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
189193
descriptorWrites[0].pImageInfo = &imageInfo;
190194
VK(vkUpdateDescriptorSets(context->device, ARRAY_COUNT(descriptorWrites), descriptorWrites, 0, 0));
191195
}
192196

197+
{
198+
VkDescriptorPoolSize poolSizes[] = {
199+
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, FRAMES_IN_FLIGHT},
200+
};
201+
VkDescriptorPoolCreateInfo createInfo = {VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
202+
createInfo.maxSets = FRAMES_IN_FLIGHT;
203+
createInfo.poolSizeCount = ARRAY_COUNT(poolSizes);
204+
createInfo.pPoolSizes = poolSizes;
205+
VKA(vkCreateDescriptorPool(context->device, &createInfo, 0, &modelDescriptorPool));
206+
}
207+
for(uint32_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
208+
createBuffer(context, &modelUniformBuffers[i], sizeof(glm::mat4), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
209+
}
210+
{
211+
VkDescriptorSetLayoutBinding bindings[] = {
212+
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT, 0},
213+
};
214+
VkDescriptorSetLayoutCreateInfo createInfo = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
215+
createInfo.bindingCount = ARRAY_COUNT(bindings);
216+
createInfo.pBindings = bindings;
217+
VKA(vkCreateDescriptorSetLayout(context->device, &createInfo, 0, &modelDescriptorSetLayout));
218+
219+
for(uint32_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
220+
VkDescriptorSetAllocateInfo allocateInfo = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
221+
allocateInfo.descriptorPool = modelDescriptorPool;
222+
allocateInfo.descriptorSetCount = 1;
223+
allocateInfo.pSetLayouts = &modelDescriptorSetLayout;
224+
VKA(vkAllocateDescriptorSets(context->device, &allocateInfo, &modelDescriptorSets[i]));
225+
226+
VkDescriptorBufferInfo bufferInfo = {modelUniformBuffers[i].buffer, 0, sizeof(glm::mat4)};
227+
VkWriteDescriptorSet descriptorWrites[1];
228+
descriptorWrites[0] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
229+
descriptorWrites[0].dstSet = modelDescriptorSets[i];
230+
descriptorWrites[0].dstBinding = 0;
231+
descriptorWrites[0].descriptorCount = 1;
232+
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
233+
descriptorWrites[0].pBufferInfo = &bufferInfo;
234+
VK(vkUpdateDescriptorSets(context->device, ARRAY_COUNT(descriptorWrites), descriptorWrites, 0, 0));
235+
}
236+
}
237+
193238
VkVertexInputAttributeDescription vertexAttributeDescriptions[3] = {};
194239
vertexAttributeDescriptions[0].binding = 0;
195240
vertexAttributeDescriptions[0].location = 0;
@@ -208,7 +253,7 @@ void initApplication(SDL_Window* window) {
208253
vertexInputBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
209254
vertexInputBinding.stride = sizeof(float) * 7;
210255
spritePipeline = createPipeline(context, "../shaders/texture_vert.spv", "../shaders/texture_frag.spv", renderPass, swapchain.width, swapchain.height,
211-
vertexAttributeDescriptions, ARRAY_COUNT(vertexAttributeDescriptions), &vertexInputBinding, 1, &descriptorLayout, 0);
256+
vertexAttributeDescriptions, ARRAY_COUNT(vertexAttributeDescriptions), &vertexInputBinding, 1, &spriteDescriptorLayout, 0);
212257

213258

214259
VkVertexInputAttributeDescription modelAttributeDescriptions[2] = {};
@@ -224,12 +269,8 @@ void initApplication(SDL_Window* window) {
224269
modelInputBinding.binding = 0;
225270
modelInputBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
226271
modelInputBinding.stride = sizeof(float) * 6;
227-
VkPushConstantRange pushConstant = {};
228-
pushConstant.offset = 0;
229-
pushConstant.size = sizeof(glm::mat4);
230-
pushConstant.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
231272
modelPipeline = createPipeline(context, "../shaders/model_vert.spv", "../shaders/model_frag.spv", renderPass, swapchain.width, swapchain.height,
232-
modelAttributeDescriptions, ARRAY_COUNT(modelAttributeDescriptions), &modelInputBinding, 0, 0, &pushConstant);
273+
modelAttributeDescriptions, ARRAY_COUNT(modelAttributeDescriptions), &modelInputBinding, 1, &modelDescriptorSetLayout, 0);
233274

234275
for(uint32_t i = 0; i < ARRAY_COUNT(fences); ++i) {
235276
VkFenceCreateInfo createInfo = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
@@ -256,11 +297,11 @@ void initApplication(SDL_Window* window) {
256297
VKA(vkAllocateCommandBuffers(context->device, &allocateInfo, &commandBuffers[i]));
257298
}
258299

259-
createBuffer(context, &vertexBuffer, sizeof(vertexData), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
260-
uploadDataToBuffer(context, &vertexBuffer, vertexData, sizeof(vertexData));
300+
createBuffer(context, &spriteVertexBuffer, sizeof(vertexData), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
301+
uploadDataToBuffer(context, &spriteVertexBuffer, vertexData, sizeof(vertexData));
261302

262-
createBuffer(context, &indexBuffer, sizeof(indexData), VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
263-
uploadDataToBuffer(context, &indexBuffer, indexData, sizeof(indexData));
303+
createBuffer(context, &spriteIndexBuffer, sizeof(indexData), VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
304+
uploadDataToBuffer(context, &spriteIndexBuffer, indexData, sizeof(indexData));
264305

265306
model = createModel(context, "../data/models/monkey.glb");
266307
}
@@ -344,9 +385,9 @@ void renderApplication() {
344385
#if 0
345386
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, spritePipeline.pipeline);
346387
VkDeviceSize offset = 0;
347-
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertexBuffer.buffer, &offset);
348-
vkCmdBindIndexBuffer(commandBuffer, indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
349-
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, spritePipeline.pipelineLayout, 0, 1, &descriptorSet, 0, 0);
388+
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &spriteVertexBuffer.buffer, &offset);
389+
vkCmdBindIndexBuffer(commandBuffer, spriteIndexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
390+
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, spritePipeline.pipelineLayout, 0, 1, &spriteDescriptorSet, 0, 0);
350391
vkCmdDrawIndexed(commandBuffer, ARRAY_COUNT(indexData), 1, 0, 0, 0);
351392
#else
352393
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 2.0f));
@@ -358,11 +399,16 @@ void renderApplication() {
358399
glm::mat4 projection = getProjectionInverseZ(glm::radians(80.0f), swapchain.width, swapchain.height, 0.01f);
359400
glm::mat4 modelViewProj = projection * modelMatrix;
360401

402+
void* mapped;
403+
VK(vkMapMemory(context->device, modelUniformBuffers[frameIndex].memory, 0, sizeof(glm::mat4), 0, &mapped));
404+
memcpy(mapped, &modelViewProj, sizeof(modelViewProj));
405+
VK(vkUnmapMemory(context->device, modelUniformBuffers[frameIndex].memory));
406+
361407
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, modelPipeline.pipeline);
362408
VkDeviceSize offset = 0;
363409
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &model.vertexBuffer.buffer, &offset);
364410
vkCmdBindIndexBuffer(commandBuffer, model.indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT16);
365-
vkCmdPushConstants(commandBuffer, modelPipeline.pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(modelViewProj), &modelViewProj);
411+
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, modelPipeline.pipelineLayout, 0, 1, &modelDescriptorSets[frameIndex], 0, 0);
366412
vkCmdDrawIndexed(commandBuffer, model.numIndices, 1, 0, 0, 0);
367413
#endif
368414

@@ -402,13 +448,17 @@ void renderApplication() {
402448
void shutdownApplication() {
403449
VKA(vkDeviceWaitIdle(context->device));
404450

405-
VK(vkDestroyDescriptorPool(context->device, descriptorPool, 0));
406-
VK(vkDestroyDescriptorSetLayout(context->device, descriptorLayout, 0));
407-
451+
VK(vkDestroyDescriptorPool(context->device, modelDescriptorPool, 0));
452+
VK(vkDestroyDescriptorSetLayout(context->device, modelDescriptorSetLayout, 0));
408453
destroyModel(context, &model);
409-
destroyBuffer(context, &vertexBuffer);
410-
destroyBuffer(context, &indexBuffer);
454+
for(uint32_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
455+
destroyBuffer(context, &modelUniformBuffers[i]);
456+
}
411457

458+
VK(vkDestroyDescriptorPool(context->device, spriteDescriptorPool, 0));
459+
VK(vkDestroyDescriptorSetLayout(context->device, spriteDescriptorLayout, 0));
460+
destroyBuffer(context, &spriteVertexBuffer);
461+
destroyBuffer(context, &spriteIndexBuffer);
412462
destroyImage(context, &image);
413463

414464
for(uint32_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {

0 commit comments

Comments
 (0)