Triforce v0.3.6

This commit is contained in:
ruckus7382sfgsgdf
2026-11-14 00:33:23 -06:00
parent 2f1e350320
commit a02bcb21a9
106 changed files with 7683 additions and 1422 deletions

View File

@@ -117,6 +117,15 @@ void EmitInst(EmitContext& ctx, IR::Inst* inst) {
EmitInstOf(ctx, inst);
} catch (const NotImplementedException& e) {
throw NotImplementedException("{} ({})", e.what(), IR::NameOf(inst->GetOpcode()));
} catch (const std::runtime_error& e) { // (super build: sirit's null Id, named by the IR op that asked for it)
std::string args;
for (size_t i = 0; i < inst->NumArgs(); ++i) {
const IR::Value a{inst->Arg(i)};
args += a.IsImmediate() ? " imm" : a.IsEmpty() ? " empty" : a.IsIdentity() ? " identity"
: (" " + std::string(IR::NameOf(a.InstRecursive()->GetOpcode())) +
(a.InstRecursive()->Definition<Id>().value == 0 ? "(undefined)" : ""));
}
throw LogicError("{} ({}; args:{})", e.what(), IR::NameOf(inst->GetOpcode()), args);
}
}

View File

@@ -45,6 +45,13 @@ constexpr u32 MESH_FALLBACK_PAYLOAD_PUSH_OFFSET = MESH_FALLBACK_PUSH_OFFSET;
constexpr u32 MESH_FALLBACK_RECORDS_PUSH_OFFSET = MESH_FALLBACK_PUSH_OFFSET + 8;
constexpr u32 MESH_FALLBACK_INDIRECT_INPUT_PUSH_OFFSET = MESH_FALLBACK_PUSH_OFFSET + 16;
constexpr u32 MESH_FALLBACK_PUSH_END = MESH_FALLBACK_PUSH_OFFSET + 24;
// (hookshot) a scene vertex stage's position capture (RuntimeInfo::capture_position), after them: the u64 address its slots
// start at (0: none), the vertex index of slot 0 (a u32, the slot taken from VertexIndex wrapping) and the slots there are
// (u32). A vertex stores its position (vec4, 16 bytes) at address + (VertexIndex - first) * 16 when that is below the count
constexpr u32 CAPTURE_PUSH_OFFSET = MESH_FALLBACK_PUSH_END;
constexpr u32 CAPTURE_FIRST_PUSH_OFFSET = CAPTURE_PUSH_OFFSET + 8;
constexpr u32 CAPTURE_COUNT_PUSH_OFFSET = CAPTURE_PUSH_OFFSET + 12;
constexpr u32 CAPTURE_PUSH_END = CAPTURE_PUSH_OFFSET + 16;
// (hookshot) the fallback's mesh workgroups a draw can run: each a slot, a word of the payload buffer saying whether its task
// started it; then each task workgroup's payload. It was 1024, a record block each, and Ocarina's floor grass (433 tasks of
// 32 slots) lost 13 of 14 meshlets. At 16384, Hyrule Field's far trees (3cb76827/4f0c0b40: 721 tasks of 32) lost the tasks

View File

@@ -323,7 +323,16 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) {
}
const Id pointer{
AttrPointer(ctx, generic.pointer_type, vertex, generic.id, ctx.Const(element))};
const Id value{ctx.OpLoad(generic.component_type, pointer)};
Id value{ctx.OpLoad(generic.component_type, pointer)};
if (ctx.stage == Stage::Fragment && ctx.noperspective_as_smooth_inputs[index]) {
// Written times Position.w and interpolated perspective-correct: undo with 1/w.
// Only float inputs are marked, and DefineInputs forces FragCoord for them.
ASSERT(generic.load_op == InputGenericLoadOp::None && ValidId(ctx.input_position));
const Id frag_w{ctx.OpLoad(
ctx.F32[1],
ctx.OpAccessChain(ctx.input_f32, ctx.input_position, ctx.Const(3u)))};
value = ctx.OpFMul(ctx.F32[1], value, frag_w);
}
return [&ctx, generic, value]() {
switch (generic.load_op) {
case InputGenericLoadOp::Bitcast:
@@ -502,7 +511,7 @@ void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, Id value) {
const Id pointer{ctx.OpAccessChain(ctx.output_f32, ctx.frag_color.at(index), component_id)};
if (frag_nan_guard) { // (amd-fix, mma_layout.h) NaN -> 0, the rest clamped to +-1e30
const Id clamped{ctx.OpNMin(ctx.F32[1], ctx.OpNMax(ctx.F32[1], value, ctx.Const(-1e30f)), ctx.Const(1e30f))};
value = ctx.OpSelect(ctx.F32[1], ctx.OpIsNan(ctx.U1, value), ctx.Const(0.0f), clamped);
value = ctx.OpSelect(ctx.F32[1], IsNan(ctx, value, 32), ctx.Const(0.0f), clamped);
}
ctx.OpStore(pointer, value);
} else if (type == AttributeType::UnsignedInt) {

View File

@@ -155,7 +155,7 @@ Id EmitConvertF16F32(EmitContext& ctx, Id value) {
return ctx.OpFConvert(ctx.F16[1], value);
#else
const auto result = ctx.OpFConvert(ctx.F16[1], value);
const auto isOverflowing = ctx.OpIsNan(ctx.U1, result);
const auto isOverflowing = IsNan(ctx, result, 16); // (hookshot: mma_layout.h nan_bits)
return ctx.OpSelect(ctx.F16[1], isOverflowing, ctx.Constant(ctx.F16[1], 0), result);
#endif
}

View File

@@ -4,6 +4,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
#include "shader_recompiler/backend/spirv/mma_layout.h"
#include "shader_recompiler/backend/spirv/spirv_emit_context.h"
#include "shader_recompiler/frontend/ir/modifiers.h"
@@ -381,16 +382,40 @@ Id EmitFPUnordGreaterThanEqual64(EmitContext& ctx, Id lhs, Id rhs) {
return FPUnordCompare(&EmitContext::OpFUnordGreaterThanEqual, ctx, lhs, rhs);
}
// (hookshot) isnan of a scalar float of that many bits. With nan_bits (mma_layout.h: AMD's older Windows shader compiler) it is
// a test of the value's bits, a NaN being an exponent of all ones and a mantissa that is not 0, which no float-mode setting of the
// compiler can fold away; else OpIsNan, as before
Id IsNan(EmitContext& ctx, Id value, u32 bits) {
if (!nan_bits) {
return ctx.OpIsNan(ctx.U1, value);
}
if (bits == 64) {
const Id words{ctx.OpBitcast(ctx.U32[2], value)};
const Id high{ctx.OpBitwiseAnd(ctx.U32[1], ctx.OpCompositeExtract(ctx.U32[1], words, 1U),
ctx.Const(0x7fffffffu))};
const Id low{ctx.OpCompositeExtract(ctx.U32[1], words, 0U)};
const Id infinity{ctx.Const(0x7ff00000u)};
return ctx.OpLogicalOr(ctx.U1, ctx.OpUGreaterThan(ctx.U1, high, infinity),
ctx.OpLogicalAnd(ctx.U1, ctx.OpIEqual(ctx.U1, high, infinity),
ctx.OpINotEqual(ctx.U1, low, ctx.u32_zero_value)));
}
if (bits == 16) {
value = ctx.OpFConvert(ctx.F32[1], value); // (exact: a NaN stays a NaN, and nothing else becomes one)
}
const Id magnitude{ctx.OpBitwiseAnd(ctx.U32[1], ctx.OpBitcast(ctx.U32[1], value), ctx.Const(0x7fffffffu))};
return ctx.OpUGreaterThan(ctx.U1, magnitude, ctx.Const(0x7f800000u));
}
Id EmitFPIsNan16(EmitContext& ctx, Id value) {
return ctx.OpIsNan(ctx.U1, value);
return IsNan(ctx, value, 16);
}
Id EmitFPIsNan32(EmitContext& ctx, Id value) {
return ctx.OpIsNan(ctx.U1, value);
return IsNan(ctx, value, 32);
}
Id EmitFPIsNan64(EmitContext& ctx, Id value) {
return ctx.OpIsNan(ctx.U1, value);
return IsNan(ctx, value, 64);
}
} // namespace Shader::Backend::SPIRV

View File

@@ -9,6 +9,7 @@
#include "common/settings.h"
#include "shader_recompiler/backend/spirv/emit_spirv.h"
#include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
#include "shader_recompiler/backend/spirv/mma_layout.h"
#include "shader_recompiler/backend/spirv/spirv_emit_context.h"
#include "shader_recompiler/frontend/ir/modifiers.h"
@@ -445,9 +446,10 @@ Id ImageGatherSubpixelOffset(EmitContext& ctx, const IR::TextureInstInfo& info,
case TextureType::Color2DRect:
return calculate_coords(2);
case TextureType::ColorArray2D:
case TextureType::ColorCube:
return calculate_coords(3);
default:
// (hookshot) a cube's coordinates are a direction: no texel to nudge by, and its size query has two components, not
// three (the cube case here made invalid SPIR-V: OpImageQuerySizeLod's result of three components on a cube)
return coords;
}
}
@@ -488,6 +490,64 @@ void AddOffsetToCoordinates(EmitContext& ctx, const IR::TextureInstInfo& info, I
}
coords = ctx.OpIAdd(result_type, coords, offset);
}
// (hookshot, mma_layout.h fetch_bounds) Whether a texel fetch of image at coords (with its level, or its sample, when it has one)
// is inside the image, as U1; coords, lod and sample are made ones that are (a coordinate out of range 0, a level or sample past
// the last 0), so that the fetch itself never leaves the image, and the caller answers zero for it. NVIDIA's GPUs answer an
// out-of-bounds fetch with zero (measured on an RTX 3090 with robustness off: every texture type, past each edge, past the last
// level and the last sample; the alpha of a format without one is 1 there, as in range, and 0 here: the translation does not
// know the format). Vulkan leaves such a fetch undefined without robustImageAccess, and on AMD the ground fog's depth reads at
// points projected off screen took the other branch: a pale box at the screen's edges (found by Ocarina of Lain's author on
// an RX 9070). Id{}: a type left as it was: a texel buffer while robustBufferAccess2 is on (on by default: the Robust buffers
// row), which answers its out-of-bounds reads with zero already (fetch_bounds_buffers), and the cube types, which no fetch reads
Id FetchInBounds(EmitContext& ctx, const IR::TextureInstInfo& info, Id image, Id& coords, Id& lod, Id& sample) {
u32 dims{};
switch (info.type) {
case TextureType::Buffer:
if (!fetch_bounds_buffers) {
return Id{};
}
dims = 1;
break;
case TextureType::Color1D:
dims = 1;
break;
case TextureType::ColorArray1D:
case TextureType::Color2D:
case TextureType::Color2DRect:
dims = 2;
break;
case TextureType::ColorArray2D:
case TextureType::Color3D:
dims = 3;
break;
default:
return Id{};
}
const Id size_type{ctx.U32[dims]};
Id level_ok{};
Id size{};
if (Sirit::ValidId(lod)) {
const Id levels{ctx.OpImageQueryLevels(ctx.U32[1], image)};
level_ok = ctx.OpULessThan(ctx.U1, lod, levels);
lod = ctx.OpSelect(ctx.U32[1], level_ok, lod, ctx.u32_zero_value);
size = ctx.OpImageQuerySizeLod(size_type, image, lod);
} else {
size = ctx.OpImageQuerySize(size_type, image); // (a texel buffer, or a multisampled image)
}
// (unsigned: a negative coordinate is past the end too)
const Id inside{ctx.OpULessThan(dims == 1 ? ctx.U1 : ctx.TypeVector(ctx.U1, dims), coords, size)};
coords = ctx.OpSelect(size_type, inside, coords, ctx.ConstantNull(size_type));
Id in_bounds{dims == 1 ? inside : ctx.OpAll(ctx.U1, inside)};
if (Sirit::ValidId(level_ok)) {
in_bounds = ctx.OpLogicalAnd(ctx.U1, in_bounds, level_ok);
}
if (Sirit::ValidId(sample)) {
const Id sample_ok{ctx.OpULessThan(ctx.U1, sample, ctx.OpImageQuerySamples(ctx.U32[1], image))};
sample = ctx.OpSelect(ctx.U32[1], sample_ok, sample, ctx.u32_zero_value);
in_bounds = ctx.OpLogicalAnd(ctx.U1, in_bounds, sample_ok);
}
return in_bounds;
}
} // Anonymous namespace
Id EmitBindlessImageSampleImplicitLod(EmitContext&) {
@@ -724,6 +784,17 @@ Id EmitImageFetch(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id c
// This image is multisampled, lod must be implicit
lod = Id{};
}
if (fetch_bounds) { // (hookshot: FetchInBounds)
const Id image{TextureImage(ctx, info, index)};
const Id in_bounds{FetchInBounds(ctx, info, image, coords, lod, ms)};
const ImageOperands operands(lod, ms);
Id color{Emit(&EmitContext::OpImageSparseFetch, &EmitContext::OpImageFetch, ctx, inst,
result_type, image, coords, operands.MaskOptional(), operands.Span())};
if (Sirit::ValidId(in_bounds)) {
color = ctx.OpSelect(result_type, in_bounds, color, ctx.ConstantNull(result_type));
}
return is_integer ? ctx.OpBitcast(ctx.F32[4], color) : color;
}
const ImageOperands operands(lod, ms);
const Id color{Emit(&EmitContext::OpImageSparseFetch, &EmitContext::OpImageFetch, ctx, inst,
result_type, TextureImage(ctx, info, index), coords,

View File

@@ -644,4 +644,7 @@ Id EmitDPdyFine(EmitContext& ctx, Id op_a);
Id EmitDPdxCoarse(EmitContext& ctx, Id op_a);
Id EmitDPdyCoarse(EmitContext& ctx, Id op_a);
// (hookshot) Helpers the emitters share, not IR instructions
Id IsNan(EmitContext& ctx, Id value, u32 bits); // (OpIsNan, or a test of the bits: mma_layout.h nan_bits)
} // namespace Shader::Backend::SPIRV

View File

@@ -4,6 +4,9 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <utility>
#include <vector>
#include "shader_recompiler/backend/spirv/emit_spirv.h"
#include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
#include "shader_recompiler/backend/spirv/spirv_emit_context.h"
@@ -20,6 +23,74 @@ void ConvertDepthMode(EmitContext& ctx) {
ctx.OpStore(ctx.output_position, vector);
}
/// Output variable and the unscaled value it held before ScaleNoPerspectiveOutputs.
using ScaledOutputs = std::vector<std::pair<Id, Id>>;
/// Write screen-linear generic outputs times Position.w (see RuntimeInfo::noperspective_outputs).
/// Returns the unscaled values so a geometry shader can restore them after EmitVertex.
ScaledOutputs ScaleNoPerspectiveOutputs(EmitContext& ctx) {
ScaledOutputs saved;
if (ctx.runtime_info.noperspective_outputs.none() || !Sirit::ValidId(ctx.output_position)) {
return saved;
}
const Id position{ctx.OpLoad(ctx.F32[4], ctx.output_position)};
const Id w{ctx.OpCompositeExtract(ctx.F32[1], position, 3u)};
for (size_t index = 0; index < ctx.output_generics.size(); ++index) {
if (!ctx.runtime_info.noperspective_outputs.test(index)) {
continue;
}
Id last{};
for (const GenericElementInfo& element : ctx.output_generics[index]) {
if (!Sirit::ValidId(element.id) || element.id.value == last.value ||
element.num_components == 0) {
continue;
}
last = element.id;
const Id type{ctx.F32[element.num_components]};
const Id value{ctx.OpLoad(type, element.id)};
const Id scaled{element.num_components == 1 ? ctx.OpFMul(type, value, w)
: ctx.OpVectorTimesScalar(type, value, w)};
ctx.OpStore(element.id, scaled);
saved.emplace_back(element.id, value);
}
}
return saved;
}
/// A geometry shader keeps its outputs between EmitVertex calls: restore the unscaled values.
void RestoreNoPerspectiveOutputs(EmitContext& ctx, const ScaledOutputs& saved) {
for (const auto& [id, value] : saved) {
ctx.OpStore(id, value);
}
}
/// (hookshot) RuntimeInfo::capture_position: the position as the stage leaves it, stored at the pushed address's slot for
/// this vertex (CAPTURE_PUSH_OFFSET), when the address is not 0 and the slot below the pushed count. What is drawn is not
/// changed: the store is the stage's last act, and reads only what it wrote
void CapturePosition(EmitContext& ctx) {
const auto push = [&](Id type, u32 member) {
const Id pointer{ctx.TypePointer(spv::StorageClass::PushConstant, type)};
return ctx.OpLoad(type, ctx.OpAccessChain(pointer, ctx.rescaling_push_constants, ctx.Const(member)));
};
const Id address{push(ctx.U64, ctx.capture_address_member_index)};
const Id first{push(ctx.U32[1], ctx.capture_first_member_index)};
const Id count{push(ctx.U32[1], ctx.capture_count_member_index)};
const Id slot{ctx.OpISub(ctx.U32[1], ctx.OpLoad(ctx.U32[1], ctx.capture_vertex_index), first)};
const Id zero{ctx.Constant(ctx.U64, u64{0})};
const Id wanted{ctx.OpLogicalAnd(ctx.U1, ctx.OpINotEqual(ctx.U1, address, zero), ctx.OpULessThan(ctx.U1, slot, count))};
const Id store_label{ctx.OpLabel()};
const Id end_label{ctx.OpLabel()};
ctx.OpSelectionMerge(end_label, spv::SelectionControlMask::MaskNone);
ctx.OpBranchConditional(wanted, store_label, end_label);
ctx.AddLabel(store_label);
const Id offset{ctx.OpIMul(ctx.U64, ctx.OpUConvert(ctx.U64, slot), ctx.Constant(ctx.U64, u64{16}))};
const Id at{ctx.OpIAdd(ctx.U64, address, offset)};
const Id position{ctx.OpLoad(ctx.F32[4], ctx.output_position)};
ctx.OpStore(ctx.OpConvertUToPtr(ctx.capture_pointer, at), position, spv::MemoryAccessMask::Aligned, 16U);
ctx.OpBranch(end_label);
ctx.AddLabel(end_label);
}
void SetFixedPipelinePointSize(EmitContext& ctx) {
if (ctx.runtime_info.fixed_state_point_size) {
const float point_size{*ctx.runtime_info.fixed_state_point_size};
@@ -151,6 +222,12 @@ void EmitEpilogue(EmitContext& ctx) {
!ctx.profile.support_native_ndc) {
ConvertDepthMode(ctx);
}
if (ctx.stage == Stage::VertexB || ctx.stage == Stage::TessellationEval) {
ScaleNoPerspectiveOutputs(ctx);
}
if (ctx.stage == Stage::VertexB && ctx.wants_capture) { // (after the depth's conversion: the position as drawn)
CapturePosition(ctx);
}
if (ctx.stage == Stage::Fragment) {
AlphaTest(ctx);
}
@@ -160,6 +237,7 @@ void EmitEmitVertex(EmitContext& ctx, const IR::Value& stream) {
if (ctx.runtime_info.convert_depth_mode && !ctx.profile.support_native_ndc) {
ConvertDepthMode(ctx);
}
const ScaledOutputs scaled{ScaleNoPerspectiveOutputs(ctx)};
if (!ctx.profile.support_geometry_streams) {
throw NotImplementedException("Geometry streams");
} else if (stream.IsImmediate()) {
@@ -170,6 +248,7 @@ void EmitEmitVertex(EmitContext& ctx, const IR::Value& stream) {
}
// Restore fixed pipeline point size after emitting the vertex
SetFixedPipelinePointSize(ctx);
RestoreNoPerspectiveOutputs(ctx, scaled);
}
void EmitEndPrimitive(EmitContext& ctx, const IR::Value& stream) {

View File

@@ -25,5 +25,15 @@ inline bool frag_nan_guard = false;
// the colour pass is then rejected in spots: holes showing the bright sky, blown up by bloom (the glowing flowers).
// On for every card but NVIDIA (vk_coopmat.cpp); RX_VK_NO_INVARIANT=1 off. From NX2navi patch 0007.
inline bool position_invariant = false;
// (hookshot) An out-of-bounds texel fetch answers zero, as NVIDIA's GPUs answer it (measured on an RTX 3090), rather than
// whatever the host's does (EmitImageFetch). Vulkan leaves it undefined without robustImageAccess; on AMD the ground fog's
// off-screen depth reads drew a pale box at the screen's edges. Every card but NVIDIA (gpu/vk/vk_vendor.h); RX_VK_FETCH_BOUNDS
inline bool fetch_bounds = false;
// (hookshot) and texel buffers' fetches too: only when robustBufferAccess2 is off (the Debug tab's Robust buffers Off, RX_VK_ROBUST=0,
// or a driver without it), which otherwise answers their out-of-bounds reads with zero already (vk_coopmat.cpp)
inline bool fetch_bounds_buffers = false;
// (hookshot) isnan tested on the value's bits (IsNan in emit_spirv_floating_point.cpp), not OpIsNan, for AMD's older Windows
// shader compiler (RX 5000 and RX 6000: gpu/vk/vk_vendor.h); RX_VK_NAN_BITS=0 or 1
inline bool nan_bits = false;
} // namespace Shader::Backend::SPIRV

View File

@@ -524,6 +524,12 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf
if (runtime_info.mesh_fallback || runtime_info.mesh_fallback_vertex) {
wants_mesh_fallback = true; // (before any DefineRescalingInputPushConstant, which is made once)
}
// (hookshot) a scene vertex stage's position capture: its members go in the one push block, so decided before it is made.
// Not with the render area's block (a second push block; full-screen passes, never the scene's geometry)
if (runtime_info.capture_position && stage == Stage::VertexB && !runtime_info.mesh_fallback_vertex &&
profile.unified_descriptor_binding && profile.support_int64 && !program.info.uses_render_area) {
wants_capture = true;
}
DefineCommonTypes(program.info);
DefineCommonConstants();
DefineInterfaces(program);
@@ -576,6 +582,21 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf
mesh_info = program.mesh;
DefineRescalingInputPushConstant();
}
if (wants_capture) { // (the store itself: EmitEpilogue, emit_spirv_special.cpp)
if (!Sirit::ValidId(U64)) {
AddCapability(spv::Capability::Int64);
U64 = Name(TypeInt(64, false), "u64");
}
AddCapability(spv::Capability::PhysicalStorageBufferAddresses);
AddExtension("SPV_KHR_physical_storage_buffer");
SetMemoryModel(spv::AddressingModel::PhysicalStorageBuffer64, spv::MemoryModel::GLSL450);
// (Vulkan's VertexIndex, base vertex included: the redraw draws the same indices with the same base)
capture_vertex_index = Sirit::ValidId(vertex_index)
? vertex_index
: DefineInput(*this, U32[1], true, spv::BuiltIn::VertexIndex);
capture_pointer = TypePointer(spv::StorageClass::PhysicalStorageBuffer, F32[4]);
program.info.captures_position = true;
}
DefineMesh(program);
DefineLocalMemory(program);
DefineSharedMemory(program);
@@ -592,6 +613,13 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf
DefineGlobalMemoryFunctions(program.info);
DefineRescalingInput(program.info);
DefineRenderArea(program.info);
// (the capture's push block, when nothing above made it. Not made before them: the block is made once, with the members
// wanted by then, and DefineGlobalMemoryFunctions asks for the page table's (wants_guest_pages) only as it runs. Made
// earlier, the block had no such member, the stage read the table's address from member 0, and the GPU was lost at the
// first frame in play)
if (wants_capture) {
DefineRescalingInputPushConstant();
}
}
EmitContext::~EmitContext() = default;
@@ -1603,6 +1631,14 @@ void EmitContext::DefineRescalingInputPushConstant() {
mesh_fallback_indirect_input_member_index = member_index++;
}
}
if (wants_capture) {
members.push_back(U64);
capture_address_member_index = member_index++;
members.push_back(U32[1]);
capture_first_member_index = member_index++;
members.push_back(U32[1]);
capture_count_member_index = member_index++;
}
const Id push_constant_struct{TypeStruct(std::span(members.data(), members.size()))};
Decorate(push_constant_struct, spv::Decoration::Block);
Name(push_constant_struct, "ResolutionInfo");
@@ -1641,6 +1677,14 @@ void EmitContext::DefineRescalingInputPushConstant() {
MESH_FALLBACK_INDIRECT_INPUT_PUSH_OFFSET);
}
}
if (wants_capture) {
MemberDecorate(push_constant_struct, capture_address_member_index, spv::Decoration::Offset, CAPTURE_PUSH_OFFSET);
MemberDecorate(push_constant_struct, capture_first_member_index, spv::Decoration::Offset, CAPTURE_FIRST_PUSH_OFFSET);
MemberDecorate(push_constant_struct, capture_count_member_index, spv::Decoration::Offset, CAPTURE_COUNT_PUSH_OFFSET);
MemberName(push_constant_struct, capture_address_member_index, "capture_address");
MemberName(push_constant_struct, capture_first_member_index, "capture_first");
MemberName(push_constant_struct, capture_count_member_index, "capture_count");
}
const Id pointer_type{TypePointer(spv::StorageClass::PushConstant, push_constant_struct)};
rescaling_push_constants = AddGlobalVariable(pointer_type, spv::StorageClass::PushConstant);
Name(rescaling_push_constants, "rescaling_push_constants");
@@ -2017,6 +2061,12 @@ void EmitContext::DefineImages(const Info& info, u32& binding, u32& scaling_inde
void EmitContext::DefineInputs(const IR::Program& program) {
const Info& info{program.info};
const VaryingState loads{info.loads.mask | info.passthrough.mask};
const bool noperspective_as_smooth{
stage == Stage::Fragment && runtime_info.scaled_noperspective_inputs &&
std::ranges::any_of(info.interpolation,
[](Interpolation mode) {
return mode == Interpolation::NoPerspective;
})};
if (info.uses_workgroup_id) {
workgroup_id = DefineInput(*this, U32[3], false, spv::BuiltIn::WorkgroupId);
@@ -2084,7 +2134,8 @@ void EmitContext::DefineInputs(const IR::Program& program) {
layer = DefineInput(*this, U32[1], false, spv::BuiltIn::Layer);
Decorate(layer, spv::Decoration::Flat);
}
if (loads.AnyComponent(IR::Attribute::PositionX)) {
if (loads.AnyComponent(IR::Attribute::PositionX) ||
info.passthrough.AnyComponent(IR::Attribute::PositionX) || noperspective_as_smooth) {
const bool is_fragment{stage == Stage::Fragment};
if (!is_fragment && profile.has_broken_spirv_position_input) {
need_input_position_indirect = true;
@@ -2189,7 +2240,12 @@ void EmitContext::DefineInputs(const IR::Program& program) {
case Interpolation::Smooth:
break;
case Interpolation::NoPerspective:
Decorate(id, spv::Decoration::NoPerspective);
// The previous stage may write it times Position.w (noperspective_outputs).
if (!runtime_info.scaled_noperspective_inputs) {
Decorate(id, spv::Decoration::NoPerspective);
} else {
noperspective_as_smooth_inputs[index] = true;
}
break;
case Interpolation::Flat:
Decorate(id, spv::Decoration::Flat);

View File

@@ -369,6 +369,8 @@ public:
bool need_input_position_indirect{};
Id input_position{};
std::array<InputGenericInfo, 32> input_generics{};
/// Screen-linear fragment inputs read through perspective interpolation, times FragCoord.w.
std::array<bool, 32> noperspective_as_smooth_inputs{};
Id output_point_size{};
Id output_position{};
@@ -429,6 +431,13 @@ public:
u32 mesh_fallback_payload_member_index{};
u32 mesh_fallback_records_member_index{};
u32 mesh_fallback_indirect_input_member_index{};
/// (hookshot) RuntimeInfo::capture_position: the push block's capture members, the vertex index and a vec4 pointer type
bool wants_capture{};
u32 capture_address_member_index{};
u32 capture_first_member_index{};
u32 capture_count_member_index{};
Id capture_vertex_index{};
Id capture_pointer{};
/// A pointer to the u32 at a PhysicalStorageBuffer device address
Id MeshFallbackWord(Id address);

View File

@@ -6,6 +6,7 @@
#pragma once
#include <string>
#include "shader_recompiler/environment.h"
#include "shader_recompiler/frontend/ir/program.h"
@@ -31,6 +32,11 @@ void SsaRewritePass(IR::Program& program);
void PositionPass(Environment& env, IR::Program& program);
void BlitConstantsPass(Environment& env, IR::Program& program);
void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo& host_info);
/// (hookshot) The texture handles of the last translation on this thread that no handle program could follow (they take
/// the first constant buffer word on their path): how many, and the operations the programs stopped at. Resets them.
u32 TakeHandleFallbacks(std::string& stopped_at);
/// (Triforce) The texture handles of the last translation on this thread that were given a handle table. Resets them.
u32 TakeHandleTables();
void LayerPass(IR::Program& program, const HostTranslateInfo& host_info);
void VendorWorkaroundPass(IR::Program& program);
void VerificationPass(const IR::Program& program);

View File

@@ -5,6 +5,9 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <algorithm>
#include <atomic> // (EDEN_IR_PRE below. The players' compiler has libc++, which does not bring it in by itself)
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <unordered_map>
#include <unordered_set>
@@ -248,10 +251,16 @@ bool IsStorageImageOpcode(IR::Opcode opcode) {
u32 DynamicSampledTextureArrayCount(const TextureInstVector& to_replace) {
u32 count{};
// (hookshot) the samples of one handle table share one descriptor: counted once (the water's 69 shadow samples of
// one table cut its share of the budget to 14 elements)
std::unordered_set<u32> tables;
for (const TextureInst& inst : to_replace) {
const auto flags{inst.inst->Flags<IR::TextureInstInfo>()};
if (inst.cbuf.count > 1 && !IsStorageImageOpcode(IndexedInstruction(*inst.inst)) &&
flags.type != TextureType::Buffer) {
if (inst.cbuf.handle_program != ~0U && !tables.insert(inst.cbuf.handle_program).second) {
continue;
}
++count;
}
}
@@ -451,6 +460,13 @@ bool IsUndefined(IR::Opcode opcode) {
thread_local std::unordered_map<const IR::Inst*, u32> g_handle_slots;
thread_local u32 g_handle_slot_count{};
thread_local int g_handle_depth{};
// (hookshot) A handle table read in a loop (a light's shadow map, the light's index from a per-pixel list): the
// element index, built as HandleOp::Index wherever the handle reads it
thread_local const IR::Inst* g_index_operand{};
// (hookshot) The handles no handle program followed in this translation, and where they stopped (TakeHandleFallbacks)
thread_local u32 g_fallbacks{};
thread_local u32 g_tables{}; // (Triforce: the handles given a table in this translation, TakeHandleTables)
thread_local std::string g_fallback_ops;
bool BuildHandleNode(const IR::Value& value, std::vector<u32>& code, int& budget,
const IR::Block* block);
@@ -524,6 +540,10 @@ bool BuildHandleNode(const IR::Value& value, std::vector<u32>& code, int& budget
return true;
}
const IR::Inst* const inst{value.InstRecursive()};
if (inst == g_index_operand) {
emit(Op::Index);
return true;
}
const auto build = [&](size_t arg) { return BuildHandleProgram(inst->Arg(arg), code, budget, block); };
const auto binary = [&](Op op) {
if (!build(0) || !build(1)) return false;
@@ -587,7 +607,8 @@ bool BuildHandleNode(const IR::Value& value, std::vector<u32>& code, int& budget
case IR::Opcode::GetCbufU32:
case IR::Opcode::LoadStorage32:
return load_word(inst, 0);
case IR::Opcode::CompositeExtractU32x2: {
case IR::Opcode::CompositeExtractU32x2:
case IR::Opcode::CompositeExtractU32x4: {
const IR::Value element{inst->Arg(1)};
if (!element.IsImmediate() || inst->Arg(0).IsImmediate()) return false;
return load_word(inst->Arg(0).InstRecursive(), element.U32() * 4);
@@ -930,10 +951,140 @@ std::optional<ConstBufferAddr> TryGetConstBuffer(const IR::Inst* inst, Environme
};
}
IR::Value SkipIdentity(IR::Value v) {
for (int i = 0; i < 16 && !v.IsImmediate() && v.InstRecursive()->GetOpcode() == IR::Opcode::Identity; ++i) {
v = v.InstRecursive()->Arg(0);
}
return v;
}
// The value a 16-bit half extract reads (BitFieldUExtract x, 0, 16: the low half of an XMAD pair's index), else itself
IR::Value IndexThroughLowHalf(IR::Value v) {
v = SkipIdentity(v);
if (v.IsImmediate()) return v;
const IR::Inst* const i{v.InstRecursive()};
if (i->GetOpcode() == IR::Opcode::BitFieldUExtract) {
const IR::Value offset{SkipIdentity(i->Arg(1))};
const IR::Value count{SkipIdentity(i->Arg(2))};
if (offset.IsImmediate() && count.IsImmediate() && offset.U32() == 0 && count.U32() == 16) {
return SkipIdentity(i->Arg(0));
}
}
return v;
}
// (hookshot) The handle is a word of a storage buffer record whose address adds index * stride, a table of records:
// Ocarina of Time's water and wet floors read each light's shadow map handle from the light's record, for the lights
// of the pixel's cluster. The index (in *index) when the handle is read so: a multiply by a stride of 4..4096 bytes (or
// an XMAD pair's halves of one) or a shift by 2..12, in the sums that make the record's address.
bool FindHandleTableIndex(const IR::Value& handle, IR::Value& index) {
const IR::Value v{SkipIdentity(handle)};
if (v.IsImmediate()) return false;
const IR::Inst* load{v.InstRecursive()};
if (load->GetOpcode() == IR::Opcode::CompositeExtractU32x2 ||
load->GetOpcode() == IR::Opcode::CompositeExtractU32x4) {
const IR::Value src{SkipIdentity(load->Arg(0))};
if (src.IsImmediate()) return false;
load = src.InstRecursive();
}
switch (load->GetOpcode()) {
case IR::Opcode::LoadStorage32:
case IR::Opcode::LoadStorage64:
case IR::Opcode::LoadStorage128:
break;
default:
return false;
}
// Through the sums that make the record's address only
std::vector<IR::Value> work{load->Arg(1)};
for (int steps = 0; !work.empty() && steps < 64; ++steps) {
const IR::Value cur{SkipIdentity(work.back())};
work.pop_back();
if (cur.IsImmediate()) continue;
const IR::Inst* const i{cur.InstRecursive()};
switch (i->GetOpcode()) {
case IR::Opcode::IAdd32:
case IR::Opcode::ISub32:
work.push_back(i->Arg(0));
work.push_back(i->Arg(1));
break;
case IR::Opcode::IMul32:
for (size_t a = 0; a < 2; ++a) {
const IR::Value k{SkipIdentity(i->Arg(a))};
const IR::Value x{IndexThroughLowHalf(i->Arg(1 - a))};
if (k.IsImmediate() && k.U32() >= 4 && k.U32() <= 4096 && !x.IsImmediate()) {
index = x;
return true;
}
}
break;
case IR::Opcode::ShiftLeftLogical32: {
const IR::Value k{SkipIdentity(i->Arg(1))};
const IR::Value x{SkipIdentity(i->Arg(0))};
if (k.IsImmediate() && k.U32() >= 2 && k.U32() <= 12 && !x.IsImmediate()) {
index = x;
return true;
}
break;
}
default:
break;
}
}
return false;
}
// (Triforce, 11-13) The handle behind the merges of a register. Ocarina of Time's wet floors and water filter each
// projector's shadow map in one of several ways, picked by a jump table; the 9- and the 16-sample filters come after the
// others, and their handle is a Phi of the record's handle word (the one value, on every path that leads to them) and of
// an earlier filter's RESULT, which the same register held on a path that does not lead to them. The search for the
// handle's constant buffer word went into that result's sample, down to its coordinates, and took the first constant it
// met there for the handle: a float, 0.001, no texture. Every one of those 25 samples then read the stand-in texture,
// "in shadow", and the projector's whole cluster tiles lost their local lights: the dark squares on wet floors (upstream
// issue 2509; its pull request 3047 gives the handle's table to the 44 samples that read the record directly, and
// these 25 stayed as they were). A texture sample's result is never a handle: such inputs are left out, and when what is
// left is one and the same value, that value is the handle. Anything else is returned as it is.
// EDEN_HANDLE_PHI=0: as before
IR::Value ResolveHandlePhi(const IR::Value& handle) {
static const char* const off_env{std::getenv("EDEN_HANDLE_PHI")};
if (off_env && off_env[0] == '0') return handle;
const IR::Value start{SkipIdentity(handle)};
if (start.IsImmediate() || start.InstRecursive()->GetOpcode() != IR::Opcode::Phi) return handle;
std::vector<IR::Value> work{start};
std::unordered_set<const IR::Inst*> seen;
const IR::Inst* found{};
IR::Value found_value;
for (int steps = 0; !work.empty(); ++steps) {
if (steps > 512) return handle;
const IR::Value cur{SkipIdentity(work.back())};
work.pop_back();
if (cur.IsImmediate()) return handle; // (a constant on a path: not this case)
const IR::Inst* const i{cur.InstRecursive()};
if (i->GetOpcode() == IR::Opcode::Phi) {
if (seen.insert(i).second) {
for (size_t a = 0; a < i->NumArgs(); ++a) work.push_back(i->Arg(a));
}
continue;
}
// (a sample's result, or its bits)
const IR::Inst* source{i};
if (source->GetOpcode() == IR::Opcode::BitCastU32F32) {
const IR::Value bits{SkipIdentity(source->Arg(0))};
if (!bits.IsImmediate()) source = bits.InstRecursive();
}
if (IsTextureInstruction(*source)) continue;
if (found && found != i) return handle; // (two different values: a real choice, left to the search)
found = i;
found_value = cur;
}
return found ? found_value : handle;
}
TextureInst MakeInst(Environment& env, IR::Block* block, IR::Inst& inst, const HostTranslateInfo& host_info) {
ConstBufferAddr addr;
if (IsBindless(inst)) {
const std::optional<ConstBufferAddr> track_addr{TrackCached(inst.Arg(0), env, host_info, block)};
const IR::Value handle{ResolveHandlePhi(inst.Arg(0))};
const std::optional<ConstBufferAddr> track_addr{TrackCached(handle, env, host_info, block)};
if (track_addr) {
addr = *track_addr;
@@ -942,10 +1093,61 @@ TextureInst MakeInst(Environment& env, IR::Block* block, IR::Inst& inst, const H
std::vector<u32> code;
int budget = 4096;
g_handle_failure = IR::Opcode::Void;
if (!g_handle_programs || !BuildHandleProgram(inst.Arg(0), code, budget, block)) {
if (!g_handle_programs || !BuildHandleProgram(handle, code, budget, block)) {
// (hookshot) A table read in a loop: one texture for each element, picked in the shader by the
// element's index (RX_VK_HANDLE_TABLE=0: the search below, one texture for all of them)
static const char* const table_env = std::getenv("RX_VK_HANDLE_TABLE");
static const bool table_on = !(table_env && table_env[0] == '0');
static const u32 table_count = [] {
const char* const c = std::getenv("RX_VK_HANDLE_TABLE_COUNT");
const long n = c ? std::strtol(c, nullptr, 0) : 64;
return static_cast<u32>(std::clamp<long>(n, 2, 256));
}();
IR::Value index_value;
if (table_on && g_handle_programs && FindHandleTableIndex(handle, index_value)) {
code.clear();
budget = 4096;
g_index_operand = index_value.InstRecursive();
const bool built = BuildHandleProgram(handle, code, budget, block);
g_index_operand = nullptr;
if (built) {
++g_tables;
auto& programs{*g_handle_programs};
auto it{std::ranges::find(programs, code)};
if (it == programs.end()) {
programs.push_back(std::move(code));
it = programs.end() - 1;
}
// The element's index as a descriptor offset (the array reads it back: offset >> 3)
IR::IREmitter ir{*block, IR::Block::InstructionList::s_iterator_to(inst)};
const IR::U32 offset{ir.ShiftLeftLogical(IR::U32{index_value}, ir.Imm32(DESCRIPTOR_SIZE_SHIFT))};
return TextureInst{
.cbuf = ConstBufferAddr{
.index = 0,
.offset = 0,
.shift_left = 0,
.secondary_index = 0,
.secondary_offset = 0,
.secondary_shift_left = 0,
.dynamic_offset = offset,
.count = table_count,
.has_secondary = false,
.handle_program = static_cast<u32>(it - programs.begin()),
},
.inst = &inst,
.block = block,
};
}
code.clear();
budget = 4096;
}
++g_fallbacks;
if (g_fallback_ops.size() < 200) {
g_fallback_ops += fmt::format("{}{}", g_fallback_ops.empty() ? "" : ", ", g_handle_failure);
}
// Not computable for a whole draw (a handle that changes in a loop): keep the
// earlier search through loads, which at least gives the shader a texture.
const std::optional<ConstBufferAddr> legacy{IR::BreadthFirstSearch(inst.Arg(0),
const std::optional<ConstBufferAddr> legacy{IR::BreadthFirstSearch(handle,
[&env, &host_info, block](const IR::Inst* candidate) {
return TryGetConstBuffer(candidate, env, host_info, block);
}, [&](IR::Value operand) { return ResolveConditionalPhi(operand, block); })};
@@ -1167,7 +1369,39 @@ void PatchTexelFetch(IR::Block& block, IR::Inst& inst, TexturePixelFormat pixel_
}
} // Anonymous namespace
u32 TakeHandleTables() {
const u32 n = g_tables;
g_tables = 0;
return n;
}
u32 TakeHandleFallbacks(std::string& stopped_at) {
const u32 n = g_fallbacks;
stopped_at = std::move(g_fallback_ops);
g_fallbacks = 0;
g_fallback_ops.clear();
return n;
}
void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo& host_info) {
// (lab, 11-13) EDEN_IR_PRE=<n>: the programs with n depth-compare samples whose handle is still to be found are
// written out as they come INTO this pass (ir_pre_<n>_<blocks>.txt beside the game): where a sample's handle comes
// from can only be read there, this pass replaces it. An EDEN_ name: the shaders are translated again for it
if (static const char* const pre = std::getenv("EDEN_IR_PRE"); pre) {
size_t n = 0;
for (IR::Block* const b : program.blocks)
for (IR::Inst& i : b->Instructions()) n += i.GetOpcode() == IR::Opcode::BindlessImageSampleDrefImplicitLod;
static std::atomic<int> written{0}; // (at least n of them; the first twelve programs)
if (n && n >= static_cast<size_t>(std::atoi(pre)) && written++ < 12) {
char name[64];
std::snprintf(name, sizeof name, "ir_pre_%zu_%zu.txt", n, program.blocks.size());
if (FILE* const f = std::fopen(name, "w")) {
const std::string text{IR::DumpProgram(program)};
std::fwrite(text.data(), 1, text.size(), f);
std::fclose(f);
}
}
}
// reset per-pass caches
env.cbuf_word_cache.clear();
env.handle_cache.clear();

View File

@@ -88,6 +88,12 @@ struct RuntimeInfo {
VaryingState previous_stage_stores;
/// Fragment stage after a mesh program: generic inputs it writes per primitive.
std::bitset<32> per_primitive_generics{};
/// Last pre-rasterization stage: generic outputs the fragment stage reads screen-linear.
/// They are written times Position.w and the fragment stage reads them perspective-correct
/// times FragCoord.w, so clipping a primitive close to the camera interpolates them right.
std::bitset<32> noperspective_outputs{};
/// Fragment stage: the previous stage writes noperspective_outputs (not a mesh program).
bool scaled_noperspective_inputs{};
/// Fragment stage after a mesh program that draws points: generic inputs the hardware's point sprite
/// replaces with the coordinate within the point (s, t, 0, 1), as NVN's do with the generics the mesh
/// program leaves unwritten.
@@ -111,6 +117,10 @@ struct RuntimeInfo {
u32 mesh_fallback_task_threads{};
/// (hookshot) the fallback's record slots: mesh workgroups past this one write nothing
u32 mesh_fallback_capacity{};
/// (hookshot) a vertex stage that also stores its final position (clip space, after convert_depth_mode) at a pushed
/// device address, one vec4 a vertex: the temporal upscaler's motion of what the game moves in its shaders
/// (vk_temporal.cpp). A zero address stores nothing (Info::captures_position says it was emitted)
bool capture_position{};
TessPrimitive tess_primitive{};
TessSpacing tess_spacing{};

View File

@@ -363,6 +363,7 @@ struct Info {
bool uses_rescaling_uniform{};
bool uses_cbuf_indirect{};
bool uses_render_area{};
bool captures_position{}; ///< (hookshot) RuntimeInfo::capture_position, emitted: CAPTURE_PUSH_OFFSET is read
IR::Type used_constant_buffer_types{};
IR::Type used_storage_buffer_types{};
@@ -420,13 +421,14 @@ enum class HandleOp : u32 {
Select, ///< condition, true value, false value
Tee, ///< slot: keeps the top value in the slot (shared subexpressions)
Load, ///< slot: the value kept there
Index, ///< (hookshot) the array element being evaluated (a handle table read in a loop: one texture an element)
};
/// Runs a handle program; read_cbuf(index, offset) and read_storage(binding, offset) return the
/// words it reads. Returns nothing for a malformed program.
template <typename ReadCbuf, typename ReadStorage>
std::optional<u32> EvaluateHandleProgram(std::span<const u32> code, ReadCbuf&& read_cbuf,
ReadStorage&& read_storage) {
ReadStorage&& read_storage, u32 element = 0) {
std::array<u32, 64> stack{};
std::array<u32, 256> slots{};
size_t size = 0;
@@ -461,6 +463,10 @@ std::optional<u32> EvaluateHandleProgram(std::span<const u32> code, ReadCbuf&& r
push(op == HandleOp::Neg ? 0U - a : op == HandleOp::Not ? ~a : (a == 0 ? 1U : 0U));
continue;
}
if (op == HandleOp::Index) {
push(element);
continue;
}
if (op == HandleOp::Tee) {
const u32 slot = operand();
if (slot < slots.size() && size > 0) slots[slot] = stack[size - 1];