renderPassManager.h 13 KB

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