Topic/drawable 2 (#1047)

uniforms
This commit is contained in:
Tim Sylvester
2023-04-19 15:30:59 -07:00
committed by Bart Louwers
parent aceb0394ba
commit a646cbf938
9 changed files with 209 additions and 59 deletions

View File

@@ -19,16 +19,13 @@ using mat4 = std::array<double, 4*4>;
namespace gfx {
// static_assert(sizeof(Type) < 256, "vertex type must be smaller than 256 bytes");
// static_assert(std::is_standard_layout_v<Type>, "vertex type must use standard layout");
// static_assert(I < Descriptor::data.count, "attribute index must be in range");
class VertexAttributeArray;
class ShaderProgramBase;
class VertexAttribute {
public:
using float2 = std::array<float, 2>;
using float3 = std::array<float, 3>;
using float4 = std::array<float, 4>;
using matf2 = std::array<float, 2*2>;
using matf3 = std::array<float, 3*3>;
using matf4 = std::array<float, 4*4>;
@@ -40,14 +37,16 @@ public:
// Can only be created by VertexAttributeArray implementations
protected:
VertexAttribute(int index_, AttributeDataType dataType_, std::size_t count_)
VertexAttribute(int index_, AttributeDataType dataType_, int size_, std::size_t count_)
: index(index_),
size(size_),
dataType(dataType_),
items(count_) {
}
VertexAttribute(const VertexAttribute &) = default;
VertexAttribute(VertexAttribute&& other)
: index(other.index),
size(other.size),
dataType(other.dataType),
items(std::move(other.items)) {
}
@@ -56,6 +55,10 @@ public:
~VertexAttribute() = default;
int getIndex() const { return index; }
void setIndex(int value) { index = value; }
int getSize() const { return size; }
std::size_t getCount() const { return items.size(); }
AttributeDataType getDataType() const { return dataType; }
@@ -67,10 +70,14 @@ public:
return items[i] = value;
}
bool getDirty() const { return dirty; }
void clearDirty() { dirty = false; }
protected:
VertexAttribute& operator=(const VertexAttribute&) = default;
VertexAttribute& operator=(const VertexAttribute&& other) {
index = other.index;
size = other.size;
dataType = other.dataType;
items = std::move(other.items);
return *this;
@@ -78,6 +85,7 @@ protected:
protected:
int index;
int size;
/// indicates that a value has changed and any cached result should be discarded
mutable bool dirty = true;
@@ -106,17 +114,32 @@ public:
/// Add a new attribute element.
/// Returns a pointer to the new element on success, or null if the attribute already exists.
/// The result is valid only until the next non-const method call on this class.
VertexAttribute* add(std::string name, int index, AttributeDataType, std::size_t count);
VertexAttribute* add(std::string name, int index, AttributeDataType, int size, std::size_t count);
/// Add a new attribute element if it doesn't already exist.
/// Returns a pointer to the new element on success, or null if the type or count conflict with an existing entry.
/// The result is valid only until the next non-const method call on this class.
VertexAttribute* getOrAdd(std::string name, int index, AttributeDataType, std::size_t count);
VertexAttribute* getOrAdd(std::string name, int index, AttributeDataType, int size, std::size_t count);
// Set a value if the element is present
template <typename T>
const VertexAttribute::ElementType& set(const std::string& name, std::size_t i, T value) {
if (auto *item = get(name)) {
item->set(i, value);
}
}
/// Indicates whether any values have changed
bool isDirty() const;
void resetDirty();
using ResolveDelegate = std::function<void(const std::string&, const VertexAttribute&, const VertexAttribute*)>;
/// Call the provided delegate with each value, providing the override if one exists.
void resolve(const VertexAttributeArray& overrides, ResolveDelegate) const;
/// Apply to the active program
virtual void applyUniforms(const ShaderProgramBase&) = 0;
VertexAttributeArray& operator=(VertexAttributeArray &&);
VertexAttributeArray& operator=(const VertexAttributeArray&);
@@ -131,7 +154,8 @@ protected:
}
}
virtual std::unique_ptr<VertexAttribute> create(int index, AttributeDataType dataType, std::size_t count) = 0;
virtual std::unique_ptr<VertexAttribute> create(int index, AttributeDataType dataType,
int size, std::size_t count) = 0;
virtual std::unique_ptr<VertexAttribute> copy(const VertexAttribute& attr) = 0;
protected:

View File

@@ -12,8 +12,8 @@ class VertexAttributeGL final : public gfx::VertexAttribute {
// Can only be created by VertexAttributeArrayGL
private:
friend VertexAttributeArrayGL;
VertexAttributeGL(int index_, gfx::AttributeDataType dataType_, std::size_t count_)
: VertexAttribute(index_, dataType_, count_) {
VertexAttributeGL(int index_, gfx::AttributeDataType dataType_, int size_, std::size_t count_)
: VertexAttribute(index_, dataType_, size_, count_) {
}
VertexAttributeGL(const VertexAttributeGL& other)
: VertexAttribute(other),
@@ -58,9 +58,12 @@ public:
return *this;
}
void applyUniforms(const gfx::ShaderProgramBase&) override;
private:
std::unique_ptr<gfx::VertexAttribute> create(int index, gfx::AttributeDataType dataType, std::size_t count) override {
return std::unique_ptr<gfx::VertexAttribute>(new VertexAttributeGL(index, dataType, count));
std::unique_ptr<gfx::VertexAttribute> create(int index, gfx::AttributeDataType dataType,
int size, std::size_t count) override {
return std::unique_ptr<gfx::VertexAttribute>(new VertexAttributeGL(index, dataType, size, count));
}
std::unique_ptr<gfx::VertexAttribute> copy(const gfx::VertexAttribute& attr) override {
return std::unique_ptr<gfx::VertexAttribute>(new VertexAttributeGL(static_cast<const VertexAttributeGL&>(attr)));

View File

@@ -10,7 +10,9 @@ namespace gl {
class ShaderProgramGL final : public gfx::ShaderProgramBase {
public:
ShaderProgramGL(UniqueProgram&& glProgram_);
ShaderProgramGL(UniqueProgram&& glProgram_, VertexAttributeArrayGL&& attributes);
ShaderProgramGL(UniqueProgram&&,
VertexAttributeArrayGL&& uniforms,
VertexAttributeArrayGL&& attributes);
ShaderProgramGL(ShaderProgramGL&& other);
virtual ~ShaderProgramGL() noexcept = default;
@@ -23,13 +25,21 @@ public:
std::string_view vertexSource,
std::string_view fragmentSource) noexcept(false);
const gfx::VertexAttributeArray& getUniforms() const override { return uniforms; }
const gfx::VertexAttributeArray& getVertexAttributes() const override { return vertexAttributes; }
ProgramID getGLProgramID() const { return glProgram; }
protected:
gfx::VertexAttributeArray& mutableUniforms() override { return uniforms; }
gfx::VertexAttributeArray& mutableVertexAttributes() override { return vertexAttributes; }
protected:
UniqueProgram glProgram;
VertexAttributeArrayGL uniforms;
VertexAttributeArrayGL vertexAttributes;
};

View File

@@ -14,14 +14,43 @@ protected:
ShaderProgramBase() { }
ShaderProgramBase(ShaderProgramBase&&) { }
template <typename T>
bool set(gfx::VertexAttributeArray& attrs, const std::string& name, std::size_t i, T value) {
auto *item = attrs.get(name);
if (item && i < item->getCount()) {
item->set(i, value);
return true;
}
return false;
}
public:
virtual ~ShaderProgramBase() noexcept = default;
const util::SimpleIdentity& getID() const { return shaderProgramID; }
/// Get the available vertex attributes and their default values
virtual const gfx::VertexAttributeArray& getUniforms() const = 0;
/// Get the available vertex attributes and their default values
virtual const gfx::VertexAttributeArray& getVertexAttributes() const = 0;
// Set a value if the element is present
template <typename T>
bool setUniform(const std::string& name, std::size_t i, T value) {
return set(mutableUniforms(), name, i, value);
}
template <typename T>
bool setAttribute(const std::string& name, std::size_t i, T value) {
return set(mutableVertexAttributes(), name, i, value);
}
void updateUniforms() { mutableUniforms().applyUniforms(*this); }
protected:
virtual gfx::VertexAttributeArray& mutableUniforms() = 0;
virtual gfx::VertexAttributeArray& mutableVertexAttributes() = 0;
protected:
util::SimpleIdentity shaderProgramID;
};

View File

@@ -35,10 +35,10 @@ VertexAttribute* VertexAttributeArray::get(const std::string& name) const {
}
VertexAttribute* VertexAttributeArray::add(std::string name, int index,
AttributeDataType dataType, std::size_t count) {
AttributeDataType dataType, int size, std::size_t count) {
const auto result = attrs.insert(std::make_pair(std::move(name), std::unique_ptr<VertexAttribute>()));
if (result.second) {
result.first->second = create(index, dataType, count);
result.first->second = create(index, dataType, size, count);
return result.first->second.get();
} else {
return nullptr;
@@ -46,18 +46,34 @@ VertexAttribute* VertexAttributeArray::add(std::string name, int index,
}
VertexAttribute* VertexAttributeArray::getOrAdd(std::string name, int index,
AttributeDataType dataType, std::size_t count) {
AttributeDataType dataType, int size, std::size_t count) {
//attrs.emplace_back(std::make_unique<VertexAttribute>(dataType, count));
const auto result = attrs.insert(std::make_pair(std::move(name), std::unique_ptr<VertexAttribute>()));
if (result.second) {
result.first->second = create(index, dataType, count);
result.first->second = create(index, dataType, size, count);
} else if (result.first->second->getDataType() != dataType ||
result.first->second->getSize() != size ||
result.first->second->getCount() != count) {
return nullptr;
}
return result.first->second.get();
}
bool VertexAttributeArray::isDirty() const {
for (const auto& kv : attrs) {
if (kv.second->getDirty()) {
return true;
}
}
return false;
}
void VertexAttributeArray::resetDirty() {
for (auto& kv : attrs) {
kv.second->clearDirty();
}
}
void VertexAttributeArray::resolve(const VertexAttributeArray& overrides,
ResolveDelegate delegate) const {
for (auto& kv : attrs) {

View File

@@ -70,28 +70,29 @@ void RendererBackend::initShaders(gfx::ShaderRegistry& shaders) {
gl::Context& glContext = static_cast<gl::Context&>(*context);
// "layout (location = 0) in vec2 a_pos;\nuniform mat4 u_matrix;\n\nvoid main() {\n
// gl_Position = u_matrix * vec4(a_pos, 0, 1);\n}\n", fragment = "uniform vec4 u_color;\nuniform float u_opacity;\n\nvoid main() {\n fragColor = u_color * u_opacity;\n\n#ifdef OVERDRAW_INSPECTOR\n fragColor = vec4(1.0);\n#endif\n}\n"
auto shader = shaders.get<gl::ShaderProgramGL>(shaderName);
if (!shader) {
constexpr auto vert = R"(
#version 300 es
precision highp float;
uniform float a;
uniform int b;
uniform mat4 c;
layout (location = 0) in vec3 pos;
in float d;
in int e;
in mat2 f;
in mat4 g;
in vec2 a_pos;
uniform mat4 u_matrix;
void main() {
gl_Position = vec4(d, float(e), f[0][0], a)/1.0e12; // optimized away if unused
gl_Position = vec4(pos, 1.0);
gl_Position = u_matrix * vec4(a_pos, 0, 1);
switch (gl_VertexID) { // fake it for now
case 0: gl_Position = vec4(-1.0, -1.0, 1.0, 1.0); break;
case 1: gl_Position = vec4( 1.0, -1.0, 1.0, 1.0); break;
case 2: gl_Position = vec4( 1.0, 1.0, 1.0, 1.0); break;
case 3: gl_Position = vec4(-1.0, 1.0, 1.0, 1.0); break;
}
gl_Position = u_matrix * vec4(gl_Position.xy * 8192.0, 0, 1);
//gl_Position = u_matrix * vec4(a_pos, 0, 1);
})";
constexpr auto frag = R"(
#version 300 es
@@ -106,16 +107,14 @@ void RendererBackend::initShaders(gfx::ShaderRegistry& shaders) {
// Compile
shader = gl::ShaderProgramGL::create(glContext, shaderName, vert, frag);
if (shader) {
// Set uniforms
//shader->setUniform("a", 0, 3.21f);
//shader->setUniform("b", 0, 4.32f);
// Set default values
if (auto *attr = shader->getVertexAttributes().get("d")) {
attr->set(0, 12.3f);
}
if (auto *attr = shader->getVertexAttributes().get("e")) {
attr->set(0, 123);
}
if (auto *attr = shader->getVertexAttributes().get("f")) {
attr->set(0, gfx::VertexAttribute::matf2{ 1.0f, 2.0f, 3.0f, 4.0f });
}
//shader->setAttribute("d", 0, 12.3f);
//shader->setAttribute("e", 0, 123);
//shader->setAttribute("f", 0, gfx::VertexAttribute::matf2{ 1.0f, 2.0f, 3.0f, 4.0f });
// Add to the registry
if (!shaders.registerShader(shader, shaderName)) {

View File

@@ -1,6 +1,8 @@
#include <mbgl/gl/vertex_attribute_gl.hpp>
#include <mbgl/gl/defines.hpp>
#include <mbgl/platform/gl_functions.hpp>
#include <mbgl/shaders/gl/shader_program_gl.hpp>
#include <cstring>
@@ -69,6 +71,43 @@ const std::vector<uint8_t>& VertexAttributeGL::getRaw() const {
return rawData;
}
struct ApplyUniform {
GLint location;
template <typename T> void operator()(const T&);
template <> void operator()(const std::int32_t&) { }
template <> void operator()(const gfx::VertexAttribute::int2&) { }
template <> void operator()(const gfx::VertexAttribute::int3&) { }
template <> void operator()(const gfx::VertexAttribute::int4&) { }
template <> void operator()(const float& ) { }
template <> void operator()(const gfx::VertexAttribute::float2& ) { }
template <> void operator()(const gfx::VertexAttribute::float3& ) { }
template <> void operator()(const gfx::VertexAttribute::float4& ) { }
template <> void operator()(const gfx::VertexAttribute::matf3& ) { }
template <> void operator()(const gfx::VertexAttribute::matf4& value) {
MBGL_CHECK_ERROR(glUniformMatrix4fv(location, 1, GL_FALSE, &value[0]));
}
};
void VertexAttributeArrayGL::applyUniforms(const gfx::ShaderProgramBase& shader) {
const auto& glShader = static_cast<const ShaderProgramGL&>(shader);
const auto program = glShader.getGLProgramID();
for (auto& kv : attrs) {
const auto& name = kv.first;
auto& uniform = kv.second;
if (uniform->getDirty()) {
if (uniform->getIndex() < 0) {
const auto index = MBGL_CHECK_ERROR(glGetUniformLocation(program, name.c_str()));
uniform->setIndex(index);
}
std::visit(ApplyUniform { uniform->getIndex() }, uniform->get(0));
uniform->clearDirty();
}
}
}
} // namespace gfx
} // namespace mbgl

View File

@@ -16,6 +16,7 @@
#include <mbgl/renderer/render_static_data.hpp>
#include <mbgl/renderer/render_tree.hpp>
#include <mbgl/shaders/gl/shader_program_gl.hpp>
#include <mbgl/util/convert.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/logging.hpp>
@@ -111,9 +112,16 @@ void Renderer::Impl::render(const RenderTree& renderTree) {
tweaker->execute(drawable, parameters);
}
// Generate a vertex array object for the drawable state, if necessary
auto& drawableGL = static_cast<gl::DrawableGL&>(drawable);
const auto& shader = drawable.getShader();
auto& shader = drawable.getShader();
// Where should this happen?
const mat4 matrix = parameters.matrixForTile({ 0, 0, 0 }); //tileID.toUnwrapped());
shader->setUniform("u_matrix", 0, util::convert<float>(matrix));
shader->updateUniforms();
// Generate a vertex array object for the drawable state, if necessary
if (shader && !drawableGL.getVertexArray().isValid()) {
const auto usage = gfx::BufferUsageType::StaticDraw;

View File

@@ -19,7 +19,7 @@ static gfx::AttributeDataType mapType(platform::GLenum attrType) {
case GL_FLOAT_VEC4: return T::Float4;
case GL_FLOAT_MAT2: return T::Float4; // does that work ?
case GL_FLOAT_MAT3:
case GL_FLOAT_MAT4: return T::Invalid;
case GL_FLOAT_MAT4: return T::Float4;
case GL_INT: return T::Int;
case GL_INT_VEC2: return T::Int2;
case GL_INT_VEC3: return T::Int3;
@@ -52,24 +52,49 @@ static gfx::AttributeDataType mapType(platform::GLenum attrType) {
}
}
// No `AttributeDataType` for 4x4, so we have to account for it separately...
static int mapCount(const platform::GLenum attrType) {
switch (attrType) {
case GL_FLOAT_MAT3: return 3;
case GL_FLOAT_MAT4: return 4;
default: return 1;
}
}
ShaderProgramGL::ShaderProgramGL(UniqueProgram&& glProgram_)
: ShaderProgramBase(),
glProgram(std::move(glProgram_)) {
}
ShaderProgramGL::ShaderProgramGL(UniqueProgram&& glProgram_,
VertexAttributeArrayGL&& attributes)
ShaderProgramGL::ShaderProgramGL(UniqueProgram&& program,
VertexAttributeArrayGL&& uniforms_,
VertexAttributeArrayGL&& attributes_)
: ShaderProgramBase(),
glProgram(std::move(glProgram_)),
vertexAttributes(std::move(attributes)) {
glProgram(std::move(program)),
uniforms(std::move(uniforms_)),
vertexAttributes(std::move(attributes_)) {
}
ShaderProgramGL::ShaderProgramGL(ShaderProgramGL&& other)
: ShaderProgramBase(std::forward<ShaderProgramBase&&>(other)),
glProgram(std::move(other.glProgram)),
uniforms(std::move(other.uniforms)),
vertexAttributes(std::move(other.vertexAttributes)) {
}
using namespace platform;
static void addAttr(VertexAttributeArrayGL& attrs, const char* name, GLint index,
GLsizei length, GLint count, GLenum glType) {
const auto elementType = mapType(glType);
const auto elementCount = mapCount(glType); // number of `elementType`, hopefully temporary
if (elementType != gfx::AttributeDataType::Invalid && length > 0) {
if (auto newAttr = attrs.add(name, index, elementType, elementCount, count)) {
static_cast<VertexAttributeGL*>(newAttr)->setGLType(glType);
}
}
}
std::shared_ptr<ShaderProgramGL> ShaderProgramGL::create(
Context& context,
std::string_view /*name*/,
@@ -83,14 +108,14 @@ std::shared_ptr<ShaderProgramGL> ShaderProgramGL::create(
auto fragProg = context.createShader(ShaderType::Fragment, std::initializer_list<const char*>{fragmentSource.data()});
auto program = context.createProgram(vertProg, fragProg, firstAttrib);
using namespace platform;
// ES3
// GLES3.1
//GLint numAttribs;
//glGetProgramInterfaceiv(program, GL_PROGRAM_INPUT, GL_ACTIVE_RESOURCES, &numAttribs);
GLint count;
GLint maxLength;
VertexAttributeArrayGL uniforms;
GLint count = 0;
GLint maxLength = 0;
MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &count));
MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLength));
@@ -98,12 +123,14 @@ std::shared_ptr<ShaderProgramGL> ShaderProgramGL::create(
for (GLint index = 0; index < count; ++index) {
GLsizei length = 0;
GLint size = 0;
GLenum type = 0;
MBGL_CHECK_ERROR(glGetActiveUniform(program, index, maxLength, &length, &size, &type, name.get()));
GLenum glType = 0;
MBGL_CHECK_ERROR(glGetActiveUniform(program, index, maxLength, &length, &size, &glType, name.get()));
addAttr(uniforms, name.get(), index, length, size, glType);
}
VertexAttributeArrayGL attrs;
count = 0;
MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &count));
MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength));
for (GLint index = 0; index < count; ++index) {
@@ -111,15 +138,10 @@ std::shared_ptr<ShaderProgramGL> ShaderProgramGL::create(
GLint size = 0; // "size of the attribute variable, in units of the type returned in type"
GLenum glType = 0;
MBGL_CHECK_ERROR(glGetActiveAttrib(program, index, maxLength, &length, &size, &glType, name.get()));
const auto elementType = mapType(glType);
if (elementType != gfx::AttributeDataType::Invalid && length > 0) {
if (auto newAttr = attrs.add(name.get(), index, elementType, size)) {
static_cast<VertexAttributeGL*>(newAttr)->setGLType(glType);
}
}
addAttr(attrs, name.get(), index, length, size, glType);
}
return std::make_shared<ShaderProgramGL>(std::move(program), std::move(attrs));
return std::make_shared<ShaderProgramGL>(std::move(program), std::move(uniforms), std::move(attrs));
}
} // namespace gl