From 66722ac100d2f5ee69edfec22a20ad8dce5b44a1 Mon Sep 17 00:00:00 2001 From: asrael Date: Sat, 15 Nov 2025 14:23:53 -0600 Subject: [PATCH] optimize texture sampling --- demo/mod/worldgen.fnl | 2 +- pxl8.sh | 7 +++++-- src/pxl8_gfx.c | 13 ++++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/demo/mod/worldgen.fnl b/demo/mod/worldgen.fnl index a10a8fc..61a1529 100644 --- a/demo/mod/worldgen.fnl +++ b/demo/mod/worldgen.fnl @@ -4,7 +4,7 @@ (var cam-x 1000) (var cam-y 64) (var cam-z 1000) -(var cam-yaw 0) +(var cam-yaw 0.074) (var cam-pitch 0) (var bob-time 0) (var velocity-y 0) diff --git a/pxl8.sh b/pxl8.sh index 3096771..adb1612 100755 --- a/pxl8.sh +++ b/pxl8.sh @@ -284,6 +284,8 @@ else BINDIR="$BINDIR/debug" fi +DEP_CFLAGS="-O3 -ffast-math -funroll-loops" + case "$COMMAND" in build) mkdir -p "$BUILDDIR" @@ -333,7 +335,8 @@ case "$COMMAND" in INCLUDES="-Isrc -Ilib -Ilib/microui/src -Ilib/luajit/src -Ilib/linenoise -Ilib/miniz" COMPILE_FLAGS="$CFLAGS $INCLUDES" - + DEP_COMPILE_FLAGS="$DEP_CFLAGS $INCLUDES" + EXECUTABLE="$BINDIR/pxl8" LIB_SOURCE_FILES="lib/linenoise/linenoise.c lib/microui/src/microui.c lib/miniz/miniz.c" @@ -376,7 +379,7 @@ case "$COMMAND" in if [[ "$src_file" -nt "$obj_file" ]]; then NEED_LINK=true - compile_source_file "$src_file" "$obj_file" "$COMPILE_FLAGS" + compile_source_file "$src_file" "$obj_file" "$DEP_COMPILE_FLAGS" SOURCES_COMPILED="yes" fi done diff --git a/src/pxl8_gfx.c b/src/pxl8_gfx.c index dd7f71b..b3cb16d 100644 --- a/src/pxl8_gfx.c +++ b/src/pxl8_gfx.c @@ -810,8 +810,9 @@ void pxl8_3d_clear_zbuffer(pxl8_gfx* gfx) { i32 count = gfx->zbuffer_width * gfx->zbuffer_height; const f32 far_z = 1e30f; + f32* ptr = gfx->zbuffer; for (i32 i = 0; i < count; i++) { - gfx->zbuffer[i] = far_z; + ptr[i] = far_z; } } @@ -1002,11 +1003,13 @@ static inline u32 pxl8_sample_texture(pxl8_gfx* gfx, u32 texture_id, f32 u, f32 return 0; } - u = u - floorf(u); - v = v - floorf(v); + i32 tex_u = (i32)(u * entry->w); + i32 tex_v = (i32)(v * entry->h); - i32 tx = (i32)(u * entry->w) % entry->w; - i32 ty = (i32)(v * entry->h) % entry->h; + i32 tx = tex_u % entry->w; + if (tx < 0) tx += entry->w; + i32 ty = tex_v % entry->h; + if (ty < 0) ty += entry->h; i32 atlas_x = entry->x + tx; i32 atlas_y = entry->y + ty;