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