diff --git a/shader/shader.frag.glsl b/shader/shader.frag.glsl
index 4d63c2b..187b463 100644
--- a/shader/shader.frag.glsl
+++ b/shader/shader.frag.glsl
@@ -19,9 +19,9 @@
#version 450
-layout(location = 0) out vec4 outColor;
-layout(location = 0) in vec3 fragColor;
+layout(location = 0) out vec4 out_color;
+layout(location = 0) in vec3 frag_color;
void main() {
- outColor = vec4(fragColor, 1.0);
+ out_color = vec4(frag_color, 1.0);
}
diff --git a/shader/shader.vertex.glsl b/shader/shader.vertex.glsl
index 650f27b..e2f1f79 100644
--- a/shader/shader.vertex.glsl
+++ b/shader/shader.vertex.glsl
@@ -19,21 +19,12 @@
#version 450
-layout(location = 0) out vec3 fragColor;
+layout(location = 0) in vec2 in_position;
+layout(location = 2) in vec3 in_color;
-vec2 positions[3] = vec2[](
- vec2(0.0, -0.5),
- vec2(0.5, 0.5),
- vec2(-0.5, 0.5)
-);
-
-vec3 colors[3] = vec3[](
- vec3(1.0, 0.0, 0.0),
- vec3(0.0, 1.0, 0.0),
- vec3(0.0, 0.0, 1.0)
-);
+layout(location = 0) out vec3 frag_color;
void main() {
- gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
- fragColor = colors[gl_VertexIndex];
+ gl_Position = vec4(in_position, 0.5, 1.0);
+ frag_color = in_color;
}
diff --git a/src/main.cpp b/src/main.cpp
index 5e5ee3a..a1ca584 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -17,11 +17,14 @@
* along with this program. If not, see .
*/
+#include
+#include
#include
#include
#include
#include
#include
+#include
#define VULKAN_HPP_NO_CONSTRUCTORS
#include
@@ -31,8 +34,7 @@
#include
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
-#include
-#include
+#include
#include
@@ -107,6 +109,43 @@ class VulkanExampleApplication {
}
} vk_physical_card_info;
+ std::unique_ptr vk_buffer_vertex_memory; // in order to get a clean desctruction sequence, instantiate the DeviceMemory for the vertex buffer first
+ std::unique_ptr vk_buffer_vertex; // https://github.com/KhronosGroup/Vulkan-Hpp/blob/6f72ceca515d59f40d64b64cf2734f6261e1f9f2/RAII_Samples/13_InitVertexBuffer/13_InitVertexBuffer.cpp
+
+ struct Vertex {
+ glm::vec2 pos;
+ glm::vec3 color;
+
+ static constexpr vk::VertexInputBindingDescription GetBindingDescription() {
+ constexpr vk::VertexInputBindingDescription binding_description {
+ .binding = 0,
+ .stride = sizeof(Vertex),
+ .inputRate = vk::VertexInputRate::eVertex
+ };
+ return binding_description;
+ }
+ static constexpr std::array GetAttributeDescriptions() {
+ constexpr std::array attribute_descriptions {
+ vk::VertexInputAttributeDescription { // https://docs.vulkan.org/tutorial/latest/04_Vertex_buffers/00_Vertex_input_description.html
+ .location = 0,
+ .binding = 0,
+ .format = vk::Format::eR32G32Sfloat,
+ .offset = offsetof(Vertex, pos) },
+ vk::VertexInputAttributeDescription {
+ .location = 2,
+ .binding = 0,
+ .format = vk::Format::eR32G32B32Sfloat,
+ .offset = offsetof(Vertex, color) }
+ };
+ return attribute_descriptions;
+ }
+ };
+ static inline const std::vector triangle_vertices = {
+ { { 0.0f, -0.5f }, { 1.0f, 1.0f, 1.0f } },
+ { { 0.5f, 0.5f }, { 0.0f, 1.0f, 0.0f } },
+ { { -0.5f, 0.5f }, { 0.0f, 0.0f, 1.0f } }
+ };
+
public:
VulkanExampleApplication() {
try { // try me // all the code is in init :O boo hoo, ill shove all ur ram out of scope ASAP i can, dont seperate them, just scope them instead dumb dumb.
@@ -419,11 +458,15 @@ public:
.dynamicStateCount = static_cast(dynamic_states.size()),
.pDynamicStates = dynamic_states.data()
};
- constexpr vk::PipelineVertexInputStateCreateInfo vertex_input_create_info {
- .vertexBindingDescriptionCount = 0,
- .pVertexBindingDescriptions = nullptr, // Optional
- .vertexAttributeDescriptionCount = 0,
- .pVertexAttributeDescriptions = nullptr // Optional
+
+ // vertex info
+ constexpr auto vertex_binding_description = Vertex::GetBindingDescription();
+ constexpr auto vertex_attribute_descriptions = Vertex::GetAttributeDescriptions();
+ const vk::PipelineVertexInputStateCreateInfo vertex_input_create_info {
+ .vertexBindingDescriptionCount = 1,
+ .pVertexBindingDescriptions = &vertex_binding_description, // Optional
+ .vertexAttributeDescriptionCount = vertex_attribute_descriptions.size(),
+ .pVertexAttributeDescriptions = vertex_attribute_descriptions.data() // Optional
};
constexpr vk::PipelineInputAssemblyStateCreateInfo input_assembly_create_info {
.topology = vk::PrimitiveTopology::eTriangleList,
@@ -596,6 +639,42 @@ public:
};
this->vk_command_buffers = std::make_unique(*this->vk_gpu, command_buffer_alloc_info);
}
+ {
+ const vk::BufferCreateInfo vertex_buffer_create_info {
+ .size = sizeof(triangle_vertices[0]) * triangle_vertices.size(),
+ .usage = vk::BufferUsageFlagBits::eVertexBuffer,
+ .sharingMode = vk::SharingMode::eExclusive
+ };
+ this->vk_buffer_vertex = std::make_unique(*this->vk_gpu, vertex_buffer_create_info);
+
+ const auto memory_requirements = this->vk_buffer_vertex->getMemoryRequirements();
+ const auto memory_properties = this->vk_gfx_card->getMemoryProperties();
+ const auto FindMemoryType = [&memory_properties](const std::uint32_t& type_filter, const vk::MemoryPropertyFlags& wanted_properties) -> std::uint32_t {
+ const std::uint32_t memory_type_count = memory_properties.memoryTypeCount;
+
+ std::uint32_t memory_type_to_test;
+ for (memory_type_to_test = 0; memory_type_to_test < memory_type_count; memory_type_to_test++) {
+ if ((*reinterpret_cast*>(&type_filter))[memory_type_to_test]) {
+ if ((memory_properties.memoryTypes[memory_type_to_test].propertyFlags & wanted_properties) == wanted_properties) {
+ break;
+ }
+ }
+ }
+ if (memory_type_to_test == memory_type_count)
+ throw std::runtime_error("failed to find suitable memory type!");
+ return memory_type_to_test;
+ };
+ const vk::MemoryAllocateInfo vertex_allocate_info {
+ .allocationSize = memory_requirements.size,
+ .memoryTypeIndex = FindMemoryType(memory_requirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent)
+ };
+ this->vk_buffer_vertex_memory = std::make_unique(*this->vk_gpu, vertex_allocate_info);
+ this->vk_buffer_vertex->bindMemory(**this->vk_buffer_vertex_memory, 0);
+
+ std::uint8_t* const vertex_buffer_data = static_cast(this->vk_buffer_vertex_memory->mapMemory(0, memory_requirements.size));
+ memcpy(vertex_buffer_data, triangle_vertices.data(), (std::size_t)memory_requirements.size);
+ this->vk_buffer_vertex_memory->unmapMemory();
+ }
{ // syncronizing vars
this->vk_semephore_image_available = std::make_unique(*this->vk_gpu, vk::SemaphoreCreateInfo {});
this->vk_semephore_render_finished = std::make_unique(*this->vk_gpu, vk::SemaphoreCreateInfo {});
@@ -633,7 +712,7 @@ public:
command_buffer.setViewport(0, viewport);
command_buffer.setScissor(0, scissor);
}
- void RecordCommandBuffers(const vk::raii::CommandBuffer& command_buffer, std::uint32_t image_index) const {
+ void RecordCommandBuffer(const vk::raii::CommandBuffer& command_buffer, std::uint32_t image_index) const {
constexpr vk::CommandBufferBeginInfo command_buffer_begin_info {
//.flags = vk::CommandBufferUsageFlagBits::, // Optional
//.pInheritanceInfo = nullptr // Optional
@@ -653,6 +732,7 @@ public:
command_buffer.beginRenderPass(render_pass_info, vk::SubpassContents::eInline);
command_buffer.bindPipeline(vk::PipelineBindPoint::eGraphics, **this->vk_pipeline);
command_buffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, *this->vk_pipeline_layout, 0, { this->vk_descriptor_sets->front() }, nullptr);
+ command_buffer.bindVertexBuffers(0, { **this->vk_buffer_vertex }, { 0 });
this->RecordDynamic(command_buffer);
command_buffer.draw(3, 1, 0, 0);
@@ -677,7 +757,7 @@ public:
}
this->vk_command_buffers->front().reset();
- this->RecordCommandBuffers(this->vk_command_buffers->front(), next_image.second);
+ this->RecordCommandBuffer(this->vk_command_buffers->front(), next_image.second);
const std::array wait_semaphores = { *this->vk_semephore_image_available };
constexpr auto wait_stages = vk::PipelineStageFlags(vk::PipelineStageFlagBits::eColorAttachmentOutput);