home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 January / Gamestar_69_2005-01_dvd.iso / Dema / theprojectv1-0.exe / FCData / Shaders.pak / Shaders / HWScripts / CryFX / Common.cryFX next >
Encoding:
Text File  |  2004-10-27  |  9.9 KB  |  366 lines

  1. #ifdef ATI
  2.   #define FLOAT float
  3.   #define FLOAT2 float2
  4.   #define FLOAT3 float3
  5.   #define FLOAT4 float4
  6. #else
  7.   #define FLOAT half
  8.   #define FLOAT2 half2
  9.   #define FLOAT3 half3
  10.   #define FLOAT4 half4
  11. #endif
  12.  
  13. //============================================================================
  14. // Declarations macro:
  15.  
  16. #ifdef D3D
  17. #define IN_TNORMVEC float3 TNormal   : BLENDWEIGHT;
  18. #endif
  19.  
  20. #ifdef OPENGL
  21. #define IN_TNORMVEC float3 TNormal   : NORMAL;
  22. #endif
  23.  
  24. #define IN_P    float4 Position  : POSITION;
  25. #define IN_N    float3 Normal    : NORMAL;
  26. #ifdef D3D
  27. #define IN_TN   float3 TNormal   : BLENDWEIGHT;
  28. #endif
  29. #ifdef OPENGL
  30. #define IN_TN   float3 TNormal   : NORMAL;
  31. #endif
  32. #define IN_C0   float4 Color     : COLOR0;
  33. #define IN_C1   float4 Color1    : COLOR1;
  34. #define IN_T0   float4 TexCoord0 : TEXCOORD0;
  35. #define IN_T1   float4 TexCoord1 : TEXCOORD1;
  36. #define IN_T2   float4 TexCoord2 : TEXCOORD2;
  37. #define IN_T3   float4 TexCoord3 : TEXCOORD3;
  38. #define IN_T4   float4 TexCoord4 : TEXCOORD4;
  39. #define IN_T5   float4 TexCoord5 : TEXCOORD5;
  40. #define IN_T6   float4 TexCoord6 : TEXCOORD6;
  41. #define IN_T7   float4 TexCoord7 : TEXCOORD7;
  42. #define IN_T8   float4 TexCoord8 : TEXCOORD8;
  43. #define IN_TANG                                              \
  44.           float3 Tangent   : TEXCOORD2;                      \
  45.           float3 Binormal  : TEXCOORD3;                      \
  46.           IN_TNORMVEC                                        \
  47.  
  48. #define TANG_MATR                                           \
  49.           float3x3 objToTangentSpace;                       \
  50.           objToTangentSpace[0] = IN.Tangent;                \
  51.           objToTangentSpace[1] = IN.Binormal;               \
  52.           objToTangentSpace[2] = IN.TNormal;                \
  53.  
  54. //============================================================================
  55. // Vertex shader output
  56.  
  57. #define OUT_P    float4 HPosition : POSITION;
  58. #define OUT_C0   float4 Color     : COLOR0;
  59. #define OUT_C0   float4 Color     : COLOR0;
  60. #define OUT_C1   float4 Color1    : COLOR1;
  61. #define OUT_T0   float4 Tex0      : TEXCOORD0;
  62. #define OUT_T0_2 float2 Tex0      : TEXCOORD0;
  63. #define OUT_T1   float4 Tex1      : TEXCOORD1;
  64. #define OUT_T1_2 float2 Tex1      : TEXCOORD1;
  65. #define OUT_T2   float4 Tex2      : TEXCOORD2;
  66. #define OUT_T2_2 float2 Tex2      : TEXCOORD2;
  67. #define OUT_T3   float4 Tex3      : TEXCOORD3;
  68. #define OUT_T3_2 float2 Tex3      : TEXCOORD3;
  69. #define OUT_T4   float4 Tex4      : TEXCOORD4;
  70. #define OUT_T4_2 float2 Tex4      : TEXCOORD4;
  71. #define OUT_T5   float4 Tex5      : TEXCOORD5;
  72. #define OUT_T5_2 float2 Tex5      : TEXCOORD5;
  73. #define OUT_T6   float4 Tex6      : TEXCOORD6;
  74. #define OUT_T6_2 float2 Tex6      : TEXCOORD6;
  75. #define OUT_T7   float4 Tex7      : TEXCOORD7;
  76. #define OUT_T7_2 float2 Tex7      : TEXCOORD7;
  77.  
  78. //============================================================================
  79.  
  80. #define PROC_ATTENPIX lightVec * (AttenInfo.y * AttenInfo.z) + AttenInfo.z
  81. #define PROC_ATTENVERT (AttenInfo.x - 1/fiSqDist) * AttenInfo.y
  82.  
  83. #define SECOND_STATE                                        \
  84.     SecondZEnable = true;                                   \
  85.     SecondZWriteEnable = false;                             \
  86.     SecondSrcBlend = ONE;                                   \
  87.     SecondDestBlend = ONE;                                  \
  88.     SecondAlphaBlendEnable = true;                          \
  89.  
  90. #define SECOND_STATE_AB                                     \
  91.     SecondZEnable = true;                                   \
  92.     SecondZWriteEnable = false;                             \
  93.     SecondSrcBlend = SrcAlpha;                              \
  94.     SecondDestBlend = InvSrcAlpha;                          \
  95.     SecondAlphaBlendEnable = true;                          \
  96.  
  97. //============================================================================
  98. // Pixel shader output
  99.  
  100. struct pixout
  101. {
  102.   float4 Color  : COLOR0;
  103. # ifdef _MRT
  104.   float4 Color1 : COLOR1;
  105. # endif
  106. # ifdef _DEPTH_OUT
  107.   float  Depth  : DEPTH;
  108. # endif
  109. };
  110.  
  111. //============================================================================
  112. // Constants
  113.  
  114. // Vertex shader parameters
  115. #define VIEWPROJ_MATRIX uniform float4x4 ModelViewProj : register(c0)
  116. #define PROJ_MATRIX uniform float4x4 ProjMatrix : register(c0)
  117. #define LIGHT_POS uniform float4 LightPos : register(c4)
  118. #define CAMERA_POS uniform float4 CameraPos : register(c5)
  119. #define ATTEN uniform float4 AttenInfo : register(c6)
  120. #define BEND uniform float4 Bend : register(c7)
  121. #define TEX_MATRIX2x4 uniform float2x4 TexMatrix : register(c8)
  122. #define LIGHT_MATRIX uniform float4x4 LightMatrix : register(c10)
  123. #define CLIPPLANE uniform float4 ClipPlane : register(c14)
  124.  
  125. // Pixel shader parameters
  126. #define AMBIENT uniform float4 Ambient : register(c0)
  127. #define DIFFUSE uniform float4 Diffuse : register(c1)
  128. #define SPECULAR uniform float4 Specular : register(c2)
  129.  
  130. #define HDR_OVERBRIGHT 8
  131. #define HDR_EXP_OFFSET 64
  132. #define HDR_EXP_BASE   1.04
  133.  
  134. //============================================================================
  135. // Global Constants
  136.  
  137. #define VS_REG_FOG c29
  138. #define VS_REG_0_025_05_1 c28
  139.  
  140. #define PS_REG_FOG_COLOR_1_1 c7
  141. #define PS_REG_FOG_COLOR_2_0 c31
  142.  
  143. # ifdef _VS
  144. const FLOAT4 g_VSFog : register( VS_REG_FOG );
  145. // .x = 1.0f / (fogEnd - fogStart)
  146. // .y = fogEnd / (fogEnd - fogStart)
  147.  
  148. const FLOAT4 g_VSConsts0 : register( VS_REG_0_025_05_1 );
  149. # endif
  150.  
  151. # ifdef _PS
  152. const FLOAT4 g_PSFogColor_11 : register( PS_REG_FOG_COLOR_1_1 );
  153.   # ifndef _PS_1_1
  154. const FLOAT4 g_PSFogColor_20 : register( PS_REG_FOG_COLOR_2_0 );
  155.   # endif
  156. # endif
  157.  
  158. //============================================================================
  159. // Functions
  160.  
  161. FLOAT4 _pos_Common(float4x4 Matrix, FLOAT4 InPos)
  162. {
  163. # ifdef _VS
  164.   return mul(Matrix, InPos);
  165. # else
  166.   return float4(0,0,0,1);
  167. # endif    
  168. }
  169. FLOAT4 _pos_NULL(float4x4 Matrix, FLOAT4 InPos)
  170. {
  171.   return FLOAT4 (0,0,0,1);
  172. }
  173.  
  174.  
  175. FLOAT CalcFog(float4x4 Matrix, float4 vPos)
  176. {
  177.   float fFog;
  178. # if defined(_FOG_LIN) && defined(_VS)
  179.   float fCameraSpacePosZ = dot(Matrix[3], vPos);
  180.   float fFog = saturate(g_VSFog.y - g_VSFog.x * fCameraSpacePosZ);
  181. # else
  182.   fFog = 1;
  183. # endif
  184.   return fFog;
  185. }
  186.  
  187. FLOAT3 GetFogColor()
  188. {
  189. # ifdef _PS
  190.  # ifdef _PS_1_1
  191.   return g_PSFogColor_11.xyz;
  192.  # else
  193.   return g_PSFogColor_20.xyz;
  194.  # endif  
  195. # else
  196.  return float3(1,1,1);
  197. # endif  
  198. }
  199.  
  200.  
  201. // Common functions
  202. FLOAT4 EXPAND( FLOAT4 a )
  203. {
  204.   FLOAT4 result;
  205.   if (CGC)
  206.     result = 2.h*(a - 0.5h);
  207.   else
  208.     result = a*2.h - 1.h;
  209.   return result;
  210. }
  211. FLOAT3 EXPAND( FLOAT3 a )
  212. {
  213.   FLOAT3 result;
  214.   if (CGC)
  215.     result = 2.h*(a - 0.5h);
  216.   else
  217.     result = a*2.h - 1.h;
  218.   return result;
  219. }
  220. FLOAT EXPAND( FLOAT a )
  221. {
  222.   FLOAT result;
  223.   if (CGC)
  224.     result = 2.h*(a - 0.5h);
  225.   else
  226.     result = a*2.h - 1.h;
  227.   return result;
  228. }
  229.  
  230. FLOAT3 HDREncode( FLOAT3 a )
  231. {
  232.   FLOAT3 result;
  233. # if defined(_HDR) && defined(_HDR_FAKE)
  234.   result = a * HDR_OVERBRIGHT;
  235. # else
  236.   result = a * 2;
  237. # endif          
  238.   return result;
  239. }
  240. FLOAT3 HDREncodeLM( FLOAT3 a )
  241. {
  242.   FLOAT3 result;
  243. # if defined(_HDR) && defined(_HDR_FAKE)
  244.   result = a * HDR_OVERBRIGHT * 2;
  245. # else
  246.   result = a * 4;
  247. # endif          
  248.   return result;
  249. }
  250. FLOAT3 HDREncodeAmb( FLOAT3 a )
  251. {
  252.   FLOAT3 result;
  253. # if defined(_HDR) && defined(_HDR_FAKE)
  254.   result = a * HDR_OVERBRIGHT / 2;
  255. # else
  256.   result = a;
  257. # endif          
  258.   return result;
  259. }
  260.  
  261. FLOAT3 HDRFogBlend( FLOAT3 vColor, FLOAT fFog, FLOAT3 vFogColor )
  262. {
  263.   FLOAT3 result;
  264. # if defined(_HDR) && defined(_HDR_FAKE)
  265.   result = lerp(HDREncodeAmb(vFogColor), vColor, fFog);
  266. # else
  267.   result = vColor;
  268. # endif        
  269.   return result;
  270. }
  271.  
  272. FLOAT4 HDRFogBlend( FLOAT4 vColor, FLOAT fFog, FLOAT3 vFogColor )
  273. {
  274.   FLOAT4 result;
  275. # if defined(_HDR) && defined(_HDR_FAKE)
  276.   result.xyz = lerp(HDREncodeAmb(vFogColor), vColor, fFog);
  277.   result.a = vColor.a;
  278. # else
  279.   result = vColor;
  280. # endif        
  281.   return result;
  282. }
  283.  
  284. # define LOG(a, b)   ( log(b) / log(a) )
  285.  
  286. # define EXP_BASE    (1.06)
  287. # define EXP_OFFSET  (128.0)
  288.  
  289. FLOAT4 EncodeHDR_RGB_RGBE8(in float3 rgb)
  290. {
  291.   // Compute a common exponent
  292.   FLOAT fLen = dot(rgb.rgb, 1.0);
  293.   FLOAT fExp = LOG(EXP_BASE, fLen);
  294.  
  295.   FLOAT4 ret;
  296.   ret.a = (fExp + EXP_OFFSET) / 256;
  297.   ret.rgb = rgb / fLen;
  298.  
  299.   return ret;
  300. }
  301.  
  302. FLOAT3 DecodeHDR_RGBE8_RGB(in float4 rgbe)
  303. {
  304.   FLOAT fExp = rgbe.a * 256 - EXP_OFFSET;
  305.   FLOAT fScale = pow(EXP_BASE, fExp);
  306.  
  307.   return (rgbe.rgb * fScale);
  308. }
  309.  
  310. void HDROutput( out pixout OUT, FLOAT4 Color, FLOAT fDepth, FLOAT3 FogColor, FLOAT fFogFactor )
  311. {
  312.   FLOAT3 result = HDRFogBlend(Color.xyz, fFogFactor, FogColor);
  313. # ifndef _MRT
  314.   OUT.Color = float4(result, Color.a);
  315. # else
  316.   //float4 rgbe = EncodeHDR_RGB_RGBE8(result.xyz);
  317.   //OUT.Color0.xyz = rgbe.xyz;
  318.   //OUT.Color0.a = Color.a;
  319.   //OUT.Color1 = float4(rgbe.a, 0, 0, 1);
  320.   float lum = Luminance(result.xyz) / HDRFAKE_MAXOVERBRIGHT;
  321.   OUT.Color0 = float4(result.xyz, Color.a);
  322.   OUT.Color1 = float4(lum,0,0,1);
  323. # endif
  324. # ifdef _DEPTH_OUT
  325.   OUT.Depth = fDepth;
  326. # endif
  327. }
  328.  
  329. void HDROutput( out pixout OUT, FLOAT4 Color, FLOAT fDepth)
  330. {
  331. # ifndef _MRT
  332.   OUT.Color = Color;
  333. # else
  334.   //float4 rgbe = EncodeHDR_RGB_RGBE8(Color.xyz);
  335.   //OUT.Color0.xyz = rgbe.xyz;
  336.   //OUT.Color0.a = Color.a;
  337.   //OUT.Color1 = float4(rgbe.a, 0, 0, 1);
  338.   float lum = Luminance(Color.xyz) / HDRFAKE_MAXOVERBRIGHT;
  339.   OUT.Color0 = Color;
  340.   OUT.Color1 = float4(lum,0,0,1);
  341. # endif
  342. # ifdef _DEPTH_OUT
  343.   OUT.Depth = fDepth;
  344. # endif
  345. }
  346.  
  347. FLOAT3 GetNormalMap( sampler2D bumpMap, float2 bumpTC )
  348. {
  349.   FLOAT3 bumpNormal;
  350. # ifdef _3DC
  351.   bumpNormal.xy = EXPAND(tex2D(bumpMap, bumpTC.xy).xy);
  352.   bumpNormal.z = sqrt(1 - dot(bumpNormal.xy, bumpNormal.xy));
  353. # else
  354.   bumpNormal = EXPAND(tex2D(bumpMap, bumpTC.xy).xyz);
  355. # endif
  356.   return bumpNormal;
  357. }
  358.  
  359. FLOAT DepthBias( FLOAT fHeight, FLOAT3 vNormal, FLOAT3 vView, FLOAT2 vZW )
  360. {
  361.   FLOAT3 vN = vNormal * fHeight;
  362.   FLOAT fOffs = dot(vView, vN) * 0.02;
  363.   FLOAT fOrigZ = vZW.x;
  364.   return (fOrigZ - fOffs) / vZW.y;
  365. }
  366.