From 710d225d0af77f83869e892d2d7377e81bd18bee Mon Sep 17 00:00:00 2001 From: Triang3l Date: Sun, 16 Feb 2020 19:35:26 +0300 Subject: [PATCH] [GPU] --gpu_allow_invalid_fetch_constants to bypass invalid fetch constant type errors --- .../gpu/d3d12/d3d12_command_processor.cc | 22 +++++++++++--- src/xenia/gpu/d3d12/render_target_cache.cc | 2 +- src/xenia/gpu/d3d12/texture_cache.cc | 30 ++++++++++++++----- src/xenia/gpu/gpu_flags.cc | 8 +++++ src/xenia/gpu/gpu_flags.h | 2 ++ src/xenia/gpu/trace_viewer.cc | 5 +++- src/xenia/gpu/vulkan/buffer_cache.cc | 21 +++++++++++-- src/xenia/gpu/vulkan/texture_cache.cc | 25 ++++++++++++++-- .../gpu/vulkan/vulkan_command_processor.cc | 2 +- src/xenia/gpu/xenos.h | 14 +++++++-- 10 files changed, 108 insertions(+), 23 deletions(-) diff --git a/src/xenia/gpu/d3d12/d3d12_command_processor.cc b/src/xenia/gpu/d3d12/d3d12_command_processor.cc index 432a6ea11..c903202dc 100644 --- a/src/xenia/gpu/d3d12/d3d12_command_processor.cc +++ b/src/xenia/gpu/d3d12/d3d12_command_processor.cc @@ -20,6 +20,7 @@ #include "xenia/gpu/d3d12/d3d12_command_processor.h" #include "xenia/gpu/d3d12/d3d12_graphics_system.h" #include "xenia/gpu/d3d12/d3d12_shader.h" +#include "xenia/gpu/gpu_flags.h" #include "xenia/gpu/xenos.h" #include "xenia/ui/d3d12/d3d12_util.h" @@ -1476,10 +1477,23 @@ bool D3D12CommandProcessor::IssueDraw(PrimitiveType primitive_type, } const auto& vfetch_constant = regs.Get( XE_GPU_REG_SHADER_CONSTANT_FETCH_00_0 + vfetch_index * 2); - if (vfetch_constant.type != 3) { - XELOGW("Vertex fetch type is not 3 (fetch constant %u is %.8X %.8X)!", - vfetch_index, vfetch_constant.dword_0, vfetch_constant.dword_1); - return false; + switch (vfetch_constant.type) { + case xenos::FetchConstantType::kVertex: + break; + case xenos::FetchConstantType::kInvalidVertex: + if (cvars::gpu_allow_invalid_fetch_constants) { + break; + } + XELOGW( + "Vertex fetch constant %u (%.8X %.8X) has \"invalid\" type! This " + "is incorrect behavior, but you can try bypassing this by " + "launching Xenia with --gpu_allow_invalid_fetch_constants=true.", + vfetch_index, vfetch_constant.dword_0, vfetch_constant.dword_1); + return false; + default: + XELOGW("Vertex fetch constant %u (%.8X %.8X) is completely invalid!", + vfetch_index, vfetch_constant.dword_0, vfetch_constant.dword_1); + return false; } if (!shared_memory_->RequestRange(vfetch_constant.address << 2, vfetch_constant.size << 2)) { diff --git a/src/xenia/gpu/d3d12/render_target_cache.cc b/src/xenia/gpu/d3d12/render_target_cache.cc index 22c5cde59..f590566af 100644 --- a/src/xenia/gpu/d3d12/render_target_cache.cc +++ b/src/xenia/gpu/d3d12/render_target_cache.cc @@ -1087,7 +1087,7 @@ bool RenderTargetCache::Resolve(SharedMemory* shared_memory, // HACK: Vertices to use are always in vf0. const auto& fetch = regs.Get( XE_GPU_REG_SHADER_CONSTANT_FETCH_00_0); - assert_true(fetch.type == 3); + assert_true(fetch.type == xenos::FetchConstantType::kVertex); assert_true(fetch.endian == Endian::k8in32); assert_true(fetch.size == 6); trace_writer_->WriteMemoryRead(fetch.address << 2, fetch.size << 2); diff --git a/src/xenia/gpu/d3d12/texture_cache.cc b/src/xenia/gpu/d3d12/texture_cache.cc index 1d1570dee..cc8fb5d88 100644 --- a/src/xenia/gpu/d3d12/texture_cache.cc +++ b/src/xenia/gpu/d3d12/texture_cache.cc @@ -21,6 +21,7 @@ #include "xenia/base/math.h" #include "xenia/base/profiling.h" #include "xenia/gpu/d3d12/d3d12_command_processor.h" +#include "xenia/gpu/gpu_flags.h" #include "xenia/gpu/texture_info.h" #include "xenia/gpu/texture_util.h" #include "xenia/ui/d3d12/d3d12_util.h" @@ -2094,13 +2095,28 @@ void TextureCache::BindingInfoFromFetchConstant( *has_signed_out = false; } - if (fetch.type != 2) { - XELOGW( - "Texture fetch type is not 2 - ignoring (fetch constant is %.8X %.8X " - "%.8X %.8X %.8X %.8X)!", - fetch.dword_0, fetch.dword_1, fetch.dword_2, fetch.dword_3, - fetch.dword_4, fetch.dword_5); - return; + switch (fetch.type) { + case xenos::FetchConstantType::kTexture: + break; + case xenos::FetchConstantType::kInvalidTexture: + if (cvars::gpu_allow_invalid_fetch_constants) { + break; + } + XELOGW( + "Texture fetch constant (%.8X %.8X %.8X %.8X %.8X %.8X) has " + "\"invalid\" type! This is incorrect behavior, but you can try " + "bypassing this by launching Xenia with " + "--gpu_allow_invalid_fetch_constants=true.", + fetch.dword_0, fetch.dword_1, fetch.dword_2, fetch.dword_3, + fetch.dword_4, fetch.dword_5); + return; + default: + XELOGW( + "Texture fetch constant (%.8X %.8X %.8X %.8X %.8X %.8X) is " + "completely invalid!", + fetch.dword_0, fetch.dword_1, fetch.dword_2, fetch.dword_3, + fetch.dword_4, fetch.dword_5); + return; } // Validate the dimensions, get the size and clamp the maximum mip level. diff --git a/src/xenia/gpu/gpu_flags.cc b/src/xenia/gpu/gpu_flags.cc index 68f784f19..90c7b4598 100644 --- a/src/xenia/gpu/gpu_flags.cc +++ b/src/xenia/gpu/gpu_flags.cc @@ -17,3 +17,11 @@ DEFINE_string(dump_shaders, "", "Path to write GPU shaders to as they are compiled.", "GPU"); DEFINE_bool(vsync, true, "Enable VSYNC.", "GPU"); + +DEFINE_bool( + gpu_allow_invalid_fetch_constants, false, + "Allow texture and vertex fetch constants with invalid type - generally " + "unsafe because the constant may contain completely invalid values, but " + "may be used to bypass fetch constant type errors in certain games until " + "the real reason why they're invalid is found.", + "GPU"); diff --git a/src/xenia/gpu/gpu_flags.h b/src/xenia/gpu/gpu_flags.h index 17a95807a..7f1d1815e 100644 --- a/src/xenia/gpu/gpu_flags.h +++ b/src/xenia/gpu/gpu_flags.h @@ -18,4 +18,6 @@ DECLARE_string(dump_shaders); DECLARE_bool(vsync); +DECLARE_bool(gpu_allow_invalid_fetch_constants); + #endif // XENIA_GPU_GPU_FLAGS_H_ diff --git a/src/xenia/gpu/trace_viewer.cc b/src/xenia/gpu/trace_viewer.cc index 298c0b887..77ab7c022 100644 --- a/src/xenia/gpu/trace_viewer.cc +++ b/src/xenia/gpu/trace_viewer.cc @@ -19,6 +19,7 @@ #include "xenia/base/string.h" #include "xenia/base/threading.h" #include "xenia/gpu/command_processor.h" +#include "xenia/gpu/gpu_flags.h" #include "xenia/gpu/graphics_system.h" #include "xenia/gpu/packet_disassembler.h" #include "xenia/gpu/register_file.h" @@ -664,7 +665,9 @@ void TraceViewer::DrawTextureInfo( texture_binding.fetch_constant * 6; auto group = reinterpret_cast(®s.values[r]); auto& fetch = group->texture_fetch; - if (fetch.type != 0x2) { + if (fetch.type != xenos::FetchConstantType::kTexture && + (!cvars::gpu_allow_invalid_fetch_constants || + fetch.type != xenos::FetchConstantType::kInvalidTexture)) { DrawFailedTextureInfo(texture_binding, "Invalid fetch type"); return; } diff --git a/src/xenia/gpu/vulkan/buffer_cache.cc b/src/xenia/gpu/vulkan/buffer_cache.cc index d0e128e1c..af30d9da0 100644 --- a/src/xenia/gpu/vulkan/buffer_cache.cc +++ b/src/xenia/gpu/vulkan/buffer_cache.cc @@ -639,9 +639,24 @@ VkDescriptorSet BufferCache::PrepareVertexSet( break; } - if (fetch->type != 0x3) { - // TODO(DrChat): Some games use type 0x0 (with no data). - return nullptr; + // TODO(DrChat): Some games use type kInvalidTexture (with no data). + switch (fetch->type) { + case xenos::FetchConstantType::kVertex: + break; + case xenos::FetchConstantType::kInvalidVertex: + if (cvars::gpu_allow_invalid_fetch_constants) { + break; + } + XELOGW( + "Vertex fetch constant %u (%.8X %.8X) has \"invalid\" type! This " + "is incorrect behavior, but you can try bypassing this by " + "launching Xenia with --gpu_allow_invalid_fetch_constants=true.", + vertex_binding.fetch_constant, fetch->dword_0, fetch->dword_1); + return nullptr; + default: + XELOGW("Vertex fetch constant %u (%.8X %.8X) is completely invalid!", + vertex_binding.fetch_constant, fetch->dword_0, fetch->dword_1); + return nullptr; } // TODO(benvanik): compute based on indices or vertex count. diff --git a/src/xenia/gpu/vulkan/texture_cache.cc b/src/xenia/gpu/vulkan/texture_cache.cc index 92d8f9fc7..92cca8d36 100644 --- a/src/xenia/gpu/vulkan/texture_cache.cc +++ b/src/xenia/gpu/vulkan/texture_cache.cc @@ -1501,9 +1501,28 @@ bool TextureCache::SetupTextureBinding(VkCommandBuffer command_buffer, // Disabled? // TODO(benvanik): reset sampler. - if (fetch.type != 0x2) { - XELOGGPU("Fetch type is not 2!"); - return false; + switch (fetch.type) { + case xenos::FetchConstantType::kTexture: + break; + case xenos::FetchConstantType::kInvalidTexture: + if (cvars::gpu_allow_invalid_fetch_constants) { + break; + } + XELOGW( + "Texture fetch constant %u (%.8X %.8X %.8X %.8X %.8X %.8X) has " + "\"invalid\" type! This is incorrect behavior, but you can try " + "bypassing this by launching Xenia with " + "--gpu_allow_invalid_fetch_constants=true.", + binding.fetch_constant, fetch.dword_0, fetch.dword_1, fetch.dword_2, + fetch.dword_3, fetch.dword_4, fetch.dword_5); + return false; + default: + XELOGW( + "Texture fetch constant %u (%.8X %.8X %.8X %.8X %.8X %.8X) is " + "completely invalid!", + binding.fetch_constant, fetch.dword_0, fetch.dword_1, fetch.dword_2, + fetch.dword_3, fetch.dword_4, fetch.dword_5); + return false; } TextureInfo texture_info; diff --git a/src/xenia/gpu/vulkan/vulkan_command_processor.cc b/src/xenia/gpu/vulkan/vulkan_command_processor.cc index 336e08a11..46244fbb4 100644 --- a/src/xenia/gpu/vulkan/vulkan_command_processor.cc +++ b/src/xenia/gpu/vulkan/vulkan_command_processor.cc @@ -966,7 +966,7 @@ bool VulkanCommandProcessor::IssueCopy() { fetch = &group->vertex_fetch_2; break; } - assert_true(fetch->type == 3); + assert_true(fetch->type == xenos::FetchConstantType::kVertex); assert_true(fetch->endian == Endian::k8in32); assert_true(fetch->size == 6); const uint8_t* vertex_addr = memory_->TranslatePhysical(fetch->address << 2); diff --git a/src/xenia/gpu/xenos.h b/src/xenia/gpu/xenos.h index 47a5659a4..592a6684a 100644 --- a/src/xenia/gpu/xenos.h +++ b/src/xenia/gpu/xenos.h @@ -605,11 +605,19 @@ inline uint32_t GpuToCpu(uint32_t p) { return p; } inline uint32_t CpuToGpu(uint32_t p) { return p & 0x1FFFFFFF; } +// SQ_TEX_VTX_INVALID/VALID_TEXTURE/BUFFER +enum class FetchConstantType : uint32_t { + kInvalidTexture, + kInvalidVertex, + kTexture, + kVertex, +}; + // XE_GPU_REG_SHADER_CONSTANT_FETCH_* XEPACKEDUNION(xe_gpu_vertex_fetch_t, { XEPACKEDSTRUCTANONYMOUS({ - uint32_t type : 2; // +0 - uint32_t address : 30; // +2 address in dwords + FetchConstantType type : 2; // +0 + uint32_t address : 30; // +2 address in dwords Endian endian : 2; // +0 uint32_t size : 24; // +2 size in words @@ -624,7 +632,7 @@ XEPACKEDUNION(xe_gpu_vertex_fetch_t, { // XE_GPU_REG_SHADER_CONSTANT_FETCH_* XEPACKEDUNION(xe_gpu_texture_fetch_t, { XEPACKEDSTRUCTANONYMOUS({ - uint32_t type : 2; // +0 dword_0 + FetchConstantType type : 2; // +0 dword_0 TextureSign sign_x : 2; // +2 TextureSign sign_y : 2; // +4 TextureSign sign_z : 2; // +6