graphics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 552ccdd5fd43bff32b3b00929ce131971aa61c28
parent f5a392a92c9e7218630c630169ae9abddf837bfb
Author: Brian Swetland <swetland@frotz.net>
Date:   Mon,  2 Sep 2013 01:47:03 -0700

shaders: provide shared attrib binding defines between glsl and c++

Diffstat:
Mcommon/shaders.cc | 33++++++++++++++++++---------------
Acommon/shared.h | 34++++++++++++++++++++++++++++++++++
Mhello/assets/simple.glsl | 8++++----
Mtest/assets/simple.glsl | 6+++---
4 files changed, 59 insertions(+), 22 deletions(-)

diff --git a/common/shaders.cc b/common/shaders.cc @@ -19,6 +19,9 @@ #include "app.h" #include "util.h" +#define INCLUDE_SHADER_GLOBALS 1 +#include "shared.h" + #define ENABLE_SHADER_CACHE 0 static void dump_compile_error(unsigned id) { @@ -81,10 +84,10 @@ int Program::link(VertexShader *vs, GeometryShader *gs, PixelShader *ps) { /* bind uniform block indices to bindpoints based on their name */ for (n = 0; n < sizeof(_blocknames) / sizeof(_blocknames[0]); n++) { - r = glGetUniformBlockIndex(id, _blocknames[n]); - if (r != GL_INVALID_INDEX) { - fprintf(stderr,"found %s @ %d\n", _blocknames[n], r); - glUniformBlockBinding(id, r, n); + unsigned idx = glGetUniformBlockIndex(id, _blocknames[n]); + if (idx != GL_INVALID_INDEX) { + fprintf(stderr,"found %s @ %d\n", _blocknames[n], idx); + glUniformBlockBinding(id, idx, n); } } @@ -145,7 +148,9 @@ struct source { unsigned len; }; +#if ENABLE_SHADER_CACHE static source *source_cache = NULL; +#endif static struct source *load_shader_source(const char *fn) { source *src; @@ -249,8 +254,8 @@ static struct source *load_shader_source(const char *fn) { static int compile_shader_source(source *src, const char *name, unsigned id) { char misc[128]; - const char *data[3]; - int size[3]; + const char *data[4]; + int size[4]; section *part; for (part = src->sections; part; part = part->next) { @@ -260,15 +265,13 @@ static int compile_shader_source(source *src, const char *name, unsigned id) { sprintf(misc, "#line %d\n", part->lineno - 1); data[0] = src->common.str; size[0] = src->common.len; - data[1] = misc; - size[1] = strlen(misc); - data[2] = part->str; - size[2] = part->len; -#if 0 - printf("----------------\n%s%s%s\n-------------\n", - data[0], data[1], data[2]); -#endif - glShaderSource(id, 3, data, size); + data[1] = shader_globals; + size[1] = strlen(shader_globals); + data[2] = misc; + size[2] = strlen(misc); + data[3] = part->str; + size[3] = part->len; + glShaderSource(id, 4, data, size); return 0; } } diff --git a/common/shared.h b/common/shared.h @@ -0,0 +1,34 @@ +/* Copyright 2013 Brian Swetland <swetland@frotz.net> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _SHARED_H_ +#define _SHARED_H_ + +/* Constants that are shared with shader programs. */ +/* Ensure that the textual version below stays in sync. */ + +#define A_POSITION 0 +#define A_NORMAL 1 +#define A_TEXCOORD 2 + +#if INCLUDE_SHADER_GLOBALS +static const char *shader_globals = +"#define A_POSITION 0\n" +"#define A_NORMAL 1\n" +"#define A_TEXCOORD 2\n" +; +#endif + +#endif diff --git a/hello/assets/simple.glsl b/hello/assets/simple.glsl @@ -3,14 +3,14 @@ -- vertex -layout(std140) uniform cb0 { +layout(std140) uniform block0 { mat4 MVP; mat4 MV; }; -layout(location = 0) in vec4 POSITION; -layout(location = 1) in vec4 NORMAL; -layout(location = 2) in vec2 TEXCOORD; +layout(location = A_POSITION) in vec4 POSITION; +layout(location = A_NORMAL) in vec4 NORMAL; +layout(location = A_TEXCOORD) in vec2 TEXCOORD; in vec4 LOCATION; diff --git a/test/assets/simple.glsl b/test/assets/simple.glsl @@ -10,9 +10,9 @@ layout(std140) uniform block0 { mat4 MV; }; -layout (location = 0) in vec4 POSITION; -layout (location = 1) in vec4 NORMAL; -layout (location = 2) in vec2 TEXCOORD; +layout (location = A_POSITION) in vec4 POSITION; +layout (location = A_NORMAL) in vec4 NORMAL; +layout (location = A_TEXCOORD) in vec2 TEXCOORD; out vec2 vTEXCOORD; out float vDIFFUSE;