diff options
Diffstat (limited to 'src/renderer/cleanroom')
-rw-r--r-- | src/renderer/cleanroom/README.md | 1 | ||||
-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 | 182 |
8 files changed, 397 insertions, 0 deletions
diff --git a/src/renderer/cleanroom/README.md b/src/renderer/cleanroom/README.md new file mode 100644 index 0000000..d510f16 --- /dev/null +++ b/src/renderer/cleanroom/README.md @@ -0,0 +1 @@ +# Cleanroom / Re-jig of the renderer structure
\ No newline at end of file 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 new file mode 100644 index 0000000..98c2e21 --- /dev/null +++ b/src/renderer/cleanroom/types.h @@ -0,0 +1,182 @@ +#pragma once +#include "darray.h" +#include "defines.h" +#include "maths_types.h" +#include "str.h" + +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 { + // gpu_texture_type tex_type; + // gpu_texture_format format; + // u32x2 extents; +} texture_desc; + +/* + - render_types.h + - ral_types.h + - ral.h + - render.h ? +*/ + +// gpu types +typedef enum gpu_primitive_topology { + CEL_PRIMITIVE_TOPOLOGY_POINT, + CEL_PRIMITIVE_TOPOLOGY_LINE, + CEL_PRIMITIVE_TOPOLOGY_LINE_STRIP, + CEL_PRIMITIVE_TOPOLOGY_TRIANGLE, + CEL_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, + CEL_PRIMITIVE_TOPOLOGY_COUNT +} cel_primitive_topology; + +typedef enum gpu_texture_type { + CEL_TEXTURE_TYPE_2D, + CEL_TEXTURE_TYPE_3D, + CEL_TEXTURE_TYPE_2D_ARRAY, + CEL_TEXTURE_TYPE_CUBE_MAP, + CEL_TEXTURE_TYPE_COUNT +} gpu_texture_type; + +typedef enum gpu_texture_format { + CEL_TEXTURE_FORMAT_8_8_8_8_RGBA_UNORM, + CEL_TEXTURE_FORMAT_DEPTH_DEFAULT, + CEL_TEXTURE_FORMAT_COUNT +} gpu_texture_format; + +/* render_types */ +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_SKINNED, VERTEX_COUNT } vertex_format; + +typedef union vertex { + struct { + vec3 position; + vec4 colour; + vec2 tex_coords; + vec3 normal; + } static_3d; /** @brief standard vertex format for static geometry in 3D */ + + struct { + vec2 position; + vec4 colour; + vec2 tex_coords; + } sprite; /** @brief vertex format for 2D sprites or quads */ + + struct { + vec3 position; + vec4 colour; + vec2 tex_coords; + vec3 normal; + vec4i bone_ids; // Integer vector for bone IDs + vec4 bone_weights; // Weight of each bone's influence + } skinned_3d; /** @brief vertex format for skeletal (animated) geometry in 3D */ +} vertex; + +KITC_DECL_TYPED_ARRAY(vertex) +KITC_DECL_TYPED_ARRAY(u32) + +typedef struct geometry_data { + vertex_format format; + vertex_darray vertices; + bool has_indices; + u32_darray indices; +} geometry_data; + +typedef struct mesh { + buffer_handle vertex_buffer; + buffer_handle index_buffer; + u32 index_count; + bool has_indices; + geometry_data* vertices; // NULL means it has been freed +} mesh; + +/* Hot reloading: +C side - reload_model(): + - load model from disk using existing loader + - remove from transform graph so it isnt tried to be drawn + - + +*/ + +// TODO: move to some sort of render layer (not inside the abstraction layer) +typedef struct model { + str8 debug_name; + mesh* meshes; + u32 mesh_count; +} model; + +// ? How to tie together materials and shaders + +// Three registers +// 1. low level graphics api calls "ral" +// 2. higher level render calls +// 3. simplified immediate mode API + +// 3 - you don't need to know how the renderer works at all +// 2 - you need to know how the overall renderer is designed +// 1 - you need to understand graphics API specifics + +/* ral.h */ + + +// command buffer gubbins + +/* --- Backends */ + +// struct vulkan_backend { +// gpu_pipeline static_opaque_pipeline; +// gpu_pipeline skinned_opaque_pipeline; +// }; + +/* --- Renderer layer */ +/* render.h */ + +typedef struct renderer { + void* backend_context; +} renderer; + +bool renderer_init(renderer* ren); +void renderer_shutdown(renderer* ren); + +// 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); + +// 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 shader_hot_reload(const char* filepath); + +// 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); + +// Drawing + +// 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 +// } + +// 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 + +// }
\ No newline at end of file |