diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-10-27 16:25:27 +1100 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-10-27 16:25:27 +1100 |
commit | c2812d1a79a1d790b2c549abdfe9344d4fb4f4d2 (patch) | |
tree | 58cb7b3e05b8327f6b2395ee0df38a0d4b674af0 | |
parent | 7577b6d51c5180ec23f93cec79a5c77e54130558 (diff) |
removing old ocaml glue code
-rw-r--r-- | bindgen/BINDGEN.md | 2 | ||||
-rw-r--r-- | bindgen/ocaml/bindings/bindings_sys.ml | 49 | ||||
-rw-r--r-- | bindgen/ocaml/bindings/caml_bindings.c | 132 | ||||
-rw-r--r-- | bindgen/ocaml/bindings/celeritas.h | 21 | ||||
-rw-r--r-- | bindgen/ocaml/bindings/dune | 12 | ||||
-rw-r--r-- | include/celeritas.h | 112 | ||||
-rw-r--r-- | src/maths.c | 10 | ||||
-rw-r--r-- | src/mem.c | 4 |
8 files changed, 105 insertions, 237 deletions
diff --git a/bindgen/BINDGEN.md b/bindgen/BINDGEN.md index 6d32cdf..b8b8fc5 100644 --- a/bindgen/BINDGEN.md +++ b/bindgen/BINDGEN.md @@ -2,5 +2,5 @@ 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) - Rust +- OCaml (due to fairly different semantics we may have an extra high-level wrapper around the C bindings for convenience) diff --git a/bindgen/ocaml/bindings/bindings_sys.ml b/bindgen/ocaml/bindings/bindings_sys.ml deleted file mode 100644 index 1c100e9..0000000 --- a/bindgen/ocaml/bindings/bindings_sys.ml +++ /dev/null @@ -1,49 +0,0 @@ -(* automatically generated by ocaml-bindgen 0.0.1 *) - -type lifetime = - | Function - (** The value can live for the lifetime of the function call, which upon return will signal that the - value can be dropped (finalizer?) *) - | Ocaml (** The value is managed by the OCaml runtime *) - | C - (** The value is allocated and passed to C which is then in charge of cleaning it up *) - -type 'a cptr = { lifetime : lifetime; addr : nativeint } - -external bindgen_alloc : size:int -> nativeint = "bindgen_alloc" -external bindgen_free : nativeint -> unit = "bindgen_free" -external bindgen_alloc_string : string -> nativeint = "bindgen_alloc_string" - -let sizeof _ = 4 (* TODO: how to handle different types? *) - -let create_ptr (value : 'a) : 'a cptr = - let addr = bindgen_alloc ~size:(sizeof value) in - print_endline ("Addr: " ^ Nativeint.to_string addr); - Gc.finalise bindgen_free addr; - { lifetime = Ocaml; addr } - -let make_cstr (s: string) : char cptr = - let addr = bindgen_alloc_string s in - { lifetime = Ocaml; addr } - -type nonrec core -type nonrec glfwwindow -external get_global_core : unit -> core cptr = "caml_get_global_core" -external core_Bringup : - optional_window:unit cptr -> unit = "caml_core_Bringup" -external frame_Begin : unit -> unit = "caml_frame_Begin" -external frame_Draw : unit -> unit = "caml_frame_Draw" -external frame_End : unit -> unit = "caml_frame_End" -type nonrec vec2 = { - x: float ; - y: float } -type nonrec vec3 = { - x: float ; - y: float ; - z: float } -type nonrec vec4 = { - x: float ; - y: float ; - z: float ; - w: float } -external vec3_add : b:vec3 -> a:vec3 -> vec3 = "caml_vec3_add" diff --git a/bindgen/ocaml/bindings/caml_bindings.c b/bindgen/ocaml/bindings/caml_bindings.c deleted file mode 100644 index 4a7dc70..0000000 --- a/bindgen/ocaml/bindings/caml_bindings.c +++ /dev/null @@ -1,132 +0,0 @@ -/* automatically generated by ocaml-bindgen 0.0.1 */ - -#include "celeritas.h" -#include <caml/alloc.h> -#include <caml/callback.h> -#include <caml/fail.h> -#include <caml/memory.h> -#include <caml/mlvalues.h> -#include <caml/unixsupport.h> -Core* caml_get_global_core() { - CAMLparam0(); - CAMLlocal1(result); - result = get_global_core(); - CAMLreturn(result); -} - -void caml_core_Bringup(value caml_optional_window) { - CAMLparam1(caml_optional_window); - void* optional_window = Nativeint_val(Field(caml_optional_window, 1)); - core_Bringup(optional_window); - CAMLreturn0; -} - -void caml_frame_Begin() { - CAMLparam0(); - frame_Begin(); - CAMLreturn0; -} - -void caml_frame_Draw() { - CAMLparam0(); - frame_Draw(); - CAMLreturn0; -} - -void caml_frame_End() { - CAMLparam0(); - frame_End(); - CAMLreturn0; -} - -Vec2* caml_Vec2_of_value(value caml_x) { - Vec2* x = malloc(sizeof(struct Vec2)); - x->x = Double_val(Field(caml_x, 0)); - x->y = Double_val(Field(caml_x, 1)); - return x; -} - -value caml_Vec2_to_value(struct Vec2* x) { - CAMLparam0(); - CAMLlocal1(caml_x); - caml_x = caml_alloc_tuple(2); - Store_field(caml_x, 0, caml_copy_double(x->x)); - Store_field(caml_x, 1, caml_copy_double(x->y)); - CAMLreturn(caml_x); -} - -Vec3* caml_Vec3_of_value(value caml_x) { - Vec3* x = malloc(sizeof(struct Vec3)); - x->x = Double_val(Field(caml_x, 0)); - x->y = Double_val(Field(caml_x, 1)); - x->z = Double_val(Field(caml_x, 2)); - return x; -} - -value caml_Vec3_to_value(struct Vec3* x) { - CAMLparam0(); - CAMLlocal1(caml_x); - caml_x = caml_alloc_tuple(3); - Store_field(caml_x, 0, caml_copy_double(x->x)); - Store_field(caml_x, 1, caml_copy_double(x->y)); - Store_field(caml_x, 2, caml_copy_double(x->z)); - CAMLreturn(caml_x); -} - -Vec4* caml_Vec4_of_value(value caml_x) { - Vec4* x = malloc(sizeof(struct Vec4)); - x->x = Double_val(Field(caml_x, 0)); - x->y = Double_val(Field(caml_x, 1)); - x->z = Double_val(Field(caml_x, 2)); - x->w = Double_val(Field(caml_x, 3)); - return x; -} - -value caml_Vec4_to_value(struct Vec4* x) { - CAMLparam0(); - CAMLlocal1(caml_x); - caml_x = caml_alloc_tuple(4); - Store_field(caml_x, 0, caml_copy_double(x->x)); - Store_field(caml_x, 1, caml_copy_double(x->y)); - Store_field(caml_x, 2, caml_copy_double(x->z)); - Store_field(caml_x, 3, caml_copy_double(x->w)); - CAMLreturn(caml_x); -} - -Vec3 caml_vec3_add(value caml_a, value caml_b) { - CAMLparam2(caml_a, caml_b); - CAMLlocal1(result); - Vec3 a = caml_Vec3_of_value(caml_a); - Vec3 b = caml_Vec3_of_value(caml_b); - result = vec3_add(a, b); - CAMLreturn(result); -} - - -#include <stdlib.h> -value bindgen_alloc(value caml_size) { - CAMLparam1(caml_size); - - // Convert OCaml integer to C size - size_t size = Int_val(caml_size); - printf("Allocated size %ld \n", size); - - void* ptr = malloc(sizeof(size)); - if (ptr == NULL) { - // TODO: handle allocation failure - CAMLreturn(Val_unit); - } - - // Wrap the pointer as an OCaml value - CAMLreturn(caml_copy_nativeint(ptr)); -} - -void bindgen_free(value caml_addr) { - free(Nativeint_val(caml_addr)); -} - -value bindgen_alloc_string(value caml_string) { - CAMLparam1(caml_string); - char* str = String_val(caml_string); - CAMLreturn(caml_copy_nativeint((intnat)str)); -} diff --git a/bindgen/ocaml/bindings/celeritas.h b/bindgen/ocaml/bindings/celeritas.h deleted file mode 100644 index fce4d62..0000000 --- a/bindgen/ocaml/bindings/celeritas.h +++ /dev/null @@ -1,21 +0,0 @@ -/* The Goal of this file is to test ocaml-bindgen on it to start moving development over into OCaml */ - -// #include <stdbool.h> -// #include <stdint.h> - -typedef struct Core Core; -typedef struct GLFWwindow GLFWwindow; - -Core* get_global_core(); -void core_Bringup(void* optional_window); - -void frame_Begin(); -void frame_Draw(); -void frame_End(); - - -struct Vec2 { float x; float y; }; -typedef struct Vec3 { float x; float y; float z; } Vec3; -struct Vec4 { float x; float y; float z; float w; }; - -Vec3 vec3_add(Vec3 a, Vec3 b);
\ No newline at end of file diff --git a/bindgen/ocaml/bindings/dune b/bindgen/ocaml/bindings/dune index 673491d..5f109ff 100644 --- a/bindgen/ocaml/bindings/dune +++ b/bindgen/ocaml/bindings/dune @@ -1,13 +1,3 @@ (library (name bindings) - (libraries bindgen)) - -(rule - (alias gen_bindings) - (deps celeritas.h) - (targets bindings_sys.ml caml_bindings.c) - (action - (run - %{bin:ocaml-bindgen} celeritas.h bindings) - ) - (mode (promote (until-clean))))
\ No newline at end of file + (libraries ))
\ No newline at end of file diff --git a/include/celeritas.h b/include/celeritas.h index 3dc3761..420a63b 100644 --- a/include/celeritas.h +++ b/include/celeritas.h @@ -120,15 +120,18 @@ void _cel_push_error(int error_code); // --- Memory facilities: Allocators, helpers -/** @brief Fixed-size arena allocator */ -typedef struct fixed_arena { - void* storage; - size_t size; +typedef struct arena { + u8* storage; + u8* curr; + u8* end; size_t alignment; - int index; -} fixed_arena; + bool can_grow; +} arena; + +/** @brief Fixed-size arena allocator */ +typedef struct arena fixed_arena; -/** @brief Create a new fixed arena with the provided storage. */ +/** @brief Create a new fixed size arena with the provided storage. */ fixed_arena fixed_arena_new(void* backing_buffer, size_t size, size_t alignment); /** @@ -195,6 +198,18 @@ u32 void_pool_insert(void_pool* pool, void* item); // --- Strings +/** + * @brief Fat pointer representing a UTF8 encoded string + * @note when using `printf` you must use %s.*s length, string until our own modified + print routines are written. alternatively wrap in `cstr()` and pass to `%s`. + */ +typedef struct { + u8* buf; + size_t len; +} str8; + +bool str8_equals(str8 a, str8 b); + // --- Logging // Log levels @@ -375,13 +390,33 @@ typedef enum gpu_tex_type { TEXTURE_TYPE_COUNT } gpu_tex_type; -/** @brief Texture Description - used by texture creation functions */ +/** @brief Buffer Description - used by buffer creation functions and for internal bookkeeping */ +typedef struct buffer_desc { + const char* label; + u64 size; +} buffer_desc; + +/** @brief Texture Description - used by texture creation functions and for internal bookkeeping */ typedef struct texture_desc { gpu_tex_type tex_type; // GPU_TextureFormat format; int width, height, num_channels; } texture_desc; +typedef struct buffer_region { + buf_handle buffer; + u64 offset; + u64 size; +} buffer_region; + +typedef struct texture_region { + tex_handle texture; + u32 layer; + u32 x, y, z; + u32 width, height, depth; + // todo: mip level +} texture_region; + /// @strip_prefix(ATTR_) typedef enum vertex_attrib_type { ATTR_F32, @@ -480,6 +515,9 @@ typedef struct compute_pipeline_desc { /* TODO */ typedef struct render_pass_desc { } render_pass_desc; +typedef struct compute_pass_desc { +} compute_pass_desc; + // --- RAL Functions // Resources @@ -492,10 +530,11 @@ tex_handle ral_texture_load_from_file(const char* filepath); 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 */ +/** @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(); +/** @brief Grabs a new compute pass encoder and begins recording */ +gpu_compute_encoder ral_compute_encoder(compute_pass_desc cpass_desc); void ral_encoder_finish(gpu_encoder* enc); void ral_encoder_submit(gpu_encoder* enc); @@ -523,7 +562,7 @@ void ral_backend_resize_framebuffer(int width, int height); // Frame lifecycle -/** @brief A function pointer to a function that should be called with any prepared graphics resources for a frame +/** @brief A function pointer to a function that is called with any prepared graphics resources for a frame + access to the renderer. */ typedef void (*scoped_draw_commands)(); // callback that we run our draw commands within. // allows us to wrap some api-specific behaviour @@ -625,10 +664,21 @@ mat4 camera_view_proj(camera camera, f32 lens_height, f32 lens_width, mat4* out_ // --- Game and model data +/** + * @brief + * @details A 'model' is a fairly complex construct within the Celeritas renderer comprised of multiple cpu & gpu resources. + */ typedef struct model { } model; -model_handle model_load_from_gltf(const char* path); +/** + * @brief + * @note The memory required to load the GLTF is handled by the system, hence no arena parameter. On fail that temporary allocation + * will be freed for you. + * @param filepath path to the .gltf file + * @return model_handle + */ +model_handle model_load_from_gltf(const char* filepath); // --- Animation @@ -669,4 +719,42 @@ typedef struct animation_spline { // --- Platform +/** @brief Loads the contents at a filepath into a string */ +str8 plat_load_string_from_file(arena* a, const char* filepath); + +// synchronisation primitives +/** wrapper around a platform specific mutex */ +typedef struct plat_mutex plat_mutex; + +/** @brief Locks the mutex, blocking until its available */ +void plat_mutex_lock(plat_mutex* mutex); + +/** @brief Tries to lock the mutex but if it is already acquired by something else, will return immediately with false */ +bool plat_mutex_try_lock(plat_mutex* mutex); + +/** @brief Releases the lock allowing other threads to acquire it */ +void plat_mutex_unlock(plat_mutex* mutex); + +typedef struct plat_thread plat_thread; + // --- Audio + +// --- Threadpool + +typedef struct ring_queue { + +} ring_queue; + +typedef struct threadpool_worker {} threadpool_worker; + +#define MAX_THREADPOOL_WORKERS 32 + +typedef struct threadpool { + ring_queue task_queue; + plat_mutex* task_queue_lock; + // todo: atomic u16 remaining tasks + threadpool_worker workers[MAX_THREADPOOL_WORKERS]; + size_t n_workers; +} threadpool; + +void threadpool_add_task();
\ No newline at end of file diff --git a/src/maths.c b/src/maths.c index 87e9afd..d710683 100644 --- a/src/maths.c +++ b/src/maths.c @@ -8,7 +8,7 @@ #include <celeritas.h> -vec2 vec2_create(f32 x, f32 y) { return (vec2){ x,y};} +vec2 vec2_create(f32 x, f32 y) { return (vec2){ x, y }; } 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 }; } @@ -31,10 +31,8 @@ vec3 vec3_cross(vec3 a, vec3 b) { return (vec3){ .x = a.y * b.z - a.z * b.y, .y = a.z * b.x - a.x * b.z, .z = a.x * b.y - a.y * b.x }; } - vec4 vec4_create(f32 x, f32 y, f32 z, f32 w) { return (vec4){ x, y, z, w }; } - f32 quat_dot(quat a, quat b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } quat quat_normalise(quat a) { @@ -55,7 +53,6 @@ quat quat_from_axis_angle(vec3 axis, f32 angle, bool normalize) { return q; } - mat4 mat4_ident() { return (mat4){ .x_axis = { 1, 0, 0, 0 }, @@ -120,7 +117,6 @@ mat4 mat4_mult(mat4 lhs, mat4 rhs) { // return out_matrix; } - /* */ @@ -168,6 +164,4 @@ mat4 mat4_look_at(vec3 position, vec3 target, vec3 up) { // return out_matrix; } -transform transform_from_mat4(mat4 matrix) { - TODO("stuff") -}
\ No newline at end of file +transform transform_from_mat4(mat4 matrix) { TODO("stuff") }
\ No newline at end of file @@ -1,8 +1,6 @@ #include <celeritas.h> -fixed_arena fixed_arena_new(void *backing_buffer, size_t size, size_t alignment) { - TODO("") -} +fixed_arena fixed_arena_new(void* backing_buffer, size_t size, size_t alignment){ TODO("") } void_pool void_pool_create(void* storage, const char* debug_label, u64 capacity, u64 entry_size) { size_t _memory_requirements = capacity * entry_size; |