terrFeatureHLSL.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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 = (fd.features[MFT_isDeferred]) ? RenderTarget1 : DefaultTarget;
  308. meta->addStatement(new GenOp(" @;\r\n", assignColor(baseColor, Material::Mul, NULL, target)));
  309. Var* ormConfig;
  310. if ((fd.features[MFT_isDeferred]))
  311. {
  312. // Set base ORM info
  313. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(RenderTarget2));
  314. if (!ormConfig)
  315. {
  316. // create color var
  317. ormConfig = new Var;
  318. ormConfig->setType("fragout");
  319. ormConfig->setName(getOutputTargetVarName(RenderTarget2));
  320. ormConfig->setStructName("OUT");
  321. }
  322. }
  323. else
  324. {
  325. ormConfig = new Var("ORMConfig", "float4");
  326. meta->addStatement(new GenOp(" @;\r\n", new DecOp(ormConfig)));
  327. }
  328. meta->addStatement(new GenOp(" @ = float4(0.0, 1.0, 1.0, 0.0);\r\n", ormConfig));
  329. output = meta;
  330. }
  331. ShaderFeature::Resources TerrainBaseMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  332. {
  333. Resources res;
  334. res.numTexReg = 1;
  335. res.numTex = 1;
  336. return res;
  337. }
  338. U32 TerrainBaseMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  339. {
  340. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 | ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  341. }
  342. TerrainDetailMapFeatHLSL::TerrainDetailMapFeatHLSL()
  343. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" )),
  344. mTerrainDep(ShaderGen::smCommonShaderPath + String("/terrain/terrain.hlsl" ))
  345. {
  346. addDependency( &mTorqueDep );
  347. addDependency( &mTerrainDep );
  348. }
  349. void TerrainDetailMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  350. const MaterialFeatureData &fd )
  351. {
  352. const S32 detailIndex = getProcessIndex();
  353. // Grab incoming texture coords... the base map feature
  354. // made sure this was created.
  355. Var *inTex = (Var*)LangElement::find( "texCoord" );
  356. AssertFatal( inTex, "The texture coord is missing!" );
  357. // Grab the input position.
  358. Var *inPos = (Var*)LangElement::find( "inPosition" );
  359. if ( !inPos )
  360. inPos = (Var*)LangElement::find( "position" );
  361. // Get the object space eye position.
  362. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  363. MultiLine *meta = new MultiLine;
  364. // If we have parallax mapping then make sure we've sent
  365. // the negative view vector to the pixel shader.
  366. if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) &&
  367. !LangElement::find( "outNegViewTS" ) )
  368. {
  369. // Get the object to tangent transform which
  370. // will consume 3 output registers.
  371. Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta, fd );
  372. // Now use a single output register to send the negative
  373. // view vector in tangent space to the pixel shader.
  374. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  375. Var *outNegViewTS = connectComp->getElement( RT_TEXCOORD );
  376. outNegViewTS->setName( "outNegViewTS" );
  377. outNegViewTS->setStructName( "OUT" );
  378. outNegViewTS->setType( "float3" );
  379. meta->addStatement( new GenOp( " @ = mul( @, float3( @ - @.xyz ) );\r\n",
  380. outNegViewTS, objToTangentSpace, eyePos, inPos ) );
  381. }
  382. // Get the distance from the eye to this vertex.
  383. Var *dist = (Var*)LangElement::find( "dist" );
  384. if ( !dist )
  385. {
  386. dist = new Var;
  387. dist->setType( "float" );
  388. dist->setName( "dist" );
  389. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  390. new DecOp( dist ), inPos, eyePos ) );
  391. }
  392. // grab connector texcoord register
  393. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  394. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  395. outTex->setName( String::ToString( "detCoord%d", detailIndex ) );
  396. outTex->setStructName( "OUT" );
  397. outTex->setType( "float4" );
  398. // Get the detail scale and fade info.
  399. Var* detScaleAndFade = (Var*)LangElement::find("detailScaleAndFade");
  400. if (detScaleAndFade == NULL)
  401. {
  402. detScaleAndFade = new Var;
  403. detScaleAndFade->setType("float4");
  404. detScaleAndFade->setName("detailScaleAndFade");
  405. detScaleAndFade->uniform = true;
  406. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  407. }
  408. detScaleAndFade->arraySize = mMax(detScaleAndFade->arraySize, detailIndex + 1);
  409. // Setup the detail coord.
  410. //
  411. // NOTE: You see here we scale the texture coord by 'xyx'
  412. // to generate the detail coord. This y is here because
  413. // its scale is flipped to correct for the non negative y
  414. // in texCoord.
  415. //
  416. // See TerrainBaseMapFeatHLSL::processVert().
  417. //
  418. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, new IndexOp(detScaleAndFade, detailIndex) ) );
  419. // And sneak the detail fade thru the w detailCoord.
  420. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  421. outTex, new IndexOp(detScaleAndFade, detailIndex), dist, new IndexOp(detScaleAndFade, detailIndex)) );
  422. output = meta;
  423. }
  424. void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  425. const MaterialFeatureData &fd )
  426. {
  427. const S32 detailIndex = getProcessIndex();
  428. Var *inTex = getVertTexCoord( "texCoord" );
  429. MultiLine *meta = new MultiLine;
  430. // We need the negative tangent space view vector
  431. // as in parallax mapping we step towards the camera.
  432. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  433. if ( !negViewTS &&
  434. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  435. {
  436. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  437. if ( !inNegViewTS )
  438. {
  439. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  440. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  441. inNegViewTS->setName( "outNegViewTS" );
  442. inNegViewTS->setStructName( "IN" );
  443. inNegViewTS->setType( "float3" );
  444. }
  445. negViewTS = new Var( "negViewTS", "float3" );
  446. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  447. }
  448. // Get the layer samples.
  449. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  450. if ( !layerSample )
  451. {
  452. layerSample = new Var;
  453. layerSample->setType( "float4" );
  454. layerSample->setName( "layerSample" );
  455. // Get the layer texture var
  456. Var *layerTex = new Var;
  457. layerTex->setType( "SamplerState" );
  458. layerTex->setName( "layerTex" );
  459. layerTex->uniform = true;
  460. layerTex->sampler = true;
  461. layerTex->constNum = Var::getTexUnitNum();
  462. Var* layerTexObj = new Var;
  463. layerTexObj->setName("layerTexObj");
  464. layerTexObj->setType("Texture2D");
  465. layerTexObj->uniform = true;
  466. layerTexObj->texture = true;
  467. layerTexObj->constNum = layerTex->constNum;
  468. // Read the layer texture to get the samples.
  469. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  470. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  471. }
  472. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  473. if ( !layerSize )
  474. {
  475. layerSize = new Var;
  476. layerSize->setType( "float" );
  477. layerSize->setName( "layerSize" );
  478. layerSize->uniform = true;
  479. layerSize->constSortPos = cspPass;
  480. }
  481. // Grab the incoming detail coord.
  482. Var *inDet = _getInDetailCoord( componentList );
  483. // Get the detail id.
  484. Var *detailInfo = _getDetailIdStrengthParallax();
  485. // Create the detail blend var.
  486. Var *detailBlend = new Var;
  487. detailBlend->setType( "float" );
  488. detailBlend->setName( String::ToString( "detailBlend%d", detailIndex ) );
  489. // Calculate the blend for this detail texture.
  490. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  491. new DecOp( detailBlend ), new IndexOp(detailInfo, detailIndex), inTex, layerSize, layerSample ) );
  492. // If we had a parallax feature... then factor in the parallax
  493. // amount so that it fades out with the layer blending.
  494. if (fd.features.hasFeature(MFT_TerrainParallaxMap, detailIndex))
  495. {
  496. Var* normalMapArray = _getNormalMapArray();
  497. Var* normalMapSampler = _getNormalMapSampler();
  498. // Call the library function to do the rest.
  499. if (fd.features.hasFeature(MFT_IsBC3nm, detailIndex))
  500. {
  501. meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnmTexArray( @, @, float3(@.xy, @.x), @, @.z * @ );\r\n",
  502. inDet, normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, detailIndex), negViewTS, new IndexOp(detailInfo, detailIndex), detailBlend));
  503. }
  504. else
  505. {
  506. meta->addStatement(new GenOp(" @.xy += parallaxOffsetTexArray( @, @, float3(@.xy, @.x), @, @.z * @ );\r\n",
  507. inDet, normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, detailIndex), negViewTS, new IndexOp(detailInfo, detailIndex), detailBlend));
  508. }
  509. }
  510. Var *detailColor = (Var*)LangElement::find(String::ToString("detailColor%d", detailIndex));
  511. if ( !detailColor )
  512. {
  513. detailColor = new Var;
  514. detailColor->setType( "float4" );
  515. detailColor->setName( String::ToString("detailColor%d", detailIndex) );
  516. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  517. }
  518. //Sampled detail texture that is not expanded
  519. Var* detailMapArray = _getDetailMapArray();
  520. Var* detailMapSampler = _getDetailMapSampler();
  521. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  522. {
  523. meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z ) * 2.0 ) - 1.0;\r\n",
  524. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), inTex));
  525. }
  526. else
  527. {
  528. meta->addStatement(new GenOp(" @ = ( @.Sample( @, float3(@.xy, @.x) ) * 2.0 ) - 1.0;\r\n",
  529. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex)));
  530. }
  531. meta->addStatement(new GenOp(" @ *= @.y * @.w;\r\n",
  532. detailColor, new IndexOp(detailInfo, detailIndex), inDet));
  533. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  534. {
  535. // Check to see if we have a gbuffer normal.
  536. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  537. // If we have a gbuffer normal and we don't have a
  538. // normal map feature then we need to lerp in a
  539. // default normal else the normals below this layer
  540. // will show thru.
  541. if (gbNormal &&
  542. !fd.features.hasFeature(MFT_TerrainNormalMap, detailIndex))
  543. {
  544. Var* viewToTangent = getInViewToTangent(componentList);
  545. meta->addStatement(new GenOp(" @ = lerp( @, @[2], min( @, @.w ) );\r\n",
  546. gbNormal, gbNormal, viewToTangent, detailBlend, inDet));
  547. }
  548. // If we're using SM 3.0 then take advantage of
  549. // dynamic branching to skip layers per-pixel.
  550. if (GFX->getPixelShaderVersion() >= 3.0f)
  551. meta->addStatement(new GenOp(" if ( @ > 0.0f )\r\n", detailBlend));
  552. meta->addStatement(new GenOp(" {\r\n"));
  553. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  554. if (fd.features.hasFeature(MFT_isDeferred))
  555. target = ShaderFeature::RenderTarget1;
  556. Var* outColor = (Var*)LangElement::find(getOutputTargetVarName(target));
  557. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n", outColor, outColor));
  558. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  559. outColor, detailColor, detailBlend));
  560. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n", outColor, outColor));
  561. meta->addStatement(new GenOp(" }\r\n"));
  562. }
  563. output = meta;
  564. }
  565. ShaderFeature::Resources TerrainDetailMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  566. {
  567. Resources res;
  568. if ( getProcessIndex() == 0 )
  569. {
  570. // If this is the first detail pass then we
  571. // samples from the layer tex.
  572. res.numTex += 1;
  573. res.numTexReg += 1;
  574. // Add Detail TextureArray
  575. res.numTex += 1;
  576. res.numTexReg += 1;
  577. }
  578. return res;
  579. }
  580. U32 TerrainDetailMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  581. {
  582. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  583. }
  584. TerrainMacroMapFeatHLSL::TerrainMacroMapFeatHLSL()
  585. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" )),
  586. mTerrainDep(ShaderGen::smCommonShaderPath + String("/terrain/terrain.hlsl" ))
  587. {
  588. addDependency( &mTorqueDep );
  589. addDependency( &mTerrainDep );
  590. }
  591. void TerrainMacroMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  592. const MaterialFeatureData &fd )
  593. {
  594. const S32 detailIndex = getProcessIndex();
  595. // Grab incoming texture coords... the base map feature
  596. // made sure this was created.
  597. Var *inTex = (Var*)LangElement::find( "texCoord" );
  598. AssertFatal( inTex, "The texture coord is missing!" );
  599. // Grab the input position.
  600. Var *inPos = (Var*)LangElement::find( "inPosition" );
  601. if ( !inPos )
  602. inPos = (Var*)LangElement::find( "position" );
  603. // Get the object space eye position.
  604. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  605. MultiLine *meta = new MultiLine;
  606. // Get the distance from the eye to this vertex.
  607. Var *dist = (Var*)LangElement::find( "macroDist" );
  608. if ( !dist )
  609. {
  610. dist = new Var;
  611. dist->setType( "float" );
  612. dist->setName( "macroDist" );
  613. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  614. new DecOp( dist ), inPos, eyePos ) );
  615. }
  616. // grab connector texcoord register
  617. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  618. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  619. outTex->setName( String::ToString( "macroCoord%d", detailIndex ) );
  620. outTex->setStructName( "OUT" );
  621. outTex->setType( "float4" );
  622. // Get the detail scale and fade info.
  623. Var* macroScaleAndFade = (Var*)LangElement::find("macroScaleAndFade");
  624. if (macroScaleAndFade == NULL)
  625. {
  626. macroScaleAndFade = new Var;
  627. macroScaleAndFade->setType("float4");
  628. macroScaleAndFade->setName("macroScaleAndFade");
  629. macroScaleAndFade->uniform = true;
  630. macroScaleAndFade->constSortPos = cspPotentialPrimitive;
  631. }
  632. macroScaleAndFade->arraySize = mMax(macroScaleAndFade->arraySize, detailIndex + 1);
  633. // Setup the detail coord.
  634. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, new IndexOp(macroScaleAndFade, detailIndex)) );
  635. // And sneak the detail fade thru the w detailCoord.
  636. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  637. outTex, new IndexOp(macroScaleAndFade, detailIndex), dist, new IndexOp(macroScaleAndFade, detailIndex)) );
  638. output = meta;
  639. }
  640. void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  641. const MaterialFeatureData &fd )
  642. {
  643. const S32 detailIndex = getProcessIndex();
  644. Var *inTex = getVertTexCoord( "texCoord" );
  645. MultiLine *meta = new MultiLine;
  646. // We need the negative tangent space view vector
  647. // as in parallax mapping we step towards the camera.
  648. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  649. if ( !negViewTS &&
  650. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  651. {
  652. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  653. if ( !inNegViewTS )
  654. {
  655. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  656. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  657. inNegViewTS->setName( "outNegViewTS" );
  658. inNegViewTS->setStructName( "IN" );
  659. inNegViewTS->setType( "float3" );
  660. }
  661. negViewTS = new Var( "negViewTS", "float3" );
  662. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  663. }
  664. // Get the layer samples.
  665. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  666. if ( !layerSample )
  667. {
  668. layerSample = new Var;
  669. layerSample->setType( "float4" );
  670. layerSample->setName( "layerSample" );
  671. // Get the layer texture var
  672. Var *layerTex = new Var;
  673. layerTex->setType( "SamplerState" );
  674. layerTex->setName( "macrolayerTex" );
  675. layerTex->uniform = true;
  676. layerTex->sampler = true;
  677. layerTex->constNum = Var::getTexUnitNum();
  678. // Read the layer texture to get the samples.
  679. Var *layerTexObj = new Var;
  680. layerTexObj->setType("Texture2D");
  681. layerTexObj->setName("macroLayerTexObj");
  682. layerTexObj->uniform = true;
  683. layerTexObj->texture = true;
  684. layerTexObj->constNum = layerTex->constNum;
  685. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  686. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  687. }
  688. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  689. if ( !layerSize )
  690. {
  691. layerSize = new Var;
  692. layerSize->setType( "float" );
  693. layerSize->setName( "layerSize" );
  694. layerSize->uniform = true;
  695. layerSize->constSortPos = cspPass;
  696. }
  697. // Grab the incoming detail coord.
  698. Var *inDet = _getInMacroCoord( componentList );
  699. // Get the detail id.
  700. Var *detailInfo = _getMacroIdStrengthParallax();
  701. // Create the detail blend var.
  702. Var *detailBlend = new Var;
  703. detailBlend->setType( "float" );
  704. detailBlend->setName( String::ToString( "macroBlend%d", detailIndex ) );
  705. // Calculate the blend for this detail texture.
  706. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  707. new DecOp( detailBlend ), new IndexOp(detailInfo, detailIndex), inTex, layerSize, layerSample ) );
  708. // Check to see if we have a gbuffer normal.
  709. Var *gbNormal = (Var*)LangElement::find( "gbNormal" );
  710. // If we have a gbuffer normal and we don't have a
  711. // normal map feature then we need to lerp in a
  712. // default normal else the normals below this layer
  713. // will show thru.
  714. if ( gbNormal &&
  715. !fd.features.hasFeature( MFT_TerrainNormalMap, detailIndex ) )
  716. {
  717. Var *viewToTangent = getInViewToTangent( componentList );
  718. meta->addStatement( new GenOp( " @ = lerp( @, @[2], min( @, @.w ) );\r\n",
  719. gbNormal, gbNormal, viewToTangent, detailBlend, inDet ) );
  720. }
  721. Var *detailColor = (Var*)LangElement::find("macroColor");
  722. if (!detailColor)
  723. {
  724. detailColor = new Var;
  725. detailColor->setType( "float4" );
  726. detailColor->setName( "macroColor" );
  727. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  728. }
  729. // If we're using SM 3.0 then take advantage of
  730. // dynamic branching to skip layers per-pixel.
  731. if ( GFX->getPixelShaderVersion() >= 3.0f )
  732. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  733. meta->addStatement( new GenOp( " {\r\n" ) );
  734. Var* detailMapArray = _getDetailMapArray();
  735. Var* detailMapSampler = _getDetailMapSampler();
  736. // Note that we're doing the standard greyscale detail
  737. // map technique here which can darken and lighten the
  738. // diffuse texture.
  739. //
  740. // We take two color samples and lerp between them for
  741. // side projection layers... else a single sample.
  742. //
  743. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  744. {
  745. meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z ) * 2.0 ) - 1.0;\r\n",
  746. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex), inTex));
  747. }
  748. else
  749. {
  750. meta->addStatement(new GenOp(" @ = ( @.Sample( @, float3(@.xy, @.x) ) * 2.0 ) - 1.0;\r\n",
  751. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex)));
  752. }
  753. meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
  754. detailColor, new IndexOp(detailInfo, detailIndex), inDet ) );
  755. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  756. if (fd.features.hasFeature(MFT_isDeferred))
  757. target= ShaderFeature::RenderTarget1;
  758. Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
  759. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n", outColor, outColor));
  760. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  761. outColor, detailColor, detailBlend));
  762. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n", outColor, outColor));
  763. meta->addStatement( new GenOp( " }\r\n" ) );
  764. output = meta;
  765. }
  766. ShaderFeature::Resources TerrainMacroMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  767. {
  768. Resources res;
  769. if ( getProcessIndex() == 0 )
  770. {
  771. // If this is the first detail pass then we
  772. // samples from the layer tex.
  773. res.numTex += 1;
  774. }
  775. res.numTex += 1;
  776. // Finally we always send the detail texture
  777. // coord to the pixel shader.
  778. res.numTexReg += 1;
  779. return res;
  780. }
  781. U32 TerrainMacroMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  782. {
  783. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  784. }
  785. void TerrainNormalMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  786. const MaterialFeatureData &fd )
  787. {
  788. // We only need to process normals during the deferred.
  789. if ( !fd.features.hasFeature( MFT_DeferredConditioner ) )
  790. return;
  791. MultiLine *meta = new MultiLine;
  792. if ( !fd.features.hasFeature(MFT_TerrainHeightBlend) )
  793. {
  794. // Make sure the world to tangent transform
  795. // is created and available for the pixel shader.
  796. getOutViewToTangent(componentList, meta, fd);
  797. }
  798. output = meta;
  799. }
  800. void TerrainNormalMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  801. const MaterialFeatureData &fd )
  802. {
  803. // We only need to process normals during the deferred.
  804. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  805. return;
  806. MultiLine *meta = new MultiLine;
  807. const S32 normalIndex = getProcessIndex();
  808. Var *detailBlend = (Var*)LangElement::find( String::ToString( "detailBlend%d", normalIndex ) );
  809. AssertFatal( detailBlend, "The detail blend is missing!" );
  810. /// Get the texture coord.
  811. Var* inDet = _getInDetailCoord(componentList);
  812. Var* inTex = getVertTexCoord("texCoord");
  813. Var* detailInfo = _getDetailIdStrengthParallax();
  814. // Sample the normal map.
  815. //
  816. // We take two normal samples and lerp between them for
  817. // side projection layers... else a single sample.
  818. LangElement* texOp;
  819. Var* normalMapSampler = _getNormalMapSampler();
  820. Var* normalMapArray = _getNormalMapArray();
  821. if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex))
  822. {
  823. texOp = new GenOp("lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z )",
  824. normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex), normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex), inTex);
  825. }
  826. else
  827. texOp = new GenOp("@.Sample(@, float3(@.xy, @.x))", normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex));
  828. // create bump normal
  829. Var* bumpNorm = new Var;
  830. bumpNorm->setName(String::ToString("bumpNormal%d", normalIndex));
  831. bumpNorm->setType("float4");
  832. LangElement* bumpNormDecl = new DecOp(bumpNorm);
  833. meta->addStatement(expandNormalMap(texOp, bumpNormDecl, bumpNorm, fd));
  834. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  835. {
  836. Var* viewToTangent = getInViewToTangent(componentList);
  837. // This var is read from GBufferConditionerHLSL and
  838. // used in the deferred output.
  839. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  840. if (!gbNormal)
  841. {
  842. gbNormal = new Var;
  843. gbNormal->setName("gbNormal");
  844. gbNormal->setType("float3");
  845. meta->addStatement(new GenOp(" @ = @[2];\r\n", new DecOp(gbNormal), viewToTangent));
  846. }
  847. // If we're using SM 3.0 then take advantage of
  848. // dynamic branching to skip layers per-pixel.
  849. if (GFX->getPixelShaderVersion() >= 3.0f)
  850. meta->addStatement(new GenOp(" if ( @ > 0.0f )\r\n", detailBlend));
  851. meta->addStatement(new GenOp(" {\r\n"));
  852. // Normalize is done later...
  853. // Note: The reverse mul order is intentional. Affine matrix.
  854. meta->addStatement(new GenOp(" @ = lerp( @, mul( @.xyz, @ ), min( @, @.w ) );\r\n",
  855. gbNormal, gbNormal, bumpNorm, viewToTangent, detailBlend, inDet));
  856. // End the conditional block.
  857. meta->addStatement(new GenOp(" }\r\n"));
  858. }
  859. output = meta;
  860. }
  861. ShaderFeature::Resources TerrainNormalMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  862. {
  863. Resources res;
  864. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  865. {
  866. return res;
  867. }
  868. S32 featureIndex = 0, firstNormalMapIndex = 0;
  869. for (int idx = 0; idx < fd.features.getCount(); ++idx) {
  870. const FeatureType &type = fd.features.getAt(idx, &featureIndex);
  871. if (type == MFT_TerrainNormalMap) {
  872. firstNormalMapIndex = getMin(firstNormalMapIndex, featureIndex);
  873. }
  874. }
  875. // We only need to process normals during the deferred.
  876. if (getProcessIndex() == firstNormalMapIndex)
  877. {
  878. res.numTexReg += 1;
  879. res.numTex += 1;
  880. }
  881. return res;
  882. }
  883. void TerrainLightMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  884. const MaterialFeatureData &fd )
  885. {
  886. // grab connector texcoord register
  887. Var *inTex = (Var*)LangElement::find( "texCoord" );
  888. if ( !inTex )
  889. return;
  890. // Get the lightmap texture.
  891. Var *lightMap = new Var;
  892. lightMap->setType( "SamplerState" );
  893. lightMap->setName( "lightMapTex" );
  894. lightMap->uniform = true;
  895. lightMap->sampler = true;
  896. lightMap->constNum = Var::getTexUnitNum();
  897. MultiLine *meta = new MultiLine;
  898. // Find or create the lightMask value which is read by
  899. // RTLighting to mask out the lights.
  900. //
  901. // The first light is always the sunlight so we apply
  902. // the shadow mask to only the first channel.
  903. //
  904. Var *lightMask = (Var*)LangElement::find( "lightMask" );
  905. if ( !lightMask )
  906. {
  907. lightMask = new Var( "lightMask", "float4" );
  908. meta->addStatement( new GenOp( " @ = 1;\r\n", new DecOp( lightMask ) ) );
  909. }
  910. Var* lightMapTex = new Var;
  911. lightMapTex->setName("lightMapTexObj");
  912. lightMapTex->setType("Texture2D");
  913. lightMapTex->uniform = true;
  914. lightMapTex->texture = true;
  915. lightMapTex->constNum = lightMap->constNum;
  916. meta->addStatement(new GenOp(" @[0] = @.Sample( @, @.xy ).r;\r\n", lightMask, lightMapTex, lightMap, inTex));
  917. output = meta;
  918. }
  919. ShaderFeature::Resources TerrainLightMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  920. {
  921. Resources res;
  922. res.numTex = 1;
  923. return res;
  924. }
  925. //standard matInfo map contains data of the form .r = bitflags, .g = (will contain AO),
  926. //.b = specular strength, a= spec power.
  927. void TerrainORMMapFeatHLSL::processVert(Vector<ShaderComponent*> &componentList,
  928. const MaterialFeatureData &fd)
  929. {
  930. const S32 detailIndex = getProcessIndex();
  931. // Grab incoming texture coords... the base map feature
  932. // made sure this was created.
  933. Var *inTex = (Var*)LangElement::find("texCoord");
  934. AssertFatal(inTex, "The texture coord is missing!");
  935. // Grab the input position.
  936. Var *inPos = (Var*)LangElement::find("inPosition");
  937. if (!inPos)
  938. inPos = (Var*)LangElement::find("position");
  939. // Get the object space eye position.
  940. Var *eyePos = _getUniformVar("eyePos", "float3", cspPotentialPrimitive);
  941. MultiLine *meta = new MultiLine;
  942. // If we have parallax mapping then make sure we've sent
  943. // the negative view vector to the pixel shader.
  944. if (fd.features.hasFeature(MFT_TerrainParallaxMap) &&
  945. !LangElement::find("outNegViewTS"))
  946. {
  947. // Get the object to tangent transform which
  948. // will consume 3 output registers.
  949. Var *objToTangentSpace = getOutObjToTangentSpace(componentList, meta, fd);
  950. // Now use a single output register to send the negative
  951. // view vector in tangent space to the pixel shader.
  952. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
  953. Var *outNegViewTS = connectComp->getElement(RT_TEXCOORD);
  954. outNegViewTS->setName("outNegViewTS");
  955. outNegViewTS->setStructName("OUT");
  956. outNegViewTS->setType("float3");
  957. meta->addStatement(new GenOp(" @ = mul( @, float3( @ - @.xyz ) );\r\n",
  958. outNegViewTS, objToTangentSpace, eyePos, inPos));
  959. }
  960. // Get the distance from the eye to this vertex.
  961. Var *dist = (Var*)LangElement::find("dist");
  962. if (!dist)
  963. {
  964. dist = new Var;
  965. dist->setType("float");
  966. dist->setName("dist");
  967. meta->addStatement(new GenOp(" @ = distance( @.xyz, @ );\r\n",
  968. new DecOp(dist), inPos, eyePos));
  969. }
  970. // grab connector texcoord register
  971. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
  972. Var *outTex = (Var*)LangElement::find(String::ToString("detCoord%d", detailIndex));
  973. if (outTex == NULL)
  974. {
  975. outTex = connectComp->getElement(RT_TEXCOORD);
  976. outTex->setName(String::ToString("detCoord%d", detailIndex));
  977. outTex->setStructName("OUT");
  978. outTex->setType("float4");
  979. }
  980. // Get the detail scale and fade info.
  981. Var *detScaleAndFade = (Var*)LangElement::find("detailScaleAndFade");
  982. if (detScaleAndFade == NULL)
  983. {
  984. detScaleAndFade = new Var;
  985. detScaleAndFade->setType("float4");
  986. detScaleAndFade->setName("detailScaleAndFade");
  987. detScaleAndFade->uniform = true;
  988. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  989. }
  990. detScaleAndFade->arraySize = mMax(detScaleAndFade->arraySize, detailIndex + 1);
  991. // Setup the detail coord.
  992. //
  993. // NOTE: You see here we scale the texture coord by 'xyx'
  994. // to generate the detail coord. This y is here because
  995. // its scale is flipped to correct for the non negative y
  996. // in texCoord.
  997. //
  998. // See TerrainBaseMapFeatHLSL::processVert().
  999. //
  1000. meta->addStatement(new GenOp(" @.xyz = @ * @.xyx;\r\n", outTex, inTex, new IndexOp(detScaleAndFade, detailIndex)));
  1001. // And sneak the detail fade thru the w detailCoord.
  1002. meta->addStatement(new GenOp(" @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  1003. outTex, new IndexOp(detScaleAndFade, detailIndex), dist, new IndexOp(detScaleAndFade, detailIndex)));
  1004. output = meta;
  1005. }
  1006. U32 TerrainORMMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  1007. {
  1008. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  1009. }
  1010. void TerrainORMMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  1011. const MaterialFeatureData &fd)
  1012. {
  1013. /// Get the texture coord.
  1014. Var *inDet = _getInDetailCoord(componentList);
  1015. Var *inTex = getVertTexCoord("texCoord");
  1016. Var* detailInfo = _getDetailIdStrengthParallax();
  1017. const S32 compositeIndex = getProcessIndex();
  1018. // Sample the normal map.
  1019. //
  1020. // We take two normal samples and lerp between them for
  1021. // side projection layers... else a single sample.
  1022. LangElement *texOp;
  1023. Var* ormMapArray = _getOrmMapArray();
  1024. Var* ormMapSampler = _getOrmMapSampler();
  1025. if (fd.features.hasFeature(MFT_TerrainSideProject, compositeIndex))
  1026. {
  1027. texOp = new GenOp("lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z )",
  1028. ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex), ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex), inTex);
  1029. }
  1030. else
  1031. texOp = new GenOp("@.Sample(@, float3(@.xy, @.x))", ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex));
  1032. MultiLine* meta = new MultiLine;
  1033. // search for material var
  1034. Var* ormConfig;
  1035. if ((fd.features[MFT_isDeferred]))
  1036. {
  1037. // Set base ORM info
  1038. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(RenderTarget2));
  1039. if (!ormConfig)
  1040. {
  1041. // create color var
  1042. ormConfig = new Var;
  1043. ormConfig->setType("fragout");
  1044. ormConfig->setName(getOutputTargetVarName(RenderTarget2));
  1045. ormConfig->setStructName("OUT");
  1046. }
  1047. }
  1048. else
  1049. {
  1050. ormConfig = (Var*)LangElement::find("ORMConfig");
  1051. if (!ormConfig)
  1052. {
  1053. ormConfig = new Var("ORMConfig", "float4");
  1054. meta->addStatement(new GenOp(" @;\r\n", new DecOp(ormConfig)));
  1055. }
  1056. }
  1057. if (compositeIndex == 0)
  1058. {
  1059. meta->addStatement(new GenOp(" @ = float4(0.0, 0.0, 0.0, 0.0);\r\n", ormConfig));
  1060. }
  1061. Var *detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", compositeIndex));
  1062. AssertFatal(detailBlend, "The detail blend is missing!");
  1063. String matinfoName(String::ToString("matinfoCol%d", compositeIndex));
  1064. Var *matinfoCol = new Var(matinfoName, "float3");
  1065. meta->addStatement(new GenOp(" @ = @.rgb;\r\n", new DecOp(matinfoCol), texOp));
  1066. if (fd.features.hasFeature(MFT_InvertRoughness, compositeIndex))
  1067. {
  1068. meta->addStatement(new GenOp(" @.b = 1.0 - @.b;\r\n", matinfoCol, matinfoCol));
  1069. }
  1070. meta->addStatement(new GenOp(" @ = lerp(float3(1.0, 1.0, 0.0), @, @.y * @.w);\r\n", matinfoCol, matinfoCol, new IndexOp(detailInfo, compositeIndex), inDet));
  1071. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  1072. {
  1073. meta->addStatement(new GenOp(" @.gba += @ * @;\r\n", ormConfig, matinfoCol, detailBlend));
  1074. }
  1075. output = meta;
  1076. }
  1077. ShaderFeature::Resources TerrainORMMapFeatHLSL::getResources(const MaterialFeatureData &fd)
  1078. {
  1079. Resources res;
  1080. res.numTex = 1;
  1081. return res;
  1082. }
  1083. void TerrainBlankInfoMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  1084. const MaterialFeatureData &fd)
  1085. {
  1086. S32 compositeIndex = getProcessIndex();
  1087. MultiLine * meta = new MultiLine; Var* ormConfig;
  1088. if ((fd.features[MFT_isDeferred]))
  1089. {
  1090. // Set base ORM info
  1091. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(RenderTarget2));
  1092. if (!ormConfig)
  1093. {
  1094. // create color var
  1095. ormConfig = new Var;
  1096. ormConfig->setType("fragout");
  1097. ormConfig->setName(getOutputTargetVarName(RenderTarget2));
  1098. ormConfig->setStructName("OUT");
  1099. }
  1100. }
  1101. else
  1102. {
  1103. ormConfig = (Var*)LangElement::find("ORMConfig");
  1104. if (!ormConfig)
  1105. {
  1106. ormConfig = new Var("ORMConfig", "float4");
  1107. meta->addStatement(new GenOp(" @;\r\n", new DecOp(ormConfig)));
  1108. }
  1109. }
  1110. if (compositeIndex == 0)
  1111. {
  1112. meta->addStatement(new GenOp(" @ = float4(0.0, 0.0, 0.0, 0.0);\r\n", ormConfig));
  1113. }
  1114. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", compositeIndex));
  1115. AssertFatal(detailBlend, "The detail blend is missing!");
  1116. String matinfoName(String::ToString("matinfoCol%d", compositeIndex));
  1117. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  1118. {
  1119. meta->addStatement(new GenOp(" @.gba += float3(@, @, 0.0);\r\n", ormConfig, detailBlend, detailBlend));
  1120. }
  1121. output = meta;
  1122. }
  1123. void TerrainHeightMapBlendHLSL::processVert(Vector<ShaderComponent*>& componentList,
  1124. const MaterialFeatureData& fd)
  1125. {
  1126. // We only need to process normals during the deferred.
  1127. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  1128. return;
  1129. MultiLine* meta = new MultiLine;
  1130. // Handle an edge-case when there are no detail-maps available
  1131. if (fd.features.getNextFeatureIndex(MFT_TerrainDetailMap, -1) >= 0)
  1132. {
  1133. // Make sure the world to tangent transform
  1134. // is created and available for the pixel shader.
  1135. getOutViewToTangent(componentList, meta, fd);
  1136. }
  1137. output = meta;
  1138. }
  1139. void TerrainHeightMapBlendHLSL::processPix(Vector<ShaderComponent*>& componentList,
  1140. const MaterialFeatureData& fd)
  1141. {
  1142. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  1143. if (fd.features.hasFeature(MFT_isDeferred))
  1144. target = ShaderFeature::RenderTarget1;
  1145. Var* outColor = (Var*)LangElement::find(getOutputTargetVarName(target));
  1146. if (!outColor)
  1147. return;
  1148. MultiLine* meta = new MultiLine;
  1149. // Count number of detail layers
  1150. int detailCount = 0;
  1151. while (true)
  1152. {
  1153. if(LangElement::find(String::ToString("detailBlend%d", detailCount)) == NULL)
  1154. {
  1155. break;
  1156. }
  1157. ++detailCount;
  1158. }
  1159. if ( detailCount == 0 )
  1160. {
  1161. return;
  1162. }
  1163. // Compute blend factors
  1164. for (S32 idx = 0; idx < detailCount; ++idx)
  1165. {
  1166. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1167. Var* bumpNormal = (Var*)LangElement::find(String::ToString("bumpNormal%d", idx));
  1168. Var* blendDepth = (Var*)LangElement::find(String::ToString("blendDepth%d", idx));
  1169. if (!blendDepth)
  1170. {
  1171. blendDepth = new Var;
  1172. blendDepth->setType("float");
  1173. blendDepth->setName(String::ToString("blendDepth%d", idx));
  1174. blendDepth->uniform = true;
  1175. blendDepth->constSortPos = cspPrimitive;
  1176. }
  1177. Var* blendContrast = (Var*)LangElement::find(String::ToString("blendContrast%d", idx));
  1178. if (!blendContrast)
  1179. {
  1180. blendContrast = new Var;
  1181. blendContrast->setType("float");
  1182. blendContrast->setName(String::ToString("blendContrast%d", idx));
  1183. blendContrast->uniform = true;
  1184. blendContrast->constSortPos = cspPrimitive;
  1185. }
  1186. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1187. if (!detailH)
  1188. {
  1189. detailH = new Var;
  1190. detailH->setType("float");
  1191. detailH->setName(String::ToString("detailH%d", idx));
  1192. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1193. new DecOp(detailH)));
  1194. meta->addStatement(new GenOp(" if (@ > 0.0f) {\r\n", detailBlend));
  1195. if (bumpNormal != NULL)
  1196. {
  1197. meta->addStatement(new GenOp(" @ = clamp(@.a + @, 0.0, 1.0);\r\n",
  1198. detailH, bumpNormal, blendDepth));
  1199. }
  1200. else
  1201. {
  1202. meta->addStatement(new GenOp(" @ = clamp(0.5 + @, 0.0, 1.0);\r\n",
  1203. detailH, blendDepth));
  1204. }
  1205. meta->addStatement(new GenOp(" @ = max((@ * 2.0f - 1.0f) * @ + 0.5f, 0.0f);\r\n",
  1206. detailH, detailH, blendContrast));
  1207. meta->addStatement(new GenOp(" }\r\n"));
  1208. }
  1209. }
  1210. meta->addStatement(new GenOp("\r\n"));
  1211. Var* depth = (Var*)LangElement::find("baseBlendDepth");
  1212. if (depth == NULL)
  1213. {
  1214. depth = new Var;
  1215. depth->setType("float");
  1216. depth->setName("baseBlendDepth");
  1217. depth->uniform = true;
  1218. depth->constSortPos = cspPrimitive;
  1219. }
  1220. Var* ma = (Var*)LangElement::find("ma");
  1221. if (ma == NULL)
  1222. {
  1223. ma = new Var;
  1224. ma->setType("float");
  1225. ma->setName("ma");
  1226. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1227. new DecOp(ma)));
  1228. }
  1229. for (S32 idx = 0; idx < detailCount; ++idx)
  1230. {
  1231. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1232. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1233. meta->addStatement(new GenOp(" @ = max(@, @ + @);\r\n",
  1234. ma, ma, detailH, detailBlend));
  1235. }
  1236. meta->addStatement(new GenOp(" @ -= @;\r\n",
  1237. ma, depth));
  1238. meta->addStatement(new GenOp("\r\n"));
  1239. for (S32 idx = 0; idx < detailCount; ++idx)
  1240. {
  1241. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1242. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1243. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1244. if (!detailB)
  1245. {
  1246. detailB = new Var;
  1247. detailB->setType("float");
  1248. detailB->setName(String::ToString("detailB%d", idx));
  1249. meta->addStatement(new GenOp(" @ = max(@ + @ - @, 0);\r\n",
  1250. new DecOp(detailB), detailH, detailBlend, ma));
  1251. }
  1252. }
  1253. meta->addStatement(new GenOp("\r\n"));
  1254. // Compute albedo
  1255. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n",
  1256. outColor, outColor));
  1257. meta->addStatement(new GenOp(" @.rgb += (",
  1258. outColor));
  1259. for (S32 idx = 0; idx < detailCount; ++idx)
  1260. {
  1261. Var* detailColor = (Var*)LangElement::find(String::ToString("detailColor%d", idx));
  1262. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1263. if (idx > 0)
  1264. {
  1265. meta->addStatement(new GenOp(" + "));
  1266. }
  1267. meta->addStatement(new GenOp("@.rgb * @", detailColor, detailB));
  1268. }
  1269. meta->addStatement(new GenOp(") / ("));
  1270. for (S32 idx = 0; idx < detailCount; ++idx)
  1271. {
  1272. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1273. if (idx > 0)
  1274. {
  1275. meta->addStatement(new GenOp(" + "));
  1276. }
  1277. meta->addStatement(new GenOp("@", detailB));
  1278. }
  1279. meta->addStatement(new GenOp(");\r\n"));
  1280. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n",
  1281. outColor, outColor));
  1282. meta->addStatement(new GenOp("\r\n"));
  1283. // Compute ORM
  1284. Var* ormOutput;
  1285. OutputTarget targ = DefaultTarget;
  1286. if (fd.features[MFT_isDeferred])
  1287. {
  1288. targ = RenderTarget2;
  1289. }
  1290. ormOutput = (Var*)LangElement::find(getOutputTargetVarName(targ));
  1291. meta->addStatement(new GenOp(" @.gba = (",
  1292. ormOutput));
  1293. for (S32 idx = 0; idx < detailCount; ++idx)
  1294. {
  1295. Var* matinfoCol = (Var*)LangElement::find(String::ToString("matinfoCol%d", idx));
  1296. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1297. if (idx > 0)
  1298. {
  1299. meta->addStatement(new GenOp(" + "));
  1300. }
  1301. if (matinfoCol)
  1302. {
  1303. meta->addStatement(new GenOp("@ * @", matinfoCol, detailB));
  1304. }
  1305. else
  1306. {
  1307. meta->addStatement(new GenOp("float3(1.0, 1.0, 0.0) * @", detailB));
  1308. }
  1309. }
  1310. meta->addStatement(new GenOp(") / ("));
  1311. for (S32 idx = 0; idx < detailCount; ++idx)
  1312. {
  1313. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1314. if (idx > 0)
  1315. {
  1316. meta->addStatement(new GenOp(" + "));
  1317. }
  1318. meta->addStatement(new GenOp("@", detailB));
  1319. }
  1320. meta->addStatement(new GenOp(");\r\n"));
  1321. meta->addStatement(new GenOp("\r\n"));
  1322. // Compute normal-specific blending factors
  1323. // LukasPJ: I'm not sure why this is necessary, it might not be.
  1324. Var* normalMa = (Var*)LangElement::find("normalMa");
  1325. if (normalMa == NULL)
  1326. {
  1327. normalMa = new Var;
  1328. normalMa->setType("float");
  1329. normalMa->setName("normalMa");
  1330. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1331. new DecOp(normalMa)));
  1332. }
  1333. for (S32 idx = 0; idx < detailCount; ++idx)
  1334. {
  1335. Var* detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", idx));
  1336. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1337. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1338. meta->addStatement(new GenOp(" @ = max(@, @ + min(@, @.w));\r\n",
  1339. normalMa, normalMa, detailH, detailBlend, detCoord));
  1340. }
  1341. meta->addStatement(new GenOp(" @ -= @;\r\n",
  1342. normalMa, depth));
  1343. meta->addStatement(new GenOp("\r\n"));
  1344. for (S32 idx = 0; idx < detailCount; ++idx)
  1345. {
  1346. Var* detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", idx));
  1347. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1348. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1349. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1350. if (!normalDetailB)
  1351. {
  1352. normalDetailB = new Var;
  1353. normalDetailB->setType("float");
  1354. normalDetailB->setName(String::ToString("normalDetailB%d", idx));
  1355. meta->addStatement(new GenOp(" @ = max(@ + min(@, @.w) - @, 0);\r\n",
  1356. new DecOp(normalDetailB), detailH, detailBlend, detCoord, normalMa));
  1357. }
  1358. }
  1359. // Compute normals
  1360. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  1361. if (!gbNormal)
  1362. {
  1363. gbNormal = new Var;
  1364. gbNormal->setName("gbNormal");
  1365. gbNormal->setType("float3");
  1366. meta->addStatement(new GenOp(" @;\r\n", new DecOp(gbNormal)));
  1367. }
  1368. if (gbNormal != NULL)
  1369. {
  1370. meta->addStatement(new GenOp(" @ = (",
  1371. gbNormal));
  1372. for (S32 idx = 0; idx < detailCount; ++idx)
  1373. {
  1374. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1375. Var* bumpNormal = (Var*)LangElement::find(String::ToString("bumpNormal%d", idx));
  1376. Var* viewToTangent = getInViewToTangent(componentList);
  1377. if (idx > 0)
  1378. {
  1379. meta->addStatement(new GenOp(" + "));
  1380. }
  1381. if (bumpNormal != NULL)
  1382. {
  1383. meta->addStatement(new GenOp("mul(@.xyz, @) * @", bumpNormal, viewToTangent, normalDetailB));
  1384. }
  1385. else
  1386. {
  1387. meta->addStatement(new GenOp("@[2] * @", viewToTangent, normalDetailB));
  1388. }
  1389. }
  1390. meta->addStatement(new GenOp(") / ("));
  1391. for (S32 idx = 0; idx < detailCount; ++idx)
  1392. {
  1393. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1394. if (idx > 0)
  1395. {
  1396. meta->addStatement(new GenOp(" + "));
  1397. }
  1398. meta->addStatement(new GenOp("@", normalDetailB));
  1399. }
  1400. meta->addStatement(new GenOp(");\r\n"));
  1401. }
  1402. output = meta;
  1403. }