summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2024-05-21 21:17:06 +0300
committergramanas <anastasis.gramm2@gmail.com>2024-05-21 21:17:06 +0300
commit2cb9a75dc7118486c33d387871ca98bb30c26678 (patch)
treecd8618cfe09d2ee2f55fee634ce1cb571ab80da4 /src
parenta6eefcc1d2dd2479b44dbd0bb3732e8568725277 (diff)
downloadcgame-2cb9a75dc7118486c33d387871ca98bb30c26678.tar.gz
cgame-2cb9a75dc7118486c33d387871ca98bb30c26678.tar.bz2
cgame-2cb9a75dc7118486c33d387871ca98bb30c26678.zip
Dynamic rendering
Diffstat (limited to 'src')
-rw-r--r--src/game.c188
-rw-r--r--src/state.h16
2 files changed, 88 insertions, 116 deletions
diff --git a/src/game.c b/src/game.c
index 4b0ea22..c566f85 100644
--- a/src/game.c
+++ b/src/game.c
@@ -891,11 +891,17 @@ vulkan_create_graphics_pipeline()
vk_log(VK_ERROR, "failed to create pipeline layout!\n");
}
+ VkPipelineRenderingCreateInfo pipeline_rendering_create_info = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
+ .colorAttachmentCount = 1,
+ .pColorAttachmentFormats = &s.vk_swap_chain_image_format,
+ };
VkGraphicsPipelineCreateInfo pipelineInfo = {0};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = shaderStages;
+ pipelineInfo.pNext = &pipeline_rendering_create_info,
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &inputAssembly;
pipelineInfo.pViewportState = &viewportState;
@@ -905,7 +911,7 @@ vulkan_create_graphics_pipeline()
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.pDynamicState = &dynamicState;
pipelineInfo.layout = s.vk_pipeline_layout;
- pipelineInfo.renderPass = s.vk_render_pass;
+ pipelineInfo.renderPass = VK_NULL_HANDLE;
pipelineInfo.subpass = 0;
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; // Optional
pipelineInfo.basePipelineIndex = -1; // Optional
@@ -926,72 +932,6 @@ vulkan_create_graphics_pipeline()
}
void
-vulkan_create_render_pass()
-{
- VkAttachmentDescription colorAttachment = {0};
- colorAttachment.format = s.vk_swap_chain_image_format;
- colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
- colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
- colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
- colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
- colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
- colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
- colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
-
- VkAttachmentReference colorAttachmentRef = {0};
- colorAttachmentRef.attachment = 0;
- colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
-
- VkSubpassDescription subpass = {0};
- subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
- subpass.colorAttachmentCount = 1;
- subpass.pColorAttachments = &colorAttachmentRef;
-
- VkSubpassDependency dependency = {0};
- dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
- dependency.dstSubpass = 0;
- dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
- dependency.srcAccessMask = 0;
- dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
- dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
-
- VkRenderPassCreateInfo renderPassInfo = {0};
- renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
- renderPassInfo.attachmentCount = 1;
- renderPassInfo.pAttachments = &colorAttachment;
- renderPassInfo.subpassCount = 1;
- renderPassInfo.pSubpasses = &subpass;
- renderPassInfo.dependencyCount = 1;
- renderPassInfo.pDependencies = &dependency;
-
- if (vkCreateRenderPass(s.vk_device, &renderPassInfo, NULL, &s.vk_render_pass) != VK_SUCCESS) {
- vk_log(VK_ERROR, "failed to create render pass!\n");
- }
-}
-
-void
-vulkan_create_framebuffers()
-{
- s.vk_swap_chain_framebuffers_count = s.vk_swap_chain_image_count;
- for (size_t i = 0; i < s.vk_swap_chain_image_count; i++) {
- VkImageView attachments[] = {
- s.vk_swap_chain_image_views[i]
- };
-
- VkFramebufferCreateInfo framebufferInfo = {0};
- framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
- framebufferInfo.renderPass = s.vk_render_pass;
- framebufferInfo.attachmentCount = 1;
- framebufferInfo.pAttachments = attachments;
- framebufferInfo.width = s.vk_swap_chain_extent.width;
- framebufferInfo.height = s.vk_swap_chain_extent.height;
- framebufferInfo.layers = 1;
-
- VK_CHECK(vkCreateFramebuffer(s.vk_device, &framebufferInfo, NULL, &s.vk_swap_chain_framebuffers[i]));
- }
-}
-
-void
vulkan_create_command_pool()
{
QueueFamilyIndices queueFamilyIndices = vulkan_find_queue_families(s.vk_physical_device);
@@ -1029,38 +969,53 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex)
VK_CHECK(vkBeginCommandBuffer(commandBuffer, &beginInfo));
- VkRenderPassBeginInfo renderPassInfo = {0};
- renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
- renderPassInfo.renderPass = s.vk_render_pass;
- renderPassInfo.framebuffer = s.vk_swap_chain_framebuffers[imageIndex];
- renderPassInfo.renderArea.offset = (VkOffset2D){0, 0};
- renderPassInfo.renderArea.extent = s.vk_swap_chain_extent;
+ VkImageMemoryBarrier image_memory_barrier = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
+ .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
+ .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
+ .newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
+ .image = s.vk_swap_chain_images[imageIndex],
+ .subresourceRange = (VkImageSubresourceRange){
+ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
+ .baseMipLevel = 0,
+ .levelCount = 1,
+ .baseArrayLayer = 0,
+ .layerCount = 1,
+ }
+ };
+
+ vkCmdPipelineBarrier(
+ commandBuffer,
+ VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, // srcStageMask
+ VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, // dstStageMask
+ 0,
+ 0,
+ NULL,
+ 0,
+ NULL,
+ 1, // imageMemoryBarrierCount
+ &image_memory_barrier // pImageMemoryBarriers
+ );
VkClearValue clearColor = {{{0.0f, 0.0f, 0.0f, 1.0f}}};
- renderPassInfo.clearValueCount = 1;
- renderPassInfo.pClearValues = &clearColor;
// TODO DYNAMIC RENDERING
- /* VkRenderingAttachmentInfo colorAttachment = {}; */
- /* colorAttachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO; */
- /* colorAttachment.imageView = s.vk_swap_chain_image_views[imageIndex]; */
- /* colorAttachment.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; */
- /* colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; */
- /* colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; */
- /* //colorAttachment.clearValue.color = clearColor; */
-
- /* VkRenderingInfo renderingInfo = {}; */
- /* renderingInfo.sType = VK_STRUCTURE_TYPE_RENDERING_INFO; */
- /* renderingInfo.renderArea = (VkRect2D){ {0, 0}, {(float)(s.vk_swap_chain_extent.width), (float)(s.vk_swap_chain_extent.height)} }; */
- /* renderingInfo.layerCount = 1; */
- /* renderingInfo.colorAttachmentCount = 1; */
- /* renderingInfo.pColorAttachments = &colorAttachment; */
-
- /* vkCmdBeginRendering(commandBuffer, &renderingInfo); */
- /* .... */
- /* vkCmdEndRendering(commandBuffer); */
-
- vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
+ VkRenderingAttachmentInfo colorAttachment = {};
+ colorAttachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO;
+ colorAttachment.imageView = s.vk_swap_chain_image_views[imageIndex];
+ colorAttachment.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
+ colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
+ colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
+ //colorAttachment.clearValue.color = clearColor;
+
+ VkRenderingInfo renderingInfo = {};
+ renderingInfo.sType = VK_STRUCTURE_TYPE_RENDERING_INFO;
+ renderingInfo.renderArea = (VkRect2D){ {0, 0}, {(float)(s.vk_swap_chain_extent.width), (float)(s.vk_swap_chain_extent.height)} };
+ renderingInfo.layerCount = 1;
+ renderingInfo.colorAttachmentCount = 1;
+ renderingInfo.pColorAttachments = &colorAttachment;
+
+ vkCmdBeginRendering(commandBuffer, &renderingInfo);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, s.vk_graphics_pipeline);
@@ -1085,8 +1040,40 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex)
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, s.vk_pipeline_layout, 0, 1, &s.frames[currentFrame].vk_descriptor_set, 0, NULL);
vkCmdDrawIndexed(commandBuffer, INDICES_SIZE, 1, 0, 0, 0);
-
- vkCmdEndRenderPass(commandBuffer);
+ vkCmdEndRendering(commandBuffer);
+
+ /* vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); */
+
+
+ /* vkCmdEndRenderPass(commandBuffer); */
+
+ VkImageMemoryBarrier image_memory_barrier2 = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
+ .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
+ .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
+ .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
+ .image = s.vk_swap_chain_images[imageIndex],
+ .subresourceRange = (VkImageSubresourceRange){
+ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
+ .baseMipLevel = 0,
+ .levelCount = 1,
+ .baseArrayLayer = 0,
+ .layerCount = 1,
+ }
+ };
+
+ vkCmdPipelineBarrier(
+ commandBuffer,
+ VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, // srcStageMask
+ VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, // dstStageMask
+ 0,
+ 0,
+ NULL,
+ 0,
+ NULL,
+ 1, // imageMemoryBarrierCount
+ &image_memory_barrier2 // pImageMemoryBarriers
+ );
VK_CHECK(vkEndCommandBuffer(commandBuffer));
}
@@ -1339,9 +1326,6 @@ vulkan_create_descriptor_sets()
void
cleanupSwapChain()
{
- for (size_t i = 0; i < s.vk_swap_chain_image_count; i++) {
- vkDestroyFramebuffer(s.vk_device, s.vk_swap_chain_framebuffers[i], NULL);
- }
for (uint32_t i = 0; i < s.vk_swap_chain_image_count; i++) {
vkDestroyImageView(s.vk_device, s.vk_swap_chain_image_views[i], NULL);
}
@@ -1357,7 +1341,6 @@ recreateSwapChain()
vulkan_create_swap_chain();
vulkan_create_image_views();
- vulkan_create_framebuffers();
}
void
@@ -1447,10 +1430,8 @@ init_vulkan()
vulkan_create_logical_device();
vulkan_create_swap_chain();
vulkan_create_image_views();
- vulkan_create_render_pass();
vulkan_create_descriptor_set_layout();
vulkan_create_graphics_pipeline();
- vulkan_create_framebuffers();
vulkan_create_command_pool();
vulkan_create_vertex_buffer();
vulkan_create_index_buffer();
@@ -1490,7 +1471,6 @@ close_vulkan()
vkFreeMemory(s.vk_device, s.vk_index_buffer_memory, NULL);
vkDestroyPipeline(s.vk_device, s.vk_graphics_pipeline, NULL);
- vkDestroyRenderPass(s.vk_device, s.vk_render_pass, NULL);
vkDestroyPipelineLayout(s.vk_device, s.vk_pipeline_layout, NULL);
vkDestroyDevice(s.vk_device, NULL);
vkDestroySurfaceKHR(s.vk_instance, s.vk_surface, NULL);
diff --git a/src/state.h b/src/state.h
index 6feb9d1..e5cb784 100644
--- a/src/state.h
+++ b/src/state.h
@@ -74,24 +74,18 @@ typedef struct state {
VkQueue vk_present_queue;
VkSwapchainKHR vk_swap_chain;
+
uint32_t vk_swap_chain_image_count;
- // LIST vk_swap_chain_images; // VkImage
- VkImage vk_swap_chain_images[1024];
+ VkImage vk_swap_chain_images[5];
+ VkImageView vk_swap_chain_image_views[5]; // 5 for some reason
+
VkFormat vk_swap_chain_image_format;
VkExtent2D vk_swap_chain_extent;
- //LIST vk_swap_chain_image_views; // VkImageView
- VkImageView vk_swap_chain_image_views[1000];
-
- VkRenderPass vk_render_pass;
-
VkDescriptorSetLayout vk_descriptor_set_layout;
VkPipelineLayout vk_pipeline_layout;
VkPipeline vk_graphics_pipeline;
- VkFramebuffer vk_swap_chain_framebuffers[1000];
- uint32_t vk_swap_chain_framebuffers_count;
-
VkCommandPool vk_command_pool;
frame_data frames[MAX_FRAMES_IN_FLIGHT];
@@ -153,8 +147,6 @@ init_state(state_t * s)
s->vk_swap_chain_image_count = 0;
s->vk_physical_device = VK_NULL_HANDLE;
- s->vk_swap_chain_framebuffers_count = 0;
-
s->prev_vert_result = NULL;
s->prev_frag_result = NULL;
}