tearing.c
Go to the documentation of this file.
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 
72 {
73  char title[256];
74 
75  snprintf(title, sizeof(title), "Tearing detector (interval %i%s, %0.1f Hz)",
77  (swap_tear && swap_interval < 0) ? " (swap tear)" : "",
78  frame_rate);
79 
80  glfwSetWindowTitle(window, title);
81 }
82 
83 static void set_swap_interval(GLFWwindow* window, int interval)
84 {
85  swap_interval = interval;
87  update_window_title(window);
88 }
89 
90 static void error_callback(int error, const char* description)
91 {
92  fprintf(stderr, "Error: %s\n", description);
93 }
94 
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 
154 int main(int argc, char** argv)
155 {
156  unsigned long frame_count = 0;
157  double last_time, current_time;
159  GLuint vertex_buffer, vertex_shader, fragment_shader, program;
160  GLint mvp_location, vpos_location;
161 
163 
164  if (!glfwInit())
165  exit(EXIT_FAILURE);
166 
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);
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 
187 
188  glGenBuffers(1, &vertex_buffer);
189  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
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);
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);
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 
int height
Definition: glfw3.h:1532
#define glBufferData
#define glCreateProgram
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:544
#define GL_VERTEX_SHADER
vec4 mat4x4[4]
Definition: linmath.h:83
#define GL_STATIC_DRAW
khronos_float_t GLfloat
static void update_window_title(GLFWwindow *window)
Definition: tearing.c:71
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition: tearing.c:95
GLFWAPI void glfwSetWindowTitle(GLFWwindow *window, const char *title)
Sets the title of the specified window.
Definition: window.c:495
static double frame_rate
Definition: tearing.c:69
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
long long current_time()
static void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f)
Definition: linmath.h:341
#define glGetAttribLocation
float x
Definition: tearing.c:42
GLfloat GLfloat p
Definition: glext.h:12687
const GLfloat * m
Definition: glext.h:6814
void *(* GLADloadproc)(const char *name)
static int swap_interval
Definition: tearing.c:68
static GLFWwindow * window
Definition: joysticks.c:55
#define GLFW_KEY_UP
Definition: glfw3.h:421
int width
Definition: glfw3.h:1529
GLFWAPI const GLFWvidmode * glfwGetVideoMode(GLFWmonitor *monitor)
Returns the current mode of the specified monitor.
Definition: monitor.c:417
#define GLFW_CONTEXT_VERSION_MAJOR
Context client API major version hint and attribute.
Definition: glfw3.h:921
#define glDrawArrays
struct GLFWmonitor GLFWmonitor
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
int refreshRate
Definition: glfw3.h:1544
#define glUniformMatrix4fv
#define glUseProgram
#define glCreateShader
GLuint64 key
Definition: glext.h:8966
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
#define glVertexAttribPointer
GLenum mode
static void mat4x4_translate(mat4x4 T, float x, float y, float z)
Definition: linmath.h:165
#define GL_FLOAT
#define glEnableVertexAttribArray
#define GL_COLOR_BUFFER_BIT
#define GLFW_KEY_F11
Definition: glfw3.h:441
GLbitfield GLuint program
Definition: glext.h:1889
int GLint
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
Definition: context.c:658
#define glBindBuffer
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
#define glClear
GLint GLsizei GLsizei height
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
static const char * vertex_shader_text
Definition: tearing.c:51
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
static const struct @55 vertices[4]
#define GLFW_KEY_ENTER
Definition: glfw3.h:413
#define glGetUniformLocation
float y
Definition: tearing.c:42
#define GL_FRAGMENT_SHADER
#define glShaderSource
#define GLFW_MOD_ALT
If this bit is set one or more Alt keys were held down.
Definition: glfw3.h:509
action
Definition: enums.py:62
#define glViewport
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
GLFWAPI GLFWmonitor * glfwGetWindowMonitor(GLFWwindow *window)
Returns the monitor that the window uses for full screen mode.
Definition: window.c:907
static const textual_icon exit
Definition: model-views.h:254
#define glAttachShader
static int swap_tear
Definition: tearing.c:67
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
#define glCompileShader
GLFWAPI void glfwSetWindowMonitor(GLFWwindow *window, GLFWmonitor *monitor, int xpos, int ypos, int width, int height, int refreshRate)
Sets the mode, monitor, video mode and placement of a window.
Definition: window.c:916
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
static void error_callback(int error, const char *description)
Definition: tearing.c:90
#define GL_FALSE
#define GLFW_KEY_DOWN
Definition: glfw3.h:420
GLFWAPI double glfwGetTime(void)
Returns the value of the GLFW timer.
Definition: input.c:1275
Video mode type.
Definition: glfw3.h:1525
GLFWAPI void glfwGetWindowPos(GLFWwindow *window, int *xpos, int *ypos)
Retrieves the position of the client area of the specified window.
Definition: window.c:517
unsigned int GLuint
#define glGenBuffers
GLFWAPI GLFWmonitor * glfwGetPrimaryMonitor(void)
Returns the primary monitor.
Definition: monitor.c:308
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
#define glLinkProgram
#define GL_TRIANGLE_FAN
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
static void set_swap_interval(GLFWwindow *window, int interval)
Definition: tearing.c:83
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:1072
#define NULL
Definition: tinycthread.c:47
static void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b)
Definition: linmath.h:145
GLFWAPI int glfwExtensionSupported(const char *extension)
Returns whether the specified extension is available.
Definition: context.c:675
int main(int argc, char **argv)
Definition: tearing.c:154
#define GLFW_CONTEXT_VERSION_MINOR
Context client API minor version hint and attribute.
Definition: glfw3.h:927
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
#define GL_ARRAY_BUFFER
static const char * fragment_shader_text
Definition: tearing.c:60
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
GLint GLsizei width
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11