terrFeatureHLSL.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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 != Direct3D9 && type != Direct3D9_360 && 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( "shaders/common/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. inDet->mapsToSampler = true;
  91. }
  92. return inDet;
  93. }
  94. Var* TerrainFeatHLSL::_getInMacroCoord( Vector<ShaderComponent*> &componentList )
  95. {
  96. String name( String::ToString( "macroCoord%d", getProcessIndex() ) );
  97. Var *inDet = (Var*)LangElement::find( name );
  98. if ( !inDet )
  99. {
  100. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  101. inDet = connectComp->getElement( RT_TEXCOORD );
  102. inDet->setName( name );
  103. inDet->setStructName( "IN" );
  104. inDet->setType( "float4" );
  105. inDet->mapsToSampler = true;
  106. }
  107. return inDet;
  108. }
  109. Var* TerrainFeatHLSL::_getNormalMapTex()
  110. {
  111. String name(String::ToString("normalMap%d", getProcessIndex()));
  112. Var *normalMap = (Var*)LangElement::find(name);
  113. if (!normalMap)
  114. {
  115. normalMap = new Var;
  116. if (mIsDirect3D11)
  117. normalMap->setType("SamplerState");
  118. else
  119. normalMap->setType("sampler2D");
  120. normalMap->setName(name);
  121. normalMap->uniform = true;
  122. normalMap->sampler = true;
  123. normalMap->constNum = Var::getTexUnitNum();
  124. }
  125. return normalMap;
  126. }
  127. Var* TerrainFeatHLSL::_getDetailIdStrengthParallax()
  128. {
  129. String name( String::ToString( "detailIdStrengthParallax%d", getProcessIndex() ) );
  130. Var *detailInfo = (Var*)LangElement::find( name );
  131. if ( !detailInfo )
  132. {
  133. detailInfo = new Var;
  134. detailInfo->setType( "float3" );
  135. detailInfo->setName( name );
  136. detailInfo->uniform = true;
  137. detailInfo->constSortPos = cspPotentialPrimitive;
  138. }
  139. return detailInfo;
  140. }
  141. Var* TerrainFeatHLSL::_getMacroIdStrengthParallax()
  142. {
  143. String name( String::ToString( "macroIdStrengthParallax%d", getProcessIndex() ) );
  144. Var *detailInfo = (Var*)LangElement::find( name );
  145. if ( !detailInfo )
  146. {
  147. detailInfo = new Var;
  148. detailInfo->setType( "float3" );
  149. detailInfo->setName( name );
  150. detailInfo->uniform = true;
  151. detailInfo->constSortPos = cspPotentialPrimitive;
  152. }
  153. return detailInfo;
  154. }
  155. void TerrainBaseMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  156. const MaterialFeatureData &fd )
  157. {
  158. MultiLine *meta = new MultiLine;
  159. output = meta;
  160. // Generate the incoming texture var.
  161. Var *inTex;
  162. {
  163. Var *inPos = (Var*)LangElement::find( "inPosition" );
  164. if ( !inPos )
  165. inPos = (Var*)LangElement::find( "position" );
  166. inTex = new Var( "texCoord", "float3" );
  167. Var *oneOverTerrainSize = _getUniformVar( "oneOverTerrainSize", "float", cspPass );
  168. // NOTE: The y coord here should be negative to have
  169. // the texture maps not end up flipped which also caused
  170. // normal and parallax mapping to be incorrect.
  171. //
  172. // This mistake early in development means that the layer
  173. // id bilinear blend depends on it being that way.
  174. //
  175. // So instead i fixed this by flipping the base and detail
  176. // coord y scale to compensate when rendering.
  177. //
  178. meta->addStatement( new GenOp( " @ = @.xyz * float3( @, @, -@ );\r\n",
  179. new DecOp( inTex ), inPos, oneOverTerrainSize, oneOverTerrainSize, oneOverTerrainSize ) );
  180. }
  181. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  182. // Pass the texture coord to the pixel shader.
  183. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  184. outTex->setName( "outTexCoord" );
  185. outTex->setStructName( "OUT" );
  186. outTex->setType( "float3" );
  187. outTex->mapsToSampler = true;
  188. meta->addStatement( new GenOp( " @.xy = @.xy;\r\n", outTex, inTex ) );
  189. // If this shader has a side projected layer then we
  190. // pass the dot product between the +Y and the normal
  191. // thru outTexCoord.z for use in blending the textures.
  192. if ( fd.features.hasFeature( MFT_TerrainSideProject ) )
  193. {
  194. Var *inNormal = (Var*)LangElement::find( "normal" );
  195. meta->addStatement(
  196. new GenOp( " @.z = pow( abs( dot( normalize( float3( @.x, @.y, 0 ) ), float3( 0, 1, 0 ) ) ), 10.0 );\r\n",
  197. outTex, inNormal, inNormal ) );
  198. }
  199. else
  200. meta->addStatement( new GenOp( " @.z = 0;\r\n", outTex ) );
  201. // HACK: This is sort of lazy... we generate the tanget
  202. // vector here so that we're sure it exists in the parallax
  203. // and normal features which will expect "T" to exist.
  204. //
  205. // If this shader doesn't use it the shader compiler will
  206. // optimize away this code.
  207. //
  208. Var *inTangentZ = getVertTexCoord( "tcTangentZ" );
  209. Var *inTanget = new Var( "T", "float3" );
  210. Var *squareSize = _getUniformVar( "squareSize", "float", cspPass );
  211. meta->addStatement( new GenOp( " @ = normalize( float3( @, 0, @ ) );\r\n",
  212. new DecOp( inTanget ), squareSize, inTangentZ ) );
  213. }
  214. void TerrainBaseMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  215. const MaterialFeatureData &fd )
  216. {
  217. // grab connector texcoord register
  218. Var *texCoord = getInTexCoord( "texCoord", "float3", true, componentList );
  219. // create texture var
  220. Var *diffuseMap = new Var;
  221. diffuseMap->setType( "sampler2D" );
  222. diffuseMap->setName( "baseTexMap" );
  223. diffuseMap->uniform = true;
  224. diffuseMap->sampler = true;
  225. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  226. MultiLine *meta = new MultiLine;
  227. Var *baseColor = new Var;
  228. baseColor->setType( "float4" );
  229. baseColor->setName( "baseColor" );
  230. if (mIsDirect3D11)
  231. {
  232. diffuseMap->setType("SamplerState");
  233. Var *diffuseTex = new Var;
  234. diffuseTex->setType("Texture2D");
  235. diffuseTex->setName("baseTexture");
  236. diffuseTex->uniform = true;
  237. diffuseTex->texture = true;
  238. diffuseTex->constNum = diffuseMap->constNum;
  239. meta->addStatement(new GenOp(" @ = @.Sample( @, @.xy );\r\n", new DecOp(baseColor), diffuseTex, diffuseMap, texCoord));
  240. }
  241. else
  242. {
  243. meta->addStatement(new GenOp(" @ = tex2D( @, @.xy );\r\n", new DecOp(baseColor), diffuseMap, texCoord));
  244. }
  245. meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", baseColor, baseColor));
  246. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  247. if (fd.features.hasFeature(MFT_isDeferred))
  248. {
  249. target= ShaderFeature::RenderTarget1;
  250. }
  251. meta->addStatement( new GenOp( " @;\r\n", assignColor( baseColor, Material::Mul,NULL,target ) ) );
  252. output = meta;
  253. }
  254. ShaderFeature::Resources TerrainBaseMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  255. {
  256. Resources res;
  257. res.numTexReg = 1;
  258. res.numTex = 1;
  259. return res;
  260. }
  261. U32 TerrainBaseMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  262. {
  263. return fd.features[MFT_DeferredTerrainBaseMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  264. }
  265. TerrainDetailMapFeatHLSL::TerrainDetailMapFeatHLSL()
  266. : mTorqueDep( "shaders/common/torque.hlsl" ),
  267. mTerrainDep( "shaders/common/terrain/terrain.hlsl" )
  268. {
  269. addDependency( &mTorqueDep );
  270. addDependency( &mTerrainDep );
  271. }
  272. void TerrainDetailMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  273. const MaterialFeatureData &fd )
  274. {
  275. const S32 detailIndex = getProcessIndex();
  276. // Grab incoming texture coords... the base map feature
  277. // made sure this was created.
  278. Var *inTex = (Var*)LangElement::find( "texCoord" );
  279. AssertFatal( inTex, "The texture coord is missing!" );
  280. // Grab the input position.
  281. Var *inPos = (Var*)LangElement::find( "inPosition" );
  282. if ( !inPos )
  283. inPos = (Var*)LangElement::find( "position" );
  284. // Get the object space eye position.
  285. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  286. MultiLine *meta = new MultiLine;
  287. // If we have parallax mapping then make sure we've sent
  288. // the negative view vector to the pixel shader.
  289. if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) &&
  290. !LangElement::find( "outNegViewTS" ) )
  291. {
  292. // Get the object to tangent transform which
  293. // will consume 3 output registers.
  294. Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta, fd );
  295. // Now use a single output register to send the negative
  296. // view vector in tangent space to the pixel shader.
  297. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  298. Var *outNegViewTS = connectComp->getElement( RT_TEXCOORD );
  299. outNegViewTS->setName( "outNegViewTS" );
  300. outNegViewTS->setStructName( "OUT" );
  301. outNegViewTS->setType( "float3" );
  302. meta->addStatement( new GenOp( " @ = mul( @, float3( @ - @.xyz ) );\r\n",
  303. outNegViewTS, objToTangentSpace, eyePos, inPos ) );
  304. }
  305. // Get the distance from the eye to this vertex.
  306. Var *dist = (Var*)LangElement::find( "dist" );
  307. if ( !dist )
  308. {
  309. dist = new Var;
  310. dist->setType( "float" );
  311. dist->setName( "dist" );
  312. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  313. new DecOp( dist ), inPos, eyePos ) );
  314. }
  315. // grab connector texcoord register
  316. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  317. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  318. outTex->setName( String::ToString( "detCoord%d", detailIndex ) );
  319. outTex->setStructName( "OUT" );
  320. outTex->setType( "float4" );
  321. outTex->mapsToSampler = true;
  322. // Get the detail scale and fade info.
  323. Var *detScaleAndFade = new Var;
  324. detScaleAndFade->setType( "float4" );
  325. detScaleAndFade->setName( String::ToString( "detailScaleAndFade%d", detailIndex ) );
  326. detScaleAndFade->uniform = true;
  327. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  328. // Setup the detail coord.
  329. //
  330. // NOTE: You see here we scale the texture coord by 'xyx'
  331. // to generate the detail coord. This y is here because
  332. // its scale is flipped to correct for the non negative y
  333. // in texCoord.
  334. //
  335. // See TerrainBaseMapFeatHLSL::processVert().
  336. //
  337. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) );
  338. // And sneak the detail fade thru the w detailCoord.
  339. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  340. outTex, detScaleAndFade, dist, detScaleAndFade ) );
  341. output = meta;
  342. }
  343. void TerrainDetailMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  344. const MaterialFeatureData &fd )
  345. {
  346. const S32 detailIndex = getProcessIndex();
  347. Var *inTex = getVertTexCoord( "texCoord" );
  348. MultiLine *meta = new MultiLine;
  349. // We need the negative tangent space view vector
  350. // as in parallax mapping we step towards the camera.
  351. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  352. if ( !negViewTS &&
  353. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  354. {
  355. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  356. if ( !inNegViewTS )
  357. {
  358. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  359. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  360. inNegViewTS->setName( "outNegViewTS" );
  361. inNegViewTS->setStructName( "IN" );
  362. inNegViewTS->setType( "float3" );
  363. }
  364. negViewTS = new Var( "negViewTS", "float3" );
  365. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  366. }
  367. // Get the layer samples.
  368. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  369. if ( !layerSample )
  370. {
  371. layerSample = new Var;
  372. layerSample->setType( "float4" );
  373. layerSample->setName( "layerSample" );
  374. // Get the layer texture var
  375. Var *layerTex = new Var;
  376. layerTex->setType( "sampler2D" );
  377. layerTex->setName( "layerTex" );
  378. layerTex->uniform = true;
  379. layerTex->sampler = true;
  380. layerTex->constNum = Var::getTexUnitNum();
  381. if (mIsDirect3D11)
  382. {
  383. layerTex->setType("SamplerState");
  384. Var* layerTexObj = new Var;
  385. layerTexObj->setName("layerTexObj");
  386. layerTexObj->setType("Texture2D");
  387. layerTexObj->uniform = true;
  388. layerTexObj->texture = true;
  389. layerTexObj->constNum = layerTex->constNum;
  390. // Read the layer texture to get the samples.
  391. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  392. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  393. }
  394. else
  395. {
  396. // Read the layer texture to get the samples.
  397. meta->addStatement(new GenOp(" @ = round( tex2D( @, @.xy ) * 255.0f );\r\n",
  398. new DecOp(layerSample), layerTex, inTex));
  399. }
  400. }
  401. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  402. if ( !layerSize )
  403. {
  404. layerSize = new Var;
  405. layerSize->setType( "float" );
  406. layerSize->setName( "layerSize" );
  407. layerSize->uniform = true;
  408. layerSize->constSortPos = cspPass;
  409. }
  410. // Grab the incoming detail coord.
  411. Var *inDet = _getInDetailCoord( componentList );
  412. // Get the detail id.
  413. Var *detailInfo = _getDetailIdStrengthParallax();
  414. // Create the detail blend var.
  415. Var *detailBlend = new Var;
  416. detailBlend->setType( "float" );
  417. detailBlend->setName( String::ToString( "detailBlend%d", detailIndex ) );
  418. // Calculate the blend for this detail texture.
  419. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  420. new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) );
  421. // Get a var and accumulate the blend amount.
  422. Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
  423. if ( !blendTotal )
  424. {
  425. blendTotal = new Var;
  426. blendTotal->setName( "blendTotal" );
  427. blendTotal->setType( "float" );
  428. meta->addStatement( new GenOp( " @ = 0;\r\n", new DecOp( blendTotal ) ) );
  429. }
  430. // Add to the blend total.
  431. meta->addStatement(new GenOp(" @ = max( @, @ );\r\n", blendTotal, blendTotal, detailBlend));
  432. // If we had a parallax feature... then factor in the parallax
  433. // amount so that it fades out with the layer blending.
  434. if (fd.features.hasFeature(MFT_TerrainParallaxMap, detailIndex))
  435. {
  436. // Get the rest of our inputs.
  437. Var *normalMap = _getNormalMapTex();
  438. if (mIsDirect3D11)
  439. {
  440. String name(String::ToString("normalMapTex%d", getProcessIndex()));
  441. Var *normalMapTex = (Var*)LangElement::find(name);
  442. if (!normalMapTex)
  443. {
  444. normalMapTex = new Var;
  445. normalMapTex->setName(String::ToString("normalMapTex%d", getProcessIndex()));
  446. normalMapTex->setType("Texture2D");
  447. normalMapTex->uniform = true;
  448. normalMapTex->texture = true;
  449. normalMapTex->constNum = normalMap->constNum;
  450. }
  451. // Call the library function to do the rest.
  452. if (fd.features.hasFeature(MFT_IsDXTnm, detailIndex))
  453. {
  454. meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @, @.xy, @, @.z * @ );\r\n",
  455. inDet, normalMapTex, normalMap, inDet, negViewTS, detailInfo, detailBlend));
  456. }
  457. else
  458. {
  459. meta->addStatement(new GenOp(" @.xy += parallaxOffset( @, @, @.xy, @, @.z * @ );\r\n",
  460. inDet, normalMapTex, normalMap, inDet, negViewTS, detailInfo, detailBlend));
  461. }
  462. }
  463. else
  464. {
  465. // Call the library function to do the rest.
  466. if (fd.features.hasFeature(MFT_IsDXTnm, detailIndex))
  467. {
  468. meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @.xy, @, @.z * @ );\r\n",
  469. inDet, normalMap, inDet, negViewTS, detailInfo, detailBlend));
  470. }
  471. else
  472. {
  473. meta->addStatement(new GenOp(" @.xy += parallaxOffset( @, @.xy, @, @.z * @ );\r\n",
  474. inDet, normalMap, inDet, negViewTS, detailInfo, detailBlend));
  475. }
  476. }
  477. }
  478. Var *detailColor = (Var*)LangElement::find( "detailColor" );
  479. if ( !detailColor )
  480. {
  481. detailColor = new Var;
  482. detailColor->setType( "float4" );
  483. detailColor->setName( "detailColor" );
  484. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  485. }
  486. // Get the detail texture.
  487. Var *detailMap = new Var;
  488. detailMap->setType( "sampler2D" );
  489. detailMap->setName( String::ToString( "detailMap%d", detailIndex ) );
  490. detailMap->uniform = true;
  491. detailMap->sampler = true;
  492. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  493. // If we're using SM 3.0 then take advantage of
  494. // dynamic branching to skip layers per-pixel.
  495. if ( GFX->getPixelShaderVersion() >= 3.0f )
  496. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  497. meta->addStatement( new GenOp( " {\r\n" ) );
  498. // Note that we're doing the standard greyscale detail
  499. // map technique here which can darken and lighten the
  500. // diffuse texture.
  501. //
  502. // We take two color samples and lerp between them for
  503. // side projection layers... else a single sample.
  504. //
  505. //Sampled detail texture that is not expanded
  506. if (mIsDirect3D11)
  507. {
  508. detailMap->setType("SamplerState");
  509. Var* detailTex = new Var;
  510. detailTex->setName(String::ToString("detailTex%d", detailIndex));
  511. detailTex->setType("Texture2D");
  512. detailTex->uniform = true;
  513. detailTex->texture = true;
  514. detailTex->constNum = detailMap->constNum;
  515. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  516. {
  517. meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, @.yz ), @.Sample( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n",
  518. detailColor, detailTex, detailMap, inDet, detailTex, detailMap, inDet, inTex));
  519. }
  520. else
  521. {
  522. meta->addStatement(new GenOp(" @ = ( @.Sample( @, @.xy ) * 2.0 ) - 1.0;\r\n",
  523. detailColor, detailTex, detailMap, inDet));
  524. }
  525. }
  526. else
  527. {
  528. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  529. {
  530. meta->addStatement(new GenOp(" @ = ( lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n",
  531. detailColor, detailMap, inDet, detailMap, inDet, inTex));
  532. }
  533. else
  534. {
  535. meta->addStatement(new GenOp(" @ = ( tex2D( @, @.xy ) * 2.0 ) - 1.0;\r\n",
  536. detailColor, detailMap, inDet));
  537. }
  538. }
  539. meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
  540. detailColor, detailInfo, inDet ) );
  541. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  542. if(fd.features.hasFeature( MFT_DeferredTerrainDetailMap ))
  543. target= ShaderFeature::RenderTarget1;
  544. Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
  545. meta->addStatement( new GenOp( " @ += @ * @;\r\n",
  546. outColor, detailColor, detailBlend));
  547. meta->addStatement( new GenOp( " }\r\n" ) );
  548. output = meta;
  549. }
  550. ShaderFeature::Resources TerrainDetailMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  551. {
  552. Resources res;
  553. if ( getProcessIndex() == 0 )
  554. {
  555. // If this is the first detail pass then we
  556. // samples from the layer tex.
  557. res.numTex += 1;
  558. // If this material also does parallax then it
  559. // will generate the negative view vector and the
  560. // worldToTanget transform.
  561. if ( fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  562. res.numTexReg += 4;
  563. }
  564. // sample from the detail texture for diffuse coloring.
  565. res.numTex += 1;
  566. // If we have parallax for this layer then we'll also
  567. // be sampling the normal map for the parallax heightmap.
  568. if ( fd.features.hasFeature( MFT_TerrainParallaxMap, getProcessIndex() ) )
  569. res.numTex += 1;
  570. // Finally we always send the detail texture
  571. // coord to the pixel shader.
  572. res.numTexReg += 1;
  573. return res;
  574. }
  575. U32 TerrainDetailMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  576. {
  577. return fd.features[MFT_DeferredTerrainDetailMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  578. }
  579. TerrainMacroMapFeatHLSL::TerrainMacroMapFeatHLSL()
  580. : mTorqueDep( "shaders/common/torque.hlsl" ),
  581. mTerrainDep( "shaders/common/terrain/terrain.hlsl" )
  582. {
  583. addDependency( &mTorqueDep );
  584. addDependency( &mTerrainDep );
  585. }
  586. void TerrainMacroMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  587. const MaterialFeatureData &fd )
  588. {
  589. const S32 detailIndex = getProcessIndex();
  590. // Grab incoming texture coords... the base map feature
  591. // made sure this was created.
  592. Var *inTex = (Var*)LangElement::find( "texCoord" );
  593. AssertFatal( inTex, "The texture coord is missing!" );
  594. // Grab the input position.
  595. Var *inPos = (Var*)LangElement::find( "inPosition" );
  596. if ( !inPos )
  597. inPos = (Var*)LangElement::find( "position" );
  598. // Get the object space eye position.
  599. Var *eyePos = _getUniformVar( "eyePos", "float3", cspPotentialPrimitive );
  600. MultiLine *meta = new MultiLine;
  601. // Get the distance from the eye to this vertex.
  602. Var *dist = (Var*)LangElement::find( "macroDist" );
  603. if ( !dist )
  604. {
  605. dist = new Var;
  606. dist->setType( "float" );
  607. dist->setName( "macroDist" );
  608. meta->addStatement( new GenOp( " @ = distance( @.xyz, @ );\r\n",
  609. new DecOp( dist ), inPos, eyePos ) );
  610. }
  611. // grab connector texcoord register
  612. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  613. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  614. outTex->setName( String::ToString( "macroCoord%d", detailIndex ) );
  615. outTex->setStructName( "OUT" );
  616. outTex->setType( "float4" );
  617. outTex->mapsToSampler = true;
  618. // Get the detail scale and fade info.
  619. Var *detScaleAndFade = new Var;
  620. detScaleAndFade->setType( "float4" );
  621. detScaleAndFade->setName( String::ToString( "macroScaleAndFade%d", detailIndex ) );
  622. detScaleAndFade->uniform = true;
  623. detScaleAndFade->constSortPos = cspPotentialPrimitive;
  624. // Setup the detail coord.
  625. meta->addStatement( new GenOp( " @.xyz = @ * @.xyx;\r\n", outTex, inTex, detScaleAndFade ) );
  626. // And sneak the detail fade thru the w detailCoord.
  627. meta->addStatement( new GenOp( " @.w = clamp( ( @.z - @ ) * @.w, 0.0, 1.0 );\r\n",
  628. outTex, detScaleAndFade, dist, detScaleAndFade ) );
  629. output = meta;
  630. }
  631. void TerrainMacroMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  632. const MaterialFeatureData &fd )
  633. {
  634. const S32 detailIndex = getProcessIndex();
  635. Var *inTex = getVertTexCoord( "texCoord" );
  636. MultiLine *meta = new MultiLine;
  637. // We need the negative tangent space view vector
  638. // as in parallax mapping we step towards the camera.
  639. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  640. if ( !negViewTS &&
  641. fd.features.hasFeature( MFT_TerrainParallaxMap ) )
  642. {
  643. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  644. if ( !inNegViewTS )
  645. {
  646. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  647. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  648. inNegViewTS->setName( "outNegViewTS" );
  649. inNegViewTS->setStructName( "IN" );
  650. inNegViewTS->setType( "float3" );
  651. }
  652. negViewTS = new Var( "negViewTS", "float3" );
  653. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  654. }
  655. // Get the layer samples.
  656. Var *layerSample = (Var*)LangElement::find( "layerSample" );
  657. if ( !layerSample )
  658. {
  659. layerSample = new Var;
  660. layerSample->setType( "float4" );
  661. layerSample->setName( "layerSample" );
  662. // Get the layer texture var
  663. Var *layerTex = new Var;
  664. layerTex->setType( "sampler2D" );
  665. layerTex->setName( "macrolayerTex" );
  666. layerTex->uniform = true;
  667. layerTex->sampler = true;
  668. layerTex->constNum = Var::getTexUnitNum();
  669. // Read the layer texture to get the samples.
  670. if (mIsDirect3D11)
  671. {
  672. layerTex->setType("SamplerState");
  673. Var *layerTexObj = new Var;
  674. layerTexObj->setType("Texture2D");
  675. layerTexObj->setName("macroLayerTexObj");
  676. layerTexObj->uniform = true;
  677. layerTexObj->texture = true;
  678. layerTexObj->constNum = layerTex->constNum;
  679. meta->addStatement(new GenOp(" @ = round( @.Sample( @, @.xy ) * 255.0f );\r\n",
  680. new DecOp(layerSample), layerTexObj, layerTex, inTex));
  681. }
  682. else
  683. meta->addStatement(new GenOp(" @ = round( tex2D( @, @.xy ) * 255.0f );\r\n",
  684. new DecOp(layerSample), layerTex, inTex));
  685. }
  686. Var *layerSize = (Var*)LangElement::find( "layerSize" );
  687. if ( !layerSize )
  688. {
  689. layerSize = new Var;
  690. layerSize->setType( "float" );
  691. layerSize->setName( "layerSize" );
  692. layerSize->uniform = true;
  693. layerSize->constSortPos = cspPass;
  694. }
  695. // Grab the incoming detail coord.
  696. Var *inDet = _getInMacroCoord( componentList );
  697. // Get the detail id.
  698. Var *detailInfo = _getMacroIdStrengthParallax();
  699. // Create the detail blend var.
  700. Var *detailBlend = new Var;
  701. detailBlend->setType( "float" );
  702. detailBlend->setName( String::ToString( "macroBlend%d", detailIndex ) );
  703. // Calculate the blend for this detail texture.
  704. meta->addStatement( new GenOp( " @ = calcBlend( @.x, @.xy, @, @ );\r\n",
  705. new DecOp( detailBlend ), detailInfo, inTex, layerSize, layerSample ) );
  706. // Get a var and accumulate the blend amount.
  707. Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
  708. if ( !blendTotal )
  709. {
  710. blendTotal = new Var;
  711. blendTotal->setName( "blendTotal" );
  712. blendTotal->setType( "float" );
  713. meta->addStatement( new GenOp( " @ = 0;\r\n", new DecOp( blendTotal ) ) );
  714. }
  715. // Add to the blend total.
  716. meta->addStatement(new GenOp(" @ = max( @, @ );\r\n", blendTotal, blendTotal, detailBlend));
  717. Var *detailColor = (Var*)LangElement::find( "macroColor" );
  718. if ( !detailColor )
  719. {
  720. detailColor = new Var;
  721. detailColor->setType( "float4" );
  722. detailColor->setName( "macroColor" );
  723. meta->addStatement( new GenOp( " @;\r\n", new DecOp( detailColor ) ) );
  724. }
  725. // Get the detail texture.
  726. Var *detailMap = new Var;
  727. detailMap->setType( "sampler2D" );
  728. detailMap->setName( String::ToString( "macroMap%d", detailIndex ) );
  729. detailMap->uniform = true;
  730. detailMap->sampler = true;
  731. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  732. //Create texture object for directx 11
  733. Var *detailTex = NULL;
  734. if (mIsDirect3D11)
  735. {
  736. detailMap->setType("SamplerState");
  737. detailTex = new Var;
  738. detailTex->setName(String::ToString("macroMapTex%d", detailIndex));
  739. detailTex->setType("Texture2D");
  740. detailTex->uniform = true;
  741. detailTex->texture = true;
  742. detailTex->constNum = detailMap->constNum;
  743. }
  744. // If we're using SM 3.0 then take advantage of
  745. // dynamic branching to skip layers per-pixel.
  746. if ( GFX->getPixelShaderVersion() >= 3.0f )
  747. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  748. meta->addStatement( new GenOp( " {\r\n" ) );
  749. // Note that we're doing the standard greyscale detail
  750. // map technique here which can darken and lighten the
  751. // diffuse texture.
  752. //
  753. // We take two color samples and lerp between them for
  754. // side projection layers... else a single sample.
  755. //
  756. if (fd.features.hasFeature(MFT_TerrainSideProject, detailIndex))
  757. {
  758. if (mIsDirect3D11)
  759. {
  760. meta->addStatement(new GenOp(" @ = ( lerp( @.Sample( @, @.yz ), @.Sample( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n",
  761. detailColor, detailTex, detailMap, inDet, detailTex, detailMap, inDet, inTex));
  762. }
  763. else
  764. meta->addStatement(new GenOp(" @ = ( lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z ) * 2.0 ) - 1.0;\r\n",
  765. detailColor, detailMap, inDet, detailMap, inDet, inTex));
  766. }
  767. else
  768. {
  769. if (mIsDirect3D11)
  770. meta->addStatement(new GenOp(" @ = ( @.Sample( @, @.xy ) * 2.0 ) - 1.0;\r\n",
  771. detailColor, detailTex, detailMap, inDet));
  772. else
  773. meta->addStatement(new GenOp(" @ = ( tex2D( @, @.xy ) * 2.0 ) - 1.0;\r\n",
  774. detailColor, detailMap, inDet));
  775. }
  776. meta->addStatement( new GenOp( " @ *= @.y * @.w;\r\n",
  777. detailColor, detailInfo, inDet ) );
  778. ShaderFeature::OutputTarget target = ShaderFeature::DefaultTarget;
  779. if(fd.features.hasFeature(MFT_DeferredTerrainMacroMap))
  780. target= ShaderFeature::RenderTarget1;
  781. Var *outColor = (Var*)LangElement::find( getOutputTargetVarName(target) );
  782. meta->addStatement(new GenOp(" @ += @ * @;\r\n",
  783. outColor, detailColor, detailBlend));
  784. meta->addStatement( new GenOp( " }\r\n" ) );
  785. output = meta;
  786. }
  787. ShaderFeature::Resources TerrainMacroMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  788. {
  789. Resources res;
  790. if ( getProcessIndex() == 0 )
  791. {
  792. // If this is the first detail pass then we
  793. // samples from the layer tex.
  794. res.numTex += 1;
  795. }
  796. res.numTex += 1;
  797. // Finally we always send the detail texture
  798. // coord to the pixel shader.
  799. res.numTexReg += 1;
  800. return res;
  801. }
  802. U32 TerrainMacroMapFeatHLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  803. {
  804. return fd.features[MFT_DeferredTerrainMacroMap] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  805. }
  806. void TerrainNormalMapFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  807. const MaterialFeatureData &fd )
  808. {
  809. // We only need to process normals during the prepass.
  810. if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
  811. return;
  812. MultiLine *meta = new MultiLine;
  813. // Make sure the world to tangent transform
  814. // is created and available for the pixel shader.
  815. getOutViewToTangent( componentList, meta, fd );
  816. output = meta;
  817. }
  818. void TerrainNormalMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  819. const MaterialFeatureData &fd )
  820. {
  821. MultiLine *meta = new MultiLine;
  822. Var *viewToTangent = getInViewToTangent( componentList );
  823. // This var is read from GBufferConditionerHLSL and
  824. // used in the prepass output.
  825. Var *gbNormal = (Var*)LangElement::find( "gbNormal" );
  826. if ( !gbNormal )
  827. {
  828. gbNormal = new Var;
  829. gbNormal->setName( "gbNormal" );
  830. gbNormal->setType( "float3" );
  831. meta->addStatement( new GenOp( " @ = @[2];\r\n", new DecOp( gbNormal ), viewToTangent ) );
  832. }
  833. const S32 normalIndex = getProcessIndex();
  834. Var *detailBlend = (Var*)LangElement::find( String::ToString( "detailBlend%d", normalIndex ) );
  835. AssertFatal( detailBlend, "The detail blend is missing!" );
  836. // If we're using SM 3.0 then take advantage of
  837. // dynamic branching to skip layers per-pixel.
  838. if ( GFX->getPixelShaderVersion() >= 3.0f )
  839. meta->addStatement( new GenOp( " if ( @ > 0.0f )\r\n", detailBlend ) );
  840. meta->addStatement( new GenOp( " {\r\n" ) );
  841. // Get the normal map texture.
  842. Var *normalMap = _getNormalMapTex();
  843. /// Get the texture coord.
  844. Var *inDet = _getInDetailCoord( componentList );
  845. Var *inTex = getVertTexCoord( "texCoord" );
  846. // Sample the normal map.
  847. //
  848. // We take two normal samples and lerp between them for
  849. // side projection layers... else a single sample.
  850. LangElement *texOp;
  851. if (mIsDirect3D11)
  852. {
  853. String name(String::ToString("normalMapTex%d", getProcessIndex()));
  854. Var *normalMapTex = (Var*)LangElement::find(name);
  855. if (!normalMapTex)
  856. {
  857. normalMapTex = new Var;
  858. normalMapTex->setName(String::ToString("normalMapTex%d", getProcessIndex()));
  859. normalMapTex->setType("Texture2D");
  860. normalMapTex->uniform = true;
  861. normalMapTex->texture = true;
  862. normalMapTex->constNum = normalMap->constNum;
  863. }
  864. if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex))
  865. {
  866. texOp = new GenOp("lerp( @.Sample( @, @.yz ), @.Sample( @, @.xz ), @.z )",
  867. normalMapTex, normalMap, inDet, normalMapTex, normalMap, inDet, inTex);
  868. }
  869. else
  870. texOp = new GenOp("@.Sample(@, @.xy)", normalMapTex, normalMap, inDet);
  871. }
  872. else
  873. {
  874. if (fd.features.hasFeature(MFT_TerrainSideProject, normalIndex))
  875. {
  876. texOp = new GenOp("lerp( tex2D( @, @.yz ), tex2D( @, @.xz ), @.z )",
  877. normalMap, inDet, normalMap, inDet, inTex);
  878. }
  879. else
  880. texOp = new GenOp("tex2D(@, @.xy)", normalMap, inDet);
  881. }
  882. // create bump normal
  883. Var *bumpNorm = new Var;
  884. bumpNorm->setName( "bumpNormal" );
  885. bumpNorm->setType( "float4" );
  886. LangElement *bumpNormDecl = new DecOp( bumpNorm );
  887. meta->addStatement( expandNormalMap( texOp, bumpNormDecl, bumpNorm, fd ) );
  888. // Normalize is done later...
  889. // Note: The reverse mul order is intentional. Affine matrix.
  890. meta->addStatement( new GenOp( " @ = lerp( @, mul( @.xyz, @ ), min( @, @.w ) );\r\n",
  891. gbNormal, gbNormal, bumpNorm, viewToTangent, detailBlend, inDet ) );
  892. // End the conditional block.
  893. meta->addStatement( new GenOp( " }\r\n" ) );
  894. // If this is the last normal map then we
  895. // can test to see the total blend value
  896. // to see if we should clip the result.
  897. //if ( fd.features.getNextFeatureIndex( MFT_TerrainNormalMap, normalIndex ) == -1 )
  898. //meta->addStatement( new GenOp( " clip( @ - 0.0001f );\r\n", blendTotal ) );
  899. output = meta;
  900. }
  901. ShaderFeature::Resources TerrainNormalMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  902. {
  903. Resources res;
  904. // We only need to process normals during the prepass.
  905. if ( fd.features.hasFeature( MFT_PrePassConditioner ) )
  906. {
  907. // If this is the first normal map and there
  908. // are no parallax features then we will
  909. // generate the worldToTanget transform.
  910. if ( !fd.features.hasFeature( MFT_TerrainParallaxMap ) &&
  911. ( getProcessIndex() == 0 || !fd.features.hasFeature( MFT_TerrainNormalMap, getProcessIndex() - 1 ) ) )
  912. res.numTexReg = 3;
  913. res.numTex = 1;
  914. }
  915. return res;
  916. }
  917. void TerrainLightMapFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  918. const MaterialFeatureData &fd )
  919. {
  920. // grab connector texcoord register
  921. Var *inTex = (Var*)LangElement::find( "texCoord" );
  922. if ( !inTex )
  923. return;
  924. // Get the lightmap texture.
  925. Var *lightMap = new Var;
  926. lightMap->setType( "sampler2D" );
  927. lightMap->setName( "lightMapTex" );
  928. lightMap->uniform = true;
  929. lightMap->sampler = true;
  930. lightMap->constNum = Var::getTexUnitNum();
  931. MultiLine *meta = new MultiLine;
  932. // Find or create the lightMask value which is read by
  933. // RTLighting to mask out the lights.
  934. //
  935. // The first light is always the sunlight so we apply
  936. // the shadow mask to only the first channel.
  937. //
  938. Var *lightMask = (Var*)LangElement::find( "lightMask" );
  939. if ( !lightMask )
  940. {
  941. lightMask = new Var( "lightMask", "float4" );
  942. meta->addStatement( new GenOp( " @ = 1;\r\n", new DecOp( lightMask ) ) );
  943. }
  944. if (mIsDirect3D11)
  945. {
  946. lightMap->setType("SamplerState");
  947. Var* lightMapTex = new Var;
  948. lightMapTex->setName("lightMapTexObj");
  949. lightMapTex->setType("Texture2D");
  950. lightMapTex->uniform = true;
  951. lightMapTex->texture = true;
  952. lightMapTex->constNum = lightMap->constNum;
  953. meta->addStatement(new GenOp(" @[0] = @.Sample( @, @.xy ).r;\r\n", lightMask, lightMapTex, lightMap, inTex));
  954. }
  955. else
  956. meta->addStatement(new GenOp(" @[0] = tex2D( @, @.xy ).r;\r\n", lightMask, lightMap, inTex));
  957. output = meta;
  958. }
  959. ShaderFeature::Resources TerrainLightMapFeatHLSL::getResources( const MaterialFeatureData &fd )
  960. {
  961. Resources res;
  962. res.numTex = 1;
  963. return res;
  964. }
  965. void TerrainAdditiveFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  966. const MaterialFeatureData &fd )
  967. {
  968. Var *color = NULL;
  969. Var *normal = NULL;
  970. if (fd.features[MFT_DeferredTerrainDetailMap])
  971. {
  972. color = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) );
  973. normal = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::DefaultTarget) );
  974. }
  975. else
  976. color = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::DefaultTarget) );
  977. Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
  978. if ( !color || !blendTotal )
  979. return;
  980. MultiLine *meta = new MultiLine;
  981. meta->addStatement( new GenOp( " clip( @ - 0.0001 );\r\n", blendTotal ) );
  982. meta->addStatement( new GenOp( " @.a = @;\r\n", color, blendTotal ) );
  983. if (normal)
  984. meta->addStatement(new GenOp(" @.a = @;\r\n", normal, blendTotal));
  985. output = meta;
  986. }
  987. //standard matInfo map contains data of the form .r = bitflags, .g = (will contain AO),
  988. //.b = specular strength, a= spec power.
  989. //here, it's merely a cutout for now, so that lightmapping (target3) doesn't get mangled.
  990. //we'll most likely revisit that later. possibly several ways...
  991. U32 TerrainBlankInfoMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  992. {
  993. return fd.features[MFT_DeferredTerrainBaseMap] ? ShaderFeature::RenderTarget2 : ShaderFeature::RenderTarget1;
  994. }
  995. void TerrainBlankInfoMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  996. const MaterialFeatureData &fd)
  997. {
  998. // search for material var
  999. Var *material;
  1000. OutputTarget targ = RenderTarget1;
  1001. if (fd.features[MFT_isDeferred])
  1002. {
  1003. targ = RenderTarget2;
  1004. }
  1005. material = (Var*)LangElement::find(getOutputTargetVarName(targ));
  1006. MultiLine * meta = new MultiLine;
  1007. if (!material)
  1008. {
  1009. // create color var
  1010. material = new Var;
  1011. material->setType("fragout");
  1012. material->setName(getOutputTargetVarName(targ));
  1013. material->setStructName("OUT");
  1014. }
  1015. meta->addStatement(new GenOp(" @ = float4(0.0,0.0,0.0,0.0001);\r\n", material));
  1016. output = meta;
  1017. }