Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 1db4642

Browse files
committed
[core] cleanup ProgramParameters
1 parent 3c48c56 commit 1db4642

File tree

7 files changed

+46
-33
lines changed

7 files changed

+46
-33
lines changed

cmake/core-files.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ set(MBGL_CORE_FILES
148148
src/mbgl/programs/line_program.cpp
149149
src/mbgl/programs/line_program.hpp
150150
src/mbgl/programs/program.hpp
151+
src/mbgl/programs/program_parameters.cpp
151152
src/mbgl/programs/program_parameters.hpp
152153
src/mbgl/programs/programs.hpp
153154
src/mbgl/programs/raster_program.cpp

src/mbgl/gl/program.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ class Program {
5555
shaders::vertexSource(programParameters, vertexSource_);
5656
const std::string fragmentSource =
5757
shaders::fragmentSource(programParameters, fragmentSource_);
58-
const std::string cachePath =
59-
shaders::programCachePath(programParameters, name);
6058
const std::string identifier =
6159
shaders::programIdentifier(vertexSource, fragmentSource_);
6260

61+
const std::string cachePath = programParameters.cachePath(name);
62+
6363
try {
6464
if (auto cachedBinaryProgram = util::readFile(cachePath)) {
6565
const BinaryProgram binaryProgram(std::move(*cachedBinaryProgram));
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <mbgl/programs/program_parameters.hpp>
2+
3+
#include <iomanip>
4+
#include <sstream>
5+
6+
namespace mbgl {
7+
8+
ProgramParameters::ProgramParameters(const float pixelRatio,
9+
const bool overdraw,
10+
std::string cacheDir_)
11+
: defines([&] {
12+
std::ostringstream ss;
13+
ss.imbue(std::locale("C"));
14+
ss.setf(std::ios_base::showpoint);
15+
ss << "#define DEVICE_PIXEL_RATIO " << pixelRatio << std::endl;
16+
if (overdraw) {
17+
ss << "#define OVERDRAW_INSPECTOR" << std::endl;
18+
}
19+
return ss.str();
20+
}()),
21+
hash(std::hash<std::string>()(defines)),
22+
cacheDir(std::move(cacheDir_)) {
23+
}
24+
25+
std::string ProgramParameters::cachePath(const char* name) const {
26+
std::ostringstream ss;
27+
ss << cacheDir << "/com.mapbox.gl.shader." << name << "." << std::setfill('0')
28+
<< std::setw(sizeof(size_t) * 2) << std::hex << hash << ".pbf";
29+
return ss.str();
30+
}
31+
32+
} // namespace mbgl

src/mbgl/programs/program_parameters.hpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ namespace mbgl {
77

88
class ProgramParameters {
99
public:
10-
ProgramParameters(float pixelRatio_ = 1.0,
11-
bool overdraw_ = false,
12-
std::string cacheDir_ = "")
13-
: pixelRatio(pixelRatio_), overdraw(overdraw_), cacheDir(std::move(cacheDir_)) {
14-
}
10+
ProgramParameters(float pixelRatio, bool overdraw, std::string cacheDir);
1511

16-
const float pixelRatio;
17-
const bool overdraw;
12+
const std::string defines;
13+
14+
std::string cachePath(const char* name) const;
15+
16+
private:
17+
const std::size_t hash;
1818
const std::string cacheDir;
1919
};
2020

2121
} // namespace mbgl
22-

src/mbgl/programs/programs.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class Programs {
3131
symbolIcon(context, programParameters),
3232
symbolIconSDF(context, programParameters),
3333
symbolGlyph(context, programParameters),
34-
debug(context, ProgramParameters(programParameters.pixelRatio, false, programParameters.cacheDir)),
35-
collisionBox(context, ProgramParameters(programParameters.pixelRatio, false, programParameters.cacheDir)) {
34+
debug(context, programParameters),
35+
collisionBox(context, programParameters) {
3636
}
3737

3838
CircleProgram circle;

src/mbgl/shaders/shaders.cpp

+2-20
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,12 @@
99
namespace mbgl {
1010
namespace shaders {
1111

12-
static std::string pixelRatioDefine(const ProgramParameters& parameters) {
13-
std::ostringstream pixelRatioSS;
14-
pixelRatioSS.imbue(std::locale("C"));
15-
pixelRatioSS.setf(std::ios_base::showpoint);
16-
pixelRatioSS << parameters.pixelRatio;
17-
return std::string("#define DEVICE_PIXEL_RATIO ") + pixelRatioSS.str() + "\n";
18-
}
19-
2012
std::string fragmentSource(const ProgramParameters& parameters, const char* fragmentSource) {
21-
std::string source = pixelRatioDefine(parameters) + fragmentPrelude + fragmentSource;
22-
if (parameters.overdraw) {
23-
assert(source.find("#ifdef OVERDRAW_INSPECTOR") != std::string::npos);
24-
source.replace(source.find_first_of('\n'), 1, "\n#define OVERDRAW_INSPECTOR\n");
25-
}
26-
return source;
13+
return parameters.defines + fragmentPrelude + fragmentSource;
2714
}
2815

2916
std::string vertexSource(const ProgramParameters& parameters, const char* vertexSource) {
30-
return pixelRatioDefine(parameters) + vertexPrelude + vertexSource;
31-
}
32-
33-
std::string programCachePath(const ProgramParameters& parameters, const char* name) {
34-
return parameters.cacheDir + "/com.mapbox.gl.shader." + name +
35-
(parameters.overdraw ? ".overdraw.pbf" : ".pbf");
17+
return parameters.defines + vertexPrelude + vertexSource;
3618
}
3719

3820
std::string programIdentifier(const std::string& vertexSource, const std::string& fragmentSource) {

src/mbgl/shaders/shaders.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace shaders {
1010

1111
std::string fragmentSource(const ProgramParameters&, const char* fragmentSource);
1212
std::string vertexSource(const ProgramParameters&, const char* vertexSource);
13-
std::string programCachePath(const ProgramParameters&, const char* name);
1413
std::string programIdentifier(const std::string& vertexSource, const std::string& fragmentSource);
1514

1615
} // namespace shaders

0 commit comments

Comments
 (0)