main
noah metz 2024-01-10 17:50:19 -07:00
parent f280041d0b
commit bf083380d7
1 changed files with 102 additions and 100 deletions

@ -2071,31 +2071,29 @@ Object create_renderable(Mesh* mesh, Material* material, uint32_t descriptor_set
return object;
}
Mesh load_simple_mesh(VkPhysicalDeviceMemoryProperties memories, VkDevice device, struct Vertex* vertices, uint32_t vertex_count, uint16_t* indices, uint32_t index_count, VkCommandPool transfer_pool, VkQueue transfer_queue) {
Mesh mesh = {};
mesh.vertex_buffer.buffer = VK_NULL_HANDLE;
mesh.vertex_buffer.memory = VK_NULL_HANDLE;
mesh.index_buffer.buffer = VK_NULL_HANDLE;
mesh.index_buffer.memory = VK_NULL_HANDLE;
Mesh* load_simple_mesh(VkPhysicalDeviceMemoryProperties memories, VkDevice device, struct Vertex* vertices, uint32_t vertex_count, uint16_t* indices, uint32_t index_count, VkCommandPool transfer_pool, VkQueue transfer_queue) {
AllocatedBuffer vertex_buffer = create_populated_buffer(memories, device, (void*)vertices, sizeof(struct Vertex) * vertex_count, transfer_pool, transfer_queue, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
if(vertex_buffer.memory == VK_NULL_HANDLE) {
return mesh;
return 0;
}
mesh.vertex_buffer = vertex_buffer;
mesh.vertex_count = vertex_count;
AllocatedBuffer index_buffer = create_populated_buffer(memories, device, (void*)indices, sizeof(uint16_t) * index_count, transfer_pool, transfer_queue, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
if(index_buffer.memory == VK_NULL_HANDLE) {
deallocate_buffer(device, vertex_buffer);
AllocatedBuffer tmp = { .memory = VK_NULL_HANDLE, .buffer = VK_NULL_HANDLE};
mesh.vertex_buffer = tmp;
return mesh;
return 0;
}
mesh.index_buffer = index_buffer;
mesh.index_count = index_count;
Mesh* mesh = malloc(sizeof(Mesh));
if(mesh == 0) {
deallocate_buffer(device, vertex_buffer);
deallocate_buffer(device, index_buffer);
return 0;
}
mesh->vertex_buffer = vertex_buffer;
mesh->vertex_count = vertex_count;
mesh->index_buffer = index_buffer;
mesh->index_count = index_count;
return mesh;
}
@ -3079,129 +3077,133 @@ VkResult draw_frame(VulkanContext* context, SceneContext* scene, uint32_t materi
return vkQueuePresentKHR(context->queues.present, &present_info);
}
void main_loop(GLFWwindow* window, VulkanContext* context) {
SceneContext scene = create_scene_context(context->device, context->memories, context->max_frames_in_flight);
if(scene.pool == VK_NULL_HANDLE) {
return;
}
Object create_simple_mesh_object(Material* simple_mesh_material, VkPhysicalDeviceMemoryProperties memories, VkDevice device, VkCommandPool transfer_pool, VkQueue transfer_queue, uint32_t max_frames_in_flight, VkDescriptorPool pool) {
Object zero = {};
Material simple_mesh_material = create_simple_mesh_material(context->device, context->swapchain_extent, context->render_pass, scene.descriptor_layout, context->max_frames_in_flight);
if(simple_mesh_material.pipeline == VK_NULL_HANDLE) {
fprintf(stderr, "failed to create simple mesh material\n");
return;
}
Mesh triangle_mesh = load_simple_mesh(context->memories, context->device, (struct Vertex*)vertices, 4, (uint16_t*)indices, 6, context->transfer_command_pool, context->queues.transfer);
if(triangle_mesh.vertex_buffer.buffer == VK_NULL_HANDLE) {
fprintf(stderr, "failed to load triangle mesh\n");
return;
}
VkDescriptorPoolSize simple_pool_sizes[] = {
{
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = context->max_frames_in_flight,
},
};
VkDescriptorPoolCreateInfo simple_pool_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.poolSizeCount = 1,
.pPoolSizes = simple_pool_sizes,
.maxSets = context->max_frames_in_flight,
};
VkDescriptorPool simple_pool;
VkResult result = vkCreateDescriptorPool(context->device, &simple_pool_info, 0, &simple_pool);
if(result != VK_SUCCESS) {
fprintf(stderr, "failed to allocate simple_pool\n");
return;
Mesh* mesh = load_simple_mesh(memories, device, (struct Vertex*)vertices, 4, (uint16_t*)indices, 6, transfer_pool, transfer_queue);
if(mesh == 0) {
return zero;
}
VkDescriptorSetLayout* simple_set_layouts = malloc(sizeof(VkDescriptorSetLayout)*context->max_frames_in_flight);
if(simple_set_layouts == 0) {
fprintf(stderr, "failed to allocate simple_set_layouts\n");
return;
VkDescriptorSetLayout* layouts = malloc(sizeof(VkDescriptorSetLayout)*max_frames_in_flight);
if(layouts == 0) {
return zero;
}
for(uint32_t i = 0; i < context->max_frames_in_flight; i++) {
simple_set_layouts[i] = simple_mesh_material.object_set_layout;
for(uint32_t i = 0; i < max_frames_in_flight; i++) {
layouts[i] = simple_mesh_material->object_set_layout;
}
VkDescriptorSetAllocateInfo simple_alloc_info = {
VkDescriptorSetAllocateInfo allocation_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pSetLayouts = simple_set_layouts,
.descriptorSetCount = context->max_frames_in_flight,
.descriptorPool = simple_pool,
.pSetLayouts = layouts,
.descriptorSetCount = max_frames_in_flight,
.descriptorPool = pool,
};
VkDescriptorSet* simple_descriptor_sets = malloc(sizeof(VkDescriptorSet)*context->max_frames_in_flight);
if(simple_descriptor_sets == 0) {
fprintf(stderr, "failed to allocate simple_descriptor_sets\n");
return;
VkDescriptorSet* sets = malloc(sizeof(VkDescriptorSet)*max_frames_in_flight);
if(sets == 0) {
return zero;
}
result = vkAllocateDescriptorSets(context->device, &simple_alloc_info, simple_descriptor_sets);
VkResult result = vkAllocateDescriptorSets(device, &allocation_info, sets);
if(result != VK_SUCCESS) {
fprintf(stderr, "failed to allocate descriptor sets from simple_pool\n");
return;
return zero;
}
Object triangle_object = create_renderable(&triangle_mesh, &simple_mesh_material, 1, simple_descriptor_sets, context->max_frames_in_flight);
if(triangle_object.attributes.buckets == 0) {
fprintf(stderr, "failed to create renderable triangle object\n");
return;
Object object = create_renderable(mesh, simple_mesh_material, 1, sets, max_frames_in_flight);
if(object.attributes.buckets == 0) {
return zero;
}
Position* simple_position = malloc(sizeof(Position));
if(simple_position == 0) {
fprintf(stderr, "failed to allocate simple_position\n");
return;
Position* position = malloc(sizeof(Position));
if(position == 0) {
return zero;
}
glm_quat_identity(simple_position->rotation);
glm_vec3_adds(simple_position->scale, 1.0f, simple_position->scale);
simple_position->position[0] = 0.0f;
simple_position->position[1] = 0.0f;
simple_position->position[2] = 2.0f;
bool map_result = map_add(&triangle_object.attributes, ATTRIBUTE_ID_POSITION, simple_position);
glm_quat_identity(position->rotation);
glm_vec3_adds(position->scale, 1.0f, position->scale);
position->position[0] = 0.0f;
position->position[1] = 0.0f;
position->position[2] = 2.0f;
bool map_result = map_add(&object.attributes, ATTRIBUTE_ID_POSITION, position);
if(map_result == 0) {
return;
return zero;
}
AllocatedBuffer* simple_position_buffers = allocate_buffers(context->memories, context->device, sizeof(struct ModelUBO), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, context->max_frames_in_flight);
if(simple_position_buffers == 0) {
fprintf(stderr, "failed to allocate simple_position_buffers\n");
return;
AllocatedBuffer* position_buffers = allocate_buffers(memories, device, sizeof(struct ModelUBO), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, max_frames_in_flight);
if(position_buffers == 0) {
return zero;
}
MaybeValue maybe_simple_descriptors = map_lookup(triangle_object.attributes, ATTRIBUTE_ID_DESCRIPTORS);
if(maybe_simple_descriptors.has_value == false) {
fprintf(stderr, "didn't find ATTRIBUTE_ID_DESCRIPTORS\n");
return;
MaybeValue maybe_ptrs = map_lookup(object.attributes, ATTRIBUTE_ID_DESCRIPTORS);
if(maybe_ptrs.has_value == false) {
return zero;
}
void*** simple_descriptors = maybe_simple_descriptors.value;
for(uint32_t i = 0; i < context->max_frames_in_flight; i++) {
VkResult result = vkMapMemory(context->device, simple_position_buffers[i].memory, 0, sizeof(struct ModelUBO), 0, &simple_descriptors[i][0]);
void*** ptrs = maybe_ptrs.value;
for(uint32_t i = 0; i < max_frames_in_flight; i++) {
VkResult result = vkMapMemory(device, position_buffers[i].memory, 0, sizeof(struct ModelUBO), 0, &ptrs[i][0]);
if(result != VK_SUCCESS) {
return;
return zero;
}
VkDescriptorBufferInfo buffer_info = {
.buffer = simple_position_buffers[i].buffer,
.buffer = position_buffers[i].buffer,
.offset = 0,
.range = sizeof(struct ModelUBO),
};
VkWriteDescriptorSet write_info = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = simple_descriptor_sets[i],
.dstSet = sets[i],
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.pBufferInfo = &buffer_info,
};
vkUpdateDescriptorSets(context->device, 1, &write_info, 0, 0);
vkUpdateDescriptorSets(device, 1, &write_info, 0, 0);
}
return object;
}
void main_loop(GLFWwindow* window, VulkanContext* context) {
SceneContext scene = create_scene_context(context->device, context->memories, context->max_frames_in_flight);
if(scene.pool == VK_NULL_HANDLE) {
return;
}
Material simple_mesh_material = create_simple_mesh_material(context->device, context->swapchain_extent, context->render_pass, scene.descriptor_layout, context->max_frames_in_flight);
if(simple_mesh_material.pipeline == VK_NULL_HANDLE) {
fprintf(stderr, "failed to create simple mesh material\n");
return;
}
VkDescriptorPoolSize simple_pool_sizes[] = {
{
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = context->max_frames_in_flight,
},
};
VkDescriptorPoolCreateInfo simple_pool_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.poolSizeCount = 1,
.pPoolSizes = simple_pool_sizes,
.maxSets = context->max_frames_in_flight,
};
VkDescriptorPool simple_pool;
VkResult result = vkCreateDescriptorPool(context->device, &simple_pool_info, 0, &simple_pool);
if(result != VK_SUCCESS) {
fprintf(stderr, "failed to allocate simple_pool\n");
return;
}
Object triangle_object = create_simple_mesh_object(&simple_mesh_material, context->memories, context->device, context->transfer_command_pool, context->queues.transfer, context->max_frames_in_flight, simple_pool);
if(triangle_object.attributes.buckets == 0) {
fprintf(stderr, "failed to create simple mesh object\n");
return;
}
Material texture_mesh_material = create_texture_mesh_material(context->device, context->swapchain_extent, context->render_pass, scene.descriptor_layout, context->max_frames_in_flight);
@ -3287,7 +3289,7 @@ void main_loop(GLFWwindow* window, VulkanContext* context) {
position->position[0] = 0.0f;
position->position[1] = 0.0f;
position->position[2] = 1.0f;
map_result = map_add(&triangle_object_textured.attributes, ATTRIBUTE_ID_POSITION, position);
bool map_result = map_add(&triangle_object_textured.attributes, ATTRIBUTE_ID_POSITION, position);
if(map_result == 0) {
return;
}