D3D11GraphicsImpl.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../../Graphics/ConstantBuffer.h"
  24. #include "../../Graphics/GraphicsDefs.h"
  25. #include "../../Graphics/ShaderProgram.h"
  26. #include "../../Graphics/VertexDeclaration.h"
  27. #include "../../Math/Color.h"
  28. #include <d3d11.h>
  29. #include <dxgi.h>
  30. namespace Atomic
  31. {
  32. #define ATOMIC_SAFE_RELEASE(p) if (p) { ((IUnknown*)p)->Release(); p = 0; }
  33. #define ATOMIC_LOGD3DERROR(msg, hr) ATOMIC_LOGERRORF("%s (HRESULT %x)", msg, (unsigned)hr)
  34. typedef HashMap<Pair<ShaderVariation*, ShaderVariation*>, SharedPtr<ShaderProgram> > ShaderProgramMap;
  35. typedef HashMap<unsigned long long, SharedPtr<VertexDeclaration> > VertexDeclarationMap;
  36. typedef HashMap<unsigned, SharedPtr<ConstantBuffer> > ConstantBufferMap;
  37. /// %Graphics implementation. Holds API-specific objects.
  38. class ATOMIC_API GraphicsImpl
  39. {
  40. friend class Graphics;
  41. public:
  42. /// Construct.
  43. GraphicsImpl();
  44. /// Return Direct3D device.
  45. ID3D11Device* GetDevice() const { return device_; }
  46. /// Return Direct3D immediate device context.
  47. ID3D11DeviceContext* GetDeviceContext() const { return deviceContext_; }
  48. /// Return swapchain.
  49. IDXGISwapChain* GetSwapChain() const { return swapChain_; }
  50. /// Return whether multisampling is supported for a given texture format and sample count.
  51. bool CheckMultiSampleSupport(DXGI_FORMAT format, unsigned sampleCount) const;
  52. /// Return multisample quality level for a given texture format and sample count. The sample count must be supported. On D3D feature level 10.1+, uses the standard level. Below that uses the best quality.
  53. unsigned GetMultiSampleQuality(DXGI_FORMAT format, unsigned sampleCount) const;
  54. private:
  55. /// Graphics device.
  56. ID3D11Device* device_;
  57. /// Immediate device context.
  58. ID3D11DeviceContext* deviceContext_;
  59. /// Swap chain.
  60. IDXGISwapChain* swapChain_;
  61. /// Default (backbuffer) rendertarget view.
  62. ID3D11RenderTargetView* defaultRenderTargetView_;
  63. /// Default depth-stencil texture.
  64. ID3D11Texture2D* defaultDepthTexture_;
  65. /// Default depth-stencil view.
  66. ID3D11DepthStencilView* defaultDepthStencilView_;
  67. /// Current color rendertarget views.
  68. ID3D11RenderTargetView* renderTargetViews_[MAX_RENDERTARGETS];
  69. /// Current depth-stencil view.
  70. ID3D11DepthStencilView* depthStencilView_;
  71. /// Created blend state objects.
  72. HashMap<unsigned, ID3D11BlendState*> blendStates_;
  73. /// Created depth state objects.
  74. HashMap<unsigned, ID3D11DepthStencilState*> depthStates_;
  75. /// Created rasterizer state objects.
  76. HashMap<unsigned, ID3D11RasterizerState*> rasterizerStates_;
  77. /// Intermediate texture for multisampled screenshots and less than whole viewport multisampled resolve, created on demand.
  78. ID3D11Texture2D* resolveTexture_;
  79. /// Bound shader resource views.
  80. ID3D11ShaderResourceView* shaderResourceViews_[MAX_TEXTURE_UNITS];
  81. /// Bound sampler state objects.
  82. ID3D11SamplerState* samplers_[MAX_TEXTURE_UNITS];
  83. /// Bound vertex buffers.
  84. ID3D11Buffer* vertexBuffers_[MAX_VERTEX_STREAMS];
  85. /// Bound constant buffers.
  86. ID3D11Buffer* constantBuffers_[2][MAX_SHADER_PARAMETER_GROUPS];
  87. /// Vertex sizes per buffer.
  88. unsigned vertexSizes_[MAX_VERTEX_STREAMS];
  89. /// Vertex stream offsets per buffer.
  90. unsigned vertexOffsets_[MAX_VERTEX_STREAMS];
  91. /// Rendertargets dirty flag.
  92. bool renderTargetsDirty_;
  93. /// Textures dirty flag.
  94. bool texturesDirty_;
  95. /// Vertex declaration dirty flag.
  96. bool vertexDeclarationDirty_;
  97. /// Blend state dirty flag.
  98. bool blendStateDirty_;
  99. /// Depth state dirty flag.
  100. bool depthStateDirty_;
  101. /// Rasterizer state dirty flag.
  102. bool rasterizerStateDirty_;
  103. /// Scissor rect dirty flag.
  104. bool scissorRectDirty_;
  105. /// Stencil ref dirty flag.
  106. bool stencilRefDirty_;
  107. /// Hash of current blend state.
  108. unsigned blendStateHash_;
  109. /// Hash of current depth state.
  110. unsigned depthStateHash_;
  111. /// Hash of current rasterizer state.
  112. unsigned rasterizerStateHash_;
  113. /// First dirtied texture unit.
  114. unsigned firstDirtyTexture_;
  115. /// Last dirtied texture unit.
  116. unsigned lastDirtyTexture_;
  117. /// First dirtied vertex buffer.
  118. unsigned firstDirtyVB_;
  119. /// Last dirtied vertex buffer.
  120. unsigned lastDirtyVB_;
  121. /// Vertex declarations.
  122. VertexDeclarationMap vertexDeclarations_;
  123. /// Constant buffer search map.
  124. ConstantBufferMap allConstantBuffers_;
  125. /// Currently dirty constant buffers.
  126. PODVector<ConstantBuffer*> dirtyConstantBuffers_;
  127. /// Shader programs.
  128. ShaderProgramMap shaderPrograms_;
  129. /// Shader program in use.
  130. ShaderProgram* shaderProgram_;
  131. };
  132. }