Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

sharedtex.c 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Test sharing of texture objects by two rendering contexts.
  3. * In particular, test that changing a texture object in one context
  4. * effects the texture in the second context.
  5. *
  6. * Brian Paul
  7. * 30 Apr 2008
  8. *
  9. * Copyright (C) 2008 Brian Paul All Rights Reserved.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included
  19. * in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  25. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <GL/gl.h>
  29. #include <GL/glx.h>
  30. #include <assert.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <X11/keysym.h>
  36. #define MAX_CONTEXTS 2
  37. #define TEX_SIZE 32
  38. static const char *DisplayName = NULL;
  39. static Display *Dpy;
  40. static XVisualInfo *VisInfo;
  41. static Window Win;
  42. static GLXContext Contexts[MAX_CONTEXTS];
  43. static int WinWidth = 300, WinHeight = 300;
  44. static int DrawContext = 0, TexContext = 1;
  45. static GLuint TexObj = 0;
  46. static GLboolean NewTexture = GL_FALSE;
  47. static void
  48. Error(const char *msg)
  49. {
  50. fprintf(stderr, "sharedtex error: %s\n", msg);
  51. exit(1);
  52. }
  53. static void
  54. CreateWindow(const char *name)
  55. {
  56. int attrib[] = { GLX_RGBA,
  57. GLX_RED_SIZE, 1,
  58. GLX_GREEN_SIZE, 1,
  59. GLX_BLUE_SIZE, 1,
  60. GLX_DOUBLEBUFFER,
  61. None };
  62. int scrnum;
  63. XSetWindowAttributes attr;
  64. unsigned long mask;
  65. Window root;
  66. int xpos = 0, ypos = 0;
  67. static int n = 0;
  68. scrnum = DefaultScreen(Dpy);
  69. root = RootWindow(Dpy, scrnum);
  70. VisInfo = glXChooseVisual(Dpy, scrnum, attrib);
  71. if (!VisInfo) {
  72. Error("Unable to find RGB, double-buffered visual");
  73. }
  74. /* window attributes */
  75. xpos = (n % 10) * 100;
  76. ypos = (n / 10) * 100;
  77. n++;
  78. attr.background_pixel = 0;
  79. attr.border_pixel = 0;
  80. attr.colormap = XCreateColormap(Dpy, root, VisInfo->visual, AllocNone);
  81. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  82. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  83. Win = XCreateWindow(Dpy, root, xpos, ypos, WinWidth, WinHeight,
  84. 0, VisInfo->depth, InputOutput,
  85. VisInfo->visual, mask, &attr);
  86. if (!Win) {
  87. Error("Couldn't create window");
  88. }
  89. {
  90. XSizeHints sizehints;
  91. sizehints.x = xpos;
  92. sizehints.y = ypos;
  93. sizehints.width = WinWidth;
  94. sizehints.height = WinHeight;
  95. sizehints.flags = USSize | USPosition;
  96. XSetNormalHints(Dpy, Win, &sizehints);
  97. XSetStandardProperties(Dpy, Win, name, name,
  98. None, (char **)NULL, 0, &sizehints);
  99. }
  100. XMapWindow(Dpy, Win);
  101. }
  102. /**
  103. * Change texture image, using TexContext
  104. */
  105. static void
  106. ModifyTexture(void)
  107. {
  108. GLuint tex[TEX_SIZE][TEX_SIZE];
  109. GLuint c0, c1;
  110. int i, j;
  111. if (Win && !glXMakeCurrent(Dpy, Win, Contexts[TexContext])) {
  112. Error("glXMakeCurrent failed");
  113. }
  114. /* choose two random colors */
  115. c0 = rand() & 0xffffffff;
  116. c1 = rand() & 0xffffffff;
  117. for (i = 0; i < TEX_SIZE; i++) {
  118. for (j = 0; j < TEX_SIZE; j++) {
  119. if (((i / 4) ^ (j / 4)) & 1) {
  120. tex[i][j] = c0;
  121. }
  122. else {
  123. tex[i][j] = c1;
  124. }
  125. }
  126. }
  127. glBindTexture(GL_TEXTURE_2D, TexObj);
  128. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_SIZE, TEX_SIZE, 0,
  129. GL_RGBA, GL_UNSIGNED_BYTE, tex);
  130. NewTexture = GL_TRUE;
  131. }
  132. static void
  133. InitContext(void)
  134. {
  135. glGenTextures(1, &TexObj);
  136. assert(TexObj);
  137. glBindTexture(GL_TEXTURE_2D, TexObj);
  138. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  139. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  140. glEnable(GL_TEXTURE_2D);
  141. printf("GL_RENDERER = %s\n", (char*) glGetString(GL_RENDERER));
  142. }
  143. static void
  144. Setup(void)
  145. {
  146. int i;
  147. Dpy = XOpenDisplay(DisplayName);
  148. if (!Dpy) {
  149. Error("Unable to open display");
  150. }
  151. CreateWindow("sharedtex");
  152. for (i = 0; i < MAX_CONTEXTS; i++) {
  153. GLXContext share = i > 0 ? Contexts[0] : 0;
  154. Contexts[i] = glXCreateContext(Dpy, VisInfo, share, True);
  155. if (!Contexts[i]) {
  156. Error("Unable to create GLX context");
  157. }
  158. if (!glXMakeCurrent(Dpy, Win, Contexts[i])) {
  159. Error("glXMakeCurrent failed");
  160. }
  161. InitContext();
  162. }
  163. ModifyTexture();
  164. }
  165. /**
  166. * Redraw window, using DrawContext
  167. */
  168. static void
  169. Redraw(void)
  170. {
  171. static float rot = 0.0;
  172. float ar;
  173. rot += 1.0;
  174. if (Win && !glXMakeCurrent(Dpy, Win, Contexts[DrawContext])) {
  175. Error("glXMakeCurrent failed");
  176. }
  177. glViewport(0, 0, WinWidth, WinHeight);
  178. ar = (float) WinWidth / (float) WinHeight;
  179. glMatrixMode(GL_PROJECTION);
  180. glLoadIdentity();
  181. glOrtho(-ar, ar, -1.0, 1.0, -1.0, 1.0);
  182. glMatrixMode(GL_MODELVIEW);
  183. glShadeModel(GL_FLAT);
  184. glClearColor(0.5, 0.5, 0.5, 1.0);
  185. glClear(GL_COLOR_BUFFER_BIT);
  186. glPushMatrix();
  187. glRotatef(rot, 0, 0, 1);
  188. glScalef(0.7, 0.7, 0.7);
  189. if (NewTexture) {
  190. /* rebind to get new contents */
  191. glBindTexture(GL_TEXTURE_2D, TexObj);
  192. NewTexture = GL_FALSE;
  193. }
  194. /* draw textured quad */
  195. glBegin(GL_POLYGON);
  196. glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
  197. glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
  198. glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
  199. glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
  200. glEnd();
  201. glPopMatrix();
  202. if (Win)
  203. glXSwapBuffers(Dpy, Win);
  204. }
  205. static void
  206. EventLoop(void)
  207. {
  208. while (1) {
  209. while (XPending(Dpy) > 0) {
  210. XEvent event;
  211. XNextEvent(Dpy, &event);
  212. switch (event.type) {
  213. case Expose:
  214. Redraw();
  215. break;
  216. case ConfigureNotify:
  217. WinWidth = event.xconfigure.width;
  218. WinHeight = event.xconfigure.height;
  219. break;
  220. case KeyPress:
  221. {
  222. char buf[100];
  223. KeySym keySym;
  224. XComposeStatus stat;
  225. XLookupString(&event.xkey, buf, sizeof(buf), &keySym, &stat);
  226. switch (keySym) {
  227. case XK_Escape:
  228. exit(0);
  229. break;
  230. case XK_t:
  231. case XK_T:
  232. ModifyTexture();
  233. break;
  234. default:
  235. ;
  236. }
  237. }
  238. Redraw();
  239. break;
  240. default:
  241. /*no-op*/ ;
  242. }
  243. }
  244. Redraw();
  245. usleep(10000);
  246. }
  247. }
  248. int
  249. main(int argc, char *argv[])
  250. {
  251. int i;
  252. for (i = 1; i < argc; i++) {
  253. if (strcmp(argv[i], "-display") == 0 && i < argc) {
  254. DisplayName = argv[i+1];
  255. i++;
  256. }
  257. }
  258. Setup();
  259. printf("Press 't' to change texture image/colors\n");
  260. EventLoop();
  261. return 0;
  262. }