gfxD3D11Device.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2015 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 _GFXD3D11DEVICE_H_
  23. #define _GFXD3D11DEVICE_H_
  24. #include <d3d11_1.h>
  25. #include "platform/tmm_off.h"
  26. #include "platformWin32/platformWin32.h"
  27. #include "gfx/D3D11/gfxD3D11Shader.h"
  28. #include "gfx/D3D11/gfxD3D11StateBlock.h"
  29. #include "gfx/D3D11/gfxD3D11TextureManager.h"
  30. #include "gfx/D3D11/gfxD3D11Cubemap.h"
  31. #include "gfx/D3D11/gfxD3D11PrimitiveBuffer.h"
  32. #include "gfx/gfxInit.h"
  33. #include "gfx/gfxResource.h"
  34. #include "platform/tmm_on.h"
  35. #define D3D11 static_cast<GFXD3D11Device*>(GFX)
  36. #define D3D11DEVICE D3D11->getDevice()
  37. #define D3D11DEVICECONTEXT D3D11->getDeviceContext()
  38. class PlatformWindow;
  39. class GFXD3D11ShaderConstBuffer;
  40. class OculusVRHMDDevice;
  41. class D3D11OculusTexture;
  42. //------------------------------------------------------------------------------
  43. class GFXD3D11Device : public GFXDevice
  44. {
  45. public:
  46. typedef Map<U32, ID3D11SamplerState*> SamplerMap;
  47. private:
  48. friend class GFXResource;
  49. friend class GFXD3D11PrimitiveBuffer;
  50. friend class GFXD3D11VertexBuffer;
  51. friend class GFXD3D11TextureObject;
  52. friend class GFXD3D11TextureTarget;
  53. friend class GFXD3D11WindowTarget;
  54. friend class OculusVRHMDDevice;
  55. friend class D3D11OculusTexture;
  56. virtual GFXFormat selectSupportedFormat(GFXTextureProfile *profile,
  57. const Vector<GFXFormat> &formats, bool texture, bool mustblend, bool mustfilter);
  58. virtual void enumerateVideoModes();
  59. virtual GFXWindowTarget *allocWindowTarget(PlatformWindow *window);
  60. virtual GFXTextureTarget *allocRenderToTextureTarget(bool genMips = true);
  61. virtual void enterDebugEvent(ColorI color, const char *name);
  62. virtual void leaveDebugEvent();
  63. virtual void setDebugMarker(ColorI color, const char *name);
  64. protected:
  65. class D3D11VertexDecl : public GFXVertexDecl
  66. {
  67. public:
  68. D3D11VertexDecl() :decl(NULL) {}
  69. virtual ~D3D11VertexDecl()
  70. {
  71. SAFE_RELEASE( decl );
  72. }
  73. ID3D11InputLayout *decl;
  74. };
  75. virtual void initStates() { };
  76. static GFXAdapter::CreateDeviceInstanceDelegate mCreateDeviceInstance;
  77. MatrixF mTempMatrix; ///< Temporary matrix, no assurances on value at all
  78. RectI mClipRect;
  79. typedef StrongRefPtr<GFXD3D11VertexBuffer> RPGDVB;
  80. Vector<RPGDVB> mVolatileVBList;
  81. /// Used to lookup a vertex declaration for the vertex format.
  82. /// @see allocVertexDecl
  83. typedef Map<String,D3D11VertexDecl*> VertexDeclMap;
  84. VertexDeclMap mVertexDecls;
  85. /// Used to lookup sampler state for a given hash key
  86. SamplerMap mSamplersMap;
  87. ID3D11RenderTargetView* mDeviceBackBufferView;
  88. ID3D11DepthStencilView* mDeviceDepthStencilView;
  89. ID3D11Texture2D *mDeviceBackbuffer;
  90. ID3D11Texture2D *mDeviceDepthStencil;
  91. /// The stream 0 vertex buffer used for volatile VB offseting.
  92. GFXD3D11VertexBuffer *mVolatileVB;
  93. //-----------------------------------------------------------------------
  94. StrongRefPtr<GFXD3D11PrimitiveBuffer> mDynamicPB;
  95. GFXD3D11PrimitiveBuffer *mCurrentPB;
  96. ID3D11VertexShader *mLastVertShader;
  97. ID3D11PixelShader *mLastPixShader;
  98. S32 mCreateFenceType;
  99. IDXGISwapChain *mSwapChain;
  100. ID3D11Device* mD3DDevice;
  101. ID3D11DeviceContext* mD3DDeviceContext;
  102. GFXShaderRef mGenericShader[GS_COUNT];
  103. GFXShaderConstBufferRef mGenericShaderBuffer[GS_COUNT];
  104. GFXShaderConstHandle *mModelViewProjSC[GS_COUNT];
  105. U32 mAdapterIndex;
  106. F32 mPixVersion;
  107. D3D_FEATURE_LEVEL mFeatureLevel;
  108. // Shader Model targers
  109. String mVertexShaderTarget;
  110. String mPixelShaderTarget;
  111. // String for use with shader macros in the form of shader model version * 10
  112. String mShaderModel;
  113. bool mDebugLayers;
  114. DXGI_SAMPLE_DESC mMultisampleDesc;
  115. bool mOcclusionQuerySupported;
  116. U32 mDrawInstancesCount;
  117. /// To manage creating and re-creating of these when device is aquired
  118. void reacquireDefaultPoolResources();
  119. /// To release all resources we control from D3DPOOL_DEFAULT
  120. void releaseDefaultPoolResources();
  121. virtual GFXD3D11VertexBuffer* findVBPool( const GFXVertexFormat *vertexFormat, U32 numVertsNeeded );
  122. virtual GFXD3D11VertexBuffer* createVBPool( const GFXVertexFormat *vertexFormat, U32 vertSize );
  123. // State overrides
  124. // {
  125. ///
  126. virtual void setTextureInternal(U32 textureUnit, const GFXTextureObject* texture);
  127. /// Called by GFXDevice to create a device specific stateblock
  128. virtual GFXStateBlockRef createStateBlockInternal(const GFXStateBlockDesc& desc);
  129. /// Called by GFXDevice to actually set a stateblock.
  130. virtual void setStateBlockInternal(GFXStateBlock* block, bool force);
  131. /// Track the last const buffer we've used. Used to notify new constant buffers that
  132. /// they should send all of their constants up
  133. StrongRefPtr<GFXD3D11ShaderConstBuffer> mCurrentConstBuffer;
  134. /// Called by base GFXDevice to actually set a const buffer
  135. virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer);
  136. // }
  137. // Index buffer management
  138. // {
  139. virtual void _setPrimitiveBuffer( GFXPrimitiveBuffer *buffer );
  140. virtual void drawIndexedPrimitive( GFXPrimitiveType primType,
  141. U32 startVertex,
  142. U32 minIndex,
  143. U32 numVerts,
  144. U32 startIndex,
  145. U32 primitiveCount );
  146. // }
  147. virtual GFXShader* createShader();
  148. /// Device helper function
  149. virtual DXGI_SWAP_CHAIN_DESC setupPresentParams( const GFXVideoMode &mode, const HWND &hwnd );
  150. String _createTempShaderInternal(const GFXVertexFormat *vertexFormat);
  151. // Supress any debug layer messages we don't want to see
  152. void _suppressDebugMessages();
  153. public:
  154. static GFXDevice *createInstance( U32 adapterIndex );
  155. static void enumerateAdapters( Vector<GFXAdapter*> &adapterList );
  156. ID3D11DepthStencilView* getDepthStencilView() { return mDeviceDepthStencilView; }
  157. ID3D11RenderTargetView* getRenderTargetView() { return mDeviceBackBufferView; }
  158. ID3D11Texture2D* getBackBufferTexture() { return mDeviceBackbuffer; }
  159. /// Constructor
  160. /// @param d3d Direct3D object to instantiate this device with
  161. /// @param index Adapter index since D3D can use multiple graphics adapters
  162. GFXD3D11Device( U32 index );
  163. virtual ~GFXD3D11Device();
  164. // Activate/deactivate
  165. // {
  166. virtual void init( const GFXVideoMode &mode, PlatformWindow *window = NULL );
  167. virtual void preDestroy() { GFXDevice::preDestroy(); if(mTextureManager) mTextureManager->kill(); }
  168. GFXAdapterType getAdapterType(){ return Direct3D11; }
  169. U32 getAdaterIndex() const { return mAdapterIndex; }
  170. virtual GFXCubemap *createCubemap();
  171. virtual GFXCubemapArray *createCubemapArray();
  172. virtual GFXTextureArray* createTextureArray();
  173. virtual F32 getPixelShaderVersion() const { return mPixVersion; }
  174. virtual void setPixelShaderVersion( F32 version ){ mPixVersion = version;}
  175. virtual void setShader(GFXShader *shader, bool force = false);
  176. virtual U32 getNumSamplers() const { return 16; }
  177. virtual U32 getNumRenderTargets() const { return 8; }
  178. // }
  179. // Copy methods
  180. // {
  181. virtual void copyResource(GFXTextureObject *pDst, GFXCubemap *pSrc, const U32 face);
  182. // }
  183. // Misc rendering control
  184. // {
  185. virtual void clear( U32 flags, const LinearColorF& color, F32 z, U32 stencil );
  186. virtual void clearColorAttachment(const U32 attachment, const LinearColorF& color);
  187. virtual bool beginSceneInternal();
  188. virtual void endSceneInternal();
  189. virtual void setClipRect( const RectI &rect );
  190. virtual const RectI& getClipRect() const { return mClipRect; }
  191. // }
  192. /// @name Render Targets
  193. /// @{
  194. virtual void _updateRenderTargets();
  195. /// @}
  196. // Vertex/Index buffer management
  197. // {
  198. virtual GFXVertexBuffer* allocVertexBuffer( U32 numVerts,
  199. const GFXVertexFormat *vertexFormat,
  200. U32 vertSize,
  201. GFXBufferType bufferType,
  202. void* data = NULL);
  203. virtual GFXPrimitiveBuffer *allocPrimitiveBuffer( U32 numIndices,
  204. U32 numPrimitives,
  205. GFXBufferType bufferType,
  206. void* data = NULL);
  207. virtual GFXVertexDecl* allocVertexDecl( const GFXVertexFormat *vertexFormat );
  208. virtual void setVertexDecl( const GFXVertexDecl *decl );
  209. virtual void setVertexStream( U32 stream, GFXVertexBuffer *buffer );
  210. virtual void setVertexStreamFrequency( U32 stream, U32 frequency );
  211. // }
  212. virtual U32 getMaxDynamicVerts() { return GFX_MAX_DYNAMIC_VERTS; }
  213. virtual U32 getMaxDynamicIndices() { return GFX_MAX_DYNAMIC_INDICES; }
  214. inline U32 primCountToIndexCount(GFXPrimitiveType primType, U32 primitiveCount);
  215. // Rendering
  216. // {
  217. virtual void drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount );
  218. // }
  219. ID3D11DeviceContext* getDeviceContext(){ return mD3DDeviceContext; }
  220. ID3D11Device* getDevice(){ return mD3DDevice; }
  221. //IDXGISwapChain* getSwapChain() { return mSwapChain; }
  222. /// Reset
  223. //void reset( DXGI_SWAP_CHAIN_DESC &d3dpp );
  224. void beginReset();
  225. void endReset(GFXD3D11WindowTarget* windowTarget);
  226. virtual void setupGenericShaders( GenericShaderType type = GSColor );
  227. inline virtual F32 getFillConventionOffset() const { return 0.0f; }
  228. virtual void doParanoidStateCheck() {};
  229. GFXFence *createFence();
  230. GFXOcclusionQuery* createOcclusionQuery();
  231. // Default multisample parameters
  232. DXGI_SAMPLE_DESC getMultisampleType() const { return mMultisampleDesc; }
  233. // Get feature level this gfx device supports
  234. D3D_FEATURE_LEVEL getFeatureLevel() const { return mFeatureLevel; }
  235. // Shader Model targers
  236. const String &getVertexShaderTarget() const { return mVertexShaderTarget; }
  237. const String &getPixelShaderTarget() const { return mPixelShaderTarget; }
  238. const String &getShaderModel() const { return mShaderModel; }
  239. // grab the sampler map
  240. const SamplerMap &getSamplersMap() const { return mSamplersMap; }
  241. SamplerMap &getSamplersMap(){ return mSamplersMap; }
  242. const char* interpretDebugResult(long result);
  243. };
  244. #endif