diff --git a/src/main.c b/src/main.c index 633ef71..7048a42 100644 --- a/src/main.c +++ b/src/main.c @@ -207,9 +207,74 @@ void map_destroy(Map map) { free(map.bucket_usage); } -#define ATTRIBUTE_ID_MESH 0x00000001 -#define ATTRIBUTE_ID_MATERIAL 0x00000002 -#define ATTRIBUTE_ID_DESCRIPTOR 0x00000003 +typedef struct MapIteratorStruct { + uint32_t count; + uint32_t* keys; + void** vals; +} MapIterator; + +MapIterator map_iterator_create(Map map) { + MapIterator iterator = { + .keys = 0, + .vals = 0, + .count = 0, + }; + + uint32_t count = 0; + for(uint32_t i = 0; i < map.buckets_count; i++) { + count += map.bucket_usage[i]; + } + + uint32_t* keys = malloc(sizeof(uint32_t)*count); + if(keys == 0) { + return iterator; + } + + void** vals = malloc(sizeof(void*)*count); + if(vals == 0) { + free(keys); + return iterator; + } + + uint32_t idx = 0; + for(uint32_t i = 0; i < map.buckets_count; i++) { + for(uint32_t j = 0; j < map.bucket_usage[i]; j++) { + keys[idx] = map.buckets[i][j].key; + vals[idx] = map.buckets[i][j].value; + idx += 1; + } + } + + iterator.keys = keys; + iterator.vals = vals; + iterator.count = count; + + return iterator; +} + +void map_iterator_free(MapIterator iterator) { + if(iterator.count > 0) { + free(iterator.keys); + free(iterator.vals); + } +} + +typedef struct PositionStruct { + vec4 position; + versor rotation; +} Position; + +typedef struct AttributeMappingStruct { + uint32_t attribute_id; // Which attribute to map + uint32_t mapping_type; // What function to use to map it + uint32_t binding; // Which index to use in the ATTRIBUTE_ID_DESTRO +} AttributeMapping; + +#define ATTRIBUTE_ID_MESH 0x00000001 +#define ATTRIBUTE_ID_MATERIAL 0x00000002 +#define ATTRIBUTE_ID_DESCRIPTORS 0x00000003 +#define ATTRIBUTE_ID_DESCRIPTOR_SETS 0x00000004 +#define ATTRIBUTE_ID_POSITION 0x00000005 typedef struct ObjectStruct { Map attributes; @@ -1751,7 +1816,7 @@ void command_draw_object(Material material, Object object, uint32_t frame_num, V 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); + MaybeValue maybe_descriptors = map_lookup(object.attributes, ATTRIBUTE_ID_DESCRIPTOR_SETS); if(maybe_descriptors.has_value == false) { return; } @@ -1937,7 +2002,7 @@ Object create_object() { return ret; } -Object create_renderable(Mesh* mesh, Material* material) { +Object create_renderable(Mesh* mesh, Material* material, VkDescriptorSet* descriptor_sets) { Object zero = { .attributes = { .buckets = 0, @@ -1965,6 +2030,12 @@ Object create_renderable(Mesh* mesh, Material* material) { return zero; } + result = map_add(&attributes, ATTRIBUTE_ID_DESCRIPTOR_SETS, descriptor_sets); + if(result == false) { + map_destroy(attributes); + return zero; + } + Object ret = { .attributes = attributes, }; @@ -2576,7 +2647,7 @@ VulkanContext* init_vulkan(GLFWwindow* window, uint32_t max_frames_in_flight) { context->triangle_mesh = triangle_mesh; } - Object triangle_object = create_renderable(&context->triangle_mesh, &context->simple_mesh_material); + Object triangle_object = create_renderable(&context->triangle_mesh, &context->simple_mesh_material, 0); if(triangle_object.attributes.buckets == 0) { fprintf(stderr, "failed to create renderable triangle object\n"); return 0; @@ -2600,14 +2671,6 @@ VulkanContext* init_vulkan(GLFWwindow* window, uint32_t max_frames_in_flight) { context->triangle_mesh_textured = triangle_mesh_textured; } - Object triangle_object_textured = create_renderable(&context->triangle_mesh_textured, &context->texture_mesh_material); - if(triangle_object_textured.attributes.buckets == 0) { - fprintf(stderr, "failed to create renderable textured triangle object\n"); - return 0; - } else { - context->triangle_object_textured = triangle_object_textured; - } - VkDescriptorPoolSize TODO_sizes[] = { { .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, @@ -2658,9 +2721,12 @@ VulkanContext* init_vulkan(GLFWwindow* window, uint32_t max_frames_in_flight) { 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"); + Object triangle_object_textured = create_renderable(&context->triangle_mesh_textured, &context->texture_mesh_material, TODO_sets); + if(triangle_object_textured.attributes.buckets == 0) { + fprintf(stderr, "failed to create renderable textured triangle object\n"); + return 0; + } else { + context->triangle_object_textured = triangle_object_textured; } VkExtent2D texture_size = { @@ -2853,7 +2919,7 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod vec3 world_position = {0.0f, 0.0f, 0.0f}; versor world_rotation = {-1.0f, 0.0f, 0.0f, 0.0f}; -VkResult update_scene_ubo(void** buffers, uint32_t frame_index, vec3 world_position, versor world_rotation, float aspect_ratio, float time_delta) { +VkResult update_scene_descriptor(void** buffers, uint32_t frame_index, vec3 world_position, versor world_rotation, float aspect_ratio, float time_delta) { vec3 movement_sum = {0.0f, 0.0f, 0.0f}; if(key_flags.forward) { @@ -2865,37 +2931,37 @@ VkResult update_scene_ubo(void** buffers, uint32_t frame_index, vec3 world_posit } if(key_flags.left) { - movement_sum[0] -= 1 * time_delta; + movement_sum[0] += 1 * time_delta; } if(key_flags.right) { - movement_sum[0] += 1 * time_delta; + movement_sum[0] -= 1 * time_delta; } if(key_flags.up) { - movement_sum[1] -= 1 * time_delta; + movement_sum[1] += 1 * time_delta; } if(key_flags.down) { - movement_sum[1] += 1 * time_delta; + movement_sum[1] -= 1 * time_delta; } vec3 eular_rotation = {0.0f, 0.0f, 0.0f}; if(key_flags.turn_right) { - eular_rotation[0] += 1 * time_delta; + eular_rotation[0] -= 1 * time_delta; } if(key_flags.turn_left) { - eular_rotation[0] -= 1 * time_delta; + eular_rotation[0] += 1 * time_delta; } if(key_flags.turn_up) { - eular_rotation[1] -= 1 * time_delta; + eular_rotation[1] += 1 * time_delta; } if(key_flags.turn_down) { - eular_rotation[1] += 1 * time_delta; + eular_rotation[1] -= 1 * time_delta; } if(key_flags.roll_right) { @@ -2932,7 +2998,7 @@ VkResult update_scene_ubo(void** buffers, uint32_t frame_index, vec3 world_posit struct SceneUBO ubo = {}; - glm_perspective(90.0f, aspect_ratio, 0.1, 100, ubo.proj); + glm_perspective(100.0f, aspect_ratio, 0.01, 100, ubo.proj); glm_quat_look(world_position, world_rotation, ubo.view); memcpy(buffers[frame_index], (void*)&ubo, sizeof(ubo)); @@ -2941,7 +3007,7 @@ VkResult update_scene_ubo(void** buffers, uint32_t frame_index, vec3 world_posit } VkResult draw_frame(VulkanContext* context) { - update_scene_ubo(context->scene_ubo_ptrs, context->current_frame, world_position, world_rotation, (float)context->swapchain_extent.width/(float)context->swapchain_extent.height, 0.01); + update_scene_descriptor(context->scene_ubo_ptrs, context->current_frame, world_position, world_rotation, (float)context->swapchain_extent.width/(float)context->swapchain_extent.height, 0.01); VkResult result; result = vkWaitForFences(context->device, 1, &context->in_flight_fences[context->current_frame], VK_TRUE, UINT64_MAX);