terrFeatureGLSL.cpp 50 KB

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