WIP - port to Mali EGL
[mesa-demos/mali.git] / src / egl / opengles1 / msaa.c
blob07869b201deefd6a3dd9bd450bbf0b064dca1d11
1 /*
2 * Copyright (C) 2008 Brian Paul All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Test MSAA with X/EGL and OpenGL ES 1.x
24 * Brian Paul
25 * 15 September 2008
28 #define USE_FULL_GL 0
31 #include <assert.h>
32 #include <math.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xutil.h>
38 #include <X11/keysym.h>
39 #if USE_FULL_GL
40 #include "gl_wrap.h" /* use full OpenGL */
41 #else
42 #include <GLES/gl.h> /* use OpenGL ES 1.x */
43 #include <GLES/glext.h>
44 #endif
45 #include <EGL/egl.h>
49 static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
50 static GLboolean AA = 0*GL_TRUE;
53 static void
54 draw(void)
56 float a;
58 static const GLfloat verts[4][2] = {
59 { -1, -.1 },
60 { 1, -.1 },
61 { -1, .1 },
62 { 1, .1 }
64 static const GLfloat colors[4][4] = {
65 { 1, 0, 0, 1 },
66 { 0, 1, 0, 1 },
67 { 0, 0, 1, 1 },
68 { 1, 0, 1, 1 }
71 if (AA) {
72 printf("MSAA enabled\n");
73 glEnable(GL_MULTISAMPLE);
75 else {
76 printf("MSAA disabled\n");
77 glDisable(GL_MULTISAMPLE);
80 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
82 glPushMatrix();
83 glRotatef(view_rotx, 1, 0, 0);
84 glRotatef(view_roty, 0, 1, 0);
85 glRotatef(view_rotz, 0, 0, 1);
88 glVertexPointer(2, GL_FLOAT, 0, verts);
89 glColorPointer(4, GL_FLOAT, 0, colors);
91 glEnableClientState(GL_VERTEX_ARRAY);
92 glEnableClientState(GL_COLOR_ARRAY);
94 for (a = 0; a < 360; a += 20.0) {
95 glPushMatrix();
97 glRotatef(a, 0, 0, 1);
98 glTranslatef(1.5, 0, 0);
100 /* draw triangle */
101 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
103 glPopMatrix();
106 glDisableClientState(GL_VERTEX_ARRAY);
107 glDisableClientState(GL_COLOR_ARRAY);
110 glPopMatrix();
114 /* new window size or exposure */
115 static void
116 reshape(int width, int height)
118 GLfloat ary = 3.0;
119 GLfloat arx = ary * (GLfloat) width / (GLfloat) height;
121 glViewport(0, 0, (GLint) width, (GLint) height);
123 glMatrixMode(GL_PROJECTION);
124 glLoadIdentity();
125 #ifdef GL_VERSION_ES_CM_1_0
126 glOrthof(-arx, arx, -ary, ary, -1.0, 1.0);
127 #else
128 glOrtho(-arx, arx, -ary, ary, -1.0, 1.0);
129 #endif
131 glMatrixMode(GL_MODELVIEW);
132 glLoadIdentity();
137 static void
138 init(void)
140 printf("Press 'a' to toggle multisample antialiasing\n");
141 printf("Press 'Esc' to exit\n");
146 * Create an RGB, double-buffered X window.
147 * Return the window and context handles.
149 static void
150 make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
151 const char *name,
152 int x, int y, int width, int height,
153 Window *winRet,
154 EGLContext *ctxRet,
155 EGLSurface *surfRet)
157 static const EGLint attribs[] = {
158 EGL_RED_SIZE, 1,
159 EGL_GREEN_SIZE, 1,
160 EGL_BLUE_SIZE, 1,
161 EGL_DEPTH_SIZE, 1,
162 EGL_SAMPLES, 1,
163 EGL_SAMPLE_BUFFERS, 1,
164 EGL_NONE
167 int scrnum;
168 XSetWindowAttributes attr;
169 unsigned long mask;
170 Window root;
171 Window win;
172 XVisualInfo *visInfo, visTemplate;
173 int num_visuals;
174 EGLContext ctx;
175 EGLConfig config;
176 EGLint num_configs;
177 EGLint vid;
179 scrnum = DefaultScreen( x_dpy );
180 root = RootWindow( x_dpy, scrnum );
182 if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
183 printf("Error: couldn't get an EGL visual config\n");
184 exit(1);
187 if (num_configs < 1) {
188 printf("Error: Unable to find multisample pixel format.\n");
189 printf("Try running glxinfo to see if your server supports MSAA.\n");
190 exit(1);
193 if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
194 printf("Error: eglGetConfigAttrib() failed\n");
195 exit(1);
198 /* The X window visual must match the EGL config */
199 visTemplate.visualid = vid;
200 visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
201 if (!visInfo) {
202 printf("Error: couldn't get X visual\n");
203 exit(1);
206 /* window attributes */
207 attr.background_pixel = 0;
208 attr.border_pixel = 0;
209 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
210 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
211 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
213 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
214 0, visInfo->depth, InputOutput,
215 visInfo->visual, mask, &attr );
217 /* set hints and properties */
219 XSizeHints sizehints;
220 sizehints.x = x;
221 sizehints.y = y;
222 sizehints.width = width;
223 sizehints.height = height;
224 sizehints.flags = USSize | USPosition;
225 XSetNormalHints(x_dpy, win, &sizehints);
226 XSetStandardProperties(x_dpy, win, name, name,
227 None, (char **)NULL, 0, &sizehints);
230 #if USE_FULL_GL
231 eglBindAPI(EGL_OPENGL_API);
232 #else
233 eglBindAPI(EGL_OPENGL_ES_API);
234 #endif
236 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
237 if (!ctx) {
238 printf("Error: eglCreateContext failed\n");
239 exit(1);
242 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
244 if (!*surfRet) {
245 printf("Error: eglCreateWindowSurface failed\n");
246 exit(1);
249 XFree(visInfo);
251 *winRet = win;
252 *ctxRet = ctx;
256 static void
257 event_loop(Display *dpy, Window win,
258 EGLDisplay egl_dpy, EGLSurface egl_surf)
260 while (1) {
261 int redraw = 0;
262 XEvent event;
264 XNextEvent(dpy, &event);
266 switch (event.type) {
267 case Expose:
268 redraw = 1;
269 break;
270 case ConfigureNotify:
271 reshape(event.xconfigure.width, event.xconfigure.height);
272 break;
273 case KeyPress:
275 char buffer[10];
276 int r, code;
277 code = XLookupKeysym(&event.xkey, 0);
278 if (code == XK_Left) {
279 view_roty += 5.0;
281 else if (code == XK_Right) {
282 view_roty -= 5.0;
284 else if (code == XK_Up) {
285 view_rotx += 5.0;
287 else if (code == XK_Down) {
288 view_rotx -= 5.0;
290 else {
291 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
292 NULL, NULL);
293 if (buffer[0] == 'a') {
294 AA = !AA;
295 redraw = 1;
297 else if (buffer[0] == 27) {
298 /* escape */
299 return;
303 redraw = 1;
304 break;
305 default:
306 ; /*no-op*/
309 if (redraw) {
310 draw();
311 eglSwapBuffers(egl_dpy, egl_surf);
317 static void
318 usage(void)
320 printf("Usage:\n");
321 printf(" -display <displayname> set the display to run on\n");
322 printf(" -info display OpenGL renderer info\n");
327 main(int argc, char *argv[])
329 const int winWidth = 600, winHeight = 600;
330 Display *x_dpy;
331 Window win;
332 EGLSurface egl_surf;
333 EGLContext egl_ctx;
334 EGLDisplay egl_dpy;
335 char *dpyName = NULL;
336 GLboolean printInfo = GL_FALSE;
337 EGLint egl_major, egl_minor;
338 int i;
339 const char *s;
341 static struct {
342 char *name;
343 GLenum value;
344 enum {GetString, GetInteger} type;
345 } info_items[] = {
346 {"GL_RENDERER", GL_RENDERER, GetString},
347 {"GL_VERSION", GL_VERSION, GetString},
348 {"GL_VENDOR", GL_VENDOR, GetString},
349 {"GL_EXTENSIONS", GL_EXTENSIONS, GetString},
350 {"GL_MAX_PALETTE_MATRICES_OES", GL_MAX_PALETTE_MATRICES_OES, GetInteger},
351 {"GL_MAX_VERTEX_UNITS_OES", GL_MAX_VERTEX_UNITS_OES, GetInteger},
354 for (i = 1; i < argc; i++) {
355 if (strcmp(argv[i], "-display") == 0) {
356 dpyName = argv[i+1];
357 i++;
359 else if (strcmp(argv[i], "-info") == 0) {
360 printInfo = GL_TRUE;
362 else {
363 usage();
364 return -1;
368 x_dpy = XOpenDisplay(dpyName);
369 if (!x_dpy) {
370 printf("Error: couldn't open display %s\n",
371 dpyName ? dpyName : getenv("DISPLAY"));
372 return -1;
375 egl_dpy = eglGetDisplay(x_dpy);
376 if (!egl_dpy) {
377 printf("Error: eglGetDisplay() failed\n");
378 return -1;
381 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
382 printf("Error: eglInitialize() failed\n");
383 return -1;
386 s = eglQueryString(egl_dpy, EGL_VERSION);
387 printf("EGL_VERSION = %s\n", s);
389 s = eglQueryString(egl_dpy, EGL_VENDOR);
390 printf("EGL_VENDOR = %s\n", s);
392 s = eglQueryString(egl_dpy, EGL_EXTENSIONS);
393 printf("EGL_EXTENSIONS = %s\n", s);
395 s = eglQueryString(egl_dpy, EGL_CLIENT_APIS);
396 printf("EGL_CLIENT_APIS = %s\n", s);
398 make_x_window(x_dpy, egl_dpy,
399 "msaa", 0, 0, winWidth, winHeight,
400 &win, &egl_ctx, &egl_surf);
402 XMapWindow(x_dpy, win);
403 if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
404 printf("Error: eglMakeCurrent() failed\n");
405 return -1;
408 if (printInfo) {
409 for (i = 0; i < sizeof(info_items)/sizeof(info_items[0]); i++) {
410 switch (info_items[i].type) {
411 case GetString:
412 printf("%s = %s\n", info_items[i].name, (char *)glGetString(info_items[i].value));
413 break;
414 case GetInteger: {
415 GLint rv = -1;
416 glGetIntegerv(info_items[i].value, &rv);
417 printf("%s = %d\n", info_items[i].name, rv);
418 break;
423 init();
425 /* Set initial projection/viewing transformation.
426 * We can't be sure we'll get a ConfigureNotify event when the window
427 * first appears.
429 reshape(winWidth, winHeight);
431 event_loop(x_dpy, win, egl_dpy, egl_surf);
433 eglDestroyContext(egl_dpy, egl_ctx);
434 eglDestroySurface(egl_dpy, egl_surf);
435 eglTerminate(egl_dpy);
438 XDestroyWindow(x_dpy, win);
439 XCloseDisplay(x_dpy);
441 return 0;