diff --git a/assets/shaders/shader.metal b/assets/shaders/shader.metal deleted file mode 100644 index 2de5ee9..0000000 --- a/assets/shaders/shader.metal +++ /dev/null @@ -1,9 +0,0 @@ -vertex float4 basic_vertex( // 1 - const device packed_float3* vertex_array [[ buffer(0) ]], // 2 - unsigned int vid [[ vertex_id ]]) { // 3 - return float4(vertex_array[vid], 1.0); // 4 -} - -fragment half4 basic_fragment() { // 1 - return half4(1.0); // 2 -} diff --git a/core/arena.c b/core/arena.c new file mode 100644 index 0000000..f383155 --- /dev/null +++ b/core/arena.c @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: BSD-3-Clause +#include "arena.h" +#include + +struct arena_allocator *arena_init(usize size) +{ + struct arena_allocator *allocator = (struct arena_allocator *) malloc(sizeof(struct arena_allocator)); + allocator->size = size; + allocator->base = (usize) malloc(size); + allocator->position = 0; + + return allocator; +} + +void arena_deinit(struct arena_allocator *allocator) +{ + free((void *)allocator->base); + free((void *)allocator); +} + +void *arena_alloc(struct arena_allocator *allocator, usize size) +{ + void *ptr = (void *)(allocator->base + allocator->position); + allocator->position += size; + return ptr; +} + +void *arena_zalloc(struct arena_allocator *allocator, usize size) +{ + void *ptr = arena_alloc(allocator, size); + for (usize i=0; i < size; i++) { + ((u8 *)ptr)[i] = 0x0; + } + + return ptr; +} + +void arena_bump(struct arena_allocator *allocator) +{ + allocator->position = 0; +} diff --git a/core/arena.h b/core/arena.h new file mode 100644 index 0000000..9d2b0fa --- /dev/null +++ b/core/arena.h @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: BSD-3-Clause +#ifndef ARENA_H +#define ARENA_H + +#include "../types.h" + +/* + * An arena is a fast allocator that just + * keeps everything in a contiguous chunk + * of memory and moves a "pointer" when + * allocating new memory. The allocated + * memory is then free'd all at once. + */ +struct arena_allocator { + usize size; + usize base; + usize position; +}; + + +/* Create a new arena allocator of size `size` */ +struct arena_allocator *arena_init(usize size); +/* Destroy the allocator and */ +void arena_deinit(struct arena_allocator *allocator); + +/* + * Allocate a chunk of memory of size `size` on the + * arena. + */ +void *arena_alloc(struct arena_allocator *allocator, usize size); +/* + * Same as `arena_alloc()` but also set all the allocated + * memory to zero. + */ +void *arena_zalloc(struct arena_allocator *allocator, usize size); +/* + * Free all the allocated memory at once. + * This just sets the allocator cursor to its + * starting position. + */ +void arena_bump(struct arena_allocator *allocator); + +#endif diff --git a/makefile b/makefile index c2d0abf..6983ae6 100644 --- a/makefile +++ b/makefile @@ -2,13 +2,18 @@ include config.mk -SRC:=topaz.c linear.c +SRC:=\ + topaz.c\ + linear.c\ + core/arena.c ifeq (${BACKEND},gl) - SRC += gl/gl.c gl/platform.c + SRC += gl/gl.c\ + gl/platform.c endif ifeq (${BACKEND},vk) - SRC += vk/platform.c + SRC += vk/platform.c\ + vk/renderer.c endif OBJ:=${SRC:.c=.o}