summaryrefslogtreecommitdiffstats
path: root/src/video_core/renderer_opengl/gl_shader_decompiler.h
blob: a5bdbaf7adea09e0aabc650b3b7fb52519628760 (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
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
// Copyright 2018 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <array>
#include <string>
#include <utility>
#include <vector>
#include "common/common_types.h"
#include "video_core/engines/maxwell_3d.h"
#include "video_core/shader/shader_ir.h"

namespace VideoCommon::Shader {
class ShaderIR;
}

namespace OpenGL::GLShader {

using Maxwell = Tegra::Engines::Maxwell3D::Regs;

class ConstBufferEntry : public VideoCommon::Shader::ConstBuffer {
public:
    explicit ConstBufferEntry(const VideoCommon::Shader::ConstBuffer& entry,
                              Maxwell::ShaderStage stage, const std::string& name, u32 index)
        : VideoCommon::Shader::ConstBuffer{entry}, stage{stage}, name{name}, index{index} {}

    const std::string& GetName() const {
        return name;
    }

    Maxwell::ShaderStage GetStage() const {
        return stage;
    }

    u32 GetIndex() const {
        return index;
    }

private:
    std::string name;
    Maxwell::ShaderStage stage{};
    u32 index{};
};

class SamplerEntry : public VideoCommon::Shader::Sampler {
public:
    explicit SamplerEntry(const VideoCommon::Shader::Sampler& entry, Maxwell::ShaderStage stage,
                          const std::string& name)
        : VideoCommon::Shader::Sampler{entry}, stage{stage}, name{name} {}

    const std::string& GetName() const {
        return name;
    }

    Maxwell::ShaderStage GetStage() const {
        return stage;
    }

private:
    std::string name;
    Maxwell::ShaderStage stage{};
};

class GlobalMemoryEntry {
public:
    explicit GlobalMemoryEntry(u32 cbuf_index, u32 cbuf_offset, Maxwell::ShaderStage stage,
                               std::string name)
        : cbuf_index{cbuf_index}, cbuf_offset{cbuf_offset}, stage{stage}, name{std::move(name)} {}

    u32 GetCbufIndex() const {
        return cbuf_index;
    }

    u32 GetCbufOffset() const {
        return cbuf_offset;
    }

    const std::string& GetName() const {
        return name;
    }

    Maxwell::ShaderStage GetStage() const {
        return stage;
    }

private:
    u32 cbuf_index{};
    u32 cbuf_offset{};
    Maxwell::ShaderStage stage{};
    std::string name;
};

class GlobalMemoryEntry {
public:
    explicit GlobalMemoryEntry(u32 cbuf_index, u32 cbuf_offset, Maxwell::ShaderStage stage,
                               std::string name)
        : cbuf_index{cbuf_index}, cbuf_offset{cbuf_offset}, stage{stage}, name{std::move(name)} {}

    u32 GetCbufIndex() const {
        return cbuf_index;
    }

    u32 GetCbufOffset() const {
        return cbuf_offset;
    }

    const std::string& GetName() const {
        return name;
    }

    Maxwell::ShaderStage GetStage() const {
        return stage;
    }

    u32 GetHash() const {
        return (static_cast<u32>(stage) << 24) | (cbuf_index << 16) | cbuf_offset;
    }

private:
    u32 cbuf_index{};
    u32 cbuf_offset{};
    Maxwell::ShaderStage stage{};
    std::string name;
};

struct ShaderEntries {
    std::vector<ConstBufferEntry> const_buffers;
    std::vector<SamplerEntry> samplers;
    std::vector<GlobalMemoryEntry> global_memory_entries;
    std::array<bool, Maxwell::NumClipDistances> clip_distances{};
    std::size_t shader_length{};
};

using ProgramResult = std::pair<std::string, ShaderEntries>;

std::string GetCommonDeclarations();

ProgramResult Decompile(const VideoCommon::Shader::ShaderIR& ir, Maxwell::ShaderStage stage,
                        const std::string& suffix);

} // namespace OpenGL::GLShader