1 /* gltest - small OpenGL tearing test program
2 * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 // extension functions we use
27 typedef GLuint (*GLCREATESHADERPROC) (GLenum type);
28 typedef void (*GLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
29 typedef void (*GLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
30 typedef void (*GLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const GLchar* const *string, const GLint *length);
31 typedef void (*GLCOMPILESHADERPROC) (GLuint shader);
32 typedef GLuint (*GLCREATEPROGRAMPROC) (void);
33 typedef void (*GLATTACHSHADERPROC) (GLuint program, GLuint shader);
34 typedef void (*GLLINKPROGRAMPROC) (GLuint program);
35 typedef GLint (*GLGETATTRIBLOCATIONPROC) (GLuint program, const GLchar *name);
36 typedef void (*GLUSEPROGRAMPROC) (GLuint program);
38 typedef void (*GLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
39 typedef void (*GLENABLEVERTEXATTRIBARRAYPROC) (GLuint index);
40 typedef void (*GLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);
41 typedef void (*GLBINDBUFFERPROC) (GLenum target, GLuint buffer);
42 typedef void (*GLBUFFERDATAPROC) (GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);
43 typedef void (*GLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers);
46 // VAOs are only supported in GL3+, and not needed in earler contexts
47 typedef void (*GLBINDVERTEXARRAYPROC) (GLuint array);
48 typedef void (*GLDELETEVERTEXARRAYSPROC) (GLsizei n, const GLuint *arrays);
49 typedef void (*GLGENVERTEXARRAYSPROC) (GLsizei n, GLuint *arrays);
52 GLGETSHADERIVPROC p_glGetShaderiv = NULL;
53 GLGETSHADERINFOLOGPROC p_glGetShaderInfoLog = NULL;
54 GLCREATESHADERPROC p_glCreateShader = NULL;
55 GLSHADERSOURCEPROC p_glShaderSource = NULL;
56 GLCOMPILESHADERPROC p_glCompileShader = NULL;
57 GLCREATEPROGRAMPROC p_glCreateProgram = NULL;
58 GLATTACHSHADERPROC p_glAttachShader = NULL;
59 GLLINKPROGRAMPROC p_glLinkProgram = NULL;
60 GLGETATTRIBLOCATIONPROC p_glGetAttribLocation = NULL;
61 GLUSEPROGRAMPROC p_glUseProgram = NULL;
63 GLVERTEXATTRIBPOINTERPROC p_glVertexAttribPointer = NULL;
64 GLENABLEVERTEXATTRIBARRAYPROC p_glEnableVertexAttribArray = NULL;
65 GLGENBUFFERSPROC p_glGenBuffers = NULL;
66 GLBINDBUFFERPROC p_glBindBuffer = NULL;
67 GLBUFFERDATAPROC p_glBufferData = NULL;
68 GLDELETEBUFFERSPROC p_glDeleteBuffers = NULL;
71 GLBINDVERTEXARRAYPROC p_glBindVertexArray = NULL;
72 GLDELETEVERTEXARRAYSPROC p_glDeleteVertexArrays = NULL;
73 GLGENVERTEXARRAYSPROC p_glGenVertexArrays = NULL;
76 // resolve function pointers
77 static T_proc resolveFunctionPointer(T_glGetProcAddress p_glGetProcAddress, const char *name)
79 T_proc proc = p_glGetProcAddress(name);
81 die("Error resolvung function %s\n", name);
86 void resolveFunctionPointers(T_glGetProcAddress p_glGetProcAddress)
89 // we need to check if we have GL 2.0 or greater
90 std::string version = (char*)glGetString(GL_VERSION);
91 std::stringstream ssv(version);
93 std::getline(ssv, major, '.');
94 std::stringstream ssm(major);
97 if (majorVersion < 2) {
98 die("Need at least GL 2.0 to function properly, but detected version %d\n", majorVersion);
102 // now get the function pointers
103 p_glGetShaderiv = (GLGETSHADERIVPROC)resolveFunctionPointer(p_glGetProcAddress, "glGetShaderiv");
104 p_glGetShaderInfoLog = (GLGETSHADERINFOLOGPROC)resolveFunctionPointer(p_glGetProcAddress, "glGetShaderInfoLog");
105 p_glCreateShader = (GLCREATESHADERPROC)resolveFunctionPointer(p_glGetProcAddress, "glCreateShader");
106 p_glShaderSource = (GLSHADERSOURCEPROC)resolveFunctionPointer(p_glGetProcAddress, "glShaderSource");
107 p_glCompileShader = (GLCOMPILESHADERPROC)resolveFunctionPointer(p_glGetProcAddress, "glCompileShader");
108 p_glCreateProgram = (GLCREATEPROGRAMPROC)resolveFunctionPointer(p_glGetProcAddress, "glCreateProgram");
109 p_glAttachShader = (GLATTACHSHADERPROC)resolveFunctionPointer(p_glGetProcAddress, "glAttachShader");
110 p_glLinkProgram = (GLLINKPROGRAMPROC)resolveFunctionPointer(p_glGetProcAddress, "glLinkProgram");
111 p_glGetAttribLocation = (GLGETATTRIBLOCATIONPROC)resolveFunctionPointer(p_glGetProcAddress, "glGetAttribLocation");
114 p_glBindVertexArray = (GLBINDVERTEXARRAYPROC)resolveFunctionPointer(p_glGetProcAddress, "glBindVertexArray");
115 p_glDeleteVertexArrays = (GLDELETEVERTEXARRAYSPROC)resolveFunctionPointer(p_glGetProcAddress, "glDeleteVertexArrays");
116 p_glGenVertexArrays = (GLGENVERTEXARRAYSPROC)resolveFunctionPointer(p_glGetProcAddress, "glGenVertexArrays");
119 p_glUseProgram = (GLUSEPROGRAMPROC)resolveFunctionPointer(p_glGetProcAddress, "glUseProgram");
120 p_glVertexAttribPointer = (GLVERTEXATTRIBPOINTERPROC)resolveFunctionPointer(p_glGetProcAddress, "glVertexAttribPointer");
121 p_glEnableVertexAttribArray = (GLENABLEVERTEXATTRIBARRAYPROC)resolveFunctionPointer(p_glGetProcAddress, "glEnableVertexAttribArray");
122 p_glGenBuffers = (GLGENBUFFERSPROC)resolveFunctionPointer(p_glGetProcAddress, "glGenBuffers");
123 p_glBindBuffer = (GLBINDBUFFERPROC)resolveFunctionPointer(p_glGetProcAddress, "glBindBuffer");
124 p_glBufferData = (GLBUFFERDATAPROC)resolveFunctionPointer(p_glGetProcAddress, "glBufferData");
125 p_glDeleteBuffers = (GLDELETEBUFFERSPROC)resolveFunctionPointer(p_glGetProcAddress, "glDeleteBuffers");
129 static const char *vertex_shader_source =
131 precision mediump float;\n\
132 precision mediump int;\n\
133 attribute vec2 position; \n\
134 attribute vec3 color; \n\
135 varying vec3 frag_color; \n\
138 frag_color = color; \n\
139 gl_Position = vec4(position * vec2(2) - vec2(1), 0.0, 1.0); \n\
141 static const char *fragment_shader_source =
143 precision mediump float;\n\
144 precision mediump int;\n\
145 varying vec3 frag_color; \n\
148 gl_FragColor = vec4(frag_color, 1.0); \n\
152 // OpenGL IDs (yeah, this is a bad hack... do we care?)
153 static GLuint program;
159 // some local hlper functions
160 static void show_info_log(GLuint object)
163 p_glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length);
164 char *log = new char[log_length];
165 p_glGetShaderInfoLog(object, log_length, NULL, log);
166 fprintf(stderr, "%s", log);
170 static GLuint compile_shader(GLenum shader_type, const char *source)
172 GLuint shader = p_glCreateShader(shader_type);
173 p_glShaderSource(shader, 1, &source, NULL);
174 p_glCompileShader(shader);
176 p_glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
178 fprintf(stderr, "Failed to compile shader\n");
179 show_info_log(shader);
182 checkGlError("Compiling shader");
186 static GLuint createArrayBuffer(GLsizeiptr size, GLfloat *data)
189 p_glGenBuffers(1, &buffer);
190 p_glBindBuffer(GL_ARRAY_BUFFER, buffer);
191 p_glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
192 checkGlError("Creating array buffer");
197 void initialise2dProjection()
199 GLuint vertex_shader = compile_shader(GL_VERTEX_SHADER, vertex_shader_source);
200 GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER, fragment_shader_source);
201 program = p_glCreateProgram();
202 p_glAttachShader(program, vertex_shader);
203 p_glAttachShader(program, fragment_shader);
204 p_glLinkProgram(program);
205 attributes.position = p_glGetAttribLocation(program, "position");
206 attributes.color = p_glGetAttribLocation(program, "color");
210 void drawQuad(GLfloat red, GLfloat green, GLfloat blue, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
212 p_glUseProgram(program);
213 checkGlError("Enabling the shaders");
216 // create a vertex array obect and use it
218 p_glGenVertexArrays(1, &vao);
219 p_glBindVertexArray(vao);
220 checkGlError("Creating a VAO");
223 // send vertex data to card with given attribute
224 GLfloat vertex_buffer_data[] = {
230 GLuint vertex_buffer = createArrayBuffer(sizeof(vertex_buffer_data), vertex_buffer_data);
231 p_glVertexAttribPointer(
232 attributes.position, /* attribute */
235 GL_FALSE, /* normalized? */
237 (void*)0 /* array buffer offset */
239 checkGlError("Preparing vertex data");
240 p_glEnableVertexAttribArray(attributes.position);
241 checkGlError("Sending vertex data");
243 // same with color data
244 GLfloat color_buffer_data[] = {
250 GLuint color_buffer = createArrayBuffer(sizeof(color_buffer_data), color_buffer_data);
251 p_glVertexAttribPointer(
252 attributes.color, /* attribute */
255 GL_FALSE, /* normalized? */
257 (void*)0 /* array buffer offset */
259 checkGlError("Preparing color data");
260 p_glEnableVertexAttribArray(attributes.color);
261 checkGlError("Sending color data");
264 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
265 checkGlError("Drawing");
268 p_glDeleteBuffers(1, &vertex_buffer);
269 p_glDeleteBuffers(1, &color_buffer);
271 p_glDeleteVertexArrays(1, &vao);
273 checkGlError("Doing cleanup");