Fixed crash, it was because without material descriptor set in the layout I was trying to bind to index 2 with only 2 sets

main
noah metz 2024-01-09 23:06:08 -07:00
parent c337e0f728
commit 86f9f9b911
3 changed files with 90 additions and 50 deletions

@ -1,4 +1,4 @@
CFLAGS = -I/usr/local/include -O0 -g -Wall -Wextra CFLAGS = -fsanitize=address -I/usr/local/include -O0 -g -Wall -Wextra
LDFLAGS = -L/usr/local/lib -lglfw -lvulkan -ldl -Xlinker -rpath -Xlinker /usr/local/lib LDFLAGS = -L/usr/local/lib -lglfw -lvulkan -ldl -Xlinker -rpath -Xlinker /usr/local/lib
CC = clang CC = clang
GDB = lldb GDB = lldb

@ -1,6 +1,6 @@
#version 450 #version 450
layout(binding = 0) uniform sampler2D texSampler; layout(set = 2, binding = 0) uniform sampler2D texSampler;
layout(location = 0) in vec3 fragColor; layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTex; layout(location = 1) in vec2 fragTex;
@ -8,5 +8,5 @@ layout(location = 1) in vec2 fragTex;
layout(location = 0) out vec4 outColor; layout(location = 0) out vec4 outColor;
void main() { void main() {
outColor = vec4(fragTex, 0.0f, 1.0); outColor = texture(texSampler, fragTex);
} }

@ -333,10 +333,10 @@ const struct Vertex vertices[] = {
}; };
const struct TextureVertex texture_vertices[] = { const struct TextureVertex texture_vertices[] = {
{.pos = {-0.5f, -0.5f, 0.5f}, .color = {1.0f, 0.0f, 0.0f}, .tex = {0.0f, 1.0f}}, {.pos = {-0.5f, -0.5f, 0.5f}, .color = {1.0f, 0.0f, 0.0f}, .tex = {1.0f, 1.0f}},
{.pos = { 0.5f, -0.5f, 0.5f}, .color = {0.0f, 1.0f, 0.0f}, .tex = {1.0f, 0.0f}}, {.pos = { 0.5f, -0.5f, 0.5f}, .color = {0.0f, 1.0f, 0.0f}, .tex = {0.0f, 1.0f}},
{.pos = { 0.5f, 0.5f, 0.5f}, .color = {0.0f, 0.0f, 1.0f}, .tex = {1.0f, 1.0f}}, {.pos = { 0.5f, 0.5f, 0.5f}, .color = {0.0f, 0.0f, 1.0f}, .tex = {0.0f, 0.0f}},
{.pos = {-0.5f, 0.5f, 0.5f}, .color = {1.0f, 1.0f, 1.0f}, .tex = {0.5f, 0.5f}}, {.pos = {-0.5f, 0.5f, 0.5f}, .color = {1.0f, 1.0f, 1.0f}, .tex = {1.0f, 0.0f}},
}; };
const uint16_t indices[] = { const uint16_t indices[] = {
@ -346,8 +346,7 @@ const uint16_t indices[] = {
const char * validation_layers[] = { const char * validation_layers[] = {
"VK_LAYER_KHRONOS_validation", "VK_LAYER_KHRONOS_validation",
//"VK_LAYER_LUNARG_api_dump", //"VK_LAYER_LUNARG_api_dump",
//"VK_LAYER_KHRONOS_profiles", "VK_LAYER_KHRONOS_synchronization2",
//"VK_LAYER_KHRONOS_synchronization2",
"VK_LAYER_KHRONOS_shader_object", "VK_LAYER_KHRONOS_shader_object",
}; };
uint32_t validation_layer_count = sizeof(validation_layers) / sizeof(const char *); uint32_t validation_layer_count = sizeof(validation_layers) / sizeof(const char *);
@ -1424,7 +1423,6 @@ Texture load_texture(VkPhysicalDevice physical_device, VkDevice device, VkComman
return ret; return ret;
} }
VkImageView view; VkImageView view;
VkImageViewCreateInfo view_info = { VkImageViewCreateInfo view_info = {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
@ -1461,7 +1459,7 @@ Texture load_texture(VkPhysicalDevice physical_device, VkDevice device, VkComman
.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT,
.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT,
.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT,
.anisotropyEnable = VK_TRUE, .anisotropyEnable = VK_FALSE,
.maxAnisotropy = 2.0f, .maxAnisotropy = 2.0f,
.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK, .borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK,
.unnormalizedCoordinates = VK_FALSE, .unnormalizedCoordinates = VK_FALSE,
@ -1474,8 +1472,8 @@ Texture load_texture(VkPhysicalDevice physical_device, VkDevice device, VkComman
}; };
result = vkCreateSampler(device, &sampler_info, 0, &sampler); result = vkCreateSampler(device, &sampler_info, 0, &sampler);
deallocate_buffer(device, staging);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
deallocate_buffer(device, staging);
deallocate_image(device, image); deallocate_image(device, image);
return ret; return ret;
} }
@ -1717,7 +1715,6 @@ void command_draw_object(Material material, Object object, uint32_t frame_num, V
} }
VkDescriptorSet* descriptor_sets = maybe_descriptors.value; VkDescriptorSet* descriptor_sets = maybe_descriptors.value;
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, material.layout, 2, 1, &descriptor_sets[frame_num], 0, 0); vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, material.layout, 2, 1, &descriptor_sets[frame_num], 0, 0);
} }
@ -1979,24 +1976,22 @@ Material create_material(
VkDescriptorSetLayout material_set_layout; VkDescriptorSetLayout material_set_layout;
VkDescriptorSetLayout mesh_set_layout; VkDescriptorSetLayout mesh_set_layout;
VkDescriptorSetLayout all_layouts[3] = {scene_ubo_layout, VK_NULL_HANDLE, VK_NULL_HANDLE};
uint32_t num_layouts = 1;
VkDescriptorPool material_descriptor_pool = VK_NULL_HANDLE; VkDescriptorPool material_descriptor_pool = VK_NULL_HANDLE;
VkDescriptorSet* material_descriptors = 0; VkDescriptorSet* material_descriptors = 0;
if(pipeline_layout.material_bindings_count > 0) { VkDescriptorSetLayoutCreateInfo material_layout_info = {
VkDescriptorSetLayoutCreateInfo layout_info = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .bindingCount = pipeline_layout.material_bindings_count,
.bindingCount = pipeline_layout.material_bindings_count, .pBindings = pipeline_layout.material_bindings,
.pBindings = pipeline_layout.material_bindings, };
};
VkResult result = vkCreateDescriptorSetLayout(device, &layout_info, 0, &material_set_layout); VkResult result = vkCreateDescriptorSetLayout(device, &material_layout_info, 0, &material_set_layout);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return zero_material; return zero_material;
} }
if(pipeline_layout.material_bindings_count > 0) {
material_descriptors = malloc(sizeof(VkDescriptorSet)*max_frames_in_flight); material_descriptors = malloc(sizeof(VkDescriptorSet)*max_frames_in_flight);
if(material_descriptors == 0) { if(material_descriptors == 0) {
return zero_material; return zero_material;
@ -2051,37 +2046,30 @@ Material create_material(
vkDestroyDescriptorPool(device, material_descriptor_pool, 0); vkDestroyDescriptorPool(device, material_descriptor_pool, 0);
return zero_material; return zero_material;
} }
all_layouts[num_layouts] = material_set_layout;
num_layouts += 1;
} }
if(pipeline_layout.mesh_bindings_count > 0) { VkDescriptorSetLayoutCreateInfo mesh_layout_info = {
VkDescriptorSetLayoutCreateInfo layout_info = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .bindingCount = pipeline_layout.mesh_bindings_count,
.bindingCount = pipeline_layout.mesh_bindings_count, .pBindings = pipeline_layout.mesh_bindings,
.pBindings = pipeline_layout.mesh_bindings, };
};
VkResult result = vkCreateDescriptorSetLayout(device, &layout_info, 0, &mesh_set_layout);
if(result != VK_SUCCESS) {
return zero_material;
}
all_layouts[num_layouts] = mesh_set_layout; result = vkCreateDescriptorSetLayout(device, &mesh_layout_info, 0, &mesh_set_layout);
num_layouts += 1; if(result != VK_SUCCESS) {
return zero_material;
} }
VkDescriptorSetLayout all_layouts[3] = {scene_ubo_layout, material_set_layout, mesh_set_layout};
VkPipelineLayout layout; VkPipelineLayout layout;
VkPipelineLayoutCreateInfo layout_info = {
VkPipelineLayoutCreateInfo layout_info = {}; .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; .setLayoutCount = 3,
layout_info.setLayoutCount = num_layouts; .pSetLayouts = all_layouts,
layout_info.pSetLayouts = all_layouts; .pushConstantRangeCount = 0,
layout_info.pushConstantRangeCount = 0; // TODO .pPushConstantRanges = 0,
layout_info.pPushConstantRanges = 0; // TODO };
VkResult result = vkCreatePipelineLayout(device, &layout_info, 0, &layout); result = vkCreatePipelineLayout(device, &layout_info, 0, &layout);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return zero_material; return zero_material;
} }
@ -2633,6 +2621,58 @@ VulkanContext* init_vulkan(GLFWwindow* window, uint32_t max_frames_in_flight) {
fprintf(stderr, "failed to add texture to renderable triangle object\n"); fprintf(stderr, "failed to add texture to renderable triangle object\n");
} }
VkExtent2D texture_size = {
.width = 10,
.height = 10,
};
struct __attribute__((__packed__)) texel {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};
struct texel WHT = {255, 255, 255, 255};
struct texel BLK = {0, 0, 0, 255};
struct texel RED = {255, 0, 0, 255};
struct texel GRN = {0, 255, 0, 255};
struct texel BLU = {0, 0, 255, 255};
struct texel texture_data[100] = {
RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK,
RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK,
RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, WHT,
RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, WHT,
RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK,
RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK,
RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, WHT,
RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, WHT,
RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK,
RED, WHT, GRN, WHT, BLU, WHT, RED, WHT, GRN, BLK,
};
Texture test_texture = load_texture(context->physical_device, context->device, context->transfer_command_pool, context->queues.transfer, texture_size, 4, VK_FORMAT_R8G8B8A8_SRGB, texture_data);
for(uint32_t i = 0; i < max_frames_in_flight; i++) {
VkDescriptorImageInfo image_info = {
.sampler = test_texture.sampler,
.imageView = test_texture.view,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
};
VkWriteDescriptorSet descriptor_write = {};
descriptor_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptor_write.dstSet = TODO_sets[i];
descriptor_write.dstBinding = 0;
descriptor_write.dstArrayElement = 0;
descriptor_write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptor_write.descriptorCount = 1;
descriptor_write.pBufferInfo = 0;
descriptor_write.pImageInfo = &image_info;
descriptor_write.pTexelBufferView = 0;
vkUpdateDescriptorSets(device, 1, &descriptor_write, 0, 0);
}
return context; return context;
} }