132001f49Smrg/* 232001f49Smrg * Test sharing of texture objects by two rendering contexts. 332001f49Smrg * In particular, test that changing a texture object in one context 432001f49Smrg * effects the texture in the second context. 532001f49Smrg * 632001f49Smrg * Brian Paul 732001f49Smrg * 30 Apr 2008 832001f49Smrg * 932001f49Smrg * Copyright (C) 2008 Brian Paul All Rights Reserved. 1032001f49Smrg * 1132001f49Smrg * Permission is hereby granted, free of charge, to any person obtaining a 1232001f49Smrg * copy of this software and associated documentation files (the "Software"), 1332001f49Smrg * to deal in the Software without restriction, including without limitation 1432001f49Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 1532001f49Smrg * and/or sell copies of the Software, and to permit persons to whom the 1632001f49Smrg * Software is furnished to do so, subject to the following conditions: 1732001f49Smrg * 1832001f49Smrg * The above copyright notice and this permission notice shall be included 1932001f49Smrg * in all copies or substantial portions of the Software. 2032001f49Smrg * 2132001f49Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 2232001f49Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2332001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2432001f49Smrg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2532001f49Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2632001f49Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2732001f49Smrg */ 2832001f49Smrg 2932001f49Smrg 3032001f49Smrg#include <GL/gl.h> 3132001f49Smrg#include <GL/glx.h> 3232001f49Smrg#include <assert.h> 3332001f49Smrg#include <stdio.h> 3432001f49Smrg#include <stdlib.h> 3532001f49Smrg#include <string.h> 3632001f49Smrg#include <unistd.h> 3732001f49Smrg#include <X11/keysym.h> 3832001f49Smrg 3932001f49Smrg 4032001f49Smrg#define MAX_CONTEXTS 2 4132001f49Smrg 4232001f49Smrg#define TEX_SIZE 32 4332001f49Smrg 4432001f49Smrgstatic const char *DisplayName = NULL; 4532001f49Smrgstatic Display *Dpy; 4632001f49Smrgstatic XVisualInfo *VisInfo; 4732001f49Smrgstatic Window Win; 4832001f49Smrgstatic GLXContext Contexts[MAX_CONTEXTS]; 4932001f49Smrgstatic int WinWidth = 300, WinHeight = 300; 5032001f49Smrg 5132001f49Smrgstatic int DrawContext = 0, TexContext = 1; 5232001f49Smrg 5332001f49Smrgstatic GLuint TexObj = 0; 5432001f49Smrgstatic GLboolean NewTexture = GL_FALSE; 5532001f49Smrg 5632001f49Smrg 5732001f49Smrgstatic void 5832001f49SmrgError(const char *msg) 5932001f49Smrg{ 6032001f49Smrg fprintf(stderr, "sharedtex error: %s\n", msg); 6132001f49Smrg exit(1); 6232001f49Smrg} 6332001f49Smrg 6432001f49Smrg 6532001f49Smrgstatic void 6632001f49SmrgCreateWindow(const char *name) 6732001f49Smrg{ 6832001f49Smrg int attrib[] = { GLX_RGBA, 6932001f49Smrg GLX_RED_SIZE, 1, 7032001f49Smrg GLX_GREEN_SIZE, 1, 7132001f49Smrg GLX_BLUE_SIZE, 1, 7232001f49Smrg GLX_DOUBLEBUFFER, 7332001f49Smrg None }; 7432001f49Smrg int scrnum; 7532001f49Smrg XSetWindowAttributes attr; 7632001f49Smrg unsigned long mask; 7732001f49Smrg Window root; 7832001f49Smrg int xpos = 0, ypos = 0; 7932001f49Smrg static int n = 0; 8032001f49Smrg 8132001f49Smrg scrnum = DefaultScreen(Dpy); 8232001f49Smrg root = RootWindow(Dpy, scrnum); 8332001f49Smrg 8432001f49Smrg VisInfo = glXChooseVisual(Dpy, scrnum, attrib); 8532001f49Smrg if (!VisInfo) { 8632001f49Smrg Error("Unable to find RGB, double-buffered visual"); 8732001f49Smrg } 8832001f49Smrg 8932001f49Smrg /* window attributes */ 9032001f49Smrg xpos = (n % 10) * 100; 9132001f49Smrg ypos = (n / 10) * 100; 9232001f49Smrg n++; 9332001f49Smrg 9432001f49Smrg attr.background_pixel = 0; 9532001f49Smrg attr.border_pixel = 0; 9632001f49Smrg attr.colormap = XCreateColormap(Dpy, root, VisInfo->visual, AllocNone); 9732001f49Smrg attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; 9832001f49Smrg mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; 9932001f49Smrg 10032001f49Smrg Win = XCreateWindow(Dpy, root, xpos, ypos, WinWidth, WinHeight, 10132001f49Smrg 0, VisInfo->depth, InputOutput, 10232001f49Smrg VisInfo->visual, mask, &attr); 10332001f49Smrg if (!Win) { 10432001f49Smrg Error("Couldn't create window"); 10532001f49Smrg } 10632001f49Smrg 10732001f49Smrg { 10832001f49Smrg XSizeHints sizehints; 10932001f49Smrg sizehints.x = xpos; 11032001f49Smrg sizehints.y = ypos; 11132001f49Smrg sizehints.width = WinWidth; 11232001f49Smrg sizehints.height = WinHeight; 11332001f49Smrg sizehints.flags = USSize | USPosition; 11432001f49Smrg XSetNormalHints(Dpy, Win, &sizehints); 11532001f49Smrg XSetStandardProperties(Dpy, Win, name, name, 11632001f49Smrg None, (char **)NULL, 0, &sizehints); 11732001f49Smrg } 11832001f49Smrg 11932001f49Smrg XMapWindow(Dpy, Win); 12032001f49Smrg} 12132001f49Smrg 12232001f49Smrg 12332001f49Smrg/** 12432001f49Smrg * Change texture image, using TexContext 12532001f49Smrg */ 12632001f49Smrgstatic void 12732001f49SmrgModifyTexture(void) 12832001f49Smrg{ 12932001f49Smrg GLuint tex[TEX_SIZE][TEX_SIZE]; 13032001f49Smrg GLuint c0, c1; 13132001f49Smrg int i, j; 13232001f49Smrg 13332001f49Smrg if (Win && !glXMakeCurrent(Dpy, Win, Contexts[TexContext])) { 13432001f49Smrg Error("glXMakeCurrent failed"); 13532001f49Smrg } 13632001f49Smrg 13732001f49Smrg /* choose two random colors */ 13832001f49Smrg c0 = rand() & 0xffffffff; 13932001f49Smrg c1 = rand() & 0xffffffff; 14032001f49Smrg 14132001f49Smrg for (i = 0; i < TEX_SIZE; i++) { 14232001f49Smrg for (j = 0; j < TEX_SIZE; j++) { 14332001f49Smrg if (((i / 4) ^ (j / 4)) & 1) { 14432001f49Smrg tex[i][j] = c0; 14532001f49Smrg } 14632001f49Smrg else { 14732001f49Smrg tex[i][j] = c1; 14832001f49Smrg } 14932001f49Smrg } 15032001f49Smrg } 15132001f49Smrg 15232001f49Smrg glBindTexture(GL_TEXTURE_2D, TexObj); 15332001f49Smrg glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_SIZE, TEX_SIZE, 0, 15432001f49Smrg GL_RGBA, GL_UNSIGNED_BYTE, tex); 15532001f49Smrg 15632001f49Smrg NewTexture = GL_TRUE; 15732001f49Smrg} 15832001f49Smrg 15932001f49Smrg 16032001f49Smrgstatic void 16132001f49SmrgInitContext(void) 16232001f49Smrg{ 16332001f49Smrg glGenTextures(1, &TexObj); 16432001f49Smrg assert(TexObj); 16532001f49Smrg glBindTexture(GL_TEXTURE_2D, TexObj); 16632001f49Smrg glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 16732001f49Smrg glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 16832001f49Smrg glEnable(GL_TEXTURE_2D); 16932001f49Smrg 17032001f49Smrg printf("GL_RENDERER = %s\n", (char*) glGetString(GL_RENDERER)); 17132001f49Smrg} 17232001f49Smrg 17332001f49Smrg 17432001f49Smrgstatic void 17532001f49SmrgSetup(void) 17632001f49Smrg{ 17732001f49Smrg int i; 17832001f49Smrg 17932001f49Smrg Dpy = XOpenDisplay(DisplayName); 18032001f49Smrg if (!Dpy) { 18132001f49Smrg Error("Unable to open display"); 18232001f49Smrg } 18332001f49Smrg 18432001f49Smrg CreateWindow("sharedtex"); 18532001f49Smrg 18632001f49Smrg for (i = 0; i < MAX_CONTEXTS; i++) { 18732001f49Smrg GLXContext share = i > 0 ? Contexts[0] : 0; 18832001f49Smrg 18932001f49Smrg Contexts[i] = glXCreateContext(Dpy, VisInfo, share, True); 19032001f49Smrg if (!Contexts[i]) { 19132001f49Smrg Error("Unable to create GLX context"); 19232001f49Smrg } 19332001f49Smrg 19432001f49Smrg if (!glXMakeCurrent(Dpy, Win, Contexts[i])) { 19532001f49Smrg Error("glXMakeCurrent failed"); 19632001f49Smrg } 19732001f49Smrg 19832001f49Smrg InitContext(); 19932001f49Smrg } 20032001f49Smrg 20132001f49Smrg ModifyTexture(); 20232001f49Smrg} 20332001f49Smrg 20432001f49Smrg 20532001f49Smrg/** 20632001f49Smrg * Redraw window, using DrawContext 20732001f49Smrg */ 20832001f49Smrgstatic void 20932001f49SmrgRedraw(void) 21032001f49Smrg{ 21132001f49Smrg static float rot = 0.0; 21232001f49Smrg float ar; 21332001f49Smrg 21432001f49Smrg rot += 1.0; 21532001f49Smrg 21632001f49Smrg if (Win && !glXMakeCurrent(Dpy, Win, Contexts[DrawContext])) { 21732001f49Smrg Error("glXMakeCurrent failed"); 21832001f49Smrg } 21932001f49Smrg 22032001f49Smrg glViewport(0, 0, WinWidth, WinHeight); 22132001f49Smrg ar = (float) WinWidth / (float) WinHeight; 22232001f49Smrg glMatrixMode(GL_PROJECTION); 22332001f49Smrg glLoadIdentity(); 22432001f49Smrg glOrtho(-ar, ar, -1.0, 1.0, -1.0, 1.0); 22532001f49Smrg glMatrixMode(GL_MODELVIEW); 22632001f49Smrg 22732001f49Smrg glShadeModel(GL_FLAT); 22832001f49Smrg glClearColor(0.5, 0.5, 0.5, 1.0); 22932001f49Smrg glClear(GL_COLOR_BUFFER_BIT); 23032001f49Smrg 23132001f49Smrg glPushMatrix(); 23232001f49Smrg glRotatef(rot, 0, 0, 1); 23332001f49Smrg glScalef(0.7, 0.7, 0.7); 23432001f49Smrg 23532001f49Smrg if (NewTexture) { 23632001f49Smrg /* rebind to get new contents */ 23732001f49Smrg glBindTexture(GL_TEXTURE_2D, TexObj); 23832001f49Smrg NewTexture = GL_FALSE; 23932001f49Smrg } 24032001f49Smrg 24132001f49Smrg /* draw textured quad */ 24232001f49Smrg glBegin(GL_POLYGON); 24332001f49Smrg glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 ); 24432001f49Smrg glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 ); 24532001f49Smrg glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 ); 24632001f49Smrg glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 ); 24732001f49Smrg glEnd(); 24832001f49Smrg 24932001f49Smrg glPopMatrix(); 25032001f49Smrg 25132001f49Smrg if (Win) 25232001f49Smrg glXSwapBuffers(Dpy, Win); 25332001f49Smrg} 25432001f49Smrg 25532001f49Smrg 25632001f49Smrgstatic void 25732001f49SmrgEventLoop(void) 25832001f49Smrg{ 25932001f49Smrg while (1) { 26032001f49Smrg while (XPending(Dpy) > 0) { 26132001f49Smrg XEvent event; 26232001f49Smrg XNextEvent(Dpy, &event); 26332001f49Smrg 26432001f49Smrg switch (event.type) { 26532001f49Smrg case Expose: 26632001f49Smrg Redraw(); 26732001f49Smrg break; 26832001f49Smrg case ConfigureNotify: 26932001f49Smrg WinWidth = event.xconfigure.width; 27032001f49Smrg WinHeight = event.xconfigure.height; 27132001f49Smrg break; 27232001f49Smrg case KeyPress: 27332001f49Smrg { 27432001f49Smrg char buf[100]; 27532001f49Smrg KeySym keySym; 27632001f49Smrg XComposeStatus stat; 27732001f49Smrg XLookupString(&event.xkey, buf, sizeof(buf), &keySym, &stat); 27832001f49Smrg switch (keySym) { 27932001f49Smrg case XK_Escape: 28032001f49Smrg exit(0); 28132001f49Smrg break; 28232001f49Smrg case XK_t: 28332001f49Smrg case XK_T: 28432001f49Smrg ModifyTexture(); 28532001f49Smrg break; 28632001f49Smrg default: 28732001f49Smrg ; 28832001f49Smrg } 28932001f49Smrg } 29032001f49Smrg Redraw(); 29132001f49Smrg break; 29232001f49Smrg default: 29332001f49Smrg /*no-op*/ ; 29432001f49Smrg } 29532001f49Smrg } 29632001f49Smrg 29732001f49Smrg Redraw(); 29832001f49Smrg usleep(10000); 29932001f49Smrg } 30032001f49Smrg} 30132001f49Smrg 30232001f49Smrg 30332001f49Smrg 30432001f49Smrg 30532001f49Smrgint 30632001f49Smrgmain(int argc, char *argv[]) 30732001f49Smrg{ 30832001f49Smrg int i; 30932001f49Smrg 31032001f49Smrg for (i = 1; i < argc; i++) { 31132001f49Smrg if (strcmp(argv[i], "-display") == 0 && i < argc) { 31232001f49Smrg DisplayName = argv[i+1]; 31332001f49Smrg i++; 31432001f49Smrg } 31532001f49Smrg } 31632001f49Smrg 31732001f49Smrg Setup(); 31832001f49Smrg 31932001f49Smrg printf("Press 't' to change texture image/colors\n"); 32032001f49Smrg 32132001f49Smrg EventLoop(); 32232001f49Smrg 32332001f49Smrg return 0; 32432001f49Smrg} 325