summaryrefslogtreecommitdiff
path: root/retrace/daemon/gldispatch/glframe_glhelper.hpp
blob: 4d443e5db92752625bf22d63906db151ccbbfc5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (C) Intel Corp.  2015.  All Rights Reserved.

// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:

// The above copyright notice and this permission notice (including the
// next paragraph) shall be included in all copies or substantial
// portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

//  **********************************************************************/
//  * Authors:
//  *   Mark Janes <mark.a.janes@intel.com>
//  **********************************************************************/

#ifndef RETRACE_DAEMON_BARGRAPH_GLFRAME_GLHELPER_H_
#define RETRACE_DAEMON_BARGRAPH_GLFRAME_GLHELPER_H_

#include <stdio.h>

#include <GLES2/gl2.h>
#include <GL/gl.h>

#include <string>
#include <vector>

namespace glretrace {

class GlFunctions {
 public:
  static void Init(void *lookup_fn = NULL);
  static GLuint CreateProgram(void);
  static GLuint CreateShader(GLenum type);
  static void ShaderSource(GLuint shader, GLsizei count,
                            const GLchar *const*str,
                            const GLint *length);
  static void CompileShader(GLuint shader);
  static void AttachShader(GLuint program, GLuint shader);
  static void LinkProgram(GLuint program);
  static void UseProgram(GLuint program);
  static GLenum GetError();
  static void GetShaderiv(GLuint shader, GLenum pname, GLint *params);
  static void GetShaderInfoLog(GLuint shader, GLsizei bufSize,
                               GLsizei *length, GLchar *infoLog);
  static void GenBuffers(GLsizei n, GLuint *buffers);
  static void BindBuffer(GLenum target, GLuint buffer);
  static GLint GetAttribLocation(GLuint program, const GLchar *name);
  static GLint GetUniformLocation(GLuint program, const GLchar *name);

  static void ClearColor(GLclampf red, GLclampf green, GLclampf blue,
                         GLclampf alpha);
  static void Clear(GLbitfield mask);
  static void Uniform1f(GLint location, GLfloat v0);
  static void Uniform4f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2,
                        GLfloat v3);
  static void BufferData(GLenum target, GLsizeiptr size, const void *data,
                         GLenum usage);
  static void EnableVertexAttribArray(GLuint index);
  static void VertexAttribPointer(GLuint index, GLint size, GLenum type,
                                  GLboolean normalized, GLsizei stride,
                                  const void *pointer);
  static void DrawArrays(GLenum mode, GLint first, GLsizei count);
  static void DisableVertexAttribArray(GLuint index);
  static void Enable(GLenum cap);

  static void ReadPixels(GLint x, GLint y,
                           GLsizei width, GLsizei height,
                           GLenum format, GLenum type,
                           GLvoid *pixels);
  static void DrawElements(GLenum mode, GLsizei count,
                           GLenum type, const GLvoid *indices);
  static void BlendFunc(GLenum sfactor, GLenum dfactor);

  static void GetFirstPerfQueryIdINTEL(GLuint *queryId);
  static void GetNextPerfQueryIdINTEL(GLuint queryId, GLuint *nextQueryId);
  static void GetIntegerv(GLenum pname, GLint *params);
  static const GLubyte *GetStringi(GLenum pname, GLuint index);
  static void GetPerfQueryInfoINTEL(GLuint queryId, GLuint queryNameLength,
                                    GLchar *queryName, GLuint *dataSize,
                                    GLuint *noCounters, GLuint *noInstances,
                                    GLuint *capsMask);
  static void GetPerfCounterInfoINTEL(GLuint queryId, GLuint counterId,
                                      GLuint counterNameLength,
                                      GLchar *counterName,
                                      GLuint counterDescLength,
                                      GLchar *counterDesc,
                                      GLuint *counterOffset,
                                      GLuint *counterDataSize,
                                      GLuint *counterTypeEnum,
                                      GLuint *counterDataTypeEnum,
                                      GLuint64 *rawCounterMaxValue);

  static void CreatePerfQueryINTEL(GLuint queryId, GLuint *queryHandle);
  static void DeletePerfQueryINTEL(GLuint queryHandle);
  static void BeginPerfQueryINTEL(GLuint queryHandle);
  static void EndPerfQueryINTEL(GLuint queryHandle);
  static void GetPerfQueryDataINTEL(GLuint queryHandle, GLuint flags,
                                    GLsizei dataSize, GLvoid *data,
                                    GLuint *bytesWritten);
  static void GetProgramiv(GLuint program, GLenum pname, GLint *params);
  static void GetProgramInfoLog(GLuint program, GLsizei bufSize,
                                GLsizei *length, GLchar *infoLog);
  static void GetProgramResourceName(GLuint program, GLenum programInterface,
                                     GLuint index, GLsizei bufSize,
                                     GLsizei *length, GLchar *name);

  static void GetBooleanv(GLenum pname, GLboolean *params);
  static void GetFloatv(GLenum pname, GLfloat *params);
  static void BlendColor(GLclampf red, GLclampf green,
                         GLclampf blue, GLclampf alpha);
  static void BlendFuncSeparate(GLenum sfactorRGB, GLenum dfactorRGB,
                                GLenum sfactorAlpha, GLenum dfactorAlpha);
  static void Disable(GLenum cap);
  static void BlendEquation(GLenum mode);
  static void BlendEquationSeparate(GLenum modeRGB, GLenum modeAlpha);

  static void BindAttribLocation(GLuint program, GLuint index,
                                 const GLchar *name);

  static void ValidateProgram(GLuint program);
  static GLboolean IsEnabled(GLenum cap);

  static GLuint GetUniformBlockIndex(GLuint program,
                                     const GLchar *uniformBlockName);
  static void UniformBlockBinding(GLuint program, GLuint uniformBlockIndex,
                                  GLuint uniformBlockBinding);
  static void BindFragDataLocation(GLuint program, GLuint colorNumber,
                                   const char * name);
  static void GetActiveUniform(GLuint program, GLuint index,
                               GLsizei bufSize, GLsizei *length,
                               GLint *size, GLenum *type,
                               GLchar *name);
  static void GetUniformfv(GLuint program, GLint location, GLfloat *params);
  static void GetUniformiv(GLuint program, GLint location, GLint *params);
  static void Uniform1fv(GLint location, GLsizei count, const GLfloat *value);
  static void Uniform1iv(GLint location, GLsizei count, const GLint *value);
  static void Uniform1uiv(GLint location, GLsizei count, const GLuint *value);
  static void Uniform2fv(GLint location, GLsizei count, const GLfloat *value);
  static void Uniform2iv(GLint location, GLsizei count, const GLint *value);
  static void Uniform2uiv(GLint location, GLsizei count, const GLuint *value);
  static void Uniform3fv(GLint location, GLsizei count, const GLfloat *value);
  static void Uniform3iv(GLint location, GLsizei count, const GLint *value);
  static void Uniform3uiv(GLint location, GLsizei count, const GLuint *value);
  static void Uniform4fv(GLint location, GLsizei count, const GLfloat *value);
  static void Uniform4iv(GLint location, GLsizei count, const GLint *value);
  static void Uniform4uiv(GLint location, GLsizei count, const GLuint *value);
  static void UniformMatrix2fv(GLint location, GLsizei count,
                               GLboolean transpose, const GLfloat *value);
  static void UniformMatrix2x3fv(GLint location, GLsizei count,
                                 GLboolean transpose, const GLfloat *value);
  static void UniformMatrix2x4fv(GLint location, GLsizei count,
                                 GLboolean transpose, const GLfloat *value);
  static void UniformMatrix3fv(GLint location, GLsizei count,
                               GLboolean transpose, const GLfloat *value);
  static void UniformMatrix3x2fv(GLint location, GLsizei count,
                                 GLboolean transpose, const GLfloat *value);
  static void UniformMatrix3x4fv(GLint location, GLsizei count,
                                 GLboolean transpose, const GLfloat *value);
  static void UniformMatrix4fv(GLint location, GLsizei count,
                               GLboolean transpose, const GLfloat *value);
  static void UniformMatrix4x2fv(GLint location, GLsizei count,
                                 GLboolean transpose, const GLfloat *value);
  static void UniformMatrix4x3fv(GLint location, GLsizei count,
                                 GLboolean transpose, const GLfloat *value);
  static void Finish();
  static void CullFace(GLenum mode);
  static void LineWidth(GLfloat width);
  static void ColorMask(GLboolean red, GLboolean green,
                        GLboolean blue, GLboolean alpha);
  static void ClearDepthf(GLfloat d);
  static void DepthFunc(GLenum func);
  static void DepthRangef(GLfloat n, GLfloat f);
  static void DepthMask(GLboolean flag);
  static void FrontFace(GLenum mode);

 private:
  GlFunctions();
  static bool m_is_initialized;
};

typedef GlFunctions GL;

inline void CheckError(const char * file, int line) {
  const int error = GL::GetError();
  if ( error == GL_NO_ERROR)
    return;
  printf("ERROR: %x %s:%i\n", error, file, line);
}

inline void GetCompileError(GLint shader, std::string *message) {
  GLint status;
  GL::GetShaderiv(shader, GL_COMPILE_STATUS, &status);
  if (status == GL_TRUE)
    return;
  static const int MAXLEN = 1024;
  std::vector<char> log(MAXLEN);
  GLsizei len;
  GL::GetShaderInfoLog(shader,  MAXLEN,  &len, log.data());
  *message = log.data();
}

inline void GetLinkError(GLint program, std::string *message) {
  GLint status;
  GL::GetProgramiv(program, GL_LINK_STATUS, &status);
  if (status == GL_TRUE) {
    *message = "";
    return;
  }
  static const int MAXLEN = 1024;
  std::vector<char> log(MAXLEN);
  GLsizei len;
  GL::GetProgramInfoLog(program, MAXLEN, &len, log.data());
  *message = log.data();
}

inline void PrintCompileError(GLint shader) {
  std::string message;
  GetCompileError(shader, &message);
  if (message.size() > 0)
    printf("ERROR -- compile failed: %s\n", message.c_str());
}

}  // namespace glretrace

#define GL_CHECK() CheckError(__FILE__, __LINE__)

#endif  // RETRACE_DAEMON_BARGRAPH_GLFRAME_GLHELPER_H_