accuFeatureHLSL.cpp 9.1 KB

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