accuFeatureHLSL.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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( "col1" );
  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. {
  79. accuScale = new Var;
  80. accuScale->setType( "float" );
  81. accuScale->setName( "accuScale" );
  82. accuScale->uniform = true;
  83. accuScale->sampler = false;
  84. accuScale->constSortPos = cspPotentialPrimitive;
  85. }
  86. Var *accuDirection = (Var*)LangElement::find( "accuDirection" );
  87. if ( !accuDirection )
  88. {
  89. accuDirection = new Var;
  90. accuDirection->setType( "float" );
  91. accuDirection->setName( "accuDirection" );
  92. accuDirection->uniform = true;
  93. accuDirection->sampler = false;
  94. accuDirection->constSortPos = cspPotentialPrimitive;
  95. }
  96. Var *accuStrength = (Var*)LangElement::find( "accuStrength" );
  97. if ( !accuStrength )
  98. {
  99. accuStrength = new Var;
  100. accuStrength->setType( "float" );
  101. accuStrength->setName( "accuStrength" );
  102. accuStrength->uniform = true;
  103. accuStrength->sampler = false;
  104. accuStrength->constSortPos = cspPotentialPrimitive;
  105. }
  106. Var *accuCoverage = (Var*)LangElement::find( "accuCoverage" );
  107. if ( !accuCoverage )
  108. {
  109. accuCoverage = new Var;
  110. accuCoverage->setType( "float" );
  111. accuCoverage->setName( "accuCoverage" );
  112. accuCoverage->uniform = true;
  113. accuCoverage->sampler = false;
  114. accuCoverage->constSortPos = cspPotentialPrimitive;
  115. }
  116. Var *accuSpecular = (Var*)LangElement::find( "accuSpecular" );
  117. if ( !accuSpecular )
  118. {
  119. accuSpecular = new Var;
  120. accuSpecular->setType( "float" );
  121. accuSpecular->setName( "accuSpecular" );
  122. accuSpecular->uniform = true;
  123. accuSpecular->sampler = false;
  124. accuSpecular->constSortPos = cspPotentialPrimitive;
  125. }
  126. Var *inTex = getInTexCoord( "texCoord", "float2", true, componentList );
  127. Var *accuVec = getInTexCoord( "accuVec", "float3", true, componentList );
  128. Var *bumpNorm = (Var *)LangElement::find( "bumpSample" );
  129. if( bumpNorm == NULL )
  130. {
  131. bumpNorm = (Var *)LangElement::find( "bumpNormal" );
  132. if (!bumpNorm)
  133. return;
  134. }
  135. // get the accu pixel color
  136. if (mIsDirect3D11)
  137. {
  138. Var *accuMapTex = new Var;
  139. accuMapTex->setType("Texture2D");
  140. accuMapTex->setName("accuMapTex");
  141. accuMapTex->uniform = true;
  142. accuMapTex->texture = true;
  143. accuMapTex->constNum = accuMap->constNum;
  144. meta->addStatement(new GenOp(" @ = @.Sample(@, @ * @);\r\n", colorAccuDecl, accuMapTex, accuMap, inTex, accuScale));
  145. }
  146. else
  147. meta->addStatement(new GenOp(" @ = tex2D(@, @ * @);\r\n", colorAccuDecl, accuMap, inTex, accuScale));
  148. // scale up normals
  149. meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 0.5;\r\n", bumpNorm, bumpNorm ) );
  150. // assign direction
  151. meta->addStatement( new GenOp( " @.z *= @*2.0;\r\n", accuVec, accuDirection ) );
  152. // saturate based on strength
  153. meta->addStatement( new GenOp( " @ = saturate( dot( @, @.xyz * pow(@, 5) ) );\r\n", plcAccu, bumpNorm, accuVec, accuStrength ) );
  154. // add coverage
  155. meta->addStatement( new GenOp( " @.a += (2 * pow(@/2, 5)) - 0.5;\r\n", accuPlc, accuCoverage ) );
  156. // clamp to a sensible value
  157. meta->addStatement( new GenOp( " @.a = clamp(@.a, 0, 1);\r\n", accuPlc, accuPlc ) );
  158. // light
  159. Var *lightColor = (Var*) LangElement::find( "d_lightcolor" );
  160. if(lightColor != NULL)
  161. meta->addStatement( new GenOp( " @ *= float4(@, 1.0);\r\n\r\n", accuColor, lightColor ) );
  162. // lerp with current pixel - use the accu alpha as well
  163. meta->addStatement( new GenOp( " @ = lerp( @, @, @.a * @.a);\r\n", color, color, accuColor, accuPlc, accuColor ) );
  164. // the result should always be opaque
  165. meta->addStatement( new GenOp( " @.a = 1.0;\r\n", color ) );
  166. }
  167. void AccuTexFeatHLSL::setTexData( Material::StageData &stageDat,
  168. const MaterialFeatureData &fd,
  169. RenderPassData &passData,
  170. U32 &texIndex )
  171. {
  172. //GFXTextureObject *tex = stageDat.getTex( MFT_AccuMap );
  173. //if ( tex )
  174. //{
  175. passData.mSamplerNames[ texIndex ] = "AccuMap";
  176. passData.mTexType[ texIndex++ ] = Material::AccuMap;
  177. //}
  178. }
  179. void AccuTexFeatHLSL::getAccuVec( MultiLine *meta, LangElement *accuVec )
  180. {
  181. // Get the transform to world space.
  182. Var *objTrans = (Var*)LangElement::find( "objTrans" );
  183. if ( !objTrans )
  184. {
  185. objTrans = new Var;
  186. objTrans->setType( "float4x4" );
  187. objTrans->setName( "objTrans" );
  188. objTrans->uniform = true;
  189. objTrans->constSortPos = cspPrimitive;
  190. }
  191. // accu obj trans
  192. Var *aobjTrans = new Var;
  193. aobjTrans->setType( "float4x4" );
  194. aobjTrans->setName( "accuObjTrans" );
  195. LangElement *accuObjTransDecl = new DecOp( aobjTrans );
  196. Var *outObjToTangentSpace = (Var*)LangElement::find( "objToTangentSpace" );
  197. Var *tav = new Var;
  198. tav->setType( "float4" );
  199. tav->setName( "tAccuVec" );
  200. LangElement *tavDecl = new DecOp( tav );
  201. meta->addStatement( new GenOp( " @ = float4(0,0,1,0);\r\n", tavDecl ) );
  202. meta->addStatement( new GenOp( " @ = transpose(@);\r\n", accuObjTransDecl, objTrans ) );
  203. meta->addStatement( new GenOp( " @ = mul(@, @);\r\n", tav, aobjTrans, tav ) );
  204. meta->addStatement( new GenOp( " @.xyz = mul(@, @.xyz);\r\n", tav, outObjToTangentSpace, tav ) );
  205. meta->addStatement( new GenOp( " @.y *= -1;\r\n", tav ) );
  206. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", accuVec, tav ) );
  207. }
  208. Var* AccuTexFeatHLSL::addOutAccuVec( Vector<ShaderComponent*> &componentList, MultiLine *meta )
  209. {
  210. Var *outAccuVec = (Var*)LangElement::find( "accuVec" );
  211. if ( !outAccuVec )
  212. {
  213. // Setup the connector.
  214. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  215. outAccuVec = connectComp->getElement( RT_TEXCOORD );
  216. outAccuVec->setName( "accuVec" );
  217. outAccuVec->setStructName( "OUT" );
  218. outAccuVec->setType( "float3" );
  219. outAccuVec->mapsToSampler = false;
  220. getAccuVec( meta, outAccuVec );
  221. }
  222. return outAccuVec;
  223. }