Ayala 发表于 2021-2-23 17:49:10

重造轮子,把a5的NASM汇编光追第二弹改写成masm版本的

本帖最后由 Ayala 于 2021-2-26 23:41 编辑

原帖 https://www.0xaa55.com/thread-26336-1-1.html


;******************************************************************************
; DOS Ray-tracing rendering Demo
; by 0xAA55
; 2021-2-17
;
; This program is a DOS `COM` program which is able to be run in native DOS
; system in Real mode x86 CPU
;******************************************************************************



.model tiny
.286
;option prologue:none
;option epilogue:none
option casemap:none
option prologue:prologuedef
option epilogue:epiloguedef

;DEBUGMODE EQU 1

Interlaced equ 1
LightPow   equ 20
NumSpheres equ 3

WWWWW      equ 120
MAXWW      equ 320

HHHHH      equ 80
MAXHH      equ 200

OWAOH      = MAXWW * HHHHH + WWWWW

include RAYTRC.inc


;std
;.data
;#

;shellcode
.code
_entry:
        call Start
;#       
       
       
_this   label tClass

;tConst struct
        gF65536    dd 65536.0
        gF30625    dd 3.0625
        gFM175   dd -1.75
        gFTimeFreq dd 1193186.0
        gFBigValue dd 9999999.0
        gFZero   dd 0.0
        gF05       dd 0.5
        gFM1       dd -1.0
        gC2      dw 2
        gC255      dw 255
        gC256      dw 256
        gC1000   dw 1000
;tConst ends
       
       
;tData struct
        gDistEpsilon    dd 0.01
        gDistEpsilon2   dd 0.02
        gFogDistance    dd 100.0
        gSavedVideoMode dw 0
        gTempW          dw 0
        gSizeOf_Spheredw sizeof tSphere
        gRes            tPos <WWWWW,HHHHH>
;tData ends


DitherMatrix label byte
        dm_y = 0
        REPEAT 16
                dm_x= 0
                REPEAT 16
                                xor_val = dm_x XOR dm_y
                                dm_val =         (((xor_val AND 01h) SHR 0) SHL 7) OR (((dm_y AND 01) SHR 0) SHL 6)
                                dm_val = dm_val OR (((xor_val AND 02h) SHR 1) SHL 5) OR (((dm_y AND 02) SHR 1) SHL 4)
                                dm_val = dm_val OR (((xor_val AND 04h) SHR 2) SHL 3) OR (((dm_y AND 04) SHR 2) SHL 2)
                                dm_val = dm_val OR (((xor_val AND 08h) SHR 3) SHR 1) OR (((dm_y AND 08) SHR 3) SHR 0)
                                db dm_val
                                dm_x = dm_x + 1
                ENDM
                dm_y = dm_y + 1
        ENDM


        SampleDepth   EQU 12
        MapCast_Iterate EQU 12

;tMapCastDist struct       
        gLightRay        tLightRay<<0,0,0>,<<0,0,0>,<1.0,-1.0, 1.0>>,0>
        gMapCast    tMapCast<<0,0,0>,<0,0,0>,0,0>
        gMapDist    tMapDist<<0,0,0>,0,0>
;tMapCastDist ends

        gMask       tColor    <0, 0, 0>

        gColorMix    tColorMix <<0.2, 0.5, 0.8>,\
                      <1.0, 0.8, 0.6>,\
                      <0.8, 0.9, 1.0>,\
                      <0, 0, 0>\
                      >
        gCamPos   tPosition <0.0, 2.0, 7.0>

;tBB struct
        gBB         tPosition < 2.5, 4.5, 3.5>
        gBBneg      tPosition <-4.5,-0.5,-0.5>
;tBB ends

        gPixel      tPixel    <<0, 0>,<0, 0, 0>>

        gSpheres    tSphere << 0.0, 2.0, 0.0>, 2.0, <0.7, 0.9, 0.1>>, \
                             << 1.0, 1.0, 2.0>, 1.0, <0.1, 0.7, 0.9>>,\
                         <<-3.0, 1.0, 0.0>, 1.0, <0.9, 0.1, 0.7>>
                  
        gReg       dw 8 dup(0)
        gCheck   dw 0aa55h   ;data end for check
       
IF ($ - offset _this) NE (sizeof tClass)
%   ECHO STRUCTCHECK
%   ECHO@CatStr(%(sizeof tClass) )
        .ERR
ENDIF
               db '$',0
       
;std
;.code
;org 100h
;#
Start:
;shellcode
        pop bx ;shellcode
       
;std
    ;lea bx,offset _this
        assume bx:ptr tClass
       
        cmp .Check, 0AA55h
        mov al,'1'
        jnz @F
       
        call SetupVideoMode
       
        cmp .Check, 0AA55h
        mov al,'2'
        jnz @F
       
        call RenderScreen
       
        cmp .Check, 0AA55h
        mov al,'3'
        jnz @F
       
        call RestoreVideoMode
        mov al,'4'

@@: call @F
        byte ' potato','$',13,10
@@:        pop bx
        mov ,al
        mov dx,bx
        mov ah,9
        int 21h       
       
        ;Exit program
        int 20h
        ret
       
;******************************************************************************
; Setup our specific palette to use our color-system.
; We use RGB:233 color format to encode the color index, and it's easier to
; implement the ordered-dithering algorithm to gain a better appearance.
;******************************************************************************
SetupPalette proc
assume si:error
assume di:error       
        ;Set up palette
        mov bl, 0
        mov cx, 256
       
LoopSetPalette:

                push cx
                mov dx, 03C8h
                mov al, bl ;The current color index
                out dx, al

                inc dl ;0x03C9 port

                ;Red: 2 bits
                and al, 03h
                mov bh, 55h
                mul bh
                out dx, al

                ;Green: 3 bits
                mov al, bl
                mov cl, 2
                shr al, cl
                and al, 07h
                mov bh, 49h
                mul bh
                out dx, al

                ;Blue: 3 bits
                mov al, bl
                mov cl, 5
                shr al, cl
                and al, 07h
                mul bh
                out dx, al

                ;Increase the color index
                inc bl
                pop cx
        loop LoopSetPalette
assume si:nothing
assume di:nothing
        ret
SetupPalette endp


;******************************************************************************
; Setup video mode to 0x13: Graphic mode, 320x200 resolution, 256 colors.
; The VRAM buffer is 0xA000:0x0000
; Each byte of the buffer represents the color index of a palette.
; The previous video mode will be saved.
;******************************************************************************
SetupVideoMode proc
assume si:error
assume di:error
        ;Get current display mode
       
        ;push bx
        mov .R.bp_,bp
        mov bp,bx
       
        mov ah, 0fh
        int 10h;The bx data is corrupted
       
        mov bx,bp
        mov byte ptr .Data_.SavedVideoMode, al

        ;Set display mode to 320x200 graphic 256-color mode
        mov ax, 13h
        int 10h   ;The bx data is corrupted
       
        mov bx,bp
        call SetupPalette ;The bx data is corrupted
       
        ;pop bx
        mov bx,bp
        mov bp,.R.bp_
       
assume si:nothing
assume di:nothing
        ret
SetupVideoMode endp

;******************************************************************************
; Restore the video mode to the saved video mode.
; The saved video mode was saved by calling `SetupVideoMode`.
;******************************************************************************

RestoreVideoMode proc
assume si:error
assume di:error

        mov .R.bp_,bp
        mov bp,bx
        ;Restore video mode
        mov ax, .Data_.SavedVideoMode
        int 10h
       
        mov bx,bp
        mov bp,.R.bp_
       
assume si:nothing
assume di:nothing
        ret
RestoreVideoMode endp
;******************************************************************************
; Pickup the best color for pixel position of `Pixel_X` and `Pixel_Y` with
; the color value of `Pixel_R` `Pixel_G` `Pixel_B`.
; Returns the best color index through register `AL`.
;******************************************************************************

PickColor proc uses si

assume di:error
        mov al, byte ptr .P.Pos.x ;
        and al, 0fh
        mov ah, byte ptr .P.Pos.y
        and ah, 0fh
        mov cl, 4
        shl ah, cl
        or al, ah
        mov ah, 0
               
        mov si,ax
        mov al,.DM
;        push bx
;        lea bx,.DM
;        xlatb
;        pop bx
       
        mov dl, al
        mov cl, 2
        shr dl, cl
        inc cl
        shr al, cl
       
               
        add byte ptr .P.Col.R, dl
        jnc @F
        mov byte ptr .P.Col.R, 0FFh
@@:; .RedReady:
        add byte ptr .P.Col.G, al
        jnc @F
        mov byte ptr .P.Col.G, 0FFh
@@: ;.GreenReady:
        add byte ptr .P.Col.B, al
        jnc @F
        mov byte ptr .P.Col.B, 0FFh
@@: ;.BlueReady:

        and byte ptr .P.Col.R, 0C0h
        and byte ptr .P.Col.G, 0E0h
        and byte ptr .P.Col.B, 0E0h
        mov al, byte ptr .P.Col.R
        shr al, cl ;cl = 3
        or al, byte ptr .P.Col.G
        shr al, cl
        or al, byte ptr .P.Col.B
       
assume di:nothing
        ret
PickColor endp

;******************************************************************************
; Render the scene
;******************************************************************************
RenderScreen proc uses di si

        LOCAL Local_Cam_U:DWORD
        LOCAL Local_Cam_V:DWORD
        LOCAL Local_Cam_W:DWORD
        LOCAL Local_Cam_D:DWORD
       
       
        mov ax, 0a000h
        mov es, ax
       
       
        fld .Val_.FM175;
        fstp

        ;Do some initialize
        ;L = sqrt(x^2 + y^2 + z^2)
        fld.M.Ray.dir.Light.x
        fmul st(0),st(0)
        fld.M.Ray.dir.Light.y
        fmul st(0),st(0)
        fadd
        fld.M.Ray.dir.Light.z
        fmul st(0),st(0)
        fadd
        fsqrt
       
        ;x = x / L
        fld st(0)
        fdivr .M.Ray.dir.Light.x
        fstp.M.Ray.dir.Light.x
       
        ;y = y / L
        fld st(0)
        fdivr .M.Ray.dir.Light.y
        fstp.M.Ray.dir.Light.y
       
        ;z = z / L
        fdivr .M.Ray.dir.Light.z
        fstp.M.Ray.dir.Light.z

        ;di: Used to write display buffer
        mov di,OWAOH
        ;Loop for scan lines
       
        mov.P.Pos.y, 0
@@:
        .repeat
                ;Calculate Ray-V
                mov ax, .P.Pos.y
                add ax, ax
                sub ax, .Data_.Res.y
                not ax
                inc ax
               
                mov word ptr , ax
                fild word ptr
                fidiv .Data_.Res.y
                fstp

                ;Loop for pixels per scan line
                mov .P.Pos.x,0
               
                .repeat
                        ;Calculate Ray-U
                        mov ax, .P.Pos.x
                        add ax, ax
                        sub ax, .Data_.Res.x
                       
                        mov word ptr , ax
                        fild word ptr
                        fidiv word ptr .Data_.Res.y
                        fst

                        ;Normalize Ray
                        fmul st(0),st(0)
                        fld
                        fmul st(0),st(0)
                        fadd
                        fadd .Val_.F30625;
                        fsqrt
                        ;D = sqrt(U^2 + V^2 + W^2)
                       
                        ;x = U / D
                        fst
                        fdivr
                        fstp.M.Ray.dir.Ray.x
                       
                        ;y = V / D
                        fld
                        fdiv
                        fstp.M.Ray.dir.Ray.y
                       
                        ;z = W / D
                        fld
                        fdiv
                        fstp.M.Ray.dir.Ray.z


                        ; Render the current pixel color
                        call RenderScene
                       
                        ;push si
                        ;mov si,.P.Pos.x
                        ;and si,0ffh
                        ;mov al,.DM
                        ;pop si
                       
                        stosb
                       
       
                        inc.P.Pos.x
                .until .P.Pos.x >= WWWWW

                lea di,
IFDEF Interlaced

                lea di,
                inc .P.Pos.y
ENDIF
                inc .P.Pos.y
        .until .P.Pos.y>= HHHHH
       
IFDEF Interlaced
        and .P.Pos.y,1
        mov .P.Pos.y,1
        mov di,OWAOH + MAXWW
        jz        @B
ENDIF       
       
       
        ; Wait for any keystroke to exit
        mov ah, 07h
        int 21h

        ret
RenderScreen endp


;******************************************************************************
; Pick up a color that represents the sky from the `RayDir` vector.
;******************************************************************************
GetSkyColor proc
        ; SunLum = Dot(RayDir, -LightDir)
assume si:error
assume di:error
        fld.M.Ray.dir.Ray.x
        fmul .M.Ray.dir.Light.x
        fld.M.Ray.dir.Ray.y
        fmul .M.Ray.dir.Light.y
        fadd
        fld.M.Ray.dir.Ray.z
        fmul .M.Ray.dir.Light.z
        fadd
        fchs

        ; SunLum = Max(SunLum, 0)
        fld st(0)
        fabs
        fadd
        fidiv .Val_.C2 ;
       

        ; SunLum = Pow(SunLum, LightPow)
        IF LightPow GT 0
                fld st(0)
               
                REPEAT LightPow - 1
               
                fmul st(0),st(1)
                ENDM
                fmul
        ENDIF

        ; FogDensity = 1 - abs(RayDir_y)
        fld1
        fld.M.Ray.dir.Ray.y
        fabs
        fsub

        ; Mix(SkyColor, FogColor, FogDensity)
       
        fld st(0)
        fmul.Mix.FogColor.R
        fstp.Mix.GetSkyColor.R
        fld st(0)
        fmul.Mix.FogColor.G
        fstp.Mix.GetSkyColor.G
        fld st(0)
        fmul.Mix.FogColor.B
        fstp.Mix.GetSkyColor.B

        ; 1 - FogDensity
        fld1
        fsubr
        fld st(0)
        fmul.Mix.SkyColor.R
        fadd.Mix.GetSkyColor.R
        fstp.Mix.GetSkyColor.R
        fld st(0)
        fmul.Mix.SkyColor.G
        fadd.Mix.GetSkyColor.G
        fstp.Mix.GetSkyColor.G

        fmul.Mix.SkyColor.B
        fadd.Mix.GetSkyColor.B
        fstp.Mix.GetSkyColor.B

        ; LightColor
        fld st(0)
        fmul.Mix.LightColor.R
        fadd.Mix.GetSkyColor.R
        fstp.Mix.GetSkyColor.R
        fld st(0)
        fmul.Mix.LightColor.G
        fadd.Mix.GetSkyColor.G
        fstp.Mix.GetSkyColor.G

        fmul.Mix.LightColor.B
        fadd.Mix.GetSkyColor.B
        fstp.Mix.GetSkyColor.B

assume si:nothing
assume di:nothing
        ret
GetSkyColor endp
;******************************************************************************
; Pick up a color that represents the sky from the `RayDir` vector.
;******************************************************************************
MapDistProc proc uses si di


        fld.M.MapDist.O.y
        fstp .M.MapDist.D
        mov.M.MapDist.I,-1
       
        mov cx, NumSpheres
        mov si, 0
        mov di, 0

               
@@:

        fld.S.Position.x
        fsub .M.MapDist.O.x
        fmul st(0),st(0)
       
        fld.S.Position.y
        fsub .M.MapDist.O.y
        fmul st(0),st(0)
        fadd
        fld.S.Position.z
        fsub .M.MapDist.O.z
        fmul st(0),st(0)
        fadd
        fsqrt
        fsub .S.Radius
        fcom .M.MapDist.D
        fstsw ax
        sahf
.if CARRY?
        fst.M.MapDist.D
        mov.M.MapDist.I, di
.endif
        fstp st(0)
        inc di
        add si,.Data_.SizeOf_Sphere
        loop @B

        ret
MapDistProc endp

;******************************************************************************
; Calculate a ray from origin `RayOrg` and towards the direction `RayDir` that
; casts to the scene. Returns the cast point coordinates `MapCast` and the
; distance to the origin of the ray and the surface normal from the scene.
;******************************************************************************
MapCastProc proc uses si

assume di:error
        fldz
        fst.M.MapCast.D
        fst.M.MapCast.N.x
        fst.M.MapCast.N.y
        fstp .M.MapCast.N.z


; Stepping the point to go forward
        mov cx, MapCast_Iterate
LoopIterate:

        fld .M.Ray.O.x
        fld .M.Ray.dir.Ray.x
        fmul .M.MapCast.D
        fadd
        fstp .M.MapDist.O.x
       

        fld .M.Ray.O.y
        fld .M.Ray.dir.Ray.y
        fmul .M.MapCast.D
        fadd
        fstp .M.MapDist.O.y
       
       
        fld .M.Ray.O.z
        fld .M.Ray.dir.Ray.z
        fmul .M.MapCast.D
        fadd
        fstp .M.MapDist.O.z

        call IsAwayFromBB
        jnc InsideBB

        fld .M.Ray.dir.Ray.y
        fldz
        fcompp
        fstsw ax
        sahf
        jbe ToSky
       
        ; Hit the ground outside the bounding box
        fld1
        fstp .M.MapCast.N.y

        fld .M.Ray.O.y
        fldz
        fsub .M.Ray.dir.Ray.y
        fdiv
        fstp .M.MapCast.D

        call SetCastCrd

        mov .M.MapCast.I, -1
        stc
        jmp done
ToSky:
; The origin of the ray is from outside of the bounding box and it's going to the sky

        fld .M.MapDist.O.x
        fstp .M.MapCast.O.x
        fld .M.MapDist.O.y
        fstp .M.MapCast.O.y
        fld .M.MapDist.O.z
        fstp .M.MapCast.O.z
        mov .M.MapCast.I, -2
        clc
        jmp done

; The origin of the ray is inside the bounding box
InsideBB:
        ;push cx
        mov si,cx
        call MapDistProc
        ;pop cx
        mov cx,si

        fld .M.MapDist.D
        fcomp .Data_.DistEpsilon;
        fstsw ax
        sahf
        ja NotNearEnough

        mov ax, .M.MapDist.I
        mov .M.MapCast.I, ax
        cmp ax, 0
        jge NotHitGround

        ; Hit the ground inside the bounding box
        fld1
        fstp .M.MapCast.N.y

        fld .M.MapDist.O.x
        fstp .M.MapCast.O.x
        fld .M.MapDist.O.y
        fstp .M.MapCast.O.y
        fld .M.MapDist.O.z
        fstp .M.MapCast.O.z
        stc
        jmp done

; Should iterate again
NotNearEnough:
        fld .M.MapCast.D
        fadd .M.MapDist.D
        fstp .M.MapCast.D
        dec cx
        jcxz ExitIterate
        jmp LoopIterate

; Hit the spheres
NotHitGround:

        mul .Data_.SizeOf_Sphere;
        mov si,ax
       
        ; Calculate the normal
        fld .M.MapDist.O.x
        fsub .S.Position.x
        fst .M.MapCast.N.x
        fmul st(0),st(0)
        fld .M.MapDist.O.y
        fsub .S.Position.y
        fst .M.MapCast.N.y
        fmul st(0),st(0)
        fadd
        fld .M.MapDist.O.z
        fsub .S.Position.z
        fst .M.MapCast.N.z
        fmul st(0),st(0)
        fadd
        fsqrt ; Normalize the normal
        fld st(0)
        fld .M.MapCast.N.x
        fdivr
        fstp .M.MapCast.N.x
        fld st(0)
        fld .M.MapCast.N.y
        fdivr
        fstp .M.MapCast.N.y
        fdivr .M.MapCast.N.z
        fstp .M.MapCast.N.z

        ; Set cast coord
        fld .M.MapDist.O.x
        fstp .M.MapCast.O.x
        fld .M.MapDist.O.y
        fstp .M.MapCast.O.y
        fld .M.MapDist.O.z
        fstp .M.MapCast.O.z
        stc
        jmp done

; Finished iteration
ExitIterate:
        fld .M.MapDist.O.x
        fstp .M.MapCast.O.x
        fld .M.MapDist.O.y
        fstp .M.MapCast.O.y
        fld .M.MapDist.O.z
        fstp .M.MapCast.O.z
        mov .M.MapCast.I, -2
        clc
done:
assume di:nothing
        ret
MapCastProc endp

; Subroutine: MapCast = RayOrg + RayDir * Dist
SetCastCrd proc
assume si:error
assume di:error

        fld .M.Ray.O.x
        fld .M.Ray.dir.Ray.x
        fmul .M.MapCast.D
        fadd
        fstp .M.MapCast.O.x

        fld .M.Ray.O.y
        fld .M.Ray.dir.Ray.y
        fmul .M.MapCast.D
        fadd
        fstp .M.MapCast.O.y

        fld .M.Ray.O.z
        fld .M.Ray.dir.Ray.z
        fmul .M.MapCast.D
        fadd
        fstp .M.MapCast.O.z

        ret
       
assume si:nothing
assume di:nothing

SetCastCrd endp       

;******************************************************************************
; Check if the ray from the origin `RayOrg` towards the direction `RayDir` is
; going away from the bounding box
;******************************************************************************
IsAwayFromBB proc
assume si:error
assume di:error
       
        fld .M.Ray.O.x
        fcomp .BB.N.x;
        fstsw ax
        sahf
       
jae XNIsOK
        fld .M.Ray.dir.Ray.x
        fcomp .Val_.FZero;
        fstsw ax
        sahf
       
jae XNIsOK
        stc
        jmp done
       
XNIsOK:

        fld .M.Ray.O.x
        fcomp .BB.O.x;
        fstsw ax
        sahf
       
jbe XPIsOK
        fld .M.Ray.dir.Ray.x
        fcomp .Val_.FZero;
        fstsw ax
        sahf
       
jbe XPIsOK
        stc
        jmp done
       
XPIsOK:
       
        fld .M.Ray.O.y
        fcomp.BB.N.y;
        fstsw ax
        sahf
       
jae YNIsOK
        fld .M.Ray.dir.Ray.y
        fcomp .Val_.FZero;
        fstsw ax
        sahf
jae YNIsOK
        stc
        jmp done
YNIsOK:

        fld .M.Ray.O.y
        fcomp .BB.O.y;
        fstsw ax
        sahf
       
jbe YPIsOK

        fld .M.Ray.dir.Ray.y
        fcomp .Val_.FZero;
        fstsw ax
        sahf
jbe YPIsOK
        stc
        jmp done
YPIsOK:

        fld .M.Ray.O.z
        fcomp .BB.N.z;
        fstsw ax
        sahf
       
jae ZNIsOK
        fld .M.Ray.dir.Ray.z
        fcomp .Val_.FZero;
        fstsw ax
        sahf
       
jae ZNIsOK
        stc
        jmp done
       
ZNIsOK:

        fld .M.Ray.O.z
        fcomp .BB.O.z;
        fstsw ax
        sahf
jbe ZPIsOK

        fld .M.Ray.dir.Ray.z
        fcomp .Val_.FZero;
        fstsw ax
        sahf
jbe ZPIsOK
        stc
        jmp done
       
ZPIsOK:

        clc
done:

        ret
assume si:nothing
assume di:nothing
IsAwayFromBB endp
       
;******************************************************************************
; Render the pixel by a given ray from the origin `RayOrg` towards the
; direction `RayDir`
;******************************************************************************
RenderScene proc uses si
assume di:error
        fld1
        fst.Mask_.R
        fst.Mask_.G
        fstp .Mask_.B


        fld.Cam.x
        fstp .M.Ray.O.x
        fld.Cam.y
        fstp.M.Ray.O.y
        fld.Cam.z
        fstp.M.Ray.O.z
       

       
        mov cx, SampleDepth
LoopSampling:
        ;push cx
        mov si,cx
        call MapCastProc
        ;pop cx
        mov cx,si
       
;.if !CARRY?       
        jc CastSphereOrGround

; If not cast then still step forward
NotCast:
        fld .M.MapCast.O.x
        fstp .M.Ray.O.x
        fld .M.MapCast.O.y
        fstp .M.Ray.O.y
        fld .M.MapCast.O.z
        fstp .M.Ray.O.z

        dec cx
        jcxz Finished1
        jmp LoopSampling

Finished1:
        jmp Finished

; If cast to the spheres or the ground, do coloring
CastSphereOrGround:
        mov ax, .M.MapCast.I
        cmp ax, 0
        jl CastGround

        ; Casting spheres
        mul .Data_.SizeOf_Sphere
        mov si,ax
       
        fld .Mask_.R
        fmul .S.Color.R
        fstp .Mask_.R
        fld .Mask_.G
        fmul .S.Color.G
        fstp .Mask_.G
        fld .Mask_.B
        fmul .S.Color.B
        fstp .Mask_.B

        ; Reflection
        fld .M.MapCast.N.x
        fmul .M.Ray.dir.Ray.x
        fld .M.MapCast.N.y
        fmul .M.Ray.dir.Ray.y
        fadd
        fld .M.MapCast.N.z
        fmul .M.Ray.dir.Ray.z
        fadd
        fadd st(0),st(0) ; (Normal dot Ray) * 2
       
        fld st(0)
        fmul .M.MapCast.N.x
        fsubr .M.Ray.dir.Ray.x
        fstp .M.Ray.dir.Ray.x
       
        fld st(0)
        fmul .M.MapCast.N.y
        fsubr .M.Ray.dir.Ray.y
        fstp .M.Ray.dir.Ray.y

        fmul .M.MapCast.N.z
        fsubr .M.Ray.dir.Ray.z
        fstp .M.Ray.dir.Ray.z

        call SetRayOrg

        dec cx
        jcxz Finished
        jmp LoopSampling
;.endif

; The ray is casting the ground
CastGround:
        fld .M.MapCast.O.x
        fadd st(0),st(0)
        fistp .Data_.TempW;
        fwait
        mov ax, .Data_.TempW;
        fld.M.MapCast.O.z
        fadd st(0),st(0)
        fistp .Data_.TempW;
        fwait
        xor ax,.Data_.TempW;
        test ax, 1
        jz GroundColorPattern

        fld.Mask_.R
        fmul .Val_.F05;
        fstp .Mask_.R
        fld.Mask_.G
        fmul .Val_.F05;
        fstp .Mask_.G
        fld.Mask_.B
        fmul .Val_.F05;
        fstp .Mask_.B

GroundColorPattern:
        fld .M.Ray.dir.Ray.y
        fabs
        fstp .M.Ray.dir.Ray.y

        call SetRayOrg

        dec cx
        jcxz Finished
        jmp LoopSampling

; Finished iteration
Finished:
        call GetSkyColor
        fld .Mix.GetSkyColor.R
        fmul .Mask_.R
        fimul .Val_.C255;
        fistp .P.Col.R
        fld .Mix.GetSkyColor.G
        fmul .Mask_.G
        fimul .Val_.C255;
        fistp .P.Col.G
        fld .Mix.GetSkyColor.B
        fmul.Mask_.B
        fimul .Val_.C255;
        fistp .P.Col.B
        fwait

; Clamp the color not to overflow
        cmp .P.Col.R, 255
        jle R_OK
        mov .P.Col.R, 255
R_OK:
        cmp .P.Col.G, 255
        jle G_OK
        mov .P.Col.G, 255
G_OK:
        cmp .P.Col.B, 255
        jle B_OK
        mov .P.Col.B, 255
B_OK:
       

        call PickColor
       
        ret
assume si:nothing
assume di:nothing
RenderScene endp

; When cast, set the ray origin near the cast point
SetRayOrg proc
assume si:error
assume di:error

        fld.M.MapCast.O.x
        fld.M.Ray.dir.Ray.x
        fmul .Data_.DistEpsilon2;
        fadd
        fstp.M.Ray.O.x
       
        fld.M.MapCast.O.y
        fld.M.Ray.dir.Ray.y
        fmul .Data_.DistEpsilon2;
        fadd
        fstp .M.Ray.O.y

        fld.M.MapCast.O.z
        fld.M.Ray.dir.Ray.z
        fmul .Data_.DistEpsilon2;
        fadd
        fstp .M.Ray.O.z

        ret
assume si:nothing
assume di:nothing
SetRayOrg endp

;std
;end start

;shellcode
end _entry








tPosition struc
        x dd ?
        y dd ?
        z dd ?
tPosition ends

tPos struc
        x dw ?
        y dw ?
tPos ends


tColor struc
        R dd ?
        G dd ?
        B dd ?
tColor ends
tCol struc
        R dw ?
        G dw ?
        B dw ?
tCol ends

tPixel struct
        Pos tPos <>
        Col tCol <>
tPixel ends

tSphere struct
        Position tPosition <>
        Radius   dd      ?
        Color    tColor    <>
tSphere ends

tColorMix struct
        SkyColor    tColor <>
        LightColortColor <>
        FogColor    tColor <>
        GetSkyColor tColor <>
tColorMix ends


tBB struct
        OtPosition <>
        NtPosition <>
tBB ends

tConst struct
        F65536    dd ?;65536.0
        F30625    dd ?;3.0625
        FM175   dd ?;-1.75
        FTimeFreq dd ?;1193186.0
        FBigValue dd ?;9999999.0
        FZero   dd ?;0
        F05       dd ?;0.5
        FM1       dd ?;-1.0
        C2      dw ?;2
        C255      dw ?;255
        C256      dw ?;256
        C1000   dw ?;1000
tConst ends

tData struct
        DistEpsilon    dd ?;0.01
        DistEpsilon2   dd ?;0.02
        FogDistance    dd ?;100.0
        SavedVideoMode dw ?;0
        TempW          dw ?;0
        SizeOf_Spheredw ?;sizeof tSphere
        Res            tPos <>;<320,200>
tData ends


tLightRay struc
        O    tPosition    <>
        struc dir
                Ray   tPosition <>
                Light tPosition <>
        ends
        D    dd ?
tLightRay ends

tMapCast struc
        O tPosition <>
    N tPosition <>
    D dd ?
    I dw ?
tMapCast ends

tMapDist struc
        O tPosition <>
        D dd ?
        I dw ?
tMapDist ends



tMapCastDist struct
        Ray   tLightRay <>
        MapCast tMapCast<>
        MapDist tMapDist<>
tMapCastDist ends



tREG struct
        ax_ dw ?
        cx_ dw ?
        dx_ dw ?
        bx_ dw ?
        sp_ dw ?
        bp_ dw ?
        si_ dw ?
        di_ dw ?
tREG ends


tClass struct
        Val_tConst      <>
        Data_ tData         <>
        DM    db16*16dup(?)
        M          tMapCastDist<>
        Mask_ tColor      <>
        Mix   tColorMix   <>
    Cam   tPosition   <>
    BB    tBB         <>
        P   tPixel      <>
        S   tSphere NumSpheres dup(<>)
       
        R   tREG          <>
        Check dw            <> ;0AA55h
tClass ends
STRUCTCHECK TEXTEQU <@CatStr(%($ - offset _this))>













https://github.com/AyalaRs/RAYTRC_

0xAA55 发表于 2021-2-23 21:27:35

噗 masm的“宏汇编”看起来比nasm的好看很多。

以及,既然都.686p了,左移右移寄存器不需要再用cl存储位数了吧

Ayala 发表于 2021-2-23 21:41:38

0xAA55 发表于 2021-2-23 21:27
噗 masm的“宏汇编”看起来比nasm的好看很多。

以及,既然都.686p了,左移右移寄存器不需要再用cl存储位数 ...

方便调试使用了686p, .286就能编译过,最大程度还原源代码
页: [1]
查看完整版本: 重造轮子,把a5的NASM汇编光追第二弹改写成masm版本的