Skip to content

Commit e0511f4

Browse files
authored
Align docu in 00_Vertex_input_description.adoc and 01_Vertex_buffer_creation.adoc with sources in 19_vertex_buffer.cpp (#348)
1 parent c9f3ee2 commit e0511f4

3 files changed

Lines changed: 92 additions & 66 deletions

File tree

attachments/19_vertex_buffer.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ struct Vertex
4040

4141
static vk::VertexInputBindingDescription getBindingDescription()
4242
{
43-
return {0, sizeof(Vertex), vk::VertexInputRate::eVertex};
43+
return {.binding = 0, .stride = sizeof(Vertex), .inputRate = vk::VertexInputRate::eVertex};
4444
}
4545

4646
static std::array<vk::VertexInputAttributeDescription, 2> getAttributeDescriptions()
4747
{
48-
return {
49-
vk::VertexInputAttributeDescription(0, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, pos)),
50-
vk::VertexInputAttributeDescription(1, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, color))};
48+
return {{{.location = 0, .binding = 0, .format = vk::Format::eR32G32Sfloat, .offset = offsetof(Vertex, pos)},
49+
{.location = 1, .binding = 0, .format = vk::Format::eR32G32B32Sfloat, .offset = offsetof(Vertex, color)}}};
5150
}
5251
};
5352

@@ -278,6 +277,7 @@ class HelloTriangleApplication
278277
vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>();
279278
bool supportsRequiredFeatures = features.template get<vk::PhysicalDeviceVulkan11Features>().shaderDrawParameters &&
280279
features.template get<vk::PhysicalDeviceVulkan13Features>().dynamicRendering &&
280+
features.template get<vk::PhysicalDeviceVulkan13Features>().synchronization2 &&
281281
features.template get<vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>().extendedDynamicState;
282282

283283
// Return true if the physicalDevice meets all the criteria
@@ -316,12 +316,16 @@ class HelloTriangleApplication
316316
}
317317

318318
// query for required features (Vulkan 1.1 and 1.3)
319-
vk::StructureChain<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceVulkan11Features, vk::PhysicalDeviceVulkan13Features, vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT> featureChain = {
320-
{}, // vk::PhysicalDeviceFeatures2
321-
{.shaderDrawParameters = true}, // vk::PhysicalDeviceVulkan11Features
322-
{.synchronization2 = true, .dynamicRendering = true}, // vk::PhysicalDeviceVulkan13Features
323-
{.extendedDynamicState = true} // vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT
324-
};
319+
vk::StructureChain<vk::PhysicalDeviceFeatures2,
320+
vk::PhysicalDeviceVulkan11Features,
321+
vk::PhysicalDeviceVulkan13Features,
322+
vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>
323+
featureChain = {
324+
{}, // vk::PhysicalDeviceFeatures2
325+
{.shaderDrawParameters = true}, // vk::PhysicalDeviceVulkan11Features
326+
{.synchronization2 = true, .dynamicRendering = true}, // vk::PhysicalDeviceVulkan13Features
327+
{.extendedDynamicState = true} // vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT
328+
};
325329

326330
// create a Device
327331
float queuePriority = 0.5f;
@@ -389,7 +393,10 @@ class HelloTriangleApplication
389393

390394
auto bindingDescription = Vertex::getBindingDescription();
391395
auto attributeDescriptions = Vertex::getAttributeDescriptions();
392-
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{.vertexBindingDescriptionCount = 1, .pVertexBindingDescriptions = &bindingDescription, .vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size()), .pVertexAttributeDescriptions = attributeDescriptions.data()};
396+
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{.vertexBindingDescriptionCount = 1,
397+
.pVertexBindingDescriptions = &bindingDescription,
398+
.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size()),
399+
.pVertexAttributeDescriptions = attributeDescriptions.data()};
393400
vk::PipelineInputAssemblyStateCreateInfo inputAssembly{.topology = vk::PrimitiveTopology::eTriangleList};
394401
vk::PipelineViewportStateCreateInfo viewportState{.viewportCount = 1, .scissorCount = 1};
395402

@@ -442,11 +449,15 @@ class HelloTriangleApplication
442449

443450
void createVertexBuffer()
444451
{
445-
vk::BufferCreateInfo bufferInfo{.size = sizeof(vertices[0]) * vertices.size(), .usage = vk::BufferUsageFlagBits::eVertexBuffer, .sharingMode = vk::SharingMode::eExclusive};
452+
vk::BufferCreateInfo bufferInfo{.size = sizeof(vertices[0]) * vertices.size(),
453+
.usage = vk::BufferUsageFlagBits::eVertexBuffer,
454+
.sharingMode = vk::SharingMode::eExclusive};
446455
vertexBuffer = vk::raii::Buffer(device, bufferInfo);
447456

448457
vk::MemoryRequirements memRequirements = vertexBuffer.getMemoryRequirements();
449-
vk::MemoryAllocateInfo memoryAllocateInfo{.allocationSize = memRequirements.size, .memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent)};
458+
vk::MemoryAllocateInfo memoryAllocateInfo{
459+
.allocationSize = memRequirements.size,
460+
.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent)};
450461
vertexBufferMemory = vk::raii::DeviceMemory(device, memoryAllocateInfo);
451462

452463
vertexBuffer.bindMemory(*vertexBufferMemory, 0);
@@ -482,7 +493,8 @@ class HelloTriangleApplication
482493
{
483494
auto &commandBuffer = commandBuffers[frameIndex];
484495
commandBuffer.begin({});
485-
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
496+
497+
// Before starting rendering, transition the swapchain image to vk::ImageLayout::eColorAttachmentOptimal
486498
transition_image_layout(
487499
imageIndex,
488500
vk::ImageLayout::eUndefined,
@@ -509,9 +521,10 @@ class HelloTriangleApplication
509521
commandBuffer.setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast<float>(swapChainExtent.width), static_cast<float>(swapChainExtent.height), 0.0f, 1.0f));
510522
commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
511523
commandBuffer.bindVertexBuffers(0, *vertexBuffer, {0});
512-
commandBuffer.draw(3, 1, 0, 0);
524+
commandBuffer.draw(static_cast<uint32_t>(vertices.size()), 1, 0, 0);
513525
commandBuffer.endRendering();
514-
// After rendering, transition the swapchain image to PRESENT_SRC
526+
527+
// After rendering, transition the swapchain image to vk::ImageLayout::ePresentSrcKHR
515528
transition_image_layout(
516529
imageIndex,
517530
vk::ImageLayout::eColorAttachmentOptimal,

en/04_Vertex_buffers/00_Vertex_input_description.adoc

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ struct Vertex {
9393
glm::vec2 pos;
9494
glm::vec3 color;
9595
96-
static vk::VertexInputBindingDescription getBindingDescription() {
97-
return { 0, sizeof(Vertex), vk::VertexInputRate::eVertex };
96+
static vk::VertexInputBindingDescription getBindingDescription()
97+
{
98+
return {.binding = 0, .stride = sizeof(Vertex), .inputRate = vk::VertexInputRate::eVertex};
9899
}
99100
};
100101
----
@@ -122,21 +123,21 @@ We're going to add another helper function to `Vertex` to fill in these structs.
122123
123124
...
124125
125-
static std::array<vk::VertexInputAttributeDescription, 2> getAttributeDescriptions() {
126-
return {
127-
vk::VertexInputAttributeDescription( 0, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, pos) ),
128-
vk::VertexInputAttributeDescription( 1, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, color) )
129-
};
126+
static std::array<vk::VertexInputAttributeDescription, 2> getAttributeDescriptions()
127+
{
128+
return {{{.location = 0, .binding = 0, .format = vk::Format::eR32G32Sfloat, .offset = offsetof(Vertex, pos)},
129+
{.location = 1, .binding = 0, .format = vk::Format::eR32G32B32Sfloat, .offset = offsetof(Vertex, color)}}};
130+
}
130131
}
131132
----
132133

133134
As the function prototype indicates, there are going to be two of these structures.
134135
An attribute description struct describes how to extract a vertex attribute from a chunk of vertex data originating from a binding description.
135136
We have two attributes, position and color, so we need two attribute description structs.
136137

137-
The `binding` parameter tells Vulkan from which binding the per-vertex data comes.
138138
The `location` parameter references the `location` directive of the input in the vertex shader.
139139
The input in the vertex shader with location `0` is the position, which has two 32-bit float components.
140+
The `binding` parameter tells Vulkan from which binding the per-vertex data comes.
140141

141142
The `format` parameter describes the type of data for the attribute.
142143
A bit confusingly, the formats are specified using the same enumeration as color formats.
@@ -170,12 +171,12 @@ Find the `vertexInputInfo` struct and modify it to reference the two description
170171

171172
[,c++]
172173
----
173-
auto bindingDescription = Vertex::getBindingDescription();
174-
auto attributeDescriptions = Vertex::getAttributeDescriptions();
175-
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{ .vertexBindingDescriptionCount = 1,
176-
.pVertexBindingDescriptions = &bindingDescription,
177-
.vertexAttributeDescriptionCount = static_cast<uint32_t>( attributeDescriptions.size() ),
178-
.pVertexAttributeDescriptions = attributeDescriptions.data() };
174+
auto bindingDescription = Vertex::getBindingDescription();
175+
auto attributeDescriptions = Vertex::getAttributeDescriptions();
176+
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{.vertexBindingDescriptionCount = 1,
177+
.pVertexBindingDescriptions = &bindingDescription,
178+
.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size()),
179+
.pVertexAttributeDescriptions = attributeDescriptions.data()};
179180
----
180181

181182
The pipeline is now ready to accept vertex data in the format of the `vertices` container and pass it on to our vertex shader.

0 commit comments

Comments
 (0)