yuzu-emu
/
yuzu-android
Archived
1
0
Fork 0

Merge pull request #2563 from ReinUsesLisp/shader-initializers

gl_shader_cache: Use static constructors for CachedShader initialization
This commit is contained in:
Zach Hilman 2019-07-03 20:20:05 -04:00 committed by GitHub
commit da5a537029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 52 deletions

View File

@ -221,44 +221,37 @@ std::set<GLenum> GetSupportedFormats() {
} // Anonymous namespace } // Anonymous namespace
CachedShader::CachedShader(const Device& device, VAddr cpu_addr, u64 unique_identifier, CachedShader::CachedShader(const ShaderParameters& params, Maxwell::ShaderProgram program_type,
Maxwell::ShaderProgram program_type, ShaderDiskCacheOpenGL& disk_cache, GLShader::ProgramResult result)
const PrecompiledPrograms& precompiled_programs, : RasterizerCacheObject{params.host_ptr}, host_ptr{params.host_ptr}, cpu_addr{params.cpu_addr},
ProgramCode&& program_code, ProgramCode&& program_code_b, u8* host_ptr) unique_identifier{params.unique_identifier}, program_type{program_type},
: RasterizerCacheObject{host_ptr}, host_ptr{host_ptr}, cpu_addr{cpu_addr}, disk_cache{params.disk_cache}, precompiled_programs{params.precompiled_programs},
unique_identifier{unique_identifier}, program_type{program_type}, disk_cache{disk_cache}, entries{result.second}, code{std::move(result.first)}, shader_length{entries.shader_length} {}
precompiled_programs{precompiled_programs} {
const std::size_t code_size{CalculateProgramSize(program_code)}; Shader CachedShader::CreateStageFromMemory(const ShaderParameters& params,
const std::size_t code_size_b{program_code_b.empty() ? 0 Maxwell::ShaderProgram program_type,
: CalculateProgramSize(program_code_b)}; ProgramCode&& program_code,
GLShader::ProgramResult program_result{ ProgramCode&& program_code_b) {
CreateProgram(device, program_type, program_code, program_code_b)}; const auto code_size{CalculateProgramSize(program_code)};
if (program_result.first.empty()) { const auto code_size_b{CalculateProgramSize(program_code_b)};
auto result{CreateProgram(params.device, program_type, program_code, program_code_b)};
if (result.first.empty()) {
// TODO(Rodrigo): Unimplemented shader stages hit here, avoid using these for now // TODO(Rodrigo): Unimplemented shader stages hit here, avoid using these for now
return; return {};
} }
code = program_result.first; params.disk_cache.SaveRaw(ShaderDiskCacheRaw(
entries = program_result.second; params.unique_identifier, program_type, static_cast<u32>(code_size / sizeof(u64)),
shader_length = entries.shader_length; static_cast<u32>(code_size_b / sizeof(u64)), std::move(program_code),
std::move(program_code_b)));
const ShaderDiskCacheRaw raw(unique_identifier, program_type, return std::make_shared<CachedShader>(params, program_type, std::move(result));
static_cast<u32>(code_size / sizeof(u64)),
static_cast<u32>(code_size_b / sizeof(u64)),
std::move(program_code), std::move(program_code_b));
disk_cache.SaveRaw(raw);
} }
CachedShader::CachedShader(VAddr cpu_addr, u64 unique_identifier, Shader CachedShader::CreateStageFromCache(const ShaderParameters& params,
Maxwell::ShaderProgram program_type, ShaderDiskCacheOpenGL& disk_cache, Maxwell::ShaderProgram program_type,
const PrecompiledPrograms& precompiled_programs, GLShader::ProgramResult result) {
GLShader::ProgramResult result, u8* host_ptr) return std::make_shared<CachedShader>(params, program_type, std::move(result));
: RasterizerCacheObject{host_ptr}, cpu_addr{cpu_addr}, unique_identifier{unique_identifier},
program_type{program_type}, disk_cache{disk_cache}, precompiled_programs{
precompiled_programs} {
code = std::move(result.first);
entries = result.second;
shader_length = entries.shader_length;
} }
std::tuple<GLuint, BaseBindings> CachedShader::GetProgramHandle(GLenum primitive_mode, std::tuple<GLuint, BaseBindings> CachedShader::GetProgramHandle(GLenum primitive_mode,
@ -570,18 +563,17 @@ Shader ShaderCacheOpenGL::GetStageProgram(Maxwell::ShaderProgram program) {
memory_manager.GetPointer(program_addr_b)); memory_manager.GetPointer(program_addr_b));
} }
const u64 unique_identifier = GetUniqueIdentifier(program, program_code, program_code_b); const auto unique_identifier = GetUniqueIdentifier(program, program_code, program_code_b);
const VAddr cpu_addr{*memory_manager.GpuToCpuAddress(program_addr)}; const auto cpu_addr{*memory_manager.GpuToCpuAddress(program_addr)};
const ShaderParameters params{disk_cache, precompiled_programs, device, cpu_addr,
host_ptr, unique_identifier};
const auto found = precompiled_shaders.find(unique_identifier); const auto found = precompiled_shaders.find(unique_identifier);
if (found != precompiled_shaders.end()) { if (found == precompiled_shaders.end()) {
// Create a shader from the cache shader = CachedShader::CreateStageFromMemory(params, program, std::move(program_code),
shader = std::make_shared<CachedShader>(cpu_addr, unique_identifier, program, disk_cache, std::move(program_code_b));
precompiled_programs, found->second, host_ptr);
} else { } else {
// Create a shader from guest memory shader = CachedShader::CreateStageFromCache(params, program, found->second);
shader = std::make_shared<CachedShader>(
device, cpu_addr, unique_identifier, program, disk_cache, precompiled_programs,
std::move(program_code), std::move(program_code_b), host_ptr);
} }
Register(shader); Register(shader);

View File

@ -41,17 +41,27 @@ using Maxwell = Tegra::Engines::Maxwell3D::Regs;
using PrecompiledPrograms = std::unordered_map<ShaderDiskCacheUsage, CachedProgram>; using PrecompiledPrograms = std::unordered_map<ShaderDiskCacheUsage, CachedProgram>;
using PrecompiledShaders = std::unordered_map<u64, GLShader::ProgramResult>; using PrecompiledShaders = std::unordered_map<u64, GLShader::ProgramResult>;
struct ShaderParameters {
ShaderDiskCacheOpenGL& disk_cache;
const PrecompiledPrograms& precompiled_programs;
const Device& device;
VAddr cpu_addr;
u8* host_ptr;
u64 unique_identifier;
};
class CachedShader final : public RasterizerCacheObject { class CachedShader final : public RasterizerCacheObject {
public: public:
explicit CachedShader(const Device& device, VAddr cpu_addr, u64 unique_identifier, explicit CachedShader(const ShaderParameters& params, Maxwell::ShaderProgram program_type,
Maxwell::ShaderProgram program_type, ShaderDiskCacheOpenGL& disk_cache, GLShader::ProgramResult result);
const PrecompiledPrograms& precompiled_programs,
ProgramCode&& program_code, ProgramCode&& program_code_b, u8* host_ptr);
explicit CachedShader(VAddr cpu_addr, u64 unique_identifier, static Shader CreateStageFromMemory(const ShaderParameters& params,
Maxwell::ShaderProgram program_type, ShaderDiskCacheOpenGL& disk_cache, Maxwell::ShaderProgram program_type,
const PrecompiledPrograms& precompiled_programs, ProgramCode&& program_code, ProgramCode&& program_code_b);
GLShader::ProgramResult result, u8* host_ptr);
static Shader CreateStageFromCache(const ShaderParameters& params,
Maxwell::ShaderProgram program_type,
GLShader::ProgramResult result);
VAddr GetCpuAddr() const override { VAddr GetCpuAddr() const override {
return cpu_addr; return cpu_addr;
@ -99,10 +109,9 @@ private:
ShaderDiskCacheOpenGL& disk_cache; ShaderDiskCacheOpenGL& disk_cache;
const PrecompiledPrograms& precompiled_programs; const PrecompiledPrograms& precompiled_programs;
std::size_t shader_length{};
GLShader::ShaderEntries entries; GLShader::ShaderEntries entries;
std::string code; std::string code;
std::size_t shader_length{};
std::unordered_map<BaseBindings, CachedProgram> programs; std::unordered_map<BaseBindings, CachedProgram> programs;
std::unordered_map<BaseBindings, GeometryPrograms> geometry_programs; std::unordered_map<BaseBindings, GeometryPrograms> geometry_programs;