sharing.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //========================================================================
  2. // Context sharing test program
  3. // Copyright (c) Camilla Löwy <[email protected]>
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would
  16. // be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not
  19. // be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. //
  24. //========================================================================
  25. //
  26. // This program is used to test sharing of objects between contexts
  27. //
  28. //========================================================================
  29. #include <glad/glad.h>
  30. #include <GLFW/glfw3.h>
  31. #include <time.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "getopt.h"
  35. #include "linmath.h"
  36. static const char* vertex_shader_text =
  37. "uniform mat4 MVP;\n"
  38. "attribute vec2 vPos;\n"
  39. "varying vec2 texcoord;\n"
  40. "void main()\n"
  41. "{\n"
  42. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  43. " texcoord = vPos;\n"
  44. "}\n";
  45. static const char* fragment_shader_text =
  46. "uniform sampler2D texture;\n"
  47. "uniform vec3 color;\n"
  48. "varying vec2 texcoord;\n"
  49. "void main()\n"
  50. "{\n"
  51. " gl_FragColor = vec4(color * texture2D(texture, texcoord).rgb, 1.0);\n"
  52. "}\n";
  53. static const vec2 vertices[4] =
  54. {
  55. { 0.f, 0.f },
  56. { 1.f, 0.f },
  57. { 1.f, 1.f },
  58. { 0.f, 1.f }
  59. };
  60. static void error_callback(int error, const char* description)
  61. {
  62. fprintf(stderr, "Error: %s\n", description);
  63. }
  64. void APIENTRY debug_callback(GLenum source,
  65. GLenum type,
  66. GLuint id,
  67. GLenum severity,
  68. GLsizei length,
  69. const GLchar* message,
  70. const void* user)
  71. {
  72. fprintf(stderr, "Error: %s\n", message);
  73. }
  74. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  75. {
  76. if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
  77. glfwSetWindowShouldClose(window, GLFW_TRUE);
  78. }
  79. int main(int argc, char** argv)
  80. {
  81. int ch;
  82. GLFWwindow* windows[2];
  83. GLuint texture, program, vertex_buffer;
  84. GLint mvp_location, vpos_location, color_location, texture_location;
  85. srand((unsigned int) time(NULL));
  86. glfwSetErrorCallback(error_callback);
  87. if (!glfwInit())
  88. exit(EXIT_FAILURE);
  89. while ((ch = getopt(argc, argv, "d")) != -1)
  90. {
  91. switch (ch)
  92. {
  93. case 'd':
  94. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
  95. break;
  96. }
  97. }
  98. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  99. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  100. windows[0] = glfwCreateWindow(400, 400, "First", NULL, NULL);
  101. if (!windows[0])
  102. {
  103. glfwTerminate();
  104. exit(EXIT_FAILURE);
  105. }
  106. glfwSetKeyCallback(windows[0], key_callback);
  107. glfwMakeContextCurrent(windows[0]);
  108. // Only enable vsync for the first of the windows to be swapped to
  109. // avoid waiting out the interval for each window
  110. glfwSwapInterval(1);
  111. // The contexts are created with the same APIs so the function
  112. // pointers should be re-usable between them
  113. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  114. if (GLAD_GL_KHR_debug)
  115. {
  116. glDebugMessageCallback(debug_callback, NULL);
  117. glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
  118. }
  119. // Create the OpenGL objects inside the first context, created above
  120. // All objects will be shared with the second context, created below
  121. {
  122. int x, y;
  123. char pixels[16 * 16];
  124. GLuint vertex_shader, fragment_shader;
  125. glGenTextures(1, &texture);
  126. glBindTexture(GL_TEXTURE_2D, texture);
  127. for (y = 0; y < 16; y++)
  128. {
  129. for (x = 0; x < 16; x++)
  130. pixels[y * 16 + x] = rand() % 256;
  131. }
  132. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 16, 16, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
  133. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  134. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  135. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  136. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  137. glCompileShader(vertex_shader);
  138. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  139. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  140. glCompileShader(fragment_shader);
  141. program = glCreateProgram();
  142. glAttachShader(program, vertex_shader);
  143. glAttachShader(program, fragment_shader);
  144. glLinkProgram(program);
  145. mvp_location = glGetUniformLocation(program, "MVP");
  146. color_location = glGetUniformLocation(program, "color");
  147. texture_location = glGetUniformLocation(program, "texture");
  148. vpos_location = glGetAttribLocation(program, "vPos");
  149. glGenBuffers(1, &vertex_buffer);
  150. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  151. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  152. }
  153. glUseProgram(program);
  154. glUniform1i(texture_location, 0);
  155. glEnable(GL_TEXTURE_2D);
  156. glBindTexture(GL_TEXTURE_2D, texture);
  157. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  158. glEnableVertexAttribArray(vpos_location);
  159. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  160. sizeof(vertices[0]), (void*) 0);
  161. windows[1] = glfwCreateWindow(400, 400, "Second", NULL, windows[0]);
  162. if (!windows[1])
  163. {
  164. glfwTerminate();
  165. exit(EXIT_FAILURE);
  166. }
  167. // Place the second window to the right of the first
  168. {
  169. int xpos, ypos, left, right, width;
  170. glfwGetWindowSize(windows[0], &width, NULL);
  171. glfwGetWindowFrameSize(windows[0], &left, NULL, &right, NULL);
  172. glfwGetWindowPos(windows[0], &xpos, &ypos);
  173. glfwSetWindowPos(windows[1], xpos + width + left + right, ypos);
  174. }
  175. glfwSetKeyCallback(windows[1], key_callback);
  176. glfwMakeContextCurrent(windows[1]);
  177. // While objects are shared, the global context state is not and will
  178. // need to be set up for each context
  179. glUseProgram(program);
  180. glEnable(GL_TEXTURE_2D);
  181. glBindTexture(GL_TEXTURE_2D, texture);
  182. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  183. glEnableVertexAttribArray(vpos_location);
  184. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  185. sizeof(vertices[0]), (void*) 0);
  186. while (!glfwWindowShouldClose(windows[0]) &&
  187. !glfwWindowShouldClose(windows[1]))
  188. {
  189. int i;
  190. const vec3 colors[2] =
  191. {
  192. { 0.3f, 0.4f, 1.f },
  193. { 0.8f, 0.4f, 1.f }
  194. };
  195. for (i = 0; i < 2; i++)
  196. {
  197. int width, height;
  198. mat4x4 mvp;
  199. glfwGetFramebufferSize(windows[i], &width, &height);
  200. glfwMakeContextCurrent(windows[i]);
  201. glViewport(0, 0, width, height);
  202. mat4x4_ortho(mvp, 0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
  203. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  204. glUniform3fv(color_location, 1, colors[i]);
  205. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  206. glfwSwapBuffers(windows[i]);
  207. }
  208. glfwWaitEvents();
  209. }
  210. glfwTerminate();
  211. exit(EXIT_SUCCESS);
  212. }