summaryrefslogtreecommitdiff
path: root/src/mbgl/shader/shader.cpp
blob: d47723db494fb39033a988697ee0be0c4e8233cf (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <mbgl/shader/shader.hpp>
#include <mbgl/platform/gl.hpp>
#include <mbgl/util/stopwatch.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/platform.hpp>

#include <cstring>
#include <cassert>
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace mbgl;

Shader::Shader(const char *name_, const GLchar *vertSource, const GLchar *fragSource)
    : name(name_),
      valid(false),
      program(0) {
    util::stopwatch stopwatch("shader compilation", Event::Shader);

    program = MBGL_CHECK_ERROR(glCreateProgram());

    if (!mbgl::platform::defaultShaderCache().empty()) {
        binaryFileName = mbgl::platform::defaultShaderCache() + name + ".bin";
    }

    // Load binary shader if it exists
    bool skipCompile = false;
    if (!binaryFileName.empty() && (gl::ProgramBinary != nullptr)) {
        try {
            std::ifstream binaryFile(binaryFileName, std::ios::in | std::ios::binary);
            binaryFile.exceptions(std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit);

            GLsizei binaryLength;
            GLenum binaryFormat;
            binaryFile.read(reinterpret_cast<char *>(&binaryLength), sizeof(binaryLength));
            binaryFile.read(reinterpret_cast<char *>(&binaryFormat), sizeof(binaryFormat));

            GLint numBinaryFormats;
            MBGL_CHECK_ERROR(glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &numBinaryFormats));

            std::unique_ptr<GLenum[]> validBinaryFormats = mbgl::util::make_unique<GLenum[]>(numBinaryFormats);
            MBGL_CHECK_ERROR(glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, reinterpret_cast<GLint *>(validBinaryFormats.get())));

            bool validBinaryFormat = false;
            for (GLint i = 0; i < numBinaryFormats; i++) {
                if (validBinaryFormats[i] == binaryFormat) {
                    validBinaryFormat = true;
                }
            }
            if (!validBinaryFormat) {
                throw std::runtime_error("Trying load program binary with an invalid binaryFormat!");
            }

            if (binaryLength == 0) {
                throw std::runtime_error("Trying load program binary with a zero length binary!");
            }

            std::unique_ptr<char[]> binary = mbgl::util::make_unique<char[]>(binaryLength);
            binaryFile.read(binary.get(), binaryLength);

            MBGL_CHECK_ERROR(gl::ProgramBinary(program, binaryFormat, binary.get(), binaryLength));

            // Check if the binary was valid
            GLint status;
            MBGL_CHECK_ERROR(glGetProgramiv(program, GL_LINK_STATUS, &status));
            if (status == GL_TRUE) {
                skipCompile = true;
            }
        } catch(const std::exception& e) {
            Log::Error(Event::Shader, "Loading binary shader failed!");

            // Delete the bad file
            std::remove(binaryFileName.c_str());
        }
    }

    GLuint vertShader = 0;
    GLuint fragShader = 0;
    if (!skipCompile) {
        if (!compileShader(&vertShader, GL_VERTEX_SHADER, vertSource)) {
            Log::Error(Event::Shader, "Vertex shader %s failed to compile: %s", name, vertSource);
            MBGL_CHECK_ERROR(glDeleteProgram(program));
            program = 0;
            return;
        }

        if (!compileShader(&fragShader, GL_FRAGMENT_SHADER, fragSource)) {
            Log::Error(Event::Shader, "Fragment shader %s failed to compile: %s", name, fragSource);
            MBGL_CHECK_ERROR(glDeleteShader(vertShader));
            vertShader = 0;
            MBGL_CHECK_ERROR(glDeleteProgram(program));
            program = 0;
            return;
        }

        // Attach shaders
        MBGL_CHECK_ERROR(glAttachShader(program, vertShader));
        MBGL_CHECK_ERROR(glAttachShader(program, fragShader));

        {
            if (!binaryFileName.empty() && (gl::ProgramParameteri != nullptr)) {
                MBGL_CHECK_ERROR(gl::ProgramParameteri(program, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE));
            }

            // Link program
            GLint status;
            MBGL_CHECK_ERROR(glLinkProgram(program));

            MBGL_CHECK_ERROR(glGetProgramiv(program, GL_LINK_STATUS, &status));
            if (status == 0) {
                GLint logLength;
                MBGL_CHECK_ERROR(glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength));
                if (logLength > 0) {
                    std::unique_ptr<GLchar[]> log = mbgl::util::make_unique<GLchar[]>(logLength);
                    MBGL_CHECK_ERROR(glGetProgramInfoLog(program, logLength, &logLength, log.get()));
                    Log::Error(Event::Shader, "Program failed to link: %s", log.get());
                }

                MBGL_CHECK_ERROR(glDeleteShader(vertShader));
                vertShader = 0;
                MBGL_CHECK_ERROR(glDeleteShader(fragShader));
                fragShader = 0;
                MBGL_CHECK_ERROR(glDeleteProgram(program));
                program = 0;
                return;
            }
        }
    }

    {
        // Validate program
        GLint status;
        MBGL_CHECK_ERROR(glValidateProgram(program));

        MBGL_CHECK_ERROR(glGetProgramiv(program, GL_VALIDATE_STATUS, &status));
        if (status == 0) {
            GLint logLength;
            MBGL_CHECK_ERROR(glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength));
            if (logLength > 0) {
                std::unique_ptr<GLchar[]> log = mbgl::util::make_unique<GLchar[]>(logLength);
                MBGL_CHECK_ERROR(glGetProgramInfoLog(program, logLength, &logLength, log.get()));
                Log::Error(Event::Shader, "Program failed to validate: %s", log.get());
            }

            MBGL_CHECK_ERROR(glDeleteShader(vertShader));
            vertShader = 0;
            MBGL_CHECK_ERROR(glDeleteShader(fragShader));
            fragShader = 0;
            MBGL_CHECK_ERROR(glDeleteProgram(program));
            program = 0;
        }
    }

    if (!skipCompile) {
        // Remove the compiled shaders; they are now part of the program.
        MBGL_CHECK_ERROR(glDetachShader(program, vertShader));
        MBGL_CHECK_ERROR(glDeleteShader(vertShader));
        MBGL_CHECK_ERROR(glDetachShader(program, fragShader));
        MBGL_CHECK_ERROR(glDeleteShader(fragShader));
    }

    valid = true;
}


bool Shader::compileShader(GLuint *shader, GLenum type, const GLchar *source) {
    GLint status;

    *shader = MBGL_CHECK_ERROR(glCreateShader(type));
    const GLchar *strings[] = { source };
    const GLsizei lengths[] = { static_cast<GLsizei>(std::strlen(source)) };
    MBGL_CHECK_ERROR(glShaderSource(*shader, 1, strings, lengths));

    MBGL_CHECK_ERROR(glCompileShader(*shader));

    MBGL_CHECK_ERROR(glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
    if (status == 0) {
        GLint logLength;
        MBGL_CHECK_ERROR(glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength));
        if (logLength > 0) {
            std::unique_ptr<GLchar[]> log = mbgl::util::make_unique<GLchar[]>(logLength);
            MBGL_CHECK_ERROR(glGetShaderInfoLog(*shader, logLength, &logLength, log.get()));
            Log::Error(Event::Shader, "Shader failed to compile: %s", log.get());
        }

        MBGL_CHECK_ERROR(glDeleteShader(*shader));
        *shader = 0;
        return false;
    }

    MBGL_CHECK_ERROR(glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
    if (status == GL_FALSE) {
    Log::Error(Event::Shader, "Shader %s failed to compile.", name, type);
        MBGL_CHECK_ERROR(glDeleteShader(*shader));
        *shader = 0;
        return false;
    }

    return true;
}

Shader::~Shader() {
    if (!binaryFileName.empty() && (gl::GetProgramBinary != nullptr)) {
        // Retrieve the program binary
        GLsizei binaryLength;
        GLenum binaryFormat;
        MBGL_CHECK_ERROR(glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &binaryLength));
        if (binaryLength > 0) {
            std::unique_ptr<char[]> binary = mbgl::util::make_unique<char[]>(binaryLength);
            MBGL_CHECK_ERROR(gl::GetProgramBinary(program, binaryLength, nullptr, &binaryFormat, binary.get()));

            Log::Error(Event::OpenGL, "glGetProgramBinary(%u, %u, nullptr, %u, %u)", program, binaryLength, binaryFormat, binary.get());

            try {
                // Write the binary to a file
                std::ofstream binaryFile(binaryFileName, std::ios::out | std::ios::trunc | std::ios::binary);
                binaryFile.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit);

                binaryFile.write(reinterpret_cast<char *>(&binaryLength), sizeof(binaryLength));
                binaryFile.write(reinterpret_cast<char *>(&binaryFormat), sizeof(binaryFormat));
                binaryFile.write(binary.get(), binaryLength);

            } catch(const std::exception& e) {
                Log::Error(Event::Shader, "Saving binary shader failed!");

                // Delete the bad file
                std::remove(binaryFileName.c_str());
            }
        }
    }

    if (program) {
        MBGL_CHECK_ERROR(glDeleteProgram(program));
        program = 0;
        valid = false;
    }
}