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 --- src/log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/log.c') 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/log.c') 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 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/log.c') 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 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/log.c') 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