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.

145 lines
5.1 KiB

  1. //=========== Copyright � Valve Corporation, All rights reserved. ===========//
  2. //
  3. // Purpose: Create embroidery effect textures from arbitrary images
  4. //
  5. //===========================================================================//
  6. #include "BaseVSShader.h"
  7. #include "embroider_vs30.inc"
  8. #include "embroider_ps30.inc"
  9. #include "../materialsystem_global.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. BEGIN_VS_SHADER( Embroider, "Help for Embroider" )
  13. BEGIN_SHADER_PARAMS
  14. SHADER_PARAM( BASETEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "Original image" )
  15. SHADER_PARAM( STITCHTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "Stitching pattern with normal and alpha" )
  16. SHADER_PARAM( BACKINGTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "Backing pattern with normal and ao in alpha" )
  17. SHADER_PARAM( SAMPLEOFFSETTEXTURE, SHADER_PARAM_TYPE_TEXTURE, "", "Backing pattern offset for choosing stitch color" )
  18. SHADER_PARAM( SPHERENORMAL, SHADER_PARAM_TYPE_TEXTURE, "", "Spherical normal to aid in influencing anisotropic specular highlights in end-use render" )
  19. SHADER_PARAM( NCOLORS, SHADER_PARAM_TYPE_INTEGER, "10", "Number of colors for posterization" )
  20. SHADER_PARAM( COLORGAMMA, SHADER_PARAM_TYPE_FLOAT, "0.8", "Gamma for posterization" )
  21. SHADER_PARAM( TEXTUREMODE, SHADER_PARAM_TYPE_INTEGER, "1", "Multiple textures need to be generated for end-use render. Mode determines which one we're working on." )
  22. END_SHADER_PARAMS
  23. SHADER_INIT_PARAMS()
  24. {
  25. if ( !params[STITCHTEXTURE]->IsDefined() )
  26. {
  27. params[STITCHTEXTURE]->SetStringValue( "models/player/customization/source/stitch_normal" );
  28. }
  29. if ( !params[SPHERENORMAL]->IsDefined() )
  30. {
  31. params[SPHERENORMAL]->SetStringValue( "models/player/customization/source/spherical_normal" );
  32. }
  33. if ( !params[BACKINGTEXTURE]->IsDefined() )
  34. {
  35. params[BACKINGTEXTURE]->SetStringValue( "models/player/customization/source/backing_normal" );
  36. }
  37. if ( !params[SAMPLEOFFSETTEXTURE]->IsDefined() )
  38. {
  39. params[SAMPLEOFFSETTEXTURE]->SetStringValue( "models/player/customization/source/stitch_sample_offset" );
  40. }
  41. if ( !params[NCOLORS]->IsDefined() )
  42. {
  43. params[NCOLORS]->SetIntValue( 10 );
  44. }
  45. if ( !params[COLORGAMMA]->IsDefined() )
  46. {
  47. params[COLORGAMMA]->SetFloatValue( 0.8 );
  48. }
  49. if ( !params[TEXTUREMODE]->IsDefined() )
  50. {
  51. params[TEXTUREMODE]->SetIntValue( 0 );
  52. }
  53. }
  54. SHADER_FALLBACK
  55. {
  56. return 0;
  57. }
  58. SHADER_INIT
  59. {
  60. LoadTexture( BASETEXTURE, TEXTUREFLAGS_BORDER );
  61. LoadTexture( STITCHTEXTURE );
  62. LoadTexture( BACKINGTEXTURE );
  63. LoadTexture( SPHERENORMAL );
  64. LoadTexture( SAMPLEOFFSETTEXTURE );
  65. }
  66. SHADER_DRAW
  67. {
  68. SHADOW_STATE
  69. {
  70. // Set stream format (note that this shader supports compression)
  71. int flags = VERTEX_POSITION | VERTEX_FORMAT_COMPRESSED;
  72. // NOTE: Have to say that we want 1 texcoord here even though we don't use it or you'll get this Warning in another part of the code:
  73. // "ERROR: shader asking for a too-narrow vertex format - you will see errors if running with debug D3D DLLs!\n\tPadding the vertex format with extra texcoords"
  74. int nTexCoordCount = 1;
  75. int userDataSize = 0;
  76. int mode = params[TEXTUREMODE]->GetIntValue();
  77. int palettize = params[NCOLORS]->GetIntValue() > 0;
  78. pShaderShadow->VertexShaderVertexFormat( flags, nTexCoordCount, NULL, userDataSize );
  79. // Vertex Shader
  80. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  81. {
  82. DECLARE_STATIC_VERTEX_SHADER( embroider_vs30 );
  83. SET_STATIC_VERTEX_SHADER( embroider_vs30 );
  84. }
  85. // Pixel Shader
  86. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  87. {
  88. DECLARE_STATIC_PIXEL_SHADER( embroider_ps30 );
  89. SET_STATIC_PIXEL_SHADER_COMBO( MODE, mode );
  90. SET_STATIC_PIXEL_SHADER_COMBO( PALETTIZE, palettize );
  91. SET_STATIC_PIXEL_SHADER( embroider_ps30 );
  92. }
  93. pShaderShadow->EnableTexture( SHADER_SAMPLER0, true );
  94. pShaderShadow->EnableTexture( SHADER_SAMPLER1, true );
  95. pShaderShadow->EnableTexture( SHADER_SAMPLER2, true );
  96. pShaderShadow->EnableTexture( SHADER_SAMPLER3, true );
  97. pShaderShadow->EnableSRGBWrite( false ); // diffuse writes srgb
  98. pShaderShadow->EnableAlphaWrites( ( mode != 2 ) ); // aniso and diffuse write alpha
  99. }
  100. DYNAMIC_STATE
  101. {
  102. BindTexture( SHADER_SAMPLER0, TEXTURE_BINDFLAGS_NONE, BASETEXTURE, -1 ); // NOT srgb! We are posterizing the color and assuming linear gives us better spread to the colors at the low end
  103. BindTexture( SHADER_SAMPLER1, TEXTURE_BINDFLAGS_NONE, STITCHTEXTURE, -1 );
  104. BindTexture( SHADER_SAMPLER2, TEXTURE_BINDFLAGS_NONE, BACKINGTEXTURE, -1 );
  105. BindTexture( SHADER_SAMPLER3, TEXTURE_BINDFLAGS_NONE, SPHERENORMAL, -1 );
  106. BindTexture( SHADER_SAMPLER4, TEXTURE_BINDFLAGS_NONE, SAMPLEOFFSETTEXTURE, -1 );
  107. SetPixelShaderConstant( 0, NCOLORS );
  108. SetPixelShaderConstant( 1, COLORGAMMA );
  109. // Vertex Shader
  110. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  111. {
  112. DECLARE_DYNAMIC_VERTEX_SHADER( embroider_vs30 );
  113. SET_DYNAMIC_VERTEX_SHADER( embroider_vs30 );
  114. }
  115. // Pixel Shader
  116. if ( g_pHardwareConfig->SupportsPixelShaders_3_0() )
  117. {
  118. DECLARE_DYNAMIC_PIXEL_SHADER( embroider_ps30 );
  119. SET_DYNAMIC_PIXEL_SHADER( embroider_ps30 );
  120. }
  121. }
  122. Draw();
  123. }
  124. END_SHADER