Skip to content

Commit 5dea67d

Browse files
ZERICO2005mateoconlechuga
authored andcommitted
optimized gfx_triangle stack frame
1 parent bfb0551 commit 5dea67d

1 file changed

Lines changed: 135 additions & 127 deletions

File tree

src/graphx/graphx.asm

Lines changed: 135 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -4056,60 +4056,77 @@ gfx_FillTriangle:
40564056
; Returns:
40574057
; None
40584058
ld hl, gfx_HorizLine
4059+
tri_frame_offset := 30
4060+
tri_x0 := tri_frame_offset + 6
4061+
tri_y0 := tri_frame_offset + 9
4062+
tri_x1 := tri_frame_offset + 12
4063+
tri_y1 := tri_frame_offset + 15
4064+
tri_x2 := tri_frame_offset + 18
4065+
tri_y2 := tri_frame_offset + 21
4066+
tri_y_counter := tri_frame_offset - 3
4067+
tri_sa := tri_frame_offset - 6
4068+
tri_sb := tri_frame_offset - 9
4069+
tri_dx02 := tri_frame_offset - 12
4070+
tri_last := tri_frame_offset - 15
4071+
tri_dy02 := tri_frame_offset - 18
4072+
tri_dx12 := tri_frame_offset - 21
4073+
tri_dy01 := tri_frame_offset - 24
4074+
tri_dx01 := tri_frame_offset - 27
4075+
tri_dy12 := tri_frame_offset - 30
40594076
_FillTriangle:
40604077
ld (.line0), hl
40614078
ld (.line1), hl
40624079
ld (.line2), hl
40634080
push ix
4064-
ld ix, 0
4081+
ld ix, -tri_frame_offset
40654082
add ix, sp
4066-
lea hl, ix - 39
4067-
ld sp, hl
4083+
ld sp, ix
4084+
or a, a
40684085
sbc hl, hl
4069-
ld (ix - 15), hl
4070-
ld (ix - 18), hl ; int sa = 0, sb = 0;
4071-
ld hl, (ix + 9) ; sort coordinates by y order (y2 >= y1 >= y0)
4072-
ld de, (ix + 15) ; if (y0 > y1)
4086+
ld (ix + tri_sa), hl
4087+
ld (ix + tri_sb), hl ; int sa = 0, sb = 0;
4088+
ld hl, (ix + tri_y0) ; sort coordinates by y order (y2 >= y1 >= y0)
4089+
ld de, (ix + tri_y1) ; if (y0 > y1)
40734090
call _SignedCompare
40744091
jr c, .cmp0
4075-
ld hl, (ix + 9)
4076-
ld (ix + 9), de
4077-
ld (ix + 15), hl
4078-
ld hl, (ix + 6)
4079-
ld de, (ix + 12)
4080-
ld (ix + 6), de
4081-
ld (ix + 12), hl
4092+
ld hl, (ix + tri_y0)
4093+
ld (ix + tri_y0), de
4094+
ld (ix + tri_y1), hl
4095+
ld hl, (ix + tri_x0)
4096+
ld de, (ix + tri_x1)
4097+
ld (ix + tri_x0), de
4098+
ld (ix + tri_x1), hl
40824099
.cmp0:
4083-
ld hl, (ix + 15)
4084-
ld de, (ix + 21)
4100+
ld hl, (ix + tri_y1)
4101+
ld de, (ix + tri_y2)
40854102
call _SignedCompare
40864103
jr c, .cmp1
4087-
ld hl, (ix + 15)
4088-
ld (ix + 15), de
4089-
ld (ix + 21), hl
4090-
ld hl, (ix + 12)
4091-
ld de, (ix + 18)
4092-
ld (ix + 12), de
4093-
ld (ix + 18), hl
4104+
ld hl, (ix + tri_y1)
4105+
ld (ix + tri_y1), de
4106+
ld (ix + tri_y2), hl
4107+
ld hl, (ix + tri_x1)
4108+
ld de, (ix + tri_x2)
4109+
ld (ix + tri_x1), de
4110+
ld (ix + tri_x2), hl
40944111
.cmp1:
4095-
ld hl, (ix + 9)
4096-
ld de, (ix + 15)
4112+
ld hl, (ix + tri_y0)
4113+
ld de, (ix + tri_y1)
40974114
call _SignedCompare
40984115
jr c, .cmp2
4099-
ld hl, (ix + 9)
4100-
ld (ix + 9), de
4101-
ld (ix + 15), hl
4102-
ld hl, (ix + 6)
4103-
ld de, (ix + 12)
4104-
ld (ix + 6), de
4105-
ld (ix + 12), hl
4116+
ld hl, (ix + tri_y0)
4117+
ld (ix + tri_y0), de
4118+
ld (ix + tri_y1), hl
4119+
ld hl, (ix + tri_x0)
4120+
ld de, (ix + tri_x1)
4121+
ld (ix + tri_x0), de
4122+
ld (ix + tri_x1), hl
41064123
.cmp2:
4107-
ld hl, (ix + 21) ; if (y0 == y2) - handle awkward all-on-same-line case as its own thing
4108-
ld bc, (ix + 9)
4124+
ld hl, (ix + tri_y2) ; if (y0 == y2) - handle awkward all-on-same-line case as its own thing
4125+
ld bc, (ix + tri_y0)
41094126
or a, a
41104127
sbc hl, bc
4111-
ld de, (ix + 6) ; x0
4112-
ld hl, (ix + 12) ; x1
4128+
ld de, (ix + tri_x0) ; x0
4129+
ld hl, (ix + tri_x1) ; x1
41134130
jr nz, .notflat
41144131
;-------------------------------------------------------------------------------
41154132
; draw a flat horizontal triangle
@@ -4118,13 +4135,13 @@ _FillTriangle:
41184135
; horizline(x_min, y0, x_max - x_min + 1)
41194136
; DE = x0, HL = x1, BC = y0
41204137
call _Minimum.no_carry
4121-
ld de, (ix + 18) ; x2
4138+
ld de, (ix + tri_x2) ; x2
41224139
call _Minimum
41234140
push hl ; x_min
4124-
ld hl, (ix + 6) ; x0
4125-
ld de, (ix + 12) ; x1
4141+
ld hl, (ix + tri_x0) ; x0
4142+
ld de, (ix + tri_x1) ; x1
41264143
call _Maximum
4127-
ld de, (ix + 18) ; x2
4144+
ld de, (ix + tri_x2) ; x2
41284145
call _Maximum
41294146
pop de ; x_min
41304147
or a, a
@@ -4135,75 +4152,76 @@ _FillTriangle:
41354152
push de ; x_min
41364153
call 0 ; horizline(x_min, y0, x_max - x_min + 1)
41374154
.line0 := $-3
4138-
ld sp, ix
4155+
lea hl, ix + tri_frame_offset
4156+
ld sp, hl
41394157
pop ix
41404158
ret
41414159
;-------------------------------------------------------------------------------
41424160
.notflat:
41434161
or a, a
41444162
sbc hl, de
4145-
ld (ix - 36), hl ; dx01 = x1 - x0;
4146-
ld hl, (ix + 18)
4163+
ld (ix + tri_dx01), hl ; dx01 = x1 - x0;
4164+
ld hl, (ix + tri_x2)
41474165
or a, a
41484166
sbc hl, de
4149-
ld (ix - 21), hl ; dx02 = x2 - x0;
4167+
ld (ix + tri_dx02), hl ; dx02 = x2 - x0;
41504168

4151-
ld de, (ix + 9) ; y0
4152-
ld hl, (ix + 15)
4169+
ld de, (ix + tri_y0) ; y0
4170+
ld hl, (ix + tri_y1)
41534171
or a, a
41544172
sbc hl, de
4155-
ld (ix - 33), hl ; dy01 = y1 - y0;
4156-
ld hl, (ix + 21)
4173+
ld (ix + tri_dy01), hl ; dy01 = y1 - y0;
4174+
ld hl, (ix + tri_y2)
41574175
or a, a
41584176
sbc hl, de
4159-
ld (ix - 27), hl ; dy02 = y2 - y0;
4177+
ld (ix + tri_dy02), hl ; dy02 = y2 - y0;
41604178

4161-
ld de, (ix + 12)
4162-
ld hl, (ix + 18)
4179+
ld de, (ix + tri_x1)
4180+
ld hl, (ix + tri_x2)
41634181
or a, a
41644182
sbc hl, de
4165-
ld (ix - 30), hl ; dx12 = x2 - x1;
4183+
ld (ix + tri_dx12), hl ; dx12 = x2 - x1;
41664184

4167-
ld bc, (ix + 15)
4168-
ld hl, (ix + 21)
4185+
ld bc, (ix + tri_y1)
4186+
ld hl, (ix + tri_y2)
41694187
or a, a
41704188
sbc hl, bc
4171-
ld (ix - 39), hl ; dy12 = y2 - y1;
4189+
ld (ix + tri_dy12), hl ; dy12 = y2 - y1;
41724190
; if (y1 == y2) { last = y1; }
41734191
jr z, .sublast
41744192
; else { last = y1-1; }
41754193
dec bc
41764194
.sublast:
4177-
ld (ix - 24), bc
4178-
ld bc, (ix + 9)
4179-
ld (ix - 12), bc ; for (y = y0; y <= last; y++)
4195+
ld (ix + tri_last), bc
4196+
ld bc, (ix + tri_y0)
4197+
ld (ix + tri_y_counter), bc ; for (y = y0; y <= last; y++)
41804198
jr .firstloopstart
41814199
;-------------------------------------------------------------------------------
4200+
.cmp50:
4201+
jp pe, .firstloopfinish
41824202
.firstloop:
4183-
ld hl, (ix - 15)
4184-
ld bc, (ix - 33)
4203+
ld hl, (ix + tri_sa)
4204+
ld bc, (ix + tri_dy01)
41854205
call _DivideHLBC
4186-
ld bc, (ix + 6)
4206+
ld bc, (ix + tri_x0)
41874207
add hl, bc
4188-
; a = x0 + sa / dy01;
4189-
push hl ; ld (ix - 3), hl
4190-
ld hl, (ix - 18)
4191-
ld bc, (ix - 27)
4208+
push hl ; a = x0 + sa / dy01;
4209+
ld hl, (ix + tri_sb)
4210+
ld bc, (ix + tri_dy02)
41924211
call _DivideHLBC
4193-
ld bc, (ix + 6)
4212+
ld bc, (ix + tri_x0)
41944213
add hl, bc
4195-
; b = x0 + sb / dy02;
4196-
push hl ; ld (ix - 6), hl
4197-
ld bc, (ix - 36)
4198-
ld hl, (ix - 15)
4214+
push hl ; b = x0 + sb / dy02;
4215+
ld hl, (ix + tri_sa)
4216+
ld bc, (ix + tri_dx01)
41994217
add hl, bc
4200-
ld (ix - 15), hl ; sa += dx01;
4201-
ld bc, (ix - 21)
4202-
ld hl, (ix - 18)
4218+
ld (ix + tri_sa), hl ; sa += dx01;
4219+
ld hl, (ix + tri_sb)
4220+
ld bc, (ix + tri_dx02)
42034221
add hl, bc
4204-
ld (ix - 18), hl ; sb += dx02;
4205-
pop hl ; ld hl, (ix - 6)
4206-
pop de ; ld de, (ix - 3)
4222+
ld (ix + tri_sb), hl ; sb += dx02;
4223+
pop hl ; HL = b
4224+
pop de ; DE = a
42074225
or a, a
42084226
sbc hl, de ; if (b < a) { swap(a, b); }
42094227
add hl, de
@@ -4217,69 +4235,64 @@ _FillTriangle:
42174235
sbc hl, de
42184236
inc hl
42194237
push hl
4220-
ld bc, (ix - 12)
4238+
ld bc, (ix + tri_y_counter)
42214239
push bc
42224240
push de
42234241
call 0 ; horizline(a, y, b-a+1);
42244242
.line1 := $-3
4225-
pop bc
4226-
pop bc
4227-
pop bc
4228-
ld bc, (ix - 12)
4243+
ld sp, ix
4244+
ld bc, (ix + tri_y_counter)
42294245
inc bc
4230-
ld (ix - 12), bc
4246+
ld (ix + tri_y_counter), bc
42314247
.firstloopstart:
4232-
ld hl, (ix - 24)
4248+
ld hl, (ix + tri_last)
42334249
or a, a
42344250
sbc hl, bc
42354251
jp p, .cmp50
42364252
jp pe, .firstloop
4237-
jr .cmp52
4238-
.cmp50:
4239-
jp po, .firstloop
4240-
.cmp52:
4241-
ld bc, (ix + 15)
4242-
ld hl, (ix - 12)
4253+
.firstloopfinish:
4254+
ld bc, (ix + tri_y1)
4255+
ld hl, (ix + tri_y_counter)
42434256
or a, a
42444257
sbc hl, bc
4245-
ld de, (ix - 30)
4258+
ld de, (ix + tri_dx12)
42464259
call _MultiplyHLDE ; sa = dx12 * (y - y1);
4247-
ld (ix - 15), hl
4248-
ld bc, (ix + 9)
4249-
ld hl, (ix - 12)
4260+
ld (ix + tri_sa), hl
4261+
ld bc, (ix + tri_y0)
4262+
ld hl, (ix + tri_y_counter)
42504263
or a, a
42514264
sbc hl, bc
4252-
ld de, (ix - 21)
4265+
ld de, (ix + tri_dx02)
42534266
call _MultiplyHLDE ; sb = dx02 * (y - y0);
4254-
ld (ix - 18), hl
4255-
ld bc, (ix - 12)
4267+
ld (ix + tri_sb), hl
4268+
ld bc, (ix + tri_y_counter)
42564269
jr .secondloopstart ; for (; y <= y2; y++)
42574270
;-------------------------------------------------------------------------------
4271+
.cmp70:
4272+
jp pe, .secondloopfinish
42584273
.secondloop:
4259-
ld hl, (ix - 15)
4260-
ld bc, (ix - 39)
4274+
ld hl, (ix + tri_sa)
4275+
ld bc, (ix + tri_dy12)
42614276
call _DivideHLBC
4262-
ld bc, (ix + 12)
4277+
ld bc, (ix + tri_x1)
42634278
add hl, bc
4264-
; a = x1 + sa / dy12;
4265-
push hl ; ld (ix - 3), hl
4266-
ld hl, (ix - 18)
4267-
ld bc, (ix - 27)
4279+
push hl ; a = x1 + sa / dy12;
4280+
ld hl, (ix + tri_sb)
4281+
ld bc, (ix + tri_dy02)
42684282
call _DivideHLBC
4269-
ld bc, (ix + 6)
4283+
ld bc, (ix + tri_x0)
42704284
add hl, bc
4271-
; b = x0 + sb / dy02;
4272-
push hl ; ld (ix - 6), hl
4273-
ld bc, (ix - 30)
4274-
ld hl, (ix - 15)
4285+
push hl ; b = x0 + sb / dy02;
4286+
ld hl, (ix + tri_sa)
4287+
ld bc, (ix + tri_dx12)
42754288
add hl, bc
4276-
ld (ix - 15), hl ; sa += dx12;
4277-
ld bc, (ix - 21)
4278-
ld hl, (ix - 18)
4289+
ld (ix + tri_sa), hl ; sa += dx12;
4290+
ld hl, (ix + tri_sb)
4291+
ld bc, (ix + tri_dx02)
42794292
add hl, bc
4280-
ld (ix - 18), hl ; sb += dx02;
4281-
pop hl ; ld hl, (ix - 6)
4282-
pop de ; ld de, (ix - 3)
4293+
ld (ix + tri_sb), hl ; sb += dx02;
4294+
pop hl ; HL = b
4295+
pop de ; DE = a
42834296
or a, a
42844297
sbc hl, de ; if (b < a) { swap(a, b); }
42854298
add hl, de
@@ -4293,29 +4306,24 @@ _FillTriangle:
42934306
sbc hl, de
42944307
inc hl
42954308
push hl
4296-
ld bc, (ix - 12)
4309+
ld bc, (ix + tri_y_counter)
42974310
push bc
42984311
push de
42994312
call 0 ; horizline(a, y, b-a+1);
43004313
.line2 := $-3
4301-
pop bc
4302-
pop bc
4303-
pop bc
4304-
ld bc, (ix - 12)
4314+
ld sp, ix
4315+
ld bc, (ix + tri_y_counter)
43054316
inc bc
4306-
ld (ix - 12), bc
4317+
ld (ix + tri_y_counter), bc
43074318
.secondloopstart:
4308-
ld hl, (ix + 21)
4319+
ld hl, (ix + tri_y2)
43094320
or a, a
43104321
sbc hl, bc
43114322
jp p, .cmp70
43124323
jp pe, .secondloop
4313-
ld sp, ix
4314-
pop ix
4315-
ret
4316-
.cmp70:
4317-
jp po, .secondloop
4318-
ld sp, ix
4324+
.secondloopfinish:
4325+
lea hl, ix + tri_frame_offset
4326+
ld sp, hl
43194327
pop ix
43204328
ret
43214329

0 commit comments

Comments
 (0)