Skip to content

Commit cfeb1fe

Browse files
committed
Replace dynamic sizing with arrays using constexpr for max concurrent frames
1 parent 9752a41 commit cfeb1fe

81 files changed

Lines changed: 351 additions & 504 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/bloom/bloom.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class VulkanExample : public VulkanExampleBase
4848
vks::Buffer skyBox;
4949
vks::Buffer blurParams;
5050
};
51-
std::vector<UniformBuffers> uniformBuffers;
51+
std::array<UniformBuffers, maxConcurrentFrames> uniformBuffers{};
5252

5353
struct {
5454
VkPipelineLayout blur;
@@ -74,7 +74,7 @@ class VulkanExample : public VulkanExampleBase
7474
VkDescriptorSet scene;
7575
VkDescriptorSet skyBox;
7676
};
77-
std::vector<DescriptorSets> descriptorSets;
77+
std::array<DescriptorSets, maxConcurrentFrames> descriptorSets{};
7878

7979
// Framebuffer for offscreen rendering
8080
struct FrameBufferAttachment {
@@ -103,8 +103,6 @@ class VulkanExample : public VulkanExampleBase
103103
camera.setPosition(glm::vec3(0.0f, 0.0f, -10.25f));
104104
camera.setRotation(glm::vec3(7.5f, -343.0f, 0.0f));
105105
camera.setPerspective(45.0f, (float)width / (float)height, 0.1f, 256.0f);
106-
uniformBuffers.resize(maxConcurrentFrames);
107-
descriptorSets.resize(maxConcurrentFrames);
108106
}
109107

110108
~VulkanExample()

examples/computecloth/computecloth.cpp

Lines changed: 173 additions & 154 deletions
Large diffs are not rendered by default.

examples/computeparticles/computeparticles.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class VulkanExample : public VulkanExampleBase
4141

4242
// We use a shader storage buffer object to store the particlces
4343
// This is updated by the compute pipeline and displayed as a vertex buffer by the graphics pipeline
44-
std::vector<vks::Buffer> storageBuffers;
44+
std::array<vks::Buffer, maxConcurrentFrames> storageBuffers;
4545

4646
// Resources for the graphics part of the example
4747
struct Graphics {
@@ -54,20 +54,20 @@ class VulkanExample : public VulkanExampleBase
5454

5555
// Resources for the compute part of the example
5656
struct Compute {
57-
uint32_t queueFamilyIndex; // Used to check if compute and graphics queue families differ and require additional barriers
58-
VkQueue queue; // Separate queue for compute commands (queue family may differ from the one used for graphics)
59-
VkCommandPool commandPool; // Use a separate command pool (queue family may differ from the one used for graphics)
60-
std::vector<VkCommandBuffer> commandBuffers;// Command buffer storing the dispatch commands and barriers
61-
std::vector<VkFence> fences; // Synchronization fence to avoid rewriting compute CB if still in use
62-
VkDescriptorSetLayout descriptorSetLayout; // Compute shader binding layout
63-
std::vector<VkDescriptorSet> descriptorSets;// Compute shader bindings
64-
VkPipelineLayout pipelineLayout; // Layout of the compute pipeline
65-
VkPipeline pipeline; // Compute pipeline for updating particle positions
66-
std::vector<vks::Buffer> uniformBuffers; // Uniform buffer object containing particle system parameters
67-
struct UniformData { // Compute shader uniform block object
68-
float deltaT; // Frame delta time
69-
float destX; // x position of the attractor
70-
float destY; // y position of the attractor
57+
uint32_t queueFamilyIndex; // Used to check if compute and graphics queue families differ and require additional barriers
58+
VkQueue queue; // Separate queue for compute commands (queue family may differ from the one used for graphics)
59+
VkCommandPool commandPool; // Use a separate command pool (queue family may differ from the one used for graphics)
60+
std::array<VkCommandBuffer, maxConcurrentFrames> commandBuffers; // Command buffer storing the dispatch commands and barriers
61+
std::array<VkFence, maxConcurrentFrames> fences; // Synchronization fence to avoid rewriting compute CB if still in use
62+
VkDescriptorSetLayout descriptorSetLayout; // Compute shader binding layout
63+
std::array<VkDescriptorSet, maxConcurrentFrames> descriptorSets{}; // Compute shader bindings
64+
VkPipelineLayout pipelineLayout; // Layout of the compute pipeline
65+
VkPipeline pipeline; // Compute pipeline for updating particle positions
66+
std::array<vks::Buffer, maxConcurrentFrames> uniformBuffers; // Uniform buffer object containing particle system parameters
67+
struct UniformData { // Compute shader uniform block object
68+
float deltaT; // Frame delta time
69+
float destX; // x position of the attractor
70+
float destY; // y position of the attractor
7171
int32_t particleCount = PARTICLE_COUNT;
7272
} uniformData{};
7373
} compute{};
@@ -76,11 +76,6 @@ class VulkanExample : public VulkanExampleBase
7676
{
7777
useNewSync = true;
7878
title = "Compute shader particle system";
79-
storageBuffers.resize(maxConcurrentFrames);
80-
compute.uniformBuffers.resize(maxConcurrentFrames);
81-
compute.descriptorSets.resize(maxConcurrentFrames);
82-
compute.commandBuffers.resize(maxConcurrentFrames);
83-
compute.fences.resize(maxConcurrentFrames);
8479
}
8580

8681
~VulkanExample()
@@ -296,14 +291,12 @@ class VulkanExample : public VulkanExampleBase
296291
// Some objects need to be duplicated per frames in flight
297292

298293
// Create command buffers for compute operations
299-
compute.commandBuffers.resize(maxConcurrentFrames);
300294
VkCommandBufferAllocateInfo cmdBufAllocateInfo = vks::initializers::commandBufferAllocateInfo(compute.commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1);
301295
for (auto& commandBuffer : compute.commandBuffers) {
302296
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &commandBuffer));
303297
}
304298

305299
// Fences for compute CB sync
306-
compute.fences.resize(maxConcurrentFrames);
307300
for (auto& fence : compute.fences) {
308301
VkFenceCreateInfo fenceCreateInfo = vks::initializers::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT);
309302
VK_CHECK_RESULT(vkCreateFence(device, &fenceCreateInfo, nullptr, &fence));

examples/computeraytracing/computeraytracing.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ class VulkanExample : public VulkanExampleBase
3030

3131
// Resources for the compute part of the example
3232
struct Compute {
33-
VkQueue queue{ VK_NULL_HANDLE }; // Separate queue for compute commands (queue family may differ from the one used for graphics)
34-
VkCommandPool commandPool{ VK_NULL_HANDLE }; // Use a separate command pool (queue family may differ from the one used for graphics)
35-
std::vector<VkCommandBuffer> commandBuffers; // Command buffers storing the dispatch commands and barriers
36-
std::vector<VkFence> fences; // Synchronization fence to avoid rewriting compute CB if still in use
37-
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE }; // Compute shader binding layout
38-
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE }; // Layout of the compute pipeline
39-
VkPipeline pipeline{ VK_NULL_HANDLE }; // Compute raytracing pipeline
40-
struct UniformDataCompute { // Compute shader uniform block object
33+
VkQueue queue{ VK_NULL_HANDLE }; // Separate queue for compute commands (queue family may differ from the one used for graphics)
34+
VkCommandPool commandPool{ VK_NULL_HANDLE }; // Use a separate command pool (queue family may differ from the one used for graphics)
35+
std::array<VkCommandBuffer, maxConcurrentFrames> commandBuffers; // Command buffers storing the dispatch commands and barriers
36+
std::array<VkFence, maxConcurrentFrames> fences; // Synchronization fence to avoid rewriting compute CB if still in use
37+
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE }; // Compute shader binding layout
38+
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE }; // Layout of the compute pipeline
39+
VkPipeline pipeline{ VK_NULL_HANDLE }; // Compute raytracing pipeline
40+
struct UniformDataCompute { // Compute shader uniform block object
4141
glm::vec3 lightPos;
4242
float aspectRatio{ 1.0f };
4343
glm::vec4 fogColor = glm::vec4(0.0f);
@@ -53,8 +53,8 @@ class VulkanExample : public VulkanExampleBase
5353
vks::Buffer objectStorageBuffer;
5454
// Uniform buffer object containing scene parameters
5555
// These need to be per frames in flight, as CPU writes to while GPU reads from
56-
std::vector<vks::Buffer> uniformBuffers;
57-
std::vector<VkDescriptorSet> descriptorSets;
56+
std::array<vks::Buffer, maxConcurrentFrames> uniformBuffers;
57+
std::array<VkDescriptorSet, maxConcurrentFrames> descriptorSets{};
5858
} compute;
5959

6060
// Definitions for scene objects
@@ -87,8 +87,6 @@ class VulkanExample : public VulkanExampleBase
8787
camera.setTranslation(glm::vec3(0.0f, 0.0f, -4.0f));
8888
camera.rotationSpeed = 0.0f;
8989
camera.movementSpeed = 2.5f;
90-
compute.uniformBuffers.resize(maxConcurrentFrames);
91-
compute.descriptorSets.resize(maxConcurrentFrames);
9290
}
9391

9492
~VulkanExample()
@@ -371,14 +369,12 @@ class VulkanExample : public VulkanExampleBase
371369
// Some objects need to be duplicated per frames in flight
372370

373371
// Create command buffers for compute operations
374-
compute.commandBuffers.resize(maxConcurrentFrames);
375372
VkCommandBufferAllocateInfo cmdBufAllocateInfo = vks::initializers::commandBufferAllocateInfo(compute.commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1);
376373
for (auto& commandBuffer : compute.commandBuffers) {
377374
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &commandBuffer));
378375
}
379376

380377
// Fences for compute CB sync
381-
compute.fences.resize(maxConcurrentFrames);
382378
for (auto& fence : compute.fences) {
383379
VkFenceCreateInfo fenceCreateInfo = vks::initializers::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT);
384380
VK_CHECK_RESULT(vkCreateFence(device, &fenceCreateInfo, nullptr, &fence));

examples/computeshader/computeshader.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ class VulkanExample : public VulkanExampleBase
3131
VkDescriptorSet preCompute{ VK_NULL_HANDLE }; // Image display shader bindings before compute shader image manipulation
3232
VkDescriptorSet postCompute{ VK_NULL_HANDLE }; // Image display shader bindings after compute shader image manipulation
3333
};
34-
std::vector<DescriptorSets> descriptorSets;
34+
std::array<DescriptorSets, maxConcurrentFrames> descriptorSets;
3535
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE }; // Layout of the graphics pipeline
3636
VkPipeline pipeline{ VK_NULL_HANDLE }; // Image display pipeline
3737
// Used to pass data to the graphics shaders
3838
struct UniformData {
3939
glm::mat4 projection;
4040
glm::mat4 modelView;
4141
} uniformData;
42-
std::vector<vks::Buffer> uniformBuffers;
42+
std::array<vks::Buffer, maxConcurrentFrames> uniformBuffers;
4343
} graphics;
4444

4545
// Resources for the compute part of the example
4646
struct Compute {
4747
VkQueue queue{ VK_NULL_HANDLE }; // Separate queue for compute commands (queue family may differ from the one used for graphics)
4848
VkCommandPool commandPool{ VK_NULL_HANDLE }; // Use a separate command pool (queue family may differ from the one used for graphics)
49-
std::vector<VkCommandBuffer> commandBuffers; // Command buffers storing the dispatch commands and barriers
50-
std::vector<VkFence> fences; // Synchronization fence to avoid rewriting compute CB if still in use
49+
std::array<VkCommandBuffer, maxConcurrentFrames> commandBuffers;// Command buffers storing the dispatch commands and barriers
50+
std::array<VkFence, maxConcurrentFrames> fences; // Synchronization fence to avoid rewriting compute CB if still in use
5151
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE }; // Compute shader binding layout
5252
VkDescriptorSet descriptorSet{ VK_NULL_HANDLE }; // Compute shader bindings
5353
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE }; // Layout of the compute pipeline
@@ -70,8 +70,6 @@ class VulkanExample : public VulkanExampleBase
7070
camera.setPosition(glm::vec3(0.0f, 0.0f, -2.0f));
7171
camera.setRotation(glm::vec3(0.0f));
7272
camera.setPerspective(60.0f, (float)width * 0.5f / (float)height, 1.0f, 256.0f);
73-
graphics.uniformBuffers.resize(maxConcurrentFrames);
74-
graphics.descriptorSets.resize(maxConcurrentFrames);
7573
}
7674

7775
~VulkanExample()
@@ -348,14 +346,12 @@ class VulkanExample : public VulkanExampleBase
348346
// Some objects need to be duplicated per frames in flight
349347

350348
// Create command buffers for compute operations
351-
compute.commandBuffers.resize(maxConcurrentFrames);
352349
VkCommandBufferAllocateInfo cmdBufAllocateInfo = vks::initializers::commandBufferAllocateInfo(compute.commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1);
353350
for (auto& commandBuffer : compute.commandBuffers) {
354351
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &commandBuffer));
355352
}
356353

357354
// Fences for compute CB sync
358-
compute.fences.resize(maxConcurrentFrames);
359355
for (auto& fence : compute.fences) {
360356
VkFenceCreateInfo fenceCreateInfo = vks::initializers::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT);
361357
VK_CHECK_RESULT(vkCreateFence(device, &fenceCreateInfo, nullptr, &fence));

examples/conditionalrender/conditionalrender.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ class VulkanExample : public VulkanExampleBase
2727
glm::mat4 view;
2828
glm::mat4 model;
2929
} uniformData;
30-
std::vector<vks::Buffer> uniformBuffers;
30+
std::array<vks::Buffer, maxConcurrentFrames> uniformBuffers;
3131

3232
std::vector<int32_t> conditionalVisibility{};
33-
std::vector<vks::Buffer> conditionalBuffers;
33+
std::array<vks::Buffer, maxConcurrentFrames> conditionalBuffers;
3434

3535
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
3636
VkPipeline pipeline{ VK_NULL_HANDLE };
3737
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
38-
std::vector<VkDescriptorSet> descriptorSets;
38+
std::array<VkDescriptorSet, maxConcurrentFrames> descriptorSets{};
3939

4040
VulkanExample() : VulkanExampleBase()
4141
{
@@ -46,8 +46,6 @@ class VulkanExample : public VulkanExampleBase
4646
camera.setRotation(glm::vec3(-2.25f, -52.0f, 0.0f));
4747
camera.setTranslation(glm::vec3(1.9f, -2.05f, -18.0f));
4848
camera.rotationSpeed *= 0.25f;
49-
uniformBuffers.resize(maxConcurrentFrames);
50-
descriptorSets.resize(maxConcurrentFrames);
5149

5250
/*
5351
[POI] Enable extension required for conditional rendering
@@ -246,7 +244,6 @@ class VulkanExample : public VulkanExampleBase
246244
This sample renders multiple rows of objects conditionally, so we setup a buffer with one value per row
247245
As buffers are written by the CPU and read by the GPU, we need one buffer per frame in flight
248246
*/
249-
conditionalBuffers.resize(maxConcurrentFrames);
250247
for (auto& buffer : conditionalBuffers) {
251248
VK_CHECK_RESULT(vulkanDevice->createBuffer(
252249
VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT,

examples/conservativeraster/conservativeraster.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class VulkanExample : public VulkanExampleBase
3535
glm::mat4 projection;
3636
glm::mat4 model;
3737
} uniformData;
38-
std::vector<vks::Buffer> uniformBuffers;
38+
std::array<vks::Buffer, maxConcurrentFrames> uniformBuffers;
3939

4040
struct PipelineLayouts {
4141
VkPipelineLayout scene{ VK_NULL_HANDLE };
@@ -58,7 +58,7 @@ class VulkanExample : public VulkanExampleBase
5858
VkDescriptorSet scene{ VK_NULL_HANDLE };
5959
VkDescriptorSet fullscreen{ VK_NULL_HANDLE };
6060
};
61-
std::vector<DescriptorSets> descriptorSets;
61+
std::array<DescriptorSets, maxConcurrentFrames> descriptorSets;
6262

6363
// Framebuffer for offscreen rendering
6464
struct FrameBufferAttachment {
@@ -83,8 +83,6 @@ class VulkanExample : public VulkanExampleBase
8383
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 512.0f);
8484
camera.setRotation(glm::vec3(0.0f));
8585
camera.setTranslation(glm::vec3(0.0f, 0.0f, -2.0f));
86-
uniformBuffers.resize(maxConcurrentFrames);
87-
descriptorSets.resize(maxConcurrentFrames);
8886

8987
// Enable extension required for conservative rasterization
9088
enabledDeviceExtensions.push_back(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME);

examples/debugprintf/debugprintf.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class VulkanExample : public VulkanExampleBase
2828
glm::mat4 model;
2929
glm::vec4 lightPos = glm::vec4(0.0f, 5.0f, 15.0f, 1.0f);
3030
} uniformData;
31-
std::vector<vks::Buffer> uniformBuffers;
31+
std::array<vks::Buffer, maxConcurrentFrames> uniformBuffers;
3232

3333
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
3434
VkPipeline pipeline{ VK_NULL_HANDLE };
3535
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
36-
std::vector<VkDescriptorSet> descriptorSets;
36+
std::array<VkDescriptorSet, maxConcurrentFrames> descriptorSets{};
3737

3838
VulkanExample() : VulkanExampleBase()
3939
{
@@ -43,8 +43,6 @@ class VulkanExample : public VulkanExampleBase
4343
camera.setRotationSpeed(0.5f);
4444
camera.setPosition(glm::vec3(0.1f, 1.1f, -8.5f));
4545
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
46-
uniformBuffers.resize(maxConcurrentFrames);
47-
descriptorSets.resize(maxConcurrentFrames);
4846

4947
// Using printf requires the non semantic info extension to be enabled
5048
enabledDeviceExtensions.push_back(VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME);

examples/debugutils/debugutils.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class VulkanExample : public VulkanExampleBase
2424
glm::mat4 model;
2525
glm::vec4 lightPos = glm::vec4(0.0f, 5.0f, 15.0f, 1.0f);
2626
} uniformData;
27-
std::vector<vks::Buffer> uniformBuffers;
27+
std::array<vks::Buffer, maxConcurrentFrames> uniformBuffers;
2828

2929
struct Pipelines {
3030
VkPipeline toonshading;
@@ -35,7 +35,7 @@ class VulkanExample : public VulkanExampleBase
3535

3636
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
3737
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
38-
std::vector<VkDescriptorSet> descriptorSets;
38+
std::array<VkDescriptorSet, maxConcurrentFrames> descriptorSets{};
3939

4040
// Framebuffer for offscreen rendering
4141
struct FrameBufferAttachment {
@@ -74,8 +74,6 @@ class VulkanExample : public VulkanExampleBase
7474
camera.setRotationSpeed(0.5f);
7575
camera.setPosition(glm::vec3(0.1f, 1.1f, -8.5f));
7676
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
77-
uniformBuffers.resize(maxConcurrentFrames);
78-
descriptorSets.resize(maxConcurrentFrames);
7977
}
8078

8179
// Enable physical device features required for this example

examples/deferred/deferred.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class VulkanExample : public VulkanExampleBase
5959
vks::Buffer offscreen;
6060
vks::Buffer composition;
6161
};
62-
std::vector<UniformBuffers> uniformBuffers;
62+
std::array<UniformBuffers, maxConcurrentFrames> uniformBuffers;
6363

6464
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
6565
struct {
@@ -73,7 +73,7 @@ class VulkanExample : public VulkanExampleBase
7373
VkDescriptorSet floor{ VK_NULL_HANDLE };
7474
VkDescriptorSet composition{ VK_NULL_HANDLE };
7575
};
76-
std::vector<DescriptorSets> descriptorSets;
76+
std::array<DescriptorSets, maxConcurrentFrames> descriptorSets;
7777

7878
// Framebuffers holding the deferred attachments
7979
struct FrameBufferAttachment {
@@ -106,8 +106,6 @@ class VulkanExample : public VulkanExampleBase
106106
camera.position = { 2.15f, 0.3f, -8.75f };
107107
camera.setRotation(glm::vec3(-0.75f, 12.5f, 0.0f));
108108
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
109-
uniformBuffers.resize(maxConcurrentFrames);
110-
descriptorSets.resize(maxConcurrentFrames);
111109
}
112110

113111
~VulkanExample()

0 commit comments

Comments
 (0)