diff options
author | Chia-I Wu <olv@lunarg.com> | 2010-03-29 13:58:58 +0800 |
---|---|---|
committer | Chia-I Wu <olv@lunarg.com> | 2010-03-29 14:09:25 +0800 |
commit | c7df1d481a31d8c2f4d297228c283c5c412c7080 (patch) | |
tree | 8a0ccc781f8a074996c747b3063e27a33a8dd390 | |
parent | 884d41de8aabefc18e48ef4b28aba996b37991fd (diff) |
progs/gallium: Add libG3D demos.gallium-g3d
-rw-r--r-- | progs/gallium/g3d/Makefile | 37 | ||||
-rw-r--r-- | progs/gallium/g3d/clear.c | 65 | ||||
-rw-r--r-- | progs/gallium/g3d/eglut.c | 293 | ||||
-rw-r--r-- | progs/gallium/g3d/eglut.h | 31 |
4 files changed, 426 insertions, 0 deletions
diff --git a/progs/gallium/g3d/Makefile b/progs/gallium/g3d/Makefile new file mode 100644 index 0000000000..77b226b85a --- /dev/null +++ b/progs/gallium/g3d/Makefile @@ -0,0 +1,37 @@ +# progs/gallium/raw/Makefile + +TOP = ../../.. +include $(TOP)/configs/current + +INCLUDES = \ + -I. \ + -I$(TOP)/include \ + -I$(TOP)/src/gallium/include \ + -I$(TOP)/src/gallium/auxiliary + +SOURCES = \ + clear.c + +OBJECTS = $(SOURCES:.c=.o) + +EGLUT_OBJECTS = eglut.o +EGLUT_LIBS = -lEGL -lX11 -L$(TOP)/$(LIB_DIR)/gallium -Wl,-rpath,$(TOP)/$(LIB_DIR)/gallium -lG3D + +PROGS = $(OBJECTS:.o=) + +##### TARGETS ##### + +default: $(PROGS) + +clean: + -rm -f $(PROGS) + -rm -f *.o + -rm -f result.bmp + +##### RULES ##### + +$(OBJECTS) $(EGLUT_OBJECTS): %.o: %.c + $(CC) -c $(INCLUDES) $(CFLAGS) $(DEFINES) $(PROG_DEFINES) $< -o $@ + +$(PROGS): %: %.o $(EGLUT_OBJECTS) + $(CC) $(LDFLAGS) $< $(EGLUT_OBJECTS) $(LINKS) $(EGLUT_LIBS) -lm -o $@ diff --git a/progs/gallium/g3d/clear.c b/progs/gallium/g3d/clear.c new file mode 100644 index 0000000000..62cee1293f --- /dev/null +++ b/progs/gallium/g3d/clear.c @@ -0,0 +1,65 @@ +/* Display a cleared blue window. This demo has no dependencies on + * any utility code, just the graw interface and gallium. + */ + +#include "pipe/p_screen.h" +#include "pipe/p_context.h" +#include "pipe/p_state.h" +#include "pipe/p_defines.h" + +#include "eglut.h" + +static const int WIDTH = 300; +static const int HEIGHT = 300; + +static void +init(void) +{ +} + +/* new window size or exposure */ +static void +reshape(int w, int h) +{ + struct pipe_context *pipe; + struct pipe_texture *tex; + struct pipe_surface *surf; + struct pipe_framebuffer_state fb; + + pipe = get_pipe_context(); + tex = get_display_texture(pipe); + + surf = pipe->screen->get_tex_surface(pipe->screen, tex, 0, 0, 0, + PIPE_TEXTURE_USAGE_RENDER_TARGET | + PIPE_TEXTURE_USAGE_DISPLAY_TARGET); + if (surf == NULL) + exit(5); + + memset(&fb, 0, sizeof fb); + fb.nr_cbufs = 1; + fb.width = w; + fb.height = h; + fb.cbufs[0] = surf; + + pipe->set_framebuffer_state(pipe, &fb); + + put_display_texture(pipe, tex); +} + +static void +draw(void) +{ + struct pipe_context *pipe; + float clear_color[4] = {1,0,1,1}; + + pipe = get_pipe_context(); + + pipe->clear(pipe, PIPE_CLEAR_COLOR, clear_color, 0, 0); +} + +int main( int argc, char *argv[] ) +{ + set_window_size(WIDTH, HEIGHT); + return run(argc, argv, init, reshape, + draw, 0); +} diff --git a/progs/gallium/g3d/eglut.c b/progs/gallium/g3d/eglut.c new file mode 100644 index 0000000000..5f0c71080b --- /dev/null +++ b/progs/gallium/g3d/eglut.c @@ -0,0 +1,293 @@ +#include "eglut.h" + +#include "state_tracker/g3d.h" +#include "util/u_inlines.h" /* for pipe_texture_reference */ + +#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 <EGL/egl.h> +#include <EGL/eglext.h> + + +static init_func init = 0; +static draw_func draw = 0; +static reshape_func reshape = 0; +static key_func keyPress = 0; +static EGLint width = 300, height = 300; + + +void set_window_size(int w, int h) +{ + width = w; + height = h; +} + +/* + * 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_RENDERABLE_TYPE, EGL_GALLIUM_BIT_MESA, + 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) || + !num_configs) { + printf("Error: couldn't get an EGL visual config\n"); + exit(1); + } + + assert(config); + + 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_GALLIUM_API_MESA); + + 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: + if (reshape) { + width = event.xconfigure.width; + height = event.xconfigure.height; + reshape(event.xconfigure.width, event.xconfigure.height); + } + break; + case KeyPress: + { + char buffer[10]; + int r, code; + code = XLookupKeysym(&event.xkey, 0); + if (!keyPress || !keyPress(code)) { + 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); + } + } +} + +int window_width(void) +{ + return width; +} + +int window_height(void) +{ + return height; +} + +int run(int argc, char **argv, + init_func init_f, + reshape_func resh_f, + draw_func draw_f, + key_func key_f) +{ + const int winWidth = width, winHeight = height; + Display *x_dpy; + Window win; + EGLSurface egl_surf; + EGLContext egl_ctx; + EGLDisplay egl_dpy; + char *dpyName = NULL; + EGLint egl_major, egl_minor; + int i; + const char *s; + + init = init_f; + draw = draw_f; + reshape = resh_f; + keyPress = key_f; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-display") == 0) { + dpyName = argv[i+1]; + i++; + } + } + + 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); + + make_x_window(x_dpy, egl_dpy, + "OpenVG Example", 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 (init) + init(); + + /* Set initial projection/viewing transformation. + * We can't be sure we'll get a ConfigureNotify event when the window + * first appears. + */ + if (reshape) + reshape(winWidth, winHeight); + + event_loop(x_dpy, win, egl_dpy, egl_surf); + + eglMakeCurrent(egl_dpy, 0, 0, 0); + eglDestroyContext(egl_dpy, egl_ctx); + eglDestroySurface(egl_dpy, egl_surf); + eglTerminate(egl_dpy); + + + XDestroyWindow(x_dpy, win); + XCloseDisplay(x_dpy); + + return 0; +} + +struct pipe_context * +get_pipe_context(void) +{ + EGLDisplay dpy = eglGetCurrentDisplay(); + EGLContext ctx = eglGetCurrentContext(); + + return g3dGetPipeContext((void *) dpy, (void *) ctx); +} + +struct pipe_texture * +get_display_texture(struct pipe_context *ctx) +{ + return g3dValidatePipeTexture(ctx); +} + +void +put_display_texture(struct pipe_context *ctx, struct pipe_texture *ptex) +{ + pipe_texture_reference(&ptex, NULL); +} diff --git a/progs/gallium/g3d/eglut.h b/progs/gallium/g3d/eglut.h new file mode 100644 index 0000000000..b1039876ab --- /dev/null +++ b/progs/gallium/g3d/eglut.h @@ -0,0 +1,31 @@ +#ifndef EGLCOMMON_H +#define EGLCOMMON_H + +typedef void (*init_func)(); +typedef void (*reshape_func)(int, int); +typedef void (*draw_func)(); +typedef int (*key_func)(unsigned key); + +struct pipe_context; +struct pipe_texture; + +void set_window_size(int width, int height); +int window_width(void); +int window_height(void); + +int run(int argc, char **argv, + init_func init, + reshape_func resh, + draw_func draw, + key_func key); + +struct pipe_context * +get_pipe_context(void); + +struct pipe_texture * +get_display_texture(struct pipe_context *ctx); + +void +put_display_texture(struct pipe_context *ctx, struct pipe_texture *ptex); + +#endif |