AGX Dynamics  2.37.3.2
OpenGL.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXGL_OPENGL_H
18 #define AGXGL_OPENGL_H
19 
20 
21 #include <agx/config/AGX_USE_OPENGL.h>
22 #include <agx/config/AGX_USE_EGL.h>
23 #include <agx/config/AGX_USE_OSG.h>
24 #include <agx/agx.h>
25 #include <agx/debug.h>
26 #include <agx/agxCore_export.h>
27 #include <agx/Device.h>
28 #include <agx/Task.h>
29 #include <agx/Kernel.h>
30 #include <agx/Vec4.h>
31 
32 
33 #if AGX_USE_OPENGL()
34 #include <agx/PushDisableWarnings.h> // Disabling warnings. Include agx/PopDisableWarnings.h below!
35 
36 #if defined( __APPLE__ )
37  #define GL_SILENCE_DEPRECATION 1
38 
39  #ifdef AGX_APPLE_IOS
40  #include <OpenGLES/ES1/gl.h>
41  #include <OpenGLES/ES1/glext.h>
42  #include <OpenGLES/ES2/gl.h>
43  #include <OpenGLES/ES2/glext.h>
44  #include <CoreGraphics/CGContext.h>
45  #else
46  #include <OpenGL/OpenGL.h>
47  // #include <OpenGL/gl3.h>
48  #include <OpenGL/gl.h>
49  #include <OpenGL/glu.h>
50  #endif
51 
52 #elif defined( _WIN32 )
53  #define GL_GLEXT_PROTOTYPES
54  #define AGX_CUSTOM_GLAD_API_CALL_EXPORT
55  #include <agx/Windows.h>
56  #include <agxGL/gl.h>
57  #include <GL/glu.h>
58  //#include <GL/glext.h>
59  //#include <GL/wglext.h>
60 
61 
62 #else
63  #define GL_GLEXT_PROTOTYPES
64 
65  #if AGX_USE_EGL()
66  #include <EGL/egl.h>
67  #endif
68 
69  #include <agxGL/gl.h>
70  #include <GL/glx.h>
71  #undef Status
72  #undef Bool
73  #undef Convex
74  #include <GL/glu.h>
75 #endif
76 #include <agx/PopDisableWarnings.h> // End of disabled warnings.
77 
78 // #if AGX_USE_OSG()
79 // #include <osg/GraphicsContext>
80 // #endif
81 
82 
83  #define GL_REAL GL_DOUBLE
84  #define glVertex3r glVertex3d
85  #define glVertex3rv glVertex3dv
86  #define glColor3r glColor3d
87  #define glColor3rv glColor3dv
88  #define glColor4r glColor4d
89  #define glColor4rv glColor4dv
90  #define glNormal3r glNormal3d
91  #define glNormal3rv glNormal3dv
92  #define glMultMatrixr glMultMatrixd
93  #define glTranslater glTranslated
94  #define glRotater glRotated
95 
96 
97 #define glCheckError() glCheckError_impl(__FILE__,__LINE__)
98 #define glCheckErrorTitle(title_) glCheckError_impl(__FILE__,__LINE__, (title_))
99 
100 #ifdef AGX_APPLE_IOS
101 #define gluErrorString(x) agx::String::format("%d", x).c_str()
102 #endif
103 
104 extern "C"
105 {
106  void AGXCORE_EXPORT glCheckError_impl(const char *file, int line, const char* title=nullptr);
107 }
108 
109 namespace agx
110 {
111  class TiXmlElement;
112 }
113 
114 
115 namespace agxGL {
116 
124 
125  #ifdef __APPLE__
126  #ifdef AGX_APPLE_IOS
127  typedef CGContextRef ContextHandle;
128  #else
129  typedef CGLContextObj ContextHandle;
130  #endif
131  #elif defined( UNIX ) || defined( __linux__ )
132  typedef GLXContext ContextHandle;
133  #elif defined _WIN32
134  typedef HGLRC ContextHandle;
135  #else
136  #error "OpenGL context handling on other platforms"
137  #endif
138 
139  void AGXCORE_EXPORT init();
140  ContextHandle AGXCORE_EXPORT getCurrentContext();
141  void AGXCORE_EXPORT setCurrentContext(ContextHandle context);
142 
144 
145 
146 
147  class AGXCORE_EXPORT SyncTag : public agx::SyncTag
148  {
149  public:
150  virtual Status wait() override { return SYNC_TAG_COMPLETED; }
151  virtual Status getStatus() override { return SYNC_TAG_COMPLETED; }
152  };
153 
155  {
156  public:
157  static Device *instance();
158 
159 
161  virtual agxData::Buffer* createBuffer(agxData::Format* format) override;
162 
163  virtual agx::SyncTag* sendData(
164  const void* hostBuffer, agxData::Buffer* deviceBuffer,
165  size_t offset, size_t size, bool blocking) override;
166 
167  virtual agx::SyncTag* receiveData(
168  void* hostBuffer, agxData::Buffer* deviceBuffer,
169  size_t offset, size_t size, bool blocking) override;
170 
171  // #if AGX_USE_OSG()
172  // /**
173  // Allow device to keep reference to context to prevent agxGL::Buffer from being deallocated
174  // _after_ the OpenGL context is removed.
175  // */
176  // void setOsgContext(osg::GraphicsContext *context);
177  // #endif
178 
179  static void clearGLDevice();
180  protected:
181  virtual ~Device();
182 
183  private:
184  Device(const agx::String name = "OpenGlDevice");
185 
186  // #if AGX_USE_OSG()
187  // osg::ref_ptr<osg::GraphicsContext> m_osgContext;
188  // #endif
189  };
190 
191 
192 
194  {
195  public:
196  Buffer(agxData::Format *format, Device *device, GLenum usage = GL_STREAM_DRAW);
197  GLuint getImplementation();
198 
199 
200  // using agxData::Buffer::read;
201  // virtual void read(const void* /*buffer*/, size_t /*numElements*/, const agxData::Format* /*format*/) override { agxAbort1("TODO"); }
202 
203  protected:
204  virtual ~Buffer();
205  virtual void reallocate(size_t size) override;
206  static void initializeElements(agxData::Buffer *buffer, const agx::IndexRange& range);
207 
208  private:
209  friend class Device;
210  void activate();
211  GLuint allocate(size_t memsize);
212  bool deallocate(GLuint handle);
213 
214  private:
215  GLuint m_bufferHandle;
216  GLenum m_usageFlags;
217  };
218 
219 
221  {
222  public:
223  Task(const agx::Name& name, agx::Device *device = Device::instance());
224 
225  private:
226  void pre(agx::Task *);
227  void post(agx::Task *);
228 
229  private:
232  };
233 
234 
236  {
237  public:
238  Shader();
239  ~Shader();
240 
241  GLuint getImplementation();
242  protected:
243  agx::String readSource();
244  void build_impl(GLenum shaderType, const agx::String& buildFlags);
245 
246  protected:
249  };
250 
252  {
253  public:
254  void build(const agx::String& shaderSourcePath, const agx::String& buildFlags);
255  private:
256  };
257 
258 
260  {
261  public:
262  void build(const agx::String& shaderSourcePath, const agx::String& buildFlags);
263  private:
264  };
265 
267  {
268  public:
269  void build(const agx::String& shaderSourcePath, const agx::String& buildFlags);
270  private:
271  };
272 
273 
275  {
276  public:
277  State(const agx::String& name);
278  static State* load(agx::TiXmlElement *eState, agx::Device* );
279  virtual void apply() = 0;
280  virtual void addedToKernel(Kernel* kernel);
281  };
282 
284  {
285  public:
286  CapabilityState(GLenum capability, agx::String& capabilityName, agx::String& bindPath, agx::Bool enabled=true);
287  virtual void apply();
288  static CapabilityState* load(agx::TiXmlElement *eState);
289  virtual void addedToKernel(Kernel* kernel);
290  private:
291  GLenum m_capability;
292  agxData::Val<agx::Bool> m_enabled;
293  //agx::Bool m_enabled;
294  };
295 
297  {
298  public:
299  PointSizeState(GLfloat size);
300  virtual void apply();
301  private:
302  GLfloat m_pointSize;
303  };
304 
306  {
307  public:
308  LineWidthState(GLfloat size);
309  virtual void apply();
310  private:
311  GLfloat m_lineWidth;
312  };
313 
315  {
316  public:
317  DepthMaskState(GLboolean enabled);
318  virtual void apply();
319  private:
320  GLboolean m_enabled;
321  };
322 
323 
325  {
326  public:
327  TexEnvState(GLenum target, GLenum pname, GLint param);
328  virtual void apply();
329  private:
330  GLenum m_target;
331  GLenum m_pname;
332  GLint m_param;
333  };
334 
336  {
337  public:
338  PolygonModeState(GLenum face, GLenum mode);
339  virtual void apply();
340  private:
341  GLenum m_face;
342  GLenum m_mode;
343  };
344 
345 
347  {
348  public:
349  static Kernel *load(agx::TiXmlElement *eKernel, agx::Device *device);
350  virtual void configure(agx::TiXmlElement *eKernel) override;
351 
352  public:
353  Kernel(const agx::Name& name);
354  Kernel(const agx::Name& name, agx::Device *device, const agx::String& shaderSourcePath, const agx::String& buildFlags);
355 
356  void build(agx::Device *device);
357  void dispatch();
358  void voidDispatch();
359  virtual void addState(State* state);
360  GLuint getShaderProgram();
361 
362  protected:
363  virtual ~Kernel();
364  void parsePrimitive(agx::TiXmlElement* eKernel);
365  void parseState(agx::TiXmlElement* eKernel);
366  void locateUniforms();
367  void locateAttributes();
368  void updateUniforms();
369  void updateAttributes();
370  void addUniform(agx::TiXmlElement *eUniform);
371  void addAttribute(agx::TiXmlElement *eAttribute);
372  void addSharedAttribute(agx::TiXmlElement *eAttribute);
373  bool generateVao();
374  bool deallocateVao();
375 
376  protected:
382  GLuint m_program;
383 
385  GLsizei m_numElements;
386  GLenum m_primitive;
388 
389  GLuint m_vao;
390 
392  {
393  ShaderVariable();
395  const agx::String& name, agx::Parameter* parameter_, GLint numComponents_ = 1,
396  GLsizei stride_ = 1, agx::ScalarParameter* divisor_ = nullptr);
397 
398  GLuint id;
400  GLsizei stride;
404  };
405 
413  };
414 
415 }
416 
417 // AGX_USE_OPENGL
418 #endif
419 
420 #endif /* _AGXGL_OPENGL_H_ */
GLuint m_shaderHandle
Definition: OpenGL.h:247
ParallelTask.
Definition: Task.h:515
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
agx::ArrayParameterRef m_multiDrawFirstArgument
Definition: OpenGL.h:410
agx::ParameterRef parameter
Definition: OpenGL.h:402
VertexShader m_vertexShader
Definition: OpenGL.h:379
bool Bool
Definition: Integer.h:48
agx::Vector< StateRef > m_states
Definition: OpenGL.h:387
agx::Vector< ShaderVariable > m_uniforms
Definition: OpenGL.h:406
GeometryShader m_geometryShader
Definition: OpenGL.h:378
A format is an implementation of a agxData::Type.
Definition: Format.h:63
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
A representation of a generic task.
Definition: Task.h:57
agx::ScalarParameterRef m_numInstances
Definition: OpenGL.h:412
Representation of a name string.
Definition: Name.h:32
void AGXCORE_EXPORT setCurrentContext(ContextHandle context)
GLuint m_program
Definition: OpenGL.h:382
Templated vector class.
Definition: Vector.h:52
GLuint m_vao
Definition: OpenGL.h:389
GLsizei m_numVerticesPerPrimitive
Definition: OpenGL.h:384
virtual Status wait() override
Definition: OpenGL.h:150
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
GLsizei m_numElements
Definition: OpenGL.h:385
A representation of a task/kernel parameter, which can be bound using a string path or explicitly to ...
Definition: Parameter.h:61
A kernel is an abstract representation of data parallel tasks.
Definition: Kernel.h:46
Abstract representation of a data buffer.
Definition: Buffer.h:55
bool m_multiDraw
Definition: OpenGL.h:411
Scalar parameter.
Definition: Parameter.h:255
Callback1< T1 > CallbackType
Definition: Event.h:37
void AGXCORE_EXPORT glCheckError_impl(const char *file, int line, const char *title=nullptr)
agxData::Buffer * createBuffer(agxData::Type *type)
AGXCORE_EXPORT agx::Event initEvent
agx::String m_sourcePath
Definition: OpenGL.h:377
agx::ArrayParameterRef m_indexArgument
Definition: OpenGL.h:408
ContextHandle AGXCORE_EXPORT getCurrentContext()
agx::String m_buildFlags
Definition: OpenGL.h:381
agx::Vector< ShaderVariable > m_attributes
Definition: OpenGL.h:407
This namespace contains OpenGL specific details for connecting OpenCL/OpenGL.
An agx::Device is an abstract representation of a device on which data can be stored and processed...
Definition: Device.h:57
virtual Status getStatus() override
Definition: OpenGL.h:151
An event with no arguments.
Definition: Event.h:89
agx::String m_sourceFilePath
Definition: OpenGL.h:248
#define AGXCORE_EXPORT
FragmentShader m_fragmentShader
Definition: OpenGL.h:380
GLenum m_primitive
Definition: OpenGL.h:386
agx::ArrayParameterRef m_multiDrawCountArgument
Definition: OpenGL.h:409
agx::ScalarParameterRef divisor
Definition: OpenGL.h:403
agx::Object is a refcounted object with a name.
Definition: Object.h:58