terrFeatureHLSL.cpp 52 KB

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