www.pudn.com > fluid.rar > util.fxh, change:2006-10-27,size:4636b


 
//map position from relative projection coordinate space ( [-1,1] ) 
//to relate render target coordinate space 
//fDx : 1 / render target width 
//fDy : 1 / render target height  
inline float2 relProj2RelRenTar(float2 p_rp,float fDx,float fDy) 
{ 
 
  return float2(1.0+p_rp.x+fDx,  
                1.0-p_rp.y+fDy)*0.5; 
                 
} 
 
inline float2 relTex2RelRenTar(float2 p_rt,float fDx,float fDy) 
{ 
 
  return p_rt+float2(fDx,fDy)*0.5; 
                 
} 
 
 
//map position from relative projection coordinate space ( [-1,1] ) 
//to relate texture coordinate space  
inline float2 relProj2RelTex(float2 p_rp) 
{ 
   return float2( (1.0+p_rp.x) /2.0, 
                (1.0-p_rp.y) /2.0); 
} 
 
inline float2 relTex2RelProj(float2 p_rt,float fDx,float fDy) 
{ 
   return float2( 2.0*(p_rt.x-0.5), 
                  2.0*(0.5-p_rt.y)); 
} 
 
/*! \brief draw screen aligned quad,without texture coordinate shift. 
for float point texture,its texcoordinate is different from non-float point texture, 
they need not to be shifted by (dx/2,dy/2). 
    \param pos_rp 
    position in relate projection space 
    \param oPos_rp 
    position in relate projection space 
    \param oPos_rt 
    position in relate texture space 
*/ 
void screenAlignedQuadFVS( float2 pos_rp: POSITION, 
                  out float4  oPos_rp: POSITION, 
                  out float2  oPos_rt:TEXCOORD0 
                  ) 
{ 
   //snap vectors to four corner 
   pos_rp.xy = sign( pos_rp); 
   oPos_rp =float4(pos_rp,0,1); 
   oPos_rt = relProj2RelTex(pos_rp); 
}   
 
/*! \brief draw screen aligned quad,with texture coordinate shift for  
non-float point texture 
*/ 
void screenAlignedQuadVS( float2 pos_rp: POSITION, 
                  out float4  oPos_rp: POSITION, 
                  out float2  oPos_rt:TEXCOORD0, 
                  uniform float2 RTTexelSize 
                  ) 
{ 
   //snap vectors to four corner 
   pos_rp.xy = sign( pos_rp); 
   oPos_rp =float4(pos_rp,0,1); 
   oPos_rt = relProj2RelRenTar(pos_rp,RTTexelSize.x,RTTexelSize.y); 
}   
    
/*! \brief draw screen aligned boundary  
    \param pos_rp 
    position in relate projection space 
    \param oPos_rp 
    position in relate projection space 
    \remark 
    use VPOS*texelSize to get texture coordinat in pixel shader 
 
*/ 
void screenAlignedBoundaryVS( float2 pos_rp: POSITION, 
                      out float4 oPos_rp:POSITION, 
                  uniform float2 pixelSize //render target textel size 
                  ) 
{ 
   //snap vectors to four corner 
   pos_rp.xy = sign( pos_rp.xy); 
    
  
   if(pos_rp.y<0.0) 
   { 
      pos_rp.y += 2*pixelSize.y; 
   } 
   if(pos_rp.x>0.0) 
   { 
      pos_rp.x -= 2*pixelSize.x; 
   } 
 
    
   oPos_rp =float4(pos_rp,0,1); 
 
   
}  
 
//bilinear filter for float point texture 
float4 tex2D_bilinear( sampler2D sam, float2 texcoord,float2 texSize,float fDx,float fDy ) 
{ 
float2 f = frac( texcoord.xy * texSize ); 
float4 t00 = tex2D( sam, texcoord ); 
float4 t10 = tex2D( sam, texcoord + float2( fDx, 0.0f )); 
float4 tA = lerp( t00, t10, f.x ); 
float4 t01 = tex2D( sam, texcoord + float2( 0.0f, fDy ) ); 
float4 t11 = tex2D( sam, texcoord + float2( fDx, fDy ) ); 
float4 tB = lerp( t01, t11, f.x ); 
return lerp( tA, tB, f.y ); 
} 
 
//bilinear filter for float point texture 
float4 tex2Dlod_bilinear( sampler2D sam, float4 texcoord,float2 texSize,float fDx,float fDy ) 
{ 
float2 f = frac( texcoord.xy * texSize ); 
float4 t00 = tex2Dlod( sam, texcoord ); 
float4 t10 = tex2Dlod( sam, texcoord + float4( fDx, 0.0f ,0,0)); 
float4 tA = lerp( t00, t10, f.x ); 
float4 t01 = tex2Dlod( sam, texcoord + float4( 0.0f, fDy ,0,0) ); 
float4 t11 = tex2Dlod( sam, texcoord + float4( fDx, fDy ,0,0) ); 
float4 tB = lerp( t01, t11, f.x ); 
return lerp( tA, tB, f.y ); 
} 
 
 
float4 tex2DRect( sampler sam,float2 tex,float2 texelSize) 
{ 
     float4 oCol; 
     oCol = tex2D(sam,tex+float2(1,0)*texelSize); 
     oCol += tex2D(sam,tex+float2(-1,0)*texelSize); 
     oCol += tex2D(sam,tex+float2(0, 1)*texelSize); 
     oCol += tex2D(sam,tex+float2(0,-1)*texelSize); 
     return oCol/4.0; 
} 
 
float4 tex2DRect_bilinear( sampler sam,float2 tex,float2 texSize,float2 texelSize) 
{ 
     float4 oCol; 
     oCol = tex2D_bilinear(sam,tex+float2(1,0)*texelSize,texSize,texelSize.x,texelSize.y); 
     oCol += tex2D_bilinear(sam,tex+float2(-1,0)*texelSize,texSize,texelSize.x,texelSize.y); 
     oCol += tex2D_bilinear(sam,tex+float2(0, 1)*texelSize,texSize,texelSize.x,texelSize.y); 
     oCol += tex2D_bilinear(sam,tex+float2(0,-1)*texelSize,texSize,texelSize.x,texelSize.y); 
     return oCol/4.0; 
}