renderPassManager.h 13 KB

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