terrFeatureHLSL.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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 "platform/platform.h"
  23. #include "terrain/hlsl/terrFeatureHLSL.h"
  24. #include "terrain/terrFeatureTypes.h"
  25. #include "materials/materialFeatureTypes.h"
  26. #include "materials/materialFeatureData.h"
  27. #include "materials/processedMaterial.h"
  28. #include "gfx/gfxDevice.h"
  29. #include "shaderGen/langElement.h"
  30. #include "shaderGen/shaderOp.h"
  31. #include "shaderGen/featureType.h"
  32. #include "shaderGen/featureMgr.h"
  33. #include "shaderGen/shaderGen.h"
  34. #include "core/module.h"
  35. namespace
  36. {
  37. void register_hlsl_shader_features_for_terrain(GFXAdapterType type)
  38. {
  39. if (type != Direct3D11)
  40. return;
  41. FEATUREMGR->registerFeature( MFT_TerrainBaseMap, new TerrainBaseMapFeatHLSL );
  42. FEATUREMGR->registerFeature( MFT_TerrainParallaxMap, new NamedFeatureHLSL( "Terrain Parallax Texture" ) );
  43. FEATUREMGR->registerFeature( MFT_TerrainDetailMap, new TerrainDetailMapFeatHLSL );
  44. FEATUREMGR->registerFeature( MFT_TerrainNormalMap, new TerrainNormalMapFeatHLSL );
  45. FEATUREMGR->registerFeature( MFT_TerrainMacroMap, new NamedFeatureHLSL("TerrainMacroMap Deprecated")); // new TerrainMacroMapFeatHLSL);
  46. FEATUREMGR->registerFeature( MFT_TerrainLightMap, new TerrainLightMapFeatHLSL );
  47. FEATUREMGR->registerFeature( MFT_TerrainSideProject, new NamedFeatureHLSL( "Terrain Side Projection" ) );
  48. FEATUREMGR->registerFeature( MFT_TerrainHeightBlend, new TerrainHeightMapBlendHLSL );
  49. FEATUREMGR->registerFeature( MFT_TerrainORMMap, new TerrainORMMapFeatHLSL );
  50. FEATUREMGR->registerFeature( MFT_DeferredTerrainBlankInfoMap, new TerrainBlankInfoMapFeatHLSL );
  51. }
  52. };
  53. MODULE_BEGIN( TerrainFeatHLSL )
  54. MODULE_INIT_AFTER( ShaderGen )
  55. MODULE_INIT
  56. {
  57. SHADERGEN->getFeatureInitSignal().notify(&register_hlsl_shader_features_for_terrain);
  58. }
  59. MODULE_END;
  60. TerrainFeatHLSL::TerrainFeatHLSL()
  61. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" ))
  62. {
  63. addDependency( &mTorqueDep );
  64. }
  65. Var* TerrainFeatHLSL::_getUniformVar( const char *name, const char *type, ConstantSortPosition csp )
  66. {
  67. Var *theVar = (Var*)LangElement::find( name );
  68. if ( !theVar )
  69. {
  70. theVar = new Var;
  71. theVar->setType( type );
  72. theVar->setName( name );
  73. theVar->uniform = true;
  74. theVar->constSortPos = csp;
  75. }
  76. return theVar;
  77. }
  78. Var* TerrainFeatHLSL::_getInDetailCoord( Vector<ShaderComponent*> &componentList )
  79. {
  80. String name( String::ToString( "detCoord%d", getProcessIndex() ) );
  81. Var *inDet = (Var*)LangElement::find( name );
  82. if ( !inDet )
  83. {
  84. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  85. inDet = connectComp->getElement( RT_TEXCOORD );
  86. inDet->setName( name );
  87. inDet->setStructName( "IN" );
  88. inDet->setType( "float4" );
  89. }
  90. return inDet;
  91. }
  92. Var* TerrainFeatHLSL::_getInMacroCoord( Vector<ShaderComponent*> &componentList )
  93. {
  94. String name( String::ToString( "macroCoord%d", getProcessIndex() ) );
  95. Var *inDet = (Var*)LangElement::find( name );
  96. if ( !inDet )
  97. {
  98. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  99. inDet = connectComp->getElement( RT_TEXCOORD );
  100. inDet->setName( name );
  101. inDet->setStructName( "IN" );
  102. inDet->setType( "float4" );
  103. }
  104. return inDet;
  105. }
  106. Var* TerrainFeatHLSL::_getDetailMapSampler()
  107. {
  108. String name("detailMapSampler");
  109. Var* detailMapSampler = (Var*)LangElement::find(name);
  110. if(!detailMapSampler)
  111. {
  112. detailMapSampler = new Var;
  113. detailMapSampler->setName(name);
  114. detailMapSampler->setType("SamplerState");
  115. detailMapSampler->uniform = true;
  116. detailMapSampler->sampler = true;
  117. detailMapSampler->constNum = Var::getTexUnitNum();
  118. }
  119. return detailMapSampler;
  120. }
  121. Var* TerrainFeatHLSL::_getDetailMapArray()
  122. {
  123. String name("detailMapArray");
  124. Var* detailMapArray = (Var*)LangElement::find(name);
  125. if(!detailMapArray)
  126. {
  127. detailMapArray = new Var;
  128. detailMapArray->setName(name);
  129. detailMapArray->setType("Texture2DArray");
  130. detailMapArray->uniform = true;
  131. detailMapArray->texture = true;
  132. detailMapArray->constNum = _getDetailMapSampler()->constNum;
  133. }
  134. return detailMapArray;
  135. }
  136. Var* TerrainFeatHLSL::_getNormalMapSampler()
  137. {
  138. String name("normalMapSampler");
  139. Var* normalMapSampler = (Var*)LangElement::find(name);
  140. if (!normalMapSampler)
  141. {
  142. normalMapSampler = new Var;
  143. normalMapSampler->setName(name);
  144. normalMapSampler->setType("SamplerState");
  145. normalMapSampler->uniform = true;
  146. normalMapSampler->sampler = true;
  147. normalMapSampler->constNum = Var::getTexUnitNum();
  148. }
  149. return normalMapSampler;
  150. }
  151. Var* TerrainFeatHLSL::_getNormalMapArray()
  152. {
  153. String name("normalMapArray");
  154. Var* normalMapArray = (Var*)LangElement::find(name);
  155. if (!normalMapArray)
  156. {
  157. normalMapArray = new Var;
  158. normalMapArray->setName(name);
  159. normalMapArray->setType("Texture2DArray");
  160. normalMapArray->uniform = true;
  161. normalMapArray->texture = true;
  162. normalMapArray->constNum = _getNormalMapSampler()->constNum;
  163. }
  164. return normalMapArray;
  165. }
  166. Var* TerrainFeatHLSL::_getOrmMapSampler()
  167. {
  168. String name("ormMapSampler");
  169. Var* ormMapSampler = (Var*)LangElement::find(name);
  170. if (!ormMapSampler)
  171. {
  172. ormMapSampler = new Var;
  173. ormMapSampler->setName(name);
  174. ormMapSampler->setType("SamplerState");
  175. ormMapSampler->uniform = true;
  176. ormMapSampler->sampler = true;
  177. ormMapSampler->constNum = Var::getTexUnitNum();
  178. }
  179. return ormMapSampler;
  180. }
  181. Var* TerrainFeatHLSL::_getOrmMapArray()
  182. {
  183. String name("ormMapArray");
  184. Var* ormMapArray = (Var*)LangElement::find(name);
  185. if (!ormMapArray)
  186. {
  187. ormMapArray = new Var;
  188. ormMapArray->setName(name);
  189. ormMapArray->setType("Texture2DArray");
  190. ormMapArray->uniform = true;
  191. ormMapArray->texture = true;
  192. ormMapArray->constNum = _getOrmMapSampler()->constNum;
  193. }
  194. return ormMapArray;
  195. }
  196. Var* TerrainFeatHLSL::_getDetailIdStrengthParallax()
  197. {
  198. String name( String::ToString( "detailIdStrengthParallax", getProcessIndex() ) );
  199. Var *detailInfo = (Var*)LangElement::find( name );
  200. if ( !detailInfo )
  201. {
  202. detailInfo = new Var;
  203. detailInfo->setType( "float4" );
  204. detailInfo->setName( name );
  205. detailInfo->uniform = true;
  206. detailInfo->constSortPos = cspPotentialPrimitive;
  207. detailInfo->arraySize = getProcessIndex();
  208. }
  209. detailInfo->arraySize = mMax(detailInfo->arraySize, getProcessIndex() + 1);
  210. return detailInfo;
  211. }
  212. Var* TerrainFeatHLSL::_getMacroIdStrengthParallax()
  213. {
  214. String name( String::ToString( "macroIdStrengthParallax%d", getProcessIndex() ) );
  215. Var *detailInfo = (Var*)LangElement::find( name );
  216. if ( !detailInfo )
  217. {
  218. detailInfo = new Var;
  219. detailInfo->setType( "float3" );
  220. detailInfo->setName( name );
  221. detailInfo->uniform = true;
  222. detailInfo->constSortPos = cspPotentialPrimitive;
  223. }
  224. return detailInfo;
  225. }
  226. void TerrainBaseMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  227. const MaterialFeatureData &fd )
  228. {
  229. MultiLine *meta = new MultiLine;
  230. output = meta;
  231. // Generate the incoming texture var.
  232. Var *inTex;
  233. {
  234. Var *inPos = (Var*)LangElement::find( "inPosition" );
  235. if ( !inPos )
  236. inPos = (Var*)LangElement::find( "position" );
  237. inTex = new Var( "texCoord", "float3" );
  238. Var *oneOverTerrainSize = _getUniformVar( "oneOverTerrainSize", "float", cspPass );
  239. // NOTE: The y coord here should be negative to have
  240. // the texture maps not end up flipped which also caused
  241. // normal and parallax mapping to be incorrect.
  242. //
  243. // This mistake early in development means that the layer
  244. // id bilinear blend depends on it being that way.
  245. //
  246. // So instead i fixed this by flipping the base and detail
  247. // coord y scale to compensate when rendering.
  248. //
  249. meta->addStatement( new GenOp( " @ = @.xyz * float3( @, @, -@ );\r\n",
  250. new DecOp( inTex ), inPos, oneOverTerrainSize, oneOverTerrainSize, oneOverTerrainSize ) );
  251. }
  252. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  253. // Pass the texture coord to the pixel shader.
  254. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  255. outTex->setName( "outTexCoord" );
  256. outTex->setStructName( "OUT" );
  257. outTex->setType( "float3" );
  258. meta->addStatement( new GenOp( " @.xy = @.xy;\r\n", outTex, inTex ) );
  259. // If this shader has a side projected layer then we
  260. // pass the dot product between the +Y and the normal
  261. // thru outTexCoord.z for use in blending the textures.
  262. if ( fd.features.hasFeature( MFT_TerrainSideProject ) )
  263. {
  264. Var *inNormal = (Var*)LangElement::find( "normal" );
  265. meta->addStatement(
  266. new GenOp( " @.z = pow( abs( dot( normalize( float3( @.x, @.y, 0 ) ), float3( 0, 1, 0 ) ) ), 10.0 );\r\n",
  267. outTex, inNormal, inNormal ) );
  268. }
  269. else
  270. meta->addStatement( new GenOp( " @.z = 0;\r\n", outTex ) );
  271. // HACK: This is sort of lazy... we generate the tanget
  272. // vector here so that we're sure it exists in the parallax
  273. // and normal features which will expect "T" to exist.
  274. //
  275. // If this shader doesn't use it the shader compiler will
  276. // optimize away this code.
  277. //
  278. Var *inTangentZ = getVertTexCoord( "tcTangentZ" );
  279. Var *inTanget = new Var( "T", "float3" );
  280. Var *squareSize = _getUniformVar( "squareSize", "float", cspPass );
  281. meta->addStatement( new GenOp( " @ = normalize( float3( @, 0, @ ) );\r\n",
  282. new DecOp( inTanget ), squareSize, inTangentZ ) );
  283. }
  284. void TerrainBaseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  285. const MaterialFeatureData &fd )
  286. {
  287. // grab connector texcoord register
  288. Var *texCoord = getInTexCoord( "texCoord", "float3", componentList );
  289. // create texture var
  290. Var *diffuseMap = new Var;
  291. diffuseMap->setType( "SamplerState" );
  292. diffuseMap->setName( "baseTexMap" );
  293. diffuseMap->uniform = true;
  294. diffuseMap->sampler = true;
  295. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  296. MultiLine *meta = new MultiLine;
  297. Var *baseColor = new Var;
  298. baseColor->setType( "float4" );
  299. baseColor->setName( "baseColor" );
  300. Var *diffuseTex = new Var;
  301. diffuseTex->setType("Texture2D");
  302. diffuseTex->setName("baseTexture");
  303. diffuseTex->uniform = true;
  304. diffuseTex->texture = true;
  305. diffuseTex->constNum = diffuseMap->constNum;
  306. meta->addStatement(new GenOp(" @ = @.Sample( @, @.xy );\r\n", new DecOp(baseColor), diffuseTex, diffuseMap, texCoord));
  307. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  308. if (fd.features.hasFeature(MFT_isDeferred))
  309. {
  310. target= ShaderFeature::RenderTarget1;
  311. }
  312. meta->addStatement( new GenOp( " @;\r\n", assignColor( baseColor, Material::Mul,NULL,target ) ) );
  313. if (fd.features[MFT_isDeferred])
  314. {
  315. // Set base ORM info
  316. Var* ormConfig;
  317. OutputTarget targ = RenderTarget1;
  318. targ = RenderTarget2;
  319. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(targ));
  320. if (!ormConfig)
  321. {
  322. // create color var
  323. ormConfig = new Var;
  324. ormConfig->setType("fragout");
  325. ormConfig->setName(getOutputTargetVarName(targ));
  326. ormConfig->setStructName("OUT");
  327. }
  328. meta->addStatement(new GenOp(" @ = float4(0.0, 1.0, 1.0, 0.0);\r\n", ormConfig));
  329. }
  330. output = meta;
  331. }
  332. ShaderFeature::Resources TerrainBaseMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  333. {
  334. Resources res;
  335. res.numTexReg = 1;
  336. res.numTex = 1;
  337. return res;
  338. }
  339. U32 TerrainBaseMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  340. {
  341. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 | ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  342. }
  343. TerrainDetailMapFeatHLSL::TerrainDetailMapFeatHLSL()
  344. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" )),
  345. mTerrainDep(ShaderGen::smCommonShaderPath + String("/terrain/terrain.hlsl" ))
  346. {
  347. addDependency( &mTorqueDep );
  348. addDependency( &mTerrainDep );
  349. }
  350. void TerrainDetailMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  351. const MaterialFeatureData &fd )
  352. {
  353. const S32 detailIndex = getProcessIndex();
  354. // Grab incoming texture coords... the base map feature
  355. // made sure this was created.
  356. Var *inTex = (Var*)LangElement::find( "texCoord" );
  357. AssertFatal( inTex, "The texture coord is missing!" );
  358. // Grab the input position.
  359. Var *inPos = (Var*)LangElement::find( "inPosition" );
  360. if ( !inPos )
  361. inPos = (Var*)LangElement::find( "position" );
  362. // Get the object space eye position.
  363. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  364. MultiLine *meta = new MultiLine;
  365. // If we have parallax mapping then make sure we've sent
  366. // the negative view vector to the pixel shader.
  367. if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) &&
  368. !LangElement::find( "outNegViewTS" ) )
  369. {
  370. // Get the object to tangent transform which
  371. // will consume 3 output registers.
  372. Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta, fd );
  373. // Now use a single output register to send the negative
  374. // view vector in tangent space to the pixel shader.
  375. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  376. Var *outNegViewTS = connectComp->getElement( RT_TEXCOORD );
  377. outNegViewTS->setName( "outNegViewTS" );
  378. outNegViewTS->setStructName( "OUT" );
  379. outNegViewTS->setType( "float3" );
  380. meta->addStatement( new GenOp( " @ = mul( @, float3( @ - @.xyz ) );\r\n",
  381. outNegViewTS, objToTangentSpace, eyePos, inPos ) );
  382. }
  383. // Get the distance from the eye to this vertex.
  384. Var *dist = (Var*)LangElement::find( "dist" );
  385. if ( !dist )
  386. {
  387. dist = new Var;
  388. dist->setType( "float" );
  389. dist->setName( "dist" );
  390. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  391. new DecOp( dist ), inPos, eyePos ) );
  392. }
  393. // grab connector texcoord register
  394. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  395. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  396. outTex->setName( String::ToString( "detCoord%d", detailIndex ) );
  397. outTex->setStructName( "OUT" );
  398. outTex->setType( "float4" );
  399. // Get the detail scale and fade info.
  400. Var* detScaleAndFade = (Var*)LangElement::find("detailScaleAndFade");
  401. if (detScaleAndFade == NULL)
  402. {
  403. detScaleAndFade = new Var;
  404. detScaleAndFade->setType("float4");
  405. detScaleAndFade->setName("detailScaleAndFade");
  406. detScaleAndFade->uniform = true;
  407. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  408. }
  409. detScaleAndFade->arraySize = mMax(detScaleAndFade->arraySize, detailIndex + 1);
  410. // Setup the detail coord.
  411. //
  412. // NOTE: You see here we scale the texture coord by 'xyx'
  413. // to generate the detail coord. This y is here because
  414. // its scale is flipped to correct for the non negative y
  415. // in texCoord.
  416. //
  417. // See TerrainBaseMapFeatHLSL::processVert().
  418. //
  419. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, new IndexOp(detScaleAndFade, detailIndex) ) );
  420. // And sneak the detail fade thru the w detailCoord.
  421. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  422. outTex, new IndexOp(detScaleAndFade, detailIndex), dist, new IndexOp(detScaleAndFade, detailIndex)) );
  423. output = meta;
  424. }
  425. void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  426. const MaterialFeatureData &fd )
  427. {
  428. const S32 detailIndex = getProcessIndex();
  429. Var *inTex = getVertTexCoord( "texCoord" );
  430. MultiLine *meta = new MultiLine;
  431. // We need the negative tangent space view vector
  432. // as in parallax mapping we step towards the camera.
  433. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  434. if ( !negViewTS &&
  435. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  436. {
  437. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  438. if ( !inNegViewTS )
  439. {
  440. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  441. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  442. inNegViewTS->setName( "outNegViewTS" );
  443. inNegViewTS->setStructName( "IN" );
  444. inNegViewTS->setType( "float3" );
  445. }
  446. negViewTS = new Var( "negViewTS", "float3" );
  447. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  448. }
  449. // Get the layer samples.
  450. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  451. if ( !layerSample )
  452. {
  453. layerSample = new Var;
  454. layerSample->setType( "float4" );
  455. layerSample->setName( "layerSample" );
  456. // Get the layer texture var
  457. Var *layerTex = new Var;
  458. layerTex->setType( "SamplerState" );
  459. layerTex->setName( "layerTex" );
  460. layerTex->uniform = true;
  461. layerTex->sampler = true;
  462. layerTex->constNum = Var::getTexUnitNum();
  463. Var* layerTexObj = new Var;
  464. layerTexObj->setName("layerTexObj");
  465. layerTexObj->setType("Texture2D");
  466. layerTexObj->uniform = true;
  467. layerTexObj->texture = true;
  468. layerTexObj->constNum = layerTex->constNum;
  469. // Read the layer texture to get the samples.
  470. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  471. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  472. }
  473. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  474. if ( !layerSize )
  475. {
  476. layerSize = new Var;
  477. layerSize->setType( "float" );
  478. layerSize->setName( "layerSize" );
  479. layerSize->uniform = true;
  480. layerSize->constSortPos = cspPass;
  481. }
  482. // Grab the incoming detail coord.
  483. Var *inDet = _getInDetailCoord( componentList );
  484. // Get the detail id.
  485. Var *detailInfo = _getDetailIdStrengthParallax();
  486. // Create the detail blend var.
  487. Var *detailBlend = new Var;
  488. detailBlend->setType( "float" );
  489. detailBlend->setName( String::ToString( "detailBlend%d", detailIndex ) );
  490. // Calculate the blend for this detail texture.
  491. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  492. new DecOp( detailBlend ), new IndexOp(detailInfo, detailIndex), inTex, layerSize, layerSample ) );
  493. // If we had a parallax feature... then factor in the parallax
  494. // amount so that it fades out with the layer blending.
  495. if (fd.features.hasFeature(MFT_TerrainParallaxMap, detailIndex))
  496. {
  497. Var* normalMapArray = _getNormalMapArray();
  498. Var* normalMapSampler = _getNormalMapSampler();
  499. // Call the library function to do the rest.
  500. if (fd.features.hasFeature(MFT_IsBC3nm, detailIndex))
  501. {
  502. meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnmTexArray( @, @, float3(@.xy, @.x), @, @.z * @ );\r\n",
  503. inDet, normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, detailIndex), negViewTS, new IndexOp(detailInfo, detailIndex), detailBlend));
  504. }
  505. else
  506. {
  507. meta->addStatement(new GenOp(" @.xy += parallaxOffsetTexArray( @, @, float3(@.xy, @.x), @, @.z * @ );\r\n",
  508. inDet, normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, detailIndex), negViewTS, new IndexOp(detailInfo, detailIndex), detailBlend));
  509. }
  510. }
  511. Var *detailColor = (Var*)LangElement::find(String::ToString("detailColor%d", detailIndex));
  512. if ( !detailColor )
  513. {
  514. detailColor = new Var;
  515. detailColor->setType( "float4" );
  516. detailColor->setName( String::ToString("detailColor%d", detailIndex) );
  517. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  518. }
  519. //Sampled detail texture that is not expanded
  520. Var* detailMapArray = _getDetailMapArray();
  521. Var* detailMapSampler = _getDetailMapSampler();
  522. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  523. {
  524. meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z ) * 2.0 ) - 1.0;\r\n",
  525. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), inTex));
  526. }
  527. else
  528. {
  529. meta->addStatement(new GenOp(" @ = ( @.Sample( @, float3(@.xy, @.x) ) * 2.0 ) - 1.0;\r\n",
  530. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex)));
  531. }
  532. meta->addStatement(new GenOp(" @ *= @.y * @.w;\r\n",
  533. detailColor, new IndexOp(detailInfo, detailIndex), inDet));
  534. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  535. {
  536. // Check to see if we have a gbuffer normal.
  537. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  538. // If we have a gbuffer normal and we don't have a
  539. // normal map feature then we need to lerp in a
  540. // default normal else the normals below this layer
  541. // will show thru.
  542. if (gbNormal &&
  543. !fd.features.hasFeature(MFT_TerrainNormalMap, detailIndex))
  544. {
  545. Var* viewToTangent = getInViewToTangent(componentList);
  546. meta->addStatement(new GenOp(" @ = lerp( @, @[2], min( @, @.w ) );\r\n",
  547. gbNormal, gbNormal, viewToTangent, detailBlend, inDet));
  548. }
  549. // If we're using SM 3.0 then take advantage of
  550. // dynamic branching to skip layers per-pixel.
  551. if (GFX->getPixelShaderVersion() >= 3.0f)
  552. meta->addStatement(new GenOp(" if ( @ > 0.0f )\r\n", detailBlend));
  553. meta->addStatement(new GenOp(" {\r\n"));
  554. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  555. if (fd.features.hasFeature(MFT_isDeferred))
  556. target = ShaderFeature::RenderTarget1;
  557. Var* outColor = (Var*)LangElement::find(getOutputTargetVarName(target));
  558. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n", outColor, outColor));
  559. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  560. outColor, detailColor, detailBlend));
  561. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n", outColor, outColor));
  562. meta->addStatement(new GenOp(" }\r\n"));
  563. }
  564. output = meta;
  565. }
  566. ShaderFeature::Resources TerrainDetailMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  567. {
  568. Resources res;
  569. if ( getProcessIndex() == 0 )
  570. {
  571. // If this is the first detail pass then we
  572. // samples from the layer tex.
  573. res.numTex += 1;
  574. res.numTexReg += 1;
  575. // Add Detail TextureArray
  576. res.numTex += 1;
  577. res.numTexReg += 1;
  578. }
  579. return res;
  580. }
  581. U32 TerrainDetailMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  582. {
  583. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  584. }
  585. TerrainMacroMapFeatHLSL::TerrainMacroMapFeatHLSL()
  586. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" )),
  587. mTerrainDep(ShaderGen::smCommonShaderPath + String("/terrain/terrain.hlsl" ))
  588. {
  589. addDependency( &mTorqueDep );
  590. addDependency( &mTerrainDep );
  591. }
  592. void TerrainMacroMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  593. const MaterialFeatureData &fd )
  594. {
  595. const S32 detailIndex = getProcessIndex();
  596. // Grab incoming texture coords... the base map feature
  597. // made sure this was created.
  598. Var *inTex = (Var*)LangElement::find( "texCoord" );
  599. AssertFatal( inTex, "The texture coord is missing!" );
  600. // Grab the input position.
  601. Var *inPos = (Var*)LangElement::find( "inPosition" );
  602. if ( !inPos )
  603. inPos = (Var*)LangElement::find( "position" );
  604. // Get the object space eye position.
  605. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  606. MultiLine *meta = new MultiLine;
  607. // Get the distance from the eye to this vertex.
  608. Var *dist = (Var*)LangElement::find( "macroDist" );
  609. if ( !dist )
  610. {
  611. dist = new Var;
  612. dist->setType( "float" );
  613. dist->setName( "macroDist" );
  614. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  615. new DecOp( dist ), inPos, eyePos ) );
  616. }
  617. // grab connector texcoord register
  618. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  619. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  620. outTex->setName( String::ToString( "macroCoord%d", detailIndex ) );
  621. outTex->setStructName( "OUT" );
  622. outTex->setType( "float4" );
  623. // Get the detail scale and fade info.
  624. Var* macroScaleAndFade = (Var*)LangElement::find("macroScaleAndFade");
  625. if (macroScaleAndFade == NULL)
  626. {
  627. macroScaleAndFade = new Var;
  628. macroScaleAndFade->setType("float4");
  629. macroScaleAndFade->setName("macroScaleAndFade");
  630. macroScaleAndFade->uniform = true;
  631. macroScaleAndFade->constSortPos = cspPotentialPrimitive;
  632. }
  633. macroScaleAndFade->arraySize = mMax(macroScaleAndFade->arraySize, detailIndex + 1);
  634. // Setup the detail coord.
  635. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, new IndexOp(macroScaleAndFade, detailIndex)) );
  636. // And sneak the detail fade thru the w detailCoord.
  637. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  638. outTex, new IndexOp(macroScaleAndFade, detailIndex), dist, new IndexOp(macroScaleAndFade, detailIndex)) );
  639. output = meta;
  640. }
  641. void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  642. const MaterialFeatureData &fd )
  643. {
  644. const S32 detailIndex = getProcessIndex();
  645. Var *inTex = getVertTexCoord( "texCoord" );
  646. MultiLine *meta = new MultiLine;
  647. // We need the negative tangent space view vector
  648. // as in parallax mapping we step towards the camera.
  649. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  650. if ( !negViewTS &&
  651. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  652. {
  653. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  654. if ( !inNegViewTS )
  655. {
  656. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  657. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  658. inNegViewTS->setName( "outNegViewTS" );
  659. inNegViewTS->setStructName( "IN" );
  660. inNegViewTS->setType( "float3" );
  661. }
  662. negViewTS = new Var( "negViewTS", "float3" );
  663. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  664. }
  665. // Get the layer samples.
  666. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  667. if ( !layerSample )
  668. {
  669. layerSample = new Var;
  670. layerSample->setType( "float4" );
  671. layerSample->setName( "layerSample" );
  672. // Get the layer texture var
  673. Var *layerTex = new Var;
  674. layerTex->setType( "SamplerState" );
  675. layerTex->setName( "macrolayerTex" );
  676. layerTex->uniform = true;
  677. layerTex->sampler = true;
  678. layerTex->constNum = Var::getTexUnitNum();
  679. // Read the layer texture to get the samples.
  680. Var *layerTexObj = new Var;
  681. layerTexObj->setType("Texture2D");
  682. layerTexObj->setName("macroLayerTexObj");
  683. layerTexObj->uniform = true;
  684. layerTexObj->texture = true;
  685. layerTexObj->constNum = layerTex->constNum;
  686. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  687. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  688. }
  689. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  690. if ( !layerSize )
  691. {
  692. layerSize = new Var;
  693. layerSize->setType( "float" );
  694. layerSize->setName( "layerSize" );
  695. layerSize->uniform = true;
  696. layerSize->constSortPos = cspPass;
  697. }
  698. // Grab the incoming detail coord.
  699. Var *inDet = _getInMacroCoord( componentList );
  700. // Get the detail id.
  701. Var *detailInfo = _getMacroIdStrengthParallax();
  702. // Create the detail blend var.
  703. Var *detailBlend = new Var;
  704. detailBlend->setType( "float" );
  705. detailBlend->setName( String::ToString( "macroBlend%d", detailIndex ) );
  706. // Calculate the blend for this detail texture.
  707. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  708. new DecOp( detailBlend ), new IndexOp(detailInfo, detailIndex), inTex, layerSize, layerSample ) );
  709. // Check to see if we have a gbuffer normal.
  710. Var *gbNormal = (Var*)LangElement::find( "gbNormal" );
  711. // If we have a gbuffer normal and we don't have a
  712. // normal map feature then we need to lerp in a
  713. // default normal else the normals below this layer
  714. // will show thru.
  715. if ( gbNormal &&
  716. !fd.features.hasFeature( MFT_TerrainNormalMap, detailIndex ) )
  717. {
  718. Var *viewToTangent = getInViewToTangent( componentList );
  719. meta->addStatement( new GenOp( " @ = lerp( @, @[2], min( @, @.w ) );\r\n",
  720. gbNormal, gbNormal, viewToTangent, detailBlend, inDet ) );
  721. }
  722. Var *detailColor = (Var*)LangElement::find("macroColor");
  723. if (!detailColor)
  724. {
  725. detailColor = new Var;
  726. detailColor->setType( "float4" );
  727. detailColor->setName( "macroColor" );
  728. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  729. }
  730. // If we're using SM 3.0 then take advantage of
  731. // dynamic branching to skip layers per-pixel.
  732. if ( GFX->getPixelShaderVersion() >= 3.0f )
  733. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  734. meta->addStatement( new GenOp( " {\r\n" ) );
  735. Var* detailMapArray = _getDetailMapArray();
  736. Var* detailMapSampler = _getDetailMapSampler();
  737. // Note that we're doing the standard greyscale detail
  738. // map technique here which can darken and lighten the
  739. // diffuse texture.
  740. //
  741. // We take two color samples and lerp between them for
  742. // side projection layers... else a single sample.
  743. //
  744. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  745. {
  746. meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z ) * 2.0 ) - 1.0;\r\n",
  747. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), inTex));
  748. }
  749. else
  750. {
  751. meta->addStatement(new GenOp(" @ = ( @.Sample( @, float3(@.xy, @.x) ) * 2.0 ) - 1.0;\r\n",
  752. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex)));
  753. }
  754. meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
  755. detailColor, new IndexOp(detailInfo, detailIndex), inDet ) );
  756. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  757. if (fd.features.hasFeature(MFT_isDeferred))
  758. target= ShaderFeature::RenderTarget1;
  759. Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
  760. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n", outColor, outColor));
  761. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  762. outColor, detailColor, detailBlend));
  763. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n", outColor, outColor));
  764. meta->addStatement( new GenOp( " }\r\n" ) );
  765. output = meta;
  766. }
  767. ShaderFeature::Resources TerrainMacroMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  768. {
  769. Resources res;
  770. if ( getProcessIndex() == 0 )
  771. {
  772. // If this is the first detail pass then we
  773. // samples from the layer tex.
  774. res.numTex += 1;
  775. }
  776. res.numTex += 1;
  777. // Finally we always send the detail texture
  778. // coord to the pixel shader.
  779. res.numTexReg += 1;
  780. return res;
  781. }
  782. U32 TerrainMacroMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  783. {
  784. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  785. }
  786. void TerrainNormalMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  787. const MaterialFeatureData &fd )
  788. {
  789. // We only need to process normals during the deferred.
  790. if ( !fd.features.hasFeature( MFT_DeferredConditioner ) )
  791. return;
  792. MultiLine *meta = new MultiLine;
  793. if ( !fd.features.hasFeature(MFT_TerrainHeightBlend) )
  794. {
  795. // Make sure the world to tangent transform
  796. // is created and available for the pixel shader.
  797. getOutViewToTangent(componentList, meta, fd);
  798. }
  799. output = meta;
  800. }
  801. void TerrainNormalMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  802. const MaterialFeatureData &fd )
  803. {
  804. // We only need to process normals during the deferred.
  805. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  806. return;
  807. MultiLine *meta = new MultiLine;
  808. const S32 normalIndex = getProcessIndex();
  809. Var *detailBlend = (Var*)LangElement::find( String::ToString( "detailBlend%d", normalIndex ) );
  810. AssertFatal( detailBlend, "The detail blend is missing!" );
  811. /// Get the texture coord.
  812. Var* inDet = _getInDetailCoord(componentList);
  813. Var* inTex = getVertTexCoord("texCoord");
  814. Var* detailInfo = _getDetailIdStrengthParallax();
  815. // Sample the normal map.
  816. //
  817. // We take two normal samples and lerp between them for
  818. // side projection layers... else a single sample.
  819. LangElement* texOp;
  820. Var* normalMapSampler = _getNormalMapSampler();
  821. Var* normalMapArray = _getNormalMapArray();
  822. if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex))
  823. {
  824. texOp = new GenOp("lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z )",
  825. normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex), normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex), inTex);
  826. }
  827. else
  828. texOp = new GenOp("@.Sample(@, float3(@.xy, @.x))", normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex));
  829. // create bump normal
  830. Var* bumpNorm = new Var;
  831. bumpNorm->setName(String::ToString("bumpNormal%d", normalIndex));
  832. bumpNorm->setType("float4");
  833. LangElement* bumpNormDecl = new DecOp(bumpNorm);
  834. meta->addStatement(expandNormalMap(texOp, bumpNormDecl, bumpNorm, fd));
  835. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  836. {
  837. Var* viewToTangent = getInViewToTangent(componentList);
  838. // This var is read from GBufferConditionerHLSL and
  839. // used in the deferred output.
  840. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  841. if (!gbNormal)
  842. {
  843. gbNormal = new Var;
  844. gbNormal->setName("gbNormal");
  845. gbNormal->setType("float3");
  846. meta->addStatement(new GenOp(" @ = @[2];\r\n", new DecOp(gbNormal), viewToTangent));
  847. }
  848. // If we're using SM 3.0 then take advantage of
  849. // dynamic branching to skip layers per-pixel.
  850. if (GFX->getPixelShaderVersion() >= 3.0f)
  851. meta->addStatement(new GenOp(" if ( @ > 0.0f )\r\n", detailBlend));
  852. meta->addStatement(new GenOp(" {\r\n"));
  853. // Normalize is done later...
  854. // Note: The reverse mul order is intentional. Affine matrix.
  855. meta->addStatement(new GenOp(" @ = lerp( @, mul( @.xyz, @ ), min( @, @.w ) );\r\n",
  856. gbNormal, gbNormal, bumpNorm, viewToTangent, detailBlend, inDet));
  857. // End the conditional block.
  858. meta->addStatement(new GenOp(" }\r\n"));
  859. }
  860. output = meta;
  861. }
  862. ShaderFeature::Resources TerrainNormalMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  863. {
  864. Resources res;
  865. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  866. {
  867. return res;
  868. }
  869. S32 featureIndex = 0, firstNormalMapIndex = 0;
  870. for (int idx = 0; idx < fd.features.getCount(); ++idx) {
  871. const FeatureType &type = fd.features.getAt(idx, &featureIndex);
  872. if (type == MFT_TerrainNormalMap) {
  873. firstNormalMapIndex = getMin(firstNormalMapIndex, featureIndex);
  874. }
  875. }
  876. // We only need to process normals during the deferred.
  877. if (getProcessIndex() == firstNormalMapIndex)
  878. {
  879. res.numTexReg += 1;
  880. res.numTex += 1;
  881. }
  882. return res;
  883. }
  884. void TerrainLightMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  885. const MaterialFeatureData &fd )
  886. {
  887. // grab connector texcoord register
  888. Var *inTex = (Var*)LangElement::find( "texCoord" );
  889. if ( !inTex )
  890. return;
  891. // Get the lightmap texture.
  892. Var *lightMap = new Var;
  893. lightMap->setType( "SamplerState" );
  894. lightMap->setName( "lightMapTex" );
  895. lightMap->uniform = true;
  896. lightMap->sampler = true;
  897. lightMap->constNum = Var::getTexUnitNum();
  898. MultiLine *meta = new MultiLine;
  899. // Find or create the lightMask value which is read by
  900. // RTLighting to mask out the lights.
  901. //
  902. // The first light is always the sunlight so we apply
  903. // the shadow mask to only the first channel.
  904. //
  905. Var *lightMask = (Var*)LangElement::find( "lightMask" );
  906. if ( !lightMask )
  907. {
  908. lightMask = new Var( "lightMask", "float4" );
  909. meta->addStatement( new GenOp( " @ = 1;\r\n", new DecOp( lightMask ) ) );
  910. }
  911. Var* lightMapTex = new Var;
  912. lightMapTex->setName("lightMapTexObj");
  913. lightMapTex->setType("Texture2D");
  914. lightMapTex->uniform = true;
  915. lightMapTex->texture = true;
  916. lightMapTex->constNum = lightMap->constNum;
  917. meta->addStatement(new GenOp(" @[0] = @.Sample( @, @.xy ).r;\r\n", lightMask, lightMapTex, lightMap, inTex));
  918. output = meta;
  919. }
  920. ShaderFeature::Resources TerrainLightMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  921. {
  922. Resources res;
  923. res.numTex = 1;
  924. return res;
  925. }
  926. //standard matInfo map contains data of the form .r = bitflags, .g = (will contain AO),
  927. //.b = specular strength, a= spec power.
  928. void TerrainORMMapFeatHLSL::processVert(Vector<ShaderComponent*> &componentList,
  929. const MaterialFeatureData &fd)
  930. {
  931. const S32 detailIndex = getProcessIndex();
  932. // Grab incoming texture coords... the base map feature
  933. // made sure this was created.
  934. Var *inTex = (Var*)LangElement::find("texCoord");
  935. AssertFatal(inTex, "The texture coord is missing!");
  936. // Grab the input position.
  937. Var *inPos = (Var*)LangElement::find("inPosition");
  938. if (!inPos)
  939. inPos = (Var*)LangElement::find("position");
  940. // Get the object space eye position.
  941. Var *eyePos = _getUniformVar("eyePos", "float3", cspPotentialPrimitive);
  942. MultiLine *meta = new MultiLine;
  943. // If we have parallax mapping then make sure we've sent
  944. // the negative view vector to the pixel shader.
  945. if (fd.features.hasFeature(MFT_TerrainParallaxMap) &&
  946. !LangElement::find("outNegViewTS"))
  947. {
  948. // Get the object to tangent transform which
  949. // will consume 3 output registers.
  950. Var *objToTangentSpace = getOutObjToTangentSpace(componentList, meta, fd);
  951. // Now use a single output register to send the negative
  952. // view vector in tangent space to the pixel shader.
  953. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
  954. Var *outNegViewTS = connectComp->getElement(RT_TEXCOORD);
  955. outNegViewTS->setName("outNegViewTS");
  956. outNegViewTS->setStructName("OUT");
  957. outNegViewTS->setType("float3");
  958. meta->addStatement(new GenOp(" @ = mul( @, float3( @ - @.xyz ) );\r\n",
  959. outNegViewTS, objToTangentSpace, eyePos, inPos));
  960. }
  961. // Get the distance from the eye to this vertex.
  962. Var *dist = (Var*)LangElement::find("dist");
  963. if (!dist)
  964. {
  965. dist = new Var;
  966. dist->setType("float");
  967. dist->setName("dist");
  968. meta->addStatement(new GenOp(" @ = distance( @.xyz, @ );\r\n",
  969. new DecOp(dist), inPos, eyePos));
  970. }
  971. // grab connector texcoord register
  972. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
  973. Var *outTex = (Var*)LangElement::find(String::ToString("detCoord%d", detailIndex));
  974. if (outTex == NULL)
  975. {
  976. outTex = connectComp->getElement(RT_TEXCOORD);
  977. outTex->setName(String::ToString("detCoord%d", detailIndex));
  978. outTex->setStructName("OUT");
  979. outTex->setType("float4");
  980. }
  981. // Get the detail scale and fade info.
  982. Var *detScaleAndFade = (Var*)LangElement::find("detailScaleAndFade");
  983. if (detScaleAndFade == NULL)
  984. {
  985. detScaleAndFade = new Var;
  986. detScaleAndFade->setType("float4");
  987. detScaleAndFade->setName("detailScaleAndFade");
  988. detScaleAndFade->uniform = true;
  989. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  990. }
  991. detScaleAndFade->arraySize = mMax(detScaleAndFade->arraySize, detailIndex + 1);
  992. // Setup the detail coord.
  993. //
  994. // NOTE: You see here we scale the texture coord by 'xyx'
  995. // to generate the detail coord. This y is here because
  996. // its scale is flipped to correct for the non negative y
  997. // in texCoord.
  998. //
  999. // See TerrainBaseMapFeatHLSL::processVert().
  1000. //
  1001. meta->addStatement(new GenOp(" @.xyz = @ * @.xyx;\r\n", outTex, inTex, new IndexOp(detScaleAndFade, detailIndex)));
  1002. // And sneak the detail fade thru the w detailCoord.
  1003. meta->addStatement(new GenOp(" @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  1004. outTex, new IndexOp(detScaleAndFade, detailIndex), dist, new IndexOp(detScaleAndFade, detailIndex)));
  1005. output = meta;
  1006. }
  1007. U32 TerrainORMMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  1008. {
  1009. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  1010. }
  1011. void TerrainORMMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  1012. const MaterialFeatureData &fd)
  1013. {
  1014. /// Get the texture coord.
  1015. Var *inDet = _getInDetailCoord(componentList);
  1016. Var *inTex = getVertTexCoord("texCoord");
  1017. Var* detailInfo = _getDetailIdStrengthParallax();
  1018. const S32 compositeIndex = getProcessIndex();
  1019. // Sample the normal map.
  1020. //
  1021. // We take two normal samples and lerp between them for
  1022. // side projection layers... else a single sample.
  1023. LangElement *texOp;
  1024. Var* ormMapArray = _getOrmMapArray();
  1025. Var* ormMapSampler = _getOrmMapSampler();
  1026. if (fd.features.hasFeature(MFT_TerrainSideProject, compositeIndex))
  1027. {
  1028. texOp = new GenOp("lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z )",
  1029. ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex), ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex), inTex);
  1030. }
  1031. else
  1032. texOp = new GenOp("@.Sample(@, float3(@.xy, @.x))", ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex));
  1033. // search for material var
  1034. Var * ormConfig;
  1035. OutputTarget targ = RenderTarget1;
  1036. if (fd.features[MFT_isDeferred])
  1037. {
  1038. targ = RenderTarget2;
  1039. }
  1040. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(targ));
  1041. MultiLine * meta = new MultiLine;
  1042. if (!ormConfig)
  1043. {
  1044. // create color var
  1045. ormConfig = new Var;
  1046. ormConfig->setType("fragout");
  1047. ormConfig->setName(getOutputTargetVarName(targ));
  1048. ormConfig->setStructName("OUT");
  1049. }
  1050. Var *detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", compositeIndex));
  1051. AssertFatal(detailBlend, "The detail blend is missing!");
  1052. String matinfoName(String::ToString("matinfoCol%d", compositeIndex));
  1053. Var *matinfoCol = new Var(matinfoName, "float3");
  1054. if (compositeIndex == 0)
  1055. {
  1056. meta->addStatement(new GenOp(" @ = float4(0.0, 0.0, 0.0, 0.0);\r\n", ormConfig));
  1057. }
  1058. meta->addStatement(new GenOp(" @ = @.rgb;\r\n", new DecOp(matinfoCol), texOp));
  1059. if (fd.features.hasFeature(MFT_InvertRoughness, compositeIndex))
  1060. {
  1061. meta->addStatement(new GenOp(" @.b = 1.0 - @.b;\r\n", matinfoCol, matinfoCol));
  1062. }
  1063. meta->addStatement(new GenOp(" @ = lerp(float3(1.0, 1.0, 0.0), @, @.y * @.w);\r\n", matinfoCol, matinfoCol, new IndexOp(detailInfo, compositeIndex), inDet));
  1064. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  1065. {
  1066. meta->addStatement(new GenOp(" @.gba += @ * @;\r\n", ormConfig, matinfoCol, detailBlend));
  1067. }
  1068. output = meta;
  1069. }
  1070. ShaderFeature::Resources TerrainORMMapFeatHLSL::getResources(const MaterialFeatureData &fd)
  1071. {
  1072. Resources res;
  1073. res.numTex = 1;
  1074. return res;
  1075. }
  1076. // reminder, the matinfo buffer is flags, smooth, ao, metal
  1077. U32 TerrainBlankInfoMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  1078. {
  1079. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::RenderTarget1;
  1080. }
  1081. void TerrainBlankInfoMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  1082. const MaterialFeatureData &fd)
  1083. {
  1084. S32 compositeIndex = getProcessIndex();
  1085. // search for material var
  1086. Var *material;
  1087. OutputTarget ormConfig = RenderTarget1;
  1088. if (fd.features[MFT_isDeferred])
  1089. {
  1090. ormConfig = RenderTarget2;
  1091. }
  1092. material = (Var*)LangElement::find(getOutputTargetVarName(ormConfig));
  1093. MultiLine * meta = new MultiLine;
  1094. if (!material)
  1095. {
  1096. // create color var
  1097. material = new Var;
  1098. material->setType("fragout");
  1099. material->setName(getOutputTargetVarName(ormConfig));
  1100. material->setStructName("OUT");
  1101. }
  1102. if (compositeIndex == 0)
  1103. {
  1104. meta->addStatement(new GenOp(" @ = float4(0.0, 0.0, 0.0, 0.0);\r\n", material));
  1105. }
  1106. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", compositeIndex));
  1107. AssertFatal(detailBlend, "The detail blend is missing!");
  1108. String matinfoName(String::ToString("matinfoCol%d", compositeIndex));
  1109. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  1110. {
  1111. meta->addStatement(new GenOp(" @.gba += float3(@, @, 0.0);\r\n", material, detailBlend, detailBlend));
  1112. }
  1113. output = meta;
  1114. }
  1115. void TerrainHeightMapBlendHLSL::processVert(Vector<ShaderComponent*>& componentList,
  1116. const MaterialFeatureData& fd)
  1117. {
  1118. // We only need to process normals during the deferred.
  1119. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  1120. return;
  1121. MultiLine* meta = new MultiLine;
  1122. // Handle an edge-case when there are no detail-maps available
  1123. if (fd.features.getNextFeatureIndex(MFT_TerrainDetailMap, -1) >= 0)
  1124. {
  1125. // Make sure the world to tangent transform
  1126. // is created and available for the pixel shader.
  1127. getOutViewToTangent(componentList, meta, fd);
  1128. }
  1129. output = meta;
  1130. }
  1131. void TerrainHeightMapBlendHLSL::processPix(Vector<ShaderComponent*>& componentList,
  1132. const MaterialFeatureData& fd)
  1133. {
  1134. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  1135. if (fd.features.hasFeature(MFT_isDeferred))
  1136. target = ShaderFeature::RenderTarget1;
  1137. Var* outColor = (Var*)LangElement::find(getOutputTargetVarName(target));
  1138. if (!outColor)
  1139. return;
  1140. MultiLine* meta = new MultiLine;
  1141. // Count number of detail layers
  1142. int detailCount = 0;
  1143. while (true)
  1144. {
  1145. if(LangElement::find(String::ToString("detailBlend%d", detailCount)) == NULL)
  1146. {
  1147. break;
  1148. }
  1149. ++detailCount;
  1150. }
  1151. if ( detailCount == 0 )
  1152. {
  1153. return;
  1154. }
  1155. // Compute blend factors
  1156. for (S32 idx = 0; idx < detailCount; ++idx)
  1157. {
  1158. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1159. Var* bumpNormal = (Var*)LangElement::find(String::ToString("bumpNormal%d", idx));
  1160. Var* blendDepth = (Var*)LangElement::find(String::ToString("blendDepth%d", idx));
  1161. if (!blendDepth)
  1162. {
  1163. blendDepth = new Var;
  1164. blendDepth->setType("float");
  1165. blendDepth->setName(String::ToString("blendDepth%d", idx));
  1166. blendDepth->uniform = true;
  1167. blendDepth->constSortPos = cspPrimitive;
  1168. }
  1169. Var* blendContrast = (Var*)LangElement::find(String::ToString("blendContrast%d", idx));
  1170. if (!blendContrast)
  1171. {
  1172. blendContrast = new Var;
  1173. blendContrast->setType("float");
  1174. blendContrast->setName(String::ToString("blendContrast%d", idx));
  1175. blendContrast->uniform = true;
  1176. blendContrast->constSortPos = cspPrimitive;
  1177. }
  1178. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1179. if (!detailH)
  1180. {
  1181. detailH = new Var;
  1182. detailH->setType("float");
  1183. detailH->setName(String::ToString("detailH%d", idx));
  1184. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1185. new DecOp(detailH)));
  1186. meta->addStatement(new GenOp(" if (@ > 0.0f) {\r\n", detailBlend));
  1187. if (bumpNormal != NULL)
  1188. {
  1189. meta->addStatement(new GenOp(" @ = clamp(@.a + @, 0.0, 1.0);\r\n",
  1190. detailH, bumpNormal, blendDepth));
  1191. }
  1192. else
  1193. {
  1194. meta->addStatement(new GenOp(" @ = clamp(0.5 + @, 0.0, 1.0);\r\n",
  1195. detailH, blendDepth));
  1196. }
  1197. meta->addStatement(new GenOp(" @ = max((@ * 2.0f - 1.0f) * @ + 0.5f, 0.0f);\r\n",
  1198. detailH, detailH, blendContrast));
  1199. meta->addStatement(new GenOp(" }\r\n"));
  1200. }
  1201. }
  1202. meta->addStatement(new GenOp("\r\n"));
  1203. Var* depth = (Var*)LangElement::find("baseBlendDepth");
  1204. if (depth == NULL)
  1205. {
  1206. depth = new Var;
  1207. depth->setType("float");
  1208. depth->setName("baseBlendDepth");
  1209. depth->uniform = true;
  1210. depth->constSortPos = cspPrimitive;
  1211. }
  1212. Var* ma = (Var*)LangElement::find("ma");
  1213. if (ma == NULL)
  1214. {
  1215. ma = new Var;
  1216. ma->setType("float");
  1217. ma->setName("ma");
  1218. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1219. new DecOp(ma)));
  1220. }
  1221. for (S32 idx = 0; idx < detailCount; ++idx)
  1222. {
  1223. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1224. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1225. meta->addStatement(new GenOp(" @ = max(@, @ + @);\r\n",
  1226. ma, ma, detailH, detailBlend));
  1227. }
  1228. meta->addStatement(new GenOp(" @ -= @;\r\n",
  1229. ma, depth));
  1230. meta->addStatement(new GenOp("\r\n"));
  1231. for (S32 idx = 0; idx < detailCount; ++idx)
  1232. {
  1233. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1234. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1235. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1236. if (!detailB)
  1237. {
  1238. detailB = new Var;
  1239. detailB->setType("float");
  1240. detailB->setName(String::ToString("detailB%d", idx));
  1241. meta->addStatement(new GenOp(" @ = max(@ + @ - @, 0);\r\n",
  1242. new DecOp(detailB), detailH, detailBlend, ma));
  1243. }
  1244. }
  1245. meta->addStatement(new GenOp("\r\n"));
  1246. // Compute albedo
  1247. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n",
  1248. outColor, outColor));
  1249. meta->addStatement(new GenOp(" @.rgb += (",
  1250. outColor));
  1251. for (S32 idx = 0; idx < detailCount; ++idx)
  1252. {
  1253. Var* detailColor = (Var*)LangElement::find(String::ToString("detailColor%d", idx));
  1254. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1255. if (idx > 0)
  1256. {
  1257. meta->addStatement(new GenOp(" + "));
  1258. }
  1259. meta->addStatement(new GenOp("@.rgb * @", detailColor, detailB));
  1260. }
  1261. meta->addStatement(new GenOp(") / ("));
  1262. for (S32 idx = 0; idx < detailCount; ++idx)
  1263. {
  1264. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1265. if (idx > 0)
  1266. {
  1267. meta->addStatement(new GenOp(" + "));
  1268. }
  1269. meta->addStatement(new GenOp("@", detailB));
  1270. }
  1271. meta->addStatement(new GenOp(");\r\n"));
  1272. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n",
  1273. outColor, outColor));
  1274. meta->addStatement(new GenOp("\r\n"));
  1275. // Compute ORM
  1276. Var* ormOutput;
  1277. OutputTarget targ = DefaultTarget;
  1278. if (fd.features[MFT_isDeferred])
  1279. {
  1280. targ = RenderTarget2;
  1281. }
  1282. ormOutput = (Var*)LangElement::find(getOutputTargetVarName(targ));
  1283. meta->addStatement(new GenOp(" @.gba = (",
  1284. ormOutput));
  1285. for (S32 idx = 0; idx < detailCount; ++idx)
  1286. {
  1287. Var* matinfoCol = (Var*)LangElement::find(String::ToString("matinfoCol%d", idx));
  1288. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1289. if (idx > 0)
  1290. {
  1291. meta->addStatement(new GenOp(" + "));
  1292. }
  1293. if (matinfoCol)
  1294. {
  1295. meta->addStatement(new GenOp("@ * @", matinfoCol, detailB));
  1296. }
  1297. else
  1298. {
  1299. meta->addStatement(new GenOp("float3(1.0, 1.0, 0.0) * @", detailB));
  1300. }
  1301. }
  1302. meta->addStatement(new GenOp(") / ("));
  1303. for (S32 idx = 0; idx < detailCount; ++idx)
  1304. {
  1305. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1306. if (idx > 0)
  1307. {
  1308. meta->addStatement(new GenOp(" + "));
  1309. }
  1310. meta->addStatement(new GenOp("@", detailB));
  1311. }
  1312. meta->addStatement(new GenOp(");\r\n"));
  1313. meta->addStatement(new GenOp("\r\n"));
  1314. // Compute normal-specific blending factors
  1315. // LukasPJ: I'm not sure why this is necessary, it might not be.
  1316. Var* normalMa = (Var*)LangElement::find("normalMa");
  1317. if (normalMa == NULL)
  1318. {
  1319. normalMa = new Var;
  1320. normalMa->setType("float");
  1321. normalMa->setName("normalMa");
  1322. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1323. new DecOp(normalMa)));
  1324. }
  1325. for (S32 idx = 0; idx < detailCount; ++idx)
  1326. {
  1327. Var* detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", idx));
  1328. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1329. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1330. meta->addStatement(new GenOp(" @ = max(@, @ + min(@, @.w));\r\n",
  1331. normalMa, normalMa, detailH, detailBlend, detCoord));
  1332. }
  1333. meta->addStatement(new GenOp(" @ -= @;\r\n",
  1334. normalMa, depth));
  1335. meta->addStatement(new GenOp("\r\n"));
  1336. for (S32 idx = 0; idx < detailCount; ++idx)
  1337. {
  1338. Var* detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", idx));
  1339. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1340. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1341. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1342. if (!normalDetailB)
  1343. {
  1344. normalDetailB = new Var;
  1345. normalDetailB->setType("float");
  1346. normalDetailB->setName(String::ToString("normalDetailB%d", idx));
  1347. meta->addStatement(new GenOp(" @ = max(@ + min(@, @.w) - @, 0);\r\n",
  1348. new DecOp(normalDetailB), detailH, detailBlend, detCoord, normalMa));
  1349. }
  1350. }
  1351. // Compute normals
  1352. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  1353. if (!gbNormal)
  1354. {
  1355. gbNormal = new Var;
  1356. gbNormal->setName("gbNormal");
  1357. gbNormal->setType("float3");
  1358. meta->addStatement(new GenOp(" @;\r\n", new DecOp(gbNormal)));
  1359. }
  1360. if (gbNormal != NULL)
  1361. {
  1362. meta->addStatement(new GenOp(" @ = (",
  1363. gbNormal));
  1364. for (S32 idx = 0; idx < detailCount; ++idx)
  1365. {
  1366. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1367. Var* bumpNormal = (Var*)LangElement::find(String::ToString("bumpNormal%d", idx));
  1368. Var* viewToTangent = getInViewToTangent(componentList);
  1369. if (idx > 0)
  1370. {
  1371. meta->addStatement(new GenOp(" + "));
  1372. }
  1373. if (bumpNormal != NULL)
  1374. {
  1375. meta->addStatement(new GenOp("mul(@.xyz, @) * @", bumpNormal, viewToTangent, normalDetailB));
  1376. }
  1377. else
  1378. {
  1379. meta->addStatement(new GenOp("@[2] * @", viewToTangent, normalDetailB));
  1380. }
  1381. }
  1382. meta->addStatement(new GenOp(") / ("));
  1383. for (S32 idx = 0; idx < detailCount; ++idx)
  1384. {
  1385. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1386. if (idx > 0)
  1387. {
  1388. meta->addStatement(new GenOp(" + "));
  1389. }
  1390. meta->addStatement(new GenOp("@", normalDetailB));
  1391. }
  1392. meta->addStatement(new GenOp(");\r\n"));
  1393. }
  1394. output = meta;
  1395. }