gfxD3D11Device.h 12 KB

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