tearing.c 7.0 KB

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