gfxDevice.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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 _GFXDEVICE_H_
  23. #define _GFXDEVICE_H_
  24. #ifndef _GFXADAPTER_H_
  25. #include "gfx/gfxAdapter.h"
  26. #endif
  27. #ifndef _GFXTARGET_H_
  28. #include "gfx/gfxTarget.h"
  29. #endif
  30. #ifndef _GFXVERTEXBUFFER_H_
  31. #include "gfx/gfxVertexBuffer.h"
  32. #endif
  33. #ifndef _GFXSTATEBLOCK_H_
  34. #include "gfx/gfxStateBlock.h"
  35. #endif
  36. #ifndef _GFXSHADER_H_
  37. #include "gfx/gfxShader.h"
  38. #endif
  39. #ifndef _GFXCUBEMAP_H_
  40. #include "gfx/gfxCubemap.h"
  41. #endif
  42. #ifndef _TDICTIONARY_H_
  43. #include "core/util/tDictionary.h"
  44. #endif
  45. #ifndef _TSIGNAL_H_
  46. #include "core/util/tSignal.h"
  47. #endif
  48. #ifndef _GFXDEVICESTATISTICS_H_
  49. #include "gfx/gfxDeviceStatistics.h"
  50. #endif
  51. #ifndef _MATHUTIL_FRUSTUM_H_
  52. #include "math/util/frustum.h"
  53. #endif
  54. #ifndef _PLATFORM_PLATFORMTIMER_H_
  55. #include "platform/platformTimer.h"
  56. #endif
  57. class FontRenderBatcher;
  58. class GFont;
  59. class GFXCardProfiler;
  60. class GFXDrawUtil;
  61. class GFXFence;
  62. class GFXOcclusionQuery;
  63. class GFXPrimitiveBuffer;
  64. class GFXShader;
  65. class GFXStateBlock;
  66. class GFXShaderConstBuffer;
  67. class GFXTextureManager;
  68. // Global macro
  69. #define GFX GFXDevice::get()
  70. #define MAX_MRT_TARGETS 4
  71. //-----------------------------------------------------------------------------
  72. /// GFXDevice is the TSE graphics interface layer. This allows the TSE to
  73. /// do many things, such as use multiple render devices for multi-head systems,
  74. /// and allow a game to render in DirectX 9, OpenGL or any other API which has
  75. /// a GFX implementation seamlessly. There are many concepts in GFX device which
  76. /// may not be familiar to you, especially if you have not used DirectX.
  77. /// @n
  78. /// <b>Buffers</b>
  79. /// There are three types of buffers in GFX: vertex, index and primitive. Please
  80. /// note that index buffers are not accessable outside the GFX layer, they are wrapped
  81. /// by primitive buffers. Primitive buffers will be explained in detail later.
  82. /// Buffers are allocated and deallocated using their associated allocXBuffer and
  83. /// freeXBuffer methods on the device. When a buffer is allocated you pass in a
  84. /// pointer to, depending on the buffer, a vertex type pointer or a U16 pointer.
  85. /// During allocation, this pointer is set to the address of where you should
  86. /// copy in the information for this buffer. You must the tell the GFXDevice
  87. /// that the information is in, and it should prepare the buffer for use by calling
  88. /// the prepare method on it. Dynamic vertex buffer example:
  89. /// @code
  90. /// GFXVertexP *verts; // Making a buffer containing verticies with only position
  91. ///
  92. /// // Allocate a dynamic vertex buffer to hold 3 vertices and use *verts as the location to copy information into
  93. /// GFXVertexBufferHandle vb = GFX->allocVertexBuffer( 3, &verts, true );
  94. ///
  95. /// // Now set the information, we're making a triangle
  96. /// verts[0].point = Point3F( 200.f, 200.f, 0.f );
  97. /// verts[1].point = Point3F( 200.f, 400.f, 0.f );
  98. /// verts[2].point = Point3F( 400.f, 200.f, 0.f );
  99. ///
  100. /// // Tell GFX that the information is in and it should be made ready for use
  101. /// // Note that nothing is done with verts, this should not and MUST NOT be deleted
  102. /// // stored, or otherwise used after prepare is called.
  103. /// GFX->prepare( vb );
  104. ///
  105. /// // Because this is a dynamic vertex buffer, it is only assured to be valid until someone
  106. /// // else allocates a dynamic vertex buffer, so we will render it now
  107. /// GFX->setVertexBuffer( vb );
  108. /// GFX->drawPrimitive( GFXTriangleStrip, 0, 1 );
  109. ///
  110. /// // Now because this is a dynamic vertex buffer it MUST NOT BE FREED you are only
  111. /// // given a handle to a vertex buffer which belongs to the device
  112. /// @endcode
  113. ///
  114. /// To use a static vertex buffer, it is very similar, this is an example using a
  115. /// static primitive buffer:
  116. /// @n
  117. /// This takes place inside a constructor for a class which has a member variable
  118. /// called mPB which is the primitive buffer for the class instance.
  119. /// @code
  120. /// U16 *idx; // This is going to be where to write indices
  121. /// GFXPrimitiveInfo *primitiveInfo; // This will be where to write primitive information
  122. ///
  123. /// // Allocate a primitive buffer with 4 indices, and 1 primitive described for use
  124. /// mPB = GFX->allocPrimitiveBuffer( 4, &idx, 1, &primitiveInfo );
  125. ///
  126. /// // Write the index information, this is going to be for the outline of a triangle using
  127. /// // a line strip
  128. /// idx[0] = 0;
  129. /// idx[1] = 1;
  130. /// idx[2] = 2;
  131. /// idx[3] = 0;
  132. ///
  133. /// // Write the information for the primitive
  134. /// primitiveInfo->indexStart = 0; // Starting with index 0
  135. /// primitiveInfo->minVertex = 0; // The minimum vertex index is 0
  136. /// primitiveInfo->maxVertex = 3; // The maximum vertex index is 3
  137. /// primitiveInfo->primitiveCount = 3; // There are 3 lines we are drawing
  138. /// primitiveInfo->type = GFXLineStrip; // This primitive info describes a line strip
  139. /// @endcode
  140. /// The following code takes place in the destructor for the same class
  141. /// @code
  142. /// // Because this is a static buffer it's our responsibility to free it when we are done
  143. /// GFX->freePrimitiveBuffer( mPB );
  144. /// @endcode
  145. /// This last bit takes place in the rendering function for the class
  146. /// @code
  147. /// // You need to set a vertex buffer as well, primitive buffers contain indexing
  148. /// // information, not vertex information. This is so you could have, say, a static
  149. /// // vertex buffer, and a dynamic primitive buffer.
  150. ///
  151. /// // This sets the primitive buffer to the static buffer we allocated in the constructor
  152. /// GFX->setPrimitiveBuffer( mPB );
  153. ///
  154. /// // Draw the first primitive contained in the set primitive buffer, our primitive buffer
  155. /// // has only one primitive, so we could also technically call GFX->drawPrimitives(); and
  156. /// // get the same result.
  157. /// GFX->drawPrimitive( 0 );
  158. /// @endcode
  159. /// If you need any more examples on how to use these buffers please see the rest of the engine.
  160. /// @n
  161. /// <b>Primitive Buffers</b>
  162. /// @n
  163. /// Primitive buffers wrap and extend the concept of index buffers. The purpose of a primitive
  164. /// buffer is to let objects store all information they have to render their primitives in
  165. /// a central place. Say that a shape is made up of triangle strips and triangle fans, it would
  166. /// still have only one primitive buffer which contained primitive information for each strip
  167. /// and fan. It could then draw itself with one call.
  168. ///
  169. /// TO BE FINISHED LATER
  170. class GFXDevice
  171. {
  172. private:
  173. friend class GFXInit;
  174. friend class GFXPrimitiveBufferHandle;
  175. friend class GFXVertexBufferHandleBase;
  176. friend class GFXTextureObject;
  177. friend class GFXTexHandle;
  178. friend class GFXVertexFormat;
  179. friend class GFXTestFullscreenToggle;
  180. friend class TestGFXTextureCube;
  181. friend class TestGFXRenderTargetCube;
  182. friend class TestGFXRenderTargetStack;
  183. friend class GFXResource;
  184. friend class LightMatInstance; // For stencil interface
  185. //--------------------------------------------------------------------------
  186. // Static GFX interface
  187. //--------------------------------------------------------------------------
  188. public:
  189. enum GFXDeviceEventType
  190. {
  191. /// The device has been created, but not initialized
  192. deCreate,
  193. /// The device has been initialized
  194. deInit,
  195. /// The device is about to be destroyed.
  196. deDestroy,
  197. /// The device has started rendering a frame
  198. deStartOfFrame,
  199. /// The device is about to finish rendering a frame
  200. deEndOfFrame,
  201. /// The device has rendered a frame and ended the scene
  202. dePostFrame,
  203. /// The device has started rendering a frame's field (such as for side-by-side rendering)
  204. deStartOfField,
  205. /// left stereo frame has been rendered
  206. deLeftStereoFrameRendered,
  207. /// right stereo frame has been rendered
  208. deRightStereoFrameRendered,
  209. /// The device is about to finish rendering a frame's field
  210. deEndOfField,
  211. };
  212. typedef Signal <bool (GFXDeviceEventType)> DeviceEventSignal;
  213. static DeviceEventSignal& getDeviceEventSignal();
  214. static GFXDevice *get() { return smGFXDevice; }
  215. static void initConsole();
  216. static bool destroy();
  217. static bool devicePresent() { return (smGFXDevice && smGFXDevice->getAdapterType() != NullDevice); }
  218. private:
  219. /// @name Device management variables
  220. /// @{
  221. static GFXDevice * smGFXDevice; ///< Global GFXDevice
  222. /// @}
  223. //--------------------------------------------------------------------------
  224. // Core GFX interface
  225. //--------------------------------------------------------------------------
  226. public:
  227. enum GFXDeviceRenderStyles
  228. {
  229. RS_Standard = 0,
  230. RS_StereoSideBySide = (1<<0), // Render into current Render Target side-by-side
  231. RS_StereoSeparate = (1<<1) // Render in two separate passes (then combined by vr compositor)
  232. };
  233. enum GFXDeviceLimits
  234. {
  235. NumStereoPorts = 2
  236. };
  237. private:
  238. /// Adapter for this device.
  239. GFXAdapter mAdapter;
  240. protected:
  241. /// List of valid video modes for this device.
  242. Vector<GFXVideoMode> mVideoModes;
  243. /// The CardProfiler for this device.
  244. GFXCardProfiler *mCardProfiler;
  245. /// Head of the resource list.
  246. ///
  247. /// @see GFXResource
  248. GFXResource *mResourceListHead;
  249. /// Set once the device is active.
  250. bool mCanCurrentlyRender;
  251. /// Set if we're in a mode where we want rendering to occur.
  252. bool mAllowRender;
  253. /// The style of rendering that is to be performed, based on GFXDeviceRenderStyles
  254. U32 mCurrentRenderStyle;
  255. /// Current stereo target being rendered to
  256. S32 mCurrentStereoTarget;
  257. /// Eye offset used when using a stereo rendering style
  258. Point3F mStereoEyeOffset[NumStereoPorts];
  259. /// Center matrix for head
  260. MatrixF mStereoHeadTransform;
  261. /// Left and right matrix for eyes
  262. MatrixF mStereoEyeTransforms[NumStereoPorts];
  263. /// Inverse of mStereoEyeTransforms
  264. MatrixF mInverseStereoEyeTransforms[NumStereoPorts];
  265. /// Fov port settings
  266. FovPort mFovPorts[NumStereoPorts];
  267. /// Destination viewports for stereo rendering
  268. RectI mStereoViewports[NumStereoPorts];
  269. /// Destination targets for stereo rendering
  270. GFXTextureTarget* mStereoTargets[NumStereoPorts];
  271. /// This will allow querying to see if a device is initialized and ready to
  272. /// have operations performed on it.
  273. bool mInitialized;
  274. bool mReset;
  275. /// This is called before this, or any other device, is deleted in the global destroy()
  276. /// method. It allows the device to clean up anything while everything is still valid.
  277. virtual void preDestroy();
  278. /// Set the adapter that this device is using. For use by GFXInit::createDevice only.
  279. virtual void setAdapter(const GFXAdapter& adapter) { mAdapter = adapter; }
  280. /// Notify GFXDevice that we are initialized
  281. virtual void deviceInited();
  282. public:
  283. GFXDevice();
  284. virtual ~GFXDevice();
  285. /// Initialize this GFXDevice, optionally specifying a platform window to
  286. /// bind to.
  287. virtual void init( const GFXVideoMode &mode, PlatformWindow *window = NULL ) = 0;
  288. /// Returns true if the scene has begun and its
  289. /// safe to make rendering calls.
  290. /// @see beginScene
  291. /// @see endScene
  292. bool canCurrentlyRender() const { return mCanCurrentlyRender; }
  293. bool recentlyReset(){ return mReset; }
  294. void beginReset(){ mReset = true; }
  295. void finalizeReset(){ mReset = false; }
  296. void setAllowRender( bool render ) { mAllowRender = render; }
  297. inline bool allowRender() const { return mAllowRender; }
  298. /// Retrieve the current rendering style based on GFXDeviceRenderStyles
  299. U32 getCurrentRenderStyle() const { return mCurrentRenderStyle; }
  300. /// Retrieve the current stereo target being rendered to
  301. S32 getCurrentStereoTarget() const { return mCurrentStereoTarget; }
  302. /// Set the current rendering style, based on GFXDeviceRenderStyles
  303. void setCurrentRenderStyle(U32 style) { mCurrentRenderStyle = style; }
  304. /// Set the current stereo target being rendered to (in case we're doing anything with postfx)
  305. void setCurrentStereoTarget(const F32 targetId) { mCurrentStereoTarget = targetId; }
  306. /// Get the current eye offset used during stereo rendering
  307. const Point3F* getStereoEyeOffsets() { return mStereoEyeOffset; }
  308. const MatrixF& getStereoHeadTransform() { return mStereoHeadTransform; }
  309. const MatrixF* getStereoEyeTransforms() { return mStereoEyeTransforms; }
  310. const MatrixF* getInverseStereoEyeTransforms() { return mInverseStereoEyeTransforms; }
  311. /// Sets the head matrix for stereo rendering
  312. void setStereoHeadTransform(const MatrixF &mat) { mStereoHeadTransform = mat; }
  313. /// Set the current eye offset used during stereo rendering
  314. void setStereoEyeOffsets(Point3F *offsets) { dMemcpy(mStereoEyeOffset, offsets, sizeof(Point3F) * NumStereoPorts); }
  315. void setStereoEyeTransforms(MatrixF *transforms) { dMemcpy(mStereoEyeTransforms, transforms, sizeof(mStereoEyeTransforms)); dMemcpy(mInverseStereoEyeTransforms, transforms, sizeof(mInverseStereoEyeTransforms)); mInverseStereoEyeTransforms[0].inverse(); mInverseStereoEyeTransforms[1].inverse(); }
  316. /// Set the current eye offset used during stereo rendering. Assumes NumStereoPorts are available.
  317. void setStereoFovPort(const FovPort *ports) { dMemcpy(mFovPorts, ports, sizeof(mFovPorts)); }
  318. /// Get the current eye offset used during stereo rendering
  319. const FovPort* getStereoFovPort() { return mFovPorts; }
  320. /// Sets stereo viewports
  321. void setSteroViewports(const RectI *ports) { dMemcpy(mStereoViewports, ports, sizeof(RectI) * NumStereoPorts); }
  322. /// Sets stereo render targets
  323. void setStereoTargets(GFXTextureTarget **targets) { mStereoTargets[0] = targets[0]; mStereoTargets[1] = targets[1]; }
  324. RectI* getStereoViewports() { return mStereoViewports; }
  325. /// Activates a stereo render target, setting the correct viewport to render eye contents.
  326. /// If eyeId is -1, set a viewport encompassing the entire size of the render targets.
  327. void activateStereoTarget(S32 eyeId)
  328. {
  329. if (eyeId == -1)
  330. {
  331. if (mStereoTargets[0])
  332. {
  333. setActiveRenderTarget(mStereoTargets[0], true);
  334. }
  335. }
  336. else
  337. {
  338. if (mStereoTargets[eyeId])
  339. {
  340. setActiveRenderTarget(mStereoTargets[eyeId], false);
  341. }
  342. setViewport(mStereoViewports[eyeId]);
  343. }
  344. mCurrentStereoTarget = eyeId;
  345. }
  346. GFXCardProfiler* getCardProfiler() const { return mCardProfiler; }
  347. /// Returns active graphics adapter type.
  348. virtual GFXAdapterType getAdapterType()=0;
  349. /// Returns the Adapter that was used to create this device
  350. virtual const GFXAdapter& getAdapter() { return mAdapter; }
  351. /// @}
  352. /// @name Debug Methods
  353. /// @{
  354. virtual void enterDebugEvent(ColorI color, const char *name) = 0;
  355. virtual void leaveDebugEvent() = 0;
  356. virtual void setDebugMarker(ColorI color, const char *name) = 0;
  357. virtual const char* interpretDebugResult(long result) { return "Not Implemented"; };
  358. /// @}
  359. /// @name Resource debug methods
  360. /// @{
  361. /// Lists how many of each GFX resource (e.g. textures, texture targets, shaders, etc.) GFX is aware of
  362. /// @param unflaggedOnly If true, this method only counts unflagged resources
  363. virtual void listResources(bool unflaggedOnly);
  364. /// Flags all resources GFX is currently aware of
  365. virtual void flagCurrentResources();
  366. /// Clears the flag on all resources GFX is currently aware of
  367. virtual void clearResourceFlags();
  368. /// Dumps a description of the specified resource types to the console
  369. /// @param resNames A string of space separated class names (e.g. "GFXTextureObject GFXTextureTarget GFXShader")
  370. /// to describe to the console
  371. /// @param file A path to the file to write the descriptions to. If it is NULL or "", descriptions are
  372. /// written to the console.
  373. /// @param unflaggedOnly If true, this method only counts unflagged resources
  374. /// @note resNames is case sensitive because there is no dStristr function.
  375. virtual void describeResources(const char* resName, const char* file, bool unflaggedOnly);
  376. /// Returns the current GFXDeviceStatistics, stats are cleared every ::beginScene call.
  377. GFXDeviceStatistics* getDeviceStatistics() { return &mDeviceStatistics; }
  378. protected:
  379. GFXDeviceStatistics mDeviceStatistics;
  380. /// This is a helper method for describeResourcesToFile. It walks through the
  381. /// GFXResource list and sorts it by item type, putting the resources into the proper vector.
  382. /// @see describeResources
  383. virtual void fillResourceVectors(const char* resNames, bool unflaggedOnly, Vector<GFXResource*> &textureObjects,
  384. Vector<GFXResource*> &textureTargets, Vector<GFXResource*> &windowTargets, Vector<GFXResource*> &vertexBuffers,
  385. Vector<GFXResource*> &primitiveBuffers, Vector<GFXResource*> &fences, Vector<GFXResource*> &cubemaps,
  386. Vector<GFXResource*> &shaders, Vector<GFXResource*> &stateblocks);
  387. public:
  388. /// @}
  389. /// @name Video Mode Functions
  390. /// @{
  391. /// Enumerates the supported video modes of the device
  392. virtual void enumerateVideoModes() = 0;
  393. /// Returns the video mode list.
  394. /// @see GFXVideoMode
  395. const Vector<GFXVideoMode>* const getVideoModeList() const { return &mVideoModes; }
  396. /// Returns the first format from the list which meets all
  397. /// the criteria of the texture profile and query options.
  398. virtual GFXFormat selectSupportedFormat(GFXTextureProfile *profile,
  399. const Vector<GFXFormat> &formats, bool texture, bool mustblend, bool mustfilter) = 0;
  400. /// @}
  401. //-----------------------------------------------------------------------------
  402. protected:
  403. /// @name State tracking variables
  404. /// @{
  405. /// Set if ANY state is dirty, including matrices or primitive buffers.
  406. bool mStateDirty;
  407. enum TexDirtyType
  408. {
  409. GFXTDT_Normal,
  410. GFXTDT_Cube,
  411. GFXTDT_CubeArray
  412. };
  413. GFXTexHandle mCurrentTexture[TEXTURE_STAGE_COUNT];
  414. GFXTexHandle mNewTexture[TEXTURE_STAGE_COUNT];
  415. GFXCubemapHandle mCurrentCubemap[TEXTURE_STAGE_COUNT];
  416. GFXCubemapHandle mNewCubemap[TEXTURE_STAGE_COUNT];
  417. GFXCubemapArrayHandle mCurrentCubemapArray[TEXTURE_STAGE_COUNT];
  418. GFXCubemapArrayHandle mNewCubemapArray[TEXTURE_STAGE_COUNT];
  419. TexDirtyType mTexType[TEXTURE_STAGE_COUNT];
  420. bool mTextureDirty[TEXTURE_STAGE_COUNT];
  421. bool mTexturesDirty;
  422. // This maps a GFXStateBlockDesc hash value to a GFXStateBlockRef
  423. typedef Map<U32, GFXStateBlockRef> StateBlockMap;
  424. StateBlockMap mCurrentStateBlocks;
  425. // This tracks whether or not our state block is dirty.
  426. bool mStateBlockDirty;
  427. GFXStateBlockRef mCurrentStateBlock;
  428. GFXStateBlockRef mNewStateBlock;
  429. GFXShaderConstBuffer *mCurrentShaderConstBuffer;
  430. /// A global forced wireframe mode.
  431. static bool smWireframe;
  432. /// The global vsync state.
  433. static bool smDisableVSync;
  434. /// The forced shader model version if non-zero.
  435. static F32 smForcedPixVersion;
  436. /// Disable all hardware occlusion queries causing
  437. /// them to return only the visibile state.
  438. static bool smDisableOcclusionQuery;
  439. /// @}
  440. /// @name Light Tracking
  441. /// @{
  442. GFXLightInfo mCurrentLight[LIGHT_STAGE_COUNT];
  443. bool mCurrentLightEnable[LIGHT_STAGE_COUNT];
  444. bool mLightDirty[LIGHT_STAGE_COUNT];
  445. bool mLightsDirty;
  446. LinearColorF mGlobalAmbientColor;
  447. bool mGlobalAmbientColorDirty;
  448. /// @}
  449. /// @name Fixed function material tracking
  450. /// @{
  451. GFXLightMaterial mCurrentLightMaterial;
  452. bool mLightMaterialDirty;
  453. /// @}
  454. /// @name Bitmap modulation and color stack
  455. /// @{
  456. ///
  457. /// @}
  458. /// @see getDeviceSwizzle32
  459. Swizzle<U8, 4> *mDeviceSwizzle32;
  460. /// @see getDeviceSwizzle24
  461. Swizzle<U8, 3> *mDeviceSwizzle24;
  462. //-----------------------------------------------------------------------------
  463. /// @name Matrix managing variables
  464. /// @{
  465. ///
  466. MatrixF mWorldMatrix[WORLD_STACK_MAX];
  467. bool mWorldMatrixDirty;
  468. S32 mWorldStackSize;
  469. MatrixF mProjectionMatrix;
  470. bool mProjectionMatrixDirty;
  471. MatrixF mViewMatrix;
  472. bool mViewMatrixDirty;
  473. MatrixF mTextureMatrix[TEXTURE_STAGE_COUNT];
  474. bool mTextureMatrixDirty[TEXTURE_STAGE_COUNT];
  475. bool mTextureMatrixCheckDirty;
  476. /// @}
  477. /// @name Current frustum planes
  478. /// @{
  479. ///
  480. Frustum mFrustum;
  481. //-----------------------------------------------------------------------------
  482. /// @name Stateblock functions
  483. /// @{
  484. /// Called by GFXDevice to create a device specific stateblock
  485. virtual GFXStateBlockRef createStateBlockInternal(const GFXStateBlockDesc& desc) = 0;
  486. /// Called by GFXDevice to actually set a stateblock.
  487. /// @param force If true, set all states
  488. virtual void setStateBlockInternal(GFXStateBlock* block, bool force) = 0;
  489. /// @}
  490. /// Called by base GFXDevice to actually set a const buffer
  491. virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer) = 0;
  492. virtual void setTextureInternal(U32 textureUnit, const GFXTextureObject*texture) = 0;
  493. virtual void setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable) = 0;
  494. virtual void setGlobalAmbientInternal(LinearColorF color) = 0;
  495. virtual void setLightMaterialInternal(const GFXLightMaterial mat) = 0;
  496. virtual bool beginSceneInternal() = 0;
  497. virtual void endSceneInternal() = 0;
  498. /// @name State Initialization.
  499. /// @{
  500. /// State initialization. This MUST BE CALLED in setVideoMode after the device
  501. /// is created.
  502. virtual void initStates() = 0;
  503. /// @}
  504. //-----------------------------------------------------------------------------
  505. /// This function must be implemented differently per
  506. /// API and it should set ONLY the current matrix.
  507. /// For example, in OpenGL, there should be NO matrix stack
  508. /// activity, all the stack stuff is managed in the GFX layer.
  509. ///
  510. /// OpenGL does not have separate world and
  511. /// view matrices. It has ModelView which is world * view.
  512. /// You must take this into consideration.
  513. ///
  514. /// @param mtype Which matrix to set, world/view/projection
  515. /// @param mat Matrix to assign
  516. virtual void setMatrix( GFXMatrixType mtype, const MatrixF &mat ) = 0;
  517. //-----------------------------------------------------------------------------
  518. protected:
  519. /// @name Buffer Allocation
  520. /// These methods are implemented per-device and are called by the GFX layer
  521. /// when a user calls an alloc
  522. ///
  523. /// @note Primitive Buffers are NOT implemented per device, they wrap index buffers
  524. /// @{
  525. /// This allocates a vertex buffer and returns a pointer to the allocated buffer.
  526. /// This function should not be called directly - rather it should be used by
  527. /// the GFXVertexBufferHandle class.
  528. virtual GFXVertexBuffer *allocVertexBuffer( U32 numVerts,
  529. const GFXVertexFormat *vertexFormat,
  530. U32 vertSize,
  531. GFXBufferType bufferType,
  532. void* data = NULL ) = 0;
  533. /// Called from GFXVertexFormat to allocate the hardware
  534. /// specific vertex declaration for rendering.
  535. virtual GFXVertexDecl* allocVertexDecl( const GFXVertexFormat *vertexFormat ) = 0;
  536. /// Sets the current vertex declaration on the device.
  537. virtual void setVertexDecl( const GFXVertexDecl *decl ) = 0;
  538. /// Sets the vertex buffer on the device.
  539. virtual void setVertexStream( U32 stream, GFXVertexBuffer *buffer ) = 0;
  540. /// Set the vertex stream frequency on the device.
  541. virtual void setVertexStreamFrequency( U32 stream, U32 frequency ) = 0;
  542. /// The maximum number of supported vertex streams which
  543. /// may be more than the device supports.
  544. static const U32 VERTEX_STREAM_COUNT = 4;
  545. StrongRefPtr<GFXVertexBuffer> mCurrentVertexBuffer[VERTEX_STREAM_COUNT];
  546. bool mVertexBufferDirty[VERTEX_STREAM_COUNT];
  547. U32 mVertexBufferFrequency[VERTEX_STREAM_COUNT];
  548. bool mVertexBufferFrequencyDirty[VERTEX_STREAM_COUNT];
  549. const GFXVertexDecl *mCurrVertexDecl;
  550. bool mVertexDeclDirty;
  551. StrongRefPtr<GFXPrimitiveBuffer> mCurrentPrimitiveBuffer;
  552. bool mPrimitiveBufferDirty;
  553. /// This allocates a primitive buffer and returns a pointer to the allocated buffer.
  554. /// A primitive buffer's type argument refers to the index data - the primitive data will
  555. /// always be preserved from call to call.
  556. ///
  557. /// @note All index buffers use unsigned 16-bit indices.
  558. virtual GFXPrimitiveBuffer *allocPrimitiveBuffer( U32 numIndices,
  559. U32 numPrimitives,
  560. GFXBufferType bufferType,
  561. void* data = NULL ) = 0;
  562. /// @}
  563. //---------------------------------------
  564. // SFX buffer
  565. //---------------------------------------
  566. protected:
  567. GFXTexHandle mFrontBuffer[2];
  568. U32 mCurrentFrontBufferIdx;
  569. //---------------------------------------
  570. // Render target related
  571. //---------------------------------------
  572. /// A stack of previously active render targets.
  573. Vector<GFXTargetRef> mRTStack;
  574. /// The current render target which may or may not
  575. /// not be yet activated.
  576. /// @see mRTDirty
  577. GFXTargetRef mCurrentRT;
  578. /// This tracks a previously activated render target
  579. /// which need to be deactivated.
  580. GFXTargetRef mRTDeactivate;
  581. /// This is set when the current and/or deactivate render
  582. /// targets have changed and the device need to update
  583. /// its state on the next draw/clear.
  584. bool mRTDirty;
  585. /// Updates the render targets and viewport in a device
  586. /// specific manner when they are dirty.
  587. virtual void _updateRenderTargets() = 0;
  588. /// The current viewport rect.
  589. RectI mViewport;
  590. /// If true the viewport has been changed and
  591. /// it must be updated on the next draw/clear.
  592. bool mViewportDirty;
  593. public:
  594. /// @name Texture functions
  595. /// @{
  596. protected:
  597. GFXTextureManager * mTextureManager;
  598. public:
  599. virtual GFXCubemap * createCubemap() = 0;
  600. virtual GFXCubemapArray *createCubemapArray() = 0;
  601. inline GFXTextureManager *getTextureManager()
  602. {
  603. return mTextureManager;
  604. }
  605. ///@}
  606. /// Swizzle to convert 32bpp bitmaps from RGBA to the native device format.
  607. const Swizzle<U8, 4> *getDeviceSwizzle32() const
  608. {
  609. return mDeviceSwizzle32;
  610. }
  611. /// Swizzle to convert 24bpp bitmaps from RGB to the native device format.
  612. const Swizzle<U8, 3> *getDeviceSwizzle24() const
  613. {
  614. return mDeviceSwizzle24;
  615. }
  616. /// @name Render Target functions
  617. /// @{
  618. /// Allocate a target for doing render to texture operations, with no
  619. /// depth/stencil buffer.
  620. virtual GFXTextureTarget *allocRenderToTextureTarget(bool genMips = true) = 0;
  621. /// Allocate a target for a given window.
  622. virtual GFXWindowTarget *allocWindowTarget(PlatformWindow *window)=0;
  623. /// Store the current render target to restore later.
  624. void pushActiveRenderTarget();
  625. /// Restore the previous render target.
  626. void popActiveRenderTarget();
  627. /// Assign a new active render target.
  628. void setActiveRenderTarget( GFXTarget *target, bool updateViewport=true );
  629. /// Returns the current active render target.
  630. inline GFXTarget* getActiveRenderTarget() { return mCurrentRT; }
  631. ///@}
  632. /// @name Shader functions
  633. /// @{
  634. virtual F32 getPixelShaderVersion() const = 0;
  635. virtual void setPixelShaderVersion( F32 version ) = 0;
  636. /// Returns the number of texture samplers that can be used in a shader rendering pass
  637. virtual U32 getNumSamplers() const = 0;
  638. /// Returns the number of simultaneous render targets supported by the device.
  639. virtual U32 getNumRenderTargets() const = 0;
  640. virtual void setShader( GFXShader *shader, bool force = false ) {}
  641. /// Set the buffer! (Actual set happens on the next draw call, just like textures, state blocks, etc)
  642. void setShaderConstBuffer(GFXShaderConstBuffer* buffer);
  643. /// Creates a new empty shader which must be initialized
  644. /// and deleted by the caller.
  645. /// @see GFXShader::init
  646. virtual GFXShader* createShader() = 0;
  647. /// @}
  648. //-----------------------------------------------------------------------------
  649. /// @name Copying methods
  650. /// @{
  651. virtual void copyResource(GFXTextureObject *pDst, GFXCubemap *pSrc, const U32 face) = 0;
  652. /// @}
  653. /// @name Rendering methods
  654. /// @{
  655. ///
  656. virtual void clear( U32 flags, const LinearColorF& color, F32 z, U32 stencil ) = 0;
  657. virtual void clearColorAttachment(const U32 attachment, const LinearColorF& color) = 0;
  658. virtual bool beginScene();
  659. virtual void endScene();
  660. virtual void beginField();
  661. virtual void endField();
  662. PlatformTimer *mFrameTime;
  663. virtual GFXTexHandle & getFrontBuffer(){ return mFrontBuffer[mCurrentFrontBufferIdx]; }
  664. void setPrimitiveBuffer( GFXPrimitiveBuffer *buffer );
  665. /// Sets the vertex buffer.
  666. ///
  667. /// When setting the stream 0 vertex buffer it will automatically
  668. /// set its associated vertex format as the active format.
  669. ///
  670. /// @param buffer The vertex buffer or NULL to clear the buffer.
  671. /// @param stream The stream index of the vertex source stream to place the buffer.
  672. /// @param frequency The stream frequency of the vertex buffer.
  673. void setVertexBuffer( GFXVertexBuffer *buffer, U32 stream = 0, U32 frequency = 0 );
  674. /// Sets the current vertex format.
  675. ///
  676. /// This should only be used if the vertex format of the stream 0 vertex
  677. /// buffer is different from the one associated to it. Typically this
  678. /// is used when rendering from multiple vertex streams.
  679. ///
  680. void setVertexFormat( const GFXVertexFormat *vertexFormat );
  681. virtual void drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount ) = 0;
  682. /// The parameters to drawIndexedPrimitive are somewhat complicated. From a raw-data stand point
  683. /// they evaluate to something like the following:
  684. /// @code
  685. /// U16 indicies[] = { 0, 1, 2, 1, 0, 0, 2 };
  686. /// Point3F verts[] = { Point3F( 0.0f, 0.0f, 0.0f ), Point3F( 0.0f, 1.0f, 0.0f ), Point3F( 0.0f, 0.0f, 1.0f ) };
  687. ///
  688. /// GFX->drawIndexedPrimitive( GFXLineList, // Drawing a list of lines, each line is two verts
  689. /// 0, // vertex 0 will be referenced so minIndex = 0
  690. /// 3, // 3 verticies will be used for this draw call
  691. /// 1, // We want index 1 to be the first index used, so indicies 1-6 will be used
  692. /// 3 // Drawing 3 LineList primitives, meaning 6 verts will be drawn
  693. /// );
  694. ///
  695. /// U16 *idxPtr = &indicies[1]; // 1 = startIndex, so the pointer is offset such that:
  696. /// // idxPtr[0] is the same as indicies[1]
  697. ///
  698. /// U32 numVertsToDrawFromBuffer = primitiveCount * 2; // 2 verts define a line in the GFXLineList primitive type (6)
  699. /// @endcode
  700. ///
  701. /// @param primType Type of primitive to draw
  702. ///
  703. /// @param startVertex This defines index zero. Its the offset from the start of
  704. /// the vertex buffer to the first vertex.
  705. ///
  706. /// @param minIndex The smallest index into the vertex stream which will be used for this draw call.
  707. /// This is a zero based index relative to startVertex. It is strictly a performance
  708. /// hint for implementations. No vertex below minIndex will be referenced by this draw
  709. /// call. For device implementors, this should _not_ be used to offset the vertex buffer,
  710. /// or index buffer.
  711. ///
  712. /// @param numVerts The number of verticies which will be referenced in this draw call. This is not
  713. /// the number of verticies which will be drawn. That is a function of 'primType' and
  714. /// 'primitiveCount'.
  715. ///
  716. /// @param startIndex An offset from the start of the index buffer to specify where to start. If
  717. /// 'idxBuffer' is a pointer to an array of integers, this could be written as
  718. /// int *offsetIdx = idxBuffer + startIndex;
  719. ///
  720. /// @param primitiveCount The number of primitives of type 'primType' to draw.
  721. ///
  722. virtual void drawIndexedPrimitive( GFXPrimitiveType primType,
  723. U32 startVertex,
  724. U32 minIndex,
  725. U32 numVerts,
  726. U32 startIndex,
  727. U32 primitiveCount ) = 0;
  728. void drawPrimitive( const GFXPrimitive &prim );
  729. void drawPrimitive( U32 primitiveIndex );
  730. void drawPrimitives();
  731. void drawPrimitiveBuffer( GFXPrimitiveBuffer *buffer );
  732. /// @}
  733. //-----------------------------------------------------------------------------
  734. /// Allocate a fence. The API specific implementation of GFXDevice is responsible
  735. /// to make sure that the proper type is used. GFXGeneralFence should work in
  736. /// all cases.
  737. virtual GFXFence *createFence() = 0;
  738. /// Returns a hardware occlusion query object or NULL
  739. /// if this device does not support them.
  740. virtual GFXOcclusionQuery* createOcclusionQuery() { return NULL; }
  741. /// @name Light Settings
  742. /// NONE of these should be overridden by API implementations
  743. /// because of the state caching stuff.
  744. /// @{
  745. void setLight(U32 stage, GFXLightInfo* light);
  746. void setLightMaterial(const GFXLightMaterial& mat);
  747. void setGlobalAmbientColor(const LinearColorF& color);
  748. /// @}
  749. /// @name Texture State Settings
  750. /// NONE of these should be overridden by API implementations
  751. /// because of the state caching stuff.
  752. /// @{
  753. ///
  754. void setTexture(U32 stage, GFXTextureObject *texture);
  755. void setCubeTexture( U32 stage, GFXCubemap *cubemap );
  756. void setCubeArrayTexture( U32 stage, GFXCubemapArray *cubemapArray);
  757. inline GFXTextureObject* getCurrentTexture( U32 stage ) { return mCurrentTexture[stage]; }
  758. /// @}
  759. /// @name State Block Interface
  760. /// @{
  761. /// Creates a state block object based on the desc passed in. This object
  762. /// represents an immutable state.
  763. virtual GFXStateBlockRef createStateBlock( const GFXStateBlockDesc &desc );
  764. /// Sets the current stateblock (actually activated in ::updateStates)
  765. virtual void setStateBlock( GFXStateBlock *block );
  766. GFXStateBlock* getStateBlock() { return mNewStateBlock; }
  767. /// This sets a stateblock directly from the description
  768. /// structure. Its acceptable to use this for debug rendering
  769. /// and other low frequency rendering tasks.
  770. virtual void setStateBlockByDesc( const GFXStateBlockDesc &desc );
  771. /// @}
  772. /// @name General state interface
  773. /// @{
  774. /// Sets the dirty Render/Texture/Sampler states from the caching system
  775. void updateStates(bool forceSetAll = false);
  776. void clearTextureStateImmediate(U32 stage);
  777. /// Returns the forced global wireframe state.
  778. static bool getWireframe() { return smWireframe; }
  779. /// Returns true if the occlusion query is disabled.
  780. static bool getDisableOcclusionQuery() { return smDisableOcclusionQuery; }
  781. /// @}
  782. //-----------------------------------------------------------------------------
  783. /// @name Matrix interface
  784. /// @{
  785. /// Sets the top of the world matrix stack
  786. /// @param newWorld New world matrix to set
  787. void setWorldMatrix( const MatrixF &newWorld );
  788. /// Gets the matrix on the top of the world matrix stack
  789. inline const MatrixF &getWorldMatrix() const { return mWorldMatrix[mWorldStackSize]; }
  790. /// Pushes the world matrix stack and copies the current top
  791. /// matrix to the new top of the stack
  792. void pushWorldMatrix();
  793. /// Pops the world matrix stack
  794. void popWorldMatrix();
  795. /// Sets the projection matrix
  796. /// @param newProj New projection matrix to set
  797. void setProjectionMatrix( const MatrixF &newProj );
  798. /// Gets the projection matrix
  799. inline const MatrixF &getProjectionMatrix() const { return mProjectionMatrix; }
  800. /// Sets the view matrix
  801. /// @param newView New view matrix to set
  802. void setViewMatrix( const MatrixF &newView );
  803. /// Gets the view matrix
  804. inline const MatrixF &getViewMatrix() const { return mViewMatrix; }
  805. /// Multiplies the matrix at the top of the world matrix stack by a matrix
  806. /// and replaces the top of the matrix stack with the result
  807. /// @param mat Matrix to multiply
  808. void multWorld( const MatrixF &mat );
  809. /// Set texture matrix for a sampler
  810. void setTextureMatrix( const U32 stage, const MatrixF &texMat );
  811. /// Set an area of the target to render to.
  812. void setViewport( const RectI &rect );
  813. /// Get the current area of the target we will render to.
  814. const RectI &getViewport() const { return mViewport; }
  815. virtual void setClipRect( const RectI &rect ) = 0;
  816. virtual const RectI &getClipRect() const = 0;
  817. /// Set the projection frustum.
  818. ///
  819. /// @see MathUtils::makeFrustum()
  820. virtual void setFrustum( F32 left, F32 right,
  821. F32 bottom, F32 top,
  822. F32 nearPlane, F32 farPlane,
  823. bool bRotate = true);
  824. ///
  825. virtual void setFrustum( const Frustum& frust,
  826. bool bRotate = true );
  827. /// Get the projection frustum.
  828. void getFrustum( F32 *left,
  829. F32 *right,
  830. F32 *bottom,
  831. F32 *top,
  832. F32 *nearPlane,
  833. F32 *farPlane,
  834. bool *isOrtho ) const;
  835. /// Get the projection frustum.
  836. const Frustum& getFrustum() const { return mFrustum; }
  837. /// This will construct and apply an orthographic projection matrix with the provided parameters
  838. /// @param doRotate If set to true, the resulting matrix will be rotated PI/2 around the X axis
  839. // for support in tsShapeInstance. You probably want to leave this as 'false'.
  840. void setOrtho(F32 left, F32 right, F32 bottom, F32 top, F32 nearPlane, F32 farPlane, bool doRotate = false);
  841. /// Return true if the current frustum uses orthographic projection rather than perspective projection.
  842. bool isFrustumOrtho() const { return mFrustum.isOrtho(); }
  843. /// @}
  844. /// Returns the scale for converting world space
  845. /// units to screen space units... aka pixels.
  846. ///
  847. /// This is the true scale which is best used for GUI
  848. /// drawing. For doing lod calculations you should be
  849. /// using the functions in SceneState which is adjusted
  850. /// for special cases like shadows and reflections.
  851. ///
  852. /// @see SceneState::getWorldToScreenScale()
  853. /// @see SceneState::projectRadius()
  854. ///
  855. Point2F getWorldToScreenScale() const;
  856. public:
  857. enum GenericShaderType
  858. {
  859. GSColor = 0,
  860. GSTexture,
  861. GSModColorTexture,
  862. GSAddColorTexture,
  863. GSTargetRestore,
  864. GS_COUNT
  865. };
  866. /// This is a helper function to set a default shader for rendering GUI elements
  867. /// on systems which do not support fixed-function operations as well as for
  868. /// things which need just generic position/texture/color shaders
  869. ///
  870. /// @param type Type of generic shader, add your own if you need
  871. virtual void setupGenericShaders( GenericShaderType type = GSColor ) {};
  872. /// Get the fill convention for this device
  873. virtual F32 getFillConventionOffset() const = 0;
  874. virtual U32 getMaxDynamicVerts() = 0;
  875. virtual U32 getMaxDynamicIndices() = 0;
  876. virtual void doParanoidStateCheck(){};
  877. /// Get access to this device's drawing utility class.
  878. GFXDrawUtil *getDrawUtil();
  879. #ifndef TORQUE_SHIPPING
  880. /// This is a method designed for debugging. It will allow you to dump the states
  881. /// in the render manager out to a file so that it can be diffed and examined.
  882. void dumpStates( const char *fileName ) const;
  883. #else
  884. void dumpStates( const char *fileName ) const {};
  885. #endif
  886. protected:
  887. GFXDrawUtil *mDrawer;
  888. };
  889. //-----------------------------------------------------------------------------
  890. // Matrix interface
  891. inline void GFXDevice::setWorldMatrix( const MatrixF &newWorld )
  892. {
  893. mWorldMatrixDirty = true;
  894. mStateDirty = true;
  895. mWorldMatrix[mWorldStackSize] = newWorld;
  896. }
  897. inline void GFXDevice::pushWorldMatrix()
  898. {
  899. mWorldMatrixDirty = true;
  900. mStateDirty = true;
  901. mWorldStackSize++;
  902. AssertFatal( mWorldStackSize < WORLD_STACK_MAX, "GFX: Exceeded world matrix stack size" );
  903. mWorldMatrix[mWorldStackSize] = mWorldMatrix[mWorldStackSize - 1];
  904. }
  905. inline void GFXDevice::popWorldMatrix()
  906. {
  907. mWorldMatrixDirty = true;
  908. mStateDirty = true;
  909. mWorldStackSize--;
  910. AssertFatal( mWorldStackSize >= 0, "GFX: Negative WorldStackSize!" );
  911. }
  912. inline void GFXDevice::multWorld( const MatrixF &mat )
  913. {
  914. mWorldMatrixDirty = true;
  915. mStateDirty = true;
  916. mWorldMatrix[mWorldStackSize].mul(mat);
  917. }
  918. inline void GFXDevice::setProjectionMatrix( const MatrixF &newProj )
  919. {
  920. mProjectionMatrixDirty = true;
  921. mStateDirty = true;
  922. mProjectionMatrix = newProj;
  923. }
  924. inline void GFXDevice::setViewMatrix( const MatrixF &newView )
  925. {
  926. mStateDirty = true;
  927. mViewMatrixDirty = true;
  928. mViewMatrix = newView;
  929. }
  930. inline void GFXDevice::setTextureMatrix( const U32 stage, const MatrixF &texMat )
  931. {
  932. AssertFatal( stage < TEXTURE_STAGE_COUNT, "Out of range texture sampler" );
  933. mStateDirty = true;
  934. mTextureMatrixDirty[stage] = true;
  935. mTextureMatrix[stage] = texMat;
  936. mTextureMatrixCheckDirty = true;
  937. }
  938. //-----------------------------------------------------------------------------
  939. // Buffer management
  940. inline void GFXDevice::setVertexBuffer( GFXVertexBuffer *buffer, U32 stream, U32 frequency )
  941. {
  942. AssertFatal( stream < VERTEX_STREAM_COUNT, "GFXDevice::setVertexBuffer - Bad stream index!" );
  943. if ( buffer && stream == 0 )
  944. setVertexFormat( &buffer->mVertexFormat );
  945. if ( buffer != mCurrentVertexBuffer[stream] )
  946. {
  947. mCurrentVertexBuffer[stream] = buffer;
  948. mVertexBufferDirty[stream] = true;
  949. mStateDirty = true;
  950. }
  951. if ( mVertexBufferFrequency[stream] != frequency )
  952. {
  953. mVertexBufferFrequency[stream] = frequency;
  954. mVertexBufferFrequencyDirty[stream] = true;
  955. mStateDirty = true;
  956. }
  957. }
  958. inline void GFXDevice::setVertexFormat( const GFXVertexFormat *vertexFormat )
  959. {
  960. if ( vertexFormat->getDecl() == mCurrVertexDecl )
  961. return;
  962. mCurrVertexDecl = vertexFormat->getDecl();
  963. mVertexDeclDirty = true;
  964. mStateDirty = true;
  965. }
  966. #if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
  967. #define GFXAssertFatal(x, error) AssertFatal(x, error)
  968. #else
  969. #define GFXAssertFatal(x, error)
  970. #endif
  971. #endif // _GFXDEVICE_H_