renderPassManager.h 13 KB

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