This commit is contained in:
jacekpoz 2024-09-11 19:55:26 +02:00
parent 9fba9e626e
commit 9d043c3594
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
2 changed files with 143 additions and 80 deletions

View file

@ -5,13 +5,16 @@
layout(constant_id = 0) const int PTK_COMPONENT_TYPE_ELLIPSE = 0; 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 = 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 = 0) in vec3 fragColor;
layout(location = 1) flat in int shapeType; layout(location = 1) flat in int shapeType;
layout(location = 2) in vec2 uv; layout(location = 2) in vec2 uv;
layout(location = 0) out vec4 outColor; 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() { void main() {
if (shapeType == PTK_COMPONENT_TYPE_ELLIPSE) { if (shapeType == PTK_COMPONENT_TYPE_ELLIPSE) {
@ -20,7 +23,7 @@ void main() {
} }
} }
if (shapeType == PTK_COMPONENT_TYPE_IMAGE) { if (shapeType == PTK_COMPONENT_TYPE_IMAGE) {
outColor = texture(texSampler, uv); outColor = texture(sampler2D(textures[0], samp), uv);
} else { } else {
outColor = vec4(fragColor, 1.0); outColor = vec4(fragColor, 1.0);
} }

View file

@ -721,7 +721,33 @@ bool create_render_pass(void) {
return true; return true;
} }
PTK_ARRAY_DEFINE(VkDescriptorSetLayoutBinding);
bool create_descriptor_set_layout(void) { 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, VK_TRY(false,
vkCreateDescriptorSetLayout( vkCreateDescriptorSetLayout(
g_dev, g_dev,
@ -729,23 +755,8 @@ bool create_descriptor_set_layout(void) {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = NULL, .pNext = NULL,
.flags = 0, .flags = 0,
.bindingCount = 2, .bindingCount = bindings.size,
.pBindings = (VkDescriptorSetLayoutBinding []){ .pBindings = bindings.data,
(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,
},
},
}, },
NULL, NULL,
&m_descriptor_set_layout &m_descriptor_set_layout
@ -755,6 +766,8 @@ bool create_descriptor_set_layout(void) {
return true; return true;
} }
PTK_ARRAY_DEFINE(VkSpecializationMapEntry);
bool create_graphics_pipeline(void) { bool create_graphics_pipeline(void) {
const PTK_STRING vert_shader_code = read_spv("target/shaders/shader.vert.spv"); 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"); const PTK_STRING frag_shader_code = read_spv("target/shaders/shader.frag.spv");
@ -773,24 +786,32 @@ bool create_graphics_pipeline(void) {
return false; return false;
} }
const VkSpecializationInfo spec_info = { PTK_ARRAY(VkSpecializationMapEntry) frag_spec_map_entries = PTK_ARRAY_NEW(VkSpecializationMapEntry, {
.mapEntryCount = 2, (VkSpecializationMapEntry){
.pMapEntries = (VkSpecializationMapEntry []){ .constantID = 0,
(VkSpecializationMapEntry){ .offset = 0,
.constantID = 0, .size = sizeof(int),
.offset = 0,
.size = sizeof(int),
},
(VkSpecializationMapEntry){
.constantID = 1,
.offset = sizeof(int),
.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 []){ .pData = (int []){
PTK_COMPONENT_TYPE_ELLIPSE, 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, .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
.module = frag_shader_module.value, .module = frag_shader_module.value,
.pName = "main", .pName = "main",
.pSpecializationInfo = &spec_info, .pSpecializationInfo = &frag_spec_info,
}, },
}; };
@ -1291,7 +1312,24 @@ bool create_uniform_buffers(void) {
return true; return true;
} }
PTK_ARRAY_DEFINE(VkDescriptorPoolSize);
bool create_descriptor_pool(void) { 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, VK_TRY(false,
vkCreateDescriptorPool( vkCreateDescriptorPool(
g_dev, g_dev,
@ -1299,18 +1337,9 @@ bool create_descriptor_pool(void) {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.pNext = NULL, .pNext = NULL,
.flags = 0, .flags = 0,
.maxSets = g_max_frames_in_flight * g_max_images, .maxSets = g_max_frames_in_flight,
.poolSizeCount = 2, .poolSizeCount = pool_sizes.size,
.pPoolSizes = (VkDescriptorPoolSize []){ .pPoolSizes = pool_sizes.data,
(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,
},
},
}, },
NULL, NULL,
&m_descriptor_pool &m_descriptor_pool
@ -1321,56 +1350,87 @@ bool create_descriptor_pool(void) {
} }
PTK_LIST_DEFINE(VkDescriptorSetLayout); PTK_LIST_DEFINE(VkDescriptorSetLayout);
PTK_LIST_DEFINE(VkWriteDescriptorSet);
void vk_update_descriptor_sets(void) { 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); PTK_DEBUG("g_image_count: %d", g_image_count);
for (size_t i = 0; i < g_image_count; ++i) { for (size_t i = 0; i < g_image_count; ++i) {
image_infos[i] = (VkDescriptorImageInfo){ image_infos[i] = (VkDescriptorImageInfo){
.sampler = g_sampler, .sampler = VK_NULL_HANDLE,
.imageView = g_image_views.data[i], .imageView = g_image_views.data[i],
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
}; };
} }
for (size_t i = 0; i < g_max_frames_in_flight; ++i) { 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( vkUpdateDescriptorSets(
g_dev, g_dev,
2, descriptor_writes.size,
(VkWriteDescriptorSet []){ descriptor_writes.data,
(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,
},
},
0, 0,
NULL NULL
); );
PTK_LIST_FREE(descriptor_writes);
} }
} }