advancedLightingFeaturesGLSL.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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", "directLightingBuffer" );
  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( "directLightingBuffer" );
  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. // If this has an interlaced pre-pass, do averaging here
  124. if( fd.features[MFT_InterlacedDeferred] )
  125. {
  126. Var *oneOverTargetSize = (Var*) LangElement::find( "oneOverTargetSize" );
  127. if( !oneOverTargetSize )
  128. {
  129. oneOverTargetSize = new Var;
  130. oneOverTargetSize->setType( "vec2" );
  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 vec3 id_lightcolor;\r\n" ) );
  136. meta->addStatement( new GenOp( avar( " %s(tex2D(@, @ + vec2(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( "vec4(@, 1.0)", d_lightcolor ), Material::Mul ) ) );
  145. output = meta;
  146. }
  147. ShaderFeature::Resources DeferredRTLightingFeatGLSL::getResources( const MaterialFeatureData &fd )
  148. {
  149. // Skip deferred features, and use forward shading instead
  150. if ( !fd.features[MFT_isDeferred] )
  151. return Parent::getResources( fd );
  152. // HACK: See DeferredRTLightingFeatGLSL::setTexData.
  153. mLastTexIndex = 0;
  154. Resources res;
  155. res.numTex = 1;
  156. res.numTexReg = 1;
  157. return res;
  158. }
  159. void DeferredRTLightingFeatGLSL::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_isDeferred] )
  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 DeferredRTLightingFeatGLSL::processPix()
  174. // which cannot deduce the texture unit itself.
  175. mLastTexIndex = texIndex;
  176. passData.mTexType[ texIndex ] = Material::TexTarget;
  177. passData.mSamplerNames[ texIndex ]= "directLightingBuffer";
  178. passData.mTexSlot[ texIndex++ ].texTarget = texTarget;
  179. }
  180. }
  181. void DeferredBumpFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  182. const MaterialFeatureData &fd )
  183. {
  184. if( fd.features[MFT_DeferredConditioner] )
  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. const bool useTexAnim = fd.features[MFT_TexAnim];
  192. // Make sure there are texcoords
  193. if( !fd.features[MFT_Parallax] && !fd.features[MFT_DiffuseMap])
  194. {
  195. getOutTexCoord( "texCoord",
  196. "vec2",
  197. useTexAnim,
  198. meta,
  199. componentList );
  200. }
  201. if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
  202. addOutDetailTexCoord( componentList,
  203. meta,
  204. useTexAnim );
  205. output = meta;
  206. }
  207. else if ( fd.materialFeatures[MFT_NormalsOut] ||
  208. !fd.features[MFT_isDeferred] ||
  209. !fd.features[MFT_RTLighting] )
  210. {
  211. Parent::processVert( componentList, fd );
  212. return;
  213. }
  214. else
  215. {
  216. output = NULL;
  217. }
  218. }
  219. void DeferredBumpFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  220. const MaterialFeatureData &fd )
  221. {
  222. // NULL output in case nothing gets handled
  223. output = NULL;
  224. if( fd.features[MFT_DeferredConditioner] )
  225. {
  226. MultiLine *meta = new MultiLine;
  227. Var *viewToTangent = getInViewToTangent( componentList );
  228. // create texture var
  229. Var *bumpMap = getNormalMapTex();
  230. Var *texCoord = getInTexCoord( "texCoord", "vec2", componentList );
  231. LangElement *texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
  232. // create bump normal
  233. Var *bumpNorm = new Var;
  234. bumpNorm->setName( "bumpNormal" );
  235. bumpNorm->setType( "vec4" );
  236. LangElement *bumpNormDecl = new DecOp( bumpNorm );
  237. meta->addStatement( expandNormalMap( texOp, bumpNormDecl, bumpNorm, fd ) );
  238. // If we have a detail normal map we add the xy coords of
  239. // it to the base normal map. This gives us the effect we
  240. // want with few instructions and minial artifacts.
  241. if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
  242. {
  243. bumpMap = new Var;
  244. bumpMap->setType( "sampler2D" );
  245. bumpMap->setName( "detailBumpMap" );
  246. bumpMap->uniform = true;
  247. bumpMap->sampler = true;
  248. bumpMap->constNum = Var::getTexUnitNum();
  249. texCoord = getInTexCoord( "detCoord", "vec2", componentList );
  250. texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
  251. Var *detailBump = new Var;
  252. detailBump->setName( "detailBump" );
  253. detailBump->setType( "vec4" );
  254. meta->addStatement( expandNormalMap( texOp, new DecOp( detailBump ), detailBump, fd ) );
  255. Var *detailBumpScale = new Var;
  256. detailBumpScale->setType( "float" );
  257. detailBumpScale->setName( "detailBumpStrength" );
  258. detailBumpScale->uniform = true;
  259. detailBumpScale->constSortPos = cspPass;
  260. meta->addStatement( new GenOp( " @.xy += @.xy * @;\r\n", bumpNorm, detailBump, detailBumpScale ) );
  261. }
  262. // This var is read from GBufferConditionerGLSL and
  263. // used in the deferred output.
  264. //
  265. // By using the 'half' type here we get a bunch of partial
  266. // precision optimized code on further operations on the normal
  267. // which helps alot on older Geforce cards.
  268. //
  269. Var *gbNormal = new Var;
  270. gbNormal->setName( "gbNormal" );
  271. gbNormal->setType( "half3" );
  272. LangElement *gbNormalDecl = new DecOp( gbNormal );
  273. // Normalize is done later...
  274. // Note: The reverse mul order is intentional. Affine matrix.
  275. meta->addStatement( new GenOp( " @ = half3(tMul( @.xyz, @ ));\r\n", gbNormalDecl, bumpNorm, viewToTangent ) );
  276. output = meta;
  277. return;
  278. }
  279. else if (fd.features[MFT_AccuMap])
  280. {
  281. Var *bumpSample = (Var *)LangElement::find("bumpSample");
  282. if (bumpSample == NULL)
  283. {
  284. MultiLine *meta = new MultiLine;
  285. Var *texCoord = getInTexCoord("texCoord", "vec2", componentList);
  286. Var *bumpMap = getNormalMapTex();
  287. bumpSample = new Var;
  288. bumpSample->setType("vec4");
  289. bumpSample->setName("bumpSample");
  290. LangElement *bumpSampleDecl = new DecOp(bumpSample);
  291. meta->addStatement(new GenOp(" @ = tex2D(@, @);\r\n", bumpSampleDecl, bumpMap, texCoord));
  292. if (fd.features.hasFeature(MFT_DetailNormalMap))
  293. {
  294. bumpMap = (Var*)LangElement::find("detailBumpMap");
  295. if (!bumpMap) {
  296. bumpMap = new Var;
  297. bumpMap->setType("sampler2D");
  298. bumpMap->setName("detailBumpMap");
  299. bumpMap->uniform = true;
  300. bumpMap->sampler = true;
  301. bumpMap->constNum = Var::getTexUnitNum();
  302. }
  303. texCoord = getInTexCoord("detCoord", "vec2", componentList);
  304. LangElement *texOp = new GenOp("tex2D(@, @)", bumpMap, texCoord);
  305. Var *detailBump = new Var;
  306. detailBump->setName("detailBump");
  307. detailBump->setType("vec4");
  308. meta->addStatement(expandNormalMap(texOp, new DecOp(detailBump), detailBump, fd));
  309. Var *detailBumpScale = new Var;
  310. detailBumpScale->setType("float");
  311. detailBumpScale->setName("detailBumpStrength");
  312. detailBumpScale->uniform = true;
  313. detailBumpScale->constSortPos = cspPass;
  314. meta->addStatement(new GenOp(" @.xy += @.xy * @;\r\n", bumpSample, detailBump, detailBumpScale));
  315. }
  316. output = meta;
  317. return;
  318. }
  319. }
  320. else if ( fd.materialFeatures[MFT_NormalsOut] ||
  321. !fd.features[MFT_isDeferred] ||
  322. !fd.features[MFT_RTLighting] )
  323. {
  324. Parent::processPix( componentList, fd );
  325. return;
  326. }
  327. else if ( fd.features[MFT_PixSpecular] && !fd.features[MFT_SpecularMap] )
  328. {
  329. Var *bumpSample = (Var *)LangElement::find( "bumpSample" );
  330. if( bumpSample == NULL )
  331. {
  332. Var *texCoord = getInTexCoord( "texCoord", "vec2", componentList );
  333. Var *bumpMap = getNormalMapTex();
  334. bumpSample = new Var;
  335. bumpSample->setType( "vec4" );
  336. bumpSample->setName( "bumpSample" );
  337. LangElement *bumpSampleDecl = new DecOp( bumpSample );
  338. output = new GenOp( " @ = tex2D(@, @);\r\n", bumpSampleDecl, bumpMap, texCoord );
  339. return;
  340. }
  341. }
  342. output = NULL;
  343. }
  344. ShaderFeature::Resources DeferredBumpFeatGLSL::getResources( const MaterialFeatureData &fd )
  345. {
  346. if ( fd.materialFeatures[MFT_NormalsOut] ||
  347. !fd.features[MFT_isDeferred] ||
  348. fd.features[MFT_Parallax] ||
  349. !fd.features[MFT_RTLighting] )
  350. return Parent::getResources( fd );
  351. Resources res;
  352. if(!fd.features[MFT_SpecularMap])
  353. {
  354. res.numTex = 1;
  355. res.numTexReg = 1;
  356. if ( fd.features[MFT_DeferredConditioner] &&
  357. fd.features.hasFeature( MFT_DetailNormalMap ) )
  358. {
  359. res.numTex += 1;
  360. if ( !fd.features.hasFeature( MFT_DetailMap ) )
  361. res.numTexReg += 1;
  362. }
  363. }
  364. return res;
  365. }
  366. void DeferredBumpFeatGLSL::setTexData( Material::StageData &stageDat,
  367. const MaterialFeatureData &fd,
  368. RenderPassData &passData,
  369. U32 &texIndex )
  370. {
  371. if ( fd.materialFeatures[MFT_NormalsOut] ||
  372. !fd.features[MFT_isDeferred] ||
  373. !fd.features[MFT_RTLighting] )
  374. {
  375. Parent::setTexData( stageDat, fd, passData, texIndex );
  376. return;
  377. }
  378. if (!fd.features[MFT_DeferredConditioner] && fd.features[MFT_AccuMap])
  379. {
  380. passData.mTexType[texIndex] = Material::Bump;
  381. passData.mSamplerNames[texIndex] = "bumpMap";
  382. passData.mTexSlot[texIndex++].texObject = stageDat.getTex(MFT_NormalMap);
  383. if (fd.features.hasFeature(MFT_DetailNormalMap))
  384. {
  385. passData.mTexType[texIndex] = Material::DetailBump;
  386. passData.mSamplerNames[texIndex] = "detailBumpMap";
  387. passData.mTexSlot[texIndex++].texObject = stageDat.getTex(MFT_DetailNormalMap);
  388. }
  389. }
  390. else if (!fd.features[MFT_Parallax] && !fd.features[MFT_SpecularMap] &&
  391. ( fd.features[MFT_DeferredConditioner] ||
  392. fd.features[MFT_PixSpecular] ) )
  393. {
  394. passData.mTexType[ texIndex ] = Material::Bump;
  395. passData.mSamplerNames[ texIndex ] = "bumpMap";
  396. passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_NormalMap );
  397. if ( fd.features[MFT_DeferredConditioner] &&
  398. fd.features.hasFeature( MFT_DetailNormalMap ) )
  399. {
  400. passData.mTexType[ texIndex ] = Material::DetailBump;
  401. passData.mSamplerNames[ texIndex ] = "detailBumpMap";
  402. passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_DetailNormalMap );
  403. }
  404. }
  405. }
  406. void DeferredPixelSpecularGLSL::processVert( Vector<ShaderComponent*> &componentList,
  407. const MaterialFeatureData &fd )
  408. {
  409. if( !fd.features[MFT_isDeferred] || !fd.features[MFT_RTLighting] )
  410. {
  411. Parent::processVert( componentList, fd );
  412. return;
  413. }
  414. output = NULL;
  415. }
  416. void DeferredPixelSpecularGLSL::processPix( Vector<ShaderComponent*> &componentList,
  417. const MaterialFeatureData &fd )
  418. {
  419. if( !fd.features[MFT_isDeferred] || !fd.features[MFT_RTLighting] )
  420. {
  421. Parent::processPix( componentList, fd );
  422. return;
  423. }
  424. MultiLine *meta = new MultiLine;
  425. Var *specular = new Var;
  426. specular->setType( "float" );
  427. specular->setName( "specular" );
  428. LangElement * specDecl = new DecOp( specular );
  429. Var *specCol = (Var*)LangElement::find( "specularColor" );
  430. if(specCol == NULL)
  431. {
  432. specCol = new Var;
  433. specCol->setType( "vec4" );
  434. specCol->setName( "specularColor" );
  435. specCol->uniform = true;
  436. specCol->constSortPos = cspPotentialPrimitive;
  437. }
  438. Var *smoothness = (Var*)LangElement::find("smoothness");
  439. if (!smoothness)
  440. {
  441. smoothness = new Var("smoothness", "float");
  442. // If the gloss map flag is set, than the specular power is in the alpha
  443. // channel of the specular map
  444. if (fd.features[MFT_GlossMap])
  445. meta->addStatement(new GenOp(" @ = @.a;\r\n", new DecOp(smoothness), specCol));
  446. else
  447. {
  448. smoothness->uniform = true;
  449. smoothness->constSortPos = cspPotentialPrimitive;
  450. }
  451. }
  452. Var *metalness = (Var*)LangElement::find("metalness");
  453. if (!metalness)
  454. {
  455. metalness = new Var("metalness", "float");
  456. metalness->uniform = true;
  457. metalness->constSortPos = cspPotentialPrimitive;
  458. }
  459. Var *lightInfoSamp = (Var *)LangElement::find( "lightInfoSample" );
  460. Var *d_specular = (Var*)LangElement::find( "d_specular" );
  461. Var *d_NL_Att = (Var*)LangElement::find( "d_NL_Att" );
  462. AssertFatal( lightInfoSamp && d_specular && d_NL_Att,
  463. "DeferredPixelSpecularGLSL::processPix - Something hosed the deferred features!" );
  464. if (fd.features[MFT_AccuMap]) {
  465. // change specularity where the accu texture is applied
  466. Var *accuPlc = (Var*)LangElement::find("plc");
  467. Var *accuSpecular = (Var*)LangElement::find("accuSpecular");
  468. if (accuPlc != NULL && accuSpecular != NULL)
  469. //d_specular = clamp(lerp( d_specular, accuSpecular * d_specular, plc.a), 0, 1)
  470. meta->addStatement(new GenOp(" @ = clamp( lerp( @, @ * @, @.a), 0, 1);\r\n", d_specular, d_specular, accuSpecular, d_specular, accuPlc));
  471. }
  472. // (a^m)^n = a^(m*n)
  473. meta->addStatement( new GenOp( " @ = pow( abs(@), max((@ / AL_ConstantSpecularPower),1.0f)) * @;\r\n",
  474. specDecl, d_specular, smoothness, metalness));
  475. LangElement *specMul = new GenOp( "vec4( @.rgb, 0 ) * @", specCol, specular );
  476. LangElement *final = specMul;
  477. // We we have a normal map then mask the specular
  478. if( !fd.features[MFT_SpecularMap] && fd.features[MFT_NormalMap] )
  479. {
  480. Var *bumpSample = (Var*)LangElement::find( "bumpSample" );
  481. final = new GenOp( "@ * @.a", final, bumpSample );
  482. }
  483. // add to color
  484. meta->addStatement( new GenOp( " @;\r\n", assignColor( final, Material::Add ) ) );
  485. output = meta;
  486. }
  487. ShaderFeature::Resources DeferredPixelSpecularGLSL::getResources( const MaterialFeatureData &fd )
  488. {
  489. if( !fd.features[MFT_isDeferred] || !fd.features[MFT_RTLighting] )
  490. return Parent::getResources( fd );
  491. Resources res;
  492. return res;
  493. }
  494. ShaderFeature::Resources DeferredMinnaertGLSL::getResources( const MaterialFeatureData &fd )
  495. {
  496. Resources res;
  497. if( fd.features[MFT_isDeferred] && fd.features[MFT_RTLighting] )
  498. {
  499. res.numTex = 1;
  500. res.numTexReg = 1;
  501. }
  502. return res;
  503. }
  504. void DeferredMinnaertGLSL::setTexData( Material::StageData &stageDat,
  505. const MaterialFeatureData &fd,
  506. RenderPassData &passData,
  507. U32 &texIndex )
  508. {
  509. if( fd.features[MFT_isDeferred] && fd.features[MFT_RTLighting] )
  510. {
  511. NamedTexTarget *texTarget = NamedTexTarget::find(RenderDeferredMgr::BufferName);
  512. if ( texTarget )
  513. {
  514. passData.mTexType[texIndex] = Material::TexTarget;
  515. passData.mSamplerNames[texIndex] = "deferredBuffer";
  516. passData.mTexSlot[ texIndex++ ].texTarget = texTarget;
  517. }
  518. }
  519. }
  520. void DeferredMinnaertGLSL::processPixMacros( Vector<GFXShaderMacro> &macros,
  521. const MaterialFeatureData &fd )
  522. {
  523. if( fd.features[MFT_isDeferred] && fd.features[MFT_RTLighting] )
  524. {
  525. // Pull in the uncondition method for the g buffer
  526. NamedTexTarget *texTarget = NamedTexTarget::find( RenderDeferredMgr::BufferName );
  527. if ( texTarget && texTarget->getConditioner() )
  528. {
  529. ConditionerMethodDependency *unconditionMethod = texTarget->getConditioner()->getConditionerMethodDependency(ConditionerFeature::UnconditionMethod);
  530. unconditionMethod->createMethodMacro( String::ToLower(RenderDeferredMgr::BufferName) + "Uncondition", macros );
  531. addDependency(unconditionMethod);
  532. }
  533. }
  534. }
  535. void DeferredMinnaertGLSL::processVert( Vector<ShaderComponent*> &componentList,
  536. const MaterialFeatureData &fd )
  537. {
  538. // If there is no deferred information, bail on this feature
  539. if( !fd.features[MFT_isDeferred] || !fd.features[MFT_RTLighting] )
  540. {
  541. output = NULL;
  542. return;
  543. }
  544. // Make sure we pass the world space position to the
  545. // pixel shader so we can calculate a view vector.
  546. MultiLine *meta = new MultiLine;
  547. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  548. output = meta;
  549. }
  550. void DeferredMinnaertGLSL::processPix( Vector<ShaderComponent*> &componentList,
  551. const MaterialFeatureData &fd )
  552. {
  553. // If there is no deferred information, bail on this feature
  554. if( !fd.features[MFT_isDeferred] || !fd.features[MFT_RTLighting] )
  555. {
  556. output = NULL;
  557. return;
  558. }
  559. Var *minnaertConstant = new Var;
  560. minnaertConstant->setType( "float" );
  561. minnaertConstant->setName( "minnaertConstant" );
  562. minnaertConstant->uniform = true;
  563. minnaertConstant->constSortPos = cspPotentialPrimitive;
  564. // create texture var
  565. Var *deferredBuffer = new Var;
  566. deferredBuffer->setType( "sampler2D" );
  567. deferredBuffer->setName( "deferredBuffer" );
  568. deferredBuffer->uniform = true;
  569. deferredBuffer->sampler = true;
  570. deferredBuffer->constNum = Var::getTexUnitNum(); // used as texture unit num here
  571. // Texture coord
  572. Var *uvScene = (Var*) LangElement::find( "uvScene" );
  573. AssertFatal(uvScene != NULL, "Unable to find UVScene, no RTLighting feature?");
  574. MultiLine *meta = new MultiLine;
  575. // Get the world space view vector.
  576. Var *wsViewVec = getWsView( getInWsPosition( componentList ), meta );
  577. String unconditionDeferredMethod = String::ToLower(RenderDeferredMgr::BufferName) + "Uncondition";
  578. Var *d_NL_Att = (Var*)LangElement::find( "d_NL_Att" );
  579. meta->addStatement( new GenOp( avar( " vec4 normalDepth = %s(@, @);\r\n", unconditionDeferredMethod.c_str() ), deferredBuffer, uvScene ) );
  580. meta->addStatement( new GenOp( " float vDotN = dot(normalDepth.xyz, @);\r\n", wsViewVec ) );
  581. meta->addStatement( new GenOp( " float Minnaert = pow( @, @) * pow(vDotN, 1.0 - @);\r\n", d_NL_Att, minnaertConstant, minnaertConstant ) );
  582. meta->addStatement( new GenOp( " @;\r\n", assignColor( new GenOp( "vec4(Minnaert, Minnaert, Minnaert, 1.0)" ), Material::Mul ) ) );
  583. output = meta;
  584. }
  585. void DeferredSubSurfaceGLSL::processPix( Vector<ShaderComponent*> &componentList,
  586. const MaterialFeatureData &fd )
  587. {
  588. Var *subSurfaceParams = new Var;
  589. subSurfaceParams->setType( "vec4" );
  590. subSurfaceParams->setName( "subSurfaceParams" );
  591. subSurfaceParams->uniform = true;
  592. subSurfaceParams->constSortPos = cspPotentialPrimitive;
  593. Var *d_lightcolor = (Var*)LangElement::find( "d_lightcolor" );
  594. Var *d_NL_Att = (Var*)LangElement::find( "d_NL_Att" );
  595. MultiLine *meta = new MultiLine;
  596. if (fd.features[MFT_isDeferred])
  597. {
  598. Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
  599. meta->addStatement(new GenOp(" @.rgb += @.rgb*@.a;\r\n", targ, subSurfaceParams, subSurfaceParams));
  600. output = meta;
  601. return;
  602. }
  603. output = meta;
  604. }