GteDX11Engine.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.2 (2016/11/13)
7 
8 #pragma once
9 
14 
15 namespace gte
16 {
17 
18 class DX11DrawTarget;
19 class DX11GeometryShader;
20 class DX11GraphicsObject;
21 class DX11PixelShader;
22 class DX11Shader;
23 class DX11Texture2;
24 class DX11VertexShader;
25 
27 {
28 // Interface specific to DX11.
29 public:
30  // Construction and destruction.
31  virtual ~DX11Engine();
32 
33  // Constructors for computing. The first constructor uses the default
34  // adapter and tries for DX11.0 hardware acceleration (the maximum feature
35  // level) without debugging support; it is equivalent to the second
36  // constructor call:
37  // DX11Engine(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0,
38  // D3D_FEATURE_LEVEL_11_0);
39  //
40  DX11Engine(D3D_FEATURE_LEVEL minFeatureLevel = D3D_FEATURE_LEVEL_11_0);
41  DX11Engine(IDXGIAdapter* adapter, D3D_DRIVER_TYPE driverType,
42  HMODULE softwareModule, UINT flags,
43  D3D_FEATURE_LEVEL minFeatureLevel);
44 
45  // Constructors for graphics (automatically get computing capabilities).
46  // The first constructor uses the default adapter and tries for DX11.0
47  // hardware acceleration (the maximum feature level) without debugging
48  // support; it is equivalent to the second constructor call:
49  // DX11Engine(nullptr, handle, xSize, ySize, D3D_DRIVER_TYPE_HARDWARE,
50  // nullptr, 0, D3D_FEATURE_LEVEL_11_0);
51  //
52  DX11Engine(HWND handle, UINT xSize, UINT ySize,
53  D3D_FEATURE_LEVEL minFeatureLevel = D3D_FEATURE_LEVEL_11_0);
54  DX11Engine(IDXGIAdapter* adapter, HWND handle, UINT xSize, UINT ySize,
55  D3D_DRIVER_TYPE driverType, HMODULE softwareModule, UINT flags,
56  D3D_FEATURE_LEVEL minFeatureLevel);
57 
58  // Access to members that correspond to constructor inputs.
59  inline IDXGIAdapter* GetAdapter() const;
60  inline D3D_DRIVER_TYPE GetDriverType() const;
61  inline HMODULE GetSoftwareModule() const;
62  inline UINT GetFlags() const;
63  inline D3D_FEATURE_LEVEL GetMinFeatureLevel() const;
64  inline ID3D11Device* GetDevice() const;
65  inline ID3D11DeviceContext* GetImmediate() const;
66  inline D3D_FEATURE_LEVEL GetFeatureLevel() const;
67 
68  // Support for sharing textures by devices. If the 'texture' is bound to
69  // the 'engine', then it is shared by 'this'. If it is not bound to
70  // 'engine', this function behaves just like 'Bind'. In both cases,
71  // a new DX11Texture2 wrapper is returned that is associated with 'this'.
72  // You may unbind as desired. If the shared texture is updated on one
73  // device, Flush() must be called on that device.
74  DX11GraphicsObject* Share(std::shared_ptr<Texture2> const& texture, DX11Engine* engine);
75 
76  // Mapped copying from CPU to GPU.
77  D3D11_MAPPED_SUBRESOURCE MapForWrite(std::shared_ptr<Resource> const& resource, unsigned int sri);
78  void Unmap(std::shared_ptr<Resource> const& resource, unsigned int sri);
79 
80  // Support for toggling between window and fullscreen modes. The return
81  // value is 'true' iff the operation succeeded.
82  bool IsFullscreen(DXGIOutput const& output) const;
83  bool SetFullscreen(DXGIOutput const& output, bool fullscreen);
84  void ExitFullscreen();
85 
86  // Support for GPU timing. This is a coarse-level measurement system.
87  // Each graphics card manufacturer provides more accurate measurements
88  // and more performance counters than just simple timing.
89  void BeginTimer(DX11PerformanceCounter& counter);
90  void EndTimer(DX11PerformanceCounter& counter);
91 
92 private:
93  // Helpers for construction and destruction.
94  void Initialize(IDXGIAdapter* adapter, D3D_DRIVER_TYPE driverType,
95  HMODULE softwareModule, UINT flags, D3D_FEATURE_LEVEL minFeatureLevel);
96  bool CreateDevice();
97  bool CreateBestMatchingDevice();
98  bool CreateSwapChain(HWND handle, UINT xSize, UINT ySize);
99  bool CreateBackBuffer(UINT xSize, UINT ySize);
100  void CreateDefaultObjects();
101  void DestroyDefaultObjects();
102  bool DestroyDevice();
103  bool DestroySwapChain();
104  bool DestroyBackBuffer();
105 
106  // Support for drawing.
107  uint64_t DrawPrimitive(VertexBuffer const* vbuffer, IndexBuffer const* ibuffer);
108  ID3D11Query* BeginOcclusionQuery();
109  uint64_t EndOcclusionQuery(ID3D11Query* occlusionQuery);
110 
111  // Support for enabling and disabling resources used by shaders.
112  bool EnableShaders(std::shared_ptr<VisualEffect> const& effect,
113  DX11VertexShader*& dxVShader, DX11GeometryShader*& dxGShader, DX11PixelShader*& dxPShader);
114  void DisableShaders(std::shared_ptr<VisualEffect> const& effect,
115  DX11VertexShader* dxVShader, DX11GeometryShader* dxGShader, DX11PixelShader* dxPShader);
116  void Enable(Shader const* shader, DX11Shader* dxShader);
117  void Disable(Shader const* shader, DX11Shader* dxShader);
118  void EnableCBuffers(Shader const* shader, DX11Shader* dxShader);
119  void DisableCBuffers(Shader const* shader, DX11Shader* dxShader);
120  void EnableTBuffers(Shader const* shader, DX11Shader* dxShader);
121  void DisableTBuffers(Shader const* shader, DX11Shader* dxShader);
122  void EnableSBuffers(Shader const* shader, DX11Shader* dxShader);
123  void DisableSBuffers(Shader const* shader, DX11Shader* dxShader);
124  void EnableRBuffers(Shader const* shader, DX11Shader* dxShader);
125  void DisableRBuffers(Shader const* shader, DX11Shader* dxShader);
126  void EnableTextures(Shader const* shader, DX11Shader* dxShader);
127  void DisableTextures(Shader const* shader, DX11Shader* dxShader);
128  void EnableTextureArrays(Shader const* shader, DX11Shader* dxShader);
129  void DisableTextureArrays(Shader const* shader, DX11Shader* dxShader);
130  void EnableSamplers(Shader const* shader, DX11Shader* dxShader);
131  void DisableSamplers(Shader const* shader, DX11Shader* dxShader);
132 
133  // Inputs to the constructors.
134  IDXGIAdapter* mAdapter;
135  D3D_DRIVER_TYPE mDriverType;
138  D3D_FEATURE_LEVEL mMinFeatureLevel;
139 
140  // Objects created by the constructors.
141  ID3D11Device* mDevice;
142  ID3D11DeviceContext* mImmediate;
143  D3D_FEATURE_LEVEL mFeatureLevel;
144 
145  // Objects created by the constructors for graphics engines.
146  IDXGISwapChain* mSwapChain;
147  ID3D11Texture2D* mColorBuffer;
148  ID3D11RenderTargetView* mColorView;
149  ID3D11Texture2D* mDepthStencilBuffer;
150  ID3D11DepthStencilView* mDepthStencilView;
151  D3D11_VIEWPORT mViewport;
152 
153  // Support for draw target enabling and disabling.
154  unsigned int mNumActiveRTs;
155  std::array<ID3D11RenderTargetView*, D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT> mActiveRT;
156  ID3D11DepthStencilView* mActiveDS;
157  D3D11_VIEWPORT mSaveViewport;
158  std::array<ID3D11RenderTargetView*, D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT> mSaveRT;
159  ID3D11DepthStencilView* mSaveDS;
160 
161  // For synchronization (wait for the GPU command buffer to be fully
162  // executed). The query is created on-demand (the first time
163  // WaitForFinish is called).
164  ID3D11Query* mWaitQuery;
165 
166  // Keep track of whether the window is fullscreen or normal mode. As
167  // recommended by MSDN documentation, we do not use swapChainDesc.Windowed
168  // to control this; rather, we create a windowed-mode device and allow the
169  // programmer to use SetFullscreenState to toggle between windowed and
170  // fullscreen.
171  std::map<std::wstring, bool> mFullscreenState;
172 
173 
174 // Overrides from BaseEngine.
175 public:
176  // Viewport management. The measurements are in window coordinates. The
177  // origin of the window is (x,y), the window width is w, and the window
178  // height is h. The depth range for the view volume is [zmin,zmax]. The
179  // DX11 viewport is left-handed with origin the upper-left corner of the
180  // window, the x-axis is directed rightward, the y-axis is directed
181  // downward, and the depth range is a subset of [0,1].
182  virtual void SetViewport(int x, int y, int w, int h) override;
183  virtual void GetViewport(int& x, int& y, int& w, int& h) const override;
184  virtual void SetDepthRange(float zmin, float zmax) override;
185  virtual void GetDepthRange(float& zmin, float& zmax) const override;
186 
187  // Window resizing.
188  virtual bool Resize(unsigned int w, unsigned int h) override;
189 
190  // Support for clearing the color, depth, and stencil back buffers.
191  virtual void ClearColorBuffer() override;
192  virtual void ClearDepthBuffer() override;
193  virtual void ClearStencilBuffer() override;
194  virtual void ClearBuffers() override;
195  virtual void DisplayColorBuffer(unsigned int syncInterval) override;
196 
197  // Global drawing state. The default states are shown in GteBlendState.h,
198  // GteDepthStencil.h, and GteRasterizerState.h.
199  virtual void SetBlendState(std::shared_ptr<BlendState> const& state) override;
200  virtual void SetDepthStencilState(std::shared_ptr<DepthStencilState> const& state) override;
201  virtual void SetRasterizerState(std::shared_ptr<RasterizerState> const& state) override;
202 
203  // Support for drawing to offscreen memory (i.e. not to the back buffer).
204  // The DrawTarget object encapsulates render targets (color information)
205  // and depth-stencil target.
206  virtual void Enable(std::shared_ptr<DrawTarget> const& target) override;
207  virtual void Disable(std::shared_ptr<DrawTarget> const& target) override;
208 
209  // Allow creation of a DX11Texture2 object from known ID3D11* interfaces.
210  DX11Texture2* BindTexture(std::shared_ptr<Texture2> const& texture,
211  ID3D11Texture2D* dxTexture, ID3D11ShaderResourceView* dxSRView);
212 
213  // Support for copying from CPU to GPU via mapped memory.
214  virtual bool Update(std::shared_ptr<Buffer> const& buffer) override;
215  virtual bool Update(std::shared_ptr<TextureSingle> const& texture) override;
216  virtual bool Update(std::shared_ptr<TextureSingle> const& texture, unsigned int level) override;
217  virtual bool Update(std::shared_ptr<TextureArray> const& textureArray) override;
218  virtual bool Update(std::shared_ptr<TextureArray> const& textureArray, unsigned int item, unsigned int level) override;
219 
220  // Support for copying from CPU to GPU via staging memory.
221  virtual bool CopyCpuToGpu(std::shared_ptr<Buffer> const& buffer) override;
222  virtual bool CopyCpuToGpu(std::shared_ptr<TextureSingle> const& texture) override;
223  virtual bool CopyCpuToGpu(std::shared_ptr<TextureSingle> const& texture, unsigned int level) override;
224  virtual bool CopyCpuToGpu(std::shared_ptr<TextureArray> const& textureArray) override;
225  virtual bool CopyCpuToGpu(std::shared_ptr<TextureArray> const& textureArray, unsigned int item, unsigned int level) override;
226 
227  // Support for copying from GPU to CPU via staging memory.
228  virtual bool CopyGpuToCpu(std::shared_ptr<Buffer> const& buffer) override;
229  virtual bool CopyGpuToCpu(std::shared_ptr<TextureSingle> const& texture) override;
230  virtual bool CopyGpuToCpu(std::shared_ptr<TextureSingle> const& texture, unsigned int level) override;
231  virtual bool CopyGpuToCpu(std::shared_ptr<TextureArray> const& textureArray) override;
232  virtual bool CopyGpuToCpu(std::shared_ptr<TextureArray> const& textureArray, unsigned int item, unsigned int level) override;
233 
234  // Support for copying from GPU to GPU directly. TODO: We will improve on
235  // the feature set for such copies later. For now, the restrictions are
236  // that the resources are different, of the same type, have identical
237  // dimensions, and have compatible formats (if of texture type).
238  virtual void CopyGpuToGpu(
239  std::shared_ptr<Buffer> const& buffer0,
240  std::shared_ptr<Buffer> const& buffer1) override;
241 
242  virtual void CopyGpuToGpu(
243  std::shared_ptr<TextureSingle> const& texture0,
244  std::shared_ptr<TextureSingle> const& texture1) override;
245 
246  virtual void CopyGpuToGpu(
247  std::shared_ptr<TextureSingle> const& texture0,
248  std::shared_ptr<TextureSingle> const& texture1,
249  unsigned int level) override;
250 
251  virtual void CopyGpuToGpu(
252  std::shared_ptr<TextureArray> const& textureArray0,
253  std::shared_ptr<TextureArray> const& textureArray1) override;
254 
255  virtual void CopyGpuToGpu(
256  std::shared_ptr<TextureArray> const& textureArray0,
257  std::shared_ptr<TextureArray> const& textureArray1,
258  unsigned int item, unsigned int level) override;
259 
260  // Counted buffer management. GetNumActiveElements stores the result in 'buffer'.
261  virtual bool GetNumActiveElements(std::shared_ptr<StructuredBuffer> const& buffer) override;
262 
263  // Execute the compute program. If you want the CPU to stall to wait for
264  // the results, call WaitForFinish() immediately after Execute(...).
265  // However, you can synchronize CPU and GPU activity by calling
266  // WaitForFinish() at some later time, the goal being not to stall the
267  // CPU before obtaining the GPU results.
268  virtual bool BindProgram(std::shared_ptr<ComputeProgram> const& program) override;
269  virtual void Execute(std::shared_ptr<ComputeProgram> const& program,
270  unsigned int numXGroups, unsigned int numYGroups, unsigned int numZGroups) override;
271 
272  // Have the CPU wait until the GPU finishes its current command buffer.
273  virtual void WaitForFinish() override;
274 
275  // Flush the command buffer.
276  virtual void Flush() override;
277 
278 private:
279  // Support for drawing. If occlusion queries are enabled, the return
280  // value is the number of samples that passed the depth and stencil
281  // tests, effectively the number of pixels drawn. If occlusion queries
282  // are disabled, the function returns 0.
283  virtual uint64_t DrawPrimitive(
284  std::shared_ptr<VertexBuffer> const& vbuffer,
285  std::shared_ptr<IndexBuffer> const& ibuffer,
286  std::shared_ptr<VisualEffect> const& effect) override;
287 };
288 
289 inline IDXGIAdapter* DX11Engine::GetAdapter() const
290 {
291  return mAdapter;
292 }
293 
294 inline D3D_DRIVER_TYPE DX11Engine::GetDriverType() const
295 {
296  return mDriverType;
297 }
298 
299 inline HMODULE DX11Engine::GetSoftwareModule() const
300 {
301  return mSoftwareModule;
302 }
303 
305 {
306  return mFlags;
307 }
308 
309 inline D3D_FEATURE_LEVEL DX11Engine::GetMinFeatureLevel() const
310 {
311  return mMinFeatureLevel;
312 }
313 
314 inline ID3D11Device* DX11Engine::GetDevice() const
315 {
316  return mDevice;
317 }
318 
319 inline ID3D11DeviceContext* DX11Engine::GetImmediate() const
320 {
321  return mImmediate;
322 }
323 
324 inline D3D_FEATURE_LEVEL DX11Engine::GetFeatureLevel() const
325 {
326  return mFeatureLevel;
327 }
328 
329 }
ID3D11DepthStencilView * mDepthStencilView
ID3D11DeviceContext * GetImmediate() const
D3D_DRIVER_TYPE GetDriverType() const
std::array< ID3D11RenderTargetView *, D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT > mActiveRT
GLint level
Definition: glcorearb.h:103
D3D11_VIEWPORT mViewport
GLbitfield GLuint program
Definition: glcorearb.h:1926
ID3D11Device * GetDevice() const
D3D_FEATURE_LEVEL mFeatureLevel
D3D11_VIEWPORT mSaveViewport
D3D_FEATURE_LEVEL GetFeatureLevel() const
GLenum target
Definition: glcorearb.h:1662
ID3D11Texture2D * mDepthStencilBuffer
GLint GLenum GLint x
Definition: glcorearb.h:404
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:852
HMODULE GetSoftwareModule() const
HMODULE mSoftwareModule
IDXGIAdapter * GetAdapter() const
GLuint counter
Definition: glext.h:5361
D3D_DRIVER_TYPE mDriverType
UINT GetFlags() const
GLuint texture
Definition: glcorearb.h:410
ID3D11RenderTargetView * mColorView
GLbitfield flags
Definition: glcorearb.h:1591
IDXGISwapChain * mSwapChain
GLclampd zmax
Definition: glext.h:6450
IDXGIAdapter * mAdapter
ID3D11DeviceContext * mImmediate
std::map< std::wstring, bool > mFullscreenState
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:1997
GLuint shader
Definition: glcorearb.h:780
ID3D11DepthStencilView * mSaveDS
ID3D11DepthStencilView * mActiveDS
ID3D11Texture2D * mColorBuffer
ID3D11Query * mWaitQuery
unsigned int mNumActiveRTs
typedef UINT(WINAPI *PFNWGLGETGPUIDSAMDPROC)(UINT maxCount
std::array< ID3D11RenderTargetView *, D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT > mSaveRT
GLuint buffer
Definition: glcorearb.h:655
GLint y
Definition: glcorearb.h:98
D3D_FEATURE_LEVEL GetMinFeatureLevel() const
#define GTE_IMPEXP
Definition: GTEngineDEF.h:63
ID3D11Device * mDevice
D3D_FEATURE_LEVEL mMinFeatureLevel


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59