GteGraphicsEngine.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.0 (2016/06/19)
7 
8 #pragma once
9 
11 #include <Graphics/GteBaseEngine.h>
12 #include <Graphics/GteBlendState.h>
16 #include <Graphics/GteGEObject.h>
20 #include <Graphics/GteVisual.h>
21 
22 // Allow names to be assigned to graphics API-specific objects for debugging.
23 #define GTE_GRAPHICS_USE_NAMED_OBJECTS
24 
25 namespace gte
26 {
27 
29 {
30 public:
31  // Abstract base class.
32  virtual ~GraphicsEngine();
33 
34  // Support for clearing the color, depth, and stencil back buffers.
35  virtual void ClearColorBuffer() = 0;
36  virtual void ClearDepthBuffer() = 0;
37  virtual void ClearStencilBuffer() = 0;
38  virtual void ClearBuffers() = 0;
39 
40  // Support for bitmapped fonts used in text rendering. The default font
41  // is Arial (height 18, no italics, no bold).
42  virtual void SetFont(std::shared_ptr<Font> const& font) override;
43 
44  // Support for drawing. If occlusion queries are enabled, the return
45  // values are the number of samples that passed the depth and stencil
46  // tests, effectively the number of pixels drawn. If occlusion queries
47  // are disabled, the functions return 0.
48 
49  // Draw geometric primitives.
50  uint64_t Draw(Visual* visual);
51  uint64_t Draw(std::vector<Visual*> const& visuals);
52  uint64_t Draw(std::shared_ptr<Visual> const& visual);
53  uint64_t Draw(std::vector<std::shared_ptr<Visual>> const& visuals);
54 
55  // Draw 2D text.
56  uint64_t Draw(int x, int y, std::array<float, 4> const& color, std::string const& message);
57 
58  // Draw a 2D rectangular overlay. This is useful for adding buttons,
59  // controls, thumbnails, and other GUI objects to an application window.
60  virtual uint64_t Draw(std::shared_ptr<OverlayEffect> const& overlay) override;
61 
62  // Support for occlusion queries. When enabled, Draw functions return the
63  // number of samples that passed the depth and stencil tests, effectively
64  // the number of pixels drawn. The default value is 'false'.
65  inline void AllowOcclusionQuery(bool allow);
66 
67  // Support for drawing to offscreen memory (i.e. not to the back buffer).
68  // The DrawTarget object encapsulates render targets (color information)
69  // and depth-stencil target.
70  virtual void Enable(std::shared_ptr<DrawTarget> const& target) = 0;
71  virtual void Disable(std::shared_ptr<DrawTarget> const& target) = 0;
72 
73  // Graphics object management. The Bind function creates a graphics
74  // API-specific object that corresponds to the input GTEngine object.
75  // GraphicsEngine manages this bridge mapping internally. The Unbind
76  // function destroys the graphics API-specific object. These may be
77  // called explicitly, but the engine is designed to create on demand
78  // and to destroy on device destruction.
79  GEObject* Bind(std::shared_ptr<GraphicsObject> const& object);
80  virtual bool BindProgram(std::shared_ptr<ComputeProgram> const& program) = 0;
81  GEDrawTarget* Bind(std::shared_ptr<DrawTarget> const& target);
82  GEObject* Get(std::shared_ptr<GraphicsObject> const& object) const;
83  GEDrawTarget* Get(std::shared_ptr<DrawTarget> const& target) const;
84  inline bool Unbind(std::shared_ptr<GraphicsObject> const& object);
85  inline bool Unbind(std::shared_ptr<DrawTarget> const& target);
86  void GetTotalAllocation(size_t& numBytes, size_t& numObjects) const;
87 
88  // Support for copying from CPU to GPU via mapped memory.
89  virtual bool Update(std::shared_ptr<Buffer> const& buffer) = 0;
90  virtual bool Update(std::shared_ptr<TextureSingle> const& texture) = 0;
91  virtual bool Update(std::shared_ptr<TextureSingle> const& texture, unsigned int level) = 0;
92  virtual bool Update(std::shared_ptr<TextureArray> const& textureArray) = 0;
93  virtual bool Update(std::shared_ptr<TextureArray> const& textureArray, unsigned int item, unsigned int level) = 0;
94 
95  // Support for copying from CPU to GPU via staging memory.
96  virtual bool CopyCpuToGpu(std::shared_ptr<Buffer> const& buffer) = 0;
97  virtual bool CopyCpuToGpu(std::shared_ptr<TextureSingle> const& texture) = 0;
98  virtual bool CopyCpuToGpu(std::shared_ptr<TextureSingle> const& texture, unsigned int level) = 0;
99  virtual bool CopyCpuToGpu(std::shared_ptr<TextureArray> const& textureArray) = 0;
100  virtual bool CopyCpuToGpu(std::shared_ptr<TextureArray> const& textureArray, unsigned int item, unsigned int level) = 0;
101 
102  // Support for copying from GPU to CPU via staging memory.
103  virtual bool CopyGpuToCpu(std::shared_ptr<Buffer> const& buffer) = 0;
104  virtual bool CopyGpuToCpu(std::shared_ptr<TextureSingle> const& texture) = 0;
105  virtual bool CopyGpuToCpu(std::shared_ptr<TextureSingle> const& texture, unsigned int level) = 0;
106  virtual bool CopyGpuToCpu(std::shared_ptr<TextureArray> const& textureArray) = 0;
107  virtual bool CopyGpuToCpu(std::shared_ptr<TextureArray> const& textureArray, unsigned int item, unsigned int level) = 0;
108 
109  // Support for copying from GPU to GPU directly. TODO: We will improve on
110  // the feature set for such copies later. For now, the restrictions are
111  // that the resources are different, of the same type, have identical
112  // dimensions, and have compatible formats (if of texture type).
113  virtual void CopyGpuToGpu(
114  std::shared_ptr<Buffer> const& buffer0,
115  std::shared_ptr<Buffer> const& buffer1) = 0;
116 
117  virtual void CopyGpuToGpu(
118  std::shared_ptr<TextureSingle> const& texture0,
119  std::shared_ptr<TextureSingle> const& texture1) = 0;
120 
121  virtual void CopyGpuToGpu(
122  std::shared_ptr<TextureSingle> const& texture0,
123  std::shared_ptr<TextureSingle> const& texture1,
124  unsigned int level) = 0;
125 
126  virtual void CopyGpuToGpu(
127  std::shared_ptr<TextureArray> const& textureArray0,
128  std::shared_ptr<TextureArray> const& textureArray1) = 0;
129 
130  virtual void CopyGpuToGpu(
131  std::shared_ptr<TextureArray> const& textureArray0,
132  std::shared_ptr<TextureArray> const& textureArray1,
133  unsigned int item, unsigned int level) = 0;
134 
135  // Counted buffer management. GetNumActiveElements stores the result in 'buffer'.
136  virtual bool GetNumActiveElements(std::shared_ptr<StructuredBuffer> const& buffer) = 0;
137 
138  // Execute the compute program. If you want the CPU to stall to wait for
139  // the results, call WaitForFinish() immediately after Execute(...).
140  // However, you can synchronize CPU and GPU activity by calling
141  // WaitForFinish() at some later time, the goal being not to stall the
142  // CPU before obtaining the GPU results.
143  virtual void Execute(std::shared_ptr<ComputeProgram> const& program,
144  unsigned int numXGroups, unsigned int numYGroups, unsigned int numZGroups) = 0;
145 
146  // Have the CPU wait until the GPU finishes its current command buffer.
147  virtual void WaitForFinish() = 0;
148 
149  // Flush the command buffer.
150  virtual void Flush() = 0;
151 
152  // Set the warning to 'true' if you want the DX11Engine destructor to
153  // report that the bridge maps are nonempty. If they are, the application
154  // did not destroy GraphicsObject items before the engine was destroyed.
155  // The default values is 'true'.
156  inline void WarnOnNonemptyBridges(bool warn);
157 
158 protected:
159  // Abstract base class. Graphics engine objects may not be copied.
160  GraphicsEngine();
161  GraphicsEngine(GraphicsEngine const&) = delete;
162  GraphicsEngine& operator=(GraphicsEngine const&) = delete;
163 
164  // Helper for destruction.
165  virtual void DestroyDefaultGlobalState();
166 
167  // Support for drawing. If occlusion queries are enabled, the return
168  // values are the number of samples that passed the depth and stencil
169  // tests, effectively the number of pixels drawn. If occlusion queries
170  // are disabled, the functions return 0.
171  virtual uint64_t DrawPrimitive(
172  std::shared_ptr<VertexBuffer> const& vbuffer,
173  std::shared_ptr<IndexBuffer> const& ibuffer,
174  std::shared_ptr<VisualEffect> const& effect) = 0;
175 
176  // Support for GOListener::OnDestroy and DTListener::OnDestroy, because
177  // they are passed raw pointers from resource destructors. These are
178  // also used by the Unbind calls whose inputs are std::shared_ptr<T>.
179  bool Unbind(GraphicsObject const* object);
180  bool Unbind(DrawTarget const* target);
181 
182 
183  // Bridge pattern to create graphics API-specific objects that correspond
184  // to front-end objects. The Bind, Get, and Unbind operations act on
185  // these maps.
188  std::unique_ptr<GEInputLayoutManager> mILMap;
189 
190  // Creation functions for adding objects to the bridges. The function
191  // pointers are assigned during construction.
192  typedef std::shared_ptr<GEObject>(*CreateGEObject)(void*, GraphicsObject const*);
193  typedef std::shared_ptr<GEDrawTarget> (*CreateGEDrawTarget)(DrawTarget const*,
194  std::vector<GEObject*>&, GEObject*);
195  std::array<CreateGEObject, GT_NUM_TYPES> mCreateGEObject;
196  CreateGEDrawTarget mCreateGEDrawTarget;
198 
199  // Track GraphicsObject destruction and delete to-be-destroyed objects
200  // from the bridge map.
202  {
203  public:
204  virtual ~GOListener();
205  GOListener(GraphicsEngine* engine);
206  virtual void OnDestroy(GraphicsObject const* object);
207  private:
209  };
210 
211  std::shared_ptr<GOListener> mGOListener;
212 
213  // Track DrawTarget destruction and delete to-be-destroyed objects from
214  // the draw target map.
216  {
217  public:
218  virtual ~DTListener();
219  DTListener(GraphicsEngine* engine);
220  virtual void OnDestroy(DrawTarget const* target);
221  private:
223  };
224 
225  std::shared_ptr<DTListener> mDTListener;
226 
229 };
230 
231 
232 inline bool GraphicsEngine::Unbind(std::shared_ptr<GraphicsObject> const& object)
233 {
234  return Unbind(object.get());
235 }
236 
237 inline bool GraphicsEngine::Unbind(std::shared_ptr<DrawTarget> const& target)
238 {
239  return Unbind(target.get());
240 }
241 
242 inline void GraphicsEngine::AllowOcclusionQuery(bool allow)
243 {
244  mAllowOcclusionQuery = allow;
245 }
246 
248 {
249  mWarnOnNonemptyBridges = warn;
250 }
251 
252 }
ThreadSafeMap< GraphicsObject const *, std::shared_ptr< GEObject > > mGOMap
std::array< CreateGEObject, GT_NUM_TYPES > mCreateGEObject
CreateGEDrawTarget mCreateGEDrawTarget
GLuint color
Definition: glcorearb.h:1256
void WarnOnNonemptyBridges(bool warn)
GLint level
Definition: glcorearb.h:103
GLbitfield GLuint program
Definition: glcorearb.h:1926
std::unique_ptr< GEInputLayoutManager > mILMap
void AllowOcclusionQuery(bool allow)
GLenum target
Definition: glcorearb.h:1662
GLint GLenum GLint x
Definition: glcorearb.h:404
std::shared_ptr< GOListener > mGOListener
bool Unbind(std::shared_ptr< GraphicsObject > const &object)
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
GLuint texture
Definition: glcorearb.h:410
ThreadSafeMap< DrawTarget const *, std::shared_ptr< GEDrawTarget > > mDTMap
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2538
GLuint buffer
Definition: glcorearb.h:655
std::shared_ptr< DTListener > mDTListener
GLint y
Definition: glcorearb.h:98
#define GTE_IMPEXP
Definition: GTEngineDEF.h:63


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:00