renderParticleMgr.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 "renderInstance/renderParticleMgr.h"
  24. #include "renderInstance/renderPrePassMgr.h"
  25. #include "scene/sceneManager.h"
  26. #include "scene/sceneObject.h"
  27. #include "scene/sceneRenderState.h"
  28. #include "gfx/gfxPrimitiveBuffer.h"
  29. #include "gfx/gfxTransformSaver.h"
  30. #include "gfx/gfxDebugEvent.h"
  31. #include "materials/shaderData.h"
  32. #include "materials/sceneData.h"
  33. #include "materials/matInstance.h"
  34. #include "gfx/util/screenspace.h"
  35. #include "gfx/gfxDrawUtil.h"
  36. #include "collision/clippedPolyList.h"
  37. static const Point4F cubePoints[9] =
  38. {
  39. Point4F(-0.5, -0.5, -0.5, 1.0f), Point4F(-0.5, -0.5, 0.5, 1.0f), Point4F(-0.5, 0.5, -0.5, 1.0f), Point4F(-0.5, 0.5, 0.5, 1.0f),
  40. Point4F( 0.5, -0.5, -0.5, 1.0f), Point4F( 0.5, -0.5, 0.5, 1.0f), Point4F( 0.5, 0.5, -0.5, 1.0f), Point4F( 0.5, 0.5, 0.5, 1.0f)
  41. };
  42. GFXImplementVertexFormat( CompositeQuadVert )
  43. {
  44. addElement( "COLOR", GFXDeclType_Color );
  45. }
  46. IMPLEMENT_CONOBJECT(RenderParticleMgr);
  47. ConsoleDocClass( RenderParticleMgr,
  48. "@brief A render bin which renders particle geometry.\n\n"
  49. "This render bin gathers particle render instances, sorts, and renders them. "
  50. "It is currently used by ParticleEmitter and LightFlareData.\n\n"
  51. "@ingroup RenderBin\n" );
  52. const RenderInstType RenderParticleMgr::RIT_Particles("ParticleSystem");
  53. // TODO: Replace these once they are supported via options
  54. const bool RenderToParticleTarget = true;
  55. const bool RenderToSingleTarget = true;
  56. RenderParticleMgr::RenderParticleMgr()
  57. : Parent( RenderParticleMgr::RIT_Particles,
  58. 1.0f,
  59. 1.0f,
  60. GFXFormatR8G8B8A8,
  61. Point2I( Parent::DefaultTargetSize, Parent::DefaultTargetSize),
  62. RenderToParticleTarget ? Parent::DefaultTargetChainLength : 0 ),
  63. mParticleShader( NULL )
  64. {
  65. // Render particles at 1/4 resolution
  66. mTargetSizeType = WindowSizeScaled;
  67. mTargetScale.set(0.25f, 0.25f);
  68. // We use the target chain like a texture pool, not like a swap chain
  69. if(!RenderToSingleTarget)
  70. setTargetChainLength(5);
  71. else
  72. mOffscreenSystems.setSize(1);
  73. LightManager::smActivateSignal.notify( this, &RenderParticleMgr::_onLMActivate );
  74. }
  75. RenderParticleMgr::~RenderParticleMgr()
  76. {
  77. LightManager::smActivateSignal.remove( this, &RenderParticleMgr::_onLMActivate );
  78. }
  79. void RenderParticleMgr::setTargetChainLength( const U32 chainLength )
  80. {
  81. Parent::setTargetChainLength(chainLength);
  82. if(!RenderToSingleTarget)
  83. mOffscreenSystems.setSize(chainLength);
  84. }
  85. void RenderParticleMgr::addElement( RenderInst *inst )
  86. {
  87. ParticleRenderInst *pri = reinterpret_cast<ParticleRenderInst *>(inst);
  88. // If this system isn't waiting for an offscreen draw, skip it
  89. if( pri->systemState != PSS_AwaitingOffscreenDraw )
  90. return;
  91. // If offscreen rendering isn't enabled, set to high-res, and skip
  92. if(!mOffscreenRenderEnabled)
  93. {
  94. pri->systemState = PSS_AwaitingHighResDraw;
  95. return;
  96. }
  97. // Assign a target index
  98. RectF screenRect;
  99. S32 chainIndex = -1;
  100. if(RenderToSingleTarget)
  101. {
  102. pri->targetIndex = 0;
  103. screenRect.point.set(-1.0f, -1.0f);
  104. screenRect.extent.set(2.0f, 2.0f);
  105. mElementList.setSize(1);
  106. }
  107. else
  108. {
  109. // If we can't fit this into the offscreen systems, skip it, it will render
  110. // high resolution
  111. //
  112. // TODO: Improve this once we are grouping systems
  113. if( mTargetChainIdx == OffscreenPoolSize )
  114. return;
  115. // Transform bounding box into screen space
  116. const static PlaneF planes[] = {
  117. PlaneF(Point3F( 1.0f, 0.0f, 0.0f), Point3F(-1.0f, 0.0f, 0.0f)),
  118. PlaneF(Point3F(-1.0f, 0.0f, 0.0f), Point3F( 1.0f, 0.0f, 0.0f)),
  119. PlaneF(Point3F( 0.0f, 1.0f, 0.0f), Point3F( 0.0f, -1.0f, 0.0f)),
  120. PlaneF(Point3F( 0.0f, -1.0f, 0.0f), Point3F( 0.0f, 1.0f, 0.0f)),
  121. PlaneF(Point3F( 0.0f, 0.0f, 0.0f), Point3F( 0.0f, 0.0f, 1.0f)),
  122. PlaneF(Point3F( 0.0f, 0.0f, 1.0f), Point3F( 0.0f, 0.0f, -1.0f)),
  123. };
  124. const static dsize_t numPlanes = sizeof(planes) / sizeof(PlaneF);
  125. // Set up a clipper
  126. ClippedPolyList screenClipper;
  127. screenClipper.setBaseTransform(MatrixF::Identity);
  128. screenClipper.setTransform(&MatrixF::Identity, Point3F::One);
  129. TORQUE_UNUSED(numPlanes);
  130. Point4F tempPt(0.0f, 0.0f, 0.0f, 1.0f);
  131. pri->bbModelViewProj->mul(tempPt);
  132. tempPt = tempPt / tempPt.w;
  133. for(S32 i = 0; i < 1; i++)
  134. {
  135. screenClipper.mPlaneList.push_back(planes[i]);
  136. screenClipper.mPlaneList.last() += tempPt.asPoint3F();
  137. }
  138. Box3F screenSpaceBoundingBox;
  139. screenSpaceBoundingBox.minExtents = Point3F::Zero;
  140. screenSpaceBoundingBox.maxExtents = Point3F::Zero;
  141. for(S32 i = 0; i < 8; i++)
  142. {
  143. tempPt = cubePoints[i];
  144. pri->bbModelViewProj->mul(tempPt);
  145. tempPt = tempPt / tempPt.w;
  146. screenSpaceBoundingBox.maxExtents.setMax(tempPt.asPoint3F());
  147. screenSpaceBoundingBox.minExtents.setMin(tempPt.asPoint3F());
  148. }
  149. screenClipper.addBox(screenSpaceBoundingBox);
  150. screenClipper.cullUnusedVerts();
  151. //screenClipper.triangulate();
  152. // Empty vertex list? Skip!
  153. if(screenClipper.mVertexList.empty())
  154. {
  155. pri->systemState = PSS_AwaitingHighResDraw;
  156. return;
  157. }
  158. Point2F minExtents(0.0f, 0.0f), maxExtents(0.0f, 0.0f);
  159. for(ClippedPolyList::VertexList::const_iterator itr = screenClipper.mVertexList.begin();
  160. itr != screenClipper.mVertexList.end(); itr++)
  161. {
  162. minExtents.x = getMin(minExtents.x, (*itr).point.x);
  163. minExtents.y = getMin(minExtents.y, (*itr).point.y);
  164. maxExtents.x = getMax(maxExtents.x, (*itr).point.x);
  165. maxExtents.y = getMax(maxExtents.y, (*itr).point.y);
  166. }
  167. screenRect.set( minExtents, maxExtents - minExtents );
  168. // Check the size of the system on screen. If it is small, it won't
  169. // be eating fillrate anyway, so just draw it high-resolution.
  170. // The value it checks against is one I found from experimentation,
  171. // not anything really meaningful.
  172. if( screenRect.extent.x < 0.35f || screenRect.extent.y < 0.35f )
  173. {
  174. pri->systemState = PSS_AwaitingHighResDraw;
  175. return;
  176. }
  177. pri->targetIndex = mTargetChainIdx;
  178. chainIndex = mTargetChainIdx;
  179. mTargetChainIdx++;
  180. // TODO: Rewrite this...
  181. mElementList.increment();
  182. }
  183. // Set up system entry
  184. OffscreenSystemEntry &systemEntry = mOffscreenSystems[pri->targetIndex];
  185. systemEntry.screenRect = screenRect;
  186. systemEntry.targetChainIdx = chainIndex;
  187. systemEntry.pInstances.push_back(pri);
  188. // TODO: Rewrite this block
  189. // Assign proper values to sort element
  190. MainSortElem& elem = mElementList.last();
  191. elem.inst = reinterpret_cast<RenderInst *>(&systemEntry);
  192. elem.key = *((U32*)&inst->sortDistSq);
  193. elem.key2 = inst->defaultKey;
  194. // TODO: [re]move this block
  195. systemEntry.clipMatrix.identity();
  196. if(!RenderToSingleTarget)
  197. {
  198. // Construct crop matrix
  199. Point3F scale( getMax(2.0f / systemEntry.screenRect.extent.x, 1.0f),
  200. getMax(2.0f / systemEntry.screenRect.extent.y, 1.0f),
  201. 1.0f);
  202. Point3F offset((systemEntry.screenRect.point.x + systemEntry.screenRect.extent.x * 0.5f) * scale.x,
  203. (systemEntry.screenRect.point.y + systemEntry.screenRect.extent.y * 0.5f) * scale.y,
  204. 0.0f);
  205. //systemEntry.clipMatrix.scale(scale);
  206. //systemEntry.clipMatrix.setPosition(-offset);
  207. }
  208. // The translucent mgr will also pick up particles, and will call this class
  209. // to composiste the particle systems back into the main scene
  210. }
  211. void RenderParticleMgr::sort()
  212. {
  213. Parent::sort();
  214. }
  215. void RenderParticleMgr::clear()
  216. {
  217. Parent::clear();
  218. // Reset pool index
  219. if(!RenderToSingleTarget)
  220. mTargetChainIdx = 0;
  221. for(Vector<OffscreenSystemEntry>::iterator itr = mOffscreenSystems.begin();
  222. itr != mOffscreenSystems.end(); itr++)
  223. {
  224. (*itr).drawnThisFrame = false;
  225. (*itr).pInstances.clear();
  226. }
  227. }
  228. void RenderParticleMgr::render( SceneRenderState *state )
  229. {
  230. PROFILE_SCOPE(RenderParticleMgr_render);
  231. // Early out if nothing to draw
  232. if( !mElementList.size() ||
  233. (!mParticleShader && !_initShader()) )
  234. return;
  235. GFXDEBUGEVENT_SCOPE(RenderParticleMgr_Render, ColorI::RED);
  236. GFXTransformSaver saver;
  237. // Iterate render instances
  238. for( Vector<MainSortElem>::const_iterator itr = mElementList.begin();
  239. itr != mElementList.end(); itr++ )
  240. {
  241. OffscreenSystemEntry &systemEntry = *reinterpret_cast<OffscreenSystemEntry *>(itr->inst);
  242. // Setup target
  243. // NOTE: If you are using this on the Xbox360 with Basic Lighting,
  244. // you are going to have to mess with either the render order, or
  245. // you are going to have to make this a 'preserve' draw
  246. if(!RenderToSingleTarget)
  247. mTargetChainIdx = systemEntry.targetChainIdx;
  248. _onPreRender(state);
  249. // Clear offscreen target
  250. GFX->clear(GFXClearTarget, ColorI::ZERO, 1.0f, 0);
  251. // Draw offscreen systems
  252. for( Vector<ParticleRenderInst *>::const_iterator itr2 = systemEntry.pInstances.begin();
  253. itr2 != systemEntry.pInstances.end(); itr2++ )
  254. {
  255. ParticleRenderInst *ri = *itr2;
  256. // Sanity check
  257. if(ri->systemState == PSS_AwaitingOffscreenDraw)
  258. {
  259. // If this is not a diffuse path, flag the system appropriately, and skip
  260. // the offscreen processing.
  261. if( !state->isDiffusePass() )
  262. {
  263. if(state->isReflectPass())
  264. ri->systemState = PSS_AwaitingHighResDraw;
  265. else
  266. ri->systemState = PSS_DrawComplete;
  267. continue;
  268. }
  269. // Draw system offscreen
  270. renderInstance(ri, state);
  271. }
  272. }
  273. // Cleanup
  274. _onPostRender();
  275. }
  276. }
  277. void RenderParticleMgr::_initGFXResources()
  278. {
  279. // Screen quad
  280. U16 *prims = NULL;
  281. mScreenQuadPrimBuff.set(GFX, 4, 2, GFXBufferTypeStatic);
  282. mScreenQuadPrimBuff.lock(&prims);
  283. (*prims++) = 0;
  284. (*prims++) = 1;
  285. (*prims++) = 2;
  286. (*prims++) = 3;
  287. mScreenQuadPrimBuff.unlock();
  288. mScreenQuadVertBuff.set(GFX, 4, GFXBufferTypeStatic);
  289. CompositeQuadVert *verts = mScreenQuadVertBuff.lock();
  290. (*verts++).uvCoord.set(0, 0, 0, 0);
  291. (*verts++).uvCoord.set(0, 255, 0, 0);
  292. (*verts++).uvCoord.set(255, 0, 0, 0);
  293. (*verts++).uvCoord.set(255, 255, 0, 0);
  294. mScreenQuadVertBuff.unlock();
  295. // Stencil setup state block
  296. GFXStateBlockDesc d;
  297. d.setCullMode(GFXCullNone);
  298. d.setColorWrites(false, false, false, false);
  299. d.setBlend(false);
  300. d.setZReadWrite(false, false);
  301. d.stencilDefined = true;
  302. d.stencilEnable = true;
  303. d.stencilMask = RenderParticleMgr::ParticleSystemStencilMask;
  304. d.stencilWriteMask = RenderParticleMgr::ParticleSystemStencilMask;
  305. d.stencilFunc = GFXCmpAlways;
  306. d.stencilPassOp = GFXStencilOpZero;
  307. mStencilClearSB = GFX->createStateBlock(d);
  308. }
  309. void RenderParticleMgr::renderInstance(ParticleRenderInst *ri, SceneRenderState *state)
  310. {
  311. // Draw system path, or draw composite path
  312. if(ri->systemState == PSS_DrawComplete)
  313. return;
  314. if(ri->systemState != PSS_AwaitingCompositeDraw)
  315. {
  316. // Set proper stateblock, and update state
  317. if(ri->systemState == PSS_AwaitingOffscreenDraw)
  318. {
  319. GFX->setStateBlock( _getOffscreenStateBlock(ri) );
  320. ri->systemState = PSS_AwaitingCompositeDraw;
  321. mParticleShaderConsts.mShaderConsts->setSafe( mParticleShaderConsts.mModelViewProjSC,
  322. *ri->modelViewProj * mOffscreenSystems[ri->targetIndex].clipMatrix );
  323. }
  324. else
  325. {
  326. if(ri->systemState == PSS_AwaitingMixedResDraw)
  327. GFX->setStateBlock( _getMixedResStateBlock( ri ) );
  328. else
  329. GFX->setStateBlock( _getHighResStateBlock( ri ) );
  330. ri->systemState = PSS_DrawComplete;
  331. mParticleShaderConsts.mShaderConsts->setSafe( mParticleShaderConsts.mModelViewProjSC, *ri->modelViewProj );
  332. }
  333. // We want to turn everything into variation on a pre-multiplied alpha blend
  334. F32 alphaFactor = 0.0f, alphaScale = 1.0f;
  335. switch(ri->blendStyle)
  336. {
  337. // SrcAlpha, InvSrcAlpha
  338. case ParticleRenderInst::BlendNormal:
  339. alphaFactor = 1.0f;
  340. break;
  341. // SrcAlpha, One
  342. case ParticleRenderInst::BlendAdditive:
  343. alphaFactor = 1.0f;
  344. alphaScale = 0.0f;
  345. break;
  346. // SrcColor, One
  347. case ParticleRenderInst::BlendGreyscale:
  348. alphaFactor = -1.0f;
  349. alphaScale = 0.0f;
  350. break;
  351. }
  352. mParticleShaderConsts.mShaderConsts->setSafe( mParticleShaderConsts.mAlphaFactorSC, alphaFactor );
  353. mParticleShaderConsts.mShaderConsts->setSafe( mParticleShaderConsts.mAlphaScaleSC, alphaScale );
  354. mParticleShaderConsts.mShaderConsts->setSafe( mParticleShaderConsts.mFSModelViewProjSC, *ri->modelViewProj );
  355. mParticleShaderConsts.mShaderConsts->setSafe( mParticleShaderConsts.mOneOverFarSC, 1.0f / state->getFarPlane() );
  356. if ( mParticleShaderConsts.mOneOverSoftnessSC->isValid() )
  357. {
  358. F32 oneOverSoftness = 1.0f;
  359. if ( ri->softnessDistance > 0.0f )
  360. oneOverSoftness = 1.0f / ( ri->softnessDistance / state->getFarPlane() );
  361. mParticleShaderConsts.mShaderConsts->set( mParticleShaderConsts.mOneOverSoftnessSC, oneOverSoftness );
  362. }
  363. GFX->setShader( mParticleShader );
  364. GFX->setShaderConstBuffer( mParticleShaderConsts.mShaderConsts );
  365. GFX->setTexture( 0, ri->diffuseTex );
  366. // Set up the prepass texture.
  367. if ( mParticleShaderConsts.mPrePassTargetParamsSC->isValid() )
  368. {
  369. GFXTextureObject *texObject = mPrepassTarget ? mPrepassTarget->getTexture(0) : NULL;
  370. GFX->setTexture( 1, texObject );
  371. Point4F rtParams( 0.0f, 0.0f, 1.0f, 1.0f );
  372. if ( texObject )
  373. ScreenSpace::RenderTargetParameters(texObject->getSize(), mPrepassTarget->getViewport(), rtParams);
  374. mParticleShaderConsts.mShaderConsts->set( mParticleShaderConsts.mPrePassTargetParamsSC, rtParams );
  375. }
  376. GFX->setPrimitiveBuffer( *ri->primBuff );
  377. GFX->setVertexBuffer( *ri->vertBuff );
  378. GFX->drawIndexedPrimitive( GFXTriangleList, 0, 0, ri->count * 4, 0, ri->count * 2 );
  379. }
  380. else if(ri->systemState == PSS_AwaitingCompositeDraw)
  381. {
  382. OffscreenSystemEntry &systemEntry = mOffscreenSystems[ri->targetIndex];
  383. // If this system has already been composited this frame, skip it
  384. if(systemEntry.drawnThisFrame)
  385. return;
  386. // Non-target render, composite the particle system back into the scene
  387. GFX->setVertexBuffer(mScreenQuadVertBuff);
  388. GFX->setPrimitiveBuffer(mScreenQuadPrimBuff);
  389. // Set up shader constants
  390. mParticleCompositeShaderConsts.mShaderConsts->setSafe( mParticleCompositeShaderConsts.mScreenRect, *((Point4F *)&systemEntry.screenRect) );
  391. // Set offscreen texture
  392. Point4F rtParams;
  393. GFXTextureObject *particleSource = mNamedTarget.getTexture();
  394. GFX->setTexture( 0, particleSource );
  395. if(particleSource)
  396. {
  397. ScreenSpace::RenderTargetParameters(particleSource->getSize(), mNamedTarget.getViewport(), rtParams);
  398. mParticleCompositeShaderConsts.mShaderConsts->setSafe( mParticleCompositeShaderConsts.mOffscreenTargetParamsSC, rtParams );
  399. }
  400. // And edges
  401. GFXTextureObject *texObject = mEdgeTarget ? mEdgeTarget->getTexture() : NULL;
  402. GFX->setTexture( 1, texObject );
  403. if(texObject)
  404. {
  405. ScreenSpace::RenderTargetParameters(texObject->getSize(), mEdgeTarget->getViewport(), rtParams);
  406. mParticleCompositeShaderConsts.mShaderConsts->setSafe( mParticleCompositeShaderConsts.mEdgeTargetParamsSC, rtParams );
  407. }
  408. // Set shader and constant buffer
  409. GFX->setShader( mParticleCompositeShader );
  410. GFX->setShaderConstBuffer( mParticleCompositeShaderConsts.mShaderConsts );
  411. // Draw to stencil buffer only to clear the stencil values
  412. GFX->setStateBlock( mStencilClearSB );
  413. GFX->drawIndexedPrimitive( GFXTriangleStrip, 0, 0, 4, 0, 2 );
  414. // composite particle system back into the scene
  415. GFX->setStateBlock( _getCompositeStateBlock(ri) );
  416. GFX->drawIndexedPrimitive( GFXTriangleStrip, 0, 0, 4, 0, 2 );
  417. // Re-draw the particle systems in high-res, but only to the stenciled
  418. // areas which were enabled via the edge buffer
  419. for( Vector<ParticleRenderInst *>::const_iterator itr = systemEntry.pInstances.begin();
  420. itr != systemEntry.pInstances.end(); itr++ )
  421. {
  422. ParticleRenderInst *pri = *itr;
  423. if(pri->systemState == PSS_AwaitingCompositeDraw)
  424. {
  425. pri->systemState = PSS_AwaitingMixedResDraw;
  426. renderInstance(pri, state);
  427. }
  428. }
  429. // Mark this system as having been composited this frame
  430. systemEntry.drawnThisFrame = true;
  431. }
  432. }
  433. bool RenderParticleMgr::_initShader()
  434. {
  435. ShaderData *shaderData = NULL;
  436. bool ret = true;
  437. // Need depth from pre-pass, so get the macros
  438. Vector<GFXShaderMacro> macros;
  439. if ( mPrepassTarget )
  440. mPrepassTarget->getShaderMacros( &macros );
  441. // Create particle shader
  442. if ( !Sim::findObject( "ParticlesShaderData", shaderData ) || !shaderData )
  443. Con::warnf( "RenderParticleMgr::_initShader - failed to locate shader ParticlesShaderData!" );
  444. if( shaderData )
  445. mParticleShader = shaderData->getShader( macros );
  446. ret &= (mParticleShader != NULL);
  447. if ( mParticleShader )
  448. {
  449. mParticleShaderConsts.mShaderConsts = mParticleShader->allocConstBuffer();
  450. mParticleShaderConsts.mModelViewProjSC = mParticleShader->getShaderConstHandle( "$modelViewProj" );
  451. mParticleShaderConsts.mOneOverFarSC = mParticleShader->getShaderConstHandle( "$oneOverFar" );
  452. mParticleShaderConsts.mOneOverSoftnessSC = mParticleShader->getShaderConstHandle( "$oneOverSoftness" );
  453. mParticleShaderConsts.mAlphaFactorSC = mParticleShader->getShaderConstHandle( "$alphaFactor" );
  454. mParticleShaderConsts.mAlphaScaleSC = mParticleShader->getShaderConstHandle( "$alphaScale" );
  455. mParticleShaderConsts.mFSModelViewProjSC = mParticleShader->getShaderConstHandle( "$fsModelViewProj" );
  456. mParticleShaderConsts.mPrePassTargetParamsSC = mParticleShader->getShaderConstHandle( "$prePassTargetParams" );
  457. }
  458. shaderData = NULL;
  459. // Create off screen particle composite shader
  460. if ( !Sim::findObject( "OffscreenParticleCompositeShaderData", shaderData ) || !shaderData )
  461. Con::warnf( "RenderParticleMgr::_initShader - failed to locate shader OffscreenParticleCompositeShaderData!" );
  462. if( shaderData )
  463. mParticleCompositeShader = shaderData->getShader( macros );
  464. ret &= (mParticleCompositeShader != NULL);
  465. if ( mParticleCompositeShader )
  466. {
  467. mParticleCompositeShaderConsts.mShaderConsts = mParticleCompositeShader->allocConstBuffer();
  468. mParticleCompositeShaderConsts.mScreenRect = mParticleCompositeShader->getShaderConstHandle( "$screenRect" );
  469. mParticleCompositeShaderConsts.mEdgeTargetParamsSC = mParticleCompositeShader->getShaderConstHandle( "$edgeTargetParams" );
  470. mParticleCompositeShaderConsts.mOffscreenTargetParamsSC = mParticleCompositeShader->getShaderConstHandle( "$offscreenTargetParams" );
  471. }
  472. return ret;
  473. }
  474. void RenderParticleMgr::_onLMActivate( const char*, bool activate )
  475. {
  476. RenderPassManager *rpm = getRenderPass();
  477. if ( !rpm )
  478. return;
  479. // Hunt for the pre-pass manager/target
  480. RenderPrePassMgr *prePassBin = NULL;
  481. for( U32 i = 0; i < rpm->getManagerCount(); i++ )
  482. {
  483. RenderBinManager *bin = rpm->getManager(i);
  484. if( bin->getRenderInstType() == RenderPrePassMgr::RIT_PrePass )
  485. {
  486. prePassBin = (RenderPrePassMgr*)bin;
  487. break;
  488. }
  489. }
  490. // If we found the prepass bin, set this bin to render very shortly afterwards
  491. // and re-add this render-manager. If there is no pre-pass bin, or it doesn't
  492. // have a depth-texture, we can't render offscreen.
  493. mOffscreenRenderEnabled = prePassBin && (prePassBin->getTargetChainLength() > 0);
  494. if(mOffscreenRenderEnabled)
  495. {
  496. rpm->removeManager(this);
  497. setRenderOrder( prePassBin->getRenderOrder() + 0.011f );
  498. rpm->addManager(this);
  499. }
  500. // Find the targets we use
  501. mPrepassTarget = NamedTexTarget::find( "prepass" );
  502. mEdgeTarget = NamedTexTarget::find( "edge" );
  503. // Setup the shader
  504. if ( activate )
  505. _initShader();
  506. if ( mScreenQuadVertBuff.isNull() )
  507. _initGFXResources();
  508. }
  509. GFXStateBlockRef RenderParticleMgr::_getOffscreenStateBlock(ParticleRenderInst *ri)
  510. {
  511. const U8 blendStyle = ri->blendStyle;
  512. if ( mOffscreenBlocks[blendStyle].isValid() )
  513. return mOffscreenBlocks[blendStyle];
  514. GFXStateBlockDesc d;
  515. d.setCullMode(GFXCullNone);
  516. d.setZReadWrite(false, false); // No zreads or writes, all z-testing is done in the pixel shader
  517. // Draw everything either subtractive, or using a variation on premultiplied
  518. // alpha
  519. if(blendStyle == ParticleRenderInst::BlendSubtractive)
  520. d.setBlend(true, GFXBlendZero, GFXBlendInvSrcColor);
  521. else
  522. d.setBlend(true, GFXBlendOne, GFXBlendInvSrcAlpha);
  523. // Offscreen target, we need to add alpha.
  524. d.separateAlphaBlendDefined = true;
  525. d.separateAlphaBlendEnable = true;
  526. d.separateAlphaBlendSrc = GFXBlendOne;
  527. d.separateAlphaBlendDest = GFXBlendInvSrcAlpha;
  528. d.samplersDefined = true;
  529. // Diffuse texture sampler
  530. d.samplers[0] = GFXSamplerStateDesc::getClampLinear();
  531. d.samplers[0].alphaOp = GFXTOPModulate;
  532. d.samplers[0].alphaArg1 = GFXTATexture;
  533. d.samplers[0].alphaArg2 = GFXTADiffuse;
  534. // Prepass sampler
  535. d.samplers[1] = GFXSamplerStateDesc::getClampPoint();
  536. mOffscreenBlocks[blendStyle] = GFX->createStateBlock(d);
  537. return mOffscreenBlocks[blendStyle];
  538. }
  539. GFXStateBlockRef RenderParticleMgr::_getHighResStateBlock(ParticleRenderInst *ri)
  540. {
  541. const U8 blendStyle = ri->blendStyle;
  542. if ( mHighResBlocks[blendStyle].isValid() )
  543. return mHighResBlocks[blendStyle];
  544. GFXStateBlockDesc d;
  545. d.setZReadWrite(true, false);
  546. d.setCullMode(GFXCullNone);
  547. // Draw everything either subtractive, or using a variation on premultiplied
  548. // alpha
  549. if(blendStyle == ParticleRenderInst::BlendSubtractive)
  550. d.setBlend(true, GFXBlendZero, GFXBlendInvSrcColor);
  551. else
  552. d.setBlend(true, GFXBlendOne, GFXBlendInvSrcAlpha);
  553. d.samplersDefined = true;
  554. // Diffuse texture sampler
  555. d.samplers[0] = GFXSamplerStateDesc::getClampLinear();
  556. d.samplers[0].alphaOp = GFXTOPModulate;
  557. d.samplers[0].alphaArg1 = GFXTATexture;
  558. d.samplers[0].alphaArg2 = GFXTADiffuse;
  559. // Prepass sampler
  560. d.samplers[1] = GFXSamplerStateDesc::getClampPoint();
  561. mHighResBlocks[blendStyle] = GFX->createStateBlock(d);
  562. return mHighResBlocks[blendStyle];
  563. }
  564. GFXStateBlockRef RenderParticleMgr::_getMixedResStateBlock(ParticleRenderInst *ri)
  565. {
  566. const U8 blendStyle = ri->blendStyle;
  567. if ( mHighResBlocks[blendStyle].isValid() )
  568. return mHighResBlocks[blendStyle];
  569. GFXStateBlockDesc d;
  570. d.setZReadWrite(true, false);
  571. d.setCullMode(GFXCullNone);
  572. /*
  573. // Old blend styles...
  574. switch (blendStyle)
  575. {
  576. case ParticleRenderInst::BlendNormal:
  577. d.blendSrc = GFXBlendSrcAlpha;
  578. d.blendDest = GFXBlendInvSrcAlpha;
  579. break;
  580. case ParticleRenderInst::BlendSubtractive:
  581. d.blendSrc = GFXBlendZero;
  582. d.blendDest = GFXBlendInvSrcColor;
  583. break;
  584. case ParticleRenderInst::BlendPremultAlpha:
  585. d.blendSrc = GFXBlendOne;
  586. d.blendDest = GFXBlendInvSrcAlpha;
  587. break;
  588. // Default to additive blend mode
  589. case ParticleRenderInst::BlendAdditive:
  590. case ParticleRenderInst::BlendUndefined:
  591. default:
  592. d.blendSrc = GFXBlendSrcAlpha;
  593. d.blendDest = GFXBlendOne;
  594. break;
  595. }
  596. */
  597. // Draw everything either subtractive, or using a variation on premultiplied
  598. // alpha
  599. if(blendStyle == ParticleRenderInst::BlendSubtractive)
  600. d.setBlend(true, GFXBlendZero, GFXBlendInvSrcColor);
  601. else
  602. d.setBlend(true, GFXBlendOne, GFXBlendInvSrcAlpha);
  603. // Draw to anything but the stencil ref value (the edges)
  604. d.stencilDefined = true;
  605. d.stencilEnable = true;
  606. d.stencilRef = RenderParticleMgr::HighResStencilRef;
  607. d.stencilMask = RenderParticleMgr::ParticleSystemStencilMask;
  608. d.stencilPassOp = GFXStencilOpKeep;
  609. d.stencilFailOp = GFXStencilOpKeep;
  610. d.stencilZFailOp = GFXStencilOpKeep;
  611. d.stencilFunc = GFXCmpNotEqual;
  612. d.samplersDefined = true;
  613. // Diffuse texture sampler
  614. d.samplers[0] = GFXSamplerStateDesc::getClampLinear();
  615. d.samplers[0].alphaOp = GFXTOPModulate;
  616. d.samplers[0].alphaArg1 = GFXTATexture;
  617. d.samplers[0].alphaArg2 = GFXTADiffuse;
  618. // Prepass sampler
  619. d.samplers[1] = GFXSamplerStateDesc::getClampPoint();
  620. mHighResBlocks[blendStyle] = GFX->createStateBlock(d);
  621. return mHighResBlocks[blendStyle];
  622. }
  623. GFXStateBlockRef RenderParticleMgr::_getCompositeStateBlock(ParticleRenderInst *ri)
  624. {
  625. const U8 blendStyle = ri->blendStyle;
  626. if ( mBackbufferBlocks[blendStyle].isValid() )
  627. return mBackbufferBlocks[blendStyle];
  628. GFXStateBlockDesc d;
  629. // This is a billboard
  630. d.setCullMode(GFXCullNone);
  631. d.setZReadWrite(false, false);
  632. // When we re-composite the particles, it is always either a pre-mult alpha
  633. // blend, or a subtractive blend!
  634. if(blendStyle == ParticleRenderInst::BlendSubtractive)
  635. d.setBlend(true, GFXBlendZero, GFXBlendInvSrcColor);
  636. else
  637. d.setBlend(true, GFXBlendOne, GFXBlendInvSrcAlpha);
  638. // All areas which are not along the edges of geometry where the particle system
  639. // is being drawn get assigned a stencil ref value as the system is composited
  640. // back into the scene. The high-res stateblock uses this value as a mask, and
  641. // draws only in areas which are NOT this ref value. This causes high resolution
  642. // draws to ONLY the edge areas.
  643. d.stencilDefined = true;
  644. d.stencilEnable = true;
  645. d.stencilRef = RenderParticleMgr::HighResStencilRef;
  646. d.stencilWriteMask = RenderParticleMgr::ParticleSystemStencilMask;
  647. d.stencilMask = RenderParticleMgr::ParticleSystemStencilMask;
  648. d.stencilPassOp = GFXStencilOpReplace;
  649. d.stencilFunc = GFXCmpGreater;
  650. // Diffuse texture sampler and
  651. d.samplersDefined = true;
  652. d.samplers[0] = GFXSamplerStateDesc::getClampLinear();
  653. d.samplers[1] = GFXSamplerStateDesc::getClampLinear();
  654. mBackbufferBlocks[blendStyle] = GFX->createStateBlock(d);
  655. return mBackbufferBlocks[blendStyle];
  656. }
  657. bool RenderParticleMgr::_handleGFXEvent( GFXDevice::GFXDeviceEventType event )
  658. {
  659. if(RenderToSingleTarget)
  660. return Parent::_handleGFXEvent( event );
  661. // Do nothing. This render manager uses its target chain as a pool of targets.
  662. return true;
  663. }