GteGraphicsEngine.cpp
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 #include <GTEnginePCH.h>
10 using namespace gte;
11 
13 {
14 }
15 
17  :
18  mCreateGEDrawTarget(nullptr),
19  mGEObjectCreator(nullptr),
20  mAllowOcclusionQuery(false),
22 {
23  mCreateGEObject.fill(nullptr);
24 
25  mGOListener = std::make_shared<GOListener>(this);
27 
28  mDTListener = std::make_shared<DTListener>(this);
30 }
31 
32 void GraphicsEngine::SetFont(std::shared_ptr<Font> const& font)
33 {
34  if (font)
35  {
36  if (font != mActiveFont)
37  {
38  // Destroy font resources in GPU memory. The mActiveFont should
39  // be null once, only when the mDefaultFont is created.
40  if (mActiveFont)
41  {
42  Unbind(mActiveFont->GetVertexBuffer());
43  Unbind(mActiveFont->GetIndexBuffer());
44  Unbind(mActiveFont->GetTextEffect()->GetTranslate());
45  Unbind(mActiveFont->GetTextEffect()->GetColor());
46  Unbind(mActiveFont->GetTextEffect()->GetVertexShader());
47  Unbind(mActiveFont->GetTextEffect()->GetPixelShader());
48  }
49 
50  mActiveFont = font;
51 
52  // Create font resources in GPU memory.
53  Bind(mActiveFont->GetVertexBuffer());
54  Bind(mActiveFont->GetIndexBuffer());
55  Bind(mActiveFont->GetTextEffect()->GetTranslate());
56  Bind(mActiveFont->GetTextEffect()->GetColor());
57  Bind(mActiveFont->GetTextEffect()->GetVertexShader());
58  Bind(mActiveFont->GetTextEffect()->GetPixelShader());
59  }
60  }
61  else
62  {
63  LogError("Input font is null.");
64  }
65 }
66 
67 uint64_t GraphicsEngine::Draw(Visual* visual)
68 {
69  if (visual)
70  {
71  auto const& vbuffer = visual->GetVertexBuffer();
72  auto const& ibuffer = visual->GetIndexBuffer();
73  auto const& effect = visual->GetEffect();
74  if (vbuffer && ibuffer && effect)
75  {
76  return DrawPrimitive(vbuffer, ibuffer, effect);
77  }
78  }
79 
80  LogError("Null input to Draw.");
81  return 0;
82 }
83 
84 uint64_t GraphicsEngine::Draw(std::vector<Visual*> const& visuals)
85 {
86  uint64_t numPixelsDrawn = 0;
87  for (auto const& visual : visuals)
88  {
89  numPixelsDrawn += Draw(visual);
90  }
91  return numPixelsDrawn;
92 }
93 
94 uint64_t GraphicsEngine::Draw(std::shared_ptr<Visual> const& visual)
95 {
96  return Draw(visual.get());
97 }
98 
99 uint64_t GraphicsEngine::Draw(std::vector<std::shared_ptr<Visual>> const& visuals)
100 {
101  uint64_t numPixelsDrawn = 0;
102  for (auto const& visual : visuals)
103  {
104  numPixelsDrawn += Draw(visual);
105  }
106  return numPixelsDrawn;
107 }
108 
109 uint64_t GraphicsEngine::Draw(int x, int y, std::array<float, 4> const& color, std::string const& message)
110 {
111  uint64_t numPixelsDrawn;
112 
113  if (message.length() > 0)
114  {
115  int vx, vy, vw, vh;
116  GetViewport(vx, vy, vw, vh);
117  mActiveFont->Typeset(vw, vh, x, y, color, message);
118 
119  Update(mActiveFont->GetTextEffect()->GetTranslate());
120  Update(mActiveFont->GetTextEffect()->GetColor());
121  Update(mActiveFont->GetVertexBuffer());
122 
123  // We need to restore default state for text drawing. Remember the
124  // current state so that we can reset it after drawing.
125  std::shared_ptr<BlendState> bState = GetBlendState();
126  std::shared_ptr<DepthStencilState> dState = GetDepthStencilState();
127  std::shared_ptr<RasterizerState> rState = GetRasterizerState();
131 
132  numPixelsDrawn = DrawPrimitive(mActiveFont->GetVertexBuffer(),
133  mActiveFont->GetIndexBuffer(), mActiveFont->GetTextEffect());
134 
135  SetBlendState(bState);
136  SetDepthStencilState(dState);
137  SetRasterizerState(rState);
138  }
139  else
140  {
141  numPixelsDrawn = 0;
142  }
143 
144  return numPixelsDrawn;
145 }
146 
147 uint64_t GraphicsEngine::Draw(std::shared_ptr<OverlayEffect> const& overlay)
148 {
149  if (overlay)
150  {
151  auto const& vbuffer = overlay->GetVertexBuffer();
152  auto const& ibuffer = overlay->GetIndexBuffer();
153  auto const& effect = overlay->GetEffect();
154  if (vbuffer && ibuffer && effect)
155  {
156  return DrawPrimitive(vbuffer, ibuffer, effect);
157  }
158  }
159 
160  LogError("Null input to Draw.");
161  return 0;
162 }
163 
164 GEObject* GraphicsEngine::Bind(std::shared_ptr<GraphicsObject> const& object)
165 {
166  if (!object)
167  {
168  LogError("Attempt to bind a null object.");
169  return nullptr;
170  }
171 
172  GraphicsObject const* gtObject = object.get();
173  std::shared_ptr<GEObject> geObject;
174  if (!mGOMap.Get(gtObject, geObject))
175  {
176  // The 'create' function is not null with the current engine design.
177  // If the assertion is triggered, someone changed the hierarchy of
178  // GraphicsObjectType but did not change msCreateFunctions[] to match.
179  CreateGEObject create = mCreateGEObject[object->GetType()];
180  if (!create)
181  {
182  // No logger message is generated here because GL4 does not
183  // have shader creation functions.
184  return nullptr;
185  }
186 
187  geObject = create(mGEObjectCreator, gtObject);
188  LogAssert(geObject, "Null object. Out of memory?");
189 
190  mGOMap.Insert(gtObject, geObject);
191 #if defined(GTE_GRAPHICS_USE_NAMED_OBJECTS)
192  geObject->SetName(object->GetName());
193 #endif
194  }
195  return geObject.get();
196 }
197 
198 GEDrawTarget* GraphicsEngine::Bind(std::shared_ptr<DrawTarget> const& target)
199 {
200  DrawTarget const* gtTarget = target.get();
201  std::shared_ptr<GEDrawTarget> geTarget;
202  if (!mDTMap.Get(gtTarget, geTarget))
203  {
204  unsigned int const numTargets = target->GetNumTargets();
205  std::vector<GEObject*> rtTextures(numTargets);
206  for (unsigned int i = 0; i < numTargets; ++i)
207  {
208  rtTextures[i] = static_cast<GEObject*>(Bind(target->GetRTTexture(i)));
209  }
210 
211  std::shared_ptr<TextureDS> object = target->GetDSTexture();
212  GEObject* dsTexture;
213  if (object)
214  {
215  dsTexture = static_cast<GEObject*>(Bind(object));
216  }
217  else
218  {
219  dsTexture = nullptr;
220  }
221 
222  geTarget = mCreateGEDrawTarget(gtTarget, rtTextures, dsTexture);
223  mDTMap.Insert(gtTarget, geTarget);
224  }
225  return geTarget.get();
226 }
227 
228 GEObject* GraphicsEngine::Get(std::shared_ptr<GraphicsObject> const& object) const
229 {
230  GraphicsObject const* gtObject = object.get();
231  std::shared_ptr<GEObject> geObject;
232  if (mGOMap.Get(gtObject, geObject))
233  {
234  return geObject.get();
235  }
236  return nullptr;
237 }
238 
239 GEDrawTarget* GraphicsEngine::Get(std::shared_ptr<DrawTarget> const& target) const
240 {
241  DrawTarget const* gtTarget = target.get();
242  std::shared_ptr<GEDrawTarget> geTarget;
243  if (!mDTMap.Get(gtTarget, geTarget))
244  {
245  LogWarning("Cannot find draw target.");
246  }
247  return geTarget.get();
248 }
249 
250 void GraphicsEngine::GetTotalAllocation(size_t& numBytes, size_t& numObjects) const
251 {
252  numBytes = 0;
253  numObjects = 0;
254  std::vector<std::shared_ptr<GEObject>> objects;
255  mGOMap.GatherAll(objects);
256  for (auto object : objects)
257  {
258  if (object)
259  {
260  auto resource = dynamic_cast<Resource*>(object->GetGraphicsObject());
261  if (resource)
262  {
263  ++numObjects;
264  numBytes += resource->GetNumBytes();
265  }
266  }
267  }
268 }
269 
271 {
272  if (mDefaultBlendState)
273  {
275  }
276 
278  {
280  }
281 
283  {
285  }
286 
288 }
289 
291 {
292  std::shared_ptr<GEObject> dxObject;
293  if (mGOMap.Get(object, dxObject))
294  {
295  GraphicsObjectType type = object->GetType();
296  if (type == GT_VERTEX_BUFFER)
297  {
298  mILMap->Unbind(static_cast<VertexBuffer const*>(object));
299  }
300  else if (type == GT_VERTEX_SHADER)
301  {
302  mILMap->Unbind(static_cast<Shader const*>(object));
303  }
304 
305  if (mGOMap.Remove(object, dxObject))
306  {
307  return true;
308  }
309  }
310  return false;
311 }
312 
314 {
315  std::shared_ptr<GEDrawTarget> dxTarget = nullptr;
316  if (mDTMap.Remove(target, dxTarget))
317  {
318  return true;
319  }
320 
321  return false;
322 }
323 
325 {
326 }
327 
329  :
330  mEngine(engine)
331 {
332 }
333 
335 {
336  if (mEngine)
337  {
338  mEngine->Unbind(object);
339  }
340 }
341 
343 {
344 }
345 
347  :
348  mEngine(engine)
349 {
350 }
351 
353 {
354  if (mEngine)
355  {
356  mEngine->Unbind(target);
357  }
358 }
std::shared_ptr< RasterizerState > mDefaultRasterizerState
ThreadSafeMap< GraphicsObject const *, std::shared_ptr< GEObject > > mGOMap
std::shared_ptr< Font > mActiveFont
std::array< CreateGEObject, GT_NUM_TYPES > mCreateGEObject
GT_VERTEX_BUFFER
void GetTotalAllocation(size_t &numBytes, size_t &numObjects) const
virtual void SetDepthStencilState(std::shared_ptr< DepthStencilState > const &state)=0
CreateGEDrawTarget mCreateGEDrawTarget
#define LogAssert(condition, message)
Definition: GteLogger.h:86
GLuint color
Definition: glcorearb.h:1256
GT_VERTEX_SHADER
DTListener(GraphicsEngine *engine)
GEObject * Get(std::shared_ptr< GraphicsObject > const &object) const
std::shared_ptr< VertexBuffer > const & GetVertexBuffer() const
Definition: GteVisual.h:77
std::unique_ptr< GEInputLayoutManager > mILMap
void SetDefaultDepthStencilState()
virtual void SetFont(std::shared_ptr< Font > const &font) override
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)
virtual bool Update(std::shared_ptr< Buffer > const &buffer)=0
virtual void DestroyDefaultGlobalState()
static void SubscribeForDestruction(std::shared_ptr< ListenerForDestruction > const &listener)
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
#define LogError(message)
Definition: GteLogger.h:92
std::shared_ptr< IndexBuffer > const & GetIndexBuffer() const
Definition: GteVisual.h:82
std::shared_ptr< VisualEffect > const & GetEffect() const
Definition: GteVisual.h:87
unsigned int GetNumTargets() const
virtual void OnDestroy(DrawTarget const *target)
ThreadSafeMap< DrawTarget const *, std::shared_ptr< GEDrawTarget > > mDTMap
virtual void SetBlendState(std::shared_ptr< BlendState > const &state)=0
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2538
#define LogWarning(message)
Definition: GteLogger.h:95
unsigned int GetNumBytes() const
Definition: GteResource.h:116
GOListener(GraphicsEngine *engine)
std::shared_ptr< BlendState > const & GetBlendState() const
GEObject * Bind(std::shared_ptr< GraphicsObject > const &object)
uint64_t Draw(Visual *visual)
std::shared_ptr< DepthStencilState > const & GetDepthStencilState() const
virtual void SetRasterizerState(std::shared_ptr< RasterizerState > const &state)=0
virtual void GetViewport(int &x, int &y, int &w, int &h) const =0
static void SubscribeForDestruction(std::shared_ptr< ListenerForDestruction > const &listener)
std::shared_ptr< GEObject >(* CreateGEObject)(void *, GraphicsObject const *)
std::shared_ptr< BlendState > mDefaultBlendState
virtual void OnDestroy(GraphicsObject const *object)
virtual void DestroyDefaultGlobalState()
std::shared_ptr< RasterizerState > const & GetRasterizerState() const
void SetDefaultBlendState()
void * dxObject
Definition: wglext.h:626
std::shared_ptr< DepthStencilState > mDefaultDepthStencilState
std::shared_ptr< DTListener > mDTListener
void SetDefaultRasterizerState()
GLint y
Definition: glcorearb.h:98
virtual uint64_t DrawPrimitive(std::shared_ptr< VertexBuffer > const &vbuffer, std::shared_ptr< IndexBuffer > const &ibuffer, std::shared_ptr< VisualEffect > const &effect)=0
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:103


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