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