D3D11GraphicsImpl.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../../GraphicsAPI/ConstantBuffer.h"
  5. #include "../../GraphicsAPI/Direct3D11/D3D11ShaderProgram.h"
  6. #include "../../GraphicsAPI/Direct3D11/D3D11VertexDeclaration.h"
  7. #include "../../GraphicsAPI/GraphicsDefs.h"
  8. #include "../../Math/Color.h"
  9. #include <d3d11.h>
  10. #include <dxgi.h>
  11. namespace Urho3D
  12. {
  13. #define URHO3D_SAFE_RELEASE(p) if (p) { ((IUnknown*)p)->Release(); p = 0; }
  14. #define URHO3D_LOGD3DERROR(msg, hr) URHO3D_LOGERRORF("%s (HRESULT %x)", msg, (unsigned)hr)
  15. using ShaderProgramMap_D3D11 = HashMap<Pair<ShaderVariation*, ShaderVariation*>, SharedPtr<ShaderProgram_D3D11>>;
  16. using VertexDeclarationMap_D3D11 = HashMap<hash64, SharedPtr<VertexDeclaration_D3D11>>;
  17. using ConstantBufferMap = HashMap<hash32, SharedPtr<ConstantBuffer>>;
  18. /// %Graphics implementation. Holds API-specific objects.
  19. class URHO3D_API GraphicsImpl_D3D11
  20. {
  21. friend class Graphics;
  22. public:
  23. /// Construct.
  24. GraphicsImpl_D3D11();
  25. /// Return Direct3D device.
  26. ID3D11Device* GetDevice() const { return device_; }
  27. /// Return Direct3D immediate device context.
  28. ID3D11DeviceContext* GetDeviceContext() const { return deviceContext_; }
  29. /// Return swapchain.
  30. IDXGISwapChain* GetSwapChain() const { return swapChain_; }
  31. /// Return whether multisampling is supported for a given texture format and sample count.
  32. bool CheckMultiSampleSupport(DXGI_FORMAT format, unsigned sampleCount) const;
  33. /// 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.
  34. unsigned GetMultiSampleQuality(DXGI_FORMAT format, unsigned sampleCount) const;
  35. private:
  36. /// Graphics device.
  37. ID3D11Device* device_;
  38. /// Immediate device context.
  39. ID3D11DeviceContext* deviceContext_;
  40. /// Swap chain.
  41. IDXGISwapChain* swapChain_;
  42. /// Default (backbuffer) rendertarget view.
  43. ID3D11RenderTargetView* defaultRenderTargetView_;
  44. /// Default depth-stencil texture.
  45. ID3D11Texture2D* defaultDepthTexture_;
  46. /// Default depth-stencil view.
  47. ID3D11DepthStencilView* defaultDepthStencilView_;
  48. /// Current color rendertarget views.
  49. ID3D11RenderTargetView* renderTargetViews_[MAX_RENDERTARGETS];
  50. /// Current depth-stencil view.
  51. ID3D11DepthStencilView* depthStencilView_;
  52. /// Created blend state objects.
  53. HashMap<hash32, ID3D11BlendState*> blendStates_;
  54. /// Created depth state objects.
  55. HashMap<hash32, ID3D11DepthStencilState*> depthStates_;
  56. /// Created rasterizer state objects.
  57. HashMap<hash32, ID3D11RasterizerState*> rasterizerStates_;
  58. /// Intermediate texture for multisampled screenshots and less than whole viewport multisampled resolve, created on demand.
  59. ID3D11Texture2D* resolveTexture_;
  60. /// Bound shader resource views.
  61. ID3D11ShaderResourceView* shaderResourceViews_[MAX_TEXTURE_UNITS];
  62. /// Bound sampler state objects.
  63. ID3D11SamplerState* samplers_[MAX_TEXTURE_UNITS];
  64. /// Bound vertex buffers.
  65. ID3D11Buffer* vertexBuffers_[MAX_VERTEX_STREAMS];
  66. /// Bound constant buffers.
  67. ID3D11Buffer* constantBuffers_[2][MAX_SHADER_PARAMETER_GROUPS];
  68. /// Vertex sizes per buffer.
  69. unsigned vertexSizes_[MAX_VERTEX_STREAMS];
  70. /// Vertex stream offsets per buffer.
  71. unsigned vertexOffsets_[MAX_VERTEX_STREAMS];
  72. /// Rendertargets dirty flag.
  73. bool renderTargetsDirty_;
  74. /// Textures dirty flag.
  75. bool texturesDirty_;
  76. /// Vertex declaration dirty flag.
  77. bool vertexDeclarationDirty_;
  78. /// Blend state dirty flag.
  79. bool blendStateDirty_;
  80. /// Depth state dirty flag.
  81. bool depthStateDirty_;
  82. /// Rasterizer state dirty flag.
  83. bool rasterizerStateDirty_;
  84. /// Scissor rect dirty flag.
  85. bool scissorRectDirty_;
  86. /// Stencil ref dirty flag.
  87. bool stencilRefDirty_;
  88. /// Hash of current blend state.
  89. hash32 blendStateHash_;
  90. /// Hash of current depth state.
  91. hash32 depthStateHash_;
  92. /// Hash of current rasterizer state.
  93. hash32 rasterizerStateHash_;
  94. /// First dirtied texture unit.
  95. unsigned firstDirtyTexture_;
  96. /// Last dirtied texture unit.
  97. unsigned lastDirtyTexture_;
  98. /// First dirtied vertex buffer.
  99. unsigned firstDirtyVB_;
  100. /// Last dirtied vertex buffer.
  101. unsigned lastDirtyVB_;
  102. /// Vertex declarations.
  103. VertexDeclarationMap_D3D11 vertexDeclarations_;
  104. /// Constant buffer search map.
  105. ConstantBufferMap allConstantBuffers_;
  106. /// Currently dirty constant buffers.
  107. Vector<ConstantBuffer*> dirtyConstantBuffers_;
  108. /// Shader programs.
  109. ShaderProgramMap_D3D11 shaderPrograms_;
  110. /// Shader program in use.
  111. ShaderProgram_D3D11* shaderProgram_;
  112. };
  113. }