summaryrefslogtreecommitdiffstats
path: root/src/video_core/renderer_opengl/gl_shader_util.h
blob: a1fa9e814ae1829c482ecb091601055b82573228 (plain) (blame)
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
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <vector>
#include <glad/glad.h>
#include "common/assert.h"
#include "common/logging/log.h"

namespace GLShader {

/**
 * Utility function to create and compile an OpenGL GLSL shader
 * @param source String of the GLSL shader program
 * @param type Type of the shader (GL_VERTEX_SHADER, GL_GEOMETRY_SHADER or GL_FRAGMENT_SHADER)
 */
GLuint LoadShader(const char* source, GLenum type);

/**
 * Utility function to create and compile an OpenGL GLSL shader program (vertex + fragment shader)
 * @param separable_program whether to create a separable program
 * @param shaders ID of shaders to attach to the program
 * @returns Handle of the newly created OpenGL program object
 */
template <typename... T>
GLuint LoadProgram(bool separable_program, T... shaders) {
    // Link the program
    NGLOG_DEBUG(Render_OpenGL, "Linking program...");

    GLuint program_id = glCreateProgram();

    ((shaders == 0 ? (void)0 : glAttachShader(program_id, shaders)), ...);

    if (separable_program) {
        glProgramParameteri(program_id, GL_PROGRAM_SEPARABLE, GL_TRUE);
    }

    glLinkProgram(program_id);

    // Check the program
    GLint result = GL_FALSE;
    GLint info_log_length;
    glGetProgramiv(program_id, GL_LINK_STATUS, &result);
    glGetProgramiv(program_id, GL_INFO_LOG_LENGTH, &info_log_length);

    if (info_log_length > 1) {
        std::string program_error(info_log_length, ' ');
        glGetProgramInfoLog(program_id, info_log_length, nullptr, &program_error[0]);
        if (result == GL_TRUE) {
            NGLOG_DEBUG(Render_OpenGL, "{}", program_error);
        } else {
            NGLOG_ERROR(Render_OpenGL, "Error linking shader:\n{}", program_error);
        }
    }

    ASSERT_MSG(result == GL_TRUE, "Shader not linked");

    ((shaders == 0 ? (void)0 : glDetachShader(program_id, shaders)), ...);

    return program_id;
}

} // namespace GLShader