gfxD3D9Device.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. GFXShaderRef mGenericShader[GS_COUNT];
  129. GFXShaderConstBufferRef mGenericShaderBuffer[GS_COUNT];
  130. GFXShaderConstHandle *mModelViewProjSC[GS_COUNT];
  131. S32 mCreateFenceType;
  132. LPDIRECT3D9 mD3D; ///< D3D Handle
  133. LPDIRECT3DDEVICE9 mD3DDevice; ///< Handle for D3DDevice
  134. #if !defined(TORQUE_OS_XENON)
  135. LPDIRECT3D9EX mD3DEx; ///< D3D9Ex Handle
  136. LPDIRECT3DDEVICE9EX mD3DDeviceEx; ///< Handle for D3DDevice9Ex
  137. #endif
  138. U32 mAdapterIndex; ///< Adapter index because D3D supports multiple adapters
  139. F32 mPixVersion;
  140. U32 mNumSamplers; ///< Profiled (via caps)
  141. U32 mNumRenderTargets; ///< Profiled (via caps)
  142. D3DMULTISAMPLE_TYPE mMultisampleType;
  143. DWORD mMultisampleLevel;
  144. bool mOcclusionQuerySupported;
  145. /// The current adapter display mode.
  146. D3DDISPLAYMODE mDisplayMode;
  147. /// To manage creating and re-creating of these when device is aquired
  148. void reacquireDefaultPoolResources();
  149. /// To release all resources we control from D3DPOOL_DEFAULT
  150. void releaseDefaultPoolResources();
  151. /// This you will probably never, ever use, but it is used to generate the code for
  152. /// the initStates() function
  153. void regenStates();
  154. virtual GFXD3D9VertexBuffer* findVBPool( const GFXVertexFormat *vertexFormat, U32 numVertsNeeded );
  155. virtual GFXD3D9VertexBuffer* createVBPool( const GFXVertexFormat *vertexFormat, U32 vertSize );
  156. #ifdef TORQUE_DEBUG
  157. /// @name Debug Vertex Buffer information/management
  158. /// @{
  159. ///
  160. U32 mNumAllocatedVertexBuffers; ///< To keep track of how many are allocated and freed
  161. GFXD3D9VertexBuffer *mVBListHead;
  162. void addVertexBuffer( GFXD3D9VertexBuffer *buffer );
  163. void removeVertexBuffer( GFXD3D9VertexBuffer *buffer );
  164. void logVertexBuffers();
  165. /// @}
  166. #endif
  167. // State overrides
  168. // {
  169. ///
  170. virtual void setTextureInternal(U32 textureUnit, const GFXTextureObject* texture);
  171. /// Called by GFXDevice to create a device specific stateblock
  172. virtual GFXStateBlockRef createStateBlockInternal(const GFXStateBlockDesc& desc);
  173. /// Called by GFXDevice to actually set a stateblock.
  174. virtual void setStateBlockInternal(GFXStateBlock* block, bool force);
  175. /// Track the last const buffer we've used. Used to notify new constant buffers that
  176. /// they should send all of their constants up
  177. StrongRefPtr<GFXD3D9ShaderConstBuffer> mCurrentConstBuffer;
  178. /// Called by base GFXDevice to actually set a const buffer
  179. virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer);
  180. // CodeReview - How exactly do we want to deal with this on the Xenon?
  181. // Right now it's just in an #ifndef in gfxD3D9Device.cpp - AlexS 4/11/07
  182. virtual void setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable);
  183. virtual void setLightMaterialInternal(const GFXLightMaterial mat);
  184. virtual void setGlobalAmbientInternal(ColorF color);
  185. virtual void initStates()=0;
  186. // }
  187. // Index buffer management
  188. // {
  189. virtual void _setPrimitiveBuffer( GFXPrimitiveBuffer *buffer );
  190. virtual void drawIndexedPrimitive( GFXPrimitiveType primType,
  191. U32 startVertex,
  192. U32 minIndex,
  193. U32 numVerts,
  194. U32 startIndex,
  195. U32 primitiveCount );
  196. // }
  197. virtual GFXShader* createShader();
  198. void disableShaders(bool force = false);
  199. /// Device helper function
  200. virtual D3DPRESENT_PARAMETERS setupPresentParams( const GFXVideoMode &mode, const HWND &hwnd ) const = 0;
  201. public:
  202. static D3DXFNTable smD3DX;
  203. static GFXDevice *createInstance( U32 adapterIndex );
  204. GFXTextureObject* createRenderSurface( U32 width, U32 height, GFXFormat format, U32 mipLevel );
  205. const D3DDISPLAYMODE& getDisplayMode() const { return mDisplayMode; }
  206. /// Constructor
  207. /// @param d3d Direct3D object to instantiate this device with
  208. /// @param index Adapter index since D3D can use multiple graphics adapters
  209. GFXD3D9Device( LPDIRECT3D9 d3d, U32 index );
  210. virtual ~GFXD3D9Device();
  211. // Activate/deactivate
  212. // {
  213. virtual void init( const GFXVideoMode &mode, PlatformWindow *window = NULL ) = 0;
  214. virtual void preDestroy() { Parent::preDestroy(); if(mTextureManager) mTextureManager->kill(); }
  215. GFXAdapterType getAdapterType(){ return Direct3D9; }
  216. U32 getAdaterIndex() const { return mAdapterIndex; }
  217. virtual GFXCubemap *createCubemap();
  218. virtual F32 getPixelShaderVersion() const { return mPixVersion; }
  219. virtual void setPixelShaderVersion( F32 version ){ mPixVersion = version; }
  220. virtual void setShader( GFXShader *shader, bool force = false );
  221. virtual U32 getNumSamplers() const { return mNumSamplers; }
  222. virtual U32 getNumRenderTargets() const { return mNumRenderTargets; }
  223. // }
  224. // Misc rendering control
  225. // {
  226. virtual void clear( U32 flags, ColorI color, F32 z, U32 stencil );
  227. virtual bool beginSceneInternal();
  228. virtual void endSceneInternal();
  229. virtual void setClipRect( const RectI &rect );
  230. virtual const RectI& getClipRect() const { return mClipRect; }
  231. // }
  232. /// @name Render Targets
  233. /// @{
  234. virtual void _updateRenderTargets();
  235. /// @}
  236. // Vertex/Index buffer management
  237. // {
  238. virtual GFXVertexBuffer* allocVertexBuffer( U32 numVerts,
  239. const GFXVertexFormat *vertexFormat,
  240. U32 vertSize,
  241. GFXBufferType bufferType,
  242. void* data = NULL );
  243. virtual GFXPrimitiveBuffer *allocPrimitiveBuffer( U32 numIndices,
  244. U32 numPrimitives,
  245. GFXBufferType bufferType,
  246. void* data = NULL );
  247. virtual void deallocVertexBuffer( GFXD3D9VertexBuffer *vertBuff );
  248. virtual GFXVertexDecl* allocVertexDecl( const GFXVertexFormat *vertexFormat );
  249. virtual void setVertexDecl( const GFXVertexDecl *decl );
  250. virtual void setVertexStream( U32 stream, GFXVertexBuffer *buffer );
  251. virtual void setVertexStreamFrequency( U32 stream, U32 frequency );
  252. // }
  253. virtual U32 getMaxDynamicVerts() { return MAX_DYNAMIC_VERTS; }
  254. virtual U32 getMaxDynamicIndices() { return MAX_DYNAMIC_INDICES; }
  255. // Rendering
  256. // {
  257. virtual void drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount );
  258. // }
  259. virtual LPDIRECT3DDEVICE9 getDevice(){ return mD3DDevice; }
  260. virtual LPDIRECT3D9 getD3D() { return mD3D; }
  261. /// Reset
  262. virtual void reset( D3DPRESENT_PARAMETERS &d3dpp ) = 0;
  263. virtual void setupGenericShaders( GenericShaderType type = GSColor );
  264. // Function only really used on the, however a centralized function for
  265. // destroying resources is probably a good thing -patw
  266. virtual void destroyD3DResource( IDirect3DResource9 *d3dResource ) { SAFE_RELEASE( d3dResource ); };
  267. inline virtual F32 getFillConventionOffset() const { return 0.5f; }
  268. virtual void doParanoidStateCheck();
  269. GFXFence *createFence();
  270. GFXOcclusionQuery* createOcclusionQuery();
  271. // Default multisample parameters
  272. D3DMULTISAMPLE_TYPE getMultisampleType() const { return mMultisampleType; }
  273. DWORD getMultisampleLevel() const { return mMultisampleLevel; }
  274. // Whether or not the Direct3D device was created with Direct3D9Ex support
  275. #if !defined(TORQUE_OS_XENON)
  276. virtual bool isD3D9Ex() { return mD3DEx != NULL; }
  277. #else
  278. virtual bool isD3D9Ex() { return false; }
  279. #endif
  280. // Get the backbuffer, currently only access for WPF support
  281. virtual IDirect3DSurface9* getBackBuffer() { return mDeviceBackbuffer; }
  282. };
  283. #endif // _GFXD3D9DEVICE_H_