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

#pragma once

#include <array>
#include <atomic>
#include <bitset>
#include <memory>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <glad/glad.h>

#include "common/common_types.h"
#include "video_core/engines/shader_type.h"
#include "video_core/rasterizer_cache.h"
#include "video_core/renderer_opengl/gl_resource_manager.h"
#include "video_core/renderer_opengl/gl_shader_decompiler.h"
#include "video_core/renderer_opengl/gl_shader_disk_cache.h"
#include "video_core/shader/const_buffer_locker.h"
#include "video_core/shader/shader_ir.h"

namespace Core {
class System;
}

namespace Core::Frontend {
class EmuWindow;
}

namespace OpenGL {

class CachedShader;
class Device;
class RasterizerOpenGL;
struct UnspecializedShader;

using Shader = std::shared_ptr<CachedShader>;
using CachedProgram = std::shared_ptr<OGLProgram>;
using Maxwell = Tegra::Engines::Maxwell3D::Regs;
using PrecompiledPrograms = std::unordered_map<ShaderDiskCacheUsage, CachedProgram>;
using PrecompiledVariants = std::vector<PrecompiledPrograms::iterator>;

struct UnspecializedShader {
    GLShader::ShaderEntries entries;
    Tegra::Engines::ShaderType type;
    ProgramCode code;
    ProgramCode code_b;
};

struct ShaderParameters {
    Core::System& system;
    ShaderDiskCacheOpenGL& disk_cache;
    const PrecompiledVariants* precompiled_variants;
    const Device& device;
    VAddr cpu_addr;
    u8* host_ptr;
    u64 unique_identifier;
};

class CachedShader final : public RasterizerCacheObject {
public:
    static Shader CreateStageFromMemory(const ShaderParameters& params,
                                        Maxwell::ShaderProgram program_type,
                                        ProgramCode program_code, ProgramCode program_code_b);
    static Shader CreateKernelFromMemory(const ShaderParameters& params, ProgramCode code);

    static Shader CreateFromCache(const ShaderParameters& params,
                                  const UnspecializedShader& unspecialized);

    VAddr GetCpuAddr() const override {
        return cpu_addr;
    }

    std::size_t GetSizeInBytes() const override {
        return code.size() * sizeof(u64);
    }

    /// Gets the shader entries for the shader
    const GLShader::ShaderEntries& GetShaderEntries() const {
        return entries;
    }

    /// Gets the GL program handle for the shader
    GLuint GetHandle(const ProgramVariant& variant);

private:
    struct LockerVariant {
        std::unique_ptr<VideoCommon::Shader::ConstBufferLocker> locker;
        std::unordered_map<ProgramVariant, CachedProgram> programs;
    };

    explicit CachedShader(const ShaderParameters& params, Tegra::Engines::ShaderType shader_type,
                          GLShader::ShaderEntries entries, ProgramCode program_code,
                          ProgramCode program_code_b);

    bool EnsureValidLockerVariant();

    ShaderDiskCacheUsage GetUsage(const ProgramVariant& variant,
                                  const VideoCommon::Shader::ConstBufferLocker& locker) const;

    Core::System& system;
    ShaderDiskCacheOpenGL& disk_cache;
    const Device& device;

    VAddr cpu_addr{};

    u64 unique_identifier{};
    Tegra::Engines::ShaderType shader_type{};

    GLShader::ShaderEntries entries;

    ProgramCode code;
    ProgramCode code_b;

    LockerVariant* curr_locker_variant = nullptr;
    std::vector<std::unique_ptr<LockerVariant>> locker_variants;
};

class ShaderCacheOpenGL final : public RasterizerCache<Shader> {
public:
    explicit ShaderCacheOpenGL(RasterizerOpenGL& rasterizer, Core::System& system,
                               Core::Frontend::EmuWindow& emu_window, const Device& device);

    /// Loads disk cache for the current game
    void LoadDiskCache(const std::atomic_bool& stop_loading,
                       const VideoCore::DiskResourceLoadCallback& callback);

    /// Gets the current specified shader stage program
    Shader GetStageProgram(Maxwell::ShaderProgram program);

    /// Gets a compute kernel in the passed address
    Shader GetComputeKernel(GPUVAddr code_addr);

protected:
    // We do not have to flush this cache as things in it are never modified by us.
    void FlushObjectInner(const Shader& object) override {}

private:
    bool GenerateUnspecializedShaders(const std::atomic_bool& stop_loading,
                                      const VideoCore::DiskResourceLoadCallback& callback,
                                      const std::vector<ShaderDiskCacheRaw>& raws);

    CachedProgram GeneratePrecompiledProgram(const ShaderDiskCacheDump& dump,
                                             const std::unordered_set<GLenum>& supported_formats);

    const PrecompiledVariants* GetPrecompiledVariants(u64 unique_identifier) const;

    Core::System& system;
    Core::Frontend::EmuWindow& emu_window;
    const Device& device;

    ShaderDiskCacheOpenGL disk_cache;

    PrecompiledPrograms precompiled_programs;
    std::unordered_map<u64, PrecompiledVariants> precompiled_variants;

    std::unordered_map<u64, UnspecializedShader> unspecialized_shaders;

    std::array<Shader, Maxwell::MaxShaderProgram> last_shaders;
};

} // namespace OpenGL