From 65d74bdb26af833b5380046dec204f685f745cc1 Mon Sep 17 00:00:00 2001 From: Omniscient Date: Thu, 11 Jul 2024 18:03:29 +1000 Subject: changing styles plus simplifying a bit --- src/camera.c | 17 --------- src/camera.h | 34 ------------------ src/core.c | 89 ------------------------------------------------ src/core.h | 40 ---------------------- src/core/README.md | 3 ++ src/core/camera.c | 22 ++++++++++++ src/core/camera.h | 37 ++++++++++++++++++++ src/core/core.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ src/core/core.h | 40 ++++++++++++++++++++++ src/defines.h | 8 +++-- src/maths/maths_types.h | 50 +++++++++++++-------------- src/new_render/pbr.h | 60 ++++++++++++++++++++++++++++++++ src/new_render/shadows.h | 30 ++++++++++++++++ src/ral/README.md | 5 +++ src/ral/ral_common.h | 13 +++---- src/ral/ral_types.h | 5 ++- xmake.lua | 11 +++--- 17 files changed, 335 insertions(+), 218 deletions(-) delete mode 100644 src/camera.c delete mode 100644 src/camera.h delete mode 100644 src/core.c delete mode 100644 src/core.h create mode 100644 src/core/README.md create mode 100644 src/core/camera.c create mode 100644 src/core/camera.h create mode 100644 src/core/core.c create mode 100644 src/core/core.h create mode 100644 src/new_render/pbr.h create mode 100644 src/new_render/shadows.h create mode 100644 src/ral/README.md diff --git a/src/camera.c b/src/camera.c deleted file mode 100644 index 8ec1251..0000000 --- a/src/camera.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "camera.h" - -#include "maths.h" - -camera camera_create(vec3 pos, vec3 front, vec3 up, f32 fov) { - camera c = { .position = pos, .front = front, .up = up, .fov = fov }; - return c; -} - -void camera_view_projection(camera *c, f32 screen_height, f32 screen_width, mat4 *out_view, - mat4 *out_proj) { - mat4 proj = mat4_perspective(c->fov, screen_width / screen_height, 0.1, 100.0); - vec3 camera_direction = vec3_add(c->position, c->front); - mat4 view = mat4_look_at(c->position, camera_direction, c->up); - *out_view = view; - *out_proj = proj; -} \ No newline at end of file diff --git a/src/camera.h b/src/camera.h deleted file mode 100644 index ec867c5..0000000 --- a/src/camera.h +++ /dev/null @@ -1,34 +0,0 @@ -/** - * @file camera.h - * @author your name (you@domain.com) - * @brief - * @version 0.1 - * @date 2024-02-24 - * - * @copyright Copyright (c) 2024 - * - */ -#pragma once - -#include "maths_types.h" - -typedef struct camera { - vec3 position; - vec3 front; - vec3 up; - f32 fov; -} camera; - -/** @brief create a camera */ -camera camera_create(vec3 pos, vec3 front, vec3 up, f32 fov); - -/** @brief get a 4x4 transform matrix for the view and perspective projection */ -void camera_view_projection(camera *c, f32 screen_height, f32 screen_width, mat4 *out_view, - mat4 *out_proj); - -// TODO: Basic reusable camera controls -/* -Right click + move = pan -Left click = orbit camera -WASD = forward/backward/left/right -*/ \ No newline at end of file diff --git a/src/core.c b/src/core.c deleted file mode 100644 index 1d8fe91..0000000 --- a/src/core.c +++ /dev/null @@ -1,89 +0,0 @@ -#include "core.h" - -#include - -#include "glfw3.h" -#include "input.h" -#include "keys.h" -#include "log.h" -#include "mem.h" -#include "render.h" -#include "render_types.h" -#include "scene.h" -// #include "threadpool.h" - -#define SCR_WIDTH 1000 -#define SCR_HEIGHT 1000 - -core g_core; /** @brief global `core` that other files can use */ - -inline core* get_global_core() { return &g_core; } - -void core_bringup() { - INFO("Initiate Core bringup"); - renderer_config conf = { .window_name = { "Celeritas Engine Core" }, - .scr_width = SCR_WIDTH, - .scr_height = SCR_HEIGHT, - .clear_colour = (vec3){ .08, .08, .1 } }; - g_core.renderer.config = conf; - g_core.renderer.backend_context = NULL; - - // threadpool_create(&c->threadpool, 6, 256); - // threadpool_set_ctx(&c->threadpool, c); // Gives the threadpool access to the core - - // initialise all subsystems - if (!renderer_init(&g_core.renderer)) { - // FATAL("Failed to start renderer"); - ERROR_EXIT("Failed to start renderer\n"); - } - if (!input_system_init(&g_core.input, g_core.renderer.window)) { - // the input system needs the glfw window which is created by the renderer - // hence the order here is important - FATAL("Failed to start input system"); - ERROR_EXIT("Failed to start input system\n"); - } - /* - if (!text_system_init(&c->text)) { - // FATAL("Failed to start text system"); - ERROR_EXIT("Failed to start text system\n"); - } - if (!screenspace_2d_init(&c->screenspace)) { - // FATAL("Failed to start screenspace 2d plugin"); - ERROR_EXIT("Failed to start screenspace 2d plugin\n"); - } - */ - - size_t model_data_max = 1024 * 1024 * 1024; - arena model_arena = arena_create(malloc(model_data_max), model_data_max); - - model_pool model_pool = model_pool_create(&model_arena, 256, sizeof(model)); - g_core.models = model_pool; - INFO("Created model pool allocator"); - - INFO("Creating default scene"); - scene_init(&g_core.default_scene); -} - -#include - -/* bool should_window_close(core* core) { glfwWindowShouldClose(core->renderer.window); } */ -void core_input_update() { input_update(&g_core.input); } -void core_frame_begin(core* core) { render_frame_begin(&core->renderer); } -void core_frame_end(core* core) { render_frame_end(&core->renderer); } - -void core_shutdown() { - // threadpool_destroy(&core->threadpool); - input_system_shutdown(&g_core.input); - renderer_shutdown(&g_core.renderer); -} - -bool should_exit() { - return key_just_released(KEYCODE_ESCAPE) || glfwWindowShouldClose(g_core.renderer.window); -} - -void frame_begin() { - glfwPollEvents(); - render_frame_begin(&g_core.renderer); -} -void frame_draw() {} -void frame_end() { render_frame_end(&g_core.renderer); } diff --git a/src/core.h b/src/core.h deleted file mode 100644 index 89702fd..0000000 --- a/src/core.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include "input.h" -#include "render_types.h" -#include "scene.h" -#include "screenspace.h" -#include "terrain.h" -#include "text.h" -// #include "threadpool.h" - -typedef struct core { - const char* app_name; - // foundations - renderer renderer; - // threadpool threadpool; - // systems - input_state input; - text_system_state text; - terrain_state terrain; - screenspace_state screenspace; - // data storage - scene default_scene; - model_pool models; - // model_darray* models; -} core; - -core* get_global_core(); - -// --- Lifecycle - -/** @brief Throws error if the core cannot be instantiated */ -void core_bringup(); -void core_shutdown(); -bool should_exit(); - -void frame_begin(); -void frame_draw(); -void frame_end(); - -void core_input_update(); diff --git a/src/core/README.md b/src/core/README.md new file mode 100644 index 0000000..19cc1d0 --- /dev/null +++ b/src/core/README.md @@ -0,0 +1,3 @@ +# Core + +Core engine facilities diff --git a/src/core/camera.c b/src/core/camera.c new file mode 100644 index 0000000..50c4054 --- /dev/null +++ b/src/core/camera.c @@ -0,0 +1,22 @@ +#include "camera.h" + +#include "maths.h" + +Camera Camera_Create(vec3 pos, vec3 front, vec3 up, f32 fov) { + Camera c = { .position = pos, .front = front, .up = up, .fov = fov }; + return c; +} + +mat4 Camera_ViewProj(Camera *c, f32 lens_height, f32 lens_width, mat4 *out_view, + mat4 *out_proj) { + mat4 proj = mat4_perspective(c->fov, lens_width / lens_height, 0.1, 100.0); + vec3 camera_direction = vec3_add(c->position, c->front); + mat4 view = mat4_look_at(c->position, camera_direction, c->up); + if (out_view) { + *out_view = view; + } + if (out_proj) { + *out_proj = proj; + } + return mat4_mult(view, proj); +} diff --git a/src/core/camera.h b/src/core/camera.h new file mode 100644 index 0000000..233f5f3 --- /dev/null +++ b/src/core/camera.h @@ -0,0 +1,37 @@ +/** + * @file camera.h + * @brief + */ +#pragma once + +#include "defines.h" +#include "maths_types.h" + +typedef struct Camera { + vec3 position; + vec3 front; + vec3 up; + f32 fov; +} Camera; + +/** @brief create a camera */ +Camera Camera_Create(vec3 pos, vec3 front, vec3 up, f32 fov); + +/** + * @brief Get 3D camera transform matrix + * @param out_view optionally stores just the view matrix + * @param out_proj optionally stores just the projection matrix + * @returns the camera's view projection matrix pre-multiplied +*/ +PUB mat4 Camera_ViewProj(Camera* c, f32 lens_height, f32 lens_width, mat4* out_view, mat4* out_proj); + +/** @brief Get 2D camera transform matrix */ +PUB mat4 Camera_View2D(Camera* c); // TODO: 2D cameras + + +// TODO: Basic reusable camera controls +/* +Right click + move = pan +Left click = orbit camera +WASD = forward/backward/left/right +*/ diff --git a/src/core/core.c b/src/core/core.c new file mode 100644 index 0000000..1d8fe91 --- /dev/null +++ b/src/core/core.c @@ -0,0 +1,89 @@ +#include "core.h" + +#include + +#include "glfw3.h" +#include "input.h" +#include "keys.h" +#include "log.h" +#include "mem.h" +#include "render.h" +#include "render_types.h" +#include "scene.h" +// #include "threadpool.h" + +#define SCR_WIDTH 1000 +#define SCR_HEIGHT 1000 + +core g_core; /** @brief global `core` that other files can use */ + +inline core* get_global_core() { return &g_core; } + +void core_bringup() { + INFO("Initiate Core bringup"); + renderer_config conf = { .window_name = { "Celeritas Engine Core" }, + .scr_width = SCR_WIDTH, + .scr_height = SCR_HEIGHT, + .clear_colour = (vec3){ .08, .08, .1 } }; + g_core.renderer.config = conf; + g_core.renderer.backend_context = NULL; + + // threadpool_create(&c->threadpool, 6, 256); + // threadpool_set_ctx(&c->threadpool, c); // Gives the threadpool access to the core + + // initialise all subsystems + if (!renderer_init(&g_core.renderer)) { + // FATAL("Failed to start renderer"); + ERROR_EXIT("Failed to start renderer\n"); + } + if (!input_system_init(&g_core.input, g_core.renderer.window)) { + // the input system needs the glfw window which is created by the renderer + // hence the order here is important + FATAL("Failed to start input system"); + ERROR_EXIT("Failed to start input system\n"); + } + /* + if (!text_system_init(&c->text)) { + // FATAL("Failed to start text system"); + ERROR_EXIT("Failed to start text system\n"); + } + if (!screenspace_2d_init(&c->screenspace)) { + // FATAL("Failed to start screenspace 2d plugin"); + ERROR_EXIT("Failed to start screenspace 2d plugin\n"); + } + */ + + size_t model_data_max = 1024 * 1024 * 1024; + arena model_arena = arena_create(malloc(model_data_max), model_data_max); + + model_pool model_pool = model_pool_create(&model_arena, 256, sizeof(model)); + g_core.models = model_pool; + INFO("Created model pool allocator"); + + INFO("Creating default scene"); + scene_init(&g_core.default_scene); +} + +#include + +/* bool should_window_close(core* core) { glfwWindowShouldClose(core->renderer.window); } */ +void core_input_update() { input_update(&g_core.input); } +void core_frame_begin(core* core) { render_frame_begin(&core->renderer); } +void core_frame_end(core* core) { render_frame_end(&core->renderer); } + +void core_shutdown() { + // threadpool_destroy(&core->threadpool); + input_system_shutdown(&g_core.input); + renderer_shutdown(&g_core.renderer); +} + +bool should_exit() { + return key_just_released(KEYCODE_ESCAPE) || glfwWindowShouldClose(g_core.renderer.window); +} + +void frame_begin() { + glfwPollEvents(); + render_frame_begin(&g_core.renderer); +} +void frame_draw() {} +void frame_end() { render_frame_end(&g_core.renderer); } diff --git a/src/core/core.h b/src/core/core.h new file mode 100644 index 0000000..89702fd --- /dev/null +++ b/src/core/core.h @@ -0,0 +1,40 @@ +#pragma once + +#include "input.h" +#include "render_types.h" +#include "scene.h" +#include "screenspace.h" +#include "terrain.h" +#include "text.h" +// #include "threadpool.h" + +typedef struct core { + const char* app_name; + // foundations + renderer renderer; + // threadpool threadpool; + // systems + input_state input; + text_system_state text; + terrain_state terrain; + screenspace_state screenspace; + // data storage + scene default_scene; + model_pool models; + // model_darray* models; +} core; + +core* get_global_core(); + +// --- Lifecycle + +/** @brief Throws error if the core cannot be instantiated */ +void core_bringup(); +void core_shutdown(); +bool should_exit(); + +void frame_begin(); +void frame_draw(); +void frame_end(); + +void core_input_update(); diff --git a/src/defines.h b/src/defines.h index a35dbf4..e0a7782 100644 --- a/src/defines.h +++ b/src/defines.h @@ -38,7 +38,7 @@ _Static_assert(sizeof(i64) == 8, "type i64 should be 8 byte"); _Static_assert(sizeof(f32) == 4, "type f32 should be 4 bytes"); _Static_assert(sizeof(f64) == 8, "type f64 should be 8 bytes"); -_Static_assert(sizeof(ptrdiff_t) == 8, ""); +_Static_assert(sizeof(ptrdiff_t) == 8, "type ptrdiff_t should be 8 bytes"); #define alignof(x) _Alignof(x) @@ -49,6 +49,10 @@ _Static_assert(sizeof(ptrdiff_t) == 8, ""); u32 raw; \ } +CORE_DEFINE_HANDLE(Handle); // Untyped handle that can be casted to a strongly typed resource handle + +#define PUB // For collecting public APIs to expose in an amalgamation header file + /* Possible platform defines: #define CEL_PLATFORM_LINUX 1 @@ -79,4 +83,4 @@ Renderer backend defines: #if defined(CEL_PLATFORM_MAC) // #define CEL_REND_BACKEND_METAL 1 #define CEL_REND_BACKEND_OPENGL 1 -#endif \ No newline at end of file +#endif diff --git a/src/maths/maths_types.h b/src/maths/maths_types.h index 609672c..5bfc43c 100644 --- a/src/maths/maths_types.h +++ b/src/maths/maths_types.h @@ -22,54 +22,54 @@ // --- Types /** @brief 2D Vector */ -typedef struct vec2 { +typedef struct Vec2 { f32 x, y; -} vec2; +} Vec2; /** @brief 3D Vector */ -typedef struct vec3 { +typedef struct Vec3 { f32 x, y, z; -} vec3; +} Vec3; /** @brief 4D Vector */ -typedef struct vec4 { +typedef struct Vec4 { f32 x, y, z, w; -} vec4; +} Vec4; /** @brief Quaternion */ -typedef vec4 quat; +typedef Vec4 Quat; /** @brief 4x4 Matrix */ -typedef struct mat4 { +typedef union Mat4 { // TODO: use this format for more readable code: vec4 x_axis, y_axis, z_axis, w_axis; f32 data[16]; -} mat4; + Vec4 cols[4]; +} Mat4; /** @brief Three dimensional bounding box */ -typedef struct bbox_3d { - vec3 min; // minimum point of the box - vec3 max; // maximum point of the box -} bbox_3d; +typedef struct Bbox_3D { + Vec3 min; // minimum point of the box + Vec3 max; // maximum point of the box +} Bbox_3d; /** @brief 3D Axis-aligned bounding box */ -typedef bbox_3d aabb_3d; +typedef Bbox_3d Aabb_3D; /** @brief 3D affine transformation */ -typedef struct transform { - vec3 position; - quat rotation; +typedef struct Transform { + Vec3 position; + Quat rotation; f32 scale; bool is_dirty; -} transform; -typedef transform transform3d; +} Transform; -typedef struct vec4i { +typedef struct Vec4i { i32 x, y, z, w; -} vec4i; +} Vec4i; -typedef struct vec4u { +typedef struct Vec4u { u32 x, y, z, w; -} vec4u; +} Vec4u; // --- Some other types typedef struct u32x3 { @@ -96,8 +96,8 @@ typedef struct u32x2 { // Type aliass -typedef struct vec2 f32x2; +typedef struct Vec2 f32x2; #define f32x2(x, y) ((f32x2){ x, y }) -typedef struct vec3 f32x3; +typedef struct Vec3 f32x3; #define f32x3(x, y, z) ((f32x3){ x, y, z }) diff --git a/src/new_render/pbr.h b/src/new_render/pbr.h new file mode 100644 index 0000000..dd29301 --- /dev/null +++ b/src/new_render/pbr.h @@ -0,0 +1,60 @@ +/** + * @file pbr.h + * @brief PBR render pass + */ + +#pragma once +#include "defines.h" +#include "camera.h" +#include "maths_types.h" +#include "ral/ral.h" +#include "ral/ral_common.h" + +// PBR; + +// --- Public API +typedef struct PBR_Storage PBR_Storage; // Stores all necessary data and handles + +PUB void PBR_Init(PBR_Storage* storage); + +// NOTE: For simplicity's sake we will render this pass directly to the default framebuffer +PUB void PBR_Run( + PBR_Storage* storage + // light data + // camera + // geometry + // materials +); + +typedef struct PBR_Params { + Vec3 albedo; + f32 metallic; + f32 roughness; + f32 ao; +} PBR_Params; + +typedef struct PBR_Textures { + TextureHandle albedo_map; + TextureHandle normal_map; + bool metal_roughness_combined; + TextureHandle metallic_map; + TextureHandle roughness_map; + TextureHandle ao_map; +} PBR_Textures; + +// --- Internal + + +GPU_Renderpass* PBR_RPassCreate(); +GPU_Pipeline* PBR_PipelineCreate(GPU_Renderpass* rpass); +void PBR_Execute( + PBR_Storage* storage, + Camera camera, + TextureHandle shadowmap_tex, + MaterialMap* materials, // map of String -> Material + RenderEnt* entities, + size_t entity_count + +); + +// Internally this will need to update material parameters diff --git a/src/new_render/shadows.h b/src/new_render/shadows.h new file mode 100644 index 0000000..4f10f35 --- /dev/null +++ b/src/new_render/shadows.h @@ -0,0 +1,30 @@ +/** + * @file shadows.h + * @brief Functions for adding shadows to scene rendering. +*/ + + +#pragma once +#include "defines.h" +#include "ral/ral_types.h" + +typedef struct Shadow_Storage Shadow_Storage; + +typedef struct RenderEnt RenderEnt; +typedef struct Camera Camera; +typedef struct Mat4 Mat4; + +// --- Public API +PUB void Shadow_Init(Shadow_Storage* storage); + +/** @brief Run shadow map generation for given entities, and store in a texture. + * @note Uses active directional light for now */ +PUB void Shadow_Run(Shadow_Storage* storage, RenderEnt* entities, size_t entity_count); + +/** @brief Get the shadow texture generated from shadowmap pass */ +PUB Handle Shadow_GetShadowMapTexture(Shadow_Storage* storage); + +// --- Internal +GPU_Renderpass* Shadow_RPassCreate(); // Creates the render pass +GPU_Pipeline* Shadow_PipelineCreate(GPU_Renderpass* rpass); // Creates the pipeline +void Shadow_ShadowmapExecute(Shadow_Storage* storage, Mat4 light_space_transform, RenderEnt* entites, size_t entity_count); diff --git a/src/ral/README.md b/src/ral/README.md new file mode 100644 index 0000000..f66b95a --- /dev/null +++ b/src/ral/README.md @@ -0,0 +1,5 @@ +# RAL + +**Render Abstraction Layer** is a thin abstraction over graphics APIs. Everything in `render` builds on top of the code in +this folder in order to be API-agnostic. It also makes writing graphics code easier as it smooths over some of the discrepancies +between APIs like texture/buffer creation and updating shader values. \ No newline at end of file diff --git a/src/ral/ral_common.h b/src/ral/ral_common.h index bc86945..fabf264 100644 --- a/src/ral/ral_common.h +++ b/src/ral/ral_common.h @@ -4,10 +4,11 @@ #include "ral_types.h" #include "ral_impl.h" -CORE_DEFINE_HANDLE(buffer_handle); -CORE_DEFINE_HANDLE(texture_handle); -CORE_DEFINE_HANDLE(sampler_handle); -CORE_DEFINE_HANDLE(shader_handle); +CORE_DEFINE_HANDLE(BufferHandle); +CORE_DEFINE_HANDLE(TextureHandle); +CORE_DEFINE_HANDLE(SamplerHandle); +CORE_DEFINE_HANDLE(ShaderHandle); + CORE_DEFINE_HANDLE(pipeline_layout_handle); CORE_DEFINE_HANDLE(pipeline_handle); CORE_DEFINE_HANDLE(renderpass_handle); @@ -18,7 +19,7 @@ CORE_DEFINE_HANDLE(renderpass_handle); #define MAX_PIPELINES 128 #define MAX_RENDERPASSES 128 -TYPED_POOL(gpu_buffer, buffer); +TYPED_POOL(GPU_Buffer, Buffer); TYPED_POOL(gpu_texture, texture); TYPED_POOL(gpu_pipeline_layout, pipeline_layout); TYPED_POOL(gpu_pipeline, pipeline); @@ -42,4 +43,4 @@ struct resource_pools { }; void resource_pools_init(arena* a, struct resource_pools* res_pools); -// vertex_description static_3d_vertex_description(); \ No newline at end of file +// vertex_description static_3d_vertex_description(); diff --git a/src/ral/ral_types.h b/src/ral/ral_types.h index bc24d7f..a27bbd2 100644 --- a/src/ral/ral_types.h +++ b/src/ral/ral_types.h @@ -11,6 +11,9 @@ typedef struct gpu_cmd_buffer gpu_cmd_buffer; // Ready for submission typedef struct gpu_buffer gpu_buffer; typedef struct gpu_texture gpu_texture; +typedef struct GPU_Renderpass GPU_Renderpass; +typedef struct GPU_Pipeline GPU_Pipeline; + typedef enum gpu_primitive_topology { CEL_PRIMITIVE_TOPOLOGY_POINT, CEL_PRIMITIVE_TOPOLOGY_LINE, @@ -77,4 +80,4 @@ typedef struct gpu_renderpass_desc { texture_handle color_target; // for now only support one bool has_depth_stencil; texture_handle depth_stencil; -} gpu_renderpass_desc; \ No newline at end of file +} gpu_renderpass_desc; diff --git a/xmake.lua b/xmake.lua index 335e751..f794221 100644 --- a/xmake.lua +++ b/xmake.lua @@ -60,6 +60,8 @@ local core_sources = { "src/maths/*.c", "src/platform/*.c", "src/physics/*.c", + "src/ral/*.c", + "src/ral/backends/opengl/*.c", "src/renderer/*.c", "src/renderer/backends/*.c", "src/renderer/backends/opengl/*.c", @@ -112,10 +114,11 @@ target("core_config") add_includedirs("src/maths/", {public = true}) add_includedirs("src/platform/", {public = true}) add_includedirs("src/physics/", {public = true}) - add_includedirs("src/renderer/", {public = true}) - add_includedirs("src/renderer/backends/", {public = true}) - add_includedirs("src/renderer/backends/opengl", {public = true}) - add_includedirs("src/renderer/backends/metal", {public = true}) + add_includedirs("src/ral", {public = true}) + -- add_includedirs("src/renderer/", {public = true}) + -- add_includedirs("src/renderer/backends/", {public = true}) + -- add_includedirs("src/renderer/backends/opengl", {public = true}) + -- add_includedirs("src/renderer/backends/metal", {public = true}) add_includedirs("src/resources/", {public = true}) add_includedirs("src/std/", {public = true}) add_includedirs("src/std/containers", {public = true}) -- cgit v1.2.3-70-g09d2