terrFeatureGLSL.cpp 55 KB

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