summaryrefslogtreecommitdiff
path: root/src/mbgl/shader/shader.cpp
blob: 8216de5b4d9b3f1ecd33d8ad6189c478eebfef38 (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
#include <mbgl/shader/shader.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/util/stopwatch.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/platform.hpp>

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

namespace mbgl {

Shader::Shader(const char *name_, const GLchar *vertSource, const GLchar *fragSource, gl::GLObjectStore& glObjectStore)
    : name(name_)
{
    util::stopwatch stopwatch("shader compilation", Event::Shader);

    program.create(glObjectStore);
    vertexShader.create(glObjectStore);
    if (!compileShader(vertexShader, &vertSource)) {
        Log::Error(Event::Shader, "Vertex shader %s failed to compile: %s", name, vertSource);
        throw util::ShaderException(std::string { "Vertex shader " } + name + " failed to compile");
    }

    fragmentShader.create(glObjectStore);
    if (!compileShader(fragmentShader, &fragSource)) {
        Log::Error(Event::Shader, "Fragment shader %s failed to compile: %s", name, fragSource);
        throw util::ShaderException(std::string { "Fragment shader " } + name + " failed to compile");
    }

    // Attach shaders
    MBGL_CHECK_ERROR(glAttachShader(program.getID(), vertexShader.getID()));
    MBGL_CHECK_ERROR(glAttachShader(program.getID(), fragmentShader.getID()));

    {
        // Link program
        GLint status;
        MBGL_CHECK_ERROR(glLinkProgram(program.getID()));

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

    a_pos = MBGL_CHECK_ERROR(glGetAttribLocation(program.getID(), "a_pos"));
}

bool Shader::compileShader(gl::ShaderHolder& shader, const GLchar *source[]) {
    GLint status = 0;

    const GLsizei lengths = static_cast<GLsizei>(std::strlen(*source));
    MBGL_CHECK_ERROR(glShaderSource(shader.getID(), 1, source, &lengths));

    MBGL_CHECK_ERROR(glCompileShader(shader.getID()));

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

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

    return true;
}

Shader::~Shader() {
    if (program) {
        MBGL_CHECK_ERROR(glDetachShader(program.getID(), vertexShader.getID()));
        MBGL_CHECK_ERROR(glDetachShader(program.getID(), fragmentShader.getID()));
    }
}

} // namespace mbgl