mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-25 18:48:51 -04:00
Triforce v0.8.0
Evil HD (SMAA Ultra, 16x filtering, full textures, ambient occlusion with bounce light, sunlight shafts, foliage drawn 4x as far, the remastered texture pack's loader and the launcher's download of it), FSR 3.1 frame generation for every GPU, Intel XeSS, FSR 4 model choice, ReShade presets, Auto (this PC), HUD placement, the options menu in 13 languages, a second input for every control, hold to toggle, DualSense speaker and haptics, NFC from the player's own dumps, the above-60 fps game fixes, the Debug tab's flight and Inventory fixes, the launcher's full settings, and over 250 fixes.
This commit is contained in:
@@ -169,6 +169,13 @@ void Traverse(EmitContext& ctx, IR::Program& program) {
|
||||
ctx.OpAtomicIAdd(ctx.U32[1], counter, ctx.Const(static_cast<u32>(spv::Scope::Device)), ctx.u32_zero_value,
|
||||
ctx.Const(1U));
|
||||
}
|
||||
if (ctx.wmma16_fused.active) {
|
||||
if (ctx.wmma16_fused.skip_insts.contains(&inst)) continue;
|
||||
if (inst.GetOpcode() == IR::Opcode::MatrixMulAddF16) {
|
||||
EmitMatrixMulAddF16Fused(ctx, &inst);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
EmitInst(ctx, &inst);
|
||||
}
|
||||
break;
|
||||
@@ -176,6 +183,8 @@ void Traverse(EmitContext& ctx, IR::Program& program) {
|
||||
case IR::AbstractSyntaxNode::Type::If: {
|
||||
const Id if_label{node.data.if_node.body->Definition<Id>()};
|
||||
const Id endif_label{node.data.if_node.merge->Definition<Id>()};
|
||||
// (Triforce: the shared warp's region, said before the merge instruction, which must stand right before its branch)
|
||||
WarpOpenRegion(ctx, node.data.if_node.body, ctx.Def(node.data.if_node.cond));
|
||||
ctx.OpSelectionMerge(endif_label, spv::SelectionControlMask::MaskNone);
|
||||
ctx.OpBranchConditional(ctx.Def(node.data.if_node.cond), if_label, endif_label);
|
||||
break;
|
||||
@@ -196,6 +205,7 @@ void Traverse(EmitContext& ctx, IR::Program& program) {
|
||||
break;
|
||||
}
|
||||
case IR::AbstractSyntaxNode::Type::EndIf:
|
||||
WarpCloseRegion(ctx);
|
||||
if (current_block) {
|
||||
ctx.OpBranch(node.data.end_if.merge->Definition<Id>());
|
||||
}
|
||||
@@ -509,9 +519,11 @@ void SetupCapabilities(const Profile& profile, const Info& info, EmitContext& ct
|
||||
ctx.AddExtension("SPV_KHR_shader_draw_parameters");
|
||||
ctx.AddCapability(spv::Capability::DrawParameters);
|
||||
}
|
||||
// (Triforce: not for a stage on the shared warp, which has no subgroup instruction; its check keeps them)
|
||||
const bool subgroup_free{ctx.warp_shared && !ctx.warp_shared_check};
|
||||
if ((info.uses_subgroup_vote || info.uses_subgroup_invocation_id ||
|
||||
info.uses_subgroup_shuffles || info.uses_subgroup_mask) &&
|
||||
profile.support_vote && profile.SupportsSubgroupStage(ctx.stage)) {
|
||||
profile.support_vote && profile.SupportsSubgroupStage(ctx.stage) && !subgroup_free) {
|
||||
ctx.AddCapability(spv::Capability::GroupNonUniformBallot);
|
||||
ctx.AddCapability(spv::Capability::GroupNonUniformShuffle);
|
||||
if (info.uses_subgroup_shuffles && profile.support_shuffle_relative) {
|
||||
@@ -684,6 +696,12 @@ void PatchMeshInstructions(std::vector<u32>& code, const EmitContext& ctx) {
|
||||
std::vector<u32> EmitSPIRV(const Profile& profile, const RuntimeInfo& runtime_info, IR::Program& program, Bindings& bindings) {
|
||||
EmitContext ctx{profile, runtime_info, program, bindings};
|
||||
if (program.info.uses_cooperative_matrix && mma_software) DefineSoftwareMatrixFunction(ctx);
|
||||
if (program.info.uses_cooperative_matrix && !mma_software && mma_wmma16) {
|
||||
if (!DefineWmma16MatrixFunction(ctx, program))
|
||||
DefineSoftwareMatrixFunction(ctx); // (the tiles do not fit the shared memory)
|
||||
else if (mma_wmma16_fused)
|
||||
DefineWmma16PairFunction(ctx, program);
|
||||
}
|
||||
Id main{};
|
||||
if (runtime_info.mesh_fallback_vertex) { // (hookshot: the fallback's generated vertex stage, which reads the records)
|
||||
const Id void_function{ctx.TypeFunction(ctx.void_id)};
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "common/common_types.h"
|
||||
#include "shader_recompiler/backend/bindings.h"
|
||||
#include "shader_recompiler/backend/spirv/mma_layout.h"
|
||||
#include "shader_recompiler/frontend/ir/program.h"
|
||||
#include "shader_recompiler/profile.h"
|
||||
#include "shader_recompiler/runtime_info.h"
|
||||
@@ -68,12 +69,75 @@ constexpr u32 MESH_FALLBACK_ALLOC_WORD = 4; // (records taken, past the en
|
||||
// a count from the GPU (DrawMeshTasksIndirect, the floor grass) the host knew none, and dispatched no mesh workgroup)
|
||||
constexpr u32 MESH_FALLBACK_DISPATCH_WORD = 5;
|
||||
constexpr u32 MESH_FALLBACK_HEADER_WORDS = 8;
|
||||
constexpr u32 MESH_FALLBACK_RECORD_WORDS = 16u << 20; // (64 MB: ~47k grass primitives of 31 words are 17 MB)
|
||||
// (128 MB. Every program takes records from it, so a program with longer records fits fewer: MeshFallbackFit. At 16M words
|
||||
// the floor grass at the Grass distance's 4x lost its blades: their program's records are 22 words and fit 762600,
|
||||
// its partner's 7 words, and each drew ~1.02M records a frame, so the blades past the fit were left out and drawn white.
|
||||
// 32M words fit 1.52M of 22 words)
|
||||
constexpr u32 MESH_FALLBACK_RECORD_WORDS = 32u << 20;
|
||||
// (emit_spirv_mesh.cpp BufferWordAt makes a word's byte offset in 32 bits)
|
||||
static_assert((u64{MESH_FALLBACK_HEADER_WORDS} + MESH_FALLBACK_RECORD_WORDS) * 4 < (u64{1} << 32));
|
||||
// (Triforce, mma_layout.h mesh_fallback_global_attributes) the mesh workgroups' attribute regions out of workgroup memory: a pool
|
||||
// after the records, in the same buffer. Its header is a ticket counter and a word a slot, the ticket generation that may take
|
||||
// that slot next (both zeroed before each draw, with the records' header); then the slots, each one workgroup's region
|
||||
constexpr u64 MESH_FALLBACK_POOL_OFFSET = u64{MESH_FALLBACK_RECORD_WORDS} * 4 + 4096; // (past the header and the records)
|
||||
constexpr u32 MESH_FALLBACK_POOL_SLOTS = 1024; // (a power of two: a ticket's slot is its low bits)
|
||||
constexpr u32 MESH_FALLBACK_POOL_SLOT_SHIFT = 10;
|
||||
constexpr u32 MESH_FALLBACK_POOL_HEADER_BYTES = 8192; // (the counter and a word a slot: 4100 bytes)
|
||||
constexpr u32 MESH_FALLBACK_POOL_SLOT_BYTES = 32768; // (the largest region a slot holds; a larger one stays in workgroup memory)
|
||||
constexpr u64 MESH_FALLBACK_POOL_BYTES = MESH_FALLBACK_POOL_HEADER_BYTES + u64{MESH_FALLBACK_POOL_SLOTS} * MESH_FALLBACK_POOL_SLOT_BYTES;
|
||||
constexpr u32 MESH_FALLBACK_POOL_WAIT = 1u << 20; // (a wait for a slot's last holder gives up after this many reads)
|
||||
static_assert((MESH_FALLBACK_POOL_SLOTS >> MESH_FALLBACK_POOL_SLOT_SHIFT) == 1 && MESH_FALLBACK_POOL_HEADER_BYTES >= 4 + MESH_FALLBACK_POOL_SLOTS * 4);
|
||||
static_assert(MESH_FALLBACK_POOL_OFFSET >= (u64{MESH_FALLBACK_HEADER_WORDS} + MESH_FALLBACK_RECORD_WORDS) * 4);
|
||||
// (Triforce, mma_layout.h mesh_fallback_shared_warp) the shared warp's words in workgroup memory: an if with a warp operation
|
||||
// inside has the lanes that entered it and those that reached it; a ballot or a vote its lanes and those that arrived; a shuffle
|
||||
// a value a lane and those that arrived, or one value where the source lane is a constant
|
||||
constexpr u32 WARP_REGION_WORDS = 2;
|
||||
constexpr u32 WARP_BALLOT_WORDS = 2;
|
||||
constexpr u32 WARP_SHUFFLE_WORDS = 33;
|
||||
constexpr u32 WARP_BROADCAST_WORDS = 2;
|
||||
constexpr u32 WARP_WAIT = 1u << 16; // (a wait for other lanes gives up after this many reads: a wrong word, not a hung GPU)
|
||||
// (its counters, four words right past the records buffer's regular end, which vk_draw.cpp fallback_buffers_of adds and
|
||||
// clears when the shared warp or the attribute pool is on: the waits that gave up, with RX_VK_MESH_SHARED_WARP_CHECK the
|
||||
// answers compared with the subgroup's and those that differed, and the attribute pool's waits for a slot that gave up
|
||||
// (mma_layout.h mesh_fallback_global_attributes). They only grow and wrap; fallback_warp_counters logs them)
|
||||
constexpr u32 WARP_COUNTER_BYTES = 16;
|
||||
constexpr u32 WARP_COUNTER_WAITS = 0;
|
||||
constexpr u32 WARP_COUNTER_COMPARED = 4;
|
||||
constexpr u32 WARP_COUNTER_DIFFERENT = 8;
|
||||
constexpr u32 WARP_COUNTER_POOL_WAITS = 12;
|
||||
// (the largest records buffer vk_draw.cpp fallback_buffers_of makes, a recording thread's: past it, every program is left
|
||||
// without the fallback. The largest layout, the records with the attribute pool and the counters, ~160 MB, fits)
|
||||
constexpr u64 MESH_FALLBACK_RECORDS_LIMIT = 192ull << 20;
|
||||
static_assert(MESH_FALLBACK_POOL_OFFSET + MESH_FALLBACK_POOL_BYTES + WARP_COUNTER_BYTES <= MESH_FALLBACK_RECORDS_LIMIT);
|
||||
/// Whether the records buffer carries those counters past its regular end: the shared warp's and the pool's waits count there
|
||||
[[nodiscard]] inline bool MeshFallbackCounters() {
|
||||
return mesh_fallback_shared_warp || mesh_fallback_global_attributes;
|
||||
}
|
||||
/// The fallback's records buffer without those counters: the header and the records, and the attribute regions' pool where
|
||||
/// mma_layout.h mesh_fallback_global_attributes is on
|
||||
[[nodiscard]] inline u64 MeshFallbackRecordsBytes() {
|
||||
return mesh_fallback_global_attributes ? MESH_FALLBACK_POOL_OFFSET + MESH_FALLBACK_POOL_BYTES
|
||||
: (u64{MESH_FALLBACK_HEADER_WORDS} + MESH_FALLBACK_RECORD_WORDS) * 4;
|
||||
}
|
||||
|
||||
// (Triforce) how the records lie in their area (RuntimeInfo::mesh_fallback_layout, RX_VK_MESH_FB_LAYOUT):
|
||||
// - Aos: record after record, a record's words together (word H + record * W + line). Neighbouring threads write words a
|
||||
// record apart, so each store of a mesh workgroup touches as many cache lines as it has threads.
|
||||
// - Soa: line after line, the area cut into W lines of `fit` words (word H + line * fit + record), and the threads take the
|
||||
// records in turn, so neighbouring threads write neighbouring words. The same records, the same count, the same draw.
|
||||
enum class MeshFallbackLayout : u32 { Aos = 0, Soa = 1 };
|
||||
/// A line's words: the records of `record_words` words the area holds
|
||||
[[nodiscard]] constexpr u32 MeshFallbackFit(u32 record_words) {
|
||||
return MESH_FALLBACK_RECORD_WORDS / (record_words ? record_words : 1);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::vector<u32> EmitSPIRV(const Profile& profile, const RuntimeInfo& runtime_info, IR::Program& program, Bindings& bindings);
|
||||
[[nodiscard]] inline std::vector<u32> EmitSPIRV(const Profile& profile, IR::Program& program) {
|
||||
Bindings binding;
|
||||
return EmitSPIRV(profile, {}, program, binding);
|
||||
}
|
||||
// (Triforce, AMD RDNA 3/4) whether HMMA_nx2 in the program runs on the WMMA 16x16x16 tiles (mma_layout.h mma_wmma16):
|
||||
// every subgroup full and the tiles fit the shared memory of the device. Otherwise EmitSPIRV gives it the software multiply
|
||||
[[nodiscard]] bool Wmma16Fits(const Profile& profile, const IR::Program& program);
|
||||
|
||||
} // namespace Shader::Backend::SPIRV
|
||||
|
||||
@@ -372,8 +372,15 @@ Id EmitStorageAtomicExchange32x2(EmitContext& ctx, const IR::Value& binding,
|
||||
|
||||
Id EmitStorageAtomicAddF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset,
|
||||
Id value) {
|
||||
if (ctx.native_f32_add) { // (hookshot) the GPU's own float add: one atomic, never given up (DefineStorageBuffers)
|
||||
const Id pointer{StoragePointer(ctx, ctx.storage_types.F32, &StorageDefinitions::F32, binding, offset,
|
||||
sizeof(f32))};
|
||||
const auto [scope, semantics]{AtomicArgs(ctx)};
|
||||
return ctx.OpAtomicFAddEXT(ctx.F32[1], pointer, scope, semantics, value);
|
||||
}
|
||||
const Id ssbo{ctx.ssbos[binding.U32()].U32};
|
||||
const Id base_index{StorageIndex(ctx, offset, sizeof(u32))};
|
||||
// Reducing operands first can overflow even when adding each to the stored value would not.
|
||||
return ctx.OpFunctionCall(ctx.F32[1], ctx.f32_add_cas, base_index, value, ssbo);
|
||||
}
|
||||
|
||||
|
||||
@@ -105,6 +105,11 @@ Id EmitFPMul16(EmitContext& ctx, IR::Inst* inst, Id a, Id b) {
|
||||
}
|
||||
|
||||
Id EmitFPMul32(EmitContext& ctx, IR::Inst* inst, Id a, Id b) {
|
||||
// ((Triforce) mma_layout.h mesh_fallback_strict_fmul: a * b as fma(a, b, -0.0), the same value to the last bit, which
|
||||
// AMD's older Windows compiler does not take apart)
|
||||
if (ctx.mesh_fallback && mesh_fallback_strict_fmul && inst->Flags<IR::FpControl>().no_contraction) {
|
||||
return Decorate(ctx, inst, ctx.OpFma(ctx.F32[1], a, b, ctx.Const(-0.0f)));
|
||||
}
|
||||
return Decorate(ctx, inst, ctx.OpFMul(ctx.F32[1], a, b));
|
||||
}
|
||||
|
||||
|
||||
@@ -264,7 +264,7 @@ Id TextureImage(EmitContext& ctx, IR::TextureInstInfo info, const IR::Value& ind
|
||||
const Id idx{index.IsImmediate() ? ctx.Const(index.U32()) : ctx.Def(index)};
|
||||
const bool non_uniform{
|
||||
MarkNonUniform(ctx, idx, index, NonUniformKind::UniformTexelBuffer)};
|
||||
const Id ptr{ctx.OpAccessChain(ctx.image_buffer_type, def.id, idx)};
|
||||
const Id ptr{ctx.OpAccessChain(def.pointer_type, def.id, idx)};
|
||||
const Id object{ctx.OpLoad(ctx.image_buffer_type, ptr)};
|
||||
if (non_uniform) {
|
||||
DecorateNonUniform(ctx, ptr);
|
||||
|
||||
@@ -1,22 +1,52 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <tuple>
|
||||
|
||||
#include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
|
||||
#include "shader_recompiler/backend/spirv/spirv_emit_context.h"
|
||||
#include "shader_recompiler/frontend/ir/modifiers.h"
|
||||
|
||||
namespace Shader::Backend::SPIRV {
|
||||
namespace {
|
||||
Id Image(EmitContext& ctx, IR::TextureInstInfo info) {
|
||||
if (info.type == TextureType::Buffer) {
|
||||
const ImageBufferDefinition def{ctx.image_buffers.at(info.descriptor_index)};
|
||||
return def.id;
|
||||
} else {
|
||||
const ImageDefinition def{ctx.images.at(info.descriptor_index)};
|
||||
return def.id;
|
||||
void DecorateNonUniform(EmitContext& ctx, Id object) { // (as emit_spirv_image.cpp's)
|
||||
if (ctx.non_uniform_ids.insert(object.value).second) {
|
||||
ctx.Decorate(object, spv::Decoration::NonUniform);
|
||||
}
|
||||
}
|
||||
|
||||
// (Triforce) The image an atomic works on: the image itself, or for an array (count above 1, DefineImages and
|
||||
// DefineImageBuffers) the element the texture pass's index picks, as Image() in emit_spirv_image.cpp takes it for loads and
|
||||
// stores; nonuniform (true) where the profile indexes that kind of array so. A single image still takes no index
|
||||
Id Image(EmitContext& ctx, IR::TextureInstInfo info, const IR::Value& index, bool& non_uniform) {
|
||||
const bool buffer{info.type == TextureType::Buffer};
|
||||
const auto [id, pointer_type, count]{[&] {
|
||||
if (buffer) {
|
||||
const ImageBufferDefinition& def{ctx.image_buffers.at(info.descriptor_index)};
|
||||
return std::tuple{def.id, def.pointer_type, def.count};
|
||||
}
|
||||
const ImageDefinition& def{ctx.images.at(info.descriptor_index)};
|
||||
return std::tuple{def.id, def.pointer_type, def.count};
|
||||
}()};
|
||||
non_uniform = false;
|
||||
if (count <= 1) {
|
||||
if (!index.IsImmediate() || index.U32() != 0) {
|
||||
throw NotImplementedException("Image indexing");
|
||||
}
|
||||
return id;
|
||||
}
|
||||
const Id idx{index.IsImmediate() ? ctx.Const(index.U32()) : ctx.Def(index)};
|
||||
const Id element{ctx.OpAccessChain(pointer_type, id, idx)};
|
||||
non_uniform = !index.IsImmediate() && (buffer ? ctx.profile.support_storage_texel_buffer_array_nonuniform_indexing
|
||||
: ctx.profile.support_storage_image_array_nonuniform_indexing);
|
||||
if (non_uniform) {
|
||||
DecorateNonUniform(ctx, idx);
|
||||
DecorateNonUniform(ctx, element);
|
||||
(buffer ? ctx.uses_nonuniform_storage_texel_buffer : ctx.uses_nonuniform_storage_image) = true;
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
std::pair<Id, Id> AtomicArgs(EmitContext& ctx) {
|
||||
const Id scope{ctx.Const(static_cast<u32>(spv::Scope::Device))};
|
||||
const Id semantics{ctx.u32_zero_value};
|
||||
@@ -25,13 +55,13 @@ std::pair<Id, Id> AtomicArgs(EmitContext& ctx) {
|
||||
|
||||
Id ImageAtomicU32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id value,
|
||||
Id (Sirit::Module::*atomic_func)(Id, Id, Id, Id, Id)) {
|
||||
if (!index.IsImmediate() || index.U32() != 0) {
|
||||
// TODO: handle layers
|
||||
throw NotImplementedException("Image indexing");
|
||||
}
|
||||
const auto info{inst->Flags<IR::TextureInstInfo>()};
|
||||
const Id image{Image(ctx, info)};
|
||||
bool non_uniform{};
|
||||
const Id image{Image(ctx, info, index, non_uniform)};
|
||||
const Id pointer{ctx.OpImageTexelPointer(ctx.image_u32, image, coords, ctx.Const(0U))};
|
||||
if (non_uniform) {
|
||||
DecorateNonUniform(ctx, pointer);
|
||||
}
|
||||
const auto [scope, semantics]{AtomicArgs(ctx)};
|
||||
return (ctx.*atomic_func)(ctx.U32[1], pointer, scope, semantics, value);
|
||||
}
|
||||
|
||||
@@ -13,8 +13,10 @@
|
||||
namespace Shader::IR {
|
||||
enum class Attribute : u64;
|
||||
enum class Patch : u64;
|
||||
class Block;
|
||||
class Inst;
|
||||
class Value;
|
||||
struct Program;
|
||||
} // namespace Shader::IR
|
||||
|
||||
namespace Shader::Backend::SPIRV {
|
||||
@@ -63,6 +65,7 @@ void EmitSetAttributeIndexed(EmitContext& ctx, Id offset, Id value, Id vertex);
|
||||
Id EmitGetPatch(EmitContext& ctx, IR::Patch patch);
|
||||
void EmitSetPatch(EmitContext& ctx, IR::Patch patch, Id value);
|
||||
Id EmitIsbeRead(EmitContext& ctx, Id address);
|
||||
Id EmitIsbeReadOutput(EmitContext& ctx, Id address);
|
||||
void EmitIsbeWriteAttribute(EmitContext& ctx, Id address, Id value);
|
||||
void EmitIsbeWriteMapU8(EmitContext& ctx, Id address, Id value);
|
||||
void EmitIsbeWriteMapU32(EmitContext& ctx, const IR::Value& address, Id value);
|
||||
@@ -113,6 +116,7 @@ void EmitWriteGlobalS16(EmitContext& ctx, Id address, Id value);
|
||||
void EmitWriteGlobal32(EmitContext& ctx, Id address, Id value);
|
||||
void EmitWriteGlobal64(EmitContext& ctx, Id address, Id value);
|
||||
void EmitWriteGlobal128(EmitContext& ctx, Id address, Id value);
|
||||
Id EmitStorageBufferLength(EmitContext& ctx, const IR::Value& binding);
|
||||
Id EmitLoadStorageU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset);
|
||||
Id EmitLoadStorageS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset);
|
||||
Id EmitLoadStorageU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset);
|
||||
@@ -616,6 +620,11 @@ Id EmitImageAtomicXor32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index
|
||||
Id value);
|
||||
Id EmitImageAtomicExchange32(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
|
||||
Id value);
|
||||
// (Triforce, mma_layout.h mesh_fallback_shared_warp: emit_spirv_warp.cpp) the shared warp's start in the entry block, and the
|
||||
// region an if with a warp operation inside opens for its body
|
||||
void WarpPrologue(EmitContext& ctx);
|
||||
void WarpOpenRegion(EmitContext& ctx, const IR::Block* body, Id cond);
|
||||
void WarpCloseRegion(EmitContext& ctx);
|
||||
Id EmitLaneId(EmitContext& ctx);
|
||||
Id EmitVoteAll(EmitContext& ctx, Id pred);
|
||||
Id EmitVoteAny(EmitContext& ctx, Id pred);
|
||||
@@ -637,6 +646,9 @@ Id EmitShuffleButterfly(EmitContext& ctx, IR::Inst* inst, Id value, Id index, Id
|
||||
Id EmitQuadBroadcast(EmitContext& ctx, Id value, Id lane);
|
||||
Id EmitMatrixMulAddF16(EmitContext& ctx, Id a, Id b, Id c);
|
||||
void DefineSoftwareMatrixFunction(EmitContext& ctx);
|
||||
bool DefineWmma16MatrixFunction(EmitContext& ctx, const IR::Program& program);
|
||||
void DefineWmma16PairFunction(EmitContext& ctx, const IR::Program& program);
|
||||
void EmitMatrixMulAddF16Fused(EmitContext& ctx, IR::Inst* inst);
|
||||
Id EmitQuadSwap(EmitContext& ctx, Id value, Id direction);
|
||||
Id EmitFSwizzleAdd(EmitContext& ctx, Id op_a, Id op_b, Id swizzle);
|
||||
Id EmitDPdxFine(EmitContext& ctx, Id op_a);
|
||||
|
||||
@@ -78,6 +78,16 @@ void WriteStorageByCasLoop(EmitContext& ctx, const IR::Value& binding, const IR:
|
||||
}
|
||||
} // Anonymous namespace
|
||||
|
||||
// ((Triforce) simulated turf, ir_opt/sim_turf_pool_pass.h) The descriptor's range in bytes. The simulated-turf lists take every
|
||||
// cap from it: the producers' guards and the readers' bounds follow what is bound, a list of the renderer's own of any size or
|
||||
// the game's own list (whose range is its header and its cap's records: the game's cap, exactly)
|
||||
Id EmitStorageBufferLength(EmitContext& ctx, const IR::Value& binding) {
|
||||
if (!binding.IsImmediate()) throw NotImplementedException("Dynamic storage buffer length");
|
||||
const Id ssbo{ctx.ssbos[binding.U32()].U32};
|
||||
const Id words{ctx.OpArrayLength(ctx.U32[1], ssbo, 0U)};
|
||||
return ctx.OpShiftLeftLogical(ctx.U32[1], words, ctx.Const(2u));
|
||||
}
|
||||
|
||||
// (hookshot) 8- and 16-bit loads through a pointer no storage buffer was found for: the aligned 32-bit word by the same
|
||||
// search as LoadGlobal32, and the byte or half of it the address names (the console's loads are naturally aligned). They
|
||||
// threw "not implemented": 30 of OoT's mesh programs (LoadGlobalS16) were left untranslated on every card, issue 351
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
// epilogue (reached by every invocation, see structured_control_flow.cpp) hands them to Vulkan.
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <span>
|
||||
|
||||
@@ -20,6 +21,14 @@
|
||||
namespace Shader::Backend::SPIRV {
|
||||
namespace {
|
||||
void Barrier(EmitContext& ctx) {
|
||||
if (ctx.mesh_attributes_global) { // (Triforce: the attribute region in device memory, its writes seen across the workgroup too)
|
||||
ctx.OpControlBarrier(ctx.Const(static_cast<u32>(spv::Scope::Workgroup)),
|
||||
ctx.Const(static_cast<u32>(spv::Scope::Workgroup)),
|
||||
ctx.Const(static_cast<u32>(spv::MemorySemanticsMask::AcquireRelease |
|
||||
spv::MemorySemanticsMask::WorkgroupMemory |
|
||||
spv::MemorySemanticsMask::UniformMemory)));
|
||||
return;
|
||||
}
|
||||
ctx.OpControlBarrier(ctx.Const(static_cast<u32>(spv::Scope::Workgroup)),
|
||||
ctx.Const(static_cast<u32>(spv::Scope::Workgroup)),
|
||||
ctx.Const(static_cast<u32>(spv::MemorySemanticsMask::AcquireRelease |
|
||||
@@ -166,6 +175,30 @@ void StoreWord(EmitContext& ctx, Id pointer, Id value) {
|
||||
ctx.OpStore(pointer, value, spv::MemoryAccessMask::Aligned, 4U);
|
||||
}
|
||||
|
||||
/// (Triforce, mma_layout.h mesh_fallback_global_attributes) word `index` of the attribute region: in the workgroup's pool slot, or in
|
||||
/// workgroup memory as before
|
||||
Id LoadAttribute(EmitContext& ctx, Id index) {
|
||||
if (ctx.mesh_attributes_global) {
|
||||
return LoadWord(ctx, BufferWordAt(ctx, ctx.mesh_attribute_base, index));
|
||||
}
|
||||
return ctx.OpLoad(ctx.U32[1], SharedWord(ctx, ctx.mesh_attributes, index));
|
||||
}
|
||||
|
||||
void StoreAttribute(EmitContext& ctx, Id index, Id value) {
|
||||
if (ctx.mesh_attributes_global) {
|
||||
StoreWord(ctx, BufferWordAt(ctx, ctx.mesh_attribute_base, index), value);
|
||||
return;
|
||||
}
|
||||
ctx.OpStore(SharedWord(ctx, ctx.mesh_attributes, index), value);
|
||||
}
|
||||
|
||||
/// (the same) the pool's word `word`: 0 the ticket counter, 1 + slot the generation that may take that slot next
|
||||
Id PoolWord(EmitContext& ctx, Id word) {
|
||||
const Id pool{ctx.OpIAdd(ctx.U64, PushU64(ctx, ctx.mesh_fallback_records_member_index),
|
||||
ctx.Constant(ctx.U64, u64{MESH_FALLBACK_POOL_OFFSET}))};
|
||||
return BufferWordAt(ctx, pool, word);
|
||||
}
|
||||
|
||||
Id WorkgroupId(EmitContext& ctx) {
|
||||
return ctx.OpCompositeExtract(ctx.U32[1], ctx.OpLoad(ctx.U32[3], ctx.workgroup_id), 0U);
|
||||
}
|
||||
@@ -186,25 +219,90 @@ void Strided(EmitContext& ctx, u32 count, u32 threads, Id invocation, Func&& bod
|
||||
}
|
||||
}
|
||||
|
||||
/// The raw stage buffer word of line `line` for element `element` (a vertex or a primitive).
|
||||
Id LineWord(EmitContext& ctx, Id array, Id element, u32 base, u32 num_lines, u32 line) {
|
||||
/// The attribute region's index of line `line` (a value) for element `element` (a vertex or a primitive): base + (element / 32)
|
||||
/// * num_lines * 32 + element % 32 + line * 32.
|
||||
Id LineIndex(EmitContext& ctx, Id element, u32 base, u32 num_lines, Id line) {
|
||||
const Id group{ctx.OpShiftRightLogical(ctx.U32[1], element, ctx.Const(5U))};
|
||||
const Id slot{ctx.OpBitwiseAnd(ctx.U32[1], element, ctx.Const(31U))};
|
||||
const Id first{ctx.OpIAdd(
|
||||
ctx.U32[1], ctx.OpIMul(ctx.U32[1], group, ctx.Const(num_lines * 32)),
|
||||
ctx.OpIAdd(ctx.U32[1], slot, ctx.Const(base)))};
|
||||
return ctx.OpLoad(
|
||||
ctx.U32[1],
|
||||
SharedWord(ctx, array, ctx.OpIAdd(ctx.U32[1], first, ctx.Const(line * 32))));
|
||||
return ctx.OpIAdd(ctx.U32[1], first, ctx.OpShiftLeftLogical(ctx.U32[1], line, ctx.Const(5U)));
|
||||
}
|
||||
|
||||
/// (Triforce) A loop that is not unrolled: body(index) for index = first, first + step, ... below end. var is a function variable
|
||||
/// of the entry block (EmitMeshPrologue): one declared at the call, mid-function, is invalid SPIR-V
|
||||
template <typename Func>
|
||||
void Loop(EmitContext& ctx, Id var, Id first, Id end, u32 step, Func&& body) {
|
||||
ctx.OpStore(var, first);
|
||||
const Id header{ctx.OpLabel()}, block{ctx.OpLabel()}, next{ctx.OpLabel()}, done{ctx.OpLabel()};
|
||||
ctx.OpBranch(header);
|
||||
ctx.AddLabel(header);
|
||||
const Id at{ctx.OpLoad(ctx.U32[1], var)};
|
||||
const Id more{ctx.OpULessThan(ctx.U1, at, end)}; // (before the merge: it must be the branch's neighbour)
|
||||
ctx.OpLoopMerge(done, next, spv::LoopControlMask::DontUnroll);
|
||||
ctx.OpBranchConditional(more, block, done);
|
||||
ctx.AddLabel(block);
|
||||
body(at);
|
||||
ctx.OpBranch(next);
|
||||
ctx.AddLabel(next);
|
||||
ctx.OpStore(var, ctx.OpIAdd(ctx.U32[1], at, ctx.Const((std::max)(step, 1U))));
|
||||
ctx.OpBranch(header);
|
||||
ctx.AddLabel(done);
|
||||
}
|
||||
|
||||
/// The Soa layout's records (MeshFallbackLayout): record k of the workgroup's, base + k, is corner k % corners of primitive
|
||||
/// k / corners, its index byte at 4 + k. The threads take the records in turn, and each line's words are neighbours: line l of
|
||||
/// record r at H + l * fit + r.
|
||||
/// (Triforce) Two loops, not unrolled: thread t takes records t, t + threads, ... below taken, and writes each one's lines in
|
||||
/// turn, so at each step the threads write one line's neighbouring words, as the unrolled form did. Unrolled, a thread had a
|
||||
/// store of its own for every line of every record it could take (348 for 126 primitives of 3 corners and 28 lines), which
|
||||
/// Mesa's Intel compiler took minutes and gigabytes over, as it did the Aos form's (MeshFallbackEpilogue)
|
||||
void WriteFallbackLines(EmitContext& ctx, Id base, Id taken) {
|
||||
const MeshInfo& mesh{ctx.mesh_info};
|
||||
const u32 corners{mesh.Corners()};
|
||||
const u32 words{FallbackRecordWords(mesh)};
|
||||
const u32 fit{MeshFallbackFit(words)};
|
||||
const u32 vertex_lines{static_cast<u32>(mesh.vertex_lines.size())};
|
||||
const u32 primitive_lines{static_cast<u32>(mesh.primitive_lines.size())};
|
||||
const Id records{PushU64(ctx, ctx.mesh_fallback_records_member_index)};
|
||||
const Id first{ctx.OpIAdd(ctx.U32[1], ctx.Const(MESH_FALLBACK_HEADER_WORDS), base)}; // (line 0 of record base)
|
||||
Loop(ctx, ctx.mesh_fallback_epilogue_word, LocalInvocation(ctx), taken, ctx.mesh_threads, [&](Id k) {
|
||||
// (an index byte the program left unwritten is held to the vertex count)
|
||||
const Id vertex{Clamped(ctx, ctx.OpLoad(ctx.U32[1], SharedWord(ctx, ctx.mesh_map, ctx.OpIAdd(ctx.U32[1], k, ctx.Const(4U)))),
|
||||
mesh.max_vertices)};
|
||||
const Id primitive{corners == 1 ? k : ctx.OpUDiv(ctx.U32[1], k, ctx.Const(corners))};
|
||||
const Id at{ctx.OpIAdd(ctx.U32[1], first, k)};
|
||||
Loop(ctx, ctx.mesh_fallback_epilogue_line, ctx.u32_zero_value, ctx.Const(words), 1, [&](Id word) {
|
||||
// (word 0 the validity, 1 .. vertex_lines its vertex's lines, then its primitive's: one load, its index clamped into
|
||||
// the region, as word 0 and the lines of the other kind ask for one past it)
|
||||
const Id line{ctx.OpISub(ctx.U32[1], word, ctx.Const(1U))};
|
||||
const Id of_vertex{ctx.OpULessThan(ctx.U1, line, ctx.Const(vertex_lines))};
|
||||
const Id vertex_index{LineIndex(ctx, vertex, 0, vertex_lines, line)};
|
||||
const Id primitive_index{LineIndex(ctx, primitive, ctx.mesh_primitive_base, primitive_lines,
|
||||
ctx.OpISub(ctx.U32[1], line, ctx.Const(vertex_lines)))};
|
||||
const Id index{Clamped(ctx, ctx.OpSelect(ctx.U32[1], of_vertex, vertex_index, primitive_index),
|
||||
ctx.mesh_attribute_words)};
|
||||
const Id line_value{LoadAttribute(ctx, index)};
|
||||
const Id value{ctx.OpSelect(ctx.U32[1], ctx.OpIEqual(ctx.U1, word, ctx.u32_zero_value), ctx.Const(1U), line_value)};
|
||||
StoreWord(ctx, BufferWordAt(ctx, records, ctx.OpIAdd(ctx.U32[1], at, ctx.OpIMul(ctx.U32[1], word, ctx.Const(fit)))),
|
||||
value);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// A mesh workgroup takes records for its primitives' corners from the records buffer's allocator and writes them; the vertex
|
||||
/// stage's indirect draw covers the records taken (MESH_FALLBACK_DRAW_WORD). A task's flag first bounds its count.
|
||||
/// (Triforce) The records are written by one loop over their words, the workgroup's threads side by side: thread t writes
|
||||
/// words t, t + threads, ... of the workgroup's records, each word a load from the attribute region and a store. They were
|
||||
/// written out unrolled, a primitive's corners and every line of each, its own address each: 348 stores a thread for a mesh
|
||||
/// of 126 primitives and 28 lines, which Mesa's Intel compiler (Iris Xe, 32 lanes) took 207 s and 3.8 GB to build (41 s and
|
||||
/// 1.5 GB without them). Side by side, the stores of a step are one run of memory, too.
|
||||
void MeshFallbackEpilogue(EmitContext& ctx) {
|
||||
const MeshInfo& mesh{ctx.mesh_info};
|
||||
const u32 corners{mesh.Corners()};
|
||||
const u32 words{FallbackRecordWords(mesh)};
|
||||
const u32 fit{MESH_FALLBACK_RECORD_WORDS / words}; // (this program's records the buffer holds)
|
||||
const u32 fit{MeshFallbackFit(words)}; // (this program's records the buffer holds)
|
||||
const Id none{ctx.Const(0xFFFFFFFFU)};
|
||||
Barrier(ctx);
|
||||
// (with a task program every workgroup dispatched was started by it: the dispatch is the map's, TaskEpilogue)
|
||||
@@ -237,46 +335,67 @@ void MeshFallbackEpilogue(EmitContext& ctx) {
|
||||
Barrier(ctx);
|
||||
const Id base{ctx.OpLoad(ctx.U32[1], ctx.mesh_primitive_count)};
|
||||
If(ctx, ctx.OpINotEqual(ctx.U1, base, none), [&] {
|
||||
const Id invocation{LocalInvocation(ctx)};
|
||||
Strided(ctx, mesh.max_primitives, ctx.mesh_threads, invocation, [&](Id primitive) {
|
||||
If(ctx, ctx.OpULessThan(ctx.U1, primitive, count), [&] {
|
||||
for (u32 corner = 0; corner < corners; ++corner) {
|
||||
const Id byte{ctx.OpIAdd(
|
||||
ctx.U32[1], ctx.OpIMul(ctx.U32[1], primitive, ctx.Const(corners)),
|
||||
ctx.Const(4U + corner))};
|
||||
// (an index byte the program left unwritten is held to the vertex count)
|
||||
const Id vertex{Clamped(
|
||||
ctx, ctx.OpLoad(ctx.U32[1], SharedWord(ctx, ctx.mesh_map, byte)),
|
||||
mesh.max_vertices)};
|
||||
const Id record{ctx.OpIAdd(
|
||||
ctx.U32[1], base,
|
||||
ctx.OpIAdd(ctx.U32[1], ctx.OpIMul(ctx.U32[1], primitive, ctx.Const(corners)),
|
||||
ctx.Const(corner)))};
|
||||
const Id word{ctx.OpIAdd(ctx.U32[1], ctx.Const(MESH_FALLBACK_HEADER_WORDS),
|
||||
ctx.OpIMul(ctx.U32[1], record, ctx.Const(words)))};
|
||||
StoreWord(ctx, BufferWord(ctx, ctx.mesh_fallback_records_member_index, word),
|
||||
ctx.Const(1U));
|
||||
for (u32 line = 0; line < mesh.vertex_lines.size(); ++line) {
|
||||
const Id value{LineWord(ctx, ctx.mesh_attributes, vertex, 0,
|
||||
static_cast<u32>(mesh.vertex_lines.size()), line)};
|
||||
StoreWord(ctx, BufferWord(ctx, ctx.mesh_fallback_records_member_index,
|
||||
ctx.OpIAdd(ctx.U32[1], word, ctx.Const(1U + line))),
|
||||
value);
|
||||
}
|
||||
for (u32 line = 0; line < mesh.primitive_lines.size(); ++line) {
|
||||
const Id value{LineWord(ctx, ctx.mesh_attributes, primitive,
|
||||
ctx.mesh_primitive_base,
|
||||
static_cast<u32>(mesh.primitive_lines.size()), line)};
|
||||
StoreWord(ctx, BufferWord(ctx, ctx.mesh_fallback_records_member_index,
|
||||
ctx.OpIAdd(ctx.U32[1], word,
|
||||
ctx.Const(static_cast<u32>(1U + mesh.vertex_lines.size()) +
|
||||
line))),
|
||||
value);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
if (ctx.runtime_info.mesh_fallback_layout == static_cast<u32>(MeshFallbackLayout::Soa)) {
|
||||
WriteFallbackLines(ctx, base, taken);
|
||||
return;
|
||||
}
|
||||
const u32 vertex_lines{static_cast<u32>(mesh.vertex_lines.size())};
|
||||
const u32 primitive_lines{static_cast<u32>(mesh.primitive_lines.size())};
|
||||
// (the workgroup's records are words first .. first + taken * words: its first record's, then each record's validity,
|
||||
// its vertex's lines and its primitive's lines)
|
||||
const Id records{PushU64(ctx, ctx.mesh_fallback_records_member_index)};
|
||||
const Id first{ctx.OpIAdd(ctx.U32[1], ctx.Const(MESH_FALLBACK_HEADER_WORDS),
|
||||
ctx.OpIMul(ctx.U32[1], base, ctx.Const(words)))};
|
||||
const Id total{ctx.OpIMul(ctx.U32[1], taken, ctx.Const(words))};
|
||||
const Id at_var{ctx.mesh_fallback_epilogue_word}; // (declared in the entry block: EmitMeshPrologue)
|
||||
ctx.OpStore(at_var, LocalInvocation(ctx));
|
||||
const Id header{ctx.OpLabel()}, body{ctx.OpLabel()}, next{ctx.OpLabel()}, done{ctx.OpLabel()};
|
||||
ctx.OpBranch(header);
|
||||
ctx.AddLabel(header);
|
||||
const Id at{ctx.OpLoad(ctx.U32[1], at_var)};
|
||||
const Id more{ctx.OpULessThan(ctx.U1, at, total)}; // (before the merge: it must be the branch's neighbour)
|
||||
ctx.OpLoopMerge(done, next, spv::LoopControlMask::DontUnroll);
|
||||
ctx.OpBranchConditional(more, body, done);
|
||||
ctx.AddLabel(body);
|
||||
// (record r is corner r % corners of primitive r / corners: its index byte is the map's byte 4 + r)
|
||||
const Id record{ctx.OpUDiv(ctx.U32[1], at, ctx.Const(words))};
|
||||
const Id word{ctx.OpISub(ctx.U32[1], at, ctx.OpIMul(ctx.U32[1], record, ctx.Const(words)))};
|
||||
const Id primitive{ctx.OpUDiv(ctx.U32[1], record, ctx.Const(corners))};
|
||||
// (an index byte the program left unwritten is held to the vertex count)
|
||||
const Id vertex{Clamped(
|
||||
ctx, ctx.OpLoad(ctx.U32[1], SharedWord(ctx, ctx.mesh_map, ctx.OpIAdd(ctx.U32[1], record, ctx.Const(4U)))),
|
||||
mesh.max_vertices)};
|
||||
// (word 0 the validity, 1 .. vertex_lines its vertex's lines, then its primitive's: one load, its index clamped into the
|
||||
// region, as the word 0 and the lines of the other kind ask for one past it)
|
||||
const Id line{ctx.OpISub(ctx.U32[1], word, ctx.Const(1U))};
|
||||
const Id of_vertex{ctx.OpULessThan(ctx.U1, line, ctx.Const(vertex_lines))};
|
||||
const Id vertex_index{LineIndex(ctx, vertex, 0, vertex_lines, line)};
|
||||
const Id primitive_index{LineIndex(ctx, primitive, ctx.mesh_primitive_base, primitive_lines,
|
||||
ctx.OpISub(ctx.U32[1], line, ctx.Const(vertex_lines)))};
|
||||
const Id index{Clamped(ctx, ctx.OpSelect(ctx.U32[1], of_vertex, vertex_index, primitive_index),
|
||||
ctx.mesh_attribute_words)};
|
||||
const Id line_value{LoadAttribute(ctx, index)};
|
||||
const Id value{ctx.OpSelect(ctx.U32[1], ctx.OpIEqual(ctx.U1, word, ctx.u32_zero_value), ctx.Const(1U), line_value)};
|
||||
StoreWord(ctx, BufferWordAt(ctx, records, ctx.OpIAdd(ctx.U32[1], first, at)), value);
|
||||
ctx.OpBranch(next);
|
||||
ctx.AddLabel(next);
|
||||
ctx.OpStore(at_var, ctx.OpIAdd(ctx.U32[1], at, ctx.Const((std::max)(ctx.mesh_threads, 1U))));
|
||||
ctx.OpBranch(header);
|
||||
ctx.AddLabel(done);
|
||||
});
|
||||
if (ctx.mesh_attributes_global) {
|
||||
// (every thread has read what it copies from the region: thread 0 hands the slot on to the ticket a pool's length later)
|
||||
Barrier(ctx);
|
||||
If(ctx, ctx.OpIEqual(ctx.U1, LocalInvocation(ctx), ctx.u32_zero_value), [&] {
|
||||
const Id ticket{ctx.mesh_attribute_ticket};
|
||||
const Id slot{ctx.OpBitwiseAnd(ctx.U32[1], ticket, ctx.Const(MESH_FALLBACK_POOL_SLOTS - 1))};
|
||||
const Id next{ctx.OpIAdd(ctx.U32[1], ctx.OpShiftRightLogical(ctx.U32[1], ticket, ctx.Const(MESH_FALLBACK_POOL_SLOT_SHIFT)),
|
||||
ctx.Const(1U))};
|
||||
// (an exchange: Sirit's OpAtomicStore is emitted a word too long, and spirv-val and the driver reject it)
|
||||
ctx.OpAtomicExchange(ctx.U32[1], PoolWord(ctx, ctx.OpIAdd(ctx.U32[1], slot, ctx.Const(1U))),
|
||||
ctx.Const(static_cast<u32>(spv::Scope::Device)), ctx.u32_zero_value, next);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void StorePosition(EmitContext& ctx, u32 element, Id value) {
|
||||
@@ -366,8 +485,15 @@ void MeshEpilogue(EmitContext& ctx) {
|
||||
return;
|
||||
}
|
||||
Barrier(ctx);
|
||||
const Id count{ctx.OpUMin(ctx.U32[1], ctx.OpLoad(ctx.U32[1], ctx.mesh_primitive_count),
|
||||
ctx.Const(mesh.max_primitives))};
|
||||
Id count{ctx.OpUMin(ctx.U32[1], ctx.OpLoad(ctx.U32[1], ctx.mesh_primitive_count),
|
||||
ctx.Const(mesh.max_primitives))};
|
||||
if (Sirit::ValidId(ctx.num_workgroups)) {
|
||||
// (Triforce, , as Lichii ) keep the NumWorkgroups load live through NIR's dead-code elimination into
|
||||
// ac_nir_lower_ngg_ms so RADV < 26.2.5 reserves the mesh grid-size registers (NumWorkgroups.x > 0 for every
|
||||
// launched mesh workgroup)
|
||||
const Id grid_x{ctx.OpCompositeExtract(ctx.U32[1], ctx.OpLoad(ctx.U32[3], ctx.num_workgroups), 0U)};
|
||||
count = ctx.OpSelect(ctx.U32[1], ctx.OpINotEqual(ctx.U1, grid_x, ctx.u32_zero_value), count, ctx.u32_zero_value);
|
||||
}
|
||||
const Id max_vertices{ctx.Const(mesh.max_vertices)};
|
||||
// Marker for OpSetMeshOutputsEXT max_vertices, count (PatchMeshInstructions).
|
||||
ctx.OpStore(max_vertices, count);
|
||||
@@ -474,16 +600,19 @@ void EmitMeshFallbackVertexBody(EmitContext& ctx) {
|
||||
const u32 primitive_lines{static_cast<u32>(mesh.primitive_lines.size())};
|
||||
const u32 words{FallbackRecordWords(mesh)};
|
||||
// (a record a vertex, in the order the mesh workgroups took them (MeshFallbackEpilogue): the indirect draw's count is the
|
||||
// records taken, so every one it reaches was written)
|
||||
// records taken, so every one it reaches was written. Its words a record apart (Aos) or a line apart (Soa))
|
||||
const bool lines{ctx.runtime_info.mesh_fallback_layout == static_cast<u32>(MeshFallbackLayout::Soa)};
|
||||
const u32 stride{lines ? MeshFallbackFit(words) : 1U};
|
||||
const Id vertex{ctx.OpLoad(ctx.U32[1], ctx.vertex_index)};
|
||||
const Id first{ctx.OpIAdd(ctx.U32[1], ctx.Const(MESH_FALLBACK_HEADER_WORDS),
|
||||
ctx.OpIMul(ctx.U32[1], vertex, ctx.Const(words)))};
|
||||
const Id first{lines ? ctx.OpIAdd(ctx.U32[1], ctx.Const(MESH_FALLBACK_HEADER_WORDS), vertex)
|
||||
: ctx.OpIAdd(ctx.U32[1], ctx.Const(MESH_FALLBACK_HEADER_WORDS),
|
||||
ctx.OpIMul(ctx.U32[1], vertex, ctx.Const(words)))};
|
||||
const Id valid{LoadWord(ctx, BufferWord(ctx, ctx.mesh_fallback_records_member_index, first))};
|
||||
const auto read = [&](u32 word) {
|
||||
return ctx.OpBitcast(
|
||||
ctx.F32[1],
|
||||
LoadWord(ctx, BufferWord(ctx, ctx.mesh_fallback_records_member_index,
|
||||
ctx.OpIAdd(ctx.U32[1], first, ctx.Const(word)))));
|
||||
ctx.OpIAdd(ctx.U32[1], first, ctx.Const(word * stride)))));
|
||||
};
|
||||
std::array<Id, 4> position{};
|
||||
for (u32 line = 0; line < vertex_lines; ++line) {
|
||||
@@ -563,14 +692,23 @@ Id EmitIsbeRead(EmitContext& ctx, Id address) {
|
||||
word);
|
||||
}
|
||||
|
||||
/// (hookshot) ISBERD.O: the word EmitIsbeWriteAttribute writes at that address, read back (a task program reads its
|
||||
/// payload: one of OoT's task programs stopped at it)
|
||||
Id EmitIsbeReadOutput(EmitContext& ctx, Id address) {
|
||||
const Id index{ctx.OpShiftRightLogical(ctx.U32[1], address, ctx.Const(2U))};
|
||||
if (ctx.stage == Stage::Task) {
|
||||
return LoadWord(ctx, PayloadWord(ctx, index)); // (as EmitIsbeWriteAttribute's StoreWord wrote it)
|
||||
}
|
||||
return LoadAttribute(ctx, Clamped(ctx, index, ctx.mesh_attribute_words));
|
||||
}
|
||||
|
||||
void EmitIsbeWriteAttribute(EmitContext& ctx, Id address, Id value) {
|
||||
const Id index{ctx.OpShiftRightLogical(ctx.U32[1], address, ctx.Const(2U))};
|
||||
if (ctx.stage == Stage::Task) {
|
||||
StoreWord(ctx, PayloadWord(ctx, index), value);
|
||||
return;
|
||||
}
|
||||
ctx.OpStore(SharedWord(ctx, ctx.mesh_attributes, Clamped(ctx, index, ctx.mesh_attribute_words)),
|
||||
value);
|
||||
StoreAttribute(ctx, Clamped(ctx, index, ctx.mesh_attribute_words), value);
|
||||
}
|
||||
|
||||
void EmitIsbeWriteMapU8(EmitContext& ctx, Id address, Id value) {
|
||||
@@ -590,6 +728,16 @@ void EmitIsbeWriteMapU32(EmitContext& ctx, const IR::Value& address, Id value) {
|
||||
}
|
||||
|
||||
void EmitMeshPrologue(EmitContext& ctx) {
|
||||
if (ctx.stage == Stage::Mesh && ctx.mesh_fallback) {
|
||||
// (Triforce) the epilogue's loop index (MeshFallbackEpilogue): a function's variables open its first block, and the
|
||||
// prologue is the first thing in it. Before anything else here
|
||||
ctx.mesh_fallback_epilogue_word = ctx.AddLocalVariable(
|
||||
ctx.TypePointer(spv::StorageClass::Function, ctx.U32[1]), spv::StorageClass::Function);
|
||||
ctx.mesh_fallback_epilogue_line = ctx.AddLocalVariable( // (WriteFallbackLines' inner loop)
|
||||
ctx.TypePointer(spv::StorageClass::Function, ctx.U32[1]), spv::StorageClass::Function);
|
||||
}
|
||||
// (Triforce, mma_layout.h mesh_fallback_shared_warp) the shared warp's lane and its words at zero, before the barriers below
|
||||
WarpPrologue(ctx);
|
||||
if (ctx.stage == Stage::Task) {
|
||||
// (hookshot) the count starts at nothing: task payload memory holds whatever the last task left, and a task that keeps no meshlet
|
||||
// never writes it. The barrier: every thread has done this before any of them goes on to write the real count
|
||||
@@ -605,6 +753,20 @@ void EmitMeshPrologue(EmitContext& ctx) {
|
||||
ctx.mesh_fallback_slot =
|
||||
LoadWord(ctx, BufferWordAt(ctx, PushU64(ctx, ctx.mesh_fallback_payload_member_index), WorkgroupId(ctx)));
|
||||
}
|
||||
if (ctx.mesh_attributes_global) {
|
||||
// (Triforce, mma_layout.h mesh_fallback_global_attributes) the workgroup's pool slot, before anything writes the region:
|
||||
// thread 0 takes a ticket (a call: no branch here), every thread reads it after the barrier
|
||||
ctx.OpFunctionCall(ctx.void_id, ctx.mesh_attribute_claim, std::array{LocalInvocation(ctx)});
|
||||
Barrier(ctx);
|
||||
ctx.mesh_attribute_ticket = ctx.OpLoad(ctx.U32[1], ctx.mesh_attribute_ticket_shared);
|
||||
const Id slot{ctx.OpBitwiseAnd(ctx.U32[1], ctx.mesh_attribute_ticket, ctx.Const(MESH_FALLBACK_POOL_SLOTS - 1))};
|
||||
const Id offset{ctx.OpIMul(ctx.U32[1], slot, ctx.Const(ctx.mesh_attribute_slot_bytes))};
|
||||
ctx.mesh_attribute_base =
|
||||
ctx.OpIAdd(ctx.U64,
|
||||
ctx.OpIAdd(ctx.U64, PushU64(ctx, ctx.mesh_fallback_records_member_index),
|
||||
ctx.Constant(ctx.U64, u64{MESH_FALLBACK_POOL_OFFSET} + MESH_FALLBACK_POOL_HEADER_BYTES)),
|
||||
ctx.OpUConvert(ctx.U64, offset));
|
||||
}
|
||||
// (local) The stage buffer's map and attribute regions start at zero, as they come out on NVIDIA: workgroup memory holds
|
||||
// whatever the last workgroup on that unit left, and a vertex or an index byte the program left unwritten was that. On an
|
||||
// RX 7700S the foliage drawn into the 2400 x 2400 shadow map around Link came out with hundreds of long thin triangles
|
||||
@@ -625,6 +787,12 @@ void EmitMeshPrologue(EmitContext& ctx) {
|
||||
};
|
||||
zero_all(ctx.mesh_map, ctx.mesh_map_bytes);
|
||||
zero_all(ctx.mesh_attributes, ctx.mesh_attribute_words);
|
||||
if (ctx.mesh_attributes_global) { // (Triforce: the region in its pool slot, zeroed the same way)
|
||||
for (u32 first = 0; first < ctx.mesh_attribute_words; first += ctx.mesh_threads) {
|
||||
StoreAttribute(ctx, Clamped(ctx, ctx.OpIAdd(ctx.U32[1], invocation, ctx.Const(first)), ctx.mesh_attribute_words),
|
||||
ctx.u32_zero_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ctx.mesh_fallback && !ctx.runtime_info.mesh_fallback_task && ctx.mesh_threads) {
|
||||
// (a mesh program with no task reads its workgroup index from the payload: NVN's hardware puts it at byte 0)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,6 +16,28 @@ inline std::array<uint32_t, 8> mma_order_a{0, 1, 2, 3, 4, 5, 6, 7};
|
||||
inline std::array<uint32_t, 4> mma_order_b{0, 1, 2, 3};
|
||||
inline std::array<uint32_t, 4> mma_order_c{0, 1, 2, 3};
|
||||
inline bool mma_software = false; // (HMMA_nx2 in software: emit_spirv_warp.cpp, RX_VK_MMA_SOFTWARE=1)
|
||||
// (Triforce) and in which form (mma_software.h SoftwareMmaForm: 0 the loop, 1 unrolled, 2 unrolled with the GPU's f16 rounding),
|
||||
// chosen by vk_coopmat.cpp's self-test; and whether its quad broadcasts may be used (the device has quad operations in every stage)
|
||||
inline uint32_t mma_software_form = 0;
|
||||
inline bool mma_software_quad = false;
|
||||
// (Triforce, AMD RDNA 3/4) HMMA_nx2 on 16x16x16 f16 cooperative matrices: the console's fragments are staged through a
|
||||
// per-subgroup tile of shared memory at their PTX m16n8k16 positions, so the device's own lane layout does not matter
|
||||
// (emit_spirv_warp.cpp DefineWmma16MatrixFunction, vk_coopmat.cpp). B and C are padded to 16 columns with zeros.
|
||||
inline bool mma_wmma16 = false;
|
||||
// (with mma_wmma16) one 16x16x16 multiply instead of two K=8 halves rounded to f16 between them as HMMA.F16 does
|
||||
inline bool mma_wmma16_fast = false;
|
||||
// (with mma_wmma16) fuse N-pairs (two HMMA_nx2 sharing A into one 16x16 [B0|B1] DontInline call)
|
||||
inline bool mma_wmma16_fused = true;
|
||||
// shared memory the WMMA16 tiles add per subgroup, in u32 words: A (16x16 row major), B's two K halves (each 16x16 column
|
||||
// major, the other half's rows zero) and C/D (16x16 row major), 16x16 f16 = 128 words each
|
||||
inline constexpr uint32_t mma_wmma16_tile_words = 4 * 128;
|
||||
inline constexpr uint32_t mma_wmma16_tile_bytes = mma_wmma16_tile_words * 4;
|
||||
// the device's maxComputeSharedMemorySize (vk_coopmat.cpp): a program whose tiles would pass it multiplies in software
|
||||
inline uint32_t mma_wmma16_shared_limit = 0;
|
||||
// the device's computeFullSubgroups (vk_coopmat.cpp): with it a workgroup X width that is a multiple of 32 is created with
|
||||
// REQUIRE_FULL_SUBGROUPS (vk_draw.cpp pin_subgroup), so no subgroup has lanes missing; without it no program uses the tiles
|
||||
inline bool mma_wmma16_full_subgroups = false;
|
||||
inline uint32_t mma_wmma16_max_subgroups = 0;
|
||||
// (amd-fix) float colour outputs carry no NaN or infinity to their target (EmitSetFragColor): one NaN texel spreads
|
||||
// through bloom/blur/downsample averages into glowing blocks (OoT's flowers on AMD, Hookshot issue 606). On for every
|
||||
// card but NVIDIA (vk_coopmat.cpp coopmat_init); RX_VK_NO_NAN_GUARD=1 turns it off. Idea from NX2navi patch 0027.
|
||||
@@ -35,5 +57,63 @@ 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;
|
||||
// (Triforce) In compute-fallback mesh stages, NoContraction f32 multiplies as OpFma(a, b, -0.0f) (EmitFPMul32): AMD's older
|
||||
// Windows shader compiler (RDNA 1 and 2, RX 5000 and RX 6000: gpu/vk/vk_vendor.h amd_older_compiler) ignores NoContraction
|
||||
// on a multiply, even in a pipeline made without its optimizer, and turns fma(t, v * rsq, add) into fma(rsq * t, v, add) in
|
||||
// the depth-only mesh fallback programs, where v * rsq has one use, while the colour pass's keeps v * rsq. A fallback stage
|
||||
// writes its positions to a storage buffer, so gl_Position cannot be decorated Invariant there. The grass blades' tips then
|
||||
// differ by one unit in the last place between the two passes, fail the colour pass's EQUAL depth test, and single
|
||||
// triangles flash white for a frame. OpFma(a, b, -0.0f) is a * b exactly (signed zeros, subnormals, infinities and NaNs
|
||||
// too) and is not taken apart. On for that compiler (vk_coopmat.cpp); RX_VK_MESH_STRICT_FMUL=0 or 1
|
||||
inline bool mesh_fallback_strict_fmul = false;
|
||||
// (Triforce) A compute stage's float adds to storage buffers that take the compare-exchange loop (no native add on this GPU:
|
||||
// gpu/vk/native_fadd.h) are admitted to the loop one at a time a subgroup (spirv_emit_context.cpp SubgroupCasLoop,
|
||||
// OpGroupNonUniformElect). The sky's light sums (compute 499dbfd0721d2858) add into a few words from every invocation; each
|
||||
// failed attempt is another adder's success, so the attempts grow with the square of the adders, and on Intel's Arc cards
|
||||
// the dispatch outlasted the driver's timeout: the device lost after the intro. On for Intel's GPUs (vk_coopmat.cpp), where
|
||||
// that was seen; elsewhere the plain loop, as it was, until the admission has run on those drivers (a subgroup operation
|
||||
// inside a loop the invocations leave one by one). RX_VK_FADD_SUBGROUP=0 or 1 on any GPU
|
||||
inline bool f32_add_subgroup = false;
|
||||
// (hookshot) a storage buffer table's entries searched in a loop the driver is asked not to unroll (DefineGlobalMemoryFunctions),
|
||||
// not one test an entry written out sixteen times. Mesa inlines every function, DontInline or not: OoT's foliage culling
|
||||
// (53caac5f) has 46 global accesses, each with its ~70 table entries written out, over 100k instructions that Intel's
|
||||
// compiler did not finish in minutes, and every compute pipeline queued behind it on Intel's one build thread waited:
|
||||
// mesh-drawn rocks and doors white, the forest grey. In a loop: 12 s. On for Intel's Mesa driver (vk_coopmat.cpp); RX_VK_GM_LOOP=0 or 1
|
||||
inline bool global_table_loop = false;
|
||||
// (Triforce) The mesh fallback's task and mesh stages read global memory through the guest page table alone, the way the console's
|
||||
// GPU reads it, not by searching the program's storage buffers (DefineGlobalMemoryFunctions). The search is the cost there: on an
|
||||
// Iris Xe the fallback's colour mesh stages are held to 32 lanes at the register file's limit, and each of their global loads,
|
||||
// inlined with its four buffers and three table entries, filled the program with spill code: 739acd67 (Hyrule Castle's) took
|
||||
// 12.3 ms a frame, 5.7 ms with its loads through the table, its 243 million loads the same words both ways. A storage buffer in a
|
||||
// virtual pool (from 1 TB) is not in the table and reads 0. (Triforce) Off by default on every GPU: the table must give each bound
|
||||
// storage buffer the memory its binding reads, which was not checked against Triforce's memory code (vk_coopmat.cpp); RX_VK_MESH_PAGES=1
|
||||
inline bool mesh_fallback_pages = false;
|
||||
// (Triforce) Vertex, fragment, compute, task and mesh stages' global loads (native task and mesh stages and the mesh fallback's) through the guest page
|
||||
// table alone, as mesh_fallback_pages does. The search
|
||||
// over every bound storage buffer, inlined at each load, made a vertex stage with 61 loads 585 ms of RADV's compile (44 ms through
|
||||
// the table). 1: the table alone; 2: the search kept, a load where the two disagree recorded in the miss log's buffer
|
||||
// (vk_draw.cpp gp_table_check_log). (Triforce) Off by default: on where the table holds the pools (vk_coopmat.cpp) with
|
||||
// RX_VK_GP_TABLE=1 or 2. Every storage buffer bound to such a stage is then looked at in the table (vk_draw.cpp gp_table_cover)
|
||||
inline int graphics_table_loads = 0;
|
||||
// (Triforce) The mesh fallback's mesh workgroups keep their attribute region (the stage buffer's vertex and primitive lines, up to
|
||||
// 32 KB) in a slot of a pool in the records buffer instead of in workgroup memory (emit_spirv.h MESH_FALLBACK_POOL_*). Intel
|
||||
// gives workgroup memory in powers of two, so Hyrule Castle's colour mesh stages, 9 to 18 KB of it, took 16 or 32 KB each: few
|
||||
// workgroups fit on a subslice at once, and nothing hid the stages' spill traffic. On an Iris Xe 739acd67 took 5.3-6.9 ms a
|
||||
// dispatch with its region in workgroup memory, 7.4 ms given 8 KB more of it, and 2.6-2.8 ms with the region in a slot.
|
||||
// On for Intel's Mesa driver (vk_coopmat.cpp); RX_VK_MESH_GLOBAL_ATTRS=0 or 1
|
||||
inline bool mesh_fallback_global_attributes = false;
|
||||
// (Triforce) The mesh fallback's task and mesh stages keep their warp in workgroup memory, not in the host's subgroup: a lane is the
|
||||
// invocation's place in its 32-thread workgroup, and ballots, votes and shuffles go through words of workgroup memory that every
|
||||
// lane of the block's region writes and waits for (emit_spirv_warp.cpp, the shared warp). Such a stage has no subgroup
|
||||
// instruction left, so it gives the same result at any lane count, and the renderer runs it at 8 lanes (vk_draw.cpp): on an
|
||||
// Iris Xe the colour mesh stages, held to 32 lanes for their subgroup instructions, spent most of their time spilling
|
||||
// (d8e59341: 843k cycles a workgroup at 32 lanes by Mesa's count, 4 x 70k at 8). Only stages it can be done for: 32-thread
|
||||
// workgroups, no warp operation in a loop, no read of the stage buffer's outputs (EmitContext::PlanSharedWarp). On for Intel's
|
||||
// Mesa driver (vk_coopmat.cpp); RX_VK_MESH_SHARED_WARP=0 or 1
|
||||
inline bool mesh_fallback_shared_warp = false;
|
||||
// (RX_VK_MESH_SHARED_WARP_CHECK=1, for a developer: each shared ballot, vote and shuffle beside the subgroup's own, those
|
||||
// compared and those that differ counted past the fallback's records (emit_spirv.h WARP_COUNTER_*) and logged; the stage
|
||||
// keeps its subgroup instructions, so it stays at 32 lanes)
|
||||
inline bool mesh_fallback_shared_warp_check = false;
|
||||
|
||||
} // namespace Shader::Backend::SPIRV
|
||||
|
||||
228
gpu/vk/eden/shader_recompiler/backend/spirv/mma_software.h
Normal file
228
gpu/vk/eden/shader_recompiler/backend/spirv/mma_software.h
Normal file
@@ -0,0 +1,228 @@
|
||||
// (Triforce) HMMA.16816.F16 in software: D(16x8) = A(16x16) * B(16x8) + C on the subgroup's lanes, for GPUs without
|
||||
// cooperative matrices (AMD before RDNA 3, the Steam Deck among them; GTX 10 and 16; Intel's integrated GPUs). The 32 lanes'
|
||||
// fragments (PTX mma.m16n8k16's layout: lane t, g = t/4, i = t%4) are exchanged through subgroup operations; the products are
|
||||
// accumulated with FMA in f32 and rounded to f16 after every 8 columns of K, as HMMA.F16 does. One function, called for each
|
||||
// HMMA_nx2 (emit_spirv_warp.cpp EmitMatrixMulAddF16), and built the same way by the device's self-test (vk_coopmat.cpp), which
|
||||
// is why it takes a plain Sirit module and the types it needs rather than the translator's EmitContext.
|
||||
//
|
||||
// Two forms compute the same words:
|
||||
// - Loop: Lichii's (zortos293/Lichii, emit_spirv_tensor.cpp, GPL-3.0). The 8 steps (4 K-pairs in each half of K) in a loop the
|
||||
// driver is asked not to unroll; A and B from other lanes through shuffles, the f16 rounding in integer operations.
|
||||
// - Unrolled: the steps written out (in a loop that runs once: see below). A lane's rows of A are in its own quad (lanes 4g..4g+3), so A comes through quad broadcasts
|
||||
// (DPP on AMD, not the LDS crossbar a shuffle takes), and A's lane indices are constants (B's are still the lane's
|
||||
// warp_base + col_base + c). The rounding to f16 is either the same
|
||||
// integer operations or the GPU's own f32 -> f16 -> f32 conversion (round to nearest even, f16 subnormals kept). A conversion
|
||||
// applied to an FMA's result may be folded into it by the driver (RADV: v_fma_mixlo_f16), which rounds the exact sum once
|
||||
// instead of rounding it to f32 first: a tie in f32 then goes the other way (1 word in about 17,000 of random data). The value
|
||||
// is passed through a shuffle with lane xor 0 (the same lane) before the conversion, which keeps them apart; RADV folds that
|
||||
// shuffle into the conversion as a DPP identity, so it costs nothing. vk_coopmat.cpp runs both forms on the device and uses the
|
||||
// unrolled one only where its words are the loop's. The shuffle keeps them apart only while the driver keeps a shuffle by
|
||||
// xor 0: one that folds it away rounds once again, and the tie inputs then make the self-test keep the loop (slower, said
|
||||
// only in the log).
|
||||
//
|
||||
// On the Steam Deck (RADV), one multiply a lane over a 1280x800 dispatch: 0.469 ms looped, 0.363 ms unrolled with the integer
|
||||
// rounding, 0.177 ms unrolled with the conversion (a fully fused conversion, not the same words, 0.153 ms). OoT's gameplay network
|
||||
// at Resolution 100% (9dc98871, Kokiri Forest): 8.61 ms a frame looped, 3.82 ms unrolled with the conversion.
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
#include <sirit/sirit.h>
|
||||
|
||||
namespace Shader::Backend::SPIRV {
|
||||
|
||||
enum class SoftwareMmaForm : uint32_t {
|
||||
Loop = 0, // (Lichii's: the steps in a loop, shuffles, integer rounding)
|
||||
Unrolled = 1, // (written out, A through quad broadcasts, integer rounding)
|
||||
UnrolledF16 = 2, // (written out, A through quad broadcasts, the GPU's f16 conversion rounds)
|
||||
};
|
||||
|
||||
struct SoftwareMmaTypes {
|
||||
Sirit::Id u32, u32x2, u32x4, f32, f32x2, f16, boolean;
|
||||
};
|
||||
|
||||
/// The function (A: 4 words, B: 2, C: 2) -> D: 2 words, as the console's fragments hold them, two f16 a word. lane_variable is the
|
||||
/// SubgroupLocalInvocationId input; quad: OpGroupNonUniformQuadBroadcast may be used (the caller adds GroupNonUniformQuad), else
|
||||
/// A's lanes are shuffled to. The caller adds GroupNonUniform and GroupNonUniformShuffle.
|
||||
inline Sirit::Id DefineSoftwareMma(Sirit::Module& m, const SoftwareMmaTypes& t, Sirit::Id lane_variable, SoftwareMmaForm form,
|
||||
bool quad) {
|
||||
using Sirit::Id;
|
||||
const Id u32ty = t.u32, f32ty = t.f32;
|
||||
const auto imm = [&](uint32_t v) { return m.Constant(u32ty, v); };
|
||||
const auto add = [&](Id a, Id b) { return m.OpIAdd(u32ty, a, b); };
|
||||
const auto and_ = [&](Id a, uint32_t b) { return m.OpBitwiseAnd(u32ty, a, imm(b)); };
|
||||
const auto unpack = [&](Id v) { return m.OpUnpackHalf2x16(t.f32x2, v); };
|
||||
const auto round_half = [&](Id value) {
|
||||
// Do not express the intermediate rounding as unpack(pack(value)): drivers may
|
||||
// eliminate that round trip. Round normal significands explicitly, ties to even;
|
||||
// half subnormals lie on the exactly representable 2^-24 grid.
|
||||
const Id bits = m.OpBitcast(u32ty, value);
|
||||
const Id magnitude = and_(bits, 0x7fffffffU);
|
||||
const Id tie = and_(m.OpShiftRightLogical(u32ty, magnitude, imm(13)), 1);
|
||||
const Id rounded = and_(add(add(magnitude, imm(0xfff)), tie), ~0x1fffU);
|
||||
const Id normal = m.OpSelect(u32ty, m.OpUGreaterThanEqual(t.boolean, rounded, imm(0x47800000)), imm(0x7f800000), rounded);
|
||||
const Id scaled = m.OpFMul(f32ty, m.OpBitcast(f32ty, magnitude), m.Constant(f32ty, 16777216.0f));
|
||||
const Id subnormal = m.OpBitcast(u32ty, m.OpFMul(f32ty, m.OpRoundEven(f32ty, scaled), m.Constant(f32ty, 0x1p-24f)));
|
||||
const Id finite = m.OpSelect(u32ty, m.OpUGreaterThanEqual(t.boolean, magnitude, imm(0x38800000)), normal, subnormal);
|
||||
const Id result = m.OpSelect(u32ty, m.OpUGreaterThanEqual(t.boolean, magnitude, imm(0x7f800000)), magnitude, finite);
|
||||
return m.OpBitcast(f32ty, m.OpBitwiseOr(u32ty, result, and_(bits, 0x80000000)));
|
||||
};
|
||||
const auto round_half_f16 = [&](Id value) {
|
||||
// (through the same lane first: the conversion is then not folded into the FMA that made the value, see above)
|
||||
const Id same = m.OpBitcast(f32ty, m.OpGroupNonUniformShuffleXor(u32ty, imm(3), m.OpBitcast(u32ty, value), imm(0)));
|
||||
const Id half = m.OpFConvert(t.f16, same);
|
||||
m.Decorate(half, spv::Decoration::NoContraction);
|
||||
const Id back = m.OpFConvert(f32ty, half);
|
||||
m.Decorate(back, spv::Decoration::NoContraction);
|
||||
return back;
|
||||
};
|
||||
|
||||
const Id type = m.TypeFunction(t.u32x2, t.u32x4, t.u32x2, t.u32x2);
|
||||
const Id fn = m.OpFunction(t.u32x2, spv::FunctionControlMask::DontInline, type);
|
||||
const Id matrix_a = m.OpFunctionParameter(t.u32x4);
|
||||
const Id matrix_b = m.OpFunctionParameter(t.u32x2);
|
||||
const Id matrix_c = m.OpFunctionParameter(t.u32x2);
|
||||
m.AddLabel();
|
||||
const bool loop = form == SoftwareMmaForm::Loop;
|
||||
// (a function's variables come first in its first block; the counter first in either form, as in Lichii's)
|
||||
const Id counter = m.AddLocalVariable(m.TypePointer(spv::StorageClass::Function, u32ty), spv::StorageClass::Function);
|
||||
Id result{};
|
||||
std::array<Id, 4> accumulators{};
|
||||
if (!loop)
|
||||
result = m.AddLocalVariable(m.TypePointer(spv::StorageClass::Function, t.u32x2), spv::StorageClass::Function);
|
||||
else
|
||||
for (auto& acc : accumulators)
|
||||
acc = m.AddLocalVariable(m.TypePointer(spv::StorageClass::Function, f32ty), spv::StorageClass::Function);
|
||||
std::array<Id, 8> inputs;
|
||||
for (uint32_t i = 0; i < 4; ++i) inputs[i] = m.OpCompositeExtract(u32ty, matrix_a, i);
|
||||
for (uint32_t i = 0; i < 2; ++i) {
|
||||
inputs[4 + i] = m.OpCompositeExtract(u32ty, matrix_b, i);
|
||||
inputs[6 + i] = m.OpCompositeExtract(u32ty, matrix_c, i);
|
||||
}
|
||||
// (the lane's warp, its quad and its B lanes' base; read where Lichii's function reads them, so that the loop's SPIR-V is
|
||||
// the same as before word for word)
|
||||
Id warp_base{}, row_base{}, col_base{};
|
||||
const auto read_lane = [&] {
|
||||
const Id lane = m.OpLoad(u32ty, lane_variable);
|
||||
warp_base = and_(lane, ~31U);
|
||||
row_base = and_(lane, 28);
|
||||
col_base = m.OpShiftLeftLogical(u32ty, and_(lane, 3), imm(3));
|
||||
};
|
||||
const auto subgroup = [&] { return imm(3); }; // (spv::Scope::Subgroup)
|
||||
// acc[row * 2 + col] += A[row's register].half * B[col's lane].half, each half of the pair in turn (NoContraction: each FMA
|
||||
// rounds to f32 as the CPU's would)
|
||||
const auto multiply_add = [&](Id value, Id as, Id bs) {
|
||||
for (uint32_t half = 0; half < 2; ++half) {
|
||||
value = m.OpFma(f32ty, m.OpCompositeExtract(f32ty, as, half), m.OpCompositeExtract(f32ty, bs, half), value);
|
||||
m.Decorate(value, spv::Decoration::NoContraction);
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
if (!loop) {
|
||||
read_lane();
|
||||
std::array<Id, 4> acc;
|
||||
for (uint32_t row = 0; row < 2; ++row) {
|
||||
const Id c = unpack(inputs[6 + row]);
|
||||
for (uint32_t col = 0; col < 2; ++col) acc[row * 2 + col] = m.OpCompositeExtract(f32ty, c, col);
|
||||
}
|
||||
// (the body in a loop that runs once, which the driver is asked not to unroll. Mesa inlines every call, and with the bodies
|
||||
// side by side its CSE shares the shuffles of an operand that several multiplies take: fewer of them, but each held until
|
||||
// its last use. OoT's gameplay network then took 256 VGPRs (4 waves a SIMD on the Steam Deck, not 8) and the 3/4 one spilled
|
||||
// 181 to scratch; in a loop of their own they keep the loop form's 128. On the Deck, at 1280x800: 3.82 ms a frame in a loop,
|
||||
// 4.60 ms without)
|
||||
m.OpStore(counter, imm(0));
|
||||
const Id header = m.OpLabel(), body = m.OpLabel(), next = m.OpLabel(), end = m.OpLabel();
|
||||
m.OpBranch(header);
|
||||
m.AddLabel(header);
|
||||
const Id trip = m.OpLoad(u32ty, counter);
|
||||
const Id more = m.OpULessThan(t.boolean, trip, imm(1));
|
||||
m.OpLoopMerge(end, next, spv::LoopControlMask::DontUnroll);
|
||||
m.OpBranchConditional(more, body, end);
|
||||
m.AddLabel(body);
|
||||
for (uint32_t upper = 0; upper < 2; ++upper) { // (K 0-7, then 8-15: B's word and A's two of that half)
|
||||
for (uint32_t component = 0; component < 4; ++component) { // (the K pair 2 * component of that half)
|
||||
std::array<Id, 2> bs; // (B's column 2i + col, from lane 8i + component + 4 col)
|
||||
for (uint32_t col = 0; col < 2; ++col)
|
||||
bs[col] = unpack(m.OpGroupNonUniformShuffle(u32ty, subgroup(), inputs[4 + upper],
|
||||
add(warp_base, add(col_base, imm(component + col * 4)))));
|
||||
for (uint32_t row = 0; row < 2; ++row) { // (A's row g + 8 row, from lane 4g + component: this lane's quad)
|
||||
const Id a = inputs[upper * 2 + row];
|
||||
const Id as = unpack(quad ? m.OpGroupNonUniformQuadBroadcast(u32ty, subgroup(), a, imm(component))
|
||||
: m.OpGroupNonUniformShuffle(u32ty, subgroup(), a, add(warp_base, add(row_base, imm(component)))));
|
||||
for (uint32_t col = 0; col < 2; ++col) acc[row * 2 + col] = multiply_add(acc[row * 2 + col], as, bs[col]);
|
||||
}
|
||||
}
|
||||
// HMMA.F16 rounds after each K=8 group, including the midpoint of K=16.
|
||||
for (auto& v : acc) v = form == SoftwareMmaForm::UnrolledF16 ? round_half_f16(v) : round_half(v);
|
||||
}
|
||||
std::array<Id, 2> packed;
|
||||
for (uint32_t row = 0; row < 2; ++row)
|
||||
packed[row] = m.OpPackHalf2x16(u32ty, m.OpCompositeConstruct(t.f32x2, acc[row * 2], acc[row * 2 + 1]));
|
||||
m.OpStore(result, m.OpCompositeConstruct(t.u32x2, packed[0], packed[1]));
|
||||
m.OpBranch(next);
|
||||
m.AddLabel(next);
|
||||
m.OpStore(counter, add(trip, imm(1)));
|
||||
m.OpBranch(header);
|
||||
m.AddLabel(end);
|
||||
m.OpReturnValue(m.OpLoad(t.u32x2, result));
|
||||
m.OpFunctionEnd();
|
||||
return fn;
|
||||
}
|
||||
|
||||
for (uint32_t row = 0; row < 2; ++row) {
|
||||
const Id c = unpack(inputs[6 + row]);
|
||||
for (uint32_t col = 0; col < 2; ++col) m.OpStore(accumulators[row * 2 + col], m.OpCompositeExtract(f32ty, c, col));
|
||||
}
|
||||
m.OpStore(counter, imm(0));
|
||||
read_lane();
|
||||
const Id header = m.OpLabel(), body = m.OpLabel(), next = m.OpLabel(), end = m.OpLabel();
|
||||
m.OpBranch(header);
|
||||
m.AddLabel(header);
|
||||
const Id pair = m.OpLoad(u32ty, counter);
|
||||
const Id more = m.OpULessThan(t.boolean, pair, imm(8));
|
||||
m.OpLoopMerge(end, next, spv::LoopControlMask::DontUnroll);
|
||||
m.OpBranchConditional(more, body, end);
|
||||
m.AddLabel(body);
|
||||
const Id upper = m.OpUGreaterThanEqual(t.boolean, pair, imm(4));
|
||||
const Id component = and_(pair, 3);
|
||||
const Id a_lane = add(warp_base, add(row_base, component));
|
||||
const Id b_lane = add(warp_base, add(col_base, component));
|
||||
const Id b = m.OpSelect(u32ty, upper, inputs[5], inputs[4]);
|
||||
std::array<Id, 2> bs;
|
||||
for (uint32_t col = 0; col < 2; ++col)
|
||||
bs[col] = unpack(m.OpGroupNonUniformShuffle(u32ty, subgroup(), b, add(b_lane, imm(col * 4))));
|
||||
const Id round_now = m.OpIEqual(t.boolean, component, imm(3));
|
||||
for (uint32_t row = 0; row < 2; ++row) {
|
||||
const Id a = m.OpSelect(u32ty, upper, inputs[row + 2], inputs[row]);
|
||||
const Id as = unpack(m.OpGroupNonUniformShuffle(u32ty, subgroup(), a, a_lane));
|
||||
for (uint32_t col = 0; col < 2; ++col)
|
||||
m.OpStore(accumulators[row * 2 + col], multiply_add(m.OpLoad(f32ty, accumulators[row * 2 + col]), as, bs[col]));
|
||||
}
|
||||
// HMMA.F16 rounds after each K=8 group, including the midpoint of K=16.
|
||||
// OpSelect evaluates both operands: keep the expensive half conversion in
|
||||
// a branch so the other three pairs do no rounding work. The counter is
|
||||
// identical in every participating lane and all shuffles precede this branch.
|
||||
const Id round_block = m.OpLabel(), round_merge = m.OpLabel();
|
||||
m.OpSelectionMerge(round_merge, spv::SelectionControlMask::DontFlatten);
|
||||
m.OpBranchConditional(round_now, round_block, round_merge);
|
||||
m.AddLabel(round_block);
|
||||
for (const Id accumulator : accumulators) m.OpStore(accumulator, round_half(m.OpLoad(f32ty, accumulator)));
|
||||
m.OpBranch(round_merge);
|
||||
m.AddLabel(round_merge);
|
||||
m.OpBranch(next);
|
||||
m.AddLabel(next);
|
||||
m.OpStore(counter, add(pair, imm(1)));
|
||||
m.OpBranch(header);
|
||||
m.AddLabel(end);
|
||||
std::array<Id, 2> packed;
|
||||
for (uint32_t row = 0; row < 2; ++row)
|
||||
packed[row] = m.OpPackHalf2x16(u32ty, m.OpCompositeConstruct(t.f32x2, m.OpLoad(f32ty, accumulators[row * 2]),
|
||||
m.OpLoad(f32ty, accumulators[row * 2 + 1])));
|
||||
m.OpReturnValue(m.OpCompositeConstruct(t.u32x2, packed[0], packed[1]));
|
||||
m.OpFunctionEnd();
|
||||
return fn;
|
||||
}
|
||||
|
||||
} // namespace Shader::Backend::SPIRV
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
#include <unordered_set>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <climits>
|
||||
@@ -476,6 +478,45 @@ Id CasLoop(EmitContext& ctx, Operation operation, Id array_pointer, Id element_p
|
||||
return func;
|
||||
}
|
||||
|
||||
// Admit one active invocation per subgroup to the CAS loop at a time. Each invocation
|
||||
// still adds its own value and receives its own old value, including on overflow.
|
||||
Id SubgroupCasLoop(EmitContext& ctx, Id cas_func, Id array_pointer) {
|
||||
const Id subgroup{ctx.Const(static_cast<u32>(spv::Scope::Subgroup))};
|
||||
const Id func_type{ctx.TypeFunction(ctx.F32[1], ctx.U32[1], ctx.F32[1], array_pointer)};
|
||||
const Id func{ctx.OpFunction(ctx.F32[1], spv::FunctionControlMask::MaskNone, func_type)};
|
||||
const Id index{ctx.OpFunctionParameter(ctx.U32[1])};
|
||||
const Id value{ctx.OpFunctionParameter(ctx.F32[1])};
|
||||
const Id base{ctx.OpFunctionParameter(array_pointer)};
|
||||
ctx.AddLabel();
|
||||
const Id result{ctx.AddLocalVariable(ctx.TypePointer(spv::StorageClass::Function, ctx.F32[1]),
|
||||
spv::StorageClass::Function)};
|
||||
const Id header{ctx.OpLabel()};
|
||||
const Id selected{ctx.OpLabel()};
|
||||
const Id selection_end{ctx.OpLabel()};
|
||||
const Id next{ctx.OpLabel()};
|
||||
const Id merge{ctx.OpLabel()};
|
||||
ctx.OpBranch(header);
|
||||
ctx.AddLabel(header);
|
||||
ctx.OpLoopMerge(merge, next, spv::LoopControlMask::MaskNone);
|
||||
const Id elect_block{ctx.OpLabel()};
|
||||
ctx.OpBranch(elect_block);
|
||||
ctx.AddLabel(elect_block);
|
||||
const Id elected{ctx.OpGroupNonUniformElect(ctx.U1, subgroup)};
|
||||
ctx.OpSelectionMerge(selection_end, spv::SelectionControlMask::MaskNone);
|
||||
ctx.OpBranchConditional(elected, selected, selection_end);
|
||||
ctx.AddLabel(selected);
|
||||
ctx.OpStore(result, ctx.OpFunctionCall(ctx.F32[1], cas_func, index, value, base));
|
||||
ctx.OpBranch(selection_end);
|
||||
ctx.AddLabel(selection_end);
|
||||
ctx.OpBranch(next);
|
||||
ctx.AddLabel(next);
|
||||
ctx.OpBranchConditional(elected, merge, header);
|
||||
ctx.AddLabel(merge);
|
||||
ctx.OpReturnValue(ctx.OpLoad(ctx.F32[1], result));
|
||||
ctx.OpFunctionEnd();
|
||||
return func;
|
||||
}
|
||||
|
||||
template <typename Desc>
|
||||
std::string NameOf(Stage stage, const Desc& desc, std::string_view prefix) {
|
||||
if (desc.count > 1) {
|
||||
@@ -487,6 +528,10 @@ std::string NameOf(Stage stage, const Desc& desc, std::string_view prefix) {
|
||||
}
|
||||
}
|
||||
|
||||
// (Triforce) Textures, texture buffers, image buffers and images alike: a descriptor of count above 1 is an array of count,
|
||||
// the descriptors the renderer writes to its binding (vk_draw.cpp, ensure_layout), indexed with an OpAccessChain to an element
|
||||
// (pointer_type). Texture buffers were refused ("Array of texture buffers") and image buffers and images declared one image,
|
||||
// which that OpAccessChain cannot index (invalid SPIR-V). With a count of 1 the declaration is what it was.
|
||||
Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) {
|
||||
if (count > 1) {
|
||||
const Id array_type{ctx.TypeArray(sampled_type, ctx.Const(count))};
|
||||
@@ -532,7 +577,16 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf
|
||||
}
|
||||
DefineCommonTypes(program.info);
|
||||
DefineCommonConstants();
|
||||
PlanSharedWarp(program); // (Triforce: before the inputs, which a stage on the shared warp declares none of the subgroup's)
|
||||
DefineInterfaces(program);
|
||||
// (Triforce, mma_layout.h graphics_table_loads) a native task or mesh stage's global loads read the guest page table, as the
|
||||
// other stages' do (DefineGlobalMemoryFunctions table_stage): its push block holds the table's address, so the member is
|
||||
// wanted before any block is made (RX_VK_MESH_FIRST's below makes one)
|
||||
if ((stage == Stage::Task || stage == Stage::Mesh) && !runtime_info.mesh_fallback && graphics_table_loads != 0 &&
|
||||
program.info.uses_global_memory && profile.support_int64 && profile.unified_descriptor_binding &&
|
||||
profile.supported_spirv >= 0x00010400) {
|
||||
wants_guest_pages = true;
|
||||
}
|
||||
// (RX_VK_MESH_FIRST=1, with RX_VK_MESH_ARGS=1: off, one task program with it hung or ended the game at its first draw)
|
||||
if (stage == Stage::Task && !runtime_info.mesh_fallback && getenv("RX_VK_MESH_FIRST") &&
|
||||
profile.unified_descriptor_binding && profile.support_int64) {
|
||||
@@ -1093,9 +1147,14 @@ void EmitContext::DefineWriteStorageCasLoopFunction(const Info& info) {
|
||||
constexpr unsigned long long kGuestPagesBase = 0x1000000000ull;
|
||||
constexpr unsigned long long kGuestPagesBytes = 8ull << 30;
|
||||
// (a second window, its entries after the first's: the pools the game's GPU allocator puts at 0x7fxxxxxxxx, where its storage
|
||||
// buffers are. Pointers there read 0 with the first alone)
|
||||
// buffers are. Pointers there read 0 with the first alone. (Triforce) Kept at 4 GB from 508 GB: a wider one changes what every
|
||||
// GPU's compute stages read through it; the pool census (page_table_census.h) logs the pools outside it)
|
||||
constexpr unsigned long long kGuestPagesBase2 = 0x7f00000000ull;
|
||||
constexpr unsigned long long kGuestPagesBytes2 = 4ull << 30;
|
||||
// (and a third, after the second's: NVN's virtual pools from 1 TB (nvn_native.c VBASE), their entries the memory behind each
|
||||
// virtual page, written where a storage buffer there is bound (vk_memory.cpp memory_table_cover))
|
||||
constexpr unsigned long long kGuestPagesBase3 = 0x10000000000ull;
|
||||
constexpr unsigned long long kGuestPagesBytes3 = 4ull << 30;
|
||||
|
||||
void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
if (!info.uses_global_memory || !profile.support_int64) {
|
||||
@@ -1118,8 +1177,12 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
const bool graphics_stage{stage == Stage::VertexB || stage == Stage::Fragment};
|
||||
const bool follow_pointers{(stage == Stage::Compute || (!no_mesh && (stage == Stage::Task || stage == Stage::Mesh)) ||
|
||||
(graphics && graphics_stage && !info.uses_render_area)) && !no_follow};
|
||||
// (Triforce) a native task or mesh stage with the page table's member wanted (the constructor): addresses, and the push block
|
||||
// that holds the table's, for its loads through the table (table_stage below). Its searches are left as they were: no
|
||||
// follow_pointers fallback for its stores
|
||||
const bool native_mesh_table{(stage == Stage::Task || stage == Stage::Mesh) && !mesh_fallback && wants_guest_pages};
|
||||
Id ptr_u64{};
|
||||
if (follow_pointers) {
|
||||
if (follow_pointers || native_mesh_table) {
|
||||
wants_guest_pages = true;
|
||||
if (profile.unified_descriptor_binding && rescaling_push_constants.value == 0) DefineRescalingInputPushConstant(); // (the table's address is in it)
|
||||
AddCapability(spv::Capability::PhysicalStorageBufferAddresses);
|
||||
@@ -1157,23 +1220,44 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
// and must end that block (a return). Falls through after it)
|
||||
// (RX_VK_FOLLOW_STORES=1: writes and atomics through the table too; off by default, see the storage buffer pass)
|
||||
static const bool follow_stores{getenv("RX_VK_FOLLOW_STORES") != nullptr};
|
||||
// (hookshot) size: the access's bytes. An access that runs past its 4 KB page is left to the miss path: the table maps pages one
|
||||
// (Triforce, mma_layout.h graphics_table_loads) a vertex, fragment or compute stage that has the table, the task and mesh
|
||||
// stages, native and the mesh fallback's: its loads through it alone (1), or the search kept and checked against it (2, not
|
||||
// the fallback's task and mesh stages: the miss log's buffer is not theirs). Not where the render area's push block leaves
|
||||
// no table (follow_pointers). A native task or mesh stage inlines its load helpers (, define_load): with the search,
|
||||
// every bound storage buffer's range tested at every load
|
||||
const bool table_stage{graphics_stage || stage == Stage::Compute || stage == Stage::Task || stage == Stage::Mesh};
|
||||
const bool graphics_table{graphics_table_loads != 0 && table_stage && wants_guest_pages &&
|
||||
rescaling_push_constants.value != 0 && profile.supported_spirv >= 0x00010400};
|
||||
const bool table_alone{graphics_table && graphics_table_loads == 1};
|
||||
const bool table_check{graphics_table && graphics_table_loads == 2 &&
|
||||
!(mesh_fallback && (stage == Stage::Task || stage == Stage::Mesh))};
|
||||
// (Triforce) size: the access's bytes. An access that runs past its 4 KB page is left to the miss path: the table maps pages one
|
||||
// by one, the next page may be another allocation's or none, and a 16-byte read at a page's end faulted the device (the culling
|
||||
// passes 53caac5f, b6122162 at a save's load)
|
||||
// (Triforce) where an address's entry is in the table: inside one of its windows (page_table_census.h kPageWindows), its page
|
||||
// numbered after the windows before it
|
||||
const auto table_window = [&](Id addr) {
|
||||
const Id offset_a{OpISub(U64, addr, Constant(U64, u64{kGuestPagesBase}))};
|
||||
const Id offset_b{OpISub(U64, addr, Constant(U64, u64{kGuestPagesBase2}))};
|
||||
const Id offset_c{OpISub(U64, addr, Constant(U64, u64{kGuestPagesBase3}))};
|
||||
const Id in_a{OpULessThan(U1, offset_a, Constant(U64, u64{kGuestPagesBytes}))};
|
||||
const Id in_b{OpULessThan(U1, offset_b, Constant(U64, u64{kGuestPagesBytes2}))};
|
||||
const Id in_c{OpULessThan(U1, offset_c, Constant(U64, u64{kGuestPagesBytes3}))};
|
||||
const Id page_c{OpIAdd(U64, OpShiftRightLogical(U64, offset_c, Const(12U)),
|
||||
Constant(U64, u64{(kGuestPagesBytes + kGuestPagesBytes2) >> 12}))};
|
||||
const Id page_b{OpIAdd(U64, OpShiftRightLogical(U64, offset_b, Const(12U)), Constant(U64, u64{kGuestPagesBytes >> 12}))};
|
||||
const Id page_index{OpSelect(U64, in_a, OpShiftRightLogical(U64, offset_a, Const(12U)), OpSelect(U64, in_b, page_b, page_c))};
|
||||
return std::pair{OpLogicalOr(U1, in_a, OpLogicalOr(U1, in_b, in_c)), page_index};
|
||||
};
|
||||
const auto guest_lookup = [&](Id addr, u32 size, auto&& hit) {
|
||||
if (!follow_pointers || gp_level < 2) return;
|
||||
const Id table{wants_guest_pages && rescaling_push_constants.value != 0
|
||||
? OpLoad(U64, OpAccessChain(TypePointer(spv::StorageClass::PushConstant, U64), rescaling_push_constants,
|
||||
Const(guest_pages_member_index)))
|
||||
: Constant(U64, u64{0})}; // (pushed: the table's device address, 0 for none)
|
||||
const Id offset_a{OpISub(U64, addr, Constant(U64, u64{kGuestPagesBase}))};
|
||||
const Id offset_b{OpISub(U64, addr, Constant(U64, u64{kGuestPagesBase2}))};
|
||||
const Id in_a{OpULessThan(U1, offset_a, Constant(U64, u64{kGuestPagesBytes}))};
|
||||
const Id in_b{OpULessThan(U1, offset_b, Constant(U64, u64{kGuestPagesBytes2}))};
|
||||
const Id page_index{OpSelect(U64, in_a, OpShiftRightLogical(U64, offset_a, Const(12U)),
|
||||
OpIAdd(U64, OpShiftRightLogical(U64, offset_b, Const(12U)), Constant(U64, u64{kGuestPagesBytes >> 12})))};
|
||||
const auto [in_window, page_index] = table_window(addr);
|
||||
const Id fits{OpULessThanEqual(U1, OpBitwiseAnd(U64, addr, Constant(U64, u64{0xfff})), Constant(U64, u64{0x1000 - size}))};
|
||||
const Id inside{OpLogicalAnd(U1, OpLogicalAnd(U1, OpLogicalOr(U1, in_a, in_b), OpINotEqual(U1, table, Constant(U64, u64{0}))), fits)};
|
||||
const Id inside{OpLogicalAnd(U1, OpLogicalAnd(U1, in_window, OpINotEqual(U1, table, Constant(U64, u64{0}))), fits)};
|
||||
const Id then_label{OpLabel()};
|
||||
const Id else_label{OpLabel()};
|
||||
OpSelectionMerge(else_label, spv::SelectionControlMask::MaskNone);
|
||||
@@ -1197,6 +1281,7 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
// (EvilOOT) the structure pointer type of a storage buffer of that member's elements, for OpArrayLength
|
||||
const auto struct_pointer_of{[&](DefPtr m) -> Id {
|
||||
if (m == &StorageDefinitions::U32) return storage_types.U32.array;
|
||||
if (m == &StorageDefinitions::F32) return storage_types.F32.array; // (hookshot: the native float add's, as U32's)
|
||||
if (m == &StorageDefinitions::U32x2) return storage_types.U32x2.array;
|
||||
if (m == &StorageDefinitions::U32x4) return storage_types.U32x4.array;
|
||||
return Id{};
|
||||
@@ -1239,6 +1324,12 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
const auto define_body{[&](DefPtr ssbo_member, Id addr, Id element_pointer, u32 shift,
|
||||
auto&& callback, bool add_label = true) {
|
||||
if (add_label) AddLabel();
|
||||
// (hookshot, global_table_loop: the tables' entries in a loop, its counter here, where the function's variables must be.
|
||||
// The CAS loops' callers open the block themselves: theirs stay written out)
|
||||
const Id table_counter{global_table_loop && add_label && !ssbo_tables.empty()
|
||||
? AddLocalVariable(TypePointer(spv::StorageClass::Function, U32[1]), spv::StorageClass::Function)
|
||||
: Id{}};
|
||||
if (Sirit::ValidId(table_counter)) AddCapability(spv::Capability::StorageBufferArrayDynamicIndexing); // (the tables by k)
|
||||
const size_t num_buffers{info.storage_buffers_descriptors.size()};
|
||||
// (from Fritchii) Info::native_global_bounds: the candidate pointers the storage buffer pass bound for accesses it
|
||||
// left global (any single descriptor not in the driver bank) carry no NVN size word: the address goes to the
|
||||
@@ -1293,7 +1384,9 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
// nvn_buffer_used[index], took a descriptor's place in the list for an NVN slot number: with the tracked
|
||||
// buffers first, the slots' own descriptors past them were skipped and their reads gave 0. A match is exact,
|
||||
// so searching a buffer the access never falls in costs a comparison and nothing else.
|
||||
if (info.storage_buffers_descriptors[index].count > 1) {
|
||||
// ((Triforce) nor a simulated-turf list's own descriptor (ir_opt/sim_turf_pool_pass.h): its offset is no constant bank
|
||||
// word, and the accesses that go to it name it)
|
||||
if (info.storage_buffers_descriptors[index].count > 1 || IsSimTurfStorage(info.storage_buffers_descriptors[index])) {
|
||||
continue;
|
||||
}
|
||||
const auto& ssbo{info.storage_buffers_descriptors[index]};
|
||||
@@ -1336,6 +1429,58 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
if (!Sirit::ValidId(table_id) || !Sirit::ValidId(cbufs[table.cbuf_index].U32x2)) {
|
||||
continue;
|
||||
}
|
||||
if (Sirit::ValidId(table_counter)) {
|
||||
// (mma_layout.h global_table_loop) the same test as the written-out one below, entry k of the loop's counter, the
|
||||
// driver asked not to unroll it. k is the same in every invocation that runs it: dynamic indexing, not non-uniform.
|
||||
// The written-out form below is left as it was: other GPUs' SPIR-V, and their drivers' caches of it, do not change
|
||||
const Id header{OpLabel()};
|
||||
const Id check{OpLabel()};
|
||||
const Id body{OpLabel()};
|
||||
const Id next{OpLabel()};
|
||||
const Id done{OpLabel()};
|
||||
OpStore(table_counter, zero);
|
||||
OpBranch(header);
|
||||
AddLabel(header);
|
||||
OpLoopMerge(done, next, spv::LoopControlMask::DontUnroll);
|
||||
OpBranch(check);
|
||||
AddLabel(check);
|
||||
const Id k{OpLoad(U32[1], table_counter)};
|
||||
OpBranchConditional(OpULessThan(U1, k, Const(table.count)), body, done);
|
||||
AddLabel(body);
|
||||
// (entry k at cbuf_offset + 16k: its address in the U32x2 word cbuf_offset / 8 + 2k, its size in the next)
|
||||
const Id word{OpIAdd(U32[1], Const(table.cbuf_offset / 8), OpShiftLeftLogical(U32[1], k, Const(1U)))};
|
||||
const Id entry_pointer{OpAccessChain(uniform_types.U32x2, cbufs[table.cbuf_index].U32x2, zero, word)};
|
||||
const Id size_pointer{OpAccessChain(uniform_types.U32x2, cbufs[table.cbuf_index].U32x2, zero,
|
||||
OpIAdd(U32[1], word, Const(1U)))};
|
||||
const u64 ssbo_align_mask{~(profile.min_ssbo_alignment - 1U)};
|
||||
const Id unaligned_addr{OpBitcast(U64, OpLoad(U32[2], entry_pointer))};
|
||||
const Id ssbo_addr{OpBitwiseAnd(U64, unaligned_addr, Constant(U64, ssbo_align_mask))};
|
||||
const Id size_word{OpCompositeExtract(U32[1], OpLoad(U32[2], size_pointer), 0U)};
|
||||
Id ssbo_end{OpIAdd(U64, unaligned_addr, OpUConvert(U64, size_word))};
|
||||
if (const Id struct_pointer{struct_pointer_of(ssbo_member)}; Sirit::ValidId(struct_pointer)) {
|
||||
const Id elements{OpArrayLength(U32[1], OpAccessChain(struct_pointer, table_id, k), 0U)};
|
||||
const Id bound_end{
|
||||
OpIAdd(U64, ssbo_addr, OpUConvert(U64, OpShiftLeftLogical(U32[1], elements, Const(shift))))};
|
||||
const Id capped{OpSelect(U64, OpULessThan(U1, bound_end, ssbo_end), bound_end, ssbo_end)};
|
||||
ssbo_end = OpSelect(U64, OpIEqual(U1, size_word, zero), bound_end, capped);
|
||||
}
|
||||
const Id cond{OpLogicalAnd(U1, OpUGreaterThanEqual(U1, addr, ssbo_addr), OpULessThan(U1, addr, ssbo_end))};
|
||||
const Id then_label{OpLabel()};
|
||||
const Id else_label{OpLabel()};
|
||||
OpSelectionMerge(else_label, spv::SelectionControlMask::MaskNone);
|
||||
OpBranchConditional(cond, then_label, else_label);
|
||||
AddLabel(then_label);
|
||||
const Id ssbo_offset{OpUConvert(U32[1], OpISub(U64, addr, ssbo_addr))};
|
||||
const Id ssbo_index{OpShiftRightLogical(U32[1], ssbo_offset, Const(shift))};
|
||||
callback(OpAccessChain(element_pointer, table_id, k, zero, ssbo_index)); // (ends in a return)
|
||||
AddLabel(else_label);
|
||||
OpBranch(next);
|
||||
AddLabel(next);
|
||||
OpStore(table_counter, OpIAdd(U32[1], k, Const(1U)));
|
||||
OpBranch(header);
|
||||
AddLabel(done);
|
||||
continue;
|
||||
}
|
||||
for (u32 k = 0; k < table.count; ++k) {
|
||||
const u32 at{table.cbuf_offset + k * 16};
|
||||
const Id entry_pointer{OpAccessChain(uniform_types.U32x2, cbufs[table.cbuf_index].U32x2, zero,
|
||||
@@ -1370,9 +1515,34 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
AddLabel(else_label);
|
||||
}
|
||||
}
|
||||
record_miss(addr); // (EvilOOT, RX_VK_MISS_LOG: here no buffer held the address)
|
||||
if (!table_check) record_miss(addr); // (EvilOOT, RX_VK_MISS_LOG: here no buffer held the address. Checking, the buffer
|
||||
// records the loads the table answers otherwise instead)
|
||||
}};
|
||||
// (hookshot) the helpers below are DontInline: inlined at each of a culling program's hundreds of global accesses, with EvilOOT's
|
||||
// (Triforce, mma_layout.h mesh_fallback_pages) the mesh fallback's task and mesh stages: a load is the guest page table's word,
|
||||
// 0 where it maps nothing, as the console's GPU reads memory. No branch and no search: a storage buffer bound below 1 TB is in a
|
||||
// pool the table maps (vk_memory.cpp guest_pages_set), so the search found the same words (739acd67's 243 million loads,
|
||||
// compared), and where it found none it answered 0 though the console reads the memory. A virtual pool's (from 1 TB,
|
||||
// nvn_native.c VBASE) is not in the table: a load from one reads 0 here (none at Hyrule Castle). The stage is held to 32 lanes and at the register
|
||||
// file's limit: what a load keeps alive is spilled, and a branch around it cost as much again (Iris Xe, Mesa 26.2). No test
|
||||
// that the access stays in its page: the guest's loads are aligned to their size (16 bytes at most), so none crosses one.
|
||||
// Stores and atomics keep the search
|
||||
const bool fallback_pages{mesh_fallback_pages && mesh_fallback && (stage == Stage::Task || stage == Stage::Mesh) &&
|
||||
wants_guest_pages && rescaling_push_constants.value != 0 &&
|
||||
profile.supported_spirv >= 0x00010400}; // (a vector's OpSelect on one condition: SPIR-V 1.4)
|
||||
const auto page_load = [&](Id type, Id addr) {
|
||||
const Id table{OpLoad(U64, OpAccessChain(TypePointer(spv::StorageClass::PushConstant, U64), rescaling_push_constants,
|
||||
Const(guest_pages_member_index)))};
|
||||
const Id u64_zero{Constant(U64, u64{0})};
|
||||
const auto [in_window, page_index] = table_window(addr);
|
||||
const Id inside{OpLogicalAnd(U1, in_window, OpINotEqual(U1, table, u64_zero))};
|
||||
// (outside the table's windows the table's own first word is read, and the answer is 0 all the same)
|
||||
const Id slot{OpSelect(U64, inside, OpIAdd(U64, table, OpShiftLeftLogical(U64, page_index, Const(3U))), table)};
|
||||
const Id page{psb_load(U64, slot, 8U)};
|
||||
const Id mapped{OpLogicalAnd(U1, inside, OpINotEqual(U1, page, u64_zero))};
|
||||
const Id target{OpSelect(U64, mapped, OpIAdd(U64, page, OpBitwiseAnd(U64, addr, Constant(U64, u64{0xfff}))), table)};
|
||||
return OpSelect(type, mapped, psb_load(type, target, 4U), ConstantNull(type));
|
||||
};
|
||||
// (Triforce) the helpers below are DontInline: inlined at each of a culling program's hundreds of global accesses, with EvilOOT's
|
||||
// fuller buffer search, NVIDIA's compiler took 41 s on 53caac5f and the driver reset. Lichii PR 408 found the same (161 s for 68 programs)
|
||||
const auto define_load{[&](DefPtr ssbo_member, Id element_pointer, Id type, u32 shift) {
|
||||
const Id function_type{TypeFunction(type, U64)};
|
||||
@@ -1390,8 +1560,37 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
const Id func_id{OpFunction(type, inline_here ? spv::FunctionControlMask::Inline : spv::FunctionControlMask::DontInline,
|
||||
function_type)};
|
||||
const Id addr{OpFunctionParameter(U64)};
|
||||
define_body(ssbo_member, addr, element_pointer, shift,
|
||||
[&](Id ssbo_pointer) { OpReturnValue(OpLoad(type, ssbo_pointer)); });
|
||||
if (fallback_pages || table_alone) {
|
||||
AddLabel();
|
||||
OpReturnValue(page_load(type, addr));
|
||||
OpFunctionEnd();
|
||||
return func_id;
|
||||
}
|
||||
define_body(ssbo_member, addr, element_pointer, shift, [&](Id ssbo_pointer) {
|
||||
const Id value{OpLoad(type, ssbo_pointer)};
|
||||
if (table_check) { // (the bound buffer's words against the table's, every component)
|
||||
const Id from_table{page_load(type, addr)};
|
||||
Id differ{};
|
||||
const u32 components{shift == 2 ? 1U : shift == 3 ? 2U : 4U};
|
||||
for (u32 i = 0; i < components; ++i) {
|
||||
const Id a{components == 1 ? value : OpCompositeExtract(U32[1], value, i)};
|
||||
const Id b{components == 1 ? from_table : OpCompositeExtract(U32[1], from_table, i)};
|
||||
const Id x{OpBitwiseXor(U32[1], a, b)};
|
||||
differ = i == 0 ? x : OpBitwiseOr(U32[1], differ, x);
|
||||
}
|
||||
// (the condition before the merge: OpSelectionMerge must come right before its branch, or RADV crashes)
|
||||
const Id differs{OpINotEqual(U1, differ, zero)};
|
||||
const Id then_label{OpLabel()};
|
||||
const Id end_label{OpLabel()};
|
||||
OpSelectionMerge(end_label, spv::SelectionControlMask::MaskNone);
|
||||
OpBranchConditional(differs, then_label, end_label);
|
||||
AddLabel(then_label);
|
||||
record_miss(addr);
|
||||
OpBranch(end_label);
|
||||
AddLabel(end_label);
|
||||
}
|
||||
OpReturnValue(value);
|
||||
});
|
||||
guest_lookup(addr, 1U << shift, [&](Id target) {
|
||||
if (gp_level >= 3) OpReturnValue(psb_load(type, target, 4U));
|
||||
else OpReturnValue(ConstantNull(type));
|
||||
@@ -1532,17 +1731,18 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
write_global_cas_loop_func = func;
|
||||
}
|
||||
// (hookshot) RED/ATOM.E.ADD.F32 at an address no buffer was tracked to: the 32-bit atomics' fallback, with the float
|
||||
// sum made in a compare-exchange loop as the storage buffers' f32_add_cas does
|
||||
// sum made in a compare-exchange loop as the storage buffers' f32_add_cas does, or the GPU's own float add where
|
||||
// the storage buffers' adds are (native_f32_add: the f32 views, searched as the u32 ones)
|
||||
if (info.stores_global_memory) {
|
||||
const Id func_type{TypeFunction(F32[1], U64, F32[1])};
|
||||
const Id func{OpFunction(F32[1], spv::FunctionControlMask::DontInline, func_type)};
|
||||
const Id addr{OpFunctionParameter(U64)};
|
||||
const Id value{OpFunctionParameter(F32[1])};
|
||||
AddLabel();
|
||||
const Id expected_var{AddLocalVariable(TypePointer(spv::StorageClass::Function, U32[1]),
|
||||
spv::StorageClass::Function)};
|
||||
const Id tries_var{AddLocalVariable(TypePointer(spv::StorageClass::Function, U32[1]),
|
||||
spv::StorageClass::Function)};
|
||||
const Id expected_var{native_f32_add ? Id{} : AddLocalVariable(TypePointer(spv::StorageClass::Function, U32[1]),
|
||||
spv::StorageClass::Function)};
|
||||
const Id tries_var{native_f32_add ? Id{} : AddLocalVariable(TypePointer(spv::StorageClass::Function, U32[1]),
|
||||
spv::StorageClass::Function)};
|
||||
const auto cas_loop{[&](Id pointer) {
|
||||
OpStore(expected_var, OpLoad(U32[1], pointer, spv::MemoryAccessMask::Aligned, 4U));
|
||||
OpStore(tries_var, u32_zero_value); // (a ceiling of attempts, as every compare-exchange loop has: #548)
|
||||
@@ -1569,11 +1769,19 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
|
||||
AddLabel(continue_label);
|
||||
OpBranch(begin_label);
|
||||
}};
|
||||
define_body(&StorageDefinitions::U32, addr, storage_types.U32.element, 2,
|
||||
[&](Id ssbo_pointer) { cas_loop(ssbo_pointer); }, false);
|
||||
const auto native_add{[&](Id pointer) {
|
||||
OpReturnValue(OpAtomicFAddEXT(F32[1], pointer, Const(static_cast<u32>(spv::Scope::Device)), u32_zero_value, value));
|
||||
}};
|
||||
if (native_f32_add) {
|
||||
define_body(&StorageDefinitions::F32, addr, storage_types.F32.element, 2, native_add, false);
|
||||
} else {
|
||||
define_body(&StorageDefinitions::U32, addr, storage_types.U32.element, 2,
|
||||
[&](Id ssbo_pointer) { cas_loop(ssbo_pointer); }, false);
|
||||
}
|
||||
if (follow_stores) guest_lookup(addr, 4U, [&](Id target) {
|
||||
if (gp_level >= 3) cas_loop(psb_member(U32[1], target));
|
||||
else OpReturnValue(ConstantNull(F32[1]));
|
||||
if (gp_level < 3) OpReturnValue(ConstantNull(F32[1]));
|
||||
else if (native_f32_add) native_add(psb_member(F32[1], target));
|
||||
else cas_loop(psb_member(U32[1], target));
|
||||
});
|
||||
OpReturnValue(ConstantNull(F32[1]));
|
||||
OpFunctionEnd();
|
||||
@@ -1858,6 +2066,14 @@ void EmitContext::DefineConstantBufferIndirectFunctions(const Info& info) {
|
||||
}
|
||||
|
||||
void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) {
|
||||
// (hookshot) RED/ATOM.ADD.F32 as the GPU's own float add where it has one, on an f32 view of the buffer: the
|
||||
// compare-exchange loop gives up after CAS_LOOP_MAX_TRIES attempts, and an add that gives up is lost. Decided before
|
||||
// the early return: DefineGlobalMemoryFunctions' fallback reads it too
|
||||
native_f32_add = profile.support_float32_atomic_add && profile.support_descriptor_aliasing && info.uses_atomic_f32_add;
|
||||
if (native_f32_add) {
|
||||
AddExtension("SPV_EXT_shader_atomic_float_add");
|
||||
AddCapability(spv::Capability::AtomicFloat32AddEXT);
|
||||
}
|
||||
if (info.storage_buffers_descriptors.empty()) {
|
||||
return;
|
||||
}
|
||||
@@ -1866,6 +2082,9 @@ void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) {
|
||||
IR::Type used_types{profile.support_descriptor_aliasing ? info.used_storage_buffer_types
|
||||
: IR::Type::U32};
|
||||
used_types |= IR::Type::U32;
|
||||
if (native_f32_add) {
|
||||
used_types |= IR::Type::F32;
|
||||
}
|
||||
if (profile.support_int8 && profile.support_storage_buffer_8bit &&
|
||||
True(used_types & IR::Type::U8)) {
|
||||
DefineSsbos(*this, storage_types.U8, &StorageDefinitions::U8, info, binding, U8,
|
||||
@@ -1918,9 +2137,14 @@ void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) {
|
||||
decrement_cas_ssbo = CasLoop(*this, Operation::Decrement, storage_types.U32.array,
|
||||
storage_types.U32.element, U32[1], spv::Scope::Device);
|
||||
}
|
||||
if (info.uses_atomic_f32_add) {
|
||||
if (info.uses_atomic_f32_add && !native_f32_add) {
|
||||
f32_add_cas = CasLoop(*this, Operation::FPAdd, storage_types.U32.array,
|
||||
storage_types.U32.element, F32[1], spv::Scope::Device);
|
||||
if (f32_add_subgroup && stage == Stage::Compute && !mesh_fallback && profile.supported_spirv >= 0x00010300 &&
|
||||
profile.SupportsSubgroupStage(stage)) {
|
||||
AddCapability(spv::Capability::GroupNonUniform);
|
||||
f32_add_cas = SubgroupCasLoop(*this, f32_add_cas, storage_types.U32.array);
|
||||
}
|
||||
}
|
||||
if (info.uses_atomic_f16x2_add) {
|
||||
f16x2_add_cas = CasLoop(*this, Operation::FPAdd, storage_types.U32.array,
|
||||
@@ -1958,15 +2182,14 @@ void EmitContext::DefineTextureBuffers(const Info& info, u32& binding) {
|
||||
const Id type{TypePointer(spv::StorageClass::UniformConstant, image_buffer_type)};
|
||||
texture_buffers.reserve(info.texture_buffer_descriptors.size());
|
||||
for (const TextureBufferDescriptor& desc : info.texture_buffer_descriptors) {
|
||||
if (desc.count != 1) {
|
||||
throw NotImplementedException("Array of texture buffers");
|
||||
}
|
||||
const Id id{AddGlobalVariable(type, spv::StorageClass::UniformConstant)};
|
||||
const Id desc_type{DescType(*this, image_buffer_type, type, desc.count)};
|
||||
const Id id{AddGlobalVariable(desc_type, spv::StorageClass::UniformConstant)};
|
||||
Decorate(id, spv::Decoration::Binding, binding);
|
||||
Decorate(id, spv::Decoration::DescriptorSet, 0U);
|
||||
Name(id, NameOf(stage, desc, "texbuf"));
|
||||
texture_buffers.push_back({
|
||||
.id = id,
|
||||
.pointer_type = type,
|
||||
.count = desc.count,
|
||||
});
|
||||
if (profile.supported_spirv >= 0x00010400) {
|
||||
@@ -1984,7 +2207,8 @@ void EmitContext::DefineImageBuffers(const Info& info, u32& binding) {
|
||||
const Id image_type{
|
||||
TypeImage(sampled_type, spv::Dim::Buffer, false, false, false, 2, format)};
|
||||
const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)};
|
||||
const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
|
||||
const Id desc_type{DescType(*this, image_type, pointer_type, desc.count)};
|
||||
const Id id{AddGlobalVariable(desc_type, spv::StorageClass::UniformConstant)};
|
||||
Decorate(id, spv::Decoration::Binding, binding);
|
||||
Decorate(id, spv::Decoration::DescriptorSet, 0U);
|
||||
Name(id, NameOf(stage, desc, "imgbuf"));
|
||||
@@ -2039,7 +2263,8 @@ void EmitContext::DefineImages(const Info& info, u32& binding, u32& scaling_inde
|
||||
const Id sampled_type{desc.is_integer ? U32[1] : F32[1]};
|
||||
const Id image_type{ImageType(*this, desc, sampled_type)};
|
||||
const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)};
|
||||
const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
|
||||
const Id desc_type{DescType(*this, image_type, pointer_type, desc.count)};
|
||||
const Id id{AddGlobalVariable(desc_type, spv::StorageClass::UniformConstant)};
|
||||
Decorate(id, spv::Decoration::Binding, binding);
|
||||
Decorate(id, spv::Decoration::DescriptorSet, 0U);
|
||||
Name(id, NameOf(stage, desc, "img"));
|
||||
@@ -2071,6 +2296,12 @@ void EmitContext::DefineInputs(const IR::Program& program) {
|
||||
if (info.uses_workgroup_id) {
|
||||
workgroup_id = DefineInput(*this, U32[3], false, spv::BuiltIn::WorkgroupId);
|
||||
}
|
||||
if (stage == Stage::Mesh && !runtime_info.mesh_fallback && profile.need_mesh_num_workgroups) {
|
||||
// (Triforce, , as Lichii ) RADV before Mesa 26.2.5 (!45092) miscounted the mesh grid-size registers and
|
||||
// emitted an invalid SET_SH_REG packet (reg_count == 0, RING_GFP_TIMEOUT on RDNA 3/4) when a native mesh shader
|
||||
// did not read gl_NumWorkGroups
|
||||
num_workgroups = DefineInput(*this, U32[3], false, spv::BuiltIn::NumWorkgroups);
|
||||
}
|
||||
if (info.uses_local_invocation_id) {
|
||||
local_invocation_id = DefineInput(*this, U32[3], false, spv::BuiltIn::LocalInvocationId);
|
||||
}
|
||||
@@ -2090,7 +2321,9 @@ void EmitContext::DefineInputs(const IR::Program& program) {
|
||||
if (info.uses_is_helper_invocation) {
|
||||
is_helper_invocation = DefineInput(*this, U1, false, spv::BuiltIn::HelperInvocation);
|
||||
}
|
||||
if (info.uses_subgroup_mask && profile.SupportsSubgroupStage(stage)) {
|
||||
// (Triforce: a stage on the shared warp reads none of the subgroup's inputs, and declares none; its check keeps them)
|
||||
const bool subgroup_free{warp_shared && !warp_shared_check};
|
||||
if (info.uses_subgroup_mask && profile.SupportsSubgroupStage(stage) && !subgroup_free) {
|
||||
subgroup_mask_eq = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupEqMaskKHR);
|
||||
subgroup_mask_lt = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupLtMaskKHR);
|
||||
subgroup_mask_le = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupLeMaskKHR);
|
||||
@@ -2107,7 +2340,7 @@ void EmitContext::DefineInputs(const IR::Program& program) {
|
||||
if ((info.uses_fswzadd || info.uses_subgroup_invocation_id || info.uses_subgroup_shuffles ||
|
||||
(profile.warp_size_potentially_larger_than_guest &&
|
||||
(info.uses_subgroup_vote || info.uses_subgroup_mask))) &&
|
||||
profile.SupportsSubgroupStage(stage)) {
|
||||
profile.SupportsSubgroupStage(stage) && !subgroup_free) {
|
||||
AddCapability(spv::Capability::GroupNonUniform);
|
||||
subgroup_local_invocation_id =
|
||||
DefineInput(*this, U32[1], false, spv::BuiltIn::SubgroupLocalInvocationId);
|
||||
@@ -2269,6 +2502,166 @@ void EmitContext::DefineInputs(const IR::Program& program) {
|
||||
}
|
||||
}
|
||||
|
||||
/// (Triforce, mma_layout.h mesh_fallback_shared_warp) Whether this fallback stage's warp can live in workgroup memory, and if so
|
||||
/// how many words it takes. It can when the workgroup is one warp (32 threads), no ballot, vote or shuffle is in a loop (a
|
||||
/// site's words serve one pass), nothing leaves the program inside an if (a lane that entered a region reaches its sites), and
|
||||
/// nothing depends on the lanes running in one thread in program order: no quad or matrix operation, no subgroup barrier, no
|
||||
/// read of the stage buffer's outputs. The ifs with a site inside become regions (emit_spirv_warp.cpp). Nothing is emitted
|
||||
/// here: it runs before the stage's inputs are declared (DefineInputs), which depend on the answer.
|
||||
void EmitContext::PlanSharedWarp(const IR::Program& program) {
|
||||
// (one warp a workgroup: the lane is the invocation's x, so a workgroup of more than one row would be two warps on one set of words)
|
||||
if (!runtime_info.mesh_fallback || (stage != Stage::Task && stage != Stage::Mesh) || !mesh_fallback_shared_warp ||
|
||||
program.workgroup_size[0] != 32 || program.workgroup_size[1] != 1 || program.workgroup_size[2] != 1) {
|
||||
return;
|
||||
}
|
||||
std::vector<const IR::Block*> open;
|
||||
std::unordered_set<const IR::Block*> regions;
|
||||
u32 loops{};
|
||||
u32 words{};
|
||||
for (const IR::AbstractSyntaxNode& node : program.syntax_list) {
|
||||
switch (node.type) {
|
||||
case IR::AbstractSyntaxNode::Type::Block:
|
||||
for (const IR::Inst& inst : std::as_const(*node.data.block).Instructions()) {
|
||||
u32 site{};
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::SubgroupBallot:
|
||||
case IR::Opcode::VoteAll:
|
||||
case IR::Opcode::VoteAny:
|
||||
case IR::Opcode::VoteEqual:
|
||||
site = WARP_BALLOT_WORDS;
|
||||
break;
|
||||
case IR::Opcode::ShuffleIndex: {
|
||||
const IR::Value lane{inst.Arg(1).Resolve()};
|
||||
const IR::Value segment{inst.Arg(3).Resolve()};
|
||||
site = lane.IsImmediate() && segment.IsImmediate() && segment.U32() == 0 ? WARP_BROADCAST_WORDS
|
||||
: WARP_SHUFFLE_WORDS;
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::ShuffleUp:
|
||||
case IR::Opcode::ShuffleDown:
|
||||
case IR::Opcode::ShuffleButterfly:
|
||||
site = WARP_SHUFFLE_WORDS;
|
||||
break;
|
||||
case IR::Opcode::QuadBroadcast:
|
||||
case IR::Opcode::QuadSwap:
|
||||
case IR::Opcode::FSwizzleAdd:
|
||||
case IR::Opcode::MatrixMulAddF16:
|
||||
case IR::Opcode::SubgroupBarrier:
|
||||
case IR::Opcode::IsbeReadOutput:
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!site) {
|
||||
continue;
|
||||
}
|
||||
if (loops) {
|
||||
return;
|
||||
}
|
||||
words += site;
|
||||
regions.insert(open.begin(), open.end());
|
||||
}
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::If:
|
||||
open.push_back(node.data.if_node.body);
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::EndIf:
|
||||
if (open.empty()) {
|
||||
return;
|
||||
}
|
||||
open.pop_back();
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Loop:
|
||||
++loops;
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Repeat:
|
||||
if (!loops) {
|
||||
return;
|
||||
}
|
||||
--loops;
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Break:
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Return:
|
||||
case IR::AbstractSyntaxNode::Type::Unreachable:
|
||||
if (!open.empty() || loops) {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!open.empty() || loops) {
|
||||
return;
|
||||
}
|
||||
warp_shared = true;
|
||||
warp_shared_check = mesh_fallback_shared_warp_check;
|
||||
warp_region_ifs = std::move(regions);
|
||||
warp_word_count = (std::max)(words + WARP_REGION_WORDS * static_cast<u32>(warp_region_ifs.size()), 1U);
|
||||
}
|
||||
|
||||
/// (Triforce, mma_layout.h mesh_fallback_shared_warp) The shared warp's words and the function that waits on them, for a stage
|
||||
/// PlanSharedWarp took
|
||||
void EmitContext::DefineSharedWarp() {
|
||||
if (!warp_shared) {
|
||||
return;
|
||||
}
|
||||
warp_word_pointer = TypePointer(spv::StorageClass::Workgroup, U32[1]);
|
||||
// (its length a signed constant: Sirit gives back the type already made for the same words, and u32 arrays of that
|
||||
// length made for a block (ResolutionInfo's u32[4]) carry an ArrayStride, which a Workgroup variable must not have)
|
||||
warp_words = DefineVariable(*this, TypeArray(U32[1], SConst(static_cast<s32>(warp_word_count))), std::nullopt,
|
||||
spv::StorageClass::Workgroup);
|
||||
Name(warp_words, "warp_words");
|
||||
// (the wait: until every one of `lanes` has its bit in the word. The lanes waited for are the region's, which reach the
|
||||
// site as this one did; bounded all the same, as the pool's wait is, and a wait that gives up is counted past the records
|
||||
// (emit_spirv.h WARP_COUNTER_WAITS: the host logs it, vk_draw.cpp fallback_warp_counters), since the ballot or shuffle
|
||||
// after it may be wrong. A function: a block that calls it keeps its label, which later blocks' phis name)
|
||||
const Id function_type{TypeFunction(void_id, U32[1], U32[1])};
|
||||
warp_wait = OpFunction(void_id, spv::FunctionControlMask::MaskNone, function_type);
|
||||
Name(warp_wait, "warp_wait");
|
||||
const Id word{OpFunctionParameter(U32[1])};
|
||||
const Id lanes{OpFunctionParameter(U32[1])};
|
||||
AddLabel();
|
||||
const Id waited{AddLocalVariable(TypePointer(spv::StorageClass::Function, U32[1]), spv::StorageClass::Function)};
|
||||
const Id pointer{OpAccessChain(warp_word_pointer, warp_words, word)};
|
||||
OpStore(waited, u32_zero_value);
|
||||
const Id header{OpLabel()};
|
||||
const Id body{OpLabel()};
|
||||
const Id next{OpLabel()};
|
||||
const Id done{OpLabel()};
|
||||
OpBranch(header);
|
||||
AddLabel(header);
|
||||
OpLoopMerge(done, next, spv::LoopControlMask::DontUnroll);
|
||||
OpBranch(body);
|
||||
AddLabel(body);
|
||||
const Id seen{OpAtomicLoad(U32[1], pointer, Const(static_cast<u32>(spv::Scope::Workgroup)), u32_zero_value)};
|
||||
const Id count{OpLoad(U32[1], waited)};
|
||||
const Id go{OpLogicalOr(U1, OpIEqual(U1, OpBitwiseAnd(U32[1], seen, lanes), lanes),
|
||||
OpUGreaterThanEqual(U1, count, Const(WARP_WAIT)))};
|
||||
OpBranchConditional(go, done, next);
|
||||
AddLabel(next);
|
||||
OpStore(waited, OpIAdd(U32[1], count, Const(1U)));
|
||||
OpBranch(header);
|
||||
AddLabel(done);
|
||||
const Id gave_up{OpUGreaterThanEqual(U1, OpLoad(U32[1], waited), Const(WARP_WAIT))};
|
||||
const Id counted{OpLabel()};
|
||||
const Id end{OpLabel()};
|
||||
OpSelectionMerge(end, spv::SelectionControlMask::MaskNone);
|
||||
OpBranchConditional(gave_up, counted, end);
|
||||
AddLabel(counted);
|
||||
OpAtomicIAdd(U32[1], MeshFallbackWarpCounter(WARP_COUNTER_WAITS), Const(static_cast<u32>(spv::Scope::Device)), u32_zero_value,
|
||||
Const(1U));
|
||||
OpBranch(end);
|
||||
AddLabel(end);
|
||||
OpReturn();
|
||||
OpFunctionEnd();
|
||||
}
|
||||
|
||||
Id EmitContext::MeshFallbackWarpCounter(u32 counter) {
|
||||
const Id records{OpLoad(U64, OpAccessChain(TypePointer(spv::StorageClass::PushConstant, U64), rescaling_push_constants,
|
||||
Const(mesh_fallback_records_member_index)))};
|
||||
return MeshFallbackWord(OpIAdd(U64, records, Constant(U64, MeshFallbackRecordsBytes() + counter)));
|
||||
}
|
||||
|
||||
void EmitContext::DefineMesh(const IR::Program& program) {
|
||||
if (stage != Stage::Task && stage != Stage::Mesh) {
|
||||
return;
|
||||
@@ -2280,6 +2673,7 @@ void EmitContext::DefineMesh(const IR::Program& program) {
|
||||
}
|
||||
mesh_info = program.mesh;
|
||||
mesh_threads = program.workgroup_size[0];
|
||||
DefineSharedWarp();
|
||||
// The task's output region, which its mesh workgroups read as their input region.
|
||||
const Id payload_type{TypeArray(U32[1], Const(mesh_info.payload_words))};
|
||||
if (mesh_fallback) {
|
||||
@@ -2318,9 +2712,15 @@ void EmitContext::DefineMesh(const IR::Program& program) {
|
||||
// triangles over Kokiri's foliage and ground (as nxnative found: "primitive index bytes for every primitive block")
|
||||
mesh_map_bytes = 4 + mesh_info.Corners() * Common::DivCeil(mesh_info.max_primitives, 32U) * 32;
|
||||
mesh_shared_u32 = TypePointer(spv::StorageClass::Workgroup, U32[1]);
|
||||
mesh_attributes = DefineVariable(*this, TypeArray(U32[1], Const(mesh_attribute_words)),
|
||||
std::nullopt, spv::StorageClass::Workgroup);
|
||||
Name(mesh_attributes, "isbe_attributes");
|
||||
// (Triforce, mma_layout.h mesh_fallback_global_attributes) the region in a slot of the records buffer's pool, where it fits one
|
||||
const u32 slot_bytes{(mesh_attribute_words * 4 + 255) & ~255U};
|
||||
mesh_attributes_global = mesh_fallback && stage == Stage::Mesh && mesh_fallback_global_attributes &&
|
||||
slot_bytes <= MESH_FALLBACK_POOL_SLOT_BYTES;
|
||||
if (!mesh_attributes_global) {
|
||||
mesh_attributes = DefineVariable(*this, TypeArray(U32[1], Const(mesh_attribute_words)),
|
||||
std::nullopt, spv::StorageClass::Workgroup);
|
||||
Name(mesh_attributes, "isbe_attributes");
|
||||
}
|
||||
mesh_map = DefineVariable(*this, TypeArray(U32[1], Const(mesh_map_bytes)), std::nullopt,
|
||||
spv::StorageClass::Workgroup);
|
||||
Name(mesh_map, "isbe_map");
|
||||
@@ -2328,6 +2728,73 @@ void EmitContext::DefineMesh(const IR::Program& program) {
|
||||
DefineVariable(*this, U32[1], std::nullopt, spv::StorageClass::Workgroup);
|
||||
Name(mesh_primitive_count, "isbe_primitive_count");
|
||||
|
||||
if (mesh_attributes_global) {
|
||||
mesh_attribute_slot_bytes = slot_bytes;
|
||||
mesh_attribute_ticket_shared = DefineVariable(*this, U32[1], std::nullopt, spv::StorageClass::Workgroup);
|
||||
Name(mesh_attribute_ticket_shared, "isbe_attribute_ticket");
|
||||
// (thread 0 takes the next ticket, waits until the slot's holder a pool's length of tickets before has handed it on, and
|
||||
// leaves the ticket in the workgroup word. In a function of its own: the prologue must not branch, its block is a phi
|
||||
// predecessor of later ones. The holder started before, so it runs and hands the slot on; the wait is bounded all the
|
||||
// same, as a hung GPU is worse than a wrong region: MESH_FALLBACK_POOL_WAIT reads. A wait that gives up shares the
|
||||
// slot with its last holder, so it is counted past the records (emit_spirv.h WARP_COUNTER_POOL_WAITS: the host logs
|
||||
// it, vk_draw.cpp fallback_warp_counters))
|
||||
const Id function_type{TypeFunction(void_id, U32[1])};
|
||||
mesh_attribute_claim = OpFunction(void_id, spv::FunctionControlMask::MaskNone, function_type);
|
||||
Name(mesh_attribute_claim, "isbe_attribute_claim");
|
||||
const Id local{OpFunctionParameter(U32[1])};
|
||||
AddLabel();
|
||||
const Id waited{AddLocalVariable(TypePointer(spv::StorageClass::Function, U32[1]), spv::StorageClass::Function)};
|
||||
const Id records{OpLoad(U64, OpAccessChain(TypePointer(spv::StorageClass::PushConstant, U64), rescaling_push_constants,
|
||||
Const(mesh_fallback_records_member_index)))};
|
||||
const Id pool{OpIAdd(U64, records, Constant(U64, u64{MESH_FALLBACK_POOL_OFFSET}))};
|
||||
const auto pool_word = [&](Id word) {
|
||||
return MeshFallbackWord(OpIAdd(U64, pool, OpUConvert(U64, OpShiftLeftLogical(U32[1], word, Const(2U)))));
|
||||
};
|
||||
const Id device{Const(static_cast<u32>(spv::Scope::Device))};
|
||||
const Id first_label{OpLabel()};
|
||||
const Id end_label{OpLabel()};
|
||||
const Id is_first{OpIEqual(U1, local, u32_zero_value)}; // (before the merge: it must stand right before its branch)
|
||||
OpSelectionMerge(end_label, spv::SelectionControlMask::MaskNone);
|
||||
OpBranchConditional(is_first, first_label, end_label);
|
||||
AddLabel(first_label);
|
||||
const Id ticket{OpAtomicIAdd(U32[1], pool_word(u32_zero_value), device, u32_zero_value, Const(1U))};
|
||||
const Id slot{OpBitwiseAnd(U32[1], ticket, Const(MESH_FALLBACK_POOL_SLOTS - 1))};
|
||||
const Id generation{OpShiftRightLogical(U32[1], ticket, Const(MESH_FALLBACK_POOL_SLOT_SHIFT))};
|
||||
const Id slot_word{pool_word(OpIAdd(U32[1], slot, Const(1U)))};
|
||||
OpStore(waited, u32_zero_value);
|
||||
const Id header{OpLabel()};
|
||||
const Id body{OpLabel()};
|
||||
const Id next{OpLabel()};
|
||||
const Id done{OpLabel()};
|
||||
OpBranch(header);
|
||||
AddLabel(header);
|
||||
OpLoopMerge(done, next, spv::LoopControlMask::DontUnroll);
|
||||
OpBranch(body);
|
||||
AddLabel(body);
|
||||
const Id seen{OpAtomicLoad(U32[1], slot_word, device, u32_zero_value)};
|
||||
const Id count{OpLoad(U32[1], waited)};
|
||||
const Id go{OpLogicalOr(U1, OpIEqual(U1, seen, generation), OpUGreaterThanEqual(U1, count, Const(MESH_FALLBACK_POOL_WAIT)))};
|
||||
OpBranchConditional(go, done, next);
|
||||
AddLabel(next);
|
||||
OpStore(waited, OpIAdd(U32[1], count, Const(1U)));
|
||||
OpBranch(header);
|
||||
AddLabel(done);
|
||||
const Id gave_up{OpUGreaterThanEqual(U1, OpLoad(U32[1], waited), Const(MESH_FALLBACK_POOL_WAIT))};
|
||||
const Id counted{OpLabel()};
|
||||
const Id told{OpLabel()};
|
||||
OpSelectionMerge(told, spv::SelectionControlMask::MaskNone);
|
||||
OpBranchConditional(gave_up, counted, told);
|
||||
AddLabel(counted);
|
||||
OpAtomicIAdd(U32[1], MeshFallbackWarpCounter(WARP_COUNTER_POOL_WAITS), device, u32_zero_value, Const(1U));
|
||||
OpBranch(told);
|
||||
AddLabel(told);
|
||||
OpStore(mesh_attribute_ticket_shared, ticket);
|
||||
OpBranch(end_label);
|
||||
AddLabel(end_label);
|
||||
OpReturn();
|
||||
OpFunctionEnd();
|
||||
}
|
||||
|
||||
if (mesh_fallback) {
|
||||
return; // (no mesh outputs here: the epilogue writes records)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <array>
|
||||
|
||||
#include <sirit/sirit.h>
|
||||
@@ -45,6 +47,7 @@ struct TextureDefinition {
|
||||
|
||||
struct TextureBufferDefinition {
|
||||
Id id;
|
||||
Id pointer_type;
|
||||
u32 count;
|
||||
};
|
||||
|
||||
@@ -261,13 +264,15 @@ public:
|
||||
std::vector<ImageDefinition> images;
|
||||
|
||||
Id workgroup_id{};
|
||||
Id num_workgroups{};
|
||||
Id local_invocation_id{};
|
||||
Id invocation_id{};
|
||||
Id patch_vertices_in{};
|
||||
Id sample_id{};
|
||||
Id is_helper_invocation{};
|
||||
Id subgroup_local_invocation_id{};
|
||||
Id software_matrix_function{}; // (hookshot: emit_spirv_warp.cpp)
|
||||
Id software_matrix_function{}; // (Triforce: emit_spirv_warp.cpp)
|
||||
Id wmma16_function{}; // (Triforce: emit_spirv_warp.cpp DefineWmma16MatrixFunction)
|
||||
Id subgroup_mask_eq{};
|
||||
Id subgroup_mask_lt{};
|
||||
Id subgroup_mask_le{};
|
||||
@@ -334,6 +339,7 @@ public:
|
||||
Id decrement_cas_shared{};
|
||||
Id decrement_cas_ssbo{};
|
||||
Id f32_add_cas{};
|
||||
bool native_f32_add{}; // (hookshot) the float adds are OpAtomicFAddEXT, not f32_add_cas (DefineStorageBuffers)
|
||||
Id f16x2_add_cas{};
|
||||
Id f16x2_min_cas{};
|
||||
Id f16x2_max_cas{};
|
||||
@@ -402,7 +408,35 @@ public:
|
||||
/// (hookshot) a fallback mesh workgroup's slot (task workgroup * threads + its index), read from the task's map in the
|
||||
/// entry block (EmitMeshPrologue); invalid without a task program, where the slot is the workgroup id
|
||||
Id mesh_fallback_slot{};
|
||||
Id mesh_fallback_epilogue_word{}; ///< (Triforce) the fallback epilogue's loop index, a function variable of the entry block
|
||||
Id mesh_fallback_epilogue_line{}; ///< (Triforce) the Soa records' line, WriteFallbackLines' inner loop index, as above
|
||||
/// (Triforce, mma_layout.h mesh_fallback_global_attributes) the attribute region in a pool slot, not in workgroup memory: the
|
||||
/// slot's bytes, the function that takes a ticket for it (thread 0, into the workgroup word), and in the entry block the
|
||||
/// ticket and the region's device address (EmitMeshPrologue)
|
||||
bool mesh_attributes_global{};
|
||||
u32 mesh_attribute_slot_bytes{};
|
||||
Id mesh_attribute_ticket_shared{};
|
||||
Id mesh_attribute_claim{};
|
||||
Id mesh_attribute_ticket{};
|
||||
Id mesh_attribute_base{};
|
||||
Id mesh_shared_u32{}; ///< pointer type to one workgroup word
|
||||
/// (Triforce, mma_layout.h mesh_fallback_shared_warp) the warp in workgroup memory: the words (a region's entered and
|
||||
/// reached lanes, a ballot's lanes and arrivals, a shuffle's values and arrivals), the function that waits for lanes'
|
||||
/// bits in a word, this invocation's lane and bit (entry block), the lanes that run the block being emitted and those of
|
||||
/// the ifs around it, and the ifs (by their body) with a ballot, vote or shuffle inside (PlanSharedWarp)
|
||||
bool warp_shared{};
|
||||
bool warp_shared_check{};
|
||||
Id warp_words{};
|
||||
Id warp_word_pointer{};
|
||||
Id warp_wait{};
|
||||
Id warp_lane{};
|
||||
Id warp_bit{};
|
||||
Id warp_region{};
|
||||
std::vector<Id> warp_regions;
|
||||
std::vector<bool> warp_region_opened; ///< each open if: whether it opened a region
|
||||
u32 warp_word_count{};
|
||||
u32 warp_word_next{};
|
||||
std::unordered_set<const IR::Block*> warp_region_ifs;
|
||||
Id mesh_primitive_indices{};
|
||||
MeshInfo mesh_info{};
|
||||
u32 mesh_threads{};
|
||||
@@ -438,6 +472,8 @@ public:
|
||||
Id capture_pointer{};
|
||||
/// A pointer to the u32 at a PhysicalStorageBuffer device address
|
||||
Id MeshFallbackWord(Id address);
|
||||
/// (Triforce, mma_layout.h mesh_fallback_shared_warp) a pointer to one of the shared warp's counters past the records
|
||||
Id MeshFallbackWarpCounter(u32 counter);
|
||||
|
||||
Id load_const_func_u8{};
|
||||
Id load_const_func_u16{};
|
||||
@@ -454,6 +490,28 @@ public:
|
||||
bool uses_nonuniform_uniform_texel_buffer{};
|
||||
bool uses_nonuniform_storage_texel_buffer{};
|
||||
|
||||
// (Triforce, AMD RDNA 3/3.5/4) Fused WMMA 16x16x16 N-pair, zero-C, quad-A, and K-chain state.
|
||||
struct Wmma16Step {
|
||||
enum class Kind : u8 { Skip, Single, Pair, PairZeroC, QuadZeroC, PairK2ZeroC };
|
||||
Kind kind{Kind::Single};
|
||||
IR::Inst* m0{};
|
||||
IR::Inst* m1{};
|
||||
IR::Inst* m2{};
|
||||
IR::Inst* m3{};
|
||||
std::vector<IR::Inst*> deferred_extracts;
|
||||
};
|
||||
struct Wmma16FusedState {
|
||||
bool active{};
|
||||
Id pair_fn{};
|
||||
Id pair_zero_c_fn{};
|
||||
Id quad_zero_c_fn{};
|
||||
Id quad_ret_type{};
|
||||
Id pair_k2_zero_c_fn{};
|
||||
std::unordered_map<const IR::Inst*, Wmma16Step> steps;
|
||||
std::unordered_set<const IR::Inst*> skip_insts;
|
||||
};
|
||||
Wmma16FusedState wmma16_fused{};
|
||||
|
||||
private:
|
||||
void DefineCommonTypes(const Info& info);
|
||||
void DefineCommonConstants();
|
||||
@@ -479,6 +537,8 @@ private:
|
||||
void DefineInputs(const IR::Program& program);
|
||||
void DefineOutputs(const IR::Program& program);
|
||||
void DefineMesh(const IR::Program& program);
|
||||
void PlanSharedWarp(const IR::Program& program);
|
||||
void DefineSharedWarp();
|
||||
};
|
||||
|
||||
} // namespace Shader::Backend::SPIRV
|
||||
|
||||
Reference in New Issue
Block a user