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-09-25 04:34:18 +02:00
|
|
|
#include <string>
|
2019-01-05 05:00:06 +01:00
|
|
|
#include <tuple>
|
2019-02-03 01:14:36 +01:00
|
|
|
#include <unordered_map>
|
2019-09-25 04:34:18 +02:00
|
|
|
#include <unordered_set>
|
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"
|
2019-11-18 22:35:21 +01:00
|
|
|
#include "video_core/engines/shader_type.h"
|
2018-08-23 23:30:27 +02:00
|
|
|
#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"
|
2019-09-26 05:23:08 +02:00
|
|
|
#include "video_core/shader/const_buffer_locker.h"
|
2019-09-25 04:34:18 +02:00
|
|
|
#include "video_core/shader/shader_ir.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>;
|
|
|
|
using Maxwell = Tegra::Engines::Maxwell3D::Regs;
|
2020-02-26 20:13:47 +01:00
|
|
|
|
|
|
|
struct PrecompiledShader {
|
|
|
|
std::shared_ptr<OGLProgram> program;
|
|
|
|
std::shared_ptr<VideoCommon::Shader::ConstBufferLocker> locker;
|
|
|
|
ShaderEntries entries;
|
2019-09-25 04:34:18 +02:00
|
|
|
};
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2019-05-31 02:56:37 +02:00
|
|
|
struct ShaderParameters {
|
2019-09-23 21:40:58 +02:00
|
|
|
Core::System& system;
|
2019-09-25 04:34:18 +02:00
|
|
|
ShaderDiskCacheOpenGL& disk_cache;
|
2019-05-31 02:56:37 +02:00
|
|
|
const Device& device;
|
|
|
|
VAddr cpu_addr;
|
|
|
|
u8* host_ptr;
|
|
|
|
u64 unique_identifier;
|
|
|
|
};
|
|
|
|
|
2018-10-16 22:51:53 +02:00
|
|
|
class CachedShader final : public RasterizerCacheObject {
|
2018-08-23 23:30:27 +02:00
|
|
|
public:
|
2020-02-26 20:13:47 +01:00
|
|
|
~CachedShader();
|
2019-05-31 02:56:37 +02:00
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
/// Gets the GL program handle for the shader
|
|
|
|
GLuint GetHandle() const;
|
2019-07-15 03:25:13 +02:00
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
/// Returns the guest CPU address of the shader
|
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
|
|
|
}
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
/// Returns the size in bytes of the shader
|
2018-10-16 22:51:53 +02:00
|
|
|
std::size_t GetSizeInBytes() const override {
|
2020-02-26 20:13:47 +01:00
|
|
|
return size_in_bytes;
|
2018-08-23 23:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the shader entries for the shader
|
2020-02-26 20:13:47 +01:00
|
|
|
const ShaderEntries& GetEntries() const {
|
2018-08-23 23:30:27 +02:00
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
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);
|
2019-09-25 04:34:18 +02:00
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
static Shader CreateFromCache(const ShaderParameters& params,
|
|
|
|
const PrecompiledShader& precompiled_shader,
|
|
|
|
std::size_t size_in_bytes);
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
private:
|
|
|
|
explicit CachedShader(const u8* host_ptr, VAddr cpu_addr, std::size_t size_in_bytes,
|
|
|
|
std::shared_ptr<VideoCommon::Shader::ConstBufferLocker> locker,
|
|
|
|
ShaderEntries entries, std::shared_ptr<OGLProgram> program);
|
|
|
|
|
|
|
|
std::shared_ptr<VideoCommon::Shader::ConstBufferLocker> locker;
|
|
|
|
ShaderEntries entries;
|
|
|
|
VAddr cpu_addr = 0;
|
|
|
|
std::size_t size_in_bytes = 0;
|
|
|
|
std::shared_ptr<OGLProgram> program;
|
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-07-15 03:25:13 +02:00
|
|
|
/// Gets a compute kernel in the passed address
|
|
|
|
Shader GetComputeKernel(GPUVAddr code_addr);
|
|
|
|
|
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:
|
2020-02-26 20:13:47 +01:00
|
|
|
std::shared_ptr<OGLProgram> GeneratePrecompiledProgram(
|
|
|
|
const ShaderDiskCacheEntry& entry, const ShaderDiskCachePrecompiled& precompiled_entry,
|
|
|
|
const std::unordered_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;
|
2020-02-26 20:13:47 +01:00
|
|
|
std::unordered_map<u64, PrecompiledShader> runtime_cache;
|
2019-09-25 04:34:18 +02:00
|
|
|
|
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
|