accuFeatureHLSL.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "shaderGen/HLSL/accuFeatureHLSL.h"
  23. #include "shaderGen/shaderFeature.h"
  24. #include "shaderGen/shaderOp.h"
  25. #include "shaderGen/featureMgr.h"
  26. #include "materials/materialFeatureTypes.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "materials/processedMaterial.h"
  29. //****************************************************************************
  30. // Accu Texture
  31. //****************************************************************************
  32. void AccuTexFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  33. const MaterialFeatureData &fd )
  34. {
  35. MultiLine *meta = new MultiLine;
  36. getOutTexCoord( "texCoord",
  37. "float2",
  38. true,
  39. false,
  40. meta,
  41. componentList );
  42. getOutObjToTangentSpace( componentList, meta, fd );
  43. addOutAccuVec( componentList, meta );
  44. output = meta;
  45. }
  46. void AccuTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  47. const MaterialFeatureData &fd )
  48. {
  49. MultiLine *meta = new MultiLine;
  50. output = meta;
  51. // OUT.col
  52. Var *color = (Var*) LangElement::find( "col" );
  53. if (!color)
  54. {
  55. output = new GenOp(" //NULL COLOR!");
  56. return;
  57. }
  58. // accu map
  59. Var *accuMap = new Var;
  60. accuMap->setType( "sampler2D" );
  61. accuMap->setName( "accuMap" );
  62. accuMap->uniform = true;
  63. accuMap->sampler = true;
  64. accuMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  65. // accuColor var
  66. Var *accuColor = new Var;
  67. accuColor->setType( "float4" );
  68. accuColor->setName( "accuColor" );
  69. LangElement *colorAccuDecl = new DecOp( accuColor );
  70. // plc (placement)
  71. Var *accuPlc = new Var;
  72. accuPlc->setType( "float4" );
  73. accuPlc->setName( "plc" );
  74. LangElement *plcAccu = new DecOp( accuPlc );
  75. // accu constants
  76. Var *accuScale = (Var*)LangElement::find( "accuScale" );
  77. if ( !accuScale ) {
  78. accuScale = new Var;
  79. accuScale->setType( "float" );
  80. accuScale->setName( "accuScale" );
  81. accuScale->uniform = true;
  82. accuScale->sampler = false;
  83. accuScale->constSortPos = cspPotentialPrimitive;
  84. }
  85. Var *accuDirection = (Var*)LangElement::find( "accuDirection" );
  86. if ( !accuDirection ) {
  87. accuDirection = new Var;
  88. accuDirection->setType( "float" );
  89. accuDirection->setName( "accuDirection" );
  90. accuDirection->uniform = true;
  91. accuDirection->sampler = false;
  92. accuDirection->constSortPos = cspPotentialPrimitive;
  93. }
  94. Var *accuStrength = (Var*)LangElement::find( "accuStrength" );
  95. if ( !accuStrength ) {
  96. accuStrength = new Var;
  97. accuStrength->setType( "float" );
  98. accuStrength->setName( "accuStrength" );
  99. accuStrength->uniform = true;
  100. accuStrength->sampler = false;
  101. accuStrength->constSortPos = cspPotentialPrimitive;
  102. }
  103. Var *accuCoverage = (Var*)LangElement::find( "accuCoverage" );
  104. if ( !accuCoverage ) {
  105. accuCoverage = new Var;
  106. accuCoverage->setType( "float" );
  107. accuCoverage->setName( "accuCoverage" );
  108. accuCoverage->uniform = true;
  109. accuCoverage->sampler = false;
  110. accuCoverage->constSortPos = cspPotentialPrimitive;
  111. }
  112. Var *accuSpecular = (Var*)LangElement::find( "accuSpecular" );
  113. if ( !accuSpecular ) {
  114. accuSpecular = new Var;
  115. accuSpecular->setType( "float" );
  116. accuSpecular->setName( "accuSpecular" );
  117. accuSpecular->uniform = true;
  118. accuSpecular->sampler = false;
  119. accuSpecular->constSortPos = cspPotentialPrimitive;
  120. }
  121. Var *inTex = getInTexCoord( "texCoord", "float2", true, componentList );
  122. Var *accuVec = getInTexCoord( "accuVec", "float3", true, componentList );
  123. Var *bumpNorm = (Var *)LangElement::find( "bumpSample" );
  124. if( bumpNorm == NULL ) {
  125. bumpNorm = (Var *)LangElement::find( "bumpNormal" );
  126. if (!bumpNorm)
  127. return;
  128. }
  129. // get the accu pixel color
  130. meta->addStatement( new GenOp( " @ = tex2D(@, @ * @);\r\n", colorAccuDecl, accuMap, inTex, accuScale ) );
  131. if (!fd.features[MFT_Imposter])
  132. meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", accuColor, accuColor));
  133. // scale up normals
  134. meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 0.5;\r\n", bumpNorm, bumpNorm ) );
  135. // assign direction
  136. meta->addStatement( new GenOp( " @.z *= @*2.0;\r\n", accuVec, accuDirection ) );
  137. // saturate based on strength
  138. meta->addStatement( new GenOp( " @ = saturate( dot( @, @.xyz * pow(@, 5) ) );\r\n", plcAccu, bumpNorm, accuVec, accuStrength ) );
  139. // add coverage
  140. meta->addStatement( new GenOp( " @.a += (2 * pow(@/2, 5)) - 0.5;\r\n", accuPlc, accuCoverage ) );
  141. // clamp to a sensible value
  142. meta->addStatement( new GenOp( " @.a = clamp(@.a, 0, 1);\r\n", accuPlc, accuPlc ) );
  143. // light
  144. Var *lightColor = (Var*) LangElement::find( "d_lightcolor" );
  145. if(lightColor != NULL)
  146. meta->addStatement( new GenOp( " @ *= float4(@, 1.0);\r\n\r\n", accuColor, lightColor ) );
  147. // lerp with current pixel - use the accu alpha as well
  148. meta->addStatement( new GenOp( " @ = lerp( @, @, @.a * @.a);\r\n", color, color, accuColor, accuPlc, accuColor ) );
  149. // the result should always be opaque
  150. meta->addStatement( new GenOp( " @.a = 1.0;\r\n", color ) );
  151. }
  152. void AccuTexFeatHLSL::setTexData( Material::StageData &stageDat,
  153. const MaterialFeatureData &fd,
  154. RenderPassData &passData,
  155. U32 &texIndex )
  156. {
  157. //GFXTextureObject *tex = stageDat.getTex( MFT_AccuMap );
  158. //if ( tex )
  159. //{
  160. passData.mSamplerNames[ texIndex ] = "AccuMap";
  161. passData.mTexType[ texIndex++ ] = Material::AccuMap;
  162. //}
  163. }
  164. void AccuTexFeatHLSL::getAccuVec( MultiLine *meta, LangElement *accuVec )
  165. {
  166. // Get the transform to world space.
  167. Var *objTrans = (Var*)LangElement::find( "objTrans" );
  168. if ( !objTrans )
  169. {
  170. objTrans = new Var;
  171. objTrans->setType( "float4x4" );
  172. objTrans->setName( "objTrans" );
  173. objTrans->uniform = true;
  174. objTrans->constSortPos = cspPrimitive;
  175. }
  176. // accu obj trans
  177. Var *aobjTrans = new Var;
  178. aobjTrans->setType( "float4x4" );
  179. aobjTrans->setName( "accuObjTrans" );
  180. LangElement *accuObjTransDecl = new DecOp( aobjTrans );
  181. Var *outObjToTangentSpace = (Var*)LangElement::find( "objToTangentSpace" );
  182. Var *tav = new Var;
  183. tav->setType( "float4" );
  184. tav->setName( "tAccuVec" );
  185. LangElement *tavDecl = new DecOp( tav );
  186. meta->addStatement( new GenOp( " @ = float4(0,0,1,0);\r\n", tavDecl ) );
  187. meta->addStatement( new GenOp( " @ = transpose(@);\r\n", accuObjTransDecl, objTrans ) );
  188. meta->addStatement( new GenOp( " @ = mul(@, @);\r\n", tav, aobjTrans, tav ) );
  189. meta->addStatement( new GenOp( " @.xyz = mul(@, @.xyz);\r\n", tav, outObjToTangentSpace, tav ) );
  190. meta->addStatement( new GenOp( " @.y *= -1;\r\n", tav ) );
  191. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", accuVec, tav ) );
  192. }
  193. Var* AccuTexFeatHLSL::addOutAccuVec( Vector<ShaderComponent*> &componentList, MultiLine *meta )
  194. {
  195. Var *outAccuVec = (Var*)LangElement::find( "accuVec" );
  196. if ( !outAccuVec )
  197. {
  198. // Setup the connector.
  199. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  200. outAccuVec = connectComp->getElement( RT_TEXCOORD );
  201. outAccuVec->setName( "accuVec" );
  202. outAccuVec->setStructName( "OUT" );
  203. outAccuVec->setType( "float3" );
  204. outAccuVec->mapsToSampler = false;
  205. getAccuVec( meta, outAccuVec );
  206. }
  207. return outAccuVec;
  208. }