advancedLightingFeaturesHLSL.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 "lighting/advanced/hlsl/advancedLightingFeaturesHLSL.h"
  24. #include "lighting/advanced/advancedLightBinManager.h"
  25. #include "shaderGen/langElement.h"
  26. #include "shaderGen/shaderOp.h"
  27. #include "shaderGen/conditionerFeature.h"
  28. #include "renderInstance/renderDeferredMgr.h"
  29. #include "materials/processedMaterial.h"
  30. #include "materials/materialFeatureTypes.h"
  31. void DeferredRTLightingFeatHLSL::processPixMacros( Vector<GFXShaderMacro> &macros,
  32. const MaterialFeatureData &fd )
  33. {
  34. // Skip deferred features, and use forward shading instead
  35. if ( !fd.features[MFT_isDeferred] )
  36. {
  37. Parent::processPixMacros( macros, fd );
  38. return;
  39. }
  40. // Pull in the uncondition method for the light info buffer
  41. NamedTexTarget *texTarget = NamedTexTarget::find( AdvancedLightBinManager::smBufferName );
  42. if ( texTarget && texTarget->getConditioner() )
  43. {
  44. ConditionerMethodDependency *unconditionMethod = texTarget->getConditioner()->getConditionerMethodDependency(ConditionerFeature::UnconditionMethod);
  45. unconditionMethod->createMethodMacro( String::ToLower( AdvancedLightBinManager::smBufferName ) + "Uncondition", macros );
  46. addDependency(unconditionMethod);
  47. }
  48. }
  49. void DeferredRTLightingFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  50. const MaterialFeatureData &fd )
  51. {
  52. // Skip deferred features, and use forward shading instead
  53. if ( !fd.features[MFT_isDeferred] )
  54. {
  55. Parent::processVert( componentList, fd );
  56. return;
  57. }
  58. // Pass screen space position to pixel shader to compute a full screen buffer uv
  59. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  60. Var *ssPos = connectComp->getElement( RT_TEXCOORD );
  61. ssPos->setName( "screenspacePos" );
  62. ssPos->setStructName( "OUT" );
  63. ssPos->setType( "float4" );
  64. Var *outPosition = (Var*) LangElement::find( "hpos" );
  65. AssertFatal( outPosition, "No hpos, ohnoes." );
  66. output = new GenOp( " @ = @;\r\n", ssPos, outPosition );
  67. }
  68. void DeferredRTLightingFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  69. const MaterialFeatureData &fd )
  70. {
  71. // Skip deferred features, and use forward shading instead
  72. if ( !fd.features[MFT_isDeferred] )
  73. {
  74. Parent::processPix(componentList, fd);
  75. return;
  76. }
  77. MultiLine *meta = new MultiLine;
  78. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>(componentList[C_CONNECTOR]);
  79. Var *ssPos = connectComp->getElement(RT_TEXCOORD);
  80. ssPos->setName("screenspacePos");
  81. ssPos->setStructName("IN");
  82. ssPos->setType("float4");
  83. Var *uvScene = new Var;
  84. uvScene->setType("float2");
  85. uvScene->setName("uvScene");
  86. LangElement *uvSceneDecl = new DecOp(uvScene);
  87. String rtParamName = String::ToString("rtParams%s", "diffuseLightingBuffer");
  88. Var *rtParams = (Var*)LangElement::find(rtParamName);
  89. if (!rtParams)
  90. {
  91. rtParams = new Var;
  92. rtParams->setType("float4");
  93. rtParams->setName(rtParamName);
  94. rtParams->uniform = true;
  95. rtParams->constSortPos = cspPass;
  96. }
  97. meta->addStatement( new GenOp( " @ = @.xy / @.w;\r\n", uvSceneDecl, ssPos, ssPos ) ); // get the screen coord... its -1 to +1
  98. meta->addStatement( new GenOp( " @ = ( @ + 1.0 ) / 2.0;\r\n", uvScene, uvScene ) ); // get the screen coord to 0 to 1
  99. meta->addStatement( new GenOp( " @.y = 1.0 - @.y;\r\n", uvScene, uvScene ) ); // flip the y axis
  100. meta->addStatement( new GenOp( " @ = ( @ * @.zw ) + @.xy;\r\n", uvScene, uvScene, rtParams, rtParams) ); // scale it down and offset it to the rt size
  101. Var *lightInfoSamp = new Var;
  102. lightInfoSamp->setType( "float4" );
  103. lightInfoSamp->setName( "lightInfoSample" );
  104. // create texture var
  105. Var *lightInfoBuffer = new Var;
  106. lightInfoBuffer->setType( "SamplerState" );
  107. lightInfoBuffer->setName( "lightInfoBuffer" );
  108. lightInfoBuffer->uniform = true;
  109. lightInfoBuffer->sampler = true;
  110. lightInfoBuffer->constNum = Var::getTexUnitNum(); // used as texture unit num here
  111. Var* lightBufferTex = new Var;
  112. lightBufferTex->setName("lightInfoBufferTex");
  113. lightBufferTex->setType("Texture2D");
  114. lightBufferTex->uniform = true;
  115. lightBufferTex->texture = true;
  116. lightBufferTex->constNum = lightInfoBuffer->constNum;
  117. // Declare the RTLighting variables in this feature, they will either be assigned
  118. // in this feature, or in the tonemap/lightmap feature
  119. Var *d_lightcolor = new Var( "d_lightcolor", "float3" );
  120. meta->addStatement( new GenOp( " @;\r\n", new DecOp( d_lightcolor ) ) );
  121. Var *d_NL_Att = new Var( "d_NL_Att", "float" );
  122. meta->addStatement( new GenOp( " @;\r\n", new DecOp( d_NL_Att ) ) );
  123. Var *d_specular = new Var( "d_specular", "float" );
  124. meta->addStatement( new GenOp( " @;\r\n", new DecOp( d_specular ) ) );
  125. // Perform the uncondition here.
  126. String unconditionLightInfo = String::ToLower( AdvancedLightBinManager::smBufferName ) + "Uncondition";
  127. meta->addStatement(new GenOp(avar(" %s(@.Sample(@, @), @, @, @);\r\n",
  128. unconditionLightInfo.c_str()), lightBufferTex, lightInfoBuffer, uvScene, d_lightcolor, d_NL_Att, d_specular));
  129. // This is kind of weak sauce
  130. if( !fd.features[MFT_VertLit] && !fd.features[MFT_ToneMap] && !fd.features[MFT_LightMap] && !fd.features[MFT_SubSurface] )
  131. meta->addStatement( new GenOp( " @;\r\n", assignColor( new GenOp( "float4(@, 1.0)", d_lightcolor ), Material::Mul ) ) );
  132. output = meta;
  133. }
  134. ShaderFeature::Resources DeferredRTLightingFeatHLSL::getResources( const MaterialFeatureData &fd )
  135. {
  136. // Skip deferred features, and use forward shading instead
  137. if ( !fd.features[MFT_isDeferred] )
  138. return Parent::getResources( fd );
  139. // HACK: See DeferredRTLightingFeatHLSL::setTexData.
  140. mLastTexIndex = 0;
  141. Resources res;
  142. res.numTex = 1;
  143. res.numTexReg = 1;
  144. return res;
  145. }
  146. void DeferredRTLightingFeatHLSL::setTexData( Material::StageData &stageDat,
  147. const MaterialFeatureData &fd,
  148. RenderPassData &passData,
  149. U32 &texIndex )
  150. {
  151. // Skip deferred features, and use forward shading instead
  152. if ( !fd.features[MFT_isDeferred] )
  153. {
  154. Parent::setTexData( stageDat, fd, passData, texIndex );
  155. return;
  156. }
  157. NamedTexTarget *texTarget = NamedTexTarget::find( AdvancedLightBinManager::smBufferName );
  158. if( texTarget )
  159. {
  160. // HACK: We store this for use in DeferredRTLightingFeatHLSL::processPix()
  161. // which cannot deduce the texture unit itself.
  162. mLastTexIndex = texIndex;
  163. passData.mTexType[ texIndex ] = Material::TexTarget;
  164. passData.mSamplerNames[ texIndex ]= "diffuseLightingBuffer";
  165. passData.mTexSlot[ texIndex++ ].texTarget = texTarget;
  166. }
  167. }
  168. void DeferredBumpFeatHLSL::processVert( Vector<ShaderComponent*> &componentList,
  169. const MaterialFeatureData &fd )
  170. {
  171. if( fd.features[MFT_DeferredConditioner] )
  172. {
  173. // There is an output conditioner active, so we need to supply a transform
  174. // to the pixel shader.
  175. MultiLine *meta = new MultiLine;
  176. // We need the view to tangent space transform in the pixel shader.
  177. getOutViewToTangent( componentList, meta, fd );
  178. const bool useTexAnim = fd.features[MFT_TexAnim];
  179. // Make sure there are texcoords
  180. if( !fd.features[MFT_Parallax] && !fd.features[MFT_DiffuseMap])
  181. {
  182. getOutTexCoord( "texCoord",
  183. "float2",
  184. useTexAnim,
  185. meta,
  186. componentList );
  187. }
  188. const bool useFoliageTexCoord = fd.features[MFT_Foliage];
  189. if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
  190. addOutDetailTexCoord( componentList,
  191. meta,
  192. useTexAnim, useFoliageTexCoord);
  193. output = meta;
  194. }
  195. else if ( fd.materialFeatures[MFT_NormalsOut] ||
  196. !fd.features[MFT_isDeferred] ||
  197. !fd.features[MFT_RTLighting] )
  198. {
  199. Parent::processVert( componentList, fd );
  200. return;
  201. }
  202. else
  203. {
  204. output = NULL;
  205. }
  206. }
  207. void DeferredBumpFeatHLSL::processPix( Vector<ShaderComponent*> &componentList,
  208. const MaterialFeatureData &fd )
  209. {
  210. // NULL output in case nothing gets handled
  211. output = NULL;
  212. if( fd.features[MFT_DeferredConditioner] )
  213. {
  214. MultiLine *meta = new MultiLine;
  215. Var *viewToTangent = getInViewToTangent( componentList );
  216. // create texture var
  217. Var *bumpMap = getNormalMapTex();
  218. Var *texCoord = getInTexCoord("texCoord", "float2", componentList);
  219. Var *bumpMapTex = (Var*)LangElement::find("bumpMapTex");
  220. LangElement *texOp = new GenOp("@.Sample(@, @)", bumpMapTex, bumpMap, texCoord);
  221. // create bump normal
  222. Var *bumpNorm = new Var;
  223. bumpNorm->setName( "bumpNormal" );
  224. bumpNorm->setType( "float4" );
  225. LangElement *bumpNormDecl = new DecOp( bumpNorm );
  226. meta->addStatement( expandNormalMap( texOp, bumpNormDecl, bumpNorm, fd ) );
  227. // If we have a detail normal map we add the xy coords of
  228. // it to the base normal map. This gives us the effect we
  229. // want with few instructions and minial artifacts.
  230. if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
  231. {
  232. bumpMap = new Var;
  233. bumpMap->setType( "SamplerState" );
  234. bumpMap->setName( "detailBumpMap" );
  235. bumpMap->uniform = true;
  236. bumpMap->sampler = true;
  237. bumpMap->constNum = Var::getTexUnitNum();
  238. Var* detailNormalTex = new Var;
  239. detailNormalTex->setName("detailBumpMapTex");
  240. detailNormalTex->setType("Texture2D");
  241. detailNormalTex->uniform = true;
  242. detailNormalTex->texture = true;
  243. detailNormalTex->constNum = bumpMap->constNum;
  244. texCoord = getInTexCoord("detCoord", "float2", componentList);
  245. texOp = new GenOp("@.Sample(@, @)", detailNormalTex, bumpMap, texCoord);
  246. Var *detailBump = new Var;
  247. detailBump->setName( "detailBump" );
  248. detailBump->setType( "float4" );
  249. meta->addStatement( expandNormalMap( texOp, new DecOp( detailBump ), detailBump, fd ) );
  250. Var *detailBumpScale = new Var;
  251. detailBumpScale->setType( "float" );
  252. detailBumpScale->setName( "detailBumpStrength" );
  253. detailBumpScale->uniform = true;
  254. detailBumpScale->constSortPos = cspPass;
  255. meta->addStatement( new GenOp( " @.xy += @.xy * @;\r\n", bumpNorm, detailBump, detailBumpScale ) );
  256. }
  257. // This var is read from GBufferConditionerHLSL and
  258. // used in the deferred output.
  259. //
  260. // By using the 'half' type here we get a bunch of partial
  261. // precision optimized code on further operations on the normal
  262. // which helps alot on older Geforce cards.
  263. //
  264. Var *gbNormal = new Var;
  265. gbNormal->setName( "gbNormal" );
  266. gbNormal->setType( "half3" );
  267. LangElement *gbNormalDecl = new DecOp( gbNormal );
  268. // Normalize is done later...
  269. // Note: The reverse mul order is intentional. Affine matrix.
  270. meta->addStatement( new GenOp( " @ = (half3)mul( @.xyz, @ );\r\n", gbNormalDecl, bumpNorm, viewToTangent ) );
  271. output = meta;
  272. return;
  273. }
  274. else if (fd.features[MFT_AccuMap])
  275. {
  276. Var *bumpSample = (Var *)LangElement::find( "bumpSample" );
  277. if (bumpSample == NULL)
  278. {
  279. MultiLine *meta = new MultiLine;
  280. Var *texCoord = getInTexCoord("texCoord", "float2", componentList);
  281. Var *bumpMap = getNormalMapTex();
  282. bumpSample = new Var;
  283. bumpSample->setType("float4");
  284. bumpSample->setName("bumpSample");
  285. LangElement *bumpSampleDecl = new DecOp(bumpSample);
  286. Var *bumpMapTex = (Var *)LangElement::find("bumpMapTex");
  287. output = new GenOp(" @ = @.Sample(@, @);\r\n", bumpSampleDecl, bumpMapTex, bumpMap, texCoord);
  288. if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
  289. {
  290. bumpMap = (Var*)LangElement::find( "detailBumpMap" );
  291. if ( !bumpMap )
  292. {
  293. bumpMap = new Var;
  294. bumpMap->setType( "sampler2D" );
  295. bumpMap->setName( "detailBumpMap" );
  296. bumpMap->uniform = true;
  297. bumpMap->sampler = true;
  298. bumpMap->constNum = Var::getTexUnitNum();
  299. }
  300. bumpMapTex = (Var*)LangElement::find("detailBumpMap");
  301. if (!bumpMapTex)
  302. {
  303. bumpMap->setType("SamplerState");
  304. bumpMapTex = new Var;
  305. bumpMapTex->setName("detailBumpMapTex");
  306. bumpMapTex->setType("Texture2D");
  307. bumpMapTex->uniform = true;
  308. bumpMapTex->texture = true;
  309. bumpMapTex->constNum = bumpMap->constNum;
  310. }
  311. texCoord = getInTexCoord( "detCoord", "float2", componentList );
  312. LangElement *texOp = new GenOp("@.Sample(@, @)", bumpMap, bumpMapTex, texCoord);
  313. Var *detailBump = new Var;
  314. detailBump->setName( "detailBump" );
  315. detailBump->setType( "float4" );
  316. meta->addStatement( expandNormalMap( texOp, new DecOp( detailBump ), detailBump, fd ) );
  317. Var *detailBumpScale = new Var;
  318. detailBumpScale->setType( "float" );
  319. detailBumpScale->setName( "detailBumpStrength" );
  320. detailBumpScale->uniform = true;
  321. detailBumpScale->constSortPos = cspPass;
  322. meta->addStatement( new GenOp( " @.xy += @.xy * @;\r\n", bumpSample, detailBump, detailBumpScale ) );
  323. }
  324. output = meta;
  325. return;
  326. }
  327. }
  328. else if ( fd.materialFeatures[MFT_NormalsOut] ||
  329. !fd.features[MFT_isDeferred] ||
  330. !fd.features[MFT_RTLighting] )
  331. {
  332. Parent::processPix( componentList, fd );
  333. return;
  334. }
  335. else if (!fd.features[MFT_PBRConfigMap] )
  336. {
  337. Var *bumpSample = (Var *)LangElement::find( "bumpSample" );
  338. if( bumpSample == NULL )
  339. {
  340. Var *texCoord = getInTexCoord( "texCoord", "float2", componentList );
  341. Var *bumpMap = getNormalMapTex();
  342. Var *bumpMapTex = (Var *)LangElement::find("bumpMapTex");
  343. bumpSample = new Var;
  344. bumpSample->setType("float4");
  345. bumpSample->setName("bumpSample");
  346. LangElement *bumpSampleDecl = new DecOp(bumpSample);
  347. output = new GenOp(" @ = @.Sample(@, @);\r\n", bumpSampleDecl, bumpMapTex, bumpMap, texCoord);
  348. return;
  349. }
  350. }
  351. output = NULL;
  352. }
  353. ShaderFeature::Resources DeferredBumpFeatHLSL::getResources( const MaterialFeatureData &fd )
  354. {
  355. if ( fd.materialFeatures[MFT_NormalsOut] ||
  356. !fd.features[MFT_isDeferred] ||
  357. fd.features[MFT_Parallax] ||
  358. !fd.features[MFT_RTLighting] )
  359. return Parent::getResources( fd );
  360. Resources res;
  361. if(!fd.features[MFT_PBRConfigMap])
  362. {
  363. res.numTex = 1;
  364. res.numTexReg = 1;
  365. if ( fd.features[MFT_DeferredConditioner] &&
  366. fd.features.hasFeature( MFT_DetailNormalMap ) )
  367. {
  368. res.numTex += 1;
  369. if ( !fd.features.hasFeature( MFT_DetailMap ) )
  370. res.numTexReg += 1;
  371. }
  372. }
  373. return res;
  374. }
  375. void DeferredBumpFeatHLSL::setTexData( Material::StageData &stageDat,
  376. const MaterialFeatureData &fd,
  377. RenderPassData &passData,
  378. U32 &texIndex )
  379. {
  380. if ( fd.materialFeatures[MFT_NormalsOut] ||
  381. !fd.features[MFT_isDeferred] ||
  382. !fd.features[MFT_RTLighting] )
  383. {
  384. Parent::setTexData( stageDat, fd, passData, texIndex );
  385. return;
  386. }
  387. if ( !fd.features[MFT_DeferredConditioner] && fd.features[MFT_AccuMap] )
  388. {
  389. passData.mTexType[ texIndex ] = Material::Bump;
  390. passData.mSamplerNames[ texIndex ] = "bumpMap";
  391. passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_NormalMap );
  392. if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
  393. {
  394. passData.mTexType[ texIndex ] = Material::DetailBump;
  395. passData.mSamplerNames[texIndex] = "detailBumpMap";
  396. passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_DetailNormalMap );
  397. }
  398. }
  399. else if ( !fd.features[MFT_Parallax] && !fd.features[MFT_PBRConfigMap] &&
  400. ( fd.features[MFT_DeferredConditioner]) )
  401. {
  402. passData.mTexType[ texIndex ] = Material::Bump;
  403. passData.mSamplerNames[ texIndex ] = "bumpMap";
  404. passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_NormalMap );
  405. if ( fd.features[MFT_DeferredConditioner] &&
  406. fd.features.hasFeature( MFT_DetailNormalMap ) )
  407. {
  408. passData.mTexType[ texIndex ] = Material::DetailBump;
  409. passData.mSamplerNames[ texIndex ] = "detailBumpMap";
  410. passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_DetailNormalMap );
  411. }
  412. }
  413. }
  414. ShaderFeature::Resources DeferredMinnaertHLSL::getResources( const MaterialFeatureData &fd )
  415. {
  416. Resources res;
  417. if( fd.features[MFT_isDeferred] && fd.features[MFT_RTLighting] )
  418. {
  419. res.numTex = 1;
  420. res.numTexReg = 1;
  421. }
  422. return res;
  423. }
  424. void DeferredMinnaertHLSL::setTexData( Material::StageData &stageDat,
  425. const MaterialFeatureData &fd,
  426. RenderPassData &passData,
  427. U32 &texIndex )
  428. {
  429. if( fd.features[MFT_isDeferred] && fd.features[MFT_RTLighting] )
  430. {
  431. NamedTexTarget *texTarget = NamedTexTarget::find(RenderDeferredMgr::BufferName);
  432. if ( texTarget )
  433. {
  434. passData.mTexType[texIndex] = Material::TexTarget;
  435. passData.mSamplerNames[texIndex] = "deferredBuffer";
  436. passData.mTexSlot[ texIndex++ ].texTarget = texTarget;
  437. }
  438. }
  439. }
  440. void DeferredMinnaertHLSL::processPixMacros( Vector<GFXShaderMacro> &macros,
  441. const MaterialFeatureData &fd )
  442. {
  443. if( fd.features[MFT_isDeferred] && fd.features[MFT_RTLighting] )
  444. {
  445. // Pull in the uncondition method for the g buffer
  446. NamedTexTarget *texTarget = NamedTexTarget::find( RenderDeferredMgr::BufferName );
  447. if ( texTarget && texTarget->getConditioner() )
  448. {
  449. ConditionerMethodDependency *unconditionMethod = texTarget->getConditioner()->getConditionerMethodDependency(ConditionerFeature::UnconditionMethod);
  450. unconditionMethod->createMethodMacro( String::ToLower(RenderDeferredMgr::BufferName) + "Uncondition", macros );
  451. addDependency(unconditionMethod);
  452. }
  453. }
  454. }
  455. void DeferredMinnaertHLSL::processVert( Vector<ShaderComponent*> &componentList,
  456. const MaterialFeatureData &fd )
  457. {
  458. // If there is no deferred information, bail on this feature
  459. if( !fd.features[MFT_isDeferred] || !fd.features[MFT_RTLighting] )
  460. {
  461. output = NULL;
  462. return;
  463. }
  464. // Make sure we pass the world space position to the
  465. // pixel shader so we can calculate a view vector.
  466. MultiLine *meta = new MultiLine;
  467. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  468. output = meta;
  469. }
  470. void DeferredMinnaertHLSL::processPix( Vector<ShaderComponent*> &componentList,
  471. const MaterialFeatureData &fd )
  472. {
  473. // If there is no deferred information, bail on this feature
  474. if( !fd.features[MFT_isDeferred] || !fd.features[MFT_RTLighting] )
  475. {
  476. output = NULL;
  477. return;
  478. }
  479. Var *minnaertConstant = new Var;
  480. minnaertConstant->setType( "float" );
  481. minnaertConstant->setName( "minnaertConstant" );
  482. minnaertConstant->uniform = true;
  483. minnaertConstant->constSortPos = cspPotentialPrimitive;
  484. // create texture var
  485. Var *deferredBuffer = new Var;
  486. deferredBuffer->setType( "SamplerState" );
  487. deferredBuffer->setName( "deferredBuffer" );
  488. deferredBuffer->uniform = true;
  489. deferredBuffer->sampler = true;
  490. deferredBuffer->constNum = Var::getTexUnitNum(); // used as texture unit num here
  491. Var* deferredTex = new Var;
  492. deferredTex->setName("deferredTex");
  493. deferredTex->setType("Texture2D");
  494. deferredTex->uniform = true;
  495. deferredTex->texture = true;
  496. deferredTex->constNum = deferredBuffer->constNum;
  497. // Texture coord
  498. Var *uvScene = (Var*) LangElement::find( "uvScene" );
  499. AssertFatal(uvScene != NULL, "Unable to find UVScene, no RTLighting feature?");
  500. MultiLine *meta = new MultiLine;
  501. // Get the world space view vector.
  502. Var *wsViewVec = getWsView( getInWsPosition( componentList ), meta );
  503. String unconditionDeferredMethod = String::ToLower(RenderDeferredMgr::BufferName) + "Uncondition";
  504. Var *d_NL_Att = (Var*)LangElement::find( "d_NL_Att" );
  505. meta->addStatement(new GenOp(avar(" float4 normalDepth = %s(@, ,@, @);\r\n", unconditionDeferredMethod.c_str()), deferredBuffer, deferredTex, uvScene));
  506. meta->addStatement( new GenOp( " float vDotN = dot(normalDepth.xyz, @);\r\n", wsViewVec ) );
  507. meta->addStatement( new GenOp( " float Minnaert = pow( @, @) * pow(vDotN, 1.0 - @);\r\n", d_NL_Att, minnaertConstant, minnaertConstant ) );
  508. meta->addStatement( new GenOp( " @;\r\n", assignColor( new GenOp( "float4(Minnaert, Minnaert, Minnaert, 1.0)" ), Material::Mul ) ) );
  509. output = meta;
  510. }
  511. void DeferredSubSurfaceHLSL::processPix( Vector<ShaderComponent*> &componentList,
  512. const MaterialFeatureData &fd )
  513. {
  514. Var *subSurfaceParams = new Var;
  515. subSurfaceParams->setType( "float4" );
  516. subSurfaceParams->setName( "subSurfaceParams" );
  517. subSurfaceParams->uniform = true;
  518. subSurfaceParams->constSortPos = cspPotentialPrimitive;
  519. Var *d_lightcolor = (Var*)LangElement::find( "d_lightcolor" );
  520. Var *d_NL_Att = (Var*)LangElement::find( "d_NL_Att" );
  521. MultiLine *meta = new MultiLine;
  522. Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  523. if (fd.features[MFT_isDeferred])
  524. {
  525. targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  526. meta->addStatement(new GenOp(" @.rgb += @.rgb*@.a;\r\n", targ, subSurfaceParams, subSurfaceParams));
  527. output = meta;
  528. return;
  529. }
  530. output = meta;
  531. }