ScrapExplorer - tearing.c

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