renderPassManager.h 14 KB

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