aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-04-04 13:36:45 +0200
committerDavid Robillard <d@drobilla.net>2020-04-04 13:36:45 +0200
commitecc281c56a8c3c297cfd2f0b6d4671b2000efd9b (patch)
tree6cabb79d95f19da7fc89318fd2aacf5f18c0200e
parentac3036fd7343ce71377fbfecafd8ba672372a5b9 (diff)
Shader Demo: Factor out version-dependent GLSL header
-rw-r--r--examples/pugl_gl3_demo.c4
-rw-r--r--examples/shader_utils.h21
-rw-r--r--shaders/header_330.glsl1
-rw-r--r--shaders/rect.frag2
-rw-r--r--shaders/rect.vert2
5 files changed, 17 insertions, 13 deletions
diff --git a/examples/pugl_gl3_demo.c b/examples/pugl_gl3_demo.c
index f43592d..6f7ed91 100644
--- a/examples/pugl_gl3_demo.c
+++ b/examples/pugl_gl3_demo.c
@@ -259,6 +259,7 @@ setupGl(PuglTestApp* app)
}
// Load shader sources
+ char* const headerSource = loadShader("shaders/header_330.glsl");
char* const vertexSource = loadShader("shaders/rect.vert");
char* const fragmentSource = loadShader("shaders/rect.frag");
if (!vertexSource || !fragmentSource) {
@@ -267,9 +268,10 @@ setupGl(PuglTestApp* app)
}
// Compile rectangle shaders and program
- app->drawRect = compileProgram(vertexSource, fragmentSource);
+ app->drawRect = compileProgram(headerSource, vertexSource, fragmentSource);
free(fragmentSource);
free(vertexSource);
+ free(headerSource);
if (!app->drawRect.program) {
return PUGL_FAILURE;
}
diff --git a/examples/shader_utils.h b/examples/shader_utils.h
index 834d8fc..2fd26c5 100644
--- a/examples/shader_utils.h
+++ b/examples/shader_utils.h
@@ -28,11 +28,12 @@ typedef struct
} Program;
static GLuint
-compileShader(const char* source, const GLenum type)
+compileShader(const char* header, const char* source, const GLenum type)
{
- GLuint shader = glCreateShader(type);
- const int sourceLength = (int)strlen(source);
- glShaderSource(shader, 1, &source, &sourceLength);
+ const GLchar* sources[] = {header, source};
+ const GLint lengths[] = {(GLint)strlen(header), (GLint)strlen(source)};
+ GLuint shader = glCreateShader(type);
+ glShaderSource(shader, 2, sources, lengths);
glCompileShader(shader);
int status;
@@ -61,13 +62,17 @@ deleteProgram(Program program)
}
static Program
-compileProgram(const char* vertexSource, const char* fragmentSource)
+compileProgram(const char* headerSource,
+ const char* vertexSource,
+ const char* fragmentSource)
{
static const Program nullProgram = {0, 0, 0};
- Program program = {compileShader(vertexSource, GL_VERTEX_SHADER),
- compileShader(fragmentSource, GL_FRAGMENT_SHADER),
- glCreateProgram()};
+ Program program = {
+ compileShader(headerSource, vertexSource, GL_VERTEX_SHADER),
+ compileShader(headerSource, fragmentSource, GL_FRAGMENT_SHADER),
+ glCreateProgram(),
+ };
if (!program.vertexShader || !program.fragmentShader || !program.program) {
deleteProgram(program);
diff --git a/shaders/header_330.glsl b/shaders/header_330.glsl
new file mode 100644
index 0000000..5ae7f43
--- /dev/null
+++ b/shaders/header_330.glsl
@@ -0,0 +1 @@
+#version 330 core
diff --git a/shaders/rect.frag b/shaders/rect.frag
index 5e3af9d..8622782 100644
--- a/shaders/rect.frag
+++ b/shaders/rect.frag
@@ -1,5 +1,3 @@
-#version 330 core
-
/* The fragment shader uses the UV coordinates to calculate whether it is in
the T, R, B, or L border. These are then mixed with the border color, and
their inverse is mixed with the fill color, to calculate the fragment color.
diff --git a/shaders/rect.vert b/shaders/rect.vert
index bf2e951..337f105 100644
--- a/shaders/rect.vert
+++ b/shaders/rect.vert
@@ -1,5 +1,3 @@
-#version 330 core
-
/* The vertex shader is trivial, but forwards scaled UV coordinates (in pixels)
to the fragment shader for drawing the border. */