tearing.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //========================================================================
  2. // Vsync enabling test
  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 test renders a high contrast, horizontally moving bar, allowing for
  27. // visual verification of whether the set swap interval is indeed obeyed
  28. //
  29. //========================================================================
  30. #define GLAD_GL_IMPLEMENTATION
  31. #include <glad/gl.h>
  32. #define GLFW_INCLUDE_NONE
  33. #include <GLFW/glfw3.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <math.h>
  37. #include "linmath.h"
  38. static const struct
  39. {
  40. float x, y;
  41. } vertices[4] =
  42. {
  43. { -0.25f, -1.f },
  44. { 0.25f, -1.f },
  45. { 0.25f, 1.f },
  46. { -0.25f, 1.f }
  47. };
  48. static const char* vertex_shader_text =
  49. "#version 110\n"
  50. "uniform mat4 MVP;\n"
  51. "attribute vec2 vPos;\n"
  52. "void main()\n"
  53. "{\n"
  54. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  55. "}\n";
  56. static const char* fragment_shader_text =
  57. "#version 110\n"
  58. "void main()\n"
  59. "{\n"
  60. " gl_FragColor = vec4(1.0);\n"
  61. "}\n";
  62. static int swap_tear;
  63. static int swap_interval;
  64. static double frame_rate;
  65. static void update_window_title(GLFWwindow* window)
  66. {
  67. char title[256];
  68. snprintf(title, sizeof(title), "Tearing detector (interval %i%s, %0.1f Hz)",
  69. swap_interval,
  70. (swap_tear && swap_interval < 0) ? " (swap tear)" : "",
  71. frame_rate);
  72. glfwSetWindowTitle(window, title);
  73. }
  74. static void set_swap_interval(GLFWwindow* window, int interval)
  75. {
  76. swap_interval = interval;
  77. glfwSwapInterval(swap_interval);
  78. update_window_title(window);
  79. }
  80. static void error_callback(int error, const char* description)
  81. {
  82. fprintf(stderr, "Error: %s\n", description);
  83. }
  84. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  85. {
  86. if (action != GLFW_PRESS)
  87. return;
  88. switch (key)
  89. {
  90. case GLFW_KEY_UP:
  91. {
  92. if (swap_interval + 1 > swap_interval)
  93. set_swap_interval(window, swap_interval + 1);
  94. break;
  95. }
  96. case GLFW_KEY_DOWN:
  97. {
  98. if (swap_tear)
  99. {
  100. if (swap_interval - 1 < swap_interval)
  101. set_swap_interval(window, swap_interval - 1);
  102. }
  103. else
  104. {
  105. if (swap_interval - 1 >= 0)
  106. set_swap_interval(window, swap_interval - 1);
  107. }
  108. break;
  109. }
  110. case GLFW_KEY_ESCAPE:
  111. glfwSetWindowShouldClose(window, 1);
  112. break;
  113. case GLFW_KEY_F11:
  114. case GLFW_KEY_ENTER:
  115. {
  116. static int x, y, width, height;
  117. if (mods != GLFW_MOD_ALT)
  118. return;
  119. if (glfwGetWindowMonitor(window))
  120. glfwSetWindowMonitor(window, NULL, x, y, width, height, 0);
  121. else
  122. {
  123. GLFWmonitor* monitor = glfwGetPrimaryMonitor();
  124. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  125. glfwGetWindowPos(window, &x, &y);
  126. glfwGetWindowSize(window, &width, &height);
  127. glfwSetWindowMonitor(window, monitor,
  128. 0, 0, mode->width, mode->height,
  129. mode->refreshRate);
  130. }
  131. break;
  132. }
  133. }
  134. }
  135. int main(int argc, char** argv)
  136. {
  137. unsigned long frame_count = 0;
  138. double last_time, current_time;
  139. GLFWwindow* window;
  140. GLuint vertex_buffer, vertex_shader, fragment_shader, program;
  141. GLint mvp_location, vpos_location;
  142. glfwSetErrorCallback(error_callback);
  143. if (!glfwInit())
  144. exit(EXIT_FAILURE);
  145. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  146. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  147. window = glfwCreateWindow(640, 480, "Tearing detector", NULL, NULL);
  148. if (!window)
  149. {
  150. glfwTerminate();
  151. exit(EXIT_FAILURE);
  152. }
  153. glfwMakeContextCurrent(window);
  154. gladLoadGL(glfwGetProcAddress);
  155. set_swap_interval(window, 0);
  156. last_time = glfwGetTime();
  157. frame_rate = 0.0;
  158. swap_tear = (glfwExtensionSupported("WGL_EXT_swap_control_tear") ||
  159. glfwExtensionSupported("GLX_EXT_swap_control_tear"));
  160. glfwSetKeyCallback(window, key_callback);
  161. glGenBuffers(1, &vertex_buffer);
  162. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  163. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  164. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  165. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  166. glCompileShader(vertex_shader);
  167. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  168. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  169. glCompileShader(fragment_shader);
  170. program = glCreateProgram();
  171. glAttachShader(program, vertex_shader);
  172. glAttachShader(program, fragment_shader);
  173. glLinkProgram(program);
  174. mvp_location = glGetUniformLocation(program, "MVP");
  175. vpos_location = glGetAttribLocation(program, "vPos");
  176. glEnableVertexAttribArray(vpos_location);
  177. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  178. sizeof(vertices[0]), (void*) 0);
  179. while (!glfwWindowShouldClose(window))
  180. {
  181. int width, height;
  182. mat4x4 m, p, mvp;
  183. float position = cosf((float) glfwGetTime() * 4.f) * 0.75f;
  184. glfwGetFramebufferSize(window, &width, &height);
  185. glViewport(0, 0, width, height);
  186. glClear(GL_COLOR_BUFFER_BIT);
  187. mat4x4_ortho(p, -1.f, 1.f, -1.f, 1.f, 0.f, 1.f);
  188. mat4x4_translate(m, position, 0.f, 0.f);
  189. mat4x4_mul(mvp, p, m);
  190. glUseProgram(program);
  191. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  192. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  193. glfwSwapBuffers(window);
  194. glfwPollEvents();
  195. frame_count++;
  196. current_time = glfwGetTime();
  197. if (current_time - last_time > 1.0)
  198. {
  199. frame_rate = frame_count / (current_time - last_time);
  200. frame_count = 0;
  201. last_time = current_time;
  202. update_window_title(window);
  203. }
  204. }
  205. glfwTerminate();
  206. exit(EXIT_SUCCESS);
  207. }