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/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"
|
2020-02-29 00:53:10 +01:00
|
|
|
#include "video_core/shader/registry.h"
|
2019-09-25 04:34:18 +02:00
|
|
|
#include "video_core/shader/shader_ir.h"
|
2020-05-23 01:55:38 +02:00
|
|
|
#include "video_core/shader_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
|
|
|
|
2020-07-10 05:36:38 +02:00
|
|
|
namespace VideoCommon::Shader {
|
|
|
|
class AsyncShaders;
|
|
|
|
}
|
|
|
|
|
2018-08-23 23:30:27 +02:00
|
|
|
namespace OpenGL {
|
|
|
|
|
2019-04-10 23:03:52 +02:00
|
|
|
class Device;
|
2018-11-08 12:08:00 +01:00
|
|
|
class RasterizerOpenGL;
|
|
|
|
|
2018-08-23 23:30:27 +02:00
|
|
|
using Maxwell = Tegra::Engines::Maxwell3D::Regs;
|
2020-02-26 20:13:47 +01:00
|
|
|
|
2020-05-18 03:32:49 +02:00
|
|
|
struct ProgramHandle {
|
|
|
|
OGLProgram source_program;
|
|
|
|
OGLAssemblyProgram assembly_program;
|
|
|
|
};
|
|
|
|
using ProgramSharedPtr = std::shared_ptr<ProgramHandle>;
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
struct PrecompiledShader {
|
2020-05-18 03:32:49 +02:00
|
|
|
ProgramSharedPtr program;
|
2020-02-29 00:53:10 +01:00
|
|
|
std::shared_ptr<VideoCommon::Shader::Registry> registry;
|
2020-02-26 20:13:47 +01:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2020-07-10 05:36:38 +02:00
|
|
|
ProgramSharedPtr BuildShader(const Device& device, Tegra::Engines::ShaderType shader_type,
|
|
|
|
u64 unique_identifier, const VideoCommon::Shader::ShaderIR& ir,
|
|
|
|
const VideoCommon::Shader::Registry& registry,
|
|
|
|
bool hint_retrievable = false);
|
|
|
|
|
2020-05-23 01:55:38 +02:00
|
|
|
class Shader final {
|
2018-08-23 23:30:27 +02:00
|
|
|
public:
|
2020-05-23 01:55:38 +02:00
|
|
|
~Shader();
|
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-07-10 05:36:38 +02:00
|
|
|
bool IsBuilt() const;
|
|
|
|
|
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-07-10 05:36:38 +02:00
|
|
|
const VideoCommon::Shader::Registry& GetRegistry() const {
|
|
|
|
return *registry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mark a OpenGL shader as built
|
|
|
|
void AsyncOpenGLBuilt(OGLProgram new_program);
|
|
|
|
|
|
|
|
/// Mark a GLASM shader as built
|
|
|
|
void AsyncGLASMBuilt(OGLAssemblyProgram new_program);
|
|
|
|
|
|
|
|
static std::unique_ptr<Shader> CreateStageFromMemory(
|
|
|
|
const ShaderParameters& params, Maxwell::ShaderProgram program_type,
|
|
|
|
ProgramCode program_code, ProgramCode program_code_b,
|
|
|
|
VideoCommon::Shader::AsyncShaders& async_shaders, VAddr cpu_addr);
|
|
|
|
|
2020-05-23 01:55:38 +02:00
|
|
|
static std::unique_ptr<Shader> CreateKernelFromMemory(const ShaderParameters& params,
|
|
|
|
ProgramCode code);
|
2019-09-25 04:34:18 +02:00
|
|
|
|
2020-05-23 01:55:38 +02:00
|
|
|
static std::unique_ptr<Shader> CreateFromCache(const ShaderParameters& params,
|
|
|
|
const PrecompiledShader& precompiled_shader);
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
private:
|
2020-05-23 01:55:38 +02:00
|
|
|
explicit Shader(std::shared_ptr<VideoCommon::Shader::Registry> registry, ShaderEntries entries,
|
2020-07-10 05:36:38 +02:00
|
|
|
ProgramSharedPtr program, bool is_built = true);
|
2020-02-26 20:13:47 +01:00
|
|
|
|
2020-02-29 00:53:10 +01:00
|
|
|
std::shared_ptr<VideoCommon::Shader::Registry> registry;
|
2020-02-26 20:13:47 +01:00
|
|
|
ShaderEntries entries;
|
2020-05-18 03:32:49 +02:00
|
|
|
ProgramSharedPtr program;
|
|
|
|
GLuint handle = 0;
|
2020-07-10 05:36:38 +02:00
|
|
|
bool is_built{};
|
2018-08-23 23:30:27 +02:00
|
|
|
};
|
|
|
|
|
2020-05-23 01:55:38 +02:00
|
|
|
class ShaderCacheOpenGL final : public VideoCommon::ShaderCache<Shader> {
|
2018-08-23 23:30:27 +02:00
|
|
|
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);
|
2020-05-23 01:55:38 +02:00
|
|
|
~ShaderCacheOpenGL() override;
|
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
|
2020-07-10 05:36:38 +02:00
|
|
|
Shader* GetStageProgram(Maxwell::ShaderProgram program,
|
|
|
|
VideoCommon::Shader::AsyncShaders& async_shaders);
|
2019-01-06 07:58:43 +01:00
|
|
|
|
2019-07-15 03:25:13 +02:00
|
|
|
/// Gets a compute kernel in the passed address
|
2020-05-23 01:55:38 +02:00
|
|
|
Shader* GetComputeKernel(GPUVAddr code_addr);
|
2019-04-16 22:39:27 +02:00
|
|
|
|
2019-01-06 07:58:43 +01:00
|
|
|
private:
|
2020-05-18 03:32:49 +02:00
|
|
|
ProgramSharedPtr GeneratePrecompiledProgram(
|
2020-02-26 20:13:47 +01:00
|
|
|
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
|
|
|
|
2020-05-23 01:55:38 +02:00
|
|
|
std::unique_ptr<Shader> null_shader;
|
|
|
|
std::unique_ptr<Shader> null_kernel;
|
2020-04-16 19:50:12 +02:00
|
|
|
|
2020-05-23 01:55:38 +02:00
|
|
|
std::array<Shader*, Maxwell::MaxShaderProgram> last_shaders{};
|
2018-08-23 23:30:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OpenGL
|