Skip to content

Commit

Permalink
#368 GBA pre-fill prev/next VertexLink's for non-clipped faces
Browse files Browse the repository at this point in the history
  • Loading branch information
XProger committed May 26, 2022
1 parent cb681c2 commit c820952
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 217 deletions.
9 changes: 5 additions & 4 deletions src/platform/gba/asm/common_asm.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@
.equ CLIP_DISCARD, (CLIP_LEFT + CLIP_RIGHT + CLIP_TOP + CLIP_BOTTOM + CLIP_FAR + CLIP_NEAR)
.equ CLIP_MASK, 0xFF00

.equ FACE_TYPE_SHIFT, 14
.equ FACE_TEXTURE_BITS, 14
.equ FACE_TEXTURE, ((1 << FACE_TEXTURE_BITS) - 1)
.equ FACE_TEXTURE, ((1 << FACE_TYPE_SHIFT) - 1)
.equ FACE_GOURAUD, (2 << FACE_TYPE_SHIFT)
.equ FACE_CLIPPED, (1 << 18)
.equ FACE_TRIANGLE, (1 << 19)
.equ FACE_TRIANGLE_BIT, 19
.equ FACE_TRIANGLE, (1 << FACE_TRIANGLE_BIT)

.equ FACE_FLAGS, 0
.equ FACE_NEXT, 4
.equ FACE_INDICES, 8

.equ FACE_TYPE_SHIFT, 14
.equ FACE_TYPE_MASK, (15 << FACE_TYPE_SHIFT)
.equ FACE_TYPE_MASK, (15 << FACE_TYPE_SHIFT)

.equ FACE_TYPE_SHADOW, (0 << FACE_TYPE_SHIFT)
.equ FACE_TYPE_F, (1 << FACE_TYPE_SHIFT)
Expand Down
2 changes: 0 additions & 2 deletions src/platform/gba/asm/faceAddRoomQuads.s
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ depth .req vg0
tmp .req flags
next .req vp0

SP_SIZE = 4

.global faceAddRoomQuads_asm
faceAddRoomQuads_asm:
stmfd sp!, {r4-r7}
Expand Down
120 changes: 84 additions & 36 deletions src/platform/gba/asm/flush.s
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#include "common_asm.inc"

flags .req r0 // flags is always in r0 for rasterize & draw* calls
vXY .req r1
vZG .req r2
tmp .req r3

OT .req r4
flags .req r0 // flags must be in r0 for rasterize & draw* calls
ptr .req r1 // must be in r1
vXY .req r2
vZG .req r3
tmp .req r4
list .req r5
face .req r6
VERTICES .req r7
TEXTURES .req r8
SPRITES .req r9
OT .req r9
TILE .req r10
MASK .req r11

Expand Down Expand Up @@ -39,9 +38,20 @@ vZG0 .req vZG
vXY1 .req index01
vZG1 .req index23

SP_SIZE = (16 * VERTEX_SIZEOF)
vA .req vXY
vB .req vZG

Qs .req ptr
Qe .req TILE
Ts .req MASK
Te .req index01
PN .req index23
sprites .req index01

.extern rasterize_c, drawTriangle, drawQuad, drawPoly
SP_SIZE = (7 * VERTEX_SIZEOF) + 4
SP_SPRITES = SP_SIZE - 4

.extern rasterize_c, drawPoly

.global flush_asm
flush_asm:
Expand All @@ -61,6 +71,25 @@ flush_asm:

str faces, [tmp]

// fill VertexLink prev & next indices
sub sp, #SP_SIZE
add tmp, sp, #VERTEX_PREV
mov Qs, #255
add Qs, #4
mvn Qe, #512
sub Ts, Qs, #1
mvn Te, #256
mvn PN, #65024
// quad
strh Qs, [tmp], #VERTEX_SIZEOF
strh PN, [tmp], #VERTEX_SIZEOF
strh PN, [tmp], #VERTEX_SIZEOF
strh Qe, [tmp], #VERTEX_SIZEOF
// triangle
strh Ts, [tmp], #VERTEX_SIZEOF
strh PN, [tmp], #VERTEX_SIZEOF
strh Te, [tmp], #VERTEX_SIZEOF

ldr tmp, =level
ldr TILE, =gTile
ldr TEXTURES, [tmp, #LEVEL_TEXTURES]
Expand All @@ -71,7 +100,7 @@ flush_asm:
mov MASK, #0xFF00
orr MASK, MASK, MASK, lsl #16

sub sp, #SP_SIZE
str SPRITES, [sp, #SP_SPRITES]
.loop_ot:
ldr face, [list], #-4 // read the first face from the list and decrement
cmp face, #0
Expand All @@ -84,37 +113,40 @@ flush_asm:
ldmia face, {flags, face, index01, index23} // read face params and next face

and type, flags, #FACE_TYPE_MASK

.draw_primitive: // shadows, triangles, quads and clipped polys
cmp type, #FACE_TYPE_GTA
bgt .draw_sprite

tst flags, #FACE_TRIANGLE
moveq ptr, sp // ptr to quad
addne ptr, sp, #(VERTEX_SIZEOF * 4) // ptr to triangle

.set_vertices:
// 1st vertex
mov vertex, index01, lsl #16
add vertex, VERTICES, vertex, lsr #(16 - 3)
ldmia vertex, {vXY, vZG}
stmia sp, {vXY, vZG}
stmia ptr, {vXY, vZG}

// 2nd vertex
add vertex, VERTICES, index01, lsr #(16 - 3) // assumption: vertex index will never exceed 8191
ldmia vertex, {vXY, vZG}
str vXY, [sp, #(VERTEX_X + VERTEX_SIZEOF * 1)]
str vZG, [sp, #(VERTEX_Z + VERTEX_SIZEOF * 1)]
str vXY, [ptr, #(VERTEX_X + VERTEX_SIZEOF * 1)]
str vZG, [ptr, #(VERTEX_Z + VERTEX_SIZEOF * 1)]

// 3rd vertex
mov vertex, index23, lsl #16
add vertex, VERTICES, vertex, lsr #(16 - 3)
ldmia vertex, {vXY, vZG}
str vXY, [sp, #(VERTEX_X + VERTEX_SIZEOF * 2)]
str vZG, [sp, #(VERTEX_Z + VERTEX_SIZEOF * 2)]
str vXY, [ptr, #(VERTEX_X + VERTEX_SIZEOF * 2)]
str vZG, [ptr, #(VERTEX_Z + VERTEX_SIZEOF * 2)]

// 4th vertex (quads only)
tst flags, #FACE_TRIANGLE
addeq vertex, VERTICES, index23, lsr #(16 - 3)
ldmeqia vertex, {vXY, vZG}
streq vXY, [sp, #(VERTEX_X + VERTEX_SIZEOF * 3)]
streq vZG, [sp, #(VERTEX_Z + VERTEX_SIZEOF * 3)]
streq vXY, [ptr, #(VERTEX_X + VERTEX_SIZEOF * 3)]
streq vZG, [ptr, #(VERTEX_Z + VERTEX_SIZEOF * 3)]

// skip texturing for FACE_TYPE_SHADOW and FACE_TYPE_F
cmp type, #FACE_TYPE_F
Expand All @@ -133,35 +165,50 @@ flush_asm:
str texTile, [TILE]

and uv, MASK, uv01
str uv, [sp, #(VERTEX_T + VERTEX_SIZEOF * 0)]
str uv, [ptr, #(VERTEX_T + VERTEX_SIZEOF * 0)]
and uv, MASK, uv01, lsl #8
str uv, [sp, #(VERTEX_T + VERTEX_SIZEOF * 1)]
str uv, [ptr, #(VERTEX_T + VERTEX_SIZEOF * 1)]
and uv, MASK, uv23
str uv, [sp, #(VERTEX_T + VERTEX_SIZEOF * 2)]
str uv, [ptr, #(VERTEX_T + VERTEX_SIZEOF * 2)]
and uv, MASK, uv23, lsl #8
str uv, [sp, #(VERTEX_T + VERTEX_SIZEOF * 3)]
str uv, [ptr, #(VERTEX_T + VERTEX_SIZEOF * 3)]

.draw:
// r0 = flags
mov r1, sp
// r1 = ptr
adr lr, .next_face

tst flags, #FACE_CLIPPED
bne drawPoly
tst flags, #FACE_TRIANGLE
bne drawTriangle
beq drawQuad

// get top vertex for tri or quad rasterization
mov tmp, ptr
ldrsh vA, [tmp, #(VERTEX_Y + VERTEX_SIZEOF * 0)]
ldrsh vB, [tmp, #(VERTEX_Y + VERTEX_SIZEOF * 1)]
cmp vA, vB
addgt ptr, tmp, #(VERTEX_SIZEOF * 1)
movgt vA, vB
ldrsh vB, [tmp, #(VERTEX_Y + VERTEX_SIZEOF * 2)]
cmp vA, vB
addgt ptr, tmp, #(VERTEX_SIZEOF * 2)
movgt vA, vB
lsls vB, flags, #(31 - FACE_TRIANGLE_BIT) // check #FACE_TRIANGLE as sign bit for both pl and gt w/o branch
ldrplsh vB, [tmp, #(VERTEX_Y + VERTEX_SIZEOF * 3)]
cmppl vA, vB
addgt ptr, tmp, #(VERTEX_SIZEOF * 3)
b rasterize_asm

.draw_sprite: // sprites and gui elements
mov ptr, sp
mov vertex, index01, lsl #16
add vertex, VERTICES, vertex, lsr #(16 - 3)
ldmia vertex, {vXY0, vZG0, vXY1, vZG1}
stmia sp, {vXY0, vZG0}
str vXY1, [sp, #(VERTEX_X + VERTEX_SIZEOF * 1)]
str vZG1, [sp, #(VERTEX_Z + VERTEX_SIZEOF * 1)]
stmia ptr, {vXY0, vZG0}
str vXY1, [ptr, #(VERTEX_X + VERTEX_SIZEOF * 1)]
str vZG1, [ptr, #(VERTEX_Z + VERTEX_SIZEOF * 1)]

// r0 = flags
mov r1, sp
// r1 = ptr
adr lr, .next_face

// gui
Expand All @@ -170,17 +217,18 @@ flush_asm:

// sprite
and sprIndex, flags, #0xFF
add sprite, SPRITES, sprIndex, lsl #4
ldr sprites, [sp, #SP_SPRITES]
add sprite, sprites, sprIndex, lsl #4
ldmia sprite, {sprTile, uwvh}
str sprTile, [TILE]
and uv, uwvh, MASK
str uv, [sp, #(VERTEX_T + VERTEX_SIZEOF * 0)]
str uv, [ptr, #(VERTEX_T + VERTEX_SIZEOF * 0)]
bic uv, uwvh, MASK
str uv, [sp, #(VERTEX_T + VERTEX_SIZEOF * 1)]
str uv, [ptr, #(VERTEX_T + VERTEX_SIZEOF * 1)]
b rasterize_asm

.next_face:
cmp face, #0
tst face, face
bne .loop_list

.next_ot:
Expand Down
Loading

0 comments on commit c820952

Please sign in to comment.