summaryrefslogtreecommitdiff
path: root/src/mbgl/programs
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2017-06-05 12:15:27 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2017-06-13 12:21:50 -0700
commit9d276f3b0f0e5f1b5089a6cd727927361e6634ac (patch)
treebbf0c446055126f060e8ee759b7b71281dea8f80 /src/mbgl/programs
parent18b50f51b4fa14b9f2fe6f865dc47490b22e74c6 (diff)
downloadqtlocation-mapboxgl-9d276f3b0f0e5f1b5089a6cd727927361e6634ac.tar.gz
[core] Dynamic program compilation for data-driven properties
Diffstat (limited to 'src/mbgl/programs')
-rw-r--r--src/mbgl/programs/program.hpp35
-rw-r--r--src/mbgl/programs/program_parameters.cpp30
-rw-r--r--src/mbgl/programs/program_parameters.hpp15
-rw-r--r--src/mbgl/programs/programs.hpp26
-rw-r--r--src/mbgl/programs/symbol_program.hpp3
5 files changed, 81 insertions, 28 deletions
diff --git a/src/mbgl/programs/program.hpp b/src/mbgl/programs/program.hpp
index 3351f5eeae..36bbdf349e 100644
--- a/src/mbgl/programs/program.hpp
+++ b/src/mbgl/programs/program.hpp
@@ -9,18 +9,21 @@
#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 PaintProperties>
+ 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>;
@@ -71,4 +74,34 @@ public:
}
};
+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::PossiblyEvaluated& 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
diff --git a/src/mbgl/programs/program_parameters.cpp b/src/mbgl/programs/program_parameters.cpp
index f9f680ac1e..e76ec4be71 100644
--- a/src/mbgl/programs/program_parameters.cpp
+++ b/src/mbgl/programs/program_parameters.cpp
@@ -7,7 +7,7 @@ namespace mbgl {
ProgramParameters::ProgramParameters(const float pixelRatio,
const bool overdraw,
- std::string cacheDir_)
+ optional<std::string> cacheDir_)
: defines([&] {
std::ostringstream ss;
ss.imbue(std::locale("C"));
@@ -18,15 +18,31 @@ ProgramParameters::ProgramParameters(const float pixelRatio,
}
return ss.str();
}()),
- hash(std::hash<std::string>()(defines)),
cacheDir(std::move(cacheDir_)) {
}
-std::string ProgramParameters::cachePath(const char* name) const {
- std::ostringstream ss;
- ss << cacheDir << "/com.mapbox.gl.shader." << name << "." << std::setfill('0')
- << std::setw(sizeof(size_t) * 2) << std::hex << hash << ".pbf";
- return ss.str();
+const std::string& ProgramParameters::getDefines() const {
+ return defines;
+}
+
+optional<std::string> ProgramParameters::cachePath(const char* name) const {
+ if (!cacheDir) {
+ return {};
+ } else {
+ std::ostringstream ss;
+ ss << *cacheDir << "/com.mapbox.gl.shader." << name << "." << std::setfill('0')
+ << std::setw(sizeof(size_t) * 2) << std::hex << std::hash<std::string>()(defines) << ".pbf";
+ return ss.str();
+ }
+}
+
+ProgramParameters ProgramParameters::withAdditionalDefines(const std::vector<std::string>& additionalDefines) const {
+ ProgramParameters result(*this);
+ for (const auto& define : additionalDefines) {
+ result.defines += define;
+ result.defines += "\n";
+ }
+ return result;
}
} // namespace mbgl
diff --git a/src/mbgl/programs/program_parameters.hpp b/src/mbgl/programs/program_parameters.hpp
index d286ff57d1..e94e61c217 100644
--- a/src/mbgl/programs/program_parameters.hpp
+++ b/src/mbgl/programs/program_parameters.hpp
@@ -1,21 +1,24 @@
#pragma once
+#include <mbgl/util/optional.hpp>
+
#include <string>
-#include <utility>
+#include <vector>
namespace mbgl {
class ProgramParameters {
public:
- ProgramParameters(float pixelRatio, bool overdraw, std::string cacheDir);
+ ProgramParameters(float pixelRatio, bool overdraw, optional<std::string> cacheDir);
- const std::string defines;
+ const std::string& getDefines() const;
+ optional<std::string> cachePath(const char* name) const;
- std::string cachePath(const char* name) const;
+ ProgramParameters withAdditionalDefines(const std::vector<std::string>& defines) const;
private:
- const std::size_t hash;
- const std::string cacheDir;
+ std::string defines;
+ optional<std::string> cacheDir;
};
} // namespace mbgl
diff --git a/src/mbgl/programs/programs.hpp b/src/mbgl/programs/programs.hpp
index b58d2e3bee..37ced32745 100644
--- a/src/mbgl/programs/programs.hpp
+++ b/src/mbgl/programs/programs.hpp
@@ -35,21 +35,21 @@ public:
collisionBox(context, programParameters) {
}
- CircleProgram circle;
+ ProgramMap<CircleProgram> circle;
ExtrusionTextureProgram extrusionTexture;
- FillProgram fill;
- FillExtrusionProgram fillExtrusion;
- FillExtrusionPatternProgram fillExtrusionPattern;
- FillPatternProgram fillPattern;
- FillOutlineProgram fillOutline;
- FillOutlinePatternProgram fillOutlinePattern;
- LineProgram line;
- LineSDFProgram lineSDF;
- LinePatternProgram linePattern;
+ ProgramMap<FillProgram> fill;
+ ProgramMap<FillExtrusionProgram> fillExtrusion;
+ ProgramMap<FillExtrusionPatternProgram> fillExtrusionPattern;
+ ProgramMap<FillPatternProgram> fillPattern;
+ ProgramMap<FillOutlineProgram> fillOutline;
+ ProgramMap<FillOutlinePatternProgram> fillOutlinePattern;
+ ProgramMap<LineProgram> line;
+ ProgramMap<LineSDFProgram> lineSDF;
+ ProgramMap<LinePatternProgram> linePattern;
RasterProgram raster;
- SymbolIconProgram symbolIcon;
- SymbolSDFIconProgram symbolIconSDF;
- SymbolSDFTextProgram symbolGlyph;
+ ProgramMap<SymbolIconProgram> symbolIcon;
+ ProgramMap<SymbolSDFIconProgram> symbolIconSDF;
+ ProgramMap<SymbolSDFTextProgram> symbolGlyph;
DebugProgram debug;
CollisionBoxProgram collisionBox;
diff --git a/src/mbgl/programs/symbol_program.hpp b/src/mbgl/programs/symbol_program.hpp
index 087d3a4567..358fa08ecd 100644
--- a/src/mbgl/programs/symbol_program.hpp
+++ b/src/mbgl/programs/symbol_program.hpp
@@ -324,7 +324,7 @@ template <class Shaders,
class Primitive,
class LayoutAttrs,
class Uniforms,
- class PaintProperties>
+ class PaintProps>
class SymbolProgram {
public:
using LayoutAttributes = LayoutAttrs;
@@ -332,6 +332,7 @@ public:
using LayoutAndSizeAttributes = gl::ConcatenateAttributes<LayoutAttributes, SymbolSizeAttributes>;
+ using PaintProperties = PaintProps;
using PaintPropertyBinders = typename PaintProperties::Binders;
using PaintAttributes = typename PaintPropertyBinders::Attributes;
using Attributes = gl::ConcatenateAttributes<LayoutAndSizeAttributes, PaintAttributes>;