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 
23 #pragma once
24 
25 #include "../../Graphics/ConstantBuffer.h"
26 #include "../../Graphics/GraphicsDefs.h"
27 #include "../../Graphics/ShaderProgram.h"
28 #include "../../Graphics/VertexDeclaration.h"
29 #include "../../Math/Color.h"
30 
31 #include <d3d11.h>
32 #include <dxgi.h>
33 
34 namespace Urho3D
35 {
36 
37 #define URHO3D_SAFE_RELEASE(p) if (p) { ((IUnknown*)p)->Release();  p = 0; }
38 
39 #define URHO3D_LOGD3DERROR(msg, hr) URHO3D_LOGERRORF("%s (HRESULT %x)", msg, (unsigned)hr)
40 
41 typedef HashMap<Pair<ShaderVariation*, ShaderVariation*>, SharedPtr<ShaderProgram> > ShaderProgramMap;
42 typedef HashMap<unsigned long long, SharedPtr<VertexDeclaration> > VertexDeclarationMap;
43 typedef HashMap<unsigned, SharedPtr<ConstantBuffer> > ConstantBufferMap;
44 
45 /// %Graphics implementation. Holds API-specific objects.
46 class URHO3D_API GraphicsImpl
47 {
48     friend class Graphics;
49 
50 public:
51     /// Construct.
52     GraphicsImpl();
53 
54     /// Return Direct3D device.
GetDevice()55     ID3D11Device* GetDevice() const { return device_; }
56 
57     /// Return Direct3D immediate device context.
GetDeviceContext()58     ID3D11DeviceContext* GetDeviceContext() const { return deviceContext_; }
59 
60     /// Return swapchain.
GetSwapChain()61     IDXGISwapChain* GetSwapChain() const { return swapChain_; }
62 
63     /// Return whether multisampling is supported for a given texture format and sample count.
64     bool CheckMultiSampleSupport(DXGI_FORMAT format, unsigned sampleCount) const;
65 
66     /// 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.
67     unsigned GetMultiSampleQuality(DXGI_FORMAT format, unsigned sampleCount) const;
68 
69 private:
70     /// Graphics device.
71     ID3D11Device* device_;
72     /// Immediate device context.
73     ID3D11DeviceContext* deviceContext_;
74     /// Swap chain.
75     IDXGISwapChain* swapChain_;
76     /// Default (backbuffer) rendertarget view.
77     ID3D11RenderTargetView* defaultRenderTargetView_;
78     /// Default depth-stencil texture.
79     ID3D11Texture2D* defaultDepthTexture_;
80     /// Default depth-stencil view.
81     ID3D11DepthStencilView* defaultDepthStencilView_;
82     /// Current color rendertarget views.
83     ID3D11RenderTargetView* renderTargetViews_[MAX_RENDERTARGETS];
84     /// Current depth-stencil view.
85     ID3D11DepthStencilView* depthStencilView_;
86     /// Created blend state objects.
87     HashMap<unsigned, ID3D11BlendState*> blendStates_;
88     /// Created depth state objects.
89     HashMap<unsigned, ID3D11DepthStencilState*> depthStates_;
90     /// Created rasterizer state objects.
91     HashMap<unsigned, ID3D11RasterizerState*> rasterizerStates_;
92     /// Intermediate texture for multisampled screenshots and less than whole viewport multisampled resolve, created on demand.
93     ID3D11Texture2D* resolveTexture_;
94     /// Bound shader resource views.
95     ID3D11ShaderResourceView* shaderResourceViews_[MAX_TEXTURE_UNITS];
96     /// Bound sampler state objects.
97     ID3D11SamplerState* samplers_[MAX_TEXTURE_UNITS];
98     /// Bound vertex buffers.
99     ID3D11Buffer* vertexBuffers_[MAX_VERTEX_STREAMS];
100     /// Bound constant buffers.
101     ID3D11Buffer* constantBuffers_[2][MAX_SHADER_PARAMETER_GROUPS];
102     /// Vertex sizes per buffer.
103     unsigned vertexSizes_[MAX_VERTEX_STREAMS];
104     /// Vertex stream offsets per buffer.
105     unsigned vertexOffsets_[MAX_VERTEX_STREAMS];
106     /// Rendertargets dirty flag.
107     bool renderTargetsDirty_;
108     /// Textures dirty flag.
109     bool texturesDirty_;
110     /// Vertex declaration dirty flag.
111     bool vertexDeclarationDirty_;
112     /// Blend state dirty flag.
113     bool blendStateDirty_;
114     /// Depth state dirty flag.
115     bool depthStateDirty_;
116     /// Rasterizer state dirty flag.
117     bool rasterizerStateDirty_;
118     /// Scissor rect dirty flag.
119     bool scissorRectDirty_;
120     /// Stencil ref dirty flag.
121     bool stencilRefDirty_;
122     /// Hash of current blend state.
123     unsigned blendStateHash_;
124     /// Hash of current depth state.
125     unsigned depthStateHash_;
126     /// Hash of current rasterizer state.
127     unsigned rasterizerStateHash_;
128     /// First dirtied texture unit.
129     unsigned firstDirtyTexture_;
130     /// Last dirtied texture unit.
131     unsigned lastDirtyTexture_;
132     /// First dirtied vertex buffer.
133     unsigned firstDirtyVB_;
134     /// Last dirtied vertex buffer.
135     unsigned lastDirtyVB_;
136     /// Vertex declarations.
137     VertexDeclarationMap vertexDeclarations_;
138     /// Constant buffer search map.
139     ConstantBufferMap allConstantBuffers_;
140     /// Currently dirty constant buffers.
141     PODVector<ConstantBuffer*> dirtyConstantBuffers_;
142     /// Shader programs.
143     ShaderProgramMap shaderPrograms_;
144     /// Shader program in use.
145     ShaderProgram* shaderProgram_;
146 };
147 
148 }
149