From 8b75f00918958ca174fb1e77c60d4dfb18cbc923 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:38:51 +1100 Subject: gonna go back to snake case --- src/backend_mtl.c | 0 src/maths.c | 2 +- src/ral.c | 5 +++++ 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 src/backend_mtl.c (limited to 'src') diff --git a/src/backend_mtl.c b/src/backend_mtl.c new file mode 100644 index 0000000..e69de29 diff --git a/src/maths.c b/src/maths.c index 3ad1e2e..f12c852 100644 --- a/src/maths.c +++ b/src/maths.c @@ -1,3 +1,3 @@ #include -Vec3 Vec3_Create(f32 x, f32 y, f32 z) { return (Vec3){ x, y, z }; } +vec3 vec3_create(f32 x, f32 y, f32 z) { return (vec3){ x, y, z }; } diff --git a/src/ral.c b/src/ral.c index e69de29..45d202b 100644 --- a/src/ral.c +++ b/src/ral.c @@ -0,0 +1,5 @@ +#ifdef GPU_VULKAN + +#elif GPU_METAL + +#endif \ No newline at end of file -- cgit v1.2.3-70-g09d2 From ff96a533014174d0857872f86f1536a06089f3d2 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 17 Oct 2024 15:29:29 +1100 Subject: more snakecase --- include/celeritas.h | 135 ++++++++++++++++++++++++++-------------------------- src/log.c | 2 +- 2 files changed, 69 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/include/celeritas.h b/include/celeritas.h index 185ab09..6645dc6 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -59,34 +59,34 @@ _Static_assert(sizeof(ptrdiff_t) == 8, "type ptrdiff_t should be 8 bytes"); // Platform informs renderer backend (unless user overrides) #if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_WINDOWS) -#define CEL_REND_BACKEND_VULKAN 1 +#define GPU_VULKAN 1 #elif defined(CEL_PLATFORM_MAC) -#define CEL_REND_BACKEND_METAL 1 +#define GPU_METAL 1 #endif // --- Forward declare vital structures -typedef struct Core Core; -typedef struct Renderer Renderer; -typedef struct InputState InputState; +typedef struct core core; +typedef struct renderer renderer; +typedef struct input_state input_state; struct GLFWwindow; // Global getters -Core* GetGlobalCore(); -Renderer* GetRenderer(); -struct GLFWwindow* GetWindow(); +core* get_g_core(); +renderer* get_g_renderer(); +struct GLFWwindow* get_window(); -struct Core { +struct core { const char* app_name; struct GLFWwindow* window; - Renderer* renderer; - InputState* input; + renderer* renderer; + input_state* input; }; -extern Core g_core; /** @brief global `Core` that other files can use */ +extern core g_core; /** @brief global `Core` that other files can use */ -void Core_Bringup(const char* window_name, struct GLFWwindow* optional_window); -void Core_Shutdown(); -void Core_ResizeViewport(int width, int height); -bool AppShouldExit(); +void core_bringup(const char* window_name, struct GLFWwindow* optional_window); +void core_shutdown(); +void core_resize_viewport(int width, int height); +bool app_should_exit(); // --- Memory facilities: Allocators, helpers @@ -98,16 +98,16 @@ bool AppShouldExit(); // --- Logging // Log levels -typedef enum LogLevel { +typedef enum loglevel { LOG_LEVEL_FATAL = 0, LOG_LEVEL_ERROR = 1, LOG_LEVEL_WARN = 2, LOG_LEVEL_INFO = 3, LOG_LEVEL_DEBUG = 4, LOG_LEVEL_TRACE = 5, -} LogLevel; +} loglevel; -void log_output(char* module, LogLevel level, const char* msg, ...); +void log_output(char* module, loglevel level, const char* msg, ...); #define NAMESPACED_LOGGER(module) \ static inline void FATAL(const char* msg, ...) { \ @@ -189,9 +189,9 @@ typedef struct transform { inlined vec3 vec3_create(f32 x, f32 y, f32 z); inlined vec3 vec3_add(vec3 u, vec3 v); -inlined vec3 Vec3_sub(vec3 u, vec3 v); -inlined vec3 Vec3_mult(vec3 u, f32 s); -inlined vec3 Vec3_div(vec3 u, f32 s); +inlined vec3 vec3_sub(vec3 u, vec3 v); +inlined vec3 vec3_mult(vec3 u, f32 s); +inlined vec3 vec3_div(vec3 u, f32 s); // --- RAL @@ -203,13 +203,13 @@ DEFINE_HANDLE(pipeline_handle); #define MAX_SHADER_BINDINGS 16 // Backend-specific structs -typedef struct gpu_swapchain GPU_Swapchain; +typedef struct gpu_swapchain gpu_swapchain; typedef struct gpu_compute_pipeline gpu_compute_pipeline; typedef struct gpu_gfx_pipeline gpu_gfx_pipeline; typedef struct gpu_encoder gpu_encoder; // Command encoder // NOTE: Can we just use Storage buffer for everything? -typedef enum gpu_buf_type {} gpu_buf_type; +// typedef enum gpu_buf_type {} gpu_buf_type; typedef enum gpu_tex_type { TEXTURE_TYPE_2D, @@ -227,7 +227,7 @@ typedef struct texture_desc { } texture_desc; /// @strip_prefix(ATTR_) -typedef enum VertexAttribType { +typedef enum vertex_attrib_type { ATTR_F32, ATTR_F32x2, ATTR_F32x3, @@ -240,18 +240,15 @@ typedef enum VertexAttribType { ATTR_I32x2, ATTR_I32x3, ATTR_I32x4, -} VertexAttribType; +} vertex_attrib_type; -typedef struct VertexDesc { +typedef struct vertex_desc { const char* label; - VertexAttribType attributes[MAX_VERTEX_ATTRIBUTES]; + vertex_attrib_type attributes[MAX_VERTEX_ATTRIBUTES]; u32 attribute_count; -} VertexDesc; +} vertex_desc; -typedef struct ShaderDesc { -} ShaderDesc; - -typedef enum ShaderBindingKind { +typedef enum shader_binding_type { BINDING_BYTES, BINDING_BUFFER, BINDING_BUFFER_ARRAY, @@ -259,53 +256,57 @@ typedef enum ShaderBindingKind { BINDING_TEXTURE_ARRAY, BINDING_SAMPLER, BINDING_COUNT -} ShaderBindingKind; +} shader_binding_type; -typedef enum ShaderVisibility { +typedef enum shader_vis { VISIBILITY_VERTEX = 1 << 0, VISIBILITY_FRAGMENT = 1 << 1, VISIBILITY_COMPUTE = 1 << 2, -} ShaderVisibility; +} shader_vis; -typedef struct ShaderBinding { +typedef struct shader_binding { const char* label; - ShaderBindingKind kind; - ShaderVisibility vis; + shader_binding_type binding_type; + shader_vis vis; union { struct { u32 size; void* data; } bytes; struct { - BufHandle handle; + buf_handle handle; } buffer; struct { - TexHandle handle; + tex_handle handle; } texture; } data; -} ShaderBinding; +} shader_binding; -typedef struct ShaderDataLayout { - ShaderBinding bindings[MAX_SHADER_BINDINGS]; +typedef struct shader_data_layout { + shader_binding bindings[MAX_SHADER_BINDINGS]; size_t binding_count; -} ShaderDataLayout; +} shader_data_layout; + +typedef struct shader_desc { + // TODO +} shader_desc; -typedef enum CullMode { CULL_BACK_FACE, CULL_FRONT_FACE } CullMode; +typedef enum cull_mode { CULL_BACK_FACE, CULL_FRONT_FACE } cull_mode; -typedef struct GraphicsPipelineDesc { +typedef struct gfx_pipeline_desc { const char* label; - VertexDesc vertex_desc; - ShaderDesc vs; - ShaderDesc fs; + vertex_desc vertex_desc; + shader_desc vs; + shader_desc fs; // ShaderDataLayout data_layouts[MAX_SHADER_DATA_LAYOUTS]; // u32 data_layouts_count; -} GraphicsPipelineDesc; +} gfx_pipeline_desc; // --- RAL Functions -BufHandle GPU_BufferCreate(u64 size, const void* data); -void GPU_BufferDestroy(BufHandle handle); -TexHandle GPU_TextureCreate(TextureDesc desc, bool create_view, const void* data); -void GPU_TextureDestroy(TexHandle handle); +buf_handle ral_buffer_create(u64 size, const void* data); +void ral_buffer_destroy(buf_handle handle); +tex_handle ral_texture_create(texture_desc desc, bool create_view, const void* data); +void ral_texture_destroy(tex_handle handle); // --- Containers (Forward declared as internals are unnecessary for external header) typedef struct u32_darray u32_darray; @@ -316,18 +317,18 @@ DEFINE_HANDLE(MeshHandle); DEFINE_HANDLE(MaterialHandle); DEFINE_HANDLE(ModelHandle); -typedef struct Geometry { - VertexDesc vertex_format; +typedef struct geometry { + vertex_desc vertex_format; void* vertex_data; bool has_indices; // When this is false indexed drawing is not used u32_darray* indices; -} Geometry; +} geometry; typedef struct mesh { - BufHandle vertex_buffer; - BufHandle index_buffer; + buf_handle vertex_buffer; + buf_handle index_buffer; MaterialHandle material; - Geometry geometry; + geometry geometry; // bool is_skinned; // false = its static // Armature armature; // bool is_uploaded; // has the data been uploaded to the GPU @@ -335,12 +336,12 @@ typedef struct mesh { // --- Render primitives -Geometry Geo_CreatePlane(f32 x_scale, f32 z_scale, u32 tiling_u, u32 tiling_v); -Geometry Geo_CreateCuboid(f32 x_scale, f32 y_scale, f32 z_scale); -Geometry Geo_CreateCylinder(f32 radius, f32 height, u32 resolution); -Geometry Geo_CreateCone(f32 radius, f32 height, u32 resolution); -Geometry Geo_CreateUVsphere(f32 radius, u32 north_south_lines, u32 east_west_lines); -Geometry Geo_CreateIcosphere(f32 radius, f32 n_subdivisions); +geometry geo_plane(f32 x_scale, f32 z_scale, u32 tiling_u, u32 tiling_v); +geometry geo_cuboid(f32 x_scale, f32 y_scale, f32 z_scale); +geometry geo_cylinder(f32 radius, f32 height, u32 resolution); +geometry geo_cone(f32 radius, f32 height, u32 resolution); +geometry geo_uv_sphere(f32 radius, u32 north_south_lines, u32 east_west_lines); +geometry geo_ico_sphere(f32 radius, f32 n_subdivisions); // --- Scene / Transform Hierarchy diff --git a/src/log.c b/src/log.c index f0dfd87..2a75201 100644 --- a/src/log.c +++ b/src/log.c @@ -1,6 +1,6 @@ #include -void log_output(char* module, LogLevel level, const char* message, ...) { +void log_output(char* module, loglevel level, const char* message, ...) { char out_msg[4096]; printf("Msg: %s\n", message); -- cgit v1.2.3-70-g09d2 From 16afbddeada7161e931dc261d3404bb5bbc1743d Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 17 Oct 2024 15:54:22 +1100 Subject: add .m on mac. log INFO and module name --- Makefile | 11 ++++++++- examples/triangle.c | 6 ++++- include/celeritas.h | 66 +++++++++++++++++++++++++++++++++++++++++++++-------- src/backend_mtl.c | 0 src/backend_mtl.m | 22 ++++++++++++++++++ src/core.c | 6 ++--- src/log.c | 6 ++++- src/ral.c | 5 ---- 8 files changed, 102 insertions(+), 20 deletions(-) delete mode 100644 src/backend_mtl.c create mode 100644 src/backend_mtl.m (limited to 'src') diff --git a/Makefile b/Makefile index 7c50323..9689dae 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,11 @@ CFLAGS := -Wall -Wextra -O2 $(INCLUDES) LDFLAGS := -lglfw # Detect OS -UNAME_S := $(shell uname -s) +ifeq ($(OS),Windows_NT) + DETECTED_OS := Windows +else + DETECTED_OS := $(shell uname -s) +endif # Directories SRC_DIR := src @@ -18,6 +22,11 @@ EXAMPLES_DIR := examples SRCS := $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/**/*.c) OBJS := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS)) +# Add Metal backend written in Objective C only on Mac platform +ifeq ($(DETECTED_OS),Darwin) + SRCS += $(SRC_DIR)/backend_mtl.m +endif + # Library outputs STATIC_LIB := $(BUILD_DIR)/libceleritas.a ifeq ($(UNAME_S),Darwin) diff --git a/examples/triangle.c b/examples/triangle.c index 1c81d38..83da5e4 100644 --- a/examples/triangle.c +++ b/examples/triangle.c @@ -3,7 +3,11 @@ #include int main() { - Core_Bringup("Celeritas Example: Triangle", NULL); + core_bringup("Celeritas Example: Triangle", NULL); + + while (!app_should_exit()) { + // + } return 0; } diff --git a/include/celeritas.h b/include/celeritas.h index 6645dc6..0437e15 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -60,7 +60,7 @@ _Static_assert(sizeof(ptrdiff_t) == 8, "type ptrdiff_t should be 8 bytes"); // Platform informs renderer backend (unless user overrides) #if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_WINDOWS) #define GPU_VULKAN 1 -#elif defined(CEL_PLATFORM_MAC) +#else #define GPU_METAL 1 #endif @@ -88,10 +88,45 @@ void core_shutdown(); void core_resize_viewport(int width, int height); bool app_should_exit(); +// --- Error handling + +// global error handling +#define CEL_OK 0 +extern int g_last_error; + +/** @brief get last global status value */ +int cel_check_status(); +void _cel_push_error(int error_code); + // --- Memory facilities: Allocators, helpers // TODO: Arenas -// TODO: Pool allocator + +// Pool +typedef struct void_pool_header void_pool_header; // TODO: change name of this +struct void_pool_header { + void_pool_header* next; +}; + +typedef struct void_pool { + u64 capacity; + u64 entry_size; + u64 count; + void* backing_buffer; + void_pool_header* free_list_head; + const char* debug_label; +} void_pool; + +void_pool void_pool_create(void* storage, const char* debug_label, u64 capacity, u64 entry_size); +void void_pool_free_all(void_pool* pool); +bool void_pool_is_empty(void_pool* pool); +bool void_pool_is_full(void_pool* pool); +void* void_pool_get(void_pool* pool, u32 raw_handle); +void* void_pool_alloc(void_pool* pool, u32* out_raw_handle); +void void_pool_dealloc(void_pool* pool, u32 raw_handle); +u32 void_pool_insert(void_pool* pool, void* item); + +// TODO: Typed pool // --- Strings @@ -119,31 +154,31 @@ void log_output(char* module, loglevel level, const char* msg, ...); static inline void ERROR(const char* msg, ...) { \ va_list args; \ va_start(args, msg); \ - log_output(#module, LOG_LEVEL_FATAL, msg, args); \ + log_output(#module, LOG_LEVEL_ERROR, msg, args); \ va_end(args); \ } \ static inline void WARN(const char* msg, ...) { \ va_list args; \ va_start(args, msg); \ - log_output(#module, LOG_LEVEL_FATAL, msg, args); \ + log_output(#module, LOG_LEVEL_WARN, msg, args); \ va_end(args); \ } \ static inline void INFO(const char* msg, ...) { \ va_list args; \ va_start(args, msg); \ - log_output(#module, LOG_LEVEL_FATAL, msg, args); \ + log_output(#module, LOG_LEVEL_INFO, msg, args); \ va_end(args); \ } \ static inline void DEBUG(const char* msg, ...) { \ va_list args; \ va_start(args, msg); \ - log_output(#module, LOG_LEVEL_FATAL, msg, args); \ + log_output(#module, LOG_LEVEL_DEBUG, msg, args); \ va_end(args); \ } \ static inline void TRACE(const char* msg, ...) { \ va_list args; \ va_start(args, msg); \ - log_output(#module, LOG_LEVEL_FATAL, msg, args); \ + log_output(#module, LOG_LEVEL_TRACE, msg, args); \ va_end(args); \ } @@ -203,10 +238,13 @@ DEFINE_HANDLE(pipeline_handle); #define MAX_SHADER_BINDINGS 16 // Backend-specific structs +typedef struct gpu_device gpu_device; typedef struct gpu_swapchain gpu_swapchain; typedef struct gpu_compute_pipeline gpu_compute_pipeline; typedef struct gpu_gfx_pipeline gpu_gfx_pipeline; typedef struct gpu_encoder gpu_encoder; // Command encoder +typedef struct gpu_buffer gpu_buffer; +typedef struct gpu_texture gpu_texture; // NOTE: Can we just use Storage buffer for everything? // typedef enum gpu_buf_type {} gpu_buf_type; @@ -347,16 +385,26 @@ geometry geo_ico_sphere(f32 radius, f32 n_subdivisions); // --- Gameplay -typedef struct Camera { +typedef struct camera { vec3 position; quat orientation; f32 fov; -} Camera; +} camera; // --- Reference Renderer // TODO: Filament PBR model +// --- Game and model data + +DEFINE_HANDLE(model_handle); + +typedef struct model { + +} model; + +model_handle model_load_from_gltf(const char* path); + // --- Animation // --- Collisions diff --git a/src/backend_mtl.c b/src/backend_mtl.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/backend_mtl.m b/src/backend_mtl.m new file mode 100644 index 0000000..9a99e14 --- /dev/null +++ b/src/backend_mtl.m @@ -0,0 +1,22 @@ +#define GPU_METAL 1 + +#ifdef GPU_METAL +#include + +// Obj-C imports +#import +#import +#import +#import + +// --- RAL types +struct gpu_device { + id id; +}; + +struct gpu_swapchain { + int width, height; + CAMetalLayer* swapchain; +}; + +#endif \ No newline at end of file diff --git a/src/core.c b/src/core.c index 79b9d6e..0c3c5ea 100644 --- a/src/core.c +++ b/src/core.c @@ -4,12 +4,12 @@ NAMESPACED_LOGGER(core); -void Core_Bringup(const char* window_name, struct GLFWwindow* optional_window) { +void core_bringup(const char* window_name, struct GLFWwindow* optional_window) { // INFO("Initiate Core bringup"); INFO("Initiate Core bringup"); INFO("Create GLFW window"); } -void Core_Shutdown() {} +void core_shutdown() {} -bool AppShouldExit() { return true; } +bool app_should_exit() { return false; } diff --git a/src/log.c b/src/log.c index 2a75201..5a6ca5c 100644 --- a/src/log.c +++ b/src/log.c @@ -1,7 +1,11 @@ #include +static const char* log_level_strings[] = { + "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" +}; + void log_output(char* module, loglevel level, const char* message, ...) { char out_msg[4096]; - printf("Msg: %s\n", message); + printf("[%s] %s Msg: %s\n", module, log_level_strings[level], message); } diff --git a/src/ral.c b/src/ral.c index 45d202b..e69de29 100644 --- a/src/ral.c +++ b/src/ral.c @@ -1,5 +0,0 @@ -#ifdef GPU_VULKAN - -#elif GPU_METAL - -#endif \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 3e1aea0243f54e0b68baa3b19ac19f3d965484e0 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:49:11 +1100 Subject: start on metal backend --- Makefile | 27 +++++------ TODO.md | 1 + .../render/archive/backends/metal/backend_metal.m | 2 +- examples/triangle.c | 2 +- include/celeritas.h | 19 +++++++- src/backend_mtl.m | 52 ++++++++++++++++++++-- src/core.c | 29 ++++++++++-- 7 files changed, 108 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/Makefile b/Makefile index 9689dae..67b79a4 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,7 @@ CFLAGS := -Wall -Wextra -O2 $(INCLUDES) LDFLAGS := -lglfw # Detect OS -ifeq ($(OS),Windows_NT) - DETECTED_OS := Windows -else - DETECTED_OS := $(shell uname -s) -endif +UNAME_S := $(shell uname -s) # Directories SRC_DIR := src @@ -22,17 +18,14 @@ EXAMPLES_DIR := examples SRCS := $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/**/*.c) OBJS := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS)) -# Add Metal backend written in Objective C only on Mac platform -ifeq ($(DETECTED_OS),Darwin) - SRCS += $(SRC_DIR)/backend_mtl.m -endif - # Library outputs STATIC_LIB := $(BUILD_DIR)/libceleritas.a ifeq ($(UNAME_S),Darwin) SHARED_LIB := $(BUILD_DIR)/libceleritas.dylib SHARED_FLAGS := -dynamiclib - LDFLAGS += -framework Foundation -framework CoreFoundation -framework CoreGraphics -framework AppKit + LDFLAGS += -framework Foundation -framework CoreFoundation -framework CoreGraphics -framework AppKit -framework QuartzCore -framework Metal -framework MetalKit + SRCS += $(SRC_DIR)/backend_mtl.m + OBJS += $(OBJ_DIR)/backend_mtl.o else SHARED_LIB := $(BUILD_DIR)/libceleritas.so SHARED_FLAGS := -shared @@ -47,6 +40,11 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c @mkdir -p $(OBJ_DIR) $(CC) $(CFLAGS) -c $< -o $@ +# Objective C +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.m + @mkdir -p $(OBJ_DIR) + $(CC) $(CFLAGS) -c $< -o $@ + $(SHARED_LIB): $(OBJS) @mkdir -p $(BUILD_DIR) $(CC) $(SHARED_FLAGS) -o $@ $^ $(LDFLAGS) @@ -63,11 +61,10 @@ static: $(STATIC_LIB) all: shared static .PHONY: triangle -triangle: build/triangle.bin - -build/triangle.bin: $(EXAMPLES_DIR)/triangle.c $(STATIC_LIB) +triangle: $(EXAMPLES_DIR)/triangle.c $(SHARED_LIB) @mkdir -p $(BUILD_DIR) - $(CC) $(CFLAGS) $< -o $@ -L$(BUILD_DIR) -lceleritas $(LDFLAGS) + $(CC) $(CFLAGS) $(EXAMPLES_DIR)/triangle.c -L$(BUILD_DIR) -lceleritas $(LDFLAGS) + MTL_DEBUG_LAYER=1 build/triangle.bin .PHONY: clean clean: diff --git a/TODO.md b/TODO.md index 6e255fa..aa01249 100644 --- a/TODO.md +++ b/TODO.md @@ -6,6 +6,7 @@ - compile example - [ ] Consolidate down to a handful of examples - [x] Get rid of doxygen +- [ ] make format - [ ] Move to Vulkan-first rendering - [ ] Build in pipeline (needs vulkan) - [ ] Incorporate vma \ No newline at end of file diff --git a/archive/src/render/archive/backends/metal/backend_metal.m b/archive/src/render/archive/backends/metal/backend_metal.m index 4787755..0e9399e 100644 --- a/archive/src/render/archive/backends/metal/backend_metal.m +++ b/archive/src/render/archive/backends/metal/backend_metal.m @@ -1,5 +1,5 @@ #include -// #define CEL_REND_BACKEND_METAL +#define CEL_REND_BACKEND_METAL #if defined(CEL_REND_BACKEND_METAL) #include #include "ral_types.h" diff --git a/examples/triangle.c b/examples/triangle.c index 83da5e4..ccbcea3 100644 --- a/examples/triangle.c +++ b/examples/triangle.c @@ -6,7 +6,7 @@ int main() { core_bringup("Celeritas Example: Triangle", NULL); while (!app_should_exit()) { - // + glfwPollEvents(); } return 0; diff --git a/include/celeritas.h b/include/celeritas.h index 0437e15..c157918 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -78,6 +78,7 @@ struct GLFWwindow* get_window(); struct core { const char* app_name; struct GLFWwindow* window; + bool should_exit; renderer* renderer; input_state* input; }; @@ -238,7 +239,6 @@ DEFINE_HANDLE(pipeline_handle); #define MAX_SHADER_BINDINGS 16 // Backend-specific structs -typedef struct gpu_device gpu_device; typedef struct gpu_swapchain gpu_swapchain; typedef struct gpu_compute_pipeline gpu_compute_pipeline; typedef struct gpu_gfx_pipeline gpu_gfx_pipeline; @@ -341,11 +341,21 @@ typedef struct gfx_pipeline_desc { } gfx_pipeline_desc; // --- RAL Functions + +// Resources buf_handle ral_buffer_create(u64 size, const void* data); void ral_buffer_destroy(buf_handle handle); tex_handle ral_texture_create(texture_desc desc, bool create_view, const void* data); void ral_texture_destroy(tex_handle handle); +// Backend lifecycle +void ral_backend_init(const char* window_name, struct GLFWwindow* window); +void ral_backend_shutdown(); + +// Frame lifecycle +void ral_frame_start(); +void ral_frame_end(); + // --- Containers (Forward declared as internals are unnecessary for external header) typedef struct u32_darray u32_darray; @@ -381,6 +391,11 @@ geometry geo_cone(f32 radius, f32 height, u32 resolution); geometry geo_uv_sphere(f32 radius, u32 north_south_lines, u32 east_west_lines); geometry geo_ico_sphere(f32 radius, f32 n_subdivisions); +// --- Renderer + +// void renderer_init(renderer* rend); +// void renderer_shutdown(renderer* rend); + // --- Scene / Transform Hierarchy // --- Gameplay @@ -407,6 +422,8 @@ model_handle model_load_from_gltf(const char* path); // --- Animation +// Compute shader approach so we only need one kind of vertex format + // --- Collisions // --- Physics diff --git a/src/backend_mtl.m b/src/backend_mtl.m index 9a99e14..b3cd224 100644 --- a/src/backend_mtl.m +++ b/src/backend_mtl.m @@ -3,20 +3,66 @@ #ifdef GPU_METAL #include +#define MTL_DEBUG_LAYER 1 + // Obj-C imports #import #import #import #import +#define GLFW_INCLUDE_NONE +#import +#define GLFW_EXPOSE_NATIVE_COCOA +#import + // --- RAL types -struct gpu_device { - id id; -}; struct gpu_swapchain { int width, height; CAMetalLayer* swapchain; }; +typedef struct metal_context { + GLFWwindow* window; + NSWindow* metal_window; + + id device; + id surface; + gpu_swapchain default_swapchain; + + id command_queue; +} metal_context; + +static metal_context ctx; + +void ral_backend_init(const char* window_name, struct GLFWwindow* window) { + printf("loading Metal backend\n"); + + printf("gpu device creation\n"); + const id gpu = MTLCreateSystemDefaultDevice(); + ctx.device = gpu; + + printf("window init\n"); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + glfwMakeContextCurrent(window); + NSWindow* nswindow = glfwGetCocoaWindow(window); + ctx.metal_window = nswindow; + + // effectively the "framebuffer" + CAMetalLayer* metal_layer = [CAMetalLayer layer]; + metal_layer.device = gpu; + metal_layer.pixelFormat = MTLPixelFormatBGRA8Unorm; + ctx.metal_window.contentView.layer = metal_layer; + ctx.metal_window.contentView.wantsLayer = true; + + printf("command queue creation\n"); + const id queue = [ctx.device newCommandQueue]; + ctx.command_queue = queue; +} + +void ral_backend_shutdown() { + // no-op +} + #endif \ No newline at end of file diff --git a/src/core.c b/src/core.c index 0c3c5ea..080e806 100644 --- a/src/core.c +++ b/src/core.c @@ -4,12 +4,35 @@ NAMESPACED_LOGGER(core); +core g_core = {0}; + +// forward declares +void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods); + void core_bringup(const char* window_name, struct GLFWwindow* optional_window) { - // INFO("Initiate Core bringup"); INFO("Initiate Core bringup"); INFO("Create GLFW window"); + glfwInit(); + GLFWwindow* glfw_window = glfwCreateWindow(800, 600, window_name, NULL, NULL); + g_core.window = glfw_window; + + // This may move into a renderer struct + ral_backend_init(window_name, glfw_window); + + glfwSetKeyCallback(glfw_window, key_callback); +} +void core_shutdown() { + ral_backend_shutdown(); + glfwTerminate(); +} + +bool app_should_exit() { + return glfwWindowShouldClose(g_core.window) || g_core.should_exit; } -void core_shutdown() {} -bool app_should_exit() { return false; } +void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { + if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) { + g_core.should_exit = true; + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2 From c557763010a8976680f37609ca10e666ff849cd9 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 17 Oct 2024 19:18:38 +1100 Subject: metal triangle! --- Makefile | 17 ++++- README.md | 2 +- assets/shaders/triangle.metal | 38 ++++------- examples/triangle.c | 46 +++++++++++++ include/celeritas.h | 77 +++++++++++++++++++--- src/backend_mtl.m | 150 ++++++++++++++++++++++++++++++++++++++++-- src/core.c | 13 +++- src/log.c | 2 +- src/mem.c | 89 +++++++++++++++++++++++++ 9 files changed, 387 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/Makefile b/Makefile index 67b79a4..cc1c3c0 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,11 @@ EXAMPLES_DIR := examples SRCS := $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/**/*.c) OBJS := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS)) +# Shader files +METAL_SHADERS := $(wildcard $(SHADER_DIR)/*.metal) +METAL_AIR_FILES := $(patsubst $(SHADER_DIR)/%.metal,$(SHADER_OUT_DIR)/%.air,$(METAL_SHADERS)) +METAL_LIB := $(SHADER_OUT_DIR)/default.metallib + # Library outputs STATIC_LIB := $(BUILD_DIR)/libceleritas.a ifeq ($(UNAME_S),Darwin) @@ -57,13 +62,21 @@ shared: $(SHARED_LIB) static: $(STATIC_LIB) +# Shaders +$(SHADER_OUT_DIR)/%.air: $(SHADER_DIR)/%.metal + @mkdir -p $(SHADER_OUT_DIR) + xcrun -sdk macosx metal -c $< -o $@ + +$(METAL_LIB): $(METAL_AIR_FILES) + xcrun -sdk macosx metallib $^ -o $(SHADER_OUT_DIR)/default.metallib + .PHONY: all all: shared static .PHONY: triangle -triangle: $(EXAMPLES_DIR)/triangle.c $(SHARED_LIB) +triangle: $(EXAMPLES_DIR)/triangle.c $(SHARED_LIB) $(SHADER_OUT_DIR)/triangle.air $(METAL_LIB) @mkdir -p $(BUILD_DIR) - $(CC) $(CFLAGS) $(EXAMPLES_DIR)/triangle.c -L$(BUILD_DIR) -lceleritas $(LDFLAGS) + $(CC) $(CFLAGS) $(EXAMPLES_DIR)/triangle.c -L$(BUILD_DIR) -lceleritas $(LDFLAGS) -o $(BUILD_DIR)/triangle.bin MTL_DEBUG_LAYER=1 build/triangle.bin .PHONY: clean diff --git a/README.md b/README.md index 1226c6f..60d59a6 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,4 @@ Renderer Goals: - Check symbols in an 'archive' (static library) - `nm -C build/libcore.a` - Generate compiler_commands.json - - `xmake project -k compile_commands` + - `bear -- make` diff --git a/assets/shaders/triangle.metal b/assets/shaders/triangle.metal index 6055705..6522360 100644 --- a/assets/shaders/triangle.metal +++ b/assets/shaders/triangle.metal @@ -1,33 +1,17 @@ #include - using namespace metal; -struct VertexIn { - float2 position; - float3 color; -}; - -struct VertexOut { - float4 computedPosition [[position]]; - float3 fragColor; -}; - -// Vertex shader -vertex VertexOut basic_vertex( - const device VertexIn* vertex_array [[ buffer(0) ]], - unsigned int vid [[ vertex_id ]] - ) { - VertexIn v = vertex_array[vid]; - - VertexOut outVertex = VertexOut(); - outVertex.computedPosition = float4(v.position.xy, 0.0, 1.0); - outVertex.fragColor = v.color; - return outVertex; +vertex float4 +vertexShader(uint vertexID [[vertex_id]], + constant simd::float3* vertexPositions) +{ + float4 vertexOutPositions = float4(vertexPositions[vertexID][0], + vertexPositions[vertexID][1], + vertexPositions[vertexID][2], + 1.0f); + return vertexOutPositions; } -// Fragment shader -fragment float4 basic_fragment( - VertexOut interpolated [[stage_in]] -) { - return float4(interpolated.fragColor, 1.0); +fragment float4 fragmentShader(float4 vertexOutPositions [[stage_in]]) { + return float4(182.0f/255.0f, 240.0f/255.0f, 228.0f/255.0f, 1.0f); } \ No newline at end of file diff --git a/examples/triangle.c b/examples/triangle.c index ccbcea3..c304562 100644 --- a/examples/triangle.c +++ b/examples/triangle.c @@ -2,11 +2,57 @@ #include +static vec4 vertices[] = { + {-0.5f, -0.5f, 0.0f, 1.0}, + { 0.5f, -0.5f, 0.0f, 1.0}, + { 0.0f, 0.5f, 0.0f, 1.0} +}; + +pipeline_handle draw_pipeline; +buf_handle tri_vert_buffer; + +void draw() { + render_pass_desc d = {}; + gpu_encoder* enc = ral_render_encoder(d); + ral_encode_bind_pipeline(enc, draw_pipeline); + ral_encode_set_vertex_buf(enc, tri_vert_buffer); + ral_encode_draw_tris(enc, 0, 3); + ral_encoder_finish_and_submit(enc); +} + int main() { core_bringup("Celeritas Example: Triangle", NULL); + // create rendering pipeline + gfx_pipeline_desc pipeline_desc = { + .label = "Triangle drawing pipeline", + .vertex_desc = NULL, // TODO + .vertex = { + .source = NULL, + .is_spirv = false, + .entry_point = "vertexShader", + .shader_stage = VISIBILITY_VERTEX, + }, + .fragment = { + .source = NULL, + .is_spirv = false, + .entry_point = "fragmentShader", + .shader_stage = VISIBILITY_FRAGMENT, + }, + }; + + draw_pipeline = ral_gfx_pipeline_create(pipeline_desc); + + // create our buffer to hold vertices + printf("size of vertices %ld\n", sizeof(vec4) * 3); + tri_vert_buffer = ral_buffer_create(sizeof(vec4) * 3, &vertices); + while (!app_should_exit()) { glfwPollEvents(); + + ral_frame_start(); + ral_frame_draw(&draw); + ral_frame_end(); } return 0; diff --git a/include/celeritas.h b/include/celeritas.h index c157918..d4dab98 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include // Third party dependency includes #include @@ -127,7 +129,28 @@ void* void_pool_alloc(void_pool* pool, u32* out_raw_handle); void void_pool_dealloc(void_pool* pool, u32 raw_handle); u32 void_pool_insert(void_pool* pool, void* item); -// TODO: Typed pool +#define TYPED_POOL(T, Name) \ + typedef struct Name##_pool { \ + void_pool inner; \ + } Name##_pool; \ + \ + static Name##_pool Name##_pool_create(void* storage, u64 cap, u64 entry_size) { \ + void_pool p = void_pool_create(storage, "\"" #Name "\"", cap, entry_size); \ + return (Name##_pool){ .inner = p }; \ + } \ + static inline T* Name##_pool_get(Name##_pool* pool, Name##_handle handle) { \ + return (T*)void_pool_get(&pool->inner, handle.raw); \ + } \ + static inline T* Name##_pool_alloc(Name##_pool* pool, Name##_handle* out_handle) { \ + return (T*)void_pool_alloc(&pool->inner, &out_handle->raw); \ + } \ + static inline void Name##_pool_dealloc(Name##_pool* pool, Name##_handle handle) { \ + void_pool_dealloc(&pool->inner, handle.raw); \ + } \ + static Name##_handle Name##_pool_insert(Name##_pool* pool, T* item) { \ + u32 raw_handle = void_pool_insert(pool, item); \ + return (Name##_handle){ .raw = raw_handle }; \ + } // --- Strings @@ -234,15 +257,15 @@ inlined vec3 vec3_div(vec3 u, f32 s); DEFINE_HANDLE(buf_handle); DEFINE_HANDLE(tex_handle); DEFINE_HANDLE(pipeline_handle); +DEFINE_HANDLE(compute_pipeline_handle); #define MAX_VERTEX_ATTRIBUTES 16 #define MAX_SHADER_BINDINGS 16 // Backend-specific structs typedef struct gpu_swapchain gpu_swapchain; -typedef struct gpu_compute_pipeline gpu_compute_pipeline; -typedef struct gpu_gfx_pipeline gpu_gfx_pipeline; -typedef struct gpu_encoder gpu_encoder; // Command encoder +typedef struct gpu_encoder gpu_encoder; // Render command encoder +typedef struct gpu_compute_encoder gpu_compute_encoder; typedef struct gpu_buffer gpu_buffer; typedef struct gpu_texture gpu_texture; @@ -325,21 +348,30 @@ typedef struct shader_data_layout { size_t binding_count; } shader_data_layout; -typedef struct shader_desc { - // TODO -} shader_desc; +typedef struct shader_function { + const char* source; + bool is_spirv; + const char* entry_point; + shader_vis shader_stage; +} shader_function; typedef enum cull_mode { CULL_BACK_FACE, CULL_FRONT_FACE } cull_mode; typedef struct gfx_pipeline_desc { const char* label; vertex_desc vertex_desc; - shader_desc vs; - shader_desc fs; + shader_function vertex; + shader_function fragment; // ShaderDataLayout data_layouts[MAX_SHADER_DATA_LAYOUTS]; // u32 data_layouts_count; } gfx_pipeline_desc; +typedef struct compute_pipeline_desc { /* TODO */ } compute_pipeline_desc; + +typedef struct render_pass_desc { + +} render_pass_desc; + // --- RAL Functions // Resources @@ -348,12 +380,39 @@ void ral_buffer_destroy(buf_handle handle); tex_handle ral_texture_create(texture_desc desc, bool create_view, const void* data); void ral_texture_destroy(tex_handle handle); +// Encoders / cmd buffers +/** @brief grabs a new command encoder from the pool of available ones and begins recording */ +gpu_encoder* ral_render_encoder(render_pass_desc rpass_desc); + +gpu_compute_encoder ral_compute_encoder(); + +void ral_encoder_finish(gpu_encoder* enc); +void ral_encoder_submit(gpu_encoder* enc); +void ral_encoder_finish_and_submit(gpu_encoder* enc); + +pipeline_handle ral_gfx_pipeline_create(gfx_pipeline_desc desc); +void ral_gfx_pipeline_destroy(pipeline_handle handle); + +compute_pipeline_handle ral_compute_pipeline_create(compute_pipeline_desc); +void ral_compute_pipeline_destroy(compute_pipeline_handle handle); + +// Encoding +void ral_encode_bind_pipeline(gpu_encoder* enc, pipeline_handle pipeline); +void ral_encode_set_vertex_buf(gpu_encoder* enc, buf_handle vbuf); +void ral_encode_set_index_buf(gpu_encoder* enc, buf_handle ibuf); +void ral_encode_draw_tris(gpu_encoder* enc, size_t start, size_t count); + // Backend lifecycle void ral_backend_init(const char* window_name, struct GLFWwindow* window); void ral_backend_shutdown(); // Frame lifecycle + +typedef void (*scoped_draw_commands)(); // callback that we run our draw commands within. +// allows us to wrap some api-specific behaviour + void ral_frame_start(); +void ral_frame_draw(scoped_draw_commands draw_fn); void ral_frame_end(); // --- Containers (Forward declared as internals are unnecessary for external header) diff --git a/src/backend_mtl.m b/src/backend_mtl.m index b3cd224..6ff5058 100644 --- a/src/backend_mtl.m +++ b/src/backend_mtl.m @@ -1,7 +1,6 @@ -#define GPU_METAL 1 +#include #ifdef GPU_METAL -#include #define MTL_DEBUG_LAYER 1 @@ -16,6 +15,8 @@ #define GLFW_EXPOSE_NATIVE_COCOA #import +NAMESPACED_LOGGER(metal); + // --- RAL types struct gpu_swapchain { @@ -23,6 +24,22 @@ struct gpu_swapchain { CAMetalLayer* swapchain; }; +struct gpu_encoder { + id cmd_buffer; + id cmd_encoder; +}; + +typedef struct metal_pipeline { + id pso; +} metal_pipeline; + +typedef struct metal_buffer { + id id; +} metal_buffer; + +TYPED_POOL(metal_buffer, buf); +TYPED_POOL(metal_pipeline, pipeline); + typedef struct metal_context { GLFWwindow* window; NSWindow* metal_window; @@ -30,20 +47,25 @@ typedef struct metal_context { id device; id surface; gpu_swapchain default_swapchain; + id default_library; id command_queue; + + /* pools */ + buf_pool bufpool; + pipeline_pool psopool; // pso = pipeline state object } metal_context; static metal_context ctx; void ral_backend_init(const char* window_name, struct GLFWwindow* window) { - printf("loading Metal backend\n"); + TRACE("loading Metal backend"); - printf("gpu device creation\n"); + TRACE("gpu device creation"); const id gpu = MTLCreateSystemDefaultDevice(); ctx.device = gpu; - printf("window init\n"); + TRACE("window init"); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwMakeContextCurrent(window); NSWindow* nswindow = glfwGetCocoaWindow(window); @@ -55,14 +77,130 @@ void ral_backend_init(const char* window_name, struct GLFWwindow* window) { metal_layer.pixelFormat = MTLPixelFormatBGRA8Unorm; ctx.metal_window.contentView.layer = metal_layer; ctx.metal_window.contentView.wantsLayer = true; + ctx.default_swapchain.swapchain = metal_layer; - printf("command queue creation\n"); + TRACE("command queue creation"); const id queue = [ctx.device newCommandQueue]; ctx.command_queue = queue; + + TRACE("resource pool init"); + metal_buffer* buffer_storage = malloc(sizeof(metal_buffer) * 100); + ctx.bufpool = buf_pool_create(buffer_storage, 100, sizeof(metal_buffer)); + + metal_pipeline* pipeline_storage = malloc(sizeof(metal_pipeline) * 100); + ctx.psopool = pipeline_pool_create(pipeline_storage, 100, sizeof(metal_pipeline)); + + TRACE("create default metal lib"); + NSError* nserr = 0x0; + id default_library = [ctx.device newLibraryWithFile:@"build/shaders/default.metallib" error:&nserr]; + if (!default_library) { + ERROR("Error loading metal lib\n"); + exit(1); + } + ctx.default_library = default_library; + + INFO("Successfully initialised Metal RAL backend"); } void ral_backend_shutdown() { // no-op } +buf_handle ral_buffer_create(u64 size, const void *data) { + buf_handle handle; + metal_buffer* buffer = buf_pool_alloc(&ctx.bufpool, &handle); + buffer->id = [ctx.device newBufferWithBytes:data length:size options:MTLResourceStorageModeShared]; + + return handle; +} + +pipeline_handle ral_gfx_pipeline_create(gfx_pipeline_desc desc) { + TRACE("creating graphics pipeline"); + + pipeline_handle handle; + metal_pipeline* p = pipeline_pool_alloc(&ctx.psopool, &handle); + + @autoreleasepool { + // setup vertex and fragment shaders + NSString* vertex_entry_point = [NSString stringWithUTF8String:desc.vertex.entry_point]; + id vertex_func = [ctx.default_library newFunctionWithName:vertex_entry_point]; + assert(vertex_func); + + NSString* fragment_entry_point = [NSString stringWithUTF8String:desc.fragment.entry_point]; + id fragment_func = [ctx.default_library newFunctionWithName:fragment_entry_point]; + assert(fragment_func); + + NSError* err = 0x0; + MTLRenderPipelineDescriptor* pld = [[MTLRenderPipelineDescriptor alloc] init]; + // in auto release pool so dont need to call release() + + [pld setLabel:@"Pipeline"]; + [pld setVertexFunction:vertex_func]; + [pld setFragmentFunction:fragment_func]; + pld.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm; + pld.colorAttachments[0].blendingEnabled = YES; + assert(pld); + + id pso = [ctx.device newRenderPipelineStateWithDescriptor:pld error:&err]; + assert(pso); + p->pso = pso; + } + + return handle; +} + +gpu_encoder* ral_render_encoder(render_pass_desc rpass_desc) { + id buffer = [ctx.command_queue commandBuffer]; + + // create renderpass descriptor + MTLRenderPassDescriptor* rpd = [[MTLRenderPassDescriptor alloc] init]; + MTLRenderPassColorAttachmentDescriptor* cd = rpd.colorAttachments[0]; + [cd setTexture:ctx.surface.texture]; + [cd setLoadAction:MTLLoadActionClear]; + MTLClearColor clearColor = MTLClearColorMake(41.0f/255.0f, 42.0f/255.0f, 48.0f/255.0f, 1.0); + [cd setClearColor:clearColor]; + [cd setStoreAction:MTLStoreActionStore]; + + id encoder = [buffer renderCommandEncoderWithDescriptor:rpd]; + + gpu_encoder* enc = malloc(sizeof(gpu_encoder)); + enc->cmd_buffer = buffer; + enc->cmd_encoder = encoder; + + return enc; +} + +void ral_encoder_finish_and_submit(gpu_encoder* enc) { + [enc->cmd_encoder endEncoding]; + [enc->cmd_buffer presentDrawable:ctx.surface]; + [enc->cmd_buffer commit]; + [enc->cmd_buffer waitUntilCompleted]; +} + +void ral_encode_bind_pipeline(gpu_encoder *enc, pipeline_handle pipeline) { + metal_pipeline* p = pipeline_pool_get(&ctx.psopool, pipeline); + [enc->cmd_encoder setRenderPipelineState:p->pso]; +} + +void ral_encode_set_vertex_buf(gpu_encoder *enc, buf_handle vbuf) { + metal_buffer* b = buf_pool_get(&ctx.bufpool, vbuf); + [enc->cmd_encoder setVertexBuffer:b->id offset:0 atIndex:0 ]; +} + +void ral_encode_draw_tris(gpu_encoder* enc, size_t start, size_t count) { + MTLPrimitiveType tri_primitive = MTLPrimitiveTypeTriangle; + [enc->cmd_encoder drawPrimitives:tri_primitive vertexStart:start vertexCount:count]; +} + +void ral_frame_start() {} + +void ral_frame_draw(scoped_draw_commands draw_fn) { + @autoreleasepool { + ctx.surface = [ctx.default_swapchain.swapchain nextDrawable]; + draw_fn(); + } +} + +void ral_frame_end() {} + #endif \ No newline at end of file diff --git a/src/core.c b/src/core.c index 080e806..210c282 100644 --- a/src/core.c +++ b/src/core.c @@ -1,11 +1,18 @@ // The engine "core" #include +#include NAMESPACED_LOGGER(core); core g_core = {0}; +#ifdef GPU_METAL +static const char* gapi = "Metal"; +#else +static const char* gapi = "Vulkan"; +#endif + // forward declares void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods); @@ -14,7 +21,11 @@ void core_bringup(const char* window_name, struct GLFWwindow* optional_window) { INFO("Create GLFW window"); glfwInit(); - GLFWwindow* glfw_window = glfwCreateWindow(800, 600, window_name, NULL, NULL); + + char* full_window_name = malloc(sizeof(char) * 100); + int _offset = sprintf(full_window_name, "%s (%s)", window_name, gapi); + + GLFWwindow* glfw_window = glfwCreateWindow(800, 600, full_window_name, NULL, NULL); g_core.window = glfw_window; // This may move into a renderer struct diff --git a/src/log.c b/src/log.c index 5a6ca5c..1083f29 100644 --- a/src/log.c +++ b/src/log.c @@ -7,5 +7,5 @@ static const char* log_level_strings[] = { void log_output(char* module, loglevel level, const char* message, ...) { char out_msg[4096]; - printf("[%s] %s Msg: %s\n", module, log_level_strings[level], message); + printf("[%s] %s - %s\n", module, log_level_strings[level], message); } diff --git a/src/mem.c b/src/mem.c index e69de29..ba122d7 100644 --- a/src/mem.c +++ b/src/mem.c @@ -0,0 +1,89 @@ +#include + +void_pool void_pool_create(void* storage, const char* debug_label, u64 capacity, u64 entry_size) { + size_t memory_requirements = capacity * entry_size; + // void* backing_buf = arena_alloc(a, memory_requirements); + + assert(entry_size >= sizeof(void_pool_header)); // TODO: create my own assert with error message + + void_pool pool = { .capacity = capacity, + .entry_size = entry_size, + .count = 0, + .backing_buffer = storage, + .free_list_head = NULL, + .debug_label = debug_label }; + + void_pool_free_all(&pool); + + return pool; +} + +void void_pool_free_all(void_pool* pool) { + // set all entries to be free + for (u64 i = 0; i < pool->capacity; i++) { + void* ptr = &pool->backing_buffer[i * pool->entry_size]; + void_pool_header* free_node = + (void_pool_header*)ptr; // we reuse the actual entry itself to hold the header + if (i == (pool->capacity - 1)) { + // if the last one we make its next pointer NULL indicating its full + free_node->next = NULL; + } + free_node->next = pool->free_list_head; + // now the head points to this entry + pool->free_list_head = free_node; + } +} + +void* void_pool_get(void_pool* pool, u32 raw_handle) { + // An handle is an index into the array essentially + void* ptr = pool->backing_buffer + (raw_handle * pool->entry_size); + return ptr; +} + +void* void_pool_alloc(void_pool* pool, u32* out_raw_handle) { + // get the next free node + if (pool->count == pool->capacity) { + // WARN("Pool is full!"); + return NULL; + } + if (pool->free_list_head == NULL) { + // ERROR("%s Pool is full (head = null)", pool->debug_label); + return NULL; + } + void_pool_header* free_node = pool->free_list_head; + + // What index does this become? + uintptr_t start = (uintptr_t)pool->backing_buffer; + uintptr_t cur = (uintptr_t)free_node; + // TRACE("%ld %ld ", start, cur); + assert(cur > start); + u32 index = (u32)((cur - start) / pool->entry_size); + /* printf("Index %d\n", index); */ + if (out_raw_handle != NULL) { + *out_raw_handle = index; + } + + pool->free_list_head = free_node->next; + + memset(free_node, 0, pool->entry_size); + pool->count++; + return (void*)free_node; +} + +void void_pool_dealloc(void_pool* pool, u32 raw_handle) { + // push free node back onto the free list + void* ptr = void_pool_get(pool, raw_handle); + void_pool_header* freed_node = (void_pool_header*)ptr; + + freed_node->next = pool->free_list_head; + pool->free_list_head = freed_node; + + pool->count--; +} + +u32 void_pool_insert(void_pool* pool, void* item) { + u32 raw_handle; + void* item_dest = void_pool_alloc(pool, &raw_handle); + memcpy(item_dest, item, pool->entry_size); + return raw_handle; +} -- cgit v1.2.3-70-g09d2 From 70175cfce551a6b534771bd2b1dea6cfb417be1f Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 17 Oct 2024 21:44:44 +1100 Subject: starting on cube --- Makefile | 8 +++- assets/textures/mc_grass.jpeg | Bin 0 -> 20480 bytes bindgen/BINDGEN.md | 1 - bindgen/odin/README.md | 1 - examples/cube.c | 57 +++++++++++++++++++++++++++ examples/triangle.c | 38 ++++++++++++++---- include/celeritas.h | 28 ++++++++++++++ src/backend_mtl.m | 61 ++++++++++++++++++++++++++++- src/core.c | 9 +++++ src/geometry.c | 88 ++++++++++++++++++++++++++++++++++++++++++ src/impl.c | 4 ++ src/maths.c | 2 + 12 files changed, 284 insertions(+), 13 deletions(-) create mode 100644 assets/textures/mc_grass.jpeg delete mode 100644 bindgen/odin/README.md create mode 100644 src/geometry.c create mode 100644 src/impl.c (limited to 'src') diff --git a/Makefile b/Makefile index cc1c3c0..46890cd 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC := clang -INCLUDES := -I./include -Ideps/glfw-3.3.8/include/GLFW +INCLUDES := -I./include -Ideps/glfw-3.3.8/include/GLFW -Ideps/stb_image CFLAGS := -Wall -Wextra -O2 $(INCLUDES) LDFLAGS := -lglfw @@ -79,6 +79,12 @@ triangle: $(EXAMPLES_DIR)/triangle.c $(SHARED_LIB) $(SHADER_OUT_DIR)/triangle.ai $(CC) $(CFLAGS) $(EXAMPLES_DIR)/triangle.c -L$(BUILD_DIR) -lceleritas $(LDFLAGS) -o $(BUILD_DIR)/triangle.bin MTL_DEBUG_LAYER=1 build/triangle.bin +.PHONY: cube +cube: $(EXAMPLES_DIR)/cube.c $(SHARED_LIB) $(SHADER_OUT_DIR)/cube.air $(METAL_LIB) + @mkdir -p $(BUILD_DIR) + $(CC) $(CFLAGS) $(EXAMPLES_DIR)/cube.c -L$(BUILD_DIR) -lceleritas $(LDFLAGS) -o $(BUILD_DIR)/cube.bin + MTL_DEBUG_LAYER=1 build/cube.bin + .PHONY: clean clean: rm -rf $(BUILD_DIR) diff --git a/assets/textures/mc_grass.jpeg b/assets/textures/mc_grass.jpeg new file mode 100644 index 0000000..4f87254 Binary files /dev/null and b/assets/textures/mc_grass.jpeg differ diff --git a/bindgen/BINDGEN.md b/bindgen/BINDGEN.md index da7c785..6d32cdf 100644 --- a/bindgen/BINDGEN.md +++ b/bindgen/BINDGEN.md @@ -3,5 +3,4 @@ This is where we host code generation tools for generating bindings to `celerita Planned languages are: - OCaml (due to fairly different semantics we may have an extra high-level wrapper around the C bindings for convenience) -- Odin - Rust diff --git a/bindgen/odin/README.md b/bindgen/odin/README.md deleted file mode 100644 index f87f5c1..0000000 --- a/bindgen/odin/README.md +++ /dev/null @@ -1 +0,0 @@ -# TODO \ No newline at end of file diff --git a/examples/cube.c b/examples/cube.c index e69de29..6642e2a 100644 --- a/examples/cube.c +++ b/examples/cube.c @@ -0,0 +1,57 @@ +#include + +// vertices + +pipeline_handle draw_pipeline; +buf_handle cube_vbuf; +tex_handle texture; + +void draw() { + render_pass_desc d = {}; + gpu_encoder* enc = ral_render_encoder(d); + ral_encode_bind_pipeline(enc, draw_pipeline); + ral_encode_set_vertex_buf(enc, cube_vbuf); + ral_encode_set_texture(enc, texture, 0); + ral_encode_draw_tris(enc, 0, 36); + ral_encoder_finish_and_submit(enc); +} + +int main() { + core_bringup("Celeritas Example: Triangle", NULL); + + // create rendering pipeline + gfx_pipeline_desc pipeline_desc = { + .label = "Textured cube pipeline", + .vertex_desc = static_3d_vertex_format(), + .vertex = { + .source = NULL, + .is_spirv = false, + .entry_point = "cubeVertexShader", + .stage = STAGE_VERTEX, + }, + .fragment = { + .source = NULL, + .is_spirv = false, + .entry_point = "cubeFragmentShader", + .stage = STAGE_FRAGMENT, + }, + }; + + draw_pipeline = ral_gfx_pipeline_create(pipeline_desc); + + // create the cube geometry + geometry cube = geo_cuboid(1.0, 1.0, 1.0); + + // upload vertex data to the gpu + cube_vbuf = ral_buffer_create(64 * 36, cube.vertex_data); + + while (!app_should_exit()) { + glfwPollEvents(); + + ral_frame_start(); + ral_frame_draw(&draw); + ral_frame_end(); + } + + return 0; +} \ No newline at end of file diff --git a/examples/triangle.c b/examples/triangle.c index 401b5ac..e5cf92d 100644 --- a/examples/triangle.c +++ b/examples/triangle.c @@ -2,21 +2,39 @@ #include -static vec4 vertices[] = { - {-0.5f, -0.5f, 0.0f, 1.0}, - { 0.5f, -0.5f, 0.0f, 1.0}, - { 0.0f, 0.5f, 0.0f, 1.0} -}; +// static vec4 vertices[] = { +// {-0.5f, -0.5f, 0.0f, 1.0}, +// { 0.5f, -0.5f, 0.0f, 1.0}, +// { 0.0f, 0.5f, 0.0f, 1.0} +// }; + +typedef struct VertexData { + vec4 position; + vec2 texCoords; + f32 pad1; + f32 pad2; +} VertexData; + +VertexData squareVertices[] = { + {{-0.5, -0.5, 0.5, 1.0f}, {0.0f, 0.0f}, 0.0, 0.0}, + {{-0.5, 0.5, 0.5, 1.0f}, {0.0f, 1.0f}, 0.0, 0.0}, + {{ 0.5, 0.5, 0.5, 1.0f}, {1.0f, 1.0f}, 0.0, 0.0}, + {{-0.5, -0.5, 0.5, 1.0f}, {0.0f, 0.0f}, 0.0, 0.0}, + {{ 0.5, 0.5, 0.5, 1.0f}, {1.0f, 1.0f}, 0.0, 0.0}, + {{ 0.5, -0.5, 0.5, 1.0f}, {1.0f, 0.0f}, 0.0, 0.0} + }; pipeline_handle draw_pipeline; buf_handle tri_vert_buffer; +tex_handle texture; void draw() { render_pass_desc d = {}; gpu_encoder* enc = ral_render_encoder(d); ral_encode_bind_pipeline(enc, draw_pipeline); ral_encode_set_vertex_buf(enc, tri_vert_buffer); - ral_encode_draw_tris(enc, 0, 3); + ral_encode_set_texture(enc, texture, 0); + ral_encode_draw_tris(enc, 0, 6); ral_encoder_finish_and_submit(enc); } @@ -43,9 +61,13 @@ int main() { draw_pipeline = ral_gfx_pipeline_create(pipeline_desc); + // load texture from file + texture = ral_texture_load_from_file("assets/textures/mc_grass.jpeg"); + // create our buffer to hold vertices - printf("size of vertices %ld\n", sizeof(vec4) * 3); - tri_vert_buffer = ral_buffer_create(sizeof(vec4) * 3, &vertices); + size_t buffer_size = sizeof(VertexData) * 6; + printf("size of vertices %ld\n", buffer_size); + tri_vert_buffer = ral_buffer_create(buffer_size, &squareVertices); while (!app_should_exit()) { glfwPollEvents(); diff --git a/include/celeritas.h b/include/celeritas.h index 14e39dd..77b7a9b 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -252,6 +253,25 @@ inlined vec3 vec3_sub(vec3 u, vec3 v); inlined vec3 vec3_mult(vec3 u, f32 s); inlined vec3 vec3_div(vec3 u, f32 s); +inlined vec4 vec4_create(f32 x, f32 y, f32 z, f32 w); + +// helpers + +#define vec3(x,y,z) ((vec3){ x, y, z }) +#define vec4(x,y,z,w) ((vec4){ x, y, z, w }) +inlined vec4 v3tov4(vec3 v3) { return vec4_create(v3.x, v3.y, v3.z, 0.0); } + +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_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); +static const vec3 VEC3_ONES = vec3(1.0, 1.0, 1.0); + + + // --- RAL DEFINE_HANDLE(buf_handle); @@ -307,8 +327,12 @@ typedef struct vertex_desc { const char* label; vertex_attrib_type attributes[MAX_VERTEX_ATTRIBUTES]; u32 attribute_count; + u32 padding; } vertex_desc; +// Some default formats +vertex_desc static_3d_vertex_format(); + typedef enum shader_binding_type { BINDING_BYTES, BINDING_BUFFER, @@ -377,7 +401,9 @@ typedef struct render_pass_desc { // Resources buf_handle ral_buffer_create(u64 size, const void* data); void ral_buffer_destroy(buf_handle handle); + tex_handle ral_texture_create(texture_desc desc, bool create_view, const void* data); +tex_handle ral_texture_load_from_file(const char* filepath); void ral_texture_destroy(tex_handle handle); // Encoders / cmd buffers @@ -400,11 +426,13 @@ void ral_compute_pipeline_destroy(compute_pipeline_handle handle); void ral_encode_bind_pipeline(gpu_encoder* enc, pipeline_handle pipeline); void ral_encode_set_vertex_buf(gpu_encoder* enc, buf_handle vbuf); void ral_encode_set_index_buf(gpu_encoder* enc, buf_handle ibuf); +void ral_encode_set_texture(gpu_encoder* enc, tex_handle texture, u32 slot); void ral_encode_draw_tris(gpu_encoder* enc, size_t start, size_t count); // Backend lifecycle void ral_backend_init(const char* window_name, struct GLFWwindow* window); void ral_backend_shutdown(); +void ral_backend_resize_framebuffer(int width, int height); // Frame lifecycle diff --git a/src/backend_mtl.m b/src/backend_mtl.m index 6ff5058..48e0ab0 100644 --- a/src/backend_mtl.m +++ b/src/backend_mtl.m @@ -2,18 +2,20 @@ #ifdef GPU_METAL -#define MTL_DEBUG_LAYER 1 +#define MTL_DEBUG_LAYER 1 // enable all metal validation layers // Obj-C imports #import #import #import #import +#include #define GLFW_INCLUDE_NONE #import #define GLFW_EXPOSE_NATIVE_COCOA #import +#include "stb_image.h" NAMESPACED_LOGGER(metal); @@ -37,7 +39,12 @@ typedef struct metal_buffer { id id; } metal_buffer; +typedef struct metal_texture { + id id; +} metal_texture; + TYPED_POOL(metal_buffer, buf); +TYPED_POOL(metal_texture, tex); TYPED_POOL(metal_pipeline, pipeline); typedef struct metal_context { @@ -53,6 +60,7 @@ typedef struct metal_context { /* pools */ buf_pool bufpool; + tex_pool texpool; pipeline_pool psopool; // pso = pipeline state object } metal_context; @@ -66,15 +74,18 @@ void ral_backend_init(const char* window_name, struct GLFWwindow* window) { ctx.device = gpu; TRACE("window init"); - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwMakeContextCurrent(window); NSWindow* nswindow = glfwGetCocoaWindow(window); ctx.metal_window = nswindow; + int width, height; + glfwGetFramebufferSize(window, &width, &height); + // effectively the "framebuffer" CAMetalLayer* metal_layer = [CAMetalLayer layer]; metal_layer.device = gpu; metal_layer.pixelFormat = MTLPixelFormatBGRA8Unorm; + metal_layer.drawableSize = CGSizeMake(width, height); ctx.metal_window.contentView.layer = metal_layer; ctx.metal_window.contentView.wantsLayer = true; ctx.default_swapchain.swapchain = metal_layer; @@ -87,6 +98,9 @@ void ral_backend_init(const char* window_name, struct GLFWwindow* window) { metal_buffer* buffer_storage = malloc(sizeof(metal_buffer) * 100); ctx.bufpool = buf_pool_create(buffer_storage, 100, sizeof(metal_buffer)); + metal_texture* texture_storage = malloc(sizeof(metal_texture) * 100); + ctx.texpool = tex_pool_create(texture_storage, 100, sizeof(metal_texture)); + metal_pipeline* pipeline_storage = malloc(sizeof(metal_pipeline) * 100); ctx.psopool = pipeline_pool_create(pipeline_storage, 100, sizeof(metal_pipeline)); @@ -114,6 +128,39 @@ buf_handle ral_buffer_create(u64 size, const void *data) { return handle; } +tex_handle ral_texture_create(texture_desc desc, bool create_view, const void *data) { + tex_handle handle; + metal_texture* texture = tex_pool_alloc(&ctx.texpool, &handle); + + MTLTextureDescriptor* texture_descriptor = [[MTLTextureDescriptor alloc] init]; + [texture_descriptor setPixelFormat:MTLPixelFormatRGBA8Unorm]; + [texture_descriptor setWidth:desc.width]; + [texture_descriptor setHeight:desc.height]; + + texture->id = [ctx.device newTextureWithDescriptor:texture_descriptor]; + + MTLRegion region = MTLRegionMake2D(0, 0, desc.width, desc.height); + u32 bytes_per_row = 4 * desc.width; + + [texture->id replaceRegion:region mipmapLevel:0 withBytes:data bytesPerRow:bytes_per_row]; + + [texture_descriptor release]; + + return handle; +} + +tex_handle ral_texture_load_from_file(const char* filepath) { + texture_desc desc; + + stbi_set_flip_vertically_on_load(true); + unsigned char* image = stbi_load(filepath, &desc.width, &desc.height, &desc.num_channels, STBI_rgb_alpha); + assert(image != NULL); + + tex_handle handle = ral_texture_create(desc, false, image); + stbi_image_free(image); + return handle; +} + pipeline_handle ral_gfx_pipeline_create(gfx_pipeline_desc desc) { TRACE("creating graphics pipeline"); @@ -187,6 +234,11 @@ void ral_encode_set_vertex_buf(gpu_encoder *enc, buf_handle vbuf) { [enc->cmd_encoder setVertexBuffer:b->id offset:0 atIndex:0 ]; } +void ral_encode_set_texture(gpu_encoder* enc, tex_handle texture, u32 slot) { + metal_texture* t = tex_pool_get(&ctx.texpool, texture); + [enc->cmd_encoder setFragmentTexture:t->id atIndex:slot]; +} + void ral_encode_draw_tris(gpu_encoder* enc, size_t start, size_t count) { MTLPrimitiveType tri_primitive = MTLPrimitiveTypeTriangle; [enc->cmd_encoder drawPrimitives:tri_primitive vertexStart:start vertexCount:count]; @@ -203,4 +255,9 @@ void ral_frame_draw(scoped_draw_commands draw_fn) { void ral_frame_end() {} +void ral_backend_resize_framebuffer(int width, int height) { + TRACE("resizing framebuffer"); + ctx.default_swapchain.swapchain.drawableSize = CGSizeMake((float)width, (float)height); +} + #endif \ No newline at end of file diff --git a/src/core.c b/src/core.c index 210c282..9359a6b 100644 --- a/src/core.c +++ b/src/core.c @@ -2,6 +2,7 @@ #include #include +#include "glfw3.h" NAMESPACED_LOGGER(core); @@ -15,6 +16,7 @@ static const char* gapi = "Vulkan"; // forward declares void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods); +void resize_callback(GLFWwindow* win, int width, int height); void core_bringup(const char* window_name, struct GLFWwindow* optional_window) { INFO("Initiate Core bringup"); @@ -22,6 +24,8 @@ void core_bringup(const char* window_name, struct GLFWwindow* optional_window) { INFO("Create GLFW window"); glfwInit(); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + char* full_window_name = malloc(sizeof(char) * 100); int _offset = sprintf(full_window_name, "%s (%s)", window_name, gapi); @@ -32,6 +36,7 @@ void core_bringup(const char* window_name, struct GLFWwindow* optional_window) { ral_backend_init(window_name, glfw_window); glfwSetKeyCallback(glfw_window, key_callback); + glfwSetFramebufferSizeCallback(glfw_window, resize_callback); } void core_shutdown() { ral_backend_shutdown(); @@ -46,4 +51,8 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) { g_core.should_exit = true; } +} + +void resize_callback(GLFWwindow* window, int width, int height) { + ral_backend_resize_framebuffer(width, height); } \ No newline at end of file diff --git a/src/geometry.c b/src/geometry.c new file mode 100644 index 0000000..d9b0aee --- /dev/null +++ b/src/geometry.c @@ -0,0 +1,88 @@ +#include + +typedef struct static_3d_vert { + vec4 pos; + vec4 norm; + vec2 uv; + vec2 pad; +} static_3d_vert; + +vertex_desc static_3d_vertex_format() { + vertex_desc desc; + desc.label = "Static 3D Vertex"; + desc.attributes[0] = ATTR_F32x4; // position + desc.attributes[1] = ATTR_F32x4; // normal + desc.attributes[2] = ATTR_F32x2; // tex coord + desc.attribute_count = 3; + desc.padding = 16; // 16 bytes padding + + return desc; +} + +geometry geo_cuboid(f32 x_scale, f32 y_scale, f32 z_scale) { + vec4 BACK_BOT_LEFT = (vec4){ 0, 0, 0, 0 }; + vec4 BACK_BOT_RIGHT = (vec4){ 1, 0, 0, 0 }; + vec4 BACK_TOP_LEFT = (vec4){ 0, 1, 0, 0 }; + vec4 BACK_TOP_RIGHT = (vec4){ 1, 1, 0, 0 }; + vec4 FRONT_BOT_LEFT = (vec4){ 0, 0, 1, 0 }; + vec4 FRONT_BOT_RIGHT = (vec4){ 1, 0, 1, 0 }; + vec4 FRONT_TOP_LEFT = (vec4){ 0, 1, 1, 0 }; + vec4 FRONT_TOP_RIGHT = (vec4){ 1, 1, 1, 0 }; + + // allocate the data + static_3d_vert* vertices = malloc(36 * 64); + + vertices[0] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = (v3tov4(VEC3_NEG_Z)), .uv = {0, 0}}; + vertices[1] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Z), .uv = {0, 1} }; + vertices[2] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_NEG_Z), .uv = {0, 0} }; + vertices[3] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = v3tov4(VEC3_NEG_Z), .uv = {1, 0} }; + vertices[4] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Z), .uv = {1, 1} }; + vertices[5] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Z), .uv = {0, 1} }; + + // front faces + vertices[6] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_Z), .uv = {0, 1} }; + vertices[7] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Z), .uv = {1, 0} }; + vertices[8] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_Z), .uv = {0, 0} }; + vertices[9] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_Z), .uv = {0, 1} }; + vertices[10] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_Z), .uv = {1, 1} }; + vertices[11] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Z), .uv = {1, 0} }; + + // top faces + vertices[12] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_Y), .uv = {0, 0} }; + vertices[13] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_Y), .uv = {0, 1} }; + vertices[14] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Y), .uv = {1, 1} }; + vertices[15] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_Y), .uv = {0, 0} }; + vertices[16] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Y), .uv = {1, 1} }; + vertices[17] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = v3tov4(VEC3_Y), .uv = {1, 0} }; + + // bottom faces + vertices[18] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Y), .uv = {0, 1} }; + vertices[19] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Y), .uv = {1, 1} }; + vertices[20] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Y), .uv = {0, 1} }; + vertices[21] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Y), .uv = {0, 1} }; + vertices[22] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Y), .uv = {1, 1} }; + vertices[23] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Y), .uv = {0, 1} }; + + // right faces + vertices[24] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_X), .uv = {0, 0} }; + vertices[25] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_X), .uv = {1, 1} }; + vertices[26] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = v3tov4(VEC3_X), .uv = {1, 0} }; + vertices[27] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_X), .uv = {1, 1} }; + vertices[28] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_X), .uv = {0, 0} }; + vertices[29] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_X), .uv = {0, 1} }; + + // left faces + vertices[30] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; + vertices[31] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; + vertices[32] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; + vertices[33] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; + vertices[34] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; + vertices[35] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; + + return (geometry) { + .vertex_format = static_3d_vertex_format(), + .vertex_data = vertices, + .has_indices = false, + .indices = NULL + }; +} \ No newline at end of file diff --git a/src/impl.c b/src/impl.c new file mode 100644 index 0000000..18f2549 --- /dev/null +++ b/src/impl.c @@ -0,0 +1,4 @@ +// For pulling in implementation files of single-header libraries + +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" \ No newline at end of file diff --git a/src/maths.c b/src/maths.c index f12c852..95283e6 100644 --- a/src/maths.c +++ b/src/maths.c @@ -1,3 +1,5 @@ #include vec3 vec3_create(f32 x, f32 y, f32 z) { return (vec3){ x, y, z }; } + +vec4 vec4_create(f32 x, f32 y, f32 z, f32 w) { return (vec4){ x, y, z , w }; } \ No newline at end of file -- cgit v1.2.3-70-g09d2 From df81a840a5276c35df5f35d55610f1bf31487153 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Fri, 18 Oct 2024 00:18:50 +1100 Subject: port a few more math functions --- assets/shaders/cube.metal | 29 +++++++++++++++ examples/cube.c | 21 ++++++++--- include/celeritas.h | 91 +++++++++++++++++++++++++++++++---------------- src/maths.c | 36 ++++++++++++++++++- 4 files changed, 141 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/assets/shaders/cube.metal b/assets/shaders/cube.metal index e69de29..dc7ace1 100644 --- a/assets/shaders/cube.metal +++ b/assets/shaders/cube.metal @@ -0,0 +1,29 @@ +#include +using namespace metal; + +struct VertexData { + float4 position; + float4 normal; + float2 texCoords; +}; + +struct VertexOut { + float4 position [[position]]; + float2 textureCoordinate; +}; + +struct TransformationData { + float4x4 modelMatrix; + float4x4 viewMatrix; + float4x4 perspectiveMatrix; +}; + +vertex VertexOut cubeVertexShader(uint vertexID [[vertex_id]], + constant VertexData* vertexData, + constant TransformationData* transformationData) +{ + VertexOut out; + out.position = transformationData->perspectiveMatrix * transformationData->viewMatrix * transformationData->modelMatrix * vertexData[vertexID].position; + out.textureCoordinate = vertexData[vertexID].texCoords; + return out; +} \ No newline at end of file diff --git a/examples/cube.c b/examples/cube.c index 6642e2a..629a570 100644 --- a/examples/cube.c +++ b/examples/cube.c @@ -1,12 +1,25 @@ #include - -// vertices +#include "glfw3.h" pipeline_handle draw_pipeline; buf_handle cube_vbuf; tex_handle texture; +// transformation data +typedef struct MVPData { + mat4 model; + mat4 view; + mat4 projection; +} MVPData; + void draw() { + // prepare data + mat4 translation_matrix = mat4_translation(vec3(0, 0, -1)); + + f32 angle_degrees = glfwGetTime() / 2.0 * 45.0; + f32 angle = angle_degrees * PI / 180.0; + mat4 rotation_matrix = mat4_rotation(quat_from_axis_angle(VEC3_Y, angle, true)); + render_pass_desc d = {}; gpu_encoder* enc = ral_render_encoder(d); ral_encode_bind_pipeline(enc, draw_pipeline); @@ -32,7 +45,7 @@ int main() { .fragment = { .source = NULL, .is_spirv = false, - .entry_point = "cubeFragmentShader", + .entry_point = "fragmentShader", .stage = STAGE_FRAGMENT, }, }; @@ -54,4 +67,4 @@ int main() { } return 0; -} \ No newline at end of file +} diff --git a/include/celeritas.h b/include/celeritas.h index 77b7a9b..51a9a3d 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -1,13 +1,14 @@ #pragma once // Standard library includes +#include +#include #include #include #include #include #include #include -#include #include // Third party dependency includes @@ -107,7 +108,7 @@ void _cel_push_error(int error_code); // TODO: Arenas // Pool -typedef struct void_pool_header void_pool_header; // TODO: change name of this +typedef struct void_pool_header void_pool_header; // TODO: change name of this struct void_pool_header { void_pool_header* next; }; @@ -130,26 +131,26 @@ void* void_pool_alloc(void_pool* pool, u32* out_raw_handle); void void_pool_dealloc(void_pool* pool, u32 raw_handle); u32 void_pool_insert(void_pool* pool, void* item); -#define TYPED_POOL(T, Name) \ - typedef struct Name##_pool { \ - void_pool inner; \ - } Name##_pool; \ - \ - static Name##_pool Name##_pool_create(void* storage, u64 cap, u64 entry_size) { \ - void_pool p = void_pool_create(storage, "\"" #Name "\"", cap, entry_size); \ - return (Name##_pool){ .inner = p }; \ - } \ +#define TYPED_POOL(T, Name) \ + typedef struct Name##_pool { \ + void_pool inner; \ + } Name##_pool; \ + \ + static Name##_pool Name##_pool_create(void* storage, u64 cap, u64 entry_size) { \ + void_pool p = void_pool_create(storage, "\"" #Name "\"", cap, entry_size); \ + return (Name##_pool){ .inner = p }; \ + } \ static inline T* Name##_pool_get(Name##_pool* pool, Name##_handle handle) { \ - return (T*)void_pool_get(&pool->inner, handle.raw); \ - } \ + return (T*)void_pool_get(&pool->inner, handle.raw); \ + } \ static inline T* Name##_pool_alloc(Name##_pool* pool, Name##_handle* out_handle) { \ - return (T*)void_pool_alloc(&pool->inner, &out_handle->raw); \ - } \ + return (T*)void_pool_alloc(&pool->inner, &out_handle->raw); \ + } \ static inline void Name##_pool_dealloc(Name##_pool* pool, Name##_handle handle) { \ - void_pool_dealloc(&pool->inner, handle.raw); \ - } \ + void_pool_dealloc(&pool->inner, handle.raw); \ + } \ static Name##_handle Name##_pool_insert(Name##_pool* pool, T* item) { \ - u32 raw_handle = void_pool_insert(pool, item); \ + u32 raw_handle = void_pool_insert(pool, item); \ return (Name##_handle){ .raw = raw_handle }; \ } @@ -185,13 +186,13 @@ void log_output(char* module, loglevel level, const char* msg, ...); static inline void WARN(const char* msg, ...) { \ va_list args; \ va_start(args, msg); \ - log_output(#module, LOG_LEVEL_WARN, msg, args); \ + log_output(#module, LOG_LEVEL_WARN, msg, args); \ va_end(args); \ } \ static inline void INFO(const char* msg, ...) { \ va_list args; \ va_start(args, msg); \ - log_output(#module, LOG_LEVEL_INFO, msg, args); \ + log_output(#module, LOG_LEVEL_INFO, msg, args); \ va_end(args); \ } \ static inline void DEBUG(const char* msg, ...) { \ @@ -236,7 +237,6 @@ typedef vec4 quat; typedef struct mat4 { // TODO: use this format for more readable code: vec4 x_axis, y_axis, z_axis, w_axis; f32 data[16]; - } mat4; /** @brief 3D affine transformation */ @@ -247,19 +247,49 @@ typedef struct transform { bool is_dirty; } transform; +_Static_assert(alignof(vec3) == 4, "vec3 is 4 byte aligned"); +_Static_assert(sizeof(vec3) == 12, "vec3 is 12 bytes so has no padding"); +_Static_assert(alignof(vec4) == 4, "vec4 is 4 byte aligned"); + inlined vec3 vec3_create(f32 x, f32 y, f32 z); inlined vec3 vec3_add(vec3 u, vec3 v); inlined vec3 vec3_sub(vec3 u, vec3 v); inlined vec3 vec3_mult(vec3 u, f32 s); inlined vec3 vec3_div(vec3 u, f32 s); +inlined vec3 vec3_len_squared(vec3 a); +inlined f32 vec3_len(vec3 a); +inlined vec3 vec3_negate(vec3 a); +inlined vec3 vec3_normalise(vec3 a); +inlined f32 vec3_dot(vec3 a, vec3 b); +inlined vec3 vec3_cross(vec3 a, vec3 b); inlined vec4 vec4_create(f32 x, f32 y, f32 z, f32 w); +// quaternion functions +inlined quat quat_ident(); +quat quat_from_axis_angle(vec3 axis, f32 angle, bool normalise); +quat quat_slerp(quat a, quat b, f32 percentage); + +// matrix functions +inlined mat4 mat4_ident(); +mat4 mat4_translation(vec3 position); +mat4 mat4_scale(vec3 scale); +mat4 mat4_rotation(quat rotation); +mat4 mat4_mult(mat4 lhs, mat4 rhs); +mat4 mat4_transposed(mat4 m); +mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_clip, f32 far_clip); +mat4 mat4_orthographic(f32 left, f32 right, f32 bottom, f32 top, f32 near_clip, f32 far_clip); +mat4 mat4_look_at(vec3 position, vec3 target, vec3 up); + +// transform functions +inlined transform transform_create(vec3 pos, quat rot, vec3 scale); +mat4 transform_to_mat(transform* tf); + // helpers -#define vec3(x,y,z) ((vec3){ x, y, z }) -#define vec4(x,y,z,w) ((vec4){ x, y, z, w }) -inlined vec4 v3tov4(vec3 v3) { return vec4_create(v3.x, v3.y, v3.z, 0.0); } +#define vec3(x, y, z) ((vec3){ x, y, z }) +#define vec4(x, y, z, w) ((vec4){ x, y, z, w }) +inlined vec4 v3tov4(vec3 v3) { return vec4_create(v3.x, v3.y, v3.z, 1.0); } 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); @@ -270,8 +300,6 @@ 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); static const vec3 VEC3_ONES = vec3(1.0, 1.0, 1.0); - - // --- RAL DEFINE_HANDLE(buf_handle); @@ -284,7 +312,7 @@ DEFINE_HANDLE(compute_pipeline_handle); // Backend-specific structs typedef struct gpu_swapchain gpu_swapchain; -typedef struct gpu_encoder gpu_encoder; // Render command encoder +typedef struct gpu_encoder gpu_encoder; // Render command encoder typedef struct gpu_compute_encoder gpu_compute_encoder; typedef struct gpu_buffer gpu_buffer; typedef struct gpu_texture gpu_texture; @@ -390,10 +418,10 @@ typedef struct gfx_pipeline_desc { // u32 data_layouts_count; } gfx_pipeline_desc; -typedef struct compute_pipeline_desc { /* TODO */ } compute_pipeline_desc; +typedef struct compute_pipeline_desc { /* TODO */ +} compute_pipeline_desc; typedef struct render_pass_desc { - } render_pass_desc; // --- RAL Functions @@ -436,7 +464,7 @@ void ral_backend_resize_framebuffer(int width, int height); // Frame lifecycle -typedef void (*scoped_draw_commands)(); // callback that we run our draw commands within. +typedef void (*scoped_draw_commands)(); // callback that we run our draw commands within. // allows us to wrap some api-specific behaviour void ral_frame_start(); @@ -493,6 +521,8 @@ typedef struct camera { f32 fov; } camera; +mat4 camera_view_proj(camera); + // --- Reference Renderer // TODO: Filament PBR model @@ -502,7 +532,6 @@ typedef struct camera { DEFINE_HANDLE(model_handle); typedef struct model { - } model; model_handle model_load_from_gltf(const char* path); diff --git a/src/maths.c b/src/maths.c index 95283e6..40223c6 100644 --- a/src/maths.c +++ b/src/maths.c @@ -2,4 +2,38 @@ vec3 vec3_create(f32 x, f32 y, f32 z) { return (vec3){ x, y, z }; } -vec4 vec4_create(f32 x, f32 y, f32 z, f32 w) { return (vec4){ x, y, z , w }; } \ No newline at end of file +vec4 vec4_create(f32 x, f32 y, f32 z, f32 w) { return (vec4){ x, y, z, w }; } + +mat4 mat4_ident() { + return (mat4){ .data = { 1.0, 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.0 } }; +} + +mat4 mat4_mult(mat4 lhs, mat4 rhs) { + mat4 out_matrix = mat4_ident(); + + const f32* m1_ptr = lhs.data; + const f32* m2_ptr = rhs.data; + f32* dst_ptr = out_matrix.data; + + for (i32 i = 0; i < 4; ++i) { + for (i32 j = 0; j < 4; ++j) { + *dst_ptr = m1_ptr[0] * m2_ptr[0 + j] + m1_ptr[1] * m2_ptr[4 + j] + m1_ptr[2] * m2_ptr[8 + j] + + m1_ptr[3] * m2_ptr[12 + j]; + dst_ptr++; + } + m1_ptr += 4; + } + + return out_matrix; +} + +mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_z, f32 far_z) { + f32 half_tan_fov = tanf(fov_radians * 0.5f); + mat4 out_matrix = { .data = { 0 } }; + out_matrix.data[0] = 1.0f / (aspect_ratio * half_tan_fov); + out_matrix.data[5] = 1.0f / half_tan_fov; + out_matrix.data[10] = -((far_z + near_z) / (far_z - near_z)); + out_matrix.data[11] = -1.0f; + out_matrix.data[14] = -((2.0f * far_z * near_z) / (far_z - near_z)); + return out_matrix; +} -- cgit v1.2.3-70-g09d2 From 5234133f2d55dc7f24eaa63b86e3952decaaba91 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Fri, 18 Oct 2024 00:23:30 +1100 Subject: chore: format --- .clang-format | 2 +- Makefile | 11 +++++++ examples/triangle.c | 11 +++---- src/core.c | 16 ++++------ src/geometry.c | 87 ++++++++++++++++++++++++++--------------------------- src/log.c | 4 +-- src/maths.c | 4 +-- src/mem.c | 3 +- 8 files changed, 67 insertions(+), 71 deletions(-) (limited to 'src') diff --git a/.clang-format b/.clang-format index 059381e..404bcb1 100644 --- a/.clang-format +++ b/.clang-format @@ -1,7 +1,7 @@ --- Language: Cpp BasedOnStyle: Google -ColumnLimit: 100 +ColumnLimit: 120 Cpp11BracedListStyle: false # {.x = 1.0, .y = 2.0} -> { .x = 1.0, .y = 2.0 } IncludeBlocks: Preserve # sort each block of include statements instead of all DerivePointerAlignment: false diff --git a/Makefile b/Makefile index 46890cd..b4cf4d9 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,9 @@ EXAMPLES_DIR := examples SRCS := $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/**/*.c) OBJS := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS)) +# Format-able files +FORMAT_FILES := include/celeritas.h $(SRC_DIR)/*.c $(EXAMPLES_DIR)/*.c + # Shader files METAL_SHADERS := $(wildcard $(SHADER_DIR)/*.metal) METAL_AIR_FILES := $(patsubst $(SHADER_DIR)/%.metal,$(SHADER_OUT_DIR)/%.air,$(METAL_SHADERS)) @@ -85,6 +88,14 @@ cube: $(EXAMPLES_DIR)/cube.c $(SHARED_LIB) $(SHADER_OUT_DIR)/cube.air $(METAL_LI $(CC) $(CFLAGS) $(EXAMPLES_DIR)/cube.c -L$(BUILD_DIR) -lceleritas $(LDFLAGS) -o $(BUILD_DIR)/cube.bin MTL_DEBUG_LAYER=1 build/cube.bin +.PHONY: format +format: + clang-format -i $(FORMAT_FILES) + +.PHONY: tidy +tidy: + clang-tidy $(SRCS) $(EXAMPLES_DIR)/*.c -- $(CFLAGS) + .PHONY: clean clean: rm -rf $(BUILD_DIR) diff --git a/examples/triangle.c b/examples/triangle.c index e5cf92d..7770fd1 100644 --- a/examples/triangle.c +++ b/examples/triangle.c @@ -16,13 +16,10 @@ typedef struct VertexData { } VertexData; VertexData squareVertices[] = { - {{-0.5, -0.5, 0.5, 1.0f}, {0.0f, 0.0f}, 0.0, 0.0}, - {{-0.5, 0.5, 0.5, 1.0f}, {0.0f, 1.0f}, 0.0, 0.0}, - {{ 0.5, 0.5, 0.5, 1.0f}, {1.0f, 1.0f}, 0.0, 0.0}, - {{-0.5, -0.5, 0.5, 1.0f}, {0.0f, 0.0f}, 0.0, 0.0}, - {{ 0.5, 0.5, 0.5, 1.0f}, {1.0f, 1.0f}, 0.0, 0.0}, - {{ 0.5, -0.5, 0.5, 1.0f}, {1.0f, 0.0f}, 0.0, 0.0} - }; + { { -0.5, -0.5, 0.5, 1.0f }, { 0.0f, 0.0f }, 0.0, 0.0 }, { { -0.5, 0.5, 0.5, 1.0f }, { 0.0f, 1.0f }, 0.0, 0.0 }, + { { 0.5, 0.5, 0.5, 1.0f }, { 1.0f, 1.0f }, 0.0, 0.0 }, { { -0.5, -0.5, 0.5, 1.0f }, { 0.0f, 0.0f }, 0.0, 0.0 }, + { { 0.5, 0.5, 0.5, 1.0f }, { 1.0f, 1.0f }, 0.0, 0.0 }, { { 0.5, -0.5, 0.5, 1.0f }, { 1.0f, 0.0f }, 0.0, 0.0 } +}; pipeline_handle draw_pipeline; buf_handle tri_vert_buffer; diff --git a/src/core.c b/src/core.c index 9359a6b..0cbaa09 100644 --- a/src/core.c +++ b/src/core.c @@ -6,7 +6,7 @@ NAMESPACED_LOGGER(core); -core g_core = {0}; +core g_core = { 0 }; #ifdef GPU_METAL static const char* gapi = "Metal"; @@ -43,16 +43,12 @@ void core_shutdown() { glfwTerminate(); } -bool app_should_exit() { - return glfwWindowShouldClose(g_core.window) || g_core.should_exit; -} +bool app_should_exit() { return glfwWindowShouldClose(g_core.window) || g_core.should_exit; } void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { - if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) { - g_core.should_exit = true; - } + if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) { + g_core.should_exit = true; + } } -void resize_callback(GLFWwindow* window, int width, int height) { - ral_backend_resize_framebuffer(width, height); -} \ No newline at end of file +void resize_callback(GLFWwindow* window, int width, int height) { ral_backend_resize_framebuffer(width, height); } \ No newline at end of file diff --git a/src/geometry.c b/src/geometry.c index d9b0aee..05414d3 100644 --- a/src/geometry.c +++ b/src/geometry.c @@ -10,11 +10,11 @@ typedef struct static_3d_vert { vertex_desc static_3d_vertex_format() { vertex_desc desc; desc.label = "Static 3D Vertex"; - desc.attributes[0] = ATTR_F32x4; // position - desc.attributes[1] = ATTR_F32x4; // normal - desc.attributes[2] = ATTR_F32x2; // tex coord + desc.attributes[0] = ATTR_F32x4; // position + desc.attributes[1] = ATTR_F32x4; // normal + desc.attributes[2] = ATTR_F32x2; // tex coord desc.attribute_count = 3; - desc.padding = 16; // 16 bytes padding + desc.padding = 16; // 16 bytes padding return desc; } @@ -32,57 +32,54 @@ geometry geo_cuboid(f32 x_scale, f32 y_scale, f32 z_scale) { // allocate the data static_3d_vert* vertices = malloc(36 * 64); - vertices[0] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = (v3tov4(VEC3_NEG_Z)), .uv = {0, 0}}; - vertices[1] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Z), .uv = {0, 1} }; - vertices[2] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_NEG_Z), .uv = {0, 0} }; - vertices[3] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = v3tov4(VEC3_NEG_Z), .uv = {1, 0} }; - vertices[4] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Z), .uv = {1, 1} }; - vertices[5] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Z), .uv = {0, 1} }; + vertices[0] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = (v3tov4(VEC3_NEG_Z)), .uv = { 0, 0 } }; + vertices[1] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Z), .uv = { 0, 1 } }; + vertices[2] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_NEG_Z), .uv = { 0, 0 } }; + vertices[3] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = v3tov4(VEC3_NEG_Z), .uv = { 1, 0 } }; + vertices[4] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Z), .uv = { 1, 1 } }; + vertices[5] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Z), .uv = { 0, 1 } }; // front faces - vertices[6] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_Z), .uv = {0, 1} }; - vertices[7] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Z), .uv = {1, 0} }; - vertices[8] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_Z), .uv = {0, 0} }; - vertices[9] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_Z), .uv = {0, 1} }; - vertices[10] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_Z), .uv = {1, 1} }; - vertices[11] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Z), .uv = {1, 0} }; + vertices[6] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_Z), .uv = { 0, 1 } }; + vertices[7] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Z), .uv = { 1, 0 } }; + vertices[8] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_Z), .uv = { 0, 0 } }; + vertices[9] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_Z), .uv = { 0, 1 } }; + vertices[10] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_Z), .uv = { 1, 1 } }; + vertices[11] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Z), .uv = { 1, 0 } }; // top faces - vertices[12] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_Y), .uv = {0, 0} }; - vertices[13] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_Y), .uv = {0, 1} }; - vertices[14] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Y), .uv = {1, 1} }; - vertices[15] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_Y), .uv = {0, 0} }; - vertices[16] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Y), .uv = {1, 1} }; - vertices[17] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = v3tov4(VEC3_Y), .uv = {1, 0} }; + vertices[12] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_Y), .uv = { 0, 0 } }; + vertices[13] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_Y), .uv = { 0, 1 } }; + vertices[14] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Y), .uv = { 1, 1 } }; + vertices[15] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_Y), .uv = { 0, 0 } }; + vertices[16] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_Y), .uv = { 1, 1 } }; + vertices[17] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = v3tov4(VEC3_Y), .uv = { 1, 0 } }; // bottom faces - vertices[18] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Y), .uv = {0, 1} }; - vertices[19] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Y), .uv = {1, 1} }; - vertices[20] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Y), .uv = {0, 1} }; - vertices[21] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Y), .uv = {0, 1} }; - vertices[22] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Y), .uv = {1, 1} }; - vertices[23] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Y), .uv = {0, 1} }; + vertices[18] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Y), .uv = { 0, 1 } }; + vertices[19] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Y), .uv = { 1, 1 } }; + vertices[20] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Y), .uv = { 0, 1 } }; + vertices[21] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_Y), .uv = { 0, 1 } }; + vertices[22] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Y), .uv = { 1, 1 } }; + vertices[23] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_NEG_Y), .uv = { 0, 1 } }; // right faces - vertices[24] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_X), .uv = {0, 0} }; - vertices[25] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_X), .uv = {1, 1} }; - vertices[26] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = v3tov4(VEC3_X), .uv = {1, 0} }; - vertices[27] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_X), .uv = {1, 1} }; - vertices[28] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_X), .uv = {0, 0} }; - vertices[29] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_X), .uv = {0, 1} }; + vertices[24] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_X), .uv = { 0, 0 } }; + vertices[25] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_X), .uv = { 1, 1 } }; + vertices[26] = (static_3d_vert){ .pos = BACK_TOP_RIGHT, .norm = v3tov4(VEC3_X), .uv = { 1, 0 } }; + vertices[27] = (static_3d_vert){ .pos = BACK_BOT_RIGHT, .norm = v3tov4(VEC3_X), .uv = { 1, 1 } }; + vertices[28] = (static_3d_vert){ .pos = FRONT_TOP_RIGHT, .norm = v3tov4(VEC3_X), .uv = { 0, 0 } }; + vertices[29] = (static_3d_vert){ .pos = FRONT_BOT_RIGHT, .norm = v3tov4(VEC3_X), .uv = { 0, 1 } }; // left faces - vertices[30] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; - vertices[31] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; - vertices[32] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; - vertices[33] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; - vertices[34] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; - vertices[35] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = {0, 0} }; + vertices[30] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = { 0, 0 } }; + vertices[31] = (static_3d_vert){ .pos = BACK_TOP_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = { 0, 0 } }; + vertices[32] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = { 0, 0 } }; + vertices[33] = (static_3d_vert){ .pos = BACK_BOT_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = { 0, 0 } }; + vertices[34] = (static_3d_vert){ .pos = FRONT_BOT_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = { 0, 0 } }; + vertices[35] = (static_3d_vert){ .pos = FRONT_TOP_LEFT, .norm = v3tov4(VEC3_NEG_X), .uv = { 0, 0 } }; - return (geometry) { - .vertex_format = static_3d_vertex_format(), - .vertex_data = vertices, - .has_indices = false, - .indices = NULL + return (geometry){ + .vertex_format = static_3d_vertex_format(), .vertex_data = vertices, .has_indices = false, .indices = NULL }; } \ No newline at end of file diff --git a/src/log.c b/src/log.c index 1083f29..66ffe2d 100644 --- a/src/log.c +++ b/src/log.c @@ -1,8 +1,6 @@ #include -static const char* log_level_strings[] = { - "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" -}; +static const char* log_level_strings[] = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" }; void log_output(char* module, loglevel level, const char* message, ...) { char out_msg[4096]; diff --git a/src/maths.c b/src/maths.c index 40223c6..fbfcd72 100644 --- a/src/maths.c +++ b/src/maths.c @@ -4,9 +4,7 @@ vec3 vec3_create(f32 x, f32 y, f32 z) { return (vec3){ x, y, z }; } vec4 vec4_create(f32 x, f32 y, f32 z, f32 w) { return (vec4){ x, y, z, w }; } -mat4 mat4_ident() { - return (mat4){ .data = { 1.0, 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.0 } }; -} +mat4 mat4_ident() { return (mat4){ .data = { 1.0, 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.0 } }; } mat4 mat4_mult(mat4 lhs, mat4 rhs) { mat4 out_matrix = mat4_ident(); diff --git a/src/mem.c b/src/mem.c index ba122d7..dd8acda 100644 --- a/src/mem.c +++ b/src/mem.c @@ -22,8 +22,7 @@ void void_pool_free_all(void_pool* pool) { // set all entries to be free for (u64 i = 0; i < pool->capacity; i++) { void* ptr = &pool->backing_buffer[i * pool->entry_size]; - void_pool_header* free_node = - (void_pool_header*)ptr; // we reuse the actual entry itself to hold the header + void_pool_header* free_node = (void_pool_header*)ptr; // we reuse the actual entry itself to hold the header if (i == (pool->capacity - 1)) { // if the last one we make its next pointer NULL indicating its full free_node->next = NULL; -- cgit v1.2.3-70-g09d2 From f5190745aed65b231e380fa3f25fce46d220dcbc Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Fri, 18 Oct 2024 12:55:02 +1100 Subject: removing some outdated docs files --- TODO.md | 4 +- archive/xmake.lua | 205 ++++++++++++++++++++++++++++++++++ bindgen/rust/celeritas-sys/src/lib.rs | 43 +++---- docs/getting-started.md | 7 -- docs/index.md | 2 +- docs/project-layout.md | 32 ------ include/celeritas.h | 95 ++++++++++++---- src/camera.c | 15 +++ src/mem.c | 2 +- src/scene.c | 26 +++++ xmake.lua | 205 ---------------------------------- 11 files changed, 347 insertions(+), 289 deletions(-) create mode 100644 archive/xmake.lua delete mode 100644 docs/getting-started.md delete mode 100644 docs/project-layout.md create mode 100644 src/camera.c create mode 100644 src/scene.c delete mode 100644 xmake.lua (limited to 'src') diff --git a/TODO.md b/TODO.md index aa01249..00fb831 100644 --- a/TODO.md +++ b/TODO.md @@ -4,9 +4,9 @@ - ~~compile shared lib~~ - compile and run tests - compile example -- [ ] Consolidate down to a handful of examples +- [x] Consolidate down to a handful of examples - [x] Get rid of doxygen - [ ] make format - [ ] Move to Vulkan-first rendering -- [ ] Build in pipeline (needs vulkan) +- [ ] Build in CI pipeline (needs vulkan) - [ ] Incorporate vma \ No newline at end of file diff --git a/archive/xmake.lua b/archive/xmake.lua new file mode 100644 index 0000000..3c05a70 --- /dev/null +++ b/archive/xmake.lua @@ -0,0 +1,205 @@ +-- set_project("celeritas") +-- set_version("0.1.0") +-- set_config("cc", "clang") + +-- add_rules("mode.debug", "mode.release") -- we have two modes: debug & release + +-- -- -Wall : base set of warnings +-- -- -Wextra : additional warnings not covered by -Wall +-- -- -Wundef : undefined macros +-- -- -Wdouble-promotion : catch implicit converion of float to double +-- add_cflags("-Wall", "-Wextra", "-Wundef", "-Wdouble-promotion") + +-- if is_mode("debug") then +-- add_cflags("-g") -- Add debug symbols in debug mode +-- add_defines("CDEBUG") +-- elseif is_mode("release") then +-- add_defines("CRELEASE") +-- end + +-- -- Platform defines and system packages +-- if is_plat("linux") then +-- add_defines("CEL_PLATFORM_LINUX") +-- add_syslinks("dl", "X11", "pthread") --, "vulkan") +-- elseif is_plat("windows") then +-- add_defines("CEL_PLATFORM_WINDOWS") +-- add_syslinks("user32", "gdi32", "kernel32", "shell32") +-- -- add_requires("vulkansdk", { system = true }) +-- -- add_links("pthreadVC2-w64") +-- elseif is_plat("macosx") then +-- add_defines("CEL_PLATFORM_MAC") +-- add_frameworks("Cocoa", "IOKit", "CoreVideo", "OpenGL") +-- add_frameworks("Foundation", "Metal", "QuartzCore") +-- set_runenv("MTL_DEBUG_LAYER", "1") +-- add_requires("vulkansdk", { system = true }) +-- -- add_syslinks("GL") +-- end + +-- -- Compile GLFW from source +-- package("local_glfw") +-- add_deps("cmake") +-- set_sourcedir(path.join(os.scriptdir(), "deps/glfw-3.3.8")) +-- on_install(function(package) +-- local configs = {} +-- -- NOTE(omni): Keeping these around as comments in case we need to modify glfw flags +-- -- table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release")) +-- -- table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF")) +-- import("package.tools.cmake").install(package, configs) +-- end) +-- on_test(function(package) +-- -- assert(package:has_cfuncs("add", {includes = "foo.h"})) +-- end) +-- package_end() + +-- add_requires("local_glfw") + +-- local core_sources = { +-- "deps/glad/src/glad.c", +-- "src/*.c", +-- "src/core/*.c", +-- "src/logos/*.c", +-- "src/maths/*.c", +-- "src/platform/*.c", +-- "src/ral/*.c", +-- "src/ral/backends/opengl/*.c", +-- "src/render/*.c", +-- "src/resources/*.c", +-- "src/std/*.c", +-- "src/std/containers/*.c", +-- "src/systems/*.c", +-- } + +-- local unity_sources = { +-- "deps/Unity/src/unity.c", +-- "deps/Unity/extras/fixture/src/unity_fixture.c", +-- "deps/Unity/extras/memory/src/unity_memory.c", +-- } + +-- rule("compile_glsl_vert_shaders") +-- set_extensions(".vert") +-- on_buildcmd_file(function(target, batchcmds, sourcefile, opt) +-- local targetfile = path.join(target:targetdir(), path.basename(sourcefile) .. ".vert.spv") + +-- print("Compiling shader: %s to %s", sourcefile, targetfile) +-- batchcmds:vrunv('glslc', { sourcefile, "-o", targetfile }) +-- -- batchcmds:add_depfiles(sourcefile) +-- end) + +-- rule("compile_glsl_frag_shaders") +-- set_extensions(".frag") +-- on_buildcmd_file(function(target, batchcmds, sourcefile, opt) +-- local targetfile = path.join(target:targetdir(), path.basename(sourcefile) .. ".frag.spv") + +-- print("Compiling shader: %s to %s", sourcefile, targetfile) +-- batchcmds:vrunv('glslc', { sourcefile, "-o", targetfile }) +-- -- batchcmds:add_depfiles(sourcefile) +-- end) +-- -- TODO: Metal shaders compilation + +-- -- common configuration for both static and shared libraries +-- target("core_config") +-- set_kind("static") -- kind is required but you can ignore it since it's just for common settings +-- add_packages("local_glfw") +-- add_defines("CEL_REND_BACKEND_OPENGL") +-- add_includedirs("deps/cgltf", { public = true }) +-- add_includedirs("deps/glfw-3.3.8/include/GLFW", { public = true }) +-- add_includedirs("deps/glad/include", { public = true }) +-- add_includedirs("deps/stb_image", { public = true }) +-- add_includedirs("deps/stb_image_write", { public = true }) +-- add_includedirs("deps/stb_truetype", { public = true }) +-- add_includedirs("include/", { public = true }) +-- add_includedirs("src/", { public = true }) +-- add_includedirs("src/core", { public = true }) +-- add_includedirs("src/logos/", { public = true }) +-- add_includedirs("src/maths/", { public = true }) +-- add_includedirs("src/platform/", { public = true }) +-- add_includedirs("src/ral", { public = true }) +-- add_includedirs("src/ral/backends/opengl", { public = true }) +-- add_includedirs("src/render", { public = true }) +-- add_includedirs("src/ral/backends/vulkan", { public = true }) +-- add_includedirs("src/resources/", { public = true }) +-- add_includedirs("src/std/", { public = true }) +-- add_includedirs("src/std/containers", { public = true }) +-- add_includedirs("src/systems/", { public = true }) +-- add_files("src/empty.c") -- for some reason we need this on Mac so it doesnt call 'ar' with no files and error +-- -- add_rules("compile_glsl_vert_shaders") +-- -- add_rules("compile_glsl_frag_shaders") +-- -- add_files("assets/shaders/*.frag") +-- if is_plat("windows") then +-- -- add_includedirs("$(env VULKAN_SDK)/Include", { public = true }) +-- -- add_linkdirs("$(env VULKAN_SDK)/Lib", { public = true }) +-- -- add_links("vulkan-1") +-- end +-- if is_plat("macosx") then +-- -- add_files("src/renderer/backends/metal/*.m") +-- -- add_files("src/ral/backends/vulkan/*.c") +-- end +-- set_default(false) -- prevents standalone building of this target + +-- target("core_static") +-- set_kind("static") +-- add_deps("core_config") -- inherit common configurations +-- set_policy("build.merge_archive", true) +-- add_files(core_sources) +-- -- Link against static CRT +-- if is_plat("windows") then +-- add_links("libcmt", "legacy_stdio_definitions") -- for release builds +-- add_links("libcmtd", "legacy_stdio_definitions") -- for debug builds +-- end + +-- target("core_shared") +-- set_kind("shared") +-- add_deps("core_config") -- inherit common configurations +-- add_files(core_sources) +-- -- Link against dynamic CRT +-- if is_plat("windows") then +-- add_links("msvcrt", "legacy_stdio_definitions") -- for release builds +-- add_links("msvcrtd", "legacy_stdio_definitions") -- for debug builds +-- end + +-- -- target("main_loop") +-- -- set_kind("binary") +-- -- set_group("examples") +-- -- add_deps("core_static") +-- -- add_files("examples/main_loop/ex_main_loop.c") +-- -- set_rundir("$(projectdir)") + +-- -- target("tri") +-- -- set_kind("binary") +-- -- set_group("examples") +-- -- add_deps("core_static") +-- -- add_files("examples/triangle/ex_triangle.c") +-- -- set_rundir("$(projectdir)") +-- -- if is_plat("macosx") then +-- -- before_build(function (target) +-- -- print("build metal shaders lib") +-- -- os.exec("mkdir -p build/shaders") +-- -- os.exec("xcrun -sdk macosx metal -c assets/shaders/triangle.metal -o build/shaders/gfx.air") +-- -- os.exec("xcrun -sdk macosx metallib build/shaders/gfx.air -o build/gfx.metallib") +-- -- end) +-- -- end + +-- target("skinned") +-- set_kind("binary") +-- set_group("examples") +-- add_deps("core_shared") +-- add_files("examples/skinned_animation/ex_skinned_animation.c") +-- set_rundir("$(projectdir)") + +-- target("game") +-- set_kind("binary") +-- set_group("examples") +-- add_deps("core_static") +-- add_files("examples/game_demo/game_demo.c") +-- set_rundir("$(projectdir)") + +-- target("pool_tests") +-- set_kind("binary") +-- set_group("tests") +-- add_deps("core_static") +-- add_files(unity_sources) +-- add_includedirs("deps/Unity/src", {public = true}) +-- add_includedirs("deps/Unity/extras/fixture/src", {public = true}) +-- add_includedirs("deps/Unity/extras/memory/src", {public = true}) +-- add_files("tests/pool_tests.c") +-- add_files("tests/pool_test_runner.c") diff --git a/bindgen/rust/celeritas-sys/src/lib.rs b/bindgen/rust/celeritas-sys/src/lib.rs index 3d877a4..5b65f5e 100644 --- a/bindgen/rust/celeritas-sys/src/lib.rs +++ b/bindgen/rust/celeritas-sys/src/lib.rs @@ -6,28 +6,29 @@ use serde::{Deserialize, Serialize}; include!(concat!(env!("OUT_DIR"), "/bindings.rs")); -// // --- Conversions -// pub mod conversions { -// use crate::{Mat4, Vec3, Vec4}; +// --- Conversions +pub mod conversions { + use crate::{mat4, vec3, vec4}; -// impl From for glam::Vec3 { -// fn from(v: Vec3) -> Self { -// Self { -// x: v.x, -// y: v.y, -// z: v.z, -// } -// } -// } -// impl From for Vec3 { -// fn from(v: glam::Vec3) -> Self { -// Self { -// x: v.x, -// y: v.y, -// z: v.z, -// } -// } -// } + impl From for glam::Vec3 { + fn from(v: vec3) -> Self { + Self { + x: v.x, + y: v.y, + z: v.z, + } + } + } + impl From for vec3 { + fn from(v: glam::Vec3) -> Self { + Self { + x: v.x, + y: v.y, + z: v.z, + } + } + } +} // impl From for glam::Vec4 { // fn from(v: Vec4) -> Self { diff --git a/docs/getting-started.md b/docs/getting-started.md deleted file mode 100644 index 0baf21c..0000000 --- a/docs/getting-started.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -title: Getting up and running ---- - -The main build tool we use is [xmake](https://xmake.io/#/getting_started) so installing that is the main prerequisite. - -Once that is installed you *should* be able to simply run `xmake build` in the top-level directory. \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index fd5b8f6..fb3b6e2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -20,7 +20,7 @@ Celeritas is the original Latin word for celerity that the English is derived fr ## Feature Set -[See here (README)](https://github.com/omnisci3nce/celeritas-core/blob/winter-cleaning/README.md#todo) +[See here (README)](https://github.com/omnisci3nce/celeritas-core/blob/masterREADME.md#goals) ## Getting started diff --git a/docs/project-layout.md b/docs/project-layout.md deleted file mode 100644 index 9a33e8f..0000000 --- a/docs/project-layout.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -title: Project Structure ---- - -``` -assets/ - shaders and bundled assets for examples (must be licensed open) -bindgen/ - bindings generation -deps/ - third-party dependencies -docs/ - these docs you're reading now that get built with mkdocs -src/ - core/ - core game engine facilities - logos/ - - maths/ - platform/ - ral/ - render/ - resources/ - std/ - systems/ - ui/ -``` - - -#### Core - -Core holds specifically functionality vital to making games or 3D applications. Contrast this with `std` which contains -code that could form the base layer of almost any software out there. - -#### Std - -Data structures, algorithms, memory management, etc - all code here forms a foundation for everything above it and can conceivably -be reused in non-game applications. \ No newline at end of file diff --git a/include/celeritas.h b/include/celeritas.h index 51a9a3d..6b8a673 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -1,5 +1,13 @@ #pragma once +/* + Common abbreviations: + buf = buffer + tex = texture + desc = description + idx = index +*/ + // Standard library includes #include #include @@ -407,7 +415,7 @@ typedef struct shader_function { shader_stage stage; } shader_function; -typedef enum cull_mode { CULL_BACK_FACE, CULL_FRONT_FACE } cull_mode; +typedef enum cull_mode { Cull_BackFace, Cull_FrontFace } cull_mode; typedef struct gfx_pipeline_desc { const char* label; @@ -476,9 +484,9 @@ typedef struct u32_darray u32_darray; // --- Base Renderer types -DEFINE_HANDLE(MeshHandle); -DEFINE_HANDLE(MaterialHandle); -DEFINE_HANDLE(ModelHandle); +DEFINE_HANDLE(mesh_handle); +DEFINE_HANDLE(material_handle); +DEFINE_HANDLE(model_handle); typedef struct geometry { vertex_desc vertex_format; @@ -487,17 +495,30 @@ typedef struct geometry { u32_darray* indices; } geometry; +typedef u32 joint_idx; +typedef struct armature { +} armature; + +/** @brief Mesh data that has been uploaded to GPU and is ready to be rendered each frame + Gets stored in a pool */ typedef struct mesh { buf_handle vertex_buffer; buf_handle index_buffer; - MaterialHandle material; - geometry geometry; - // bool is_skinned; // false = its static - // Armature armature; - // bool is_uploaded; // has the data been uploaded to the GPU + // todo: material? + geometry geo; // the originating mesh data + const armature* skinning_data; } mesh; -// --- Render primitives +typedef struct draw_mesh_cmd { + mesh_handle mesh; + mat4 transform; + vec3 bounding_sphere_center; + f32 bounding_sphere_radius; + const armature* skinning_data; // NULL = static mesh + bool cast_shadows; +} draw_mesh_cmd; + +// --- Geometry/Mesh primitives geometry geo_plane(f32 x_scale, f32 z_scale, u32 tiling_u, u32 tiling_v); geometry geo_cuboid(f32 x_scale, f32 y_scale, f32 z_scale); @@ -505,31 +526,33 @@ geometry geo_cylinder(f32 radius, f32 height, u32 resolution); geometry geo_cone(f32 radius, f32 height, u32 resolution); geometry geo_uv_sphere(f32 radius, u32 north_south_lines, u32 east_west_lines); geometry geo_ico_sphere(f32 radius, f32 n_subdivisions); +void geo_scale_uniform(geometry* geo, f32 scale); +void geo_scale_xyz(geometry* geo, vec3 scale_xyz); // --- Renderer // void renderer_init(renderer* rend); // void renderer_shutdown(renderer* rend); -// --- Scene / Transform Hierarchy - -// --- Gameplay - typedef struct camera { vec3 position; - quat orientation; + // TODO: move to using a quaternion for the camera's orientation - need to update + // how the view transformation matrix is calculated + vec3 forwards; + vec3 up; f32 fov; } camera; -mat4 camera_view_proj(camera); - -// --- Reference Renderer +/** @brief calculates the view and projection matrices for a camera */ +mat4 camera_view_proj(camera camera, f32 lens_height, f32 lens_width, mat4* out_view, mat4* out_proj); // TODO: Filament PBR model -// --- Game and model data +// --- Scene / Transform Hierarchy -DEFINE_HANDLE(model_handle); +// --- Gameplay + +// --- Game and model data typedef struct model { } model; @@ -538,8 +561,40 @@ model_handle model_load_from_gltf(const char* path); // --- Animation +typedef enum keyframe_kind { Keyframe_Rotation, Keyframe_Translation, Keyframe_Scale, Keyframe_Weights } keyframe_kind; + +const char* keyframe_kind_strings[4] = { "ROTATION", "TRANSLATION", "SCALE", "WEIGHTS" }; + +typedef union keyframe { + quat rotation; + vec3 translation; + vec3 scale; + f32 weights[4]; +} keyframe; + +typedef struct keyframes { + keyframe_kind kind; + keyframe* values; + size_t n_frames; +} keyframes; + +typedef enum interpolation { + Interpolation_Step, + Interpolation_Linear, + Interpolation_Cubic +} interpolation; + +typedef struct animation_spline { + f32* timestamps; + size_t n_timestamps; + keyframes frames; +} animation_spline; + // Compute shader approach so we only need one kind of vertex format +// --- Input + + // --- Collisions // --- Physics diff --git a/src/camera.c b/src/camera.c new file mode 100644 index 0000000..79f9c19 --- /dev/null +++ b/src/camera.c @@ -0,0 +1,15 @@ +#include + +mat4 camera_view_proj(camera camera, f32 lens_height, f32 lens_width, mat4* out_view, mat4* out_proj) { + mat4 projection_matrix = mat4_perspective(camera.fov, lens_width / lens_height, 0.1, 1000.0); + // TODO: store near/far on camera rather than hard-coding here. + + vec3 camera_direction = vec3_add(camera.position, camera.forwards); + mat4 view_matrix = mat4_look_at(camera.position, camera_direction, camera.up); + if (out_view) + *out_view = view_matrix; + if (out_proj) + *out_proj = projection_matrix; + + return mat4_mult(view_matrix, projection_matrix); +} \ No newline at end of file diff --git a/src/mem.c b/src/mem.c index dd8acda..2649db5 100644 --- a/src/mem.c +++ b/src/mem.c @@ -1,7 +1,7 @@ #include void_pool void_pool_create(void* storage, const char* debug_label, u64 capacity, u64 entry_size) { - size_t memory_requirements = capacity * entry_size; + size_t _memory_requirements = capacity * entry_size; // void* backing_buf = arena_alloc(a, memory_requirements); assert(entry_size >= sizeof(void_pool_header)); // TODO: create my own assert with error message diff --git a/src/scene.c b/src/scene.c new file mode 100644 index 0000000..fa4d9b5 --- /dev/null +++ b/src/scene.c @@ -0,0 +1,26 @@ +/** + * @file scene.c + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-10-18 + * + * @copyright Copyright (c) 2024 + * + */ +#include + +// Retained mode scene tree that handles performant transform propagation, and allows systems, or other languages via bindings, to +// manipulate rendering/scene data without *owning* said data. + +typedef struct scene_tree_node { + const char* label; +} scene_tree_node; + +DEFINE_HANDLE(scene_node_handle); +TYPED_POOL(scene_tree_node, scene_node); + +typedef struct render_scene_tree { +} render_scene_tree; + +// What kind of operations and mutations can we perform on the tree? diff --git a/xmake.lua b/xmake.lua deleted file mode 100644 index 3c05a70..0000000 --- a/xmake.lua +++ /dev/null @@ -1,205 +0,0 @@ --- set_project("celeritas") --- set_version("0.1.0") --- set_config("cc", "clang") - --- add_rules("mode.debug", "mode.release") -- we have two modes: debug & release - --- -- -Wall : base set of warnings --- -- -Wextra : additional warnings not covered by -Wall --- -- -Wundef : undefined macros --- -- -Wdouble-promotion : catch implicit converion of float to double --- add_cflags("-Wall", "-Wextra", "-Wundef", "-Wdouble-promotion") - --- if is_mode("debug") then --- add_cflags("-g") -- Add debug symbols in debug mode --- add_defines("CDEBUG") --- elseif is_mode("release") then --- add_defines("CRELEASE") --- end - --- -- Platform defines and system packages --- if is_plat("linux") then --- add_defines("CEL_PLATFORM_LINUX") --- add_syslinks("dl", "X11", "pthread") --, "vulkan") --- elseif is_plat("windows") then --- add_defines("CEL_PLATFORM_WINDOWS") --- add_syslinks("user32", "gdi32", "kernel32", "shell32") --- -- add_requires("vulkansdk", { system = true }) --- -- add_links("pthreadVC2-w64") --- elseif is_plat("macosx") then --- add_defines("CEL_PLATFORM_MAC") --- add_frameworks("Cocoa", "IOKit", "CoreVideo", "OpenGL") --- add_frameworks("Foundation", "Metal", "QuartzCore") --- set_runenv("MTL_DEBUG_LAYER", "1") --- add_requires("vulkansdk", { system = true }) --- -- add_syslinks("GL") --- end - --- -- Compile GLFW from source --- package("local_glfw") --- add_deps("cmake") --- set_sourcedir(path.join(os.scriptdir(), "deps/glfw-3.3.8")) --- on_install(function(package) --- local configs = {} --- -- NOTE(omni): Keeping these around as comments in case we need to modify glfw flags --- -- table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release")) --- -- table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF")) --- import("package.tools.cmake").install(package, configs) --- end) --- on_test(function(package) --- -- assert(package:has_cfuncs("add", {includes = "foo.h"})) --- end) --- package_end() - --- add_requires("local_glfw") - --- local core_sources = { --- "deps/glad/src/glad.c", --- "src/*.c", --- "src/core/*.c", --- "src/logos/*.c", --- "src/maths/*.c", --- "src/platform/*.c", --- "src/ral/*.c", --- "src/ral/backends/opengl/*.c", --- "src/render/*.c", --- "src/resources/*.c", --- "src/std/*.c", --- "src/std/containers/*.c", --- "src/systems/*.c", --- } - --- local unity_sources = { --- "deps/Unity/src/unity.c", --- "deps/Unity/extras/fixture/src/unity_fixture.c", --- "deps/Unity/extras/memory/src/unity_memory.c", --- } - --- rule("compile_glsl_vert_shaders") --- set_extensions(".vert") --- on_buildcmd_file(function(target, batchcmds, sourcefile, opt) --- local targetfile = path.join(target:targetdir(), path.basename(sourcefile) .. ".vert.spv") - --- print("Compiling shader: %s to %s", sourcefile, targetfile) --- batchcmds:vrunv('glslc', { sourcefile, "-o", targetfile }) --- -- batchcmds:add_depfiles(sourcefile) --- end) - --- rule("compile_glsl_frag_shaders") --- set_extensions(".frag") --- on_buildcmd_file(function(target, batchcmds, sourcefile, opt) --- local targetfile = path.join(target:targetdir(), path.basename(sourcefile) .. ".frag.spv") - --- print("Compiling shader: %s to %s", sourcefile, targetfile) --- batchcmds:vrunv('glslc', { sourcefile, "-o", targetfile }) --- -- batchcmds:add_depfiles(sourcefile) --- end) --- -- TODO: Metal shaders compilation - --- -- common configuration for both static and shared libraries --- target("core_config") --- set_kind("static") -- kind is required but you can ignore it since it's just for common settings --- add_packages("local_glfw") --- add_defines("CEL_REND_BACKEND_OPENGL") --- add_includedirs("deps/cgltf", { public = true }) --- add_includedirs("deps/glfw-3.3.8/include/GLFW", { public = true }) --- add_includedirs("deps/glad/include", { public = true }) --- add_includedirs("deps/stb_image", { public = true }) --- add_includedirs("deps/stb_image_write", { public = true }) --- add_includedirs("deps/stb_truetype", { public = true }) --- add_includedirs("include/", { public = true }) --- add_includedirs("src/", { public = true }) --- add_includedirs("src/core", { public = true }) --- add_includedirs("src/logos/", { public = true }) --- add_includedirs("src/maths/", { public = true }) --- add_includedirs("src/platform/", { public = true }) --- add_includedirs("src/ral", { public = true }) --- add_includedirs("src/ral/backends/opengl", { public = true }) --- add_includedirs("src/render", { public = true }) --- add_includedirs("src/ral/backends/vulkan", { public = true }) --- add_includedirs("src/resources/", { public = true }) --- add_includedirs("src/std/", { public = true }) --- add_includedirs("src/std/containers", { public = true }) --- add_includedirs("src/systems/", { public = true }) --- add_files("src/empty.c") -- for some reason we need this on Mac so it doesnt call 'ar' with no files and error --- -- add_rules("compile_glsl_vert_shaders") --- -- add_rules("compile_glsl_frag_shaders") --- -- add_files("assets/shaders/*.frag") --- if is_plat("windows") then --- -- add_includedirs("$(env VULKAN_SDK)/Include", { public = true }) --- -- add_linkdirs("$(env VULKAN_SDK)/Lib", { public = true }) --- -- add_links("vulkan-1") --- end --- if is_plat("macosx") then --- -- add_files("src/renderer/backends/metal/*.m") --- -- add_files("src/ral/backends/vulkan/*.c") --- end --- set_default(false) -- prevents standalone building of this target - --- target("core_static") --- set_kind("static") --- add_deps("core_config") -- inherit common configurations --- set_policy("build.merge_archive", true) --- add_files(core_sources) --- -- Link against static CRT --- if is_plat("windows") then --- add_links("libcmt", "legacy_stdio_definitions") -- for release builds --- add_links("libcmtd", "legacy_stdio_definitions") -- for debug builds --- end - --- target("core_shared") --- set_kind("shared") --- add_deps("core_config") -- inherit common configurations --- add_files(core_sources) --- -- Link against dynamic CRT --- if is_plat("windows") then --- add_links("msvcrt", "legacy_stdio_definitions") -- for release builds --- add_links("msvcrtd", "legacy_stdio_definitions") -- for debug builds --- end - --- -- target("main_loop") --- -- set_kind("binary") --- -- set_group("examples") --- -- add_deps("core_static") --- -- add_files("examples/main_loop/ex_main_loop.c") --- -- set_rundir("$(projectdir)") - --- -- target("tri") --- -- set_kind("binary") --- -- set_group("examples") --- -- add_deps("core_static") --- -- add_files("examples/triangle/ex_triangle.c") --- -- set_rundir("$(projectdir)") --- -- if is_plat("macosx") then --- -- before_build(function (target) --- -- print("build metal shaders lib") --- -- os.exec("mkdir -p build/shaders") --- -- os.exec("xcrun -sdk macosx metal -c assets/shaders/triangle.metal -o build/shaders/gfx.air") --- -- os.exec("xcrun -sdk macosx metallib build/shaders/gfx.air -o build/gfx.metallib") --- -- end) --- -- end - --- target("skinned") --- set_kind("binary") --- set_group("examples") --- add_deps("core_shared") --- add_files("examples/skinned_animation/ex_skinned_animation.c") --- set_rundir("$(projectdir)") - --- target("game") --- set_kind("binary") --- set_group("examples") --- add_deps("core_static") --- add_files("examples/game_demo/game_demo.c") --- set_rundir("$(projectdir)") - --- target("pool_tests") --- set_kind("binary") --- set_group("tests") --- add_deps("core_static") --- add_files(unity_sources) --- add_includedirs("deps/Unity/src", {public = true}) --- add_includedirs("deps/Unity/extras/fixture/src", {public = true}) --- add_includedirs("deps/Unity/extras/memory/src", {public = true}) --- add_files("tests/pool_tests.c") --- add_files("tests/pool_test_runner.c") -- cgit v1.2.3-70-g09d2 From 68f590c848e0e91d70e7116655464f392ed6df46 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:40:34 +1100 Subject: chore: format --- include/celeritas.h | 7 +------ src/camera.c | 6 ++---- src/scene.c | 10 +++++----- 3 files changed, 8 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/include/celeritas.h b/include/celeritas.h index 6b8a673..534fb85 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -578,11 +578,7 @@ typedef struct keyframes { size_t n_frames; } keyframes; -typedef enum interpolation { - Interpolation_Step, - Interpolation_Linear, - Interpolation_Cubic -} interpolation; +typedef enum interpolation { Interpolation_Step, Interpolation_Linear, Interpolation_Cubic } interpolation; typedef struct animation_spline { f32* timestamps; @@ -594,7 +590,6 @@ typedef struct animation_spline { // --- Input - // --- Collisions // --- Physics diff --git a/src/camera.c b/src/camera.c index 79f9c19..eb8fc7d 100644 --- a/src/camera.c +++ b/src/camera.c @@ -6,10 +6,8 @@ mat4 camera_view_proj(camera camera, f32 lens_height, f32 lens_width, mat4* out_ vec3 camera_direction = vec3_add(camera.position, camera.forwards); mat4 view_matrix = mat4_look_at(camera.position, camera_direction, camera.up); - if (out_view) - *out_view = view_matrix; - if (out_proj) - *out_proj = projection_matrix; + if (out_view) *out_view = view_matrix; + if (out_proj) *out_proj = projection_matrix; return mat4_mult(view_matrix, projection_matrix); } \ No newline at end of file diff --git a/src/scene.c b/src/scene.c index fa4d9b5..26e19dc 100644 --- a/src/scene.c +++ b/src/scene.c @@ -1,17 +1,17 @@ /** * @file scene.c * @author your name (you@domain.com) - * @brief + * @brief * @version 0.1 * @date 2024-10-18 - * + * * @copyright Copyright (c) 2024 - * + * */ #include -// Retained mode scene tree that handles performant transform propagation, and allows systems, or other languages via bindings, to -// manipulate rendering/scene data without *owning* said data. +// Retained mode scene tree that handles performant transform propagation, and allows systems, or other languages via +// bindings, to manipulate rendering/scene data without *owning* said data. typedef struct scene_tree_node { const char* label; -- cgit v1.2.3-70-g09d2 From 28bd234a45995b62544e6b5f70d40dfe39ef2e99 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:54:31 +1100 Subject: recompile .o files when header changes --- Makefile | 2 +- TODO.md | 2 +- include/celeritas.h | 4 +++- src/core.c | 17 +++++++++++------ src/debug_strings.c | 5 +++++ src/maths.c | 8 ++++++++ 6 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 src/debug_strings.c (limited to 'src') diff --git a/Makefile b/Makefile index 0c88a03..597ecdc 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ endif # $^ - prerequisites of current rule separated by spaces # $< - first prerequisite file only -$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c include/celeritas.h @mkdir -p $(OBJ_DIR) $(CC) $(CFLAGS) -c $< -o $@ diff --git a/TODO.md b/TODO.md index 00fb831..c4aac15 100644 --- a/TODO.md +++ b/TODO.md @@ -6,7 +6,7 @@ - compile example - [x] Consolidate down to a handful of examples - [x] Get rid of doxygen -- [ ] make format +- [x] make format - [ ] Move to Vulkan-first rendering - [ ] Build in CI pipeline (needs vulkan) - [ ] Incorporate vma \ No newline at end of file diff --git a/include/celeritas.h b/include/celeritas.h index 534fb85..0b56bf5 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -563,7 +563,7 @@ model_handle model_load_from_gltf(const char* path); typedef enum keyframe_kind { Keyframe_Rotation, Keyframe_Translation, Keyframe_Scale, Keyframe_Weights } keyframe_kind; -const char* keyframe_kind_strings[4] = { "ROTATION", "TRANSLATION", "SCALE", "WEIGHTS" }; +extern const char* keyframe_kind_strings[4]; typedef union keyframe { quat rotation; @@ -580,6 +580,8 @@ typedef struct keyframes { typedef enum interpolation { Interpolation_Step, Interpolation_Linear, Interpolation_Cubic } interpolation; +extern const char* interpolation_strings[3]; + typedef struct animation_spline { f32* timestamps; size_t n_timestamps; diff --git a/src/core.c b/src/core.c index 0cbaa09..e1ffeab 100644 --- a/src/core.c +++ b/src/core.c @@ -27,16 +27,21 @@ void core_bringup(const char* window_name, struct GLFWwindow* optional_window) { glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); char* full_window_name = malloc(sizeof(char) * 100); - int _offset = sprintf(full_window_name, "%s (%s)", window_name, gapi); + sprintf(full_window_name, "%s (%s)", window_name, gapi); + + if (optional_window) { + g_core.window = optional_window; + } else { + GLFWwindow* glfw_window = glfwCreateWindow(800, 600, full_window_name, NULL, NULL); + g_core.window = glfw_window; + } - GLFWwindow* glfw_window = glfwCreateWindow(800, 600, full_window_name, NULL, NULL); - g_core.window = glfw_window; // This may move into a renderer struct - ral_backend_init(window_name, glfw_window); + ral_backend_init(window_name, g_core.window); - glfwSetKeyCallback(glfw_window, key_callback); - glfwSetFramebufferSizeCallback(glfw_window, resize_callback); + glfwSetKeyCallback(g_core.window, key_callback); + glfwSetFramebufferSizeCallback(g_core.window, resize_callback); } void core_shutdown() { ral_backend_shutdown(); diff --git a/src/debug_strings.c b/src/debug_strings.c new file mode 100644 index 0000000..b1e09aa --- /dev/null +++ b/src/debug_strings.c @@ -0,0 +1,5 @@ +#include + +const char* keyframe_kind_strings[4] = { "ROTATION", "TRANSLATION", "SCALE", "WEIGHTS" }; + +const char* interpolation_strings[3] = { "Step", "Linear", "Cubic" }; \ No newline at end of file diff --git a/src/maths.c b/src/maths.c index fbfcd72..10fa9e0 100644 --- a/src/maths.c +++ b/src/maths.c @@ -2,6 +2,10 @@ vec3 vec3_create(f32 x, f32 y, f32 z) { return (vec3){ x, y, z }; } +vec3 vec3_add(vec3 u, vec3 v) { + return (vec3){ .x = u.x + v.x, .y = u.y + v.y, .z = u.z + v.z }; +} + vec4 vec4_create(f32 x, f32 y, f32 z, f32 w) { return (vec4){ x, y, z, w }; } mat4 mat4_ident() { return (mat4){ .data = { 1.0, 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.0 } }; } @@ -35,3 +39,7 @@ mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_z, f32 far_z) out_matrix.data[14] = -((2.0f * far_z * near_z) / (far_z - near_z)); return out_matrix; } + +mat4 mat4_look_at(vec3 position, vec3 target, vec3 up) { + // TODO +} \ No newline at end of file -- cgit v1.2.3-70-g09d2 From d6d742f8a24490f885e4a1ae3fea7ec46b01f58c Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:54:52 +1100 Subject: chore: format --- src/core.c | 1 - src/maths.c | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) (limited to 'src') diff --git a/src/core.c b/src/core.c index e1ffeab..ed09597 100644 --- a/src/core.c +++ b/src/core.c @@ -36,7 +36,6 @@ void core_bringup(const char* window_name, struct GLFWwindow* optional_window) { g_core.window = glfw_window; } - // This may move into a renderer struct ral_backend_init(window_name, g_core.window); diff --git a/src/maths.c b/src/maths.c index 10fa9e0..c1fab2a 100644 --- a/src/maths.c +++ b/src/maths.c @@ -2,9 +2,7 @@ vec3 vec3_create(f32 x, f32 y, f32 z) { return (vec3){ x, y, z }; } -vec3 vec3_add(vec3 u, vec3 v) { - return (vec3){ .x = u.x + v.x, .y = u.y + v.y, .z = u.z + v.z }; -} +vec3 vec3_add(vec3 u, vec3 v) { return (vec3){ .x = u.x + v.x, .y = u.y + v.y, .z = u.z + v.z }; } vec4 vec4_create(f32 x, f32 y, f32 z, f32 w) { return (vec4){ x, y, z, w }; } -- cgit v1.2.3-70-g09d2