diff --git a/projects/apis/opengl/src/commands/buffers/opengl_command_buffer.c b/projects/apis/opengl/src/commands/buffers/opengl_command_buffer.c index 06ffba2..a23fe70 100644 --- a/projects/apis/opengl/src/commands/buffers/opengl_command_buffer.c +++ b/projects/apis/opengl/src/commands/buffers/opengl_command_buffer.c @@ -1,6 +1,22 @@ #include "opengl_command_buffer.h" +#include "commands/pool/opengl_command_pool.h" gnReturnCode openglCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* commandBuffers, uint32_t count, gnCommandPoolHandle pool) { + for (int i = 0; i < count; i++) { + gnBool wasAbleToAllocate = GN_FALSE; + for (int c = i; c < pool->commandPool->allocatedCommandBufferCount; c++) { + if (pool->commandPool->canBeReallocated[c] == GN_TRUE) { + pool->commandPool->canBeReallocated[c] = GN_FALSE; + commandBuffers[i]->commandBuffer = &pool->commandPool->commandBuffers[c]; + wasAbleToAllocate = GN_TRUE; + break; + } + } + + if (!wasAbleToAllocate) { + openglResizeCommandPool(pool); + } + } return GN_SUCCESS; } diff --git a/projects/apis/opengl/src/commands/buffers/opengl_command_buffer.h b/projects/apis/opengl/src/commands/buffers/opengl_command_buffer.h index 5b7b844..3eca201 100644 --- a/projects/apis/opengl/src/commands/buffers/opengl_command_buffer.h +++ b/projects/apis/opengl/src/commands/buffers/opengl_command_buffer.h @@ -2,6 +2,6 @@ #include "core/src/command/command_buffer/gryphn_command_buffer.h" typedef struct gnPlatformCommandBuffer_t { - + int index; } gnPlatformCommandBuffer; gnReturnCode openglCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* commandBuffers, uint32_t count, gnCommandPoolHandle pool); diff --git a/projects/apis/opengl/src/commands/pool/opengl_command_pool.c b/projects/apis/opengl/src/commands/pool/opengl_command_pool.c index 4da4ab6..b4223b0 100644 --- a/projects/apis/opengl/src/commands/pool/opengl_command_pool.c +++ b/projects/apis/opengl/src/commands/pool/opengl_command_pool.c @@ -1,6 +1,31 @@ #include "opengl_command_pool.h" +#include "stdlib.h" gnReturnCode openglCreateCommandPool(gnCommandPool commandPool, gnDevice device, gnCommandPoolInfo info) { + commandPool->commandPool = malloc(sizeof(gnPlatformCommandPool)); + + uint32_t baseCommandBufferCount = 10; + commandPool->commandPool->allocatedCommandBufferCount = baseCommandBufferCount; + commandPool->commandPool->commandBuffers = malloc(sizeof(gnPlatformCommandBuffer) * baseCommandBufferCount); + commandPool->commandPool->canBeReallocated = malloc(sizeof(gnBool) * baseCommandBufferCount); + + for (int i = 0; i < baseCommandBufferCount; i++) { + commandPool->commandPool->commandBuffers[i].index = i; + commandPool->commandPool->canBeReallocated[i] = GN_TRUE; + } + return GN_SUCCESS; } -void openglDestroyCommandPool(gnCommandPool commandPool) {} +void openglDestroyCommandPool(gnCommandPool commandPool) { + commandPool->commandPool->allocatedCommandBufferCount = 0; + free(commandPool->commandPool->commandBuffers); + free(commandPool->commandPool->canBeReallocated); + free(commandPool->commandPool); +} + + +void openglResizeCommandPool(gnCommandPoolHandle pool) { + pool->commandPool->allocatedCommandBufferCount *= 2; + pool->commandPool->commandBuffers = realloc(pool->commandPool->commandBuffers, sizeof(gnPlatformCommandBuffer) * pool->commandPool->allocatedCommandBufferCount); + pool->commandPool->canBeReallocated = realloc(pool->commandPool->canBeReallocated, sizeof(gnBool) * pool->commandPool->allocatedCommandBufferCount); +} diff --git a/projects/apis/opengl/src/commands/pool/opengl_command_pool.h b/projects/apis/opengl/src/commands/pool/opengl_command_pool.h index 7d7f76a..c1ea8d3 100644 --- a/projects/apis/opengl/src/commands/pool/opengl_command_pool.h +++ b/projects/apis/opengl/src/commands/pool/opengl_command_pool.h @@ -1,6 +1,15 @@ #pragma once #include "core/src/command/command_pool/gryphn_command_pool.h" +#include "commands/buffers/opengl_command_buffer.h" +#include "utils/gryphn_bool.h" + +typedef struct gnPlatformCommandPool_t { + uint32_t allocatedCommandBufferCount; + gnPlatformCommandBuffer* commandBuffers; + gnBool* canBeReallocated; +} gnPlatformCommandPool; -typedef struct gnPlatformCommandPool_t gnPlatformCommandPool; gnReturnCode openglCreateCommandPool(gnCommandPool commandPool, gnDevice device, gnCommandPoolInfo info); void openglDestroyCommandPool(gnCommandPool commandPool); + +void openglResizeCommandPool(gnCommandPoolHandle pool);