optimize texture sampling

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

View file

@ -4,7 +4,7 @@
(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-yaw 0.074)
(var cam-pitch 0) (var cam-pitch 0)
(var bob-time 0) (var bob-time 0)
(var velocity-y 0) (var velocity-y 0)

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;