Refactor shaders for drawables (#1132)

This commit is contained in:
mwilsnd
2023-05-12 11:29:34 -04:00
committed by Bart Louwers
parent 20a30e41b7
commit 57c165e604
46 changed files with 242 additions and 111 deletions

View File

@@ -927,7 +927,8 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/shader_manifest.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/prelude.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/background.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/background_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_background.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_fill.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/background_pattern.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/circle.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/clipping_mask.hpp

View File

@@ -6,6 +6,9 @@
#include <mutex>
namespace mbgl {
class ProgramParameters;
namespace gfx {
class BackendScope;
@@ -42,7 +45,7 @@ public:
virtual Renderable& getDefaultRenderable() = 0;
/// One-time shader initialization
virtual void initShaders(gfx::ShaderRegistry&) = 0;
virtual void initShaders(gfx::ShaderRegistry&, const ProgramParameters&) = 0;
protected:
virtual std::unique_ptr<Context> createContext() = 0;

View File

@@ -6,6 +6,9 @@
#include <mbgl/util/util.hpp>
namespace mbgl {
class ProgramParameters;
namespace gl {
using ProcAddress = void (*)();
@@ -20,7 +23,7 @@ public:
virtual void updateAssumedState() = 0;
/// One-time shader initialization
void initShaders(gfx::ShaderRegistry&) override;
void initShaders(gfx::ShaderRegistry&, const ProgramParameters& programParameters) override;
protected:
std::unique_ptr<gfx::Context> createContext() override;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::BackgroundProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "BackgroundProgram";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
uniform mat4 u_matrix;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::BackgroundPatternProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "BackgroundPatternProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
uniform vec2 u_pattern_size_a;
uniform vec2 u_pattern_size_b;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::CircleProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "CircleProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
uniform bool u_scale_with_map;
uniform bool u_pitch_with_map;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::ClippingMaskProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "ClippingMaskProgram";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
uniform mat4 u_matrix;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::CollisionBoxProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "CollisionBoxProgram";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
layout (location = 1) in vec2 a_anchor_pos;
layout (location = 2) in vec2 a_extrude;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::CollisionCircleProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "CollisionCircleProgram";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
layout (location = 1) in vec2 a_anchor_pos;
layout (location = 2) in vec2 a_extrude;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::DebugProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "DebugProgram";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
out vec2 v_uv;

View File

@@ -0,0 +1,37 @@
// Generated code, do not modify this file!
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
namespace mbgl {
namespace shaders {
template <>
struct ShaderSource<BuiltIn::BackgroundShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "BackgroundShader";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
layout (std140) uniform DrawableUBO {
mat4 u_matrix;
};
void main() {
gl_Position = u_matrix * vec4(a_pos, 0, 1);
}
)";
static constexpr const char* fragment = R"(layout (std140) uniform BackgroundLayerUBO {
vec4 u_color;
float u_opacity;
};
void main() {
fragColor = u_color * u_opacity;
#ifdef OVERDRAW_INSPECTOR
fragColor = vec4(1.0);
#endif
}
)";
};
} // namespace shaders
} // namespace mbgl

View File

@@ -1,3 +1,6 @@
// Generated code, do not modify this file!
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -5,12 +8,9 @@ namespace mbgl {
namespace shaders {
template <>
struct ShaderSource<BuiltIn::BackgroundProgramUBO, gfx::Backend::Type::OpenGL> {
static constexpr const char* vertex = R"(#version 300 es
precision highp float;
layout (location = 0) in vec2 a_pos;
struct ShaderSource<BuiltIn::FillShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "FillShader";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
layout (std140) uniform DrawableUBO {
mat4 u_matrix;
};
@@ -19,19 +19,13 @@ void main() {
gl_Position = u_matrix * vec4(a_pos, 0, 1);
}
)";
static constexpr const char* fragment = R"(#version 300 es
precision highp float;
layout (std140) uniform BackgroundLayerUBO {
static constexpr const char* fragment = R"(layout (std140) uniform BackgroundLayerUBO {
vec4 u_color;
float u_opacity;
};
out vec4 fragColor;
void main() {
fragColor = u_color * u_opacity;
#ifdef OVERDRAW_INSPECTOR
fragColor = vec4(1.0);
#endif

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::FillProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "FillProgram";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
uniform mat4 u_matrix;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::FillExtrusionProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "FillExtrusionProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
uniform vec3 u_lightcolor;
uniform lowp vec3 u_lightpos;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::FillExtrusionPatternProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "FillExtrusionPatternProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
uniform vec2 u_pixel_coord_upper;
uniform vec2 u_pixel_coord_lower;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::FillOutlineProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "FillOutlineProgram";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
uniform mat4 u_matrix;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::FillOutlinePatternProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "FillOutlinePatternProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
uniform vec2 u_world;
uniform vec2 u_pixel_coord_upper;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::FillPatternProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "FillPatternProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
uniform vec2 u_pixel_coord_upper;
uniform vec2 u_pixel_coord_lower;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::HeatmapProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "HeatmapProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
uniform float u_extrude_scale;
uniform float u_opacity;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::HeatmapTextureProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "HeatmapTextureProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
uniform vec2 u_world;
layout (location = 0) in vec2 a_pos;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::HillshadeProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "HillshadeProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
layout (location = 0) in vec2 a_pos;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::HillshadePrepareProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "HillshadePrepareProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
uniform vec2 u_dimension;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::LineProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "LineProgram";
static constexpr const char* vertex = R"(// floor(127 / 2) == 63.0
// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is
// stored in a byte (-128..127). we scale regular normals up to length 63, but

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::LineGradientProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "LineGradientProgram";
static constexpr const char* vertex = R"(
// the attribute conveying progress along a line is scaled to [0, 2^15)
#define MAX_LINE_DISTANCE 32767.0

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::LinePatternProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "LinePatternProgram";
static constexpr const char* vertex = R"(// floor(127 / 2) == 63.0
// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is
// stored in a byte (-128..127). we scale regular normals up to length 63, but

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::LineSDFProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "LineSDFProgram";
static constexpr const char* vertex = R"(// floor(127 / 2) == 63.0
// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is
// stored in a byte (-128..127). we scale regular normals up to length 63, but

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::Prelude, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "Prelude";
static constexpr const char* vertex = R"(#ifdef GL_ES
precision highp float;
#else

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::RasterProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "RasterProgram";
static constexpr const char* vertex = R"(uniform mat4 u_matrix;
uniform vec2 u_tl_parent;
uniform float u_scale_parent;

View File

@@ -6,6 +6,9 @@
#include <mbgl/shaders/shader_program_base.hpp>
namespace mbgl {
class ProgramParameters;
namespace gl {
class ShaderProgramGL final : public gfx::ShaderProgramBase {
@@ -19,9 +22,11 @@ public:
const std::string_view typeName() const noexcept override { return Name; }
static std::shared_ptr<ShaderProgramGL> create(Context&,
std::string_view name,
std::string_view vertexSource,
std::string_view fragmentSource) noexcept(false);
const ProgramParameters& programParameters,
const std::string& name,
const std::string& vertexSource,
const std::string& fragmentSource,
const std::string& additionalDefines = "") noexcept(false);
const gfx::UniformBlockArray& getUniformBlocks() const override { return uniformBlocks; }

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::SymbolIconProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "SymbolIconProgram";
static constexpr const char* vertex = R"(const float PI = 3.141592653589793;
layout (location = 0) in vec4 a_pos_offset;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::SymbolSDFIconProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "SymbolSDFIconProgram";
static constexpr const char* vertex = R"(const float PI = 3.141592653589793;
layout (location = 0) in vec4 a_pos_offset;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::SymbolSDFTextProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "SymbolSDFTextProgram";
static constexpr const char* vertex = R"(const float PI = 3.141592653589793;
layout (location = 0) in vec4 a_pos_offset;

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
@@ -9,6 +9,7 @@ namespace shaders {
template <>
struct ShaderSource<BuiltIn::SymbolTextAndIconProgram, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "SymbolTextAndIconProgram";
static constexpr const char* vertex = R"(const float PI = 3.141592653589793;
layout (location = 0) in vec4 a_pos_offset;

View File

@@ -1,10 +1,12 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/shaders/shader_source.hpp>
#ifdef MBGL_RENDER_BACKEND_OPENGL
#include <mbgl/shaders/gl/drawable_background.hpp>
#include <mbgl/shaders/gl/drawable_fill.hpp>
#include <mbgl/shaders/gl/prelude.hpp>
#include <mbgl/shaders/gl/background.hpp>
#include <mbgl/shaders/gl/background_pattern.hpp>
@@ -32,6 +34,4 @@
#include <mbgl/shaders/gl/symbol_sdf_text.hpp>
#include <mbgl/shaders/gl/symbol_sdf_icon.hpp>
#include <mbgl/shaders/gl/symbol_text_and_icon.hpp>
#include <mbgl/shaders/gl/background_ubo.hpp>
#endif

View File

@@ -1,5 +1,5 @@
// Generated code, do not modify this file!
// Generated on 2023-04-05T16:25:15.886Z by mwilsnd using shaders/generate_shader_code.js
// Generated on 2023-05-11T18:11:44.911Z by mwilsnd using shaders/generate_shader_code.js
#pragma once
#include <mbgl/gfx/backend.hpp>
@@ -11,6 +11,8 @@ namespace shaders {
/// source code for the desired program and graphics back-end.
enum class BuiltIn {
None,
BackgroundShader,
FillShader,
Prelude,
BackgroundProgram,
BackgroundPatternProgram,
@@ -37,9 +39,7 @@ enum class BuiltIn {
SymbolIconProgram,
SymbolSDFTextProgram,
SymbolSDFIconProgram,
SymbolTextAndIconProgram,
BackgroundProgramUBO
SymbolTextAndIconProgram
};
/// @brief Select shader source based on a program type and a desired
@@ -53,6 +53,7 @@ struct ShaderSource;
/// @brief A specialization of the ShaderSource template for no shader code.
template <>
struct ShaderSource<BuiltIn::None, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "";
static constexpr const char* vertex = "";
static constexpr const char* fragment = "";
};

View File

@@ -0,0 +1,11 @@
layout (std140) uniform BackgroundLayerUBO {
vec4 u_color;
float u_opacity;
};
void main() {
fragColor = u_color * u_opacity;
#ifdef OVERDRAW_INSPECTOR
fragColor = vec4(1.0);
#endif
}

View File

@@ -0,0 +1,8 @@
layout (location = 0) in vec2 a_pos;
layout (std140) uniform DrawableUBO {
mat4 u_matrix;
};
void main() {
gl_Position = u_matrix * vec4(a_pos, 0, 1);
}

View File

@@ -0,0 +1,11 @@
layout (std140) uniform BackgroundLayerUBO {
vec4 u_color;
float u_opacity;
};
void main() {
fragColor = u_color * u_opacity;
#ifdef OVERDRAW_INSPECTOR
fragColor = vec4(1.0);
#endif
}

View File

@@ -0,0 +1,8 @@
layout (location = 0) in vec2 a_pos;
layout (std140) uniform DrawableUBO {
mat4 u_matrix;
};
void main() {
gl_Position = u_matrix * vec4(a_pos, 0, 1);
}

View File

@@ -243,11 +243,6 @@ JSON.parse(fs.readFileSync(path.join(shaderRoot, "manifest.json")))
? pragmaMapConvertOnlyVertexArrays(vertexSource, pragmaMap, attribMap, "vertex")
: pragmaMapConvert(vertexSource, pragmaMap, attribMap, "vertex");
const glRoot = path.join(outputRoot, "gl");
if (!fs.existsSync(glRoot)) {
fs.mkdirSync(glRoot, {recursive: true}); // Ensure target directory is available
}
fs.writeFileSync(
path.join(glRoot, elem.header + ".hpp"),
`${generatedHeader}
@@ -257,8 +252,7 @@ JSON.parse(fs.readFileSync(path.join(shaderRoot, "manifest.json")))
namespace mbgl {
namespace shaders {
template <>
struct ShaderSource<BuiltIn::${elem.name}, gfx::Backend::Type::OpenGL> {
template <> struct ShaderSource<BuiltIn::${elem.name}, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "${elem.name}";
static constexpr const char* vertex = R"(${args.strip ? strip(vert) : vert})";
static constexpr const char* fragment = R"(${args.strip ? strip(frag) : frag})";
@@ -308,8 +302,7 @@ template <BuiltIn T, gfx::Backend::Type>
struct ShaderSource;
/// @brief A specialization of the ShaderSource template for no shader code.
template <>
struct ShaderSource<BuiltIn::None, gfx::Backend::Type::OpenGL> {
template <> struct ShaderSource<BuiltIn::None, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "";
static constexpr const char* vertex = "";
static constexpr const char* fragment = "";

View File

@@ -1,4 +1,19 @@
[
{
"name": "BackgroundShader",
"header": "drawable_background",
"glsl_vert": "drawable.background.vertex.glsl",
"glsl_frag": "drawable.background.fragment.glsl",
"uses_ubos": true
},
{
"name": "FillShader",
"header": "drawable_fill",
"glsl_vert": "drawable.fill.vertex.glsl",
"glsl_frag": "drawable.fill.fragment.glsl",
"uses_ubos": true
},
{
"name": "Prelude",
"header": "prelude",

View File

@@ -66,36 +66,35 @@ void RendererBackend::setScissorTest(bool enabled) {
RendererBackend::~RendererBackend() = default;
void RendererBackend::initShaders(gfx::ShaderRegistry& shaders) {
constexpr auto shaderName = "BackgroundProgramUBO";
/// @brief Register a list of types with a shader registry instance
/// @tparam ...ShaderID Pack of BuiltIn:: shader IDs
/// @param registry A shader registry instance
/// @param glContext The GL context instance
/// @param programParameters ProgramParameters used to initialize each instance
template <shaders::BuiltIn... ShaderID>
void registerTypes(gfx::ShaderRegistry& registry, gl::Context& glContext, const ProgramParameters& programParameters) {
/// The following fold expression will create a shader for every type
/// in the parameter pack and register it with the shader registry.
gl::Context& glContext = static_cast<gl::Context&>(*context);
/// Registration calls are wrapped in a lambda that throws on registration
/// failure, we shouldn't expect registration to faill unless the shader
/// registry instance provided already has conflicting programs present.
(
[&](const std::string& name, const std::string& vert, const std::string& frag) {
using Ty = shaders::ShaderSource<ShaderID, gfx::Backend::Type::OpenGL>;
if (!registry.registerShader(gl::ShaderProgramGL::create(glContext, programParameters, name, vert, frag),
name)) {
throw std::runtime_error("Failed to register " + std::string(Ty::name) + " with shader registry!");
}
}(shaders::ShaderSource<ShaderID, gfx::Backend::Type::OpenGL>::name,
shaders::ShaderSource<ShaderID, gfx::Backend::Type::OpenGL>::vertex,
shaders::ShaderSource<ShaderID, gfx::Backend::Type::OpenGL>::fragment),
...);
}
auto shader = shaders.get<gl::ShaderProgramGL>(shaderName);
if (!shader) {
auto vert = shaders::ShaderSource<shaders::BuiltIn::BackgroundProgramUBO, gfx::Backend::Type::OpenGL>::vertex;
auto frag = shaders::ShaderSource<shaders::BuiltIn::BackgroundProgramUBO, gfx::Backend::Type::OpenGL>::fragment;
try {
// Compile
shader = gl::ShaderProgramGL::create(glContext, shaderName, vert, frag);
if (shader) {
// Set default values
// shader->setAttribute("a_color", gfx::VertexAttribute::matf4{ 1.0f, 1.0f, 1.0f, 1.0f });
// Add to the registry
if (!shaders.registerShader(shader, shaderName)) {
Log::Warning(Event::General, "Shader conflict - " + std::string(shaderName));
return;
}
} else {
Log::Warning(Event::General, "Shader create failed - " + std::string(shaderName));
return;
}
} catch (const std::runtime_error& ex) {
Log::Warning(Event::General, "Shader create exception - " + std::string(ex.what()));
return;
}
}
void RendererBackend::initShaders(gfx::ShaderRegistry& shaders, const ProgramParameters& programParameters) {
registerTypes<shaders::BuiltIn::BackgroundShader, shaders::BuiltIn::FillShader>(
shaders, static_cast<gl::Context&>(*context), programParameters);
}
} // namespace gl

View File

@@ -197,8 +197,6 @@ void RenderBackgroundLayer::prepare(const LayerPrepareParameters& params) {
}
}
constexpr auto shaderName = "BackgroundProgramUBO";
void RenderBackgroundLayer::layerRemoved(UniqueChangeRequestVec& changes) {
// TODO: This isn't happening on style change, so old tile drawables are being left active
@@ -221,7 +219,7 @@ void RenderBackgroundLayer::update(const int32_t layerIndex,
std::unique_lock<std::mutex> guard(mutex);
if (!shader) {
shader = context.getGenericShader(shaders, shaderName);
shader = context.getGenericShader(shaders, "BackgroundDrawable");
}
changes.emplace_back(

View File

@@ -261,8 +261,6 @@ bool RenderFillLayer::queryIntersectsFeature(const GeometryCoordinates& queryGeo
feature.getGeometries());
}
constexpr auto shaderName = "BackgroundProgramUBO";
void RenderFillLayer::layerRemoved(UniqueChangeRequestVec& changes) {
// Remove everything
decltype(tileDrawables) localDrawables;
@@ -283,7 +281,7 @@ void RenderFillLayer::update(const int32_t layerIndex,
std::unique_lock<std::mutex> guard(mutex);
if (!shader) {
shader = context.getGenericShader(shaders, shaderName);
shader = context.getGenericShader(shaders, "FillDrawable");
}
// const auto& evaluated = getEvaluated<FillLayerProperties>(evaluatedProperties);

View File

@@ -53,22 +53,25 @@ void Renderer::Impl::render(const RenderTree& renderTree) {
observer->onWillStartRenderingFrame();
const auto& renderTreeParameters = renderTree.getParameters();
auto& context = backend.getContext();
// Blocks execution until the renderable is available.
backend.getDefaultRenderable().wait();
if (!staticData) {
staticData = std::make_unique<RenderStaticData>(pixelRatio, std::make_unique<gfx::ShaderRegistry>());
// Initialize legacy shader programs
staticData->programs.registerWith(*staticData->shaders);
// Initialize shaders for drawables
const auto programParameters = ProgramParameters{pixelRatio, false};
backend.initShaders(*staticData->shaders, programParameters);
// Notify post-shader registration
observer->onRegisterShaders(*staticData->shaders);
}
staticData->has3D = renderTreeParameters.has3D;
auto& context = backend.getContext();
// Now that the shader registry is set up, do one-time shader init.
// TODO: Both should be done earlier.
backend.initShaders(*staticData->shaders);
// Blocks execution until the renderable is available.
backend.getDefaultRenderable().wait();
PaintParameters parameters{context,
pixelRatio,
backend,

View File

@@ -4,6 +4,8 @@
#include <mbgl/gl/types.hpp>
#include <mbgl/gl/vertex_attribute_gl.hpp>
#include <mbgl/platform/gl_functions.hpp>
#include <mbgl/programs/program_parameters.hpp>
#include <mbgl/shaders/shader_manifest.hpp>
#include <cstring>
#include <utility>
@@ -110,15 +112,29 @@ static void addAttr(
}
std::shared_ptr<ShaderProgramGL> ShaderProgramGL::create(Context& context,
std::string_view /*name*/,
std::string_view vertexSource,
std::string_view fragmentSource) noexcept(false) {
const ProgramParameters& programParameters,
const std::string& /*name*/,
const std::string& vertexSource,
const std::string& fragmentSource,
const std::string& additionalDefines) noexcept(false) {
const auto firstAttrib = "a_pos";
// throws on compile error
auto vertProg = context.createShader(ShaderType::Vertex, std::initializer_list<const char*>{vertexSource.data()});
auto fragProg = context.createShader(ShaderType::Fragment,
std::initializer_list<const char*>{fragmentSource.data()});
auto vertProg = context.createShader(
ShaderType::Vertex,
std::initializer_list<const char*>{
"#version 300 es\n",
programParameters.getDefines().c_str(),
additionalDefines.c_str(),
shaders::ShaderSource<shaders::BuiltIn::Prelude, gfx::Backend::Type::OpenGL>::vertex,
vertexSource.c_str()});
auto fragProg = context.createShader(
ShaderType::Fragment,
{"#version 300 es\n",
programParameters.getDefines().c_str(),
additionalDefines.c_str(),
shaders::ShaderSource<shaders::BuiltIn::Prelude, gfx::Backend::Type::OpenGL>::fragment,
fragmentSource.c_str()});
auto program = context.createProgram(vertProg, fragProg, firstAttrib);
// GLES3.1