]> git.cworth.org Git - apitrace-tests/blob - cli/src/glxsimple.c
a4c9560dfb9e908b8d38ba8ca34486f08c67370c
[apitrace-tests] / cli / src / glxsimple.c
1 /**************************************************************************
2  * Copyright 2012 Intel corporation
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  *
23  **************************************************************************/
24
25 #include <stdio.h>
26
27 #include <X11/Xlib.h>
28 #include <GL/glew.h>
29 #include <GL/gl.h>
30 #include <GL/glx.h>
31
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 int width = 64;
38 int height = 64;
39
40 static void
41 set_2d_projection (void)
42 {
43         glMatrixMode (GL_PROJECTION);
44         glLoadIdentity ();
45         glOrtho (0, width, height, 0, 0, 1);
46         glMatrixMode (GL_MODELVIEW);
47 }
48
49 static void
50 draw_fullscreen_quad (void)
51 {
52         glBegin (GL_QUADS);
53         glVertex2f (0, 0);
54         glVertex2f (width, 0);
55         glVertex2f (width, height);
56         glVertex2f (0, height);
57         glEnd ();
58 }
59
60 static void
61 draw_fullscreen_textured_quad (void)
62 {
63         glBegin (GL_QUADS);
64         glTexCoord2f(0, 0); glVertex2f (0, 0);
65         glTexCoord2f(1, 0); glVertex2f (width, 0);
66         glTexCoord2f(1, 1); glVertex2f (width, height);
67         glTexCoord2f(0, 1); glVertex2f (0, height);
68         glEnd ();
69 }
70
71 static void
72 paint_rgb_using_clear (double r, double g, double b)
73 {
74         glClearColor(r, g, b, 1.0);
75         glClear(GL_COLOR_BUFFER_BIT);
76 }
77
78 static void
79 paint_rgb_using_glsl (double r, double g, double b)
80 {
81         const char * vs_source =
82                 "void main()\n"
83                 "{\n"
84                 "        gl_Position = ftransform();\n"
85                 "}\n";
86         const char * fs_source =
87                 "#version 120\n"
88                 "uniform vec4 color;\n"
89                 "void main()\n"
90                 "{\n"
91                 "        gl_FragColor = color;\n"
92                 "}\n";
93
94         GLuint vs, fs, program;
95         GLint color;
96
97         vs = glCreateShader (GL_VERTEX_SHADER);
98         glShaderSource (vs, 1, &vs_source, NULL);
99         glCompileShader (vs);
100
101         fs = glCreateShader (GL_FRAGMENT_SHADER);
102         glShaderSource (fs, 1, &fs_source, NULL);
103         glCompileShader (fs);
104
105         program = glCreateProgram ();
106         glAttachShader (program, vs);
107         glAttachShader (program, fs);
108
109         glLinkProgram (program);
110         glUseProgram (program);
111
112         color = glGetUniformLocation (program, "color");
113
114         glUniform4f (color, r, g, b, 1.0);
115
116         draw_fullscreen_quad ();
117
118         glUseProgram (0);
119 }
120
121 static GLuint
122 create_rgb_texture (double r, double g, double b)
123 {
124         uint8_t data[3];
125         GLuint texture = 0;
126
127         data[0] = (uint8_t) (255.0 * r);
128         data[1] = (uint8_t) (255.0 * g);
129         data[2] = (uint8_t) (255.0 * b);
130
131         glGenTextures (1, &texture);
132
133         glBindTexture (GL_TEXTURE_2D, texture);
134
135         glTexImage2D (GL_TEXTURE_2D,
136                       0, GL_COMPRESSED_RGBA,
137                       1, 1, 0,
138                       GL_RGB, GL_UNSIGNED_BYTE, data);
139
140         return texture;
141 }
142
143 static void
144 paint_using_texture (GLuint texture)
145 {
146         glBindTexture (GL_TEXTURE_2D, texture);
147
148         glEnable (GL_TEXTURE_2D);
149
150         draw_fullscreen_textured_quad ();
151
152         glDisable (GL_TEXTURE_2D);
153 }
154
155 static void
156 draw (Display *dpy, Window window, int width, int height)
157 {
158 #define PASSES 2
159         int i;
160         GLenum glew_err;
161         GLuint texture[PASSES];
162
163         int visual_attr[] = {
164                 GLX_RGBA,
165                 GLX_RED_SIZE,           8,
166                 GLX_GREEN_SIZE,         8,
167                 GLX_BLUE_SIZE,          8,
168                 GLX_ALPHA_SIZE,         8,
169                 GLX_DOUBLEBUFFER,
170                 GLX_DEPTH_SIZE,         24,
171                 GLX_STENCIL_SIZE,       8,
172                 GLX_X_VISUAL_TYPE,      GLX_DIRECT_COLOR,
173                 None
174         };
175
176         /* Window and context setup. */
177         XVisualInfo *visual_info = glXChooseVisual(dpy, 0, visual_attr);
178         GLXContext ctx = glXCreateContext(dpy, visual_info, NULL, True);
179         glXMakeCurrent(dpy, window, ctx);
180
181         glew_err = glewInit();
182         if (glew_err != GLEW_OK)
183         {
184                 fprintf (stderr, "glewInit failed: %s\n",
185                          glewGetErrorString(glew_err));
186                 exit (1);
187         }
188
189         glViewport(0, 0, width, height);
190
191         set_2d_projection ();
192
193 /* Simply count through some colors, frame by frame. */
194 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
195
196         int frame = 0;
197         for (i = 0; i < PASSES; i++) {
198
199                 /* Frame: Draw a solid frame using glClear. */
200                 paint_rgb_using_clear (RGB(frame));
201                 glXSwapBuffers (dpy, window);
202                 frame++;
203
204                 /* Frame: Draw a solid frame using GLSL. */
205                 paint_rgb_using_glsl (RGB(frame));
206                 glXSwapBuffers (dpy, window);
207                 frame++;
208
209                 /* Frame: Draw a solid frame using a texture. */
210                 texture[i] = create_rgb_texture (RGB(frame));
211                 paint_using_texture (texture[i]);
212                 glXSwapBuffers (dpy, window);
213                 frame++;
214         }
215
216         /* Draw another frame with a re-used texture. */
217         paint_using_texture (texture[0]);
218         glXSwapBuffers (dpy, window);
219         frame++;
220
221         /* Cleanup */
222         glXDestroyContext (dpy, ctx);
223 }
224
225 static void
226 handle_events(Display *dpy, Window window, int width, int height)
227 {
228         XEvent xev;
229         KeyCode quit_code = XKeysymToKeycode (dpy, XStringToKeysym("Q"));
230
231         XNextEvent (dpy, &xev);
232
233         while (1) {
234                 XNextEvent (dpy, &xev);
235                 switch (xev.type) {
236                 case KeyPress:
237                         if (xev.xkey.keycode == quit_code) {
238                                 return;
239                         }
240                         break;
241                 case ConfigureNotify:
242                         width = xev.xconfigure.width;
243                         height = xev.xconfigure.height;
244                         break;
245                 case Expose:
246                         if (xev.xexpose.count == 0) {
247                                 draw (dpy, window, width, height);
248                                 return;
249                         }
250                         break;
251                 }
252         }
253 }
254
255 int
256 main (void)
257 {
258         Display *dpy;
259         Window window;
260
261         dpy = XOpenDisplay (NULL);
262
263         if (dpy == NULL) {
264                 fprintf(stderr, "Failed to open display %s\n",
265                         XDisplayName(NULL));
266                 return 1;
267         }
268
269         window = XCreateSimpleWindow(dpy, DefaultRootWindow (dpy),
270                                      0, 0, width, height, 0,
271                                      BlackPixel (dpy, DefaultScreen (dpy)),
272                                      BlackPixel (dpy, DefaultScreen (dpy)));
273
274         XSelectInput(dpy, window,
275                      KeyPressMask | StructureNotifyMask | ExposureMask);
276
277         XMapWindow (dpy, window);
278
279         handle_events (dpy, window, width, height);
280
281         XDestroyWindow (dpy, window);
282         XCloseDisplay (dpy);
283
284         return 0;
285 }