GteGL4Engine.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.1 (2016/06/30)
7 
8 #pragma once
9 
12 
13 namespace gte
14 {
15 
16 class GL4GraphicsObject;
17 class GL4DrawTarget;
18 
20 {
21 // Interface specific to GL4.
22 public:
23  // Abstract base class for platform-specific OpenGL engines.
24  virtual ~GL4Engine();
25  GL4Engine();
26 
27  // Currently, OpenGL 4.3 or later is required for compute shaders.
28  // Because of deferred construction via Initialize(...), the
29  // requirements are not known until that function is called.
30  // TODO: Redesign the OpenGL system to allow for earlier versions of
31  // OpenGL if the sample application does not require OpenGL 4.3. This
32  // is akin to setting the parameters.featureLevel for Direct3D 11
33  // applications.
34  inline bool MeetsRequirements() const;
35 
36  // Allow the user to switch between OpenGL contexts when there are
37  // multiple instances of GL4Engine in an application.
38  virtual bool IsActive() const = 0;
39  virtual void MakeActive() = 0;
40 
41 protected:
42  // Helpers for construction and destruction.
43  virtual bool Initialize(int requiredMajor, int requiredMinor, bool saveDriverInfo);
44  void Terminate();
45  void CreateDefaultFont();
46  void DestroyDefaultFont();
47 
48  // GTEngine GL4 requires OpenGL 4.3 or later for compute shaders.
49  int mMajor, mMinor;
51 
52 private:
53  // Support for drawing.
54  uint64_t DrawPrimitive(VertexBuffer const* vbuffer, IndexBuffer const* ibuffer);
55 
56  // Support for enabling and disabling resources used by shaders.
57  bool EnableShaders(std::shared_ptr<VisualEffect> const& effect, GLuint program);
58  void DisableShaders(std::shared_ptr<VisualEffect> const& effect, GLuint program);
59  void Enable(Shader const* shader, GLuint program);
60  void Disable(Shader const* shader, GLuint program);
61  void EnableCBuffers(Shader const* shader, GLuint program);
62  void DisableCBuffers(Shader const* shader, GLuint program);
63  void EnableTBuffers(Shader const* shader, GLuint program);
64  void DisableTBuffers(Shader const* shader, GLuint program);
65  void EnableSBuffers(Shader const* shader, GLuint program);
66  void DisableSBuffers(Shader const* shader, GLuint program);
67  void EnableRBuffers(Shader const* shader, GLuint program);
68  void DisableRBuffers(Shader const* shader, GLuint program);
69  void EnableTextures(Shader const* shader, GLuint program);
70  void DisableTextures(Shader const* shader, GLuint program);
71  void EnableTextureArrays(Shader const* shader, GLuint program);
72  void DisableTextureArrays(Shader const* shader, GLuint program);
73  void EnableSamplers(Shader const* shader, GLuint program);
74  void DisableSamplers(Shader const* shader, GLuint program);
75 
76  // A front-end object (hidden from the user) is created for each
77  // atomic counter buffer object declared in use for a shader that
78  // is executed. After execution, these objects are left for use
79  // the next time. They are only destroyed to create new ones
80  // when a larger buffer is required, but the buffer size never
81  // becomes smaller. A RawBuffer type is used here because it
82  // is by definition 4-bytes per element where 4 bytes is the
83  // size for each atomic_uint counter.
84  std::vector<std::shared_ptr<RawBuffer>> mAtomicCounterRawBuffers;
85 
86  // Keep track of available texture sampler/image units
87  // and uniform/shaderstorage buffer units.
88  // If unit is in use, then link count is positive and
89  // program+index bound to that unit will be stored.
91  {
92  public:
95 
96  int AcquireUnit(GLint program, GLint index);
97  int GetUnit(GLint program, GLint index) const;
98  void ReleaseUnit(unsigned index);
99  unsigned GetUnitLinkCount(unsigned unit) const;
100  bool GetUnitProgramIndex(unsigned unit, GLint &program, GLint &index) const;
101  private:
102  struct LinkInfo
103  {
104  unsigned linkCount;
107  };
108 
109  std::vector<LinkInfo> mLinkMap;
110  };
115 
116 
117 // Overrides from GraphicsEngine.
118 public:
119  // Viewport management. The measurements are in window coordinates. The
120  // origin of the window is (x,y), the window width is w, and the window
121  // height is h. The depth range for the view volume is [zmin,zmax]. The
122  // OpenGL viewport is right-handed with origin the lower-left corner of
123  // the window, the x-axis is directed rightward, the y-axis is directed
124  // upward, and the depth range is a subset of [-1,1].
125  virtual void SetViewport(int x, int y, int w, int h) override;
126  virtual void GetViewport(int& x, int& y, int& w, int& h) const override;
127  virtual void SetDepthRange(float zmin, float zmax) override;
128  virtual void GetDepthRange(float& zmin, float& zmax) const override;
129 
130  // Window resizing.
131  virtual bool Resize(unsigned int w, unsigned int h) override;
132 
133  // Support for clearing the color, depth, and stencil back buffers.
134  virtual void ClearColorBuffer() override;
135  virtual void ClearDepthBuffer() override;
136  virtual void ClearStencilBuffer() override;
137  virtual void ClearBuffers() override;
138 
139  // Global drawing state. The default states are shown in GteBlendState.h,
140  // GteDepthStencil.h, and GteRasterizerState.h.
141  virtual void SetBlendState(std::shared_ptr<BlendState> const& state) override;
142  virtual void SetDepthStencilState(std::shared_ptr<DepthStencilState> const& state) override;
143  virtual void SetRasterizerState(std::shared_ptr<RasterizerState> const& state) override;
144 
145  // Support for drawing to offscreen memory (i.e. not to the back buffer).
146  // The DrawTarget object encapsulates render targets (color information)
147  // and depth-stencil target.
148  virtual void Enable(std::shared_ptr<DrawTarget> const& target) override;
149  virtual void Disable(std::shared_ptr<DrawTarget> const& target) override;
150 
151  // Support for copying from CPU to GPU via mapped memory.
152  virtual bool Update(std::shared_ptr<Buffer> const& buffer) override;
153  virtual bool Update(std::shared_ptr<TextureSingle> const& texture) override;
154  virtual bool Update(std::shared_ptr<TextureSingle> const& texture, unsigned int level) override;
155  virtual bool Update(std::shared_ptr<TextureArray> const& textureArray) override;
156  virtual bool Update(std::shared_ptr<TextureArray> const& textureArray, unsigned int item, unsigned int level) override;
157 
158  // Support for copying from CPU to GPU via staging memory.
159  virtual bool CopyCpuToGpu(std::shared_ptr<Buffer> const& buffer) override;
160  virtual bool CopyCpuToGpu(std::shared_ptr<TextureSingle> const& texture) override;
161  virtual bool CopyCpuToGpu(std::shared_ptr<TextureSingle> const& texture, unsigned int level) override;
162  virtual bool CopyCpuToGpu(std::shared_ptr<TextureArray> const& textureArray) override;
163  virtual bool CopyCpuToGpu(std::shared_ptr<TextureArray> const& textureArray, unsigned int item, unsigned int level) override;
164 
165  // Support for copying from GPU to CPU via staging memory.
166  virtual bool CopyGpuToCpu(std::shared_ptr<Buffer> const& buffer) override;
167  virtual bool CopyGpuToCpu(std::shared_ptr<TextureSingle> const& texture) override;
168  virtual bool CopyGpuToCpu(std::shared_ptr<TextureSingle> const& texture, unsigned int level) override;
169  virtual bool CopyGpuToCpu(std::shared_ptr<TextureArray> const& textureArray) override;
170  virtual bool CopyGpuToCpu(std::shared_ptr<TextureArray> const& textureArray, unsigned int item, unsigned int level) override;
171 
172  // Support for copying from GPU to GPU directly. TODO: We will improve on
173  // the feature set for such copies later. For now, the restrictions are
174  // that the resources are different, of the same type, have identical
175  // dimensions, and have compatible formats (if of texture type).
176  virtual void CopyGpuToGpu(
177  std::shared_ptr<Buffer> const& buffer0,
178  std::shared_ptr<Buffer> const& buffer1) override;
179 
180  virtual void CopyGpuToGpu(
181  std::shared_ptr<TextureSingle> const& texture0,
182  std::shared_ptr<TextureSingle> const& texture1) override;
183 
184  virtual void CopyGpuToGpu(
185  std::shared_ptr<TextureSingle> const& texture0,
186  std::shared_ptr<TextureSingle> const& texture1,
187  unsigned int level) override;
188 
189  virtual void CopyGpuToGpu(
190  std::shared_ptr<TextureArray> const& textureArray0,
191  std::shared_ptr<TextureArray> const& textureArray1) override;
192 
193  virtual void CopyGpuToGpu(
194  std::shared_ptr<TextureArray> const& textureArray0,
195  std::shared_ptr<TextureArray> const& textureArray1,
196  unsigned int item, unsigned int level) override;
197 
198  // Counted buffer management. GetNumActiveElements stores the result in 'buffer'.
199  virtual bool GetNumActiveElements(std::shared_ptr<StructuredBuffer> const& buffer) override;
200 
201  // Execute the compute program. If you want the CPU to stall to wait for
202  // the results, call WaitForFinish() immediately after Execute(...).
203  // However, you can synchronize CPU and GPU activity by calling
204  // WaitForFinish() at some later time, the goal being not to stall the
205  // CPU before obtaining the GPU results.
206  virtual bool BindProgram(std::shared_ptr<ComputeProgram> const& program) override;
207  virtual void Execute(std::shared_ptr<ComputeProgram> const& program,
208  unsigned int numXGroups, unsigned int numYGroups, unsigned int numZGroups) override;
209 
210  // Have the CPU wait until the GPU finishes its current command buffer.
211  virtual void WaitForFinish() override;
212 
213  // Flush the command buffer.
214  virtual void Flush() override;
215 
216 private:
217  // Support for drawing. If occlusion queries are enabled, the return
218  // value is the number of samples that passed the depth and stencil
219  // tests, effectively the number of pixels drawn. If occlusion queries
220  // are disabled, the function returns 0.
221  virtual uint64_t DrawPrimitive(
222  std::shared_ptr<VertexBuffer> const& vbuffer,
223  std::shared_ptr<IndexBuffer> const& ibuffer,
224  std::shared_ptr<VisualEffect> const& effect) override;
225 };
226 
227 
228 inline bool GL4Engine::MeetsRequirements() const
229 {
230  return mMeetsRequirements;
231 }
232 
233 }
ProgramIndexUnitMap mUniformUnitMap
Definition: GteGL4Engine.h:113
int GLint
Definition: glcorearb.h:85
unsigned int GLuint
Definition: glcorearb.h:89
std::vector< LinkInfo > mLinkMap
Definition: GteGL4Engine.h:109
bool mMeetsRequirements
Definition: GteGL4Engine.h:50
GLint level
Definition: glcorearb.h:103
ProgramIndexUnitMap mShaderStorageUnitMap
Definition: GteGL4Engine.h:114
GLbitfield GLuint program
Definition: glcorearb.h:1926
ProgramIndexUnitMap mTextureImageUnitMap
Definition: GteGL4Engine.h:112
bool MeetsRequirements() const
Definition: GteGL4Engine.h:228
GLenum target
Definition: glcorearb.h:1662
GLint GLenum GLint x
Definition: glcorearb.h:404
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:852
ProgramIndexUnitMap mTextureSamplerUnitMap
Definition: GteGL4Engine.h:111
GLuint texture
Definition: glcorearb.h:410
GLclampd zmax
Definition: glext.h:6450
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:1997
GLuint shader
Definition: glcorearb.h:780
GLuint index
Definition: glcorearb.h:781
GLuint buffer
Definition: glcorearb.h:655
GLint y
Definition: glcorearb.h:98
std::vector< std::shared_ptr< RawBuffer > > mAtomicCounterRawBuffers
Definition: GteGL4Engine.h:84
#define GTE_IMPEXP
Definition: GTEngineDEF.h:63


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