gfxD3D9Device.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 _GFXD3D9DEVICE_H_
  23. #define _GFXD3D9DEVICE_H_
  24. #include "platform/tmm_off.h"
  25. #ifdef TORQUE_OS_XENON
  26. # include "platformXbox/platformXbox.h"
  27. #else
  28. # include <d3dx9.h>
  29. # include "platformWin32/platformWin32.h"
  30. #endif
  31. #ifndef _GFXD3D9STATEBLOCK_H_
  32. #include "gfx/D3D9/gfxD3D9StateBlock.h"
  33. #endif
  34. #ifndef _GFXD3DTEXTUREMANAGER_H_
  35. #include "gfx/D3D9/gfxD3D9TextureManager.h"
  36. #endif
  37. #ifndef _GFXD3D9CUBEMAP_H_
  38. #include "gfx/D3D9/gfxD3D9Cubemap.h"
  39. #endif
  40. #ifndef _GFXD3D9PRIMITIVEBUFFER_H_
  41. #include "gfx/D3D9/gfxD3D9PrimitiveBuffer.h"
  42. #endif
  43. #ifndef _GFXINIT_H_
  44. #include "gfx/gfxInit.h"
  45. #endif
  46. #ifndef _PLATFORMDLIBRARY_H
  47. #include "platform/platformDlibrary.h"
  48. #endif
  49. #ifndef TORQUE_OS_XENON
  50. #include <DxErr.h>
  51. #else
  52. #include <dxerr9.h>
  53. #define DXGetErrorStringA DXGetErrorString9A
  54. #define DXGetErrorDescriptionA DXGetErrorDescription9A
  55. #endif
  56. #include "platform/tmm_on.h"
  57. inline void D3D9Assert( HRESULT hr, const char *info )
  58. {
  59. #if defined( TORQUE_DEBUG )
  60. if( FAILED( hr ) )
  61. {
  62. char buf[256];
  63. dSprintf( buf, 256, "%s\n%s\n%s", DXGetErrorStringA( hr ), DXGetErrorDescriptionA( hr ), info );
  64. AssertFatal( false, buf );
  65. // DXTrace( __FILE__, __LINE__, hr, info, true );
  66. }
  67. #endif
  68. }
  69. // Typedefs
  70. #define D3DX_FUNCTION(fn_name, fn_return, fn_args) \
  71. typedef fn_return (WINAPI *D3DXFNPTR##fn_name)fn_args;
  72. #include "gfx/D3D9/d3dx9Functions.h"
  73. #undef D3DX_FUNCTION
  74. // Function table
  75. struct D3DXFNTable
  76. {
  77. D3DXFNTable() : isLoaded( false ){};
  78. bool isLoaded;
  79. DLibraryRef dllRef;
  80. DLibraryRef compilerDllRef;
  81. #define D3DX_FUNCTION(fn_name, fn_return, fn_args) \
  82. D3DXFNPTR##fn_name fn_name;
  83. #include "gfx/D3D9/d3dx9Functions.h"
  84. #undef D3DX_FUNCTION
  85. };
  86. #define GFXD3DX static_cast<GFXD3D9Device *>(GFX)->smD3DX
  87. class GFXResource;
  88. class GFXD3D9ShaderConstBuffer;
  89. //------------------------------------------------------------------------------
  90. class GFXD3D9Device : public GFXDevice
  91. {
  92. friend class GFXResource;
  93. friend class GFXD3D9PrimitiveBuffer;
  94. friend class GFXD3D9VertexBuffer;
  95. friend class GFXD3D9TextureObject;
  96. friend class GFXPCD3D9TextureTarget;
  97. friend class GFXPCD3D9WindowTarget;
  98. typedef GFXDevice Parent;
  99. protected:
  100. MatrixF mTempMatrix; ///< Temporary matrix, no assurances on value at all
  101. RectI mClipRect;
  102. typedef StrongRefPtr<GFXD3D9VertexBuffer> RPGDVB;
  103. Vector<RPGDVB> mVolatileVBList;
  104. class D3D9VertexDecl : public GFXVertexDecl
  105. {
  106. public:
  107. virtual ~D3D9VertexDecl()
  108. {
  109. SAFE_RELEASE( decl );
  110. }
  111. IDirect3DVertexDeclaration9 *decl;
  112. };
  113. /// Used to lookup a vertex declaration for the vertex format.
  114. /// @see allocVertexDecl
  115. typedef Map<String,D3D9VertexDecl*> VertexDeclMap;
  116. VertexDeclMap mVertexDecls;
  117. IDirect3DSurface9 *mDeviceBackbuffer;
  118. IDirect3DSurface9 *mDeviceDepthStencil;
  119. IDirect3DSurface9 *mDeviceColor;
  120. /// The stream 0 vertex buffer used for volatile VB offseting.
  121. GFXD3D9VertexBuffer *mVolatileVB;
  122. static void initD3DXFnTable();
  123. //-----------------------------------------------------------------------
  124. StrongRefPtr<GFXD3D9PrimitiveBuffer> mDynamicPB; ///< Dynamic index buffer
  125. GFXD3D9PrimitiveBuffer *mCurrentPB;
  126. IDirect3DVertexShader9 *mLastVertShader;
  127. IDirect3DPixelShader9 *mLastPixShader;
  128. S32 mCreateFenceType;
  129. LPDIRECT3D9 mD3D; ///< D3D Handle
  130. LPDIRECT3DDEVICE9 mD3DDevice; ///< Handle for D3DDevice
  131. #if !defined(TORQUE_OS_XENON)
  132. LPDIRECT3D9EX mD3DEx; ///< D3D9Ex Handle
  133. LPDIRECT3DDEVICE9EX mD3DDeviceEx; ///< Handle for D3DDevice9Ex
  134. #endif
  135. U32 mAdapterIndex; ///< Adapter index because D3D supports multiple adapters
  136. F32 mPixVersion;
  137. U32 mNumSamplers; ///< Profiled (via caps)
  138. U32 mNumRenderTargets; ///< Profiled (via caps)
  139. D3DMULTISAMPLE_TYPE mMultisampleType;
  140. DWORD mMultisampleLevel;
  141. bool mOcclusionQuerySupported;
  142. /// The current adapter display mode.
  143. D3DDISPLAYMODE mDisplayMode;
  144. /// To manage creating and re-creating of these when device is aquired
  145. void reacquireDefaultPoolResources();
  146. /// To release all resources we control from D3DPOOL_DEFAULT
  147. void releaseDefaultPoolResources();
  148. /// This you will probably never, ever use, but it is used to generate the code for
  149. /// the initStates() function
  150. void regenStates();
  151. virtual GFXD3D9VertexBuffer* findVBPool( const GFXVertexFormat *vertexFormat, U32 numVertsNeeded );
  152. virtual GFXD3D9VertexBuffer* createVBPool( const GFXVertexFormat *vertexFormat, U32 vertSize );
  153. #ifdef TORQUE_DEBUG
  154. /// @name Debug Vertex Buffer information/management
  155. /// @{
  156. ///
  157. U32 mNumAllocatedVertexBuffers; ///< To keep track of how many are allocated and freed
  158. GFXD3D9VertexBuffer *mVBListHead;
  159. void addVertexBuffer( GFXD3D9VertexBuffer *buffer );
  160. void removeVertexBuffer( GFXD3D9VertexBuffer *buffer );
  161. void logVertexBuffers();
  162. /// @}
  163. #endif
  164. // State overrides
  165. // {
  166. ///
  167. virtual void setTextureInternal(U32 textureUnit, const GFXTextureObject* texture);
  168. /// Called by GFXDevice to create a device specific stateblock
  169. virtual GFXStateBlockRef createStateBlockInternal(const GFXStateBlockDesc& desc);
  170. /// Called by GFXDevice to actually set a stateblock.
  171. virtual void setStateBlockInternal(GFXStateBlock* block, bool force);
  172. /// Track the last const buffer we've used. Used to notify new constant buffers that
  173. /// they should send all of their constants up
  174. StrongRefPtr<GFXD3D9ShaderConstBuffer> mCurrentConstBuffer;
  175. /// Called by base GFXDevice to actually set a const buffer
  176. virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer);
  177. // CodeReview - How exactly do we want to deal with this on the Xenon?
  178. // Right now it's just in an #ifndef in gfxD3D9Device.cpp - AlexS 4/11/07
  179. virtual void setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable);
  180. virtual void setLightMaterialInternal(const GFXLightMaterial mat);
  181. virtual void setGlobalAmbientInternal(ColorF color);
  182. virtual void initStates()=0;
  183. // }
  184. // Index buffer management
  185. // {
  186. virtual void _setPrimitiveBuffer( GFXPrimitiveBuffer *buffer );
  187. virtual void drawIndexedPrimitive( GFXPrimitiveType primType,
  188. U32 startVertex,
  189. U32 minIndex,
  190. U32 numVerts,
  191. U32 startIndex,
  192. U32 primitiveCount );
  193. // }
  194. virtual GFXShader* createShader();
  195. void disableShaders(bool force = false);
  196. /// Device helper function
  197. virtual D3DPRESENT_PARAMETERS setupPresentParams( const GFXVideoMode &mode, const HWND &hwnd ) const = 0;
  198. public:
  199. static D3DXFNTable smD3DX;
  200. static GFXDevice *createInstance( U32 adapterIndex );
  201. GFXTextureObject* createRenderSurface( U32 width, U32 height, GFXFormat format, U32 mipLevel );
  202. const D3DDISPLAYMODE& getDisplayMode() const { return mDisplayMode; }
  203. /// Constructor
  204. /// @param d3d Direct3D object to instantiate this device with
  205. /// @param index Adapter index since D3D can use multiple graphics adapters
  206. GFXD3D9Device( LPDIRECT3D9 d3d, U32 index );
  207. virtual ~GFXD3D9Device();
  208. // Activate/deactivate
  209. // {
  210. virtual void init( const GFXVideoMode &mode, PlatformWindow *window = NULL ) = 0;
  211. virtual void preDestroy() { Parent::preDestroy(); if(mTextureManager) mTextureManager->kill(); }
  212. GFXAdapterType getAdapterType(){ return Direct3D9; }
  213. U32 getAdaterIndex() const { return mAdapterIndex; }
  214. virtual GFXCubemap *createCubemap();
  215. virtual F32 getPixelShaderVersion() const { return mPixVersion; }
  216. virtual void setPixelShaderVersion( F32 version ){ mPixVersion = version; }
  217. virtual void setShader( GFXShader *shader, bool force = false );
  218. virtual U32 getNumSamplers() const { return mNumSamplers; }
  219. virtual U32 getNumRenderTargets() const { return mNumRenderTargets; }
  220. // }
  221. // Misc rendering control
  222. // {
  223. virtual void clear( U32 flags, ColorI color, F32 z, U32 stencil );
  224. virtual bool beginSceneInternal();
  225. virtual void endSceneInternal();
  226. virtual void setClipRect( const RectI &rect );
  227. virtual const RectI& getClipRect() const { return mClipRect; }
  228. // }
  229. /// @name Render Targets
  230. /// @{
  231. virtual void _updateRenderTargets();
  232. /// @}
  233. // Vertex/Index buffer management
  234. // {
  235. virtual GFXVertexBuffer* allocVertexBuffer( U32 numVerts,
  236. const GFXVertexFormat *vertexFormat,
  237. U32 vertSize,
  238. GFXBufferType bufferType );
  239. virtual GFXPrimitiveBuffer *allocPrimitiveBuffer( U32 numIndices,
  240. U32 numPrimitives,
  241. GFXBufferType bufferType );
  242. virtual void deallocVertexBuffer( GFXD3D9VertexBuffer *vertBuff );
  243. virtual GFXVertexDecl* allocVertexDecl( const GFXVertexFormat *vertexFormat );
  244. virtual void setVertexDecl( const GFXVertexDecl *decl );
  245. virtual void setVertexStream( U32 stream, GFXVertexBuffer *buffer );
  246. virtual void setVertexStreamFrequency( U32 stream, U32 frequency );
  247. // }
  248. virtual U32 getMaxDynamicVerts() { return MAX_DYNAMIC_VERTS; }
  249. virtual U32 getMaxDynamicIndices() { return MAX_DYNAMIC_INDICES; }
  250. // Rendering
  251. // {
  252. virtual void drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount );
  253. // }
  254. virtual LPDIRECT3DDEVICE9 getDevice(){ return mD3DDevice; }
  255. virtual LPDIRECT3D9 getD3D() { return mD3D; }
  256. /// Reset
  257. virtual void reset( D3DPRESENT_PARAMETERS &d3dpp ) = 0;
  258. GFXShaderRef mGenericShader[GS_COUNT];
  259. virtual void setupGenericShaders( GenericShaderType type = GSColor );
  260. // Function only really used on the, however a centralized function for
  261. // destroying resources is probably a good thing -patw
  262. virtual void destroyD3DResource( IDirect3DResource9 *d3dResource ) { SAFE_RELEASE( d3dResource ); };
  263. inline virtual F32 getFillConventionOffset() const { return 0.5f; }
  264. virtual void doParanoidStateCheck();
  265. GFXFence *createFence();
  266. GFXOcclusionQuery* createOcclusionQuery();
  267. // Default multisample parameters
  268. D3DMULTISAMPLE_TYPE getMultisampleType() const { return mMultisampleType; }
  269. DWORD getMultisampleLevel() const { return mMultisampleLevel; }
  270. // Whether or not the Direct3D device was created with Direct3D9Ex support
  271. #if !defined(TORQUE_OS_XENON)
  272. virtual bool isD3D9Ex() { return mD3DEx != NULL; }
  273. #else
  274. virtual bool isD3D9Ex() { return false; }
  275. #endif
  276. // Get the backbuffer, currently only access for WPF support
  277. virtual IDirect3DSurface9* getBackBuffer() { return mDeviceBackbuffer; }
  278. };
  279. #endif // _GFXD3D9DEVICE_H_