Skip to content

Commit cb74b70

Browse files
authored
Use (2, 0) for instance data (#18)
1 parent 195f046 commit cb74b70

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

assets/shader.vert

0 Bytes
Binary file not shown.

guide/src/descriptor_sets/instanced_rendering.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ vk::DescriptorPoolSize{vk::DescriptorType::eCombinedImageSampler, 2},
4141
vk::DescriptorPoolSize{vk::DescriptorType::eStorageBuffer, 2},
4242
```
4343
44-
This time add a new binding to set 1 (instead of a new set):
44+
Add set 2 and its new binding. Such a set layout keeps each "layer" isolated:
45+
46+
* Set 0: view / camera
47+
* Set 1: textures / material
48+
* Set 2: draw instances
4549
4650
```cpp
47-
static constexpr auto set_1_bindings_v = std::array{
48-
layout_binding(0, vk::DescriptorType::eCombinedImageSampler),
51+
static constexpr auto set_2_bindings_v = std::array{
4952
layout_binding(1, vk::DescriptorType::eStorageBuffer),
5053
};
54+
auto set_layout_cis = std::array<vk::DescriptorSetLayoutCreateInfo, 3>{};
55+
// ...
56+
set_layout_cis[2].setBindings(set_2_bindings_v);
5157
```
5258

5359
Create the instance SSBO after the view UBO:
@@ -98,13 +104,14 @@ Add another descriptor write for the SSBO:
98104
```cpp
99105
auto writes = std::array<vk::WriteDescriptorSet, 3>{};
100106
// ...
107+
auto const set2 = descriptor_sets[2];
101108
auto const instance_ssbo_info =
102109
m_instance_ssbo->descriptor_info_at(m_frame_index);
103110
write.setBufferInfo(instance_ssbo_info)
104111
.setDescriptorType(vk::DescriptorType::eStorageBuffer)
105112
.setDescriptorCount(1)
106-
.setDstSet(set1)
107-
.setDstBinding(1);
113+
.setDstSet(set2)
114+
.setDstBinding(0);
108115
writes[2] = write;
109116
```
110117

src/app.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,14 @@ void App::create_pipeline_layout() {
272272
};
273273
static constexpr auto set_1_bindings_v = std::array{
274274
layout_binding(0, vk::DescriptorType::eCombinedImageSampler),
275-
layout_binding(1, vk::DescriptorType::eStorageBuffer),
276275
};
277-
auto set_layout_cis = std::array<vk::DescriptorSetLayoutCreateInfo, 2>{};
276+
static constexpr auto set_2_bindings_v = std::array{
277+
layout_binding(0, vk::DescriptorType::eStorageBuffer),
278+
};
279+
auto set_layout_cis = std::array<vk::DescriptorSetLayoutCreateInfo, 3>{};
278280
set_layout_cis[0].setBindings(set_0_bindings_v);
279281
set_layout_cis[1].setBindings(set_1_bindings_v);
282+
set_layout_cis[2].setBindings(set_2_bindings_v);
280283

281284
for (auto const& set_layout_ci : set_layout_cis) {
282285
m_set_layouts.push_back(
@@ -656,13 +659,15 @@ void App::bind_descriptor_sets(vk::CommandBuffer const command_buffer) const {
656659
.setDstSet(set1)
657660
.setDstBinding(0);
658661
writes[1] = write;
662+
663+
auto const set2 = descriptor_sets[2];
659664
auto const instance_ssbo_info =
660665
m_instance_ssbo->descriptor_info_at(m_frame_index);
661666
write.setBufferInfo(instance_ssbo_info)
662667
.setDescriptorType(vk::DescriptorType::eStorageBuffer)
663668
.setDescriptorCount(1)
664-
.setDstSet(set1)
665-
.setDstBinding(1);
669+
.setDstSet(set2)
670+
.setDstBinding(0);
666671
writes[2] = write;
667672

668673
m_device->updateDescriptorSets(writes, {});

src/glsl/shader.vert

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ layout (set = 0, binding = 0) uniform View {
88
mat4 mat_vp;
99
};
1010

11-
layout (set = 1, binding = 1) readonly buffer Instances {
11+
layout (set = 2, binding = 0) readonly buffer Instances {
1212
mat4 mat_ms[];
1313
};
1414

0 commit comments

Comments
 (0)