summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2017-06-13 11:47:39 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2017-06-14 11:13:43 -0700
commit212ae5273dc3930a035b51c603c540b4f02fa777 (patch)
tree64e4c1b88515035fe379cb55ca0f2fcf724163e9 /src
parenta64828218a3934c036f7fb256b6799723c30830a (diff)
downloadqtlocation-mapboxgl-212ae5273dc3930a035b51c603c540b4f02fa777.tar.gz
[core] cleanup ProgramParameters
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/gl/program.hpp4
-rw-r--r--src/mbgl/programs/program_parameters.cpp32
-rw-r--r--src/mbgl/programs/program_parameters.hpp15
-rw-r--r--src/mbgl/programs/programs.hpp4
-rw-r--r--src/mbgl/shaders/shaders.cpp22
-rw-r--r--src/mbgl/shaders/shaders.hpp1
6 files changed, 45 insertions, 33 deletions
diff --git a/src/mbgl/gl/program.hpp b/src/mbgl/gl/program.hpp
index 1b23abe2b1..c36543839c 100644
--- a/src/mbgl/gl/program.hpp
+++ b/src/mbgl/gl/program.hpp
@@ -55,11 +55,11 @@ public:
shaders::vertexSource(programParameters, vertexSource_);
const std::string fragmentSource =
shaders::fragmentSource(programParameters, fragmentSource_);
- const std::string cachePath =
- shaders::programCachePath(programParameters, name);
const std::string identifier =
shaders::programIdentifier(vertexSource, fragmentSource_);
+ const std::string cachePath = programParameters.cachePath(name);
+
try {
if (auto cachedBinaryProgram = util::readFile(cachePath)) {
const BinaryProgram binaryProgram(std::move(*cachedBinaryProgram));
diff --git a/src/mbgl/programs/program_parameters.cpp b/src/mbgl/programs/program_parameters.cpp
new file mode 100644
index 0000000000..f9f680ac1e
--- /dev/null
+++ b/src/mbgl/programs/program_parameters.cpp
@@ -0,0 +1,32 @@
+#include <mbgl/programs/program_parameters.hpp>
+
+#include <iomanip>
+#include <sstream>
+
+namespace mbgl {
+
+ProgramParameters::ProgramParameters(const float pixelRatio,
+ const bool overdraw,
+ std::string cacheDir_)
+ : defines([&] {
+ std::ostringstream ss;
+ ss.imbue(std::locale("C"));
+ ss.setf(std::ios_base::showpoint);
+ ss << "#define DEVICE_PIXEL_RATIO " << pixelRatio << std::endl;
+ if (overdraw) {
+ ss << "#define OVERDRAW_INSPECTOR" << std::endl;
+ }
+ 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();
+}
+
+} // namespace mbgl
diff --git a/src/mbgl/programs/program_parameters.hpp b/src/mbgl/programs/program_parameters.hpp
index b91b41f358..420658d089 100644
--- a/src/mbgl/programs/program_parameters.hpp
+++ b/src/mbgl/programs/program_parameters.hpp
@@ -6,16 +6,15 @@ namespace mbgl {
class ProgramParameters {
public:
- ProgramParameters(float pixelRatio_ = 1.0,
- bool overdraw_ = false,
- const std::string& cacheDir_ = "")
- : pixelRatio(pixelRatio_), overdraw(overdraw_), cacheDir(cacheDir_) {
- }
+ ProgramParameters(float pixelRatio, bool overdraw, std::string cacheDir);
- const float pixelRatio;
- const bool overdraw;
+ const std::string defines;
+
+ std::string cachePath(const char* name) const;
+
+private:
+ const std::size_t hash;
const std::string cacheDir;
};
} // namespace mbgl
-
diff --git a/src/mbgl/programs/programs.hpp b/src/mbgl/programs/programs.hpp
index ff6b1cd505..b58d2e3bee 100644
--- a/src/mbgl/programs/programs.hpp
+++ b/src/mbgl/programs/programs.hpp
@@ -31,8 +31,8 @@ public:
symbolIcon(context, programParameters),
symbolIconSDF(context, programParameters),
symbolGlyph(context, programParameters),
- debug(context, ProgramParameters(programParameters.pixelRatio, false, programParameters.cacheDir)),
- collisionBox(context, ProgramParameters(programParameters.pixelRatio, false, programParameters.cacheDir)) {
+ debug(context, programParameters),
+ collisionBox(context, programParameters) {
}
CircleProgram circle;
diff --git a/src/mbgl/shaders/shaders.cpp b/src/mbgl/shaders/shaders.cpp
index 03d796edba..93e273f985 100644
--- a/src/mbgl/shaders/shaders.cpp
+++ b/src/mbgl/shaders/shaders.cpp
@@ -9,30 +9,12 @@
namespace mbgl {
namespace shaders {
-static std::string pixelRatioDefine(const ProgramParameters& parameters) {
- std::ostringstream pixelRatioSS;
- pixelRatioSS.imbue(std::locale("C"));
- pixelRatioSS.setf(std::ios_base::showpoint);
- pixelRatioSS << parameters.pixelRatio;
- return std::string("#define DEVICE_PIXEL_RATIO ") + pixelRatioSS.str() + "\n";
-}
-
std::string fragmentSource(const ProgramParameters& parameters, const char* fragmentSource) {
- std::string source = pixelRatioDefine(parameters) + fragmentPrelude + fragmentSource;
- if (parameters.overdraw) {
- assert(source.find("#ifdef OVERDRAW_INSPECTOR") != std::string::npos);
- source.replace(source.find_first_of('\n'), 1, "\n#define OVERDRAW_INSPECTOR\n");
- }
- return source;
+ return parameters.defines + fragmentPrelude + fragmentSource;
}
std::string vertexSource(const ProgramParameters& parameters, const char* vertexSource) {
- return pixelRatioDefine(parameters) + vertexPrelude + vertexSource;
-}
-
-std::string programCachePath(const ProgramParameters& parameters, const char* name) {
- return parameters.cacheDir + "/com.mapbox.gl.shader." + name +
- (parameters.overdraw ? ".overdraw.pbf" : ".pbf");
+ return parameters.defines + vertexPrelude + vertexSource;
}
std::string programIdentifier(const std::string& vertexSource, const std::string& fragmentSource) {
diff --git a/src/mbgl/shaders/shaders.hpp b/src/mbgl/shaders/shaders.hpp
index 126c64bb9e..513d37fa6f 100644
--- a/src/mbgl/shaders/shaders.hpp
+++ b/src/mbgl/shaders/shaders.hpp
@@ -10,7 +10,6 @@ namespace shaders {
std::string fragmentSource(const ProgramParameters&, const char* fragmentSource);
std::string vertexSource(const ProgramParameters&, const char* vertexSource);
-std::string programCachePath(const ProgramParameters&, const char* name);
std::string programIdentifier(const std::string& vertexSource, const std::string& fragmentSource);
} // namespace shaders