Skip to content

Instantly share code, notes, and snippets.

@chebert
Last active May 5, 2020 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chebert/23b1ccbe1cfc55c117c9d45e3e1d9ee1 to your computer and use it in GitHub Desktop.
Save chebert/23b1ccbe1cfc55c117c9d45e3e1d9ee1 to your computer and use it in GitHub Desktop.
Focused recreation of problem I'm encountering with using OpenGL versions >3.1 on my laptop
/* Folder structure
workspace
|- build.bat -- listing below
|- Glew dlls
|- SDL2 dlls
|- include\
|- SDL2\ -- SDL2 header files
|- GL\ -- Glew header files
|- lib\
|- Glew libs
|- SDL2 libs
|- gl_version_test\
|- base_test.cpp
*/
// build.bat, run in VS 2019 developer console.
/*
@echo off
SET Includes=/I include\
SET Libs=lib\SDL2main.lib lib\SDL2.lib lib\glew32.lib opengl32.lib
cl -Zi gl_version_test\base_test.cpp %Includes% /link -incremental:no /SUBSYSTEM:CONSOLE %Libs%
*/
#include "GL/glew.h"
#define SDL_MAIN_HANDLED
#include "SDL2/SDL.h"
#include <stdio.h>
// -- Introduces issues --------
// Major, Minor version for OpenGL (The culprit)
const int version[] = { 3, 1 }; // works
//const int version[] = { 4, 5 }; // introduces issues with of many dropped frames
// -- Settings I've played with, but don't seem to have an effect --------
const int dimensions[] = { 640, 480 }; // width, height
const bool fullscreen = false;
enum {
SINGLE_BUFFER = 0,
DOUBLE_BUFFER = 1,
};
const int buffer_strategy = DOUBLE_BUFFER;
//const int buffer_strategy = SINGLE_BUFFER;
enum {
SOFTWARE_RENDERING = 0,
HARDWARE_RENDERING = 1,
};
const int rendering_strategy = HARDWARE_RENDERING;
//const int rendering_strategy = SOFTWARE_RENDERING;
enum {
ADAPTIVE_VSYNC = -1,
IMMEDIATE = 0,
VSYNC = 1
};
const int sync_strategy = VSYNC;
//const int sync_strategy = IMMEDIATE;
// -- Stuff I haven't really touched --------
const int context_profile = SDL_GL_CONTEXT_PROFILE_CORE;
const unsigned int init_flags = SDL_INIT_VIDEO;
const int glew_experimental = GL_TRUE;
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define SDL_GL_ATTRS \
X(SDL_GL_RED_SIZE)\
X(SDL_GL_GREEN_SIZE)\
X(SDL_GL_BLUE_SIZE)\
X(SDL_GL_ALPHA_SIZE)\
X(SDL_GL_BUFFER_SIZE)\
X(SDL_GL_DOUBLEBUFFER)\
X(SDL_GL_DEPTH_SIZE)\
X(SDL_GL_STENCIL_SIZE)\
X(SDL_GL_ACCUM_RED_SIZE)\
X(SDL_GL_ACCUM_GREEN_SIZE)\
X(SDL_GL_ACCUM_BLUE_SIZE)\
X(SDL_GL_ACCUM_ALPHA_SIZE)\
X(SDL_GL_STEREO)\
X(SDL_GL_MULTISAMPLEBUFFERS)\
X(SDL_GL_MULTISAMPLESAMPLES)\
X(SDL_GL_ACCELERATED_VISUAL)\
X(SDL_GL_CONTEXT_MAJOR_VERSION)\
X(SDL_GL_CONTEXT_MINOR_VERSION)\
X(SDL_GL_CONTEXT_FLAGS)\
X(SDL_GL_CONTEXT_PROFILE_MASK)\
X(SDL_GL_SHARE_WITH_CURRENT_CONTEXT)\
X(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE)\
X(SDL_GL_CONTEXT_RELEASE_BEHAVIOR)
#define ARRAY_LENGTH(array) (sizeof(array)/sizeof(array[0]))
// List of { SDL_GL_RED_SIZE, SDL_GL_GREEN_SIZE, ... }
const SDL_GLattr sdl_gl_attrs[] = {
#define X(arg) arg,
SDL_GL_ATTRS
#undef X
};
// List of { "SDL_GL_RED_SIZE", "SDL_GL_GREEN_SIZE", ... }
const char *sdl_gl_attrs_names[] = {
#define X(arg) #arg,
SDL_GL_ATTRS
#undef X
};
#define GL_STRINGS \
X(GL_VENDOR)\
X(GL_RENDERER)\
X(GL_VERSION)\
X(GL_SHADING_LANGUAGE_VERSION)
// List of { GL_VENDOR, GL_RENDERER, ... }
const GLenum gl_strings[] = {
#define X(arg) arg,
GL_STRINGS
#undef X
};
// List of { "GL_VENDOR", "GL_RENDERER", ... }
const char *gl_strings_names[] = {
#define X(arg) #arg,
GL_STRINGS
#undef X
};
// Prints "<attr_name>: <attr_value>\n"
void PrintSDLGLAttr(const char *attribute_name, SDL_GLattr attribute) {
int value;
SDL_GL_GetAttribute(attribute, &value);
printf("%s: %d\n", attribute_name, value);
}
// Prints "<string_name>: <string>\n"
void PrintGLString(const char *string_name, GLenum name) {
printf("%s: %s\n", string_name, glGetString(name));
}
int main(int argc, char **args) {
SDL_Init(init_flags);
// Setup GL
// Disable deprecated functions
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, context_profile);
// OpenGL Version
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, version[0]);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, version[1]);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, buffer_strategy);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, rendering_strategy);
SDL_GL_SetSwapInterval(sync_strategy);
SDL_Window *window = SDL_CreateWindow("OpenGL Test",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
// width, height
dimensions[0], dimensions[1],
SDL_WINDOW_OPENGL |
(fullscreen ? SDL_WINDOW_FULLSCREEN : 0));
SDL_GLContext context = SDL_GL_CreateContext(window);
// Print all SDL GL Attributes
for (int i = 0; i < ARRAY_LENGTH(sdl_gl_attrs); ++i) {
PrintSDLGLAttr(sdl_gl_attrs_names[i], sdl_gl_attrs[i]);
}
// Print all GL Strings
for (int i = 0; i < ARRAY_LENGTH(gl_strings); ++i) {
PrintGLString(gl_strings_names[i], gl_strings[i]);
}
glewExperimental = glew_experimental;
glewInit();
int ticks = 0;
int last_time = SDL_GetTicks();
while (true) {
if (last_time - SDL_GetTicks() >= 16) {
// Quit after ~5 seconds
if (ticks > 5 * 60) break;
last_time = SDL_GetTicks();
float value = (ticks % 360) / 360.f;
glClearColor(value, value, value, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
++ticks;
int desired_delay = (16 - (SDL_GetTicks() - last_time));
SDL_Delay(MAX(desired_delay, 1));
int elapsed = SDL_GetTicks() - last_time;
if (elapsed > 18)
printf("%d - ms elapsed since beginning of frame (long frame)\n", elapsed);
if (elapsed < 15)
printf("%d - ms elapsed since beginning of frame (short frame)\n", elapsed);
}
}
// Cleanup
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
@chebert
Copy link
Author

chebert commented May 5, 2020

Glew binaries obtained from: http://glew.sourceforge.net/index.html "Windows 32-bit and 64-bit" Binaries. Version 2.2.0.
SDL2 Binaries obtained from: https://www.libsdl.org/download-2.0.php "SDL2-2.0.12-win32-x86.zip (32-bit Windows)" Runtime Binaries.

Installed OpenGL Development libraries via Windows 10 SDK https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk/

@chebert
Copy link
Author

chebert commented May 5, 2020

After running this with version = {4, 5} (OpenGL version 4.5), I get the following output

SDL_GL_RED_SIZE: 8
SDL_GL_GREEN_SIZE: 8
SDL_GL_BLUE_SIZE: 8
SDL_GL_ALPHA_SIZE: 8
SDL_GL_BUFFER_SIZE: 32
SDL_GL_DOUBLEBUFFER: 1
SDL_GL_DEPTH_SIZE: 16
SDL_GL_STENCIL_SIZE: 0
SDL_GL_ACCUM_RED_SIZE: 16
SDL_GL_ACCUM_GREEN_SIZE: 16
SDL_GL_ACCUM_BLUE_SIZE: 16
SDL_GL_ACCUM_ALPHA_SIZE: 16
SDL_GL_STEREO: 0
SDL_GL_MULTISAMPLEBUFFERS: 0
SDL_GL_MULTISAMPLESAMPLES: 0
SDL_GL_ACCELERATED_VISUAL: 1
SDL_GL_CONTEXT_MAJOR_VERSION: 4
SDL_GL_CONTEXT_MINOR_VERSION: 5
SDL_GL_CONTEXT_FLAGS: 0
SDL_GL_CONTEXT_PROFILE_MASK: 1
SDL_GL_SHARE_WITH_CURRENT_CONTEXT: 0
SDL_GL_FRAMEBUFFER_SRGB_CAPABLE: 0
SDL_GL_CONTEXT_RELEASE_BEHAVIOR: 33532
GL_VENDOR: Intel
GL_RENDERER: Intel(R) UHD Graphics 620
GL_VERSION: 4.5.0 - Build 24.20.100.6346
GL_SHADING_LANGUAGE_VERSION: 4.50 - Build 24.20.100.6346
Intel(R) UHD Graphics 620, 4.5.0 - Build 24.20.100.6346

@chebert
Copy link
Author

chebert commented May 5, 2020

Found it!

Solution is to call

SDL_GL_SetSwapInterval(1 /*VSYNC*/);

after the call to glewInit()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment