2020-01-07 01:18:38 +01:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-01-07 01:55:06 +01:00
|
|
|
#include <algorithm>
|
2020-01-07 01:18:38 +01:00
|
|
|
#include <cstddef>
|
2021-03-23 01:03:20 +01:00
|
|
|
#include <fstream>
|
2020-01-07 01:55:06 +01:00
|
|
|
#include <memory>
|
2021-04-05 08:56:58 +02:00
|
|
|
#include <thread>
|
2020-01-07 01:18:38 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2020-11-25 06:33:20 +01:00
|
|
|
#include "common/bit_cast.h"
|
2020-12-30 06:25:23 +01:00
|
|
|
#include "common/cityhash.h"
|
2021-04-26 08:53:26 +02:00
|
|
|
#include "common/fs/fs.h"
|
|
|
|
#include "common/fs/path_util.h"
|
2020-01-07 01:55:06 +01:00
|
|
|
#include "common/microprofile.h"
|
2021-03-23 01:03:20 +01:00
|
|
|
#include "common/thread_worker.h"
|
2020-01-07 01:55:06 +01:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/memory.h"
|
2021-03-19 23:28:31 +01:00
|
|
|
#include "shader_recompiler/backend/spirv/emit_spirv.h"
|
2021-02-17 04:59:28 +01:00
|
|
|
#include "shader_recompiler/environment.h"
|
2021-03-19 23:28:31 +01:00
|
|
|
#include "shader_recompiler/frontend/maxwell/control_flow.h"
|
|
|
|
#include "shader_recompiler/frontend/maxwell/program.h"
|
|
|
|
#include "shader_recompiler/program_header.h"
|
2021-04-24 23:27:25 +02:00
|
|
|
#include "video_core/dirty_flags.h"
|
2020-01-07 01:55:06 +01:00
|
|
|
#include "video_core/engines/kepler_compute.h"
|
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
|
|
|
#include "video_core/memory_manager.h"
|
|
|
|
#include "video_core/renderer_vulkan/fixed_pipeline_state.h"
|
|
|
|
#include "video_core/renderer_vulkan/maxwell_to_vk.h"
|
2021-03-26 22:45:38 +01:00
|
|
|
#include "video_core/renderer_vulkan/pipeline_helper.h"
|
2020-01-07 01:55:06 +01:00
|
|
|
#include "video_core/renderer_vulkan/vk_compute_pipeline.h"
|
|
|
|
#include "video_core/renderer_vulkan/vk_descriptor_pool.h"
|
2020-01-07 01:18:38 +01:00
|
|
|
#include "video_core/renderer_vulkan/vk_pipeline_cache.h"
|
2020-01-07 01:55:06 +01:00
|
|
|
#include "video_core/renderer_vulkan/vk_rasterizer.h"
|
|
|
|
#include "video_core/renderer_vulkan/vk_scheduler.h"
|
2021-02-17 04:59:28 +01:00
|
|
|
#include "video_core/renderer_vulkan/vk_shader_util.h"
|
2020-01-07 01:18:38 +01:00
|
|
|
#include "video_core/renderer_vulkan/vk_update_descriptor.h"
|
2020-05-23 02:01:36 +02:00
|
|
|
#include "video_core/shader_cache.h"
|
2021-04-26 08:53:26 +02:00
|
|
|
#include "video_core/shader_environment.h"
|
2020-08-02 19:05:41 +02:00
|
|
|
#include "video_core/shader_notify.h"
|
2020-12-26 05:19:46 +01:00
|
|
|
#include "video_core/vulkan_common/vulkan_device.h"
|
2020-12-25 01:30:11 +01:00
|
|
|
#include "video_core/vulkan_common/vulkan_wrapper.h"
|
2020-01-07 01:18:38 +01:00
|
|
|
|
|
|
|
namespace Vulkan {
|
2020-01-07 01:55:06 +01:00
|
|
|
MICROPROFILE_DECLARE(Vulkan_PipelineCache);
|
|
|
|
|
2021-03-23 01:03:20 +01:00
|
|
|
namespace {
|
|
|
|
using Shader::Backend::SPIRV::EmitSPIRV;
|
2021-04-19 01:03:38 +02:00
|
|
|
using Shader::Maxwell::MergeDualVertexPrograms;
|
2021-03-23 01:03:20 +01:00
|
|
|
using Shader::Maxwell::TranslateProgram;
|
2021-04-26 08:53:26 +02:00
|
|
|
using VideoCommon::ComputeEnvironment;
|
|
|
|
using VideoCommon::FileEnvironment;
|
|
|
|
using VideoCommon::GenericEnvironment;
|
|
|
|
using VideoCommon::GraphicsEnvironment;
|
2021-03-23 01:03:20 +01:00
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
template <typename Container>
|
|
|
|
auto MakeSpan(Container& container) {
|
|
|
|
return std::span(container.data(), container.size());
|
2021-03-23 01:03:20 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 06:32:18 +02:00
|
|
|
Shader::CompareFunction MaxwellToCompareFunction(Maxwell::ComparisonOp comparison) {
|
|
|
|
switch (comparison) {
|
|
|
|
case Maxwell::ComparisonOp::Never:
|
|
|
|
case Maxwell::ComparisonOp::NeverOld:
|
|
|
|
return Shader::CompareFunction::Never;
|
|
|
|
case Maxwell::ComparisonOp::Less:
|
|
|
|
case Maxwell::ComparisonOp::LessOld:
|
|
|
|
return Shader::CompareFunction::Less;
|
|
|
|
case Maxwell::ComparisonOp::Equal:
|
|
|
|
case Maxwell::ComparisonOp::EqualOld:
|
|
|
|
return Shader::CompareFunction::Equal;
|
|
|
|
case Maxwell::ComparisonOp::LessEqual:
|
|
|
|
case Maxwell::ComparisonOp::LessEqualOld:
|
|
|
|
return Shader::CompareFunction::LessThanEqual;
|
|
|
|
case Maxwell::ComparisonOp::Greater:
|
|
|
|
case Maxwell::ComparisonOp::GreaterOld:
|
|
|
|
return Shader::CompareFunction::Greater;
|
|
|
|
case Maxwell::ComparisonOp::NotEqual:
|
|
|
|
case Maxwell::ComparisonOp::NotEqualOld:
|
|
|
|
return Shader::CompareFunction::NotEqual;
|
|
|
|
case Maxwell::ComparisonOp::GreaterEqual:
|
|
|
|
case Maxwell::ComparisonOp::GreaterEqualOld:
|
|
|
|
return Shader::CompareFunction::GreaterThanEqual;
|
|
|
|
case Maxwell::ComparisonOp::Always:
|
|
|
|
case Maxwell::ComparisonOp::AlwaysOld:
|
|
|
|
return Shader::CompareFunction::Always;
|
|
|
|
}
|
|
|
|
UNIMPLEMENTED_MSG("Unimplemented comparison op={}", comparison);
|
|
|
|
return {};
|
|
|
|
}
|
2021-05-21 07:12:32 +02:00
|
|
|
|
|
|
|
static Shader::AttributeType CastAttributeType(const FixedPipelineState::VertexAttribute& attr) {
|
|
|
|
if (attr.enabled == 0) {
|
|
|
|
return Shader::AttributeType::Disabled;
|
|
|
|
}
|
|
|
|
switch (attr.Type()) {
|
|
|
|
case Maxwell::VertexAttribute::Type::SignedNorm:
|
|
|
|
case Maxwell::VertexAttribute::Type::UnsignedNorm:
|
|
|
|
case Maxwell::VertexAttribute::Type::UnsignedScaled:
|
|
|
|
case Maxwell::VertexAttribute::Type::SignedScaled:
|
|
|
|
case Maxwell::VertexAttribute::Type::Float:
|
|
|
|
return Shader::AttributeType::Float;
|
|
|
|
case Maxwell::VertexAttribute::Type::SignedInt:
|
|
|
|
return Shader::AttributeType::SignedInt;
|
|
|
|
case Maxwell::VertexAttribute::Type::UnsignedInt:
|
|
|
|
return Shader::AttributeType::UnsignedInt;
|
|
|
|
}
|
|
|
|
return Shader::AttributeType::Float;
|
|
|
|
}
|
|
|
|
|
2021-06-12 10:07:52 +02:00
|
|
|
Shader::AttributeType AttributeType(const FixedPipelineState& state, size_t index) {
|
|
|
|
switch (state.DynamicAttributeType(index)) {
|
|
|
|
case 0:
|
|
|
|
return Shader::AttributeType::Disabled;
|
|
|
|
case 1:
|
|
|
|
return Shader::AttributeType::Float;
|
|
|
|
case 2:
|
|
|
|
return Shader::AttributeType::SignedInt;
|
|
|
|
case 3:
|
|
|
|
return Shader::AttributeType::UnsignedInt;
|
|
|
|
}
|
|
|
|
return Shader::AttributeType::Disabled;
|
|
|
|
}
|
|
|
|
|
2021-05-21 07:12:32 +02:00
|
|
|
Shader::RuntimeInfo MakeRuntimeInfo(const GraphicsPipelineCacheKey& key,
|
|
|
|
const Shader::IR::Program& program) {
|
|
|
|
Shader::RuntimeInfo info;
|
|
|
|
|
|
|
|
const Shader::Stage stage{program.stage};
|
|
|
|
const bool has_geometry{key.unique_hashes[4] != 0};
|
|
|
|
const bool gl_ndc{key.state.ndc_minus_one_to_one != 0};
|
|
|
|
const float point_size{Common::BitCast<float>(key.state.point_size)};
|
|
|
|
switch (stage) {
|
|
|
|
case Shader::Stage::VertexB:
|
|
|
|
if (!has_geometry) {
|
|
|
|
if (key.state.topology == Maxwell::PrimitiveTopology::Points) {
|
|
|
|
info.fixed_state_point_size = point_size;
|
|
|
|
}
|
2021-06-12 10:07:52 +02:00
|
|
|
if (key.state.xfb_enabled) {
|
2021-05-21 22:19:35 +02:00
|
|
|
info.xfb_varyings = VideoCommon::MakeTransformFeedbackVaryings(key.state.xfb_state);
|
2021-05-21 07:12:32 +02:00
|
|
|
}
|
|
|
|
info.convert_depth_mode = gl_ndc;
|
|
|
|
}
|
2021-06-12 10:07:52 +02:00
|
|
|
if (key.state.dynamic_vertex_input) {
|
|
|
|
for (size_t index = 0; index < Maxwell::NumVertexAttributes; ++index) {
|
|
|
|
info.generic_input_types[index] = AttributeType(key.state, index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::ranges::transform(key.state.attributes, info.generic_input_types.begin(),
|
|
|
|
&CastAttributeType);
|
|
|
|
}
|
2021-05-21 07:12:32 +02:00
|
|
|
break;
|
|
|
|
case Shader::Stage::TessellationEval:
|
|
|
|
// We have to flip tessellation clockwise for some reason...
|
|
|
|
info.tess_clockwise = key.state.tessellation_clockwise == 0;
|
|
|
|
info.tess_primitive = [&key] {
|
|
|
|
const u32 raw{key.state.tessellation_primitive.Value()};
|
|
|
|
switch (static_cast<Maxwell::TessellationPrimitive>(raw)) {
|
|
|
|
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 = [&] {
|
|
|
|
const u32 raw{key.state.tessellation_spacing};
|
|
|
|
switch (static_cast<Maxwell::TessellationSpacing>(raw)) {
|
|
|
|
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;
|
|
|
|
case Shader::Stage::Geometry:
|
|
|
|
if (program.output_topology == Shader::OutputTopology::PointList) {
|
|
|
|
info.fixed_state_point_size = point_size;
|
|
|
|
}
|
|
|
|
if (key.state.xfb_enabled != 0) {
|
2021-05-21 22:19:35 +02:00
|
|
|
info.xfb_varyings = VideoCommon::MakeTransformFeedbackVaryings(key.state.xfb_state);
|
2021-05-21 07:12:32 +02:00
|
|
|
}
|
|
|
|
info.convert_depth_mode = gl_ndc;
|
|
|
|
break;
|
|
|
|
case Shader::Stage::Fragment:
|
|
|
|
info.alpha_test_func = MaxwellToCompareFunction(
|
|
|
|
key.state.UnpackComparisonOp(key.state.alpha_test_func.Value()));
|
|
|
|
info.alpha_test_reference = Common::BitCast<float>(key.state.alpha_test_ref);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (key.state.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;
|
|
|
|
}
|
|
|
|
info.force_early_z = key.state.early_z != 0;
|
|
|
|
info.y_negate = key.state.y_negate != 0;
|
|
|
|
return info;
|
|
|
|
}
|
2020-01-07 01:55:06 +01:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
2021-02-17 00:52:12 +01:00
|
|
|
size_t ComputePipelineCacheKey::Hash() const noexcept {
|
2020-04-23 01:52:29 +02:00
|
|
|
const u64 hash = Common::CityHash64(reinterpret_cast<const char*>(this), sizeof *this);
|
2021-02-17 00:52:12 +01:00
|
|
|
return static_cast<size_t>(hash);
|
2020-04-23 01:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ComputePipelineCacheKey::operator==(const ComputePipelineCacheKey& rhs) const noexcept {
|
|
|
|
return std::memcmp(&rhs, this, sizeof *this) == 0;
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:28:31 +01:00
|
|
|
size_t GraphicsPipelineCacheKey::Hash() const noexcept {
|
|
|
|
const u64 hash = Common::CityHash64(reinterpret_cast<const char*>(this), Size());
|
|
|
|
return static_cast<size_t>(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GraphicsPipelineCacheKey::operator==(const GraphicsPipelineCacheKey& rhs) const noexcept {
|
|
|
|
return std::memcmp(&rhs, this, Size()) == 0;
|
|
|
|
}
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
PipelineCache::PipelineCache(RasterizerVulkan& rasterizer_, Tegra::Engines::Maxwell3D& maxwell3d_,
|
2021-02-17 00:52:12 +01:00
|
|
|
Tegra::Engines::KeplerCompute& kepler_compute_,
|
|
|
|
Tegra::MemoryManager& gpu_memory_, const Device& device_,
|
2021-04-25 05:15:32 +02:00
|
|
|
VKScheduler& scheduler_, DescriptorPool& descriptor_pool_,
|
2021-03-19 23:28:31 +01:00
|
|
|
VKUpdateDescriptorQueue& update_descriptor_queue_,
|
|
|
|
RenderPassCache& render_pass_cache_, BufferCache& buffer_cache_,
|
2021-06-06 05:11:36 +02:00
|
|
|
TextureCache& texture_cache_, VideoCore::ShaderNotify& shader_notify_)
|
2021-04-26 08:53:26 +02:00
|
|
|
: VideoCommon::ShaderCache{rasterizer_, gpu_memory_, maxwell3d_, kepler_compute_},
|
|
|
|
device{device_}, scheduler{scheduler_}, descriptor_pool{descriptor_pool_},
|
2021-03-19 23:28:31 +01:00
|
|
|
update_descriptor_queue{update_descriptor_queue_}, render_pass_cache{render_pass_cache_},
|
2021-06-06 05:11:36 +02:00
|
|
|
buffer_cache{buffer_cache_}, texture_cache{texture_cache_}, shader_notify{shader_notify_},
|
2021-06-06 03:10:02 +02:00
|
|
|
use_asynchronous_shaders{Settings::values.use_asynchronous_shaders.GetValue()},
|
2021-04-06 00:15:45 +02:00
|
|
|
workers(std::max(std::thread::hardware_concurrency(), 2U) - 1, "yuzu:PipelineBuilder"),
|
2021-04-05 08:56:58 +02:00
|
|
|
serialization_thread(1, "yuzu:PipelineSerialization") {
|
2021-03-19 23:28:31 +01:00
|
|
|
const auto& float_control{device.FloatControlProperties()};
|
2021-03-20 09:04:12 +01:00
|
|
|
const VkDriverIdKHR driver_id{device.GetDriverID()};
|
2021-05-21 07:12:32 +02:00
|
|
|
profile = Shader::Profile{
|
2021-03-29 00:53:34 +02:00
|
|
|
.supported_spirv = device.IsKhrSpirv1_4Supported() ? 0x00010400U : 0x00010000U,
|
2021-03-19 23:28:31 +01:00
|
|
|
.unified_descriptor_binding = true,
|
2021-05-23 08:58:11 +02:00
|
|
|
.support_descriptor_aliasing = true,
|
2021-05-09 08:09:55 +02:00
|
|
|
.support_int8 = true,
|
2021-05-31 01:44:28 +02:00
|
|
|
.support_int16 = device.IsShaderInt16Supported(),
|
2021-03-20 23:11:56 +01:00
|
|
|
.support_vertex_instance_id = false,
|
2021-03-19 23:28:31 +01:00
|
|
|
.support_float_controls = true,
|
|
|
|
.support_separate_denorm_behavior = float_control.denormBehaviorIndependence ==
|
|
|
|
VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR,
|
|
|
|
.support_separate_rounding_mode =
|
|
|
|
float_control.roundingModeIndependence == VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR,
|
|
|
|
.support_fp16_denorm_preserve = float_control.shaderDenormPreserveFloat16 != VK_FALSE,
|
|
|
|
.support_fp32_denorm_preserve = float_control.shaderDenormPreserveFloat32 != VK_FALSE,
|
|
|
|
.support_fp16_denorm_flush = float_control.shaderDenormFlushToZeroFloat16 != VK_FALSE,
|
|
|
|
.support_fp32_denorm_flush = float_control.shaderDenormFlushToZeroFloat32 != VK_FALSE,
|
|
|
|
.support_fp16_signed_zero_nan_preserve =
|
|
|
|
float_control.shaderSignedZeroInfNanPreserveFloat16 != VK_FALSE,
|
|
|
|
.support_fp32_signed_zero_nan_preserve =
|
|
|
|
float_control.shaderSignedZeroInfNanPreserveFloat32 != VK_FALSE,
|
2021-03-22 00:28:37 +01:00
|
|
|
.support_fp64_signed_zero_nan_preserve =
|
|
|
|
float_control.shaderSignedZeroInfNanPreserveFloat64 != VK_FALSE,
|
2021-03-29 00:53:34 +02:00
|
|
|
.support_explicit_workgroup_layout = device.IsKhrWorkgroupMemoryExplicitLayoutSupported(),
|
2021-03-24 01:27:17 +01:00
|
|
|
.support_vote = true,
|
2021-04-03 10:19:13 +02:00
|
|
|
.support_viewport_index_layer_non_geometry =
|
|
|
|
device.IsExtShaderViewportIndexLayerSupported(),
|
2021-04-16 21:31:15 +02:00
|
|
|
.support_viewport_mask = device.IsNvViewportArray2Supported(),
|
2021-04-11 07:37:03 +02:00
|
|
|
.support_typeless_image_loads = device.IsFormatlessImageLoadSupported(),
|
2021-05-23 08:58:11 +02:00
|
|
|
.support_demote_to_helper_invocation = true,
|
2021-04-11 08:07:02 +02:00
|
|
|
.support_int64_atomics = device.IsExtShaderAtomicInt64Supported(),
|
2021-06-02 09:28:30 +02:00
|
|
|
.support_derivative_control = true,
|
2021-05-30 08:40:19 +02:00
|
|
|
|
2021-05-21 07:12:32 +02:00
|
|
|
.warp_size_potentially_larger_than_guest = device.IsWarpSizePotentiallyBiggerThanGuest(),
|
2021-05-30 08:40:19 +02:00
|
|
|
|
|
|
|
.lower_left_origin_mode = false,
|
|
|
|
.need_declared_frag_colors = false,
|
|
|
|
|
2021-03-20 09:04:12 +01:00
|
|
|
.has_broken_spirv_clamp = driver_id == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS_KHR,
|
2021-05-23 08:58:11 +02:00
|
|
|
.has_broken_unsigned_image_offsets = false,
|
2021-05-30 08:40:19 +02:00
|
|
|
.has_broken_signed_operations = false,
|
|
|
|
.ignore_nan_fp_comparisons = false,
|
2021-03-19 23:28:31 +01:00
|
|
|
};
|
|
|
|
}
|
2020-01-07 01:55:06 +01:00
|
|
|
|
2021-02-17 00:52:12 +01:00
|
|
|
PipelineCache::~PipelineCache() = default;
|
2020-01-07 01:55:06 +01:00
|
|
|
|
2021-03-19 23:28:31 +01:00
|
|
|
GraphicsPipeline* PipelineCache::CurrentGraphicsPipeline() {
|
|
|
|
MICROPROFILE_SCOPE(Vulkan_PipelineCache);
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
if (!RefreshStages(graphics_key.unique_hashes)) {
|
2021-04-24 23:27:25 +02:00
|
|
|
current_pipeline = nullptr;
|
2021-03-19 23:28:31 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-06-12 10:07:52 +02:00
|
|
|
graphics_key.state.Refresh(maxwell3d, device.IsExtExtendedDynamicStateSupported(),
|
|
|
|
device.IsExtVertexInputDynamicStateSupported());
|
2021-03-19 23:28:31 +01:00
|
|
|
|
2021-04-24 23:27:25 +02:00
|
|
|
if (current_pipeline) {
|
|
|
|
GraphicsPipeline* const next{current_pipeline->Next(graphics_key)};
|
|
|
|
if (next) {
|
|
|
|
current_pipeline = next;
|
2021-06-06 03:10:02 +02:00
|
|
|
return BuiltPipeline(current_pipeline);
|
2021-04-24 23:27:25 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-06 05:11:36 +02:00
|
|
|
return CurrentGraphicsPipelineSlowPath();
|
2021-03-19 23:28:31 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
ComputePipeline* PipelineCache::CurrentComputePipeline() {
|
2020-01-07 01:55:06 +01:00
|
|
|
MICROPROFILE_SCOPE(Vulkan_PipelineCache);
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
const ShaderInfo* const shader{ComputeShader()};
|
2021-02-17 04:59:28 +01:00
|
|
|
if (!shader) {
|
2021-04-26 08:53:26 +02:00
|
|
|
return nullptr;
|
2021-02-17 04:59:28 +01:00
|
|
|
}
|
2021-04-26 08:53:26 +02:00
|
|
|
const auto& qmd{kepler_compute.launch_description};
|
2021-03-23 01:03:20 +01:00
|
|
|
const ComputePipelineCacheKey key{
|
2021-04-26 08:53:26 +02:00
|
|
|
.unique_hash = shader->unique_hash,
|
|
|
|
.shared_memory_size = qmd.shared_alloc,
|
2021-03-23 01:03:20 +01:00
|
|
|
.workgroup_size{qmd.block_dim_x, qmd.block_dim_y, qmd.block_dim_z},
|
|
|
|
};
|
2021-02-17 04:59:28 +01:00
|
|
|
const auto [pair, is_new]{compute_cache.try_emplace(key)};
|
|
|
|
auto& pipeline{pair->second};
|
|
|
|
if (!is_new) {
|
2021-04-01 06:36:22 +02:00
|
|
|
return pipeline.get();
|
2021-02-17 04:59:28 +01:00
|
|
|
}
|
2021-03-23 01:03:20 +01:00
|
|
|
pipeline = CreateComputePipeline(key, shader);
|
2021-04-01 06:36:22 +02:00
|
|
|
return pipeline.get();
|
2021-02-17 04:59:28 +01:00
|
|
|
}
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
void PipelineCache::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
|
|
|
|
const VideoCore::DiskResourceLoadCallback& callback) {
|
|
|
|
if (title_id == 0) {
|
|
|
|
return;
|
2021-04-24 23:27:25 +02:00
|
|
|
}
|
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)) {
|
2021-04-26 08:53:26 +02:00
|
|
|
LOG_ERROR(Common_Filesystem, "Failed to create pipeline cache directories");
|
|
|
|
return;
|
2021-03-19 23:28:31 +01:00
|
|
|
}
|
2021-06-12 06:46:30 +02:00
|
|
|
pipeline_cache_filename = base_dir / "vulkan.bin";
|
2021-03-19 23:28:31 +01:00
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
struct {
|
|
|
|
std::mutex mutex;
|
2021-06-12 06:46:30 +02:00
|
|
|
size_t total{};
|
|
|
|
size_t built{};
|
|
|
|
bool has_loaded{};
|
2021-04-26 08:53:26 +02:00
|
|
|
} state;
|
|
|
|
|
|
|
|
const auto load_compute{[&](std::ifstream& file, FileEnvironment env) {
|
|
|
|
ComputePipelineCacheKey key;
|
|
|
|
file.read(reinterpret_cast<char*>(&key), sizeof(key));
|
|
|
|
|
|
|
|
workers.QueueWork([this, key, env = std::move(env), &state, &callback]() mutable {
|
|
|
|
ShaderPools pools;
|
|
|
|
auto pipeline{CreateComputePipeline(pools, key, env, false)};
|
|
|
|
std::lock_guard lock{state.mutex};
|
2021-05-27 22:51:00 +02:00
|
|
|
if (pipeline) {
|
|
|
|
compute_cache.emplace(key, std::move(pipeline));
|
|
|
|
}
|
2021-04-26 08:53:26 +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) {
|
|
|
|
GraphicsPipelineCacheKey key;
|
|
|
|
file.read(reinterpret_cast<char*>(&key), sizeof(key));
|
|
|
|
|
|
|
|
workers.QueueWork([this, key, envs = std::move(envs), &state, &callback]() mutable {
|
|
|
|
ShaderPools pools;
|
|
|
|
boost::container::static_vector<Shader::Environment*, 5> env_ptrs;
|
|
|
|
for (auto& env : envs) {
|
|
|
|
env_ptrs.push_back(&env);
|
|
|
|
}
|
|
|
|
auto pipeline{CreateGraphicsPipeline(pools, key, MakeSpan(env_ptrs), false)};
|
|
|
|
|
|
|
|
std::lock_guard lock{state.mutex};
|
|
|
|
graphics_cache.emplace(key, std::move(pipeline));
|
|
|
|
++state.built;
|
|
|
|
if (state.has_loaded) {
|
|
|
|
callback(VideoCore::LoadCallbackStage::Build, state.built, state.total);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
++state.total;
|
|
|
|
}};
|
|
|
|
VideoCommon::LoadPipelines(stop_loading, pipeline_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();
|
|
|
|
|
|
|
|
workers.WaitForRequests();
|
2021-03-19 23:28:31 +01:00
|
|
|
}
|
|
|
|
|
2021-06-06 05:11:36 +02:00
|
|
|
GraphicsPipeline* PipelineCache::CurrentGraphicsPipelineSlowPath() {
|
|
|
|
const auto [pair, is_new]{graphics_cache.try_emplace(graphics_key)};
|
|
|
|
auto& pipeline{pair->second};
|
|
|
|
if (is_new) {
|
|
|
|
pipeline = CreateGraphicsPipeline();
|
|
|
|
}
|
|
|
|
if (!pipeline) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (current_pipeline) {
|
|
|
|
current_pipeline->AddTransition(pipeline.get());
|
|
|
|
}
|
|
|
|
current_pipeline = pipeline.get();
|
|
|
|
return BuiltPipeline(current_pipeline);
|
|
|
|
}
|
|
|
|
|
2021-06-06 03:10:02 +02:00
|
|
|
GraphicsPipeline* PipelineCache::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) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// 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-04-01 06:36:22 +02:00
|
|
|
std::unique_ptr<GraphicsPipeline> PipelineCache::CreateGraphicsPipeline(
|
|
|
|
ShaderPools& pools, const GraphicsPipelineCacheKey& key,
|
2021-05-27 22:51:00 +02:00
|
|
|
std::span<Shader::Environment* const> envs, bool build_in_parallel) try {
|
2021-03-23 01:03:20 +01:00
|
|
|
LOG_INFO(Render_Vulkan, "0x{:016x}", key.Hash());
|
|
|
|
size_t env_index{0};
|
2021-03-19 23:28:31 +01:00
|
|
|
std::array<Shader::IR::Program, Maxwell::MaxShaderProgram> programs;
|
2021-05-01 14:56:25 +02:00
|
|
|
const bool uses_vertex_a{key.unique_hashes[0] != 0};
|
|
|
|
const bool uses_vertex_b{key.unique_hashes[1] != 0};
|
2021-03-19 23:28:31 +01:00
|
|
|
for (size_t index = 0; index < Maxwell::MaxShaderProgram; ++index) {
|
2021-04-26 08:53:26 +02:00
|
|
|
if (key.unique_hashes[index] == 0) {
|
2021-03-19 23:28:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-23 01:03:20 +01:00
|
|
|
Shader::Environment& env{*envs[env_index]};
|
|
|
|
++env_index;
|
2021-03-19 23:28:31 +01:00
|
|
|
|
2021-04-06 04:25:22 +02:00
|
|
|
const u32 cfg_offset{static_cast<u32>(env.StartAddress() + sizeof(Shader::ProgramHeader))};
|
2021-04-19 01:03:38 +02:00
|
|
|
Shader::Maxwell::Flow::CFG cfg(env, pools.flow_block, cfg_offset, index == 0);
|
|
|
|
if (!uses_vertex_a || index != 1) {
|
2021-05-01 14:56:25 +02:00
|
|
|
// Normal path
|
2021-04-19 01:03:38 +02:00
|
|
|
programs[index] = TranslateProgram(pools.inst, pools.block, env, cfg);
|
2021-05-01 14:56:25 +02:00
|
|
|
} else {
|
|
|
|
// VertexB path when VertexA is present.
|
|
|
|
Shader::IR::Program& program_va{programs[0]};
|
|
|
|
Shader::IR::Program program_vb{TranslateProgram(pools.inst, pools.block, env, cfg)};
|
|
|
|
programs[index] = MergeDualVertexPrograms(program_va, program_vb, env);
|
2021-04-19 01:03:38 +02:00
|
|
|
}
|
2021-03-19 23:28:31 +01:00
|
|
|
}
|
|
|
|
std::array<const Shader::Info*, Maxwell::MaxShaderStage> infos{};
|
|
|
|
std::array<vk::ShaderModule, Maxwell::MaxShaderStage> modules;
|
|
|
|
|
2021-05-04 01:53:00 +02:00
|
|
|
Shader::Backend::Bindings binding;
|
|
|
|
for (size_t index = uses_vertex_a && uses_vertex_b ? 1 : 0; index < Maxwell::MaxShaderProgram;
|
|
|
|
++index) {
|
2021-04-26 08:53:26 +02:00
|
|
|
if (key.unique_hashes[index] == 0) {
|
2021-03-19 23:28:31 +01:00
|
|
|
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:12:32 +02:00
|
|
|
const Shader::RuntimeInfo runtime_info{MakeRuntimeInfo(key, program)};
|
|
|
|
const std::vector<u32> code{EmitSPIRV(profile, runtime_info, program, binding)};
|
2021-04-11 07:50:30 +02:00
|
|
|
device.SaveShader(code);
|
2021-03-19 23:28:31 +01:00
|
|
|
modules[stage_index] = BuildShader(device, code);
|
2021-03-31 02:28:00 +02:00
|
|
|
if (device.HasDebuggingToolAttached()) {
|
2021-06-06 05:11:36 +02:00
|
|
|
const std::string name{fmt::format("Shader {:016x}", key.unique_hashes[index])};
|
2021-03-31 02:28:00 +02:00
|
|
|
modules[stage_index].SetObjectNameEXT(name.c_str());
|
|
|
|
}
|
2021-03-19 23:28:31 +01:00
|
|
|
}
|
2021-04-01 06:36:22 +02:00
|
|
|
Common::ThreadWorker* const thread_worker{build_in_parallel ? &workers : nullptr};
|
2021-06-06 05:11:36 +02:00
|
|
|
VideoCore::ShaderNotify* const notify{build_in_parallel ? &shader_notify : nullptr};
|
|
|
|
return std::make_unique<GraphicsPipeline>(maxwell3d, gpu_memory, scheduler, buffer_cache,
|
|
|
|
texture_cache, notify, device, descriptor_pool,
|
|
|
|
update_descriptor_queue, thread_worker,
|
|
|
|
render_pass_cache, key, std::move(modules), infos);
|
2021-05-27 22:51:00 +02:00
|
|
|
|
|
|
|
} catch (const Shader::Exception& exception) {
|
|
|
|
LOG_ERROR(Render_Vulkan, "{}", exception.what());
|
|
|
|
return nullptr;
|
2021-03-19 23:28:31 +01:00
|
|
|
}
|
|
|
|
|
2021-04-01 06:36:22 +02:00
|
|
|
std::unique_ptr<GraphicsPipeline> PipelineCache::CreateGraphicsPipeline() {
|
2021-05-23 09:28:34 +02:00
|
|
|
GraphicsEnvironments environments;
|
|
|
|
GetGraphicsEnvironments(environments, graphics_key.unique_hashes);
|
2021-03-23 01:03:20 +01:00
|
|
|
|
2021-05-23 09:28:34 +02:00
|
|
|
main_pools.ReleaseContents();
|
|
|
|
auto pipeline{CreateGraphicsPipeline(main_pools, graphics_key, environments.Span(), true)};
|
2021-05-27 22:51:00 +02:00
|
|
|
if (!pipeline || pipeline_cache_filename.empty()) {
|
2021-04-01 09:09:09 +02:00
|
|
|
return pipeline;
|
|
|
|
}
|
2021-05-23 09:28:34 +02:00
|
|
|
serialization_thread.QueueWork([this, key = graphics_key, envs = std::move(environments.envs)] {
|
2021-04-01 09:09:09 +02:00
|
|
|
boost::container::static_vector<const GenericEnvironment*, Maxwell::MaxShaderProgram>
|
|
|
|
env_ptrs;
|
|
|
|
for (size_t index = 0; index < Maxwell::MaxShaderProgram; ++index) {
|
2021-04-26 08:53:26 +02:00
|
|
|
if (key.unique_hashes[index] != 0) {
|
2021-04-01 09:09:09 +02:00
|
|
|
env_ptrs.push_back(&envs[index]);
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 22:51:00 +02:00
|
|
|
SerializePipeline(key, env_ptrs, pipeline_cache_filename);
|
2021-04-01 09:09:09 +02:00
|
|
|
});
|
2021-03-23 01:03:20 +01:00
|
|
|
return pipeline;
|
|
|
|
}
|
|
|
|
|
2021-04-01 06:36:22 +02:00
|
|
|
std::unique_ptr<ComputePipeline> PipelineCache::CreateComputePipeline(
|
|
|
|
const ComputePipelineCacheKey& key, const ShaderInfo* shader) {
|
2021-02-17 04:59:28 +01:00
|
|
|
const GPUVAddr program_base{kepler_compute.regs.code_loc.Address()};
|
|
|
|
const auto& qmd{kepler_compute.launch_description};
|
2021-03-23 01:03:20 +01:00
|
|
|
ComputeEnvironment env{kepler_compute, gpu_memory, program_base, qmd.program_start};
|
2021-03-29 02:55:47 +02:00
|
|
|
env.SetCachedSize(shader->size_bytes);
|
|
|
|
|
2021-03-23 01:03:20 +01:00
|
|
|
main_pools.ReleaseContents();
|
2021-04-01 06:36:22 +02:00
|
|
|
auto pipeline{CreateComputePipeline(main_pools, key, env, true)};
|
2021-05-27 22:51:00 +02:00
|
|
|
if (!pipeline || pipeline_cache_filename.empty()) {
|
|
|
|
return pipeline;
|
2020-01-07 01:55:06 +01:00
|
|
|
}
|
2021-05-27 22:51:00 +02:00
|
|
|
serialization_thread.QueueWork([this, key, env = std::move(env)] {
|
|
|
|
SerializePipeline(key, std::array<const GenericEnvironment*, 1>{&env},
|
|
|
|
pipeline_cache_filename);
|
|
|
|
});
|
2021-03-23 01:03:20 +01:00
|
|
|
return pipeline;
|
|
|
|
}
|
|
|
|
|
2021-04-01 06:36:22 +02:00
|
|
|
std::unique_ptr<ComputePipeline> PipelineCache::CreateComputePipeline(
|
|
|
|
ShaderPools& pools, const ComputePipelineCacheKey& key, Shader::Environment& env,
|
2021-05-27 22:51:00 +02:00
|
|
|
bool build_in_parallel) try {
|
2021-03-23 01:03:20 +01:00
|
|
|
LOG_INFO(Render_Vulkan, "0x{:016x}", key.Hash());
|
2021-03-19 23:28:31 +01:00
|
|
|
|
2021-03-23 01:03:20 +01:00
|
|
|
Shader::Maxwell::Flow::CFG cfg{env, pools.flow_block, env.StartAddress()};
|
|
|
|
Shader::IR::Program program{TranslateProgram(pools.inst, pools.block, env, cfg)};
|
2021-05-21 07:12:32 +02:00
|
|
|
const std::vector<u32> code{EmitSPIRV(profile, program)};
|
2021-04-11 07:50:30 +02:00
|
|
|
device.SaveShader(code);
|
2021-03-31 02:28:00 +02:00
|
|
|
vk::ShaderModule spv_module{BuildShader(device, code)};
|
|
|
|
if (device.HasDebuggingToolAttached()) {
|
2021-06-06 05:11:36 +02:00
|
|
|
const auto name{fmt::format("Shader {:016x}", key.unique_hash)};
|
2021-03-31 02:28:00 +02:00
|
|
|
spv_module.SetObjectNameEXT(name.c_str());
|
|
|
|
}
|
2021-04-01 06:36:22 +02:00
|
|
|
Common::ThreadWorker* const thread_worker{build_in_parallel ? &workers : nullptr};
|
2021-06-06 05:11:36 +02:00
|
|
|
VideoCore::ShaderNotify* const notify{build_in_parallel ? &shader_notify : nullptr};
|
2021-04-01 06:36:22 +02:00
|
|
|
return std::make_unique<ComputePipeline>(device, descriptor_pool, update_descriptor_queue,
|
2021-06-06 05:11:36 +02:00
|
|
|
thread_worker, notify, program.info,
|
|
|
|
std::move(spv_module));
|
2021-05-27 22:51:00 +02:00
|
|
|
|
|
|
|
} catch (const Shader::Exception& exception) {
|
|
|
|
LOG_ERROR(Render_Vulkan, "{}", exception.what());
|
|
|
|
return nullptr;
|
2020-01-07 01:55:06 +01:00
|
|
|
}
|
|
|
|
|
2020-01-07 01:18:38 +01:00
|
|
|
} // namespace Vulkan
|