Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1010 lines
32 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. //
  3. // Purpose: This is where all common code for vertex shaders go.
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef COMMON_VS_FXC_H_
  9. #define COMMON_VS_FXC_H_
  10. #include "common_fxc.h"
  11. // Put global skip commands here. . make sure and check that the appropriate vars are defined
  12. // so these aren't used on the wrong shaders!
  13. // --------------------------------------------------------------------------------
  14. // Ditch all fastpath attemps if we are doing LIGHTING_PREVIEW.
  15. // SKIP: defined $LIGHTING_PREVIEW && defined $FASTPATH && $LIGHTING_PREVIEW && $FASTPATH
  16. // --------------------------------------------------------------------------------
  17. #ifdef _PS3
  18. // Remap semantics for PS3 Cg
  19. #define POSITION1 TEXCOORD4
  20. #define NORMAL1 TEXCOORD5
  21. #endif // _PS3
  22. #ifndef COMPRESSED_VERTS
  23. // Default to no vertex compression
  24. #define COMPRESSED_VERTS 0
  25. #endif
  26. #if ( !defined( SHADER_MODEL_VS_2_0 ) && !defined( SHADER_MODEL_VS_3_0 ) )
  27. #if COMPRESSED_VERTS == 1
  28. #error "Vertex compression is only for DX9 and up!"
  29. #endif
  30. #endif
  31. // We're testing 2 normal compression methods
  32. // One compressed normals+tangents into a SHORT2 each (8 bytes total)
  33. // The other compresses them together, into a single UBYTE4 (4 bytes total)
  34. // FIXME: pick one or the other, compare lighting quality in important cases
  35. #define COMPRESSED_NORMALS_SEPARATETANGENTS_SHORT2 0
  36. #define COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 1
  37. //#define COMPRESSED_NORMALS_TYPE COMPRESSED_NORMALS_SEPARATETANGENTS_SHORT2
  38. #define COMPRESSED_NORMALS_TYPE COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4
  39. #define FOGTYPE_RANGE 0
  40. #define FOGTYPE_HEIGHT 1
  41. #define COMPILE_ERROR ( 1/0; )
  42. // -------------------------
  43. // CONSTANTS
  44. // -------------------------
  45. #pragma def ( vs, c0, 0.0f, 1.0f, 2.0f, 0.5f )
  46. const float4 cConstants1 : register(c1);
  47. #define cOOGamma cConstants1.x
  48. #define cOverbright 2.0f
  49. #define cOneThird cConstants1.z
  50. #define cOOOverbright ( 1.0f / 2.0f )
  51. // The g_bLightEnabled registers and g_nLightCountRegister hold the same information regarding
  52. // enabling lights, but callers internal to this file tend to use the loops, while external
  53. // callers will end up using the booleans
  54. const bool g_bLightEnabled[4] : register(b0);
  55. // through b3
  56. #ifndef _PS3
  57. const int g_nLightCountRegister : register(i0);
  58. #define g_nLightCount g_nLightCountRegister.x
  59. #endif // !_PS3
  60. const float4 cEyePos_WaterHeightW : register(c2);
  61. #define cEyePos cEyePos_WaterHeightW.xyz
  62. #define cWaterHeight cEyePos_WaterHeightW.w
  63. // This is still used by asm stuff.
  64. const float4 cObsoleteLightIndex : register(c3);
  65. const float4x4 cModelViewProj : register(c4);
  66. const float4x4 cViewProj : register(c8);
  67. // Only cFlexScale.x is used
  68. // It is a binary value used to switch on/off the addition of the flex delta stream
  69. const float4 cFlexScale : register(c13);
  70. const float4 cFogParams : register(c16);
  71. #define cFogEndOverFogRange cFogParams.x
  72. // cFogParams.y unused
  73. #define cRadialFogMaxDensity cFogParams.z //radial fog max density in fractional portion. height fog max density stored in integer portion and is multiplied by 1e10
  74. #define cOOFogRange cFogParams.w
  75. const float4x4 cViewModel : register(c17);
  76. const float3 cAmbientCubeX [ 2 ] : register ( c21 ) ;
  77. const float3 cAmbientCubeY [ 2 ] : register ( c23 ) ;
  78. const float3 cAmbientCubeZ [ 2 ] : register ( c25 ) ;
  79. #if defined ( SHADER_MODEL_VS_3_0 )
  80. const float4 cFlexWeights [ 512 ] : register ( c1024 ) ;
  81. #endif
  82. struct LightInfo
  83. {
  84. float4 color; // {xyz} is color w is light type code (see comment below)
  85. float4 dir; // {xyz} is dir w is light type code
  86. float4 pos;
  87. float4 spotParams;
  88. float4 atten;
  89. };
  90. // w components of color and dir indicate light type:
  91. // 1x - directional
  92. // 01 - spot
  93. // 00 - point
  94. // Four lights x 5 constants each = 20 constants
  95. #if (CASCADED_SHADOW_MAPPING == 1)
  96. LightInfo cLightInfo[2] : register(c27);
  97. #else
  98. LightInfo cLightInfo[4] : register(c27);
  99. #endif
  100. #define LIGHT_0_POSITION_REG c29
  101. #ifdef SHADER_MODEL_VS_1_1
  102. const float4 cModulationColor : register(c37);
  103. #define SHADER_SPECIFIC_CONST_0 c38
  104. #define SHADER_SPECIFIC_CONST_1 c39
  105. #define SHADER_SPECIFIC_CONST_2 c40
  106. #define SHADER_SPECIFIC_CONST_3 c41
  107. #define SHADER_SPECIFIC_CONST_4 c42
  108. #define SHADER_SPECIFIC_CONST_5 c43
  109. #define SHADER_SPECIFIC_CONST_6 c44
  110. #define SHADER_SPECIFIC_CONST_7 c45
  111. #define SHADER_SPECIFIC_CONST_8 c46
  112. #define SHADER_SPECIFIC_CONST_9 c47
  113. #define SHADER_SPECIFIC_CONST_10 c14
  114. #define SHADER_SPECIFIC_CONST_11 c15
  115. static const int cModel0Index = 48;
  116. const float4x3 cModel[16] : register(c48);
  117. // last cmodel is c105 for dx80, c214 for dx90
  118. #else // DX9 shaders (vs20 and beyond)
  119. const float4 cModulationColor : register( c47 );
  120. #define SHADER_SPECIFIC_CONST_0 c48
  121. #define SHADER_SPECIFIC_CONST_1 c49
  122. #define SHADER_SPECIFIC_CONST_2 c50
  123. #define SHADER_SPECIFIC_CONST_3 c51
  124. #define SHADER_SPECIFIC_CONST_4 c52
  125. #define SHADER_SPECIFIC_CONST_5 c53
  126. #define SHADER_SPECIFIC_CONST_6 c54
  127. #define SHADER_SPECIFIC_CONST_7 c55
  128. #define SHADER_SPECIFIC_CONST_8 c56
  129. #define SHADER_SPECIFIC_CONST_9 c57
  130. #define SHADER_SPECIFIC_CONST_10 c14
  131. #define SHADER_SPECIFIC_CONST_11 c15
  132. #define SHADER_SPECIFIC_CONST_12 c12
  133. static const int cModel0Index = 58;
  134. const hlsl_float4x3 cModel[53] : register( c58 );
  135. // last cmodel is c105 for dx80, c214 for dx90
  136. #define SHADER_SPECIFIC_CONST_CSM_0 c37 // c37 through c44, 2 float4x4 matrices, cascade 0, or cascade 1 and 2
  137. #define SHADER_SPECIFIC_CONST_CSM_1 c45 // row z(2) of cascade 0
  138. #define SHADER_SPECIFIC_BOOL_CONST_0 b4
  139. #define SHADER_SPECIFIC_BOOL_CONST_1 b5
  140. #define SHADER_SPECIFIC_BOOL_CONST_2 b6
  141. #define SHADER_SPECIFIC_BOOL_CONST_3 b7
  142. #define SHADER_SPECIFIC_BOOL_CONST_4 b8
  143. #define SHADER_SPECIFIC_BOOL_CONST_5 b9
  144. #define SHADER_SPECIFIC_BOOL_CONST_6 b10
  145. #define SHADER_SPECIFIC_BOOL_CONST_7 b11
  146. #endif // vertex shader model constant packing changes
  147. //=======================================================================================
  148. // Methods to decompress vertex normals
  149. //=======================================================================================
  150. //-----------------------------------------------------------------------------------
  151. // Decompress a normal from two-component compressed format
  152. // We expect this data to come from a signed SHORT2 stream in the range of -32768..32767
  153. //
  154. // -32678 and 0 are invalid encodings
  155. // w contains the sign to use in the cross product when generating a binormal
  156. void _DecompressShort2Tangent( float2 inputTangent, out float4 outputTangent )
  157. {
  158. float2 ztSigns = sign( inputTangent ); // sign bits for z and tangent (+1 or -1)
  159. float2 xyAbs = abs( inputTangent ); // 1..32767
  160. outputTangent.xy = (xyAbs - 16384.0f) / 16384.0f; // x and y
  161. outputTangent.z = ztSigns.x * sqrt( saturate( 1.0f - dot( outputTangent.xy, outputTangent.xy ) ) );
  162. outputTangent.w = ztSigns.y;
  163. }
  164. //-----------------------------------------------------------------------------------
  165. // Same code as _DecompressShort2Tangent, just one returns a float4, one a float3
  166. void _DecompressShort2Normal( float2 inputNormal, out float3 outputNormal )
  167. {
  168. float4 result;
  169. _DecompressShort2Tangent( inputNormal, result );
  170. outputNormal = result.xyz;
  171. }
  172. //-----------------------------------------------------------------------------------
  173. // Decompress normal+tangent together
  174. void _DecompressShort2NormalTangent( float2 inputNormal, float2 inputTangent, out float3 outputNormal, out float4 outputTangent )
  175. {
  176. // FIXME: if we end up sticking with the SHORT2 format, pack the normal and tangent into a single SHORT4 element
  177. // (that would make unpacking normal+tangent here together much cheaper than the sum of their parts)
  178. _DecompressShort2Normal( inputNormal, outputNormal );
  179. _DecompressShort2Tangent( inputTangent, outputTangent );
  180. }
  181. //=======================================================================================
  182. // Decompress a normal and tangent from four-component compressed format
  183. // We expect this data to come from an unsigned UBYTE4 stream in the range of 0..255
  184. // The final vTangent.w contains the sign to use in the cross product when generating a binormal
  185. void _DecompressUByte4NormalTangent( float4 inputNormal,
  186. out float3 outputNormal, // {nX, nY, nZ}
  187. out float4 outputTangent ) // {tX, tY, tZ, sign of binormal}
  188. {
  189. float fOne = 1.0f;
  190. float4 ztztSignBits = ( inputNormal - 128.0f ) < 0; // sign bits for zs and binormal (1 or 0) set-less-than (slt) asm instruction
  191. float4 xyxyAbs = abs( inputNormal - 128.0f ) - ztztSignBits; // 0..127
  192. float4 xyxySignBits = ( xyxyAbs - 64.0f ) < 0; // sign bits for xs and ys (1 or 0)
  193. float4 normTan = (abs( xyxyAbs - 64.0f ) - xyxySignBits) / 63.0f; // abs({nX, nY, tX, tY})
  194. outputNormal.xy = normTan.xy; // abs({nX, nY, __, __})
  195. outputTangent.xy = normTan.zw; // abs({tX, tY, __, __})
  196. float4 xyxySigns = 1 - 2*xyxySignBits; // Convert sign bits to signs
  197. float4 ztztSigns = 1 - 2*ztztSignBits; // ( [1,0] -> [-1,+1] )
  198. outputNormal.z = 1.0f - outputNormal.x - outputNormal.y; // Project onto x+y+z=1
  199. outputNormal.xyz = normalize( outputNormal.xyz ); // Normalize onto unit sphere
  200. outputNormal.xy *= xyxySigns.xy; // Restore x and y signs
  201. outputNormal.z *= ztztSigns.x; // Restore z sign
  202. outputTangent.z = 1.0f - outputTangent.x - outputTangent.y; // Project onto x+y+z=1
  203. outputTangent.xyz = normalize( outputTangent.xyz ); // Normalize onto unit sphere
  204. outputTangent.xy *= xyxySigns.zw; // Restore x and y signs
  205. outputTangent.z *= ztztSigns.z; // Restore z sign
  206. outputTangent.w = ztztSigns.w; // Binormal sign
  207. }
  208. //-----------------------------------------------------------------------------------
  209. // Decompress just a normal from four-component compressed format (same as above)
  210. // We expect this data to come from an unsigned UBYTE4 stream in the range of 0..255
  211. // [ When compiled, this works out to approximately 17 asm instructions ]
  212. void _DecompressUByte4Normal( float4 inputNormal,
  213. out float3 outputNormal) // {nX, nY, nZ}
  214. {
  215. float fOne = 1.0f;
  216. float2 ztSigns = ( inputNormal.xy - 128.0f ) < 0; // sign bits for zs and binormal (1 or 0) set-less-than (slt) asm instruction
  217. float2 xyAbs = abs( inputNormal.xy - 128.0f ) - ztSigns; // 0..127
  218. float2 xySigns = ( xyAbs - 64.0f ) < 0; // sign bits for xs and ys (1 or 0)
  219. outputNormal.xy = ( abs( xyAbs - 64.0f ) - xySigns ) / 63.0f; // abs({nX, nY})
  220. outputNormal.z = 1.0f - outputNormal.x - outputNormal.y; // Project onto x+y+z=1
  221. outputNormal.xyz = normalize( outputNormal.xyz ); // Normalize onto unit sphere
  222. outputNormal.xy *= lerp( fOne.xx, -fOne.xx, xySigns ); // Restore x and y signs
  223. outputNormal.z *= lerp( fOne.x, -fOne.x, ztSigns.x ); // Restore z sign
  224. }
  225. void DecompressVertex_Normal( float4 inputNormal, out float3 outputNormal )
  226. {
  227. if ( COMPRESSED_VERTS == 1 )
  228. {
  229. #ifdef _PS3
  230. // ps3 byte order is swapped
  231. inputNormal = inputNormal.wzyx;
  232. #endif // _PS3
  233. if ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_SEPARATETANGENTS_SHORT2 )
  234. {
  235. _DecompressShort2Normal( inputNormal.xy, outputNormal );
  236. }
  237. else // ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 )
  238. {
  239. _DecompressUByte4Normal( inputNormal, outputNormal );
  240. }
  241. }
  242. else
  243. {
  244. outputNormal = inputNormal.xyz;
  245. }
  246. }
  247. void DecompressVertex_NormalTangent( float4 inputNormal, float4 inputTangent, out float3 outputNormal, out float4 outputTangent )
  248. {
  249. if ( COMPRESSED_VERTS == 1 )
  250. {
  251. #ifdef _PS3
  252. // ps3 byte order is swapped
  253. inputNormal = inputNormal.wzyx;
  254. #endif // _PS3
  255. if ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_SEPARATETANGENTS_SHORT2 )
  256. {
  257. _DecompressShort2NormalTangent( inputNormal.xy, inputTangent.xy, outputNormal, outputTangent );
  258. }
  259. else // ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 )
  260. {
  261. _DecompressUByte4NormalTangent( inputNormal, outputNormal, outputTangent );
  262. }
  263. }
  264. else
  265. {
  266. outputNormal = inputNormal.xyz;
  267. outputTangent = inputTangent;
  268. }
  269. }
  270. #ifdef SHADER_MODEL_VS_3_0
  271. //-----------------------------------------------------------------------------
  272. // Methods to sample morph data from a vertex texture
  273. // NOTE: vMorphTargetTextureDim.x = width, cVertexTextureDim.y = height, cVertexTextureDim.z = # of float4 fields per vertex
  274. // For position + normal morph for example, there will be 2 fields.
  275. //-----------------------------------------------------------------------------
  276. float4 SampleMorphDelta( sampler2D vt, const float3 vMorphTargetTextureDim, const float4 vMorphSubrect, const float flVertexID, const float flField )
  277. {
  278. float flColumn = floor( flVertexID / vMorphSubrect.w );
  279. float4 t;
  280. t.x = vMorphSubrect.x + vMorphTargetTextureDim.z * flColumn + flField + 0.5f;
  281. t.y = vMorphSubrect.y + flVertexID - flColumn * vMorphSubrect.w + 0.5f;
  282. t.xy /= vMorphTargetTextureDim.xy;
  283. t.z = t.w = 0.f;
  284. return tex2Dlod( vt, t );
  285. }
  286. // Optimized version which reads 2 deltas
  287. void SampleMorphDelta2( sampler2D vt, const float3 vMorphTargetTextureDim, const float4 vMorphSubrect, const float flVertexID, out float4 delta1, out float4 delta2 )
  288. {
  289. float flColumn = floor( flVertexID / vMorphSubrect.w );
  290. float4 t;
  291. t.x = vMorphSubrect.x + vMorphTargetTextureDim.z * flColumn + 0.5f;
  292. t.y = vMorphSubrect.y + flVertexID - flColumn * vMorphSubrect.w + 0.5f;
  293. t.xy /= vMorphTargetTextureDim.xy;
  294. t.z = t.w = 0.f;
  295. delta1 = tex2Dlod( vt, t );
  296. t.x += 1.0f / vMorphTargetTextureDim.x;
  297. delta2 = tex2Dlod( vt, t );
  298. }
  299. #endif // SHADER_MODEL_VS_3_0
  300. //-----------------------------------------------------------------------------
  301. // Method to apply morphs
  302. //-----------------------------------------------------------------------------
  303. bool ApplyMorph( float3 vPosFlex, inout float3 vPosition )
  304. {
  305. // Flexes coming in from a separate stream
  306. float3 vPosDelta = vPosFlex.xyz * cFlexScale.x;
  307. vPosition.xyz += vPosDelta;
  308. return true;
  309. }
  310. bool ApplyMorph( float3 vPosFlex, float3 vNormalFlex, inout float3 vPosition, inout float3 vNormal )
  311. {
  312. // Flexes coming in from a separate stream
  313. float3 vPosDelta = vPosFlex.xyz * cFlexScale.x;
  314. float3 vNormalDelta = vNormalFlex.xyz * cFlexScale.x;
  315. vPosition.xyz += vPosDelta;
  316. vNormal += vNormalDelta;
  317. return true;
  318. }
  319. bool ApplyMorph( float3 vPosFlex, float3 vNormalFlex,
  320. inout float3 vPosition, inout float3 vNormal, inout float3 vTangent )
  321. {
  322. // Flexes coming in from a separate stream
  323. float3 vPosDelta = vPosFlex.xyz * cFlexScale.x;
  324. float3 vNormalDelta = vNormalFlex.xyz * cFlexScale.x;
  325. vPosition.xyz += vPosDelta;
  326. vNormal += vNormalDelta;
  327. vTangent.xyz += vNormalDelta;
  328. return true;
  329. }
  330. bool ApplyMorph( float4 vPosFlex, float3 vNormalFlex,
  331. inout float3 vPosition, inout float3 vNormal, inout float3 vTangent, out float flWrinkle )
  332. {
  333. // Flexes coming in from a separate stream
  334. float3 vPosDelta = vPosFlex.xyz * cFlexScale.x;
  335. float3 vNormalDelta = vNormalFlex.xyz * cFlexScale.x;
  336. flWrinkle = vPosFlex.w * cFlexScale.y;
  337. vPosition.xyz += vPosDelta;
  338. vNormal += vNormalDelta;
  339. vTangent.xyz += vNormalDelta;
  340. return true;
  341. }
  342. #ifdef SHADER_MODEL_VS_3_0
  343. bool ApplyMorph( sampler2D morphSampler, const float3 vMorphTargetTextureDim, const float4 vMorphSubrect,
  344. const float flVertexID, const float3 vMorphTexCoord,
  345. inout float3 vPosition )
  346. {
  347. #if MORPHING
  348. #if !DECAL
  349. // Flexes coming in from a separate stream
  350. float4 vPosDelta = SampleMorphDelta( morphSampler, vMorphTargetTextureDim, vMorphSubrect, flVertexID, 0 );
  351. vPosition += vPosDelta.xyz;
  352. #else
  353. float4 t = float4( vMorphTexCoord.x, vMorphTexCoord.y, 0.0f, 0.0f );
  354. float3 vPosDelta = tex2Dlod( morphSampler, t );
  355. vPosition += vPosDelta.xyz * vMorphTexCoord.z;
  356. #endif // DECAL
  357. return true;
  358. #else // !MORPHING
  359. return false;
  360. #endif
  361. }
  362. bool ApplyMorph( sampler2D morphSampler, const float3 vMorphTargetTextureDim, const float4 vMorphSubrect,
  363. const float flVertexID, const float3 vMorphTexCoord,
  364. inout float3 vPosition, inout float3 vNormal )
  365. {
  366. #if MORPHING
  367. #if !DECAL
  368. float4 vPosDelta, vNormalDelta;
  369. SampleMorphDelta2( morphSampler, vMorphTargetTextureDim, vMorphSubrect, flVertexID, vPosDelta, vNormalDelta );
  370. vPosition += vPosDelta.xyz;
  371. vNormal += vNormalDelta.xyz;
  372. #else
  373. float4 t = float4( vMorphTexCoord.x, vMorphTexCoord.y, 0.0f, 0.0f );
  374. float3 vPosDelta = tex2Dlod( morphSampler, t );
  375. t.x += 1.0f / vMorphTargetTextureDim.x;
  376. float3 vNormalDelta = tex2Dlod( morphSampler, t );
  377. vPosition += vPosDelta.xyz * vMorphTexCoord.z;
  378. vNormal += vNormalDelta.xyz * vMorphTexCoord.z;
  379. #endif // DECAL
  380. return true;
  381. #else // !MORPHING
  382. return false;
  383. #endif
  384. }
  385. bool ApplyMorph( sampler2D morphSampler, const float3 vMorphTargetTextureDim, const float4 vMorphSubrect,
  386. const float flVertexID, const float3 vMorphTexCoord,
  387. inout float3 vPosition, inout float3 vNormal, inout float3 vTangent )
  388. {
  389. #if MORPHING
  390. #if !DECAL
  391. float4 vPosDelta, vNormalDelta;
  392. SampleMorphDelta2( morphSampler, vMorphTargetTextureDim, vMorphSubrect, flVertexID, vPosDelta, vNormalDelta );
  393. vPosition += vPosDelta.xyz;
  394. vNormal += vNormalDelta.xyz;
  395. vTangent += vNormalDelta.xyz;
  396. #else
  397. float4 t = float4( vMorphTexCoord.x, vMorphTexCoord.y, 0.0f, 0.0f );
  398. float3 vPosDelta = tex2Dlod( morphSampler, t );
  399. t.x += 1.0f / vMorphTargetTextureDim.x;
  400. float3 vNormalDelta = tex2Dlod( morphSampler, t );
  401. vPosition += vPosDelta.xyz * vMorphTexCoord.z;
  402. vNormal += vNormalDelta.xyz * vMorphTexCoord.z;
  403. vTangent += vNormalDelta.xyz * vMorphTexCoord.z;
  404. #endif // DECAL
  405. return true;
  406. #else // MORPHING
  407. return false;
  408. #endif
  409. }
  410. bool ApplyMorph( sampler2D morphSampler, const float3 vMorphTargetTextureDim, const float4 vMorphSubrect,
  411. const float flVertexID, const float3 vMorphTexCoord,
  412. inout float3 vPosition, inout float3 vNormal, inout float3 vTangent, out float flWrinkle )
  413. {
  414. #if MORPHING
  415. #if !DECAL
  416. float4 vPosDelta, vNormalDelta;
  417. SampleMorphDelta2( morphSampler, vMorphTargetTextureDim, vMorphSubrect, flVertexID, vPosDelta, vNormalDelta );
  418. vPosition += vPosDelta.xyz;
  419. vNormal += vNormalDelta.xyz;
  420. vTangent += vNormalDelta.xyz;
  421. flWrinkle = vPosDelta.w;
  422. #else
  423. float4 t = float4( vMorphTexCoord.x, vMorphTexCoord.y, 0.0f, 0.0f );
  424. float4 vPosDelta = tex2Dlod( morphSampler, t );
  425. t.x += 1.0f / vMorphTargetTextureDim.x;
  426. float3 vNormalDelta = tex2Dlod( morphSampler, t );
  427. vPosition += vPosDelta.xyz * vMorphTexCoord.z;
  428. vNormal += vNormalDelta.xyz * vMorphTexCoord.z;
  429. vTangent += vNormalDelta.xyz * vMorphTexCoord.z;
  430. flWrinkle = vPosDelta.w * vMorphTexCoord.z;
  431. #endif // DECAL
  432. return true;
  433. #else // MORPHING
  434. flWrinkle = 0.0f;
  435. return false;
  436. #endif
  437. }
  438. #endif // SHADER_MODEL_VS_3_0
  439. float CalcFixedFunctionFog( const float3 worldPos, const bool bWaterFog )
  440. {
  441. if( !bWaterFog )
  442. {
  443. return CalcRangeFogFactorFixedFunction( worldPos, cEyePos, cRadialFogMaxDensity, cFogEndOverFogRange, cOOFogRange );
  444. }
  445. else
  446. {
  447. return 0.0f; //all done in the pixel shader as of ps20 (current min-spec)
  448. }
  449. }
  450. float CalcFixedFunctionFog( const float3 worldPos, const int fogType )
  451. {
  452. return CalcFixedFunctionFog( worldPos, fogType != FOGTYPE_RANGE );
  453. }
  454. float CalcNonFixedFunctionFog( const float3 worldPos, const bool bWaterFog )
  455. {
  456. if( !bWaterFog )
  457. {
  458. return CalcRangeFogFactorNonFixedFunction( worldPos, cEyePos, cRadialFogMaxDensity, cFogEndOverFogRange, cOOFogRange );
  459. }
  460. else
  461. {
  462. return 0.0f; //all done in the pixel shader as of ps20 (current min-spec)
  463. }
  464. }
  465. float CalcNonFixedFunctionFog( const float3 worldPos, const int fogType )
  466. {
  467. return CalcNonFixedFunctionFog( worldPos, fogType != FOGTYPE_RANGE );
  468. }
  469. float4 DecompressBoneWeights( const float4 weights )
  470. {
  471. float4 result = weights;
  472. if ( COMPRESSED_VERTS )
  473. {
  474. // Decompress from SHORT2 to float. In our case, [-1, +32767] -> [0, +1]
  475. // NOTE: we add 1 here so we can divide by 32768 - which is exact (divide by 32767 is not).
  476. // This avoids cracking between meshes with different numbers of bone weights.
  477. // We use SHORT2 instead of SHORT2N for a similar reason - the GPU's conversion
  478. // from [-32768,+32767] to [-1,+1] is imprecise in the same way.
  479. result += 1;
  480. result /= 32768;
  481. #ifdef _PS3
  482. // @todo [kutta] - figure out why the values are swapped
  483. // Only the first 2 values are even used, and their position seems to be swapped on PS3, so swap X & Y and who cares about z / w anyways
  484. return result.yxzw;
  485. #endif // _PS3
  486. }
  487. return result;
  488. }
  489. void SkinPosition( bool bSkinning, const float4 modelPos,
  490. const float4 boneWeights, float4 fBoneIndices,
  491. out float3 worldPos )
  492. {
  493. #if defined( _PS3 )
  494. int3 boneIndices = D3DCOLORtoUBYTE4( fBoneIndices ).zyx;
  495. #elif !defined( _X360 )
  496. int3 boneIndices = D3DCOLORtoUBYTE4( fBoneIndices ).xyz;
  497. #else
  498. int3 boneIndices = fBoneIndices.xyz;
  499. #endif
  500. // Needed for invariance issues caused by multipass rendering
  501. #if defined( _X360 )
  502. [isolate]
  503. #endif
  504. {
  505. if ( !bSkinning )
  506. {
  507. worldPos = mul4x3( modelPos, cModel[0] );
  508. }
  509. else // skinning - always three bones
  510. {
  511. hlsl_float4x3 mat1 = cModel[boneIndices[0]];
  512. hlsl_float4x3 mat2 = cModel[boneIndices[1]];
  513. hlsl_float4x3 mat3 = cModel[boneIndices[2]];
  514. float3 weights = DecompressBoneWeights( boneWeights ).xyz;
  515. weights[2] = 1 - (weights[0] + weights[1]);
  516. hlsl_float4x3 blendMatrix = mat1 * weights[0] + mat2 * weights[1] + mat3 * weights[2];
  517. worldPos = mul4x3( modelPos, blendMatrix );
  518. }
  519. }
  520. }
  521. void SkinPositionAndNormal( bool bSkinning, const float4 modelPos, const float3 modelNormal,
  522. const float4 boneWeights, float4 fBoneIndices,
  523. out float3 worldPos, out float3 worldNormal )
  524. {
  525. // Needed for invariance issues caused by multipass rendering
  526. #if defined( _X360 )
  527. [isolate]
  528. #endif
  529. {
  530. #if defined( _PS3 )
  531. int3 boneIndices = D3DCOLORtoUBYTE4( fBoneIndices ).zyx;
  532. #elif !defined( _X360 )
  533. int3 boneIndices = D3DCOLORtoUBYTE4( fBoneIndices ).xyz;
  534. #else
  535. int3 boneIndices = fBoneIndices;
  536. #endif
  537. if ( !bSkinning )
  538. {
  539. worldPos = mul4x3( modelPos, cModel[0] );
  540. worldNormal = mul3x3( modelNormal, ( const float3x3 )cModel[0] );
  541. }
  542. else // skinning - always three bones
  543. {
  544. hlsl_float4x3 mat1 = cModel[boneIndices[0]];
  545. hlsl_float4x3 mat2 = cModel[boneIndices[1]];
  546. hlsl_float4x3 mat3 = cModel[boneIndices[2]];
  547. float3 weights = DecompressBoneWeights( boneWeights ).xyz;
  548. weights[2] = 1 - (weights[0] + weights[1]);
  549. hlsl_float4x3 blendMatrix = mat1 * weights[0] + mat2 * weights[1] + mat3 * weights[2];
  550. worldPos = mul4x3( modelPos, blendMatrix );
  551. worldNormal = mul3x3( modelNormal, ( float3x3 )blendMatrix );
  552. }
  553. } // end [isolate]
  554. }
  555. // Is it worth keeping SkinPosition and SkinPositionAndNormal around since the optimizer
  556. // gets rid of anything that isn't used?
  557. void SkinPositionNormalAndTangentSpace(
  558. bool bSkinning,
  559. const float4 modelPos, const float3 modelNormal,
  560. const float4 modelTangentS,
  561. const float4 boneWeights, float4 fBoneIndices,
  562. out float3 worldPos, out float3 worldNormal,
  563. out float3 worldTangentS, out float3 worldTangentT )
  564. {
  565. #if defined( _PS3 )
  566. int3 boneIndices = D3DCOLORtoUBYTE4( fBoneIndices ).zyx;
  567. #elif !defined( _X360 )
  568. int3 boneIndices = D3DCOLORtoUBYTE4( fBoneIndices ).xyz;
  569. #else
  570. int3 boneIndices = fBoneIndices.xyz;
  571. #endif
  572. // Needed for invariance issues caused by multipass rendering
  573. #if defined( _X360 )
  574. [isolate]
  575. #endif
  576. {
  577. if ( !bSkinning )
  578. {
  579. worldPos = mul4x3( modelPos, cModel[0] );
  580. worldNormal = mul3x3( modelNormal, ( const float3x3 )cModel[0] );
  581. worldTangentS = mul3x3( ( float3 )modelTangentS, ( const float3x3 )cModel[0] );
  582. }
  583. else // skinning - always three bones
  584. {
  585. hlsl_float4x3 mat1 = cModel[boneIndices[0]];
  586. hlsl_float4x3 mat2 = cModel[boneIndices[1]];
  587. hlsl_float4x3 mat3 = cModel[boneIndices[2]];
  588. float3 weights = DecompressBoneWeights( boneWeights ).xyz;
  589. weights[2] = 1 - (weights[0] + weights[1]);
  590. hlsl_float4x3 blendMatrix = mat1 * weights[0] + mat2 * weights[1] + mat3 * weights[2];
  591. worldPos = mul4x3( modelPos, blendMatrix );
  592. worldNormal = mul3x3( modelNormal, ( const float3x3 )blendMatrix );
  593. worldTangentS = mul3x3( ( float3 )modelTangentS, ( const float3x3 )blendMatrix );
  594. }
  595. worldTangentT = cross( worldNormal, worldTangentS ) * modelTangentS.w;
  596. }
  597. }
  598. //-----------------------------------------------------------------------------
  599. // Lighting helper functions
  600. //-----------------------------------------------------------------------------
  601. float3 AmbientLight( const float3 worldNormal )
  602. {
  603. float3 nSquared = worldNormal * worldNormal;
  604. int3 isNegative = ( worldNormal < 0.0 );
  605. float3 linearColor;
  606. linearColor = nSquared.x * cAmbientCubeX[isNegative.x] +
  607. nSquared.y * cAmbientCubeY[isNegative.y] +
  608. nSquared.z * cAmbientCubeZ[isNegative.z];
  609. return linearColor;
  610. }
  611. // The following "internal" routines are called "privately" by other routines in this file which
  612. // handle the particular flavor of vs20 control flow appropriate to the original caller
  613. float VertexAttenInternal( const float3 worldPos, int lightNum )
  614. {
  615. float result = 0.0f;
  616. // Get light direction
  617. float3 lightDir = cLightInfo[lightNum].pos.xyz - worldPos;
  618. // Get light distance squared.
  619. float lightDistSquared = dot( lightDir, lightDir );
  620. // Get 1/lightDistance
  621. float ooLightDist = rsqrt( lightDistSquared );
  622. // Normalize light direction
  623. lightDir *= ooLightDist;
  624. float3 vDist;
  625. #if ( defined( _X360 ) || defined( _PS3 ) )
  626. {
  627. //X360 dynamic compile hits an internal compiler error using dst(), this is the breakdown of how dst() works from the 360 docs.
  628. vDist.x = 1;
  629. vDist.y = lightDistSquared * ooLightDist;
  630. vDist.z = lightDistSquared;
  631. //flDist.w = ooLightDist;
  632. }
  633. #else
  634. {
  635. vDist = dst( lightDistSquared, ooLightDist ).xyz;
  636. }
  637. #endif
  638. float flDistanceAtten = 1.0f / dot( cLightInfo[lightNum].atten.xyz, vDist );
  639. // Spot attenuation
  640. float flCosTheta = dot( cLightInfo[lightNum].dir.xyz, -lightDir );
  641. float flSpotAtten = (flCosTheta - cLightInfo[lightNum].spotParams.z) * cLightInfo[lightNum].spotParams.w;
  642. flSpotAtten = max( 0.0001f, flSpotAtten );
  643. flSpotAtten = pow( flSpotAtten, cLightInfo[lightNum].spotParams.x );
  644. flSpotAtten = saturate( flSpotAtten );
  645. // Select between point and spot
  646. float flAtten = lerp( flDistanceAtten, flDistanceAtten * flSpotAtten, cLightInfo[lightNum].dir.w );
  647. // Select between above and directional (no attenuation)
  648. result = lerp( flAtten, 1.0f, cLightInfo[lightNum].color.w );
  649. return result;
  650. }
  651. float CosineTermInternal( const float3 worldPos, const float3 worldNormal, int lightNum, bool bHalfLambert )
  652. {
  653. // Calculate light direction assuming this is a point or spot
  654. float3 lightDir = normalize( cLightInfo[lightNum].pos.xyz - worldPos );
  655. // Select the above direction or the one in the structure, based upon light type
  656. lightDir = lerp( lightDir, -cLightInfo[lightNum].dir.xyz, cLightInfo[lightNum].color.w );
  657. // compute N dot L
  658. float NDotL = dot( worldNormal, lightDir );
  659. if ( !bHalfLambert )
  660. {
  661. NDotL = max( 0.0f, NDotL );
  662. NDotL = SoftenCosineTerm( NDotL ); // For CS:GO
  663. }
  664. else // Half-Lambert
  665. {
  666. NDotL = NDotL * 0.5 + 0.5;
  667. NDotL = NDotL * NDotL;
  668. }
  669. return NDotL;
  670. }
  671. // This routine uses booleans to do early-outs and is meant to be called by routines OUTSIDE of this file
  672. float GetVertexAttenForLight( const float3 worldPos, int lightNum, bool bUseStaticControlFlow = true )
  673. {
  674. float result = 0.0f;
  675. // Direct3D uses static control flow but OpenGL currently does not
  676. if ( bUseStaticControlFlow )
  677. {
  678. if ( g_bLightEnabled[lightNum] )
  679. {
  680. result = VertexAttenInternal( worldPos, lightNum );
  681. }
  682. }
  683. else // OpenGL non-static-control-flow path
  684. {
  685. result = VertexAttenInternal( worldPos, lightNum );
  686. }
  687. return result;
  688. }
  689. // This routine uses booleans to do early-outs and is meant to be called by routines OUTSIDE of this file
  690. float CosineTerm( const float3 worldPos, const float3 worldNormal, int lightNum, bool bHalfLambert )
  691. {
  692. float flResult = 0.0f;
  693. if ( g_bLightEnabled[lightNum] )
  694. {
  695. flResult = CosineTermInternal( worldPos, worldNormal, lightNum, bHalfLambert );
  696. }
  697. return flResult;
  698. }
  699. float3 DoLightInternal( const float3 worldPos, const float3 worldNormal, int lightNum, bool bHalfLambert )
  700. {
  701. return cLightInfo[lightNum].color.xyz *
  702. CosineTermInternal( worldPos, worldNormal, lightNum, bHalfLambert ) *
  703. VertexAttenInternal( worldPos, lightNum );
  704. }
  705. float3 DoLighting( const float3 worldPos, const float3 worldNormal,
  706. const float3 staticLightingColor, const bool bStaticLight,
  707. const bool bDynamicLight, bool bHalfLambert )
  708. {
  709. float3 linearColor = float3( 0.0f, 0.0f, 0.0f );
  710. if( bStaticLight ) // Static light
  711. {
  712. linearColor += GammaToLinear( staticLightingColor.rgb * cOverbright );
  713. }
  714. if( bDynamicLight ) // Dynamic light
  715. {
  716. #ifdef _PS3
  717. // no integer constant support :(
  718. if ( g_bLightEnabled[0] )
  719. {
  720. linearColor += DoLightInternal( worldPos, worldNormal, 0, bHalfLambert );
  721. if ( g_bLightEnabled[1] )
  722. {
  723. linearColor += DoLightInternal( worldPos, worldNormal, 1, bHalfLambert );
  724. }
  725. }
  726. #else // _PS3
  727. for (int i = 0; i < g_nLightCount; i++)
  728. {
  729. linearColor += DoLightInternal( worldPos, worldNormal, i, bHalfLambert );
  730. }
  731. #endif // !_PS3
  732. }
  733. if( bDynamicLight )
  734. {
  735. linearColor += AmbientLight( worldNormal ); //ambient light is already remapped
  736. }
  737. return linearColor;
  738. }
  739. float3 DoLightingSeparateDirectional( const float3 worldPos, const float3 worldNormal,
  740. const float3 staticLightingColor, const bool bStaticLight,
  741. const bool bDynamicLight, bool bHalfLambert,
  742. out float3 directionalLightColor )
  743. {
  744. directionalLightColor = float3( 0.0f, 0.0f, 0.0f );
  745. float3 linearColor = float3( 0.0f, 0.0f, 0.0f );
  746. if( bStaticLight ) // Static light
  747. {
  748. linearColor += GammaToLinear( staticLightingColor.rgb * cOverbright );
  749. }
  750. if( bDynamicLight ) // Dynamic light
  751. {
  752. #if defined(_PS3)
  753. // no integer constant support :(
  754. if ( g_bLightEnabled[0] )
  755. {
  756. directionalLightColor += DoLightInternal( worldPos, worldNormal, 0, bHalfLambert );
  757. if ( g_bLightEnabled[1] )
  758. {
  759. linearColor += DoLightInternal( worldPos, worldNormal, 1, bHalfLambert );
  760. }
  761. }
  762. #else
  763. for (int i = 0; i < g_nLightCount; i++)
  764. {
  765. float3 contribColor = DoLightInternal( worldPos, worldNormal, i, bHalfLambert );
  766. if ( !i )
  767. {
  768. directionalLightColor += contribColor;
  769. }
  770. else
  771. {
  772. linearColor += contribColor;
  773. }
  774. }
  775. #endif
  776. }
  777. if( bDynamicLight )
  778. {
  779. linearColor += AmbientLight( worldNormal ); //ambient light is already remapped
  780. }
  781. return linearColor;
  782. }
  783. float3 DoLightingUnrolled( const float3 worldPos, const float3 worldNormal,
  784. const float3 staticLightingColor, const bool bStaticLight,
  785. const bool bDynamicLight, bool bHalfLambert, const int nNumLights )
  786. {
  787. float3 linearColor = float3( 0.0f, 0.0f, 0.0f );
  788. if( bStaticLight ) // Static light
  789. {
  790. linearColor += GammaToLinear( staticLightingColor * cOverbright );
  791. }
  792. if( bDynamicLight ) // Ambient light
  793. {
  794. if ( nNumLights >= 1 )
  795. linearColor += DoLightInternal( worldPos, worldNormal, 0, bHalfLambert );
  796. if ( nNumLights >= 2 )
  797. linearColor += DoLightInternal( worldPos, worldNormal, 1, bHalfLambert );
  798. if ( nNumLights >= 3 )
  799. linearColor += DoLightInternal( worldPos, worldNormal, 2, bHalfLambert );
  800. if ( nNumLights >= 4 )
  801. linearColor += DoLightInternal( worldPos, worldNormal, 3, bHalfLambert );
  802. }
  803. if( bDynamicLight )
  804. {
  805. linearColor += AmbientLight( worldNormal ); //ambient light is already remapped
  806. }
  807. return linearColor;
  808. }
  809. int4 FloatToInt( in float4 floats )
  810. {
  811. return D3DCOLORtoUBYTE4( floats.zyxw / 255.001953125 );
  812. }
  813. float2 ComputeSphereMapTexCoords( in float3 reflectionVector )
  814. {
  815. // transform reflection vector into view space
  816. reflectionVector = mul( reflectionVector, ( float3x3 )cViewModel );
  817. // generate <rx ry rz+1>
  818. float3 tmp = float3( reflectionVector.x, reflectionVector.y, reflectionVector.z + 1.0f );
  819. // find 1 / len
  820. float ooLen = dot( tmp, tmp );
  821. ooLen = 1.0f / sqrt( ooLen );
  822. // tmp = tmp/|tmp| + 1
  823. tmp.xy = ooLen * tmp.xy + 1.0f;
  824. return tmp.xy * 0.5f;
  825. }
  826. #define DEFORMATION_CLAMP_TO_BOX_IN_WORLDSPACE 1
  827. // minxyz.minsoftness / maxxyz.maxsoftness
  828. float3 ApplyDeformation( float3 worldpos, int deftype, float4 defparms0, float4 defparms1,
  829. float4 defparms2, float4 defparms3 )
  830. {
  831. float3 ret = worldpos;
  832. if ( deftype == DEFORMATION_CLAMP_TO_BOX_IN_WORLDSPACE )
  833. {
  834. ret=max( ret, defparms2.xyz );
  835. ret=min( ret, defparms3.xyz );
  836. }
  837. return ret;
  838. }
  839. #endif //#ifndef COMMON_VS_FXC_H_