diff --git a/src/main.c b/src/main.c index 178bce5..a6b48cb 100644 --- a/src/main.c +++ b/src/main.c @@ -207,8 +207,9 @@ void map_destroy(Map map) { free(map.bucket_usage); } -#define ATTRIBUTE_ID_MESH 0x00000001 -#define ATTRIBUTE_ID_MATERIAL 0x00000002 +#define ATTRIBUTE_ID_MESH 0x00000001 +#define ATTRIBUTE_ID_MATERIAL 0x00000002 +#define ATTRIBUTE_ID_DESCRIPTOR 0x00000003 typedef struct ObjectStruct { Map attributes; @@ -1695,7 +1696,7 @@ VkResult recreate_swap_chain(VulkanContext* context, VkExtent2D new_extent) { 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); if(maybe_mesh.has_value == false) { return; @@ -1709,6 +1710,17 @@ void command_draw_mesh(Object object, VkCommandBuffer command_buffer) { vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, offsets); 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); } @@ -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); } 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[] = { { - .binding = 1, + .binding = 0, .descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImmutableSamplers = 0, @@ -2566,6 +2578,62 @@ VulkanContext* init_vulkan(GLFWwindow* window, uint32_t max_frames_in_flight) { 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; }