terrFeatureHLSL.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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 = clamp(abs( dot( normalize( float3( @.x, @.y, 0 ) ), float3( 0, 1, 0 ) ) ), 0.0, 1.0);\r\n",
  254. outTex, inNormal, inNormal));
  255. meta->addStatement(
  256. new GenOp(" @.w = 1.0 - abs( 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 *inTex = (Var*)LangElement::find( "texCoord" );
  349. AssertFatal( inTex, "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 *outTex = connectComp->getElement( RT_TEXCOORD );
  388. outTex->setName( String::ToString( "detCoord%d", detailIndex ) );
  389. outTex->setStructName( "OUT" );
  390. outTex->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", outTex, inTex, new IndexOp(detScaleAndFade, detailIndex) ) );
  412. // And sneak the detail fade thru the w detailCoord.
  413. meta->addStatement(new GenOp(" @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  414. outTex, 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 *inTex = 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, inTex));
  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 *inDet = _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), inTex, 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. inDet, normalMapArray, normalMapSampler, inDet, 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. inDet, normalMapArray, normalMapSampler, inDet, 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, inDet, new IndexOp(detailInfo, detailIndex),
  519. detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex),
  520. detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex),
  521. inTex, inTex));
  522. }
  523. else
  524. {
  525. meta->addStatement(new GenOp(" @ = ( @.Sample( @, float3(@.xy, @.x) ) * 2.0 ) - 1.0;\r\n",
  526. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex)));
  527. }
  528. meta->addStatement(new GenOp(" @ *= @.y * @.w;\r\n",
  529. detailColor, new IndexOp(detailInfo, detailIndex), inDet));
  530. if (!fd.features.hasFeature(MFT_TerrainNormalMap))
  531. {
  532. // Check to see if we have a gbuffer normal.
  533. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  534. // If we have a gbuffer normal and we don't have a
  535. // normal map feature then we need to lerp in a
  536. // default normal else the normals below this layer
  537. // will show thru.
  538. if (gbNormal &&
  539. !fd.features.hasFeature(MFT_TerrainNormalMap, detailIndex))
  540. {
  541. Var* viewToTangent = getInViewToTangent(componentList);
  542. meta->addStatement(new GenOp(" @ = lerp( @, @[2], min( @, @.w ) );\r\n",
  543. gbNormal, gbNormal, viewToTangent, detailBlend, inDet));
  544. }
  545. // If we're using SM 3.0 then take advantage of
  546. // dynamic branching to skip layers per-pixel.
  547. if (GFX->getPixelShaderVersion() >= 3.0f)
  548. meta->addStatement(new GenOp(" if ( @ > 0.0f )\r\n", detailBlend));
  549. meta->addStatement(new GenOp(" {\r\n"));
  550. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  551. if (fd.features.hasFeature(MFT_isDeferred))
  552. target = ShaderFeature::RenderTarget1;
  553. Var* outColor = (Var*)LangElement::find(getOutputTargetVarName(target));
  554. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n", outColor, outColor));
  555. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  556. outColor, detailColor, detailBlend));
  557. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n", outColor, outColor));
  558. meta->addStatement(new GenOp(" }\r\n"));
  559. }
  560. output = meta;
  561. }
  562. ShaderFeature::Resources TerrainDetailMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  563. {
  564. Resources res;
  565. if ( getProcessIndex() == 0 )
  566. {
  567. // If this is the first detail pass then we
  568. // samples from the layer tex.
  569. res.numTex += 1;
  570. res.numTexReg += 1;
  571. // Add Detail TextureArray
  572. res.numTex += 1;
  573. res.numTexReg += 1;
  574. }
  575. return res;
  576. }
  577. U32 TerrainDetailMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  578. {
  579. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  580. }
  581. TerrainMacroMapFeatHLSL::TerrainMacroMapFeatHLSL()
  582. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" )),
  583. mTerrainDep(ShaderGen::smCommonShaderPath + String("/terrain/terrain.hlsl" ))
  584. {
  585. addDependency( &mTorqueDep );
  586. addDependency( &mTerrainDep );
  587. }
  588. void TerrainMacroMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  589. const MaterialFeatureData &fd )
  590. {
  591. const S32 detailIndex = getProcessIndex();
  592. // Grab incoming texture coords... the base map feature
  593. // made sure this was created.
  594. Var *inTex = (Var*)LangElement::find( "texCoord" );
  595. AssertFatal( inTex, "The texture coord is missing!" );
  596. // Grab the input position.
  597. Var *inPos = (Var*)LangElement::find( "inPosition" );
  598. if ( !inPos )
  599. inPos = (Var*)LangElement::find( "position" );
  600. // Get the object space eye position.
  601. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  602. MultiLine *meta = new MultiLine;
  603. // Get the distance from the eye to this vertex.
  604. Var *dist = (Var*)LangElement::find( "macroDist" );
  605. if ( !dist )
  606. {
  607. dist = new Var;
  608. dist->setType( "float" );
  609. dist->setName( "macroDist" );
  610. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  611. new DecOp( dist ), inPos, eyePos ) );
  612. }
  613. // grab connector texcoord register
  614. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  615. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  616. outTex->setName( String::ToString( "macroCoord%d", detailIndex ) );
  617. outTex->setStructName( "OUT" );
  618. outTex->setType( "float4" );
  619. // Get the detail scale and fade info.
  620. Var* macroScaleAndFade = (Var*)LangElement::find("macroScaleAndFade");
  621. if (macroScaleAndFade == NULL)
  622. {
  623. macroScaleAndFade = new Var;
  624. macroScaleAndFade->setType("float4");
  625. macroScaleAndFade->setName("macroScaleAndFade");
  626. macroScaleAndFade->uniform = true;
  627. macroScaleAndFade->constSortPos = cspPotentialPrimitive;
  628. }
  629. macroScaleAndFade->arraySize = mMax(macroScaleAndFade->arraySize, detailIndex + 1);
  630. // Setup the detail coord.
  631. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, new IndexOp(macroScaleAndFade, detailIndex)) );
  632. // And sneak the detail fade thru the w detailCoord.
  633. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  634. outTex, new IndexOp(macroScaleAndFade, detailIndex), dist, new IndexOp(macroScaleAndFade, detailIndex)) );
  635. output = meta;
  636. }
  637. void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  638. const MaterialFeatureData &fd )
  639. {
  640. const S32 detailIndex = getProcessIndex();
  641. Var *inTex = getVertTexCoord( "texCoord" );
  642. MultiLine *meta = new MultiLine;
  643. // We need the negative tangent space view vector
  644. // as in parallax mapping we step towards the camera.
  645. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  646. if ( !negViewTS &&
  647. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  648. {
  649. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  650. if ( !inNegViewTS )
  651. {
  652. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  653. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  654. inNegViewTS->setName( "outNegViewTS" );
  655. inNegViewTS->setStructName( "IN" );
  656. inNegViewTS->setType( "float3" );
  657. }
  658. negViewTS = new Var( "negViewTS", "float3" );
  659. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  660. }
  661. // Get the layer samples.
  662. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  663. if ( !layerSample )
  664. {
  665. layerSample = new Var;
  666. layerSample->setType( "float4" );
  667. layerSample->setName( "layerSample" );
  668. // Get the layer texture var
  669. Var *layerTex = new Var;
  670. layerTex->setType( "SamplerState" );
  671. layerTex->setName( "macrolayerTex" );
  672. layerTex->uniform = true;
  673. layerTex->sampler = true;
  674. layerTex->constNum = Var::getTexUnitNum();
  675. // Read the layer texture to get the samples.
  676. Var *layerTexObj = new Var;
  677. layerTexObj->setType("Texture2D");
  678. layerTexObj->setName("macroLayerTexObj");
  679. layerTexObj->uniform = true;
  680. layerTexObj->texture = true;
  681. layerTexObj->constNum = layerTex->constNum;
  682. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  683. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  684. }
  685. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  686. if ( !layerSize )
  687. {
  688. layerSize = new Var;
  689. layerSize->setType( "float" );
  690. layerSize->setName( "layerSize" );
  691. layerSize->uniform = true;
  692. layerSize->constSortPos = cspPass;
  693. }
  694. // Grab the incoming detail coord.
  695. Var *inDet = _getInMacroCoord( componentList );
  696. // Get the detail id.
  697. Var *detailInfo = _getMacroIdStrengthParallax();
  698. // Create the detail blend var.
  699. Var *detailBlend = new Var;
  700. detailBlend->setType( "float" );
  701. detailBlend->setName( String::ToString( "macroBlend%d", detailIndex ) );
  702. // Calculate the blend for this detail texture.
  703. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  704. new DecOp( detailBlend ), new IndexOp(detailInfo, detailIndex), inTex, layerSize, layerSample ) );
  705. // Check to see if we have a gbuffer normal.
  706. Var *gbNormal = (Var*)LangElement::find( "gbNormal" );
  707. // If we have a gbuffer normal and we don't have a
  708. // normal map feature then we need to lerp in a
  709. // default normal else the normals below this layer
  710. // will show thru.
  711. if ( gbNormal &&
  712. !fd.features.hasFeature( MFT_TerrainNormalMap, detailIndex ) )
  713. {
  714. Var *viewToTangent = getInViewToTangent( componentList );
  715. meta->addStatement( new GenOp( " @ = lerp( @, @[2], min( @, @.w ) );\r\n",
  716. gbNormal, gbNormal, viewToTangent, detailBlend, inDet ) );
  717. }
  718. Var *detailColor = (Var*)LangElement::find("macroColor");
  719. if (!detailColor)
  720. {
  721. detailColor = new Var;
  722. detailColor->setType( "float4" );
  723. detailColor->setName( "macroColor" );
  724. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  725. }
  726. // If we're using SM 3.0 then take advantage of
  727. // dynamic branching to skip layers per-pixel.
  728. if ( GFX->getPixelShaderVersion() >= 3.0f )
  729. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  730. meta->addStatement( new GenOp( " {\r\n" ) );
  731. Var* detailMapArray = _getDetailMapArray();
  732. Var* detailMapSampler = _getDetailMapSampler();
  733. // Note that we're doing the standard greyscale detail
  734. // map technique here which can darken and lighten the
  735. // diffuse texture.
  736. //
  737. // We take two color samples and lerp between them for
  738. // side projection layers... else a single sample.
  739. //
  740. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  741. {
  742. 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",
  743. detailColor,
  744. detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex),
  745. detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex),
  746. detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex),
  747. inTex, inTex));
  748. }
  749. else
  750. {
  751. meta->addStatement(new GenOp(" @ = ( @.Sample( @, float3(@.xy, @.x) ) * 2.0 ) - 1.0;\r\n",
  752. detailColor, detailMapArray, detailMapSampler, inDet, new IndexOp(detailInfo, detailIndex)));
  753. }
  754. meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
  755. detailColor, new IndexOp(detailInfo, detailIndex), inDet ) );
  756. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  757. if (fd.features.hasFeature(MFT_isDeferred))
  758. target= ShaderFeature::RenderTarget1;
  759. Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
  760. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n", outColor, outColor));
  761. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  762. outColor, detailColor, detailBlend));
  763. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n", outColor, outColor));
  764. meta->addStatement( new GenOp( " }\r\n" ) );
  765. output = meta;
  766. }
  767. ShaderFeature::Resources TerrainMacroMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  768. {
  769. Resources res;
  770. if ( getProcessIndex() == 0 )
  771. {
  772. // If this is the first detail pass then we
  773. // samples from the layer tex.
  774. res.numTex += 1;
  775. }
  776. res.numTex += 1;
  777. // Finally we always send the detail texture
  778. // coord to the pixel shader.
  779. res.numTexReg += 1;
  780. return res;
  781. }
  782. U32 TerrainMacroMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  783. {
  784. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  785. }
  786. void TerrainNormalMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  787. const MaterialFeatureData &fd )
  788. {
  789. // We only need to process normals during the deferred.
  790. if ( !fd.features.hasFeature( MFT_DeferredConditioner ) )
  791. return;
  792. MultiLine *meta = new MultiLine;
  793. if ( !fd.features.hasFeature(MFT_TerrainHeightBlend) )
  794. {
  795. // Make sure the world to tangent transform
  796. // is created and available for the pixel shader.
  797. getOutViewToTangent(componentList, meta, fd);
  798. }
  799. output = meta;
  800. }
  801. void TerrainNormalMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  802. const MaterialFeatureData &fd )
  803. {
  804. // We only need to process normals during the deferred.
  805. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  806. return;
  807. MultiLine *meta = new MultiLine;
  808. const S32 normalIndex = getProcessIndex();
  809. Var *detailBlend = (Var*)LangElement::find( String::ToString( "detailBlend%d", normalIndex ) );
  810. AssertFatal( detailBlend, "The detail blend is missing!" );
  811. /// Get the texture coord.
  812. Var* inDet = _getInDetailCoord(componentList);
  813. Var* inTex = getVertTexCoord("texCoord");
  814. Var* detailInfo = _getDetailIdStrengthParallax();
  815. // Sample the normal map.
  816. //
  817. // We take two normal samples and lerp between them for
  818. // side projection layers... else a single sample.
  819. LangElement* texOp;
  820. Var* normalMapSampler = _getNormalMapSampler();
  821. Var* normalMapArray = _getNormalMapArray();
  822. if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex))
  823. {
  824. texOp = new GenOp("lerp( @.Sample( @, float3(@.xy, @.x) ), lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z ), @.w )",
  825. normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex),
  826. normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex),
  827. normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex),
  828. inTex, inTex);
  829. }
  830. else
  831. texOp = new GenOp("@.Sample(@, float3(@.xy, @.x))", normalMapArray, normalMapSampler, inDet, new IndexOp(detailInfo, normalIndex));
  832. // create bump normal
  833. Var* bumpNorm = new Var;
  834. bumpNorm->setName(String::ToString("bumpNormal%d", normalIndex));
  835. bumpNorm->setType("float4");
  836. LangElement* bumpNormDecl = new DecOp(bumpNorm);
  837. meta->addStatement(expandNormalMap(texOp, bumpNormDecl, bumpNorm, fd));
  838. if (!fd.features.hasFeature(MFT_TerrainNormalMap))
  839. {
  840. Var* viewToTangent = getInViewToTangent(componentList);
  841. // This var is read from GBufferConditionerHLSL and
  842. // used in the deferred output.
  843. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  844. if (!gbNormal)
  845. {
  846. gbNormal = new Var;
  847. gbNormal->setName("gbNormal");
  848. gbNormal->setType("float3");
  849. meta->addStatement(new GenOp(" @ = @[2];\r\n", new DecOp(gbNormal), viewToTangent));
  850. }
  851. // If we're using SM 3.0 then take advantage of
  852. // dynamic branching to skip layers per-pixel.
  853. if (GFX->getPixelShaderVersion() >= 3.0f)
  854. meta->addStatement(new GenOp(" if ( @ > 0.0f )\r\n", detailBlend));
  855. meta->addStatement(new GenOp(" {\r\n"));
  856. // Normalize is done later...
  857. // Note: The reverse mul order is intentional. Affine matrix.
  858. meta->addStatement(new GenOp(" @ = lerp( @, mul( @.xyz, @ ), min( @, @.w ) );\r\n",
  859. gbNormal, gbNormal, bumpNorm, viewToTangent, detailBlend, inDet));
  860. // End the conditional block.
  861. meta->addStatement(new GenOp(" }\r\n"));
  862. }
  863. output = meta;
  864. }
  865. ShaderFeature::Resources TerrainNormalMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  866. {
  867. Resources res;
  868. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  869. {
  870. return res;
  871. }
  872. S32 featureIndex = 0, firstNormalMapIndex = 0;
  873. for (int idx = 0; idx < fd.features.getCount(); ++idx) {
  874. const FeatureType &type = fd.features.getAt(idx, &featureIndex);
  875. if (type == MFT_TerrainNormalMap) {
  876. firstNormalMapIndex = getMin(firstNormalMapIndex, featureIndex);
  877. }
  878. }
  879. // We only need to process normals during the deferred.
  880. if (getProcessIndex() == firstNormalMapIndex)
  881. {
  882. res.numTexReg += 1;
  883. res.numTex += 1;
  884. }
  885. return res;
  886. }
  887. void TerrainLightMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  888. const MaterialFeatureData &fd )
  889. {
  890. // grab connector texcoord register
  891. Var *inTex = (Var*)LangElement::find( "texCoord" );
  892. if ( !inTex )
  893. return;
  894. // Get the lightmap texture.
  895. Var *lightMap = new Var;
  896. lightMap->setType( "SamplerState" );
  897. lightMap->setName( "lightMapTex" );
  898. lightMap->uniform = true;
  899. lightMap->sampler = true;
  900. lightMap->constNum = Var::getTexUnitNum();
  901. MultiLine *meta = new MultiLine;
  902. // Find or create the lightMask value which is read by
  903. // RTLighting to mask out the lights.
  904. //
  905. // The first light is always the sunlight so we apply
  906. // the shadow mask to only the first channel.
  907. //
  908. Var *lightMask = (Var*)LangElement::find( "lightMask" );
  909. if ( !lightMask )
  910. {
  911. lightMask = new Var( "lightMask", "float4" );
  912. meta->addStatement( new GenOp( " @ = 1;\r\n", new DecOp( lightMask ) ) );
  913. }
  914. Var* lightMapTex = new Var;
  915. lightMapTex->setName("lightMapTexObj");
  916. lightMapTex->setType("Texture2D");
  917. lightMapTex->uniform = true;
  918. lightMapTex->texture = true;
  919. lightMapTex->constNum = lightMap->constNum;
  920. meta->addStatement(new GenOp(" @[0] = @.Sample( @, @.xy ).r;\r\n", lightMask, lightMapTex, lightMap, inTex));
  921. output = meta;
  922. }
  923. ShaderFeature::Resources TerrainLightMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  924. {
  925. Resources res;
  926. res.numTex = 1;
  927. return res;
  928. }
  929. void TerrainORMMapFeatHLSL::processVert(Vector<ShaderComponent*> &componentList,
  930. const MaterialFeatureData &fd)
  931. {
  932. // We only need to process normals during the deferred.
  933. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  934. return;
  935. MultiLine* meta = new MultiLine;
  936. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  937. {
  938. // Make sure the world to tangent transform
  939. // is created and available for the pixel shader.
  940. getOutViewToTangent(componentList, meta, fd);
  941. }
  942. output = meta;
  943. }
  944. U32 TerrainORMMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  945. {
  946. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
  947. }
  948. void TerrainORMMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  949. const MaterialFeatureData &fd)
  950. {
  951. /// Get the texture coord.
  952. Var *inDet = _getInDetailCoord(componentList);
  953. Var *inTex = getVertTexCoord("texCoord");
  954. Var* detailInfo = _getDetailIdStrengthParallax();
  955. const S32 compositeIndex = getProcessIndex();
  956. // Sample the normal map.
  957. //
  958. // We take two normal samples and lerp between them for
  959. // side projection layers... else a single sample.
  960. LangElement *texOp;
  961. Var* ormMapArray = _getOrmMapArray();
  962. Var* ormMapSampler = _getOrmMapSampler();
  963. if (fd.features.hasFeature(MFT_TerrainSideProject, compositeIndex))
  964. {
  965. texOp = new GenOp("lerp( @.Sample( @, float3(@.xy, @.x) ), lerp( @.Sample( @, float3(@.yz, @.x) ), @.Sample( @, float3(@.xz, @.x) ), @.z ), @.w )",
  966. ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex),
  967. ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex),
  968. ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex),
  969. inTex, inTex);
  970. }
  971. else
  972. texOp = new GenOp("@.Sample(@, float3(@.xy, @.x))", ormMapArray, ormMapSampler, inDet, new IndexOp(detailInfo, compositeIndex));
  973. MultiLine* meta = new MultiLine;
  974. // search for material var
  975. Var* ormConfig;
  976. if ((fd.features[MFT_isDeferred]))
  977. {
  978. // Set base ORM info
  979. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(RenderTarget2));
  980. if (!ormConfig)
  981. {
  982. // create color var
  983. ormConfig = new Var;
  984. ormConfig->setType("fragout");
  985. ormConfig->setName(getOutputTargetVarName(RenderTarget2));
  986. ormConfig->setStructName("OUT");
  987. }
  988. }
  989. else
  990. {
  991. ormConfig = (Var*)LangElement::find("ORMConfig");
  992. if (!ormConfig)
  993. {
  994. ormConfig = new Var("ORMConfig", "float4");
  995. meta->addStatement(new GenOp(" @;\r\n", new DecOp(ormConfig)));
  996. }
  997. }
  998. if (compositeIndex == 0)
  999. {
  1000. meta->addStatement(new GenOp(" @ = float4(0.0, 0.0, 0.0, 0.0);\r\n", ormConfig));
  1001. }
  1002. Var *detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", compositeIndex));
  1003. AssertFatal(detailBlend, "The detail blend is missing!");
  1004. String matinfoName(String::ToString("matinfoCol%d", compositeIndex));
  1005. Var *matinfoCol = new Var(matinfoName, "float3");
  1006. meta->addStatement(new GenOp(" @ = @.rgb;\r\n", new DecOp(matinfoCol), texOp));
  1007. if (fd.features.hasFeature(MFT_InvertRoughness, compositeIndex))
  1008. {
  1009. meta->addStatement(new GenOp(" @.b = 1.0 - @.b;\r\n", matinfoCol, matinfoCol));
  1010. }
  1011. meta->addStatement(new GenOp(" @ = lerp(float3(1.0, 1.0, 0.0), @, @.y * @.w);\r\n", matinfoCol, matinfoCol, new IndexOp(detailInfo, compositeIndex), inDet));
  1012. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  1013. {
  1014. meta->addStatement(new GenOp(" @.gba += @ * @;\r\n", ormConfig, matinfoCol, detailBlend));
  1015. }
  1016. output = meta;
  1017. }
  1018. ShaderFeature::Resources TerrainORMMapFeatHLSL::getResources(const MaterialFeatureData &fd)
  1019. {
  1020. Resources res;
  1021. res.numTex = 1;
  1022. return res;
  1023. }
  1024. void TerrainBlankInfoMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  1025. const MaterialFeatureData &fd)
  1026. {
  1027. S32 compositeIndex = getProcessIndex();
  1028. MultiLine * meta = new MultiLine; Var* ormConfig;
  1029. if ((fd.features[MFT_isDeferred]))
  1030. {
  1031. // Set base ORM info
  1032. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(RenderTarget2));
  1033. if (!ormConfig)
  1034. {
  1035. // create color var
  1036. ormConfig = new Var;
  1037. ormConfig->setType("fragout");
  1038. ormConfig->setName(getOutputTargetVarName(RenderTarget2));
  1039. ormConfig->setStructName("OUT");
  1040. }
  1041. }
  1042. else
  1043. {
  1044. ormConfig = (Var*)LangElement::find("ORMConfig");
  1045. if (!ormConfig)
  1046. {
  1047. ormConfig = new Var("ORMConfig", "float4");
  1048. meta->addStatement(new GenOp(" @;\r\n", new DecOp(ormConfig)));
  1049. }
  1050. }
  1051. if (compositeIndex == 0)
  1052. {
  1053. meta->addStatement(new GenOp(" @ = float4(0.0, 0.0, 0.0, 0.0);\r\n", ormConfig));
  1054. }
  1055. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", compositeIndex));
  1056. AssertFatal(detailBlend, "The detail blend is missing!");
  1057. String matinfoName(String::ToString("matinfoCol%d", compositeIndex));
  1058. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  1059. {
  1060. meta->addStatement(new GenOp(" @.gba += float3(@, @, 0.0);\r\n", ormConfig, detailBlend, detailBlend));
  1061. }
  1062. output = meta;
  1063. }
  1064. void TerrainHeightMapBlendHLSL::processVert(Vector<ShaderComponent*>& componentList,
  1065. const MaterialFeatureData& fd)
  1066. {
  1067. // We only need to process normals during the deferred.
  1068. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  1069. return;
  1070. MultiLine* meta = new MultiLine;
  1071. // Handle an edge-case when there are no detail-maps available
  1072. if (fd.features.getNextFeatureIndex(MFT_TerrainDetailMap, -1) >= 0)
  1073. {
  1074. // Make sure the world to tangent transform
  1075. // is created and available for the pixel shader.
  1076. getOutViewToTangent(componentList, meta, fd);
  1077. }
  1078. output = meta;
  1079. }
  1080. void TerrainHeightMapBlendHLSL::processPix(Vector<ShaderComponent*>& componentList,
  1081. const MaterialFeatureData& fd)
  1082. {
  1083. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  1084. if (fd.features.hasFeature(MFT_isDeferred))
  1085. target = ShaderFeature::RenderTarget1;
  1086. Var* outColor = (Var*)LangElement::find(getOutputTargetVarName(target));
  1087. if (!outColor)
  1088. return;
  1089. MultiLine* meta = new MultiLine;
  1090. Var* detailTot = (Var*)LangElement::find("detailTot");
  1091. if (detailTot == NULL)
  1092. {
  1093. detailTot = new Var("detailTot","float");
  1094. meta->addStatement(new GenOp("@=0;\r\n", new DecOp(detailTot)));
  1095. }
  1096. // Count number of detail layers
  1097. int detailCount = 0;
  1098. while (true)
  1099. {
  1100. if(LangElement::find(String::ToString("detailBlend%d", detailCount)) == NULL)
  1101. {
  1102. break;
  1103. }
  1104. ++detailCount;
  1105. }
  1106. if ( detailCount == 0 )
  1107. {
  1108. return;
  1109. }
  1110. // Compute blend factors
  1111. for (S32 idx = 0; idx < detailCount; ++idx)
  1112. {
  1113. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1114. Var* bumpNormal = (Var*)LangElement::find(String::ToString("bumpNormal%d", idx));
  1115. Var* blendDepth = (Var*)LangElement::find(String::ToString("blendDepth%d", idx));
  1116. if (!blendDepth)
  1117. {
  1118. blendDepth = new Var;
  1119. blendDepth->setType("float");
  1120. blendDepth->setName(String::ToString("blendDepth%d", idx));
  1121. blendDepth->uniform = true;
  1122. blendDepth->constSortPos = cspPrimitive;
  1123. }
  1124. Var* blendContrast = (Var*)LangElement::find(String::ToString("blendContrast%d", idx));
  1125. if (!blendContrast)
  1126. {
  1127. blendContrast = new Var;
  1128. blendContrast->setType("float");
  1129. blendContrast->setName(String::ToString("blendContrast%d", idx));
  1130. blendContrast->uniform = true;
  1131. blendContrast->constSortPos = cspPrimitive;
  1132. }
  1133. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1134. if (!detailH)
  1135. {
  1136. detailH = new Var;
  1137. detailH->setType("float");
  1138. detailH->setName(String::ToString("detailH%d", idx));
  1139. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1140. new DecOp(detailH)));
  1141. meta->addStatement(new GenOp(" if (@ > 0.0f) {\r\n", detailBlend));
  1142. if (bumpNormal != NULL)
  1143. {
  1144. meta->addStatement(new GenOp(" @ = clamp(@.a + @, 0.0, 1.0);\r\n",
  1145. detailH, bumpNormal, blendDepth));
  1146. }
  1147. else
  1148. {
  1149. meta->addStatement(new GenOp(" @ = clamp(@, 0.0, 1.0);\r\n",
  1150. detailH, blendDepth));
  1151. }
  1152. meta->addStatement(new GenOp(" @ = max((@ * 2.0f - 1.0f) * @, 0.0f);\r\n",
  1153. detailH, detailH, blendContrast));
  1154. meta->addStatement(new GenOp(" }\r\n"));
  1155. }
  1156. }
  1157. meta->addStatement(new GenOp("\r\n"));
  1158. Var* depth = (Var*)LangElement::find("baseBlendDepth");
  1159. if (depth == NULL)
  1160. {
  1161. depth = new Var;
  1162. depth->setType("float");
  1163. depth->setName("baseBlendDepth");
  1164. depth->uniform = true;
  1165. depth->constSortPos = cspPrimitive;
  1166. }
  1167. Var* ma = (Var*)LangElement::find("ma");
  1168. if (ma == NULL)
  1169. {
  1170. ma = new Var;
  1171. ma->setType("float");
  1172. ma->setName("ma");
  1173. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1174. new DecOp(ma)));
  1175. }
  1176. for (S32 idx = 0; idx < detailCount; ++idx)
  1177. {
  1178. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1179. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1180. meta->addStatement(new GenOp(" @ = max(@, @ + @);\r\n",
  1181. ma, ma, detailH, detailBlend));
  1182. }
  1183. meta->addStatement(new GenOp(" @ -= @;\r\n",
  1184. ma, depth));
  1185. meta->addStatement(new GenOp("\r\n"));
  1186. for (S32 idx = 0; idx < detailCount; ++idx)
  1187. {
  1188. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1189. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1190. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1191. if (!detailB)
  1192. {
  1193. detailB = new Var;
  1194. detailB->setType("float");
  1195. detailB->setName(String::ToString("detailB%d", idx));
  1196. meta->addStatement(new GenOp(" @ = max(@ + @ - @, 0);\r\n",
  1197. new DecOp(detailB), detailH, detailBlend, ma));
  1198. }
  1199. meta->addStatement(new GenOp(" @ += @;\r\n", detailTot, detailB));
  1200. }
  1201. meta->addStatement(new GenOp("\r\n"));
  1202. // Compute albedo
  1203. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n",
  1204. outColor, outColor));
  1205. meta->addStatement(new GenOp(" @.rgb += (",
  1206. outColor));
  1207. for (S32 idx = 0; idx < detailCount; ++idx)
  1208. {
  1209. Var* detailColor = (Var*)LangElement::find(String::ToString("detailColor%d", idx));
  1210. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1211. if (idx > 0)
  1212. {
  1213. meta->addStatement(new GenOp(" + "));
  1214. }
  1215. meta->addStatement(new GenOp("@.rgb * @", detailColor, detailB));
  1216. }
  1217. meta->addStatement(new GenOp(") / @;\r\n", detailTot));
  1218. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n",
  1219. outColor, outColor));
  1220. meta->addStatement(new GenOp("\r\n"));
  1221. // Compute ORM
  1222. Var* ormOutput;
  1223. OutputTarget targ = DefaultTarget;
  1224. if (fd.features[MFT_isDeferred])
  1225. {
  1226. targ = RenderTarget2;
  1227. }
  1228. ormOutput = (Var*)LangElement::find(getOutputTargetVarName(targ));
  1229. meta->addStatement(new GenOp(" @.gba = (",
  1230. ormOutput));
  1231. for (S32 idx = 0; idx < detailCount; ++idx)
  1232. {
  1233. Var* matinfoCol = (Var*)LangElement::find(String::ToString("matinfoCol%d", idx));
  1234. Var* detailB = (Var*)LangElement::find(String::ToString("detailB%d", idx));
  1235. if (idx > 0)
  1236. {
  1237. meta->addStatement(new GenOp(" + "));
  1238. }
  1239. if (matinfoCol)
  1240. {
  1241. meta->addStatement(new GenOp("@ * @", matinfoCol, detailB));
  1242. }
  1243. else
  1244. {
  1245. meta->addStatement(new GenOp("float3(1.0, 1.0, 0.0) * @", detailB));
  1246. }
  1247. }
  1248. meta->addStatement(new GenOp(") / @;\r\n", detailTot));
  1249. meta->addStatement(new GenOp("\r\n"));
  1250. // Compute normal-specific blending factors
  1251. // LukasPJ: I'm not sure why this is necessary, it might not be.
  1252. Var* normalMa = (Var*)LangElement::find("normalMa");
  1253. if (normalMa == NULL)
  1254. {
  1255. normalMa = new Var;
  1256. normalMa->setType("float");
  1257. normalMa->setName("normalMa");
  1258. meta->addStatement(new GenOp(" @ = 0;\r\n",
  1259. new DecOp(normalMa)));
  1260. }
  1261. for (S32 idx = 0; idx < detailCount; ++idx)
  1262. {
  1263. Var* detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", idx));
  1264. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1265. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1266. meta->addStatement(new GenOp(" @ = max(@, @ + min(@, @.w));\r\n",
  1267. normalMa, normalMa, detailH, detailBlend, detCoord));
  1268. }
  1269. meta->addStatement(new GenOp(" @ -= @;\r\n",
  1270. normalMa, depth));
  1271. meta->addStatement(new GenOp("\r\n"));
  1272. for (S32 idx = 0; idx < detailCount; ++idx)
  1273. {
  1274. Var* detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", idx));
  1275. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1276. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1277. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1278. if (!normalDetailB)
  1279. {
  1280. normalDetailB = new Var;
  1281. normalDetailB->setType("float");
  1282. normalDetailB->setName(String::ToString("normalDetailB%d", idx));
  1283. meta->addStatement(new GenOp(" @ = max(@ + min(@, @.w) - @, 0);\r\n",
  1284. new DecOp(normalDetailB), detailH, detailBlend, detCoord, normalMa));
  1285. }
  1286. }
  1287. // Compute normals
  1288. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  1289. if (!gbNormal)
  1290. {
  1291. gbNormal = new Var;
  1292. gbNormal->setName("gbNormal");
  1293. gbNormal->setType("float3");
  1294. meta->addStatement(new GenOp(" @;\r\n", new DecOp(gbNormal)));
  1295. }
  1296. if (gbNormal != NULL)
  1297. {
  1298. meta->addStatement(new GenOp(" @ = (",
  1299. gbNormal));
  1300. for (S32 idx = 0; idx < detailCount; ++idx)
  1301. {
  1302. Var* normalDetailB = (Var*)LangElement::find(String::ToString("normalDetailB%d", idx));
  1303. Var* bumpNormal = (Var*)LangElement::find(String::ToString("bumpNormal%d", idx));
  1304. Var* viewToTangent = getInViewToTangent(componentList);
  1305. if (idx > 0)
  1306. {
  1307. meta->addStatement(new GenOp(" + "));
  1308. }
  1309. if (bumpNormal != NULL)
  1310. {
  1311. meta->addStatement(new GenOp("mul(@.xyz, @) * @", bumpNormal, viewToTangent, normalDetailB));
  1312. }
  1313. else
  1314. {
  1315. meta->addStatement(new GenOp("@[2] * @", viewToTangent, normalDetailB));
  1316. }
  1317. }
  1318. meta->addStatement(new GenOp(") / @;\r\n", detailTot));
  1319. }
  1320. output = meta;
  1321. }