Skip to content

Commit

Permalink
#368 GBA pre-shift z by OT_SHIFT, pre-shift mesh vertex coords, fog m…
Browse files Browse the repository at this point in the history
…ath optims
  • Loading branch information
XProger committed Dec 3, 2022
1 parent b1a5590 commit 90e03fa
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 110 deletions.
10 changes: 6 additions & 4 deletions src/fixed/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
// the maximum of active enemies
#define MAX_ENEMIES 3
// visibility distance
#define VIEW_DIST (1024 * 10)
#define VIEW_DIST (10 << 10)
// skip collideSpheres for enemies
#define FAST_HITMASK
#endif
Expand All @@ -224,7 +224,7 @@
// set the maximum number of simultaneously played channels
#define SND_CHANNELS 4
// visibility distance
#define VIEW_DIST (1024 * 10)
#define VIEW_DIST (10 << 10)
// skip collideSpheres for enemies
#define FAST_HITMASK
#endif
Expand All @@ -239,7 +239,7 @@
// the maximum of active enemies
#define MAX_ENEMIES 3
// visibility distance
#define VIEW_DIST (1024 * 10)
#define VIEW_DIST (10 << 10)
// skip collideSpheres for enemies
#define FAST_HITMASK
#endif
Expand Down Expand Up @@ -457,10 +457,12 @@ extern int32 fps;
#define FOV_SHIFT 3
#define FOG_SHIFT 1
#define FOG_MAX VIEW_DIST
#define FOG_MIN (FOG_MAX - (8192 >> FOG_SHIFT))
#define FOG_MIN (FOG_MAX - 4096)
#define VIEW_MIN_F (64 << FIXED_SHIFT)
#define VIEW_MAX_F (VIEW_DIST << FIXED_SHIFT)

#define MESH_SHIFT 2

#define TEX_ATTR_AKILL 1

#define NOT_ENEMY -0x4000 // default hp for non enemies
Expand Down
32 changes: 12 additions & 20 deletions src/fixed/room.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,23 +667,19 @@ bool traceX(const Location &from, Location &to, bool accurate)
if (!d.x)
return true;

int32 dx = abs(d.x) >> 3;
ASSERT(dx < DIV_TABLE_SIZE);
dx = FixedInvU(dx);

d.y = (d.y * dx) >> (16 + 3 - TRACE_SHIFT);
d.z = (d.z * dx) >> (16 + 3 - TRACE_SHIFT);
d.y = (d.y << TRACE_SHIFT) / d.x;
d.z = (d.z << TRACE_SHIFT) / d.x;

vec3i p = from.pos;

Room* room = from.room;

if (d.x < 0)
{
d.x = -1024;
d.x = 1024;
p.x &= ~1023;
p.y -= d.y * (p.x - from.pos.x) >> TRACE_SHIFT;
p.z -= d.z * (p.x - from.pos.x) >> TRACE_SHIFT;
p.y += d.y * (p.x - from.pos.x) >> TRACE_SHIFT;
p.z += d.z * (p.x - from.pos.x) >> TRACE_SHIFT;

while (p.x > to.pos.x)
{
Expand All @@ -694,7 +690,7 @@ bool traceX(const Location &from, Location &to, bool accurate)
TRACE_CHECK(nextRoom, p.x - 1, p.y, p.z);

room = nextRoom;
p += d;
p -= d;
}
}
else
Expand Down Expand Up @@ -729,23 +725,19 @@ bool traceZ(const Location &from, Location &to, bool accurate)
if (!d.z)
return true;

int32 dz = abs(d.z) >> 3;
ASSERT(dz < DIV_TABLE_SIZE);
dz = FixedInvU(dz);

d.x = (d.x * dz) >> (16 + 3 - TRACE_SHIFT);
d.y = (d.y * dz) >> (16 + 3 - TRACE_SHIFT);
d.x = (d.x << TRACE_SHIFT) / d.z;
d.y = (d.y << TRACE_SHIFT) / d.z;

vec3i p = from.pos;

Room* room = from.room;

if (d.z < 0)
{
d.z = -1024;
d.z = 1024;
p.z &= ~1023;
p.x -= d.x * (p.z - from.pos.z) >> TRACE_SHIFT;
p.y -= d.y * (p.z - from.pos.z) >> TRACE_SHIFT;
p.x += d.x * (p.z - from.pos.z) >> TRACE_SHIFT;
p.y += d.y * (p.z - from.pos.z) >> TRACE_SHIFT;

while (p.z > to.pos.z)
{
Expand All @@ -756,7 +748,7 @@ bool traceZ(const Location &from, Location &to, bool accurate)
TRACE_CHECK(nextRoom, p.x, p.y, p.z - 1);

room = nextRoom;
p += d;
p -= d;
}
}
else
Expand Down
19 changes: 7 additions & 12 deletions src/platform/gba/asm/common_asm.inc
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,16 @@

.equ FIXED_SHIFT, 14
.equ PROJ_SHIFT, 4
.equ OT_SHIFT, 4
.equ MESH_SHIFT, 2

.equ VIEW_DIST, (1024 * 10)
.equ FOG_SHIFT, 1
.equ FOG_MAX, VIEW_DIST
.equ FOG_MIN, (FOG_MAX - (8192 >> FOG_SHIFT))
.equ VIEW_MIN, (64)
.equ VIEW_MAX, (VIEW_DIST)
.equ VIEW_MIN, 64
.equ VIEW_MAX, (10 << 10)
.equ VIEW_OFF, 4096
.equ FOG_SHIFT, 4
.equ FOG_MIN, (VIEW_MAX - 4096)

.equ OT_SIZE, 641

.equ VIEW_MIN_F, (VIEW_MIN << FIXED_SHIFT)
.equ VIEW_MAX_F, (VIEW_MAX << FIXED_SHIFT)
.equ VIEW_OFF_F, (VIEW_OFF << FIXED_SHIFT)
.equ OT_SHIFT, 4
.equ OT_SIZE, ((VIEW_MAX >> OT_SHIFT) + 1)

.equ MAX_CAUSTICS, 32
.equ MAX_RAND_TABLE, 32
Expand Down
2 changes: 1 addition & 1 deletion src/platform/gba/asm/faceAddMeshQuads.s
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ faceAddMeshQuads_asm:
add depth, vg0, vg1, lsl #16
add depth, vg2, lsl #16
add depth, vg3, lsl #16
lsr depth, #(16 + 2 + OT_SHIFT)
lsr depth, #(16 + 2)

// faceAdd
rsb vp0, vertices, vp0, lsr #3
Expand Down
2 changes: 1 addition & 1 deletion src/platform/gba/asm/faceAddMeshTriangles.s
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ faceAddMeshTriangles_asm:
lsl vg0, #16
add depth, vg0, vg1, lsl #16
add depth, vg2, lsl #17
lsr depth, #(16 + 2 + OT_SHIFT)
lsr depth, #(16 + 2)

// faceAdd
rsb vp0, vertices, vp0, lsr #3
Expand Down
3 changes: 1 addition & 2 deletions src/platform/gba/asm/faceAddRoomQuads.s
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ faceAddRoomQuads_asm:

CCW .skip

// depth = MAX_Z4
// depth (vz0) = MAX_Z4
ldrh vz0, [vp0, #VERTEX_Z]
ldrh vz1, [vp1, #VERTEX_Z]
ldrh vz2, [vp2, #VERTEX_Z]
Expand All @@ -103,7 +103,6 @@ faceAddRoomQuads_asm:
movlt vz0, vz2
cmp vz0, vz3
movlt vz0, vz3
mov depth, vz0, lsr #OT_SHIFT

// faceAdd
rsb vp0, vertices, vp0, lsr #3
Expand Down
3 changes: 1 addition & 2 deletions src/platform/gba/asm/faceAddRoomTriangles.s
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,14 @@ faceAddRoomTriangles_asm:

CCW .skip

// depth = MAX_Z3
// depth (vz0) = MAX_Z3
ldrh vz0, [vp0, #VERTEX_Z]
ldrh vz1, [vp1, #VERTEX_Z]
ldrh vz2, [vp2, #VERTEX_Z]
cmp vz0, vz1
movlt vz0, vz1
cmp vz0, vz2
movlt vz0, vz2
mov depth, vz0, lsr #OT_SHIFT

// faceAdd
rsb vp0, vertices, vp0, lsr #3
Expand Down
55 changes: 30 additions & 25 deletions src/platform/gba/asm/sphereIsVisible.s
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ vz .req r13
m .req r14
tmp .req m
vp .req m
vMinXY .req z
vMaxXY .req r
dz .req vz
minXY .req z
maxXY .req r

rMinX .req vx
rMaxX .req x
rMinY .req vy
rMaxY .req y
minX .req vx
maxX .req x
minY .req vy
maxY .req y

.global sphereIsVisible_asm
sphereIsVisible_asm:
Expand All @@ -42,38 +43,42 @@ sphereIsVisible_asm:
mla vz, my, y, vz
mla vz, mz, z, vz

cmp vz, #VIEW_MAX_F
cmp vz, #(VIEW_MAX << FIXED_SHIFT)
bhi .fail

mov x, vx, asr #FIXED_SHIFT
mov y, vy, asr #FIXED_SHIFT
mov z, vz, asr #(FIXED_SHIFT + OT_SHIFT)

mov z, vz, lsr #(FIXED_SHIFT + 4)
add z, vz, lsr #(FIXED_SHIFT + 6)
divLUT tmp, z
add dz, z, z, lsr #2
divLUT tmp, dz
mul x, tmp, x
mul y, tmp, y
mul r, tmp, r

mov x, x, asr #(16 - PROJ_SHIFT)
mov y, y, lsl #(PROJ_SHIFT)
mov y, y, asr #(16 - PROJ_SHIFT)

sub rMinX, x, r, lsr #(16 - PROJ_SHIFT)
add rMaxX, x, r, lsr #(16 - PROJ_SHIFT)
sub rMinY, y, r, lsl #PROJ_SHIFT
add rMaxY, y, r, lsl #PROJ_SHIFT
sub minX, x, r, lsr #(16 - PROJ_SHIFT)
add maxX, x, r, lsr #(16 - PROJ_SHIFT)
sub minY, y, r, lsr #(16 - PROJ_SHIFT)
add maxY, y, r, lsr #(16 - PROJ_SHIFT)

ldr vp, =viewportRel
ldmia vp, {vMinXY, vMaxXY}

cmp rMaxX, vMinXY, asr #16
blt .fail
cmp rMaxY, vMinXY, lsl #16
blt .fail
cmp rMinX, vMaxXY, asr #16
bgt .fail
cmp rMinY, vMaxXY, lsl #16
bgt .fail
ldmia vp, {minXY, maxXY}

cmp maxX, minXY, asr #16
ble .fail
cmp minX, maxXY, asr #16
bge .fail

lsl minXY, #16
lsl maxXY, #16

cmp maxY, minXY, asr #16
ble .fail
cmp minY, maxXY, asr #16
bge .fail

mov r0, #1
fiq_off
Expand Down
21 changes: 10 additions & 11 deletions src/platform/gba/asm/transformMesh.s
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ transformMesh_asm:
asr mw1, #FIXED_SHIFT
fiq_off
ldmia m, {mx2, my2, mz2, mw2}
asr mw2, #FIXED_SHIFT
asr mw2, #(FIXED_SHIFT + OT_SHIFT)
fiq_on

.loop:
Expand All @@ -71,40 +71,39 @@ transformMesh_asm:
mul x, mx0, vx
mla x, my0, vy, x
mla x, mz0, vz, x
add x, mw0, x, asr #FIXED_SHIFT
add x, mw0, x, asr #(FIXED_SHIFT - MESH_SHIFT)

// transform y
mul y, mx1, vx
mla y, my1, vy, y
mla y, mz1, vz, y
add y, mw1, y, asr #FIXED_SHIFT
add y, mw1, y, asr #(FIXED_SHIFT - MESH_SHIFT)
fiq_off

// transform z
mul z, mx2, vx
mla z, my2, vy, z
mla z, mz2, vz, z
add z, mw2, z, asr #FIXED_SHIFT
add z, mw2, z, asr #(FIXED_SHIFT - MESH_SHIFT + OT_SHIFT)

bic vg, #CLIP_MASK // clear clipping flags

// z clipping
cmp z, #VIEW_MIN
movle z, #VIEW_MIN
cmp z, #(VIEW_MIN >> OT_SHIFT)
movle z, #(VIEW_MIN >> OT_SHIFT)
orrle vg, #CLIP_NEAR
cmp z, #VIEW_MAX
movge z, #VIEW_MAX
cmp z, #(VIEW_MAX >> OT_SHIFT)
movge z, #(VIEW_MAX >> OT_SHIFT)
orrge vg, #CLIP_FAR

// project
mov dz, z, lsr #4
add dz, z, lsr #6
add dz, z, z, lsr #2
divLUT tmp, dz
mul dx, x, tmp
mul dy, y, tmp

asr x, dx, #(16 - PROJ_SHIFT)
asr y, dy, #(16 - PROJ_SHIFT)

add x, #(FRAME_WIDTH >> 1)
add y, #(FRAME_HEIGHT >> 1)

Expand Down
Loading

0 comments on commit 90e03fa

Please sign in to comment.