From bbe1950e893bed6a9e39954b75dc6aa23f84d59c Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 17 Oct 2024 15:06:22 +1100 Subject: changing names --- TODO.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'TODO.md') diff --git a/TODO.md b/TODO.md index a374d06..6e255fa 100644 --- a/TODO.md +++ b/TODO.md @@ -7,4 +7,5 @@ - [ ] Consolidate down to a handful of examples - [x] Get rid of doxygen - [ ] Move to Vulkan-first rendering -- [ ] Build in pipeline (needs vulkan) \ No newline at end of file +- [ ] Build in pipeline (needs vulkan) +- [ ] Incorporate vma \ 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 'TODO.md') 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 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 'TODO.md') 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 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 'TODO.md') 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