@@ -18,6 +18,7 @@ VkCommandBuffer commandBuffers[FRAMES_IN_FLIGHT];
1818VkFence fences[FRAMES_IN_FLIGHT];
1919VkSemaphore acquireSemaphores[FRAMES_IN_FLIGHT];
2020VkSemaphore releaseSemaphores[FRAMES_IN_FLIGHT];
21+ VulkanBuffer vertexBuffer;
2122
2223bool 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+
5668void 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
98129void 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() {
194228void 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 ));
0 commit comments