2018-08-23 23:30:27 +02:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-01-06 07:58:43 +01:00
|
|
|
#include <array>
|
2019-04-04 17:58:06 +02:00
|
|
|
#include <atomic>
|
2019-04-29 02:08:31 +02:00
|
|
|
#include <bitset>
|
2018-08-23 23:30:27 +02:00
|
|
|
#include <memory>
|
2019-01-14 04:58:15 +01:00
|
|
|
#include <set>
|
2019-01-05 05:00:06 +01:00
|
|
|
#include <tuple>
|
2019-02-03 01:14:36 +01:00
|
|
|
#include <unordered_map>
|
2019-04-04 17:58:06 +02:00
|
|
|
#include <vector>
|
2019-01-05 05:00:06 +01:00
|
|
|
|
|
|
|
#include <glad/glad.h>
|
2018-08-23 23:30:27 +02:00
|
|
|
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "video_core/rasterizer_cache.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_resource_manager.h"
|
2018-12-26 05:57:14 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_decompiler.h"
|
2019-01-14 02:36:52 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_disk_cache.h"
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2019-01-15 20:28:42 +01:00
|
|
|
namespace Core {
|
|
|
|
class System;
|
2019-04-06 22:59:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Core::Frontend {
|
|
|
|
class EmuWindow;
|
|
|
|
}
|
2019-01-15 20:28:42 +01:00
|
|
|
|
2018-08-23 23:30:27 +02:00
|
|
|
namespace OpenGL {
|
|
|
|
|
|
|
|
class CachedShader;
|
2019-04-10 23:03:52 +02:00
|
|
|
class Device;
|
2018-11-08 12:08:00 +01:00
|
|
|
class RasterizerOpenGL;
|
2019-01-14 04:58:15 +01:00
|
|
|
struct UnspecializedShader;
|
2018-11-08 12:08:00 +01:00
|
|
|
|
2018-08-23 23:30:27 +02:00
|
|
|
using Shader = std::shared_ptr<CachedShader>;
|
2019-01-14 04:58:15 +01:00
|
|
|
using CachedProgram = std::shared_ptr<OGLProgram>;
|
2018-08-23 23:30:27 +02:00
|
|
|
using Maxwell = Tegra::Engines::Maxwell3D::Regs;
|
2019-02-03 01:14:36 +01:00
|
|
|
using PrecompiledPrograms = std::unordered_map<ShaderDiskCacheUsage, CachedProgram>;
|
|
|
|
using PrecompiledShaders = std::unordered_map<u64, GLShader::ProgramResult>;
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2018-10-16 22:51:53 +02:00
|
|
|
class CachedShader final : public RasterizerCacheObject {
|
2018-08-23 23:30:27 +02:00
|
|
|
public:
|
2019-04-10 23:03:52 +02:00
|
|
|
explicit CachedShader(const Device& device, VAddr cpu_addr, u64 unique_identifier,
|
2019-02-19 02:58:32 +01:00
|
|
|
Maxwell::ShaderProgram program_type, ShaderDiskCacheOpenGL& disk_cache,
|
2019-01-14 04:58:15 +01:00
|
|
|
const PrecompiledPrograms& precompiled_programs,
|
2019-02-19 02:58:32 +01:00
|
|
|
ProgramCode&& program_code, ProgramCode&& program_code_b, u8* host_ptr);
|
2019-01-14 04:58:15 +01:00
|
|
|
|
2019-02-24 06:15:35 +01:00
|
|
|
explicit CachedShader(VAddr cpu_addr, u64 unique_identifier,
|
2019-02-19 02:58:32 +01:00
|
|
|
Maxwell::ShaderProgram program_type, ShaderDiskCacheOpenGL& disk_cache,
|
2019-01-14 04:58:15 +01:00
|
|
|
const PrecompiledPrograms& precompiled_programs,
|
2019-02-19 02:58:32 +01:00
|
|
|
GLShader::ProgramResult result, u8* host_ptr);
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2019-02-19 02:58:32 +01:00
|
|
|
VAddr GetCpuAddr() const override {
|
2019-02-24 06:15:35 +01:00
|
|
|
return cpu_addr;
|
2018-08-23 23:30:27 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 22:51:53 +02:00
|
|
|
std::size_t GetSizeInBytes() const override {
|
2018-11-20 20:14:48 +01:00
|
|
|
return shader_length;
|
2018-08-23 23:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the shader entries for the shader
|
|
|
|
const GLShader::ShaderEntries& GetShaderEntries() const {
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the GL program handle for the shader
|
2019-04-29 02:08:31 +02:00
|
|
|
std::tuple<GLuint, BaseBindings> GetProgramHandle(const ProgramVariant& variant);
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2019-01-05 05:00:06 +01:00
|
|
|
private:
|
|
|
|
// Geometry programs. These are needed because GLSL needs an input topology but it's not
|
|
|
|
// declared by the hardware. Workaround this issue by generating a different shader per input
|
|
|
|
// topology class.
|
|
|
|
struct GeometryPrograms {
|
2019-01-14 04:58:15 +01:00
|
|
|
CachedProgram points;
|
|
|
|
CachedProgram lines;
|
|
|
|
CachedProgram lines_adjacency;
|
|
|
|
CachedProgram triangles;
|
|
|
|
CachedProgram triangles_adjacency;
|
2019-01-05 05:00:06 +01:00
|
|
|
};
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2019-04-29 02:08:31 +02:00
|
|
|
GLuint GetGeometryShader(const ProgramVariant& variant);
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
/// Generates a geometry shader or returns one that already exists.
|
2019-04-29 02:08:31 +02:00
|
|
|
GLuint LazyGeometryProgram(CachedProgram& target_program, const ProgramVariant& variant);
|
2019-01-14 04:58:15 +01:00
|
|
|
|
2019-04-29 02:08:31 +02:00
|
|
|
CachedProgram TryLoadProgram(const ProgramVariant& variant) const;
|
2019-01-14 04:58:15 +01:00
|
|
|
|
2019-04-29 02:08:31 +02:00
|
|
|
ShaderDiskCacheUsage GetUsage(const ProgramVariant& variant) const;
|
2018-10-07 04:17:31 +02:00
|
|
|
|
2019-02-19 02:58:32 +01:00
|
|
|
u8* host_ptr{};
|
2019-02-24 06:15:35 +01:00
|
|
|
VAddr cpu_addr{};
|
2019-01-15 06:42:25 +01:00
|
|
|
u64 unique_identifier{};
|
|
|
|
Maxwell::ShaderProgram program_type{};
|
2019-01-14 04:58:15 +01:00
|
|
|
ShaderDiskCacheOpenGL& disk_cache;
|
|
|
|
const PrecompiledPrograms& precompiled_programs;
|
2018-12-09 22:33:10 +01:00
|
|
|
|
2019-01-05 05:00:06 +01:00
|
|
|
std::size_t shader_length{};
|
2018-08-23 23:30:27 +02:00
|
|
|
GLShader::ShaderEntries entries;
|
2018-10-07 04:17:31 +02:00
|
|
|
|
2019-01-05 05:00:06 +01:00
|
|
|
std::string code;
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2019-04-29 02:08:31 +02:00
|
|
|
std::unordered_map<ProgramVariant, CachedProgram> programs;
|
|
|
|
std::unordered_map<ProgramVariant, GeometryPrograms> geometry_programs;
|
2018-10-07 04:17:31 +02:00
|
|
|
|
2019-02-03 01:14:36 +01:00
|
|
|
std::unordered_map<u32, GLuint> cbuf_resource_cache;
|
|
|
|
std::unordered_map<u32, GLuint> gmem_resource_cache;
|
|
|
|
std::unordered_map<u32, GLint> uniform_cache;
|
2018-08-23 23:30:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ShaderCacheOpenGL final : public RasterizerCache<Shader> {
|
|
|
|
public:
|
2019-04-10 23:03:52 +02:00
|
|
|
explicit ShaderCacheOpenGL(RasterizerOpenGL& rasterizer, Core::System& system,
|
2019-04-06 22:59:56 +02:00
|
|
|
Core::Frontend::EmuWindow& emu_window, const Device& device);
|
2018-11-08 12:08:00 +01:00
|
|
|
|
2019-01-14 02:05:53 +01:00
|
|
|
/// Loads disk cache for the current game
|
2019-01-21 20:38:23 +01:00
|
|
|
void LoadDiskCache(const std::atomic_bool& stop_loading,
|
|
|
|
const VideoCore::DiskResourceLoadCallback& callback);
|
2019-01-14 02:05:53 +01:00
|
|
|
|
2018-08-23 23:30:27 +02:00
|
|
|
/// Gets the current specified shader stage program
|
|
|
|
Shader GetStageProgram(Maxwell::ShaderProgram program);
|
2019-01-06 07:58:43 +01:00
|
|
|
|
2019-04-16 22:39:27 +02:00
|
|
|
protected:
|
|
|
|
// We do not have to flush this cache as things in it are never modified by us.
|
|
|
|
void FlushObjectInner(const Shader& object) override {}
|
|
|
|
|
2019-01-06 07:58:43 +01:00
|
|
|
private:
|
2019-02-03 01:14:36 +01:00
|
|
|
std::unordered_map<u64, UnspecializedShader> GenerateUnspecializedShaders(
|
2019-01-21 20:38:23 +01:00
|
|
|
const std::atomic_bool& stop_loading, const VideoCore::DiskResourceLoadCallback& callback,
|
2019-01-15 05:07:57 +01:00
|
|
|
const std::vector<ShaderDiskCacheRaw>& raws,
|
2019-02-03 01:14:36 +01:00
|
|
|
const std::unordered_map<u64, ShaderDiskCacheDecompiled>& decompiled);
|
2019-01-14 04:58:15 +01:00
|
|
|
|
2019-01-15 05:07:57 +01:00
|
|
|
CachedProgram GeneratePrecompiledProgram(const ShaderDiskCacheDump& dump,
|
|
|
|
const std::set<GLenum>& supported_formats);
|
2019-01-14 04:58:15 +01:00
|
|
|
|
2019-05-30 19:01:40 +02:00
|
|
|
Core::System& system;
|
2019-04-06 22:59:56 +02:00
|
|
|
Core::Frontend::EmuWindow& emu_window;
|
2019-04-10 23:03:52 +02:00
|
|
|
const Device& device;
|
2019-01-14 04:58:15 +01:00
|
|
|
ShaderDiskCacheOpenGL disk_cache;
|
2019-04-06 22:59:56 +02:00
|
|
|
|
2019-01-14 04:58:15 +01:00
|
|
|
PrecompiledShaders precompiled_shaders;
|
|
|
|
PrecompiledPrograms precompiled_programs;
|
2019-04-06 22:59:56 +02:00
|
|
|
std::array<Shader, Maxwell::MaxShaderProgram> last_shaders;
|
2018-08-23 23:30:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OpenGL
|