advancedLightingFeaturesHLSL.cpp 24 KB

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