Added descriptor attribute for object descriptor set. Crash is currently due to the descriptors for the image being uninitialized.

main
noah metz 2024-01-09 21:56:56 -07:00
parent c02728c4f3
commit c337e0f728
1 changed files with 73 additions and 5 deletions

@ -209,6 +209,7 @@ void map_destroy(Map map) {
#define ATTRIBUTE_ID_MESH 0x00000001 #define ATTRIBUTE_ID_MESH 0x00000001
#define ATTRIBUTE_ID_MATERIAL 0x00000002 #define ATTRIBUTE_ID_MATERIAL 0x00000002
#define ATTRIBUTE_ID_DESCRIPTOR 0x00000003
typedef struct ObjectStruct { typedef struct ObjectStruct {
Map attributes; Map attributes;
@ -1695,7 +1696,7 @@ VkResult recreate_swap_chain(VulkanContext* context, VkExtent2D new_extent) {
return VK_SUCCESS; return VK_SUCCESS;
} }
void command_draw_mesh(Object object, VkCommandBuffer command_buffer) { void command_draw_object(Material material, Object object, uint32_t frame_num, VkCommandBuffer command_buffer) {
MaybeValue maybe_mesh = map_lookup(object.attributes, ATTRIBUTE_ID_MESH); MaybeValue maybe_mesh = map_lookup(object.attributes, ATTRIBUTE_ID_MESH);
if(maybe_mesh.has_value == false) { if(maybe_mesh.has_value == false) {
return; return;
@ -1709,6 +1710,17 @@ void command_draw_mesh(Object object, VkCommandBuffer command_buffer) {
vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, offsets); vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, offsets);
vkCmdBindIndexBuffer(command_buffer, mesh->index_buffer.buffer, 0, VK_INDEX_TYPE_UINT16); vkCmdBindIndexBuffer(command_buffer, mesh->index_buffer.buffer, 0, VK_INDEX_TYPE_UINT16);
if(material.mesh_set_layout != VK_NULL_HANDLE) {
MaybeValue maybe_descriptors = map_lookup(object.attributes, ATTRIBUTE_ID_DESCRIPTOR);
if(maybe_descriptors.has_value == false) {
return;
}
VkDescriptorSet* descriptor_sets = maybe_descriptors.value;
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, material.layout, 2, 1, &descriptor_sets[frame_num], 0, 0);
}
vkCmdDrawIndexed(command_buffer, mesh->index_count, 1, 0, 0, 0); vkCmdDrawIndexed(command_buffer, mesh->index_count, 1, 0, 0, 0);
} }
@ -1719,7 +1731,7 @@ void command_draw_material(Material material, uint32_t mesh_count, Object* objec
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, material.layout, 1, 1, &material.material_descriptors[frame_num], 0, 0); vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, material.layout, 1, 1, &material.material_descriptors[frame_num], 0, 0);
} }
for(uint32_t i = 0; i < mesh_count; i++) { for(uint32_t i = 0; i < mesh_count; i++) {
command_draw_mesh(objects[i], command_buffer); command_draw_object(material, objects[i], frame_num, command_buffer);
} }
} }
@ -2198,7 +2210,7 @@ Material create_texture_mesh_material(VkDevice device, VkExtent2D extent, VkRend
VkDescriptorSetLayoutBinding mesh_set_bindings[] = { VkDescriptorSetLayoutBinding mesh_set_bindings[] = {
{ {
.binding = 1, .binding = 0,
.descriptorCount = 1, .descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.pImmutableSamplers = 0, .pImmutableSamplers = 0,
@ -2566,6 +2578,62 @@ VulkanContext* init_vulkan(GLFWwindow* window, uint32_t max_frames_in_flight) {
context->triangle_object_textured = triangle_object_textured; context->triangle_object_textured = triangle_object_textured;
} }
VkDescriptorPoolSize TODO_sizes[] = {
{
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = max_frames_in_flight,
},
};
VkDescriptorPoolCreateInfo TODO_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.poolSizeCount = 1,
.maxSets = max_frames_in_flight,
.pPoolSizes = TODO_sizes,
};
VkDescriptorPool TODO_pool;
result = vkCreateDescriptorPool(context->device, &TODO_info, 0, &TODO_pool);
if(result != VK_SUCCESS) {
fprintf(stderr, "failed to create temporary descriptor pool\n");
return 0;
}
VkDescriptorSetLayout* TODO_layouts = malloc(sizeof(VkDescriptorSetLayout)*max_frames_in_flight);
if(TODO_layouts == 0) {
fprintf(stderr, "failed to allocate temp buffer\n");
return 0;
}
for(uint32_t i = 0; i < max_frames_in_flight; i++) {
TODO_layouts[i] = texture_mesh_material.mesh_set_layout;
}
VkDescriptorSetAllocateInfo TODO_alloc = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = TODO_pool,
.descriptorSetCount = max_frames_in_flight,
.pSetLayouts = TODO_layouts,
};
VkDescriptorSet* TODO_sets = malloc(sizeof(VkDescriptorSet)*max_frames_in_flight);
if(TODO_sets == 0) {
fprintf(stderr, "failed to allocate temp buffer\n");
return 0;
}
result = vkAllocateDescriptorSets(device, &TODO_alloc, TODO_sets);
if(result != VK_SUCCESS) {
fprintf(stderr, "failed to allocate TODO descriptors\n");
return 0;
}
bool add_result = map_add(&context->triangle_object_textured.attributes, ATTRIBUTE_ID_DESCRIPTOR, TODO_sets);
if(add_result == false) {
fprintf(stderr, "failed to add texture to renderable triangle object\n");
}
return context; return context;
} }