diff options
author | David Robillard <d@drobilla.net> | 2020-04-04 13:36:47 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-04-04 13:36:47 +0200 |
commit | 20fd80c8f20d0d6bda660bd9a273e0c4a78cb9ac (patch) | |
tree | f144e9498a0337bef0d20c9004b024e26a9d2b32 | |
parent | 11800b6179458eb962cd1862e4053efd7f28c2f4 (diff) | |
download | pugl-20fd80c8f20d0d6bda660bd9a273e0c4a78cb9ac.tar.gz pugl-20fd80c8f20d0d6bda660bd9a273e0c4a78cb9ac.tar.bz2 pugl-20fd80c8f20d0d6bda660bd9a273e0c4a78cb9ac.zip |
Shader Demo: Support both GL 3 and 4
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | examples/pugl_shader_demo.c (renamed from examples/pugl_gl3_demo.c) | 43 | ||||
-rw-r--r-- | shaders/header_330.glsl | 2 | ||||
-rw-r--r-- | shaders/header_420.glsl | 5 | ||||
-rw-r--r-- | shaders/rect.frag | 6 | ||||
-rw-r--r-- | shaders/rect.vert | 6 | ||||
-rw-r--r-- | wscript | 5 |
7 files changed, 52 insertions, 21 deletions
@@ -92,9 +92,9 @@ demonstrations: * `pugl_window_demo` demonstrates multiple top-level windows. - * `pugl_gl3_demo` demonstrates using more modern OpenGL where dynamic loading - and shaders are required. It can also be used to test performance by - passing the number of rectangles to draw on the command line. + * `pugl_shader_demo` demonstrates using more modern OpenGL (version 3 or 4) + where dynamic loading and shaders are required. It can also be used to test + performance by passing the number of rectangles to draw on the command line. * `pugl_cairo_demo` demonstrates using Cairo on top of the native windowing system (without OpenGL), and partial redrawing. diff --git a/examples/pugl_gl3_demo.c b/examples/pugl_shader_demo.c index 7b2a49f..ac5fe84 100644 --- a/examples/pugl_gl3_demo.c +++ b/examples/pugl_shader_demo.c @@ -15,7 +15,7 @@ */ /** - @file pugl_gl3_demo.c An example of drawing with OpenGL 3. + @file pugl_shader_demo.c An example of drawing with OpenGL 3/4. This is an example of using OpenGL for pixel-perfect 2D drawing. It uses pixel coordinates for positions and sizes so that things work roughly like a @@ -71,6 +71,8 @@ typedef struct GLuint instanceVbo; GLuint ibo; unsigned framesDrawn; + int glMajorVersion; + int glMinorVersion; int quit; } PuglTestApp; @@ -204,6 +206,8 @@ loadShader(const char* const path) static int parseOptions(PuglTestApp* app, int argc, char** argv) { + char* endptr = NULL; + // Parse command line options app->numRects = 1024; app->opts = puglParseTestOptions(&argc, &argv); @@ -212,11 +216,24 @@ parseOptions(PuglTestApp* app, int argc, char** argv) } // Parse number of rectangles argument, if given - if (argc == 1) { - char* endptr = NULL; - + if (argc >= 1) { app->numRects = (size_t)strtol(argv[0], &endptr, 10); if (endptr != argv[0] + strlen(argv[0])) { + logError("Invalid number of rectangles: %s\n", argv[0]); + return 1; + } + } + + // Parse OpenGL major version argument, if given + if (argc >= 2) { + app->glMajorVersion = (int)strtol(argv[1], &endptr, 10); + if (endptr != argv[1] + strlen(argv[1])) { + logError("Invalid GL major version: %s\n", argv[1]); + return 1; + } else if (app->glMajorVersion == 4) { + app->glMinorVersion = 2; + } else if (app->glMajorVersion != 3) { + logError("Unsupported GL major version %d\n", app->glMajorVersion); return 1; } } @@ -241,8 +258,8 @@ setupPugl(PuglTestApp* app, const PuglRect frame) puglSetBackend(app->view, puglGlBackend()); puglSetViewHint(app->view, PUGL_USE_COMPAT_PROFILE, PUGL_FALSE); puglSetViewHint(app->view, PUGL_USE_DEBUG_CONTEXT, app->opts.errorChecking); - puglSetViewHint(app->view, PUGL_CONTEXT_VERSION_MAJOR, 3); - puglSetViewHint(app->view, PUGL_CONTEXT_VERSION_MINOR, 3); + puglSetViewHint(app->view, PUGL_CONTEXT_VERSION_MAJOR, app->glMajorVersion); + puglSetViewHint(app->view, PUGL_CONTEXT_VERSION_MINOR, app->glMinorVersion); puglSetViewHint(app->view, PUGL_RESIZABLE, app->opts.resizable); puglSetViewHint(app->view, PUGL_SAMPLES, app->opts.samples); puglSetViewHint(app->view, PUGL_DOUBLE_BUFFER, app->opts.doubleBuffer); @@ -261,8 +278,12 @@ setupGl(PuglTestApp* app) return PUGL_FAILURE; } + const char* const headerFile = (app->glMajorVersion == 3 + ? "shaders/header_330.glsl" + : "shaders/header_420.glsl"); + // Load shader sources - char* const headerSource = loadShader("shaders/header_330.glsl"); + char* const headerSource = loadShader(headerFile); char* const vertexSource = loadShader("shaders/rect.vert"); char* const fragmentSource = loadShader("shaders/rect.frag"); if (!vertexSource || !fragmentSource) { @@ -362,14 +383,16 @@ teardownGl(PuglTestApp* app) int main(int argc, char** argv) { - PuglTestApp app; - memset(&app, 0, sizeof(app)); + PuglTestApp app = {0}; + + app.glMajorVersion = 3; + app.glMinorVersion = 3; const PuglRect frame = {0, 0, defaultWidth, defaultHeight}; // Parse command line options if (parseOptions(&app, argc, argv)) { - puglPrintTestUsage("pugl_gl3_demo", "[NUM_RECTS]"); + puglPrintTestUsage("pugl_shader_demo", "[NUM_RECTS] [GL_MAJOR]"); return 1; } diff --git a/shaders/header_330.glsl b/shaders/header_330.glsl index 1bcdf05..bfe7a00 100644 --- a/shaders/header_330.glsl +++ b/shaders/header_330.glsl @@ -1,3 +1,5 @@ #version 330 core +#define INTER(qualifiers) #define UBO(qualifiers) layout(std140) + diff --git a/shaders/header_420.glsl b/shaders/header_420.glsl new file mode 100644 index 0000000..55fbe8a --- /dev/null +++ b/shaders/header_420.glsl @@ -0,0 +1,5 @@ +#version 420 core + +#define INTER(qualifiers) layout(qualifiers) +#define UBO(qualifiers) layout(std140, qualifiers) + diff --git a/shaders/rect.frag b/shaders/rect.frag index 8622782..ecec50d 100644 --- a/shaders/rect.frag +++ b/shaders/rect.frag @@ -8,9 +8,9 @@ specified precisely in pixels to draw sharp lines. The border width is just hardcoded, but could be made a uniform or vertex attribute easily enough. */ -noperspective in vec2 f_uv; -noperspective in vec2 f_size; -noperspective in vec4 f_fillColor; +INTER(location = 0) noperspective in vec2 f_uv; +INTER(location = 1) noperspective in vec2 f_size; +INTER(location = 2) noperspective in vec4 f_fillColor; layout(location = 0) out vec4 FragColor; diff --git a/shaders/rect.vert b/shaders/rect.vert index ef8c8cc..09f1917 100644 --- a/shaders/rect.vert +++ b/shaders/rect.vert @@ -11,9 +11,9 @@ layout(location = 1) in vec2 v_origin; layout(location = 2) in vec2 v_size; layout(location = 3) in vec4 v_fillColor; -noperspective out vec2 f_uv; -noperspective out vec2 f_size; -noperspective out vec4 f_fillColor; +INTER(location = 0) noperspective out vec2 f_uv; +INTER(location = 1) noperspective out vec2 f_size; +INTER(location = 2) noperspective out vec4 f_fillColor; void main() @@ -353,8 +353,9 @@ def build(bld): build_example('pugl_print_events', ['examples/pugl_print_events.c'], platform, 'stub') - build_example('pugl_gl3_demo', - ['examples/pugl_gl3_demo.c', 'examples/glad/glad.c'], + build_example('pugl_shader_demo', + ['examples/pugl_shader_demo.c', + 'examples/glad/glad.c'], platform, 'gl', uselib=['DL', 'GL', 'M']) if bld.env.HAVE_CAIRO: |