Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

texobjshare.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Create several OpenGL rendering contexts, sharing textures, display
  3. * lists, etc. Exercise binding, deleting, etc.
  4. *
  5. * Brian Paul
  6. * 21 December 2004
  7. */
  8. #include <GL/gl.h>
  9. #include <GL/glx.h>
  10. #include <assert.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <X11/keysym.h>
  16. /*
  17. * Each display/window/context:
  18. */
  19. struct context {
  20. char DisplayName[1000];
  21. Display *Dpy;
  22. Window Win;
  23. GLXContext Context;
  24. };
  25. #define MAX_CONTEXTS 200
  26. static struct context Contexts[MAX_CONTEXTS];
  27. static int NumContexts = 0;
  28. static void
  29. Error(const char *display, const char *msg)
  30. {
  31. fprintf(stderr, "Error on display %s - %s\n", display, msg);
  32. exit(1);
  33. }
  34. static struct context *
  35. CreateContext(const char *displayName, const char *name)
  36. {
  37. Display *dpy;
  38. Window win;
  39. GLXContext ctx;
  40. int attrib[] = { GLX_RGBA,
  41. GLX_RED_SIZE, 1,
  42. GLX_GREEN_SIZE, 1,
  43. GLX_BLUE_SIZE, 1,
  44. GLX_DOUBLEBUFFER,
  45. None };
  46. int scrnum;
  47. XSetWindowAttributes attr;
  48. unsigned long mask;
  49. Window root;
  50. XVisualInfo *visinfo;
  51. int width = 90, height = 90;
  52. int xpos = 0, ypos = 0;
  53. if (NumContexts >= MAX_CONTEXTS)
  54. return NULL;
  55. dpy = XOpenDisplay(displayName);
  56. if (!dpy) {
  57. Error(displayName, "Unable to open display");
  58. return NULL;
  59. }
  60. scrnum = DefaultScreen(dpy);
  61. root = RootWindow(dpy, scrnum);
  62. visinfo = glXChooseVisual(dpy, scrnum, attrib);
  63. if (!visinfo) {
  64. Error(displayName, "Unable to find RGB, double-buffered visual");
  65. return NULL;
  66. }
  67. /* window attributes */
  68. xpos = (NumContexts % 10) * 100;
  69. ypos = (NumContexts / 10) * 100;
  70. attr.background_pixel = 0;
  71. attr.border_pixel = 0;
  72. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  73. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  74. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  75. win = XCreateWindow(dpy, root, xpos, ypos, width, height,
  76. 0, visinfo->depth, InputOutput,
  77. visinfo->visual, mask, &attr);
  78. if (!win) {
  79. Error(displayName, "Couldn't create window");
  80. return NULL;
  81. }
  82. {
  83. XSizeHints sizehints;
  84. sizehints.x = xpos;
  85. sizehints.y = ypos;
  86. sizehints.width = width;
  87. sizehints.height = height;
  88. sizehints.flags = USSize | USPosition;
  89. XSetNormalHints(dpy, win, &sizehints);
  90. XSetStandardProperties(dpy, win, name, name,
  91. None, (char **)NULL, 0, &sizehints);
  92. }
  93. if (NumContexts == 0) {
  94. ctx = glXCreateContext(dpy, visinfo, NULL, True);
  95. }
  96. else {
  97. /* share textures & dlists with 0th context */
  98. ctx = glXCreateContext(dpy, visinfo, Contexts[0].Context, True);
  99. }
  100. if (!ctx) {
  101. Error(displayName, "Couldn't create GLX context");
  102. return NULL;
  103. }
  104. XMapWindow(dpy, win);
  105. if (!glXMakeCurrent(dpy, win, ctx)) {
  106. Error(displayName, "glXMakeCurrent failed");
  107. return NULL;
  108. }
  109. if (NumContexts == 0) {
  110. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  111. }
  112. /* save the info for this context */
  113. {
  114. struct context *h = &Contexts[NumContexts];
  115. strcpy(h->DisplayName, name);
  116. h->Dpy = dpy;
  117. h->Win = win;
  118. h->Context = ctx;
  119. NumContexts++;
  120. return &Contexts[NumContexts-1];
  121. }
  122. }
  123. static void
  124. MakeCurrent(int i)
  125. {
  126. if (!glXMakeCurrent(Contexts[i].Dpy, Contexts[i].Win, Contexts[i].Context)) {
  127. fprintf(stderr, "glXMakeCurrent failed!\n");
  128. }
  129. }
  130. static void
  131. DestroyContext(int i)
  132. {
  133. XDestroyWindow(Contexts[i].Dpy, Contexts[i].Win);
  134. glXDestroyContext(Contexts[i].Dpy, Contexts[i].Context);
  135. XCloseDisplay(Contexts[i].Dpy);
  136. }
  137. int
  138. main(int argc, char *argv[])
  139. {
  140. char *dpyName = NULL;
  141. int i;
  142. GLuint t;
  143. GLint tb;
  144. for (i = 0; i < 2; i++) {
  145. CreateContext(dpyName, "context");
  146. }
  147. /* Create texture and bind it in context 0 */
  148. MakeCurrent(0);
  149. glGenTextures(1, &t);
  150. printf("Generated texture ID %u\n", t);
  151. assert(!glIsTexture(t));
  152. glBindTexture(GL_TEXTURE_2D, t);
  153. assert(glIsTexture(t));
  154. glGetIntegerv(GL_TEXTURE_BINDING_2D, &tb);
  155. assert(tb == t);
  156. /* Bind texture in context 1 */
  157. MakeCurrent(1);
  158. assert(glIsTexture(t));
  159. glBindTexture(GL_TEXTURE_2D, t);
  160. glGetIntegerv(GL_TEXTURE_BINDING_2D, &tb);
  161. assert(tb == t);
  162. /* Delete texture from context 0 */
  163. MakeCurrent(0);
  164. glDeleteTextures(1, &t);
  165. assert(!glIsTexture(t));
  166. glGetIntegerv(GL_TEXTURE_BINDING_2D, &tb);
  167. printf("After delete, binding = %d\n", tb);
  168. /* Check texture state from context 1 */
  169. MakeCurrent(1);
  170. assert(!glIsTexture(t));
  171. glGetIntegerv(GL_TEXTURE_BINDING_2D, &tb);
  172. printf("In second context, binding = %d\n", tb);
  173. glBindTexture(GL_TEXTURE_2D, 0);
  174. glGetIntegerv(GL_TEXTURE_BINDING_2D, &tb);
  175. assert(tb == 0);
  176. for (i = 0; i < NumContexts; i++) {
  177. DestroyContext(i);
  178. }
  179. printf("Success!\n");
  180. return 0;
  181. }