advancedLightBinManager.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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/advancedLightBinManager.h"
  24. #include "lighting/advanced/advancedLightManager.h"
  25. #include "lighting/advanced/advancedLightBufferConditioner.h"
  26. #include "lighting/shadowMap/shadowMapManager.h"
  27. #include "lighting/shadowMap/shadowMapPass.h"
  28. #include "lighting/shadowMap/lightShadowMap.h"
  29. #include "lighting/common/lightMapParams.h"
  30. #include "renderInstance/renderDeferredMgr.h"
  31. #include "gfx/gfxTransformSaver.h"
  32. #include "scene/sceneManager.h"
  33. #include "scene/sceneRenderState.h"
  34. #include "materials/materialManager.h"
  35. #include "materials/sceneData.h"
  36. #include "core/util/safeDelete.h"
  37. #include "core/util/rgb2luv.h"
  38. #include "gfx/gfxDebugEvent.h"
  39. #include "math/util/matrixSet.h"
  40. #include "console/consoleTypes.h"
  41. #include "gfx/gfxTextureManager.h"
  42. const RenderInstType AdvancedLightBinManager::RIT_LightInfo( "specularLighting" );
  43. const String AdvancedLightBinManager::smBufferName( "specularLighting" );
  44. ShadowFilterMode AdvancedLightBinManager::smShadowFilterMode = ShadowFilterMode_SoftShadowHighQuality;
  45. bool AdvancedLightBinManager::smPSSMDebugRender = false;
  46. bool AdvancedLightBinManager::smUseSSAOMask = false;
  47. bool AdvancedLightBinManager::smDiffuseLightViz = false;
  48. bool AdvancedLightBinManager::smSpecularLightViz = false;
  49. bool AdvancedLightBinManager::smDetailLightingViz = false;
  50. S32 AdvancedLightBinManager::smMaximumNumOfLights = -1;
  51. bool AdvancedLightBinManager::smUseLightFade = false;
  52. F32 AdvancedLightBinManager::smLightFadeStart = 50;
  53. F32 AdvancedLightBinManager::smLightFadeEnd = 75;
  54. bool AdvancedLightBinManager::smAllowLocalLightShadows = true;
  55. ImplementEnumType( ShadowFilterMode,
  56. "The shadow filtering modes for Advanced Lighting shadows.\n"
  57. "@ingroup AdvancedLighting" )
  58. { ShadowFilterMode_None, "None",
  59. "@brief Simple point sampled filtering.\n"
  60. "This is the fastest and lowest quality mode." },
  61. { ShadowFilterMode_SoftShadow, "SoftShadow",
  62. "@brief A variable tap rotated poisson disk soft shadow filter.\n"
  63. "It performs 4 taps to classify the point as in shadow, out of shadow, or along a "
  64. "shadow edge. Samples on the edge get an additional 8 taps to soften them." },
  65. { ShadowFilterMode_SoftShadowHighQuality, "SoftShadowHighQuality",
  66. "@brief A 12 tap rotated poisson disk soft shadow filter.\n"
  67. "It performs all the taps for every point without any early rejection." },
  68. EndImplementEnumType;
  69. // NOTE: The order here matches that of the LightInfo::Type enum.
  70. const String AdvancedLightBinManager::smLightMatNames[] =
  71. {
  72. "AL_PointLightMaterial", // LightInfo::Point
  73. "AL_SpotLightMaterial", // LightInfo::Spot
  74. "AL_VectorLightMaterial", // LightInfo::Vector
  75. "", // LightInfo::Ambient
  76. };
  77. // NOTE: The order here matches that of the LightInfo::Type enum.
  78. const GFXVertexFormat* AdvancedLightBinManager::smLightMatVertex[] =
  79. {
  80. getGFXVertexFormat<AdvancedLightManager::LightVertex>(), // LightInfo::Point
  81. getGFXVertexFormat<AdvancedLightManager::LightVertex>(), // LightInfo::Spot
  82. getGFXVertexFormat<FarFrustumQuadVert>(), // LightInfo::Vector
  83. NULL, // LightInfo::Ambient
  84. };
  85. // NOTE: The order here matches that of the ShadowType enum.
  86. const String AdvancedLightBinManager::smShadowTypeMacro[] =
  87. {
  88. "", // ShadowType_Spot
  89. "", // ShadowType_PSSM,
  90. "", // ShadowType_Paraboloid,
  91. "SHADOW_DUALPARABOLOID_SINGLE_PASS", // ShadowType_DualParaboloidSinglePass,
  92. "SHADOW_DUALPARABOLOID", // ShadowType_DualParaboloid,
  93. "SHADOW_CUBE", // ShadowType_CubeMap,
  94. };
  95. AdvancedLightBinManager::RenderSignal &AdvancedLightBinManager::getRenderSignal()
  96. {
  97. static RenderSignal theSignal;
  98. return theSignal;
  99. }
  100. IMPLEMENT_CONOBJECT(AdvancedLightBinManager);
  101. ConsoleDocClass( AdvancedLightBinManager,
  102. "@brief Rendering Manager responsible for lighting, shadows, and global variables affecing both.\n\n"
  103. "Should not be exposed to TorqueScript as a game object, meant for internal use only\n\n"
  104. "@ingroup Lighting"
  105. );
  106. AdvancedLightBinManager::AdvancedLightBinManager( AdvancedLightManager *lm /* = NULL */,
  107. ShadowMapManager *sm /* = NULL */,
  108. GFXFormat lightBufferFormat /* = GFXFormatR8G8B8A8 */ )
  109. : RenderBinManager( RIT_LightInfo, 1.0f, 1.0f ),
  110. mNumLightsCulled(0),
  111. mLightManager(lm),
  112. mShadowManager(sm)
  113. {
  114. mMRTLightmapsDuringDeferred = true;
  115. Con::NotifyDelegate callback( this, &AdvancedLightBinManager::_deleteLightMaterials );
  116. Con::addVariableNotify( "$pref::Shadows::filterMode", callback );
  117. Con::addVariableNotify( "$AL::PSSMDebugRender", callback );
  118. Con::addVariableNotify( "$AL::UseSSAOMask", callback );
  119. Con::addVariableNotify( "$AL::DiffuseLightViz", callback);
  120. Con::addVariableNotify( "$AL::SpecularLightViz", callback);
  121. Con::addVariableNotify( "$AL::DetailLightingViz", callback);
  122. }
  123. AdvancedLightBinManager::~AdvancedLightBinManager()
  124. {
  125. _deleteLightMaterials();
  126. Con::NotifyDelegate callback( this, &AdvancedLightBinManager::_deleteLightMaterials );
  127. Con::removeVariableNotify( "$pref::shadows::filterMode", callback );
  128. Con::removeVariableNotify( "$AL::PSSMDebugRender", callback );
  129. Con::removeVariableNotify( "$AL::UseSSAOMask", callback );
  130. Con::removeVariableNotify( "$AL::DiffuseLightViz", callback);
  131. Con::removeVariableNotify( "$AL::SpecularLightViz", callback);
  132. Con::removeVariableNotify( "$AL::DetailLightingViz", callback);
  133. }
  134. void AdvancedLightBinManager::consoleInit()
  135. {
  136. Parent::consoleInit();
  137. Con::addVariable( "$pref::shadows::filterMode",
  138. TYPEID<ShadowFilterMode>(), &smShadowFilterMode,
  139. "The filter mode to use for shadows.\n"
  140. "@ingroup AdvancedLighting\n" );
  141. Con::addVariable( "$AL::UseSSAOMask", TypeBool, &smUseSSAOMask,
  142. "Used by the SSAO PostEffect to toggle the sampling of ssaomask "
  143. "texture by the light shaders.\n"
  144. "@ingroup AdvancedLighting\n" );
  145. Con::addVariable( "$AL::PSSMDebugRender", TypeBool, &smPSSMDebugRender,
  146. "Enables debug rendering of the PSSM shadows.\n"
  147. "@ingroup AdvancedLighting\n" );
  148. Con::addVariable("$AL::DiffuseLightViz", TypeBool, &smDiffuseLightViz,
  149. "Enables debug rendering of the PSSM shadows.\n"
  150. "@ingroup AdvancedLighting\n");
  151. Con::addVariable("$AL::SpecularLightViz", TypeBool, &smSpecularLightViz,
  152. "Enables debug rendering of the PSSM shadows.\n"
  153. "@ingroup AdvancedLighting\n");
  154. Con::addVariable("$AL::DetailLightingViz", TypeBool, &smDetailLightingViz,
  155. "Enables debug rendering of the PSSM shadows.\n"
  156. "@ingroup AdvancedLighting\n");
  157. Con::addVariable("$pref::maximumNumOfLights",
  158. TypeS32, &smMaximumNumOfLights,
  159. "The maximum number of local lights that can be rendered at a time. If set to -1, then no limit.\n");
  160. Con::addVariable("$pref::useLightFade", TypeBool, &smUseLightFade, "Indicates if local lights should utilize the distance-based object fadeout logic.\n");
  161. Con::addVariable("$pref::lightFadeStart", TypeF32, &smLightFadeStart, "Distance at which light fading begins if $pref::useLightFade is on.\n");
  162. Con::addVariable("$pref::lightFadeEnd", TypeF32, &smLightFadeEnd, "Distance at which light fading should have fully faded if $pref::useLightFade is on.\n");
  163. Con::addVariable("$pref::allowLocalLightShadows", TypeBool, &smAllowLocalLightShadows, "Indicates if local lights(point/spot) can cast shadows.\n");
  164. }
  165. bool AdvancedLightBinManager::setTargetSize(const Point2I &newTargetSize)
  166. {
  167. /*bool ret = Parent::setTargetSize( newTargetSize );
  168. // We require the viewport to match the default.
  169. mNamedTarget.setViewport( GFX->getViewport() );
  170. return ret;*/
  171. return true;
  172. }
  173. bool AdvancedLightBinManager::_updateTargets()
  174. {
  175. /* PROFILE_SCOPE(AdvancedLightBinManager_updateTargets);
  176. bool ret = Parent::_updateTargets();
  177. mDiffuseLightingTarget = NamedTexTarget::find("diffuseLighting");
  178. if (mDiffuseLightingTarget.isValid())
  179. {
  180. mDiffuseLightingTex = mDiffuseLightingTarget->getTexture();
  181. for (U32 i = 0; i < mTargetChainLength; i++)
  182. mTargetChain[i]->attachTexture(GFXTextureTarget::Color1, mDiffuseLightingTex);
  183. }
  184. GFX->finalizeReset();
  185. return ret;*/
  186. return true;
  187. }
  188. void AdvancedLightBinManager::addLight( LightInfo *light )
  189. {
  190. // Get the light type.
  191. const LightInfo::Type lightType = light->getType();
  192. AssertFatal( lightType == LightInfo::Point ||
  193. lightType == LightInfo::Spot, "Bogus light type." );
  194. // Find a shadow map for this light, if it has one
  195. ShadowMapParams *lsp = light->getExtended<ShadowMapParams>();
  196. LightShadowMap *lsm = lsp->getShadowMap();
  197. // Get the right shadow type.
  198. ShadowType shadowType = ShadowType_None;
  199. if ( light->getCastShadows() && smAllowLocalLightShadows &&
  200. lsm && lsm->hasShadowTex() &&
  201. !ShadowMapPass::smDisableShadows )
  202. shadowType = lsm->getShadowType();
  203. // Add the entry
  204. LightBinEntry lEntry;
  205. lEntry.lightInfo = light;
  206. lEntry.shadowMap = lsm;
  207. lEntry.lightMaterial = _getLightMaterial( lightType, shadowType, lsp->hasCookieTex() );
  208. if( lightType == LightInfo::Spot )
  209. lEntry.vertBuffer = mLightManager->getConeMesh( lEntry.numPrims, lEntry.primBuffer );
  210. else
  211. lEntry.vertBuffer = mLightManager->getSphereMesh( lEntry.numPrims, lEntry.primBuffer );
  212. // If it's a point light, push front, spot
  213. // light, push back. This helps batches.
  214. Vector<LightBinEntry> &curBin = mLightBin;
  215. if ( light->getType() == LightInfo::Point )
  216. curBin.push_front( lEntry );
  217. else
  218. curBin.push_back( lEntry );
  219. }
  220. void AdvancedLightBinManager::clearAllLights()
  221. {
  222. Con::setIntVariable("lightMetrics::activeLights", mLightBin.size());
  223. Con::setIntVariable("lightMetrics::culledLights", mNumLightsCulled);
  224. mLightBin.clear();
  225. mNumLightsCulled = 0;
  226. }
  227. S32 QSORT_CALLBACK AdvancedLightBinManager::_lightScoreCmp(const LightBinEntry* a, const LightBinEntry* b)
  228. {
  229. F32 diff = a->lightInfo->getScore() - b->lightInfo->getScore();
  230. return diff > 0 ? 1 : diff < 0 ? -1 : 0;
  231. }
  232. void AdvancedLightBinManager::_scoreLights(const MatrixF& cameraTrans)
  233. {
  234. PROFILE_SCOPE(AdvancedLightBinManager_scoreLights);
  235. if (!LIGHTMGR)
  236. return;
  237. // Get all the lights.
  238. const Point3F lumDot(0.2125f, 0.7154f, 0.0721f);
  239. for (LightBinIterator itr = mLightBin.begin(); itr != mLightBin.end(); itr++)
  240. {
  241. // Get the light.
  242. LightBinEntry& light = *itr;
  243. F32 luminace = 0.0f;
  244. F32 weight = 0.0f;
  245. F32 score = 0.0f;
  246. const bool isSpot = light.lightInfo->getType() == LightInfo::Spot;
  247. const bool isPoint = light.lightInfo->getType() == LightInfo::Point;
  248. if (isPoint || isSpot)
  249. {
  250. Point3F distVec = light.lightInfo->getPosition() - cameraTrans.getPosition();
  251. F32 dist = distVec.len();
  252. score = dist;// light.lightInfo->getRange().x / mMax(dist, 1.0f);
  253. // Get the luminocity.
  254. luminace = mDot(light.lightInfo->getColor(), lumDot) * light.lightInfo->getBrightness();
  255. weight = light.lightInfo->getPriority();
  256. //Distance fading test
  257. if (smUseLightFade)
  258. {
  259. if (dist > smLightFadeStart)
  260. {
  261. float fadeOutAmt = (dist - smLightFadeStart) / (smLightFadeEnd - smLightFadeStart);
  262. fadeOutAmt = 1 - fadeOutAmt;
  263. light.lightInfo->setFadeAmount(fadeOutAmt);
  264. }
  265. }
  266. else
  267. {
  268. light.lightInfo->setFadeAmount(1.0);
  269. }
  270. }
  271. light.lightInfo->setScore(score * weight - luminace);
  272. }
  273. // Sort them!
  274. mLightBin.sort(_lightScoreCmp);
  275. }
  276. void AdvancedLightBinManager::render( SceneRenderState *state )
  277. {
  278. PROFILE_SCOPE( AdvancedLightManager_Render );
  279. // Take a look at the SceneRenderState and see if we should skip drawing the pre-pass
  280. if( state->disableAdvancedLightingBins() )
  281. return;
  282. // Automagically save & restore our viewport and transforms.
  283. GFXTransformSaver saver;
  284. if( !mLightManager )
  285. return;
  286. // Get the sunlight. If there's no sun, and no lights in the bins, no draw
  287. LightInfo *sunLight = mLightManager->getSpecialLight( LightManager::slSunLightType, false );
  288. GFXDEBUGEVENT_SCOPE( AdvancedLightBinManager_Render, ColorI::RED );
  289. // Tell the superclass we're about to render
  290. //if ( !_onPreRender( state ) )
  291. // return;
  292. NamedTexTargetRef sceneColorTargetRef = NamedTexTarget::find("AL_FormatToken");
  293. if (sceneColorTargetRef.isNull())
  294. return;
  295. if (mLightingTargetRef.isNull())
  296. mLightingTargetRef = GFX->allocRenderToTextureTarget();
  297. //Do a quick pass to update our probes if they're dirty
  298. //PROBEMGR->updateDirtyProbes();
  299. mLightingTargetRef->attachTexture(GFXTextureTarget::Color0, sceneColorTargetRef->getTexture());
  300. GFX->pushActiveRenderTarget();
  301. GFX->setActiveRenderTarget(mLightingTargetRef);
  302. GFX->setViewport(sceneColorTargetRef->getViewport());
  303. // Restore transforms
  304. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  305. matrixSet.restoreSceneViewProjection();
  306. // Set up the SG Data
  307. SceneData sgData;
  308. sgData.init( state );
  309. // There are cases where shadow rendering is disabled.
  310. const bool disableShadows = /*state->isReflectPass() || */ShadowMapPass::smDisableShadows;
  311. // Pick the right material for rendering the sunlight... we only
  312. // cast shadows when its enabled and we're not in a reflection.
  313. LightMaterialInfo *vectorMatInfo;
  314. if ( sunLight &&
  315. sunLight->getCastShadows() &&
  316. !disableShadows &&
  317. sunLight->getExtended<ShadowMapParams>() )
  318. vectorMatInfo = _getLightMaterial( LightInfo::Vector, ShadowType_PSSM, false );
  319. else
  320. vectorMatInfo = _getLightMaterial( LightInfo::Vector, ShadowType_None, false );
  321. // Initialize and set the per-frame parameters after getting
  322. // the vector light material as we use lazy creation.
  323. _setupPerFrameParameters( state );
  324. // Draw sunlight/ambient
  325. if ( sunLight && vectorMatInfo )
  326. {
  327. GFXDEBUGEVENT_SCOPE( AdvancedLightBinManager_Render_Sunlight, ColorI::RED );
  328. // Set up SG data
  329. setupSGData( sgData, state, sunLight );
  330. vectorMatInfo->setLightParameters( sunLight, state );
  331. // Set light holds the active shadow map.
  332. mShadowManager->setLightShadowMapForLight( sunLight );
  333. // Set geometry
  334. GFX->setVertexBuffer( mFarFrustumQuadVerts );
  335. GFX->setPrimitiveBuffer( NULL );
  336. vectorMatInfo->matInstance->mSpecialLight = true;
  337. // Render the material passes
  338. while( vectorMatInfo->matInstance->setupPass( state, sgData ) )
  339. {
  340. vectorMatInfo->matInstance->setSceneInfo( state, sgData );
  341. vectorMatInfo->matInstance->setTransforms( matrixSet, state );
  342. GFX->drawPrimitive( GFXTriangleStrip, 0, 2 );
  343. }
  344. }
  345. const Frustum& frustum = state->getCameraFrustum();
  346. MatrixF invCam(frustum.getTransform());
  347. invCam.inverse();
  348. const MatrixF& cameraTrans = frustum.getTransform();
  349. if(smUseLightFade || smMaximumNumOfLights != -1)
  350. _scoreLights(cameraTrans);
  351. S32 lightCount = 0;
  352. // Blend the lights in the bin to the light buffer
  353. for( LightBinIterator itr = mLightBin.begin(); itr != mLightBin.end(); itr++ )
  354. {
  355. if (smMaximumNumOfLights != -1 && lightCount >= smMaximumNumOfLights)
  356. break;
  357. lightCount++;
  358. LightBinEntry& curEntry = *itr;
  359. LightInfo *curLightInfo = curEntry.lightInfo;
  360. if (curEntry.lightInfo->getType() >= LightInfo::Vector)
  361. continue;
  362. LightMaterialInfo *curLightMat = curEntry.lightMaterial;
  363. const U32 numPrims = curEntry.numPrims;
  364. const U32 numVerts = curEntry.vertBuffer->mNumVerts;
  365. ShadowMapParams *lsp = curLightInfo->getExtended<ShadowMapParams>();
  366. // Skip lights which won't affect the scene.
  367. if (!curLightMat || curLightInfo->getBrightness() * curLightInfo->getFadeAmount() <= 0.001f)
  368. continue;
  369. GFXDEBUGEVENT_SCOPE( AdvancedLightBinManager_Render_Light, ColorI::RED );
  370. setupSGData( sgData, state, curLightInfo );
  371. curLightMat->setLightParameters( curLightInfo, state );
  372. mShadowManager->setLightShadowMap( curEntry.shadowMap );
  373. // Set geometry
  374. GFX->setVertexBuffer( curEntry.vertBuffer );
  375. GFX->setPrimitiveBuffer( curEntry.primBuffer );
  376. lsp->getOcclusionQuery()->begin();
  377. curLightMat->matInstance->mSpecialLight = false;
  378. // Render the material passes
  379. while( curLightMat->matInstance->setupPass( state, sgData ) )
  380. {
  381. // Set transforms
  382. matrixSet.setWorld(*sgData.objTrans);
  383. curLightMat->matInstance->setTransforms(matrixSet, state);
  384. curLightMat->matInstance->setSceneInfo(state, sgData);
  385. if(curEntry.primBuffer)
  386. GFX->drawIndexedPrimitive(GFXTriangleList, 0, 0, numVerts, 0, numPrims);
  387. else
  388. GFX->drawPrimitive(GFXTriangleList, 0, numPrims);
  389. }
  390. lsp->getOcclusionQuery()->end();
  391. }
  392. // Set NULL for active shadow map (so nothing gets confused)
  393. mShadowManager->setLightShadowMap(NULL);
  394. GFX->setVertexBuffer( NULL );
  395. GFX->setPrimitiveBuffer( NULL );
  396. // Fire off a signal to let others know that light-bin rendering is ending now
  397. getRenderSignal().trigger(state, this);
  398. // Finish up the rendering
  399. //_onPostRender();
  400. GFX->popActiveRenderTarget();
  401. }
  402. AdvancedLightBinManager::LightMaterialInfo* AdvancedLightBinManager::_getLightMaterial( LightInfo::Type lightType,
  403. ShadowType shadowType,
  404. bool useCookieTex )
  405. {
  406. PROFILE_SCOPE( AdvancedLightBinManager_GetLightMaterial );
  407. // Build the key.
  408. const LightMatKey key( lightType, shadowType, useCookieTex );
  409. // See if we've already built this one.
  410. LightMatTable::Iterator iter = mLightMaterials.find( key );
  411. if ( iter != mLightMaterials.end() )
  412. return iter->value;
  413. // If we got here we need to build a material for
  414. // this light+shadow combination.
  415. LightMaterialInfo *info = NULL;
  416. // First get the light material name and make sure
  417. // this light has a material in the first place.
  418. const String &lightMatName = smLightMatNames[ lightType ];
  419. if ( lightMatName.isNotEmpty() )
  420. {
  421. Vector<GFXShaderMacro> shadowMacros;
  422. // Setup the shadow type macros for this material.
  423. if ( shadowType == ShadowType_None )
  424. shadowMacros.push_back( GFXShaderMacro( "NO_SHADOW" ) );
  425. else
  426. {
  427. shadowMacros.push_back( GFXShaderMacro( smShadowTypeMacro[ shadowType ] ) );
  428. // Do we need to do shadow filtering?
  429. if ( smShadowFilterMode != ShadowFilterMode_None )
  430. {
  431. shadowMacros.push_back( GFXShaderMacro( "SOFTSHADOW" ) );
  432. const F32 SM = GFX->getPixelShaderVersion();
  433. if ( SM >= 3.0f && smShadowFilterMode == ShadowFilterMode_SoftShadowHighQuality )
  434. shadowMacros.push_back( GFXShaderMacro( "SOFTSHADOW_HIGH_QUALITY" ) );
  435. }
  436. }
  437. if ( useCookieTex )
  438. shadowMacros.push_back( GFXShaderMacro( "USE_COOKIE_TEX" ) );
  439. // Its safe to add the PSSM debug macro to all the materials.
  440. if ( smPSSMDebugRender )
  441. shadowMacros.push_back( GFXShaderMacro( "PSSM_DEBUG_RENDER" ) );
  442. if( smDiffuseLightViz )
  443. shadowMacros.push_back(GFXShaderMacro("DIFFUSE_LIGHT_VIZ"));
  444. else if (smSpecularLightViz)
  445. shadowMacros.push_back(GFXShaderMacro("SPECULAR_LIGHT_VIZ"));
  446. else if (smDetailLightingViz)
  447. shadowMacros.push_back(GFXShaderMacro("DETAIL_LIGHTING_VIZ"));
  448. // Now create the material info object.
  449. info = new LightMaterialInfo( lightMatName, smLightMatVertex[ lightType ], shadowMacros );
  450. }
  451. // Push this into the map and return it.
  452. mLightMaterials.insertUnique( key, info );
  453. return info;
  454. }
  455. void AdvancedLightBinManager::_deleteLightMaterials()
  456. {
  457. LightMatTable::Iterator iter = mLightMaterials.begin();
  458. for ( ; iter != mLightMaterials.end(); iter++ )
  459. delete iter->value;
  460. mLightMaterials.clear();
  461. }
  462. void AdvancedLightBinManager::_setupPerFrameParameters( const SceneRenderState *state )
  463. {
  464. PROFILE_SCOPE( AdvancedLightBinManager_SetupPerFrameParameters );
  465. const Frustum &frustum = state->getCameraFrustum();
  466. MatrixF invCam( frustum.getTransform() );
  467. invCam.inverse();
  468. const Point3F *wsFrustumPoints = frustum.getPoints();
  469. const Point3F& cameraPos = frustum.getPosition();
  470. // Perform a camera offset. We need to manually perform this offset on the sun (or vector) light's
  471. // polygon, which is at the far plane.
  472. Point3F cameraOffsetPos = cameraPos;
  473. // Now build the quad for drawing full-screen vector light
  474. // passes.... this is a volatile VB and updates every frame.
  475. FarFrustumQuadVert verts[4];
  476. {
  477. verts[0].point.set(wsFrustumPoints[Frustum::FarTopLeft] - cameraPos);
  478. invCam.mulP(wsFrustumPoints[Frustum::FarTopLeft], &verts[0].normal);
  479. verts[0].texCoord.set(-1.0, 1.0);
  480. verts[0].tangent.set(wsFrustumPoints[Frustum::FarTopLeft] - cameraOffsetPos);
  481. verts[1].point.set(wsFrustumPoints[Frustum::FarTopRight] - cameraPos);
  482. invCam.mulP(wsFrustumPoints[Frustum::FarTopRight], &verts[1].normal);
  483. verts[1].texCoord.set(1.0, 1.0);
  484. verts[1].tangent.set(wsFrustumPoints[Frustum::FarTopRight] - cameraOffsetPos);
  485. verts[2].point.set(wsFrustumPoints[Frustum::FarBottomLeft] - cameraPos);
  486. invCam.mulP(wsFrustumPoints[Frustum::FarBottomLeft], &verts[2].normal);
  487. verts[2].texCoord.set(-1.0, -1.0);
  488. verts[2].tangent.set(wsFrustumPoints[Frustum::FarBottomLeft] - cameraOffsetPos);
  489. verts[3].point.set(wsFrustumPoints[Frustum::FarBottomRight] - cameraPos);
  490. invCam.mulP(wsFrustumPoints[Frustum::FarBottomRight], &verts[3].normal);
  491. verts[3].texCoord.set(1.0, -1.0);
  492. verts[3].tangent.set(wsFrustumPoints[Frustum::FarBottomRight] - cameraOffsetPos);
  493. }
  494. mFarFrustumQuadVerts.set( GFX, 4 );
  495. dMemcpy( mFarFrustumQuadVerts.lock(), verts, sizeof( verts ) );
  496. mFarFrustumQuadVerts.unlock();
  497. PlaneF farPlane(wsFrustumPoints[Frustum::FarBottomLeft], wsFrustumPoints[Frustum::FarTopLeft], wsFrustumPoints[Frustum::FarTopRight]);
  498. PlaneF vsFarPlane(verts[0].normal, verts[1].normal, verts[2].normal);
  499. // Parameters calculated, assign them to the materials
  500. LightMatTable::Iterator iter = mLightMaterials.begin();
  501. for ( ; iter != mLightMaterials.end(); iter++ )
  502. {
  503. if ( iter->value )
  504. iter->value->setViewParameters( frustum.getNearDist(),
  505. frustum.getFarDist(),
  506. frustum.getPosition(),
  507. farPlane,
  508. vsFarPlane);
  509. }
  510. }
  511. void AdvancedLightBinManager::setupSGData( SceneData &data, const SceneRenderState* state, LightInfo *light )
  512. {
  513. PROFILE_SCOPE( AdvancedLightBinManager_setupSGData );
  514. data.lights[0] = light;
  515. data.ambientLightColor = state->getAmbientLightColor();
  516. data.objTrans = &MatrixF::Identity;
  517. if ( light )
  518. {
  519. if ( light->getType() == LightInfo::Point )
  520. {
  521. // The point light volume gets some flat spots along
  522. // the perimiter mostly visible in the constant and
  523. // quadradic falloff modes.
  524. //
  525. // To account for them slightly increase the scale
  526. // instead of greatly increasing the polycount.
  527. mLightMat = light->getTransform();
  528. mLightMat.scale( light->getRange() * 1.01f );
  529. data.objTrans = &mLightMat;
  530. }
  531. else if ( light->getType() == LightInfo::Spot )
  532. {
  533. mLightMat = light->getTransform();
  534. // Rotate it to face down the -y axis.
  535. MatrixF scaleRotateTranslate( EulerF( M_PI_F / -2.0f, 0.0f, 0.0f ) );
  536. // Calculate the radius based on the range and angle.
  537. F32 range = light->getRange().x;
  538. F32 radius = range * mSin( mDegToRad( light->getOuterConeAngle() ) * 0.5f );
  539. // NOTE: This fudge makes the cone a little bigger
  540. // to remove the facet egde of the cone geometry.
  541. radius *= 1.1f;
  542. // Use the scale to distort the cone to
  543. // match our radius and range.
  544. scaleRotateTranslate.scale( Point3F( radius, radius, range ) );
  545. // Apply the transform and set the position.
  546. mLightMat *= scaleRotateTranslate;
  547. mLightMat.setPosition( light->getPosition() );
  548. data.objTrans = &mLightMat;
  549. }
  550. }
  551. }
  552. void AdvancedLightBinManager::MRTLightmapsDuringDeferred( bool val )
  553. {
  554. // Do not enable if the GFX device can't do MRT's
  555. if ( GFX->getNumRenderTargets() < 2 )
  556. val = false;
  557. if ( mMRTLightmapsDuringDeferred != val )
  558. {
  559. mMRTLightmapsDuringDeferred = val;
  560. // Reload materials to cause a feature recalculation on deferred materials
  561. if(mLightManager->isActive())
  562. MATMGR->flushAndReInitInstances();
  563. RenderDeferredMgr *deferred;
  564. if ( Sim::findObject( "AL_DeferredBin", deferred ) && deferred->getTargetTexture( 0 ) )
  565. deferred->updateTargets();
  566. }
  567. }
  568. AdvancedLightBinManager::LightMaterialInfo::LightMaterialInfo( const String &matName,
  569. const GFXVertexFormat *vertexFormat,
  570. const Vector<GFXShaderMacro> &macros )
  571. : matInstance(NULL),
  572. zNearFarInvNearFar(NULL),
  573. farPlane(NULL),
  574. vsFarPlane(NULL),
  575. negFarPlaneDotEye(NULL),
  576. lightPosition(NULL),
  577. lightDirection(NULL),
  578. lightColor(NULL),
  579. lightRange(NULL),
  580. lightInvSqrRange(NULL),
  581. lightAmbient(NULL),
  582. lightSpotParams(NULL)
  583. {
  584. Material *mat = MATMGR->getMaterialDefinitionByName( matName );
  585. if ( !mat )
  586. return;
  587. matInstance = new LightMatInstance( *mat );
  588. for ( U32 i=0; i < macros.size(); i++ )
  589. matInstance->addShaderMacro( macros[i].name, macros[i].value );
  590. matInstance->init( MATMGR->getDefaultFeatures(), vertexFormat );
  591. lightDirection = matInstance->getMaterialParameterHandle("$lightDirection");
  592. lightAmbient = matInstance->getMaterialParameterHandle("$lightAmbient");
  593. lightSpotParams = matInstance->getMaterialParameterHandle("$lightSpotParams");
  594. lightRange = matInstance->getMaterialParameterHandle("$lightRange");
  595. lightInvSqrRange = matInstance->getMaterialParameterHandle("$lightInvSqrRange");
  596. lightPosition = matInstance->getMaterialParameterHandle("$lightPosition");
  597. farPlane = matInstance->getMaterialParameterHandle("$farPlane");
  598. vsFarPlane = matInstance->getMaterialParameterHandle("$vsFarPlane");
  599. negFarPlaneDotEye = matInstance->getMaterialParameterHandle("$negFarPlaneDotEye");
  600. zNearFarInvNearFar = matInstance->getMaterialParameterHandle("$zNearFarInvNearFar");
  601. lightColor = matInstance->getMaterialParameterHandle("$lightColor");
  602. lightBrightness = matInstance->getMaterialParameterHandle("$lightBrightness");
  603. }
  604. AdvancedLightBinManager::LightMaterialInfo::~LightMaterialInfo()
  605. {
  606. SAFE_DELETE(matInstance);
  607. }
  608. void AdvancedLightBinManager::LightMaterialInfo::setViewParameters( const F32 _zNear,
  609. const F32 _zFar,
  610. const Point3F &_eyePos,
  611. const PlaneF &_farPlane,
  612. const PlaneF &_vsFarPlane)
  613. {
  614. MaterialParameters *matParams = matInstance->getMaterialParameters();
  615. matParams->setSafe( farPlane, *((const Point4F *)&_farPlane) );
  616. matParams->setSafe( vsFarPlane, *((const Point4F *)&_vsFarPlane) );
  617. if ( negFarPlaneDotEye->isValid() )
  618. {
  619. // -dot( farPlane, eyePos )
  620. const F32 negFarPlaneDotEyeVal = -( mDot( *((const Point3F *)&_farPlane), _eyePos ) + _farPlane.d );
  621. matParams->set( negFarPlaneDotEye, negFarPlaneDotEyeVal );
  622. }
  623. matParams->setSafe( zNearFarInvNearFar, Point4F( _zNear, _zFar, 1.0f / _zNear, 1.0f / _zFar ) );
  624. }
  625. void AdvancedLightBinManager::LightMaterialInfo::setLightParameters( const LightInfo *lightInfo, const SceneRenderState* renderState )
  626. {
  627. MaterialParameters *matParams = matInstance->getMaterialParameters();
  628. matParams->setSafe( lightColor, lightInfo->getColor() );
  629. matParams->setSafe(lightBrightness, lightInfo->getBrightness() * lightInfo->getFadeAmount());
  630. switch( lightInfo->getType() )
  631. {
  632. case LightInfo::Vector:
  633. {
  634. matParams->setSafe( lightDirection, lightInfo->getDirection());
  635. matParams->setSafe( lightAmbient, renderState->getAmbientLightColor());
  636. }
  637. break;
  638. case LightInfo::Spot:
  639. {
  640. const F32 outerCone = lightInfo->getOuterConeAngle();
  641. const F32 innerCone = getMin(lightInfo->getInnerConeAngle(), outerCone);
  642. const F32 outerCos = mCos(mDegToRad(outerCone / 2.0f));
  643. const F32 innerCos = mCos(mDegToRad(innerCone / 2.0f));
  644. Point2F spotParams(outerCos,innerCos - outerCos);
  645. matParams->setSafe( lightSpotParams, spotParams );
  646. matParams->setSafe( lightDirection, lightInfo->getDirection());
  647. matParams->setSafe( lightPosition, lightInfo->getPosition());
  648. const F32 radius = lightInfo->getRange().x;
  649. const F32 invSqrRadius = 1.0f / mSquared(radius);
  650. matParams->setSafe(lightRange, radius);
  651. matParams->setSafe(lightInvSqrRange, invSqrRadius);
  652. }
  653. break;
  654. case LightInfo::Point:
  655. {
  656. matParams->setSafe(lightPosition, lightInfo->getPosition());
  657. const F32 radius = lightInfo->getRange().x;
  658. const F32 invSqrRadius = 1.0f / (radius * radius);
  659. matParams->setSafe( lightRange, radius);
  660. matParams->setSafe( lightInvSqrRange, invSqrRadius);
  661. }
  662. break;
  663. default:
  664. AssertFatal( false, "Bad light type!" );
  665. break;
  666. }
  667. }
  668. bool LightMatInstance::setupPass( SceneRenderState *state, const SceneData &sgData )
  669. {
  670. // Go no further if the material failed to initialize properly.
  671. if ( !mProcessedMaterial ||
  672. mProcessedMaterial->getNumPasses() == 0 )
  673. return false;
  674. U32 reflectStatus = Base;
  675. if (state->isReflectPass())
  676. reflectStatus = Reflecting;
  677. // Fetch the lightmap params
  678. const LightMapParams *lmParams = sgData.lights[0]->getExtended<LightMapParams>();
  679. // If no Lightmap params, let parent handle it
  680. if(lmParams == NULL)
  681. return Parent::setupPass(state, sgData);
  682. // Defaults
  683. bool bRetVal = true;
  684. // What render pass is this...
  685. if(mCurPass == -1)
  686. {
  687. // First pass, reset this flag
  688. mInternalPass = false;
  689. // Pass call to parent
  690. bRetVal = Parent::setupPass(state, sgData);
  691. }
  692. else
  693. {
  694. // If this light is represented in a lightmap, it has already done it's
  695. // job for non-lightmapped geometry. Now render the lightmapped geometry
  696. // pass (specular + shadow-darkening)
  697. if(!mInternalPass && lmParams->representedInLightmap)
  698. mInternalPass = true;
  699. else
  700. return Parent::setupPass(state, sgData);
  701. }
  702. // Set up the shader constants we need to...
  703. if(mLightMapParamsSC->isValid())
  704. {
  705. // If this is an internal pass, special case the parameters
  706. if(mInternalPass)
  707. {
  708. AssertFatal( lmParams->shadowDarkenColor.alpha == -1.0f, "Assumption failed, check unpack code!" );
  709. getMaterialParameters()->set( mLightMapParamsSC, lmParams->shadowDarkenColor );
  710. }
  711. else
  712. getMaterialParameters()->set( mLightMapParamsSC, LinearColorF::WHITE );
  713. }
  714. // Now override stateblock with our own
  715. if(!mInternalPass)
  716. {
  717. // If this is not an internal pass, and this light is represented in lightmaps
  718. // than only effect non-lightmapped geometry for this pass
  719. if (lmParams->representedInLightmap)
  720. {
  721. GFX->setStateBlock(mLitState[StaticLightNonLMGeometry][reflectStatus]);
  722. }
  723. else // This is a normal, dynamic light.
  724. {
  725. if (mSpecialLight)
  726. GFX->setStateBlock(mLitState[SunLight][reflectStatus]);
  727. else
  728. GFX->setStateBlock(mLitState[DynamicLight][reflectStatus]);
  729. }
  730. }
  731. else // Internal pass, this is the add-specular/multiply-darken-color pass
  732. GFX->setStateBlock(mLitState[StaticLightLMGeometry][reflectStatus]);
  733. return bRetVal;
  734. }
  735. bool LightMatInstance::init( const FeatureSet &features, const GFXVertexFormat *vertexFormat )
  736. {
  737. bool success = Parent::init(features, vertexFormat);
  738. // If the initialization failed don't continue.
  739. if ( !success || !mProcessedMaterial || mProcessedMaterial->getNumPasses() == 0 )
  740. return false;
  741. mLightMapParamsSC = getMaterialParameterHandle("$lightMapParams");
  742. // Grab the state block for the first render pass (since this mat instance
  743. // inserts a pass after the first pass)
  744. AssertFatal(mProcessedMaterial->getNumPasses() > 0, "No passes created! Ohnoes");
  745. const RenderPassData *rpd = mProcessedMaterial->getPass(0);
  746. AssertFatal(rpd, "No render pass data!");
  747. AssertFatal(rpd->mRenderStates[0], "No render state 0!");
  748. // Get state block desc for normal (not wireframe, not translucent, not glow, etc)
  749. // render state
  750. GFXStateBlockDesc litState = rpd->mRenderStates[0]->getDesc();
  751. // Create state blocks for each of the 3 possible combos in setupPass
  752. //DynamicLight State: This will effect lightmapped and non-lightmapped geometry
  753. // in the same way.
  754. litState.separateAlphaBlendDefined = true;
  755. litState.separateAlphaBlendEnable = false;
  756. litState.stencilMask = RenderDeferredMgr::OpaqueDynamicLitMask | RenderDeferredMgr::OpaqueStaticLitMask;
  757. litState.setCullMode(GFXCullCW);
  758. mLitState[DynamicLight][Base] = GFX->createStateBlock(litState);
  759. litState.setCullMode(GFXCullCCW);
  760. mLitState[DynamicLight][Reflecting] = GFX->createStateBlock(litState);
  761. litState.separateAlphaBlendDefined = true;
  762. litState.separateAlphaBlendEnable = false;
  763. litState.stencilMask = RenderDeferredMgr::OpaqueDynamicLitMask | RenderDeferredMgr::OpaqueStaticLitMask;
  764. litState.setCullMode(GFXCullCCW);
  765. mLitState[SunLight][Base] = GFX->createStateBlock(litState);
  766. litState.setCullMode(GFXCullCCW);
  767. mLitState[SunLight][Reflecting] = GFX->createStateBlock(litState);
  768. // StaticLightNonLMGeometry State: This will treat non-lightmapped geometry
  769. // in the usual way, but will not effect lightmapped geometry.
  770. litState.separateAlphaBlendDefined = true;
  771. litState.separateAlphaBlendEnable = false;
  772. litState.stencilMask = RenderDeferredMgr::OpaqueDynamicLitMask;
  773. litState.setCullMode(GFXCullCW);
  774. mLitState[StaticLightNonLMGeometry][Base] = GFX->createStateBlock(litState);
  775. litState.setCullMode(GFXCullCCW);
  776. mLitState[StaticLightNonLMGeometry][Reflecting] = GFX->createStateBlock(litState);
  777. // StaticLightLMGeometry State: This will add specular information (alpha) but
  778. // multiply-darken color information.
  779. litState.blendDest = GFXBlendSrcColor;
  780. litState.blendSrc = GFXBlendZero;
  781. litState.stencilMask = RenderDeferredMgr::OpaqueStaticLitMask;
  782. litState.separateAlphaBlendDefined = true;
  783. litState.separateAlphaBlendEnable = true;
  784. litState.separateAlphaBlendSrc = GFXBlendOne;
  785. litState.separateAlphaBlendDest = GFXBlendOne;
  786. litState.separateAlphaBlendOp = GFXBlendOpAdd;
  787. litState.setCullMode(GFXCullCW);
  788. mLitState[StaticLightLMGeometry][Base] = GFX->createStateBlock(litState);
  789. litState.setCullMode(GFXCullCCW);
  790. mLitState[StaticLightLMGeometry][Reflecting] = GFX->createStateBlock(litState);
  791. return true;
  792. }