These changes are necessary for programs whose set of active attributes is not fixed at compile time by a template parameter pack, but rather varies based on the generated shader text at runtime. In such cases, the attribute location of a given named attribute may vary between instances of the same Program. Previously, attribute bindings were implicitly associated with a location based on template parameter order, and -1 was used to indicate an inactive attribute. This left us unable to disable the appropriate attribute when it went from active to inactive. Now, the state tracker for bindings explicitly associates locations and state, and an empty optional is used to indicate an inactive attribute. In addition, a gl::VertexArray class is now exposed, allowing more flexibility in the relationship between Programs, Segments, and attribute bindings. In this commit, that relationship does not change, but the subsequent commit adjusts it to match gl-js, reduce rebinds, and work around buggy VAO implementations. VertexArray uses a pimpl idiom in order to support implementations that lack the VAO extension. In that case, all VertexArrays share global binding state, reflecting the platform reality in the absence of VAOs, while still providing a uniform API.
120 lines
4.2 KiB
C++
120 lines
4.2 KiB
C++
#pragma once
|
|
|
|
#include <mbgl/gl/program.hpp>
|
|
#include <mbgl/gl/features.hpp>
|
|
#include <mbgl/programs/segment.hpp>
|
|
#include <mbgl/programs/binary_program.hpp>
|
|
#include <mbgl/programs/attributes.hpp>
|
|
#include <mbgl/programs/program_parameters.hpp>
|
|
#include <mbgl/style/paint_property.hpp>
|
|
#include <mbgl/shaders/shaders.hpp>
|
|
#include <mbgl/util/io.hpp>
|
|
|
|
#include <unordered_map>
|
|
|
|
namespace mbgl {
|
|
|
|
template <class Shaders,
|
|
class Primitive,
|
|
class LayoutAttrs,
|
|
class Uniforms,
|
|
class PaintProps>
|
|
class Program {
|
|
public:
|
|
using LayoutAttributes = LayoutAttrs;
|
|
using LayoutVertex = typename LayoutAttributes::Vertex;
|
|
|
|
using PaintProperties = PaintProps;
|
|
using PaintPropertyBinders = typename PaintProperties::Binders;
|
|
using PaintAttributes = typename PaintPropertyBinders::Attributes;
|
|
using Attributes = gl::ConcatenateAttributes<LayoutAttributes, PaintAttributes>;
|
|
|
|
using UniformValues = typename Uniforms::Values;
|
|
using PaintUniforms = typename PaintPropertyBinders::Uniforms;
|
|
using AllUniforms = gl::ConcatenateUniforms<Uniforms, PaintUniforms>;
|
|
|
|
using ProgramType = gl::Program<Primitive, Attributes, AllUniforms>;
|
|
|
|
ProgramType program;
|
|
|
|
Program(gl::Context& context, const ProgramParameters& programParameters)
|
|
: program(ProgramType::createProgram(
|
|
context,
|
|
programParameters,
|
|
Shaders::name,
|
|
Shaders::vertexSource,
|
|
Shaders::fragmentSource)) {
|
|
}
|
|
|
|
template <class DrawMode>
|
|
void draw(gl::Context& context,
|
|
DrawMode drawMode,
|
|
gl::DepthMode depthMode,
|
|
gl::StencilMode stencilMode,
|
|
gl::ColorMode colorMode,
|
|
const UniformValues& uniformValues,
|
|
const gl::VertexBuffer<LayoutVertex>& layoutVertexBuffer,
|
|
const gl::IndexBuffer<DrawMode>& indexBuffer,
|
|
const SegmentVector<Attributes>& segments,
|
|
const PaintPropertyBinders& paintPropertyBinders,
|
|
const typename PaintProperties::Evaluated& currentProperties,
|
|
float currentZoom) {
|
|
typename AllUniforms::Values allUniformValues = uniformValues
|
|
.concat(paintPropertyBinders.uniformValues(currentZoom, currentProperties));
|
|
|
|
typename Attributes::Bindings allAttributeBindings = LayoutAttributes::bindings(layoutVertexBuffer)
|
|
.concat(paintPropertyBinders.attributeBindings(currentProperties));
|
|
|
|
for (auto& segment : segments) {
|
|
if (!segment.vertexArray) {
|
|
segment.vertexArray = context.createVertexArray();
|
|
}
|
|
|
|
program.draw(
|
|
context,
|
|
std::move(drawMode),
|
|
std::move(depthMode),
|
|
std::move(stencilMode),
|
|
std::move(colorMode),
|
|
allUniformValues,
|
|
*segment.vertexArray,
|
|
Attributes::offsetBindings(allAttributeBindings, segment.vertexOffset),
|
|
indexBuffer,
|
|
segment.indexOffset,
|
|
segment.indexLength);
|
|
}
|
|
}
|
|
};
|
|
|
|
template <class Program>
|
|
class ProgramMap {
|
|
public:
|
|
using PaintProperties = typename Program::PaintProperties;
|
|
using PaintPropertyBinders = typename Program::PaintPropertyBinders;
|
|
using Bitset = typename PaintPropertyBinders::Bitset;
|
|
|
|
ProgramMap(gl::Context& context_, ProgramParameters parameters_)
|
|
: context(context_),
|
|
parameters(std::move(parameters_)) {
|
|
}
|
|
|
|
Program& get(const typename PaintProperties::Evaluated& currentProperties) {
|
|
Bitset bits = PaintPropertyBinders::constants(currentProperties);
|
|
auto it = programs.find(bits);
|
|
if (it != programs.end()) {
|
|
return it->second;
|
|
}
|
|
return programs.emplace(std::piecewise_construct,
|
|
std::forward_as_tuple(bits),
|
|
std::forward_as_tuple(context,
|
|
parameters.withAdditionalDefines(PaintPropertyBinders::defines(currentProperties)))).first->second;
|
|
}
|
|
|
|
private:
|
|
gl::Context& context;
|
|
ProgramParameters parameters;
|
|
std::unordered_map<Bitset, Program> programs;
|
|
};
|
|
|
|
} // namespace mbgl
|