terrFeatureHLSL.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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( " @ += @;\r\n", 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( " @ += @;\r\n", 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. if (fd.features[MFT_DeferredTerrainDetailMap])
  970. color = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget1) );
  971. else
  972. color = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::DefaultTarget) );
  973. Var *blendTotal = (Var*)LangElement::find( "blendTotal" );
  974. if ( !color || !blendTotal )
  975. return;
  976. MultiLine *meta = new MultiLine;
  977. meta->addStatement( new GenOp( " clip( @ - 0.0001 );\r\n", blendTotal ) );
  978. meta->addStatement( new GenOp( " @.a = @;\r\n", color, blendTotal ) );
  979. output = meta;
  980. }
  981. //standard matInfo map contains data of the form .r = bitflags, .g = (will contain AO),
  982. //.b = specular strength, a= spec power.
  983. //here, it's merely a cutout for now, so that lightmapping (target3) doesn't get mangled.
  984. //we'll most likely revisit that later. possibly several ways...
  985. U32 TerrainBlankInfoMapFeatHLSL::getOutputTargets(const MaterialFeatureData &fd) const
  986. {
  987. return fd.features[MFT_DeferredTerrainBaseMap] ? ShaderFeature::RenderTarget2 : ShaderFeature::RenderTarget1;
  988. }
  989. void TerrainBlankInfoMapFeatHLSL::processPix(Vector<ShaderComponent*> &componentList,
  990. const MaterialFeatureData &fd)
  991. {
  992. // search for material var
  993. Var *material;
  994. OutputTarget targ = RenderTarget1;
  995. if (fd.features[MFT_isDeferred])
  996. {
  997. targ = RenderTarget2;
  998. }
  999. material = (Var*)LangElement::find(getOutputTargetVarName(targ));
  1000. MultiLine * meta = new MultiLine;
  1001. if (!material)
  1002. {
  1003. // create color var
  1004. material = new Var;
  1005. material->setType("fragout");
  1006. material->setName(getOutputTargetVarName(targ));
  1007. material->setStructName("OUT");
  1008. }
  1009. meta->addStatement(new GenOp(" @ = float4(0.0,0.0,0.0,0.0001);\r\n", material));
  1010. output = meta;
  1011. }