Moved object creation to use gpu_mem

main
noah metz 2024-01-12 19:58:32 -07:00
parent e987302723
commit 2decbe0e66
1 changed files with 25 additions and 57 deletions

@ -3189,7 +3189,7 @@ Object create_simple_mesh_object(Material* simple_mesh_material, VkPhysicalDevic
} }
GPUPage* transfer_memory = NULL; GPUPage* transfer_memory = NULL;
result = gpu_page_allocate(device, memories, 10000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &transfer_memory); result = gpu_page_allocate(device, memories, 100000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &transfer_memory);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return zero; return zero;
} }
@ -3252,11 +3252,18 @@ Object create_simple_mesh_object(Material* simple_mesh_material, VkPhysicalDevic
return zero; return zero;
} }
AllocatedBuffer* position_buffers = allocate_buffers(memories, device, sizeof(struct ModelUBO), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, max_frames_in_flight, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); GPUBuffer* position_buffers = malloc(sizeof(GPUBuffer)*max_frames_in_flight);
if(position_buffers == 0) { if(position_buffers == NULL) {
return zero; return zero;
} }
for(uint32_t i = 0; i < max_frames_in_flight; i++) {
result = gpu_buffer_malloc(device, transfer_memory, sizeof(struct ModelUBO), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, &position_buffers[i]);
if(result != VK_SUCCESS) {
return zero;
}
}
MaybeValue maybe_ptrs = map_lookup(object.attributes, ATTRIBUTE_ID_DESCRIPTORS); MaybeValue maybe_ptrs = map_lookup(object.attributes, ATTRIBUTE_ID_DESCRIPTORS);
if(maybe_ptrs.has_value == false) { if(maybe_ptrs.has_value == false) {
return zero; return zero;
@ -3264,13 +3271,9 @@ Object create_simple_mesh_object(Material* simple_mesh_material, VkPhysicalDevic
void*** ptrs = maybe_ptrs.value; void*** ptrs = maybe_ptrs.value;
for(uint32_t i = 0; i < max_frames_in_flight; i++) { 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]); *ptrs[i] = position_buffers[i].page->ptr + position_buffers[i].memory->offset;
if(result != VK_SUCCESS) {
return zero;
}
VkDescriptorBufferInfo buffer_info = { VkDescriptorBufferInfo buffer_info = {
.buffer = position_buffers[i].buffer, .buffer = position_buffers[i].handle,
.offset = 0, .offset = 0,
.range = sizeof(struct ModelUBO), .range = sizeof(struct ModelUBO),
}; };
@ -3295,13 +3298,13 @@ Object create_texture_mesh_object(Material* texture_mesh_material, VkPhysicalDev
Object zero = {}; Object zero = {};
GPUPage* mesh_memory = NULL; GPUPage* mesh_memory = NULL;
VkResult result = gpu_page_allocate(device, memories, 10000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT, &mesh_memory); VkResult result = gpu_page_allocate(device, memories, 100000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT, &mesh_memory);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return zero; return zero;
} }
GPUPage* transfer_memory = NULL; GPUPage* transfer_memory = NULL;
result = gpu_page_allocate(device, memories, 10000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &transfer_memory); result = gpu_page_allocate(device, memories, 100000, 0xFFFFFFFF, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &transfer_memory);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return zero; return zero;
} }
@ -3364,11 +3367,18 @@ Object create_texture_mesh_object(Material* texture_mesh_material, VkPhysicalDev
return zero; return zero;
} }
AllocatedBuffer* ubos = allocate_buffers(memories, device, sizeof(struct ModelUBO), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, max_frames_in_flight, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); GPUBuffer* position_buffers = malloc(sizeof(GPUBuffer)*max_frames_in_flight);
if(ubos == 0) { if(position_buffers == NULL) {
return zero; return zero;
} }
for(uint32_t i = 0; i < max_frames_in_flight; i++) {
result = gpu_buffer_malloc(device, transfer_memory, sizeof(struct ModelUBO), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, &position_buffers[i]);
if(result != VK_SUCCESS) {
return zero;
}
}
MaybeValue maybe_ptrs = map_lookup(object.attributes, ATTRIBUTE_ID_DESCRIPTORS); MaybeValue maybe_ptrs = map_lookup(object.attributes, ATTRIBUTE_ID_DESCRIPTORS);
if(maybe_ptrs.has_value == false) { if(maybe_ptrs.has_value == false) {
return zero; return zero;
@ -3377,13 +3387,10 @@ Object create_texture_mesh_object(Material* texture_mesh_material, VkPhysicalDev
void*** ptrs = maybe_ptrs.value; void*** ptrs = maybe_ptrs.value;
for(uint32_t i = 0; i < max_frames_in_flight; i++) { for(uint32_t i = 0; i < max_frames_in_flight; i++) {
VkResult result = vkMapMemory(device, ubos[i].memory, 0, sizeof(struct ModelUBO), 0, &ptrs[i][0]); *ptrs[i] = position_buffers[i].page->ptr + position_buffers[i].memory->offset;
if(result != VK_SUCCESS) {
return zero;
}
VkDescriptorBufferInfo buffer_info = { VkDescriptorBufferInfo buffer_info = {
.buffer = ubos[i].buffer, .buffer = position_buffers[i].handle,
.offset = 0, .offset = 0,
.range = sizeof(struct ModelUBO), .range = sizeof(struct ModelUBO),
}; };
@ -3609,45 +3616,6 @@ int main() {
return 2; return 2;
} }
GPUPage* page = NULL;
VkResult result = gpu_page_allocate(context->device, context->memories, 500, 0xFFFFFFFF, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT, &page);
if(result != VK_SUCCESS) {
return -1;
}
GPUBuffer buffers[10] = {0};
for(int i = 0; i < 10; i++) {
result = gpu_buffer_malloc(context->device, page, 100, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, &buffers[i]);
if(result != VK_SUCCESS) {
fprintf(stderr, "gpu_malloc error: %s\n", string_VkResult(result));
} else {
fprintf(stderr, "gpu_malloc: %p@%llu\n", buffers[i].handle, buffers[i].memory->offset);
fprintchunks(stderr, page->allocated);
fprintchunks(stderr, page->free);
}
}
int test[] = {3, 0, 2, 4, 1};
for(size_t i = 0; i < (sizeof(test)/sizeof(int)); i++) {
int idx = test[i];
fprintf(stderr, "freeing %llu@%llu\n", buffers[idx].memory->size, buffers[idx].memory->offset);
gpu_buffer_free(context->device, buffers[idx]);
fprintchunks(stderr, page->free);
}
for(int i = 0; i < 10; i++) {
result = gpu_buffer_malloc(context->device, page, 100, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, &buffers[i]);
if(result != VK_SUCCESS) {
fprintf(stderr, "gpu_malloc error: %s\n", string_VkResult(result));
} else {
fprintf(stderr, "gpu_malloc: %p@%llu\n", buffers[i].handle, buffers[i].memory->offset);
fprintchunks(stderr, page->allocated);
fprintchunks(stderr, page->free);
}
}
gpu_page_free(context->device, page);
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
main_loop(window, context); main_loop(window, context);