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.
|
|
|
|
|
2020-02-26 20:13:47 +01:00
|
|
|
#include <atomic>
|
2021-05-26 01:39:55 +02:00
|
|
|
#include <fstream>
|
2020-02-26 20:13:47 +01:00
|
|
|
#include <functional>
|
2019-04-06 22:59:56 +02:00
|
|
|
#include <mutex>
|
2019-09-25 04:34:18 +02:00
|
|
|
#include <string>
|
2019-04-06 22:59:56 +02:00
|
|
|
#include <thread>
|
2020-02-26 20:13:47 +01:00
|
|
|
|
2019-11-13 03:39:45 +01:00
|
|
|
#include "common/alignment.h"
|
2018-08-23 23:30:27 +02:00
|
|
|
#include "common/assert.h"
|
2021-05-26 01:39:55 +02:00
|
|
|
#include "common/fs/fs.h"
|
|
|
|
#include "common/fs/path_util.h"
|
2019-11-13 03:39:45 +01:00
|
|
|
#include "common/logging/log.h"
|
2019-04-06 22:59:56 +02:00
|
|
|
#include "common/scope_exit.h"
|
2021-06-22 07:12:11 +02:00
|
|
|
#include "common/settings.h"
|
2021-05-26 01:39:55 +02:00
|
|
|
#include "common/thread_worker.h"
|
2018-08-23 23:30:27 +02:00
|
|
|
#include "core/core.h"
|
2021-05-08 21:34:41 +02:00
|
|
|
#include "shader_recompiler/backend/glasm/emit_glasm.h"
|
2021-05-20 03:58:32 +02:00
|
|
|
#include "shader_recompiler/backend/glsl/emit_glsl.h"
|
2021-05-23 09:28:34 +02:00
|
|
|
#include "shader_recompiler/backend/spirv/emit_spirv.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/program.h"
|
|
|
|
#include "shader_recompiler/frontend/maxwell/control_flow.h"
|
2021-06-16 06:49:19 +02:00
|
|
|
#include "shader_recompiler/frontend/maxwell/translate_program.h"
|
2021-05-23 09:28:34 +02:00
|
|
|
#include "shader_recompiler/profile.h"
|
2019-09-23 21:40:58 +02:00
|
|
|
#include "video_core/engines/kepler_compute.h"
|
2018-08-23 23:30:27 +02:00
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
2019-04-06 05:59:54 +02:00
|
|
|
#include "video_core/memory_manager.h"
|
2018-11-08 12:08:00 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_rasterizer.h"
|
2020-08-24 02:23:54 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_resource_manager.h"
|
2018-08-23 23:30:27 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_cache.h"
|
2021-06-06 06:28:22 +02:00
|
|
|
#include "video_core/renderer_opengl/gl_shader_util.h"
|
2019-12-29 06:03:05 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_state_tracker.h"
|
2020-05-23 01:55:38 +02:00
|
|
|
#include "video_core/shader_cache.h"
|
2021-05-23 09:28:34 +02:00
|
|
|
#include "video_core/shader_environment.h"
|
2020-07-10 05:36:38 +02:00
|
|
|
#include "video_core/shader_notify.h"
|
2018-08-23 23:30:27 +02:00
|
|
|
|
|
|
|
namespace OpenGL {
|
2021-05-23 09:28:34 +02:00
|
|
|
namespace {
|
2021-05-08 21:34:41 +02:00
|
|
|
using Shader::Backend::GLASM::EmitGLASM;
|
2021-05-20 03:58:32 +02:00
|
|
|
using Shader::Backend::GLSL::EmitGLSL;
|
2021-05-23 09:28:34 +02:00
|
|
|
using Shader::Backend::SPIRV::EmitSPIRV;
|
2021-06-04 00:11:16 +02:00
|
|
|
using Shader::Maxwell::MergeDualVertexPrograms;
|
2021-05-23 09:28:34 +02:00
|
|
|
using Shader::Maxwell::TranslateProgram;
|
|
|
|
using VideoCommon::ComputeEnvironment;
|
2021-05-26 01:39:55 +02:00
|
|
|
using VideoCommon::FileEnvironment;
|
|
|
|
using VideoCommon::GenericEnvironment;
|
2021-05-23 09:28:34 +02:00
|
|
|
using VideoCommon::GraphicsEnvironment;
|
2021-05-27 22:51:00 +02:00
|
|
|
using VideoCommon::SerializePipeline;
|
2021-06-15 03:06:29 +02:00
|
|
|
using Context = ShaderContext::Context;
|
2021-05-23 09:28:34 +02:00
|
|
|
|
|
|
|
template <typename Container>
|
|
|
|
auto MakeSpan(Container& container) {
|
|
|
|
return std::span(container.data(), container.size());
|
|
|
|
}
|
|
|
|
|
2021-05-25 23:58:28 +02:00
|
|
|
Shader::RuntimeInfo MakeRuntimeInfo(const GraphicsPipelineKey& key,
|
2021-05-26 23:32:59 +02:00
|
|
|
const Shader::IR::Program& program,
|
2021-06-16 09:59:30 +02:00
|
|
|
const Shader::IR::Program* previous_program,
|
2021-06-02 06:33:03 +02:00
|
|
|
bool glasm_use_storage_buffers, bool use_assembly_shaders) {
|
2021-05-21 07:57:52 +02:00
|
|
|
Shader::RuntimeInfo info;
|
2021-06-16 09:59:30 +02:00
|
|
|
if (previous_program) {
|
2021-06-24 07:41:09 +02:00
|
|
|
info.previous_stage_stores = previous_program->info.stores;
|
2021-06-16 09:59:30 +02:00
|
|
|
} else {
|
2021-06-24 07:41:09 +02:00
|
|
|
// Mark all stores as available for vertex shaders
|
|
|
|
info.previous_stage_stores.mask.set();
|
2021-06-16 09:59:30 +02:00
|
|
|
}
|
2021-05-21 07:57:52 +02:00
|
|
|
switch (program.stage) {
|
2021-06-02 06:33:03 +02:00
|
|
|
case Shader::Stage::VertexB:
|
|
|
|
case Shader::Stage::Geometry:
|
|
|
|
if (!use_assembly_shaders && key.xfb_enabled != 0) {
|
|
|
|
info.xfb_varyings = VideoCommon::MakeTransformFeedbackVaryings(key.xfb_state);
|
|
|
|
}
|
|
|
|
break;
|
2021-05-21 07:57:52 +02:00
|
|
|
case Shader::Stage::TessellationEval:
|
2021-05-24 08:24:19 +02:00
|
|
|
info.tess_clockwise = key.tessellation_clockwise != 0;
|
2021-05-21 07:57:52 +02:00
|
|
|
info.tess_primitive = [&key] {
|
|
|
|
switch (key.tessellation_primitive) {
|
|
|
|
case Maxwell::TessellationPrimitive::Isolines:
|
|
|
|
return Shader::TessPrimitive::Isolines;
|
|
|
|
case Maxwell::TessellationPrimitive::Triangles:
|
|
|
|
return Shader::TessPrimitive::Triangles;
|
|
|
|
case Maxwell::TessellationPrimitive::Quads:
|
|
|
|
return Shader::TessPrimitive::Quads;
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return Shader::TessPrimitive::Triangles;
|
|
|
|
}();
|
|
|
|
info.tess_spacing = [&] {
|
|
|
|
switch (key.tessellation_spacing) {
|
|
|
|
case Maxwell::TessellationSpacing::Equal:
|
|
|
|
return Shader::TessSpacing::Equal;
|
|
|
|
case Maxwell::TessellationSpacing::FractionalOdd:
|
|
|
|
return Shader::TessSpacing::FractionalOdd;
|
|
|
|
case Maxwell::TessellationSpacing::FractionalEven:
|
|
|
|
return Shader::TessSpacing::FractionalEven;
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return Shader::TessSpacing::Equal;
|
|
|
|
}();
|
|
|
|
break;
|
2021-05-21 23:27:37 +02:00
|
|
|
case Shader::Stage::Fragment:
|
|
|
|
info.force_early_z = key.early_z != 0;
|
2021-05-21 07:57:52 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (key.gs_input_topology) {
|
|
|
|
case Maxwell::PrimitiveTopology::Points:
|
|
|
|
info.input_topology = Shader::InputTopology::Points;
|
|
|
|
break;
|
|
|
|
case Maxwell::PrimitiveTopology::Lines:
|
|
|
|
case Maxwell::PrimitiveTopology::LineLoop:
|
|
|
|
case Maxwell::PrimitiveTopology::LineStrip:
|
|
|
|
info.input_topology = Shader::InputTopology::Lines;
|
|
|
|
break;
|
|
|
|
case Maxwell::PrimitiveTopology::Triangles:
|
|
|
|
case Maxwell::PrimitiveTopology::TriangleStrip:
|
|
|
|
case Maxwell::PrimitiveTopology::TriangleFan:
|
|
|
|
case Maxwell::PrimitiveTopology::Quads:
|
|
|
|
case Maxwell::PrimitiveTopology::QuadStrip:
|
|
|
|
case Maxwell::PrimitiveTopology::Polygon:
|
|
|
|
case Maxwell::PrimitiveTopology::Patches:
|
|
|
|
info.input_topology = Shader::InputTopology::Triangles;
|
|
|
|
break;
|
|
|
|
case Maxwell::PrimitiveTopology::LinesAdjacency:
|
|
|
|
case Maxwell::PrimitiveTopology::LineStripAdjacency:
|
|
|
|
info.input_topology = Shader::InputTopology::LinesAdjacency;
|
|
|
|
break;
|
|
|
|
case Maxwell::PrimitiveTopology::TrianglesAdjacency:
|
|
|
|
case Maxwell::PrimitiveTopology::TriangleStripAdjacency:
|
|
|
|
info.input_topology = Shader::InputTopology::TrianglesAdjacency;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-26 23:32:59 +02:00
|
|
|
info.glasm_use_storage_buffers = glasm_use_storage_buffers;
|
2021-05-21 07:57:52 +02:00
|
|
|
return info;
|
|
|
|
}
|
2021-05-21 23:17:53 +02:00
|
|
|
|
|
|
|
void SetXfbState(VideoCommon::TransformFeedbackState& state, const Maxwell& regs) {
|
|
|
|
std::ranges::transform(regs.tfb_layouts, state.layouts.begin(), [](const auto& layout) {
|
|
|
|
return VideoCommon::TransformFeedbackState::Layout{
|
|
|
|
.stream = layout.stream,
|
|
|
|
.varying_count = layout.varying_count,
|
|
|
|
.stride = layout.stride,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
state.varyings = regs.tfb_varying_locs;
|
|
|
|
}
|
2021-05-23 09:28:34 +02:00
|
|
|
} // Anonymous namespace
|
2018-08-23 23:30:27 +02:00
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
ShaderCache::ShaderCache(RasterizerOpenGL& rasterizer_, Core::Frontend::EmuWindow& emu_window_,
|
2021-05-23 09:28:34 +02:00
|
|
|
Tegra::Engines::Maxwell3D& maxwell3d_,
|
2021-04-26 08:53:26 +02:00
|
|
|
Tegra::Engines::KeplerCompute& kepler_compute_,
|
2021-05-23 09:28:34 +02:00
|
|
|
Tegra::MemoryManager& gpu_memory_, const Device& device_,
|
|
|
|
TextureCache& texture_cache_, BufferCache& buffer_cache_,
|
2021-06-15 03:06:29 +02:00
|
|
|
ProgramManager& program_manager_, StateTracker& state_tracker_,
|
|
|
|
VideoCore::ShaderNotify& shader_notify_)
|
2021-04-26 08:53:26 +02:00
|
|
|
: VideoCommon::ShaderCache{rasterizer_, gpu_memory_, maxwell3d_, kepler_compute_},
|
2021-05-23 09:28:34 +02:00
|
|
|
emu_window{emu_window_}, device{device_}, texture_cache{texture_cache_},
|
2021-06-06 05:35:57 +02:00
|
|
|
buffer_cache{buffer_cache_}, program_manager{program_manager_}, state_tracker{state_tracker_},
|
2021-06-15 03:06:29 +02:00
|
|
|
shader_notify{shader_notify_}, use_asynchronous_shaders{device.UseAsynchronousShaders()},
|
2021-06-06 05:35:57 +02:00
|
|
|
profile{
|
|
|
|
.supported_spirv = 0x00010000,
|
|
|
|
|
|
|
|
.unified_descriptor_binding = false,
|
|
|
|
.support_descriptor_aliasing = false,
|
|
|
|
.support_int8 = false,
|
|
|
|
.support_int16 = false,
|
|
|
|
.support_vertex_instance_id = true,
|
|
|
|
.support_float_controls = false,
|
|
|
|
.support_separate_denorm_behavior = false,
|
|
|
|
.support_separate_rounding_mode = false,
|
|
|
|
.support_fp16_denorm_preserve = false,
|
|
|
|
.support_fp32_denorm_preserve = false,
|
|
|
|
.support_fp16_denorm_flush = false,
|
|
|
|
.support_fp32_denorm_flush = false,
|
|
|
|
.support_fp16_signed_zero_nan_preserve = false,
|
|
|
|
.support_fp32_signed_zero_nan_preserve = false,
|
|
|
|
.support_fp64_signed_zero_nan_preserve = false,
|
|
|
|
.support_explicit_workgroup_layout = false,
|
|
|
|
.support_vote = true,
|
|
|
|
.support_viewport_index_layer_non_geometry =
|
|
|
|
device.HasNvViewportArray2() || device.HasVertexViewportLayer(),
|
|
|
|
.support_viewport_mask = device.HasNvViewportArray2(),
|
|
|
|
.support_typeless_image_loads = device.HasImageLoadFormatted(),
|
|
|
|
.support_demote_to_helper_invocation = false,
|
|
|
|
.support_int64_atomics = false,
|
|
|
|
.support_derivative_control = device.HasDerivativeControl(),
|
2021-06-24 22:40:24 +02:00
|
|
|
.support_geometry_shader_passthrough = device.HasGeometryShaderPassthrough(),
|
2021-05-26 01:55:40 +02:00
|
|
|
.support_gl_nv_gpu_shader_5 = device.HasNvGpuShader5(),
|
|
|
|
.support_gl_amd_gpu_shader_half_float = device.HasAmdShaderHalfFloat(),
|
2021-06-04 06:46:46 +02:00
|
|
|
.support_gl_texture_shadow_lod = device.HasTextureShadowLod(),
|
2021-06-08 07:55:12 +02:00
|
|
|
.support_gl_warp_intrinsics = false,
|
2021-06-13 06:05:19 +02:00
|
|
|
.support_gl_variable_aoffi = device.HasVariableAoffi(),
|
|
|
|
.support_gl_sparse_textures = device.HasSparseTexture2(),
|
2021-06-15 05:33:26 +02:00
|
|
|
.support_gl_derivative_control = device.HasDerivativeControl(),
|
2021-06-06 05:35:57 +02:00
|
|
|
|
2021-05-30 06:53:26 +02:00
|
|
|
.warp_size_potentially_larger_than_guest = device.IsWarpSizePotentiallyLargerThanGuest(),
|
2021-06-06 05:35:57 +02:00
|
|
|
|
|
|
|
.lower_left_origin_mode = true,
|
|
|
|
.need_declared_frag_colors = true,
|
2021-06-15 23:23:57 +02:00
|
|
|
.need_fastmath_off = device.NeedsFastmathOff(),
|
2021-06-06 05:35:57 +02:00
|
|
|
|
|
|
|
.has_broken_spirv_clamp = true,
|
|
|
|
.has_broken_unsigned_image_offsets = true,
|
|
|
|
.has_broken_signed_operations = true,
|
2021-06-29 23:42:17 +02:00
|
|
|
.has_broken_fp16_float_controls = false,
|
2021-06-15 05:02:07 +02:00
|
|
|
.has_gl_component_indexing_bug = device.HasComponentIndexingBug(),
|
|
|
|
.has_gl_precise_bug = device.HasPreciseBug(),
|
2021-06-06 05:35:57 +02:00
|
|
|
.ignore_nan_fp_comparisons = true,
|
2021-06-16 08:03:08 +02:00
|
|
|
},
|
|
|
|
host_info{
|
|
|
|
.support_float16 = false,
|
2021-06-23 06:39:21 +02:00
|
|
|
.support_int64 = device.HasShaderInt64(),
|
2021-06-06 05:35:57 +02:00
|
|
|
} {
|
|
|
|
if (use_asynchronous_shaders) {
|
|
|
|
workers = CreateWorkers();
|
|
|
|
}
|
2021-05-22 01:46:40 +02:00
|
|
|
}
|
2021-04-26 08:53:26 +02:00
|
|
|
|
|
|
|
ShaderCache::~ShaderCache() = default;
|
2019-01-14 04:58:15 +01:00
|
|
|
|
2021-05-26 01:39:55 +02:00
|
|
|
void ShaderCache::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
|
|
|
|
const VideoCore::DiskResourceLoadCallback& callback) {
|
|
|
|
if (title_id == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-12 06:46:30 +02:00
|
|
|
const auto shader_dir{Common::FS::GetYuzuPath(Common::FS::YuzuPath::ShaderDir)};
|
|
|
|
const auto base_dir{shader_dir / fmt::format("{:016x}", title_id)};
|
|
|
|
if (!Common::FS::CreateDir(shader_dir) || !Common::FS::CreateDir(base_dir)) {
|
|
|
|
LOG_ERROR(Common_Filesystem, "Failed to create shader cache directories");
|
2021-05-26 01:39:55 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-06-12 06:46:30 +02:00
|
|
|
shader_cache_filename = base_dir / "opengl.bin";
|
2021-05-26 01:39:55 +02:00
|
|
|
|
2021-06-06 05:35:57 +02:00
|
|
|
if (!workers) {
|
|
|
|
workers = CreateWorkers();
|
|
|
|
}
|
2021-05-26 01:39:55 +02:00
|
|
|
struct {
|
|
|
|
std::mutex mutex;
|
2021-06-06 05:35:57 +02:00
|
|
|
size_t total{};
|
|
|
|
size_t built{};
|
|
|
|
bool has_loaded{};
|
2021-05-26 01:39:55 +02:00
|
|
|
} state;
|
|
|
|
|
|
|
|
const auto load_compute{[&](std::ifstream& file, FileEnvironment env) {
|
|
|
|
ComputePipelineKey key;
|
|
|
|
file.read(reinterpret_cast<char*>(&key), sizeof(key));
|
2021-06-06 05:35:57 +02:00
|
|
|
workers->QueueWork(
|
2021-05-26 01:39:55 +02:00
|
|
|
[this, key, env = std::move(env), &state, &callback](Context* ctx) mutable {
|
|
|
|
ctx->pools.ReleaseContents();
|
2021-05-27 22:51:00 +02:00
|
|
|
auto pipeline{CreateComputePipeline(ctx->pools, key, env)};
|
2021-05-26 01:39:55 +02:00
|
|
|
std::lock_guard lock{state.mutex};
|
2021-05-27 22:51:00 +02:00
|
|
|
if (pipeline) {
|
|
|
|
compute_cache.emplace(key, std::move(pipeline));
|
|
|
|
}
|
2021-05-26 01:39:55 +02:00
|
|
|
++state.built;
|
|
|
|
if (state.has_loaded) {
|
|
|
|
callback(VideoCore::LoadCallbackStage::Build, state.built, state.total);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
++state.total;
|
|
|
|
}};
|
|
|
|
const auto load_graphics{[&](std::ifstream& file, std::vector<FileEnvironment> envs) {
|
|
|
|
GraphicsPipelineKey key;
|
|
|
|
file.read(reinterpret_cast<char*>(&key), sizeof(key));
|
2021-06-06 05:35:57 +02:00
|
|
|
workers->QueueWork(
|
2021-05-26 01:39:55 +02:00
|
|
|
[this, key, envs = std::move(envs), &state, &callback](Context* ctx) mutable {
|
|
|
|
boost::container::static_vector<Shader::Environment*, 5> env_ptrs;
|
|
|
|
for (auto& env : envs) {
|
|
|
|
env_ptrs.push_back(&env);
|
|
|
|
}
|
|
|
|
ctx->pools.ReleaseContents();
|
2021-06-15 03:06:29 +02:00
|
|
|
auto pipeline{CreateGraphicsPipeline(ctx->pools, key, MakeSpan(env_ptrs), false)};
|
2021-05-26 01:39:55 +02:00
|
|
|
std::lock_guard lock{state.mutex};
|
2021-05-27 22:51:00 +02:00
|
|
|
if (pipeline) {
|
|
|
|
graphics_cache.emplace(key, std::move(pipeline));
|
|
|
|
}
|
2021-05-26 01:39:55 +02:00
|
|
|
++state.built;
|
|
|
|
if (state.has_loaded) {
|
|
|
|
callback(VideoCore::LoadCallbackStage::Build, state.built, state.total);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
++state.total;
|
|
|
|
}};
|
|
|
|
VideoCommon::LoadPipelines(stop_loading, shader_cache_filename, load_compute, load_graphics);
|
|
|
|
|
|
|
|
std::unique_lock lock{state.mutex};
|
|
|
|
callback(VideoCore::LoadCallbackStage::Build, 0, state.total);
|
|
|
|
state.has_loaded = true;
|
|
|
|
lock.unlock();
|
|
|
|
|
2021-06-06 05:35:57 +02:00
|
|
|
workers->WaitForRequests();
|
|
|
|
if (!use_asynchronous_shaders) {
|
|
|
|
workers.reset();
|
|
|
|
}
|
2021-05-26 01:39:55 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 23:58:28 +02:00
|
|
|
GraphicsPipeline* ShaderCache::CurrentGraphicsPipeline() {
|
2021-05-23 09:28:34 +02:00
|
|
|
if (!RefreshStages(graphics_key.unique_hashes)) {
|
2021-06-20 07:23:50 +02:00
|
|
|
current_pipeline = nullptr;
|
2021-05-23 09:28:34 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const auto& regs{maxwell3d.regs};
|
|
|
|
graphics_key.raw = 0;
|
|
|
|
graphics_key.early_z.Assign(regs.force_early_fragment_tests != 0 ? 1 : 0);
|
|
|
|
graphics_key.gs_input_topology.Assign(graphics_key.unique_hashes[4] != 0
|
|
|
|
? regs.draw.topology.Value()
|
|
|
|
: Maxwell::PrimitiveTopology{});
|
|
|
|
graphics_key.tessellation_primitive.Assign(regs.tess_mode.prim.Value());
|
|
|
|
graphics_key.tessellation_spacing.Assign(regs.tess_mode.spacing.Value());
|
|
|
|
graphics_key.tessellation_clockwise.Assign(regs.tess_mode.cw.Value());
|
2021-05-21 23:17:53 +02:00
|
|
|
graphics_key.xfb_enabled.Assign(regs.tfb_enabled != 0 ? 1 : 0);
|
|
|
|
if (graphics_key.xfb_enabled) {
|
|
|
|
SetXfbState(graphics_key.xfb_state, regs);
|
|
|
|
}
|
2021-06-20 07:23:50 +02:00
|
|
|
if (current_pipeline && graphics_key == current_pipeline->Key()) {
|
|
|
|
return current_pipeline->IsBuilt() ? current_pipeline : nullptr;
|
|
|
|
}
|
|
|
|
return CurrentGraphicsPipelineSlowPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsPipeline* ShaderCache::CurrentGraphicsPipelineSlowPath() {
|
2021-05-23 09:28:34 +02:00
|
|
|
const auto [pair, is_new]{graphics_cache.try_emplace(graphics_key)};
|
2021-06-20 07:23:50 +02:00
|
|
|
auto& pipeline{pair->second};
|
2021-05-23 09:28:34 +02:00
|
|
|
if (is_new) {
|
2021-06-20 07:23:50 +02:00
|
|
|
pipeline = CreateGraphicsPipeline();
|
2021-05-23 09:28:34 +02:00
|
|
|
}
|
2021-07-02 02:32:30 +02:00
|
|
|
if (!pipeline) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-06-20 07:23:50 +02:00
|
|
|
current_pipeline = pipeline.get();
|
2021-07-02 02:32:30 +02:00
|
|
|
return BuiltPipeline(current_pipeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsPipeline* ShaderCache::BuiltPipeline(GraphicsPipeline* pipeline) const noexcept {
|
|
|
|
if (pipeline->IsBuilt()) {
|
|
|
|
return pipeline;
|
|
|
|
}
|
|
|
|
if (!use_asynchronous_shaders) {
|
|
|
|
return pipeline;
|
|
|
|
}
|
|
|
|
// If something is using depth, we can assume that games are not rendering anything which
|
|
|
|
// will be used one time.
|
|
|
|
if (maxwell3d.regs.zeta_enable) {
|
2021-06-15 03:06:29 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-07-02 02:32:30 +02:00
|
|
|
// If games are using a small index count, we can assume these are full screen quads.
|
|
|
|
// Usually these shaders are only used once for building textures so we can assume they
|
|
|
|
// can't be built async
|
|
|
|
if (maxwell3d.regs.index_array.count <= 6 || maxwell3d.regs.vertex_buffer.count <= 6) {
|
|
|
|
return pipeline;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2021-05-23 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 23:58:28 +02:00
|
|
|
ComputePipeline* ShaderCache::CurrentComputePipeline() {
|
2021-05-23 09:28:34 +02:00
|
|
|
const VideoCommon::ShaderInfo* const shader{ComputeShader()};
|
|
|
|
if (!shader) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const auto& qmd{kepler_compute.launch_description};
|
2021-05-25 23:58:28 +02:00
|
|
|
const ComputePipelineKey key{
|
2021-05-23 09:28:34 +02:00
|
|
|
.unique_hash = shader->unique_hash,
|
|
|
|
.shared_memory_size = qmd.shared_alloc,
|
|
|
|
.workgroup_size{qmd.block_dim_x, qmd.block_dim_y, qmd.block_dim_z},
|
|
|
|
};
|
|
|
|
const auto [pair, is_new]{compute_cache.try_emplace(key)};
|
|
|
|
auto& pipeline{pair->second};
|
|
|
|
if (!is_new) {
|
|
|
|
return pipeline.get();
|
|
|
|
}
|
2021-05-25 23:58:28 +02:00
|
|
|
pipeline = CreateComputePipeline(key, shader);
|
2021-05-23 09:28:34 +02:00
|
|
|
return pipeline.get();
|
|
|
|
}
|
|
|
|
|
2021-05-25 23:58:28 +02:00
|
|
|
std::unique_ptr<GraphicsPipeline> ShaderCache::CreateGraphicsPipeline() {
|
2021-05-23 09:28:34 +02:00
|
|
|
GraphicsEnvironments environments;
|
|
|
|
GetGraphicsEnvironments(environments, graphics_key.unique_hashes);
|
|
|
|
|
|
|
|
main_pools.ReleaseContents();
|
2021-06-15 03:06:29 +02:00
|
|
|
auto pipeline{CreateGraphicsPipeline(main_pools, graphics_key, environments.Span(),
|
|
|
|
use_asynchronous_shaders)};
|
2021-05-27 22:51:00 +02:00
|
|
|
if (!pipeline || shader_cache_filename.empty()) {
|
2021-05-26 01:39:55 +02:00
|
|
|
return pipeline;
|
|
|
|
}
|
|
|
|
boost::container::static_vector<const GenericEnvironment*, Maxwell::MaxShaderProgram> env_ptrs;
|
|
|
|
for (size_t index = 0; index < Maxwell::MaxShaderProgram; ++index) {
|
|
|
|
if (graphics_key.unique_hashes[index] != 0) {
|
|
|
|
env_ptrs.push_back(&environments.envs[index]);
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 22:51:00 +02:00
|
|
|
SerializePipeline(graphics_key, env_ptrs, shader_cache_filename);
|
2021-05-26 01:39:55 +02:00
|
|
|
return pipeline;
|
2021-05-23 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 23:58:28 +02:00
|
|
|
std::unique_ptr<GraphicsPipeline> ShaderCache::CreateGraphicsPipeline(
|
2021-06-15 03:06:29 +02:00
|
|
|
ShaderContext::ShaderPools& pools, const GraphicsPipelineKey& key,
|
|
|
|
std::span<Shader::Environment* const> envs, bool build_in_parallel) try {
|
2021-05-23 09:28:34 +02:00
|
|
|
LOG_INFO(Render_OpenGL, "0x{:016x}", key.Hash());
|
2021-05-26 23:32:59 +02:00
|
|
|
size_t env_index{};
|
|
|
|
u32 total_storage_buffers{};
|
2021-05-23 09:28:34 +02:00
|
|
|
std::array<Shader::IR::Program, Maxwell::MaxShaderProgram> programs;
|
2021-06-04 00:11:16 +02:00
|
|
|
const bool uses_vertex_a{key.unique_hashes[0] != 0};
|
|
|
|
const bool uses_vertex_b{key.unique_hashes[1] != 0};
|
2021-05-23 09:28:34 +02:00
|
|
|
for (size_t index = 0; index < Maxwell::MaxShaderProgram; ++index) {
|
|
|
|
if (key.unique_hashes[index] == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Shader::Environment& env{*envs[env_index]};
|
|
|
|
++env_index;
|
|
|
|
|
|
|
|
const u32 cfg_offset{static_cast<u32>(env.StartAddress() + sizeof(Shader::ProgramHeader))};
|
2021-06-04 00:11:16 +02:00
|
|
|
Shader::Maxwell::Flow::CFG cfg(env, pools.flow_block, cfg_offset, index == 0);
|
|
|
|
if (!uses_vertex_a || index != 1) {
|
|
|
|
// Normal path
|
2021-06-16 08:03:08 +02:00
|
|
|
programs[index] = TranslateProgram(pools.inst, pools.block, env, cfg, host_info);
|
2021-05-26 23:32:59 +02:00
|
|
|
|
2021-06-04 00:11:16 +02:00
|
|
|
for (const auto& desc : programs[index].info.storage_buffers_descriptors) {
|
|
|
|
total_storage_buffers += desc.count;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// VertexB path when VertexA is present.
|
2021-06-16 08:03:08 +02:00
|
|
|
auto& program_va{programs[0]};
|
|
|
|
auto program_vb{TranslateProgram(pools.inst, pools.block, env, cfg, host_info)};
|
2021-06-04 00:11:16 +02:00
|
|
|
for (const auto& desc : program_vb.info.storage_buffers_descriptors) {
|
|
|
|
total_storage_buffers += desc.count;
|
|
|
|
}
|
|
|
|
programs[index] = MergeDualVertexPrograms(program_va, program_vb, env);
|
2021-05-26 23:32:59 +02:00
|
|
|
}
|
2021-05-23 09:28:34 +02:00
|
|
|
}
|
2021-05-26 23:32:59 +02:00
|
|
|
const u32 glasm_storage_buffer_limit{device.GetMaxGLASMStorageBufferBlocks()};
|
|
|
|
const bool glasm_use_storage_buffers{total_storage_buffers <= glasm_storage_buffer_limit};
|
|
|
|
|
2021-05-23 09:28:34 +02:00
|
|
|
std::array<const Shader::Info*, Maxwell::MaxShaderStage> infos{};
|
|
|
|
|
2021-05-15 23:19:08 +02:00
|
|
|
OGLProgram source_program;
|
2021-06-15 03:06:29 +02:00
|
|
|
std::array<std::string, 5> sources;
|
2021-06-22 07:12:11 +02:00
|
|
|
std::array<std::vector<u32>, 5> sources_spirv;
|
2021-05-04 01:53:00 +02:00
|
|
|
Shader::Backend::Bindings binding;
|
2021-06-16 09:59:30 +02:00
|
|
|
Shader::IR::Program* previous_program{};
|
2021-06-02 06:33:03 +02:00
|
|
|
const bool use_glasm{device.UseAssemblyShaders()};
|
2021-06-06 06:28:22 +02:00
|
|
|
const size_t first_index = uses_vertex_a && uses_vertex_b ? 1 : 0;
|
|
|
|
for (size_t index = first_index; index < Maxwell::MaxShaderProgram; ++index) {
|
2021-05-23 09:28:34 +02:00
|
|
|
if (key.unique_hashes[index] == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
UNIMPLEMENTED_IF(index == 0);
|
|
|
|
|
|
|
|
Shader::IR::Program& program{programs[index]};
|
|
|
|
const size_t stage_index{index - 1};
|
|
|
|
infos[stage_index] = &program.info;
|
2021-05-21 07:57:52 +02:00
|
|
|
|
2021-06-02 06:33:03 +02:00
|
|
|
const auto runtime_info{
|
2021-06-16 09:59:30 +02:00
|
|
|
MakeRuntimeInfo(key, program, previous_program, glasm_use_storage_buffers, use_glasm)};
|
2021-06-22 07:12:11 +02:00
|
|
|
switch (device.GetShaderBackend()) {
|
|
|
|
case Settings::ShaderBackend::GLSL:
|
2021-06-15 03:06:29 +02:00
|
|
|
sources[stage_index] = EmitGLSL(profile, runtime_info, program, binding);
|
2021-06-22 07:12:11 +02:00
|
|
|
break;
|
|
|
|
case Settings::ShaderBackend::GLASM:
|
|
|
|
sources[stage_index] = EmitGLASM(profile, runtime_info, program, binding);
|
|
|
|
break;
|
|
|
|
case Settings::ShaderBackend::SPIRV:
|
|
|
|
sources_spirv[stage_index] = EmitSPIRV(profile, runtime_info, program, binding);
|
|
|
|
break;
|
2021-05-15 23:19:08 +02:00
|
|
|
}
|
2021-06-16 09:59:30 +02:00
|
|
|
previous_program = &program;
|
2021-05-23 09:28:34 +02:00
|
|
|
}
|
2021-06-15 03:06:29 +02:00
|
|
|
auto* const thread_worker{build_in_parallel ? workers.get() : nullptr};
|
2021-06-22 07:12:11 +02:00
|
|
|
return std::make_unique<GraphicsPipeline>(
|
|
|
|
device, texture_cache, buffer_cache, gpu_memory, maxwell3d, program_manager, state_tracker,
|
|
|
|
thread_worker, &shader_notify, sources, sources_spirv, infos, key);
|
2021-05-27 22:51:00 +02:00
|
|
|
|
|
|
|
} catch (Shader::Exception& exception) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "{}", exception.what());
|
|
|
|
return nullptr;
|
2021-05-23 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 23:58:28 +02:00
|
|
|
std::unique_ptr<ComputePipeline> ShaderCache::CreateComputePipeline(
|
|
|
|
const ComputePipelineKey& key, const VideoCommon::ShaderInfo* shader) {
|
2021-05-23 09:28:34 +02:00
|
|
|
const GPUVAddr program_base{kepler_compute.regs.code_loc.Address()};
|
|
|
|
const auto& qmd{kepler_compute.launch_description};
|
|
|
|
ComputeEnvironment env{kepler_compute, gpu_memory, program_base, qmd.program_start};
|
|
|
|
env.SetCachedSize(shader->size_bytes);
|
|
|
|
|
|
|
|
main_pools.ReleaseContents();
|
2021-05-27 22:51:00 +02:00
|
|
|
auto pipeline{CreateComputePipeline(main_pools, key, env)};
|
|
|
|
if (!pipeline || shader_cache_filename.empty()) {
|
|
|
|
return pipeline;
|
2021-05-26 01:39:55 +02:00
|
|
|
}
|
2021-05-27 22:51:00 +02:00
|
|
|
SerializePipeline(key, std::array<const GenericEnvironment*, 1>{&env}, shader_cache_filename);
|
2021-05-26 01:39:55 +02:00
|
|
|
return pipeline;
|
2021-05-23 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 03:06:29 +02:00
|
|
|
std::unique_ptr<ComputePipeline> ShaderCache::CreateComputePipeline(
|
|
|
|
ShaderContext::ShaderPools& pools, const ComputePipelineKey& key,
|
|
|
|
Shader::Environment& env) try {
|
2021-05-23 09:28:34 +02:00
|
|
|
LOG_INFO(Render_OpenGL, "0x{:016x}", key.Hash());
|
|
|
|
|
|
|
|
Shader::Maxwell::Flow::CFG cfg{env, pools.flow_block, env.StartAddress()};
|
2021-06-16 08:03:08 +02:00
|
|
|
auto program{TranslateProgram(pools.inst, pools.block, env, cfg, host_info)};
|
2021-05-26 23:32:59 +02:00
|
|
|
|
|
|
|
u32 num_storage_buffers{};
|
|
|
|
for (const auto& desc : program.info.storage_buffers_descriptors) {
|
|
|
|
num_storage_buffers += desc.count;
|
|
|
|
}
|
|
|
|
Shader::RuntimeInfo info;
|
|
|
|
info.glasm_use_storage_buffers = num_storage_buffers <= device.GetMaxGLASMStorageBufferBlocks();
|
2021-06-22 07:12:11 +02:00
|
|
|
|
|
|
|
std::string code{};
|
|
|
|
std::vector<u32> code_spirv;
|
|
|
|
switch (device.GetShaderBackend()) {
|
|
|
|
case Settings::ShaderBackend::GLSL:
|
|
|
|
code = EmitGLSL(profile, program);
|
|
|
|
break;
|
|
|
|
case Settings::ShaderBackend::GLASM:
|
|
|
|
code = EmitGLASM(profile, info, program);
|
|
|
|
break;
|
|
|
|
case Settings::ShaderBackend::SPIRV:
|
|
|
|
code_spirv = EmitSPIRV(profile, program);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-26 23:32:59 +02:00
|
|
|
return std::make_unique<ComputePipeline>(device, texture_cache, buffer_cache, gpu_memory,
|
2021-06-22 07:12:11 +02:00
|
|
|
kepler_compute, program_manager, program.info, code,
|
|
|
|
code_spirv);
|
2021-05-27 22:51:00 +02:00
|
|
|
} catch (Shader::Exception& exception) {
|
|
|
|
LOG_ERROR(Render_OpenGL, "{}", exception.what());
|
|
|
|
return nullptr;
|
2021-05-23 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 03:06:29 +02:00
|
|
|
std::unique_ptr<ShaderWorker> ShaderCache::CreateWorkers() const {
|
|
|
|
return std::make_unique<ShaderWorker>(std::max(std::thread::hardware_concurrency(), 2U) - 1,
|
|
|
|
"yuzu:ShaderBuilder",
|
|
|
|
[this] { return Context{emu_window}; });
|
2021-06-06 05:35:57 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 23:19:28 +02:00
|
|
|
} // namespace OpenGL
|