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