From aa1eeebb1c05edc22335cbb48af5d42be20750c0 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Fri, 26 Jul 2024 17:20:13 +1000 Subject: moving some maths functions to implementation file --- src/core/core.c | 12 +++----- src/core/core.h | 6 ++-- src/defines.h | 2 +- src/log.h | 2 +- src/maths/maths.c | 22 ++++++++++++++ src/maths/maths.h | 52 +++++++++++++++----------------- src/new_render/pbr.c | 4 +-- src/new_render/render.c | 10 +++--- src/new_render/render.h | 3 +- src/new_render/skybox.c | 4 +-- src/ral/backends/opengl/backend_opengl.c | 3 +- src/ral/ral_impl.h | 3 +- src/ral/ral_types.h | 2 +- src/render/render.c | 1 - src/systems/terrain.c | 13 +++----- src/systems/terrain.h | 5 +-- 16 files changed, 79 insertions(+), 65 deletions(-) (limited to 'src') diff --git a/src/core/core.c b/src/core/core.c index a46e395..44db7b1 100644 --- a/src/core/core.c +++ b/src/core/core.c @@ -74,12 +74,10 @@ void Frame_Begin() { void Frame_Draw() {} void Frame_End() { Render_FrameEnd(g_core.renderer); } -Core* get_global_core() { - return &g_core; -} +Core* get_global_core() { return &g_core; } -GLFWwindow* Core_GetGlfwWindowPtr(Core* core) { - return g_core.window; -} +GLFWwindow* Core_GetGlfwWindowPtr(Core* core) { return g_core.window; } -struct Renderer* Core_GetRenderer(Core* core) { return core->renderer; } \ No newline at end of file +struct Renderer* Core_GetRenderer(Core* core) { + return core->renderer; +} \ No newline at end of file diff --git a/src/core/core.h b/src/core/core.h index 17561f3..54e6ec3 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -26,10 +26,10 @@ struct Renderer; Core* get_global_core(); -/** +/** @brief Throws error if the core cannot be instantiated - @param [in] optional_window - Leave NULL if you want Celeritas to instantiate its own window with GLFW, if you - want to provide the glfw window then pass it in here. + @param [in] optional_window - Leave NULL if you want Celeritas to instantiate its own window with + GLFW, if you want to provide the glfw window then pass it in here. */ void Core_Bringup(GLFWwindow* optional_window); void Core_Shutdown(); diff --git a/src/defines.h b/src/defines.h index ce947d8..b7bf34d 100644 --- a/src/defines.h +++ b/src/defines.h @@ -56,7 +56,7 @@ CORE_DEFINE_HANDLE( #define PUB // For collecting public APIs to expose in an amalgamation header file // #define c_static_inline static inline -#define c_static_inline static +#define c_static_inline inline #define KB(x) ((size_t)x * 1000) #define MB(x) ((size_t)x * 1000 * 1000) diff --git a/src/log.h b/src/log.h index 64d0d2e..73d6cc8 100644 --- a/src/log.h +++ b/src/log.h @@ -36,7 +36,7 @@ void logger_shutdown(); // TODO: macro that outputs logger macros for a specific subsystem or string prefix e.g. "MEMORY" -> // logs now have more context potentially have line numbers too? -void log_output(log_level level, const char* message, ...); +void log_output(log_level level, const char *message, ...); #define FATAL(message, ...) log_output(LOG_LEVEL_FATAL, message, ##__VA_ARGS__) #define ERROR(message, ...) log_output(LOG_LEVEL_ERROR, message, ##__VA_ARGS__) diff --git a/src/maths/maths.c b/src/maths/maths.c index bfe0b5b..adeee3e 100644 --- a/src/maths/maths.c +++ b/src/maths/maths.c @@ -1,5 +1,27 @@ #include "maths.h" +#define c_static_inline + +c_static_inline Vec3 vec3_create(f32 x, f32 y, f32 z) { return (Vec3){ x, y, z }; } +c_static_inline Vec3 vec3_add(Vec3 a, Vec3 b) { return (Vec3){ a.x + b.x, a.y + b.y, a.z + b.z }; } +c_static_inline Vec3 vec3_sub(Vec3 a, Vec3 b) { return (Vec3){ a.x - b.x, a.y - b.y, a.z - b.z }; } +c_static_inline Vec3 vec3_mult(Vec3 a, f32 s) { return (Vec3){ a.x * s, a.y * s, a.z * s }; } +c_static_inline Vec3 vec3_div(Vec3 a, f32 s) { return (Vec3){ a.x / s, a.y / s, a.z / s }; } + +c_static_inline f32 vec3_len_squared(Vec3 a) { return (a.x * a.x) + (a.y * a.y) + (a.z * a.z); } +c_static_inline f32 vec3_len(Vec3 a) { return sqrtf(vec3_len_squared(a)); } +c_static_inline Vec3 vec3_negate(Vec3 a) { return (Vec3){ -a.x, -a.y, -a.z }; } +PUB c_static_inline Vec3 vec3_normalise(Vec3 a) { + f32 length = vec3_len(a); + return vec3_div(a, length); +} + +c_static_inline f32 vec3_dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } +c_static_inline Vec3 vec3_cross(Vec3 a, Vec3 b) { + return ( + Vec3){ .x = a.y * b.z - a.z * b.y, .y = a.z * b.x - a.x * b.z, .z = a.x * b.y - a.y * b.x }; +} + Mat4 mat4_ident() { return (Mat4){ .data = { 1.0, 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.0 } }; } \ No newline at end of file diff --git a/src/maths/maths.h b/src/maths/maths.h index d235ca8..a6f8d80 100644 --- a/src/maths/maths.h +++ b/src/maths/maths.h @@ -24,31 +24,25 @@ // --- Vector Implementations // Dimension 3 -PUB c_static_inline Vec3 vec3_create(f32 x, f32 y, f32 z) { return (Vec3){ x, y, z }; } +PUB c_static_inline Vec3 vec3_create(f32 x, f32 y, f32 z); #define vec3(x, y, z) ((Vec3){ x, y, z }) -c_static_inline Vec3 vec3_add(Vec3 a, Vec3 b) { return (Vec3){ a.x + b.x, a.y + b.y, a.z + b.z }; } -c_static_inline Vec3 vec3_sub(Vec3 a, Vec3 b) { return (Vec3){ a.x - b.x, a.y - b.y, a.z - b.z }; } -c_static_inline Vec3 vec3_mult(Vec3 a, f32 s) { return (Vec3){ a.x * s, a.y * s, a.z * s }; } -c_static_inline Vec3 vec3_div(Vec3 a, f32 s) { return (Vec3){ a.x / s, a.y / s, a.z / s }; } - -c_static_inline f32 vec3_len_squared(Vec3 a) { return (a.x * a.x) + (a.y * a.y) + (a.z * a.z); } -c_static_inline f32 vec3_len(Vec3 a) { return sqrtf(vec3_len_squared(a)); } -c_static_inline Vec3 vec3_negate(Vec3 a) { return (Vec3){ -a.x, -a.y, -a.z }; } -PUB c_static_inline Vec3 vec3_normalise(Vec3 a) { - f32 length = vec3_len(a); - return vec3_div(a, length); -} +PUB c_static_inline Vec3 vec3_add(Vec3 a, Vec3 b); +PUB c_static_inline Vec3 vec3_sub(Vec3 a, Vec3 b); +PUB c_static_inline Vec3 vec3_mult(Vec3 a, f32 s); +PUB c_static_inline Vec3 vec3_div(Vec3 a, f32 s); -c_static_inline f32 vec3_dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } -c_static_inline Vec3 vec3_cross(Vec3 a, Vec3 b) { - return ( - Vec3){ .x = a.y * b.z - a.z * b.y, .y = a.z * b.x - a.x * b.z, .z = a.x * b.y - a.y * b.x }; -} +PUB c_static_inline f32 vec3_len_squared(Vec3 a); +PUB c_static_inline f32 vec3_len(Vec3 a); +PUB c_static_inline Vec3 vec3_negate(Vec3 a); +PUB c_static_inline Vec3 vec3_normalise(Vec3 a); + +PUB c_static_inline f32 vec3_dot(Vec3 a, Vec3 b); +PUB c_static_inline Vec3 vec3_cross(Vec3 a, Vec3 b); static const Vec3 VEC3_X = vec3(1.0, 0.0, 0.0); static const Vec3 VEC3_NEG_X = vec3(-1.0, 0.0, 0.0); static const Vec3 VEC3_Y = vec3(0.0, 1.0, 0.0); -static const Vec3 VEC3_NEG_Y = vec3(0.0, -1.0, 0.0); +static const Vec3 VEC3_NEG_Y = vec3(0.0, -1.0, 0.0); static const Vec3 VEC3_Z = vec3(0.0, 0.0, 1.0); static const Vec3 VEC3_NEG_Z = vec3(0.0, 0.0, -1.0); static const Vec3 VEC3_ZERO = vec3(0.0, 0.0, 0.0); @@ -64,13 +58,15 @@ c_static_inline Vec2 vec2_create(f32 x, f32 y) { return (Vec2){ x, y }; } c_static_inline Vec2 vec2_div(Vec2 a, f32 s) { return (Vec2){ a.x / s, a.y / s }; } // TODO: Dimension 4 -c_static_inline Vec4 vec4_create(f32 x, f32 y, f32 z, f32 w) { return (Vec4){ x, y, z, w }; } +static Vec4 vec4_create(f32 x, f32 y, f32 z, f32 w) { return (Vec4){ x, y, z, w }; } #define vec4(x, y, z, w) (vec4_create(x, y, z, w)) #define VEC4_ZERO ((Vec4){ .x = 0.0, .y = 0.0, .z = 0.0, .w = 0.0 }) // --- Quaternion Implementations -c_static_inline f32 quat_dot(Quat a, Quat b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } +c_static_inline f32 quat_dot(Quat a, Quat b) { + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; +} c_static_inline Quat quat_normalise(Quat a) { f32 length = sqrtf(quat_dot(a, a)); // same as len squared @@ -145,7 +141,7 @@ c_static_inline Quat quat_slerp(Quat a, Quat b, f32 percentage) { Mat4 mat4_ident(); -c_static_inline Mat4 mat4_translation(Vec3 position) { +static Mat4 mat4_translation(Vec3 position) { Mat4 out_matrix = mat4_ident(); out_matrix.data[12] = position.x; out_matrix.data[13] = position.y; @@ -153,7 +149,7 @@ c_static_inline Mat4 mat4_translation(Vec3 position) { return out_matrix; } -c_static_inline Mat4 mat4_scale(f32 scale) { +static Mat4 mat4_scale(f32 scale) { Mat4 out_matrix = mat4_ident(); out_matrix.data[0] = scale; out_matrix.data[5] = scale; @@ -181,7 +177,7 @@ c_static_inline Mat4 mat4_rotation(Quat rotation) { return out_matrix; } -c_static_inline Mat4 mat4_mult(Mat4 lhs, Mat4 rhs) { +static Mat4 mat4_mult(Mat4 lhs, Mat4 rhs) { Mat4 out_matrix = mat4_ident(); const f32 *m1_ptr = lhs.data; @@ -224,7 +220,7 @@ static Mat4 mat4_transposed(Mat4 matrix) { #if defined(CEL_REND_BACKEND_VULKAN) /** @brief Creates a perspective projection matrix compatible with Vulkan */ c_static_inline Mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_clip, - f32 far_clip) { + f32 far_clip) { f32 half_tan_fov = tanf(fov_radians * 0.5f); Mat4 out_matrix = { .data = { 0 } }; @@ -238,7 +234,7 @@ c_static_inline Mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 nea } #else /** @brief Creates a perspective projection matrix */ -c_static_inline Mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_clip, +static inline Mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_clip, f32 far_clip) { f32 half_tan_fov = tanf(fov_radians * 0.5f); Mat4 out_matrix = { .data = { 0 } }; @@ -252,7 +248,7 @@ c_static_inline Mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 nea #endif /** @brief Creates an orthographic projection matrix */ -c_static_inline Mat4 mat4_orthographic(f32 left, f32 right, f32 bottom, f32 top, f32 near_clip, +static inline Mat4 mat4_orthographic(f32 left, f32 right, f32 bottom, f32 top, f32 near_clip, f32 far_clip) { // source: kohi game engine. Mat4 out_matrix = mat4_ident(); @@ -272,7 +268,7 @@ c_static_inline Mat4 mat4_orthographic(f32 left, f32 right, f32 bottom, f32 top, return out_matrix; } -c_static_inline Mat4 mat4_look_at(Vec3 position, Vec3 target, Vec3 up) { +static inline Mat4 mat4_look_at(Vec3 position, Vec3 target, Vec3 up) { Mat4 out_matrix; Vec3 z_axis; z_axis.x = target.x - position.x; diff --git a/src/new_render/pbr.c b/src/new_render/pbr.c index 2393aa9..6df4e97 100644 --- a/src/new_render/pbr.c +++ b/src/new_render/pbr.c @@ -34,8 +34,8 @@ GPU_Pipeline* PBR_PipelineCreate(GPU_Renderpass* rpass) { // if (!vertex_shader.has_value || !fragment_shader.has_value) { // ERROR_EXIT("Failed to load shaders from disk") // } - char* vert_shader = string_from_file(vert_path); - char* frag_shader = string_from_file(frag_path); + char* vert_shader = string_from_file(vert_path); + char* frag_shader = string_from_file(frag_path); ShaderData camera_data = { .get_layout = &Binding_Camera_GetLayout }; ShaderData model_data = { .get_layout = &Binding_Model_GetLayout }; diff --git a/src/new_render/render.c b/src/new_render/render.c index c8660a3..cfe68d7 100644 --- a/src/new_render/render.c +++ b/src/new_render/render.c @@ -4,7 +4,6 @@ #include "render.h" #include -#include "glad/glad.h" #include #include "camera.h" #include "core.h" @@ -45,7 +44,8 @@ struct Renderer { Renderer* get_renderer() { return g_core.renderer; } -bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window, GLFWwindow* optional_window) { +bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window, + GLFWwindow* optional_window) { INFO("Renderer init"); ren->frame_arena = arena_create(malloc(FRAME_ARENA_SIZE), FRAME_ARENA_SIZE); @@ -65,14 +65,14 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window // NOTE: all platforms use GLFW at the moment but thats subject to change glfwInit(); - #if defined(CEL_REND_BACKEND_OPENGL) +#if defined(CEL_REND_BACKEND_OPENGL) glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - #elif defined(CEL_REND_BACKEND_VULKAN) +#elif defined(CEL_REND_BACKEND_VULKAN) glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - #endif +#endif window = glfwCreateWindow(config.scr_width, config.scr_height, config.window_name, NULL, NULL); INFO("Window created"); diff --git a/src/new_render/render.h b/src/new_render/render.h index 1f80290..2df684c 100644 --- a/src/new_render/render.h +++ b/src/new_render/render.h @@ -28,7 +28,8 @@ typedef struct RenderCtx { // --- Lifecycle -PUB bool Renderer_Init(RendererConfig config, Renderer* renderer, GLFWwindow** out_window, GLFWwindow* optional_window); +PUB bool Renderer_Init(RendererConfig config, Renderer* renderer, GLFWwindow** out_window, + GLFWwindow* optional_window); PUB void Renderer_Shutdown(Renderer* renderer); PUB size_t Renderer_GetMemReqs(); void Render_WindowSizeChanged(GLFWwindow* window, i32 new_width, i32 new_height); diff --git a/src/new_render/skybox.c b/src/new_render/skybox.c index 8ba4da9..5456401 100644 --- a/src/new_render/skybox.c +++ b/src/new_render/skybox.c @@ -122,9 +122,7 @@ Skybox Skybox_Create(const char** face_paths, int n) { return (Skybox){ .cube = cube, .texture = handle, .pipeline = pipeline }; } -Skybox Skybox_Default() { - return Skybox_Create(faces, 6); -} +Skybox Skybox_Default() { return Skybox_Create(faces, 6); } void Skybox_Draw(Skybox* skybox, Camera camera) { GPU_CmdEncoder* enc = GPU_GetDefaultEncoder(); diff --git a/src/ral/backends/opengl/backend_opengl.c b/src/ral/backends/opengl/backend_opengl.c index 9122e15..ad9969f 100644 --- a/src/ral/backends/opengl/backend_opengl.c +++ b/src/ral/backends/opengl/backend_opengl.c @@ -57,7 +57,8 @@ bool GPU_Backend_Init(const char* window_name, struct GLFWwindow* window, } // All of these are no-ops in OpenGL -void GPU_Backend_Shutdown() { /* TODO */ } +void GPU_Backend_Shutdown() { /* TODO */ +} bool GPU_Device_Create(GPU_Device* out_device) { return true; } void GPU_Device_Destroy(GPU_Device* device) {} bool GPU_Swapchain_Create(GPU_Swapchain* out_swapchain) { return true; } diff --git a/src/ral/ral_impl.h b/src/ral/ral_impl.h index 6e0ccc0..20b9757 100644 --- a/src/ral/ral_impl.h +++ b/src/ral/ral_impl.h @@ -79,7 +79,8 @@ PUB void GPU_EncodeSetVertexBuffer(GPU_CmdEncoder* encoder, BufferHandle buf); PUB void GPU_EncodeSetIndexBuffer(GPU_CmdEncoder* encoder, BufferHandle buf); PUB void GPU_EncodeDraw(GPU_CmdEncoder* encoder, u64 count); PUB void GPU_EncodeDrawIndexed(GPU_CmdEncoder* encoder, u64 index_count); -PUB void GPU_EncodeDrawInstanced(GPU_CmdEncoder* encoder, u64 index_count, u64 instance_count); // TODO: implement instanced rendering +PUB void GPU_EncodeDrawInstanced(GPU_CmdEncoder* encoder, u64 index_count, + u64 instance_count); // TODO: implement instanced rendering // --- Frame cycle PUB bool GPU_Backend_BeginFrame(); diff --git a/src/ral/ral_types.h b/src/ral/ral_types.h index 56fa141..fa93cd5 100644 --- a/src/ral/ral_types.h +++ b/src/ral/ral_types.h @@ -55,7 +55,7 @@ typedef enum GPU_TextureType { } GPU_TextureType; static const char* texture_type_names[] = { - "RAL Texture 2D", "RAL Texture 3D", "RAL Texture 2D Array", + "RAL Texture 2D", "RAL Texture 3D", "RAL Texture 2D Array", "RAL Texture Cubemap", "RAL Texture Count", }; diff --git a/src/render/render.c b/src/render/render.c index 22542df..f326fdd 100644 --- a/src/render/render.c +++ b/src/render/render.c @@ -48,7 +48,6 @@ bool renderer_init(renderer* ren) { glfwMakeContextCurrent(ren->window); DEBUG("Set up GLFW window callbacks"); - DEBUG("Start gpu backend init"); diff --git a/src/systems/terrain.c b/src/systems/terrain.c index 3743a9b..d2a9300 100644 --- a/src/systems/terrain.c +++ b/src/systems/terrain.c @@ -39,7 +39,7 @@ bool Terrain_Init(Terrain_Storage* storage) { } ShaderData camera_data = { .get_layout = &Binding_Camera_GetLayout }; - ShaderData terrain_data = { .get_layout = &TerrainUniforms_GetLayout}; + ShaderData terrain_data = { .get_layout = &TerrainUniforms_GetLayout }; GraphicsPipelineDesc pipeline_desc = { .debug_name = "terrain rendering pipeline", @@ -97,8 +97,8 @@ void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, f32 grid_sc Vec3 v_pos = vec3_create(i * grid_scale, height, j * grid_scale); Vec3 v_normal = VEC3_Y; float tiling_factor = 505.0f; - Vec2 v_uv = vec2((f32)i / width * tiling_factor , (f32)j / height * tiling_factor); - Vertex v = { .static_3d = {.position = v_pos, .normal = v_normal, .tex_coords = v_uv} }; + Vec2 v_uv = vec2((f32)i / width * tiling_factor, (f32)j / height * tiling_factor); + Vertex v = { .static_3d = { .position = v_pos, .normal = v_normal, .tex_coords = v_uv } }; Vertex_darray_push(vertices, v); index++; } @@ -169,13 +169,10 @@ void Terrain_Draw(Terrain_Storage* storage) { GPU_EncodeBindShaderData( enc, 0, (ShaderData){ .data = &camera_data, .get_layout = &Binding_Camera_GetLayout }); - TerrainUniforms uniforms = { - .tex_slot_1 = storage->texture - }; - ShaderData terrain_data = { .data = &uniforms, .get_layout = &TerrainUniforms_GetLayout}; + TerrainUniforms uniforms = { .tex_slot_1 = storage->texture }; + ShaderData terrain_data = { .data = &uniforms, .get_layout = &TerrainUniforms_GetLayout }; GPU_EncodeBindShaderData(enc, 1, terrain_data); - GPU_EncodeSetVertexBuffer(enc, storage->vertex_buffer); GPU_EncodeSetIndexBuffer(enc, storage->index_buffer); diff --git a/src/systems/terrain.h b/src/systems/terrain.h index 664aa8e..4399e6b 100644 --- a/src/systems/terrain.h +++ b/src/systems/terrain.h @@ -27,7 +27,7 @@ typedef struct Heightmap { bool is_uploaded; } Heightmap; - typedef struct Terrain_Storage { +typedef struct Terrain_Storage { // arena terrain_allocator; u32x2 grid_dimensions; f32 grid_scale; @@ -50,7 +50,8 @@ PUB void Terrain_Draw( Terrain_Storage* storage); // NOTE: For now it renders directly to main framebuffer /** @brief Sets the active heightmap to be rendered and collided against. */ -PUB void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, f32 grid_scale, bool free_on_upload); +PUB void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, f32 grid_scale, + bool free_on_upload); PUB Heightmap Heightmap_FromImage(Str8 filepath); PUB Heightmap Heightmap_FromPerlin(/* TODO: perlin noise generation parameters */); -- cgit v1.2.3-70-g09d2