summaryrefslogtreecommitdiff
path: root/src/mbgl/shaders/shaders.cpp
blob: 03d796edbae46c5a9e609df7294355b82295810d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <mbgl/shaders/shaders.hpp>
#include <mbgl/shaders/preludes.hpp>
#include <mbgl/programs/program_parameters.hpp>

#include <cassert>
#include <sstream>
#include <iomanip>

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;
}

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");
}

std::string programIdentifier(const std::string& vertexSource, const std::string& fragmentSource) {
    std::ostringstream ss;
    ss << std::setfill('0') << std::setw(sizeof(size_t) * 2) << std::hex;
    ss << std::hash<std::string>()(vertexSource);
    ss << std::hash<std::string>()(fragmentSource);
    return ss.str();
}

} // namespace shaders
} // namespace mbgl