jeudi 29 juin 2017

Soft shading using SSE2 SIMD technology purely in assembly language !!

 note: my 3D rendering software engine  in real time (Fast3D/2D) powered with SSE4



 // function returns the mixing of 2 3d vectors = (1-u)*a+u*b
 // function returns the mixing of 3 3d vector = (1-u-v)*a+u*b+v*c
// this application is useful for the smooth shading of the triangle

inline vec mix(float u,vec a,vec b) {
__asm {
            mov        eax, 03F800000h // 1.0f
            movd      xmm0,eax
            movss     xmm1,u
            subss      xmm0,xmm1 /// 1-u
            shufps     xmm0,xmm0,_MM_SHUFFLE(3, 0, 0, 0) // xmm0 =0,1-u,1-u,1-u
            shufps     xmm1,xmm1,_MM_SHUFFLE(3, 0, 0, 0) // xmm1 =0,u,u,u
            movaps   xmm2,a
            movaps   xmm3,b
            mulps      xmm0,xmm2 /// xmm0 = (1-u)*a
            mulps      xmm1,xmm3 /// xmm1 = u*b
            addps      xmm0,xmm1 // (1-u)*a+u*b
       }
}


// supports only SSE4.2
inline vec mix2(float u,float v,vec a,vec b,vec c)   {
......
}

mercredi 7 juin 2017

filling a triangle with shader color interpolation very useful for Zbuffer algorithm

Type pixel
x as integer
y as integer
r as integer
g as integer
b as integer
end type

ScreenRes 640, 480, 16,2,0
'screenset 1,0
FUNCTION max(x as integer,y as integer) as integer
 if (x>y) then return x else return y
end function    

FUNCTION min(x as integer,y as integer) as integer
 if (x>y) then return y else return x
end function    

sub triangle(a as pixel, b as pixel, c as pixel)
 dim as single xmin,ymin,xmax,ymax,x,y
 dim as single u,v,det
 dim as pixel am,ab,ac,m
 dim as integer red,green,blue

 xmin = min(a.x,min(b.x,c.x)):xmax = max(a.x,max(b.x,c.x))
 ymin = min(a.y,min(b.y,c.y)):ymax = max(a.y,max(b.y,c.y))

  ab.x = b.x-a.x: ab.y = b.y-a.y
  ac.x = c.x-a.x: ac.y = c.y-a.y
  
 for y = ymin to ymax
     for x = xmin to xmax
         m.x = x:m.y= y 
         am.x = x-a.x: am.y = y-a.y
         det  = ab.x*ac.y-ac.x*ab.y
         u  = (am.x*ac.y-ac.x*am.y)/det: v  = -(am.x*ab.y-ab.x*am.y)/det
         
         red  = u*b.r+v*c.r+(1-u-v)*a.r:
         green  = u*b.g+v*c.g+(1-u-v)*a.g
         blue  = u*b.b+v*c.b+(1-u-v)*a.b
         
         if ( (u>=0) and ( v>=0) and ( u+v<=1))  then pset (x,y),RGB(red,green,blue) 
           
       next x
   next y

end sub

dim as single i
dim as pixel a,b,c


for i = 0 to  2000 
a.x = 640*rnd: a.y = 480*rnd
b.x = 640*rnd:b.y = 480*rnd
c.x = 640*rnd:c.y = 480*rnd

 a.r = 255*rnd: a.g = 255*rnd: a.b = 255*rnd
 b.r = 255*rnd: b.g = 255*rnd: b.b = 255*rnd
 c.r = 255*rnd: c.g = 255*rnd: c.b = 255*rnd
triangle(a,b,c)

next i



sleep