optimize texture sampling

This commit is contained in:
asrael 2025-11-15 14:23:53 -06:00
parent 736af38518
commit b2682a2d40
3 changed files with 26 additions and 21 deletions

View file

@ -1,28 +1,27 @@
(local pxl8 (require :pxl8)) (local pxl8 (require :pxl8))
(var world nil) (local bob-amount 4.0)
(local bob-speed 8.0)
(var bob-time 0)
(var cam-x 1000) (var cam-x 1000)
(var cam-y 64) (var cam-y 64)
(var cam-z 1000) (var cam-z 1000)
(var cam-yaw 0)
(var cam-pitch 0) (var cam-pitch 0)
(var bob-time 0) (var cam-yaw 0)
(var velocity-y 0)
(var grounded? true)
(var land-squash 0)
(local move-speed 200)
(local turn-speed 2.0)
(local bob-speed 8.0)
(local bob-amount 4.0)
(local max-pitch 1.5)
(local cell-size 64) (local cell-size 64)
(local grid-size 32)
(local gravity -800) (local gravity -800)
(local jump-force 175) (local grid-size 32)
(var grounded? true)
(local ground-y 64) (local ground-y 64)
(local jump-force 175)
(var land-squash 0)
(local land-squash-amount -4) (local land-squash-amount -4)
(local land-recovery-speed 20) (local land-recovery-speed 20)
(local max-pitch 1.5)
(local move-speed 200)
(local turn-speed 2.0)
(var velocity-y 0)
(var world nil)
(fn init [] (fn init []
(set world (pxl8.world_new)) (set world (pxl8.world_new))

View file

@ -284,6 +284,8 @@ else
BINDIR="$BINDIR/debug" BINDIR="$BINDIR/debug"
fi fi
DEP_CFLAGS="-O3 -ffast-math -funroll-loops"
case "$COMMAND" in case "$COMMAND" in
build) build)
mkdir -p "$BUILDDIR" mkdir -p "$BUILDDIR"
@ -333,7 +335,8 @@ case "$COMMAND" in
INCLUDES="-Isrc -Ilib -Ilib/microui/src -Ilib/luajit/src -Ilib/linenoise -Ilib/miniz" INCLUDES="-Isrc -Ilib -Ilib/microui/src -Ilib/luajit/src -Ilib/linenoise -Ilib/miniz"
COMPILE_FLAGS="$CFLAGS $INCLUDES" COMPILE_FLAGS="$CFLAGS $INCLUDES"
DEP_COMPILE_FLAGS="$DEP_CFLAGS $INCLUDES"
EXECUTABLE="$BINDIR/pxl8" EXECUTABLE="$BINDIR/pxl8"
LIB_SOURCE_FILES="lib/linenoise/linenoise.c lib/microui/src/microui.c lib/miniz/miniz.c" 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 if [[ "$src_file" -nt "$obj_file" ]]; then
NEED_LINK=true 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" SOURCES_COMPILED="yes"
fi fi
done done

View file

@ -810,8 +810,9 @@ void pxl8_3d_clear_zbuffer(pxl8_gfx* gfx) {
i32 count = gfx->zbuffer_width * gfx->zbuffer_height; i32 count = gfx->zbuffer_width * gfx->zbuffer_height;
const f32 far_z = 1e30f; const f32 far_z = 1e30f;
f32* ptr = gfx->zbuffer;
for (i32 i = 0; i < count; i++) { 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; return 0;
} }
u = u - floorf(u); i32 tex_u = (i32)(u * entry->w);
v = v - floorf(v); i32 tex_v = (i32)(v * entry->h);
i32 tx = (i32)(u * entry->w) % entry->w; i32 tx = tex_u % entry->w;
i32 ty = (i32)(v * entry->h) % entry->h; 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_x = entry->x + tx;
i32 atlas_y = entry->y + ty; i32 atlas_y = entry->y + ty;