advancedLightingFeaturesHLSL.cpp 23 KB

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