diff options
author | Omniscient <omniscient.oce@gmail.com> | 2024-05-18 14:15:23 +1000 |
---|---|---|
committer | Omniscient <omniscient.oce@gmail.com> | 2024-05-18 14:15:23 +1000 |
commit | fcaf838886efc3c12f60b47c021c6a85965eb6e9 (patch) | |
tree | 9f65588a5a662b547f198635d987c50d6df26e46 | |
parent | f99fef4eb5b2c29a84d9e835a66b1afd333bcfc0 (diff) |
initialise gpu resource pools in renderer
-rw-r--r-- | assets/textures/texture.jpg | bin | 0 -> 77120 bytes | |||
-rw-r--r-- | scene.c | 1 | ||||
-rw-r--r-- | src/core.c | 4 | ||||
-rw-r--r-- | src/renderer/backends/backend_vulkan.c | 24 | ||||
-rw-r--r-- | src/renderer/ral.c | 12 | ||||
-rw-r--r-- | src/renderer/ral.h | 17 | ||||
-rw-r--r-- | src/renderer/render.c | 19 | ||||
-rw-r--r-- | src/renderer/render_types.h | 1 | ||||
-rw-r--r-- | src/scene.h | 6 |
9 files changed, 68 insertions, 16 deletions
diff --git a/assets/textures/texture.jpg b/assets/textures/texture.jpg Binary files differnew file mode 100644 index 0000000..975e338 --- /dev/null +++ b/assets/textures/texture.jpg @@ -0,0 +1 @@ +#include "scene.h" @@ -78,6 +78,4 @@ void frame_begin() { render_frame_begin(&g_core.renderer); } void frame_draw() {} -void frame_end() { - render_frame_end(&g_core.renderer); -} +void frame_end() { render_frame_end(&g_core.renderer); } diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c index 37f9b59..61527ee 100644 --- a/src/renderer/backends/backend_vulkan.c +++ b/src/renderer/backends/backend_vulkan.c @@ -65,6 +65,7 @@ typedef struct vulkan_context { gpu_buffer buffers[1024]; size_t buffer_count; VkDescriptorSet_darray* free_set_queue; + struct resource_pools* resource_pools; VkDebugUtilsMessengerEXT vk_debugger; } vulkan_context; @@ -1360,6 +1361,17 @@ void encode_buffer_copy(gpu_cmd_encoder* encoder, buffer_handle src, u64 src_off context.buffers[dst.raw].handle, 1, ©_region); } +texture_handle gpu_texture_create(texture_desc desc, const void* data) { + VkDeviceSize image_size = desc.extents.x * desc.extents.y * 4; + + TRACE("Uploading pixel data to texture using staging buffer"); + // Create a staging buffer + buffer_handle staging = + gpu_buffer_create(image_size, CEL_BUFFER_DEFAULT, CEL_BUFFER_FLAG_CPU, NULL); + // Copy data into it + buffer_upload_bytes(staging, (bytebuffer){ .buf = (u8*)data, .size = image_size }, 0, image_size); +} + size_t vertex_attrib_size(vertex_attrib_type attr) { switch (attr) { case ATTR_F32: @@ -1392,3 +1404,15 @@ void vertex_desc_add(vertex_description* builder, const char* name, vertex_attri builder->attributes_count++; } + +/* TYPED_POOL(gpu_buffer, buffer); */ +/* TYPED_POOL(gpu_texture, texture); */ + +void resource_pools_init(arena* a, struct resource_pools* res_pools) { + buffer_pool buf_pool = buffer_pool_create(a, MAX_BUFFERS, sizeof(gpu_buffer)); + res_pools->buffers = buf_pool; + texture_pool tex_pool = texture_pool_create(a, MAX_TEXTURES, sizeof(gpu_texture)); + res_pools->textures = tex_pool; + + context.resource_pools = res_pools; +} diff --git a/src/renderer/ral.c b/src/renderer/ral.c index 25c2909..6a417dd 100644 --- a/src/renderer/ral.c +++ b/src/renderer/ral.c @@ -13,10 +13,10 @@ /* } buffer_handle; */ /* typedef struct gpu_buffer gpu_buffer; */ -TYPED_POOL(gpu_buffer, buffer); -TYPED_POOL(gpu_texture, texture); +/* TYPED_POOL(gpu_buffer, buffer); */ +/* TYPED_POOL(gpu_texture, texture); */ -struct resource_pools { - buffer_pool buffers; - texture_pool textures; -}; +/* struct resource_pools { */ +/* buffer_pool buffers; */ +/* texture_pool textures; */ +/* }; */ diff --git a/src/renderer/ral.h b/src/renderer/ral.h index 03bdeab..da9eb93 100644 --- a/src/renderer/ral.h +++ b/src/renderer/ral.h @@ -32,6 +32,8 @@ typedef struct gpu_cmd_buffer gpu_cmd_buffer; // Ready for submission typedef struct gpu_buffer gpu_buffer; #define MAX_SHADER_DATA_LAYOUTS 5 +#define MAX_BUFFERS 256 +#define MAX_TEXTURES 256 /** @brief A*/ // typedef struct gpu_bind_group @@ -41,7 +43,7 @@ typedef struct gpu_backend_pools { // pools for each gpu structure } gpu_backend_pools; -typedef struct resource_pools resource_pools; +/* typedef struct resource_pools resource_pools; */ typedef enum pipeline_kind { PIPELINE_GRAPHICS, @@ -144,7 +146,7 @@ void gpu_buffer_upload(); void gpu_buffer_bind(buffer_handle buffer); // Textures -void gpu_texture_create(); +texture_handle gpu_texture_create(texture_desc desc, const void* data); void gpu_texture_destroy(); void gpu_texture_upload(); @@ -161,3 +163,14 @@ void vertex_desc_add(vertex_description* builder, const char* name, vertex_attri // TEMP void gpu_temp_draw(size_t n_verts); + +TYPED_POOL(gpu_buffer, buffer); +TYPED_POOL(gpu_texture, texture); + +struct resource_pools { + buffer_pool buffers; + texture_pool textures; +}; + +// Must be implemented by backends +void resource_pools_init(arena* a, struct resource_pools* res_pools); diff --git a/src/renderer/render.c b/src/renderer/render.c index 6eacc0d..c83678b 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -3,6 +3,7 @@ #include "camera.h" #include "file.h" #include "log.h" +#include "mem.h" #include "ral.h" #include "ral_types.h" @@ -46,6 +47,10 @@ bool renderer_init(renderer* ren) { gpu_device_create(&ren->device); // TODO: handle errors gpu_swapchain_create(&ren->swapchain); + DEBUG("Initialise GPU resource pools"); + arena pool_arena = arena_create(malloc(1024 * 1024), 1024 * 1024); + resource_pools_init(&pool_arena, ren->resource_pools); + // ren->blinn_phong = // shader_create_separate("assets/shaders/blinn_phong.vert", // "assets/shaders/blinn_phong.frag"); @@ -56,7 +61,7 @@ bool renderer_init(renderer* ren) { // default_material_init(); // Create default rendering pipeline - default_pipelines_init(ren); + /* default_pipelines_init(ren); */ return true; } @@ -75,11 +80,15 @@ void default_pipelines_init(renderer* ren) { ren->default_renderpass = *renderpass; - // str8 vert_path = str8lit("build/linux/x86_64/debug/triangle.vert.spv"); - // str8 frag_path = str8lit("build/linux/x86_64/debug/triangle.frag.spv"); printf("Load shaders\n"); - str8 vert_path = str8lit("/home/void/code/celeritas-engine/celeritas-core/build/linux/x86_64/debug/triangle.vert.spv"); - str8 frag_path = str8lit("/home/void/code/celeritas-engine/celeritas-core/build/linux/x86_64/debug/triangle.frag.spv"); + str8 vert_path = str8lit("build/linux/x86_64/debug/triangle.vert.spv"); + str8 frag_path = str8lit("build/linux/x86_64/debug/triangle.frag.spv"); + /* str8 vert_path = + * str8lit("/home/void/code/celeritas-engine/celeritas-core/build/linux/x86_64/debug/triangle.vert.spv"); + */ + /* str8 frag_path = + * str8lit("/home/void/code/celeritas-engine/celeritas-core/build/linux/x86_64/debug/triangle.frag.spv"); + */ str8_opt vertex_shader = str8_from_file(&scratch, vert_path); str8_opt fragment_shader = str8_from_file(&scratch, frag_path); if (!vertex_shader.has_value || !fragment_shader.has_value) { diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index ac98627..bd9ef3c 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -35,6 +35,7 @@ typedef struct renderer { gpu_renderpass default_renderpass; gpu_pipeline static_opaque_pipeline; bool frame_aborted; + struct resource_pools* resource_pools; } renderer; typedef struct geometry_data { diff --git a/src/scene.h b/src/scene.h index 24429b3..5a85e2e 100644 --- a/src/scene.h +++ b/src/scene.h @@ -32,7 +32,13 @@ void scene_free(scene* s); // Simplified API - no scene pointer; gets and sets global scene // Add/Remove objects from the scene + /* vec3 direction; */ + /* vec3 ambient; */ + /* vec3 diffuse; */ + /* vec3 specular; */ void scene_set_dir_light(directional_light light); +void _scene_set_dir_light(vec3 ambient, vec3 diffuse, vec3 specular, vec3 direction); + void scene_add_point_light(point_light light); void scene_add_model(model_handle model, transform3d transform); bool scene_remove_model(model_handle model); |