update zpl

isolation_bkp/dynres
Dominik Madarász 2021-11-03 15:02:30 +01:00
parent 30f34d51e5
commit 6405e59117
4 changed files with 120 additions and 123 deletions

View File

@ -1,15 +1,14 @@
#include "camera.h" #include "camera.h"
#include "item_placement.h" #include "item_placement.h"
#define ZPL_ENABLE_MATH
#include "zpl.h"
static bool build_submit_placements = false; static bool build_submit_placements = false;
static bool build_is_in_draw_mode = false; static bool build_is_in_draw_mode = false;
static uint8_t build_num_placements = 0; static uint8_t build_num_placements = 0;
static item_placement build_placements[BUILD_MAX_PLACEMENTS] = {0}; static item_placement build_placements[BUILD_MAX_PLACEMENTS] = {0};
#ifndef zpl_square
#define zpl_square(x) ((x) * (x))
#endif
void buildmode_clear_buffers(void) { void buildmode_clear_buffers(void) {
item_placement empty_placement = { .x = 0.0f, .y = 0.0f, .kind = -1 }; item_placement empty_placement = { .x = 0.0f, .y = 0.0f, .kind = -1 };
for (size_t i = 0; i < BUILD_MAX_PLACEMENTS; i++) { for (size_t i = 0; i < BUILD_MAX_PLACEMENTS; i++) {
@ -17,11 +16,6 @@ void buildmode_clear_buffers(void) {
} }
} }
// TODO(zaklaus):
#ifndef zpl_sign0
#define zpl_sign0(x) (x == 0.0f) ? 0.0f : ((x) >= 0.0f ? 1.0f : -1.0f)
#endif
void buildmode_draw(void) { void buildmode_draw(void) {
camera cam = camera_get(); camera cam = camera_get();
camera old_cam = cam; camera old_cam = cam;
@ -75,10 +69,10 @@ void buildmode_draw(void) {
float p2y = build_placements[1].y; float p2y = build_placements[1].y;
float p3x = (float)cam.x; float p3x = (float)cam.x;
float p3y = (float)cam.y; float p3y = (float)cam.y;
float sx = zpl_sign0(p2x-p1x); float sx = zpl_sign(p2x-p1x);
float sy = zpl_sign0(p2y-p1y); float sy = zpl_sign(p2y-p1y);
float sxx = zpl_sign0(p3x-p1x); float sxx = zpl_sign(p3x-p1x);
float syy = zpl_sign0(p3y-p1y); float syy = zpl_sign(p3y-p1y);
if (sx != sxx || sy != syy) break; if (sx != sxx || sy != syy) break;
} }

View File

@ -6,6 +6,7 @@
#include <inttypes.h> #include <inttypes.h>
#define ZPL_NANO #define ZPL_NANO
#define ZPL_ENABLE_MATH
#include "zpl.h" #include "zpl.h"
#define defer_var ZPL_CONCAT(_i_,__LINE__) #define defer_var ZPL_CONCAT(_i_,__LINE__)

View File

@ -182,11 +182,6 @@ void SwapItems(ecs_iter_t *it) {
} }
} }
// TODO(zaklaus):
#ifndef zpl_sign0
#define zpl_sign0(x) (x == 0.0f) ? 0.0f : ((x) >= 0.0f ? 1.0f : -1.0f)
#endif
void UseItem(ecs_iter_t *it) { void UseItem(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1); Input *in = ecs_column(it, Input, 1);
Position *p = ecs_column(it, Position, 2); Position *p = ecs_column(it, Position, 2);
@ -209,8 +204,8 @@ void UseItem(ecs_iter_t *it) {
float p1y = in[i].placements_y[0]; float p1y = in[i].placements_y[0];
float p2x = in[i].placements_x[1]; float p2x = in[i].placements_x[1];
float p2y = in[i].placements_y[1]; float p2y = in[i].placements_y[1];
float sx = zpl_sign0(p2x-p1x); float sx = zpl_sign(p2x-p1x);
float sy = zpl_sign0(p2y-p1y); float sy = zpl_sign(p2y-p1y);
ofs = (sx < 0.0f) ? 1 : 2; ofs = (sx < 0.0f) ? 1 : 2;
if (sx == 0.0f) { if (sx == 0.0f) {
ofs = (sy < 0.0f) ? 3 : 4; ofs = (sy < 0.0f) ? 3 : 4;

171
code/vendors/zpl.h vendored
View File

@ -31,6 +31,14 @@ GitHub:
https://github.com/zpl-c/zpl https://github.com/zpl-c/zpl
Version History: Version History:
15.0.1 - hashtable performance improvements
- zpl_sign(0) returns 0
15.0.0 - Rework zpl ring buffer
- various code improvements
14.1.7 - fix zpl_random_range_i64
- set thread's is_running before we start a thread
14.1.6 - remove windows.h dependency for header part
14.1.5 - fix array append_at 14.1.5 - fix array append_at
14.1.4 - Fix win32 missing CRITICAL_SECTION definition if 14.1.4 - Fix win32 missing CRITICAL_SECTION definition if
- ZPL_NO_WINDOWS_H is defined - ZPL_NO_WINDOWS_H is defined
@ -362,9 +370,9 @@ License:
#ifndef ZPL_H #ifndef ZPL_H
#define ZPL_H #define ZPL_H
#define ZPL_VERSION_MAJOR 14 #define ZPL_VERSION_MAJOR 15
#define ZPL_VERSION_MINOR 1 #define ZPL_VERSION_MINOR 0
#define ZPL_VERSION_PATCH 6 #define ZPL_VERSION_PATCH 0
#define ZPL_VERSION_PRE "" #define ZPL_VERSION_PRE ""
// file: zpl_hedley.h // file: zpl_hedley.h
@ -2964,22 +2972,6 @@ Type tmp = (a); \
# define zpl_abs(x) ((x) < 0 ? -(x) : (x)) # define zpl_abs(x) ((x) < 0 ? -(x) : (x))
#endif #endif
#ifndef hard_cast
# define hard_cast(type) *cast(type) &
#endif
// WARN(ZaKlaus): Supported only on GCC via GNU extensions!!!
#ifndef zpl_lambda
# define zpl_lambda(b_) ({ b_ _; })
#endif
#ifndef zpl_when
# define zpl_when(init, type, name) \
type name = init; \
if (name)
#endif
/* NOTE: Very useful bit setting */
#ifndef ZPL_MASK_SET #ifndef ZPL_MASK_SET
# define ZPL_MASK_SET(var, set, mask) \ # define ZPL_MASK_SET(var, set, mask) \
do { \ do { \
@ -3509,6 +3501,9 @@ ZPL_DEF_INLINE void zpl_pool_free(zpl_pool *pool);
ZPL_DEF_INLINE zpl_allocator zpl_pool_allocator(zpl_pool *pool); ZPL_DEF_INLINE zpl_allocator zpl_pool_allocator(zpl_pool *pool);
ZPL_DEF ZPL_ALLOCATOR_PROC(zpl_pool_allocator_proc); ZPL_DEF ZPL_ALLOCATOR_PROC(zpl_pool_allocator_proc);
//
// Scratch Memory Allocator - Ring Buffer Based Arena
//
typedef struct zpl_allocation_header_ev { typedef struct zpl_allocation_header_ev {
zpl_isize size; zpl_isize size;
@ -3525,11 +3520,6 @@ ZPL_DEF_INLINE void zpl_allocation_header_fill(zpl_allocation_header_ev *header,
#error #error
#endif #endif
//
// Scratch Memory Allocator - Ring Buffer Based Arena
//
typedef struct zpl_scratch_memory { typedef struct zpl_scratch_memory {
void *physical_start; void *physical_start;
zpl_isize total_size; zpl_isize total_size;
@ -3576,10 +3566,6 @@ ZPL_DEF_INLINE void zpl_stack_memory_free(zpl_stack_memory *s);
ZPL_DEF_INLINE zpl_allocator zpl_stack_allocator(zpl_stack_memory *s); ZPL_DEF_INLINE zpl_allocator zpl_stack_allocator(zpl_stack_memory *s);
ZPL_DEF ZPL_ALLOCATOR_PROC(zpl_stack_allocator_proc); ZPL_DEF ZPL_ALLOCATOR_PROC(zpl_stack_allocator_proc);
// TODO: Fixed heap allocator
// TODO: General heap allocator. Maybe a TCMalloc like clone?
/* inlines */ /* inlines */
ZPL_IMPL_INLINE void *zpl_alloc_align(zpl_allocator a, zpl_isize size, zpl_isize alignment) { ZPL_IMPL_INLINE void *zpl_alloc_align(zpl_allocator a, zpl_isize size, zpl_isize alignment) {
@ -3638,7 +3624,6 @@ ZPL_IMPL_INLINE void *zpl_default_resize_align(zpl_allocator a, void *old_memory
} }
} }
// //
// Heap Allocator // Heap Allocator
// //
@ -4166,30 +4151,50 @@ ZPL_END_C_DECLS
// Instantiated Circular buffer // Instantiated Circular buffer
// //
/*
Buffer type and function declaration, call: ZPL_RING_DECLARE(PREFIX, FUNC, VALUE)
Buffer function definitions, call: ZPL_RING_DEFINE(PREFIX, FUNC, VALUE)
PREFIX - a prefix for function prototypes e.g. extern, static, etc.
FUNC - the name will prefix function names
VALUE - the type of the value to be stored
funcname_init(VALUE * pad, zpl_allocator a, zpl_isize max_size)
funcname_free(VALUE * pad)
funcname_full(VALUE * pad)
funcname_empty(VALUE * pad)
funcname_append(VALUE * pad, type data)
funcname_append_array(VALUE * pad, zpl_array(type) data)
funcname_get(VALUE * pad)
funcname_get_array(VALUE * pad, zpl_usize max_size, zpl_allocator a)
*/
ZPL_BEGIN_C_DECLS ZPL_BEGIN_C_DECLS
#define ZPL_RING_DECLARE(prefix,type) \ #define ZPL_RING(PREFIX, FUNC, VALUE) \
ZPL_RING_DECLARE(PREFIX, FUNC, VALUE); \
ZPL_RING_DEFINE(FUNC, VALUE);
#define ZPL_RING_DECLARE(prefix,func,type) \
typedef struct { \ typedef struct { \
zpl_allocator backing; \ zpl_allocator backing; \
zpl_buffer(type) buf; \ zpl_buffer(type) buf; \
zpl_usize head, tail; \ zpl_usize head, tail; \
zpl_usize capacity; \ zpl_usize capacity; \
} ZPL_JOIN2(prefix, type); \ } ZPL_JOIN2(func, type); \
\ \
ZPL_DEF void ZPL_JOIN2(prefix, init)(ZPL_JOIN2(prefix, type) * pad, zpl_allocator a, zpl_isize max_size); \ prefix void ZPL_JOIN2(func, init)(ZPL_JOIN2(func, type) * pad, zpl_allocator a, zpl_isize max_size); \
ZPL_DEF void ZPL_JOIN2(prefix, free)(ZPL_JOIN2(prefix, type) * pad); \ prefix void ZPL_JOIN2(func, free)(ZPL_JOIN2(func, type) * pad); \
ZPL_DEF zpl_b32 ZPL_JOIN2(prefix, full)(ZPL_JOIN2(prefix, type) * pad); \ prefix zpl_b32 ZPL_JOIN2(func, full)(ZPL_JOIN2(func, type) * pad); \
ZPL_DEF zpl_b32 ZPL_JOIN2(prefix, empty)(ZPL_JOIN2(prefix, type) * pad); \ prefix zpl_b32 ZPL_JOIN2(func, empty)(ZPL_JOIN2(func, type) * pad); \
ZPL_DEF void ZPL_JOIN2(prefix, append)(ZPL_JOIN2(prefix, type) * pad, type data); \ prefix void ZPL_JOIN2(func, append)(ZPL_JOIN2(func, type) * pad, type data); \
ZPL_DEF void ZPL_JOIN2(prefix, append_array)(ZPL_JOIN2(prefix, type) * pad, zpl_array(type) data); \ prefix void ZPL_JOIN2(func, append_array)(ZPL_JOIN2(func, type) * pad, zpl_array(type) data); \
ZPL_DEF type *ZPL_JOIN2(prefix, get)(ZPL_JOIN2(prefix, type) * pad); \ prefix type *ZPL_JOIN2(func, get)(ZPL_JOIN2(func, type) * pad); \
ZPL_DEF zpl_array(type) \ prefix zpl_array(type) \
ZPL_JOIN2(prefix, get_array)(ZPL_JOIN2(prefix, type) * pad, zpl_usize max_size, zpl_allocator a); ZPL_JOIN2(func, get_array)(ZPL_JOIN2(func, type) * pad, zpl_usize max_size, zpl_allocator a);
#define ZPL_RING_DEFINE(prefix,type) \ #define ZPL_RING_DEFINE(func,type) \
void ZPL_JOIN2(prefix, init)(ZPL_JOIN2(prefix, type) * pad, zpl_allocator a, zpl_isize max_size) { \ void ZPL_JOIN2(func, init)(ZPL_JOIN2(func, type) * pad, zpl_allocator a, zpl_isize max_size) { \
ZPL_JOIN2(prefix, type) pad_ = { 0 }; \ ZPL_JOIN2(func, type) pad_ = { 0 }; \
*pad = pad_; \ *pad = pad_; \
\ \
pad->backing = a; \ pad->backing = a; \
@ -4197,30 +4202,30 @@ zpl_buffer_init(pad->buf, a, max_size + 1);
pad->capacity = max_size + 1; \ pad->capacity = max_size + 1; \
pad->head = pad->tail = 0; \ pad->head = pad->tail = 0; \
} \ } \
void ZPL_JOIN2(prefix, free)(ZPL_JOIN2(prefix, type) * pad) { \ void ZPL_JOIN2(func, free)(ZPL_JOIN2(func, type) * pad) { \
zpl_buffer_free(pad->buf); \ zpl_buffer_free(pad->buf); \
} \ } \
\ \
zpl_b32 ZPL_JOIN2(prefix, full)(ZPL_JOIN2(prefix, type) * pad) { \ zpl_b32 ZPL_JOIN2(func, full)(ZPL_JOIN2(func, type) * pad) { \
return ((pad->head + 1) % pad->capacity) == pad->tail; \ return ((pad->head + 1) % pad->capacity) == pad->tail; \
} \ } \
\ \
zpl_b32 ZPL_JOIN2(prefix, empty)(ZPL_JOIN2(prefix, type) * pad) { return pad->head == pad->tail; } \ zpl_b32 ZPL_JOIN2(func, empty)(ZPL_JOIN2(func, type) * pad) { return pad->head == pad->tail; } \
\ \
void ZPL_JOIN2(prefix, append)(ZPL_JOIN2(prefix, type) * pad, type data) { \ void ZPL_JOIN2(func, append)(ZPL_JOIN2(func, type) * pad, type data) { \
pad->buf[pad->head] = data; \ pad->buf[pad->head] = data; \
pad->head = (pad->head + 1) % pad->capacity; \ pad->head = (pad->head + 1) % pad->capacity; \
\ \
if (pad->head == pad->tail) { pad->tail = (pad->tail + 1) % pad->capacity; } \ if (pad->head == pad->tail) { pad->tail = (pad->tail + 1) % pad->capacity; } \
} \ } \
\ \
void ZPL_JOIN2(prefix, append_array)(ZPL_JOIN2(prefix, type) * pad, zpl_array(type) data) { \ void ZPL_JOIN2(func, append_array)(ZPL_JOIN2(func, type) * pad, zpl_array(type) data) { \
zpl_usize c = zpl_array_count(data); \ zpl_usize c = zpl_array_count(data); \
for (zpl_usize i = 0; i < c; ++i) { ZPL_JOIN2(prefix, append)(pad, data[i]); } \ for (zpl_usize i = 0; i < c; ++i) { ZPL_JOIN2(func, append)(pad, data[i]); } \
} \ } \
\ \
type *ZPL_JOIN2(prefix, get)(ZPL_JOIN2(prefix, type) * pad) { \ type *ZPL_JOIN2(func, get)(ZPL_JOIN2(func, type) * pad) { \
if (ZPL_JOIN2(prefix, empty)(pad)) { return NULL; } \ if (ZPL_JOIN2(func, empty)(pad)) { return NULL; } \
\ \
type *data = &pad->buf[pad->tail]; \ type *data = &pad->buf[pad->tail]; \
pad->tail = (pad->tail + 1) % pad->capacity; \ pad->tail = (pad->tail + 1) % pad->capacity; \
@ -4229,11 +4234,11 @@ return data;
} \ } \
\ \
zpl_array(type) \ zpl_array(type) \
ZPL_JOIN2(prefix, get_array)(ZPL_JOIN2(prefix, type) * pad, zpl_usize max_size, zpl_allocator a) { \ ZPL_JOIN2(func, get_array)(ZPL_JOIN2(func, type) * pad, zpl_usize max_size, zpl_allocator a) { \
zpl_array(type) vals = 0; \ zpl_array(type) vals = 0; \
zpl_array_init(vals, a); \ zpl_array_init(vals, a); \
while (--max_size && !ZPL_JOIN2(prefix, empty)(pad)) { \ while (--max_size && !ZPL_JOIN2(func, empty)(pad)) { \
zpl_array_append(vals, *ZPL_JOIN2(prefix, get)(pad)); \ zpl_array_append(vals, *ZPL_JOIN2(func, get)(pad)); \
} \ } \
return vals; \ return vals; \
} }
@ -4245,25 +4250,27 @@ ZPL_END_C_DECLS
@brief Instantiated hash table @brief Instantiated hash table
@defgroup hashtable Instantiated hash table @defgroup hashtable Instantiated hash table
@n
@n This is an attempt to implement a templated hash table This is an attempt to implement a templated hash table
@n NOTE: The key is always a zpl_u64 for simplicity and you will _probably_ _never_ need anything bigger. NOTE: The key is always a zpl_u64 for simplicity and you will _probably_ _never_ need anything bigger.
@n
@n Hash table type and function declaration, call: ZPL_TABLE_DECLARE(PREFIX, NAME, N, VALUE) Hash table type and function declaration, call: ZPL_TABLE_DECLARE(PREFIX, NAME, FUNC, VALUE)
@n Hash table function definitions, call: ZPL_TABLE_DEFINE(NAME, N, VALUE) Hash table function definitions, call: ZPL_TABLE_DEFINE(NAME, FUNC, VALUE)
@n
@n PREFIX - a prefix for function prototypes e.g. extern, static, etc. PREFIX - a prefix for function prototypes e.g. extern, static, etc.
@n NAME - Name of the Hash Table NAME - Name of the Hash Table
@n FUNC - the name will prefix function names FUNC - the name will prefix function names
@n VALUE - the type of the value to be stored VALUE - the type of the value to be stored
@n
@n tablename_init(NAME * h, zpl_allocator a); tablename_init(NAME * h, zpl_allocator a);
@n tablename_destroy(NAME * h); tablename_destroy(NAME * h);
@n tablename_get(NAME * h, zpl_u64 key); tablename_get(NAME * h, zpl_u64 key);
@n tablename_set(NAME * h, zpl_u64 key, VALUE value); tablename_set(NAME * h, zpl_u64 key, VALUE value);
@n tablename_grow(NAME * h); tablename_grow(NAME * h);
@n tablename_rehash(NAME * h, zpl_isize new_count); tablename_map(NAME * h, void (*map_proc)(zpl_u64 key, VALUE value))
@n tablename_remove(NAME * h, zpl_u64 key); tablename_map_mut(NAME * h, void (*map_proc)(zpl_u64 key, VALUE * value))
tablename_rehash(NAME * h, zpl_isize new_count);
tablename_remove(NAME * h, zpl_u64 key);
@{ @{
*/ */
@ -4480,7 +4487,7 @@ ZPL_DEF zpl_virtual_memory zpl_vm(void *data, zpl_isize size);
//! Allocate virtual memory at address with size. //! Allocate virtual memory at address with size.
//! @param addr The starting address of the region to reserve. If NULL, it lets operating system to decide where to allocate it. //! @param addr The starting address of the region to reserve. If NULL, it lets operating system to decide where to allocate it.
//! @param size The size to server. //! @param size The size to serve.
ZPL_DEF zpl_virtual_memory zpl_vm_alloc(void *addr, zpl_isize size); ZPL_DEF zpl_virtual_memory zpl_vm_alloc(void *addr, zpl_isize size);
//! Release the virtual memory. //! Release the virtual memory.
@ -6588,7 +6595,7 @@ typedef short zpl_half;
#endif #endif
#ifndef zpl_sign #ifndef zpl_sign
#define zpl_sign(x) ((x) >= 0 ? 1 : -1) #define zpl_sign(x) (x == 0.0f) ? 0.0f : ((x) >= 0.0f ? 1.0f : -1.0f)
#endif #endif
ZPL_DEF zpl_f32 zpl_to_radians(zpl_f32 degrees); ZPL_DEF zpl_f32 zpl_to_radians(zpl_f32 degrees);
@ -7642,7 +7649,7 @@ typedef struct {
void *data; void *data;
} zpl_thread_job; } zpl_thread_job;
ZPL_RING_DECLARE(zpl__jobs_ring_, zpl_thread_job); ZPL_RING_DECLARE(extern, zpl__jobs_ring_, zpl_thread_job);
typedef struct { typedef struct {
zpl_thread thread; zpl_thread thread;
@ -11603,19 +11610,20 @@ zpl_isize zpl_random_gen_isize(zpl_random *r) {
zpl_i64 zpl_random_range_i64(zpl_random *r, zpl_i64 lower_inc, zpl_i64 higher_inc) { zpl_i64 zpl_random_range_i64(zpl_random *r, zpl_i64 lower_inc, zpl_i64 higher_inc) {
zpl_u64 u = zpl_random_gen_u64(r); zpl_u64 u = zpl_random_gen_u64(r);
zpl_i64 i = *cast(zpl_i64 *)&u;
zpl_i64 diff = higher_inc-lower_inc+1; zpl_i64 diff = higher_inc-lower_inc+1;
i %= diff; u %= diff;
zpl_i64 i;
zpl_memcopy(&i, &u, zpl_size_of(u));
i += lower_inc; i += lower_inc;
return i; return i;
} }
zpl_isize zpl_random_range_isize(zpl_random *r, zpl_isize lower_inc, zpl_isize higher_inc) { zpl_isize zpl_random_range_isize(zpl_random *r, zpl_isize lower_inc, zpl_isize higher_inc) {
zpl_u64 u = zpl_random_gen_u64(r); zpl_u64 u = zpl_random_gen_u64(r);
zpl_isize diff = higher_inc-lower_inc+1;
u %= diff;
zpl_isize i; zpl_isize i;
zpl_memcopy(&i, &u, zpl_size_of(u)); zpl_memcopy(&i, &u, zpl_size_of(u));
zpl_isize diff = higher_inc-lower_inc+1;
i %= diff;
i += lower_inc; i += lower_inc;
return i; return i;
} }
@ -16288,6 +16296,7 @@ void zpl_thread_start_with_stack(zpl_thread *t, zpl_thread_proc proc, void *user
t->proc = proc; t->proc = proc;
t->user_data = user_data; t->user_data = user_data;
t->stack_size = stack_size; t->stack_size = stack_size;
t->is_running = true;
# if defined(ZPL_SYSTEM_WINDOWS) # if defined(ZPL_SYSTEM_WINDOWS)
t->win32_handle = CreateThread(NULL, stack_size, zpl__thread_proc, t, 0, NULL); t->win32_handle = CreateThread(NULL, stack_size, zpl__thread_proc, t, 0, NULL);
@ -16303,8 +16312,6 @@ void zpl_thread_start_with_stack(zpl_thread *t, zpl_thread_proc proc, void *user
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
} }
# endif # endif
t->is_running = true;
zpl_semaphore_wait(&t->semaphore); zpl_semaphore_wait(&t->semaphore);
} }