Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions attachments/19_vertex_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ struct Vertex

static vk::VertexInputBindingDescription getBindingDescription()
{
return {0, sizeof(Vertex), vk::VertexInputRate::eVertex};
return {.binding = 0, .stride = sizeof(Vertex), .inputRate = vk::VertexInputRate::eVertex};
}

static std::array<vk::VertexInputAttributeDescription, 2> getAttributeDescriptions()
{
return {
vk::VertexInputAttributeDescription(0, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, pos)),
vk::VertexInputAttributeDescription(1, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, color))};
return {{{.location = 0, .binding = 0, .format = vk::Format::eR32G32Sfloat, .offset = offsetof(Vertex, pos)},
{.location = 1, .binding = 0, .format = vk::Format::eR32G32B32Sfloat, .offset = offsetof(Vertex, color)}}};
}
};

Expand Down Expand Up @@ -278,6 +277,7 @@ class HelloTriangleApplication
vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>();
bool supportsRequiredFeatures = features.template get<vk::PhysicalDeviceVulkan11Features>().shaderDrawParameters &&
features.template get<vk::PhysicalDeviceVulkan13Features>().dynamicRendering &&
features.template get<vk::PhysicalDeviceVulkan13Features>().synchronization2 &&
features.template get<vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>().extendedDynamicState;

// Return true if the physicalDevice meets all the criteria
Expand Down Expand Up @@ -316,12 +316,16 @@ class HelloTriangleApplication
}

// query for required features (Vulkan 1.1 and 1.3)
vk::StructureChain<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceVulkan11Features, vk::PhysicalDeviceVulkan13Features, vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT> featureChain = {
{}, // vk::PhysicalDeviceFeatures2
{.shaderDrawParameters = true}, // vk::PhysicalDeviceVulkan11Features
{.synchronization2 = true, .dynamicRendering = true}, // vk::PhysicalDeviceVulkan13Features
{.extendedDynamicState = true} // vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT
};
vk::StructureChain<vk::PhysicalDeviceFeatures2,
vk::PhysicalDeviceVulkan11Features,
vk::PhysicalDeviceVulkan13Features,
vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>
featureChain = {
{}, // vk::PhysicalDeviceFeatures2
{.shaderDrawParameters = true}, // vk::PhysicalDeviceVulkan11Features
{.synchronization2 = true, .dynamicRendering = true}, // vk::PhysicalDeviceVulkan13Features
{.extendedDynamicState = true} // vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT
};

// create a Device
float queuePriority = 0.5f;
Expand Down Expand Up @@ -389,7 +393,10 @@ class HelloTriangleApplication

auto bindingDescription = Vertex::getBindingDescription();
auto attributeDescriptions = Vertex::getAttributeDescriptions();
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{.vertexBindingDescriptionCount = 1, .pVertexBindingDescriptions = &bindingDescription, .vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size()), .pVertexAttributeDescriptions = attributeDescriptions.data()};
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{.vertexBindingDescriptionCount = 1,
.pVertexBindingDescriptions = &bindingDescription,
.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size()),
.pVertexAttributeDescriptions = attributeDescriptions.data()};
vk::PipelineInputAssemblyStateCreateInfo inputAssembly{.topology = vk::PrimitiveTopology::eTriangleList};
vk::PipelineViewportStateCreateInfo viewportState{.viewportCount = 1, .scissorCount = 1};

Expand Down Expand Up @@ -442,11 +449,15 @@ class HelloTriangleApplication

void createVertexBuffer()
{
vk::BufferCreateInfo bufferInfo{.size = sizeof(vertices[0]) * vertices.size(), .usage = vk::BufferUsageFlagBits::eVertexBuffer, .sharingMode = vk::SharingMode::eExclusive};
vk::BufferCreateInfo bufferInfo{.size = sizeof(vertices[0]) * vertices.size(),
.usage = vk::BufferUsageFlagBits::eVertexBuffer,
.sharingMode = vk::SharingMode::eExclusive};
vertexBuffer = vk::raii::Buffer(device, bufferInfo);

vk::MemoryRequirements memRequirements = vertexBuffer.getMemoryRequirements();
vk::MemoryAllocateInfo memoryAllocateInfo{.allocationSize = memRequirements.size, .memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent)};
vk::MemoryAllocateInfo memoryAllocateInfo{
.allocationSize = memRequirements.size,
.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent)};
vertexBufferMemory = vk::raii::DeviceMemory(device, memoryAllocateInfo);

vertexBuffer.bindMemory(*vertexBufferMemory, 0);
Expand Down Expand Up @@ -482,7 +493,8 @@ class HelloTriangleApplication
{
auto &commandBuffer = commandBuffers[frameIndex];
commandBuffer.begin({});
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL

// Before starting rendering, transition the swapchain image to vk::ImageLayout::eColorAttachmentOptimal
transition_image_layout(
imageIndex,
vk::ImageLayout::eUndefined,
Expand All @@ -509,9 +521,10 @@ class HelloTriangleApplication
commandBuffer.setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast<float>(swapChainExtent.width), static_cast<float>(swapChainExtent.height), 0.0f, 1.0f));
commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), swapChainExtent));
commandBuffer.bindVertexBuffers(0, *vertexBuffer, {0});
commandBuffer.draw(3, 1, 0, 0);
commandBuffer.draw(static_cast<uint32_t>(vertices.size()), 1, 0, 0);
commandBuffer.endRendering();
// After rendering, transition the swapchain image to PRESENT_SRC

// After rendering, transition the swapchain image to vk::ImageLayout::ePresentSrcKHR
transition_image_layout(
imageIndex,
vk::ImageLayout::eColorAttachmentOptimal,
Expand Down
29 changes: 15 additions & 14 deletions en/04_Vertex_buffers/00_Vertex_input_description.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ struct Vertex {
glm::vec2 pos;
glm::vec3 color;

static vk::VertexInputBindingDescription getBindingDescription() {
return { 0, sizeof(Vertex), vk::VertexInputRate::eVertex };
static vk::VertexInputBindingDescription getBindingDescription()
{
return {.binding = 0, .stride = sizeof(Vertex), .inputRate = vk::VertexInputRate::eVertex};
}
};
----
Expand Down Expand Up @@ -122,21 +123,21 @@ We're going to add another helper function to `Vertex` to fill in these structs.

...

static std::array<vk::VertexInputAttributeDescription, 2> getAttributeDescriptions() {
return {
vk::VertexInputAttributeDescription( 0, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, pos) ),
vk::VertexInputAttributeDescription( 1, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, color) )
};
static std::array<vk::VertexInputAttributeDescription, 2> getAttributeDescriptions()
{
return {{{.location = 0, .binding = 0, .format = vk::Format::eR32G32Sfloat, .offset = offsetof(Vertex, pos)},
{.location = 1, .binding = 0, .format = vk::Format::eR32G32B32Sfloat, .offset = offsetof(Vertex, color)}}};
}
}
----

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

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

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

[,c++]
----
auto bindingDescription = Vertex::getBindingDescription();
auto attributeDescriptions = Vertex::getAttributeDescriptions();
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{ .vertexBindingDescriptionCount = 1,
.pVertexBindingDescriptions = &bindingDescription,
.vertexAttributeDescriptionCount = static_cast<uint32_t>( attributeDescriptions.size() ),
.pVertexAttributeDescriptions = attributeDescriptions.data() };
auto bindingDescription = Vertex::getBindingDescription();
auto attributeDescriptions = Vertex::getAttributeDescriptions();
vk::PipelineVertexInputStateCreateInfo vertexInputInfo{.vertexBindingDescriptionCount = 1,
.pVertexBindingDescriptions = &bindingDescription,
.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size()),
.pVertexAttributeDescriptions = attributeDescriptions.data()};
----

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