Skip to content

Commit bfb0551

Browse files
ZERICO2005mateoconlechuga
authored andcommitted
optimized gfx_FillTriangle
1 parent a5d47dd commit bfb0551

1 file changed

Lines changed: 75 additions & 120 deletions

File tree

src/graphx/graphx.asm

Lines changed: 75 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,134 +4104,96 @@ _FillTriangle:
41044104
ld (ix + 6), de
41054105
ld (ix + 12), hl
41064106
.cmp2:
4107-
ld de, (ix + 21) ; if (y0 == y2) - handle awkward all-on-same-line case as its own thing
4108-
ld hl, (ix + 9)
4109-
or a, a
4110-
sbc hl, de
4111-
jr nz, .notflat
4112-
ld bc, (ix + 6) ; x0
4113-
ld (ix - 6), bc ; a = x0
4114-
ld (ix - 3), bc ; b = x0;
4115-
ld hl, (ix + 12) ; if (x1 < a) { a = x1; }
4107+
ld hl, (ix + 21) ; if (y0 == y2) - handle awkward all-on-same-line case as its own thing
4108+
ld bc, (ix + 9)
41164109
or a, a
41174110
sbc hl, bc
4118-
jp p, .cmp00
4119-
jp pe, .cmp01
4120-
jr .cmp02
4121-
.cmp00:
4122-
jp po, .cmp01
4123-
.cmp02:
4124-
ld bc, (ix + 12)
4125-
ld (ix - 3), bc
4126-
jr .cmp11
4127-
.cmp01:
4128-
ld bc, (ix + 12)
4129-
ld hl, (ix - 6)
4130-
or a, a
4131-
sbc hl, bc ; else if (x1 > b) { b = x1; }
4132-
jp p, .cmp10
4133-
jp pe, .cmp11
4134-
jr .cmp12
4135-
.cmp10:
4136-
jp po, .cmp11
4137-
.cmp12:
4138-
ld bc, (ix + 12)
4139-
ld (ix - 6), bc
4140-
.cmp11:
4141-
ld bc, (ix - 3)
4142-
ld hl, (ix + 18)
4143-
or a, a
4144-
sbc hl, bc ; if (x2 < a) { a = x2; }
4145-
jp p, .cmp20
4146-
jp pe, .cmp21
4147-
jr .cmp22
4148-
.cmp20:
4149-
jp po, .cmp21
4150-
.cmp22:
4151-
ld bc, (ix + 18)
4152-
ld (ix - 3), bc
4153-
jr .cmp31
4154-
.cmp21:
4155-
ld bc, (ix + 18)
4156-
ld hl, (ix - 6)
4111+
ld de, (ix + 6) ; x0
4112+
ld hl, (ix + 12) ; x1
4113+
jr nz, .notflat
4114+
;-------------------------------------------------------------------------------
4115+
; draw a flat horizontal triangle
4116+
; x_min = min(x0, x1, x2)
4117+
; x_max = max(x0, x1, x2)
4118+
; horizline(x_min, y0, x_max - x_min + 1)
4119+
; DE = x0, HL = x1, BC = y0
4120+
call _Minimum.no_carry
4121+
ld de, (ix + 18) ; x2
4122+
call _Minimum
4123+
push hl ; x_min
4124+
ld hl, (ix + 6) ; x0
4125+
ld de, (ix + 12) ; x1
4126+
call _Maximum
4127+
ld de, (ix + 18) ; x2
4128+
call _Maximum
4129+
pop de ; x_min
41574130
or a, a
4158-
sbc hl, bc ; else if (x2 > b) { b = x2; }
4159-
jp p, .cmp30
4160-
jp pe, .cmp31
4161-
jr .cmp32
4131+
sbc hl, de
4132+
inc hl
4133+
push hl ; x_max - x_min + 1
4134+
push bc ; y0
4135+
push de ; x_min
4136+
call 0 ; horizline(x_min, y0, x_max - x_min + 1)
4137+
.line0 := $-3
4138+
ld sp, ix
4139+
pop ix
4140+
ret
4141+
;-------------------------------------------------------------------------------
41624142
.notflat:
4163-
ld bc, (ix + 6) ; x0
4164-
ld hl, (ix + 12)
41654143
or a, a
4166-
sbc hl, bc
4144+
sbc hl, de
41674145
ld (ix - 36), hl ; dx01 = x1 - x0;
41684146
ld hl, (ix + 18)
41694147
or a, a
4170-
sbc hl, bc
4148+
sbc hl, de
41714149
ld (ix - 21), hl ; dx02 = x2 - x0;
4172-
ld bc, (ix + 9) ; y0
4150+
4151+
ld de, (ix + 9) ; y0
41734152
ld hl, (ix + 15)
41744153
or a, a
4175-
sbc hl, bc
4154+
sbc hl, de
41764155
ld (ix - 33), hl ; dy01 = y1 - y0;
41774156
ld hl, (ix + 21)
41784157
or a, a
4179-
sbc hl, bc
4158+
sbc hl, de
41804159
ld (ix - 27), hl ; dy02 = y2 - y0;
4181-
ld bc, (ix + 12)
4160+
4161+
ld de, (ix + 12)
41824162
ld hl, (ix + 18)
41834163
or a, a
4184-
sbc hl, bc
4164+
sbc hl, de
41854165
ld (ix - 30), hl ; dx12 = x2 - x1;
4166+
41864167
ld bc, (ix + 15)
41874168
ld hl, (ix + 21)
41884169
or a, a
41894170
sbc hl, bc
41904171
ld (ix - 39), hl ; dy12 = y2 - y1;
4191-
jr nz, .elselast ; if (y1 == y2) { last = y1; }
4192-
ld (ix - 24), bc
4193-
jr .sublast
4194-
.cmp30:
4195-
jp po, .cmp31
4196-
.cmp32:
4197-
ld bc, (ix + 18)
4198-
ld (ix - 6), bc
4199-
.cmp31:
4200-
ld de, (ix - 3)
4201-
ld hl, (ix - 6)
4202-
or a, a
4203-
sbc hl, de
4204-
inc hl
4205-
push hl
4206-
ld bc, (ix + 9)
4207-
push bc
4208-
push de
4209-
call 0 ; horizline(a, y0, b-a+1);
4210-
.line0 := $-3
4211-
ld sp, ix
4212-
pop ix
4213-
ret ; return;
4214-
.elselast:
4215-
ld bc, (ix + 15) ; else { last = y1-1; }
4172+
; if (y1 == y2) { last = y1; }
4173+
jr z, .sublast
4174+
; else { last = y1-1; }
42164175
dec bc
4217-
ld (ix - 24), bc
42184176
.sublast:
4177+
ld (ix - 24), bc
42194178
ld bc, (ix + 9)
42204179
ld (ix - 12), bc ; for (y = y0; y <= last; y++)
42214180
jr .firstloopstart
4181+
;-------------------------------------------------------------------------------
42224182
.firstloop:
42234183
ld hl, (ix - 15)
42244184
ld bc, (ix - 33)
42254185
call _DivideHLBC
42264186
ld bc, (ix + 6)
42274187
add hl, bc
4228-
ld (ix - 3), hl ; a = x0 + sa / dy01;
4188+
; a = x0 + sa / dy01;
4189+
push hl ; ld (ix - 3), hl
42294190
ld hl, (ix - 18)
42304191
ld bc, (ix - 27)
42314192
call _DivideHLBC
42324193
ld bc, (ix + 6)
42334194
add hl, bc
4234-
ld (ix - 6), hl ; b = x0 + sb / dy02;
4195+
; b = x0 + sb / dy02;
4196+
push hl ; ld (ix - 6), hl
42354197
ld bc, (ix - 36)
42364198
ld hl, (ix - 15)
42374199
add hl, bc
@@ -4240,22 +4202,17 @@ _FillTriangle:
42404202
ld hl, (ix - 18)
42414203
add hl, bc
42424204
ld (ix - 18), hl ; sb += dx02;
4243-
ld de, (ix - 3)
4244-
ld hl, (ix - 6)
4205+
pop hl ; ld hl, (ix - 6)
4206+
pop de ; ld de, (ix - 3)
42454207
or a, a
42464208
sbc hl, de ; if (b < a) { swap(a, b); }
4247-
jp p, .cmp40
4248-
jp pe, .cmp41
4249-
jr .cmp42
4250-
.cmp40:
4251-
jp po, .cmp41
4252-
.cmp42:
4253-
ld hl, (ix - 3)
4254-
ld de, (ix - 6)
4255-
ld (ix - 3), de
4256-
ld (ix - 6), hl
4257-
.cmp41:
4258-
ld hl, (ix - 6)
4209+
add hl, de
4210+
jp p, .cmp43
4211+
ex de, hl
4212+
.cmp43:
4213+
jp po, .cmp44
4214+
ex de, hl
4215+
.cmp44:
42594216
or a, a
42604217
sbc hl, de
42614218
inc hl
@@ -4295,20 +4252,24 @@ _FillTriangle:
42954252
ld de, (ix - 21)
42964253
call _MultiplyHLDE ; sb = dx02 * (y - y0);
42974254
ld (ix - 18), hl
4255+
ld bc, (ix - 12)
42984256
jr .secondloopstart ; for (; y <= y2; y++)
4257+
;-------------------------------------------------------------------------------
42994258
.secondloop:
43004259
ld hl, (ix - 15)
43014260
ld bc, (ix - 39)
43024261
call _DivideHLBC
43034262
ld bc, (ix + 12)
43044263
add hl, bc
4305-
ld (ix - 3), hl ; a = x1 + sa / dy12;
4264+
; a = x1 + sa / dy12;
4265+
push hl ; ld (ix - 3), hl
43064266
ld hl, (ix - 18)
43074267
ld bc, (ix - 27)
43084268
call _DivideHLBC
43094269
ld bc, (ix + 6)
43104270
add hl, bc
4311-
ld (ix - 6), hl ; b = x0 + sb / dy02;
4271+
; b = x0 + sb / dy02;
4272+
push hl ; ld (ix - 6), hl
43124273
ld bc, (ix - 30)
43134274
ld hl, (ix - 15)
43144275
add hl, bc
@@ -4317,22 +4278,17 @@ _FillTriangle:
43174278
ld hl, (ix - 18)
43184279
add hl, bc
43194280
ld (ix - 18), hl ; sb += dx02;
4320-
ld de, (ix - 3)
4321-
ld hl, (ix - 6)
4281+
pop hl ; ld hl, (ix - 6)
4282+
pop de ; ld de, (ix - 3)
43224283
or a, a
43234284
sbc hl, de ; if (b < a) { swap(a, b); }
4324-
jp p, .cmp60
4325-
jp pe, .cmp61
4326-
jr .cmp62
4327-
.cmp60:
4328-
jp po, .cmp61
4329-
.cmp62:
4330-
ld hl, (ix - 3)
4331-
ld de, (ix - 6)
4332-
ld (ix - 3), de
4333-
ld (ix - 6), hl
4334-
.cmp61:
4335-
ld hl, (ix - 6)
4285+
add hl, de
4286+
jp p, .cmp63
4287+
ex de, hl
4288+
.cmp63:
4289+
jp po, .cmp64
4290+
ex de, hl
4291+
.cmp64:
43364292
or a, a
43374293
sbc hl, de
43384294
inc hl
@@ -4349,7 +4305,6 @@ _FillTriangle:
43494305
inc bc
43504306
ld (ix - 12), bc
43514307
.secondloopstart:
4352-
ld bc, (ix - 12)
43534308
ld hl, (ix + 21)
43544309
or a, a
43554310
sbc hl, bc

0 commit comments

Comments
 (0)