From 67eec36945ed1706f23d0fe6d3017a09516dc42b Mon Sep 17 00:00:00 2001 From: grm Date: Fri, 3 Jan 2025 02:14:17 +0200 Subject: Prepare for compute --- src/render.c | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) (limited to 'src/render.c') diff --git a/src/render.c b/src/render.c index 1ff2e13..5e9202b 100644 --- a/src/render.c +++ b/src/render.c @@ -618,6 +618,19 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) // TODO Make polygon mode dynamic //vkCmdSetPolygonModeEXT(commandBuffer, s.polygon_mode); + + + /* vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, s.vk_compute_pipeline); */ + /* vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, s.vk_compute_pipeline_layout, 0, 1, &s.frames[imageIndex].vk_compute_descriptor_set, 0, 0); */ + + /* vkCmdDispatch(commandBuffer, PARTICLE_COUNT / 256, 1, 1); */ + + + + + + + vks_transition_image_layout_info transition_info = { 0 }; transition_info.image = s.vk.swapchain.images[imageIndex]; @@ -862,9 +875,11 @@ void vulkan_create_descriptor_sets() { VkDescriptorSetLayout layouts[MAX_FRAMES_IN_FLIGHT] = {0}; + /* VkDescriptorSetLayout compute_layouts[MAX_FRAMES_IN_FLIGHT] = {0}; */ for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { layouts[i] = s.vk_descriptor_set_layout; + /* compute_layouts[i] = s.vk_compute_descriptor_set_layout; */ } // TODO Find a way to group allocation @@ -876,15 +891,23 @@ vulkan_create_descriptor_sets() allocInfo.pSetLayouts = layouts; VK_CHECK(vkAllocateDescriptorSets(s.vk.device, &allocInfo, &s.frames[i].vk_descriptor_set)); + + /* allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; */ + /* allocInfo.descriptorPool = s.vk_descriptor_pool; */ + /* allocInfo.descriptorSetCount = 1; */ + /* allocInfo.pSetLayouts = compute_layouts; */ + + /* VK_CHECK(vkAllocateDescriptorSets(s.vk.device, &allocInfo, &s.frames[i].vk_compute_descriptor_set)); */ } for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { + VkWriteDescriptorSet descriptorWrites[2] = {0}; + VkDescriptorBufferInfo bufferInfo = {0}; bufferInfo.buffer = s.frames[i].uniform_buffer.handle; bufferInfo.offset = 0; bufferInfo.range = sizeof(UniformBufferObject); - VkWriteDescriptorSet descriptorWrites[2] = {0}; descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptorWrites[0].dstSet = s.frames[i].vk_descriptor_set; descriptorWrites[0].dstBinding = 0; @@ -907,7 +930,7 @@ vulkan_create_descriptor_sets() descriptorWrites[1].pImageInfo = &imageInfo; /* VkDescriptorBufferInfo storageBufferInfoLastFrame = {0}; */ - /* storageBufferInfoLastFrame.buffer = s.frames[(i - 1) % MAX_FRAMES_IN_FLIGHT].vk_shader_storage_buffer; */ + /* storageBufferInfoLastFrame.buffer = s.frames[(i - 1) % MAX_FRAMES_IN_FLIGHT].shader_storage_buffer.handle; */ /* storageBufferInfoLastFrame.offset = 0; */ /* storageBufferInfoLastFrame.range = sizeof(Particle) * PARTICLE_COUNT; */ @@ -920,7 +943,7 @@ vulkan_create_descriptor_sets() /* descriptorWrites[2].pBufferInfo = &storageBufferInfoLastFrame; */ /* VkDescriptorBufferInfo storageBufferInfoCurrentFrame = {0}; */ - /* storageBufferInfoCurrentFrame.buffer = s.frames[i].vk_shader_storage_buffer; */ + /* storageBufferInfoCurrentFrame.buffer = s.frames[i].shader_storage_buffer.handle; */ /* storageBufferInfoCurrentFrame.offset = 0; */ /* storageBufferInfoCurrentFrame.range = sizeof(Particle) * PARTICLE_COUNT; */ @@ -932,7 +955,6 @@ vulkan_create_descriptor_sets() /* descriptorWrites[3].descriptorCount = 1; */ /* descriptorWrites[3].pBufferInfo = &storageBufferInfoCurrentFrame; */ - vkUpdateDescriptorSets(s.vk.device, VK_ARRAY_LEN(descriptorWrites), descriptorWrites, 0, NULL); } } @@ -1406,9 +1428,9 @@ void vulkan_create_compute_stuff() vkUnmapMemory(s.vk.device, stagingBuffer.memory); for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { - // vks_create_buffer(s.vk, bufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, &s.frames[i].shader_storage_buffer); - // Copy data from the staging buffer (host) to the shader storage buffer (GPU) - // vks_copy_buffer(s.vk, stagingBuffer.handle, s.frames[i].shader_storage_buffer.handle, bufferSize); + vks_create_buffer(s.vk, bufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, &s.frames[i].shader_storage_buffer); + // Copy data from the staging buffer (host) to the shader storage buffer (GPU) + vks_copy_buffer(s.vk, stagingBuffer.handle, s.frames[i].shader_storage_buffer.handle, bufferSize); } VkDescriptorSetLayoutBinding layoutBindings[3]; @@ -1434,23 +1456,20 @@ void vulkan_create_compute_stuff() layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; layoutInfo.bindingCount = 3; layoutInfo.pBindings = layoutBindings; - VK_CHECK(vkCreateDescriptorSetLayout(s.vk.device, &layoutInfo, NULL, &s.vk_compute_descriptor_set_layout)); - VkComputePipelineCreateInfo pipelineInfo = {0}; - pipelineInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; - pipelineInfo.layout = s.vk_compute_pipeline_layout; - pipelineInfo.stage = computeShaderStageInfo; - - VK_CHECK(vkCreateComputePipelines(s.vk.device, VK_NULL_HANDLE, 1, &pipelineInfo, NULL, &s.vk_compute_pipeline)); - VkPipelineLayoutCreateInfo pipelineLayoutInfo = {0}; pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutInfo.setLayoutCount = 1; pipelineLayoutInfo.pSetLayouts = &s.vk_compute_descriptor_set_layout; - VK_CHECK(vkCreatePipelineLayout(s.vk.device, &pipelineLayoutInfo, NULL, &s.vk_compute_pipeline_layout)); + VkComputePipelineCreateInfo pipelineInfo = {0}; + pipelineInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; + pipelineInfo.layout = s.vk_compute_pipeline_layout; + pipelineInfo.stage = computeShaderStageInfo; + VK_CHECK(vkCreateComputePipelines(s.vk.device, VK_NULL_HANDLE, 1, &pipelineInfo, NULL, &s.vk_compute_pipeline)); + if (s.prev_comp_result != comp_result) { shaderc_result_release(comp_result); } @@ -1470,7 +1489,7 @@ init_vulkan() vulkan_create_descriptor_set_layout(); vulkan_create_graphics_pipeline(s.polygon_mode); - //vulkan_create_compute_stuff(); + vulkan_create_compute_stuff(); vulkan_create_texture_image(); vulkan_create_texture_sampler(); -- cgit v1.2.3