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>
|
2018-09-04 20:55:34 +02:00
|
|
|
#include <map>
|
2018-08-23 23:30:27 +02:00
|
|
|
#include <memory>
|
2019-01-05 05:00:06 +01:00
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
#include <glad/glad.h>
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2018-10-07 04:17:31 +02:00
|
|
|
#include "common/assert.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"
|
2018-08-23 23:30:27 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_gen.h"
|
|
|
|
|
|
|
|
namespace OpenGL {
|
|
|
|
|
|
|
|
class CachedShader;
|
2018-11-08 12:08:00 +01:00
|
|
|
class RasterizerOpenGL;
|
|
|
|
|
2018-08-23 23:30:27 +02:00
|
|
|
using Shader = std::shared_ptr<CachedShader>;
|
|
|
|
using Maxwell = Tegra::Engines::Maxwell3D::Regs;
|
|
|
|
|
2019-01-05 05:00:06 +01:00
|
|
|
struct BaseBindings {
|
|
|
|
u32 cbuf{};
|
|
|
|
u32 gmem{};
|
|
|
|
u32 sampler{};
|
|
|
|
|
|
|
|
bool operator<(const BaseBindings& rhs) const {
|
|
|
|
return std::tie(cbuf, gmem, sampler) < std::tie(rhs.cbuf, rhs.gmem, rhs.sampler);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-16 22:51:53 +02:00
|
|
|
class CachedShader final : public RasterizerCacheObject {
|
2018-08-23 23:30:27 +02:00
|
|
|
public:
|
2018-08-28 03:35:15 +02:00
|
|
|
CachedShader(VAddr addr, Maxwell::ShaderProgram program_type);
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2018-10-16 22:51:53 +02:00
|
|
|
VAddr GetAddr() const override {
|
2018-08-23 23:30:27 +02:00
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-10 01:28:58 +02:00
|
|
|
// We do not have to flush this cache as things in it are never modified by us.
|
2018-10-16 22:51:53 +02:00
|
|
|
void Flush() override {}
|
2018-10-10 01:28:58 +02:00
|
|
|
|
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-01-05 05:00:06 +01:00
|
|
|
std::tuple<GLuint, BaseBindings> GetProgramHandle(GLenum primitive_mode,
|
|
|
|
BaseBindings base_bindings);
|
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 {
|
|
|
|
OGLProgram points;
|
|
|
|
OGLProgram lines;
|
|
|
|
OGLProgram lines_adjacency;
|
|
|
|
OGLProgram triangles;
|
|
|
|
OGLProgram triangles_adjacency;
|
|
|
|
};
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2019-01-05 05:00:06 +01:00
|
|
|
std::string AllocateBindings(BaseBindings base_bindings);
|
2019-01-05 05:01:38 +01:00
|
|
|
|
2019-01-05 05:00:06 +01:00
|
|
|
GLuint GetGeometryShader(GLenum primitive_mode, BaseBindings base_bindings);
|
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-01-05 05:00:06 +01:00
|
|
|
GLuint LazyGeometryProgram(OGLProgram& target_program, BaseBindings base_bindings,
|
|
|
|
const std::string& glsl_topology, u32 max_vertices,
|
|
|
|
const std::string& debug_name);
|
2018-10-07 04:17:31 +02:00
|
|
|
|
2018-12-09 22:33:10 +01:00
|
|
|
void CalculateProperties();
|
|
|
|
|
2019-01-05 05:00:06 +01:00
|
|
|
VAddr addr{};
|
|
|
|
std::size_t shader_length{};
|
|
|
|
Maxwell::ShaderProgram program_type{};
|
2018-08-23 23:30:27 +02:00
|
|
|
GLShader::ShaderSetup setup;
|
|
|
|
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-01-05 05:00:06 +01:00
|
|
|
std::map<BaseBindings, OGLProgram> programs;
|
|
|
|
std::map<BaseBindings, GeometryPrograms> geometry_programs;
|
2018-10-07 04:17:31 +02:00
|
|
|
|
2019-01-05 05:01:38 +01:00
|
|
|
std::map<u32, GLuint> cbuf_resource_cache;
|
|
|
|
std::map<u32, GLuint> gmem_resource_cache;
|
2018-09-04 20:55:34 +02:00
|
|
|
std::map<u32, GLint> uniform_cache;
|
2018-08-23 23:30:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ShaderCacheOpenGL final : public RasterizerCache<Shader> {
|
|
|
|
public:
|
2018-11-08 12:08:00 +01:00
|
|
|
explicit ShaderCacheOpenGL(RasterizerOpenGL& rasterizer);
|
|
|
|
|
2019-01-14 02:05:53 +01:00
|
|
|
/// Loads disk cache for the current game
|
|
|
|
void LoadDiskCache();
|
|
|
|
|
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
|
|
|
|
|
|
|
private:
|
|
|
|
std::array<Shader, Maxwell::MaxShaderProgram> last_shaders;
|
2018-08-23 23:30:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OpenGL
|