advancedLightingFeaturesGLSL.cpp 22 KB

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