diff options
Diffstat (limited to 'src/renderer/cleanroom')
-rw-r--r-- | src/renderer/cleanroom/backend_vulkan.c | 65 | ||||
-rw-r--r-- | src/renderer/cleanroom/backend_vulkan.h | 27 | ||||
-rw-r--r-- | src/renderer/cleanroom/ral.h | 86 | ||||
-rw-r--r-- | src/renderer/cleanroom/renderer.c | 4 | ||||
-rw-r--r-- | src/renderer/cleanroom/renderer.h | 14 | ||||
-rw-r--r-- | src/renderer/cleanroom/simda.h | 18 | ||||
-rw-r--r-- | src/renderer/cleanroom/types.h | 120 |
7 files changed, 265 insertions, 69 deletions
diff --git a/src/renderer/cleanroom/backend_vulkan.c b/src/renderer/cleanroom/backend_vulkan.c new file mode 100644 index 0000000..71a09f3 --- /dev/null +++ b/src/renderer/cleanroom/backend_vulkan.c @@ -0,0 +1,65 @@ +#include <stdlib.h> +#include "ral.h" +#include "types.h" +// #include "render_types.h" + +#define VULKAN_QUEUES_COUNT 2 +const char* queue_names[VULKAN_QUEUES_COUNT] = { "GRAPHICS", "TRANSFER" }; + +typedef struct gpu_device { +} gpu_device; + +typedef struct vulkan_context { + gpu_device device; + + VkInstance instance; + +} vulkan_context; + +static vulkan_context context; + +static bool select_physical_device(gpu_device* out_device) {} + +bool gpu_device_create(gpu_device* out_device) { + // Physical device + if (!select_physical_device(out_device)) { + return false; + } + INFO("Physical device selected"); + + // Logical device + VkDeviceQueueCreateInfo queue_create_info[2]; + //.. + VkDeviceCreateInfo device_create_info = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO }; + + VkResult result = vkCreateDevice(); + if (result != VK_SUCCESS) { + FATAL("Error creating logical device with status %u\n", result); + exit(1); + } + INFO("Logical device created"); + + // Queues + + // Create the command pool +} + +gpu_renderpass* gpu_renderpass_create() { + // Allocate it + // sets everything up + // return pointer to it +} + +void encode_set_pipeline(gpu_cmd_encoder* encoder, pipeline_type kind, gpu_pipeline* pipeline) { + // VK_PIPELINE_BIND_POINT_GRAPHICS, &shader->pipeline); + if (kind == PIPELINE_GRAPHICS) { + // ... + } else { + // ... + } +} + +// --- Drawing +inline void encode_draw_indexed(gpu_cmd_encoder* encoder, u64 index_count) { + vkCmdDrawIndexed(encoder->cmd_buffer, index_count, 1, 0, 0, 0); +}
\ No newline at end of file diff --git a/src/renderer/cleanroom/backend_vulkan.h b/src/renderer/cleanroom/backend_vulkan.h new file mode 100644 index 0000000..6798b13 --- /dev/null +++ b/src/renderer/cleanroom/backend_vulkan.h @@ -0,0 +1,27 @@ +#pragma once +#include "cleanroom/ral.h" + +#define GPU_SWAPCHAIN_IMG_COUNT 2 + +typedef struct gpu_swapchain {} gpu_swapchain; +typedef struct gpu_device { + // In Vulkan we store both physical and logical device here + VkPhysicalDevice physical_device; + VkDevice logical_device; + VkPhysicalDeviceProperties properties; + VkPhysicalDeviceFeatures features; + VkPhysicalDeviceMemoryProperties memory; + VkCommandPool pool; +} gpu_device; +typedef struct gpu_pipeline {} gpu_pipeline; + +typedef struct gpu_renderpass { + VkRenderPass vk_handle; + VkFramebuffer framebuffers[GPU_SWAPCHAIN_IMG_COUNT]; + u32 +} gpu_renderpass; + + +typedef struct gpu_cmd_encoder { + VkCommandBuffer cmd_buffer; +} gpu_cmd_encoder;
\ No newline at end of file diff --git a/src/renderer/cleanroom/ral.h b/src/renderer/cleanroom/ral.h new file mode 100644 index 0000000..a1e9929 --- /dev/null +++ b/src/renderer/cleanroom/ral.h @@ -0,0 +1,86 @@ +/** + * @file ral.h + * @author your name (you@domain.com) + * @brief Render Abstraction Layer + * @details API that a graphics backend *must* implement + * @version 0.1 + * @date 2024-03-31 + * + * @copyright Copyright (c) 2024 + * + */ +#pragma once + +#include "cleanroom/types.h" +#include "defines.h" + +// Forward declare structs +typedef struct gpu_swapchain gpu_swapchain; +typedef struct gpu_device gpu_device; +typedef struct gpu_pipeline gpu_pipeline; +typedef struct gpu_renderpass gpu_renderpass; +typedef struct gpu_cmd_encoder gpu_cmd_encoder; // Recording +typedef struct gpu_cmd_buffer gpu_cmd_buffer; // Ready for submission + +enum pipeline_kind { + GRAPHICS, + COMPUTE, +} pipeline_kind; + +typedef struct shader_desc { + const char* debug_name; + str8 filepath; // where it came from + str8 glsl; // contents +} shader_desc; + +struct pipeline_desc { + shader_desc vs; /** @brief Vertex shader stage */ + shader_desc fs; /** @brief Fragment shader stage */ +}; + +// lifecycle functions +gpu_device* gpu_device_create(); +void gpu_device_destroy(); + +gpu_renderpass* gpu_renderpass_create(); +void gpu_renderpass_destroy(gpu_renderpass* pass); + +gpu_pipeline* gpu_pipeline_create(enum pipeline_kind kind, struct pipeline_desc description); +void gpu_pipeline_destroy(gpu_pipeline* pipeline); + +void gpu_cmd_encoder_begin(); +void gpu_cmd_encoder_begin_render(); +void gpu_cmd_encoder_begin_compute(); + +/* Actual commands that we can encode */ +void encode_buffer_copy(gpu_cmd_encoder* encoder, buffer_handle src, u64 src_offset, + buffer_handle dst, u64 dst_offset, u64 copy_size); +void encode_clear_buffer(gpu_cmd_encoder* encoder, buffer_handle buf); +void encode_set_pipeline(gpu_cmd_encoder* encoder, gpu_pipeline* pipeline); +// render pass +void encode_set_vertex_buffer(gpu_cmd_encoder* encoder, buffer_handle buf); +void encode_set_index_buffer(gpu_cmd_encoder* encoder, buffer_handle buf); +void encode_set_bind_group(); +void encode_draw(gpu_cmd_encoder* encoder); +void encode_draw_indexed(gpu_cmd_encoder* encoder, u64 index_count); + +// FUTURE: compute passes + +/** @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 +void gpu_buffer_create(u64 size); +void gpu_buffer_destroy(buffer_handle buffer); +void gpu_buffer_upload(); +void gpu_buffer_bind(buffer_handle buffer); + +// Textures +void gpu_texture_create(); +void gpu_texture_destroy(); +void gpu_texture_upload(); + +// Samplers +void gpu_sampler_create();
\ No newline at end of file diff --git a/src/renderer/cleanroom/renderer.c b/src/renderer/cleanroom/renderer.c new file mode 100644 index 0000000..a874664 --- /dev/null +++ b/src/renderer/cleanroom/renderer.c @@ -0,0 +1,4 @@ +#include "defines.h" +#include "render_types.h" + +bool renderer_init() {}
\ No newline at end of file diff --git a/src/renderer/cleanroom/renderer.h b/src/renderer/cleanroom/renderer.h new file mode 100644 index 0000000..8012b49 --- /dev/null +++ b/src/renderer/cleanroom/renderer.h @@ -0,0 +1,14 @@ +#pragma once + +#include "cleanroom/ral.h" +#include "cleanroom/backend_vulkan.h" + +typedef struct renderer2 { + void* backend_state; + gpu_device* device; + gpu_pipeline* static_opaque_pipeline; +} renderer2; + +// mesh +// model +// material
\ No newline at end of file diff --git a/src/renderer/cleanroom/simda.h b/src/renderer/cleanroom/simda.h new file mode 100644 index 0000000..d0b4794 --- /dev/null +++ b/src/renderer/cleanroom/simda.h @@ -0,0 +1,18 @@ +#pragma once + +#include "maths_types.h" + +// 3. SIMA (simplified immediate mode api) / render.h +// - dont need to worry about uploading mesh data +// - very useful for debugging +void imm_draw_cuboid(); +void imm_draw_sphere(vec3 pos, f32 radius, vec4 colour); +void imm_draw_camera_frustum(); +static void imm_draw_model( + const char* model_filepath); // tracks internally whether the model is loaded + +static void imm_draw_model(const char* model_filepath) { + // check that model is loaded + // if not loaded, load model and upload to gpu - LRU cache for models + // else submit draw call +}
\ No newline at end of file diff --git a/src/renderer/cleanroom/types.h b/src/renderer/cleanroom/types.h index 3f62cab..98c2e21 100644 --- a/src/renderer/cleanroom/types.h +++ b/src/renderer/cleanroom/types.h @@ -4,9 +4,13 @@ #include "maths_types.h" #include "str.h" -typedef int texture_handle; -typedef int buffer_handle; -typedef int model_handle; +CORE_DEFINE_HANDLE(buffer_handle); +CORE_DEFINE_HANDLE(texture_handle); +CORE_DEFINE_HANDLE(sampler_handle); +CORE_DEFINE_HANDLE(shader_handle); +CORE_DEFINE_HANDLE(model_handle); + +typedef struct transform_hierarchy {} transform_hierarchy; /** @brief Texture Description - used by texture creation functions */ typedef struct texture_desc { @@ -47,14 +51,12 @@ typedef enum gpu_texture_format { } gpu_texture_format; /* render_types */ -typedef struct mesh mesh; -typedef struct model model; typedef struct model pbr_material; typedef struct model bp_material; // blinn-phong #include "maths_types.h" -typedef enum vertex_format { VERTEX_STATIC_3D, VERTEX_SPRITE, VERTEX_COUNT } vertex_format; +typedef enum vertex_format { VERTEX_STATIC_3D, VERTEX_SPRITE, VERTEX_SKINNED, VERTEX_COUNT } vertex_format; typedef union vertex { struct { @@ -68,7 +70,7 @@ typedef union vertex { vec2 position; vec4 colour; vec2 tex_coords; - } sprite; + } sprite; /** @brief vertex format for 2D sprites or quads */ struct { vec3 position; @@ -77,7 +79,7 @@ typedef union vertex { vec3 normal; vec4i bone_ids; // Integer vector for bone IDs vec4 bone_weights; // Weight of each bone's influence - } animated_3d; /** @brief vertex format for skeletal (animated) geometry in 3D */ + } skinned_3d; /** @brief vertex format for skeletal (animated) geometry in 3D */ } vertex; KITC_DECL_TYPED_ARRAY(vertex) @@ -86,6 +88,7 @@ KITC_DECL_TYPED_ARRAY(u32) typedef struct geometry_data { vertex_format format; vertex_darray vertices; + bool has_indices; u32_darray indices; } geometry_data; @@ -105,6 +108,7 @@ C side - reload_model(): */ +// TODO: move to some sort of render layer (not inside the abstraction layer) typedef struct model { str8 debug_name; mesh* meshes; @@ -122,79 +126,57 @@ typedef struct model { // 2 - you need to know how the overall renderer is designed // 1 - you need to understand graphics API specifics -/* render.h */ -// frontend -- these can be called from say a loop in an example, or via FFI -texture_handle texture_create(const char* debug_name, texture_desc description, const u8* data); +/* ral.h */ -void texture_data_upload(texture_handle texture); -buffer_handle buffer_create(const char* debug_name, u64 size); -bool buffer_destroy(buffer_handle buffer); -// models and meshes are implemented **in terms of the above** -mesh mesh_create(geometry_data* geometry); -model_handle model_load(const char* debug_name, const char* filepath); +// command buffer gubbins -/* ral.h */ +/* --- Backends */ -enum pipeline_type { - GRAPHICS, - COMPUTE, -} pipeline_type; +// struct vulkan_backend { +// gpu_pipeline static_opaque_pipeline; +// gpu_pipeline skinned_opaque_pipeline; +// }; -// backend -- these are not seen by the higher-level code -typedef struct gpu_swapchain gpu_swapchain; -typedef struct gpu_device gpu_device; -typedef struct gpu_pipeline gpu_pipeline; -typedef struct gpu_cmd_encoder gpu_cmd_encoder; -typedef struct gpu_cmd_buffer gpu_cmd_buffer; // Ready for submission +/* --- Renderer layer */ +/* render.h */ -void gpu_cmd_encoder_begin(); -void gpu_cmd_encoder_begin_render(); -void gpu_cmd_encoder_begin_compute(); +typedef struct renderer { + void* backend_context; +} renderer; -/* Actual commands that we can encode */ -void encode_buffer_copy(gpu_cmd_encoder* encoder, buffer_handle src, u64 src_offset, - buffer_handle dst, u64 dst_offset, u64 copy_size); -void encode_clear_buffer(gpu_cmd_encoder* encoder, buffer_handle buf); -// render pass -void encode_set_vertex_buffer(gpu_cmd_encoder* encoder, buffer_handle buf); -void encode_set_index_buffer(gpu_cmd_encoder* encoder, buffer_handle buf); -void encode_draw_indexed(gpu_cmd_encoder* encoder, u64 index_count, u64* indices); +bool renderer_init(renderer* ren); +void renderer_shutdown(renderer* ren); -// FUTURE: compute passes +// frontend -- these can be called from say a loop in an example, or via FFI +texture_handle texture_create(const char* debug_name, texture_desc description, const u8* data); -/** @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); +// Frontend Resources +void texture_data_upload(texture_handle texture); +buffer_handle buffer_create(const char* debug_name, u64 size); +bool buffer_destroy(buffer_handle buffer); +sampler_handle sampler_create(); -void gpu_queue_submit(gpu_cmd_buffer* buffer); +void shader_hot_reload(const char* filepath); -// Buffers -void gpu_buffer_create(u64 size); -void gpu_buffer_destroy(buffer_handle buffer); -void gpu_buffer_upload(); -void gpu_buffer_bind(buffer_handle buffer); +// models and meshes are implemented **in terms of the above** +mesh mesh_create(geometry_data* geometry); +model_handle model_load(const char* debug_name, const char* filepath); -// Textures -void gpu_texture_create(); -void gpu_texture_destroy(); -void gpu_texture_upload(); +// Drawing -// Samplers -void gpu_sampler_create(); +// void draw_mesh(gpu_cmd_encoder* encoder, mesh* mesh) { +// encode_set_vertex_buffer(encoder, mesh->vertex_buffer); +// encode_set_index_buffer(encoder, mesh->index_buffer); +// encode_draw_indexed(encoder, mesh->index_count) +// // vkCmdDrawIndexed +// } -// command buffer gubbins +// void draw_scene(arena* frame, model_darray* models, renderer* ren, camera* camera, +// transform_hierarchy* tfh, scene* scene) { +// // set the pipeline first +// encode_set_pipeline() +// // in open this sets the shader +// // in vulkan it sets the whole pipeline -// 3. SIMA (simplified immediate mode api) / render.h -// - dont need to worry about uploading mesh data -// - very useful for debugging -void imm_draw_cuboid(); -void imm_draw_sphere(vec3 pos, f32 radius, vec4 colour); -void imm_draw_camera_frustum(); -static void imm_draw_model( - const char* model_filepath); // tracks internally whether the model is loaded - -static void imm_draw_model(const char* model_filepath) { - // check that model is loaded - // if not loaded, load model and upload to gpu - LRU cache for models - // else submit draw call -}
\ No newline at end of file +// }
\ No newline at end of file |