terrFeatureHLSL.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "terrain/hlsl/terrFeatureHLSL.h"
  24. #include "terrain/terrFeatureTypes.h"
  25. #include "materials/materialFeatureTypes.h"
  26. #include "materials/materialFeatureData.h"
  27. #include "materials/processedMaterial.h"
  28. #include "gfx/gfxDevice.h"
  29. #include "shaderGen/langElement.h"
  30. #include "shaderGen/shaderOp.h"
  31. #include "shaderGen/featureMgr.h"
  32. #include "shaderGen/shaderGen.h"
  33. #include "core/module.h"
  34. namespace
  35. {
  36. void register_hlsl_shader_features_for_terrain(GFXAdapterType type)
  37. {
  38. if (type != Direct3D11)
  39. return;
  40. FEATUREMGR->registerFeature( MFT_TerrainBaseMap, new TerrainBaseMapFeatHLSL );
  41. FEATUREMGR->registerFeature( MFT_TerrainParallaxMap, new NamedFeatureHLSL( "Terrain Parallax Texture" ) );
  42. FEATUREMGR->registerFeature( MFT_TerrainDetailMap, new TerrainDetailMapFeatHLSL );
  43. FEATUREMGR->registerFeature( MFT_TerrainNormalMap, new TerrainNormalMapFeatHLSL );
  44. FEATUREMGR->registerFeature( MFT_TerrainMacroMap, new TerrainMacroMapFeatHLSL );
  45. FEATUREMGR->registerFeature( MFT_TerrainLightMap, new TerrainLightMapFeatHLSL );
  46. FEATUREMGR->registerFeature( MFT_TerrainSideProject, new NamedFeatureHLSL( "Terrain Side Projection" ) );
  47. FEATUREMGR->registerFeature( MFT_TerrainAdditive, new TerrainAdditiveFeatHLSL );
  48. FEATUREMGR->registerFeature( MFT_DeferredTerrainBaseMap, new TerrainBaseMapFeatHLSL );
  49. FEATUREMGR->registerFeature( MFT_DeferredTerrainMacroMap, new TerrainMacroMapFeatHLSL );
  50. FEATUREMGR->registerFeature( MFT_DeferredTerrainDetailMap, new TerrainDetailMapFeatHLSL );
  51. FEATUREMGR->registerFeature( MFT_DeferredTerrainBlankInfoMap, new TerrainBlankInfoMapFeatHLSL );
  52. }
  53. };
  54. MODULE_BEGIN( TerrainFeatHLSL )
  55. MODULE_INIT_AFTER( ShaderGen )
  56. MODULE_INIT
  57. {
  58. SHADERGEN->getFeatureInitSignal().notify(&register_hlsl_shader_features_for_terrain);
  59. }
  60. MODULE_END;
  61. TerrainFeatHLSL::TerrainFeatHLSL()
  62. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" ))
  63. {
  64. addDependency( &mTorqueDep );
  65. }
  66. Var* TerrainFeatHLSL::_getUniformVar( const char *name, const char *type, ConstantSortPosition csp )
  67. {
  68. Var *theVar = (Var*)LangElement::find( name );
  69. if ( !theVar )
  70. {
  71. theVar = new Var;
  72. theVar->setType( type );
  73. theVar->setName( name );
  74. theVar->uniform = true;
  75. theVar->constSortPos = csp;
  76. }
  77. return theVar;
  78. }
  79. Var* TerrainFeatHLSL::_getInDetailCoord( Vector<ShaderComponent*> &componentList )
  80. {
  81. String name( String::ToString( "detCoord%d", getProcessIndex() ) );
  82. Var *inDet = (Var*)LangElement::find( name );
  83. if ( !inDet )
  84. {
  85. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  86. inDet = connectComp->getElement( RT_TEXCOORD );
  87. inDet->setName( name );
  88. inDet->setStructName( "IN" );
  89. inDet->setType( "float4" );
  90. }
  91. return inDet;
  92. }
  93. Var* TerrainFeatHLSL::_getInMacroCoord( Vector<ShaderComponent*> &componentList )
  94. {
  95. String name( String::ToString( "macroCoord%d", getProcessIndex() ) );
  96. Var *inDet = (Var*)LangElement::find( name );
  97. if ( !inDet )
  98. {
  99. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  100. inDet = connectComp->getElement( RT_TEXCOORD );
  101. inDet->setName( name );
  102. inDet->setStructName( "IN" );
  103. inDet->setType( "float4" );
  104. }
  105. return inDet;
  106. }
  107. Var* TerrainFeatHLSL::_getNormalMapTex()
  108. {
  109. String name(String::ToString("normalMap%d", getProcessIndex()));
  110. Var *normalMap = (Var*)LangElement::find(name);
  111. if (!normalMap)
  112. {
  113. normalMap = new Var;
  114. normalMap->setType("SamplerState");
  115. normalMap->setName(name);
  116. normalMap->uniform = true;
  117. normalMap->sampler = true;
  118. normalMap->constNum = Var::getTexUnitNum();
  119. }
  120. return normalMap;
  121. }
  122. Var* TerrainFeatHLSL::_getDetailIdStrengthParallax()
  123. {
  124. String name( String::ToString( "detailIdStrengthParallax%d", getProcessIndex() ) );
  125. Var *detailInfo = (Var*)LangElement::find( name );
  126. if ( !detailInfo )
  127. {
  128. detailInfo = new Var;
  129. detailInfo->setType( "float3" );
  130. detailInfo->setName( name );
  131. detailInfo->uniform = true;
  132. detailInfo->constSortPos = cspPotentialPrimitive;
  133. }
  134. return detailInfo;
  135. }
  136. Var* TerrainFeatHLSL::_getMacroIdStrengthParallax()
  137. {
  138. String name( String::ToString( "macroIdStrengthParallax%d", getProcessIndex() ) );
  139. Var *detailInfo = (Var*)LangElement::find( name );
  140. if ( !detailInfo )
  141. {
  142. detailInfo = new Var;
  143. detailInfo->setType( "float3" );
  144. detailInfo->setName( name );
  145. detailInfo->uniform = true;
  146. detailInfo->constSortPos = cspPotentialPrimitive;
  147. }
  148. return detailInfo;
  149. }
  150. void TerrainBaseMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  151. const MaterialFeatureData &fd )
  152. {
  153. MultiLine *meta = new MultiLine;
  154. output = meta;
  155. // Generate the incoming texture var.
  156. Var *inTex;
  157. {
  158. Var *inPos = (Var*)LangElement::find( "inPosition" );
  159. if ( !inPos )
  160. inPos = (Var*)LangElement::find( "position" );
  161. inTex = new Var( "texCoord", "float3" );
  162. Var *oneOverTerrainSize = _getUniformVar( "oneOverTerrainSize", "float", cspPass );
  163. // NOTE: The y coord here should be negative to have
  164. // the texture maps not end up flipped which also caused
  165. // normal and parallax mapping to be incorrect.
  166. //
  167. // This mistake early in development means that the layer
  168. // id bilinear blend depends on it being that way.
  169. //
  170. // So instead i fixed this by flipping the base and detail
  171. // coord y scale to compensate when rendering.
  172. //
  173. meta->addStatement( new GenOp( " @ = @.xyz * float3( @, @, -@ );\r\n",
  174. new DecOp( inTex ), inPos, oneOverTerrainSize, oneOverTerrainSize, oneOverTerrainSize ) );
  175. }
  176. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  177. // Pass the texture coord to the pixel shader.
  178. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  179. outTex->setName( "outTexCoord" );
  180. outTex->setStructName( "OUT" );
  181. outTex->setType( "float3" );
  182. meta->addStatement( new GenOp( " @.xy = @.xy;\r\n", outTex, inTex ) );
  183. // If this shader has a side projected layer then we
  184. // pass the dot product between the +Y and the normal
  185. // thru outTexCoord.z for use in blending the textures.
  186. if ( fd.features.hasFeature( MFT_TerrainSideProject ) )
  187. {
  188. Var *inNormal = (Var*)LangElement::find( "normal" );
  189. meta->addStatement(
  190. new GenOp( " @.z = pow( abs( dot( normalize( float3( @.x, @.y, 0 ) ), float3( 0, 1, 0 ) ) ), 10.0 );\r\n",
  191. outTex, inNormal, inNormal ) );
  192. }
  193. else
  194. meta->addStatement( new GenOp( " @.z = 0;\r\n", outTex ) );
  195. // HACK: This is sort of lazy... we generate the tanget
  196. // vector here so that we're sure it exists in the parallax
  197. // and normal features which will expect "T" to exist.
  198. //
  199. // If this shader doesn't use it the shader compiler will
  200. // optimize away this code.
  201. //
  202. Var *inTangentZ = getVertTexCoord( "tcTangentZ" );
  203. Var *inTanget = new Var( "T", "float3" );
  204. Var *squareSize = _getUniformVar( "squareSize", "float", cspPass );
  205. meta->addStatement( new GenOp( " @ = normalize( float3( @, 0, @ ) );\r\n",
  206. new DecOp( inTanget ), squareSize, inTangentZ ) );
  207. }
  208. void TerrainBaseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  209. const MaterialFeatureData &fd )
  210. {
  211. // grab connector texcoord register
  212. Var *texCoord = getInTexCoord( "texCoord", "float3", componentList );
  213. // create texture var
  214. Var *diffuseMap = new Var;
  215. diffuseMap->setType( "SamplerState" );
  216. diffuseMap->setName( "baseTexMap" );
  217. diffuseMap->uniform = true;
  218. diffuseMap->sampler = true;
  219. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  220. MultiLine *meta = new MultiLine;
  221. Var *baseColor = new Var;
  222. baseColor->setType( "float4" );
  223. baseColor->setName( "baseColor" );
  224. Var *diffuseTex = new Var;
  225. diffuseTex->setType("Texture2D");
  226. diffuseTex->setName("baseTexture");
  227. diffuseTex->uniform = true;
  228. diffuseTex->texture = true;
  229. diffuseTex->constNum = diffuseMap->constNum;
  230. meta->addStatement(new GenOp(" @ = @.Sample( @, @.xy );\r\n", new DecOp(baseColor), diffuseTex, diffuseMap, texCoord));
  231. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  232. if (fd.features.hasFeature(MFT_isDeferred))
  233. {
  234. target= ShaderFeature::RenderTarget1;
  235. }
  236. meta->addStatement( new GenOp( " @;\r\n", assignColor( baseColor, Material::Mul,NULL,target ) ) );
  237. output = meta;
  238. }
  239. ShaderFeature::Resources TerrainBaseMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  240. {
  241. Resources res;
  242. res.numTexReg = 1;
  243. res.numTex = 1;
  244. return res;
  245. }
  246. U32 TerrainBaseMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  247. {
  248. return fd.features[MFT_DeferredTerrainBaseMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  249. }
  250. TerrainDetailMapFeatHLSL::TerrainDetailMapFeatHLSL()
  251. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" )),
  252. mTerrainDep(ShaderGen::smCommonShaderPath + String("/terrain/terrain.hlsl" ))
  253. {
  254. addDependency( &mTorqueDep );
  255. addDependency( &mTerrainDep );
  256. }
  257. void TerrainDetailMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  258. const MaterialFeatureData &fd )
  259. {
  260. const S32 detailIndex = getProcessIndex();
  261. // Grab incoming texture coords... the base map feature
  262. // made sure this was created.
  263. Var *inTex = (Var*)LangElement::find( "texCoord" );
  264. AssertFatal( inTex, "The texture coord is missing!" );
  265. // Grab the input position.
  266. Var *inPos = (Var*)LangElement::find( "inPosition" );
  267. if ( !inPos )
  268. inPos = (Var*)LangElement::find( "position" );
  269. // Get the object space eye position.
  270. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  271. MultiLine *meta = new MultiLine;
  272. // If we have parallax mapping then make sure we've sent
  273. // the negative view vector to the pixel shader.
  274. if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) &&
  275. !LangElement::find( "outNegViewTS" ) )
  276. {
  277. // Get the object to tangent transform which
  278. // will consume 3 output registers.
  279. Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta, fd );
  280. // Now use a single output register to send the negative
  281. // view vector in tangent space to the pixel shader.
  282. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  283. Var *outNegViewTS = connectComp->getElement( RT_TEXCOORD );
  284. outNegViewTS->setName( "outNegViewTS" );
  285. outNegViewTS->setStructName( "OUT" );
  286. outNegViewTS->setType( "float3" );
  287. meta->addStatement( new GenOp( " @ = mul( @, float3( @ - @.xyz ) );\r\n",
  288. outNegViewTS, objToTangentSpace, eyePos, inPos ) );
  289. }
  290. // Get the distance from the eye to this vertex.
  291. Var *dist = (Var*)LangElement::find( "dist" );
  292. if ( !dist )
  293. {
  294. dist = new Var;
  295. dist->setType( "float" );
  296. dist->setName( "dist" );
  297. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  298. new DecOp( dist ), inPos, eyePos ) );
  299. }
  300. // grab connector texcoord register
  301. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  302. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  303. outTex->setName( String::ToString( "detCoord%d", detailIndex ) );
  304. outTex->setStructName( "OUT" );
  305. outTex->setType( "float4" );
  306. // Get the detail scale and fade info.
  307. Var *detScaleAndFade = new Var;
  308. detScaleAndFade->setType( "float4" );
  309. detScaleAndFade->setName( String::ToString( "detailScaleAndFade%d", detailIndex ) );
  310. detScaleAndFade->uniform = true;
  311. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  312. // Setup the detail coord.
  313. //
  314. // NOTE: You see here we scale the texture coord by 'xyx'
  315. // to generate the detail coord. This y is here because
  316. // its scale is flipped to correct for the non negative y
  317. // in texCoord.
  318. //
  319. // See TerrainBaseMapFeatHLSL::processVert().
  320. //
  321. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) );
  322. // And sneak the detail fade thru the w detailCoord.
  323. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  324. outTex, detScaleAndFade, dist, detScaleAndFade ) );
  325. output = meta;
  326. }
  327. void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  328. const MaterialFeatureData &fd )
  329. {
  330. const S32 detailIndex = getProcessIndex();
  331. Var *inTex = getVertTexCoord( "texCoord" );
  332. MultiLine *meta = new MultiLine;
  333. // We need the negative tangent space view vector
  334. // as in parallax mapping we step towards the camera.
  335. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  336. if ( !negViewTS &&
  337. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  338. {
  339. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  340. if ( !inNegViewTS )
  341. {
  342. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  343. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  344. inNegViewTS->setName( "outNegViewTS" );
  345. inNegViewTS->setStructName( "IN" );
  346. inNegViewTS->setType( "float3" );
  347. }
  348. negViewTS = new Var( "negViewTS", "float3" );
  349. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  350. }
  351. // Get the layer samples.
  352. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  353. if ( !layerSample )
  354. {
  355. layerSample = new Var;
  356. layerSample->setType( "float4" );
  357. layerSample->setName( "layerSample" );
  358. // Get the layer texture var
  359. Var *layerTex = new Var;
  360. layerTex->setType( "SamplerState" );
  361. layerTex->setName( "layerTex" );
  362. layerTex->uniform = true;
  363. layerTex->sampler = true;
  364. layerTex->constNum = Var::getTexUnitNum();
  365. Var* layerTexObj = new Var;
  366. layerTexObj->setName("layerTexObj");
  367. layerTexObj->setType("Texture2D");
  368. layerTexObj->uniform = true;
  369. layerTexObj->texture = true;
  370. layerTexObj->constNum = layerTex->constNum;
  371. // Read the layer texture to get the samples.
  372. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  373. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  374. }
  375. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  376. if ( !layerSize )
  377. {
  378. layerSize = new Var;
  379. layerSize->setType( "float" );
  380. layerSize->setName( "layerSize" );
  381. layerSize->uniform = true;
  382. layerSize->constSortPos = cspPass;
  383. }
  384. // Grab the incoming detail coord.
  385. Var *inDet = _getInDetailCoord( componentList );
  386. // Get the detail id.
  387. Var *detailInfo = _getDetailIdStrengthParallax();
  388. // Create the detail blend var.
  389. Var *detailBlend = new Var;
  390. detailBlend->setType( "float" );
  391. detailBlend->setName( String::ToString( "detailBlend%d", detailIndex ) );
  392. // Calculate the blend for this detail texture.
  393. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  394. new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) );
  395. // Get a var and accumulate the blend amount.
  396. Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
  397. if ( !blendTotal )
  398. {
  399. blendTotal = new Var;
  400. blendTotal->setName( "blendTotal" );
  401. blendTotal->setType( "float" );
  402. meta->addStatement( new GenOp( " @ = 0;\r\n", new DecOp( blendTotal ) ) );
  403. }
  404. // Add to the blend total.
  405. meta->addStatement(new GenOp(" @ = max( @, @ );\r\n", blendTotal, blendTotal, detailBlend));
  406. // If we had a parallax feature... then factor in the parallax
  407. // amount so that it fades out with the layer blending.
  408. if (fd.features.hasFeature(MFT_TerrainParallaxMap, detailIndex))
  409. {
  410. // Get the rest of our inputs.
  411. Var *normalMap = _getNormalMapTex();
  412. String name(String::ToString("normalMapTex%d", getProcessIndex()));
  413. Var *normalMapTex = (Var*)LangElement::find(name);
  414. if (!normalMapTex)
  415. {
  416. normalMapTex = new Var;
  417. normalMapTex->setName(String::ToString("normalMapTex%d", getProcessIndex()));
  418. normalMapTex->setType("Texture2D");
  419. normalMapTex->uniform = true;
  420. normalMapTex->texture = true;
  421. normalMapTex->constNum = normalMap->constNum;
  422. }
  423. // Call the library function to do the rest.
  424. if (fd.features.hasFeature(MFT_IsBC3nm, detailIndex))
  425. {
  426. meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @, @.xy, @, @.z * @ );\r\n",
  427. inDet, normalMapTex, normalMap, inDet, negViewTS, detailInfo, detailBlend));
  428. }
  429. else
  430. {
  431. meta->addStatement(new GenOp(" @.xy += parallaxOffset( @, @, @.xy, @, @.z * @ );\r\n",
  432. inDet, normalMapTex, normalMap, inDet, negViewTS, detailInfo, detailBlend));
  433. }
  434. }
  435. Var *detailColor = (Var*)LangElement::find( "detailColor" );
  436. if ( !detailColor )
  437. {
  438. detailColor = new Var;
  439. detailColor->setType( "float4" );
  440. detailColor->setName( "detailColor" );
  441. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  442. }
  443. // Get the detail texture.
  444. Var *detailMap = new Var;
  445. detailMap->setType( "SamplerState" );
  446. detailMap->setName( String::ToString( "detailMap%d", detailIndex ) );
  447. detailMap->uniform = true;
  448. detailMap->sampler = true;
  449. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  450. // If we're using SM 3.0 then take advantage of
  451. // dynamic branching to skip layers per-pixel.
  452. if ( GFX->getPixelShaderVersion() >= 3.0f )
  453. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  454. meta->addStatement( new GenOp( " {\r\n" ) );
  455. // Note that we're doing the standard greyscale detail
  456. // map technique here which can darken and lighten the
  457. // diffuse texture.
  458. //
  459. // We take two color samples and lerp between them for
  460. // side projection layers... else a single sample.
  461. //
  462. //Sampled detail texture that is not expanded
  463. Var* detailTex = new Var;
  464. detailTex->setName(String::ToString("detailTex%d", detailIndex));
  465. detailTex->setType("Texture2D");
  466. detailTex->uniform = true;
  467. detailTex->texture = true;
  468. detailTex->constNum = detailMap->constNum;
  469. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  470. {
  471. meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, @.yz ), @.Sample( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n",
  472. detailColor, detailTex, detailMap, inDet, detailTex, detailMap, inDet, inTex));
  473. }
  474. else
  475. {
  476. meta->addStatement(new GenOp(" @ = ( @.Sample( @, @.xy ) * 2.0 ) - 1.0;\r\n",
  477. detailColor, detailTex, detailMap, inDet));
  478. }
  479. meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
  480. detailColor, detailInfo, inDet ) );
  481. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  482. if(fd.features.hasFeature( MFT_DeferredTerrainDetailMap ))
  483. target= ShaderFeature::RenderTarget1;
  484. Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
  485. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n", outColor, outColor));
  486. meta->addStatement( new GenOp( " @ += @ * @;\r\n",
  487. outColor, detailColor, detailBlend));
  488. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n", outColor, outColor));
  489. meta->addStatement( new GenOp( " }\r\n" ) );
  490. output = meta;
  491. }
  492. ShaderFeature::Resources TerrainDetailMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  493. {
  494. Resources res;
  495. if ( getProcessIndex() == 0 )
  496. {
  497. // If this is the first detail pass then we
  498. // samples from the layer tex.
  499. res.numTex += 1;
  500. // If this material also does parallax then it
  501. // will generate the negative view vector and the
  502. // worldToTanget transform.
  503. if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  504. res.numTexReg += 4;
  505. }
  506. // sample from the detail texture for diffuse coloring.
  507. res.numTex += 1;
  508. // If we have parallax for this layer then we'll also
  509. // be sampling the normal map for the parallax heightmap.
  510. if ( fd.features.hasFeature( MFT_TerrainParallaxMap, getProcessIndex() ) )
  511. res.numTex += 1;
  512. // Finally we always send the detail texture
  513. // coord to the pixel shader.
  514. res.numTexReg += 1;
  515. return res;
  516. }
  517. U32 TerrainDetailMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  518. {
  519. return fd.features[MFT_DeferredTerrainDetailMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  520. }
  521. TerrainMacroMapFeatHLSL::TerrainMacroMapFeatHLSL()
  522. : mTorqueDep(ShaderGen::smCommonShaderPath + String("/torque.hlsl" )),
  523. mTerrainDep(ShaderGen::smCommonShaderPath + String("/terrain/terrain.hlsl" ))
  524. {
  525. addDependency( &mTorqueDep );
  526. addDependency( &mTerrainDep );
  527. }
  528. void TerrainMacroMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  529. const MaterialFeatureData &fd )
  530. {
  531. const S32 detailIndex = getProcessIndex();
  532. // Grab incoming texture coords... the base map feature
  533. // made sure this was created.
  534. Var *inTex = (Var*)LangElement::find( "texCoord" );
  535. AssertFatal( inTex, "The texture coord is missing!" );
  536. // Grab the input position.
  537. Var *inPos = (Var*)LangElement::find( "inPosition" );
  538. if ( !inPos )
  539. inPos = (Var*)LangElement::find( "position" );
  540. // Get the object space eye position.
  541. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  542. MultiLine *meta = new MultiLine;
  543. // Get the distance from the eye to this vertex.
  544. Var *dist = (Var*)LangElement::find( "macroDist" );
  545. if ( !dist )
  546. {
  547. dist = new Var;
  548. dist->setType( "float" );
  549. dist->setName( "macroDist" );
  550. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  551. new DecOp( dist ), inPos, eyePos ) );
  552. }
  553. // grab connector texcoord register
  554. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  555. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  556. outTex->setName( String::ToString( "macroCoord%d", detailIndex ) );
  557. outTex->setStructName( "OUT" );
  558. outTex->setType( "float4" );
  559. // Get the detail scale and fade info.
  560. Var *detScaleAndFade = new Var;
  561. detScaleAndFade->setType( "float4" );
  562. detScaleAndFade->setName( String::ToString( "macroScaleAndFade%d", detailIndex ) );
  563. detScaleAndFade->uniform = true;
  564. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  565. // Setup the detail coord.
  566. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) );
  567. // And sneak the detail fade thru the w detailCoord.
  568. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  569. outTex, detScaleAndFade, dist, detScaleAndFade ) );
  570. output = meta;
  571. }
  572. void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  573. const MaterialFeatureData &fd )
  574. {
  575. const S32 detailIndex = getProcessIndex();
  576. Var *inTex = getVertTexCoord( "texCoord" );
  577. MultiLine *meta = new MultiLine;
  578. // We need the negative tangent space view vector
  579. // as in parallax mapping we step towards the camera.
  580. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  581. if ( !negViewTS &&
  582. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  583. {
  584. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  585. if ( !inNegViewTS )
  586. {
  587. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  588. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  589. inNegViewTS->setName( "outNegViewTS" );
  590. inNegViewTS->setStructName( "IN" );
  591. inNegViewTS->setType( "float3" );
  592. }
  593. negViewTS = new Var( "negViewTS", "float3" );
  594. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  595. }
  596. // Get the layer samples.
  597. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  598. if ( !layerSample )
  599. {
  600. layerSample = new Var;
  601. layerSample->setType( "float4" );
  602. layerSample->setName( "layerSample" );
  603. // Get the layer texture var
  604. Var *layerTex = new Var;
  605. layerTex->setType( "SamplerState" );
  606. layerTex->setName( "macrolayerTex" );
  607. layerTex->uniform = true;
  608. layerTex->sampler = true;
  609. layerTex->constNum = Var::getTexUnitNum();
  610. // Read the layer texture to get the samples.
  611. Var *layerTexObj = new Var;
  612. layerTexObj->setType("Texture2D");
  613. layerTexObj->setName("macroLayerTexObj");
  614. layerTexObj->uniform = true;
  615. layerTexObj->texture = true;
  616. layerTexObj->constNum = layerTex->constNum;
  617. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  618. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  619. }
  620. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  621. if ( !layerSize )
  622. {
  623. layerSize = new Var;
  624. layerSize->setType( "float" );
  625. layerSize->setName( "layerSize" );
  626. layerSize->uniform = true;
  627. layerSize->constSortPos = cspPass;
  628. }
  629. // Grab the incoming detail coord.
  630. Var *inDet = _getInMacroCoord( componentList );
  631. // Get the detail id.
  632. Var *detailInfo = _getMacroIdStrengthParallax();
  633. // Create the detail blend var.
  634. Var *detailBlend = new Var;
  635. detailBlend->setType( "float" );
  636. detailBlend->setName( String::ToString( "macroBlend%d", detailIndex ) );
  637. // Calculate the blend for this detail texture.
  638. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  639. new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) );
  640. // Get a var and accumulate the blend amount.
  641. Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
  642. if ( !blendTotal )
  643. {
  644. blendTotal = new Var;
  645. blendTotal->setName( "blendTotal" );
  646. blendTotal->setType( "float" );
  647. meta->addStatement( new GenOp( " @ = 0;\r\n", new DecOp( blendTotal ) ) );
  648. }
  649. // Add to the blend total.
  650. meta->addStatement(new GenOp(" @ = max( @, @ );\r\n", blendTotal, blendTotal, detailBlend));
  651. Var *detailColor = (Var*)LangElement::find( "macroColor" );
  652. if ( !detailColor )
  653. {
  654. detailColor = new Var;
  655. detailColor->setType( "float4" );
  656. detailColor->setName( "macroColor" );
  657. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  658. }
  659. // Get the detail texture.
  660. Var *detailMap = new Var;
  661. detailMap->setType( "SamplerState" );
  662. detailMap->setName( String::ToString( "macroMap%d", detailIndex ) );
  663. detailMap->uniform = true;
  664. detailMap->sampler = true;
  665. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  666. //Create texture object for directx 11
  667. Var *detailTex = new Var;
  668. detailTex->setName(String::ToString("macroMapTex%d", detailIndex));
  669. detailTex->setType("Texture2D");
  670. detailTex->uniform = true;
  671. detailTex->texture = true;
  672. detailTex->constNum = detailMap->constNum;
  673. // If we're using SM 3.0 then take advantage of
  674. // dynamic branching to skip layers per-pixel.
  675. if ( GFX->getPixelShaderVersion() >= 3.0f )
  676. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  677. meta->addStatement( new GenOp( " {\r\n" ) );
  678. // Note that we're doing the standard greyscale detail
  679. // map technique here which can darken and lighten the
  680. // diffuse texture.
  681. //
  682. // We take two color samples and lerp between them for
  683. // side projection layers... else a single sample.
  684. //
  685. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  686. {
  687. meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, @.yz ), @.Sample( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n",
  688. detailColor, detailTex, detailMap, inDet, detailTex, detailMap, inDet, inTex));
  689. }
  690. else
  691. {
  692. meta->addStatement(new GenOp(" @ = ( @.Sample( @, @.xy ) * 2.0 ) - 1.0;\r\n",
  693. detailColor, detailTex, detailMap, inDet));
  694. }
  695. meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
  696. detailColor, detailInfo, inDet ) );
  697. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  698. if(fd.features.hasFeature(MFT_DeferredTerrainMacroMap))
  699. target= ShaderFeature::RenderTarget1;
  700. Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
  701. meta->addStatement(new GenOp(" @.rgb = toGamma(@.rgb);\r\n", outColor, outColor));
  702. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  703. outColor, detailColor, detailBlend));
  704. meta->addStatement(new GenOp(" @.rgb = toLinear(clamp(@.rgb, 0, 1));\r\n", outColor, outColor));
  705. meta->addStatement( new GenOp( " }\r\n" ) );
  706. output = meta;
  707. }
  708. ShaderFeature::Resources TerrainMacroMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  709. {
  710. Resources res;
  711. if ( getProcessIndex() == 0 )
  712. {
  713. // If this is the first detail pass then we
  714. // samples from the layer tex.
  715. res.numTex += 1;
  716. }
  717. res.numTex += 1;
  718. // Finally we always send the detail texture
  719. // coord to the pixel shader.
  720. res.numTexReg += 1;
  721. return res;
  722. }
  723. U32 TerrainMacroMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  724. {
  725. return fd.features[MFT_DeferredTerrainMacroMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  726. }
  727. void TerrainNormalMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  728. const MaterialFeatureData &fd )
  729. {
  730. // We only need to process normals during the deferred.
  731. if ( !fd.features.hasFeature( MFT_DeferredConditioner ) )
  732. return;
  733. MultiLine *meta = new MultiLine;
  734. // Make sure the world to tangent transform
  735. // is created and available for the pixel shader.
  736. getOutViewToTangent( componentList, meta, fd );
  737. output = meta;
  738. }
  739. void TerrainNormalMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  740. const MaterialFeatureData &fd )
  741. {
  742. // We only need to process normals during the deferred.
  743. if (!fd.features.hasFeature(MFT_DeferredConditioner))
  744. return;
  745. MultiLine *meta = new MultiLine;
  746. Var *viewToTangent = getInViewToTangent( componentList );
  747. // This var is read from GBufferConditionerHLSL and
  748. // used in the deferred output.
  749. Var *gbNormal = (Var*)LangElement::find( "gbNormal" );
  750. if ( !gbNormal )
  751. {
  752. gbNormal = new Var;
  753. gbNormal->setName( "gbNormal" );
  754. gbNormal->setType( "float3" );
  755. meta->addStatement( new GenOp( " @ = @[2];\r\n", new DecOp( gbNormal ), viewToTangent ) );
  756. }
  757. const S32 normalIndex = getProcessIndex();
  758. Var *detailBlend = (Var*)LangElement::find( String::ToString( "detailBlend%d", normalIndex ) );
  759. AssertFatal( detailBlend, "The detail blend is missing!" );
  760. // If we're using SM 3.0 then take advantage of
  761. // dynamic branching to skip layers per-pixel.
  762. if ( GFX->getPixelShaderVersion() >= 3.0f )
  763. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  764. meta->addStatement( new GenOp( " {\r\n" ) );
  765. // Get the normal map texture.
  766. Var *normalMap = _getNormalMapTex();
  767. /// Get the texture coord.
  768. Var *inDet = _getInDetailCoord( componentList );
  769. Var *inTex = getVertTexCoord( "texCoord" );
  770. // Sample the normal map.
  771. //
  772. // We take two normal samples and lerp between them for
  773. // side projection layers... else a single sample.
  774. LangElement *texOp;
  775. String name(String::ToString("normalMapTex%d", getProcessIndex()));
  776. Var *normalMapTex = (Var*)LangElement::find(name);
  777. if (!normalMapTex)
  778. {
  779. normalMapTex = new Var;
  780. normalMapTex->setName(String::ToString("normalMapTex%d", getProcessIndex()));
  781. normalMapTex->setType("Texture2D");
  782. normalMapTex->uniform = true;
  783. normalMapTex->texture = true;
  784. normalMapTex->constNum = normalMap->constNum;
  785. }
  786. if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex))
  787. {
  788. texOp = new GenOp("lerp( @.Sample( @, @.yz ), @.Sample( @, @.xz ), @.z )",
  789. normalMapTex, normalMap, inDet, normalMapTex, normalMap, inDet, inTex);
  790. }
  791. else
  792. texOp = new GenOp("@.Sample(@, @.xy)", normalMapTex, normalMap, inDet);
  793. // create bump normal
  794. Var *bumpNorm = new Var;
  795. bumpNorm->setName( "bumpNormal" );
  796. bumpNorm->setType( "float4" );
  797. LangElement *bumpNormDecl = new DecOp( bumpNorm );
  798. meta->addStatement( expandNormalMap( texOp, bumpNormDecl, bumpNorm, fd ) );
  799. // Normalize is done later...
  800. // Note: The reverse mul order is intentional. Affine matrix.
  801. meta->addStatement( new GenOp( " @ = lerp( @, mul( @.xyz, @ ), min( @, @.w ) );\r\n",
  802. gbNormal, gbNormal, bumpNorm, viewToTangent, detailBlend, inDet ) );
  803. // End the conditional block.
  804. meta->addStatement( new GenOp( " }\r\n" ) );
  805. // If this is the last normal map then we
  806. // can test to see the total blend value
  807. // to see if we should clip the result.
  808. //if ( fd.features.getNextFeatureIndex( MFT_TerrainNormalMap, normalIndex ) == -1 )
  809. //meta->addStatement( new GenOp( " clip( @ - 0.0001f );\r\n", blendTotal ) );
  810. output = meta;
  811. }
  812. ShaderFeature::Resources TerrainNormalMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  813. {
  814. Resources res;
  815. // We only need to process normals during the deferred.
  816. if ( fd.features.hasFeature( MFT_DeferredConditioner ) )
  817. {
  818. // If this is the first normal map and there
  819. // are no parallax features then we will
  820. // generate the worldToTanget transform.
  821. if ( !fd.features.hasFeature( MFT_TerrainParallaxMap ) &&
  822. ( getProcessIndex() == 0 || !fd.features.hasFeature( MFT_TerrainNormalMap, getProcessIndex() - 1 ) ) )
  823. res.numTexReg = 3;
  824. res.numTex = 1;
  825. }
  826. return res;
  827. }
  828. void TerrainLightMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  829. const MaterialFeatureData &fd )
  830. {
  831. // grab connector texcoord register
  832. Var *inTex = (Var*)LangElement::find( "texCoord" );
  833. if ( !inTex )
  834. return;
  835. // Get the lightmap texture.
  836. Var *lightMap = new Var;
  837. lightMap->setType( "SamplerState" );
  838. lightMap->setName( "lightMapTex" );
  839. lightMap->uniform = true;
  840. lightMap->sampler = true;
  841. lightMap->constNum = Var::getTexUnitNum();
  842. MultiLine *meta = new MultiLine;
  843. // Find or create the lightMask value which is read by
  844. // RTLighting to mask out the lights.
  845. //
  846. // The first light is always the sunlight so we apply
  847. // the shadow mask to only the first channel.
  848. //
  849. Var *lightMask = (Var*)LangElement::find( "lightMask" );
  850. if ( !lightMask )
  851. {
  852. lightMask = new Var( "lightMask", "float4" );
  853. meta->addStatement( new GenOp( " @ = 1;\r\n", new DecOp( lightMask ) ) );
  854. }
  855. Var* lightMapTex = new Var;
  856. lightMapTex->setName("lightMapTexObj");
  857. lightMapTex->setType("Texture2D");
  858. lightMapTex->uniform = true;
  859. lightMapTex->texture = true;
  860. lightMapTex->constNum = lightMap->constNum;
  861. meta->addStatement(new GenOp(" @[0] = @.Sample( @, @.xy ).r;\r\n", lightMask, lightMapTex, lightMap, inTex));
  862. output = meta;
  863. }
  864. ShaderFeature::Resources TerrainLightMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  865. {
  866. Resources res;
  867. res.numTex = 1;
  868. return res;
  869. }
  870. void TerrainAdditiveFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  871. const MaterialFeatureData &fd )
  872. {
  873. Var *color = NULL;
  874. Var *normal = NULL;
  875. if (fd.features[MFT_DeferredTerrainDetailMap])
  876. {
  877. color = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) );
  878. normal = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::DefaultTarget) );
  879. }
  880. else
  881. color = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::DefaultTarget) );
  882. Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
  883. if ( !color || !blendTotal )
  884. return;
  885. MultiLine *meta = new MultiLine;
  886. meta->addStatement( new GenOp( " clip( @ - 0.0001 );\r\n", blendTotal ) );
  887. meta->addStatement( new GenOp( " @.a = @;\r\n", color, blendTotal ) );
  888. if (normal)
  889. meta->addStatement(new GenOp(" @.a = @;\r\n", normal, blendTotal));
  890. output = meta;
  891. }
  892. //standard matInfo map contains data of the form .r = bitflags, .g = (will contain AO),
  893. //.b = specular strength, a= spec power.
  894. //here, it's merely a cutout for now, so that lightmapping (target3) doesn't get mangled.
  895. //we'll most likely revisit that later. possibly several ways...
  896. U32 TerrainBlankInfoMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  897. {
  898. return fd.features[MFT_DeferredTerrainBaseMap] ? ShaderFeature::RenderTarget2 : ShaderFeature::RenderTarget1;
  899. }
  900. void TerrainBlankInfoMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  901. const MaterialFeatureData &fd)
  902. {
  903. // search for material var
  904. Var *material;
  905. OutputTarget targ = RenderTarget1;
  906. if (fd.features[MFT_isDeferred])
  907. {
  908. targ = RenderTarget2;
  909. }
  910. material = (Var*)LangElement::find(getOutputTargetVarName(targ));
  911. MultiLine * meta = new MultiLine;
  912. if (!material)
  913. {
  914. // create color var
  915. material = new Var;
  916. material->setType("fragout");
  917. material->setName(getOutputTargetVarName(targ));
  918. material->setStructName("OUT");
  919. }
  920. meta->addStatement(new GenOp(" @ = float4(0.0,0.0,0.0,0.0001);\r\n", material));
  921. output = meta;
  922. }