terrFeatureHLSL.cpp 50 KB

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