renderPassManager.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #ifndef _RENDERPASSMANAGER_H_
  23. #define _RENDERPASSMANAGER_H_
  24. #ifndef _GFXDEVICE_H_
  25. #include "gfx/gfxDevice.h"
  26. #endif
  27. #ifndef _SCENEOBJECT_H_
  28. #include "scene/sceneObject.h"
  29. #endif
  30. #ifndef _SIMOBJECT_H_
  31. #include "console/simObject.h"
  32. #endif
  33. #ifndef _DATACHUNKER_H_
  34. #include "core/dataChunker.h"
  35. #endif
  36. #ifndef _SCENEMANAGER_H_
  37. #include "scene/sceneManager.h"
  38. #endif
  39. class SceneRenderState;
  40. class ISceneObject;
  41. class BaseMatInstance;
  42. struct SceneData;
  43. class ShaderData;
  44. class RenderBinManager;
  45. class LightInfo;
  46. struct RenderInst;
  47. class MatrixSet;
  48. class GFXPrimitiveBufferHandle;
  49. /// A RenderInstType hash value.
  50. typedef U32 RenderInstTypeHash;
  51. /// A a tiny wrapper around String that exposes a U32 operator so
  52. /// that we can assign the RIT to RenderInst::type field.
  53. class RenderInstType
  54. {
  55. /// For direct access to mName.
  56. friend class RenderBinManager;
  57. protected:
  58. String mName;
  59. public:
  60. RenderInstType()
  61. : mName( Invalid.mName )
  62. {
  63. }
  64. RenderInstType( const RenderInstType &type )
  65. : mName( type.mName )
  66. {
  67. }
  68. RenderInstType( const String &name )
  69. : mName( name )
  70. {
  71. }
  72. ~RenderInstType() {}
  73. operator RenderInstTypeHash() const { return (RenderInstTypeHash)mName.getHashCaseInsensitive(); }
  74. const String& getName() const { return mName; }
  75. bool isValid() const { return (RenderInstTypeHash)*this != (RenderInstTypeHash)Invalid; }
  76. static const RenderInstType Invalid;
  77. };
  78. ///
  79. class RenderPassManager : public SimObject
  80. {
  81. typedef SimObject Parent;
  82. public:
  83. // Default bin types. Not necessarily the only bin types in the system.
  84. // RIT = "R"ender "I"nstance "T"ype
  85. static const RenderInstType RIT_Mesh;
  86. static const RenderInstType RIT_Shadow;
  87. static const RenderInstType RIT_Sky;
  88. static const RenderInstType RIT_Terrain;
  89. static const RenderInstType RIT_Object; // objects that do their own rendering
  90. static const RenderInstType RIT_ObjectTranslucent;// self rendering; but sorted with static const RenderInstType RIT_Translucent
  91. static const RenderInstType RIT_Decal;
  92. static const RenderInstType RIT_DecalRoad;
  93. static const RenderInstType RIT_Water;
  94. static const RenderInstType RIT_Foliage;
  95. static const RenderInstType RIT_VolumetricFog;
  96. static const RenderInstType RIT_Translucent;
  97. static const RenderInstType RIT_Begin;
  98. static const RenderInstType RIT_Custom;
  99. static const RenderInstType RIT_Particle;
  100. static const RenderInstType RIT_Occluder;
  101. static const RenderInstType RIT_Editor;
  102. public:
  103. RenderPassManager();
  104. virtual ~RenderPassManager();
  105. /// @name Allocation interface
  106. /// @{
  107. /// Allocate a render instance, use like so: MyRenderInstType* t = gRenderInstMgr->allocInst<MyRenderInstType>();
  108. /// Valid until ::clear called.
  109. template <typename T>
  110. T* allocInst()
  111. {
  112. T* inst = mChunker.alloc<T>();
  113. inst->clear();
  114. return inst;
  115. }
  116. /// Allocate a matrix, valid until ::clear called.
  117. MatrixF* allocUniqueXform(const MatrixF& data)
  118. {
  119. MatrixF *r = mChunker.alloc<MatrixF>();
  120. *r = data;
  121. return r;
  122. }
  123. enum SharedTransformType
  124. {
  125. View,
  126. Projection,
  127. };
  128. const MatrixF* allocSharedXform(SharedTransformType stt);
  129. void assignSharedXform(SharedTransformType stt, const MatrixF &xfm);
  130. MatrixSet &getMatrixSet() { return *mMatrixSet; }
  131. /// Allocate a GFXPrimitive object which will remain valid
  132. /// until the pass manager is cleared.
  133. GFXPrimitive* allocPrim() { return mChunker.alloc<GFXPrimitive>(); }
  134. /// @}
  135. /// Add a RenderInstance to the list
  136. virtual void addInst( RenderInst *inst );
  137. /// Sorts the list of RenderInst's per bin. (Normally, one should just call renderPass)
  138. void sort();
  139. /// Renders the list of RenderInsts (Normally, one should just call renderPass)
  140. void render( SceneRenderState *state );
  141. /// Resets our allocated RenderInstances and Matrices. (Normally, one should just call renderPass)
  142. void clear();
  143. // Calls sort, render, and clear
  144. void renderPass( SceneRenderState *state );
  145. /// Returns the active depth buffer for this pass (NOTE: This value may be GFXTextureTarget::sDefaultDepthStencil)
  146. GFXTextureObject *getDepthTargetTexture();
  147. /// Assigns the value for the above method
  148. void setDepthTargetTexture(GFXTextureObject *zTarget);
  149. /// @name RenderBinManager interface
  150. /// @{
  151. /// Add a render bin manager to the list of render bin manager, this SceneRenderPassManager now owns the render bin manager and will free it when needed.
  152. /// @param mgr Render manager to add
  153. /// @param processAddOrder Where to add the manager in the addInst list, set to NO_PROCESSADD to skip processing
  154. /// this is in place for RenderManagers that will bypass the main ::addInst interface and doesn't want to process
  155. /// them.
  156. /// @param renderOrder Where to add the manager in the render list.
  157. void addManager(RenderBinManager* mgr);
  158. /// Removes a manager from render and process add lists
  159. /// @param mgr Render bin manager to remove, the caller is now responsible for freeing the mgr.
  160. void removeManager(RenderBinManager* mgr);
  161. /// How many render bin managers do we have?
  162. U32 getManagerCount() const { return mRenderBins.size(); }
  163. /// Get the render manager at i
  164. RenderBinManager* getManager( S32 i ) const;
  165. /// @}
  166. /// Get scene manager which this render pass belongs to.
  167. SceneManager* getSceneManager()
  168. {
  169. if ( !mSceneManager )
  170. mSceneManager = gClientSceneGraph;
  171. return mSceneManager;
  172. }
  173. /// This signal is triggered when a render bin is about to be rendered.
  174. ///
  175. /// @param bin The render bin we're signaling.
  176. /// @param state The current scene state.
  177. /// @params preRender If true it is before the bin is rendered, else its
  178. /// after being rendered.
  179. ///
  180. typedef Signal <void ( RenderBinManager *bin,
  181. const SceneRenderState *state,
  182. bool preRender )> RenderBinEventSignal;
  183. /// @see RenderBinEventSignal
  184. static RenderBinEventSignal& getRenderBinSignal();
  185. typedef Signal<void(RenderInst *inst)> AddInstSignal;
  186. AddInstSignal& getAddSignal( RenderInstTypeHash type )
  187. {
  188. return mAddInstSignals.findOrInsert( type )->value;
  189. }
  190. // ConsoleObject interface
  191. static void initPersistFields();
  192. DECLARE_CONOBJECT(RenderPassManager);
  193. protected:
  194. MultiTypedChunker mChunker;
  195. Vector< RenderBinManager* > mRenderBins;
  196. typedef HashTable<RenderInstTypeHash,AddInstSignal> AddInstTable;
  197. AddInstTable mAddInstSignals;
  198. SceneManager * mSceneManager;
  199. GFXTexHandle mDepthBuff;
  200. MatrixSet *mMatrixSet;
  201. /// Do a sorted insert into a vector, renderOrder bool controls which test we run for insertion.
  202. void _insertSort(Vector<RenderBinManager*>& list, RenderBinManager* mgr, bool renderOrder);
  203. };
  204. //**************************************************************************
  205. // Render Instance
  206. //**************************************************************************
  207. struct RenderInst
  208. {
  209. /// The type of render instance this is.
  210. RenderInstTypeHash type;
  211. /// This should be true if the object needs to be sorted
  212. /// back to front with other translucent instances.
  213. /// @see sortDistSq
  214. bool translucentSort;
  215. /// The reference squared distance from the camera used for
  216. /// back to front sorting of the instances.
  217. /// @see translucentSort
  218. F32 sortDistSq;
  219. /// The default key used by render managers for
  220. /// internal sorting.
  221. U32 defaultKey;
  222. /// The secondary key used by render managers for
  223. /// internal sorting.
  224. U32 defaultKey2;
  225. /// Does a memset to clear the render instance.
  226. void clear();
  227. };
  228. struct ObjectRenderInst : public RenderInst
  229. {
  230. /// This is a delegate specific index which is usually
  231. /// used to define a mounted object.
  232. S32 objectIndex;
  233. /// Extra data to be used within the render callback.
  234. /// ObjectRenderInst does not own or cleanup this data.
  235. void *userData;
  236. /// The delegate callback function to call to render
  237. /// this object instance.
  238. ///
  239. /// @param ri The ObjectRenderInst that called the delegate.
  240. ///
  241. /// @param state The scene state we're rendering.
  242. ///
  243. /// @param overrideMat An alternative material to use during rendering... usually
  244. /// used for special renders like shadows. If the object doesn't
  245. /// support override materials it shouldn't render at all.
  246. Delegate<void( ObjectRenderInst *ri,
  247. SceneRenderState *state,
  248. BaseMatInstance *overrideMat )> renderDelegate;
  249. // Clear this instance.
  250. void clear();
  251. };
  252. struct MeshRenderInst : public RenderInst
  253. {
  254. ////
  255. GFXVertexBufferHandleBase *vertBuff;
  256. ////
  257. GFXPrimitiveBufferHandle *primBuff;
  258. /// If not NULL it is used to draw the primitive, else
  259. /// the primBuffIndex is used.
  260. /// @see primBuffIndex
  261. GFXPrimitive *prim;
  262. /// If prim is NULL then this index is used to draw the
  263. /// indexed primitive from the primitive buffer.
  264. /// @see prim
  265. U32 primBuffIndex;
  266. /// The material to setup when drawing this instance.
  267. BaseMatInstance *matInst;
  268. /// The object to world transform (world transform in most API's).
  269. const MatrixF *objectToWorld;
  270. /// The worldToCamera (view transform in most API's).
  271. const MatrixF* worldToCamera;
  272. /// The projection matrix.
  273. const MatrixF* projection;
  274. // misc render states
  275. U8 transFlags;
  276. bool reflective;
  277. F32 visibility;
  278. /// A generic hint value passed from the game
  279. /// code down to the material for use by shader
  280. /// features.
  281. void *materialHint;
  282. /// The lights we pass to the material for this
  283. /// mesh in order light importance.
  284. LightInfo* lights[8];
  285. // textures
  286. GFXTextureObject *lightmap;
  287. GFXTextureObject *fogTex;
  288. GFXTextureObject *backBuffTex;
  289. GFXTextureObject *reflectTex;
  290. GFXTextureObject *miscTex;
  291. GFXTextureObject *accuTex;
  292. GFXCubemap *cubemap;
  293. void clear();
  294. };
  295. enum ParticleSystemState
  296. {
  297. PSS_AwaitingHighResDraw = 0, // Keep this as first element so that if the offscreen manager rejects a particle system it will get drawn high-res
  298. PSS_AwaitingOffscreenDraw,
  299. PSS_AwaitingCompositeDraw,
  300. PSS_AwaitingMixedResDraw,
  301. PSS_DrawComplete,
  302. };
  303. /// A special render instance for particles.
  304. struct ParticleRenderInst : public RenderInst
  305. {
  306. /// The vertex buffer.
  307. GFXVertexBufferHandleBase *vertBuff;
  308. /// The primitive buffer.
  309. GFXPrimitiveBufferHandle *primBuff;
  310. /// The total particle count to render.
  311. S32 count;
  312. bool glow;
  313. /// The combined model, camera, and projection transform.
  314. const MatrixF *modelViewProj;
  315. /// Blend style for the particle system
  316. enum BlendStyle {
  317. BlendUndefined = 0,
  318. BlendNormal,
  319. BlendAdditive,
  320. BlendSubtractive,
  321. BlendPremultAlpha,
  322. BlendGreyscale,
  323. BlendStyle_COUNT,
  324. };
  325. U8 blendStyle;
  326. /// For the offscreen particle manager
  327. U8 targetIndex;
  328. /// State for the particle system
  329. ParticleSystemState systemState;
  330. /// The soft particle fade distance in meters.
  331. F32 softnessDistance;
  332. /// Bounding box render transform
  333. const MatrixF *bbModelViewProj;
  334. /// The particle texture.
  335. GFXTextureObject *diffuseTex;
  336. void clear();
  337. };
  338. class GFXOcclusionQuery;
  339. class SceneObject;
  340. /// A special render instance for occlusion tests.
  341. struct OccluderRenderInst : public RenderInst
  342. {
  343. Point3F scale;
  344. Point3F position;
  345. const MatrixF *orientation;
  346. GFXOcclusionQuery *query;
  347. // This optional query will have all pixels rendered.
  348. // Its purpose is to return to the user the full pixel count for comparison
  349. // with the other query.
  350. GFXOcclusionQuery *query2;
  351. /// Render a sphere or a box.
  352. bool isSphere;
  353. void clear();
  354. };
  355. #endif // _RENDERPASSMANAGER_H_