terrFeatureGLSL.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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 = dot( normalize( vec3( @.x, @.y, 0 ) ), vec3( 0, 1, 0 ) );\r\n",
  208. outTex, inNormal, inNormal));
  209. meta->addStatement(
  210. new GenOp(" @.w = 1.0 - 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", "vec4");
  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 * inTexCoord = (Var*)LangElement::find( "texCoord" );
  300. AssertFatal(inTexCoord, "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 * detCoord = connectComp->getElement( RT_TEXCOORD );
  339. detCoord->setName( String::ToString( "detCoord%d", detailIndex ) );
  340. detCoord->setStructName( "OUT" );
  341. detCoord->setType( "vec4" );
  342. // Get the detail scale and fade info.
  343. Var* detScaleAndFade = (Var*)LangElement::find("detailScaleAndFade");
  344. if (detScaleAndFade == NULL)
  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", detCoord, inTexCoord, new IndexOp(detScaleAndFade, detailIndex) ) );
  363. // And sneak the detail fade thru the w detailCoord.
  364. meta->addStatement(new GenOp(" @.w = ( @.z - @ ) * @.w;\r\n",
  365. detCoord, 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 *inTexCoord = 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, inTexCoord ) );
  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 * detCoord = _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), inTexCoord, 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 textures.
  435. Var* normalMapArray = _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. detCoord, normalMapArray, detCoord, 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. detCoord, normalMapArray, detCoord, 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 *detailMapArray = _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. detailMapArray, detCoord, new IndexOp(detailInfo, detailIndex),
  465. detailMapArray, detCoord, new IndexOp(detailInfo, detailIndex),
  466. detailMapArray, detCoord, new IndexOp(detailInfo, detailIndex),
  467. inTexCoord, inTexCoord));
  468. }
  469. else
  470. {
  471. meta->addStatement(new GenOp(" @ = ( tex2D( @, vec3(@.xy, @.x) ) * 2.0 ) - 1.0;\r\n",
  472. detailColor, detailMapArray, detCoord, new IndexOp(detailInfo, detailIndex)));
  473. }
  474. meta->addStatement(new GenOp(" @ *= @.y * @;\r\n", detailColor, new IndexOp(detailInfo, detailIndex), detailBlend));
  475. if (!fd.features.hasFeature(MFT_TerrainNormalMap))
  476. {
  477. // Check to see if we have a gbuffer normal.
  478. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  479. // If we have a gbuffer normal and we don't have a
  480. // normal map feature then we need to lerp in a
  481. // default normal else the normals below this layer
  482. // will show thru.
  483. if (gbNormal &&
  484. !fd.features.hasFeature(MFT_TerrainNormalMap, detailIndex))
  485. {
  486. Var* viewToTangent = getInViewToTangent(componentList);
  487. meta->addStatement(new GenOp(" @ = lerp( @, tGetMatrix3Row(@, 2), @ ) );\r\n", gbNormal, gbNormal, viewToTangent, detailBlend));
  488. }
  489. ShaderFeature::OutputTarget target = (fd.features[MFT_isDeferred]) ? RenderTarget1 : DefaultTarget;
  490. Var* outColor = (Var*)LangElement::find(getOutputTargetVarName(target));
  491. meta->addStatement(new GenOp(" @ += @ * @;\r\n", outColor, detailColor, detailBlend));
  492. }
  493. output = meta;
  494. }
  495. ShaderFeature::Resources TerrainDetailMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  496. {
  497. Resources res;
  498. if ( getProcessIndex() == 0 )
  499. {
  500. // If this is the first detail pass then we
  501. // samples from the layer tex.
  502. res.numTex = 1;
  503. res.numTexReg = 1;
  504. // Add Detail TextureArray
  505. res.numTex += 1;
  506. res.numTexReg += 1;
  507. }
  508. return res;
  509. }
  510. U32 TerrainDetailMapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  511. {
  512. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  513. }
  514. TerrainMacroMapFeatGLSL::TerrainMacroMapFeatGLSL()
  515. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" )),
  516. mTerrainDep(ShaderGen::smCommonShaderPath + String("/terrain/terrain.glsl" ))
  517. {
  518. addDependency( &mTorqueDep );
  519. addDependency( &mTerrainDep );
  520. }
  521. void TerrainMacroMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  522. const MaterialFeatureData &fd )
  523. {
  524. const S32 detailIndex = getProcessIndex();
  525. // Grab incoming texture coords... the base map feature
  526. // made sure this was created.
  527. Var *inTex = (Var*)LangElement::find( "texCoord" );
  528. AssertFatal( inTex, "The texture coord is missing!" );
  529. // Grab the input position.
  530. Var *inPos = (Var*)LangElement::find( "inPosition" );
  531. if ( !inPos )
  532. inPos = (Var*)LangElement::find( "position" );
  533. // Get the object space eye position.
  534. Var *eyePos = _getUniformVar( "eyePos", "vec3", cspPotentialPrimitive );
  535. MultiLine *meta = new MultiLine;
  536. // Get the distance from the eye to this vertex.
  537. Var *dist = (Var*)LangElement::find( "macroDist" );
  538. if ( !dist )
  539. {
  540. dist = new Var;
  541. dist->setType( "float" );
  542. dist->setName( "macroDist" );
  543. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  544. new DecOp( dist ), inPos, eyePos ) );
  545. }
  546. // grab connector texcoord register
  547. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  548. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  549. outTex->setName( String::ToString( "macroCoord%d", detailIndex ) );
  550. outTex->setStructName( "OUT" );
  551. outTex->setType( "vec4" );
  552. // Get the detail scale and fade info.
  553. Var *macroScaleAndFade = new Var;
  554. macroScaleAndFade->setType( "vec4" );
  555. macroScaleAndFade->setName( String::ToString( "macroScaleAndFade%d", detailIndex ) );
  556. macroScaleAndFade->uniform = true;
  557. macroScaleAndFade->constSortPos = cspPotentialPrimitive;
  558. // Setup the detail coord.
  559. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, macroScaleAndFade ) );
  560. // And sneak the detail fade thru the w detailCoord.
  561. meta->addStatement( new GenOp( " @.w = ( @.z - @ ) * @.w;\r\n", outTex, macroScaleAndFade, dist, macroScaleAndFade ) );
  562. output = meta;
  563. }
  564. void TerrainMacroMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  565. const MaterialFeatureData &fd )
  566. {
  567. const S32 detailIndex = getProcessIndex();
  568. Var *inTex = getVertTexCoord( "texCoord" );
  569. MultiLine *meta = new MultiLine;
  570. // We need the negative tangent space view vector
  571. // as in parallax mapping we step towards the camera.
  572. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  573. if ( !negViewTS &&
  574. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  575. {
  576. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  577. if ( !inNegViewTS )
  578. {
  579. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  580. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  581. inNegViewTS->setName( "outNegViewTS" );
  582. inNegViewTS->setStructName( "IN" );
  583. inNegViewTS->setType( "vec3" );
  584. }
  585. negViewTS = new Var( "negViewTS", "vec3" );
  586. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  587. }
  588. // Get the layer samples.
  589. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  590. if ( !layerSample )
  591. {
  592. layerSample = new Var;
  593. layerSample->setType( "vec4" );
  594. layerSample->setName( "layerSample" );
  595. // Get the layer texture var
  596. Var *layerTex = new Var;
  597. layerTex->setType( "sampler2D" );
  598. layerTex->setName( "macrolayerTex" );
  599. layerTex->uniform = true;
  600. layerTex->sampler = true;
  601. layerTex->constNum = Var::getTexUnitNum();
  602. // Read the layer texture to get the samples.
  603. meta->addStatement( new GenOp( " @ = round( tex2D( @, @.xy ) * 255.0f );\r\n",
  604. new DecOp( layerSample ), layerTex, inTex ) );
  605. }
  606. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  607. if ( !layerSize )
  608. {
  609. layerSize = new Var;
  610. layerSize->setType( "float" );
  611. layerSize->setName( "layerSize" );
  612. layerSize->uniform = true;
  613. layerSize->constSortPos = cspPass;
  614. }
  615. // Grab the incoming detail coord.
  616. Var *detCoord = _getInMacroCoord( componentList );
  617. // Get the detail id.
  618. Var *detailInfo = _getMacroIdStrengthParallax();
  619. // Create the detail blend var.
  620. Var *detailBlend = new Var;
  621. detailBlend->setType( "float" );
  622. detailBlend->setName( String::ToString( "macroBlend%d", detailIndex ) );
  623. // Calculate the blend for this detail texture.
  624. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  625. new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) );
  626. // Check to see if we have a gbuffer normal.
  627. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  628. // If we have a gbuffer normal and we don't have a
  629. // normal map feature then we need to lerp in a
  630. // default normal else the normals below this layer
  631. // will show thru.
  632. if (gbNormal &&
  633. !fd.features.hasFeature(MFT_TerrainNormalMap, detailIndex))
  634. {
  635. Var *viewToTangent = getInViewToTangent( componentList );
  636. meta->addStatement( new GenOp( " @ = lerp( @, @[2], min( @, @.w ) );\r\n",
  637. gbNormal, gbNormal, viewToTangent, detailBlend, detCoord ) );
  638. }
  639. Var *detailColor = (Var*)LangElement::find( "macroColor" );
  640. if ( !detailColor )
  641. {
  642. detailColor = new Var;
  643. detailColor->setType( "vec4" );
  644. detailColor->setName( "macroColor" );
  645. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  646. }
  647. // Get the detail texture.
  648. Var *detailMapArray = new Var;
  649. detailMapArray->setType( "sampler2D" );
  650. detailMapArray->setName( String::ToString( "macroMap%d", detailIndex ) );
  651. detailMapArray->uniform = true;
  652. detailMapArray->sampler = true;
  653. detailMapArray->constNum = Var::getTexUnitNum(); // used as texture unit num here
  654. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  655. meta->addStatement( new GenOp( " {\r\n" ) );
  656. // Note that we're doing the standard greyscale detail
  657. // map technique here which can darken and lighten the
  658. // diffuse texture.
  659. //
  660. // We take two color samples and lerp between them for
  661. // side projection layers... else a single sample.
  662. //
  663. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  664. {
  665. 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",
  666. detailColor,
  667. detailMapArray, detCoord, new IndexOp(detailInfo, detailIndex),
  668. detailMapArray, detCoord, new IndexOp(detailInfo, detailIndex),
  669. detailMapArray, detCoord, new IndexOp(detailInfo, detailIndex),
  670. inTex, inTex));
  671. }
  672. else
  673. {
  674. meta->addStatement( new GenOp( " @ = ( tex2D( @, @.xy ) * 2.0 ) - 1.0;\r\n",
  675. detailColor, detailMapArray, detCoord) );
  676. }
  677. meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n", detailColor, detailInfo, detCoord) );
  678. ShaderFeature::OutputTarget target = (fd.features[MFT_isDeferred]) ? RenderTarget1 : DefaultTarget;
  679. Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
  680. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  681. outColor, detailColor, detailBlend));
  682. meta->addStatement( new GenOp( " }\r\n" ) );
  683. output = meta;
  684. }
  685. ShaderFeature::Resources TerrainMacroMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  686. {
  687. Resources res;
  688. if ( getProcessIndex() == 0 )
  689. {
  690. // If this is the first detail pass then we
  691. // samples from the layer tex.
  692. res.numTex += 1;
  693. }
  694. res.numTex += 1;
  695. // Finally we always send the detail texture
  696. // coord to the pixel shader.
  697. res.numTexReg += 1;
  698. return res;
  699. }
  700. U32 TerrainMacroMapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  701. {
  702. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  703. }
  704. void TerrainNormalMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  705. const MaterialFeatureData &fd )
  706. {
  707. // We only need to process normals during the deferred.
  708. if ( !fd.features.hasFeature( MFT_DeferredConditioner ) )
  709. return;
  710. MultiLine *meta = new MultiLine;
  711. if ( !fd.features.hasFeature(MFT_TerrainHeightBlend) )
  712. {
  713. // Make sure the world to tangent transform
  714. // is created and available for the pixel shader.
  715. getOutViewToTangent(componentList, meta, fd);
  716. }
  717. output = meta;
  718. }
  719. void TerrainNormalMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  720. const MaterialFeatureData &fd )
  721. {
  722. // We only need to process normals during the deferred.
  723. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  724. return;
  725. MultiLine *meta = new MultiLine;
  726. const S32 normalIndex = getProcessIndex();
  727. Var *detailBlend = (Var*)LangElement::find( String::ToString( "detailBlend%d", normalIndex ) );
  728. AssertFatal( detailBlend, "The detail blend is missing!" );
  729. // Get the normal map texture.
  730. Var *normalMapArray = _getNormalMapSampler();
  731. /// Get the texture coord.
  732. Var* inDet = _getInDetailCoord(componentList);
  733. Var* inTex = getVertTexCoord("texCoord");
  734. Var* detailInfo = _getDetailIdStrengthParallax();
  735. // Sample the normal map.
  736. //
  737. // We take two normal samples and lerp between them for
  738. // side projection layers... else a single sample.
  739. LangElement* texOp;
  740. if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex))
  741. {
  742. texOp = new GenOp("lerp( tex2D( @, float3(@.xy, @.x) ), lerp( tex2D( @, float3(@.yz, @.x) ), tex2D( @, float3(@.xz, @.x) ), @.z ), @.w )",
  743. normalMapArray, inDet, new IndexOp(detailInfo, normalIndex),
  744. normalMapArray, inDet, new IndexOp(detailInfo, normalIndex),
  745. normalMapArray, inDet, new IndexOp(detailInfo, normalIndex),
  746. inTex, inTex);
  747. }
  748. else
  749. texOp = new GenOp( String::ToString("tex2D(@, vec3(@.xy, @.x))", normalIndex), normalMapArray, inDet, new IndexOp(detailInfo, normalIndex));
  750. // create bump normal
  751. Var* bumpNorm = new Var;
  752. bumpNorm->setName(String::ToString("bumpNormal%d", normalIndex));
  753. bumpNorm->setType("vec4");
  754. LangElement* bumpNormDecl = new DecOp(bumpNorm);
  755. meta->addStatement(expandNormalMap(texOp, bumpNormDecl, bumpNorm, fd));
  756. if (!fd.features.hasFeature(MFT_TerrainNormalMap))
  757. {
  758. Var* viewToTangent = getInViewToTangent(componentList);
  759. // This var is read from GBufferConditionerGLSL and
  760. // used in the deferred output.
  761. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  762. if (!gbNormal)
  763. {
  764. gbNormal = new Var;
  765. gbNormal->setName("gbNormal");
  766. gbNormal->setType("vec3");
  767. meta->addStatement(new GenOp(" @ = tGetMatrix3Row(@, 2);\r\n", new DecOp(gbNormal), viewToTangent));
  768. }
  769. // If we're using SM 3.0 then take advantage of
  770. // dynamic branching to skip layers per-pixel.
  771. if (GFX->getPixelShaderVersion() >= 3.0f)
  772. meta->addStatement(new GenOp(" if ( @ > 0.0f )\r\n", detailBlend));
  773. meta->addStatement(new GenOp(" {\r\n"));
  774. // Normalize is done later...
  775. // Note: The reverse mul order is intentional. Affine matrix.
  776. meta->addStatement(new GenOp(" @ = lerp( @, tMul( @.xyz, @ ), min( @, @.w ) );\r\n",
  777. gbNormal, gbNormal, bumpNorm, viewToTangent, detailBlend, inDet));
  778. // End the conditional block.
  779. meta->addStatement(new GenOp(" }\r\n"));
  780. }
  781. output = meta;
  782. }
  783. ShaderFeature::Resources TerrainNormalMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  784. {
  785. Resources res;
  786. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  787. {
  788. return res;
  789. }
  790. S32 featureIndex = 0, firstNormalMapIndex = 0;
  791. for (int idx = 0; idx < fd.features.getCount(); ++idx) {
  792. const FeatureType& type = fd.features.getAt(idx, &featureIndex);
  793. if (type == MFT_TerrainNormalMap) {
  794. firstNormalMapIndex = getMin(firstNormalMapIndex, featureIndex);
  795. }
  796. }
  797. // We only need to process normals during the deferred.
  798. if (getProcessIndex() == firstNormalMapIndex)
  799. {
  800. // Normal Texture Array
  801. res.numTexReg += 1;
  802. res.numTex += 1;
  803. }
  804. return res;
  805. }
  806. void TerrainLightMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  807. const MaterialFeatureData &fd )
  808. {
  809. // grab connector texcoord register
  810. Var *inTex = (Var*)LangElement::find( "texCoord" );
  811. if ( !inTex )
  812. return;
  813. // Get the lightmap texture.
  814. Var *lightMap = new Var;
  815. lightMap->setType( "sampler2D" );
  816. lightMap->setName( "lightMapTex" );
  817. lightMap->uniform = true;
  818. lightMap->sampler = true;
  819. lightMap->constNum = Var::getTexUnitNum();
  820. MultiLine *meta = new MultiLine;
  821. // Find or create the lightMask value which is read by
  822. // RTLighting to mask out the lights.
  823. //
  824. // The first light is always the sunlight so we apply
  825. // the shadow mask to only the first channel.
  826. //
  827. Var *lightMask = (Var*)LangElement::find( "lightMask" );
  828. if ( !lightMask )
  829. {
  830. lightMask = new Var( "lightMask", "vec4" );
  831. meta->addStatement( new GenOp( " @ = vec4(1);\r\n", new DecOp( lightMask ) ) );
  832. }
  833. meta->addStatement( new GenOp( " @[0] = tex2D( @, @.xy ).r;\r\n", lightMask, lightMap, inTex ) );
  834. output = meta;
  835. }
  836. ShaderFeature::Resources TerrainLightMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  837. {
  838. Resources res;
  839. res.numTex = 1;
  840. return res;
  841. }
  842. void TerrainORMMapFeatGLSL::processVert(Vector<ShaderComponent*> &componentList,
  843. const MaterialFeatureData &fd)
  844. {
  845. // We only need to process normals during the deferred.
  846. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  847. return;
  848. MultiLine* meta = new MultiLine;
  849. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  850. {
  851. // Make sure the world to tangent transform
  852. // is created and available for the pixel shader.
  853. getOutViewToTangent(componentList, meta, fd);
  854. }
  855. output = meta;
  856. }
  857. U32 TerrainORMMapFeatGLSL::getOutputTargets(const MaterialFeatureData &fd) const
  858. {
  859. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::RenderTarget1;
  860. }
  861. void TerrainORMMapFeatGLSL::processPix(Vector<ShaderComponent*> &componentList,
  862. const MaterialFeatureData &fd)
  863. {
  864. /// Get the texture coord.
  865. Var *inDet = _getInDetailCoord(componentList);
  866. Var *inTex = getVertTexCoord("texCoord");
  867. Var* detailInfo = _getDetailIdStrengthParallax();
  868. const S32 compositeIndex = getProcessIndex();
  869. Var *ormConfigMap = _getOrmMapSampler();
  870. // Sample the normal map.
  871. //
  872. // We take two normal samples and lerp between them for
  873. // side projection layers... else a single sample.
  874. LangElement *texOp;
  875. if (fd.features.hasFeature(MFT_TerrainSideProject, compositeIndex))
  876. {
  877. texOp = new GenOp("lerp( tex2D( @, vec3(@.xy, @.x) ), lerp( tex2D( @, vec3(@.yz, @.x) ), tex2D( @, vec3(@.xz, @.x) ), @.z ), @.w )",
  878. ormConfigMap, inDet, new IndexOp(detailInfo, compositeIndex),
  879. ormConfigMap, inDet, new IndexOp(detailInfo, compositeIndex),
  880. ormConfigMap, inDet, new IndexOp(detailInfo, compositeIndex),
  881. inTex, inTex);
  882. }
  883. else
  884. texOp = new GenOp("tex2D(@, vec3(@.xy, @.x))", ormConfigMap, inDet, new IndexOp(detailInfo, compositeIndex));
  885. MultiLine* meta = new MultiLine;
  886. // search for material var
  887. Var* ormConfig;
  888. if ((fd.features[MFT_isDeferred]))
  889. {
  890. // Set base ORM info
  891. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(RenderTarget2));
  892. if (!ormConfig)
  893. {
  894. // create color var
  895. ormConfig = new Var;
  896. ormConfig->setType("fragout");
  897. ormConfig->setName(getOutputTargetVarName(RenderTarget2));
  898. ormConfig->setStructName("OUT");
  899. }
  900. }
  901. else
  902. {
  903. ormConfig = (Var*)LangElement::find("ORMConfig");
  904. if (!ormConfig)
  905. {
  906. ormConfig = new Var("ORMConfig", "vec4");
  907. meta->addStatement(new GenOp(" @;\r\n", new DecOp(ormConfig)));
  908. }
  909. }
  910. if (compositeIndex == 0)
  911. {
  912. meta->addStatement(new GenOp(" @ = vec4(0.0, 0.0, 0.0, 0.0);\r\n", ormConfig));
  913. }
  914. Var *detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", compositeIndex));
  915. AssertFatal(detailBlend, "The detail blend is missing!");
  916. String matinfoName(String::ToString("matinfoCol%d", compositeIndex));
  917. Var *matinfoCol = new Var(matinfoName, "vec3");
  918. meta->addStatement(new GenOp(" @ = @.rgb;\r\n", new DecOp(matinfoCol), texOp));
  919. if (fd.features.hasFeature(MFT_InvertRoughness, compositeIndex))
  920. {
  921. meta->addStatement(new GenOp(" @.b = 1.0 - @.b;\r\n", matinfoCol, matinfoCol));
  922. }
  923. meta->addStatement(new GenOp(" @ = lerp(float3(1.0, 1.0, 0.0), @, @.y * @.w);\r\n", matinfoCol, matinfoCol, new IndexOp(detailInfo, compositeIndex), inDet));
  924. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  925. {
  926. meta->addStatement(new GenOp(" @.gba += @ * @;\r\n", ormConfig, matinfoCol, detailBlend));
  927. }
  928. output = meta;
  929. }
  930. ShaderFeature::Resources TerrainORMMapFeatGLSL::getResources(const MaterialFeatureData &fd)
  931. {
  932. Resources res;
  933. res.numTex = 1;
  934. return res;
  935. }
  936. // reminder, the matinfo buffer is flags, smooth, ao, metal
  937. void TerrainBlankInfoMapFeatGLSL::processPix(Vector<ShaderComponent*> &componentList,
  938. const MaterialFeatureData &fd)
  939. {
  940. S32 compositeIndex = getProcessIndex();
  941. MultiLine* meta = new MultiLine; Var* ormConfig;
  942. if ((fd.features[MFT_isDeferred]))
  943. {
  944. // Set base ORM info
  945. ormConfig = (Var*)LangElement::find(getOutputTargetVarName(RenderTarget2));
  946. if (!ormConfig)
  947. {
  948. // create color var
  949. ormConfig = new Var;
  950. ormConfig->setType("fragout");
  951. ormConfig->setName(getOutputTargetVarName(RenderTarget2));
  952. ormConfig->setStructName("OUT");
  953. }
  954. }
  955. else
  956. {
  957. ormConfig = (Var*)LangElement::find("ORMConfig");
  958. if (!ormConfig)
  959. {
  960. ormConfig = new Var("ORMConfig", "vec4");
  961. meta->addStatement(new GenOp(" @;\r\n", new DecOp(ormConfig)));
  962. }
  963. }
  964. if (compositeIndex == 0)
  965. {
  966. meta->addStatement(new GenOp(" @ = float4(0.0, 0.0, 0.0, 0.0);\r\n", ormConfig));
  967. }
  968. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", compositeIndex));
  969. AssertFatal(detailBlend, "The detail blend is missing!");
  970. String matinfoName(String::ToString("matinfoCol%d", compositeIndex));
  971. if (!fd.features.hasFeature(MFT_TerrainHeightBlend))
  972. {
  973. meta->addStatement(new GenOp(" @.gba += vec3(@, @, 0.0);\r\n", ormConfig, detailBlend, detailBlend));
  974. }
  975. output = meta;
  976. }
  977. void TerrainHeightMapBlendGLSL::processVert(
  978. Vector<ShaderComponent *> &componentList, const MaterialFeatureData &fd) {
  979. // We only need to process normals during the deferred.
  980. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  981. return;
  982. MultiLine* meta = new MultiLine;
  983. // Handle an edge-case when there are no detail-maps available
  984. if (fd.features.getNextFeatureIndex(MFT_TerrainDetailMap, -1) >= 0)
  985. {
  986. // Make sure the world to tangent transform
  987. // is created and available for the pixel shader.
  988. getOutViewToTangent(componentList, meta, fd);
  989. }
  990. output = meta;
  991. }
  992. void TerrainHeightMapBlendGLSL::processPix(Vector<ShaderComponent*>& componentList,
  993. const MaterialFeatureData& fd)
  994. {
  995. ShaderFeature::OutputTarget target = (fd.features[MFT_isDeferred]) ? RenderTarget1 : DefaultTarget;
  996. Var* outColor = (Var*)LangElement::find(getOutputTargetVarName(target));
  997. if (!outColor)
  998. return;
  999. MultiLine* meta = new MultiLine;
  1000. // Count number of detail layers
  1001. int detailCount = 0;
  1002. while (true)
  1003. {
  1004. if (LangElement::find(String::ToString("detailBlend%d", detailCount)) == NULL)
  1005. {
  1006. break;
  1007. }
  1008. ++detailCount;
  1009. }
  1010. if (detailCount == 0)
  1011. {
  1012. return;
  1013. }
  1014. Var* depth = (Var*)LangElement::find("baseBlendDepth");
  1015. if (depth == NULL)
  1016. {
  1017. depth = new Var;
  1018. depth->setType("float");
  1019. depth->setName("baseBlendDepth");
  1020. depth->uniform = true;
  1021. depth->constSortPos = cspPrimitive;
  1022. }
  1023. Var* heightRange = new Var("heightRange", "vec2");
  1024. meta->addStatement(new GenOp(" @ = vec2(2.0f,0);//x=min, y=max\r\n", new DecOp(heightRange)));
  1025. // Compute blend factors
  1026. for (S32 idx = 0; idx < detailCount; ++idx)
  1027. {
  1028. Var* bumpNormal = (Var*)LangElement::find(String::ToString("bumpNormal%d", idx));
  1029. Var* blendDepth = (Var*)LangElement::find(String::ToString("blendDepth%d", idx));
  1030. if (!blendDepth)
  1031. {
  1032. blendDepth = new Var;
  1033. blendDepth->setType("float");
  1034. blendDepth->setName(String::ToString("blendDepth%d", idx));
  1035. blendDepth->uniform = true;
  1036. blendDepth->constSortPos = cspPrimitive;
  1037. }
  1038. Var* blendContrast = (Var*)LangElement::find(String::ToString("blendContrast%d", idx));
  1039. if (!blendContrast)
  1040. {
  1041. blendContrast = new Var;
  1042. blendContrast->setType("float");
  1043. blendContrast->setName(String::ToString("blendContrast%d", idx));
  1044. blendContrast->uniform = true;
  1045. blendContrast->constSortPos = cspPrimitive;
  1046. }
  1047. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1048. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1049. if (!detailH)
  1050. {
  1051. detailH = new Var;
  1052. detailH->setType("float");
  1053. detailH->setName(String::ToString("detailH%d", idx));
  1054. meta->addStatement(new GenOp(" @ = @+@;\r\n", new DecOp(detailH), blendDepth, depth));
  1055. if (bumpNormal != NULL)
  1056. {
  1057. meta->addStatement(new GenOp(" @ += @.a * @;\r\n", detailH, bumpNormal, blendContrast));
  1058. }
  1059. meta->addStatement(new GenOp(" @ *= @;\r\n", detailH, detailBlend));
  1060. meta->addStatement(new GenOp(" @ = vec2(min(@.x,@),max(@.y,@));\r\n", heightRange, heightRange, detailH, heightRange, detailH));
  1061. }
  1062. }
  1063. meta->addStatement(new GenOp("\r\n"));
  1064. if (detailCount > 1)
  1065. {
  1066. for (S32 idx = 0; idx < detailCount; ++idx)
  1067. {
  1068. Var* detailBlend = (Var*)LangElement::find(String::ToString("detailBlend%d", idx));
  1069. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1070. meta->addStatement(new GenOp(" @ = (@[email protected])/(@[email protected]);\r\n", detailH, detailH, heightRange, heightRange, heightRange));
  1071. }
  1072. meta->addStatement(new GenOp("\r\n"));
  1073. }
  1074. else
  1075. {
  1076. for (S32 idx = 0; idx < detailCount; ++idx)
  1077. {
  1078. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1079. meta->addStatement(new GenOp(" @ = 1.0;\r\n", detailH));
  1080. }
  1081. }
  1082. meta->addStatement(new GenOp(" @.rgb += (", outColor));
  1083. for (S32 idx = 0; idx < detailCount; ++idx)
  1084. {
  1085. Var* detailColor = (Var*)LangElement::find(String::ToString("detailColor%d", idx));
  1086. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1087. Var* detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", idx));
  1088. if (idx > 0)
  1089. {
  1090. meta->addStatement(new GenOp(" + "));
  1091. }
  1092. meta->addStatement(new GenOp("((@.rgb * @)*max(@.w,0))", detailColor, detailH, detCoord));
  1093. }
  1094. meta->addStatement(new GenOp(");\r\n"));
  1095. // Compute ORM
  1096. Var* ormOutput;
  1097. if (fd.features[MFT_isDeferred])
  1098. {
  1099. ormOutput = (Var*)LangElement::find(getOutputTargetVarName(RenderTarget2));
  1100. }
  1101. else
  1102. {
  1103. ormOutput = (Var*)LangElement::find("ORMConfig");
  1104. }
  1105. meta->addStatement(new GenOp(" @.gba = (", ormOutput));
  1106. for (S32 idx = 0; idx < detailCount; ++idx)
  1107. {
  1108. Var* matinfoCol = (Var*)LangElement::find(String::ToString("matinfoCol%d", idx));
  1109. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1110. if (idx > 0)
  1111. {
  1112. meta->addStatement(new GenOp(" + "));
  1113. }
  1114. if (matinfoCol)
  1115. {
  1116. meta->addStatement(new GenOp("(@ * @)", matinfoCol, detailH));
  1117. }
  1118. else
  1119. {
  1120. meta->addStatement(new GenOp("(vec3(1.0, 1.0, 0.0) * @)", detailH));
  1121. }
  1122. }
  1123. meta->addStatement(new GenOp(");\r\n"));
  1124. // Compute normals
  1125. Var* viewToTangent = getInViewToTangent(componentList);
  1126. Var* bumpSum = new Var("bumpNorm", "vec3");
  1127. meta->addStatement(new GenOp(" @ = vec3(0, 0, 1)", new DecOp(bumpSum)));
  1128. for (S32 idx = 0; idx < detailCount; ++idx)
  1129. {
  1130. Var* detailH = (Var*)LangElement::find(String::ToString("detailH%d", idx));
  1131. Var* bumpNormal = (Var*)LangElement::find(String::ToString("bumpNormal%d", idx));
  1132. Var* detCoord = (Var*)LangElement::find(String::ToString("detCoord%d", idx));
  1133. if (bumpNormal != NULL)
  1134. {
  1135. meta->addStatement(new GenOp("+(@.xyz * @*max(@.w,0))", bumpNormal, detailH, detCoord));
  1136. }
  1137. }
  1138. meta->addStatement(new GenOp(";\r\n"));
  1139. Var* gbNormal = (Var*)LangElement::find("gbNormal");
  1140. if (!gbNormal)
  1141. {
  1142. gbNormal = new Var("gbNormal","vec3");
  1143. meta->addStatement(new GenOp(" @ = ", new DecOp(gbNormal)));
  1144. }
  1145. else
  1146. meta->addStatement(new GenOp(" @ = ", gbNormal));
  1147. meta->addStatement(new GenOp("tMul(normalize(@),@);\r\n", bumpSum, viewToTangent));
  1148. output = meta;
  1149. }