From 9d043c35942fdda306208c64741bf6eedee0904e Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Wed, 11 Sep 2024 19:55:26 +0200 Subject: [PATCH] closer --- shaders/shader.frag.glsl | 7 +- src/ptk_vk/init.c | 216 +++++++++++++++++++++++++-------------- 2 files changed, 143 insertions(+), 80 deletions(-) diff --git a/shaders/shader.frag.glsl b/shaders/shader.frag.glsl index 2a8d510..a5fec21 100644 --- a/shaders/shader.frag.glsl +++ b/shaders/shader.frag.glsl @@ -5,13 +5,16 @@ layout(constant_id = 0) const int PTK_COMPONENT_TYPE_ELLIPSE = 0; layout(constant_id = 1) const int PTK_COMPONENT_TYPE_IMAGE = 0; +layout(constant_id = 2) const int g_max_images = 1; + layout(location = 0) in vec3 fragColor; layout(location = 1) flat in int shapeType; layout(location = 2) in vec2 uv; layout(location = 0) out vec4 outColor; -layout(binding = 1) uniform sampler2D texSampler; +layout(binding = 1) uniform sampler samp; +layout(binding = 2) uniform texture2D textures[g_max_images]; void main() { if (shapeType == PTK_COMPONENT_TYPE_ELLIPSE) { @@ -20,7 +23,7 @@ void main() { } } if (shapeType == PTK_COMPONENT_TYPE_IMAGE) { - outColor = texture(texSampler, uv); + outColor = texture(sampler2D(textures[0], samp), uv); } else { outColor = vec4(fragColor, 1.0); } diff --git a/src/ptk_vk/init.c b/src/ptk_vk/init.c index d812b47..c1ecaa7 100644 --- a/src/ptk_vk/init.c +++ b/src/ptk_vk/init.c @@ -721,7 +721,33 @@ bool create_render_pass(void) { return true; } +PTK_ARRAY_DEFINE(VkDescriptorSetLayoutBinding); + bool create_descriptor_set_layout(void) { + PTK_ARRAY(VkDescriptorSetLayoutBinding) bindings = PTK_ARRAY_NEW(VkDescriptorSetLayoutBinding, { + (VkDescriptorSetLayoutBinding){ + .binding = 0, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, + .pImmutableSamplers = NULL, + }, + (VkDescriptorSetLayoutBinding){ + .binding = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, + .pImmutableSamplers = NULL, + }, + (VkDescriptorSetLayoutBinding){ + .binding = 2, + .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, + .descriptorCount = g_max_images, + .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, + .pImmutableSamplers = NULL, + }, + }); + VK_TRY(false, vkCreateDescriptorSetLayout( g_dev, @@ -729,23 +755,8 @@ bool create_descriptor_set_layout(void) { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .pNext = NULL, .flags = 0, - .bindingCount = 2, - .pBindings = (VkDescriptorSetLayoutBinding []){ - (VkDescriptorSetLayoutBinding){ - .binding = 0, - .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .descriptorCount = 1, - .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, - .pImmutableSamplers = NULL, - }, - (VkDescriptorSetLayoutBinding){ - .binding = 1, - .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - .descriptorCount = 1, - .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, - .pImmutableSamplers = &g_sampler, - }, - }, + .bindingCount = bindings.size, + .pBindings = bindings.data, }, NULL, &m_descriptor_set_layout @@ -755,6 +766,8 @@ bool create_descriptor_set_layout(void) { return true; } +PTK_ARRAY_DEFINE(VkSpecializationMapEntry); + bool create_graphics_pipeline(void) { const PTK_STRING vert_shader_code = read_spv("target/shaders/shader.vert.spv"); const PTK_STRING frag_shader_code = read_spv("target/shaders/shader.frag.spv"); @@ -773,24 +786,32 @@ bool create_graphics_pipeline(void) { return false; } - const VkSpecializationInfo spec_info = { - .mapEntryCount = 2, - .pMapEntries = (VkSpecializationMapEntry []){ - (VkSpecializationMapEntry){ - .constantID = 0, - .offset = 0, - .size = sizeof(int), - }, - (VkSpecializationMapEntry){ - .constantID = 1, - .offset = sizeof(int), - .size = sizeof(int), - }, + PTK_ARRAY(VkSpecializationMapEntry) frag_spec_map_entries = PTK_ARRAY_NEW(VkSpecializationMapEntry, { + (VkSpecializationMapEntry){ + .constantID = 0, + .offset = 0, + .size = sizeof(int), }, - .dataSize = sizeof(int) * 2, + (VkSpecializationMapEntry){ + .constantID = 1, + .offset = sizeof(int), + .size = sizeof(int), + }, + (VkSpecializationMapEntry){ + .constantID = 2, + .offset = sizeof(int) * 2, + .size = sizeof(int), + }, + }); + + const VkSpecializationInfo frag_spec_info = { + .mapEntryCount = frag_spec_map_entries.size, + .pMapEntries = frag_spec_map_entries.data, + .dataSize = sizeof(int) * 3, .pData = (int []){ PTK_COMPONENT_TYPE_ELLIPSE, - PTK_COMPONENT_TYPE_IMAGE + PTK_COMPONENT_TYPE_IMAGE, + g_max_images }, }; @@ -811,7 +832,7 @@ bool create_graphics_pipeline(void) { .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = frag_shader_module.value, .pName = "main", - .pSpecializationInfo = &spec_info, + .pSpecializationInfo = &frag_spec_info, }, }; @@ -1291,7 +1312,24 @@ bool create_uniform_buffers(void) { return true; } +PTK_ARRAY_DEFINE(VkDescriptorPoolSize); + bool create_descriptor_pool(void) { + PTK_ARRAY(VkDescriptorPoolSize) pool_sizes = PTK_ARRAY_NEW(VkDescriptorPoolSize, { + (VkDescriptorPoolSize){ + .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = g_max_frames_in_flight, + }, + (VkDescriptorPoolSize){ + .type = VK_DESCRIPTOR_TYPE_SAMPLER, + .descriptorCount = g_max_frames_in_flight, + }, + (VkDescriptorPoolSize){ + .type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, + .descriptorCount = g_max_frames_in_flight * g_max_images, + }, + }); + VK_TRY(false, vkCreateDescriptorPool( g_dev, @@ -1299,18 +1337,9 @@ bool create_descriptor_pool(void) { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .pNext = NULL, .flags = 0, - .maxSets = g_max_frames_in_flight * g_max_images, - .poolSizeCount = 2, - .pPoolSizes = (VkDescriptorPoolSize []){ - (VkDescriptorPoolSize){ - .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .descriptorCount = g_max_frames_in_flight, - }, - (VkDescriptorPoolSize){ - .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - .descriptorCount = g_max_images, - }, - }, + .maxSets = g_max_frames_in_flight, + .poolSizeCount = pool_sizes.size, + .pPoolSizes = pool_sizes.data, }, NULL, &m_descriptor_pool @@ -1321,56 +1350,87 @@ bool create_descriptor_pool(void) { } PTK_LIST_DEFINE(VkDescriptorSetLayout); +PTK_LIST_DEFINE(VkWriteDescriptorSet); void vk_update_descriptor_sets(void) { - VkDescriptorImageInfo image_infos[g_max_images]; + VkDescriptorImageInfo image_infos[g_image_count]; PTK_DEBUG("g_image_count: %d", g_image_count); for (size_t i = 0; i < g_image_count; ++i) { image_infos[i] = (VkDescriptorImageInfo){ - .sampler = g_sampler, + .sampler = VK_NULL_HANDLE, .imageView = g_image_views.data[i], .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, }; } for (size_t i = 0; i < g_max_frames_in_flight; ++i) { + PTK_LIST(VkWriteDescriptorSet) descriptor_writes = PTK_LIST_NEW(VkWriteDescriptorSet, 2); + + VkWriteDescriptorSet ubo_descriptor_write = (VkWriteDescriptorSet){ + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .pNext = NULL, + .dstSet = m_descriptor_sets.data[i], + .dstBinding = 0, + .dstArrayElement = 0, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .pImageInfo = NULL, + .pBufferInfo = &(VkDescriptorBufferInfo){ + .buffer = m_uniform_buffers.data[i], + .offset = 0, + .range = sizeof(UniformBufferObject), + }, + .pTexelBufferView = NULL, + }; + + PTK_LIST_ADD(VkWriteDescriptorSet, descriptor_writes, ubo_descriptor_write); + + VkWriteDescriptorSet sampler_descriptor_write = (VkWriteDescriptorSet){ + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .pNext = NULL, + .dstSet = m_descriptor_sets.data[i], + .dstBinding = 1, + .dstArrayElement = 0, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, + .pImageInfo = &(VkDescriptorImageInfo){ + .sampler = g_sampler, + .imageView = VK_NULL_HANDLE, + .imageLayout = VK_IMAGE_LAYOUT_UNDEFINED, + }, + .pBufferInfo = NULL, + .pTexelBufferView = NULL, + }; + + PTK_LIST_ADD(VkWriteDescriptorSet, descriptor_writes, sampler_descriptor_write); + + if (g_image_count != 0) { + VkWriteDescriptorSet image_descriptor_write = (VkWriteDescriptorSet){ + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .pNext = NULL, + .dstSet = m_descriptor_sets.data[i], + .dstBinding = 2, + .dstArrayElement = 0, + .descriptorCount = g_image_count, + .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, + .pImageInfo = image_infos, + .pBufferInfo = NULL, + .pTexelBufferView = NULL, + }; + + PTK_LIST_ADD(VkWriteDescriptorSet, descriptor_writes, image_descriptor_write); + } + vkUpdateDescriptorSets( g_dev, - 2, - (VkWriteDescriptorSet []){ - (VkWriteDescriptorSet){ - .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, - .pNext = NULL, - .dstSet = m_descriptor_sets.data[i], - .dstBinding = 0, - .dstArrayElement = 0, - .descriptorCount = 1, - .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .pImageInfo = NULL, - .pBufferInfo = &(VkDescriptorBufferInfo){ - .buffer = m_uniform_buffers.data[i], - .offset = 0, - .range = sizeof(UniformBufferObject), - }, - .pTexelBufferView = NULL, - }, - (VkWriteDescriptorSet){ - .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, - .pNext = NULL, - .dstSet = m_descriptor_sets.data[i], - .dstBinding = 1, - .dstArrayElement = 0, - .descriptorCount = g_image_count, - .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - .pImageInfo = image_infos, - .pBufferInfo = NULL, - .pTexelBufferView = NULL, - }, - }, + descriptor_writes.size, + descriptor_writes.data, 0, NULL ); + + PTK_LIST_FREE(descriptor_writes); } }