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.

540 lines
20 KiB

  1. //========= Copyright (c) Valve Corporation, All rights reserved. ============//
  2. #include "BaseVSShader.h"
  3. #include "customweapon_dx9_helper.h"
  4. #include "cpp_shader_constant_register_map.h"
  5. // Auto generated inc files
  6. #include "customweapon_vs20.inc"
  7. #include "customweapon_vs30.inc"
  8. #include "customweapon_ps20b.inc"
  9. #include "customweapon_ps30.inc"
  10. #include "common_hlsl_cpp_consts.h"
  11. #include "shaderlib/commandbuilder.h"
  12. void InitParamsCustomWeapon( CBaseVSShader *pShader, IMaterialVar** params, const char *pMaterialName, CustomWeaponVars_t &info )
  13. {
  14. static ConVarRef gpu_level( "gpu_level" );
  15. int nGPULevel = gpu_level.GetInt();
  16. SET_PARAM_VEC_IF_NOT_DEFINED( info.m_nCamoColor0, kDefaultCamoColor0, 3 );
  17. SET_PARAM_VEC_IF_NOT_DEFINED( info.m_nCamoColor1, kDefaultCamoColor1, 3 );
  18. SET_PARAM_VEC_IF_NOT_DEFINED( info.m_nCamoColor2, kDefaultCamoColor2, 3 );
  19. SET_PARAM_VEC_IF_NOT_DEFINED( info.m_nCamoColor3, kDefaultCamoColor3, 3 );
  20. SET_PARAM_INT_IF_NOT_DEFINED( info.m_nBaseDiffuseOverride, kDefaultBaseDiffuseOverride );
  21. SET_PARAM_INT_IF_NOT_DEFINED( info.m_nPhongExponent, kDefaultPhongExponent );
  22. SET_PARAM_INT_IF_NOT_DEFINED( info.m_nPhongIntensity, kDefaultPhongIntensity );
  23. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nWearProgress, kDefaultWearProgress );
  24. SET_PARAM_INT_IF_NOT_DEFINED( info.m_nPaintStyle, kDefaultPaintStyle );
  25. SET_PARAM_INT_IF_NOT_DEFINED( info.m_nExponentMode, kDefaultExponentMode );
  26. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nPhongAlbedoFactor, kDefaultPhongAlbedoFactor );
  27. SET_PARAM_INT_IF_NOT_DEFINED( info.m_nCheap, nGPULevel < 2 );
  28. SET_PARAM_INT_IF_NOT_DEFINED( info.m_nPreview, 0 );
  29. SET_PARAM_VEC_IF_NOT_DEFINED( info.m_nPreviewPhongFresnelRanges, kDefaultPreviewPhongFresnelRanges, 3 );
  30. SET_PARAM_INT_IF_NOT_DEFINED( info.m_nPreviewPhongAlbedoTint, 0 );
  31. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nPhongAlbedoBoost, -1 );
  32. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nPreviewPhongBoost, kDefaultPreviewPhongBoost );
  33. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nPreviewPaintPhongAlbedoBoost, kDefaultPreviewPhongBoost );
  34. SET_PARAM_INT_IF_NOT_DEFINED( info.m_nPreviewIgnoreWeaponScale, 0 );
  35. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nPreviewWeaponObjScale, kDefaultWeaponScale );
  36. SET_PARAM_FLOAT_IF_NOT_DEFINED( info.m_nPreviewWeaponUVScale, kDefaultWeaponScale );
  37. if ( ( g_pHardwareConfig->SupportsPixelShaders_3_0() ) && ( info.m_nPreview != -1 ) && ( params[info.m_nPreview]->GetIntValue() == 1 ) )
  38. {
  39. SET_FLAGS2( MATERIAL_VAR2_SUPPORTS_HW_SKINNING );
  40. SET_FLAGS2( MATERIAL_VAR2_LIGHTING_VERTEX_LIT );
  41. }
  42. }
  43. void InitCustomWeapon( CBaseVSShader *pShader, IMaterialVar** params, CustomWeaponVars_t &info )
  44. {
  45. // Load textures
  46. if ( ( info.m_nBaseTexture != -1 ) && params[info.m_nBaseTexture]->IsDefined() )
  47. {
  48. pShader->LoadTexture( info.m_nBaseTexture );
  49. }
  50. if ( ( info.m_nSurfaceTexture != -1 ) && params[info.m_nSurfaceTexture]->IsDefined() )
  51. {
  52. pShader->LoadTexture( info.m_nSurfaceTexture );
  53. }
  54. if ( ( info.m_nAOTexture != -1 ) && params[info.m_nAOTexture]->IsDefined() )
  55. {
  56. pShader->LoadTexture( info.m_nAOTexture );
  57. }
  58. if ( ( info.m_nMasksTexture != -1 ) && params[info.m_nMasksTexture]->IsDefined() )
  59. {
  60. pShader->LoadTexture( info.m_nMasksTexture );
  61. }
  62. if ( ( info.m_nPosTexture != -1 ) && params[info.m_nPosTexture]->IsDefined() )
  63. {
  64. pShader->LoadTexture( info.m_nPosTexture );
  65. }
  66. if ( ( info.m_nGrungeTexture != -1 ) && params[info.m_nGrungeTexture]->IsDefined() )
  67. {
  68. pShader->LoadTexture( info.m_nGrungeTexture );
  69. }
  70. if ( ( info.m_nWearTexture != -1 ) && params[info.m_nWearTexture]->IsDefined() )
  71. {
  72. pShader->LoadTexture( info.m_nWearTexture );
  73. }
  74. if ( ( info.m_nPaintTexture != -1 ) && params[info.m_nPaintTexture]->IsDefined() )
  75. {
  76. pShader->LoadTexture( info.m_nPaintTexture );
  77. }
  78. if ( ( info.m_nExpTexture != -1 ) && params[info.m_nExpTexture]->IsDefined() )
  79. {
  80. pShader->LoadTexture( info.m_nExpTexture );
  81. }
  82. }
  83. class CCustomWeapon_DX9_Context : public CBasePerMaterialContextData
  84. {
  85. public:
  86. CCommandBufferBuilder< CFixedCommandStorageBuffer< 800 > > m_SemiStaticCmdsOut;
  87. };
  88. void DrawCustomWeapon( CBaseVSShader *pShader, IMaterialVar** params, IShaderDynamicAPI *pShaderAPI,
  89. IShaderShadow* pShaderShadow, CustomWeaponVars_t &info, VertexCompressionType_t vertexCompression,
  90. CBasePerMaterialContextData **pContextDataPtr )
  91. {
  92. CCustomWeapon_DX9_Context *pContextData = reinterpret_cast< CCustomWeapon_DX9_Context *> ( *pContextDataPtr );
  93. bool bSurfaceTexture = ( info.m_nSurfaceTexture != -1 ) && params[info.m_nSurfaceTexture]->IsTexture();
  94. bool bAOTexture = ( info.m_nAOTexture != -1 ) && params[info.m_nAOTexture]->IsTexture();
  95. bool bMasksTexture = ( info.m_nMasksTexture != -1 ) && params[info.m_nMasksTexture]->IsTexture();
  96. bool bPosTexture = ( info.m_nPosTexture != -1 ) && params[info.m_nPosTexture]->IsTexture();
  97. bool bGrungeTexture = ( info.m_nGrungeTexture != -1 ) && params[info.m_nGrungeTexture]->IsTexture();
  98. bool bWearTexture = ( info.m_nWearTexture != -1 ) && params[info.m_nWearTexture]->IsTexture();
  99. bool bPaintTexture = ( info.m_nPaintTexture != -1 ) && params[info.m_nPaintTexture]->IsTexture();
  100. bool bExpTexture = ( info.m_nExpTexture != -1 ) && params[info.m_nExpTexture]->IsTexture();
  101. bool bBaseTexture = ( info.m_nBaseTexture != -1 ) && params[info.m_nBaseTexture]->IsTexture();
  102. bool bPreview = ( g_pHardwareConfig->SupportsPixelShaders_3_0() ) && ( info.m_nPreview != -1 ) && ( params[info.m_nPreview]->GetIntValue() == 1 );
  103. bool bPreviewPhongAlbedoTint = bPreview && ( info.m_nPreviewPhongAlbedoTint != -1 ) && ( params[info.m_nPreviewPhongAlbedoTint]->GetIntValue() == 1 );
  104. bExpTexture = bExpTexture || bPreview;
  105. float fPhongAlbedoFactor;
  106. if ( bPreview )
  107. {
  108. float fOrigPhongAlbedoBoost, fOrigPhongBoost, fPaintPhongAlbedoBoost;
  109. fOrigPhongAlbedoBoost = params[info.m_nPhongAlbedoBoost]->GetFloatValue();
  110. fOrigPhongBoost = params[info.m_nPreviewPhongBoost]->GetFloatValue();
  111. fPaintPhongAlbedoBoost = params[info.m_nPreviewPaintPhongAlbedoBoost]->GetFloatValue();
  112. fPhongAlbedoFactor = ( fOrigPhongAlbedoBoost < 0 ) ? ( ( fOrigPhongBoost < 0 ) ? 1.0f : fOrigPhongBoost ) : fOrigPhongAlbedoBoost;
  113. fPhongAlbedoFactor /= ( fPaintPhongAlbedoBoost <= 0 ) ? 1.0f : fPaintPhongAlbedoBoost;
  114. }
  115. else
  116. {
  117. fPhongAlbedoFactor = params[info.m_nPhongAlbedoFactor]->GetFloatValue();
  118. }
  119. int nPaintStyle = 0;
  120. if ( info.m_nPaintStyle != -1 )
  121. nPaintStyle = params[info.m_nPaintStyle]->GetIntValue();
  122. bool bExpMode = ( info.m_nExponentMode != -1 ) && ( params[info.m_nExponentMode]->GetIntValue() == 1);
  123. bool bCheapMode = ( params[info.m_nCheap]->GetIntValue() != 0 ) && ( ( nPaintStyle == 3 ) || ( nPaintStyle == 6 ) ) && ( !bExpMode );
  124. if ( ( pShader->IsSnapshotting() && pShaderShadow ) || (! pContextData ) || ( pContextData->m_bMaterialVarsChanged ) )
  125. {
  126. if ( pShader->IsSnapshotting() )
  127. {
  128. // Set stream format (note that this shader supports compression)
  129. int userDataSize = 0;
  130. unsigned int flags = VERTEX_POSITION | VERTEX_FORMAT_COMPRESSED;
  131. int nTexCoordCount = 1;
  132. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  133. // Vertex Shader
  134. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  135. {
  136. DECLARE_STATIC_VERTEX_SHADER( customweapon_vs30 );
  137. SET_STATIC_VERTEX_SHADER_COMBO( PREVIEW, bPreview );
  138. SET_STATIC_VERTEX_SHADER( customweapon_vs30 );
  139. }
  140. else
  141. {
  142. DECLARE_STATIC_VERTEX_SHADER( customweapon_vs20 );
  143. SET_STATIC_VERTEX_SHADER_COMBO( PREVIEW, 0 ); // can't use the preview shader in ps20b
  144. SET_STATIC_VERTEX_SHADER( customweapon_vs20 );
  145. }
  146. // Pixel Shader
  147. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  148. {
  149. DECLARE_STATIC_PIXEL_SHADER( customweapon_ps30 );
  150. SET_STATIC_PIXEL_SHADER_COMBO( PAINTSTYLE, nPaintStyle );
  151. SET_STATIC_PIXEL_SHADER_COMBO( EXPONENTMODE, bExpMode );
  152. SET_STATIC_PIXEL_SHADER_COMBO( CHEAPMODE, bCheapMode );
  153. SET_STATIC_PIXEL_SHADER_COMBO( PREVIEW, bPreview );
  154. SET_STATIC_PIXEL_SHADER_COMBO( PREVIEWPHONGALBEDOTINT, bPreview && bPreviewPhongAlbedoTint );
  155. SET_STATIC_PIXEL_SHADER_COMBO( PHONGALBEDOFACTORMODE, ( fPhongAlbedoFactor < 1.0f ) );
  156. SET_STATIC_PIXEL_SHADER( customweapon_ps30 );
  157. }
  158. else
  159. {
  160. DECLARE_STATIC_PIXEL_SHADER( customweapon_ps20b );
  161. SET_STATIC_PIXEL_SHADER_COMBO( PAINTSTYLE, nPaintStyle );
  162. SET_STATIC_PIXEL_SHADER_COMBO( EXPONENTMODE, bExpMode );
  163. SET_STATIC_PIXEL_SHADER_COMBO( CHEAPMODE, bCheapMode );
  164. SET_STATIC_PIXEL_SHADER_COMBO( PHONGALBEDOFACTORMODE, ( fPhongAlbedoFactor < 1.0f ) );
  165. SET_STATIC_PIXEL_SHADER( customweapon_ps20b );
  166. }
  167. // Textures
  168. if( bAOTexture )
  169. {
  170. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true ); // [sRGB] Ambient Occlusion
  171. }
  172. if( bWearTexture )
  173. {
  174. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true ); // Scratches
  175. }
  176. if( bExpTexture )
  177. {
  178. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true ); // Exponent
  179. }
  180. if ( bBaseTexture )
  181. {
  182. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true ); // [sRGB] Base
  183. }
  184. if( bMasksTexture )
  185. {
  186. pShaderShadow->EnableTexture( SHADER_SAMPLER4, true ); // Masks
  187. }
  188. if( bGrungeTexture )
  189. {
  190. pShaderShadow->EnableTexture( SHADER_SAMPLER5, true ); // [sRGB] Grunge
  191. }
  192. if( bSurfaceTexture )
  193. {
  194. pShaderShadow->EnableTexture( SHADER_SAMPLER6, true ); // Obj-space normal and cavity
  195. }
  196. if( bPosTexture )
  197. {
  198. pShaderShadow->EnableTexture( SHADER_SAMPLER7, true ); // High-precision Position
  199. }
  200. if( bPaintTexture )
  201. {
  202. pShaderShadow->EnableTexture( SHADER_SAMPLER8, true ); // Paint
  203. }
  204. pShaderShadow->EnableAlphaWrites( true );
  205. bool bSRGBWrite = ( bPreview || !bExpMode );
  206. pShaderShadow->EnableSRGBWrite( bSRGBWrite );
  207. if ( bPreview )
  208. {
  209. pShader->PI_BeginCommandBuffer();
  210. pShader->PI_SetPixelShaderAmbientLightCube( PSREG_AMBIENT_CUBE );
  211. pShader->PI_SetPixelShaderLocalLighting( PSREG_LIGHT_INFO_ARRAY );
  212. pShader->PI_EndCommandBuffer();
  213. }
  214. }
  215. if ( pShaderAPI && ( (! pContextData ) || ( pContextData->m_bMaterialVarsChanged ) ) )
  216. {
  217. if ( !pContextData ) // make sure allocated
  218. {
  219. pContextData = new CCustomWeapon_DX9_Context;
  220. *pContextDataPtr = pContextData;
  221. }
  222. pContextData->m_bMaterialVarsChanged = false;
  223. pContextData->m_SemiStaticCmdsOut.Reset();
  224. ///////////////////////////
  225. // Semi-static block
  226. ///////////////////////////
  227. // VS Constants
  228. if ( IS_PARAM_DEFINED( info.m_nPatternTextureTransform ) )
  229. {
  230. if ( params[info.m_nPreviewIgnoreWeaponScale]->GetIntValue() == 1 )
  231. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nPatternTextureTransform );
  232. else
  233. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nPatternTextureTransform, info.m_nPreviewWeaponUVScale );
  234. }
  235. else
  236. {
  237. if ( params[info.m_nPreviewIgnoreWeaponScale]->GetIntValue() == 1 )
  238. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nPatternTextureTransform );
  239. else
  240. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureScaledTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_0, info.m_nBaseTextureTransform, info.m_nPreviewWeaponUVScale );
  241. }
  242. if ( IS_PARAM_DEFINED( info.m_nWearTextureTransform ) )
  243. {
  244. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, info.m_nWearTextureTransform );
  245. }
  246. else
  247. {
  248. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_2, info.m_nBaseTextureTransform );
  249. }
  250. if ( IS_PARAM_DEFINED( info.m_nGrungeTextureTransform ) )
  251. {
  252. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, info.m_nGrungeTextureTransform );
  253. }
  254. else
  255. {
  256. pContextData->m_SemiStaticCmdsOut.SetVertexShaderTextureTransform( VERTEX_SHADER_SHADER_SPECIFIC_CONST_4, info.m_nBaseTextureTransform );
  257. }
  258. // PS Constants
  259. // AO
  260. if( bAOTexture )
  261. {
  262. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER0, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nAOTexture, -1 );
  263. }
  264. // Scratches
  265. if( bWearTexture )
  266. {
  267. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, info.m_nWearTexture, -1 );
  268. }
  269. // Exponent
  270. if( bExpTexture )
  271. {
  272. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER2, TEXTURE_BINDFLAGS_NONE, info.m_nExpTexture, -1 );
  273. }
  274. // Base
  275. if ( bBaseTexture )
  276. {
  277. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER3, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nBaseTexture, -1 );
  278. }
  279. // Masks
  280. if( bMasksTexture )
  281. {
  282. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER4, TEXTURE_BINDFLAGS_NONE, info.m_nMasksTexture, -1 );
  283. }
  284. //Grunge
  285. if( bGrungeTexture )
  286. {
  287. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER5, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nGrungeTexture, -1 );
  288. }
  289. // Object Space Normals
  290. if( bSurfaceTexture )
  291. {
  292. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER6, TEXTURE_BINDFLAGS_NONE, info.m_nSurfaceTexture, -1 );
  293. }
  294. // Object Space Position
  295. if( bPosTexture )
  296. {
  297. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER7, TEXTURE_BINDFLAGS_NONE, info.m_nPosTexture, -1 );
  298. }
  299. // Paint pattern
  300. if( bPaintTexture )
  301. {
  302. if ( ( nPaintStyle == 7 ) || ( nPaintStyle == 8 ) || ( nPaintStyle == 9 ) )
  303. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER8, TEXTURE_BINDFLAGS_SRGBREAD, info.m_nPaintTexture, -1 );
  304. else
  305. pContextData->m_SemiStaticCmdsOut.BindTexture( pShader, SHADER_SAMPLER8, TEXTURE_BINDFLAGS_NONE, info.m_nPaintTexture, -1 );
  306. }
  307. if ( bPreview )
  308. {
  309. pContextData->m_SemiStaticCmdsOut.BindStandardTexture( SHADER_SAMPLER9, TEXTURE_BINDFLAGS_NONE, TEXTURE_NORMALIZATION_CUBEMAP_SIGNED );
  310. }
  311. float fvCamoConst[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  312. float fvCamo3[3] = { 0.0f, 0.0f, 0.0f };
  313. params[info.m_nCamoColor3]->GetVecValue( fvCamo3, 3 );
  314. if ( bPreview )
  315. {
  316. fvCamo3[0] /= 255.0;
  317. fvCamo3[1] /= 255.0;
  318. fvCamo3[2] /= 255.0;
  319. }
  320. fvCamo3[0] = SrgbGammaToLinear( fvCamo3[0] );
  321. fvCamo3[1] = SrgbGammaToLinear( fvCamo3[1] );
  322. fvCamo3[2] = SrgbGammaToLinear( fvCamo3[2] );
  323. params[info.m_nCamoColor0]->GetVecValue( fvCamoConst, 3 );
  324. if ( bPreview )
  325. {
  326. fvCamoConst[0] /= 255.0;
  327. fvCamoConst[1] /= 255.0;
  328. fvCamoConst[2] /= 255.0;
  329. }
  330. fvCamoConst[0] = SrgbGammaToLinear( fvCamoConst[0] );
  331. fvCamoConst[1] = SrgbGammaToLinear( fvCamoConst[1] );
  332. fvCamoConst[2] = SrgbGammaToLinear( fvCamoConst[2] );
  333. fvCamoConst[3] = fvCamo3[0];
  334. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 0, fvCamoConst, 1 );
  335. params[info.m_nCamoColor1]->GetVecValue( fvCamoConst, 3 );
  336. if ( bPreview )
  337. {
  338. fvCamoConst[0] /= 255.0;
  339. fvCamoConst[1] /= 255.0;
  340. fvCamoConst[2] /= 255.0;
  341. }
  342. fvCamoConst[0] = SrgbGammaToLinear( fvCamoConst[0] );
  343. fvCamoConst[1] = SrgbGammaToLinear( fvCamoConst[1] );
  344. fvCamoConst[2] = SrgbGammaToLinear( fvCamoConst[2] );
  345. fvCamoConst[3] = fvCamo3[1];
  346. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 1, fvCamoConst, 1 );
  347. params[info.m_nCamoColor2]->GetVecValue( fvCamoConst, 3 );
  348. if ( bPreview )
  349. {
  350. fvCamoConst[0] /= 255.0;
  351. fvCamoConst[1] /= 255.0;
  352. fvCamoConst[2] /= 255.0;
  353. }
  354. fvCamoConst[0] = SrgbGammaToLinear( fvCamoConst[0] );
  355. fvCamoConst[1] = SrgbGammaToLinear( fvCamoConst[1] );
  356. fvCamoConst[2] = SrgbGammaToLinear( fvCamoConst[2] );
  357. fvCamoConst[3] = fvCamo3[2];
  358. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 2, fvCamoConst, 1 );
  359. float fvPhongFactors[4] = { kDefaultPhongAlbedoFactor, kDefaultPhongExponent, kDefaultPhongIntensity, 0 };
  360. fvPhongFactors[0] = ( fPhongAlbedoFactor > 1 ) ? 1.0/fPhongAlbedoFactor : fPhongAlbedoFactor;
  361. fvPhongFactors[1] = params[info.m_nPhongExponent]->GetFloatValue();
  362. if ( bPreview )
  363. fvPhongFactors[1] /= 255.0;
  364. fvPhongFactors[2] = params[info.m_nPhongIntensity]->GetFloatValue();
  365. if ( bPreview )
  366. {
  367. float fPhongBoost = params[info.m_nPreviewPhongBoost]->GetFloatValue();
  368. if ( ( nPaintStyle == 1 ) || ( nPaintStyle == 2 ) || ( nPaintStyle == 3 ) || ( nPaintStyle == 7 ) || ( nPaintStyle == 8 ) )
  369. {
  370. fvPhongFactors[2] = ( fPhongBoost < 0 ) ? fvPhongFactors[2] : fvPhongFactors[2] / fPhongBoost;
  371. }
  372. }
  373. fvPhongFactors[3] = params[info.m_nWearProgress]->GetFloatValue();
  374. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 3, fvPhongFactors, 1 );
  375. if ( ( nPaintStyle == 3 ) || ( nPaintStyle == 6 ) )
  376. {
  377. if ( IS_PARAM_DEFINED( info.m_nPatternTextureTransform ) )
  378. {
  379. pContextData->m_SemiStaticCmdsOut.SetPixelShaderTextureTransform( 10, info.m_nPatternTextureTransform );
  380. }
  381. else
  382. {
  383. pContextData->m_SemiStaticCmdsOut.SetPixelShaderTextureTransform( 10, info.m_nBaseTextureTransform );
  384. }
  385. }
  386. if ( bPreview )
  387. {
  388. float fvPhongConst[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  389. params[info.m_nPreviewPhongFresnelRanges]->GetVecValue( fvPhongConst, 3 );
  390. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 13, fvPhongConst, 1 );
  391. float fOrigPhongAlbedoBoost, fOrigPhongBoost, fPaintPhongAlbedoBoost;
  392. fOrigPhongAlbedoBoost = params[info.m_nPhongAlbedoBoost]->GetFloatValue();
  393. fOrigPhongBoost = params[info.m_nPreviewPhongBoost]->GetFloatValue();
  394. fPaintPhongAlbedoBoost = params[info.m_nPreviewPaintPhongAlbedoBoost]->GetFloatValue();
  395. float fPhongAlbedoBoost = ( fOrigPhongAlbedoBoost < 0 ) ? ( ( fOrigPhongBoost < 0 ) ? 1.0f : fOrigPhongBoost ) : fOrigPhongAlbedoBoost;
  396. fPhongAlbedoBoost = ( fPhongAlbedoBoost > fPaintPhongAlbedoBoost ) ? fPhongAlbedoBoost : fPaintPhongAlbedoBoost;
  397. float fvPreviewPhongBoosts[4] = { 1.0f, 1.0f, 1.0f, 1.0f };;
  398. fvPreviewPhongBoosts[0] = fPhongAlbedoBoost;
  399. fvPreviewPhongBoosts[1] = ( fOrigPhongBoost > 0 ) ? fOrigPhongBoost : 1.0f;
  400. float flWeaponObjScale = ( params[info.m_nPreviewIgnoreWeaponScale]->GetIntValue() == 1 ) ? 1.0f : params[info.m_nPreviewWeaponObjScale]->GetFloatValue();
  401. fvPreviewPhongBoosts[2] = flWeaponObjScale;
  402. pContextData->m_SemiStaticCmdsOut.SetPixelShaderConstant( 14, fvPreviewPhongBoosts, 1 );
  403. }
  404. pContextData->m_SemiStaticCmdsOut.End();
  405. }
  406. }
  407. if ( pShaderAPI ) //DYNAMIC_STATE
  408. {
  409. if ( IsPC() && pShaderAPI->InFlashlightMode() )
  410. {
  411. // Don't draw anything for the flashlight pass
  412. pShader->Draw( false );
  413. return;
  414. }
  415. #ifdef _PS3
  416. CCommandBufferBuilder< CDynamicCommandStorageBuffer > DynamicCmdsOut;
  417. ShaderApiFast( pShaderAPI )->ExecuteCommandBuffer( pContextData->m_SemiStaticCmdsOut.Base() );
  418. #else
  419. CCommandBufferBuilder< CFixedCommandStorageBuffer< 400 > > DynamicCmdsOut;
  420. DynamicCmdsOut.Call( pContextData->m_SemiStaticCmdsOut.Base() );
  421. #endif
  422. ///////////////////////////
  423. // dynamic block
  424. ///////////////////////////
  425. int numBones = ShaderApiFast( pShaderAPI )->GetCurrentNumBones();
  426. LightState_t lightState = { 0, false, false };
  427. ShaderApiFast( pShaderAPI )->GetDX9LightState( &lightState );
  428. // VERTEX SHADER SETUP
  429. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  430. {
  431. DECLARE_DYNAMIC_VERTEX_SHADER( customweapon_vs30 );
  432. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, ( numBones > 0 ) );
  433. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  434. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, bPreview ? lightState.m_nNumLights : 0 );
  435. SET_DYNAMIC_VERTEX_SHADER( customweapon_vs30 );
  436. }
  437. else
  438. {
  439. DECLARE_DYNAMIC_VERTEX_SHADER( customweapon_vs20 );
  440. SET_DYNAMIC_VERTEX_SHADER_COMBO( SKINNING, ( numBones > 0 ) );
  441. SET_DYNAMIC_VERTEX_SHADER_COMBO( COMPRESSED_VERTS, (int)vertexCompression );
  442. SET_DYNAMIC_VERTEX_SHADER_COMBO( NUM_LIGHTS, bPreview ? lightState.m_nNumLights : 0 );
  443. SET_DYNAMIC_VERTEX_SHADER( customweapon_vs20 );
  444. }
  445. // VS constants
  446. // PIXEL SHADER SETUP
  447. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  448. {
  449. DECLARE_DYNAMIC_PIXEL_SHADER( customweapon_ps30 );
  450. SET_DYNAMIC_PIXEL_SHADER_COMBO( NUM_LIGHTS, bPreview ? lightState.m_nNumLights : 0 );
  451. SET_DYNAMIC_PIXEL_SHADER( customweapon_ps30 );
  452. }
  453. else
  454. {
  455. DECLARE_DYNAMIC_PIXEL_SHADER( customweapon_ps20b );
  456. SET_DYNAMIC_PIXEL_SHADER( customweapon_ps20b );
  457. }
  458. if ( bPreview )
  459. {
  460. float vEyePos[4] = { 0, 0, 0, 0 };
  461. ShaderApiFast( pShaderAPI )->GetWorldSpaceCameraPosition( vEyePos );
  462. DynamicCmdsOut.SetPixelShaderConstant( 12, vEyePos, 1 );
  463. }
  464. DynamicCmdsOut.End();
  465. // end dynamic block
  466. pShaderAPI->ExecuteCommandBuffer( DynamicCmdsOut.Base() );
  467. }
  468. pShader->Draw();
  469. }