diff options
Diffstat (limited to 'progs')
51 files changed, 5984 insertions, 94 deletions
diff --git a/progs/SConscript b/progs/SConscript index 3b180d00bc2..66a1745271d 100644 --- a/progs/SConscript +++ b/progs/SConscript @@ -15,38 +15,39 @@ if progs_env['platform'] == 'windows': 'gdi32', ]) -# OpenGL -if progs_env['platform'] == 'windows': - progs_env.Prepend(LIBS = ['glu32', 'opengl32']) -else: - progs_env.Prepend(LIBS = ['GLU', 'GL']) - -# Glut -progs_env.Prepend(LIBS = [glut]) - -# GLEW -progs_env.Prepend(LIBS = [glew]) - -progs_env.Prepend(CPPPATH = [ - '#progs/util', -]) - -progs_env.Prepend(LIBS = [ - util, -]) - -Export('progs_env') - -SConscript([ - 'demos/SConscript', - 'glsl/SConscript', - 'redbook/SConscript', - 'samples/SConscript', - 'tests/SConscript', - 'trivial/SConscript', - 'vp/SConscript', - 'vpglsl/SConscript', - 'fp/SConscript', - 'wgl/SConscript', - 'perf/SConscript', -]) +if platform != 'embedded': + # OpenGL + if progs_env['platform'] == 'windows': + progs_env.Prepend(LIBS = ['glu32', 'opengl32']) + else: + progs_env.Prepend(LIBS = ['GLU', 'GL']) + + # Glut + progs_env.Prepend(LIBS = [glut]) + + # GLEW + progs_env.Prepend(LIBS = [glew]) + + progs_env.Prepend(CPPPATH = [ + '#progs/util', + ]) + + progs_env.Prepend(LIBS = [ + util, + ]) + + Export('progs_env') + + SConscript([ + 'demos/SConscript', + 'glsl/SConscript', + 'redbook/SConscript', + 'samples/SConscript', + 'tests/SConscript', + 'trivial/SConscript', + 'vp/SConscript', + 'vpglsl/SConscript', + 'fp/SConscript', + 'wgl/SConscript', + 'perf/SConscript', + ]) diff --git a/progs/demos/Makefile b/progs/demos/Makefile index 65fdbaaad8e..5b1d2a0b65b 100644 --- a/progs/demos/Makefile +++ b/progs/demos/Makefile @@ -20,6 +20,7 @@ PROGS = \ copypix \ cubemap \ dinoshade \ + dissolve \ drawpix \ engine \ fbo_firecube \ @@ -124,13 +125,6 @@ reflect.o: reflect.c showbuffer.h $(APP_CC) -c -I$(INCDIR) $(CFLAGS) reflect.c -shadowtex: shadowtex.o showbuffer.o - $(APP_CC) $(CFLAGS) $(LDFLAGS) shadowtex.o showbuffer.o $(LIBS) -o $@ - -shadowtex.o: shadowtex.c showbuffer.h - $(APP_CC) -c -I$(INCDIR) $(CFLAGS) shadowtex.c - - gloss: gloss.o trackball.o readtex.o $(APP_CC) $(CFLAGS) $(LDFLAGS) gloss.o trackball.o readtex.o $(LIBS) -o $@ diff --git a/progs/demos/SConscript b/progs/demos/SConscript index 742dd66f36f..10d53b50bf5 100644 --- a/progs/demos/SConscript +++ b/progs/demos/SConscript @@ -8,6 +8,7 @@ progs = [ 'clearspd', 'copypix', 'cubemap', + 'dissolve', 'drawpix', 'engine', 'fbo_firecube', diff --git a/progs/demos/dissolve.c b/progs/demos/dissolve.c new file mode 100644 index 00000000000..f1589a4e413 --- /dev/null +++ b/progs/demos/dissolve.c @@ -0,0 +1,158 @@ +/** + * Dissolve between two images using randomized stencil buffer + * and varying stencil ref. + * + * Brian Paul + * 29 Jan 2010 + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <GL/glut.h> +#include "readtex.h" + +#define FILE1 "../images/bw.rgb" +#define FILE2 "../images/arch.rgb" + + +static int Win; +static int WinWidth = 400, WinHeight = 400; +static GLboolean Anim = GL_TRUE; + +static int ImgWidth[2], ImgHeight[2]; +static GLenum ImgFormat[2]; +static GLubyte *Image[2]; +static GLfloat ScaleX[2], ScaleY[2]; + +static GLubyte StencilRef = 0; + + +static void +Idle(void) +{ + StencilRef = (GLint) (glutGet(GLUT_ELAPSED_TIME) / 10); + glutPostRedisplay(); +} + + +static void +RandomizeStencilBuffer(void) +{ + GLubyte *b = malloc(WinWidth * WinHeight); + int i; + for (i = 0; i < WinWidth * WinHeight; i++) { + b[i] = rand() & 0xff; + } + + glStencilFunc(GL_ALWAYS, 0, ~0); + glPixelZoom(1.0, 1.0); + glDrawPixels(WinWidth, WinHeight, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, b); + + free(b); +} + + + +static void +Draw(void) +{ + glClear(GL_COLOR_BUFFER_BIT); + + glPixelZoom(ScaleX[0], ScaleY[0]); + glStencilFunc(GL_LESS, StencilRef, ~0); + glDrawPixels(ImgWidth[0], ImgHeight[0], ImgFormat[0], GL_UNSIGNED_BYTE, Image[0]); + + glPixelZoom(ScaleX[1], ScaleY[1]); + glStencilFunc(GL_GEQUAL, StencilRef, ~0); + glDrawPixels(ImgWidth[1], ImgHeight[1], ImgFormat[1], GL_UNSIGNED_BYTE, Image[1]); + + glutSwapBuffers(); +} + + +static void +Reshape(int width, int height) +{ + WinWidth = width; + WinHeight = height; + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -15.0); + + RandomizeStencilBuffer(); + + ScaleX[0] = (float) width / ImgWidth[0]; + ScaleY[0] = (float) height / ImgHeight[0]; + + ScaleX[1] = (float) width / ImgWidth[1]; + ScaleY[1] = (float) height / ImgHeight[1]; +} + + +static void +Key(unsigned char key, int x, int y) +{ + (void) x; + (void) y; + switch (key) { + case 'a': + Anim = !Anim; + if (Anim) + glutIdleFunc(Idle); + else + glutIdleFunc(NULL); + break; + case 27: + glutDestroyWindow(Win); + exit(0); + break; + } + glutPostRedisplay(); +} + + + +static void +Init(void) +{ + Image[0] = LoadRGBImage(FILE1, &ImgWidth[0], &ImgHeight[0], &ImgFormat[0]); + if (!Image[0]) { + printf("Couldn't read %s\n", FILE1); + exit(0); + } + + Image[1] = LoadRGBImage(FILE2, &ImgWidth[1], &ImgHeight[1], &ImgFormat[1]); + if (!Image[1]) { + printf("Couldn't read %s\n", FILE1); + exit(0); + } + + glEnable(GL_STENCIL_TEST); + glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); +} + + +int +main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitWindowSize(WinWidth, WinHeight); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL); + Win = glutCreateWindow(argv[0]); + glutReshapeFunc(Reshape); + glutKeyboardFunc(Key); + glutDisplayFunc(Draw); + if (Anim) + glutIdleFunc(Idle); + Init(); + glutMainLoop(); + return 0; +} diff --git a/progs/demos/shadowtex.c b/progs/demos/shadowtex.c index 036f73d40b6..677a42104f8 100644 --- a/progs/demos/shadowtex.c +++ b/progs/demos/shadowtex.c @@ -38,7 +38,6 @@ #include <math.h> #include <GL/glew.h> #include <GL/glut.h> -#include "showbuffer.h" #define DEG_TO_RAD (3.14159 / 180.0) diff --git a/progs/egl/Makefile b/progs/egl/Makefile index ff9a858c569..0ddb2cabb4f 100644 --- a/progs/egl/Makefile +++ b/progs/egl/Makefile @@ -20,7 +20,6 @@ PROGRAMS = \ eglgears \ eglscreen \ peglgears \ - xeglbindtex \ xeglgears \ xeglthreads \ xegl_tri @@ -46,7 +45,7 @@ egltri: egltri.o $(HEADERS) $(LIB_DEP) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS) $(LIBDRM_LIB) eglinfo: eglinfo.o $(HEADERS) $(LIB_DEP) - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS) $(LIBDRM_LIB) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS) eglgears: eglgears.o $(HEADERS) $(LIB_DEP) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS) $(LIBDRM_LIB) @@ -57,9 +56,6 @@ eglscreen: eglscreen.o $(HEADERS) $(LIB_DEP) peglgears: peglgears.o $(HEADERS) $(LIB_DEP) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS) $(LIBDRM_LIB) -xeglbindtex: xeglbindtex.o $(HEADERS) $(LIB_DEP) - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS) -lX11 - xeglgears: xeglgears.o $(HEADERS) $(LIB_DEP) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIBS) -lX11 diff --git a/progs/egl/eglinfo.c b/progs/egl/eglinfo.c index 4486916e958..9664667a68a 100644 --- a/progs/egl/eglinfo.c +++ b/progs/egl/eglinfo.c @@ -49,16 +49,15 @@ PrintConfigs(EGLDisplay d) eglGetConfigs(d, configs, MAX_CONFIGS, &numConfigs); printf("Configurations:\n"); - printf(" bf lv d st colorbuffer dp st ms vis supported\n"); - printf(" id sz l b ro r g b a th cl ns b id surfaces \n"); - printf("--------------------------------------------------------\n"); + printf(" bf lv colorbuffer dp st ms vis cav bi renderable supported\n"); + printf(" id sz l r g b a th cl ns b id eat nd gl es es2 vg surfaces \n"); + printf("---------------------------------------------------------------------\n"); for (i = 0; i < numConfigs; i++) { EGLint id, size, level; EGLint red, green, blue, alpha; EGLint depth, stencil; - EGLint surfaces; - EGLint doubleBuf = 1, stereo = 0; - EGLint vid; + EGLint renderable, surfaces; + EGLint vid, caveat, bindRgb, bindRgba; EGLint samples, sampleBuffers; char surfString[100] = ""; @@ -73,6 +72,11 @@ PrintConfigs(EGLDisplay d) eglGetConfigAttrib(d, configs[i], EGL_DEPTH_SIZE, &depth); eglGetConfigAttrib(d, configs[i], EGL_STENCIL_SIZE, &stencil); eglGetConfigAttrib(d, configs[i], EGL_NATIVE_VISUAL_ID, &vid); + + eglGetConfigAttrib(d, configs[i], EGL_CONFIG_CAVEAT, &caveat); + eglGetConfigAttrib(d, configs[i], EGL_BIND_TO_TEXTURE_RGB, &bindRgb); + eglGetConfigAttrib(d, configs[i], EGL_BIND_TO_TEXTURE_RGBA, &bindRgba); + eglGetConfigAttrib(d, configs[i], EGL_RENDERABLE_TYPE, &renderable); eglGetConfigAttrib(d, configs[i], EGL_SURFACE_TYPE, &surfaces); eglGetConfigAttrib(d, configs[i], EGL_SAMPLES, &samples); @@ -91,13 +95,19 @@ PrintConfigs(EGLDisplay d) if (strlen(surfString) > 0) surfString[strlen(surfString) - 1] = 0; - printf("0x%02x %2d %2d %c %c %2d %2d %2d %2d %2d %2d %2d%2d 0x%02x %-12s\n", + printf("0x%02x %2d %2d %2d %2d %2d %2d %2d %2d %2d%2d 0x%03x ", id, size, level, - doubleBuf ? 'y' : '.', - stereo ? 'y' : '.', red, green, blue, alpha, depth, stencil, - samples, sampleBuffers, vid, surfString); + samples, sampleBuffers, vid); + printf(" %c %c %c %c %c %c %s\n", + (caveat != EGL_NONE) ? 'y' : ' ', + (bindRgba) ? 'a' : (bindRgb) ? 'y' : ' ', + (renderable & EGL_OPENGL_BIT) ? 'y' : ' ', + (renderable & EGL_OPENGL_ES_BIT) ? 'y' : ' ', + (renderable & EGL_OPENGL_ES2_BIT) ? 'y' : ' ', + (renderable & EGL_OPENVG_BIT) ? 'y' : ' ', + surfString); } } diff --git a/progs/egl/xeglgears.c b/progs/egl/xeglgears.c index 614a6256037..9fdf4742441 100644 --- a/progs/egl/xeglgears.c +++ b/progs/egl/xeglgears.c @@ -604,7 +604,7 @@ event_loop(struct egl_manager *eman, EGLint surface_type, EGLint w, EGLint h) break; case EGL_PBUFFER_BIT: eglWaitClient(); - if (!eglCopyBuffers(eman->xdpy, eman->pbuf, eman->xpix)) + if (!eglCopyBuffers(eman->dpy, eman->pbuf, eman->xpix)) break; /* fall through */ case EGL_PIXMAP_BIT: diff --git a/progs/es1/.gitignore b/progs/es1/.gitignore new file mode 100644 index 00000000000..4f1427531a8 --- /dev/null +++ b/progs/es1/.gitignore @@ -0,0 +1,10 @@ +screen/gears +screen/tri +xegl/drawtex +xegl/es1_info +xegl/msaa +xegl/pbuffer +xegl/render_tex +xegl/torus +xegl/tri +xegl/two_win diff --git a/progs/es1/screen/Makefile b/progs/es1/screen/Makefile new file mode 100644 index 00000000000..4ba2f9a7dc1 --- /dev/null +++ b/progs/es1/screen/Makefile @@ -0,0 +1,32 @@ +# progs/es1/screen/Makefile + +TOP = ../../.. +include $(TOP)/configs/current + +ES1_CFLAGS = -I$(TOP)/include +ES1_LIBS = -L$(TOP)/$(LIB_DIR) -lEGL -lGLESv1_CM + +ES1_LIB_DEPS = \ + $(TOP)/$(LIB_DIR)/libEGL.so \ + $(TOP)/$(LIB_DIR)/libGLESv1_CM.so + +WINSYS_OBJS = winsys.o + +PROGRAMS = \ + gears \ + tri + +.c.o: + $(CC) -c $(ES1_CFLAGS) $(CFLAGS) $< -o $@ + +default: $(PROGRAMS) + +gears: gears.o $(WINSYS_OBJS) $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) -o $@ [email protected] $(WINSYS_OBJS) $(ES1_LIBS) + +tri: tri.o $(WINSYS_OBJS) $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) -o $@ [email protected] $(WINSYS_OBJS) $(ES1_LIBS) + +clean: + -rm -f *.o *~ + -rm -f $(PROGRAMS) diff --git a/progs/es1/screen/gears.c b/progs/es1/screen/gears.c new file mode 100644 index 00000000000..c7625826b97 --- /dev/null +++ b/progs/es1/screen/gears.c @@ -0,0 +1,374 @@ +/* + * Copyright (C) 2009 Chia-I Wu <[email protected]> + * + * Based on eglgears by + * Copyright (C) 1999-2001 Brian Paul 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 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 + * BRIAN PAUL 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. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <math.h> +#include <assert.h> + +#include <GLES/gl.h> +#include "winsys.h" + +#ifndef M_PI +#define M_PI 3.14159265 +#endif + + +struct gear { + GLuint vbo; + GLfloat *vertices; + GLsizei stride; + + GLint num_teeth; +}; + +static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0; +static struct gear gears[3]; +static GLfloat angle = 0.0; + +/* + * Initialize a gear wheel. + * + * Input: gear - gear to initialize + * inner_radius - radius of hole at center + * outer_radius - radius at center of teeth + * width - width of gear + * teeth - number of teeth + * tooth_depth - depth of tooth + */ +static void +init_gear(struct gear *gear, GLfloat inner_radius, GLfloat outer_radius, + GLfloat width, GLint teeth, GLfloat tooth_depth) +{ + GLfloat r0, r1, r2; + GLfloat a0, da; + GLint verts_per_tooth, total_verts, total_size; + GLint count, i; + GLfloat *verts; + + r0 = inner_radius; + r1 = outer_radius - tooth_depth / 2.0; + r2 = outer_radius + tooth_depth / 2.0; + + a0 = 2.0 * M_PI / teeth; + da = a0 / 4.0; + + gear->vbo = 0; + gear->vertices = NULL; + gear->stride = sizeof(GLfloat) * 6; /* XYZ + normal */ + gear->num_teeth = teeth; + + verts_per_tooth = 10 + 4; + total_verts = teeth * verts_per_tooth; + total_size = total_verts * gear->stride; + + verts = malloc(total_size); + if (!verts) { + printf("failed to allocate vertices\n"); + return; + } + +#define GEAR_VERT(r, n, sign) \ + do { \ + verts[count * 6 + 0] = (r) * vx[n]; \ + verts[count * 6 + 1] = (r) * vy[n]; \ + verts[count * 6 + 2] = (sign) * width * 0.5; \ + verts[count * 6 + 3] = normal[0]; \ + verts[count * 6 + 4] = normal[1]; \ + verts[count * 6 + 5] = normal[2]; \ + count++; \ + } while (0) + + count = 0; + for (i = 0; i < teeth; i++) { + GLfloat normal[3]; + GLfloat vx[5], vy[5]; + GLfloat u, v; + + normal[0] = 0.0; + normal[1] = 0.0; + normal[2] = 0.0; + + vx[0] = cos(i * a0 + 0 * da); + vy[0] = sin(i * a0 + 0 * da); + vx[1] = cos(i * a0 + 1 * da); + vy[1] = sin(i * a0 + 1 * da); + vx[2] = cos(i * a0 + 2 * da); + vy[2] = sin(i * a0 + 2 * da); + vx[3] = cos(i * a0 + 3 * da); + vy[3] = sin(i * a0 + 3 * da); + vx[4] = cos(i * a0 + 4 * da); + vy[4] = sin(i * a0 + 4 * da); + + /* outward faces of a tooth, 10 verts */ + normal[0] = vx[0]; + normal[1] = vy[0]; + GEAR_VERT(r1, 0, 1); + GEAR_VERT(r1, 0, -1); + + u = r2 * vx[1] - r1 * vx[0]; + v = r2 * vy[1] - r1 * vy[0]; + normal[0] = v; + normal[1] = -u; + GEAR_VERT(r2, 1, 1); + GEAR_VERT(r2, 1, -1); + + normal[0] = vx[0]; + normal[1] = vy[0]; + GEAR_VERT(r2, 2, 1); + GEAR_VERT(r2, 2, -1); + + u = r1 * vx[3] - r2 * vx[2]; + v = r1 * vy[3] - r2 * vy[2]; + normal[0] = v; + normal[1] = -u; + GEAR_VERT(r1, 3, 1); + GEAR_VERT(r1, 3, -1); + + normal[0] = vx[0]; + normal[1] = vy[0]; + GEAR_VERT(r1, 4, 1); + GEAR_VERT(r1, 4, -1); + + /* inside radius cylinder, 4 verts */ + normal[0] = -vx[4]; + normal[1] = -vy[4]; + GEAR_VERT(r0, 4, 1); + GEAR_VERT(r0, 4, -1); + + normal[0] = -vx[0]; + normal[1] = -vy[0]; + GEAR_VERT(r0, 0, 1); + GEAR_VERT(r0, 0, -1); + + assert(count % verts_per_tooth == 0); + } + assert(count == total_verts); +#undef GEAR_VERT + + gear->vertices = verts; + + /* setup VBO */ + glGenBuffers(1, &gear->vbo); + if (gear->vbo) { + glBindBuffer(GL_ARRAY_BUFFER, gear->vbo); + glBufferData(GL_ARRAY_BUFFER, total_size, verts, GL_STATIC_DRAW); + } +} + + +static void +draw_gear(const struct gear *gear) +{ + GLint i; + + if (!gear->vbo && !gear->vertices) { + printf("nothing to be drawn\n"); + return; + } + + if (gear->vbo) { + glBindBuffer(GL_ARRAY_BUFFER, gear->vbo); + glVertexPointer(3, GL_FLOAT, gear->stride, (const GLvoid *) 0); + glNormalPointer(GL_FLOAT, gear->stride, (const GLvoid *) (sizeof(GLfloat) * 3)); + } else { + glBindBuffer(GL_ARRAY_BUFFER, 0); + glVertexPointer(3, GL_FLOAT, gear->stride, gear->vertices); + glNormalPointer(GL_FLOAT, gear->stride, gear->vertices + 3); + } + + glEnableClientState(GL_VERTEX_ARRAY); + + for (i = 0; i < gear->num_teeth; i++) { + const GLint base = (10 + 4) * i; + GLushort indices[7]; + + glShadeModel(GL_FLAT); + + /* front face */ + indices[0] = base + 12; + indices[1] = base + 0; + indices[2] = base + 2; + indices[3] = base + 4; + indices[4] = base + 6; + indices[5] = base + 8; + indices[6] = base + 10; + + glNormal3f(0.0, 0.0, 1.0); + glDrawElements(GL_TRIANGLE_FAN, 7, GL_UNSIGNED_SHORT, indices); + + /* back face */ + indices[0] = base + 13; + indices[1] = base + 11; + indices[2] = base + 9; + indices[3] = base + 7; + indices[4] = base + 5; + indices[5] = base + 3; + indices[6] = base + 1; + + glNormal3f(0.0, 0.0, -1.0); + glDrawElements(GL_TRIANGLE_FAN, 7, GL_UNSIGNED_SHORT, indices); + + glEnableClientState(GL_NORMAL_ARRAY); + + /* outward face of a tooth */ + glDrawArrays(GL_TRIANGLE_STRIP, base, 10); + + /* inside radius cylinder */ + glShadeModel(GL_SMOOTH); + glDrawArrays(GL_TRIANGLE_STRIP, base + 10, 4); + + glDisableClientState(GL_NORMAL_ARRAY); + } + + glDisableClientState(GL_VERTEX_ARRAY); +} + + +static void +gears_draw(void *data) +{ + static const GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 }; + static const GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 }; + static const GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 }; + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glRotatef(view_rotx, 1.0, 0.0, 0.0); + glRotatef(view_roty, 0.0, 1.0, 0.0); + glRotatef(view_rotz, 0.0, 0.0, 1.0); + + glPushMatrix(); + glTranslatef(-3.0, -2.0, 0.0); + glRotatef(angle, 0.0, 0.0, 1.0); + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red); + draw_gear(&gears[0]); + + glPopMatrix(); + + glPushMatrix(); + glTranslatef(3.1, -2.0, 0.0); + glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0); + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green); + draw_gear(&gears[1]); + + glPopMatrix(); + + glPushMatrix(); + glTranslatef(-3.1, 4.2, 0.0); + glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0); + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue); + draw_gear(&gears[2]); + + glPopMatrix(); + + glPopMatrix(); + + /* advance rotation for next frame */ + angle += 0.5; /* 0.5 degree per frame */ + if (angle > 3600.0) + angle -= 3600.0; +} + + +static void gears_fini(void) +{ + GLint i; + for (i = 0; i < 3; i++) { + struct gear *gear = &gears[i]; + if (gear->vbo) { + glDeleteBuffers(1, &gear->vbo); + gear->vbo = 0; + } + if (gear->vertices) { + free(gear->vertices); + gear->vertices = NULL; + } + } +} + + +static void gears_init(void) +{ + static const GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 }; + + glLightfv(GL_LIGHT0, GL_POSITION, pos); + glEnable(GL_CULL_FACE); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glEnable(GL_DEPTH_TEST); + glEnable(GL_NORMALIZE); + + init_gear(&gears[0], 1.0, 4.0, 1.0, 20, 0.7); + init_gear(&gears[1], 0.5, 2.0, 2.0, 10, 0.7); + init_gear(&gears[2], 1.3, 2.0, 0.5, 10, 0.7); +} + + +/* new window size or exposure */ +static void +gears_reshape(int width, int height) +{ + GLfloat h = (GLfloat) height / (GLfloat) width; + + glViewport(0, 0, (GLint) width, (GLint) height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustumf(-1.0, 1.0, -h, h, 5.0, 60.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -40.0); +} + + +static void gears_run(void) +{ + winsysRun(5.0, gears_draw, NULL); +} + + +int +main(int argc, char *argv[]) +{ + EGLint width, height; + + if (!winsysInitScreen()) + exit(1); + winsysQueryScreenSize(&width, &height); + + gears_init(); + gears_reshape(width, height); + gears_run(); + gears_fini(); + + winsysFiniScreen(); + + return 0; +} diff --git a/progs/es1/screen/tri.c b/progs/es1/screen/tri.c new file mode 100644 index 00000000000..bab9499944b --- /dev/null +++ b/progs/es1/screen/tri.c @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2009 Chia-I Wu <[email protected]> + * + * Based on egltri by + * Copyright (C) 1999-2001 Brian Paul All Rights Reserved. + * Copyright (C) 2008 Brian Paul All Rights Reserved. + * Copyright (C) 2008 Jakob Bornecrantz 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 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 + * BRIAN PAUL 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. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <GLES/gl.h> +#include "winsys.h" + +static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0; + +static void tri_init() +{ + glClearColor(0.4, 0.4, 0.4, 0.0); +} + +static void tri_reshape(int width, int height) +{ + GLfloat ar = (GLfloat) width / (GLfloat) height; + + glViewport(0, 0, (GLint) width, (GLint) height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustumf(-ar, ar, -1, 1, 5.0, 60.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -10.0); +} + +static void tri_draw(void *data) +{ + static const GLfloat verts[3][2] = { + { -1, -1 }, + { 1, -1 }, + { 0, 1 } + }; + static const GLfloat colors[3][4] = { + { 1, 0, 0, 1 }, + { 0, 1, 0, 1 }, + { 0, 0, 1, 1 } + }; + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glRotatef(view_rotx, 1, 0, 0); + glRotatef(view_roty, 0, 1, 0); + glRotatef(view_rotz, 0, 0, 1); + + { + glVertexPointer(2, GL_FLOAT, 0, verts); + glColorPointer(4, GL_FLOAT, 0, colors); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + } + + glPopMatrix(); +} + +static void tri_run(void) +{ + winsysRun(3.0, tri_draw, NULL); +} + +int main(int argc, char *argv[]) +{ + EGLint width, height; + GLboolean printInfo = GL_FALSE; + int i; + + /* parse cmd line args */ + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-info") == 0) { + printInfo = GL_TRUE; + } + else { + printf("Warning: unknown parameter: %s\n", argv[i]); + } + } + + if (!winsysInitScreen()) + exit(1); + winsysQueryScreenSize(&width, &height); + + if (printInfo) { + printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); + printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); + printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); + printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); + } + + tri_init(); + tri_reshape(width, height); + tri_run(); + + winsysFiniScreen(); + + return 0; +} diff --git a/progs/es1/screen/winsys.c b/progs/es1/screen/winsys.c new file mode 100644 index 00000000000..84d00471eba --- /dev/null +++ b/progs/es1/screen/winsys.c @@ -0,0 +1,272 @@ +/* + * Copyright (C) 2009 Chia-I Wu <[email protected]> + * + * Based on eglgears by + * Copyright (C) 1999-2001 Brian Paul 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 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 + * BRIAN PAUL 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. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> +#include <sys/time.h> + +#define EGL_EGLEXT_PROTOTYPES + +#include <EGL/egl.h> +#include <EGL/eglext.h> + +#include "winsys.h" + +#define MAX_MODES 100 + +static struct { + EGLBoolean verbose; + + EGLDisplay dpy; + EGLConfig conf; + + EGLScreenMESA screen; + EGLModeMESA mode; + EGLint width, height; + + EGLContext ctx; + EGLSurface surf; +} screen; + + +static EGLBoolean +init_screen(void) +{ + EGLModeMESA modes[MAX_MODES]; + EGLint num_screens, num_modes; + EGLint width, height, best_mode; + EGLint i; + + if (!eglGetScreensMESA(screen.dpy, &screen.screen, 1, &num_screens) || + !num_screens) { + printf("eglGetScreensMESA failed\n"); + return EGL_FALSE; + } + + if (!eglGetModesMESA(screen.dpy, screen.screen, modes, MAX_MODES, + &num_modes) || + !num_modes) { + printf("eglGetModesMESA failed!\n"); + return EGL_FALSE; + } + + printf("Found %d modes:\n", num_modes); + + best_mode = 0; + width = 0; + height = 0; + for (i = 0; i < num_modes; i++) { + EGLint w, h; + eglGetModeAttribMESA(screen.dpy, modes[i], EGL_WIDTH, &w); + eglGetModeAttribMESA(screen.dpy, modes[i], EGL_HEIGHT, &h); + printf("%3d: %d x %d\n", i, w, h); + if (w > width && h > height) { + width = w; + height = h; + best_mode = i; + } + } + + screen.mode = modes[best_mode]; + screen.width = width; + screen.height = height; + + return EGL_TRUE; +} + + +static EGLBoolean +init_display(void) +{ + EGLint maj, min; + const char *exts; + const EGLint attribs[] = { + EGL_SURFACE_TYPE, 0x0, /* should be EGL_SCREEN_BIT_MESA */ + EGL_RENDERABLE_TYPE, 0x0, /* should be EGL_OPENGL_ES_BIT */ + EGL_NONE + }; + EGLint num_configs; + + screen.dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); + if (!screen.dpy) { + printf("eglGetDisplay failed\n"); + return EGL_FALSE; + } + + if (!eglInitialize(screen.dpy, &maj, &min)) { + printf("eglInitialize failed\n"); + return EGL_FALSE; + } + + printf("EGL_VERSION = %s\n", eglQueryString(screen.dpy, EGL_VERSION)); + printf("EGL_VENDOR = %s\n", eglQueryString(screen.dpy, EGL_VENDOR)); + + exts = eglQueryString(screen.dpy, EGL_EXTENSIONS); + assert(exts); + + if (!strstr(exts, "EGL_MESA_screen_surface")) { + printf("EGL_MESA_screen_surface is not supported\n"); + return EGL_FALSE; + } + + if (!eglChooseConfig(screen.dpy, attribs, &screen.conf, 1, + &num_configs) || + !num_configs) { + printf("eglChooseConfig failed\n"); + return EGL_FALSE; + } + + return EGL_TRUE; +} + + +EGLBoolean +winsysInitScreen(void) +{ + EGLint surf_attribs[20]; + EGLint i; + EGLBoolean ok; + + if (!init_display()) + goto fail; + if (!init_screen()) + goto fail; + + /* create context */ + screen.ctx = eglCreateContext(screen.dpy, screen.conf, + EGL_NO_CONTEXT, NULL); + if (screen.ctx == EGL_NO_CONTEXT) { + printf("eglCreateContext failed\n"); + goto fail; + } + + i = 0; + surf_attribs[i++] = EGL_WIDTH; + surf_attribs[i++] = screen.width; + surf_attribs[i++] = EGL_HEIGHT; + surf_attribs[i++] = screen.height; + surf_attribs[i++] = EGL_NONE; + + /* create surface */ + printf("Using screen size: %d x %d\n", screen.width, screen.height); + screen.surf = eglCreateScreenSurfaceMESA(screen.dpy, screen.conf, + surf_attribs); + if (screen.surf == EGL_NO_SURFACE) { + printf("eglCreateScreenSurfaceMESA failed\n"); + goto fail; + } + + ok = eglMakeCurrent(screen.dpy, screen.surf, screen.surf, screen.ctx); + if (!ok) { + printf("eglMakeCurrent failed\n"); + goto fail; + } + + ok = eglShowScreenSurfaceMESA(screen.dpy, screen.screen, + screen.surf, screen.mode); + if (!ok) { + printf("eglShowScreenSurfaceMESA failed\n"); + goto fail; + } + + return EGL_TRUE; + +fail: + winsysFiniScreen(); + return EGL_FALSE; +} + + +EGLBoolean +winsysQueryScreenSize(EGLint *width, EGLint *height) +{ + if (!screen.dpy) + return EGL_FALSE; + + if (width) + *width = screen.width; + if (height) + *height = screen.height; + + return EGL_TRUE; +} + + +void +winsysFiniScreen(void) +{ + if (screen.dpy) { + eglMakeCurrent(screen.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, + EGL_NO_CONTEXT); + if (screen.surf != EGL_NO_SURFACE) + eglDestroySurface(screen.dpy, screen.surf); + if (screen.ctx != EGL_NO_CONTEXT) + eglDestroyContext(screen.dpy, screen.ctx); + eglTerminate(screen.dpy); + + memset(&screen, 0, sizeof(screen)); + } +} + + +void +winsysSwapBuffers(void) +{ + eglSwapBuffers(screen.dpy, screen.surf); +} + + +/* return current time (in seconds) */ +double +winsysNow(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (double) tv.tv_sec + tv.tv_usec / 1000000.0; +} + + +void +winsysRun(double seconds, void (*draw_frame)(void *data), void *data) +{ + double begin, end, last_frame, duration; + EGLint num_frames = 0; + + begin = winsysNow(); + end = begin + seconds; + + last_frame = begin; + while (last_frame < end) { + draw_frame(data); + winsysSwapBuffers(); + last_frame = winsysNow(); + num_frames++; + } + + duration = last_frame - begin; + printf("%d frames in %3.1f seconds = %6.3f FPS\n", + num_frames, duration, (double) num_frames / duration); +} diff --git a/progs/es1/screen/winsys.h b/progs/es1/screen/winsys.h new file mode 100644 index 00000000000..679c7e0bd6d --- /dev/null +++ b/progs/es1/screen/winsys.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2009 Chia-I Wu <[email protected]> + * + * 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 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 + * BRIAN PAUL 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. + */ + +#ifndef _WINSYS_H_ +#define _WINSYS_H_ + +#include <EGL/egl.h> + +EGLBoolean winsysInitScreen(void); +EGLBoolean winsysQueryScreenSize(EGLint *width, EGLint *height); +void winsysFiniScreen(void); + +void winsysSwapBuffers(void); +double winsysNow(void); + +void winsysRun(double seconds, void (*draw_frame)(void *data), void *data); + +#endif /* _WINSYS_H_ */ diff --git a/progs/es1/xegl/Makefile b/progs/es1/xegl/Makefile new file mode 100644 index 00000000000..6869c443065 --- /dev/null +++ b/progs/es1/xegl/Makefile @@ -0,0 +1,82 @@ +# progs/es1/xegl/Makefile + +TOP = ../../.. +include $(TOP)/configs/current + + +INCLUDE_DIRS = \ + -I$(TOP)/include \ + +HEADERS = $(TOP)/include/GLES/egl.h + + +ES1_LIB_DEPS = \ + $(TOP)/$(LIB_DIR)/libEGL.so \ + $(TOP)/$(LIB_DIR)/libGLESv1_CM.so + + +ES1_LIBS = \ + -L$(TOP)/$(LIB_DIR) -lEGL \ + -L$(TOP)/$(LIB_DIR) -lGLESv1_CM $(LIBDRM_LIB) -lX11 + +PROGRAMS = \ + bindtex \ + drawtex \ + es1_info \ + msaa \ + pbuffer \ + render_tex \ + torus \ + tri \ + two_win + + +.c.o: + $(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $< -o $@ + + + +default: $(PROGRAMS) + + + +bindtex: bindtex.o $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) bindtex.o $(ES1_LIBS) -o $@ + + +drawtex: drawtex.o $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) drawtex.o $(ES1_LIBS) -o $@ + + +es1_info: es1_info.o $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) es1_info.o $(ES1_LIBS) -o $@ + + +msaa: msaa.o $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) msaa.o $(ES1_LIBS) -o $@ + + +pbuffer: pbuffer.o $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) pbuffer.o $(ES1_LIBS) -o $@ + + +render_tex: render_tex.o $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) render_tex.o $(ES1_LIBS) -o $@ + + +torus: torus.o $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) torus.o $(ES1_LIBS) -o $@ + + +two_win: two_win.o $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) two_win.o $(ES1_LIBS) -o $@ + + +tri: tri.o $(ES1_LIB_DEPS) + $(CC) $(CFLAGS) tri.o $(ES1_LIBS) -o $@ + + +clean: + rm -f *.o *~ + rm -f $(PROGRAMS) + diff --git a/progs/egl/xeglbindtex.c b/progs/es1/xegl/bindtex.c index de0ede92ca4..c243b5941b7 100644 --- a/progs/egl/xeglbindtex.c +++ b/progs/es1/xegl/bindtex.c @@ -33,7 +33,7 @@ #include <X11/Xlib.h> #include <X11/Xutil.h> #include <X11/keysym.h> -#include <GL/gl.h> +#include <GLES/gl.h> #include <EGL/egl.h> static EGLDisplay dpy; @@ -53,7 +53,6 @@ make_pbuffer(int width, int height) EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_BIND_TO_TEXTURE_RGB, EGL_TRUE, - EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE }; EGLint pbuf_attribs[] = { @@ -72,7 +71,6 @@ make_pbuffer(int width, int height) exit(1); } - eglBindAPI(EGL_OPENGL_API); ctx_pbuf = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL ); surf_pbuf = eglCreatePbufferSurface(dpy, config, pbuf_attribs); if (surf_pbuf == EGL_NO_SURFACE) { @@ -101,7 +99,7 @@ use_pbuffer(void) glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glFrustum(-ar, ar, -1, 1, 1.0, 10.0); + glFrustumf(-ar, ar, -1, 1, 1.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -128,7 +126,6 @@ make_window(Display *x_dpy, const char *name, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 8, - EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE }; @@ -189,10 +186,9 @@ make_window(Display *x_dpy, const char *name, None, (char **)NULL, 0, &sizehints); } - eglBindAPI(EGL_OPENGL_API); ctx_win = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL ); if (!ctx_win) { - printf("Error: glXCreateContext failed\n"); + printf("Error: eglCreateContext failed\n"); exit(1); } @@ -227,10 +223,10 @@ draw_triangle(void) { 3, -3 }, { 0, 3 } }; - GLfloat colors[3][3] = { - { 1, 0, 0 }, - { 0, 1, 0 }, - { 0, 0, 1 } + GLfloat colors[3][4] = { + { 1, 0, 0, 1 }, + { 0, 1, 0, 1 }, + { 0, 0, 1, 1 } }; GLint i; @@ -250,7 +246,7 @@ draw_triangle(void) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glVertexPointer(2, GL_FLOAT, 0, verts); - glColorPointer(3, GL_FLOAT, 0, colors); + glColorPointer(4, GL_FLOAT, 0, colors); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); @@ -356,7 +352,7 @@ reshape(int width, int height) glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glFrustum(-ar, ar, -1, 1, 5.0, 60.0); + glFrustumf(-ar, ar, -1, 1, 5.0, 60.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); diff --git a/progs/es1/xegl/drawtex.c b/progs/es1/xegl/drawtex.c new file mode 100644 index 00000000000..ca0615e267b --- /dev/null +++ b/progs/es1/xegl/drawtex.c @@ -0,0 +1,427 @@ +/* + * Copyright (C) 2008 Tunsgten Graphics,Inc. All Rights Reserved. + */ + +/* + * Test GL_OES_draw_texture + * Brian Paul + * August 2008 + */ + +#define GL_GLEXT_PROTOTYPES + +#include <assert.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <EGL/egl.h> + + + +static GLfloat view_posx = 10.0, view_posy = 20.0; +static GLfloat width = 200, height = 200; + + +static void +draw(void) +{ + glClear(GL_COLOR_BUFFER_BIT); + + glDrawTexfOES(view_posx, view_posy, 0.0, width, height); +} + + +/* new window size or exposure */ +static void +reshape(int width, int height) +{ + GLfloat ar = (GLfloat) width / (GLfloat) height; + + glViewport(0, 0, (GLint) width, (GLint) height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + +#ifdef GL_VERSION_ES_CM_1_0 + glFrustumf(-ar, ar, -1, 1, 5.0, 60.0); +#else + glFrustum(-ar, ar, -1, 1, 5.0, 60.0); +#endif + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -15.0); +} + + +static float +dist(GLuint i, GLuint j, float x, float y) +{ + return sqrt((i-x) * (i-x) + (j-y) * (j-y)); +} + +static void +make_smile_texture(void) +{ +#define SZ 128 + GLenum Filter = GL_LINEAR; + GLubyte image[SZ][SZ][4]; + GLuint i, j; + GLint cropRect[4]; + + for (i = 0; i < SZ; i++) { + for (j = 0; j < SZ; j++) { + GLfloat d_mouth = dist(i, j, SZ/2, SZ/2); + GLfloat d_rt_eye = dist(i, j, SZ*3/4, SZ*3/4); + GLfloat d_lt_eye = dist(i, j, SZ*3/4, SZ*1/4); + if (d_rt_eye < SZ / 8 || d_lt_eye < SZ / 8) { + image[i][j][0] = 20; + image[i][j][1] = 50; + image[i][j][2] = 255; + image[i][j][3] = 255; + } + else if (i < SZ/2 && d_mouth < SZ/3) { + image[i][j][0] = 255; + image[i][j][1] = 20; + image[i][j][2] = 20; + image[i][j][3] = 255; + } + else { + image[i][j][0] = 200; + image[i][j][1] = 200; + image[i][j][2] = 200; + image[i][j][3] = 255; + } + } + } + + glActiveTexture(GL_TEXTURE0); /* unit 0 */ + glBindTexture(GL_TEXTURE_2D, 42); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ, SZ, 0, + GL_RGBA, GL_UNSIGNED_BYTE, image); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + + cropRect[0] = 0; + cropRect[1] = 0; + cropRect[2] = SZ; + cropRect[3] = SZ; + glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect); +#undef SZ +} + + + +static void +init(void) +{ + const char *ext = (char *) glGetString(GL_EXTENSIONS); + + if (!strstr(ext, "GL_OES_draw_texture")) { + fprintf(stderr, "Sorry, this program requires GL_OES_draw_texture"); + exit(1); + } + + glClearColor(0.4, 0.4, 0.4, 0.0); + + make_smile_texture(); + glEnable(GL_TEXTURE_2D); +} + + +/* + * Create an RGB, double-buffered X window. + * Return the window and context handles. + */ +static void +make_x_window(Display *x_dpy, EGLDisplay egl_dpy, + const char *name, + int x, int y, int width, int height, + Window *winRet, + EGLContext *ctxRet, + EGLSurface *surfRet) +{ + static const EGLint attribs[] = { + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_NONE + }; + + int scrnum; + XSetWindowAttributes attr; + unsigned long mask; + Window root; + Window win; + XVisualInfo *visInfo, visTemplate; + int num_visuals; + EGLContext ctx; + EGLConfig config; + EGLint num_configs; + EGLint vid; + + scrnum = DefaultScreen( x_dpy ); + root = RootWindow( x_dpy, scrnum ); + + if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL visual config\n"); + exit(1); + } + + assert(config); + assert(num_configs > 0); + + if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { + printf("Error: eglGetConfigAttrib() failed\n"); + exit(1); + } + + /* The X window visual must match the EGL config */ + visTemplate.visualid = vid; + visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); + if (!visInfo) { + printf("Error: couldn't get X visual\n"); + exit(1); + } + + /* window attributes */ + attr.background_pixel = 0; + attr.border_pixel = 0; + attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + win = XCreateWindow( x_dpy, root, 0, 0, width, height, + 0, visInfo->depth, InputOutput, + visInfo->visual, mask, &attr ); + + /* set hints and properties */ + { + XSizeHints sizehints; + sizehints.x = x; + sizehints.y = y; + sizehints.width = width; + sizehints.height = height; + sizehints.flags = USSize | USPosition; + XSetNormalHints(x_dpy, win, &sizehints); + XSetStandardProperties(x_dpy, win, name, name, + None, (char **)NULL, 0, &sizehints); + } + + eglBindAPI(EGL_OPENGL_ES_API); + + ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL ); + if (!ctx) { + printf("Error: eglCreateContext failed\n"); + exit(1); + } + + *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); + + if (!*surfRet) { + printf("Error: eglCreateWindowSurface failed\n"); + exit(1); + } + + XFree(visInfo); + + *winRet = win; + *ctxRet = ctx; +} + + +static void +event_loop(Display *dpy, Window win, + EGLDisplay egl_dpy, EGLSurface egl_surf) +{ + int anim = 0; + + while (1) { + int redraw = 0; + + if (!anim || XPending(dpy)) { + XEvent event; + XNextEvent(dpy, &event); + + switch (event.type) { + case Expose: + redraw = 1; + break; + case ConfigureNotify: + reshape(event.xconfigure.width, event.xconfigure.height); + break; + case KeyPress: + { + char buffer[10]; + int r, code; + code = XLookupKeysym(&event.xkey, 0); + if (code == XK_Left) { + view_posx -= 1.0; + } + else if (code == XK_Right) { + view_posx += 1.0; + } + else if (code == XK_Up) { + view_posy += 1.0; + } + else if (code == XK_Down) { + view_posy -= 1.0; + } + else { + r = XLookupString(&event.xkey, buffer, sizeof(buffer), + NULL, NULL); + if (buffer[0] == ' ') { + anim = !anim; + } + else if (buffer[0] == 'w') { + width -= 1.0f; + } + else if (buffer[0] == 'W') { + width += 1.0f; + } + else if (buffer[0] == 'h') { + height -= 1.0f; + } + else if (buffer[0] == 'H') { + height += 1.0f; + } + else if (buffer[0] == 27) { + /* escape */ + return; + } + } + } + redraw = 1; + break; + default: + ; /*no-op*/ + } + } + + if (anim) { + view_posx += 1.0; + view_posy += 2.0; + redraw = 1; + } + + if (redraw) { + draw(); + eglSwapBuffers(egl_dpy, egl_surf); + } + } +} + + +static void +usage(void) +{ + printf("Usage:\n"); + printf(" -display <displayname> set the display to run on\n"); + printf(" -info display OpenGL renderer info\n"); +} + + +int +main(int argc, char *argv[]) +{ + const int winWidth = 400, winHeight = 300; + Display *x_dpy; + Window win; + EGLSurface egl_surf; + EGLContext egl_ctx; + EGLDisplay egl_dpy; + char *dpyName = NULL; + GLboolean printInfo = GL_FALSE; + EGLint egl_major, egl_minor; + int i; + const char *s; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) { + dpyName = argv[i+1]; + i++; + } + else if (strcmp(argv[i], "-info") == 0) { + printInfo = GL_TRUE; + } + else { + usage(); + return -1; + } + } + + x_dpy = XOpenDisplay(dpyName); + if (!x_dpy) { + printf("Error: couldn't open display %s\n", + dpyName ? dpyName : getenv("DISPLAY")); + return -1; + } + + egl_dpy = eglGetDisplay(x_dpy); + if (!egl_dpy) { + printf("Error: eglGetDisplay() failed\n"); + return -1; + } + + if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { + printf("Error: eglInitialize() failed\n"); + return -1; + } + + s = eglQueryString(egl_dpy, EGL_VERSION); + printf("EGL_VERSION = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_VENDOR); + printf("EGL_VENDOR = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_EXTENSIONS); + printf("EGL_EXTENSIONS = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); + printf("EGL_CLIENT_APIS = %s\n", s); + + make_x_window(x_dpy, egl_dpy, + "drawtex", 0, 0, winWidth, winHeight, + &win, &egl_ctx, &egl_surf); + + XMapWindow(x_dpy, win); + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { + printf("Error: eglMakeCurrent() failed\n"); + return -1; + } + + if (printInfo) { + printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); + printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); + printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); + printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); + } + + init(); + + /* Set initial projection/viewing transformation. + * We can't be sure we'll get a ConfigureNotify event when the window + * first appears. + */ + reshape(winWidth, winHeight); + + event_loop(x_dpy, win, egl_dpy, egl_surf); + + eglDestroyContext(egl_dpy, egl_ctx); + eglDestroySurface(egl_dpy, egl_surf); + eglTerminate(egl_dpy); + + + XDestroyWindow(x_dpy, win); + XCloseDisplay(x_dpy); + + return 0; +} diff --git a/progs/es1/xegl/es1_info.c b/progs/es1/xegl/es1_info.c new file mode 100644 index 00000000000..93816b52150 --- /dev/null +++ b/progs/es1/xegl/es1_info.c @@ -0,0 +1,289 @@ +/* + * Copyright (C) 2008 Tunsgten Graphics,Inc. All Rights Reserved. + */ + +/* + * List OpenGL ES extensions. + * Print ES 1 or ES 2 extensions depending on which library we're + * linked with: libGLESv1_CM.so vs libGLESv2.so + */ + +#define GL_GLEXT_PROTOTYPES + +#include <assert.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <EGL/egl.h> + + +/* + * Print a list of extensions, with word-wrapping. + */ +static void +print_extension_list(const char *ext) +{ + const char *indentString = " "; + const int indent = 4; + const int max = 79; + int width, i, j; + + if (!ext || !ext[0]) + return; + + width = indent; + printf(indentString); + i = j = 0; + while (1) { + if (ext[j] == ' ' || ext[j] == 0) { + /* found end of an extension name */ + const int len = j - i; + if (width + len > max) { + /* start a new line */ + printf("\n"); + width = indent; + printf(indentString); + } + /* print the extension name between ext[i] and ext[j] */ + while (i < j) { + printf("%c", ext[i]); + i++; + } + /* either we're all done, or we'll continue with next extension */ + width += len + 1; + if (ext[j] == 0) { + break; + } + else { + i++; + j++; + if (ext[j] == 0) + break; + printf(", "); + width += 2; + } + } + j++; + } + printf("\n"); +} + + +static void +info(EGLDisplay egl_dpy) +{ + const char *s; + + s = eglQueryString(egl_dpy, EGL_VERSION); + printf("EGL_VERSION = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_VENDOR); + printf("EGL_VENDOR = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_EXTENSIONS); + printf("EGL_EXTENSIONS = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); + printf("EGL_CLIENT_APIS = %s\n", s); + + printf("GL_VERSION: %s\n", (char *) glGetString(GL_VERSION)); + printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER)); + printf("GL_EXTENSIONS:\n"); + print_extension_list((char *) glGetString(GL_EXTENSIONS)); +} + + +/* + * Create an RGB, double-buffered X window. + * Return the window and context handles. + */ +static void +make_x_window(Display *x_dpy, EGLDisplay egl_dpy, + const char *name, + int x, int y, int width, int height, int es_ver, + Window *winRet, + EGLContext *ctxRet, + EGLSurface *surfRet) +{ + EGLint attribs[] = { + EGL_RENDERABLE_TYPE, 0x0, + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_NONE + }; + EGLint ctx_attribs[] = { + EGL_CONTEXT_CLIENT_VERSION, 0, + EGL_NONE + }; + + int scrnum; + XSetWindowAttributes attr; + unsigned long mask; + Window root; + Window win; + XVisualInfo *visInfo, visTemplate; + int num_visuals; + EGLContext ctx; + EGLConfig config; + EGLint num_configs; + EGLint vid; + + scrnum = DefaultScreen( x_dpy ); + root = RootWindow( x_dpy, scrnum ); + + if (es_ver == 1) + attribs[1] = EGL_OPENGL_ES_BIT; + else + attribs[1] = EGL_OPENGL_ES2_BIT; + ctx_attribs[1] = es_ver; + + if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL visual config\n"); + exit(1); + } + + assert(config); + assert(num_configs > 0); + + if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { + printf("Error: eglGetConfigAttrib() failed\n"); + exit(1); + } + + /* The X window visual must match the EGL config */ + visTemplate.visualid = vid; + visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); + if (!visInfo) { + printf("Error: couldn't get X visual\n"); + exit(1); + } + + /* window attributes */ + attr.background_pixel = 0; + attr.border_pixel = 0; + attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + win = XCreateWindow( x_dpy, root, 0, 0, width, height, + 0, visInfo->depth, InputOutput, + visInfo->visual, mask, &attr ); + + /* set hints and properties */ + { + XSizeHints sizehints; + sizehints.x = x; + sizehints.y = y; + sizehints.width = width; + sizehints.height = height; + sizehints.flags = USSize | USPosition; + XSetNormalHints(x_dpy, win, &sizehints); + XSetStandardProperties(x_dpy, win, name, name, + None, (char **)NULL, 0, &sizehints); + } + + eglBindAPI(EGL_OPENGL_ES_API); + + ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, ctx_attribs ); + if (!ctx) { + printf("Error: eglCreateContext failed\n"); + exit(1); + } + + *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); + + if (!*surfRet) { + printf("Error: eglCreateWindowSurface failed\n"); + exit(1); + } + + XFree(visInfo); + + *winRet = win; + *ctxRet = ctx; +} + + +static void +usage(void) +{ + printf("Usage:\n"); + printf(" -display <displayname> set the display to run on\n"); +} + + +int +main(int argc, char *argv[]) +{ + const int winWidth = 400, winHeight = 300; + Display *x_dpy; + Window win; + EGLSurface egl_surf; + EGLContext egl_ctx; + EGLDisplay egl_dpy; + char *dpyName = NULL; + EGLint egl_major, egl_minor, es_ver; + int i; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) { + dpyName = argv[i+1]; + i++; + } + else { + usage(); + return -1; + } + } + + x_dpy = XOpenDisplay(dpyName); + if (!x_dpy) { + printf("Error: couldn't open display %s\n", + dpyName ? dpyName : getenv("DISPLAY")); + return -1; + } + + egl_dpy = eglGetDisplay(x_dpy); + if (!egl_dpy) { + printf("Error: eglGetDisplay() failed\n"); + return -1; + } + + if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { + printf("Error: eglInitialize() failed\n"); + return -1; + } + + es_ver = 1; + /* decide the version from the executable's name */ + if (argc > 0 && argv[0] && strstr(argv[0], "es2")) + es_ver = 2; + make_x_window(x_dpy, egl_dpy, + "ES info", 0, 0, winWidth, winHeight, es_ver, + &win, &egl_ctx, &egl_surf); + + /*XMapWindow(x_dpy, win);*/ + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { + printf("Error: eglMakeCurrent() failed\n"); + return -1; + } + + info(egl_dpy); + + eglDestroyContext(egl_dpy, egl_ctx); + eglDestroySurface(egl_dpy, egl_surf); + eglTerminate(egl_dpy); + + + XDestroyWindow(x_dpy, win); + XCloseDisplay(x_dpy); + + return 0; +} diff --git a/progs/es1/xegl/msaa.c b/progs/es1/xegl/msaa.c new file mode 100644 index 00000000000..b4c6c632177 --- /dev/null +++ b/progs/es1/xegl/msaa.c @@ -0,0 +1,442 @@ +/* + * Copyright (C) 2008 Brian Paul 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 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 + * BRIAN PAUL 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. + */ + +/* + * Test MSAA with X/EGL and OpenGL ES 1.x + * Brian Paul + * 15 September 2008 + */ + +#define USE_FULL_GL 0 + + +#include <assert.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> +#if USE_FULL_GL +#include <GL/gl.h> /* use full OpenGL */ +#else +#include <GLES/gl.h> /* use OpenGL ES 1.x */ +#include <GLES/glext.h> +#endif +#include <EGL/egl.h> + + + +static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0; +static GLboolean AA = 0*GL_TRUE; + + +static void +draw(void) +{ + float a; + + static const GLfloat verts[4][2] = { + { -1, -.1 }, + { 1, -.1 }, + { -1, .1 }, + { 1, .1 } + }; + static const GLfloat colors[4][4] = { + { 1, 0, 0, 1 }, + { 0, 1, 0, 1 }, + { 0, 0, 1, 1 }, + { 1, 0, 1, 1 } + }; + + if (AA) { + printf("MSAA enabled\n"); + glEnable(GL_MULTISAMPLE); + } + else { + printf("MSAA disabled\n"); + glDisable(GL_MULTISAMPLE); + } + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glRotatef(view_rotx, 1, 0, 0); + glRotatef(view_roty, 0, 1, 0); + glRotatef(view_rotz, 0, 0, 1); + + { + glVertexPointer(2, GL_FLOAT, 0, verts); + glColorPointer(4, GL_FLOAT, 0, colors); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + for (a = 0; a < 360; a += 20.0) { + glPushMatrix(); + + glRotatef(a, 0, 0, 1); + glTranslatef(1.5, 0, 0); + + /* draw triangle */ + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + glPopMatrix(); + } + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + } + + glPopMatrix(); +} + + +/* new window size or exposure */ +static void +reshape(int width, int height) +{ + GLfloat ary = 3.0; + GLfloat arx = ary * (GLfloat) width / (GLfloat) height; + + glViewport(0, 0, (GLint) width, (GLint) height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); +#ifdef GL_VERSION_ES_CM_1_0 + glOrthof(-arx, arx, -ary, ary, -1.0, 1.0); +#else + glOrtho(-arx, arx, -ary, ary, -1.0, 1.0); +#endif + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + + + +static void +init(void) +{ + printf("Press 'a' to toggle multisample antialiasing\n"); + printf("Press 'Esc' to exit\n"); +} + + +/* + * Create an RGB, double-buffered X window. + * Return the window and context handles. + */ +static void +make_x_window(Display *x_dpy, EGLDisplay egl_dpy, + const char *name, + int x, int y, int width, int height, + Window *winRet, + EGLContext *ctxRet, + EGLSurface *surfRet) +{ + static const EGLint attribs[] = { + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_SAMPLES, 1, + EGL_SAMPLE_BUFFERS, 1, + EGL_NONE + }; + + int scrnum; + XSetWindowAttributes attr; + unsigned long mask; + Window root; + Window win; + XVisualInfo *visInfo, visTemplate; + int num_visuals; + EGLContext ctx; + EGLConfig config; + EGLint num_configs; + EGLint vid; + + scrnum = DefaultScreen( x_dpy ); + root = RootWindow( x_dpy, scrnum ); + + if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL visual config\n"); + exit(1); + } + + if (num_configs < 1) { + printf("Error: Unable to find multisample pixel format.\n"); + printf("Try running glxinfo to see if your server supports MSAA.\n"); + exit(1); + } + + if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { + printf("Error: eglGetConfigAttrib() failed\n"); + exit(1); + } + + /* The X window visual must match the EGL config */ + visTemplate.visualid = vid; + visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); + if (!visInfo) { + printf("Error: couldn't get X visual\n"); + exit(1); + } + + /* window attributes */ + attr.background_pixel = 0; + attr.border_pixel = 0; + attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + win = XCreateWindow( x_dpy, root, 0, 0, width, height, + 0, visInfo->depth, InputOutput, + visInfo->visual, mask, &attr ); + + /* set hints and properties */ + { + XSizeHints sizehints; + sizehints.x = x; + sizehints.y = y; + sizehints.width = width; + sizehints.height = height; + sizehints.flags = USSize | USPosition; + XSetNormalHints(x_dpy, win, &sizehints); + XSetStandardProperties(x_dpy, win, name, name, + None, (char **)NULL, 0, &sizehints); + } + +#if USE_FULL_GL + eglBindAPI(EGL_OPENGL_API); +#else + eglBindAPI(EGL_OPENGL_ES_API); +#endif + + ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL ); + if (!ctx) { + printf("Error: eglCreateContext failed\n"); + exit(1); + } + + *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); + + if (!*surfRet) { + printf("Error: eglCreateWindowSurface failed\n"); + exit(1); + } + + XFree(visInfo); + + *winRet = win; + *ctxRet = ctx; +} + + +static void +event_loop(Display *dpy, Window win, + EGLDisplay egl_dpy, EGLSurface egl_surf) +{ + while (1) { + int redraw = 0; + XEvent event; + + XNextEvent(dpy, &event); + + switch (event.type) { + case Expose: + redraw = 1; + break; + case ConfigureNotify: + reshape(event.xconfigure.width, event.xconfigure.height); + break; + case KeyPress: + { + char buffer[10]; + int r, code; + code = XLookupKeysym(&event.xkey, 0); + if (code == XK_Left) { + view_roty += 5.0; + } + else if (code == XK_Right) { + view_roty -= 5.0; + } + else if (code == XK_Up) { + view_rotx += 5.0; + } + else if (code == XK_Down) { + view_rotx -= 5.0; + } + else { + r = XLookupString(&event.xkey, buffer, sizeof(buffer), + NULL, NULL); + if (buffer[0] == 'a') { + AA = !AA; + redraw = 1; + } + else if (buffer[0] == 27) { + /* escape */ + return; + } + } + } + redraw = 1; + break; + default: + ; /*no-op*/ + } + + if (redraw) { + draw(); + eglSwapBuffers(egl_dpy, egl_surf); + } + } +} + + +static void +usage(void) +{ + printf("Usage:\n"); + printf(" -display <displayname> set the display to run on\n"); + printf(" -info display OpenGL renderer info\n"); +} + + +int +main(int argc, char *argv[]) +{ + const int winWidth = 600, winHeight = 600; + Display *x_dpy; + Window win; + EGLSurface egl_surf; + EGLContext egl_ctx; + EGLDisplay egl_dpy; + char *dpyName = NULL; + GLboolean printInfo = GL_FALSE; + EGLint egl_major, egl_minor; + int i; + const char *s; + + static struct { + char *name; + GLenum value; + enum {GetString, GetInteger} type; + } info_items[] = { + {"GL_RENDERER", GL_RENDERER, GetString}, + {"GL_VERSION", GL_VERSION, GetString}, + {"GL_VENDOR", GL_VENDOR, GetString}, + {"GL_EXTENSIONS", GL_EXTENSIONS, GetString}, + {"GL_MAX_PALETTE_MATRICES_OES", GL_MAX_PALETTE_MATRICES_OES, GetInteger}, + {"GL_MAX_VERTEX_UNITS_OES", GL_MAX_VERTEX_UNITS_OES, GetInteger}, + }; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) { + dpyName = argv[i+1]; + i++; + } + else if (strcmp(argv[i], "-info") == 0) { + printInfo = GL_TRUE; + } + else { + usage(); + return -1; + } + } + + x_dpy = XOpenDisplay(dpyName); + if (!x_dpy) { + printf("Error: couldn't open display %s\n", + dpyName ? dpyName : getenv("DISPLAY")); + return -1; + } + + egl_dpy = eglGetDisplay(x_dpy); + if (!egl_dpy) { + printf("Error: eglGetDisplay() failed\n"); + return -1; + } + + if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { + printf("Error: eglInitialize() failed\n"); + return -1; + } + + s = eglQueryString(egl_dpy, EGL_VERSION); + printf("EGL_VERSION = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_VENDOR); + printf("EGL_VENDOR = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_EXTENSIONS); + printf("EGL_EXTENSIONS = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); + printf("EGL_CLIENT_APIS = %s\n", s); + + make_x_window(x_dpy, egl_dpy, + "msaa", 0, 0, winWidth, winHeight, + &win, &egl_ctx, &egl_surf); + + XMapWindow(x_dpy, win); + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { + printf("Error: eglMakeCurrent() failed\n"); + return -1; + } + + if (printInfo) { + for (i = 0; i < sizeof(info_items)/sizeof(info_items[0]); i++) { + switch (info_items[i].type) { + case GetString: + printf("%s = %s\n", info_items[i].name, (char *)glGetString(info_items[i].value)); + break; + case GetInteger: { + GLint rv = -1; + glGetIntegerv(info_items[i].value, &rv); + printf("%s = %d\n", info_items[i].name, rv); + break; + } + } + } + }; + init(); + + /* Set initial projection/viewing transformation. + * We can't be sure we'll get a ConfigureNotify event when the window + * first appears. + */ + reshape(winWidth, winHeight); + + event_loop(x_dpy, win, egl_dpy, egl_surf); + + eglDestroyContext(egl_dpy, egl_ctx); + eglDestroySurface(egl_dpy, egl_surf); + eglTerminate(egl_dpy); + + + XDestroyWindow(x_dpy, win); + XCloseDisplay(x_dpy); + + return 0; +} diff --git a/progs/es1/xegl/pbuffer.c b/progs/es1/xegl/pbuffer.c new file mode 100644 index 00000000000..011c2af58fe --- /dev/null +++ b/progs/es1/xegl/pbuffer.c @@ -0,0 +1,607 @@ +/* + * Copyright (C) 2008 Tunsgten Graphics,Inc. All Rights Reserved. + */ + +/* + * Test EGL Pbuffers + * Brian Paul + * August 2008 + */ + + +#include <assert.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <EGL/egl.h> + + + +static int WinWidth = 300, WinHeight = 300; + +static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0; + + +static void +Normal(GLfloat *n, GLfloat nx, GLfloat ny, GLfloat nz) +{ + n[0] = nx; + n[1] = ny; + n[2] = nz; +} + +static void +Vertex(GLfloat *v, GLfloat vx, GLfloat vy, GLfloat vz) +{ + v[0] = vx; + v[1] = vy; + v[2] = vz; +} + +static void +Texcoord(GLfloat *v, GLfloat s, GLfloat t) +{ + v[0] = s; + v[1] = t; +} + + +/* Borrowed from glut, adapted */ +static void +draw_torus(GLfloat r, GLfloat R, GLint nsides, GLint rings) +{ + int i, j; + GLfloat theta, phi, theta1; + GLfloat cosTheta, sinTheta; + GLfloat cosTheta1, sinTheta1; + GLfloat ringDelta, sideDelta; + GLfloat varray[100][3], narray[100][3], tarray[100][2]; + int vcount; + + glVertexPointer(3, GL_FLOAT, 0, varray); + glNormalPointer(GL_FLOAT, 0, narray); + glTexCoordPointer(2, GL_FLOAT, 0, tarray); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + ringDelta = 2.0 * M_PI / rings; + sideDelta = 2.0 * M_PI / nsides; + + theta = 0.0; + cosTheta = 1.0; + sinTheta = 0.0; + for (i = rings - 1; i >= 0; i--) { + theta1 = theta + ringDelta; + cosTheta1 = cos(theta1); + sinTheta1 = sin(theta1); + + vcount = 0; /* glBegin(GL_QUAD_STRIP); */ + + phi = 0.0; + for (j = nsides; j >= 0; j--) { + GLfloat s0, s1, t; + GLfloat cosPhi, sinPhi, dist; + + phi += sideDelta; + cosPhi = cos(phi); + sinPhi = sin(phi); + dist = R + r * cosPhi; + + s0 = 20.0 * theta / (2.0 * M_PI); + s1 = 20.0 * theta1 / (2.0 * M_PI); + t = 8.0 * phi / (2.0 * M_PI); + + Normal(narray[vcount], cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi); + Texcoord(tarray[vcount], s1, t); + Vertex(varray[vcount], cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi); + vcount++; + + Normal(narray[vcount], cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi); + Texcoord(tarray[vcount], s0, t); + Vertex(varray[vcount], cosTheta * dist, -sinTheta * dist, r * sinPhi); + vcount++; + } + + /*glEnd();*/ + assert(vcount <= 100); + glDrawArrays(GL_TRIANGLE_STRIP, 0, vcount); + + theta = theta1; + cosTheta = cosTheta1; + sinTheta = sinTheta1; + } + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); +} + + +static void +draw(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glRotatef(view_rotx, 1, 0, 0); + glRotatef(view_roty, 0, 1, 0); + glRotatef(view_rotz, 0, 0, 1); + glScalef(0.5, 0.5, 0.5); + + draw_torus(1.0, 3.0, 30, 60); + + glPopMatrix(); + + glFinish(); +} + + +/** + * Draw to both the window and pbuffer and compare results. + */ +static void +draw_both(EGLDisplay egl_dpy, EGLSurface egl_surf, EGLSurface egl_pbuf, + EGLContext egl_ctx) +{ + unsigned *wbuf, *pbuf; + int x = 100, y = 110; + int i, dif; + + wbuf = (unsigned *) malloc(WinWidth * WinHeight * 4); + pbuf = (unsigned *) malloc(WinWidth * WinHeight * 4); + + glPixelStorei(GL_PACK_ALIGNMENT, 1); + + /* first draw to window */ + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { + printf("Error: eglMakeCurrent(window) failed\n"); + return; + } + draw(); + glReadPixels(0, 0, WinWidth, WinHeight, GL_RGBA, GL_UNSIGNED_BYTE, wbuf); + printf("Window[%d,%d] = 0x%08x\n", x, y, wbuf[y*WinWidth+x]); + + /* then draw to pbuffer */ + if (!eglMakeCurrent(egl_dpy, egl_pbuf, egl_pbuf, egl_ctx)) { + printf("Error: eglMakeCurrent(pbuffer) failed\n"); + return; + } + draw(); + glReadPixels(0, 0, WinWidth, WinHeight, GL_RGBA, GL_UNSIGNED_BYTE, pbuf); + printf("Pbuffer[%d,%d] = 0x%08x\n", x, y, pbuf[y*WinWidth+x]); + + eglSwapBuffers(egl_dpy, egl_surf); + + /* compare renderings */ + for (dif = i = 0; i < WinWidth * WinHeight; i++) { + if (wbuf[i] != pbuf[i]) { + dif = 1; + break; + } + } + + if (dif) + printf("Difference at %d: 0x%08x vs. 0x%08x\n", i, wbuf[i], pbuf[i]); + else + printf("Window rendering matches Pbuffer rendering!\n"); + + free(wbuf); + free(pbuf); +} + + +/* new window size or exposure */ +static void +reshape(int width, int height) +{ + GLfloat ar = (GLfloat) width / (GLfloat) height; + + WinWidth = width; + WinHeight = height; + + glViewport(0, 0, (GLint) width, (GLint) height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + +#ifdef GL_VERSION_ES_CM_1_0 + glFrustumf(-ar, ar, -1, 1, 5.0, 60.0); +#else + glFrustum(-ar, ar, -1, 1, 5.0, 60.0); +#endif + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -15.0); +} + + +static void +make_texture(void) +{ +#define SZ 64 + GLenum Filter = GL_LINEAR; + GLubyte image[SZ][SZ][4]; + GLuint i, j; + + for (i = 0; i < SZ; i++) { + for (j = 0; j < SZ; j++) { + GLfloat d = (i - SZ/2) * (i - SZ/2) + (j - SZ/2) * (j - SZ/2); + d = sqrt(d); + if (d < SZ/3) { + image[i][j][0] = 255; + image[i][j][1] = 255; + image[i][j][2] = 255; + image[i][j][3] = 255; + } + else { + image[i][j][0] = 127; + image[i][j][1] = 127; + image[i][j][2] = 127; + image[i][j][3] = 255; + } + } + } + + glActiveTexture(GL_TEXTURE0); /* unit 0 */ + glBindTexture(GL_TEXTURE_2D, 42); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ, SZ, 0, + GL_RGBA, GL_UNSIGNED_BYTE, image); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); +#undef SZ +} + + + +static void +init(void) +{ + static const GLfloat red[4] = {1, 0, 0, 0}; + static const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0}; + static const GLfloat diffuse[4] = {0.7, 0.7, 0.7, 1.0}; + static const GLfloat specular[4] = {0.001, 0.001, 0.001, 1.0}; + static const GLfloat pos[4] = {20, 20, 50, 1}; + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red); + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white); + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 9.0); + + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, pos); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); + glLightfv(GL_LIGHT0, GL_SPECULAR, specular); + + glClearColor(0.4, 0.4, 0.4, 0.0); + glEnable(GL_DEPTH_TEST); + + make_texture(); + glEnable(GL_TEXTURE_2D); +} + + +/* + * Create an RGB, double-buffered X window. + * Return the window and context handles. + */ +static void +make_x_window(Display *x_dpy, EGLDisplay egl_dpy, + const char *name, + int x, int y, int width, int height, + Window *winRet, + EGLContext *ctxRet, + EGLSurface *surfRet) +{ + static const EGLint attribs[] = { + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_NONE + }; + + int scrnum; + XSetWindowAttributes attr; + unsigned long mask; + Window root; + Window win; + XVisualInfo *visInfo, visTemplate; + int num_visuals; + EGLContext ctx; + EGLConfig config; + EGLint num_configs; + EGLint vid; + + scrnum = DefaultScreen( x_dpy ); + root = RootWindow( x_dpy, scrnum ); + + if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL visual config\n"); + exit(1); + } + + assert(config); + assert(num_configs > 0); + + if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { + printf("Error: eglGetConfigAttrib() failed\n"); + exit(1); + } + + /* The X window visual must match the EGL config */ + visTemplate.visualid = vid; + visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); + if (!visInfo) { + printf("Error: couldn't get X visual\n"); + exit(1); + } + + /* window attributes */ + attr.background_pixel = 0; + attr.border_pixel = 0; + attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + win = XCreateWindow( x_dpy, root, 0, 0, width, height, + 0, visInfo->depth, InputOutput, + visInfo->visual, mask, &attr ); + + /* set hints and properties */ + { + XSizeHints sizehints; + sizehints.x = x; + sizehints.y = y; + sizehints.width = width; + sizehints.height = height; + sizehints.flags = USSize | USPosition; + XSetNormalHints(x_dpy, win, &sizehints); + XSetStandardProperties(x_dpy, win, name, name, + None, (char **)NULL, 0, &sizehints); + } + + eglBindAPI(EGL_OPENGL_ES_API); + + ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL ); + if (!ctx) { + printf("Error: eglCreateContext failed\n"); + exit(1); + } + + *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); + + if (!*surfRet) { + printf("Error: eglCreateWindowSurface failed\n"); + exit(1); + } + + XFree(visInfo); + + *winRet = win; + *ctxRet = ctx; +} + + +static EGLSurface +make_pbuffer(Display *x_dpy, EGLDisplay egl_dpy, int width, int height) +{ + static const EGLint config_attribs[] = { + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_NONE + }; + EGLConfig config; + EGLSurface pbuf; + EGLint num_configs; + EGLint pbuf_attribs[5]; + + pbuf_attribs[0] = EGL_WIDTH; + pbuf_attribs[1] = width; + pbuf_attribs[2] = EGL_HEIGHT; + pbuf_attribs[3] = height; + pbuf_attribs[4] = EGL_NONE; + + if (!eglChooseConfig( egl_dpy, config_attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL config for pbuffer\n"); + exit(1); + } + + pbuf = eglCreatePbufferSurface(egl_dpy, config, pbuf_attribs); + + return pbuf; +} + + +static void +event_loop(Display *dpy, Window win, + EGLDisplay egl_dpy, EGLSurface egl_surf, EGLSurface egl_pbuf, + EGLContext egl_ctx) +{ + int anim = 0; + + while (1) { + int redraw = 0; + + if (!anim || XPending(dpy)) { + XEvent event; + XNextEvent(dpy, &event); + + switch (event.type) { + case Expose: + redraw = 1; + break; + case ConfigureNotify: + if (event.xconfigure.window == win) + reshape(event.xconfigure.width, event.xconfigure.height); + break; + case KeyPress: + { + char buffer[10]; + int r, code; + code = XLookupKeysym(&event.xkey, 0); + if (code == XK_Left) { + view_roty += 5.0; + } + else if (code == XK_Right) { + view_roty -= 5.0; + } + else if (code == XK_Up) { + view_rotx += 5.0; + } + else if (code == XK_Down) { + view_rotx -= 5.0; + } + else { + r = XLookupString(&event.xkey, buffer, sizeof(buffer), + NULL, NULL); + if (buffer[0] == ' ') { + anim = !anim; + } + else if (buffer[0] == 27) { + /* escape */ + return; + } + } + } + redraw = 1; + break; + default: + ; /*no-op*/ + } + } + + if (anim) { + view_rotx += 1.0; + view_roty += 2.0; + redraw = 1; + } + + if (redraw) { + draw_both(egl_dpy, egl_surf, egl_pbuf, egl_ctx); + } + } +} + + +static void +usage(void) +{ + printf("Usage:\n"); + printf(" -display <displayname> set the display to run on\n"); + printf(" -info display OpenGL renderer info\n"); +} + + +int +main(int argc, char *argv[]) +{ + Display *x_dpy; + Window win; + EGLSurface egl_surf, egl_pbuf; + EGLContext egl_ctx; + EGLDisplay egl_dpy; + char *dpyName = NULL; + GLboolean printInfo = GL_FALSE; + EGLint egl_major, egl_minor; + int i; + const char *s; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) { + dpyName = argv[i+1]; + i++; + } + else if (strcmp(argv[i], "-info") == 0) { + printInfo = GL_TRUE; + } + else { + usage(); + return -1; + } + } + + x_dpy = XOpenDisplay(dpyName); + if (!x_dpy) { + printf("Error: couldn't open display %s\n", + dpyName ? dpyName : getenv("DISPLAY")); + return -1; + } + + egl_dpy = eglGetDisplay(x_dpy); + if (!egl_dpy) { + printf("Error: eglGetDisplay() failed\n"); + return -1; + } + + if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { + printf("Error: eglInitialize() failed\n"); + return -1; + } + + s = eglQueryString(egl_dpy, EGL_VERSION); + printf("EGL_VERSION = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_VENDOR); + printf("EGL_VENDOR = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_EXTENSIONS); + printf("EGL_EXTENSIONS = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); + printf("EGL_CLIENT_APIS = %s\n", s); + + make_x_window(x_dpy, egl_dpy, + "pbuffer", 0, 0, WinWidth, WinHeight, + &win, &egl_ctx, &egl_surf); + + egl_pbuf = make_pbuffer(x_dpy, egl_dpy, WinWidth, WinHeight); + if (!egl_pbuf) { + printf("Error: eglCreatePBufferSurface() failed\n"); + return -1; + } + + XMapWindow(x_dpy, win); + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { + printf("Error: eglMakeCurrent() failed\n"); + return -1; + } + + if (printInfo) { + printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); + printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); + printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); + printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); + } + + init(); + + /* Set initial projection/viewing transformation. + * We can't be sure we'll get a ConfigureNotify event when the window + * first appears. + */ + reshape(WinWidth, WinHeight); + + event_loop(x_dpy, win, egl_dpy, egl_surf, egl_pbuf, egl_ctx); + + eglDestroyContext(egl_dpy, egl_ctx); + eglDestroySurface(egl_dpy, egl_surf); + eglTerminate(egl_dpy); + + + XDestroyWindow(x_dpy, win); + XCloseDisplay(x_dpy); + + return 0; +} diff --git a/progs/es1/xegl/render_tex.c b/progs/es1/xegl/render_tex.c new file mode 100644 index 00000000000..0200fa4cb06 --- /dev/null +++ b/progs/es1/xegl/render_tex.c @@ -0,0 +1,659 @@ +/* + * Copyright (C) 2008 Tunsgten Graphics,Inc. All Rights Reserved. + */ + +/* + * Test EGL render to texture. + * Brian Paul + * August 2008 + */ + + +#include <assert.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <EGL/egl.h> + + +static int TexWidth = 256, TexHeight = 256; + +static int WinWidth = 300, WinHeight = 300; + +static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0; + +static GLuint DotTexture, RenderTexture; + + +static void +Normal(GLfloat *n, GLfloat nx, GLfloat ny, GLfloat nz) +{ + n[0] = nx; + n[1] = ny; + n[2] = nz; +} + +static void +Vertex(GLfloat *v, GLfloat vx, GLfloat vy, GLfloat vz) +{ + v[0] = vx; + v[1] = vy; + v[2] = vz; +} + +static void +Texcoord(GLfloat *v, GLfloat s, GLfloat t) +{ + v[0] = s; + v[1] = t; +} + + +/* Borrowed from glut, adapted */ +static void +draw_torus(GLfloat r, GLfloat R, GLint nsides, GLint rings) +{ + int i, j; + GLfloat theta, phi, theta1; + GLfloat cosTheta, sinTheta; + GLfloat cosTheta1, sinTheta1; + GLfloat ringDelta, sideDelta; + GLfloat varray[100][3], narray[100][3], tarray[100][2]; + int vcount; + + glVertexPointer(3, GL_FLOAT, 0, varray); + glNormalPointer(GL_FLOAT, 0, narray); + glTexCoordPointer(2, GL_FLOAT, 0, tarray); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + ringDelta = 2.0 * M_PI / rings; + sideDelta = 2.0 * M_PI / nsides; + + theta = 0.0; + cosTheta = 1.0; + sinTheta = 0.0; + for (i = rings - 1; i >= 0; i--) { + theta1 = theta + ringDelta; + cosTheta1 = cos(theta1); + sinTheta1 = sin(theta1); + + vcount = 0; /* glBegin(GL_QUAD_STRIP); */ + + phi = 0.0; + for (j = nsides; j >= 0; j--) { + GLfloat s0, s1, t; + GLfloat cosPhi, sinPhi, dist; + + phi += sideDelta; + cosPhi = cos(phi); + sinPhi = sin(phi); + dist = R + r * cosPhi; + + s0 = 20.0 * theta / (2.0 * M_PI); + s1 = 20.0 * theta1 / (2.0 * M_PI); + t = 8.0 * phi / (2.0 * M_PI); + + Normal(narray[vcount], cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi); + Texcoord(tarray[vcount], s1, t); + Vertex(varray[vcount], cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi); + vcount++; + + Normal(narray[vcount], cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi); + Texcoord(tarray[vcount], s0, t); + Vertex(varray[vcount], cosTheta * dist, -sinTheta * dist, r * sinPhi); + vcount++; + } + + /*glEnd();*/ + assert(vcount <= 100); + glDrawArrays(GL_TRIANGLE_STRIP, 0, vcount); + + theta = theta1; + cosTheta = cosTheta1; + sinTheta = sinTheta1; + } + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); +} + + +static void +draw_torus_to_texture(void) +{ + glViewport(0, 0, TexWidth, TexHeight); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustumf(-1, 1, -1, 1, 5.0, 60.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -15.0); + + glClearColor(0.4, 0.4, 0.4, 0.0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glBindTexture(GL_TEXTURE_2D, DotTexture); + + glEnable(GL_LIGHTING); + + glPushMatrix(); + glRotatef(view_roty, 0, 1, 0); + glScalef(0.5, 0.5, 0.5); + + draw_torus(1.0, 3.0, 30, 60); + + glPopMatrix(); + + glDisable(GL_LIGHTING); + +#if 0 + glBindTexture(GL_TEXTURE_2D, RenderTexture); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, TexWidth, TexHeight); +#endif + + glFinish(); +} + + +static void +draw_textured_quad(void) +{ + GLfloat ar = (GLfloat) WinWidth / (GLfloat) WinHeight; + + glViewport(0, 0, WinWidth, WinHeight); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustumf(-ar, ar, -1, 1, 5.0, 60.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -8.0); + + glClearColor(0.4, 0.4, 1.0, 0.0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glBindTexture(GL_TEXTURE_2D, RenderTexture); + + glPushMatrix(); + glRotatef(view_rotx, 1, 0, 0); + glRotatef(view_rotz, 0, 0, 1); + + { + static const GLfloat texcoord[4][2] = { + { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } + }; + static const GLfloat vertex[4][2] = { + { -1, -1 }, { 1, -1 }, { -1, 1 }, { 1, 1 } + }; + + glVertexPointer(2, GL_FLOAT, 0, vertex); + glTexCoordPointer(2, GL_FLOAT, 0, texcoord); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + } + + glPopMatrix(); +} + + + +static void +draw(EGLDisplay egl_dpy, EGLSurface egl_surf, EGLSurface egl_pbuf, + EGLContext egl_ctx) +{ + /*printf("Begin draw\n");*/ + + /* first draw torus to pbuffer /texture */ +#if 01 + if (!eglMakeCurrent(egl_dpy, egl_pbuf, egl_pbuf, egl_ctx)) { +#else + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { +#endif + printf("Error: eglMakeCurrent(pbuf) failed\n"); + return; + } + draw_torus_to_texture(); + + /* draw textured quad to window */ + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { + printf("Error: eglMakeCurrent(pbuffer) failed\n"); + return; + } + + glBindTexture(GL_TEXTURE_2D, RenderTexture); + eglBindTexImage(egl_dpy, egl_pbuf, EGL_BACK_BUFFER); + draw_textured_quad(); + eglReleaseTexImage(egl_dpy, egl_pbuf, EGL_BACK_BUFFER); + + eglSwapBuffers(egl_dpy, egl_surf); + + /*printf("End draw\n");*/ +} + + + +static void +make_dot_texture(void) +{ +#define SZ 64 + GLenum Filter = GL_LINEAR; + GLubyte image[SZ][SZ][4]; + GLuint i, j; + + for (i = 0; i < SZ; i++) { + for (j = 0; j < SZ; j++) { + GLfloat d = (i - SZ/2) * (i - SZ/2) + (j - SZ/2) * (j - SZ/2); + d = sqrt(d); + if (d < SZ/3) { + image[i][j][0] = 255; + image[i][j][1] = 255; + image[i][j][2] = 255; + image[i][j][3] = 255; + } + else { + image[i][j][0] = 127; + image[i][j][1] = 127; + image[i][j][2] = 127; + image[i][j][3] = 255; + } + } + } + + glGenTextures(1, &DotTexture); + glBindTexture(GL_TEXTURE_2D, DotTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ, SZ, 0, + GL_RGBA, GL_UNSIGNED_BYTE, image); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); +#undef SZ +} + + +static void +make_render_texture(void) +{ + GLenum Filter = GL_LINEAR; + glGenTextures(1, &RenderTexture); + glBindTexture(GL_TEXTURE_2D, RenderTexture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0, + GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); +} + + +static void +init(void) +{ + static const GLfloat red[4] = {1, 0, 0, 0}; + static const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0}; + static const GLfloat diffuse[4] = {0.7, 0.7, 0.7, 1.0}; + static const GLfloat specular[4] = {0.001, 0.001, 0.001, 1.0}; + static const GLfloat pos[4] = {20, 20, 50, 1}; + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red); + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white); + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 9.0); + + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, pos); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); + glLightfv(GL_LIGHT0, GL_SPECULAR, specular); + + glEnable(GL_DEPTH_TEST); + + make_dot_texture(); + make_render_texture(); + + printf("DotTexture=%u RenderTexture=%u\n", DotTexture, RenderTexture); + + glEnable(GL_TEXTURE_2D); +} + + +/* + * Create an RGB, double-buffered X window. + * Return the window and context handles. + */ +static void +make_x_window(Display *x_dpy, EGLDisplay egl_dpy, + const char *name, + int x, int y, int width, int height, + Window *winRet, + EGLContext *ctxRet, + EGLSurface *surfRet) +{ + static const EGLint attribs[] = { + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_NONE + }; + + int scrnum; + XSetWindowAttributes attr; + unsigned long mask; + Window root; + Window win; + XVisualInfo *visInfo, visTemplate; + int num_visuals; + EGLContext ctx; + EGLConfig config; + EGLint num_configs; + EGLint vid; + + scrnum = DefaultScreen( x_dpy ); + root = RootWindow( x_dpy, scrnum ); + + if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL visual config\n"); + exit(1); + } + + assert(config); + assert(num_configs > 0); + + if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { + printf("Error: eglGetConfigAttrib() failed\n"); + exit(1); + } + + /* The X window visual must match the EGL config */ + visTemplate.visualid = vid; + visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); + if (!visInfo) { + printf("Error: couldn't get X visual\n"); + exit(1); + } + + /* window attributes */ + attr.background_pixel = 0; + attr.border_pixel = 0; + attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + win = XCreateWindow( x_dpy, root, 0, 0, width, height, + 0, visInfo->depth, InputOutput, + visInfo->visual, mask, &attr ); + + /* set hints and properties */ + { + XSizeHints sizehints; + sizehints.x = x; + sizehints.y = y; + sizehints.width = width; + sizehints.height = height; + sizehints.flags = USSize | USPosition; + XSetNormalHints(x_dpy, win, &sizehints); + XSetStandardProperties(x_dpy, win, name, name, + None, (char **)NULL, 0, &sizehints); + } + + eglBindAPI(EGL_OPENGL_ES_API); + + ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL ); + if (!ctx) { + printf("Error: eglCreateContext failed\n"); + exit(1); + } + + *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); + + if (!*surfRet) { + printf("Error: eglCreateWindowSurface failed\n"); + exit(1); + } + + XFree(visInfo); + + *winRet = win; + *ctxRet = ctx; +} + + +static EGLSurface +make_pbuffer(Display *x_dpy, EGLDisplay egl_dpy, int width, int height) +{ + static const EGLint config_attribs[] = { + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_NONE + }; + EGLConfig config; + EGLSurface pbuf; + EGLint num_configs; + EGLint pbuf_attribs[15]; + int i = 0; + + pbuf_attribs[i++] = EGL_WIDTH; + pbuf_attribs[i++] = width; + pbuf_attribs[i++] = EGL_HEIGHT; + pbuf_attribs[i++] = height; + pbuf_attribs[i++] = EGL_TEXTURE_FORMAT; + pbuf_attribs[i++] = EGL_TEXTURE_RGBA; + pbuf_attribs[i++] = EGL_TEXTURE_TARGET; + pbuf_attribs[i++] = EGL_TEXTURE_2D; + pbuf_attribs[i++] = EGL_MIPMAP_TEXTURE; + pbuf_attribs[i++] = EGL_FALSE; + pbuf_attribs[i++] = EGL_NONE; + assert(i <= 15); + + if (!eglChooseConfig( egl_dpy, config_attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL config for pbuffer\n"); + exit(1); + } + + pbuf = eglCreatePbufferSurface(egl_dpy, config, pbuf_attribs); + + return pbuf; +} + + +static void +event_loop(Display *dpy, Window win, + EGLDisplay egl_dpy, EGLSurface egl_surf, EGLSurface egl_pbuf, + EGLContext egl_ctx) +{ + int anim = 0; + + while (1) { + int redraw = 0; + + if (!anim || XPending(dpy)) { + XEvent event; + XNextEvent(dpy, &event); + + switch (event.type) { + case Expose: + redraw = 1; + break; + case ConfigureNotify: + if (event.xconfigure.window == win) { + WinWidth = event.xconfigure.width; + WinHeight = event.xconfigure.height; + } + break; + case KeyPress: + { + char buffer[10]; + int r, code; + code = XLookupKeysym(&event.xkey, 0); + if (code == XK_Left) { + view_roty += 5.0; + } + else if (code == XK_Right) { + view_roty -= 5.0; + } + else if (code == XK_Up) { + view_rotx += 5.0; + } + else if (code == XK_Down) { + view_rotx -= 5.0; + } + else { + r = XLookupString(&event.xkey, buffer, sizeof(buffer), + NULL, NULL); + if (buffer[0] == ' ') { + anim = !anim; + } + else if (buffer[0] == 'z') { + view_rotz += 5.0; + } + else if (buffer[0] == 'Z') { + view_rotz -= 5.0; + } + else if (buffer[0] == 27) { + /* escape */ + return; + } + } + } + redraw = 1; + break; + default: + ; /*no-op*/ + } + } + + if (anim) { + view_rotx += 1.0; + view_roty += 2.0; + redraw = 1; + } + + if (redraw) { + draw(egl_dpy, egl_surf, egl_pbuf, egl_ctx); + } + } +} + + +static void +usage(void) +{ + printf("Usage:\n"); + printf(" -display <displayname> set the display to run on\n"); + printf(" -info display OpenGL renderer info\n"); +} + + +int +main(int argc, char *argv[]) +{ + Display *x_dpy; + Window win; + EGLSurface egl_surf, egl_pbuf; + EGLContext egl_ctx; + EGLDisplay egl_dpy; + char *dpyName = NULL; + GLboolean printInfo = GL_FALSE; + EGLint egl_major, egl_minor; + int i; + const char *s; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) { + dpyName = argv[i+1]; + i++; + } + else if (strcmp(argv[i], "-info") == 0) { + printInfo = GL_TRUE; + } + else { + usage(); + return -1; + } + } + + x_dpy = XOpenDisplay(dpyName); + if (!x_dpy) { + printf("Error: couldn't open display %s\n", + dpyName ? dpyName : getenv("DISPLAY")); + return -1; + } + + egl_dpy = eglGetDisplay(x_dpy); + if (!egl_dpy) { + printf("Error: eglGetDisplay() failed\n"); + return -1; + } + + if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { + printf("Error: eglInitialize() failed\n"); + return -1; + } + + s = eglQueryString(egl_dpy, EGL_VERSION); + printf("EGL_VERSION = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_VENDOR); + printf("EGL_VENDOR = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_EXTENSIONS); + printf("EGL_EXTENSIONS = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); + printf("EGL_CLIENT_APIS = %s\n", s); + + make_x_window(x_dpy, egl_dpy, + "render_tex", 0, 0, WinWidth, WinHeight, + &win, &egl_ctx, &egl_surf); + + egl_pbuf = make_pbuffer(x_dpy, egl_dpy, TexWidth, TexHeight); + if (!egl_pbuf) { + printf("Error: eglCreatePBufferSurface() failed\n"); + return -1; + } + + XMapWindow(x_dpy, win); + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { + printf("Error: eglMakeCurrent() failed\n"); + return -1; + } + + if (printInfo) { + printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); + printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); + printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); + printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); + } + + init(); + + event_loop(x_dpy, win, egl_dpy, egl_surf, egl_pbuf, egl_ctx); + + eglDestroyContext(egl_dpy, egl_ctx); + eglDestroySurface(egl_dpy, egl_surf); + eglTerminate(egl_dpy); + + + XDestroyWindow(x_dpy, win); + XCloseDisplay(x_dpy); + + return 0; +} diff --git a/progs/es1/xegl/torus.c b/progs/es1/xegl/torus.c new file mode 100644 index 00000000000..9438a4fe59b --- /dev/null +++ b/progs/es1/xegl/torus.c @@ -0,0 +1,656 @@ +/* + * Copyright (C) 2008 Tunsgten Graphics,Inc. All Rights Reserved. + */ + +/* + * Draw a lit, textured torus with X/EGL and OpenGL ES 1.x + * Brian Paul + * July 2008 + */ + + +#include <assert.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <EGL/egl.h> + + +static const struct { + GLenum internalFormat; + const char *name; + GLuint num_entries; + GLuint size; +} cpal_formats[] = { + { GL_PALETTE4_RGB8_OES, "GL_PALETTE4_RGB8_OES", 16, 3 }, + { GL_PALETTE4_RGBA8_OES, "GL_PALETTE4_RGBA8_OES", 16, 4 }, + { GL_PALETTE4_R5_G6_B5_OES, "GL_PALETTE4_R5_G6_B5_OES", 16, 2 }, + { GL_PALETTE4_RGBA4_OES, "GL_PALETTE4_RGBA4_OES", 16, 2 }, + { GL_PALETTE4_RGB5_A1_OES, "GL_PALETTE4_RGB5_A1_OES", 16, 2 }, + { GL_PALETTE8_RGB8_OES, "GL_PALETTE8_RGB8_OES", 256, 3 }, + { GL_PALETTE8_RGBA8_OES, "GL_PALETTE8_RGBA8_OES", 256, 4 }, + { GL_PALETTE8_R5_G6_B5_OES, "GL_PALETTE8_R5_G6_B5_OES", 256, 2 }, + { GL_PALETTE8_RGBA4_OES, "GL_PALETTE8_RGBA4_OES", 256, 2 }, + { GL_PALETTE8_RGB5_A1_OES, "GL_PALETTE8_RGB5_A1_OES", 256, 2 } +}; +#define NUM_CPAL_FORMATS (sizeof(cpal_formats) / sizeof(cpal_formats[0])) + +static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0; +static GLint tex_format = NUM_CPAL_FORMATS; + + +static void +Normal(GLfloat *n, GLfloat nx, GLfloat ny, GLfloat nz) +{ + n[0] = nx; + n[1] = ny; + n[2] = nz; +} + +static void +Vertex(GLfloat *v, GLfloat vx, GLfloat vy, GLfloat vz) +{ + v[0] = vx; + v[1] = vy; + v[2] = vz; +} + +static void +Texcoord(GLfloat *v, GLfloat s, GLfloat t) +{ + v[0] = s; + v[1] = t; +} + + +/* Borrowed from glut, adapted */ +static void +draw_torus(GLfloat r, GLfloat R, GLint nsides, GLint rings) +{ + int i, j; + GLfloat theta, phi, theta1; + GLfloat cosTheta, sinTheta; + GLfloat cosTheta1, sinTheta1; + GLfloat ringDelta, sideDelta; + GLfloat varray[100][3], narray[100][3], tarray[100][2]; + int vcount; + + glVertexPointer(3, GL_FLOAT, 0, varray); + glNormalPointer(GL_FLOAT, 0, narray); + glTexCoordPointer(2, GL_FLOAT, 0, tarray); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + ringDelta = 2.0 * M_PI / rings; + sideDelta = 2.0 * M_PI / nsides; + + theta = 0.0; + cosTheta = 1.0; + sinTheta = 0.0; + for (i = rings - 1; i >= 0; i--) { + theta1 = theta + ringDelta; + cosTheta1 = cos(theta1); + sinTheta1 = sin(theta1); + + vcount = 0; /* glBegin(GL_QUAD_STRIP); */ + + phi = 0.0; + for (j = nsides; j >= 0; j--) { + GLfloat s0, s1, t; + GLfloat cosPhi, sinPhi, dist; + + phi += sideDelta; + cosPhi = cos(phi); + sinPhi = sin(phi); + dist = R + r * cosPhi; + + s0 = 20.0 * theta / (2.0 * M_PI); + s1 = 20.0 * theta1 / (2.0 * M_PI); + t = 8.0 * phi / (2.0 * M_PI); + + Normal(narray[vcount], cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi); + Texcoord(tarray[vcount], s1, t); + Vertex(varray[vcount], cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi); + vcount++; + + Normal(narray[vcount], cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi); + Texcoord(tarray[vcount], s0, t); + Vertex(varray[vcount], cosTheta * dist, -sinTheta * dist, r * sinPhi); + vcount++; + } + + /*glEnd();*/ + assert(vcount <= 100); + glDrawArrays(GL_TRIANGLE_STRIP, 0, vcount); + + theta = theta1; + cosTheta = cosTheta1; + sinTheta = sinTheta1; + } + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); +} + + +static void +draw(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glRotatef(view_rotx, 1, 0, 0); + glRotatef(view_roty, 0, 1, 0); + glRotatef(view_rotz, 0, 0, 1); + glScalef(0.5, 0.5, 0.5); + + draw_torus(1.0, 3.0, 30, 60); + + glPopMatrix(); +} + + +/* new window size or exposure */ +static void +reshape(int width, int height) +{ + GLfloat ar = (GLfloat) width / (GLfloat) height; + + glViewport(0, 0, (GLint) width, (GLint) height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + +#ifdef GL_VERSION_ES_CM_1_0 + glFrustumf(-ar, ar, -1, 1, 5.0, 60.0); +#else + glFrustum(-ar, ar, -1, 1, 5.0, 60.0); +#endif + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -15.0); +} + + +static GLint +make_cpal_texture(GLint idx) +{ +#define SZ 64 + GLenum internalFormat = GL_PALETTE4_RGB8_OES + idx; + GLenum Filter = GL_LINEAR; + GLubyte palette[256 * 4 + SZ * SZ]; + GLubyte *indices; + GLsizei image_size; + GLuint i, j; + GLuint packed_indices = 0; + + assert(cpal_formats[idx].internalFormat == internalFormat); + + /* init palette */ + switch (internalFormat) { + case GL_PALETTE4_RGB8_OES: + case GL_PALETTE8_RGB8_OES: + /* first entry */ + palette[0] = 255; + palette[1] = 255; + palette[2] = 255; + /* second entry */ + palette[3] = 127; + palette[4] = 127; + palette[5] = 127; + break; + case GL_PALETTE4_RGBA8_OES: + case GL_PALETTE8_RGBA8_OES: + /* first entry */ + palette[0] = 255; + palette[1] = 255; + palette[2] = 255; + palette[3] = 255; + /* second entry */ + palette[4] = 127; + palette[5] = 127; + palette[6] = 127; + palette[7] = 255; + break; + case GL_PALETTE4_R5_G6_B5_OES: + case GL_PALETTE8_R5_G6_B5_OES: + { + GLushort *pal = (GLushort *) palette; + /* first entry */ + pal[0] = (31 << 11 | 63 << 5 | 31); + /* second entry */ + pal[1] = (15 << 11 | 31 << 5 | 15); + } + break; + case GL_PALETTE4_RGBA4_OES: + case GL_PALETTE8_RGBA4_OES: + { + GLushort *pal = (GLushort *) palette; + /* first entry */ + pal[0] = (15 << 12 | 15 << 8 | 15 << 4 | 15); + /* second entry */ + pal[1] = (7 << 12 | 7 << 8 | 7 << 4 | 15); + } + break; + case GL_PALETTE4_RGB5_A1_OES: + case GL_PALETTE8_RGB5_A1_OES: + { + GLushort *pal = (GLushort *) palette; + /* first entry */ + pal[0] = (31 << 11 | 31 << 6 | 31 << 1 | 1); + /* second entry */ + pal[1] = (15 << 11 | 15 << 6 | 15 << 1 | 1); + } + break; + } + + image_size = cpal_formats[idx].size * cpal_formats[idx].num_entries; + indices = palette + image_size; + for (i = 0; i < SZ; i++) { + for (j = 0; j < SZ; j++) { + GLfloat d; + GLint index; + d = (i - SZ/2) * (i - SZ/2) + (j - SZ/2) * (j - SZ/2); + d = sqrt(d); + index = (d < SZ / 3) ? 0 : 1; + + if (cpal_formats[idx].num_entries == 16) { + /* 4-bit indices packed in GLubyte */ + packed_indices |= index << (4 * (1 - (j % 2))); + if (j % 2) { + *(indices + (i * SZ + j - 1) / 2) = packed_indices & 0xff; + packed_indices = 0; + image_size += 1; + } + } + else { + /* 8-bit indices */ + *(indices + i * SZ + j) = index; + image_size += 1; + } + } + } + + glActiveTexture(GL_TEXTURE0); /* unit 0 */ + glBindTexture(GL_TEXTURE_2D, 42); + glCompressedTexImage2D(GL_TEXTURE_2D, 0, internalFormat, SZ, SZ, 0, + image_size, palette); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); +#undef SZ + + return image_size; +} + + +static GLint +make_texture(void) +{ +#define SZ 64 + GLenum Filter = GL_LINEAR; + GLubyte image[SZ][SZ][4]; + GLuint i, j; + + for (i = 0; i < SZ; i++) { + for (j = 0; j < SZ; j++) { + GLfloat d = (i - SZ/2) * (i - SZ/2) + (j - SZ/2) * (j - SZ/2); + d = sqrt(d); + if (d < SZ/3) { + image[i][j][0] = 255; + image[i][j][1] = 255; + image[i][j][2] = 255; + image[i][j][3] = 255; + } + else { + image[i][j][0] = 127; + image[i][j][1] = 127; + image[i][j][2] = 127; + image[i][j][3] = 255; + } + } + } + + glActiveTexture(GL_TEXTURE0); /* unit 0 */ + glBindTexture(GL_TEXTURE_2D, 42); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SZ, SZ, 0, + GL_RGBA, GL_UNSIGNED_BYTE, image); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); +#undef SZ + + return sizeof(image); +} + + + +static void +init(void) +{ + static const GLfloat red[4] = {1, 0, 0, 0}; + static const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0}; + static const GLfloat diffuse[4] = {0.7, 0.7, 0.7, 1.0}; + static const GLfloat specular[4] = {0.001, 0.001, 0.001, 1.0}; + static const GLfloat pos[4] = {20, 20, 50, 1}; + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red); + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white); + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 9.0); + + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, pos); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); + glLightfv(GL_LIGHT0, GL_SPECULAR, specular); + + glClearColor(0.4, 0.4, 0.4, 0.0); + glEnable(GL_DEPTH_TEST); + + make_texture(); + glEnable(GL_TEXTURE_2D); +} + + +/* + * Create an RGB, double-buffered X window. + * Return the window and context handles. + */ +static void +make_x_window(Display *x_dpy, EGLDisplay egl_dpy, + const char *name, + int x, int y, int width, int height, + Window *winRet, + EGLContext *ctxRet, + EGLSurface *surfRet) +{ + static const EGLint attribs[] = { + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_NONE + }; + + int scrnum; + XSetWindowAttributes attr; + unsigned long mask; + Window root; + Window win; + XVisualInfo *visInfo, visTemplate; + int num_visuals; + EGLContext ctx; + EGLConfig config; + EGLint num_configs; + EGLint vid; + + scrnum = DefaultScreen( x_dpy ); + root = RootWindow( x_dpy, scrnum ); + + if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL visual config\n"); + exit(1); + } + + assert(config); + assert(num_configs > 0); + + if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { + printf("Error: eglGetConfigAttrib() failed\n"); + exit(1); + } + + /* The X window visual must match the EGL config */ + visTemplate.visualid = vid; + visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); + if (!visInfo) { + printf("Error: couldn't get X visual\n"); + exit(1); + } + + /* window attributes */ + attr.background_pixel = 0; + attr.border_pixel = 0; + attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + win = XCreateWindow( x_dpy, root, 0, 0, width, height, + 0, visInfo->depth, InputOutput, + visInfo->visual, mask, &attr ); + + /* set hints and properties */ + { + XSizeHints sizehints; + sizehints.x = x; + sizehints.y = y; + sizehints.width = width; + sizehints.height = height; + sizehints.flags = USSize | USPosition; + XSetNormalHints(x_dpy, win, &sizehints); + XSetStandardProperties(x_dpy, win, name, name, + None, (char **)NULL, 0, &sizehints); + } + + eglBindAPI(EGL_OPENGL_ES_API); + + ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL ); + if (!ctx) { + printf("Error: eglCreateContext failed\n"); + exit(1); + } + + *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); + + if (!*surfRet) { + printf("Error: eglCreateWindowSurface failed\n"); + exit(1); + } + + XFree(visInfo); + + *winRet = win; + *ctxRet = ctx; +} + + +static void +event_loop(Display *dpy, Window win, + EGLDisplay egl_dpy, EGLSurface egl_surf) +{ + int anim = 1; + + while (1) { + int redraw = 0; + + if (!anim || XPending(dpy)) { + XEvent event; + XNextEvent(dpy, &event); + + switch (event.type) { + case Expose: + redraw = 1; + break; + case ConfigureNotify: + reshape(event.xconfigure.width, event.xconfigure.height); + break; + case KeyPress: + { + char buffer[10]; + int r, code; + code = XLookupKeysym(&event.xkey, 0); + if (code == XK_Left) { + view_roty += 5.0; + } + else if (code == XK_Right) { + view_roty -= 5.0; + } + else if (code == XK_Up) { + view_rotx += 5.0; + } + else if (code == XK_Down) { + view_rotx -= 5.0; + } + else if (code == XK_t) { + GLint size; + tex_format = (tex_format + 1) % (NUM_CPAL_FORMATS + 1); + if (tex_format < NUM_CPAL_FORMATS) { + size = make_cpal_texture(tex_format); + printf("Using %s (%d bytes)\n", + cpal_formats[tex_format].name, size); + } + else { + size = make_texture(); + printf("Using uncompressed texture (%d bytes)\n", size); + } + } + else { + r = XLookupString(&event.xkey, buffer, sizeof(buffer), + NULL, NULL); + if (buffer[0] == ' ') { + anim = !anim; + } + else if (buffer[0] == 27) { + /* escape */ + return; + } + } + } + redraw = 1; + break; + default: + ; /*no-op*/ + } + } + + if (anim) { + view_rotx += 1.0; + view_roty += 2.0; + redraw = 1; + } + + if (redraw) { + draw(); + eglSwapBuffers(egl_dpy, egl_surf); + } + } +} + + +static void +usage(void) +{ + printf("Usage:\n"); + printf(" -display <displayname> set the display to run on\n"); + printf(" -info display OpenGL renderer info\n"); +} + + +int +main(int argc, char *argv[]) +{ + const int winWidth = 300, winHeight = 300; + Display *x_dpy; + Window win; + EGLSurface egl_surf; + EGLContext egl_ctx; + EGLDisplay egl_dpy; + char *dpyName = NULL; + GLboolean printInfo = GL_FALSE; + EGLint egl_major, egl_minor; + int i; + const char *s; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) { + dpyName = argv[i+1]; + i++; + } + else if (strcmp(argv[i], "-info") == 0) { + printInfo = GL_TRUE; + } + else { + usage(); + return -1; + } + } + + x_dpy = XOpenDisplay(dpyName); + if (!x_dpy) { + printf("Error: couldn't open display %s\n", + dpyName ? dpyName : getenv("DISPLAY")); + return -1; + } + + egl_dpy = eglGetDisplay(x_dpy); + if (!egl_dpy) { + printf("Error: eglGetDisplay() failed\n"); + return -1; + } + + if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { + printf("Error: eglInitialize() failed\n"); + return -1; + } + + s = eglQueryString(egl_dpy, EGL_VERSION); + printf("EGL_VERSION = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_VENDOR); + printf("EGL_VENDOR = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_EXTENSIONS); + printf("EGL_EXTENSIONS = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); + printf("EGL_CLIENT_APIS = %s\n", s); + + make_x_window(x_dpy, egl_dpy, + "torus", 0, 0, winWidth, winHeight, + &win, &egl_ctx, &egl_surf); + + XMapWindow(x_dpy, win); + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { + printf("Error: eglMakeCurrent() failed\n"); + return -1; + } + + if (printInfo) { + printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); + printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); + printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); + printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); + } + + init(); + + /* Set initial projection/viewing transformation. + * We can't be sure we'll get a ConfigureNotify event when the window + * first appears. + */ + reshape(winWidth, winHeight); + + event_loop(x_dpy, win, egl_dpy, egl_surf); + + eglDestroyContext(egl_dpy, egl_ctx); + eglDestroySurface(egl_dpy, egl_surf); + eglTerminate(egl_dpy); + + + XDestroyWindow(x_dpy, win); + XCloseDisplay(x_dpy); + + return 0; +} diff --git a/progs/es1/xegl/tri.c b/progs/es1/xegl/tri.c new file mode 100644 index 00000000000..42a978207c9 --- /dev/null +++ b/progs/es1/xegl/tri.c @@ -0,0 +1,470 @@ +/* + * Copyright (C) 2008 Brian Paul 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 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 + * BRIAN PAUL 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. + */ + +/* + * Draw a triangle with X/EGL and OpenGL ES 1.x + * Brian Paul + * 5 June 2008 + */ + +#define USE_FULL_GL 0 + +#define USE_FIXED_POINT 0 + + +#include <assert.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> +#if USE_FULL_GL +#include <GL/gl.h> /* use full OpenGL */ +#else +#include <GLES/gl.h> /* use OpenGL ES 1.x */ +#include <GLES/glext.h> +#endif +#include <EGL/egl.h> + + +#define FLOAT_TO_FIXED(X) ((X) * 65535.0) + + + +static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0; + + +static void +draw(void) +{ +#if USE_FIXED_POINT + static const GLfixed verts[3][2] = { + { -65536, -65536 }, + { 65536, -65536 }, + { 0, 65536 } + }; + static const GLfixed colors[3][4] = { + { 65536, 0, 0, 65536 }, + { 0, 65536, 0 , 65536}, + { 0, 0, 65536 , 65536} + }; +#else + static const GLfloat verts[3][2] = { + { -1, -1 }, + { 1, -1 }, + { 0, 1 } + }; + static const GLfloat colors[3][4] = { + { 1, 0, 0, 1 }, + { 0, 1, 0, 1 }, + { 0, 0, 1, 1 } + }; +#endif + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glRotatef(view_rotx, 1, 0, 0); + glRotatef(view_roty, 0, 1, 0); + glRotatef(view_rotz, 0, 0, 1); + + { +#if USE_FIXED_POINT + glVertexPointer(2, GL_FIXED, 0, verts); + glColorPointer(4, GL_FIXED, 0, colors); +#else + glVertexPointer(2, GL_FLOAT, 0, verts); + glColorPointer(4, GL_FLOAT, 0, colors); +#endif + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + /* draw triangle */ + glDrawArrays(GL_TRIANGLES, 0, 3); + + /* draw some points */ + glPointSizex(FLOAT_TO_FIXED(15.5)); + glDrawArrays(GL_POINTS, 0, 3); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + } + + if (0) { + /* test code */ + GLfixed size; + glGetFixedv(GL_POINT_SIZE, &size); + printf("GL_POINT_SIZE = 0x%x %f\n", size, size / 65536.0); + } + + glPopMatrix(); +} + + +/* new window size or exposure */ +static void +reshape(int width, int height) +{ + GLfloat ar = (GLfloat) width / (GLfloat) height; + + glViewport(0, 0, (GLint) width, (GLint) height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); +#ifdef GL_VERSION_ES_CM_1_0 + glFrustumf(-ar, ar, -1, 1, 5.0, 60.0); +#else + glFrustum(-ar, ar, -1, 1, 5.0, 60.0); +#endif + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -10.0); +} + + +static void +test_query_matrix(void) +{ + PFNGLQUERYMATRIXXOESPROC procQueryMatrixx; + typedef void (*voidproc)(); + GLfixed mantissa[16]; + GLint exponent[16]; + GLbitfield rv; + int i; + + procQueryMatrixx = (PFNGLQUERYMATRIXXOESPROC) eglGetProcAddress("glQueryMatrixxOES"); + assert(procQueryMatrixx); + /* Actually try out this one */ + rv = (*procQueryMatrixx)(mantissa, exponent); + for (i = 0; i < 16; i++) { + if (rv & (1<<i)) { + printf("matrix[%d] invalid\n", i); + } + else { + printf("matrix[%d] = %f * 2^(%d)\n", i, mantissa[i]/65536.0, exponent[i]); + } + } + assert(!eglGetProcAddress("glFoo")); +} + + +static void +init(void) +{ + glClearColor(0.4, 0.4, 0.4, 0.0); + + if (0) + test_query_matrix(); +} + + +/* + * Create an RGB, double-buffered X window. + * Return the window and context handles. + */ +static void +make_x_window(Display *x_dpy, EGLDisplay egl_dpy, + const char *name, + int x, int y, int width, int height, + Window *winRet, + EGLContext *ctxRet, + EGLSurface *surfRet) +{ + static const EGLint attribs[] = { + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_NONE + }; + + int scrnum; + XSetWindowAttributes attr; + unsigned long mask; + Window root; + Window win; + XVisualInfo *visInfo, visTemplate; + int num_visuals; + EGLContext ctx; + EGLConfig config; + EGLint num_configs; + EGLint vid; + + scrnum = DefaultScreen( x_dpy ); + root = RootWindow( x_dpy, scrnum ); + + if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL visual config\n"); + exit(1); + } + + assert(config); + assert(num_configs > 0); + + if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { + printf("Error: eglGetConfigAttrib() failed\n"); + exit(1); + } + + /* The X window visual must match the EGL config */ + visTemplate.visualid = vid; + visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); + if (!visInfo) { + printf("Error: couldn't get X visual\n"); + exit(1); + } + + /* window attributes */ + attr.background_pixel = 0; + attr.border_pixel = 0; + attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + win = XCreateWindow( x_dpy, root, 0, 0, width, height, + 0, visInfo->depth, InputOutput, + visInfo->visual, mask, &attr ); + + /* set hints and properties */ + { + XSizeHints sizehints; + sizehints.x = x; + sizehints.y = y; + sizehints.width = width; + sizehints.height = height; + sizehints.flags = USSize | USPosition; + XSetNormalHints(x_dpy, win, &sizehints); + XSetStandardProperties(x_dpy, win, name, name, + None, (char **)NULL, 0, &sizehints); + } + +#if USE_FULL_GL + eglBindAPI(EGL_OPENGL_API); +#else + eglBindAPI(EGL_OPENGL_ES_API); +#endif + + ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL ); + if (!ctx) { + printf("Error: eglCreateContext failed\n"); + exit(1); + } + + *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); + + if (!*surfRet) { + printf("Error: eglCreateWindowSurface failed\n"); + exit(1); + } + + XFree(visInfo); + + *winRet = win; + *ctxRet = ctx; +} + + +static void +event_loop(Display *dpy, Window win, + EGLDisplay egl_dpy, EGLSurface egl_surf) +{ + while (1) { + int redraw = 0; + XEvent event; + + XNextEvent(dpy, &event); + + switch (event.type) { + case Expose: + redraw = 1; + break; + case ConfigureNotify: + reshape(event.xconfigure.width, event.xconfigure.height); + break; + case KeyPress: + { + char buffer[10]; + int r, code; + code = XLookupKeysym(&event.xkey, 0); + if (code == XK_Left) { + view_roty += 5.0; + } + else if (code == XK_Right) { + view_roty -= 5.0; + } + else if (code == XK_Up) { + view_rotx += 5.0; + } + else if (code == XK_Down) { + view_rotx -= 5.0; + } + else { + r = XLookupString(&event.xkey, buffer, sizeof(buffer), + NULL, NULL); + if (buffer[0] == 27) { + /* escape */ + return; + } + } + } + redraw = 1; + break; + default: + ; /*no-op*/ + } + + if (redraw) { + draw(); + eglSwapBuffers(egl_dpy, egl_surf); + } + } +} + + +static void +usage(void) +{ + printf("Usage:\n"); + printf(" -display <displayname> set the display to run on\n"); + printf(" -info display OpenGL renderer info\n"); +} + + +int +main(int argc, char *argv[]) +{ + const int winWidth = 300, winHeight = 300; + Display *x_dpy; + Window win; + EGLSurface egl_surf; + EGLContext egl_ctx; + EGLDisplay egl_dpy; + char *dpyName = NULL; + GLboolean printInfo = GL_FALSE; + EGLint egl_major, egl_minor; + int i; + const char *s; + + static struct { + char *name; + GLenum value; + enum {GetString, GetInteger} type; + } info_items[] = { + {"GL_RENDERER", GL_RENDERER, GetString}, + {"GL_VERSION", GL_VERSION, GetString}, + {"GL_VENDOR", GL_VENDOR, GetString}, + {"GL_EXTENSIONS", GL_EXTENSIONS, GetString}, + {"GL_MAX_PALETTE_MATRICES_OES", GL_MAX_PALETTE_MATRICES_OES, GetInteger}, + {"GL_MAX_VERTEX_UNITS_OES", GL_MAX_VERTEX_UNITS_OES, GetInteger}, + }; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) { + dpyName = argv[i+1]; + i++; + } + else if (strcmp(argv[i], "-info") == 0) { + printInfo = GL_TRUE; + } + else { + usage(); + return -1; + } + } + + x_dpy = XOpenDisplay(dpyName); + if (!x_dpy) { + printf("Error: couldn't open display %s\n", + dpyName ? dpyName : getenv("DISPLAY")); + return -1; + } + + egl_dpy = eglGetDisplay(x_dpy); + if (!egl_dpy) { + printf("Error: eglGetDisplay() failed\n"); + return -1; + } + + if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { + printf("Error: eglInitialize() failed\n"); + return -1; + } + + s = eglQueryString(egl_dpy, EGL_VERSION); + printf("EGL_VERSION = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_VENDOR); + printf("EGL_VENDOR = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_EXTENSIONS); + printf("EGL_EXTENSIONS = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); + printf("EGL_CLIENT_APIS = %s\n", s); + + make_x_window(x_dpy, egl_dpy, + "OpenGL ES 1.x tri", 0, 0, winWidth, winHeight, + &win, &egl_ctx, &egl_surf); + + XMapWindow(x_dpy, win); + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { + printf("Error: eglMakeCurrent() failed\n"); + return -1; + } + + if (printInfo) { + for (i = 0; i < sizeof(info_items)/sizeof(info_items[0]); i++) { + switch (info_items[i].type) { + case GetString: + printf("%s = %s\n", info_items[i].name, (char *)glGetString(info_items[i].value)); + break; + case GetInteger: { + GLint rv = -1; + glGetIntegerv(info_items[i].value, &rv); + printf("%s = %d\n", info_items[i].name, rv); + break; + } + } + } + }; + init(); + + /* Set initial projection/viewing transformation. + * We can't be sure we'll get a ConfigureNotify event when the window + * first appears. + */ + reshape(winWidth, winHeight); + + event_loop(x_dpy, win, egl_dpy, egl_surf); + + eglDestroyContext(egl_dpy, egl_ctx); + eglDestroySurface(egl_dpy, egl_surf); + eglTerminate(egl_dpy); + + + XDestroyWindow(x_dpy, win); + XCloseDisplay(x_dpy); + + return 0; +} diff --git a/progs/es1/xegl/two_win.c b/progs/es1/xegl/two_win.c new file mode 100644 index 00000000000..4785e5304d3 --- /dev/null +++ b/progs/es1/xegl/two_win.c @@ -0,0 +1,433 @@ +/* + * Copyright (C) 2008 Brian Paul 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 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 + * BRIAN PAUL 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. + */ + +/* + * Test drawing to two windows. + * Brian Paul + * August 2008 + */ + + +#include <assert.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> +#include <GLES/gl.h> +#include <GLES/glext.h> +#include <EGL/egl.h> + + +static int WinWidth[2] = {150, 300}, WinHeight[2] = {150, 300}; + + +static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0; + + +/* new window size or exposure */ +static void +reshape(int width, int height) +{ + GLfloat ar = (GLfloat) width / (GLfloat) height; + + glViewport(0, 0, (GLint) width, (GLint) height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); +#ifdef GL_VERSION_ES_CM_1_0 + glFrustumf(-ar, ar, -1, 1, 5.0, 60.0); +#else + glFrustum(-ar, ar, -1, 1, 5.0, 60.0); +#endif + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -10.0); +} + + +static void +draw(int win) +{ + static const GLfloat verts[3][2] = { + { -1, -1 }, + { 1, -1 }, + { 0, 1 } + }; + static const GLfloat colors[3][4] = { + { 1, 0, 0, 1 }, + { 0, 1, 0, 1 }, + { 0, 0, 1, 1 } + }; + + assert(win == 0 || win == 1); + + reshape(WinWidth[win], WinHeight[win]); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glRotatef(view_rotx, 1, 0, 0); + glRotatef(view_roty, 0, 1, 0); + glRotatef(view_rotz, 0, 0, 1); + + /* draw triangle */ + { + glVertexPointer(2, GL_FLOAT, 0, verts); + glColorPointer(4, GL_FLOAT, 0, colors); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + } + + glPopMatrix(); +} + + +static void +init(void) +{ + glClearColor(0.4, 0.4, 0.4, 0.0); +} + + +/* + * Create an RGB, double-buffered X window. + * Return the window and context handles. + */ +static void +make_x_window(Display *x_dpy, EGLDisplay egl_dpy, + const char *name, + int x, int y, int width, int height, + Window *winRet, + EGLContext *ctxRet, + EGLSurface *surfRet) +{ + static const EGLint attribs[] = { + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_NONE + }; + + int scrnum; + XSetWindowAttributes attr; + unsigned long mask; + Window root; + Window win; + XVisualInfo *visInfo, visTemplate; + int num_visuals; + EGLContext ctx; + EGLConfig config; + EGLint num_configs; + EGLint vid; + + scrnum = DefaultScreen( x_dpy ); + root = RootWindow( x_dpy, scrnum ); + + if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL visual config\n"); + exit(1); + } + + assert(config); + assert(num_configs > 0); + + if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { + printf("Error: eglGetConfigAttrib() failed\n"); + exit(1); + } + + /* The X window visual must match the EGL config */ + visTemplate.visualid = vid; + visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); + if (!visInfo) { + printf("Error: couldn't get X visual\n"); + exit(1); + } + + /* window attributes */ + attr.background_pixel = 0; + attr.border_pixel = 0; + attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + win = XCreateWindow( x_dpy, root, x, y, width, height, + 0, visInfo->depth, InputOutput, + visInfo->visual, mask, &attr ); + + /* set hints and properties */ + { + XSizeHints sizehints; + sizehints.x = x; + sizehints.y = y; + sizehints.width = width; + sizehints.height = height; + sizehints.flags = USSize | USPosition; + XSetNormalHints(x_dpy, win, &sizehints); + XSetStandardProperties(x_dpy, win, name, name, + None, (char **)NULL, 0, &sizehints); + } + +#if USE_FULL_GL + eglBindAPI(EGL_OPENGL_API); +#else + eglBindAPI(EGL_OPENGL_ES_API); +#endif + + if (ctxRet) { + ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL ); + if (!ctx) { + printf("Error: eglCreateContext failed\n"); + exit(1); + } + *ctxRet = ctx; + } + + *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); + + if (!*surfRet) { + printf("Error: eglCreateWindowSurface failed\n"); + exit(1); + } + + XFree(visInfo); + + *winRet = win; +} + + +static void +event_loop(Display *dpy, Window win1, Window win2, + EGLDisplay egl_dpy, EGLSurface egl_surf1, EGLSurface egl_surf2, + EGLContext egl_ctx) +{ + while (1) { + int redraw = 0; + int win; + XEvent event; + + XNextEvent(dpy, &event); + + switch (event.type) { + case Expose: + redraw = 1; + break; + case ConfigureNotify: + if (event.xconfigure.window == win1) + win = 0; + else + win = 1; + WinWidth[win] = event.xconfigure.width; + WinHeight[win] = event.xconfigure.height; + break; + case KeyPress: + { + char buffer[10]; + int r, code; + code = XLookupKeysym(&event.xkey, 0); + if (code == XK_Left) { + view_roty += 5.0; + } + else if (code == XK_Right) { + view_roty -= 5.0; + } + else if (code == XK_Up) { + view_rotx += 5.0; + } + else if (code == XK_Down) { + view_rotx -= 5.0; + } + else { + r = XLookupString(&event.xkey, buffer, sizeof(buffer), + NULL, NULL); + if (buffer[0] == 27) { + /* escape */ + return; + } + } + } + redraw = 1; + break; + default: + ; /*no-op*/ + } + + if (redraw) { + /* win 1 */ + if (!eglMakeCurrent(egl_dpy, egl_surf1, egl_surf1, egl_ctx)) { + printf("Error: eglMakeCurrent(1) failed\n"); + return; + } + draw(0); + eglSwapBuffers(egl_dpy, egl_surf1); + + /* win 2 */ + if (!eglMakeCurrent(egl_dpy, egl_surf2, egl_surf2, egl_ctx)) { + printf("Error: eglMakeCurrent(2) failed\n"); + return; + } + draw(1); + eglSwapBuffers(egl_dpy, egl_surf2); + } + } +} + + +static void +usage(void) +{ + printf("Usage:\n"); + printf(" -display <displayname> set the display to run on\n"); + printf(" -info display OpenGL renderer info\n"); +} + + +int +main(int argc, char *argv[]) +{ + Display *x_dpy; + Window win1, win2; + EGLSurface egl_surf1, egl_surf2; + EGLContext egl_ctx; + EGLDisplay egl_dpy; + char *dpyName = NULL; + GLboolean printInfo = GL_FALSE; + EGLint egl_major, egl_minor; + int i; + const char *s; + + static struct { + char *name; + GLenum value; + enum {GetString, GetInteger} type; + } info_items[] = { + {"GL_RENDERER", GL_RENDERER, GetString}, + {"GL_VERSION", GL_VERSION, GetString}, + {"GL_VENDOR", GL_VENDOR, GetString}, + {"GL_EXTENSIONS", GL_EXTENSIONS, GetString}, + {"GL_MAX_PALETTE_MATRICES_OES", GL_MAX_PALETTE_MATRICES_OES, GetInteger}, + {"GL_MAX_VERTEX_UNITS_OES", GL_MAX_VERTEX_UNITS_OES, GetInteger}, + }; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) { + dpyName = argv[i+1]; + i++; + } + else if (strcmp(argv[i], "-info") == 0) { + printInfo = GL_TRUE; + } + else { + usage(); + return -1; + } + } + + x_dpy = XOpenDisplay(dpyName); + if (!x_dpy) { + printf("Error: couldn't open display %s\n", + dpyName ? dpyName : getenv("DISPLAY")); + return -1; + } + + egl_dpy = eglGetDisplay(x_dpy); + if (!egl_dpy) { + printf("Error: eglGetDisplay() failed\n"); + return -1; + } + + if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { + printf("Error: eglInitialize() failed\n"); + return -1; + } + + s = eglQueryString(egl_dpy, EGL_VERSION); + printf("EGL_VERSION = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_VENDOR); + printf("EGL_VENDOR = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_EXTENSIONS); + printf("EGL_EXTENSIONS = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); + printf("EGL_CLIENT_APIS = %s\n", s); + + make_x_window(x_dpy, egl_dpy, + "xegl_two_win #1", 0, 0, WinWidth[0], WinHeight[0], + &win1, &egl_ctx, &egl_surf1); + + make_x_window(x_dpy, egl_dpy, + "xegl_two_win #2", WinWidth[0] + 50, 0, + WinWidth[1], WinHeight[1], + &win2, NULL, &egl_surf2); + + XMapWindow(x_dpy, win1); + + XMapWindow(x_dpy, win2); + + if (!eglMakeCurrent(egl_dpy, egl_surf1, egl_surf1, egl_ctx)) { + printf("Error: eglMakeCurrent() failed\n"); + return -1; + } + + if (printInfo) { + for (i = 0; i < sizeof(info_items)/sizeof(info_items[0]); i++) { + switch (info_items[i].type) { + case GetString: + printf("%s = %s\n", info_items[i].name, (char *)glGetString(info_items[i].value)); + break; + case GetInteger: { + GLint rv = -1; + glGetIntegerv(info_items[i].value, &rv); + printf("%s = %d\n", info_items[i].name, rv); + break; + } + } + } + }; + + init(); + + event_loop(x_dpy, win1, win2, egl_dpy, egl_surf1, egl_surf2, egl_ctx); + + eglDestroyContext(egl_dpy, egl_ctx); + eglDestroySurface(egl_dpy, egl_surf1); + eglDestroySurface(egl_dpy, egl_surf2); + eglTerminate(egl_dpy); + + XDestroyWindow(x_dpy, win1); + XDestroyWindow(x_dpy, win2); + XCloseDisplay(x_dpy); + + return 0; +} diff --git a/progs/es2/.gitignore b/progs/es2/.gitignore new file mode 100644 index 00000000000..7d5c16936c2 --- /dev/null +++ b/progs/es2/.gitignore @@ -0,0 +1,3 @@ +xegl/es2_info.c +xegl/es2_info +xegl/tri diff --git a/progs/es2/xegl/Makefile b/progs/es2/xegl/Makefile new file mode 100644 index 00000000000..5bb167c1c63 --- /dev/null +++ b/progs/es2/xegl/Makefile @@ -0,0 +1,51 @@ +# progs/es2/xegl/Makefile + +TOP = ../../.. +include $(TOP)/configs/current + + +INCLUDE_DIRS = \ + -I$(TOP)/include \ + +HEADERS = $(TOP)/include/GLES/egl.h + + +ES2_LIB_DEPS = \ + $(TOP)/$(LIB_DIR)/libEGL.so \ + $(TOP)/$(LIB_DIR)/libGLESv2.so + + +ES2_LIBS = \ + -L$(TOP)/$(LIB_DIR) -lEGL \ + -L$(TOP)/$(LIB_DIR) -lGLESv2 $(LIBDRM_LIB) -lX11 + +PROGRAMS = \ + es2_info \ + tri + + +.c.o: + $(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $< -o $@ + + + +default: $(PROGRAMS) + + + +es2_info.c: ../../es1/xegl/es1_info.c + cp -f $^ $@ + +es2_info: es2_info.o $(ES2_LIB_DEPS) + $(CC) $(CFLAGS) es2_info.o $(ES2_LIBS) -o $@ + +tri: tri.o $(ES2_LIB_DEPS) + $(CC) $(CFLAGS) tri.o $(ES2_LIBS) -o $@ + + + +clean: + rm -f *.o *~ + rm -f $(PROGRAMS) + rm -f es2_info.c + diff --git a/progs/es2/xegl/tri.c b/progs/es2/xegl/tri.c new file mode 100644 index 00000000000..7729a099578 --- /dev/null +++ b/progs/es2/xegl/tri.c @@ -0,0 +1,515 @@ +/************************************************************************** + * + * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. + * All Rights Reserved. + * + **************************************************************************/ + +/* + * Draw a triangle with X/EGL and OpenGL ES 2.x + */ + +#define USE_FULL_GL 0 + + + +#include <assert.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> +#include <X11/keysym.h> +#if USE_FULL_GL +#include <GL/gl.h> /* use full OpenGL */ +#else +#include <GLES2/gl2.h> /* use OpenGL ES 2.x */ +#endif +#include <EGL/egl.h> + + +#define FLOAT_TO_FIXED(X) ((X) * 65535.0) + + + +static GLfloat view_rotx = 0.0, view_roty = 0.0; + +static GLint u_matrix = -1; +static GLint attr_pos = 0, attr_color = 1; + + +static void +make_z_rot_matrix(GLfloat angle, GLfloat *m) +{ + float c = cos(angle * M_PI / 180.0); + float s = sin(angle * M_PI / 180.0); + int i; + for (i = 0; i < 16; i++) + m[i] = 0.0; + m[0] = m[5] = m[10] = m[15] = 1.0; + + m[0] = c; + m[1] = s; + m[4] = -s; + m[5] = c; +} + +static void +make_scale_matrix(GLfloat xs, GLfloat ys, GLfloat zs, GLfloat *m) +{ + int i; + for (i = 0; i < 16; i++) + m[i] = 0.0; + m[0] = xs; + m[5] = ys; + m[10] = zs; + m[15] = 1.0; +} + + +static void +mul_matrix(GLfloat *prod, const GLfloat *a, const GLfloat *b) +{ +#define A(row,col) a[(col<<2)+row] +#define B(row,col) b[(col<<2)+row] +#define P(row,col) p[(col<<2)+row] + GLfloat p[16]; + GLint i; + for (i = 0; i < 4; i++) { + const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); + P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0); + P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1); + P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2); + P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3); + } + memcpy(prod, p, sizeof(p)); +#undef A +#undef B +#undef PROD +} + + +static void +draw(void) +{ + static const GLfloat verts[3][2] = { + { -1, -1 }, + { 1, -1 }, + { 0, 1 } + }; + static const GLfloat colors[3][3] = { + { 1, 0, 0 }, + { 0, 1, 0 }, + { 0, 0, 1 } + }; + GLfloat mat[16], rot[16], scale[16]; + + /* Set modelview/projection matrix */ + make_z_rot_matrix(view_rotx, rot); + make_scale_matrix(0.5, 0.5, 0.5, scale); + mul_matrix(mat, rot, scale); + glUniformMatrix4fv(u_matrix, 1, GL_FALSE, mat); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + { + glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, verts); + glVertexAttribPointer(attr_color, 3, GL_FLOAT, GL_FALSE, 0, colors); + glEnableVertexAttribArray(attr_pos); + glEnableVertexAttribArray(attr_color); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glDisableVertexAttribArray(attr_pos); + glDisableVertexAttribArray(attr_color); + } +} + + +/* new window size or exposure */ +static void +reshape(int width, int height) +{ + glViewport(0, 0, (GLint) width, (GLint) height); +} + + +static void +create_shaders(void) +{ + static const char *fragShaderText = + "varying vec4 v_color;\n" + "void main() {\n" + " gl_FragColor = v_color;\n" + "}\n"; + static const char *vertShaderText = + "uniform mat4 modelviewProjection;\n" + "attribute vec4 pos;\n" + "attribute vec4 color;\n" + "varying vec4 v_color;\n" + "void main() {\n" + " gl_Position = modelviewProjection * pos;\n" + " v_color = color;\n" + "}\n"; + + GLuint fragShader, vertShader, program; + GLint stat; + + fragShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragShader, 1, (const char **) &fragShaderText, NULL); + glCompileShader(fragShader); + glGetShaderiv(fragShader, GL_COMPILE_STATUS, &stat); + if (!stat) { + printf("Error: fragment shader did not compile!\n"); + exit(1); + } + + vertShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertShader, 1, (const char **) &vertShaderText, NULL); + glCompileShader(vertShader); + glGetShaderiv(vertShader, GL_COMPILE_STATUS, &stat); + if (!stat) { + printf("Error: vertex shader did not compile!\n"); + exit(1); + } + + program = glCreateProgram(); + glAttachShader(program, fragShader); + glAttachShader(program, vertShader); + glLinkProgram(program); + + glGetProgramiv(program, GL_LINK_STATUS, &stat); + if (!stat) { + char log[1000]; + GLsizei len; + glGetProgramInfoLog(program, 1000, &len, log); + printf("Error: linking:\n%s\n", log); + exit(1); + } + + glUseProgram(program); + + if (1) { + /* test setting attrib locations */ + glBindAttribLocation(program, attr_pos, "pos"); + glBindAttribLocation(program, attr_color, "color"); + glLinkProgram(program); /* needed to put attribs into effect */ + } + else { + /* test automatic attrib locations */ + attr_pos = glGetAttribLocation(program, "pos"); + attr_color = glGetAttribLocation(program, "color"); + } + + u_matrix = glGetUniformLocation(program, "modelviewProjection"); + printf("Uniform modelviewProjection at %d\n", u_matrix); + printf("Attrib pos at %d\n", attr_pos); + printf("Attrib color at %d\n", attr_color); +} + + +static void +init(void) +{ + typedef void (*proc)(); + +#if 1 /* test code */ + proc p = eglGetProcAddress("glMapBufferOES"); + assert(p); +#endif + + glClearColor(0.4, 0.4, 0.4, 0.0); + + create_shaders(); +} + + +/* + * Create an RGB, double-buffered X window. + * Return the window and context handles. + */ +static void +make_x_window(Display *x_dpy, EGLDisplay egl_dpy, + const char *name, + int x, int y, int width, int height, + Window *winRet, + EGLContext *ctxRet, + EGLSurface *surfRet) +{ + static const EGLint attribs[] = { + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_DEPTH_SIZE, 1, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL_NONE + }; + static const EGLint ctx_attribs[] = { + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE + }; + int scrnum; + XSetWindowAttributes attr; + unsigned long mask; + Window root; + Window win; + XVisualInfo *visInfo, visTemplate; + int num_visuals; + EGLContext ctx; + EGLConfig config; + EGLint num_configs; + EGLint vid; + + scrnum = DefaultScreen( x_dpy ); + root = RootWindow( x_dpy, scrnum ); + + if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { + printf("Error: couldn't get an EGL visual config\n"); + exit(1); + } + + assert(config); + assert(num_configs > 0); + + if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { + printf("Error: eglGetConfigAttrib() failed\n"); + exit(1); + } + + /* The X window visual must match the EGL config */ + visTemplate.visualid = vid; + visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); + if (!visInfo) { + printf("Error: couldn't get X visual\n"); + exit(1); + } + + /* window attributes */ + attr.background_pixel = 0; + attr.border_pixel = 0; + attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); + attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; + mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; + + win = XCreateWindow( x_dpy, root, 0, 0, width, height, + 0, visInfo->depth, InputOutput, + visInfo->visual, mask, &attr ); + + /* set hints and properties */ + { + XSizeHints sizehints; + sizehints.x = x; + sizehints.y = y; + sizehints.width = width; + sizehints.height = height; + sizehints.flags = USSize | USPosition; + XSetNormalHints(x_dpy, win, &sizehints); + XSetStandardProperties(x_dpy, win, name, name, + None, (char **)NULL, 0, &sizehints); + } + +#if USE_FULL_GL /* XXX fix this when eglBindAPI() works */ + eglBindAPI(EGL_OPENGL_API); +#else + eglBindAPI(EGL_OPENGL_ES_API); +#endif + + ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, ctx_attribs ); + if (!ctx) { + printf("Error: eglCreateContext failed\n"); + exit(1); + } + + /* test eglQueryContext() */ + { + EGLint val; + eglQueryContext(egl_dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &val); + assert(val == 2); + } + + *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); + if (!*surfRet) { + printf("Error: eglCreateWindowSurface failed\n"); + exit(1); + } + + { + EGLint val; + eglQuerySurface(egl_dpy, *surfRet, EGL_WIDTH, &val); + assert(val == width); + eglQuerySurface(egl_dpy, *surfRet, EGL_HEIGHT, &val); + assert(val == height); + eglQuerySurface(egl_dpy, *surfRet, EGL_SURFACE_TYPE, &val); + assert(val == EGL_WINDOW_BIT); + } + + XFree(visInfo); + + *winRet = win; + *ctxRet = ctx; +} + + +static void +event_loop(Display *dpy, Window win, + EGLDisplay egl_dpy, EGLSurface egl_surf) +{ + while (1) { + int redraw = 0; + XEvent event; + + XNextEvent(dpy, &event); + + switch (event.type) { + case Expose: + redraw = 1; + break; + case ConfigureNotify: + reshape(event.xconfigure.width, event.xconfigure.height); + break; + case KeyPress: + { + char buffer[10]; + int r, code; + code = XLookupKeysym(&event.xkey, 0); + if (code == XK_Left) { + view_roty += 5.0; + } + else if (code == XK_Right) { + view_roty -= 5.0; + } + else if (code == XK_Up) { + view_rotx += 5.0; + } + else if (code == XK_Down) { + view_rotx -= 5.0; + } + else { + r = XLookupString(&event.xkey, buffer, sizeof(buffer), + NULL, NULL); + if (buffer[0] == 27) { + /* escape */ + return; + } + } + } + redraw = 1; + break; + default: + ; /*no-op*/ + } + + if (redraw) { + draw(); + eglSwapBuffers(egl_dpy, egl_surf); + } + } +} + + +static void +usage(void) +{ + printf("Usage:\n"); + printf(" -display <displayname> set the display to run on\n"); + printf(" -info display OpenGL renderer info\n"); +} + + +int +main(int argc, char *argv[]) +{ + const int winWidth = 300, winHeight = 300; + Display *x_dpy; + Window win; + EGLSurface egl_surf; + EGLContext egl_ctx; + EGLDisplay egl_dpy; + char *dpyName = NULL; + GLboolean printInfo = GL_FALSE; + EGLint egl_major, egl_minor; + int i; + const char *s; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) { + dpyName = argv[i+1]; + i++; + } + else if (strcmp(argv[i], "-info") == 0) { + printInfo = GL_TRUE; + } + else { + usage(); + return -1; + } + } + + x_dpy = XOpenDisplay(dpyName); + if (!x_dpy) { + printf("Error: couldn't open display %s\n", + dpyName ? dpyName : getenv("DISPLAY")); + return -1; + } + + egl_dpy = eglGetDisplay(x_dpy); + if (!egl_dpy) { + printf("Error: eglGetDisplay() failed\n"); + return -1; + } + + if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { + printf("Error: eglInitialize() failed\n"); + return -1; + } + + s = eglQueryString(egl_dpy, EGL_VERSION); + printf("EGL_VERSION = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_VENDOR); + printf("EGL_VENDOR = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_EXTENSIONS); + printf("EGL_EXTENSIONS = %s\n", s); + + s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); + printf("EGL_CLIENT_APIS = %s\n", s); + + make_x_window(x_dpy, egl_dpy, + "OpenGL ES 2.x tri", 0, 0, winWidth, winHeight, + &win, &egl_ctx, &egl_surf); + + XMapWindow(x_dpy, win); + if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { + printf("Error: eglMakeCurrent() failed\n"); + return -1; + } + + if (printInfo) { + printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); + printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); + printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); + printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); + } + + init(); + + /* Set initial projection/viewing transformation. + * We can't be sure we'll get a ConfigureNotify event when the window + * first appears. + */ + reshape(winWidth, winHeight); + + event_loop(x_dpy, win, egl_dpy, egl_surf); + + eglDestroyContext(egl_dpy, egl_ctx); + eglDestroySurface(egl_dpy, egl_surf); + eglTerminate(egl_dpy); + + + XDestroyWindow(x_dpy, win); + XCloseDisplay(x_dpy); + + return 0; +} diff --git a/progs/fp/position-frc-integer.txt b/progs/fp/position-frc-integer.txt new file mode 100644 index 00000000000..3a634c9b3b8 --- /dev/null +++ b/progs/fp/position-frc-integer.txt @@ -0,0 +1,7 @@ +!!ARBfp1.0 +# expected: black triangle +# brown means the wrong pixel center convention is being used +OPTION ARB_fragment_coord_pixel_center_integer; +MOV result.color, {0}.x; +FRC result.color.xy, fragment.position; +END diff --git a/progs/fp/position-frc.txt b/progs/fp/position-frc.txt new file mode 100644 index 00000000000..35ae3efa10e --- /dev/null +++ b/progs/fp/position-frc.txt @@ -0,0 +1,6 @@ +!!ARBfp1.0 +# expected: brown triangle +# black means the wrong pixel center convention is being used +MOV result.color, {0}.x; +FRC result.color.xy, fragment.position; +END diff --git a/progs/fp/position-upper-left.txt b/progs/fp/position-upper-left.txt new file mode 100644 index 00000000000..ac632dbfb7d --- /dev/null +++ b/progs/fp/position-upper-left.txt @@ -0,0 +1,7 @@ +!!ARBfp1.0 +# expected: the yellow vertex is the bottom one +# if it is the top one, the wrong origin convention is being used +OPTION ARB_fragment_coord_origin_upper_left; +MOV result.color, {0}.x; +MUL result.color.xy, fragment.position, {.005}.x; +END diff --git a/progs/fp/position.txt b/progs/fp/position.txt index 1875897d781..f59d0259c72 100644 --- a/progs/fp/position.txt +++ b/progs/fp/position.txt @@ -1,4 +1,6 @@ !!ARBfp1.0 +# expected: the yellow vertex is the top one +# if it is the bottom one, the wrong origin convention is being used MOV result.color, {0}.x; MUL result.color.xy, fragment.position, {.005}.x; END diff --git a/progs/glsl/CH11-bumpmaptex.frag b/progs/glsl/CH11-bumpmaptex.frag index b1f93b784d9..b5dabb4c8a8 100644 --- a/progs/glsl/CH11-bumpmaptex.frag +++ b/progs/glsl/CH11-bumpmaptex.frag @@ -17,7 +17,7 @@ uniform float BumpDensity; // = 16.0 uniform float BumpSize; // = 0.15 uniform float SpecularFactor; // = 0.5 -sampler2D Tex; +uniform sampler2D Tex; void main() { diff --git a/progs/glsl/bump.c b/progs/glsl/bump.c index e31afab9392..784596448c5 100644 --- a/progs/glsl/bump.c +++ b/progs/glsl/bump.c @@ -51,7 +51,6 @@ static GLint win = 0; static GLfloat xRot = 20.0f, yRot = 0.0f, zRot = 0.0f; static GLint tangentAttrib; -static GLint tangentAttribTex; static GLuint Texture; diff --git a/progs/glsl/samplers.c b/progs/glsl/samplers.c index 87dad5d8575..8f26a5e329d 100644 --- a/progs/glsl/samplers.c +++ b/progs/glsl/samplers.c @@ -41,7 +41,6 @@ #include <string.h> #include <GL/glew.h> #include "GL/glut.h" -#include "readtex.h" #include "shaderutil.h" diff --git a/progs/tests/.gitignore b/progs/tests/.gitignore index 3479ff8b33b..8b3e9f35583 100644 --- a/progs/tests/.gitignore +++ b/progs/tests/.gitignore @@ -13,6 +13,7 @@ arbvptest3 arbvptorus arbvpwarpmesh arraytexture +auxbuffer blendminmax blendsquare blendxor @@ -23,10 +24,12 @@ bug_3195 bug_texstore_i8 bumpmap calibrate_rast +condrender copypixrate crossbar cva drawbuffers +drawbuffers2 extfuncs.h exactrast fbotest1 diff --git a/progs/tests/arraytexture.c b/progs/tests/arraytexture.c index 28252a354bf..e4e86b9b4ce 100644 --- a/progs/tests/arraytexture.c +++ b/progs/tests/arraytexture.c @@ -36,7 +36,6 @@ #include <math.h> #include <GL/glew.h> #include <GL/glut.h> -#include <GL/glext.h> #if !defined(GL_EXT_texture_array) && !defined(GL_MESA_texture_array) # error "This demo requires enums for either GL_EXT_texture_array or GL_MESA_texture_array to build." diff --git a/progs/tests/bufferobj.c b/progs/tests/bufferobj.c index d4ca2700160..dc479af0452 100644 --- a/progs/tests/bufferobj.c +++ b/progs/tests/bufferobj.c @@ -29,6 +29,7 @@ struct object GLuint VertexStride; GLuint ColorStride; GLuint NumElements; + GLuint MaxElement; }; static struct object Objects[NUM_OBJECTS]; @@ -58,7 +59,8 @@ static void DrawObject( const struct object *obj ) if (obj->NumElements > 0) { /* indexed arrays */ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, obj->ElementsBufferID); - glDrawElements(GL_LINE_LOOP, obj->NumElements, GL_UNSIGNED_INT, NULL); + glDrawRangeElements(GL_LINE_LOOP, 0, obj->MaxElement, + obj->NumElements, GL_UNSIGNED_INT, NULL); } else { /* non-indexed arrays */ @@ -300,6 +302,7 @@ static void MakeObject1(struct object *obj) obj->VertexStride = 0; obj->ColorStride = 0; obj->NumElements = 0; + obj->MaxElement = 0; glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); @@ -345,6 +348,7 @@ static void MakeObject2(struct object *obj) obj->ColorStride = 6 * sizeof(GLfloat); obj->NumElements = 0; + obj->MaxElement = 0; glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); @@ -401,6 +405,7 @@ static void MakeObject3(struct object *obj) i[3] = 3; glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB); obj->NumElements = 4; + obj->MaxElement = 3; if (Have_ARB_vertex_array_object) { CreateVertexArrayObject(obj); @@ -445,6 +450,7 @@ static void MakeObject4(struct object *obj) /* Setup a buffer of indices to test the ELEMENTS path */ obj->ElementsBufferID = 0; obj->NumElements = 0; + obj->MaxElement = 0; if (Have_ARB_vertex_array_object) { CreateVertexArrayObject(obj); diff --git a/progs/tests/bug_3195.c b/progs/tests/bug_3195.c index a075b94e370..3574c1f7176 100644 --- a/progs/tests/bug_3195.c +++ b/progs/tests/bug_3195.c @@ -38,7 +38,6 @@ #include <math.h> #include <GL/glew.h> #include <GL/glut.h> -#include <GL/glext.h> #include "readtex.h" diff --git a/progs/tests/cva.c b/progs/tests/cva.c index 02d1dcba2ec..b3e041c21e3 100644 --- a/progs/tests/cva.c +++ b/progs/tests/cva.c @@ -17,7 +17,6 @@ #define GL_GLEXT_LEGACY #include <GL/glew.h> #include <GL/glut.h> -#include <GL/glext.h> GLfloat verts[][4] = { { -0.5, -0.5, -2.0, 0.0 }, diff --git a/progs/tests/mipgen.c b/progs/tests/mipgen.c index 088f643215c..48e52c9c315 100644 --- a/progs/tests/mipgen.c +++ b/progs/tests/mipgen.c @@ -48,8 +48,6 @@ #include <GL/glew.h> #include <GL/glut.h> -#include "readtex.h" - static GLfloat LodBias = 6.0; /* make smallest miplevel visible */ static GLuint texImage; diff --git a/progs/tests/mipmap_comp.c b/progs/tests/mipmap_comp.c index dd2232113b6..122d157949a 100644 --- a/progs/tests/mipmap_comp.c +++ b/progs/tests/mipmap_comp.c @@ -48,8 +48,6 @@ #include <GL/glew.h> #include <GL/glut.h> -#include "readtex.h" - #define SIZE 16 /* not larger then 16 */ static GLint BaseLevel = 0, MaxLevel = 9; diff --git a/progs/tests/mipmap_comp_tests.c b/progs/tests/mipmap_comp_tests.c index e865b30ad0f..b93a5c61393 100644 --- a/progs/tests/mipmap_comp_tests.c +++ b/progs/tests/mipmap_comp_tests.c @@ -48,8 +48,6 @@ #include <GL/glew.h> #include <GL/glut.h> -#include "readtex.h" - #define SIZE 16 /* not larger then 16 */ static GLint BaseLevel = 0, MaxLevel ; diff --git a/progs/tests/mipmap_view.c b/progs/tests/mipmap_view.c index 808d348699c..eb521970524 100644 --- a/progs/tests/mipmap_view.c +++ b/progs/tests/mipmap_view.c @@ -12,7 +12,6 @@ #include <math.h> #include <GL/glew.h> #include <GL/glut.h> -#include <GL/glext.h> #include "readtex.h" diff --git a/progs/tests/no_s3tc.c b/progs/tests/no_s3tc.c index 31cfb40b9d3..c4132cd956a 100644 --- a/progs/tests/no_s3tc.c +++ b/progs/tests/no_s3tc.c @@ -40,7 +40,6 @@ #include <string.h> #include <GL/glew.h> #include <GL/glut.h> -#include <GL/glext.h> static unsigned data[16]; diff --git a/progs/tests/streaming_rect.c b/progs/tests/streaming_rect.c index f65ac4ce36c..3b016e55af9 100644 --- a/progs/tests/streaming_rect.c +++ b/progs/tests/streaming_rect.c @@ -13,8 +13,6 @@ #include <GL/glew.h> #include <GL/glut.h> -#include "readtex.h" - #define ANIMATE 10 #define PBO 11 diff --git a/progs/tests/zcomp.c b/progs/tests/zcomp.c index 15e35f17b09..d6b9c070223 100644 --- a/progs/tests/zcomp.c +++ b/progs/tests/zcomp.c @@ -7,7 +7,6 @@ #include <math.h> #include <GL/glew.h> #include <GL/glut.h> -#include "../util/showbuffer.c" static int Win; diff --git a/progs/trivial/Makefile b/progs/trivial/Makefile index d13f28de204..6c78ae90a96 100644 --- a/progs/trivial/Makefile +++ b/progs/trivial/Makefile @@ -162,6 +162,7 @@ SOURCES = \ vbo-drawelements.c \ vbo-drawrange.c \ vp-array.c \ + vp-array-hf.c \ vp-array-int.c \ vp-clip.c \ vp-line-clip.c \ diff --git a/progs/trivial/tri-fbo-tex-mip.c b/progs/trivial/tri-fbo-tex-mip.c index df4725c7b4e..2e8fb74a00b 100644 --- a/progs/trivial/tri-fbo-tex-mip.c +++ b/progs/trivial/tri-fbo-tex-mip.c @@ -6,7 +6,6 @@ #include <assert.h> #include <stdio.h> #include <stdlib.h> -#include <math.h> /* For debug */ diff --git a/progs/trivial/vp-array-hf.c b/progs/trivial/vp-array-hf.c new file mode 100644 index 00000000000..f812436437f --- /dev/null +++ b/progs/trivial/vp-array-hf.c @@ -0,0 +1,215 @@ +/* Test glGenProgramsNV(), glIsProgramNV(), glLoadProgramNV() */ + +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <GL/glew.h> +#include <GL/glut.h> + +typedef union { GLfloat f; GLint i; } fi_type; +/** + * Convert a 4-byte float to a 2-byte half float. + * Based on code from: + * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html + */ +static GLhalf +_mesa_float_to_half(GLfloat val) +{ + + const fi_type fi = {val}; + const int flt_m = fi.i & 0x7fffff; + const int flt_e = (fi.i >> 23) & 0xff; + const int flt_s = (fi.i >> 31) & 0x1; + int s, e, m = 0; + GLhalf result; + + /* sign bit */ + s = flt_s; + + /* handle special cases */ + if ((flt_e == 0) && (flt_m == 0)) { + /* zero */ + /* m = 0; - already set */ + e = 0; + } + else if ((flt_e == 0) && (flt_m != 0)) { + /* denorm -- denorm float maps to 0 half */ + /* m = 0; - already set */ + e = 0; + } + else if ((flt_e == 0xff) && (flt_m == 0)) { + /* infinity */ + /* m = 0; - already set */ + e = 31; + } + else if ((flt_e == 0xff) && (flt_m != 0)) { + /* NaN */ + m = 1; + e = 31; + } + else { + /* regular number */ + const int new_exp = flt_e - 127; + if (new_exp < -24) { + /* this maps to 0 */ + /* m = 0; - already set */ + e = 0; + } + else if (new_exp < -14) { + /* this maps to a denorm */ + unsigned int exp_val = (unsigned int) (-14 - new_exp); /* 2^-exp_val*/ + e = 0; + switch (exp_val) { + case 0: + /* m = 0; - already set */ + break; + case 1: m = 512 + (flt_m >> 14); break; + case 2: m = 256 + (flt_m >> 15); break; + case 3: m = 128 + (flt_m >> 16); break; + case 4: m = 64 + (flt_m >> 17); break; + case 5: m = 32 + (flt_m >> 18); break; + case 6: m = 16 + (flt_m >> 19); break; + case 7: m = 8 + (flt_m >> 20); break; + case 8: m = 4 + (flt_m >> 21); break; + case 9: m = 2 + (flt_m >> 22); break; + case 10: m = 1; break; + } + } + else if (new_exp > 15) { + /* map this value to infinity */ + /* m = 0; - already set */ + e = 31; + } + else { + /* regular */ + e = new_exp + 15; + m = flt_m >> 13; + } + } + + result = (s << 15) | (e << 10) | m; + return result; +} + + +GLfloat verts[][4] = { + { 0.9, -0.9, 0.0, 1.0 }, + { 0.9, 0.9, 0.0, 1.0 }, + { -0.9, 0.9, 0.0, 1.0 }, + { -0.9, -0.9, 0.0, 1.0 }, +}; + +GLhalf hverts[16]; + +GLubyte color[][4] = { + { 0x00, 0x00, 0xff, 0x00 }, + { 0x00, 0xff, 0x00, 0x00 }, + { 0xff, 0x00, 0x00, 0x00 }, + { 0xff, 0xff, 0xff, 0x00 }, +}; + +GLuint indices[] = { 0, 1, 2, 3 }; + +static void Init( void ) +{ + GLint errno; + GLuint prognum; + GLuint i, j; + + static const char *prog1 = + "!!ARBvp1.0\n" + "MOV result.color, vertex.color;\n" + "MOV result.position, vertex.position;\n" + "END\n"; + + if (!glutExtensionSupported("GL_ARB_half_float_vertex")) { + printf("GL_ARB_half_float_vertex not found!\n"); + exit(0); + } + + glGenProgramsARB(1, &prognum); + glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum); + glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, + strlen(prog1), (const GLubyte *) prog1); + + assert(glIsProgramARB(prognum)); + errno = glGetError(); + printf("glGetError = %d\n", errno); + if (errno != GL_NO_ERROR) + { + GLint errorpos; + + glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos); + printf("errorpos: %d\n", errorpos); + printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)); + } + + for (i = 0; i < 4; i++) + for (j = 0; j < 4; j++) + hverts[i * 4 + j] = _mesa_float_to_half(verts[i][j]); + + glEnableClientState( GL_VERTEX_ARRAY ); + glEnableClientState( GL_COLOR_ARRAY ); + glVertexPointer( 4, GL_HALF_FLOAT, 8, hverts ); + glColorPointer( 4, GL_UNSIGNED_BYTE, 0, color ); + +} + + + +static void Display( void ) +{ + glClearColor(0.3, 0.3, 0.3, 1); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + + glEnable(GL_VERTEX_PROGRAM_NV); + glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices ); + + glFlush(); +} + + +static void Reshape( int width, int height ) +{ + glViewport( 0, 0, width, height ); + glMatrixMode( GL_PROJECTION ); + glLoadIdentity(); + glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + /*glTranslatef( 0.0, 0.0, -15.0 );*/ +} + + +static void Key( unsigned char key, int x, int y ) +{ + (void) x; + (void) y; + switch (key) { + case 27: + exit(0); + break; + } + glutPostRedisplay(); +} + + + + +int main( int argc, char *argv[] ) +{ + glutInit( &argc, argv ); + glutInitWindowPosition( 0, 0 ); + glutInitWindowSize( 250, 250 ); + glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH ); + glutCreateWindow(argv[0]); + glewInit(); + glutReshapeFunc( Reshape ); + glutKeyboardFunc( Key ); + glutDisplayFunc( Display ); + Init(); + glutMainLoop(); + return 0; +} diff --git a/progs/xdemos/glxgears.c b/progs/xdemos/glxgears.c index 088f25a357a..92c75caa5e6 100644 --- a/progs/xdemos/glxgears.c +++ b/progs/xdemos/glxgears.c @@ -35,6 +35,7 @@ #include <X11/keysym.h> #include <GL/gl.h> #include <GL/glx.h> +#include <GL/glxext.h> #ifndef GLX_MESA_swap_control #define GLX_MESA_swap_control 1 @@ -586,11 +587,17 @@ is_glx_extension_supported(Display *dpy, const char *query) * Attempt to determine whether or not the display is synched to vblank. */ static void -query_vsync(Display *dpy) +query_vsync(Display *dpy, GLXDrawable drawable) { int interval = 0; - +#if defined(GLX_EXT_swap_control) + if (is_glx_extension_supported(dpy, "GLX_EXT_swap_control")) { + unsigned int tmp = -1; + glXQueryDrawable(dpy, drawable, GLX_SWAP_INTERVAL_EXT, &tmp); + interval = tmp; + } else +#endif if (is_glx_extension_supported(dpy, "GLX_MESA_swap_control")) { PFNGLXGETSWAPINTERVALMESAPROC pglXGetSwapIntervalMESA = (PFNGLXGETSWAPINTERVALMESAPROC) @@ -749,7 +756,7 @@ main(int argc, char *argv[]) make_window(dpy, "glxgears", x, y, winWidth, winHeight, &win, &ctx); XMapWindow(dpy, win); glXMakeCurrent(dpy, win, ctx); - query_vsync(dpy); + query_vsync(dpy, win); if (printInfo) { printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); @@ -771,6 +778,7 @@ main(int argc, char *argv[]) glDeleteLists(gear1, 1); glDeleteLists(gear2, 1); glDeleteLists(gear3, 1); + glXMakeCurrent(dpy, None, NULL); glXDestroyContext(dpy, ctx); XDestroyWindow(dpy, win); XCloseDisplay(dpy); diff --git a/progs/xdemos/offset.c b/progs/xdemos/offset.c index 314a4fcdd17..3858a5b8020 100644 --- a/progs/xdemos/offset.c +++ b/progs/xdemos/offset.c @@ -94,6 +94,9 @@ static int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, static int dimension = 3; +static float Scale = 1.0; + + int main(int argc, char** argv) { Display *dpy; XVisualInfo *vi; @@ -182,6 +185,7 @@ draw_scene(int mx, int my) { #else glEnable(GL_POLYGON_OFFSET_FILL); #endif + glScalef(Scale, Scale, Scale); cubes(mx, my, HIDDEN_LINE); #ifdef GL_EXT_polygon_offset glDisable(GL_POLYGON_OFFSET_EXT); @@ -289,6 +293,12 @@ process_input(Display *dpy, Window win) { case KeyPress: (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL); switch (keysym) { + case 'Z': + Scale *= 1.1; + break; + case 'z': + Scale *= 0.9; + break; case XK_Escape: exit(EXIT_SUCCESS); default: |