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