;
; d_polysa.asm
; x86 assembly-language polygon model drawing code
; this file uses NASM syntax.
;
; Copyright (C) 1996-1997  Id Software, Inc.
;
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License
; as published by the Free Software Foundation; either version 2
; of the License, or (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;
; See the GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to:
; Free Software Foundation, Inc.
; 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
;

%include "asm_nasm.inc"
%include "d_polysa.inc"

; underscore prefix handling
; for C-shared symbols:
%ifmacro _sym_prefix
; C-shared externs:
 _sym_prefix alias_colormap
 _sym_prefix d_viewbuffer
 _sym_prefix d_scantable
 _sym_prefix r_refdef
 _sym_prefix a_sstepxfrac
 _sym_prefix r_affinetridesc
 _sym_prefix acolormap
 _sym_prefix d_pcolormap
 _sym_prefix d_sfrac
 _sym_prefix d_ptex
 _sym_prefix d_pedgespanpackage
 _sym_prefix d_tfrac
 _sym_prefix d_light
 _sym_prefix d_zi
 _sym_prefix d_pdest
 _sym_prefix d_pz
 _sym_prefix d_aspancount
 _sym_prefix erroradjustup
 _sym_prefix erroradjustdown
 _sym_prefix errorterm
 _sym_prefix d_xdenom
 _sym_prefix r_p0
 _sym_prefix r_p1
 _sym_prefix r_p2
 _sym_prefix a_tstepxfrac
 _sym_prefix a_ststepxwhole
 _sym_prefix zspantable
 _sym_prefix skintable
 _sym_prefix d_countextrastep
 _sym_prefix ubasestep
 _sym_prefix a_spans
 _sym_prefix d_pdestextrastep
 _sym_prefix d_pzextrastep
 _sym_prefix d_sfracextrastep
 _sym_prefix d_ptexextrastep
 _sym_prefix d_tfracextrastep
 _sym_prefix d_lightextrastep
 _sym_prefix d_ziextrastep
 _sym_prefix d_pdestbasestep
 _sym_prefix d_pzbasestep
 _sym_prefix d_sfracbasestep
 _sym_prefix d_ptexbasestep
 _sym_prefix d_tfracbasestep
 _sym_prefix d_lightbasestep
 _sym_prefix d_zibasestep
 _sym_prefix r_lstepx
 _sym_prefix r_lstepy
 _sym_prefix r_sstepx
 _sym_prefix r_sstepy
 _sym_prefix r_tstepx
 _sym_prefix r_tstepy
 _sym_prefix r_zistepx
 _sym_prefix r_zistepy
; C-shared globals:
 _sym_prefix D_PolysetAff8Start
 _sym_prefix R_PolysetDrawSpans8_Opaque
 _sym_prefix D_PolysetAff8End
 _sym_prefix D_Aff8Patch
 _sym_prefix R_PolysetScanLeftEdge
%endif	; _sym_prefix

; externs from C code
 extern alias_colormap
 extern d_viewbuffer
 extern d_scantable
 extern r_refdef
 extern a_sstepxfrac
 extern r_affinetridesc
 extern acolormap
 extern d_pcolormap
 extern d_sfrac
 extern d_ptex
 extern d_pedgespanpackage
 extern d_tfrac
 extern d_light
 extern d_zi
 extern d_pdest
 extern d_pz
 extern d_aspancount
 extern erroradjustup
 extern erroradjustdown
 extern errorterm
 extern d_xdenom
 extern r_p0
 extern r_p1
 extern r_p2
 extern a_tstepxfrac
 extern a_ststepxwhole
 extern zspantable
 extern skintable
 extern d_countextrastep
 extern ubasestep
 extern a_spans
 extern d_pdestextrastep
 extern d_pzextrastep
 extern d_sfracextrastep
 extern d_ptexextrastep
 extern d_tfracextrastep
 extern d_lightextrastep
 extern d_ziextrastep
 extern d_pdestbasestep
 extern d_pzbasestep
 extern d_sfracbasestep
 extern d_ptexbasestep
 extern d_tfracbasestep
 extern d_lightbasestep
 extern d_zibasestep
 extern r_lstepx
 extern r_lstepy
 extern r_sstepx
 extern r_sstepy
 extern r_tstepx
 extern r_tstepy
 extern r_zistepx
 extern r_zistepy

; externs from ASM-only code
 extern float_point5
 extern float_1
 extern float_minus_1
 extern float_0
 extern advancetable
 extern sstep
 extern tstep
 extern ceil_cw
 extern single_cw


SEGMENT .data

 ALIGN 4

%if 0
p10_minus_p20 dd 0
p01_minus_p21 dd 0
%endif
;temp0 dd 0
;temp1 dd 0
;Ltemp dd 0
aff8entryvec_table dd LDraw8, LDraw7, LDraw6, LDraw5
 dd LDraw4, LDraw3, LDraw2, LDraw1
lzistepx dd 0

SEGMENT .text

;;;;;;;;;;;;;;;;;;;;;;;;
; R_PolysetCalcGradients
;;;;;;;;;;;;;;;;;;;;;;;;
%if 0
 global R_PolysetCalcGradients
R_PolysetCalcGradients:
 fild dword [r_p0+0]
 fild dword [r_p2+0]
 fild dword [r_p0+4]
 fild dword [r_p2+4]
 fild dword [r_p1+0]
 fild dword [r_p1+4]
 fxch st3
 fsub st0,st2
 fxch st1
 fsub st0,st4
 fxch st5
 fsubrp st4,st0
 fxch st2
 fsubrp st1,st0
 fxch st1
 fld dword [d_xdenom]
 fxch st4
 fstp dword [p10_minus_p20]
 fstp dword [p01_minus_p21]
 fxch st2
 fild dword [r_p2+16]
 fild dword [r_p0+16]
 fild dword [r_p1+16]
 fxch st2
 fld st0
 fsubp st2,st0
 fsubp st2,st0
 fld st0
 fmul st0,st5
 fxch st2
 fld st0
 fmul dword [p01_minus_p21]
 fxch st2
 fmul dword [p10_minus_p20]
 fxch st1
 fmul st0,st5
 fxch st2
 fsubrp st3,st0
 fsubp st1,st0
 fld st2
 fmul dword [float_minus_1]
 fxch st2
 fmul st0,st3
 fxch st1
 fmul st0,st2
 fldcw word [ceil_cw]
 fistp dword [r_lstepy]
 fistp dword [r_lstepx]
 fldcw word [single_cw]
 fild dword [r_p2+8]
 fild dword [r_p0+8]
 fild dword [r_p1+8]
 fxch st2
 fld st0
 fsubp st2,st0
 fsubp st2,st0
 fld st0
 fmul st0,st6
 fxch st2
 fld st0
 fmul dword [p01_minus_p21]
 fxch st2
 fmul dword [p10_minus_p20]
 fxch st1
 fmul st0,st6
 fxch st2
 fsubrp st3,st0
 fsubp st1,st0
 fmul st0,st2
 fxch st1
 fmul st0,st3
 fxch st1
 fistp dword [r_sstepy]
 fistp dword [r_sstepx]
 fild dword [r_p2+12]
 fild dword [r_p0+12]
 fild dword [r_p1+12]
 fxch st2
 fld st0
 fsubp st2,st0
 fsubp st2,st0
 fld st0
 fmul st0,st6
 fxch st2
 fld st0
 fmul dword [p01_minus_p21]
 fxch st2
 fmul dword [p10_minus_p20]
 fxch st1
 fmul st0,st6
 fxch st2
 fsubrp st3,st0
 fsubp st1,st0
 fmul st0,st2
 fxch st1
 fmul st0,st3
 fxch st1
 fistp dword [r_tstepy]
 fistp dword [r_tstepx]
 fild dword [r_p2+20]
 fild dword [r_p0+20]
 fild dword [r_p1+20]
 fxch st2
 fld st0
 fsubp st2,st0
 fsubp st2,st0
 fld st0
 fmulp st6,st0
 fxch st1
 fld st0
 fmul dword [p01_minus_p21]
 fxch st2
 fmul dword [p10_minus_p20]
 fxch st1
 fmulp st5,st0
 fxch st5
 fsubp st1,st0
 fxch st3
 fsubrp st4,st0
 fxch st1
 fmulp st2,st0
 fmulp st2,st0
 fistp dword [r_zistepx]
 fistp dword [r_zistepy]
 mov eax, dword [r_sstepx]
 mov edx, dword [r_tstepx]
 shl eax,16
 shl edx,16
 mov dword [a_sstepxfrac],eax
 mov dword [a_tstepxfrac],edx
 mov ecx, dword [r_sstepx]
 mov eax, dword [r_tstepx]
 sar ecx,16
 sar eax,16
 imul dword [4+0+esp]
 add eax,ecx
 mov dword [a_ststepxwhole],eax
 ret
%endif

;;;;;;;;;;;;;;;;;;;;;;;;
; D_PolysetAff8Start
;;;;;;;;;;;;;;;;;;;;;;;;
 global D_PolysetAff8Start
D_PolysetAff8Start:

;;;;;;;;;;;;;;;;;;;;;;;;
; R_PolysetDrawSpans8_Opaque
;;;;;;;;;;;;;;;;;;;;;;;;

 global R_PolysetDrawSpans8_Opaque
R_PolysetDrawSpans8_Opaque:
 push esi
 push ebx
 mov esi, dword [4+8+esp]
 mov ecx, dword [r_zistepx]
 push ebp
 push edi
 ror ecx,16
 mov edx, dword [8+esi]
 mov dword [lzistepx],ecx
LSpanLoop:
 mov eax, dword [d_aspancount]
 sub eax,edx
 mov edx, dword [erroradjustup]
 mov ebx, dword [errorterm]
 add ebx,edx
 js LNoTurnover
 mov edx, dword [erroradjustdown]
 mov edi, dword [d_countextrastep]
 sub ebx,edx
 mov ebp, dword [d_aspancount]
 mov dword [errorterm],ebx
 add ebp,edi
 mov dword [d_aspancount],ebp
 jmp LRightEdgeStepped
LNoTurnover:
 mov edi, dword [d_aspancount]
 mov edx, dword [ubasestep]
 mov dword [errorterm],ebx
 add edi,edx
 mov dword [d_aspancount],edi
LRightEdgeStepped:
 cmp eax,1
 jl near LNextSpan
 jz near LExactlyOneLong
 mov ecx, dword [a_ststepxwhole]
 mov edx, dword [r_affinetridesc+8]
 mov dword [advancetable+4],ecx
 add ecx,edx
 mov dword [advancetable],ecx
 mov ecx, dword [a_tstepxfrac]
 mov cx, word [r_lstepx]
 mov edx,eax
 mov dword [tstep],ecx
 add edx,7
 shr edx,3
 mov ebx, dword [16+esi]
 mov bx,dx
 mov ecx, dword [4+esi]
 neg eax
 mov edi, dword [0+esi]
 and eax,7
 sub edi,eax
 sub ecx,eax
 sub ecx,eax
 mov edx, dword [20+esi]
 mov dx, word [24+esi]
 mov ebp, dword [28+esi]
 ror ebp,16
 push esi
 mov esi, dword [12+esi]
 jmp dword[aff8entryvec_table+eax*4]
LDrawLoop:
LDraw8:
 cmp bp, word [ecx]
 jl Lp1
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [ecx],bp
 mov al, byte [12345678h+eax]
LPatch8:
 mov byte [edi],al
Lp1:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw7:
 cmp bp, word [2+ecx]
 jl Lp2
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [2+ecx],bp
 mov al, byte [12345678h+eax]
LPatch7:
 mov byte [1+edi],al
Lp2:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw6:
 cmp bp, word [4+ecx]
 jl Lp3
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [4+ecx],bp
 mov al, byte [12345678h+eax]
LPatch6:
 mov byte [2+edi],al
Lp3:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw5:
 cmp bp, word [6+ecx]
 jl Lp4
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [6+ecx],bp
 mov al, byte [12345678h+eax]
LPatch5:
 mov byte [3+edi],al
Lp4:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw4:
 cmp bp, word [8+ecx]
 jl Lp5
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [8+ecx],bp
 mov al, byte [12345678h+eax]
LPatch4:
 mov byte [4+edi],al
Lp5:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw3:
 cmp bp, word [10+ecx]
 jl Lp6
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [10+ecx],bp
 mov al, byte [12345678h+eax]
LPatch3:
 mov byte [5+edi],al
Lp6:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw2:
 cmp bp, word [12+ecx]
 jl Lp7
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [12+ecx],bp
 mov al, byte [12345678h+eax]
LPatch2:
 mov byte [6+edi],al
Lp7:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
LDraw1:
 cmp bp, word [14+ecx]
 jl Lp8
 xor eax,eax
 mov ah,dh
 mov al, byte [esi]
 mov word [14+ecx],bp
 mov al, byte [12345678h+eax]
LPatch1:
 mov byte [7+edi],al
Lp8:
 add edx, dword [tstep]
 sbb eax,eax
 add ebp, dword [lzistepx]
 adc ebp,0
 add ebx, dword [a_sstepxfrac]
 adc esi, dword [advancetable+4+eax*4]
 add edi,8
 add ecx,16
 dec bx
 jnz near LDrawLoop
 pop esi
LNextSpan:
 add esi,32
LNextSpanESISet:
 mov edx, dword [8+esi]
 cmp edx,offset -999999
 jnz near LSpanLoop
 pop edi
 pop ebp
 pop ebx
 pop esi
 ret
LExactlyOneLong:
 mov ecx, dword [4+esi]
 mov ebp, dword [28+esi]
 ror ebp,16
 mov ebx, dword [12+esi]
 cmp bp, word [ecx]
 jl LNextSpan
 xor eax,eax
 mov edi, dword [0+esi]
 mov ah, byte [24+1+esi]
 add esi,32
 mov al, byte [ebx]
 mov word [ecx],bp
 mov al, byte [12345678h+eax]
LPatch9:
 mov byte [edi],al
 jmp LNextSpanESISet


;;;;;;;;;;;;;;;;;;;;;;;;
; D_PolysetAff8End
;;;;;;;;;;;;;;;;;;;;;;;;
 global D_PolysetAff8End
D_PolysetAff8End:

;;;;;;;;;;;;;;;;;;;;;;;;
; D_Aff8Patch
;;;;;;;;;;;;;;;;;;;;;;;;
 global D_Aff8Patch
D_Aff8Patch:
 mov eax,[alias_colormap]
 mov dword [LPatch1-4],eax
 mov dword [LPatch2-4],eax
 mov dword [LPatch3-4],eax
 mov dword [LPatch4-4],eax
 mov dword [LPatch5-4],eax
 mov dword [LPatch6-4],eax
 mov dword [LPatch7-4],eax
 mov dword [LPatch8-4],eax
 mov dword [LPatch9-4],eax
 ret


;;;;;;;;;;;;;;;;;;;;;;;;
; R_PolysetScanLeftEdge
;;;;;;;;;;;;;;;;;;;;;;;;

 global R_PolysetScanLeftEdge
R_PolysetScanLeftEdge:
 push ebp
 push esi
 push edi
 push ebx
 mov eax, dword [4+16+esp]
 mov ecx, dword [d_sfrac]
 and eax,0FFFFh
 mov ebx, dword [d_ptex]
 or ecx,eax
 mov esi, dword [d_pedgespanpackage]
 mov edx, dword [d_tfrac]
 mov edi, dword [d_light]
 mov ebp, dword [d_zi]
LScanLoop:
 mov dword [12+esi],ebx
 mov eax, dword [d_pdest]
 mov dword [0+esi],eax
 mov eax, dword [d_pz]
 mov dword [4+esi],eax
 mov eax, dword [d_aspancount]
 mov dword [8+esi],eax
 mov dword [24+esi],edi
 mov dword [28+esi],ebp
 mov dword [16+esi],ecx
 mov dword [20+esi],edx
 mov al, byte [32+esi]
 add esi,32
 mov eax, dword [erroradjustup]
 mov dword [d_pedgespanpackage],esi
 mov esi, dword [errorterm]
 add esi,eax
 mov eax, dword [d_pdest]
 js near LNoLeftEdgeTurnover
 sub esi, dword [erroradjustdown]
 add eax, dword [d_pdestextrastep]
 mov dword [errorterm],esi
 mov dword [d_pdest],eax
 mov eax, dword [d_pz]
 mov esi, dword [d_aspancount]
 add eax, dword [d_pzextrastep]
 add ecx, dword [d_sfracextrastep]
 adc ebx, dword [d_ptexextrastep]
 add esi, dword [d_countextrastep]
 mov dword [d_pz],eax
 mov eax, dword [d_tfracextrastep]
 mov dword [d_aspancount],esi
 add edx,eax
 jnc LSkip1
 add ebx, dword [r_affinetridesc+8]
LSkip1:
 add edi, dword [d_lightextrastep]
 add ebp, dword [d_ziextrastep]
 mov esi, dword [d_pedgespanpackage]
 dec ecx
 test ecx,0FFFFh
 jnz near LScanLoop
 pop ebx
 pop edi
 pop esi
 pop ebp
 ret
LNoLeftEdgeTurnover:
 mov dword [errorterm],esi
 add eax, dword [d_pdestbasestep]
 mov dword [d_pdest],eax
 mov eax, dword [d_pz]
 mov esi, dword [d_aspancount]
 add eax, dword [d_pzbasestep]
 add ecx, dword [d_sfracbasestep]
 adc ebx, dword [d_ptexbasestep]
 add esi, dword [ubasestep]
 mov dword [d_pz],eax
 mov dword [d_aspancount],esi
 mov esi, dword [d_tfracbasestep]
 add edx,esi
 jnc LSkip2
 add ebx, dword [r_affinetridesc+8]
LSkip2:
 add edi, dword [d_lightbasestep]
 add ebp, dword [d_zibasestep]
 mov esi, dword [d_pedgespanpackage]
 dec ecx
 test ecx,0FFFFh
 jnz near LScanLoop
 pop ebx
 pop edi
 pop esi
 pop ebp
 ret

