accuFeatureHLSL.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. false,
  39. meta,
  40. componentList );
  41. getOutObjToTangentSpace( componentList, meta, fd );
  42. addOutAccuVec( componentList, meta );
  43. output = meta;
  44. }
  45. void AccuTexFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  46. const MaterialFeatureData &fd )
  47. {
  48. MultiLine *meta = new MultiLine;
  49. output = meta;
  50. // OUT.col
  51. Var *color = (Var*) LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  52. if (!color)
  53. {
  54. output = new GenOp(" //NULL COLOR!");
  55. return;
  56. }
  57. // accu map
  58. Var *accuMap = new Var;
  59. accuMap->setType("SamplerState");
  60. accuMap->setName( "accuMap" );
  61. accuMap->uniform = true;
  62. accuMap->sampler = true;
  63. accuMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  64. // accuColor var
  65. Var *accuColor = new Var;
  66. accuColor->setType( "float4" );
  67. accuColor->setName( "accuColor" );
  68. LangElement *colorAccuDecl = new DecOp( accuColor );
  69. // plc (placement)
  70. Var *accuPlc = new Var;
  71. accuPlc->setType( "float4" );
  72. accuPlc->setName( "plc" );
  73. LangElement *plcAccu = new DecOp( accuPlc );
  74. // accu constants
  75. Var *accuScale = (Var*)LangElement::find( "accuScale" );
  76. if ( !accuScale )
  77. {
  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. {
  88. accuDirection = new Var;
  89. accuDirection->setType( "float" );
  90. accuDirection->setName( "accuDirection" );
  91. accuDirection->uniform = true;
  92. accuDirection->sampler = false;
  93. accuDirection->constSortPos = cspPotentialPrimitive;
  94. }
  95. Var *accuStrength = (Var*)LangElement::find( "accuStrength" );
  96. if ( !accuStrength )
  97. {
  98. accuStrength = new Var;
  99. accuStrength->setType( "float" );
  100. accuStrength->setName( "accuStrength" );
  101. accuStrength->uniform = true;
  102. accuStrength->sampler = false;
  103. accuStrength->constSortPos = cspPotentialPrimitive;
  104. }
  105. Var *accuCoverage = (Var*)LangElement::find( "accuCoverage" );
  106. if ( !accuCoverage )
  107. {
  108. accuCoverage = new Var;
  109. accuCoverage->setType( "float" );
  110. accuCoverage->setName( "accuCoverage" );
  111. accuCoverage->uniform = true;
  112. accuCoverage->sampler = false;
  113. accuCoverage->constSortPos = cspPotentialPrimitive;
  114. }
  115. Var *accuSpecular = (Var*)LangElement::find( "accuSpecular" );
  116. if ( !accuSpecular )
  117. {
  118. accuSpecular = new Var;
  119. accuSpecular->setType( "float" );
  120. accuSpecular->setName( "accuSpecular" );
  121. accuSpecular->uniform = true;
  122. accuSpecular->sampler = false;
  123. accuSpecular->constSortPos = cspPotentialPrimitive;
  124. }
  125. Var *inTex = getInTexCoord( "texCoord", "float2", componentList );
  126. Var *accuVec = getInTexCoord( "accuVec", "float3", componentList );
  127. Var *bumpNorm = (Var *)LangElement::find( "bumpSample" );
  128. if( bumpNorm == NULL )
  129. {
  130. bumpNorm = (Var *)LangElement::find( "bumpNormal" );
  131. if (!bumpNorm)
  132. return;
  133. }
  134. // get the accu pixel color
  135. Var *accuMapTex = new Var;
  136. accuMapTex->setType("Texture2D");
  137. accuMapTex->setName("accuMapTex");
  138. accuMapTex->uniform = true;
  139. accuMapTex->texture = true;
  140. accuMapTex->constNum = accuMap->constNum;
  141. meta->addStatement(new GenOp(" @ = @.Sample(@, @ * @);\r\n", colorAccuDecl, accuMapTex, accuMap, inTex, accuScale));
  142. // scale up normals
  143. meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 0.5;\r\n", bumpNorm, bumpNorm ) );
  144. // assign direction
  145. meta->addStatement( new GenOp( " @.z *= @*2.0;\r\n", accuVec, accuDirection ) );
  146. // saturate based on strength
  147. meta->addStatement( new GenOp( " @ = saturate( dot( @.xyz, @.xyz * pow(@, 5) ) );\r\n", plcAccu, bumpNorm, accuVec, accuStrength ) );
  148. // add coverage
  149. meta->addStatement( new GenOp( " @.a += (2 * pow(@/2, 5)) - 0.5;\r\n", accuPlc, accuCoverage ) );
  150. // clamp to a sensible value
  151. meta->addStatement( new GenOp( " @.a = clamp(@.a, 0, 1);\r\n", accuPlc, accuPlc ) );
  152. // light
  153. Var *lightColor = (Var*) LangElement::find( "d_lightcolor" );
  154. if(lightColor != NULL)
  155. meta->addStatement( new GenOp( " @ *= float4(@, 1.0);\r\n\r\n", accuColor, lightColor ) );
  156. // lerp with current pixel - use the accu alpha as well
  157. meta->addStatement( new GenOp( " @ = lerp( @, @, @.a * @.a);\r\n", color, color, accuColor, accuPlc, accuColor ) );
  158. // the result should always be opaque
  159. meta->addStatement( new GenOp( " @.a = 1.0;\r\n", color ) );
  160. }
  161. void AccuTexFeatHLSL::setTexData( Material::StageData &stageDat,
  162. const MaterialFeatureData &fd,
  163. RenderPassData &passData,
  164. U32 &texIndex )
  165. {
  166. //GFXTextureObject *tex = stageDat.getTex( MFT_AccuMap );
  167. //if ( tex )
  168. //{
  169. passData.mSamplerNames[ texIndex ] = "AccuMap";
  170. passData.mTexType[ texIndex++ ] = Material::AccuMap;
  171. //}
  172. }
  173. void AccuTexFeatHLSL::getAccuVec( MultiLine *meta, LangElement *accuVec )
  174. {
  175. // Get the transform to world space.
  176. Var *objTrans = (Var*)LangElement::find( "objTrans" );
  177. if ( !objTrans )
  178. {
  179. objTrans = new Var;
  180. objTrans->setType( "float4x4" );
  181. objTrans->setName( "objTrans" );
  182. objTrans->uniform = true;
  183. objTrans->constSortPos = cspPrimitive;
  184. }
  185. // accu obj trans
  186. Var *aobjTrans = new Var;
  187. aobjTrans->setType( "float4x4" );
  188. aobjTrans->setName( "accuObjTrans" );
  189. LangElement *accuObjTransDecl = new DecOp( aobjTrans );
  190. Var *outObjToTangentSpace = (Var*)LangElement::find( "objToTangentSpace" );
  191. Var *tav = new Var;
  192. tav->setType( "float4" );
  193. tav->setName( "tAccuVec" );
  194. LangElement *tavDecl = new DecOp( tav );
  195. meta->addStatement( new GenOp( " @ = float4(0,0,1,0);\r\n", tavDecl ) );
  196. meta->addStatement( new GenOp( " @ = transpose(@);\r\n", accuObjTransDecl, objTrans ) );
  197. meta->addStatement( new GenOp( " @ = mul(@, @);\r\n", tav, aobjTrans, tav ) );
  198. meta->addStatement( new GenOp( " @.xyz = mul(@, @.xyz);\r\n", tav, outObjToTangentSpace, tav ) );
  199. meta->addStatement( new GenOp( " @.y *= -1;\r\n", tav ) );
  200. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", accuVec, tav ) );
  201. }
  202. Var* AccuTexFeatHLSL::addOutAccuVec( Vector<ShaderComponent*> &componentList, MultiLine *meta )
  203. {
  204. Var *outAccuVec = (Var*)LangElement::find( "accuVec" );
  205. if ( !outAccuVec )
  206. {
  207. // Setup the connector.
  208. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  209. outAccuVec = connectComp->getElement( RT_TEXCOORD );
  210. outAccuVec->setName( "accuVec" );
  211. outAccuVec->setStructName( "OUT" );
  212. outAccuVec->setType( "float3" );
  213. getAccuVec( meta, outAccuVec );
  214. }
  215. return outAccuVec;
  216. }