D3D11GraphicsImpl.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // Copyright (c) 2008-2016 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. private:
  51. /// Graphics device.
  52. ID3D11Device* device_;
  53. /// Immediate device context.
  54. ID3D11DeviceContext* deviceContext_;
  55. /// Swap chain.
  56. IDXGISwapChain* swapChain_;
  57. /// Default (backbuffer) rendertarget view.
  58. ID3D11RenderTargetView* defaultRenderTargetView_;
  59. /// Default depth-stencil texture.
  60. ID3D11Texture2D* defaultDepthTexture_;
  61. /// Default depth-stencil view.
  62. ID3D11DepthStencilView* defaultDepthStencilView_;
  63. /// Current color rendertarget views.
  64. ID3D11RenderTargetView* renderTargetViews_[MAX_RENDERTARGETS];
  65. /// Current depth-stencil view.
  66. ID3D11DepthStencilView* depthStencilView_;
  67. /// Created blend state objects.
  68. HashMap<unsigned, ID3D11BlendState*> blendStates_;
  69. /// Created depth state objects.
  70. HashMap<unsigned, ID3D11DepthStencilState*> depthStates_;
  71. /// Created rasterizer state objects.
  72. HashMap<unsigned, ID3D11RasterizerState*> rasterizerStates_;
  73. /// Intermediate texture for multisampled screenshots and less than whole viewport multisampled resolve, created on demand.
  74. ID3D11Texture2D* resolveTexture_;
  75. /// Bound shader resource views.
  76. ID3D11ShaderResourceView* shaderResourceViews_[MAX_TEXTURE_UNITS];
  77. /// Bound sampler state objects.
  78. ID3D11SamplerState* samplers_[MAX_TEXTURE_UNITS];
  79. /// Bound vertex buffers.
  80. ID3D11Buffer* vertexBuffers_[MAX_VERTEX_STREAMS];
  81. /// Bound constant buffers.
  82. ID3D11Buffer* constantBuffers_[2][MAX_SHADER_PARAMETER_GROUPS];
  83. /// Vertex sizes per buffer.
  84. unsigned vertexSizes_[MAX_VERTEX_STREAMS];
  85. /// Vertex stream offsets per buffer.
  86. unsigned vertexOffsets_[MAX_VERTEX_STREAMS];
  87. /// Rendertargets dirty flag.
  88. bool renderTargetsDirty_;
  89. /// Textures dirty flag.
  90. bool texturesDirty_;
  91. /// Vertex declaration dirty flag.
  92. bool vertexDeclarationDirty_;
  93. /// Blend state dirty flag.
  94. bool blendStateDirty_;
  95. /// Depth state dirty flag.
  96. bool depthStateDirty_;
  97. /// Rasterizer state dirty flag.
  98. bool rasterizerStateDirty_;
  99. /// Scissor rect dirty flag.
  100. bool scissorRectDirty_;
  101. /// Stencil ref dirty flag.
  102. bool stencilRefDirty_;
  103. /// Hash of current blend state.
  104. unsigned blendStateHash_;
  105. /// Hash of current depth state.
  106. unsigned depthStateHash_;
  107. /// Hash of current rasterizer state.
  108. unsigned rasterizerStateHash_;
  109. /// First dirtied texture unit.
  110. unsigned firstDirtyTexture_;
  111. /// Last dirtied texture unit.
  112. unsigned lastDirtyTexture_;
  113. /// First dirtied vertex buffer.
  114. unsigned firstDirtyVB_;
  115. /// Last dirtied vertex buffer.
  116. unsigned lastDirtyVB_;
  117. /// Vertex declarations.
  118. VertexDeclarationMap vertexDeclarations_;
  119. /// Constant buffer search map.
  120. ConstantBufferMap allConstantBuffers_;
  121. /// Currently dirty constant buffers.
  122. PODVector<ConstantBuffer*> dirtyConstantBuffers_;
  123. /// Shader programs.
  124. ShaderProgramMap shaderPrograms_;
  125. /// Shader program in use.
  126. ShaderProgram* shaderProgram_;
  127. };
  128. }