From 58877d1b3ddf0e4521d680b85e31b7a4c6287302 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 14 Nov 2016 12:11:53 -0800 Subject: [PATCH] [core] Assign attribute locations sequentially Two reasons to prefer explicit sequential location assignment, rather than relying on the GLSL linker to assign locations: * For data-driven properties, we want to have the option to use glDisableVertexAttribArray plus glVertexAttrib*. In order to use glDisableVertexAttribArray, we must avoid using attribute location 0, which cannot be disabled. * We want to use the same VAO in cases where, say, a fill layer might be rendered with FillProgram at first, and then FillPatternProgram later. VAOs do not store the program binding, only the attribute bindings, so as long as the two programs have the same attributes and attribute locations, only a single VAO (per segment) is needed. --- src/mbgl/gl/attribute.cpp | 5 +++-- src/mbgl/gl/attribute.hpp | 7 +++++-- src/mbgl/gl/context.cpp | 17 +++++++++++------ src/mbgl/gl/context.hpp | 1 + src/mbgl/gl/program.hpp | 2 +- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/mbgl/gl/attribute.cpp b/src/mbgl/gl/attribute.cpp index 51fdd1dd1..7432fff59 100644 --- a/src/mbgl/gl/attribute.cpp +++ b/src/mbgl/gl/attribute.cpp @@ -4,8 +4,9 @@ namespace mbgl { namespace gl { -AttributeLocation attributeLocation(ProgramID id, const char* name) { - return MBGL_CHECK_ERROR(glGetAttribLocation(id, name)); +AttributeLocation bindAttributeLocation(ProgramID id, AttributeLocation location, const char* name) { + MBGL_CHECK_ERROR(glBindAttribLocation(id, location, name)); + return location; } void bindAttribute(AttributeLocation location, diff --git a/src/mbgl/gl/attribute.hpp b/src/mbgl/gl/attribute.hpp index df469fc6a..e45014127 100644 --- a/src/mbgl/gl/attribute.hpp +++ b/src/mbgl/gl/attribute.hpp @@ -17,6 +17,9 @@ public: class State { public: + explicit State(AttributeLocation location_) + : location(location_) {} + AttributeLocation location; static constexpr std::size_t count = N; static constexpr DataType type = DataTypeOf::value; @@ -130,7 +133,7 @@ const std::size_t Vertex::attributeOffsets[5] = { } // namespace detail -AttributeLocation attributeLocation(ProgramID, const char * name); +AttributeLocation bindAttributeLocation(ProgramID, AttributeLocation, const char * name); void bindAttribute(AttributeLocation location, std::size_t count, @@ -149,7 +152,7 @@ public: static constexpr std::size_t Index = TypeIndex::value; static State state(const ProgramID& id) { - return State { { attributeLocation(id, As::name) }... }; + return State { typename As::State(bindAttributeLocation(id, Index, As::name))... }; } static std::function binder(const State& state) { diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index c4485ad0f..3251be0c8 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -68,19 +68,24 @@ UniqueProgram Context::createProgram(ShaderID vertexShader, ShaderID fragmentSha MBGL_CHECK_ERROR(glAttachShader(result, vertexShader)); MBGL_CHECK_ERROR(glAttachShader(result, fragmentShader)); - MBGL_CHECK_ERROR(glLinkProgram(result)); + + return result; +} + +void Context::linkProgram(ProgramID program_) { + MBGL_CHECK_ERROR(glLinkProgram(program_)); GLint status; - MBGL_CHECK_ERROR(glGetProgramiv(result, GL_LINK_STATUS, &status)); - if (status != 0) { - return result; + MBGL_CHECK_ERROR(glGetProgramiv(program_, GL_LINK_STATUS, &status)); + if (status == GL_TRUE) { + return; } GLint logLength; - MBGL_CHECK_ERROR(glGetProgramiv(result, GL_INFO_LOG_LENGTH, &logLength)); + MBGL_CHECK_ERROR(glGetProgramiv(program_, GL_INFO_LOG_LENGTH, &logLength)); const auto log = std::make_unique(logLength); if (logLength > 0) { - MBGL_CHECK_ERROR(glGetProgramInfoLog(result, logLength, &logLength, log.get())); + MBGL_CHECK_ERROR(glGetProgramInfoLog(program_, logLength, &logLength, log.get())); Log::Error(Event::Shader, "Program failed to link: %s", log.get()); } diff --git a/src/mbgl/gl/context.hpp b/src/mbgl/gl/context.hpp index 154921a1b..33b7a555a 100644 --- a/src/mbgl/gl/context.hpp +++ b/src/mbgl/gl/context.hpp @@ -38,6 +38,7 @@ public: UniqueShader createShader(ShaderType type, const std::string& source); UniqueProgram createProgram(ShaderID vertexShader, ShaderID fragmentShader); + void linkProgram(ProgramID); UniqueTexture createTexture(); template diff --git a/src/mbgl/gl/program.hpp b/src/mbgl/gl/program.hpp index 929237037..44c16ae67 100644 --- a/src/mbgl/gl/program.hpp +++ b/src/mbgl/gl/program.hpp @@ -30,7 +30,7 @@ public: fragmentShader(context.createShader(ShaderType::Fragment, fragmentSource)), program(context.createProgram(vertexShader, fragmentShader)), attributesState(Attributes::state(program)), - uniformsState(Uniforms::state(program)) {} + uniformsState((context.linkProgram(program), Uniforms::state(program))) {} // Indexed drawing. template