From 21666e3db410d3ab9c7e4b18a3c29ae78a04fa95 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Sat, 5 Oct 2002 18:30:13 +0000 Subject: pbuffer info and demo programs --- progs/xdemos/pbdemo.c | 477 ++++++++++++++++++++++++++++++++++++++++++++++++++ progs/xdemos/pbinfo.c | 133 ++++++++++++++ progs/xdemos/pbutil.c | 230 ++++++++++++++++++++++++ progs/xdemos/pbutil.h | 38 ++++ 4 files changed, 878 insertions(+) create mode 100644 progs/xdemos/pbdemo.c create mode 100644 progs/xdemos/pbinfo.c create mode 100644 progs/xdemos/pbutil.c create mode 100644 progs/xdemos/pbutil.h (limited to 'progs') diff --git a/progs/xdemos/pbdemo.c b/progs/xdemos/pbdemo.c new file mode 100644 index 0000000000..611e7e594f --- /dev/null +++ b/progs/xdemos/pbdemo.c @@ -0,0 +1,477 @@ +/* $Id: pbdemo.c,v 1.1 2002/10/05 18:30:13 brianp Exp $ */ + +/* + * This program demonstrates how to do "off-screen" rendering using + * the GLX pixel buffer extension. + * + * Written by Brian Paul for the "OpenGL and Window System Integration" + * course presented at SIGGRAPH '97. Updated on 5 October 2002. + * + * Usage: + * pbuffers width height imgfile + * Where: + * width is the width, in pixels, of the image to generate. + * height is the height, in pixels, of the image to generate. + * imgfile is the name of the PPM image file to write. + * + * + * This demo draws 3-D boxes with random orientation. A pbuffer with + * a depth (Z) buffer is prefered but if such a pbuffer can't be created + * we use a non-depth-buffered config. + * + * On machines such as the SGI Indigo you may have to reconfigure your + * display/X server to enable pbuffers. Look in the /usr/gfx/ucode/MGRAS/vof/ + * directory for display configurationswith the _pbuf suffix. Use + * setmon -x to configure your X server and display for pbuffers. + * + * O2 systems seem to support pbuffers well. + * + * IR systems (at least 1RM systems) don't have single-buffered, RGBA, + * Z-buffered pbuffer configs. BUT, they DO have DOUBLE-buffered, RGBA, + * Z-buffered pbuffers. Note how we try four different fbconfig attribute + * lists below! + */ + + +#include +#include +#include +#include +#include +#include "pbutil.h" + + +/* Some ugly global vars */ +static GLXFBConfigSGIX gFBconfig = 0; +static Display *gDpy = NULL; +static int gScreen = 0; +static GLXPbufferSGIX gPBuffer = 0; +static int gWidth, gHeight; + + + +/* + * Create the pbuffer and return a GLXPbufferSGIX handle. + */ +static GLXPbufferSGIX +MakePbuffer( Display *dpy, int screen, int width, int height ) +{ +#define NUM_FB_CONFIGS 4 + char fbString[NUM_FB_CONFIGS][100] = { + "Single Buffered, depth buffer", + "Double Buffered, depth buffer", + "Single Buffered, no depth buffer", + "Double Buffered, no depth buffer" + }; + int fbAttribs[NUM_FB_CONFIGS][100] = { + { + /* Single buffered, with depth buffer */ + GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX, + GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX, + GLX_RED_SIZE, 1, + GLX_GREEN_SIZE, 1, + GLX_BLUE_SIZE, 1, + GLX_DEPTH_SIZE, 1, + GLX_DOUBLEBUFFER, 0, + GLX_STENCIL_SIZE, 0, + None + }, + { + /* Double buffered, with depth buffer */ + GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX, + GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX, + GLX_RED_SIZE, 1, + GLX_GREEN_SIZE, 1, + GLX_BLUE_SIZE, 1, + GLX_DEPTH_SIZE, 1, + GLX_DOUBLEBUFFER, 1, + GLX_STENCIL_SIZE, 0, + None + }, + { + /* Single bufferd, without depth buffer */ + GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX, + GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX, + GLX_RED_SIZE, 1, + GLX_GREEN_SIZE, 1, + GLX_BLUE_SIZE, 1, + GLX_DEPTH_SIZE, 0, + GLX_DOUBLEBUFFER, 0, + GLX_STENCIL_SIZE, 0, + None + }, + { + /* Double bufferd, without depth buffer */ + GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX, + GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX, + GLX_RED_SIZE, 1, + GLX_GREEN_SIZE, 1, + GLX_BLUE_SIZE, 1, + GLX_DEPTH_SIZE, 0, + GLX_DOUBLEBUFFER, 1, + GLX_STENCIL_SIZE, 0, + None + } + }; + int pbAttribs[] = { + GLX_LARGEST_PBUFFER_SGIX, True, + GLX_PRESERVED_CONTENTS_SGIX, False, + None + }; + GLXFBConfigSGIX *fbConfigs; + GLXPbufferSGIX pBuffer = None; + int nConfigs; + int i; + int attempt; + + for (attempt=0; attempt x1) { + tmp = x0; + x0 = x1; + x1 = tmp; + } + if (y0 > y1) { + tmp = y0; + y0 = y1; + y1 = tmp; + } + if (z0 > z1) { + tmp = z0; + z0 = z1; + z1 = tmp; + } + v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0; + v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1; + v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0; + v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1; + v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0; + v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1; + + for (i = 0; i < 6; i++) { + glBegin(type); + glNormal3fv(&n[i][0]); + glVertex3fv(&v[faces[i][0]][0]); + glVertex3fv(&v[faces[i][1]][0]); + glVertex3fv(&v[faces[i][2]][0]); + glVertex3fv(&v[faces[i][3]][0]); + glEnd(); + } +} + + + +/* Render a scene */ +static void +Render(void) +{ + int NumBoxes = 100; + int i; + + InitGL(); + glClearColor(0.2, 0.2, 0.9, 0.0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + for (i=0;i +#include +#include +#include "pbutil.h" + + + + +static void +PrintConfigs(Display *dpy, int screen, Bool horizFormat) +{ + GLXFBConfigSGIX *fbConfigs; + int nConfigs; + int i; + /* Note: you may want to tweek the attribute list to select a different + * set of fbconfigs. + */ + int fbAttribs[] = { + GLX_RENDER_TYPE_SGIX, 0, + GLX_DRAWABLE_TYPE_SGIX, 0, +#if 0 + GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX, + GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX, + GLX_RED_SIZE, 1, + GLX_GREEN_SIZE, 1, + GLX_BLUE_SIZE, 1, + GLX_DEPTH_SIZE, 1, + GLX_DOUBLEBUFFER, 0, + GLX_STENCIL_SIZE, 0, +#endif + None}; + + + /* Get list of possible frame buffer configurations */ +#if 0 + /* SGIX method */ + fbConfigs = glXChooseFBConfigSGIX(dpy, screen, fbAttribs, &nConfigs); +#else + /* GLX 1.3 method */ + fbConfigs = glXGetFBConfigs(dpy, screen, &nConfigs); +#endif + + if (nConfigs==0 || !fbConfigs) { + printf("Error: glxChooseFBConfigSGIX failed\n"); + return; + } + + printf("Number of fbconfigs: %d\n", nConfigs); + + if (horizFormat) { + printf(" ID VisualType Depth Lvl RGB CI DB Stereo R G B A"); + printf(" Z S AR AG AB AA MSbufs MSnum Pbuffer\n"); + } + + /* Print config info */ + for (i=0;i specify X display name\n"); + printf(" -t print in tabular format\n"); + printf(" -v print in verbose format\n"); + printf(" -help print this information\n"); +} + + +int +main(int argc, char *argv[]) +{ + Display *dpy; + int scrn; + char *dpyName = NULL; + Bool horizFormat = True; + int i; + + for (i=1; i +#include +#include "pbutil.h" + + + +/* + * Test if we pixel buffers are available for a particular X screen. + * Input: dpy - the X display + * screen - screen number + * Return: 0 = pixel buffers not available. + * 1 = pixel buffers are available. + */ +int +QueryPbuffers(Display *dpy, int screen) +{ +#if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) + char *extensions; + + extensions = (char *) glXQueryServerString(dpy, screen, GLX_EXTENSIONS); + if (!strstr(extensions,"GLX_SGIX_fbconfig")) { + return 0; + } + if (!strstr(extensions,"GLX_SGIX_pbuffer")) { + return 0; + } + return 1; +#else + return 0; +#endif +} + + + +#ifdef GLX_SGIX_fbconfig + + +/* + * Print parameters for a GLXFBConfig to stdout. + * Input: dpy - the X display + * fbConfig - the fbconfig handle + * horizFormat - if true, print in horizontal format + */ +void +PrintFBConfigInfo(Display *dpy, GLXFBConfigSGIX fbConfig, Bool horizFormat) +{ + int pbAttribs[] = {GLX_LARGEST_PBUFFER_SGIX, True, + GLX_PRESERVED_CONTENTS_SGIX, False, + None}; + GLXPbufferSGIX pBuffer; + int width=2, height=2; + int bufferSize, level, doubleBuffer, stereo, auxBuffers; + int redSize, greenSize, blueSize, alphaSize; + int depthSize, stencilSize; + int accumRedSize, accumBlueSize, accumGreenSize, accumAlphaSize; + int sampleBuffers, samples; + int drawableType, renderType, xRenderable, xVisual, id; + int maxWidth, maxHeight, maxPixels; + int optWidth, optHeight; + + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_BUFFER_SIZE, &bufferSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_LEVEL, &level); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_DOUBLEBUFFER, &doubleBuffer); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_STEREO, &stereo); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_AUX_BUFFERS, &auxBuffers); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_RED_SIZE, &redSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_GREEN_SIZE, &greenSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_BLUE_SIZE, &blueSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_ALPHA_SIZE, &alphaSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_DEPTH_SIZE, &depthSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_STENCIL_SIZE, &stencilSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_ACCUM_RED_SIZE, &accumRedSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_ACCUM_GREEN_SIZE, &accumGreenSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_ACCUM_BLUE_SIZE, &accumBlueSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_ACCUM_ALPHA_SIZE, &accumAlphaSize); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_SAMPLE_BUFFERS_SGIS, &sampleBuffers); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_SAMPLES_SGIS, &samples); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_DRAWABLE_TYPE_SGIX, &drawableType); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_RENDER_TYPE_SGIX, &renderType); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_X_RENDERABLE_SGIX, &xRenderable); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_X_VISUAL_TYPE_EXT, &xVisual); + if (!xRenderable || !(drawableType & GLX_WINDOW_BIT_SGIX)) + xVisual = -1; + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_FBCONFIG_ID_SGIX, &id); + + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_MAX_PBUFFER_WIDTH_SGIX, + &maxWidth); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_MAX_PBUFFER_HEIGHT_SGIX, + &maxHeight); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_MAX_PBUFFER_PIXELS_SGIX, + &maxPixels); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_OPTIMAL_PBUFFER_WIDTH_SGIX, + &optWidth); + glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX, + &optHeight); + + pBuffer = CreatePbuffer(dpy, fbConfig, width, height, pbAttribs); + + if (horizFormat) { + printf("0x%03x ", id); + if (xVisual==GLX_STATIC_GRAY) printf("StaticGray "); + else if (xVisual==GLX_GRAY_SCALE) printf("GrayScale "); + else if (xVisual==GLX_STATIC_COLOR) printf("StaticColor "); + else if (xVisual==GLX_PSEUDO_COLOR) printf("PseudoColor "); + else if (xVisual==GLX_TRUE_COLOR) printf("TrueColor "); + else if (xVisual==GLX_DIRECT_COLOR) printf("DirectColor "); + else printf(" -none- "); + printf(" %3d %3d %s %s %s %2s ", bufferSize, level, + (renderType & GLX_RGBA_BIT_SGIX) ? "y" : "n", + (renderType & GLX_COLOR_INDEX_BIT_SGIX) ? "y" : "n", + doubleBuffer ? "y" : "n", + stereo ? "y" : "n"); + printf("%2d %2d %2d %2d ", redSize, greenSize, blueSize, alphaSize); + printf("%2d %2d ", depthSize, stencilSize); + printf("%2d %2d %2d %2d", accumRedSize, accumGreenSize, accumBlueSize, + accumAlphaSize); + printf(" %2d %2d", sampleBuffers, samples); + printf(" %s", pBuffer ? "y" : "n"); + printf("\n"); + } + else { + printf("Id 0x%x\n", id); + printf(" Buffer Size: %d\n", bufferSize); + printf(" Level: %d\n", level); + printf(" Double Buffer: %s\n", doubleBuffer ? "yes" : "no"); + printf(" Stereo: %s\n", stereo ? "yes" : "no"); + printf(" Aux Buffers: %d\n", auxBuffers); + printf(" Red Size: %d\n", redSize); + printf(" Green Size: %d\n", greenSize); + printf(" Blue Size: %d\n", blueSize); + printf(" Alpha Size: %d\n", alphaSize); + printf(" Depth Size: %d\n", depthSize); + printf(" Stencil Size: %d\n", stencilSize); + printf(" Accum Red Size: %d\n", accumRedSize); + printf(" Accum Green Size: %d\n", accumGreenSize); + printf(" Accum Blue Size: %d\n", accumBlueSize); + printf(" Accum Alpha Size: %d\n", accumAlphaSize); + printf(" Sample Buffers: %d\n", sampleBuffers); + printf(" Samples/Pixel: %d\n", samples); + printf(" Drawable Types: "); + if (drawableType & GLX_WINDOW_BIT_SGIX) printf("Window "); + if (drawableType & GLX_PIXMAP_BIT_SGIX) printf("Pixmap "); + if (drawableType & GLX_PBUFFER_BIT_SGIX) printf("PBuffer"); + printf("\n"); + printf(" Render Types: "); + if (renderType & GLX_RGBA_BIT_SGIX) printf("RGBA "); + if (renderType & GLX_COLOR_INDEX_BIT_SGIX) printf("CI "); + printf("\n"); + printf(" X Renderable: %s\n", xRenderable ? "yes" : "no"); + /* + printf(" Max width: %d\n", maxWidth); + printf(" Max height: %d\n", maxHeight); + printf(" Max pixels: %d\n", maxPixels); + printf(" Optimum width: %d\n", optWidth); + printf(" Optimum height: %d\n", optHeight); + */ + printf(" Pbuffer: %s\n", pBuffer ? "yes" : "no"); + } + + if (pBuffer) { + glXDestroyGLXPbufferSGIX(dpy, pBuffer); + } +} + + + +/* This is only used by CreatePbuffer() */ +static int XErrorFlag = 0; +static int HandleXError( Display *dpy, XErrorEvent *event ) +{ + XErrorFlag = 1; + return 0; +} + + +/* + * Create a pixel buffer. We loop over the list of fbconfigs trying to create + * a pixel buffer. We return the first pixel buffer which we successfully + * create. This function hides the ugliness of dealing with BadAlloc X + * protocol errors. + * + * Input: dpy - the X display + * fbConfig - an FBConfig as returned by glXChooseFBConfigSGIX(). + * width, height - size of pixel buffer to request, in pixels. + * pbAttribs - list of pixel buffer attributes as used by + * glXCreateGLXPbufferSGIX(). + * Return: a pixel buffer or None. + */ +GLXPbufferSGIX +CreatePbuffer( Display *dpy, GLXFBConfigSGIX fbConfig, + int width, int height, int *pbAttribs ) +{ + int (*oldHandler)( Display *, XErrorEvent * ); + GLXPbufferSGIX pBuffer = None; + + /* Catch X protocol errors with our own error handler */ + oldHandler = XSetErrorHandler( HandleXError ); + + XErrorFlag = 0; + pBuffer = glXCreateGLXPbufferSGIX(dpy, fbConfig, width, height, pbAttribs); + + /* Restore original X error handler */ + (void) XSetErrorHandler( oldHandler ); + + /* Return pbuffer (may be None) */ + if (!XErrorFlag && pBuffer!=None) { + /*printf("config %d worked!\n", i);*/ + return pBuffer; + } + else { + return None; + } +} + + + +#endif /*GLX_SGIX_fbconfig*/ + + diff --git a/progs/xdemos/pbutil.h b/progs/xdemos/pbutil.h new file mode 100644 index 0000000000..9230b47c31 --- /dev/null +++ b/progs/xdemos/pbutil.h @@ -0,0 +1,38 @@ +/* $Id: pbutil.h,v 1.1 2002/10/05 18:30:13 brianp Exp $ */ + +/* + * OpenGL pbuffers utility functions. + * + * Brian Paul + * April 1997 + */ + + +#ifndef PBUTIL_H +#define PBUTIL_H + + +#define GLX_GLXEXT_PROTOTYPES +#include + + +extern int +QueryPbuffers(Display *dpy, int screen); + + +#ifdef GLX_SGIX_fbconfig + + +extern void +PrintFBConfigInfo(Display *dpy, GLXFBConfigSGIX fbConfig, Bool horizFormat); + + +extern GLXPbufferSGIX +CreatePbuffer( Display *dpy, GLXFBConfigSGIX fbConfig, + int width, int height, int *pbAttribs ); + + +#endif /*GLX_SGIX_fbconfig*/ + + +#endif /*PBUTIL_H*/ -- cgit v1.2.3