renderer_opengl: Implement a new shader cache.

This commit is contained in:
bunnei 2018-08-23 17:30:27 -04:00
parent a0e1566dc5
commit b55d8111e6
9 changed files with 250 additions and 285 deletions

View File

@ -27,6 +27,8 @@ add_library(video_core STATIC
renderer_opengl/gl_rasterizer_cache.cpp renderer_opengl/gl_rasterizer_cache.cpp
renderer_opengl/gl_rasterizer_cache.h renderer_opengl/gl_rasterizer_cache.h
renderer_opengl/gl_resource_manager.h renderer_opengl/gl_resource_manager.h
renderer_opengl/gl_shader_cache.cpp
renderer_opengl/gl_shader_cache.h
renderer_opengl/gl_shader_decompiler.cpp renderer_opengl/gl_shader_decompiler.cpp
renderer_opengl/gl_shader_decompiler.h renderer_opengl/gl_shader_decompiler.h
renderer_opengl/gl_shader_gen.cpp renderer_opengl/gl_shader_gen.cpp

View File

@ -178,19 +178,6 @@ std::pair<u8*, GLintptr> RasterizerOpenGL::SetupVertexArrays(u8* array_ptr,
return {array_ptr, buffer_offset}; return {array_ptr, buffer_offset};
} }
static GLShader::ProgramCode GetShaderProgramCode(Maxwell::ShaderProgram program) {
auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
// Fetch program code from memory
GLShader::ProgramCode program_code(GLShader::MAX_PROGRAM_CODE_LENGTH);
auto& shader_config = gpu.regs.shader_config[static_cast<size_t>(program)];
const u64 gpu_address{gpu.regs.code_address.CodeAddress() + shader_config.offset};
const boost::optional<VAddr> cpu_address{gpu.memory_manager.GpuToCpuAddress(gpu_address)};
Memory::ReadBlock(*cpu_address, program_code.data(), program_code.size() * sizeof(u64));
return program_code;
}
std::pair<u8*, GLintptr> RasterizerOpenGL::SetupShaders(u8* buffer_ptr, GLintptr buffer_offset) { std::pair<u8*, GLintptr> RasterizerOpenGL::SetupShaders(u8* buffer_ptr, GLintptr buffer_offset) {
auto& gpu = Core::System::GetInstance().GPU().Maxwell3D(); auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
@ -224,31 +211,17 @@ std::pair<u8*, GLintptr> RasterizerOpenGL::SetupShaders(u8* buffer_ptr, GLintptr
buffer_ptr += sizeof(ubo); buffer_ptr += sizeof(ubo);
buffer_offset += sizeof(ubo); buffer_offset += sizeof(ubo);
GLShader::ShaderSetup setup{GetShaderProgramCode(program)}; const Tegra::GPUVAddr addr{gpu.regs.code_address.CodeAddress() + shader_config.offset};
GLShader::ShaderEntries shader_resources; Shader shader{shader_cache.GetStageProgram(program)};
switch (program) { switch (program) {
case Maxwell::ShaderProgram::VertexA: { case Maxwell::ShaderProgram::VertexA:
// VertexB is always enabled, so when VertexA is enabled, we have two vertex shaders.
// Conventional HW does not support this, so we combine VertexA and VertexB into one
// stage here.
setup.SetProgramB(GetShaderProgramCode(Maxwell::ShaderProgram::VertexB));
GLShader::MaxwellVSConfig vs_config{setup};
shader_resources =
shader_program_manager->UseProgrammableVertexShader(vs_config, setup);
break;
}
case Maxwell::ShaderProgram::VertexB: { case Maxwell::ShaderProgram::VertexB: {
GLShader::MaxwellVSConfig vs_config{setup}; shader_program_manager->UseProgrammableVertexShader(shader->GetProgramHandle());
shader_resources =
shader_program_manager->UseProgrammableVertexShader(vs_config, setup);
break; break;
} }
case Maxwell::ShaderProgram::Fragment: { case Maxwell::ShaderProgram::Fragment: {
GLShader::MaxwellFSConfig fs_config{setup}; shader_program_manager->UseProgrammableFragmentShader(shader->GetProgramHandle());
shader_resources =
shader_program_manager->UseProgrammableFragmentShader(fs_config, setup);
break; break;
} }
default: default:
@ -257,18 +230,14 @@ std::pair<u8*, GLintptr> RasterizerOpenGL::SetupShaders(u8* buffer_ptr, GLintptr
UNREACHABLE(); UNREACHABLE();
} }
GLuint gl_stage_program = shader_program_manager->GetCurrentProgramStage(
static_cast<Maxwell::ShaderStage>(stage));
// Configure the const buffers for this shader stage. // Configure the const buffers for this shader stage.
std::tie(buffer_ptr, buffer_offset, current_constbuffer_bindpoint) = SetupConstBuffers( std::tie(buffer_ptr, buffer_offset, current_constbuffer_bindpoint) =
buffer_ptr, buffer_offset, static_cast<Maxwell::ShaderStage>(stage), gl_stage_program, SetupConstBuffers(buffer_ptr, buffer_offset, static_cast<Maxwell::ShaderStage>(stage),
current_constbuffer_bindpoint, shader_resources.const_buffer_entries); shader, current_constbuffer_bindpoint);
// Configure the textures for this shader stage. // Configure the textures for this shader stage.
current_texture_bindpoint = current_texture_bindpoint = SetupTextures(static_cast<Maxwell::ShaderStage>(stage), shader,
SetupTextures(static_cast<Maxwell::ShaderStage>(stage), gl_stage_program, current_texture_bindpoint);
current_texture_bindpoint, shader_resources.texture_samplers);
// When VertexA is enabled, we have dual vertex shaders // When VertexA is enabled, we have dual vertex shaders
if (program == Maxwell::ShaderProgram::VertexA) { if (program == Maxwell::ShaderProgram::VertexA) {
@ -580,6 +549,7 @@ void RasterizerOpenGL::FlushRegion(Tegra::GPUVAddr addr, u64 size) {
void RasterizerOpenGL::InvalidateRegion(Tegra::GPUVAddr addr, u64 size) { void RasterizerOpenGL::InvalidateRegion(Tegra::GPUVAddr addr, u64 size) {
MICROPROFILE_SCOPE(OpenGL_CacheManagement); MICROPROFILE_SCOPE(OpenGL_CacheManagement);
res_cache.InvalidateRegion(addr, size); res_cache.InvalidateRegion(addr, size);
shader_cache.InvalidateRegion(addr, size);
} }
void RasterizerOpenGL::FlushAndInvalidateRegion(Tegra::GPUVAddr addr, u64 size) { void RasterizerOpenGL::FlushAndInvalidateRegion(Tegra::GPUVAddr addr, u64 size) {
@ -669,15 +639,17 @@ void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Tegra::Texture::TSCEntr
} }
} }
std::tuple<u8*, GLintptr, u32> RasterizerOpenGL::SetupConstBuffers( std::tuple<u8*, GLintptr, u32> RasterizerOpenGL::SetupConstBuffers(u8* buffer_ptr,
u8* buffer_ptr, GLintptr buffer_offset, Maxwell::ShaderStage stage, GLuint program, GLintptr buffer_offset,
u32 current_bindpoint, const std::vector<GLShader::ConstBufferEntry>& entries) { Maxwell::ShaderStage stage,
Shader& shader,
u32 current_bindpoint) {
const auto& gpu = Core::System::GetInstance().GPU(); const auto& gpu = Core::System::GetInstance().GPU();
const auto& maxwell3d = gpu.Maxwell3D(); const auto& maxwell3d = gpu.Maxwell3D();
const auto& shader_stage = maxwell3d.state.shader_stages[static_cast<size_t>(stage)];
const auto& entries = shader->GetShaderEntries().const_buffer_entries;
// Upload only the enabled buffers from the 16 constbuffers of each shader stage // Upload only the enabled buffers from the 16 constbuffers of each shader stage
const auto& shader_stage = maxwell3d.state.shader_stages[static_cast<size_t>(stage)];
for (u32 bindpoint = 0; bindpoint < entries.size(); ++bindpoint) { for (u32 bindpoint = 0; bindpoint < entries.size(); ++bindpoint) {
const auto& used_buffer = entries[bindpoint]; const auto& used_buffer = entries[bindpoint];
const auto& buffer = shader_stage.const_buffers[used_buffer.GetIndex()]; const auto& buffer = shader_stage.const_buffers[used_buffer.GetIndex()];
@ -716,12 +688,9 @@ std::tuple<u8*, GLintptr, u32> RasterizerOpenGL::SetupConstBuffers(
stream_buffer.GetHandle(), const_buffer_offset, size); stream_buffer.GetHandle(), const_buffer_offset, size);
// Now configure the bindpoint of the buffer inside the shader // Now configure the bindpoint of the buffer inside the shader
const std::string buffer_name = used_buffer.GetName(); glUniformBlockBinding(shader->GetProgramHandle(),
const GLuint index = shader->GetProgramResourceIndex(used_buffer.GetName()),
glGetProgramResourceIndex(program, GL_UNIFORM_BLOCK, buffer_name.c_str()); current_bindpoint + bindpoint);
if (index != GL_INVALID_INDEX) {
glUniformBlockBinding(program, index, current_bindpoint + bindpoint);
}
} }
state.Apply(); state.Apply();
@ -729,10 +698,10 @@ std::tuple<u8*, GLintptr, u32> RasterizerOpenGL::SetupConstBuffers(
return {buffer_ptr, buffer_offset, current_bindpoint + static_cast<u32>(entries.size())}; return {buffer_ptr, buffer_offset, current_bindpoint + static_cast<u32>(entries.size())};
} }
u32 RasterizerOpenGL::SetupTextures(Maxwell::ShaderStage stage, GLuint program, u32 current_unit, u32 RasterizerOpenGL::SetupTextures(Maxwell::ShaderStage stage, Shader& shader, u32 current_unit) {
const std::vector<GLShader::SamplerEntry>& entries) {
const auto& gpu = Core::System::GetInstance().GPU(); const auto& gpu = Core::System::GetInstance().GPU();
const auto& maxwell3d = gpu.Maxwell3D(); const auto& maxwell3d = gpu.Maxwell3D();
const auto& entries = shader->GetShaderEntries().texture_samplers;
ASSERT_MSG(current_unit + entries.size() <= std::size(state.texture_units), ASSERT_MSG(current_unit + entries.size() <= std::size(state.texture_units),
"Exceeded the number of active textures."); "Exceeded the number of active textures.");
@ -742,12 +711,9 @@ u32 RasterizerOpenGL::SetupTextures(Maxwell::ShaderStage stage, GLuint program,
u32 current_bindpoint = current_unit + bindpoint; u32 current_bindpoint = current_unit + bindpoint;
// Bind the uniform to the sampler. // Bind the uniform to the sampler.
GLint uniform = glGetUniformLocation(program, entry.GetName().c_str());
if (uniform == -1) {
continue;
}
glProgramUniform1i(program, uniform, current_bindpoint); glProgramUniform1i(shader->GetProgramHandle(), shader->GetUniformLocation(entry.GetName()),
current_bindpoint);
const auto texture = maxwell3d.GetStageTexture(entry.GetStage(), entry.GetOffset()); const auto texture = maxwell3d.GetStageTexture(entry.GetStage(), entry.GetOffset());

View File

@ -17,6 +17,7 @@
#include "video_core/rasterizer_interface.h" #include "video_core/rasterizer_interface.h"
#include "video_core/renderer_opengl/gl_rasterizer_cache.h" #include "video_core/renderer_opengl/gl_rasterizer_cache.h"
#include "video_core/renderer_opengl/gl_resource_manager.h" #include "video_core/renderer_opengl/gl_resource_manager.h"
#include "video_core/renderer_opengl/gl_shader_cache.h"
#include "video_core/renderer_opengl/gl_shader_gen.h" #include "video_core/renderer_opengl/gl_shader_gen.h"
#include "video_core/renderer_opengl/gl_shader_manager.h" #include "video_core/renderer_opengl/gl_shader_manager.h"
#include "video_core/renderer_opengl/gl_state.h" #include "video_core/renderer_opengl/gl_state.h"
@ -99,26 +100,23 @@ private:
/* /*
* Configures the current constbuffers to use for the draw command. * Configures the current constbuffers to use for the draw command.
* @param stage The shader stage to configure buffers for. * @param stage The shader stage to configure buffers for.
* @param program The OpenGL program object that contains the specified stage. * @param shader The shader object that contains the specified stage.
* @param current_bindpoint The offset at which to start counting new buffer bindpoints. * @param current_bindpoint The offset at which to start counting new buffer bindpoints.
* @param entries Vector describing the buffers that are actually used in the guest shader.
* @returns The next available bindpoint for use in the next shader stage. * @returns The next available bindpoint for use in the next shader stage.
*/ */
std::tuple<u8*, GLintptr, u32> SetupConstBuffers( std::tuple<u8*, GLintptr, u32> SetupConstBuffers(
u8* buffer_ptr, GLintptr buffer_offset, Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, u8* buffer_ptr, GLintptr buffer_offset, Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
GLuint program, u32 current_bindpoint, Shader& shader, u32 current_bindpoint);
const std::vector<GLShader::ConstBufferEntry>& entries);
/* /*
* Configures the current textures to use for the draw command. * Configures the current textures to use for the draw command.
* @param stage The shader stage to configure textures for. * @param stage The shader stage to configure textures for.
* @param program The OpenGL program object that contains the specified stage. * @param shader The shader object that contains the specified stage.
* @param current_unit The offset at which to start counting unused texture units. * @param current_unit The offset at which to start counting unused texture units.
* @param entries Vector describing the textures that are actually used in the guest shader.
* @returns The next available bindpoint for use in the next shader stage. * @returns The next available bindpoint for use in the next shader stage.
*/ */
u32 SetupTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, GLuint program, u32 SetupTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, Shader& shader,
u32 current_unit, const std::vector<GLShader::SamplerEntry>& entries); u32 current_unit);
/// Syncs the viewport to match the guest state /// Syncs the viewport to match the guest state
void SyncViewport(const MathUtil::Rectangle<u32>& surfaces_rect); void SyncViewport(const MathUtil::Rectangle<u32>& surfaces_rect);
@ -157,6 +155,7 @@ private:
OpenGLState state; OpenGLState state;
RasterizerCacheOpenGL res_cache; RasterizerCacheOpenGL res_cache;
ShaderCacheOpenGL shader_cache;
Core::Frontend::EmuWindow& emu_window; Core::Frontend::EmuWindow& emu_window;

View File

@ -0,0 +1,131 @@
// Copyright 2018 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "common/assert.h"
#include "core/core.h"
#include "core/memory.h"
#include "video_core/engines/maxwell_3d.h"
#include "video_core/renderer_opengl/gl_shader_cache.h"
#include "video_core/renderer_opengl/gl_shader_manager.h"
namespace OpenGL {
/// Gets the address for the specified shader stage program
static Tegra::GPUVAddr GetShaderAddress(Maxwell::ShaderProgram program) {
auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
GLShader::ProgramCode program_code(GLShader::MAX_PROGRAM_CODE_LENGTH);
auto& shader_config = gpu.regs.shader_config[static_cast<size_t>(program)];
return gpu.regs.code_address.CodeAddress() + shader_config.offset;
}
/// Gets the shader program code from memory for the specified address
static GLShader::ProgramCode GetShaderCode(Tegra::GPUVAddr addr) {
auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
GLShader::ProgramCode program_code(GLShader::MAX_PROGRAM_CODE_LENGTH);
const boost::optional<VAddr> cpu_address{gpu.memory_manager.GpuToCpuAddress(addr)};
Memory::ReadBlock(*cpu_address, program_code.data(), program_code.size() * sizeof(u64));
return program_code;
}
/// Helper function to set shader uniform block bindings for a single shader stage
static void SetShaderUniformBlockBinding(GLuint shader, const char* name,
Maxwell::ShaderStage binding, size_t expected_size) {
const GLuint ub_index = glGetUniformBlockIndex(shader, name);
if (ub_index == GL_INVALID_INDEX) {
return;
}
GLint ub_size = 0;
glGetActiveUniformBlockiv(shader, ub_index, GL_UNIFORM_BLOCK_DATA_SIZE, &ub_size);
ASSERT_MSG(static_cast<size_t>(ub_size) == expected_size,
"Uniform block size did not match! Got {}, expected {}", ub_size, expected_size);
glUniformBlockBinding(shader, ub_index, static_cast<GLuint>(binding));
}
/// Sets shader uniform block bindings for an entire shader program
static void SetShaderUniformBlockBindings(GLuint shader) {
SetShaderUniformBlockBinding(shader, "vs_config", Maxwell::ShaderStage::Vertex,
sizeof(GLShader::MaxwellUniformData));
SetShaderUniformBlockBinding(shader, "gs_config", Maxwell::ShaderStage::Geometry,
sizeof(GLShader::MaxwellUniformData));
SetShaderUniformBlockBinding(shader, "fs_config", Maxwell::ShaderStage::Fragment,
sizeof(GLShader::MaxwellUniformData));
}
CachedShader::CachedShader(Tegra::GPUVAddr addr, Maxwell::ShaderProgram program_type)
: addr{addr}, program_type{program_type}, setup{GetShaderCode(addr)} {
GLShader::ProgramResult program_result;
GLenum gl_type{};
switch (program_type) {
case Maxwell::ShaderProgram::VertexA:
// VertexB is always enabled, so when VertexA is enabled, we have two vertex shaders.
// Conventional HW does not support this, so we combine VertexA and VertexB into one
// stage here.
setup.SetProgramB(GetShaderCode(GetShaderAddress(Maxwell::ShaderProgram::VertexB)));
case Maxwell::ShaderProgram::VertexB:
program_result = GLShader::GenerateVertexShader(setup);
gl_type = GL_VERTEX_SHADER;
break;
case Maxwell::ShaderProgram::Fragment:
program_result = GLShader::GenerateFragmentShader(setup);
gl_type = GL_FRAGMENT_SHADER;
break;
default:
LOG_CRITICAL(HW_GPU, "Unimplemented program_type={}", static_cast<u32>(program_type));
UNREACHABLE();
return;
}
entries = program_result.second;
OGLShader shader;
shader.Create(program_result.first.c_str(), gl_type);
program.Create(true, shader.handle);
SetShaderUniformBlockBindings(program.handle);
}
GLuint CachedShader::GetProgramResourceIndex(const std::string& name) {
auto search{resource_cache.find(name)};
if (search == resource_cache.end()) {
const GLuint index{
glGetProgramResourceIndex(program.handle, GL_UNIFORM_BLOCK, name.c_str())};
resource_cache[name] = index;
return index;
}
return search->second;
}
GLint CachedShader::GetUniformLocation(const std::string& name) {
auto search{uniform_cache.find(name)};
if (search == uniform_cache.end()) {
const GLint index{glGetUniformLocation(program.handle, name.c_str())};
uniform_cache[name] = index;
return index;
}
return search->second;
}
Shader ShaderCacheOpenGL::GetStageProgram(Maxwell::ShaderProgram program) {
const Tegra::GPUVAddr program_addr{GetShaderAddress(program)};
// Look up shader in the cache based on address
Shader shader{TryGet(program_addr)};
if (!shader) {
// No shader found - create a new one
shader = std::make_shared<CachedShader>(program_addr, program);
Register(shader);
}
return shader;
}
} // namespace OpenGL

View File

@ -0,0 +1,69 @@
// Copyright 2018 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <memory>
#include <unordered_map>
#include "common/common_types.h"
#include "video_core/memory_manager.h"
#include "video_core/rasterizer_cache.h"
#include "video_core/renderer_opengl/gl_resource_manager.h"
#include "video_core/renderer_opengl/gl_shader_gen.h"
namespace OpenGL {
class CachedShader;
using Shader = std::shared_ptr<CachedShader>;
using Maxwell = Tegra::Engines::Maxwell3D::Regs;
class CachedShader final {
public:
CachedShader(Tegra::GPUVAddr addr, Maxwell::ShaderProgram program_type);
/// Gets the address of the shader in guest memory, required for cache management
Tegra::GPUVAddr GetAddr() const {
return addr;
}
/// Gets the size of the shader in guest memory, required for cache management
size_t GetSizeInBytes() const {
return sizeof(GLShader::ProgramCode);
}
/// Gets the shader entries for the shader
const GLShader::ShaderEntries& GetShaderEntries() const {
return entries;
}
/// Gets the GL program handle for the shader
GLuint GetProgramHandle() const {
return program.handle;
}
/// Gets the GL program resource location for the specified resource, caching as needed
GLuint GetProgramResourceIndex(const std::string& name);
/// Gets the GL uniform location for the specified resource, caching as needed
GLint GetUniformLocation(const std::string& name);
private:
Tegra::GPUVAddr addr;
Maxwell::ShaderProgram program_type;
GLShader::ShaderSetup setup;
GLShader::ShaderEntries entries;
OGLProgram program;
std::unordered_map<std::string, GLuint> resource_cache;
std::unordered_map<std::string, GLint> uniform_cache;
};
class ShaderCacheOpenGL final : public RasterizerCache<Shader> {
public:
/// Gets the current specified shader stage program
Shader GetStageProgram(Maxwell::ShaderProgram program);
};
} // namespace OpenGL

View File

@ -13,7 +13,7 @@ using Tegra::Engines::Maxwell3D;
static constexpr u32 PROGRAM_OFFSET{10}; static constexpr u32 PROGRAM_OFFSET{10};
ProgramResult GenerateVertexShader(const ShaderSetup& setup, const MaxwellVSConfig& config) { ProgramResult GenerateVertexShader(const ShaderSetup& setup) {
std::string out = "#version 430 core\n"; std::string out = "#version 430 core\n";
out += "#extension GL_ARB_separate_shader_objects : enable\n\n"; out += "#extension GL_ARB_separate_shader_objects : enable\n\n";
out += Decompiler::GetCommonDeclarations(); out += Decompiler::GetCommonDeclarations();
@ -75,7 +75,7 @@ void main() {
return {out, program.second}; return {out, program.second};
} }
ProgramResult GenerateFragmentShader(const ShaderSetup& setup, const MaxwellFSConfig& config) { ProgramResult GenerateFragmentShader(const ShaderSetup& setup) {
std::string out = "#version 430 core\n"; std::string out = "#version 430 core\n";
out += "#extension GL_ARB_separate_shader_objects : enable\n\n"; out += "#extension GL_ARB_separate_shader_objects : enable\n\n";
out += Decompiler::GetCommonDeclarations(); out += Decompiler::GetCommonDeclarations();

View File

@ -6,12 +6,9 @@
#include <array> #include <array>
#include <string> #include <string>
#include <type_traits>
#include <utility>
#include <vector> #include <vector>
#include <boost/functional/hash.hpp>
#include "common/common_types.h" #include "common/common_types.h"
#include "common/hash.h"
namespace OpenGL::GLShader { namespace OpenGL::GLShader {
@ -124,18 +121,8 @@ struct ShaderSetup {
ProgramCode code_b; // Used for dual vertex shaders ProgramCode code_b; // Used for dual vertex shaders
} program; } program;
bool program_code_hash_dirty = true;
u64 GetProgramCodeHash() {
if (program_code_hash_dirty) {
program_code_hash = GetNewHash();
program_code_hash_dirty = false;
}
return program_code_hash;
}
/// Used in scenarios where we have a dual vertex shaders /// Used in scenarios where we have a dual vertex shaders
void SetProgramB(ProgramCode program_b) { void SetProgramB(ProgramCode&& program_b) {
program.code_b = std::move(program_b); program.code_b = std::move(program_b);
has_program_b = true; has_program_b = true;
} }
@ -145,73 +132,19 @@ struct ShaderSetup {
} }
private: private:
u64 GetNewHash() const {
size_t hash = 0;
const u64 hash_a = Common::ComputeHash64(program.code.data(), program.code.size());
boost::hash_combine(hash, hash_a);
if (has_program_b) {
// Compute hash over dual shader programs
const u64 hash_b = Common::ComputeHash64(program.code_b.data(), program.code_b.size());
boost::hash_combine(hash, hash_b);
}
return hash;
}
u64 program_code_hash{};
bool has_program_b{}; bool has_program_b{};
}; };
struct MaxwellShaderConfigCommon {
void Init(ShaderSetup& setup) {
program_hash = setup.GetProgramCodeHash();
}
u64 program_hash;
};
struct MaxwellVSConfig : Common::HashableStruct<MaxwellShaderConfigCommon> {
explicit MaxwellVSConfig(ShaderSetup& setup) {
state.Init(setup);
}
};
struct MaxwellFSConfig : Common::HashableStruct<MaxwellShaderConfigCommon> {
explicit MaxwellFSConfig(ShaderSetup& setup) {
state.Init(setup);
}
};
/** /**
* Generates the GLSL vertex shader program source code for the given VS program * Generates the GLSL vertex shader program source code for the given VS program
* @returns String of the shader source code * @returns String of the shader source code
*/ */
ProgramResult GenerateVertexShader(const ShaderSetup& setup, const MaxwellVSConfig& config); ProgramResult GenerateVertexShader(const ShaderSetup& setup);
/** /**
* Generates the GLSL fragment shader program source code for the given FS program * Generates the GLSL fragment shader program source code for the given FS program
* @returns String of the shader source code * @returns String of the shader source code
*/ */
ProgramResult GenerateFragmentShader(const ShaderSetup& setup, const MaxwellFSConfig& config); ProgramResult GenerateFragmentShader(const ShaderSetup& setup);
} // namespace OpenGL::GLShader } // namespace OpenGL::GLShader
namespace std {
template <>
struct hash<OpenGL::GLShader::MaxwellVSConfig> {
size_t operator()(const OpenGL::GLShader::MaxwellVSConfig& k) const {
return k.Hash();
}
};
template <>
struct hash<OpenGL::GLShader::MaxwellFSConfig> {
size_t operator()(const OpenGL::GLShader::MaxwellFSConfig& k) const {
return k.Hash();
}
};
} // namespace std

View File

@ -3,39 +3,10 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "core/core.h" #include "core/core.h"
#include "core/hle/kernel/process.h"
#include "video_core/engines/maxwell_3d.h"
#include "video_core/renderer_opengl/gl_shader_manager.h" #include "video_core/renderer_opengl/gl_shader_manager.h"
namespace OpenGL::GLShader { namespace OpenGL::GLShader {
namespace Impl {
static void SetShaderUniformBlockBinding(GLuint shader, const char* name,
Maxwell3D::Regs::ShaderStage binding,
size_t expected_size) {
const GLuint ub_index = glGetUniformBlockIndex(shader, name);
if (ub_index == GL_INVALID_INDEX) {
return;
}
GLint ub_size = 0;
glGetActiveUniformBlockiv(shader, ub_index, GL_UNIFORM_BLOCK_DATA_SIZE, &ub_size);
ASSERT_MSG(static_cast<size_t>(ub_size) == expected_size,
"Uniform block size did not match! Got {}, expected {}", ub_size, expected_size);
glUniformBlockBinding(shader, ub_index, static_cast<GLuint>(binding));
}
void SetShaderUniformBlockBindings(GLuint shader) {
SetShaderUniformBlockBinding(shader, "vs_config", Maxwell3D::Regs::ShaderStage::Vertex,
sizeof(MaxwellUniformData));
SetShaderUniformBlockBinding(shader, "gs_config", Maxwell3D::Regs::ShaderStage::Geometry,
sizeof(MaxwellUniformData));
SetShaderUniformBlockBinding(shader, "fs_config", Maxwell3D::Regs::ShaderStage::Fragment,
sizeof(MaxwellUniformData));
}
} // namespace Impl
void MaxwellUniformData::SetFromRegs(const Maxwell3D::State::ShaderStageInfo& shader_stage) { void MaxwellUniformData::SetFromRegs(const Maxwell3D::State::ShaderStageInfo& shader_stage) {
const auto& gpu = Core::System::GetInstance().GPU().Maxwell3D(); const auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
const auto& regs = gpu.regs; const auto& regs = gpu.regs;

View File

@ -4,12 +4,9 @@
#pragma once #pragma once
#include <tuple>
#include <unordered_map>
#include <boost/functional/hash.hpp>
#include <glad/glad.h> #include <glad/glad.h>
#include "video_core/renderer_opengl/gl_resource_manager.h" #include "video_core/renderer_opengl/gl_resource_manager.h"
#include "video_core/renderer_opengl/gl_shader_gen.h"
#include "video_core/renderer_opengl/maxwell_to_gl.h" #include "video_core/renderer_opengl/maxwell_to_gl.h"
namespace OpenGL::GLShader { namespace OpenGL::GLShader {
@ -19,10 +16,6 @@ static constexpr size_t NumTextureSamplers = 32;
using Tegra::Engines::Maxwell3D; using Tegra::Engines::Maxwell3D;
namespace Impl {
void SetShaderUniformBlockBindings(GLuint shader);
} // namespace Impl
/// Uniform structure for the Uniform Buffer Object, all vectors must be 16-byte aligned /// Uniform structure for the Uniform Buffer Object, all vectors must be 16-byte aligned
// NOTE: Always keep a vec4 at the end. The GL spec is not clear whether the alignment at // NOTE: Always keep a vec4 at the end. The GL spec is not clear whether the alignment at
// the end of a uniform block is included in UNIFORM_BLOCK_DATA_SIZE or not. // the end of a uniform block is included in UNIFORM_BLOCK_DATA_SIZE or not.
@ -36,102 +29,22 @@ static_assert(sizeof(MaxwellUniformData) == 32, "MaxwellUniformData structure si
static_assert(sizeof(MaxwellUniformData) < 16384, static_assert(sizeof(MaxwellUniformData) < 16384,
"MaxwellUniformData structure must be less than 16kb as per the OpenGL spec"); "MaxwellUniformData structure must be less than 16kb as per the OpenGL spec");
class OGLShaderStage {
public:
OGLShaderStage() = default;
void Create(const ProgramResult& program_result, GLenum type) {
OGLShader shader;
shader.Create(program_result.first.c_str(), type);
program.Create(true, shader.handle);
Impl::SetShaderUniformBlockBindings(program.handle);
entries = program_result.second;
}
GLuint GetHandle() const {
return program.handle;
}
ShaderEntries GetEntries() const {
return entries;
}
private:
OGLProgram program;
ShaderEntries entries;
};
// TODO(wwylele): beautify this doc
// This is a shader cache designed for translating PICA shader to GLSL shader.
// The double cache is needed because diffent KeyConfigType, which includes a hash of the code
// region (including its leftover unused code) can generate the same GLSL code.
template <typename KeyConfigType,
ProgramResult (*CodeGenerator)(const ShaderSetup&, const KeyConfigType&),
GLenum ShaderType>
class ShaderCache {
public:
ShaderCache() = default;
using Result = std::pair<GLuint, ShaderEntries>;
Result Get(const KeyConfigType& key, const ShaderSetup& setup) {
auto map_it = shader_map.find(key);
if (map_it == shader_map.end()) {
ProgramResult program = CodeGenerator(setup, key);
auto [iter, new_shader] = shader_cache.emplace(program.first, OGLShaderStage{});
OGLShaderStage& cached_shader = iter->second;
if (new_shader) {
cached_shader.Create(program, ShaderType);
}
shader_map[key] = &cached_shader;
return {cached_shader.GetHandle(), program.second};
} else {
return {map_it->second->GetHandle(), map_it->second->GetEntries()};
}
}
private:
std::unordered_map<KeyConfigType, OGLShaderStage*> shader_map;
std::unordered_map<std::string, OGLShaderStage> shader_cache;
};
using VertexShaders = ShaderCache<MaxwellVSConfig, &GenerateVertexShader, GL_VERTEX_SHADER>;
using FragmentShaders = ShaderCache<MaxwellFSConfig, &GenerateFragmentShader, GL_FRAGMENT_SHADER>;
class ProgramManager { class ProgramManager {
public: public:
ProgramManager() { ProgramManager() {
pipeline.Create(); pipeline.Create();
} }
ShaderEntries UseProgrammableVertexShader(const MaxwellVSConfig& config, void UseProgrammableVertexShader(GLuint program) {
const ShaderSetup& setup) { vs = program;
ShaderEntries result;
std::tie(current.vs, result) = vertex_shaders.Get(config, setup);
return result;
} }
ShaderEntries UseProgrammableFragmentShader(const MaxwellFSConfig& config, void UseProgrammableFragmentShader(GLuint program) {
const ShaderSetup& setup) { fs = program;
ShaderEntries result;
std::tie(current.fs, result) = fragment_shaders.Get(config, setup);
return result;
}
GLuint GetCurrentProgramStage(Maxwell3D::Regs::ShaderStage stage) const {
switch (stage) {
case Maxwell3D::Regs::ShaderStage::Vertex:
return current.vs;
case Maxwell3D::Regs::ShaderStage::Fragment:
return current.fs;
}
UNREACHABLE();
} }
void UseTrivialGeometryShader() { void UseTrivialGeometryShader() {
current.gs = 0; gs = 0;
} }
void ApplyTo(OpenGLState& state) { void ApplyTo(OpenGLState& state) {
@ -140,35 +53,16 @@ public:
GL_VERTEX_SHADER_BIT | GL_GEOMETRY_SHADER_BIT | GL_FRAGMENT_SHADER_BIT, GL_VERTEX_SHADER_BIT | GL_GEOMETRY_SHADER_BIT | GL_FRAGMENT_SHADER_BIT,
0); 0);
glUseProgramStages(pipeline.handle, GL_VERTEX_SHADER_BIT, current.vs); glUseProgramStages(pipeline.handle, GL_VERTEX_SHADER_BIT, vs);
glUseProgramStages(pipeline.handle, GL_GEOMETRY_SHADER_BIT, current.gs); glUseProgramStages(pipeline.handle, GL_GEOMETRY_SHADER_BIT, gs);
glUseProgramStages(pipeline.handle, GL_FRAGMENT_SHADER_BIT, current.fs); glUseProgramStages(pipeline.handle, GL_FRAGMENT_SHADER_BIT, fs);
state.draw.shader_program = 0; state.draw.shader_program = 0;
state.draw.program_pipeline = pipeline.handle; state.draw.program_pipeline = pipeline.handle;
} }
private: private:
struct ShaderTuple {
GLuint vs = 0, gs = 0, fs = 0;
bool operator==(const ShaderTuple& rhs) const {
return std::tie(vs, gs, fs) == std::tie(rhs.vs, rhs.gs, rhs.fs);
}
struct Hash {
std::size_t operator()(const ShaderTuple& tuple) const {
std::size_t hash = 0;
boost::hash_combine(hash, tuple.vs);
boost::hash_combine(hash, tuple.gs);
boost::hash_combine(hash, tuple.fs);
return hash;
}
};
};
ShaderTuple current;
VertexShaders vertex_shaders;
FragmentShaders fragment_shaders;
std::unordered_map<ShaderTuple, OGLProgram, ShaderTuple::Hash> program_cache;
OGLPipeline pipeline; OGLPipeline pipeline;
GLuint vs{}, fs{}, gs{};
}; };
} // namespace OpenGL::GLShader } // namespace OpenGL::GLShader