Skip to content

Commit 8a1c48e

Browse files
committed
Vertex Buffer
1 parent 5d0c080 commit 8a1c48e

11 files changed

Lines changed: 126 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD 11)
77

88
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")
99

10-
set(SOURCE_FILES src/main.cpp src/simple_logger.cpp src/vulkan_base/vulkan_device.cpp src/vulkan_base/vulkan_swapchain.cpp src/vulkan_base/vulkan_renderpass.cpp src/vulkan_base/vulkan_pipeline.cpp)
10+
set(SOURCE_FILES src/main.cpp src/simple_logger.cpp src/vulkan_base/vulkan_device.cpp src/vulkan_base/vulkan_swapchain.cpp src/vulkan_base/vulkan_renderpass.cpp src/vulkan_base/vulkan_pipeline.cpp src/vulkan_base/vulkan_utils.cpp)
1111

1212
# Find SDL2
1313
add_subdirectory(libs/SDL)

shaders/color_frag.glsl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#version 450 core
2+
3+
layout(location = 0) in vec3 in_color;
4+
5+
layout(location = 0) out vec4 out_color;
6+
7+
void main() {
8+
out_color = vec4(in_color, 1.0);
9+
}

shaders/color_frag.spv

572 Bytes
Binary file not shown.

shaders/color_vert.glsl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 450 core
2+
3+
layout(location = 0) in vec2 in_position;
4+
layout(location = 1) in vec3 in_color;
5+
6+
layout(location = 0) out vec3 out_color;
7+
8+
void main() {
9+
gl_Position = vec4(in_position, 0.0, 1.0);
10+
out_color = in_color;
11+
}

shaders/color_vert.spv

1.06 KB
Binary file not shown.

shaders/compile.bat

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
glslc.exe -fshader-stage=vert triangle_vert.glsl -o triangle_vert.spv
2-
glslc.exe -fshader-stage=frag triangle_frag.glsl -o triangle_frag.spv
2+
glslc.exe -fshader-stage=frag triangle_frag.glsl -o triangle_frag.spv
3+
glslc.exe -fshader-stage=vert color_vert.glsl -o color_vert.spv
4+
glslc.exe -fshader-stage=frag color_frag.glsl -o color_frag.spv

shaders/compile.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/bin/sh
22
glslc -fshader-stage=vert triangle_vert.glsl -o triangle_vert.spv
33
glslc -fshader-stage=frag triangle_frag.glsl -o triangle_frag.spv
4+
glslc -fshader-stage=vert color_vert.glsl -o color_vert.spv
5+
glslc -fshader-stage=frag color_frag.glsl -o color_frag.spv

src/main.cpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ VkCommandBuffer commandBuffers[FRAMES_IN_FLIGHT];
1818
VkFence fences[FRAMES_IN_FLIGHT];
1919
VkSemaphore acquireSemaphores[FRAMES_IN_FLIGHT];
2020
VkSemaphore releaseSemaphores[FRAMES_IN_FLIGHT];
21+
VulkanBuffer vertexBuffer;
2122

2223
bool handleMessage() {
2324
SDL_Event event;
@@ -53,6 +54,17 @@ void recreateRenderPass() {
5354
}
5455
}
5556

57+
float vertexData[] = {
58+
0.0f, -0.5f,
59+
1.0f, 0.0f, 0.0f,
60+
61+
0.5f, 0.5f,
62+
0.0f, 1.0f, 0.0f,
63+
64+
-0.5f, 0.5f,
65+
0.0f, 0.0f, 1.0f,
66+
};
67+
5668
void initApplication(SDL_Window* window) {
5769
uint32_t instanceExtensionCount;
5870
SDL_Vulkan_GetInstanceExtensions(window, &instanceExtensionCount, 0);
@@ -67,7 +79,20 @@ void initApplication(SDL_Window* window) {
6779

6880
recreateRenderPass();
6981

70-
pipeline = createPipeline(context, "../shaders/triangle_vert.spv", "../shaders/triangle_frag.spv", renderPass, swapchain.width, swapchain.height);
82+
VkVertexInputAttributeDescription vertexAttributeDescriptions[2] = {};
83+
vertexAttributeDescriptions[0].binding = 0;
84+
vertexAttributeDescriptions[0].location = 0;
85+
vertexAttributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
86+
vertexAttributeDescriptions[0].offset = 0;
87+
vertexAttributeDescriptions[1].binding = 0;
88+
vertexAttributeDescriptions[1].location = 1;
89+
vertexAttributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
90+
vertexAttributeDescriptions[1].offset = sizeof(float) * 2;
91+
VkVertexInputBindingDescription vertexInputBinding = {};
92+
vertexInputBinding.binding = 0;
93+
vertexInputBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
94+
vertexInputBinding.stride = sizeof(float) * 5;
95+
pipeline = createPipeline(context, "../shaders/color_vert.spv", "../shaders/color_frag.spv", renderPass, swapchain.width, swapchain.height, vertexAttributeDescriptions, ARRAY_COUNT(vertexAttributeDescriptions), &vertexInputBinding);
7196

7297
for(uint32_t i = 0; i < ARRAY_COUNT(fences); ++i) {
7398
VkFenceCreateInfo createInfo = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
@@ -93,6 +118,12 @@ void initApplication(SDL_Window* window) {
93118
allocateInfo.commandBufferCount = 1;
94119
VKA(vkAllocateCommandBuffers(context->device, &allocateInfo, &commandBuffers[i]));
95120
}
121+
122+
createBuffer(context, &vertexBuffer, sizeof(vertexData), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
123+
void* data;
124+
VKA(vkMapMemory(context->device, vertexBuffer.memory, 0, sizeof(vertexData), 0, &data));
125+
memcpy(data, vertexData, sizeof(vertexData));
126+
VK(vkUnmapMemory(context->device, vertexBuffer.memory));
96127
}
97128

98129
void recreateSwapchain() {
@@ -121,14 +152,14 @@ void renderApplication() {
121152

122153
// Wait for the n-2 frame to finish to be able to reuse its acquireSemaphore in vkAcquireNextImageKHR
123154
VKA(vkWaitForFences(context->device, 1, &fences[frameIndex], VK_TRUE, UINT64_MAX));
124-
VKA(vkResetFences(context->device, 1, &fences[frameIndex]));
125155

126156
VkResult result = VK(vkAcquireNextImageKHR(context->device, swapchain.swapchain, UINT64_MAX, acquireSemaphores[frameIndex], 0, &imageIndex));
127157
if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) {
128158
// Swapchain is out of date
129159
recreateSwapchain();
130160
return;
131161
} else {
162+
VKA(vkResetFences(context->device, 1, &fences[frameIndex]));
132163
ASSERT_VULKAN(result);
133164
}
134165

@@ -156,6 +187,9 @@ void renderApplication() {
156187

157188
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline);
158189

190+
VkDeviceSize offset = 0;
191+
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertexBuffer.buffer, &offset);
192+
159193
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
160194

161195
vkCmdEndRenderPass(commandBuffer);
@@ -194,6 +228,8 @@ void renderApplication() {
194228
void shutdownApplication() {
195229
VKA(vkDeviceWaitIdle(context->device));
196230

231+
destroyBuffer(context, &vertexBuffer);
232+
197233
for(uint32_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
198234
VK(vkDestroyFence(context->device, fences[i], 0));
199235
VK(vkDestroySemaphore(context->device, acquireSemaphores[i], 0));

src/vulkan_base/vulkan_base.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ struct VulkanContext {
4141
VulkanQueue graphicsQueue;
4242
};
4343

44+
struct VulkanBuffer {
45+
VkBuffer buffer;
46+
VkDeviceMemory memory;
47+
};
48+
4449
VulkanContext* initVulkan(uint32_t instanceExtensionCount, const char** instanceExtensions, uint32_t deviceExtensionCount, const char** deviceExtensions);
4550
void exitVulkan(VulkanContext* context);
4651

@@ -50,5 +55,8 @@ void destroySwapchain(VulkanContext* context, VulkanSwapchain* swapchain);
5055
VkRenderPass createRenderPass(VulkanContext* context, VkFormat format);
5156
void destroyRenderpass(VulkanContext* context, VkRenderPass renderPass);
5257

53-
VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFilename, const char* fragmentShaderFilename, VkRenderPass renderPass, uint32_t width, uint32_t height);
58+
void createBuffer(VulkanContext* context, VulkanBuffer* buffer, uint64_t size, VkBufferUsageFlags usage, VkMemoryPropertyFlags memoryProperties);
59+
void destroyBuffer(VulkanContext* context, VulkanBuffer* buffer);
60+
61+
VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFilename, const char* fragmentShaderFilename, VkRenderPass renderPass, uint32_t width, uint32_t height, VkVertexInputAttributeDescription* attributes, uint32_t numAttributes, VkVertexInputBindingDescription* binding);
5462
void destroyPipeline(VulkanContext* context, VulkanPipeline* pipeline);

src/vulkan_base/vulkan_pipeline.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ VkShaderModule createShaderModule(VulkanContext* context, const char* shaderFile
2727
return result;
2828
}
2929

30-
VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFilename, const char* fragmentShaderFilename, VkRenderPass renderPass, uint32_t width, uint32_t height) {
30+
VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFilename, const char* fragmentShaderFilename, VkRenderPass renderPass, uint32_t width, uint32_t height, VkVertexInputAttributeDescription* attributes, uint32_t numAttributes, VkVertexInputBindingDescription* binding) {
3131
VkShaderModule vertexShaderModule = createShaderModule(context, vertexShaderFilename);
3232
VkShaderModule fragmentShaderModule = createShaderModule(context, fragmentShaderFilename);
3333

@@ -42,6 +42,10 @@ VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFi
4242
shaderStages[1].pName = "main";
4343

4444
VkPipelineVertexInputStateCreateInfo vertexInputState = { VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO };
45+
vertexInputState.vertexBindingDescriptionCount = binding ? 1 : 0;
46+
vertexInputState.pVertexBindingDescriptions = binding;
47+
vertexInputState.vertexAttributeDescriptionCount = numAttributes;
48+
vertexInputState.pVertexAttributeDescriptions = attributes;
4549

4650
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = { VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO };
4751
inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;

0 commit comments

Comments
 (0)