diff --git a/src/gpu_mem.c b/src/gpu_mem.c index 55e0769..cec6189 100644 --- a/src/gpu_mem.c +++ b/src/gpu_mem.c @@ -211,7 +211,7 @@ VkResult gpu_buffer_malloc(VkDevice device, GPUPage* page, VkDeviceSize size, Vk } //TODO: use real alignment size instead of hard-coded to 16 - size += size % 16; + size += (16 - size) % 16; VkBufferCreateInfo buffer_info = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, diff --git a/src/main.c b/src/main.c index 3eb5a64..14426c1 100644 --- a/src/main.c +++ b/src/main.c @@ -56,32 +56,10 @@ typedef struct PositionStruct { versor rotation; } Position; -typedef void(*MappingFunc)(void*,void*); - -void attribute_mapping_position_to_matrix(void* dest, void* source) { - Position* position = source; - glm_translate_make(dest, position->position); - glm_quat_rotate(dest, position->rotation, dest); - glm_scale(dest, position->scale); -} - -#define MAPPING_POSITION_TO_MATRIX 0 - -MappingFunc mapping_functions[] = { - attribute_mapping_position_to_matrix, -}; - -typedef struct MappingStruct { - uint32_t mapping_type; // What function to use to map it - uint32_t index; // Which index to use in the ATTRIBUTE_ID_DESCRIPTORS array -} Mapping; - #define ATTRIBUTE_ID_MESH 0x00000001 // Mesh* #define ATTRIBUTE_ID_PIPELINE 0x00000002 // GraphicsPipeline* -#define ATTRIBUTE_ID_DESCRIPTORS 0x00000003 // void***(array of array of data pointers) -#define ATTRIBUTE_ID_DESCRIPTOR_SETS 0x00000004 // VkDescriptorSet* -#define ATTRIBUTE_ID_POSITION 0x00000005 // Position* -#define ATTRIBUTE_ID_PUSH_CONSTANTS 0x00000006 // void* +#define ATTRIBUTE_ID_POSITION 0x00000003 // Position* +#define ATTRIBUTE_ID_PIPELINE_DATA 0x00000004 // void* typedef struct ObjectStruct { Map attributes; @@ -209,7 +187,7 @@ struct Vertex { }; struct SceneUBO { - mat4 test; + uint32_t test; }; struct ScenePC { @@ -2005,8 +1983,8 @@ VkResult create_simple_mesh_pipeline(VkDevice device, VkExtent2D extent, VkRende return create_graphics_pipeline(device, extent, render_pass, offscreen_render_pass, pipeline_info, max_frames_in_flight, out); } -VkResult create_texture_mesh_pipeline(VkDevice device, VkPhysicalDeviceMemoryProperties memories, VkExtent2D extent, VkRenderPass render_pass, VkRenderPass offscreen_render_pass, VkDescriptorSetLayout scene_layout, uint32_t max_frames_in_flight, VkCommandPool transfer_pool, Queue transfer_queue, Queue graphics_queue, VkCommandPool graphics_pool, GraphicsPipeline* out) { - if(out == NULL) { +VkResult create_texture_mesh_pipeline(VkDevice device, VkExtent2D extent, VkRenderPass render_pass, VkRenderPass offscreen_render_pass, VkDescriptorSetLayout scene_layout, uint32_t max_frames_in_flight, TextureSet* texture_set, GraphicsPipeline* out) { + if(out == NULL || texture_set == NULL) { return VK_ERROR_VALIDATION_FAILED_EXT; } @@ -2092,12 +2070,6 @@ VkResult create_texture_mesh_pipeline(VkDevice device, VkPhysicalDeviceMemoryPro return result; } - TextureSet* texture_set = malloc(sizeof(TextureSet)); - result = create_texture_set(device, set_layout, 1000, texture_set); - if(result != VK_SUCCESS) { - return result; - } - VkPipelineVertexInputStateCreateInfo input_info = { .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, .pVertexBindingDescriptions = bindings, @@ -2114,89 +2086,12 @@ VkResult create_texture_mesh_pipeline(VkDevice device, VkPhysicalDeviceMemoryPro .input_info = input_info, }; - GPUPage* memory = NULL; - result = gpu_page_allocate(device, memories, 100000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 0, &memory); - if(result != VK_SUCCESS) { - return result; - } - result = create_graphics_pipeline(device, extent, render_pass, offscreen_render_pass, pipeline_info, max_frames_in_flight, out); if(result != VK_SUCCESS) { return result; } - VkExtent2D texture_size = { - .width = 10, - .height = 10, - }; - (void)texture_size; - - 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_0[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, - }; - - struct texel texture_data_1[100] = { - RED, RED, RED, RED, RED, RED, RED, RED, RED, RED, - RED, WHT, WHT, WHT, WHT, WHT, WHT, WHT, WHT, RED, - RED, WHT, GRN, GRN, GRN, GRN, GRN, GRN, WHT, RED, - RED, WHT, GRN, BLU, BLU, BLU, BLU, GRN, WHT, RED, - RED, WHT, GRN, BLU, BLK, BLK, BLU, GRN, WHT, RED, - RED, WHT, GRN, BLU, BLK, BLK, BLU, GRN, WHT, RED, - RED, WHT, GRN, BLU, BLU, BLU, BLU, GRN, WHT, RED, - RED, WHT, GRN, GRN, GRN, GRN, GRN, GRN, WHT, RED, - RED, WHT, WHT, WHT, WHT, WHT, WHT, WHT, WHT, RED, - RED, RED, RED, RED, RED, RED, RED, RED, RED, RED, - }; - - GPUPage* texture_memory = NULL; - result = gpu_page_allocate(device, memories, 100000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT, 0, &texture_memory); - if(result != VK_SUCCESS) { - return result; - } - - GPUPage* staging_memory = NULL; - result = gpu_page_allocate(device, memories, 100000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 0, &staging_memory); - if(result != VK_SUCCESS) { - return result; - } - - GPUBuffer staging = {0}; - result = gpu_buffer_malloc(device, staging_memory, 100000, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, &staging); - if(result != VK_SUCCESS) { - return result; - } - - uint32_t texture_index = 0; - Texture* test_texture_0 = malloc(sizeof(Texture)); - Texture* test_texture_1 = malloc(sizeof(Texture)); - result = texture_set_add(device, texture_set, texture_memory, staging, transfer_pool, transfer_queue, graphics_pool, graphics_queue, texture_size, VK_FORMAT_R8G8B8A8_SRGB, texture_data_0, &texture_index, test_texture_0); - if(result != VK_SUCCESS) { - return result; - } - - result = texture_set_add(device, texture_set, texture_memory, staging, transfer_pool, transfer_queue, graphics_pool, graphics_queue, texture_size, VK_FORMAT_R8G8B8A8_SRGB, texture_data_1, &texture_index, test_texture_1); + result = create_texture_set(device, set_layout, 1000, texture_set); if(result != VK_SUCCESS) { return result; } @@ -3183,7 +3078,7 @@ Object create_simple_mesh_object(PlyMesh ply_mesh, GraphicsPipeline* simple_mesh return object; } -Object create_texture_mesh_object(GraphicsPipeline* texture_mesh_pipeline, VkPhysicalDeviceMemoryProperties memories, VkDevice device, VkCommandPool transfer_pool, Queue transfer_queue) { +Object create_texture_mesh_object(GraphicsPipeline* texture_mesh_pipeline, VkPhysicalDeviceMemoryProperties memories, VkDevice device, VkCommandPool transfer_pool, Queue transfer_queue, VkCommandPool graphics_pool, Queue graphics_queue, TextureSet* texture_set) { Object zero = {}; GPUPage* mesh_memory = NULL; @@ -3230,6 +3125,68 @@ Object create_texture_mesh_object(GraphicsPipeline* texture_mesh_pipeline, VkPhy return zero; } + 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, + }; + + GPUPage* texture_memory = NULL; + result = gpu_page_allocate(device, memories, 100000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT, 0, &texture_memory); + if(result != VK_SUCCESS) { + return zero; + } + + GPUPage* staging_memory = NULL; + result = gpu_page_allocate(device, memories, 100000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 0, &staging_memory); + if(result != VK_SUCCESS) { + return zero; + } + + GPUBuffer staging = {0}; + result = gpu_buffer_malloc(device, staging_memory, 100000, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, &staging); + if(result != VK_SUCCESS) { + return zero; + } + + uint32_t* texture_index = malloc(sizeof(uint32_t)); + Texture* test_texture = malloc(sizeof(Texture)); + result = texture_set_add(device, texture_set, texture_memory, staging, transfer_pool, transfer_queue, graphics_pool, graphics_queue, texture_size, VK_FORMAT_R8G8B8A8_SRGB, texture_data, texture_index, test_texture); + if(result != VK_SUCCESS) { + return zero; + } + + // TODO retrieve this value when constructing the object data buffer for rendering the scene + map_result = map_add(&object.attributes, ATTRIBUTE_ID_PIPELINE_DATA, texture_index); + if(map_result == 0) { + return zero; + } + return object; } @@ -3253,13 +3210,14 @@ void main_loop(PlyMesh ply_mesh, GLFWwindow* window, VulkanContext* context) { return; } - result = create_texture_mesh_pipeline(context->device, context->memories, context->swapchain_extent, context->render_pass, context->g_renderpass, scene.descriptor_layout, context->max_frames_in_flight, context->transfer_command_pool, context->transfer_queue, context->graphics_queue, context->extra_graphics_pool, &texture_mesh_pipeline); + TextureSet texture_set = {0}; + result = create_texture_mesh_pipeline(context->device, context->swapchain_extent, context->render_pass, context->g_renderpass, scene.descriptor_layout, context->max_frames_in_flight, &texture_set, &texture_mesh_pipeline); if(result != VK_SUCCESS) { fprintf(stderr, "failed to create texture mesh material\n"); return; } - Object triangle_object_textured = create_texture_mesh_object(&texture_mesh_pipeline, context->memories, context->device, context->transfer_command_pool, context->transfer_queue); + Object triangle_object_textured = create_texture_mesh_object(&texture_mesh_pipeline, context->memories, context->device, context->transfer_command_pool, context->transfer_queue, context->extra_graphics_pool, context->graphics_queue, &texture_set); if(triangle_object_textured.attributes.buckets == 0) { fprintf(stderr, "failed to create texture mesh object\n"); return;