[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.
This commit is contained in:
John Firebaugh
2016-11-14 12:11:53 -08:00
parent 0fe8d8d7b4
commit 58877d1b3d
5 changed files with 21 additions and 11 deletions

View File

@@ -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,

View File

@@ -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<T>::value;
@@ -130,7 +133,7 @@ const std::size_t Vertex<A1, A2, A3, A4, A5>::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<A, As...>::value;
static State state(const ProgramID& id) {
return State { { attributeLocation(id, As::name) }... };
return State { typename As::State(bindAttributeLocation(id, Index<As>, As::name))... };
}
static std::function<void (std::size_t)> binder(const State& state) {

View File

@@ -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<GLchar[]>(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());
}

View File

@@ -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 <class Vertex, class DrawMode>

View File

@@ -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 <class DrawMode>