summaryrefslogtreecommitdiffstats
path: root/src/video_core/renderer_opengl/gl_shader_manager.h
blob: e94cd75aa00f1e3cedc1771d33b0680d30f7cb65 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2018 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <cstddef>

#include <glad/glad.h>

#include "video_core/renderer_opengl/gl_resource_manager.h"
#include "video_core/renderer_opengl/maxwell_to_gl.h"

namespace OpenGL::GLShader {

/// Uniform structure for the Uniform Buffer Object, all vectors must be 16-byte aligned
/// @note Always keep a vec4 at the end. The GL spec is not clear whether the alignment at
///       the end of a uniform block is included in UNIFORM_BLOCK_DATA_SIZE or not.
///       Not following that rule will cause problems on some AMD drivers.
struct alignas(16) MaxwellUniformData {
    void SetFromRegs(const Tegra::Engines::Maxwell3D& maxwell);

    GLfloat y_direction;
};
static_assert(sizeof(MaxwellUniformData) == 16, "MaxwellUniformData structure size is incorrect");
static_assert(sizeof(MaxwellUniformData) < 16384,
              "MaxwellUniformData structure must be less than 16kb as per the OpenGL spec");

class ProgramManager {
public:
    ~ProgramManager();

    void Create();

    void Update();

    void UseVertexShader(GLuint program) {
        current_state.vertex_shader = program;
    }

    void UseGeometryShader(GLuint program) {
        current_state.geometry_shader = program;
    }

    void UseFragmentShader(GLuint program) {
        current_state.fragment_shader = program;
    }

    GLuint GetHandle() const {
        return pipeline.handle;
    }

    void UseTrivialFragmentShader() {
        current_state.fragment_shader = 0;
    }

private:
    struct PipelineState {
        bool operator==(const PipelineState& rhs) const {
            return vertex_shader == rhs.vertex_shader && fragment_shader == rhs.fragment_shader &&
                   geometry_shader == rhs.geometry_shader;
        }

        bool operator!=(const PipelineState& rhs) const {
            return !operator==(rhs);
        }

        GLuint vertex_shader{};
        GLuint fragment_shader{};
        GLuint geometry_shader{};
    };

    OGLPipeline pipeline;
    PipelineState current_state;
    PipelineState old_state;
};

} // namespace OpenGL::GLShader