summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmniscient <omniscient.oce@gmail.com>2024-05-20 11:07:58 +1000
committerOmniscient <omniscient.oce@gmail.com>2024-05-20 11:07:58 +1000
commit74bb80d54f48a8576a796b15ccdf044cde435e79 (patch)
tree487d57ddd05fb3b42551ee1232811d6cc8acf0d7
parente904c22003c3a134201b222e6619e782fbe63947 (diff)
use pool allocator for other gpu backend entities
-rw-r--r--src/renderer/backends/backend_vulkan.c29
-rw-r--r--src/renderer/ral.h22
-rw-r--r--src/renderer/ral_types.h3
-rw-r--r--src/std/mem.h1
4 files changed, 45 insertions, 10 deletions
diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c
index ef266e0..2f8fdf7 100644
--- a/src/renderer/backends/backend_vulkan.c
+++ b/src/renderer/backends/backend_vulkan.c
@@ -40,6 +40,7 @@ typedef struct vulkan_context {
vulkan_swapchain_support_info swapchain_support;
arena temp_arena;
+ arena pool_arena;
gpu_device* device;
gpu_swapchain* swapchain;
u32 framebuffer_count;
@@ -66,6 +67,7 @@ typedef struct vulkan_context {
size_t buffer_count;
VkDescriptorSet_darray* free_set_queue;
struct resource_pools* resource_pools;
+ gpu_backend_pools gpu_pools;
VkDebugUtilsMessengerEXT vk_debugger;
} vulkan_context;
@@ -74,6 +76,8 @@ static vulkan_context context;
// --- Function forward declarations
+void backend_pools_init(arena* a, gpu_backend_pools* backend_pools);
+
/** @brief Enumerates and selects the most appropriate graphics device */
bool select_physical_device(gpu_device* out_device);
@@ -115,6 +119,11 @@ bool gpu_backend_init(const char* window_name, GLFWwindow* window) {
size_t temp_arena_size = 1024 * 1024;
context.temp_arena = arena_create(malloc(temp_arena_size), temp_arena_size);
+ size_t pool_buffer_size = 1024 * 1024;
+ context.pool_arena = arena_create(malloc(pool_buffer_size), pool_buffer_size);
+
+ backend_pools_init(&context.pool_arena, &context.gpu_pools);
+
// Setup Vulkan instance
VkApplicationInfo app_info = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
app_info.apiVersion = VK_API_VERSION_1_2;
@@ -393,8 +402,9 @@ VkFormat format_from_vertex_attr(vertex_attrib_type attr) {
gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc description) {
// Allocate
- gpu_pipeline_layout* layout = malloc(sizeof(gpu_pipeline_layout));
- gpu_pipeline* pipeline = malloc(sizeof(gpu_pipeline));
+ gpu_pipeline_layout* layout =
+ pipeline_layout_pool_alloc(&context.gpu_pools.pipeline_layouts, NULL);
+ gpu_pipeline* pipeline = pipeline_pool_alloc(&context.gpu_pools.pipelines, NULL);
// Shaders
printf("Vertex shader: %s\n", description.vs.filepath.buf);
@@ -725,8 +735,7 @@ gpu_cmd_encoder* gpu_get_default_cmd_encoder() {
}
gpu_renderpass* gpu_renderpass_create(const gpu_renderpass_desc* description) {
- // TEMP: allocate with malloc. in the future we will have a pool allocator on the context
- gpu_renderpass* renderpass = malloc(sizeof(gpu_renderpass));
+ gpu_renderpass* renderpass = renderpass_pool_alloc(&context.gpu_pools.renderpasses, NULL);
// attachments
u32 attachment_desc_count = 2;
@@ -1687,3 +1696,15 @@ void resource_pools_init(arena* a, struct resource_pools* res_pools) {
context.resource_pools = res_pools;
}
+
+void backend_pools_init(arena* a, gpu_backend_pools* backend_pools) {
+ pipeline_layout_pool pipeline_layout_pool =
+ pipeline_layout_pool_create(a, MAX_PIPELINES, sizeof(gpu_pipeline_layout));
+ backend_pools->pipeline_layouts = pipeline_layout_pool;
+ pipeline_pool pipeline_pool = pipeline_pool_create(a, MAX_PIPELINES, sizeof(gpu_pipeline));
+ backend_pools->pipelines = pipeline_pool;
+ renderpass_pool rpass_pool = renderpass_pool_create(a, MAX_RENDERPASSES, sizeof(gpu_renderpass));
+ backend_pools->renderpasses = rpass_pool;
+
+ context.gpu_pools;
+}
diff --git a/src/renderer/ral.h b/src/renderer/ral.h
index 376898f..1ca37b4 100644
--- a/src/renderer/ral.h
+++ b/src/renderer/ral.h
@@ -34,12 +34,21 @@ typedef struct gpu_texture gpu_texture;
#define MAX_SHADER_DATA_LAYOUTS 5
#define MAX_BUFFERS 256
#define MAX_TEXTURES 256
+#define MAX_PIPELINES 128
+#define MAX_RENDERPASSES 128
TYPED_POOL(gpu_buffer, buffer);
TYPED_POOL(gpu_texture, texture);
+TYPED_POOL(gpu_pipeline_layout, pipeline_layout);
+TYPED_POOL(gpu_pipeline, pipeline);
+TYPED_POOL(gpu_renderpass, renderpass);
+
// --- Pools
-typedef struct gpu_backend_pools { /* TODO: pools for each gpu structure */
+typedef struct gpu_backend_pools {
+ pipeline_pool pipelines;
+ pipeline_layout_pool pipeline_layouts;
+ renderpass_pool renderpasses;
} gpu_backend_pools;
struct resource_pools {
@@ -47,6 +56,7 @@ struct resource_pools {
texture_pool textures;
};
+// --- Pipeline description
typedef enum pipeline_kind {
PIPELINE_GRAPHICS,
PIPELINE_COMPUTE,
@@ -111,6 +121,11 @@ void gpu_cmd_encoder_end_render(gpu_cmd_encoder* encoder);
void gpu_cmd_encoder_begin_compute();
gpu_cmd_encoder* gpu_get_default_cmd_encoder();
+/** @brief Finish recording and return a command buffer that can be submitted to a queue */
+gpu_cmd_buffer gpu_cmd_encoder_finish(gpu_cmd_encoder* encoder);
+
+void gpu_queue_submit(gpu_cmd_buffer* buffer);
+
// --- Data copy commands
/** @brief Copy data from one buffer to another */
void encode_buffer_copy(gpu_cmd_encoder* encoder, buffer_handle src, u64 src_offset,
@@ -135,11 +150,6 @@ void encode_draw(gpu_cmd_encoder* encoder);
void encode_draw_indexed(gpu_cmd_encoder* encoder, u64 index_count);
void encode_clear_buffer(gpu_cmd_encoder* encoder, buffer_handle buf);
-/** @brief Finish recording and return a command buffer that can be submitted to a queue */
-gpu_cmd_buffer gpu_cmd_encoder_finish(gpu_cmd_encoder* encoder);
-
-void gpu_queue_submit(gpu_cmd_buffer* buffer);
-
// --- Buffers
buffer_handle gpu_buffer_create(u64 size, gpu_buffer_type buf_type, gpu_buffer_flags flags,
const void* data);
diff --git a/src/renderer/ral_types.h b/src/renderer/ral_types.h
index 62a2f1d..704f2cb 100644
--- a/src/renderer/ral_types.h
+++ b/src/renderer/ral_types.h
@@ -22,6 +22,9 @@ CORE_DEFINE_HANDLE(texture_handle);
CORE_DEFINE_HANDLE(sampler_handle);
CORE_DEFINE_HANDLE(shader_handle);
CORE_DEFINE_HANDLE(model_handle);
+CORE_DEFINE_HANDLE(pipeline_layout_handle);
+CORE_DEFINE_HANDLE(pipeline_handle);
+CORE_DEFINE_HANDLE(renderpass_handle);
#define ABSENT_MODEL_HANDLE 999999999
/* #define RENDERER_TYPED_HANDLES */
diff --git a/src/std/mem.h b/src/std/mem.h
index 26da778..1d508ce 100644
--- a/src/std/mem.h
+++ b/src/std/mem.h
@@ -57,6 +57,7 @@ bool void_pool_is_full(void_pool* pool);
void* void_pool_get(void_pool* pool, u32 raw_handle);
void* void_pool_alloc(void_pool* pool, u32* out_raw_handle);
void void_pool_dealloc(void_pool* pool, u32 raw_handle);
+// TODO: fn to dealloc from the pointer that was handed out
// TODO: macro that lets us specialise