mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-20 16:49:57 -04:00
Triforce v0.2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
986
gpu/third_party/suyu/shader_recompiler/ir_opt/collect_shader_info_pass.cpp
vendored
Normal file
986
gpu/third_party/suyu/shader_recompiler/ir_opt/collect_shader_info_pass.cpp
vendored
Normal file
@@ -0,0 +1,986 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "common/alignment.h"
|
||||
#include "shader_recompiler/environment.h"
|
||||
#include "shader_recompiler/frontend/ir/modifiers.h"
|
||||
#include "shader_recompiler/frontend/ir/program.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
#include "shader_recompiler/shader_info.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
namespace {
|
||||
void AddConstantBufferDescriptor(Info& info, u32 index, u32 count) {
|
||||
if (count != 1) {
|
||||
throw NotImplementedException("Constant buffer descriptor indexing");
|
||||
}
|
||||
if ((info.constant_buffer_mask & (1U << index)) != 0) {
|
||||
return;
|
||||
}
|
||||
info.constant_buffer_mask |= 1U << index;
|
||||
|
||||
auto& cbufs{info.constant_buffer_descriptors};
|
||||
cbufs.insert(std::ranges::lower_bound(cbufs, index, {}, &ConstantBufferDescriptor::index),
|
||||
ConstantBufferDescriptor{
|
||||
.index = index,
|
||||
.count = 1,
|
||||
});
|
||||
}
|
||||
|
||||
void AddRegisterIndexedLdc(Info& info) {
|
||||
info.uses_cbuf_indirect = true;
|
||||
|
||||
for (u32 i = 0; i < Info::MAX_INDIRECT_CBUFS; i++) {
|
||||
AddConstantBufferDescriptor(info, i, 1);
|
||||
|
||||
// The shader can use any possible access size
|
||||
info.constant_buffer_used_sizes[i] = 0x10'000;
|
||||
}
|
||||
}
|
||||
|
||||
u32 GetElementSize(IR::Type& used_type, Shader::IR::Opcode opcode) {
|
||||
switch (opcode) {
|
||||
case IR::Opcode::GetCbufU8:
|
||||
case IR::Opcode::GetCbufS8:
|
||||
used_type |= IR::Type::U8;
|
||||
return 1;
|
||||
case IR::Opcode::GetCbufU16:
|
||||
case IR::Opcode::GetCbufS16:
|
||||
used_type |= IR::Type::U16;
|
||||
return 2;
|
||||
case IR::Opcode::GetCbufU32:
|
||||
used_type |= IR::Type::U32;
|
||||
return 4;
|
||||
case IR::Opcode::GetCbufF32:
|
||||
used_type |= IR::Type::F32;
|
||||
return 4;
|
||||
case IR::Opcode::GetCbufU32x2:
|
||||
used_type |= IR::Type::U32x2;
|
||||
return 8;
|
||||
default:
|
||||
throw InvalidArgument("Invalid opcode {}", opcode);
|
||||
}
|
||||
}
|
||||
|
||||
void GetPatch(Info& info, IR::Patch patch) {
|
||||
if (!IR::IsGeneric(patch)) {
|
||||
throw NotImplementedException("Reading non-generic patch {}", patch);
|
||||
}
|
||||
info.uses_patches.at(IR::GenericPatchIndex(patch)) = true;
|
||||
}
|
||||
|
||||
void SetPatch(Info& info, IR::Patch patch) {
|
||||
if (IR::IsGeneric(patch)) {
|
||||
info.uses_patches.at(IR::GenericPatchIndex(patch)) = true;
|
||||
return;
|
||||
}
|
||||
switch (patch) {
|
||||
case IR::Patch::TessellationLodLeft:
|
||||
case IR::Patch::TessellationLodTop:
|
||||
case IR::Patch::TessellationLodRight:
|
||||
case IR::Patch::TessellationLodBottom:
|
||||
info.stores_tess_level_outer = true;
|
||||
break;
|
||||
case IR::Patch::TessellationLodInteriorU:
|
||||
case IR::Patch::TessellationLodInteriorV:
|
||||
info.stores_tess_level_inner = true;
|
||||
break;
|
||||
default:
|
||||
throw NotImplementedException("Set patch {}", patch);
|
||||
}
|
||||
}
|
||||
|
||||
void CheckCBufNVN(Info& info, IR::Inst& inst) {
|
||||
const IR::Value cbuf_index{inst.Arg(0)};
|
||||
if (!cbuf_index.IsImmediate()) {
|
||||
info.nvn_buffer_used.set();
|
||||
return;
|
||||
}
|
||||
const u32 index{cbuf_index.U32()};
|
||||
if (index != 0) {
|
||||
return;
|
||||
}
|
||||
const IR::Value cbuf_offset{inst.Arg(1)};
|
||||
if (!cbuf_offset.IsImmediate()) {
|
||||
info.nvn_buffer_used.set();
|
||||
return;
|
||||
}
|
||||
const u32 offset{cbuf_offset.U32()};
|
||||
const u32 descriptor_size{0x10};
|
||||
const u32 upper_limit{info.nvn_buffer_base + descriptor_size * 16};
|
||||
if (offset >= info.nvn_buffer_base && offset < upper_limit) {
|
||||
const std::size_t nvn_index{(offset - info.nvn_buffer_base) / descriptor_size};
|
||||
info.nvn_buffer_used.set(nvn_index, true);
|
||||
}
|
||||
}
|
||||
|
||||
void VisitUsages(Info& info, IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::CompositeConstructF16x2:
|
||||
case IR::Opcode::CompositeConstructF16x3:
|
||||
case IR::Opcode::CompositeConstructF16x4:
|
||||
case IR::Opcode::CompositeExtractF16x2:
|
||||
case IR::Opcode::CompositeExtractF16x3:
|
||||
case IR::Opcode::CompositeExtractF16x4:
|
||||
case IR::Opcode::CompositeInsertF16x2:
|
||||
case IR::Opcode::CompositeInsertF16x3:
|
||||
case IR::Opcode::CompositeInsertF16x4:
|
||||
case IR::Opcode::SelectF16:
|
||||
case IR::Opcode::BitCastU16F16:
|
||||
case IR::Opcode::BitCastF16U16:
|
||||
case IR::Opcode::PackFloat2x16:
|
||||
case IR::Opcode::UnpackFloat2x16:
|
||||
case IR::Opcode::ConvertS16F16:
|
||||
case IR::Opcode::ConvertS32F16:
|
||||
case IR::Opcode::ConvertS64F16:
|
||||
case IR::Opcode::ConvertU16F16:
|
||||
case IR::Opcode::ConvertU32F16:
|
||||
case IR::Opcode::ConvertU64F16:
|
||||
case IR::Opcode::ConvertF16S8:
|
||||
case IR::Opcode::ConvertF16S16:
|
||||
case IR::Opcode::ConvertF16S32:
|
||||
case IR::Opcode::ConvertF16S64:
|
||||
case IR::Opcode::ConvertF16U8:
|
||||
case IR::Opcode::ConvertF16U16:
|
||||
case IR::Opcode::ConvertF16U32:
|
||||
case IR::Opcode::ConvertF16U64:
|
||||
case IR::Opcode::ConvertF16F32:
|
||||
case IR::Opcode::ConvertF32F16:
|
||||
case IR::Opcode::FPAbs16:
|
||||
case IR::Opcode::FPAdd16:
|
||||
case IR::Opcode::FPCeil16:
|
||||
case IR::Opcode::FPFloor16:
|
||||
case IR::Opcode::FPFma16:
|
||||
case IR::Opcode::FPMul16:
|
||||
case IR::Opcode::FPNeg16:
|
||||
case IR::Opcode::FPRoundEven16:
|
||||
case IR::Opcode::FPSaturate16:
|
||||
case IR::Opcode::FPClamp16:
|
||||
case IR::Opcode::FPTrunc16:
|
||||
case IR::Opcode::FPOrdEqual16:
|
||||
case IR::Opcode::FPUnordEqual16:
|
||||
case IR::Opcode::FPOrdNotEqual16:
|
||||
case IR::Opcode::FPUnordNotEqual16:
|
||||
case IR::Opcode::FPOrdLessThan16:
|
||||
case IR::Opcode::FPUnordLessThan16:
|
||||
case IR::Opcode::FPOrdGreaterThan16:
|
||||
case IR::Opcode::FPUnordGreaterThan16:
|
||||
case IR::Opcode::FPOrdLessThanEqual16:
|
||||
case IR::Opcode::FPUnordLessThanEqual16:
|
||||
case IR::Opcode::FPOrdGreaterThanEqual16:
|
||||
case IR::Opcode::FPUnordGreaterThanEqual16:
|
||||
case IR::Opcode::FPIsNan16:
|
||||
case IR::Opcode::GlobalAtomicAddF16x2:
|
||||
case IR::Opcode::GlobalAtomicMinF16x2:
|
||||
case IR::Opcode::GlobalAtomicMaxF16x2:
|
||||
case IR::Opcode::StorageAtomicAddF16x2:
|
||||
case IR::Opcode::StorageAtomicMinF16x2:
|
||||
case IR::Opcode::StorageAtomicMaxF16x2:
|
||||
info.uses_fp16 = true;
|
||||
break;
|
||||
case IR::Opcode::CompositeConstructF64x2:
|
||||
case IR::Opcode::CompositeConstructF64x3:
|
||||
case IR::Opcode::CompositeConstructF64x4:
|
||||
case IR::Opcode::CompositeExtractF64x2:
|
||||
case IR::Opcode::CompositeExtractF64x3:
|
||||
case IR::Opcode::CompositeExtractF64x4:
|
||||
case IR::Opcode::CompositeInsertF64x2:
|
||||
case IR::Opcode::CompositeInsertF64x3:
|
||||
case IR::Opcode::CompositeInsertF64x4:
|
||||
case IR::Opcode::SelectF64:
|
||||
case IR::Opcode::BitCastU64F64:
|
||||
case IR::Opcode::BitCastF64U64:
|
||||
case IR::Opcode::PackDouble2x32:
|
||||
case IR::Opcode::UnpackDouble2x32:
|
||||
case IR::Opcode::FPAbs64:
|
||||
case IR::Opcode::FPAdd64:
|
||||
case IR::Opcode::FPCeil64:
|
||||
case IR::Opcode::FPFloor64:
|
||||
case IR::Opcode::FPFma64:
|
||||
case IR::Opcode::FPMax64:
|
||||
case IR::Opcode::FPMin64:
|
||||
case IR::Opcode::FPMul64:
|
||||
case IR::Opcode::FPNeg64:
|
||||
case IR::Opcode::FPRecip64:
|
||||
case IR::Opcode::FPRecipSqrt64:
|
||||
case IR::Opcode::FPRoundEven64:
|
||||
case IR::Opcode::FPSaturate64:
|
||||
case IR::Opcode::FPClamp64:
|
||||
case IR::Opcode::FPTrunc64:
|
||||
case IR::Opcode::FPOrdEqual64:
|
||||
case IR::Opcode::FPUnordEqual64:
|
||||
case IR::Opcode::FPOrdNotEqual64:
|
||||
case IR::Opcode::FPUnordNotEqual64:
|
||||
case IR::Opcode::FPOrdLessThan64:
|
||||
case IR::Opcode::FPUnordLessThan64:
|
||||
case IR::Opcode::FPOrdGreaterThan64:
|
||||
case IR::Opcode::FPUnordGreaterThan64:
|
||||
case IR::Opcode::FPOrdLessThanEqual64:
|
||||
case IR::Opcode::FPUnordLessThanEqual64:
|
||||
case IR::Opcode::FPOrdGreaterThanEqual64:
|
||||
case IR::Opcode::FPUnordGreaterThanEqual64:
|
||||
case IR::Opcode::FPIsNan64:
|
||||
case IR::Opcode::ConvertS16F64:
|
||||
case IR::Opcode::ConvertS32F64:
|
||||
case IR::Opcode::ConvertS64F64:
|
||||
case IR::Opcode::ConvertU16F64:
|
||||
case IR::Opcode::ConvertU32F64:
|
||||
case IR::Opcode::ConvertU64F64:
|
||||
case IR::Opcode::ConvertF32F64:
|
||||
case IR::Opcode::ConvertF64F32:
|
||||
case IR::Opcode::ConvertF64S8:
|
||||
case IR::Opcode::ConvertF64S16:
|
||||
case IR::Opcode::ConvertF64S32:
|
||||
case IR::Opcode::ConvertF64S64:
|
||||
case IR::Opcode::ConvertF64U8:
|
||||
case IR::Opcode::ConvertF64U16:
|
||||
case IR::Opcode::ConvertF64U32:
|
||||
case IR::Opcode::ConvertF64U64:
|
||||
info.uses_fp64 = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::GetCbufU8:
|
||||
case IR::Opcode::GetCbufS8:
|
||||
case IR::Opcode::UndefU8:
|
||||
case IR::Opcode::LoadGlobalU8:
|
||||
case IR::Opcode::LoadGlobalS8:
|
||||
case IR::Opcode::WriteGlobalU8:
|
||||
case IR::Opcode::WriteGlobalS8:
|
||||
case IR::Opcode::LoadStorageU8:
|
||||
case IR::Opcode::LoadStorageS8:
|
||||
case IR::Opcode::WriteStorageU8:
|
||||
case IR::Opcode::WriteStorageS8:
|
||||
case IR::Opcode::LoadSharedU8:
|
||||
case IR::Opcode::LoadSharedS8:
|
||||
case IR::Opcode::WriteSharedU8:
|
||||
case IR::Opcode::SelectU8:
|
||||
case IR::Opcode::ConvertF16S8:
|
||||
case IR::Opcode::ConvertF16U8:
|
||||
case IR::Opcode::ConvertF32S8:
|
||||
case IR::Opcode::ConvertF32U8:
|
||||
case IR::Opcode::ConvertF64S8:
|
||||
case IR::Opcode::ConvertF64U8:
|
||||
info.uses_int8 = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::GetCbufU16:
|
||||
case IR::Opcode::GetCbufS16:
|
||||
case IR::Opcode::UndefU16:
|
||||
case IR::Opcode::LoadGlobalU16:
|
||||
case IR::Opcode::LoadGlobalS16:
|
||||
case IR::Opcode::WriteGlobalU16:
|
||||
case IR::Opcode::WriteGlobalS16:
|
||||
case IR::Opcode::LoadStorageU16:
|
||||
case IR::Opcode::LoadStorageS16:
|
||||
case IR::Opcode::WriteStorageU16:
|
||||
case IR::Opcode::WriteStorageS16:
|
||||
case IR::Opcode::LoadSharedU16:
|
||||
case IR::Opcode::LoadSharedS16:
|
||||
case IR::Opcode::WriteSharedU16:
|
||||
case IR::Opcode::SelectU16:
|
||||
case IR::Opcode::BitCastU16F16:
|
||||
case IR::Opcode::BitCastF16U16:
|
||||
case IR::Opcode::ConvertS16F16:
|
||||
case IR::Opcode::ConvertS16F32:
|
||||
case IR::Opcode::ConvertS16F64:
|
||||
case IR::Opcode::ConvertU16F16:
|
||||
case IR::Opcode::ConvertU16F32:
|
||||
case IR::Opcode::ConvertU16F64:
|
||||
case IR::Opcode::ConvertF16S16:
|
||||
case IR::Opcode::ConvertF16U16:
|
||||
case IR::Opcode::ConvertF32S16:
|
||||
case IR::Opcode::ConvertF32U16:
|
||||
case IR::Opcode::ConvertF64S16:
|
||||
case IR::Opcode::ConvertF64U16:
|
||||
info.uses_int16 = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::UndefU64:
|
||||
case IR::Opcode::LoadGlobalU8:
|
||||
case IR::Opcode::LoadGlobalS8:
|
||||
case IR::Opcode::LoadGlobalU16:
|
||||
case IR::Opcode::LoadGlobalS16:
|
||||
case IR::Opcode::LoadGlobal32:
|
||||
case IR::Opcode::LoadGlobal64:
|
||||
case IR::Opcode::LoadGlobal128:
|
||||
case IR::Opcode::WriteGlobalU8:
|
||||
case IR::Opcode::WriteGlobalS8:
|
||||
case IR::Opcode::WriteGlobalU16:
|
||||
case IR::Opcode::WriteGlobalS16:
|
||||
case IR::Opcode::WriteGlobal32:
|
||||
case IR::Opcode::WriteGlobal64:
|
||||
case IR::Opcode::WriteGlobal128:
|
||||
case IR::Opcode::SelectU64:
|
||||
case IR::Opcode::BitCastU64F64:
|
||||
case IR::Opcode::BitCastF64U64:
|
||||
case IR::Opcode::PackUint2x32:
|
||||
case IR::Opcode::UnpackUint2x32:
|
||||
case IR::Opcode::IAdd64:
|
||||
case IR::Opcode::ISub64:
|
||||
case IR::Opcode::INeg64:
|
||||
case IR::Opcode::ShiftLeftLogical64:
|
||||
case IR::Opcode::ShiftRightLogical64:
|
||||
case IR::Opcode::ShiftRightArithmetic64:
|
||||
case IR::Opcode::ConvertS64F16:
|
||||
case IR::Opcode::ConvertS64F32:
|
||||
case IR::Opcode::ConvertS64F64:
|
||||
case IR::Opcode::ConvertU64F16:
|
||||
case IR::Opcode::ConvertU64F32:
|
||||
case IR::Opcode::ConvertU64F64:
|
||||
case IR::Opcode::ConvertU64U32:
|
||||
case IR::Opcode::ConvertU32U64:
|
||||
case IR::Opcode::ConvertF16U64:
|
||||
case IR::Opcode::ConvertF32U64:
|
||||
case IR::Opcode::ConvertF64U64:
|
||||
case IR::Opcode::SharedAtomicExchange64:
|
||||
case IR::Opcode::GlobalAtomicIAdd64:
|
||||
case IR::Opcode::GlobalAtomicSMin64:
|
||||
case IR::Opcode::GlobalAtomicUMin64:
|
||||
case IR::Opcode::GlobalAtomicSMax64:
|
||||
case IR::Opcode::GlobalAtomicUMax64:
|
||||
case IR::Opcode::GlobalAtomicAnd64:
|
||||
case IR::Opcode::GlobalAtomicOr64:
|
||||
case IR::Opcode::GlobalAtomicXor64:
|
||||
case IR::Opcode::GlobalAtomicExchange64:
|
||||
case IR::Opcode::StorageAtomicIAdd64:
|
||||
case IR::Opcode::StorageAtomicSMin64:
|
||||
case IR::Opcode::StorageAtomicUMin64:
|
||||
case IR::Opcode::StorageAtomicSMax64:
|
||||
case IR::Opcode::StorageAtomicUMax64:
|
||||
case IR::Opcode::StorageAtomicAnd64:
|
||||
case IR::Opcode::StorageAtomicOr64:
|
||||
case IR::Opcode::StorageAtomicXor64:
|
||||
case IR::Opcode::StorageAtomicExchange64:
|
||||
info.uses_int64 = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::WriteGlobalU8:
|
||||
case IR::Opcode::WriteGlobalS8:
|
||||
case IR::Opcode::WriteGlobalU16:
|
||||
case IR::Opcode::WriteGlobalS16:
|
||||
case IR::Opcode::WriteGlobal32:
|
||||
case IR::Opcode::WriteGlobal64:
|
||||
case IR::Opcode::WriteGlobal128:
|
||||
case IR::Opcode::GlobalAtomicIAdd32:
|
||||
case IR::Opcode::GlobalAtomicSMin32:
|
||||
case IR::Opcode::GlobalAtomicUMin32:
|
||||
case IR::Opcode::GlobalAtomicSMax32:
|
||||
case IR::Opcode::GlobalAtomicUMax32:
|
||||
case IR::Opcode::GlobalAtomicInc32:
|
||||
case IR::Opcode::GlobalAtomicDec32:
|
||||
case IR::Opcode::GlobalAtomicAnd32:
|
||||
case IR::Opcode::GlobalAtomicOr32:
|
||||
case IR::Opcode::GlobalAtomicXor32:
|
||||
case IR::Opcode::GlobalAtomicExchange32:
|
||||
case IR::Opcode::GlobalAtomicIAdd64:
|
||||
case IR::Opcode::GlobalAtomicSMin64:
|
||||
case IR::Opcode::GlobalAtomicUMin64:
|
||||
case IR::Opcode::GlobalAtomicSMax64:
|
||||
case IR::Opcode::GlobalAtomicUMax64:
|
||||
case IR::Opcode::GlobalAtomicAnd64:
|
||||
case IR::Opcode::GlobalAtomicOr64:
|
||||
case IR::Opcode::GlobalAtomicXor64:
|
||||
case IR::Opcode::GlobalAtomicExchange64:
|
||||
case IR::Opcode::GlobalAtomicIAdd32x2:
|
||||
case IR::Opcode::GlobalAtomicSMin32x2:
|
||||
case IR::Opcode::GlobalAtomicUMin32x2:
|
||||
case IR::Opcode::GlobalAtomicSMax32x2:
|
||||
case IR::Opcode::GlobalAtomicUMax32x2:
|
||||
case IR::Opcode::GlobalAtomicAnd32x2:
|
||||
case IR::Opcode::GlobalAtomicOr32x2:
|
||||
case IR::Opcode::GlobalAtomicXor32x2:
|
||||
case IR::Opcode::GlobalAtomicExchange32x2:
|
||||
case IR::Opcode::GlobalAtomicAddF32:
|
||||
case IR::Opcode::GlobalAtomicAddF16x2:
|
||||
case IR::Opcode::GlobalAtomicAddF32x2:
|
||||
case IR::Opcode::GlobalAtomicMinF16x2:
|
||||
case IR::Opcode::GlobalAtomicMinF32x2:
|
||||
case IR::Opcode::GlobalAtomicMaxF16x2:
|
||||
case IR::Opcode::GlobalAtomicMaxF32x2:
|
||||
info.stores_global_memory = true;
|
||||
[[fallthrough]];
|
||||
case IR::Opcode::LoadGlobalU8:
|
||||
case IR::Opcode::LoadGlobalS8:
|
||||
case IR::Opcode::LoadGlobalU16:
|
||||
case IR::Opcode::LoadGlobalS16:
|
||||
case IR::Opcode::LoadGlobal32:
|
||||
case IR::Opcode::LoadGlobal64:
|
||||
case IR::Opcode::LoadGlobal128:
|
||||
info.uses_int64 = true;
|
||||
info.uses_global_memory = true;
|
||||
info.used_constant_buffer_types |= IR::Type::U32 | IR::Type::U32x2;
|
||||
info.used_storage_buffer_types |= IR::Type::U32 | IR::Type::U32x2 | IR::Type::U32x4;
|
||||
break;
|
||||
case IR::Opcode::LoadLocal:
|
||||
case IR::Opcode::WriteLocal:
|
||||
info.uses_local_memory = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::DemoteToHelperInvocation:
|
||||
info.uses_demote_to_helper_invocation = true;
|
||||
break;
|
||||
case IR::Opcode::GetAttribute:
|
||||
case IR::Opcode::GetAttributeU32:
|
||||
info.loads.mask[static_cast<size_t>(inst.Arg(0).Attribute())] = true;
|
||||
break;
|
||||
case IR::Opcode::SetAttribute:
|
||||
info.stores.mask[static_cast<size_t>(inst.Arg(0).Attribute())] = true;
|
||||
break;
|
||||
case IR::Opcode::GetPatch:
|
||||
GetPatch(info, inst.Arg(0).Patch());
|
||||
break;
|
||||
case IR::Opcode::SetPatch:
|
||||
SetPatch(info, inst.Arg(0).Patch());
|
||||
break;
|
||||
case IR::Opcode::GetAttributeIndexed:
|
||||
info.loads_indexed_attributes = true;
|
||||
break;
|
||||
case IR::Opcode::SetAttributeIndexed:
|
||||
info.stores_indexed_attributes = true;
|
||||
break;
|
||||
case IR::Opcode::SetFragColor:
|
||||
info.stores_frag_color[inst.Arg(0).U32()] = true;
|
||||
break;
|
||||
case IR::Opcode::SetSampleMask:
|
||||
info.stores_sample_mask = true;
|
||||
break;
|
||||
case IR::Opcode::SetFragDepth:
|
||||
info.stores_frag_depth = true;
|
||||
break;
|
||||
case IR::Opcode::WorkgroupId:
|
||||
info.uses_workgroup_id = true;
|
||||
break;
|
||||
case IR::Opcode::LocalInvocationId:
|
||||
info.uses_local_invocation_id = true;
|
||||
break;
|
||||
case IR::Opcode::InvocationId:
|
||||
info.uses_invocation_id = true;
|
||||
break;
|
||||
case IR::Opcode::InvocationInfo:
|
||||
info.uses_invocation_info = true;
|
||||
break;
|
||||
case IR::Opcode::SampleId:
|
||||
info.uses_sample_id = true;
|
||||
break;
|
||||
case IR::Opcode::IsHelperInvocation:
|
||||
info.uses_is_helper_invocation = true;
|
||||
break;
|
||||
case IR::Opcode::ResolutionDownFactor:
|
||||
case IR::Opcode::IsTextureScaled:
|
||||
case IR::Opcode::IsImageScaled:
|
||||
info.uses_rescaling_uniform = true;
|
||||
break;
|
||||
case IR::Opcode::LaneId:
|
||||
info.uses_subgroup_invocation_id = true;
|
||||
break;
|
||||
case IR::Opcode::ShuffleIndex:
|
||||
case IR::Opcode::ShuffleUp:
|
||||
case IR::Opcode::ShuffleDown:
|
||||
case IR::Opcode::ShuffleButterfly:
|
||||
info.uses_subgroup_shuffles = true;
|
||||
break;
|
||||
case IR::Opcode::GetCbufU8:
|
||||
case IR::Opcode::GetCbufS8:
|
||||
case IR::Opcode::GetCbufU16:
|
||||
case IR::Opcode::GetCbufS16:
|
||||
case IR::Opcode::GetCbufU32:
|
||||
case IR::Opcode::GetCbufF32:
|
||||
case IR::Opcode::GetCbufU32x2: {
|
||||
const IR::Value index{inst.Arg(0)};
|
||||
const IR::Value offset{inst.Arg(1)};
|
||||
if (index.IsImmediate()) {
|
||||
AddConstantBufferDescriptor(info, index.U32(), 1);
|
||||
u32 element_size = GetElementSize(info.used_constant_buffer_types, inst.GetOpcode());
|
||||
u32& size{info.constant_buffer_used_sizes[index.U32()]};
|
||||
if (offset.IsImmediate()) {
|
||||
size = Common::AlignUp((std::max)(size, offset.U32() + element_size), 16u);
|
||||
} else {
|
||||
size = 0x10'000;
|
||||
}
|
||||
} else {
|
||||
AddRegisterIndexedLdc(info);
|
||||
GetElementSize(info.used_indirect_cbuf_types, inst.GetOpcode());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::BindlessImageSampleImplicitLod:
|
||||
case IR::Opcode::BindlessImageSampleExplicitLod:
|
||||
case IR::Opcode::BindlessImageSampleDrefImplicitLod:
|
||||
case IR::Opcode::BindlessImageSampleDrefExplicitLod:
|
||||
case IR::Opcode::BindlessImageGather:
|
||||
case IR::Opcode::BindlessImageGatherDref:
|
||||
case IR::Opcode::BindlessImageFetch:
|
||||
case IR::Opcode::BindlessImageQueryDimensions:
|
||||
case IR::Opcode::BindlessImageQueryLod:
|
||||
case IR::Opcode::BindlessImageGradient:
|
||||
case IR::Opcode::BoundImageSampleImplicitLod:
|
||||
case IR::Opcode::BoundImageSampleExplicitLod:
|
||||
case IR::Opcode::BoundImageSampleDrefImplicitLod:
|
||||
case IR::Opcode::BoundImageSampleDrefExplicitLod:
|
||||
case IR::Opcode::BoundImageGather:
|
||||
case IR::Opcode::BoundImageGatherDref:
|
||||
case IR::Opcode::BoundImageFetch:
|
||||
case IR::Opcode::BoundImageQueryDimensions:
|
||||
case IR::Opcode::BoundImageQueryLod:
|
||||
case IR::Opcode::BoundImageGradient:
|
||||
case IR::Opcode::ImageGather:
|
||||
case IR::Opcode::ImageGatherDref:
|
||||
case IR::Opcode::ImageFetch:
|
||||
case IR::Opcode::ImageQueryDimensions:
|
||||
case IR::Opcode::ImageGradient: {
|
||||
const TextureType type{inst.Flags<IR::TextureInstInfo>().type};
|
||||
info.uses_sampled_1d |= type == TextureType::Color1D || type == TextureType::ColorArray1D;
|
||||
info.uses_sparse_residency |=
|
||||
inst.GetAssociatedPseudoOperation(IR::Opcode::GetSparseFromOp) != nullptr;
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::ImageSampleImplicitLod:
|
||||
case IR::Opcode::ImageSampleExplicitLod:
|
||||
case IR::Opcode::ImageSampleDrefImplicitLod:
|
||||
case IR::Opcode::ImageSampleDrefExplicitLod:
|
||||
case IR::Opcode::ImageQueryLod: {
|
||||
const auto flags{inst.Flags<IR::TextureInstInfo>()};
|
||||
const TextureType type{flags.type};
|
||||
info.uses_sampled_1d |= type == TextureType::Color1D || type == TextureType::ColorArray1D;
|
||||
info.uses_shadow_lod |= flags.is_depth != 0;
|
||||
info.uses_sparse_residency |=
|
||||
inst.GetAssociatedPseudoOperation(IR::Opcode::GetSparseFromOp) != nullptr;
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::ImageRead: {
|
||||
const auto flags{inst.Flags<IR::TextureInstInfo>()};
|
||||
info.uses_typeless_image_reads |= flags.image_format == ImageFormat::Typeless;
|
||||
info.uses_image_1d |=
|
||||
flags.type == TextureType::Color1D || flags.type == TextureType::ColorArray1D;
|
||||
info.uses_sparse_residency |=
|
||||
inst.GetAssociatedPseudoOperation(IR::Opcode::GetSparseFromOp) != nullptr;
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::ImageWrite: {
|
||||
const auto flags{inst.Flags<IR::TextureInstInfo>()};
|
||||
info.uses_typeless_image_writes |= flags.image_format == ImageFormat::Typeless;
|
||||
info.uses_image_buffers |= flags.type == TextureType::Buffer;
|
||||
info.uses_image_1d |=
|
||||
flags.type == TextureType::Color1D || flags.type == TextureType::ColorArray1D;
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::SubgroupEqMask:
|
||||
case IR::Opcode::SubgroupLtMask:
|
||||
case IR::Opcode::SubgroupLeMask:
|
||||
case IR::Opcode::SubgroupGtMask:
|
||||
case IR::Opcode::SubgroupGeMask:
|
||||
info.uses_subgroup_mask = true;
|
||||
break;
|
||||
case IR::Opcode::VoteAll:
|
||||
case IR::Opcode::VoteAny:
|
||||
case IR::Opcode::VoteEqual:
|
||||
case IR::Opcode::SubgroupBallot:
|
||||
info.uses_subgroup_vote = true;
|
||||
break;
|
||||
case IR::Opcode::FSwizzleAdd:
|
||||
info.uses_fswzadd = true;
|
||||
break;
|
||||
case IR::Opcode::DPdxFine:
|
||||
case IR::Opcode::DPdyFine:
|
||||
case IR::Opcode::DPdxCoarse:
|
||||
case IR::Opcode::DPdyCoarse:
|
||||
info.uses_derivatives = true;
|
||||
break;
|
||||
case IR::Opcode::LoadStorageU8:
|
||||
case IR::Opcode::LoadStorageS8:
|
||||
case IR::Opcode::WriteStorageU8:
|
||||
case IR::Opcode::WriteStorageS8:
|
||||
info.used_storage_buffer_types |= IR::Type::U8;
|
||||
break;
|
||||
case IR::Opcode::LoadStorageU16:
|
||||
case IR::Opcode::LoadStorageS16:
|
||||
case IR::Opcode::WriteStorageU16:
|
||||
case IR::Opcode::WriteStorageS16:
|
||||
info.used_storage_buffer_types |= IR::Type::U16;
|
||||
break;
|
||||
case IR::Opcode::LoadStorage32:
|
||||
case IR::Opcode::WriteStorage32:
|
||||
case IR::Opcode::StorageAtomicIAdd32:
|
||||
case IR::Opcode::StorageAtomicUMin32:
|
||||
case IR::Opcode::StorageAtomicUMax32:
|
||||
case IR::Opcode::StorageAtomicAnd32:
|
||||
case IR::Opcode::StorageAtomicOr32:
|
||||
case IR::Opcode::StorageAtomicXor32:
|
||||
case IR::Opcode::StorageAtomicExchange32:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
break;
|
||||
case IR::Opcode::LoadStorage64:
|
||||
case IR::Opcode::WriteStorage64:
|
||||
case IR::Opcode::StorageAtomicIAdd32x2:
|
||||
case IR::Opcode::StorageAtomicSMin32x2:
|
||||
case IR::Opcode::StorageAtomicUMin32x2:
|
||||
case IR::Opcode::StorageAtomicSMax32x2:
|
||||
case IR::Opcode::StorageAtomicUMax32x2:
|
||||
case IR::Opcode::StorageAtomicAnd32x2:
|
||||
case IR::Opcode::StorageAtomicOr32x2:
|
||||
case IR::Opcode::StorageAtomicXor32x2:
|
||||
case IR::Opcode::StorageAtomicExchange32x2:
|
||||
info.used_storage_buffer_types |= IR::Type::U32x2;
|
||||
break;
|
||||
case IR::Opcode::LoadStorage128:
|
||||
case IR::Opcode::WriteStorage128:
|
||||
info.used_storage_buffer_types |= IR::Type::U32x4;
|
||||
break;
|
||||
case IR::Opcode::SharedAtomicSMin32:
|
||||
info.uses_atomic_s32_min = true;
|
||||
break;
|
||||
case IR::Opcode::SharedAtomicSMax32:
|
||||
info.uses_atomic_s32_max = true;
|
||||
break;
|
||||
case IR::Opcode::SharedAtomicInc32:
|
||||
info.uses_shared_increment = true;
|
||||
break;
|
||||
case IR::Opcode::SharedAtomicDec32:
|
||||
info.uses_shared_decrement = true;
|
||||
break;
|
||||
case IR::Opcode::SharedAtomicExchange64:
|
||||
info.uses_int64_bit_atomics = true;
|
||||
break;
|
||||
case IR::Opcode::GlobalAtomicInc32:
|
||||
case IR::Opcode::StorageAtomicInc32:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_global_increment = true;
|
||||
break;
|
||||
case IR::Opcode::GlobalAtomicDec32:
|
||||
case IR::Opcode::StorageAtomicDec32:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_global_decrement = true;
|
||||
break;
|
||||
case IR::Opcode::GlobalAtomicAddF32:
|
||||
case IR::Opcode::StorageAtomicAddF32:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_atomic_f32_add = true;
|
||||
break;
|
||||
case IR::Opcode::GlobalAtomicAddF16x2:
|
||||
case IR::Opcode::StorageAtomicAddF16x2:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_atomic_f16x2_add = true;
|
||||
break;
|
||||
case IR::Opcode::GlobalAtomicAddF32x2:
|
||||
case IR::Opcode::StorageAtomicAddF32x2:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_atomic_f32x2_add = true;
|
||||
break;
|
||||
case IR::Opcode::GlobalAtomicMinF16x2:
|
||||
case IR::Opcode::StorageAtomicMinF16x2:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_atomic_f16x2_min = true;
|
||||
break;
|
||||
case IR::Opcode::GlobalAtomicMinF32x2:
|
||||
case IR::Opcode::StorageAtomicMinF32x2:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_atomic_f32x2_min = true;
|
||||
break;
|
||||
case IR::Opcode::GlobalAtomicMaxF16x2:
|
||||
case IR::Opcode::StorageAtomicMaxF16x2:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_atomic_f16x2_max = true;
|
||||
break;
|
||||
case IR::Opcode::GlobalAtomicMaxF32x2:
|
||||
case IR::Opcode::StorageAtomicMaxF32x2:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_atomic_f32x2_max = true;
|
||||
break;
|
||||
case IR::Opcode::StorageAtomicSMin32:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_atomic_s32_min = true;
|
||||
break;
|
||||
case IR::Opcode::StorageAtomicSMax32:
|
||||
info.used_storage_buffer_types |= IR::Type::U32;
|
||||
info.uses_atomic_s32_max = true;
|
||||
break;
|
||||
case IR::Opcode::GlobalAtomicIAdd64:
|
||||
case IR::Opcode::GlobalAtomicSMin64:
|
||||
case IR::Opcode::GlobalAtomicUMin64:
|
||||
case IR::Opcode::GlobalAtomicSMax64:
|
||||
case IR::Opcode::GlobalAtomicUMax64:
|
||||
case IR::Opcode::GlobalAtomicAnd64:
|
||||
case IR::Opcode::GlobalAtomicOr64:
|
||||
case IR::Opcode::GlobalAtomicXor64:
|
||||
case IR::Opcode::GlobalAtomicExchange64:
|
||||
case IR::Opcode::StorageAtomicIAdd64:
|
||||
case IR::Opcode::StorageAtomicSMin64:
|
||||
case IR::Opcode::StorageAtomicUMin64:
|
||||
case IR::Opcode::StorageAtomicSMax64:
|
||||
case IR::Opcode::StorageAtomicUMax64:
|
||||
case IR::Opcode::StorageAtomicAnd64:
|
||||
case IR::Opcode::StorageAtomicOr64:
|
||||
case IR::Opcode::StorageAtomicXor64:
|
||||
info.used_storage_buffer_types |= IR::Type::U64 | IR::Type::U32x2;
|
||||
info.uses_int64_bit_atomics = true;
|
||||
break;
|
||||
case IR::Opcode::BindlessImageAtomicIAdd32:
|
||||
case IR::Opcode::BindlessImageAtomicSMin32:
|
||||
case IR::Opcode::BindlessImageAtomicUMin32:
|
||||
case IR::Opcode::BindlessImageAtomicSMax32:
|
||||
case IR::Opcode::BindlessImageAtomicUMax32:
|
||||
case IR::Opcode::BindlessImageAtomicInc32:
|
||||
case IR::Opcode::BindlessImageAtomicDec32:
|
||||
case IR::Opcode::BindlessImageAtomicAnd32:
|
||||
case IR::Opcode::BindlessImageAtomicOr32:
|
||||
case IR::Opcode::BindlessImageAtomicXor32:
|
||||
case IR::Opcode::BindlessImageAtomicExchange32:
|
||||
case IR::Opcode::BoundImageAtomicIAdd32:
|
||||
case IR::Opcode::BoundImageAtomicSMin32:
|
||||
case IR::Opcode::BoundImageAtomicUMin32:
|
||||
case IR::Opcode::BoundImageAtomicSMax32:
|
||||
case IR::Opcode::BoundImageAtomicUMax32:
|
||||
case IR::Opcode::BoundImageAtomicInc32:
|
||||
case IR::Opcode::BoundImageAtomicDec32:
|
||||
case IR::Opcode::BoundImageAtomicAnd32:
|
||||
case IR::Opcode::BoundImageAtomicOr32:
|
||||
case IR::Opcode::BoundImageAtomicXor32:
|
||||
case IR::Opcode::BoundImageAtomicExchange32:
|
||||
case IR::Opcode::ImageAtomicIAdd32:
|
||||
case IR::Opcode::ImageAtomicSMin32:
|
||||
case IR::Opcode::ImageAtomicUMin32:
|
||||
case IR::Opcode::ImageAtomicSMax32:
|
||||
case IR::Opcode::ImageAtomicUMax32:
|
||||
case IR::Opcode::ImageAtomicInc32:
|
||||
case IR::Opcode::ImageAtomicDec32:
|
||||
case IR::Opcode::ImageAtomicAnd32:
|
||||
case IR::Opcode::ImageAtomicOr32:
|
||||
case IR::Opcode::ImageAtomicXor32:
|
||||
case IR::Opcode::ImageAtomicExchange32: {
|
||||
const auto flags{inst.Flags<IR::TextureInstInfo>()};
|
||||
info.uses_atomic_image_u32 = true;
|
||||
info.uses_image_1d |=
|
||||
flags.type == TextureType::Color1D || flags.type == TextureType::ColorArray1D;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void VisitFpModifiers(Info& info, IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::FPAdd16:
|
||||
case IR::Opcode::FPFma16:
|
||||
case IR::Opcode::FPMul16:
|
||||
case IR::Opcode::FPRoundEven16:
|
||||
case IR::Opcode::FPFloor16:
|
||||
case IR::Opcode::FPCeil16:
|
||||
case IR::Opcode::FPTrunc16: {
|
||||
const auto control{inst.Flags<IR::FpControl>()};
|
||||
switch (control.fmz_mode) {
|
||||
case IR::FmzMode::DontCare:
|
||||
break;
|
||||
case IR::FmzMode::FTZ:
|
||||
case IR::FmzMode::FMZ:
|
||||
info.uses_fp16_denorms_flush = true;
|
||||
break;
|
||||
case IR::FmzMode::None:
|
||||
info.uses_fp16_denorms_preserve = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::FPAdd32:
|
||||
case IR::Opcode::FPFma32:
|
||||
case IR::Opcode::FPMul32:
|
||||
case IR::Opcode::FPRoundEven32:
|
||||
case IR::Opcode::FPFloor32:
|
||||
case IR::Opcode::FPCeil32:
|
||||
case IR::Opcode::FPTrunc32:
|
||||
case IR::Opcode::FPOrdEqual32:
|
||||
case IR::Opcode::FPUnordEqual32:
|
||||
case IR::Opcode::FPOrdNotEqual32:
|
||||
case IR::Opcode::FPUnordNotEqual32:
|
||||
case IR::Opcode::FPOrdLessThan32:
|
||||
case IR::Opcode::FPUnordLessThan32:
|
||||
case IR::Opcode::FPOrdGreaterThan32:
|
||||
case IR::Opcode::FPUnordGreaterThan32:
|
||||
case IR::Opcode::FPOrdLessThanEqual32:
|
||||
case IR::Opcode::FPUnordLessThanEqual32:
|
||||
case IR::Opcode::FPOrdGreaterThanEqual32:
|
||||
case IR::Opcode::FPUnordGreaterThanEqual32:
|
||||
case IR::Opcode::ConvertF16F32:
|
||||
case IR::Opcode::ConvertF64F32: {
|
||||
const auto control{inst.Flags<IR::FpControl>()};
|
||||
switch (control.fmz_mode) {
|
||||
case IR::FmzMode::DontCare:
|
||||
break;
|
||||
case IR::FmzMode::FTZ:
|
||||
case IR::FmzMode::FMZ:
|
||||
info.uses_fp32_denorms_flush = true;
|
||||
break;
|
||||
case IR::FmzMode::None:
|
||||
info.uses_fp32_denorms_preserve = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void VisitCbufs(Info& info, IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::GetCbufU8:
|
||||
case IR::Opcode::GetCbufS8:
|
||||
case IR::Opcode::GetCbufU16:
|
||||
case IR::Opcode::GetCbufS16:
|
||||
case IR::Opcode::GetCbufU32:
|
||||
case IR::Opcode::GetCbufF32:
|
||||
case IR::Opcode::GetCbufU32x2: {
|
||||
CheckCBufNVN(info, inst);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Visit(Info& info, IR::Inst& inst) {
|
||||
VisitUsages(info, inst);
|
||||
VisitFpModifiers(info, inst);
|
||||
VisitCbufs(info, inst);
|
||||
}
|
||||
|
||||
void GatherInfoFromHeader(Environment& env, Info& info) {
|
||||
Stage stage{env.ShaderStage()};
|
||||
if (stage == Stage::Compute) {
|
||||
return;
|
||||
}
|
||||
const auto& header{env.SPH()};
|
||||
if (stage == Stage::Fragment) {
|
||||
if (!info.loads_indexed_attributes) {
|
||||
return;
|
||||
}
|
||||
for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
|
||||
const size_t offset{static_cast<size_t>(IR::Attribute::Generic0X) + index * 4};
|
||||
const auto vector{header.ps.imap_generic_vector[index]};
|
||||
info.loads.mask[offset + 0] = vector.x != PixelImap::Unused;
|
||||
info.loads.mask[offset + 1] = vector.y != PixelImap::Unused;
|
||||
info.loads.mask[offset + 2] = vector.z != PixelImap::Unused;
|
||||
info.loads.mask[offset + 3] = vector.w != PixelImap::Unused;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (info.loads_indexed_attributes) {
|
||||
for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
|
||||
const IR::Attribute attribute{IR::Attribute::Generic0X + index * 4};
|
||||
const auto mask = header.vtg.InputGeneric(index);
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
info.loads.Set(attribute + i, mask[i]);
|
||||
}
|
||||
}
|
||||
for (size_t index = 0; index < 8; ++index) {
|
||||
const u16 mask{header.vtg.clip_distances};
|
||||
info.loads.Set(IR::Attribute::ClipDistance0 + index, ((mask >> index) & 1) != 0);
|
||||
}
|
||||
info.loads.Set(IR::Attribute::PrimitiveId, header.vtg.imap_systemb.primitive_array_id != 0);
|
||||
info.loads.Set(IR::Attribute::Layer, header.vtg.imap_systemb.rt_array_index != 0);
|
||||
info.loads.Set(IR::Attribute::ViewportIndex, header.vtg.imap_systemb.viewport_index != 0);
|
||||
info.loads.Set(IR::Attribute::PointSize, header.vtg.imap_systemb.point_size != 0);
|
||||
info.loads.Set(IR::Attribute::PositionX, header.vtg.imap_systemb.position_x != 0);
|
||||
info.loads.Set(IR::Attribute::PositionY, header.vtg.imap_systemb.position_y != 0);
|
||||
info.loads.Set(IR::Attribute::PositionZ, header.vtg.imap_systemb.position_z != 0);
|
||||
info.loads.Set(IR::Attribute::PositionW, header.vtg.imap_systemb.position_w != 0);
|
||||
info.loads.Set(IR::Attribute::PointSpriteS, header.vtg.point_sprite_s != 0);
|
||||
info.loads.Set(IR::Attribute::PointSpriteT, header.vtg.point_sprite_t != 0);
|
||||
info.loads.Set(IR::Attribute::FogCoordinate, header.vtg.fog_coordinate != 0);
|
||||
info.loads.Set(IR::Attribute::TessellationEvaluationPointU,
|
||||
header.vtg.tessellation_eval_point_u != 0);
|
||||
info.loads.Set(IR::Attribute::TessellationEvaluationPointV,
|
||||
header.vtg.tessellation_eval_point_v != 0);
|
||||
info.loads.Set(IR::Attribute::InstanceId, header.vtg.instance_id != 0);
|
||||
info.loads.Set(IR::Attribute::VertexId, header.vtg.vertex_id != 0);
|
||||
// TODO: Legacy varyings
|
||||
}
|
||||
if (info.stores_indexed_attributes) {
|
||||
for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
|
||||
const IR::Attribute attribute{IR::Attribute::Generic0X + index * 4};
|
||||
const auto mask{header.vtg.OutputGeneric(index)};
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
info.stores.Set(attribute + i, mask[i]);
|
||||
}
|
||||
}
|
||||
for (size_t index = 0; index < 8; ++index) {
|
||||
const u16 mask{header.vtg.omap_systemc.clip_distances};
|
||||
const bool used{((mask >> index) & 1) != 0};
|
||||
info.stores.Set(IR::Attribute::ClipDistance0 + index, used);
|
||||
if (used) {
|
||||
info.used_clip_distances = static_cast<u32>(index) + 1;
|
||||
}
|
||||
}
|
||||
info.stores.Set(IR::Attribute::PrimitiveId,
|
||||
header.vtg.omap_systemb.primitive_array_id != 0);
|
||||
info.stores.Set(IR::Attribute::Layer, header.vtg.omap_systemb.rt_array_index != 0);
|
||||
info.stores.Set(IR::Attribute::ViewportIndex, header.vtg.omap_systemb.viewport_index != 0);
|
||||
info.stores.Set(IR::Attribute::PointSize, header.vtg.omap_systemb.point_size != 0);
|
||||
info.stores.Set(IR::Attribute::PositionX, header.vtg.omap_systemb.position_x != 0);
|
||||
info.stores.Set(IR::Attribute::PositionY, header.vtg.omap_systemb.position_y != 0);
|
||||
info.stores.Set(IR::Attribute::PositionZ, header.vtg.omap_systemb.position_z != 0);
|
||||
info.stores.Set(IR::Attribute::PositionW, header.vtg.omap_systemb.position_w != 0);
|
||||
info.stores.Set(IR::Attribute::PointSpriteS, header.vtg.omap_systemc.point_sprite_s != 0);
|
||||
info.stores.Set(IR::Attribute::PointSpriteT, header.vtg.omap_systemc.point_sprite_t != 0);
|
||||
info.stores.Set(IR::Attribute::FogCoordinate, header.vtg.omap_systemc.fog_coordinate != 0);
|
||||
info.stores.Set(IR::Attribute::TessellationEvaluationPointU,
|
||||
header.vtg.omap_systemc.tessellation_eval_point_u != 0);
|
||||
info.stores.Set(IR::Attribute::TessellationEvaluationPointV,
|
||||
header.vtg.omap_systemc.tessellation_eval_point_v != 0);
|
||||
info.stores.Set(IR::Attribute::InstanceId, header.vtg.omap_systemc.instance_id != 0);
|
||||
info.stores.Set(IR::Attribute::VertexId, header.vtg.omap_systemc.vertex_id != 0);
|
||||
// TODO: Legacy varyings
|
||||
}
|
||||
}
|
||||
} // Anonymous namespace
|
||||
|
||||
void CollectShaderInfoPass(Environment& env, IR::Program& program) {
|
||||
Info& info{program.info};
|
||||
const u32 base{[&] {
|
||||
switch (program.stage) {
|
||||
case Stage::VertexA:
|
||||
case Stage::VertexB:
|
||||
return 0x110u;
|
||||
case Stage::TessellationControl:
|
||||
return 0x210u;
|
||||
case Stage::TessellationEval:
|
||||
return 0x310u;
|
||||
case Stage::Geometry:
|
||||
return 0x410u;
|
||||
case Stage::Fragment:
|
||||
return 0x510u;
|
||||
case Stage::Compute:
|
||||
return 0x310u;
|
||||
}
|
||||
throw InvalidArgument("Invalid stage {}", program.stage);
|
||||
}()};
|
||||
info.nvn_buffer_base = base;
|
||||
|
||||
for (IR::Block* const block : program.post_order_blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
Visit(info, inst);
|
||||
}
|
||||
}
|
||||
GatherInfoFromHeader(env, info);
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
75
gpu/third_party/suyu/shader_recompiler/ir_opt/conditional_barrier_pass.cpp
vendored
Normal file
75
gpu/third_party/suyu/shader_recompiler/ir_opt/conditional_barrier_pass.cpp
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include "shader_recompiler/frontend/ir/program.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
|
||||
// Barriers the host cannot run in conditional control flow are removed. One common case keeps its effect: a barrier
|
||||
// after invocations returned early (the guest GPU counts exited invocations as arrived), e.g. every invocation
|
||||
// stores to shared memory, all but the first return, the first waits and reads what the others stored. A barrier
|
||||
// is put at the end of the last unconditional block before the construct holding the first early return: there all
|
||||
// invocations are still running, and what they stored before returning is visible after it. (Rexcomp: without it,
|
||||
// TOTK's lighting tile classification read shared memory before the other invocations had written it.)
|
||||
void ConditionalBarrierPass(IR::Program& program) {
|
||||
s32 conditional_control_flow_count{0};
|
||||
s32 conditional_return_count{0};
|
||||
IR::Block* last_top_block{}; // the last block outside any construct
|
||||
IR::Block* before_construct{}; // the last block before the top-level construct being walked
|
||||
IR::Block* before_first_return{}; // the last block before the construct with the first early return
|
||||
bool hoisted{false};
|
||||
for (IR::AbstractSyntaxNode& node : program.syntax_list) {
|
||||
switch (node.type) {
|
||||
case IR::AbstractSyntaxNode::Type::If:
|
||||
case IR::AbstractSyntaxNode::Type::Loop:
|
||||
if (conditional_control_flow_count == 0) {
|
||||
before_construct = last_top_block;
|
||||
}
|
||||
conditional_control_flow_count++;
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::EndIf:
|
||||
case IR::AbstractSyntaxNode::Type::Repeat:
|
||||
conditional_control_flow_count--;
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Unreachable:
|
||||
case IR::AbstractSyntaxNode::Type::Return:
|
||||
if (conditional_control_flow_count > 0) {
|
||||
if (conditional_return_count == 0) {
|
||||
before_first_return = before_construct;
|
||||
}
|
||||
conditional_return_count++;
|
||||
}
|
||||
break;
|
||||
case IR::AbstractSyntaxNode::Type::Block:
|
||||
for (IR::Inst& inst : node.data.block->Instructions()) {
|
||||
// (mkw-recomp) conditional barriers stay: the GPUs this runs on (NVIDIA, as the console's) run a
|
||||
// barrier in a loop or a branch, count exited invocations as arrived, and wgpu does not hold WGSL's
|
||||
// barrier uniformity rule. Removed, MKW's compute programs that sum through shared memory in a loop
|
||||
// raced (8 of the 15 that use it lost every barrier). RX_STRIP_CONDITIONAL_BARRIERS=1: removed (and
|
||||
// the early-return one hoisted), as before
|
||||
static const bool strip = std::getenv("RX_STRIP_CONDITIONAL_BARRIERS") != nullptr;
|
||||
const bool conditional = strip && (conditional_control_flow_count > 0 || conditional_return_count > 0);
|
||||
if (conditional && inst.GetOpcode() == IR::Opcode::Barrier) {
|
||||
LOG_WARNING(Shader, "Barrier within conditional control flow");
|
||||
inst.ReplaceOpcode(IR::Opcode::Identity);
|
||||
if (conditional_control_flow_count == 0 && before_first_return && !hoisted) {
|
||||
before_first_return->PrependNewInst(before_first_return->end(), IR::Opcode::Barrier);
|
||||
hoisted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (conditional_control_flow_count == 0 && conditional_return_count == 0) {
|
||||
last_top_block = node.data.block;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
ASSERT(conditional_control_flow_count == 0);
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
1116
gpu/third_party/suyu/shader_recompiler/ir_opt/constant_propagation_pass.cpp
vendored
Normal file
1116
gpu/third_party/suyu/shader_recompiler/ir_opt/constant_propagation_pass.cpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
25
gpu/third_party/suyu/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp
vendored
Normal file
25
gpu/third_party/suyu/shader_recompiler/ir_opt/dead_code_elimination_pass.cpp
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
|
||||
void DeadCodeEliminationPass(IR::Program& program) {
|
||||
// We iterate over the instructions in reverse order.
|
||||
// This is because removing an instruction reduces the number of uses for earlier instructions.
|
||||
for (IR::Block* const block : program.post_order_blocks) {
|
||||
auto it{block->end()};
|
||||
while (it != block->begin()) {
|
||||
--it;
|
||||
if (!it->HasUses() && !it->MayHaveSideEffects()) {
|
||||
it->Invalidate();
|
||||
it = block->Instructions().erase(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
29
gpu/third_party/suyu/shader_recompiler/ir_opt/dual_vertex_pass.cpp
vendored
Normal file
29
gpu/third_party/suyu/shader_recompiler/ir_opt/dual_vertex_pass.cpp
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
|
||||
void VertexATransformPass(IR::Program& program) {
|
||||
for (IR::Block* const block : program.blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
if (inst.GetOpcode() == IR::Opcode::Epilogue) {
|
||||
return inst.Invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void VertexBTransformPass(IR::Program& program) {
|
||||
for (IR::Block* const block : program.blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
if (inst.GetOpcode() == IR::Opcode::Prologue) {
|
||||
return inst.Invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
582
gpu/third_party/suyu/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp
vendored
Normal file
582
gpu/third_party/suyu/shader_recompiler/ir_opt/global_memory_to_storage_buffer_pass.cpp
vendored
Normal file
@@ -0,0 +1,582 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include <boost/container/flat_set.hpp>
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include "common/alignment.h"
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/breadth_first_search.h"
|
||||
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/host_translate_info.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
namespace {
|
||||
/// Address in constant buffers to the storage buffer descriptor
|
||||
struct StorageBufferAddr {
|
||||
auto operator<=>(const StorageBufferAddr&) const noexcept = default;
|
||||
|
||||
u32 index;
|
||||
u32 offset;
|
||||
};
|
||||
|
||||
/// Block iterator to a global memory instruction and the storage buffer it uses
|
||||
struct StorageInst {
|
||||
StorageBufferAddr storage_buffer;
|
||||
IR::Inst* inst;
|
||||
IR::Block* block;
|
||||
};
|
||||
|
||||
/// Bias towards a certain range of constant buffers when looking for storage buffers
|
||||
struct Bias {
|
||||
u32 index;
|
||||
u32 offset_begin;
|
||||
u32 offset_end;
|
||||
u32 alignment;
|
||||
};
|
||||
|
||||
using boost::container::flat_set;
|
||||
using boost::container::small_vector;
|
||||
using StorageBufferSet =
|
||||
flat_set<StorageBufferAddr, std::less<StorageBufferAddr>, small_vector<StorageBufferAddr, 16>>;
|
||||
using StorageInstVector = small_vector<StorageInst, 24>;
|
||||
using StorageWritesSet =
|
||||
flat_set<StorageBufferAddr, std::less<StorageBufferAddr>, small_vector<StorageBufferAddr, 16>>;
|
||||
|
||||
struct StorageInfo {
|
||||
StorageBufferSet set;
|
||||
StorageInstVector to_replace;
|
||||
StorageWritesSet writes;
|
||||
};
|
||||
|
||||
/// Returns true when the instruction is a global memory instruction
|
||||
bool IsGlobalMemory(const IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::LoadGlobalS8:
|
||||
case IR::Opcode::LoadGlobalU8:
|
||||
case IR::Opcode::LoadGlobalS16:
|
||||
case IR::Opcode::LoadGlobalU16:
|
||||
case IR::Opcode::LoadGlobal32:
|
||||
case IR::Opcode::LoadGlobal64:
|
||||
case IR::Opcode::LoadGlobal128:
|
||||
case IR::Opcode::WriteGlobalS8:
|
||||
case IR::Opcode::WriteGlobalU8:
|
||||
case IR::Opcode::WriteGlobalS16:
|
||||
case IR::Opcode::WriteGlobalU16:
|
||||
case IR::Opcode::WriteGlobal32:
|
||||
case IR::Opcode::WriteGlobal64:
|
||||
case IR::Opcode::WriteGlobal128:
|
||||
case IR::Opcode::GlobalAtomicIAdd32:
|
||||
case IR::Opcode::GlobalAtomicSMin32:
|
||||
case IR::Opcode::GlobalAtomicUMin32:
|
||||
case IR::Opcode::GlobalAtomicSMax32:
|
||||
case IR::Opcode::GlobalAtomicUMax32:
|
||||
case IR::Opcode::GlobalAtomicInc32:
|
||||
case IR::Opcode::GlobalAtomicDec32:
|
||||
case IR::Opcode::GlobalAtomicAnd32:
|
||||
case IR::Opcode::GlobalAtomicOr32:
|
||||
case IR::Opcode::GlobalAtomicXor32:
|
||||
case IR::Opcode::GlobalAtomicExchange32:
|
||||
case IR::Opcode::GlobalAtomicIAdd64:
|
||||
case IR::Opcode::GlobalAtomicSMin64:
|
||||
case IR::Opcode::GlobalAtomicUMin64:
|
||||
case IR::Opcode::GlobalAtomicSMax64:
|
||||
case IR::Opcode::GlobalAtomicUMax64:
|
||||
case IR::Opcode::GlobalAtomicAnd64:
|
||||
case IR::Opcode::GlobalAtomicOr64:
|
||||
case IR::Opcode::GlobalAtomicXor64:
|
||||
case IR::Opcode::GlobalAtomicExchange64:
|
||||
case IR::Opcode::GlobalAtomicIAdd32x2:
|
||||
case IR::Opcode::GlobalAtomicSMin32x2:
|
||||
case IR::Opcode::GlobalAtomicUMin32x2:
|
||||
case IR::Opcode::GlobalAtomicSMax32x2:
|
||||
case IR::Opcode::GlobalAtomicUMax32x2:
|
||||
case IR::Opcode::GlobalAtomicAnd32x2:
|
||||
case IR::Opcode::GlobalAtomicOr32x2:
|
||||
case IR::Opcode::GlobalAtomicXor32x2:
|
||||
case IR::Opcode::GlobalAtomicExchange32x2:
|
||||
case IR::Opcode::GlobalAtomicAddF32:
|
||||
case IR::Opcode::GlobalAtomicAddF16x2:
|
||||
case IR::Opcode::GlobalAtomicAddF32x2:
|
||||
case IR::Opcode::GlobalAtomicMinF16x2:
|
||||
case IR::Opcode::GlobalAtomicMinF32x2:
|
||||
case IR::Opcode::GlobalAtomicMaxF16x2:
|
||||
case IR::Opcode::GlobalAtomicMaxF32x2:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true when the instruction is a global memory instruction
|
||||
bool IsGlobalMemoryWrite(const IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::WriteGlobalS8:
|
||||
case IR::Opcode::WriteGlobalU8:
|
||||
case IR::Opcode::WriteGlobalS16:
|
||||
case IR::Opcode::WriteGlobalU16:
|
||||
case IR::Opcode::WriteGlobal32:
|
||||
case IR::Opcode::WriteGlobal64:
|
||||
case IR::Opcode::WriteGlobal128:
|
||||
case IR::Opcode::GlobalAtomicIAdd32:
|
||||
case IR::Opcode::GlobalAtomicSMin32:
|
||||
case IR::Opcode::GlobalAtomicUMin32:
|
||||
case IR::Opcode::GlobalAtomicSMax32:
|
||||
case IR::Opcode::GlobalAtomicUMax32:
|
||||
case IR::Opcode::GlobalAtomicInc32:
|
||||
case IR::Opcode::GlobalAtomicDec32:
|
||||
case IR::Opcode::GlobalAtomicAnd32:
|
||||
case IR::Opcode::GlobalAtomicOr32:
|
||||
case IR::Opcode::GlobalAtomicXor32:
|
||||
case IR::Opcode::GlobalAtomicExchange32:
|
||||
case IR::Opcode::GlobalAtomicIAdd64:
|
||||
case IR::Opcode::GlobalAtomicSMin64:
|
||||
case IR::Opcode::GlobalAtomicUMin64:
|
||||
case IR::Opcode::GlobalAtomicSMax64:
|
||||
case IR::Opcode::GlobalAtomicUMax64:
|
||||
case IR::Opcode::GlobalAtomicAnd64:
|
||||
case IR::Opcode::GlobalAtomicOr64:
|
||||
case IR::Opcode::GlobalAtomicXor64:
|
||||
case IR::Opcode::GlobalAtomicExchange64:
|
||||
case IR::Opcode::GlobalAtomicIAdd32x2:
|
||||
case IR::Opcode::GlobalAtomicSMin32x2:
|
||||
case IR::Opcode::GlobalAtomicUMin32x2:
|
||||
case IR::Opcode::GlobalAtomicSMax32x2:
|
||||
case IR::Opcode::GlobalAtomicUMax32x2:
|
||||
case IR::Opcode::GlobalAtomicAnd32x2:
|
||||
case IR::Opcode::GlobalAtomicOr32x2:
|
||||
case IR::Opcode::GlobalAtomicXor32x2:
|
||||
case IR::Opcode::GlobalAtomicExchange32x2:
|
||||
case IR::Opcode::GlobalAtomicAddF32:
|
||||
case IR::Opcode::GlobalAtomicAddF16x2:
|
||||
case IR::Opcode::GlobalAtomicAddF32x2:
|
||||
case IR::Opcode::GlobalAtomicMinF16x2:
|
||||
case IR::Opcode::GlobalAtomicMinF32x2:
|
||||
case IR::Opcode::GlobalAtomicMaxF16x2:
|
||||
case IR::Opcode::GlobalAtomicMaxF32x2:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a global memory opcode to its storage buffer equivalent
|
||||
IR::Opcode GlobalToStorage(IR::Opcode opcode) {
|
||||
switch (opcode) {
|
||||
case IR::Opcode::LoadGlobalS8:
|
||||
return IR::Opcode::LoadStorageS8;
|
||||
case IR::Opcode::LoadGlobalU8:
|
||||
return IR::Opcode::LoadStorageU8;
|
||||
case IR::Opcode::LoadGlobalS16:
|
||||
return IR::Opcode::LoadStorageS16;
|
||||
case IR::Opcode::LoadGlobalU16:
|
||||
return IR::Opcode::LoadStorageU16;
|
||||
case IR::Opcode::LoadGlobal32:
|
||||
return IR::Opcode::LoadStorage32;
|
||||
case IR::Opcode::LoadGlobal64:
|
||||
return IR::Opcode::LoadStorage64;
|
||||
case IR::Opcode::LoadGlobal128:
|
||||
return IR::Opcode::LoadStorage128;
|
||||
case IR::Opcode::WriteGlobalS8:
|
||||
return IR::Opcode::WriteStorageS8;
|
||||
case IR::Opcode::WriteGlobalU8:
|
||||
return IR::Opcode::WriteStorageU8;
|
||||
case IR::Opcode::WriteGlobalS16:
|
||||
return IR::Opcode::WriteStorageS16;
|
||||
case IR::Opcode::WriteGlobalU16:
|
||||
return IR::Opcode::WriteStorageU16;
|
||||
case IR::Opcode::WriteGlobal32:
|
||||
return IR::Opcode::WriteStorage32;
|
||||
case IR::Opcode::WriteGlobal64:
|
||||
return IR::Opcode::WriteStorage64;
|
||||
case IR::Opcode::WriteGlobal128:
|
||||
return IR::Opcode::WriteStorage128;
|
||||
case IR::Opcode::GlobalAtomicIAdd32:
|
||||
return IR::Opcode::StorageAtomicIAdd32;
|
||||
case IR::Opcode::GlobalAtomicSMin32:
|
||||
return IR::Opcode::StorageAtomicSMin32;
|
||||
case IR::Opcode::GlobalAtomicUMin32:
|
||||
return IR::Opcode::StorageAtomicUMin32;
|
||||
case IR::Opcode::GlobalAtomicSMax32:
|
||||
return IR::Opcode::StorageAtomicSMax32;
|
||||
case IR::Opcode::GlobalAtomicUMax32:
|
||||
return IR::Opcode::StorageAtomicUMax32;
|
||||
case IR::Opcode::GlobalAtomicInc32:
|
||||
return IR::Opcode::StorageAtomicInc32;
|
||||
case IR::Opcode::GlobalAtomicDec32:
|
||||
return IR::Opcode::StorageAtomicDec32;
|
||||
case IR::Opcode::GlobalAtomicAnd32:
|
||||
return IR::Opcode::StorageAtomicAnd32;
|
||||
case IR::Opcode::GlobalAtomicOr32:
|
||||
return IR::Opcode::StorageAtomicOr32;
|
||||
case IR::Opcode::GlobalAtomicXor32:
|
||||
return IR::Opcode::StorageAtomicXor32;
|
||||
case IR::Opcode::GlobalAtomicExchange32:
|
||||
return IR::Opcode::StorageAtomicExchange32;
|
||||
case IR::Opcode::GlobalAtomicIAdd64:
|
||||
return IR::Opcode::StorageAtomicIAdd64;
|
||||
case IR::Opcode::GlobalAtomicSMin64:
|
||||
return IR::Opcode::StorageAtomicSMin64;
|
||||
case IR::Opcode::GlobalAtomicUMin64:
|
||||
return IR::Opcode::StorageAtomicUMin64;
|
||||
case IR::Opcode::GlobalAtomicSMax64:
|
||||
return IR::Opcode::StorageAtomicSMax64;
|
||||
case IR::Opcode::GlobalAtomicUMax64:
|
||||
return IR::Opcode::StorageAtomicUMax64;
|
||||
case IR::Opcode::GlobalAtomicAnd64:
|
||||
return IR::Opcode::StorageAtomicAnd64;
|
||||
case IR::Opcode::GlobalAtomicOr64:
|
||||
return IR::Opcode::StorageAtomicOr64;
|
||||
case IR::Opcode::GlobalAtomicXor64:
|
||||
return IR::Opcode::StorageAtomicXor64;
|
||||
case IR::Opcode::GlobalAtomicExchange64:
|
||||
return IR::Opcode::StorageAtomicExchange64;
|
||||
case IR::Opcode::GlobalAtomicIAdd32x2:
|
||||
return IR::Opcode::StorageAtomicIAdd32x2;
|
||||
case IR::Opcode::GlobalAtomicSMin32x2:
|
||||
return IR::Opcode::StorageAtomicSMin32x2;
|
||||
case IR::Opcode::GlobalAtomicUMin32x2:
|
||||
return IR::Opcode::StorageAtomicUMin32x2;
|
||||
case IR::Opcode::GlobalAtomicSMax32x2:
|
||||
return IR::Opcode::StorageAtomicSMax32x2;
|
||||
case IR::Opcode::GlobalAtomicUMax32x2:
|
||||
return IR::Opcode::StorageAtomicUMax32x2;
|
||||
case IR::Opcode::GlobalAtomicAnd32x2:
|
||||
return IR::Opcode::StorageAtomicAnd32x2;
|
||||
case IR::Opcode::GlobalAtomicOr32x2:
|
||||
return IR::Opcode::StorageAtomicOr32x2;
|
||||
case IR::Opcode::GlobalAtomicXor32x2:
|
||||
return IR::Opcode::StorageAtomicXor32x2;
|
||||
case IR::Opcode::GlobalAtomicExchange32x2:
|
||||
return IR::Opcode::StorageAtomicExchange32x2;
|
||||
case IR::Opcode::GlobalAtomicAddF32:
|
||||
return IR::Opcode::StorageAtomicAddF32;
|
||||
case IR::Opcode::GlobalAtomicAddF16x2:
|
||||
return IR::Opcode::StorageAtomicAddF16x2;
|
||||
case IR::Opcode::GlobalAtomicMinF16x2:
|
||||
return IR::Opcode::StorageAtomicMinF16x2;
|
||||
case IR::Opcode::GlobalAtomicMaxF16x2:
|
||||
return IR::Opcode::StorageAtomicMaxF16x2;
|
||||
case IR::Opcode::GlobalAtomicAddF32x2:
|
||||
return IR::Opcode::StorageAtomicAddF32x2;
|
||||
case IR::Opcode::GlobalAtomicMinF32x2:
|
||||
return IR::Opcode::StorageAtomicMinF32x2;
|
||||
case IR::Opcode::GlobalAtomicMaxF32x2:
|
||||
return IR::Opcode::StorageAtomicMaxF32x2;
|
||||
default:
|
||||
throw InvalidArgument("Invalid global memory opcode {}", opcode);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true when a storage buffer address satisfies a bias
|
||||
bool MeetsBias(const StorageBufferAddr& storage_buffer, const Bias& bias) noexcept {
|
||||
return storage_buffer.index == bias.index && storage_buffer.offset >= bias.offset_begin &&
|
||||
storage_buffer.offset < bias.offset_end;
|
||||
}
|
||||
|
||||
struct LowAddrInfo {
|
||||
IR::U32 value;
|
||||
s32 imm_offset;
|
||||
};
|
||||
|
||||
/// Tries to track the first 32-bits of a global memory instruction
|
||||
std::optional<LowAddrInfo> TrackLowAddress(IR::Inst* inst) {
|
||||
// The first argument is the low level GPU pointer to the global memory instruction
|
||||
const IR::Value addr{inst->Arg(0)};
|
||||
if (addr.IsImmediate()) {
|
||||
// Not much we can do if it's an immediate
|
||||
return std::nullopt;
|
||||
}
|
||||
// This address is expected to either be a PackUint2x32, a IAdd64, or a CompositeConstructU32x2
|
||||
IR::Inst* addr_inst{addr.InstRecursive()};
|
||||
s32 imm_offset{0};
|
||||
if (addr_inst->GetOpcode() == IR::Opcode::IAdd64) {
|
||||
// If it's an IAdd64, get the immediate offset it is applying and grab the address
|
||||
// instruction. This expects for the instruction to be canonicalized having the address on
|
||||
// the first argument and the immediate offset on the second one.
|
||||
const IR::U64 imm_offset_value{addr_inst->Arg(1)};
|
||||
if (!imm_offset_value.IsImmediate()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
imm_offset = static_cast<s32>(static_cast<s64>(imm_offset_value.U64()));
|
||||
const IR::U64 iadd_addr{addr_inst->Arg(0)};
|
||||
if (iadd_addr.IsImmediate()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
addr_inst = iadd_addr.InstRecursive();
|
||||
}
|
||||
// With IAdd64 handled, now PackUint2x32 is expected
|
||||
if (addr_inst->GetOpcode() == IR::Opcode::PackUint2x32) {
|
||||
// PackUint2x32 is expected to be generated from a vector
|
||||
const IR::Value vector{addr_inst->Arg(0)};
|
||||
if (vector.IsImmediate()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
addr_inst = vector.InstRecursive();
|
||||
}
|
||||
// The vector is expected to be a CompositeConstructU32x2
|
||||
if (addr_inst->GetOpcode() != IR::Opcode::CompositeConstructU32x2) {
|
||||
return std::nullopt;
|
||||
}
|
||||
// Grab the first argument from the CompositeConstructU32x2, this is the low address.
|
||||
return LowAddrInfo{
|
||||
.value{IR::U32{addr_inst->Arg(0)}},
|
||||
.imm_offset = imm_offset,
|
||||
};
|
||||
}
|
||||
|
||||
/// Tries to track the storage buffer address used by a global memory instruction
|
||||
std::optional<StorageBufferAddr> Track(const IR::Value& value, const Bias* bias) {
|
||||
const auto pred{[bias](const IR::Inst* inst) -> std::optional<StorageBufferAddr> {
|
||||
if (inst->GetOpcode() != IR::Opcode::GetCbufU32 &&
|
||||
inst->GetOpcode() != IR::Opcode::GetCbufU32x2) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const IR::Value index{inst->Arg(0)};
|
||||
const IR::Value offset{inst->Arg(1)};
|
||||
if (!index.IsImmediate()) {
|
||||
// Definitely not a storage buffer if it's read from a
|
||||
// non-immediate index
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!offset.IsImmediate()) {
|
||||
// TODO: Support SSBO arrays
|
||||
return std::nullopt;
|
||||
}
|
||||
const StorageBufferAddr storage_buffer{
|
||||
.index = index.U32(),
|
||||
.offset = offset.U32(),
|
||||
};
|
||||
const u32 alignment{bias ? bias->alignment : 8U};
|
||||
if (!Common::IsAligned(storage_buffer.offset, alignment)) {
|
||||
// The SSBO pointer has to be aligned
|
||||
return std::nullopt;
|
||||
}
|
||||
if (bias && !MeetsBias(storage_buffer, *bias)) {
|
||||
// We have to blacklist some addresses in case we wrongly
|
||||
// point to them
|
||||
return std::nullopt;
|
||||
}
|
||||
return storage_buffer;
|
||||
}};
|
||||
return BreadthFirstSearch(value, pred);
|
||||
}
|
||||
|
||||
/// Collects the storage buffer used by a global memory instruction and the instruction itself
|
||||
void CollectStorageBuffers(IR::Block& block, IR::Inst& inst, StorageInfo& info) {
|
||||
// NVN puts storage buffers in a specific range, we have to bias towards these addresses to
|
||||
// avoid getting false positives
|
||||
static constexpr Bias nvn_bias{
|
||||
.index = 0,
|
||||
.offset_begin = 0x100,
|
||||
.offset_end = 0x700,
|
||||
.alignment = 16,
|
||||
};
|
||||
// Track the low address of the instruction
|
||||
const std::optional<LowAddrInfo> low_addr_info{TrackLowAddress(&inst)};
|
||||
if (!low_addr_info) {
|
||||
// Failed to track the low address, use NVN fallbacks
|
||||
return;
|
||||
}
|
||||
// First try to find storage buffers in the NVN address
|
||||
const IR::U32 low_addr{low_addr_info->value};
|
||||
std::optional<StorageBufferAddr> storage_buffer{Track(low_addr, &nvn_bias)};
|
||||
if (!storage_buffer) {
|
||||
// If it fails, track without a bias
|
||||
storage_buffer = Track(low_addr, nullptr);
|
||||
if (!storage_buffer) {
|
||||
// If that also fails, use NVN fallbacks
|
||||
LOG_WARNING(Shader, "Storage buffer failed to track, using global memory fallbacks");
|
||||
return;
|
||||
}
|
||||
LOG_WARNING(Shader, "Storage buffer tracked without bias, index {} offset {}",
|
||||
storage_buffer->index, storage_buffer->offset);
|
||||
}
|
||||
// Collect storage buffer and the instruction
|
||||
if (IsGlobalMemoryWrite(inst)) {
|
||||
info.writes.insert(*storage_buffer);
|
||||
}
|
||||
info.set.insert(*storage_buffer);
|
||||
info.to_replace.push_back(StorageInst{
|
||||
.storage_buffer{*storage_buffer},
|
||||
.inst = &inst,
|
||||
.block = &block,
|
||||
});
|
||||
}
|
||||
|
||||
/// Returns the offset in indices (not bytes) for an equivalent storage instruction
|
||||
IR::U32 StorageOffset(IR::Block& block, IR::Inst& inst, StorageBufferAddr buffer, u32 alignment) {
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
IR::U32 offset;
|
||||
if (const std::optional<LowAddrInfo> low_addr{TrackLowAddress(&inst)}) {
|
||||
offset = low_addr->value;
|
||||
if (low_addr->imm_offset != 0) {
|
||||
offset = ir.IAdd(offset, ir.Imm32(low_addr->imm_offset));
|
||||
}
|
||||
} else {
|
||||
offset = ir.UConvert(32, IR::U64{inst.Arg(0)});
|
||||
}
|
||||
// Subtract the least significant 32 bits from the guest offset. The result is the storage
|
||||
// buffer offset in bytes.
|
||||
IR::U32 low_cbuf{ir.GetCbuf(ir.Imm32(buffer.index), ir.Imm32(buffer.offset))};
|
||||
|
||||
// Align the offset base to match the host alignment requirements
|
||||
low_cbuf = ir.BitwiseAnd(low_cbuf, ir.Imm32(~(alignment - 1U)));
|
||||
return ir.ISub(offset, low_cbuf);
|
||||
}
|
||||
|
||||
/// Replace a global memory load instruction with its storage buffer equivalent
|
||||
void ReplaceLoad(IR::Block& block, IR::Inst& inst, const IR::U32& storage_index,
|
||||
const IR::U32& offset) {
|
||||
const IR::Opcode new_opcode{GlobalToStorage(inst.GetOpcode())};
|
||||
const auto it{IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
const IR::Value value{&*block.PrependNewInst(it, new_opcode, {storage_index, offset})};
|
||||
inst.ReplaceUsesWith(value);
|
||||
}
|
||||
|
||||
/// Replace a global memory write instruction with its storage buffer equivalent
|
||||
void ReplaceWrite(IR::Block& block, IR::Inst& inst, const IR::U32& storage_index,
|
||||
const IR::U32& offset) {
|
||||
const IR::Opcode new_opcode{GlobalToStorage(inst.GetOpcode())};
|
||||
const auto it{IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
block.PrependNewInst(it, new_opcode, {storage_index, offset, inst.Arg(1)});
|
||||
inst.Invalidate();
|
||||
}
|
||||
|
||||
/// Replace an atomic operation on global memory instruction with its storage buffer equivalent
|
||||
void ReplaceAtomic(IR::Block& block, IR::Inst& inst, const IR::U32& storage_index,
|
||||
const IR::U32& offset) {
|
||||
const IR::Opcode new_opcode{GlobalToStorage(inst.GetOpcode())};
|
||||
const auto it{IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
const IR::Value value{
|
||||
&*block.PrependNewInst(it, new_opcode, {storage_index, offset, inst.Arg(1)})};
|
||||
inst.ReplaceUsesWith(value);
|
||||
}
|
||||
|
||||
/// Replace a global memory instruction with its storage buffer equivalent
|
||||
void Replace(IR::Block& block, IR::Inst& inst, const IR::U32& storage_index,
|
||||
const IR::U32& offset) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::LoadGlobalS8:
|
||||
case IR::Opcode::LoadGlobalU8:
|
||||
case IR::Opcode::LoadGlobalS16:
|
||||
case IR::Opcode::LoadGlobalU16:
|
||||
case IR::Opcode::LoadGlobal32:
|
||||
case IR::Opcode::LoadGlobal64:
|
||||
case IR::Opcode::LoadGlobal128:
|
||||
return ReplaceLoad(block, inst, storage_index, offset);
|
||||
case IR::Opcode::WriteGlobalS8:
|
||||
case IR::Opcode::WriteGlobalU8:
|
||||
case IR::Opcode::WriteGlobalS16:
|
||||
case IR::Opcode::WriteGlobalU16:
|
||||
case IR::Opcode::WriteGlobal32:
|
||||
case IR::Opcode::WriteGlobal64:
|
||||
case IR::Opcode::WriteGlobal128:
|
||||
return ReplaceWrite(block, inst, storage_index, offset);
|
||||
case IR::Opcode::GlobalAtomicIAdd32:
|
||||
case IR::Opcode::GlobalAtomicSMin32:
|
||||
case IR::Opcode::GlobalAtomicUMin32:
|
||||
case IR::Opcode::GlobalAtomicSMax32:
|
||||
case IR::Opcode::GlobalAtomicUMax32:
|
||||
case IR::Opcode::GlobalAtomicInc32:
|
||||
case IR::Opcode::GlobalAtomicDec32:
|
||||
case IR::Opcode::GlobalAtomicAnd32:
|
||||
case IR::Opcode::GlobalAtomicOr32:
|
||||
case IR::Opcode::GlobalAtomicXor32:
|
||||
case IR::Opcode::GlobalAtomicExchange32:
|
||||
case IR::Opcode::GlobalAtomicIAdd64:
|
||||
case IR::Opcode::GlobalAtomicSMin64:
|
||||
case IR::Opcode::GlobalAtomicUMin64:
|
||||
case IR::Opcode::GlobalAtomicSMax64:
|
||||
case IR::Opcode::GlobalAtomicUMax64:
|
||||
case IR::Opcode::GlobalAtomicAnd64:
|
||||
case IR::Opcode::GlobalAtomicOr64:
|
||||
case IR::Opcode::GlobalAtomicXor64:
|
||||
case IR::Opcode::GlobalAtomicExchange64:
|
||||
case IR::Opcode::GlobalAtomicIAdd32x2:
|
||||
case IR::Opcode::GlobalAtomicSMin32x2:
|
||||
case IR::Opcode::GlobalAtomicUMin32x2:
|
||||
case IR::Opcode::GlobalAtomicSMax32x2:
|
||||
case IR::Opcode::GlobalAtomicUMax32x2:
|
||||
case IR::Opcode::GlobalAtomicAnd32x2:
|
||||
case IR::Opcode::GlobalAtomicOr32x2:
|
||||
case IR::Opcode::GlobalAtomicXor32x2:
|
||||
case IR::Opcode::GlobalAtomicExchange32x2:
|
||||
case IR::Opcode::GlobalAtomicAddF32:
|
||||
case IR::Opcode::GlobalAtomicAddF16x2:
|
||||
case IR::Opcode::GlobalAtomicAddF32x2:
|
||||
case IR::Opcode::GlobalAtomicMinF16x2:
|
||||
case IR::Opcode::GlobalAtomicMinF32x2:
|
||||
case IR::Opcode::GlobalAtomicMaxF16x2:
|
||||
case IR::Opcode::GlobalAtomicMaxF32x2:
|
||||
return ReplaceAtomic(block, inst, storage_index, offset);
|
||||
default:
|
||||
throw InvalidArgument("Invalid global memory opcode {}", inst.GetOpcode());
|
||||
}
|
||||
}
|
||||
} // Anonymous namespace
|
||||
|
||||
void GlobalMemoryToStorageBufferPass(IR::Program& program, const HostTranslateInfo& host_info) {
|
||||
StorageInfo info;
|
||||
for (IR::Block* const block : program.post_order_blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
if (!IsGlobalMemory(inst)) {
|
||||
continue;
|
||||
}
|
||||
CollectStorageBuffers(*block, inst, info);
|
||||
}
|
||||
}
|
||||
for (const StorageBufferAddr& storage_buffer : info.set) {
|
||||
program.info.storage_buffers_descriptors.push_back({
|
||||
.cbuf_index = storage_buffer.index,
|
||||
.cbuf_offset = storage_buffer.offset,
|
||||
.count = 1,
|
||||
.is_written = info.writes.contains(storage_buffer),
|
||||
});
|
||||
}
|
||||
for (const StorageInst& storage_inst : info.to_replace) {
|
||||
const StorageBufferAddr storage_buffer{storage_inst.storage_buffer};
|
||||
const auto it{info.set.find(storage_inst.storage_buffer)};
|
||||
const IR::U32 index{IR::Value{static_cast<u32>(info.set.index_of(it))}};
|
||||
IR::Block* const block{storage_inst.block};
|
||||
IR::Inst* const inst{storage_inst.inst};
|
||||
const IR::U32 offset{
|
||||
StorageOffset(*block, *inst, storage_buffer, u32(host_info.min_ssbo_alignment))};
|
||||
Replace(*block, *inst, index, offset);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Descriptors, typename Descriptor, typename Func>
|
||||
static u32 Add(Descriptors& descriptors, const Descriptor& desc, Func&& pred) {
|
||||
// TODO: Handle arrays
|
||||
const auto it{std::ranges::find_if(descriptors, pred)};
|
||||
if (it != descriptors.end()) {
|
||||
return static_cast<u32>(std::distance(descriptors.begin(), it));
|
||||
}
|
||||
descriptors.push_back(desc);
|
||||
return static_cast<u32>(descriptors.size()) - 1;
|
||||
}
|
||||
|
||||
void JoinStorageInfo(Info& base, Info& source) {
|
||||
auto& descriptors = base.storage_buffers_descriptors;
|
||||
for (auto& desc : source.storage_buffers_descriptors) {
|
||||
auto it{std::ranges::find_if(descriptors, [&desc](const auto& existing) {
|
||||
return desc.cbuf_index == existing.cbuf_index &&
|
||||
desc.cbuf_offset == existing.cbuf_offset && desc.count == existing.count;
|
||||
})};
|
||||
if (it != descriptors.end()) {
|
||||
it->is_written |= desc.is_written;
|
||||
continue;
|
||||
}
|
||||
descriptors.push_back(desc);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
43
gpu/third_party/suyu/shader_recompiler/ir_opt/identity_removal_pass.cpp
vendored
Normal file
43
gpu/third_party/suyu/shader_recompiler/ir_opt/identity_removal_pass.cpp
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <vector>
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
|
||||
void IdentityRemovalPass(IR::Program& program) {
|
||||
boost::container::small_vector<IR::Inst*, 16> to_invalidate;
|
||||
for (IR::Block* const block : program.blocks) {
|
||||
for (auto it = block->begin(); it != block->end();) {
|
||||
const size_t num_args{it->NumArgs()};
|
||||
for (size_t i = 0; i < num_args; ++i) {
|
||||
IR::Value arg = it->Arg(i);
|
||||
if (arg.IsIdentity()) {
|
||||
do { // Pointer chasing (3-derefs)
|
||||
arg = arg.Inst()->Arg(0);
|
||||
} while (arg.IsIdentity());
|
||||
it->SetArg(i, arg);
|
||||
}
|
||||
}
|
||||
|
||||
if (it->GetOpcode() == IR::Opcode::Identity || it->GetOpcode() == IR::Opcode::Void) {
|
||||
to_invalidate.push_back(&*it);
|
||||
it = block->Instructions().erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (IR::Inst* const inst : to_invalidate)
|
||||
inst->Invalidate();
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
68
gpu/third_party/suyu/shader_recompiler/ir_opt/layer_pass.cpp
vendored
Normal file
68
gpu/third_party/suyu/shader_recompiler/ir_opt/layer_pass.cpp
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
#include <optional>
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include "shader_recompiler/environment.h"
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/breadth_first_search.h"
|
||||
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
||||
#include "shader_recompiler/host_translate_info.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
#include "shader_recompiler/shader_info.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
|
||||
static IR::Attribute EmulatedLayerAttribute(VaryingState& stores) {
|
||||
for (u32 i = 0; i < 32; i++) {
|
||||
if (!stores.Generic(i)) {
|
||||
return IR::Attribute::Generic0X + (i * 4);
|
||||
}
|
||||
}
|
||||
return IR::Attribute::Layer;
|
||||
}
|
||||
|
||||
static bool PermittedProgramStage(Stage stage) {
|
||||
switch (stage) {
|
||||
case Stage::VertexA:
|
||||
case Stage::VertexB:
|
||||
case Stage::TessellationControl:
|
||||
case Stage::TessellationEval:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void LayerPass(IR::Program& program, const HostTranslateInfo& host_info) {
|
||||
if (host_info.support_viewport_index_layer || !PermittedProgramStage(program.stage)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto end{program.post_order_blocks.end()};
|
||||
const auto layer_attribute = EmulatedLayerAttribute(program.info.stores);
|
||||
bool requires_layer_emulation = false;
|
||||
|
||||
for (auto block = program.post_order_blocks.begin(); block != end; ++block) {
|
||||
for (IR::Inst& inst : (*block)->Instructions()) {
|
||||
if (inst.GetOpcode() == IR::Opcode::SetAttribute &&
|
||||
inst.Arg(0).Attribute() == IR::Attribute::Layer) {
|
||||
requires_layer_emulation = true;
|
||||
inst.SetArg(0, IR::Value{layer_attribute});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (requires_layer_emulation) {
|
||||
program.info.requires_layer_emulation = true;
|
||||
program.info.emulated_layer = layer_attribute;
|
||||
program.info.stores.Set(IR::Attribute::Layer, false);
|
||||
program.info.stores.Set(layer_attribute, true);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
139
gpu/third_party/suyu/shader_recompiler/ir_opt/lower_fp16_to_fp32.cpp
vendored
Normal file
139
gpu/third_party/suyu/shader_recompiler/ir_opt/lower_fp16_to_fp32.cpp
vendored
Normal file
@@ -0,0 +1,139 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
namespace {
|
||||
IR::Opcode Replace(IR::Opcode op) {
|
||||
switch (op) {
|
||||
case IR::Opcode::FPAbs16:
|
||||
return IR::Opcode::FPAbs32;
|
||||
case IR::Opcode::FPAdd16:
|
||||
return IR::Opcode::FPAdd32;
|
||||
case IR::Opcode::FPCeil16:
|
||||
return IR::Opcode::FPCeil32;
|
||||
case IR::Opcode::FPFloor16:
|
||||
return IR::Opcode::FPFloor32;
|
||||
case IR::Opcode::FPFma16:
|
||||
return IR::Opcode::FPFma32;
|
||||
case IR::Opcode::FPMul16:
|
||||
return IR::Opcode::FPMul32;
|
||||
case IR::Opcode::FPNeg16:
|
||||
return IR::Opcode::FPNeg32;
|
||||
case IR::Opcode::FPRoundEven16:
|
||||
return IR::Opcode::FPRoundEven32;
|
||||
case IR::Opcode::FPSaturate16:
|
||||
return IR::Opcode::FPSaturate32;
|
||||
case IR::Opcode::FPClamp16:
|
||||
return IR::Opcode::FPClamp32;
|
||||
case IR::Opcode::FPTrunc16:
|
||||
return IR::Opcode::FPTrunc32;
|
||||
case IR::Opcode::CompositeConstructF16x2:
|
||||
return IR::Opcode::CompositeConstructF32x2;
|
||||
case IR::Opcode::CompositeConstructF16x3:
|
||||
return IR::Opcode::CompositeConstructF32x3;
|
||||
case IR::Opcode::CompositeConstructF16x4:
|
||||
return IR::Opcode::CompositeConstructF32x4;
|
||||
case IR::Opcode::CompositeExtractF16x2:
|
||||
return IR::Opcode::CompositeExtractF32x2;
|
||||
case IR::Opcode::CompositeExtractF16x3:
|
||||
return IR::Opcode::CompositeExtractF32x3;
|
||||
case IR::Opcode::CompositeExtractF16x4:
|
||||
return IR::Opcode::CompositeExtractF32x4;
|
||||
case IR::Opcode::CompositeInsertF16x2:
|
||||
return IR::Opcode::CompositeInsertF32x2;
|
||||
case IR::Opcode::CompositeInsertF16x3:
|
||||
return IR::Opcode::CompositeInsertF32x3;
|
||||
case IR::Opcode::CompositeInsertF16x4:
|
||||
return IR::Opcode::CompositeInsertF32x4;
|
||||
case IR::Opcode::FPOrdEqual16:
|
||||
return IR::Opcode::FPOrdEqual32;
|
||||
case IR::Opcode::FPUnordEqual16:
|
||||
return IR::Opcode::FPUnordEqual32;
|
||||
case IR::Opcode::FPOrdNotEqual16:
|
||||
return IR::Opcode::FPOrdNotEqual32;
|
||||
case IR::Opcode::FPUnordNotEqual16:
|
||||
return IR::Opcode::FPUnordNotEqual32;
|
||||
case IR::Opcode::FPOrdLessThan16:
|
||||
return IR::Opcode::FPOrdLessThan32;
|
||||
case IR::Opcode::FPUnordLessThan16:
|
||||
return IR::Opcode::FPUnordLessThan32;
|
||||
case IR::Opcode::FPOrdGreaterThan16:
|
||||
return IR::Opcode::FPOrdGreaterThan32;
|
||||
case IR::Opcode::FPUnordGreaterThan16:
|
||||
return IR::Opcode::FPUnordGreaterThan32;
|
||||
case IR::Opcode::FPOrdLessThanEqual16:
|
||||
return IR::Opcode::FPOrdLessThanEqual32;
|
||||
case IR::Opcode::FPUnordLessThanEqual16:
|
||||
return IR::Opcode::FPUnordLessThanEqual32;
|
||||
case IR::Opcode::FPOrdGreaterThanEqual16:
|
||||
return IR::Opcode::FPOrdGreaterThanEqual32;
|
||||
case IR::Opcode::FPUnordGreaterThanEqual16:
|
||||
return IR::Opcode::FPUnordGreaterThanEqual32;
|
||||
case IR::Opcode::FPIsNan16:
|
||||
return IR::Opcode::FPIsNan32;
|
||||
case IR::Opcode::ConvertS16F16:
|
||||
return IR::Opcode::ConvertS16F32;
|
||||
case IR::Opcode::ConvertS32F16:
|
||||
return IR::Opcode::ConvertS32F32;
|
||||
case IR::Opcode::ConvertS64F16:
|
||||
return IR::Opcode::ConvertS64F32;
|
||||
case IR::Opcode::ConvertU16F16:
|
||||
return IR::Opcode::ConvertU16F32;
|
||||
case IR::Opcode::ConvertU32F16:
|
||||
return IR::Opcode::ConvertU32F32;
|
||||
case IR::Opcode::ConvertU64F16:
|
||||
return IR::Opcode::ConvertU64F32;
|
||||
case IR::Opcode::PackFloat2x16:
|
||||
return IR::Opcode::PackHalf2x16;
|
||||
case IR::Opcode::UnpackFloat2x16:
|
||||
return IR::Opcode::UnpackHalf2x16;
|
||||
case IR::Opcode::ConvertF32F16:
|
||||
return IR::Opcode::Identity;
|
||||
case IR::Opcode::ConvertF16F32:
|
||||
return IR::Opcode::Identity;
|
||||
case IR::Opcode::ConvertF16S8:
|
||||
return IR::Opcode::ConvertF32S8;
|
||||
case IR::Opcode::ConvertF16S16:
|
||||
return IR::Opcode::ConvertF32S16;
|
||||
case IR::Opcode::ConvertF16S32:
|
||||
return IR::Opcode::ConvertF32S32;
|
||||
case IR::Opcode::ConvertF16S64:
|
||||
return IR::Opcode::ConvertF32S64;
|
||||
case IR::Opcode::ConvertF16U8:
|
||||
return IR::Opcode::ConvertF32U8;
|
||||
case IR::Opcode::ConvertF16U16:
|
||||
return IR::Opcode::ConvertF32U16;
|
||||
case IR::Opcode::ConvertF16U32:
|
||||
return IR::Opcode::ConvertF32U32;
|
||||
case IR::Opcode::ConvertF16U64:
|
||||
return IR::Opcode::ConvertF32U64;
|
||||
case IR::Opcode::GlobalAtomicAddF16x2:
|
||||
return IR::Opcode::GlobalAtomicAddF32x2;
|
||||
case IR::Opcode::StorageAtomicAddF16x2:
|
||||
return IR::Opcode::StorageAtomicAddF32x2;
|
||||
case IR::Opcode::GlobalAtomicMinF16x2:
|
||||
return IR::Opcode::GlobalAtomicMinF32x2;
|
||||
case IR::Opcode::StorageAtomicMinF16x2:
|
||||
return IR::Opcode::StorageAtomicMinF32x2;
|
||||
case IR::Opcode::GlobalAtomicMaxF16x2:
|
||||
return IR::Opcode::GlobalAtomicMaxF32x2;
|
||||
case IR::Opcode::StorageAtomicMaxF16x2:
|
||||
return IR::Opcode::StorageAtomicMaxF32x2;
|
||||
default:
|
||||
return op;
|
||||
}
|
||||
}
|
||||
} // Anonymous namespace
|
||||
|
||||
void LowerFp16ToFp32(IR::Program& program) {
|
||||
for (IR::Block* const block : program.blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
inst.ReplaceOpcode(Replace(inst.GetOpcode()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
185
gpu/third_party/suyu/shader_recompiler/ir_opt/lower_fp64_to_fp32.cpp
vendored
Normal file
185
gpu/third_party/suyu/shader_recompiler/ir_opt/lower_fp64_to_fp32.cpp
vendored
Normal file
@@ -0,0 +1,185 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
||||
#include "shader_recompiler/frontend/ir/opcodes.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
namespace {
|
||||
|
||||
constexpr s32 F64ToF32Exp = +1023 - 127;
|
||||
constexpr s32 F32ToF64Exp = +127 - 1023;
|
||||
|
||||
IR::F32 PackedF64ToF32(IR::IREmitter& ir, const IR::Value& packed) {
|
||||
const IR::U32 lo{ir.CompositeExtract(packed, 0)};
|
||||
const IR::U32 hi{ir.CompositeExtract(packed, 1)};
|
||||
const IR::U32 sign{ir.BitFieldExtract(hi, ir.Imm32(31), ir.Imm32(1))};
|
||||
const IR::U32 exp{ir.BitFieldExtract(hi, ir.Imm32(20), ir.Imm32(11))};
|
||||
const IR::U32 mantissa_hi{ir.BitFieldExtract(hi, ir.Imm32(0), ir.Imm32(20))};
|
||||
const IR::U32 mantissa_lo{ir.BitFieldExtract(lo, ir.Imm32(29), ir.Imm32(3))};
|
||||
const IR::U32 mantissa{
|
||||
ir.BitwiseOr(ir.ShiftLeftLogical(mantissa_hi, ir.Imm32(3)), mantissa_lo)};
|
||||
const IR::U32 exp_if_subnorm{
|
||||
ir.Select(ir.IEqual(exp, ir.Imm32(0)), ir.Imm32(0), ir.IAdd(exp, ir.Imm32(F64ToF32Exp)))};
|
||||
const IR::U32 exp_if_infnan{
|
||||
ir.Select(ir.IEqual(exp, ir.Imm32(0x7ff)), ir.Imm32(0xff), exp_if_subnorm)};
|
||||
const IR::U32 result{
|
||||
ir.BitwiseOr(ir.ShiftLeftLogical(sign, ir.Imm32(31)),
|
||||
ir.BitwiseOr(ir.ShiftLeftLogical(exp_if_infnan, ir.Imm32(23)), mantissa))};
|
||||
return ir.BitCast<IR::F32>(result);
|
||||
}
|
||||
|
||||
IR::Value F32ToPackedF64(IR::IREmitter& ir, const IR::Value& raw) {
|
||||
const IR::U32 value{ir.BitCast<IR::U32>(IR::F32(raw))};
|
||||
const IR::U32 sign{ir.BitFieldExtract(value, ir.Imm32(31), ir.Imm32(1))};
|
||||
const IR::U32 exp{ir.BitFieldExtract(value, ir.Imm32(23), ir.Imm32(8))};
|
||||
const IR::U32 mantissa{ir.BitFieldExtract(value, ir.Imm32(0), ir.Imm32(23))};
|
||||
const IR::U32 mantissa_hi{ir.BitFieldExtract(mantissa, ir.Imm32(3), ir.Imm32(20))};
|
||||
const IR::U32 mantissa_lo{ir.BitFieldExtract(mantissa, ir.Imm32(0), ir.Imm32(3))};
|
||||
const IR::U32 exp_if_subnorm{
|
||||
ir.Select(ir.IEqual(exp, ir.Imm32(0)), ir.Imm32(0), ir.IAdd(exp, ir.Imm32(F32ToF64Exp)))};
|
||||
const IR::U32 exp_if_infnan{
|
||||
ir.Select(ir.IEqual(exp, ir.Imm32(0xff)), ir.Imm32(0x7ff), exp_if_subnorm)};
|
||||
const IR::U32 lo{ir.ShiftLeftLogical(mantissa_lo, ir.Imm32(29))};
|
||||
const IR::U32 hi{
|
||||
ir.BitwiseOr(ir.ShiftLeftLogical(sign, ir.Imm32(31)),
|
||||
ir.BitwiseOr(ir.ShiftLeftLogical(exp_if_infnan, ir.Imm32(20)), mantissa_hi))};
|
||||
return ir.CompositeConstruct(lo, hi);
|
||||
}
|
||||
|
||||
IR::Opcode Replace(IR::Opcode op) {
|
||||
switch (op) {
|
||||
case IR::Opcode::FPAbs64:
|
||||
return IR::Opcode::FPAbs32;
|
||||
case IR::Opcode::FPAdd64:
|
||||
return IR::Opcode::FPAdd32;
|
||||
case IR::Opcode::FPCeil64:
|
||||
return IR::Opcode::FPCeil32;
|
||||
case IR::Opcode::FPFloor64:
|
||||
return IR::Opcode::FPFloor32;
|
||||
case IR::Opcode::FPFma64:
|
||||
return IR::Opcode::FPFma32;
|
||||
case IR::Opcode::FPMul64:
|
||||
return IR::Opcode::FPMul32;
|
||||
case IR::Opcode::FPNeg64:
|
||||
return IR::Opcode::FPNeg32;
|
||||
case IR::Opcode::FPRoundEven64:
|
||||
return IR::Opcode::FPRoundEven32;
|
||||
case IR::Opcode::FPSaturate64:
|
||||
return IR::Opcode::FPSaturate32;
|
||||
case IR::Opcode::FPClamp64:
|
||||
return IR::Opcode::FPClamp32;
|
||||
case IR::Opcode::FPTrunc64:
|
||||
return IR::Opcode::FPTrunc32;
|
||||
case IR::Opcode::CompositeConstructF64x2:
|
||||
return IR::Opcode::CompositeConstructF32x2;
|
||||
case IR::Opcode::CompositeConstructF64x3:
|
||||
return IR::Opcode::CompositeConstructF32x3;
|
||||
case IR::Opcode::CompositeConstructF64x4:
|
||||
return IR::Opcode::CompositeConstructF32x4;
|
||||
case IR::Opcode::CompositeExtractF64x2:
|
||||
return IR::Opcode::CompositeExtractF32x2;
|
||||
case IR::Opcode::CompositeExtractF64x3:
|
||||
return IR::Opcode::CompositeExtractF32x3;
|
||||
case IR::Opcode::CompositeExtractF64x4:
|
||||
return IR::Opcode::CompositeExtractF32x4;
|
||||
case IR::Opcode::CompositeInsertF64x2:
|
||||
return IR::Opcode::CompositeInsertF32x2;
|
||||
case IR::Opcode::CompositeInsertF64x3:
|
||||
return IR::Opcode::CompositeInsertF32x3;
|
||||
case IR::Opcode::CompositeInsertF64x4:
|
||||
return IR::Opcode::CompositeInsertF32x4;
|
||||
case IR::Opcode::FPOrdEqual64:
|
||||
return IR::Opcode::FPOrdEqual32;
|
||||
case IR::Opcode::FPUnordEqual64:
|
||||
return IR::Opcode::FPUnordEqual32;
|
||||
case IR::Opcode::FPOrdNotEqual64:
|
||||
return IR::Opcode::FPOrdNotEqual32;
|
||||
case IR::Opcode::FPUnordNotEqual64:
|
||||
return IR::Opcode::FPUnordNotEqual32;
|
||||
case IR::Opcode::FPOrdLessThan64:
|
||||
return IR::Opcode::FPOrdLessThan32;
|
||||
case IR::Opcode::FPUnordLessThan64:
|
||||
return IR::Opcode::FPUnordLessThan32;
|
||||
case IR::Opcode::FPOrdGreaterThan64:
|
||||
return IR::Opcode::FPOrdGreaterThan32;
|
||||
case IR::Opcode::FPUnordGreaterThan64:
|
||||
return IR::Opcode::FPUnordGreaterThan32;
|
||||
case IR::Opcode::FPOrdLessThanEqual64:
|
||||
return IR::Opcode::FPOrdLessThanEqual32;
|
||||
case IR::Opcode::FPUnordLessThanEqual64:
|
||||
return IR::Opcode::FPUnordLessThanEqual32;
|
||||
case IR::Opcode::FPOrdGreaterThanEqual64:
|
||||
return IR::Opcode::FPOrdGreaterThanEqual32;
|
||||
case IR::Opcode::FPUnordGreaterThanEqual64:
|
||||
return IR::Opcode::FPUnordGreaterThanEqual32;
|
||||
case IR::Opcode::FPIsNan64:
|
||||
return IR::Opcode::FPIsNan32;
|
||||
case IR::Opcode::ConvertS16F64:
|
||||
return IR::Opcode::ConvertS16F32;
|
||||
case IR::Opcode::ConvertS32F64:
|
||||
return IR::Opcode::ConvertS32F32;
|
||||
case IR::Opcode::ConvertS64F64:
|
||||
return IR::Opcode::ConvertS64F32;
|
||||
case IR::Opcode::ConvertU16F64:
|
||||
return IR::Opcode::ConvertU16F32;
|
||||
case IR::Opcode::ConvertU32F64:
|
||||
return IR::Opcode::ConvertU32F32;
|
||||
case IR::Opcode::ConvertU64F64:
|
||||
return IR::Opcode::ConvertU64F32;
|
||||
case IR::Opcode::ConvertF32F64:
|
||||
return IR::Opcode::Identity;
|
||||
case IR::Opcode::ConvertF64F32:
|
||||
return IR::Opcode::Identity;
|
||||
case IR::Opcode::ConvertF64S8:
|
||||
return IR::Opcode::ConvertF32S8;
|
||||
case IR::Opcode::ConvertF64S16:
|
||||
return IR::Opcode::ConvertF32S16;
|
||||
case IR::Opcode::ConvertF64S32:
|
||||
return IR::Opcode::ConvertF32S32;
|
||||
case IR::Opcode::ConvertF64S64:
|
||||
return IR::Opcode::ConvertF32S64;
|
||||
case IR::Opcode::ConvertF64U8:
|
||||
return IR::Opcode::ConvertF32U8;
|
||||
case IR::Opcode::ConvertF64U16:
|
||||
return IR::Opcode::ConvertF32U16;
|
||||
case IR::Opcode::ConvertF64U32:
|
||||
return IR::Opcode::ConvertF32U32;
|
||||
case IR::Opcode::ConvertF64U64:
|
||||
return IR::Opcode::ConvertF32U64;
|
||||
default:
|
||||
return op;
|
||||
}
|
||||
}
|
||||
|
||||
void Lower(IR::Block& block, IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::PackDouble2x32: {
|
||||
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
|
||||
inst.ReplaceUsesWith(PackedF64ToF32(ir, inst.Arg(0)));
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::UnpackDouble2x32: {
|
||||
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
|
||||
inst.ReplaceUsesWith(F32ToPackedF64(ir, inst.Arg(0)));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
inst.ReplaceOpcode(Replace(inst.GetOpcode()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // Anonymous namespace
|
||||
|
||||
void LowerFp64ToFp32(IR::Program& program) {
|
||||
for (IR::Block* const block : program.blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
Lower(*block, inst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
237
gpu/third_party/suyu/shader_recompiler/ir_opt/lower_int64_to_int32.cpp
vendored
Normal file
237
gpu/third_party/suyu/shader_recompiler/ir_opt/lower_int64_to_int32.cpp
vendored
Normal file
@@ -0,0 +1,237 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "shader_recompiler/exception.h"
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
||||
#include "shader_recompiler/frontend/ir/program.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
namespace {
|
||||
std::pair<IR::U32, IR::U32> Unpack(IR::IREmitter& ir, const IR::Value& packed) {
|
||||
if (packed.IsImmediate()) {
|
||||
const u64 value{packed.U64()};
|
||||
return {
|
||||
ir.Imm32(static_cast<u32>(value)),
|
||||
ir.Imm32(static_cast<u32>(value >> 32)),
|
||||
};
|
||||
} else {
|
||||
return std::pair<IR::U32, IR::U32>{
|
||||
ir.CompositeExtract(packed, 0u),
|
||||
ir.CompositeExtract(packed, 1u),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void IAdd64To32(IR::Block& block, IR::Inst& inst) {
|
||||
if (inst.HasAssociatedPseudoOperation()) {
|
||||
throw NotImplementedException("IAdd64 emulation with pseudo instructions");
|
||||
}
|
||||
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
|
||||
const auto [a_lo, a_hi]{Unpack(ir, inst.Arg(0))};
|
||||
const auto [b_lo, b_hi]{Unpack(ir, inst.Arg(1))};
|
||||
|
||||
const IR::U32 ret_lo{ir.IAdd(a_lo, b_lo)};
|
||||
const IR::U32 carry{ir.Select(ir.GetCarryFromOp(ret_lo), ir.Imm32(1u), ir.Imm32(0u))};
|
||||
|
||||
const IR::U32 ret_hi{ir.IAdd(ir.IAdd(a_hi, b_hi), carry)};
|
||||
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
|
||||
}
|
||||
|
||||
void ISub64To32(IR::Block& block, IR::Inst& inst) {
|
||||
if (inst.HasAssociatedPseudoOperation()) {
|
||||
throw NotImplementedException("ISub64 emulation with pseudo instructions");
|
||||
}
|
||||
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
|
||||
const auto [a_lo, a_hi]{Unpack(ir, inst.Arg(0))};
|
||||
const auto [b_lo, b_hi]{Unpack(ir, inst.Arg(1))};
|
||||
|
||||
const IR::U32 ret_lo{ir.ISub(a_lo, b_lo)};
|
||||
const IR::U1 underflow{ir.IGreaterThan(ret_lo, a_lo, false)};
|
||||
const IR::U32 underflow_bit{ir.Select(underflow, ir.Imm32(1u), ir.Imm32(0u))};
|
||||
|
||||
const IR::U32 ret_hi{ir.ISub(ir.ISub(a_hi, b_hi), underflow_bit)};
|
||||
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
|
||||
}
|
||||
|
||||
void INeg64To32(IR::Block& block, IR::Inst& inst) {
|
||||
if (inst.HasAssociatedPseudoOperation()) {
|
||||
throw NotImplementedException("INeg64 emulation with pseudo instructions");
|
||||
}
|
||||
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
|
||||
auto [lo, hi]{Unpack(ir, inst.Arg(0))};
|
||||
lo = ir.BitwiseNot(lo);
|
||||
hi = ir.BitwiseNot(hi);
|
||||
|
||||
lo = ir.IAdd(lo, ir.Imm32(1));
|
||||
|
||||
const IR::U32 carry{ir.Select(ir.GetCarryFromOp(lo), ir.Imm32(1u), ir.Imm32(0u))};
|
||||
hi = ir.IAdd(hi, carry);
|
||||
|
||||
inst.ReplaceUsesWith(ir.CompositeConstruct(lo, hi));
|
||||
}
|
||||
|
||||
void ShiftLeftLogical64To32(IR::Block& block, IR::Inst& inst) {
|
||||
if (inst.HasAssociatedPseudoOperation()) {
|
||||
throw NotImplementedException("ShiftLeftLogical64 emulation with pseudo instructions");
|
||||
}
|
||||
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
|
||||
const auto [lo, hi]{Unpack(ir, inst.Arg(0))};
|
||||
const IR::U32 shift{inst.Arg(1)};
|
||||
|
||||
const IR::U32 shifted_lo{ir.ShiftLeftLogical(lo, shift)};
|
||||
const IR::U32 shifted_hi{ir.ShiftLeftLogical(hi, shift)};
|
||||
|
||||
const IR::U32 inv_shift{ir.ISub(shift, ir.Imm32(32))};
|
||||
const IR::U1 is_long{ir.IGreaterThanEqual(inv_shift, ir.Imm32(0), true)};
|
||||
const IR::U1 is_zero{ir.IEqual(shift, ir.Imm32(0))};
|
||||
|
||||
const IR::U32 long_ret_lo{ir.Imm32(0)};
|
||||
const IR::U32 long_ret_hi{ir.ShiftLeftLogical(lo, inv_shift)};
|
||||
|
||||
const IR::U32 shift_complement{ir.ISub(ir.Imm32(32), shift)};
|
||||
const IR::U32 lo_extract{ir.BitFieldExtract(lo, shift_complement, shift, false)};
|
||||
const IR::U32 short_ret_lo{shifted_lo};
|
||||
const IR::U32 short_ret_hi{ir.BitwiseOr(shifted_hi, lo_extract)};
|
||||
|
||||
const IR::U32 zero_ret_lo{lo};
|
||||
const IR::U32 zero_ret_hi{hi};
|
||||
|
||||
const IR::U32 non_zero_lo{ir.Select(is_long, long_ret_lo, short_ret_lo)};
|
||||
const IR::U32 non_zero_hi{ir.Select(is_long, long_ret_hi, short_ret_hi)};
|
||||
|
||||
const IR::U32 ret_lo{ir.Select(is_zero, zero_ret_lo, non_zero_lo)};
|
||||
const IR::U32 ret_hi{ir.Select(is_zero, zero_ret_hi, non_zero_hi)};
|
||||
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
|
||||
}
|
||||
|
||||
void ShiftRightLogical64To32(IR::Block& block, IR::Inst& inst) {
|
||||
if (inst.HasAssociatedPseudoOperation()) {
|
||||
throw NotImplementedException("ShiftRightLogical64 emulation with pseudo instructions");
|
||||
}
|
||||
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
|
||||
const auto [lo, hi]{Unpack(ir, inst.Arg(0))};
|
||||
const IR::U32 shift{inst.Arg(1)};
|
||||
|
||||
const IR::U32 shifted_lo{ir.ShiftRightLogical(lo, shift)};
|
||||
const IR::U32 shifted_hi{ir.ShiftRightLogical(hi, shift)};
|
||||
|
||||
const IR::U32 inv_shift{ir.ISub(shift, ir.Imm32(32))};
|
||||
const IR::U1 is_long{ir.IGreaterThanEqual(inv_shift, ir.Imm32(0), true)};
|
||||
const IR::U1 is_zero{ir.IEqual(shift, ir.Imm32(0))};
|
||||
|
||||
const IR::U32 long_ret_hi{ir.Imm32(0)};
|
||||
const IR::U32 long_ret_lo{ir.ShiftRightLogical(hi, inv_shift)};
|
||||
|
||||
const IR::U32 shift_complement{ir.ISub(ir.Imm32(32), shift)};
|
||||
const IR::U32 short_hi_extract{ir.BitFieldExtract(hi, ir.Imm32(0), shift)};
|
||||
const IR::U32 short_ret_hi{shifted_hi};
|
||||
const IR::U32 short_ret_lo{
|
||||
ir.BitFieldInsert(shifted_lo, short_hi_extract, shift_complement, shift)};
|
||||
|
||||
const IR::U32 zero_ret_lo{lo};
|
||||
const IR::U32 zero_ret_hi{hi};
|
||||
|
||||
const IR::U32 non_zero_lo{ir.Select(is_long, long_ret_lo, short_ret_lo)};
|
||||
const IR::U32 non_zero_hi{ir.Select(is_long, long_ret_hi, short_ret_hi)};
|
||||
|
||||
const IR::U32 ret_lo{ir.Select(is_zero, zero_ret_lo, non_zero_lo)};
|
||||
const IR::U32 ret_hi{ir.Select(is_zero, zero_ret_hi, non_zero_hi)};
|
||||
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
|
||||
}
|
||||
|
||||
void ShiftRightArithmetic64To32(IR::Block& block, IR::Inst& inst) {
|
||||
if (inst.HasAssociatedPseudoOperation()) {
|
||||
throw NotImplementedException("ShiftRightArithmetic64 emulation with pseudo instructions");
|
||||
}
|
||||
IR::IREmitter ir(block, IR::Block::InstructionList::s_iterator_to(inst));
|
||||
const auto [lo, hi]{Unpack(ir, inst.Arg(0))};
|
||||
const IR::U32 shift{inst.Arg(1)};
|
||||
|
||||
const IR::U32 shifted_lo{ir.ShiftRightLogical(lo, shift)};
|
||||
const IR::U32 shifted_hi{ir.ShiftRightArithmetic(hi, shift)};
|
||||
|
||||
const IR::U32 sign_extension{ir.ShiftRightArithmetic(hi, ir.Imm32(31))};
|
||||
|
||||
const IR::U32 inv_shift{ir.ISub(shift, ir.Imm32(32))};
|
||||
const IR::U1 is_long{ir.IGreaterThanEqual(inv_shift, ir.Imm32(0), true)};
|
||||
const IR::U1 is_zero{ir.IEqual(shift, ir.Imm32(0))};
|
||||
|
||||
const IR::U32 long_ret_hi{sign_extension};
|
||||
const IR::U32 long_ret_lo{ir.ShiftRightArithmetic(hi, inv_shift)};
|
||||
|
||||
const IR::U32 shift_complement{ir.ISub(ir.Imm32(32), shift)};
|
||||
const IR::U32 short_hi_extract(ir.BitFieldExtract(hi, ir.Imm32(0), shift));
|
||||
const IR::U32 short_ret_hi{shifted_hi};
|
||||
const IR::U32 short_ret_lo{
|
||||
ir.BitFieldInsert(shifted_lo, short_hi_extract, shift_complement, shift)};
|
||||
|
||||
const IR::U32 zero_ret_lo{lo};
|
||||
const IR::U32 zero_ret_hi{hi};
|
||||
|
||||
const IR::U32 non_zero_lo{ir.Select(is_long, long_ret_lo, short_ret_lo)};
|
||||
const IR::U32 non_zero_hi{ir.Select(is_long, long_ret_hi, short_ret_hi)};
|
||||
|
||||
const IR::U32 ret_lo{ir.Select(is_zero, zero_ret_lo, non_zero_lo)};
|
||||
const IR::U32 ret_hi{ir.Select(is_zero, zero_ret_hi, non_zero_hi)};
|
||||
inst.ReplaceUsesWith(ir.CompositeConstruct(ret_lo, ret_hi));
|
||||
}
|
||||
|
||||
void Lower(IR::Block& block, IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::PackUint2x32:
|
||||
case IR::Opcode::UnpackUint2x32:
|
||||
return inst.ReplaceOpcode(IR::Opcode::Identity);
|
||||
case IR::Opcode::IAdd64:
|
||||
return IAdd64To32(block, inst);
|
||||
case IR::Opcode::ISub64:
|
||||
return ISub64To32(block, inst);
|
||||
case IR::Opcode::INeg64:
|
||||
return INeg64To32(block, inst);
|
||||
case IR::Opcode::ShiftLeftLogical64:
|
||||
return ShiftLeftLogical64To32(block, inst);
|
||||
case IR::Opcode::ShiftRightLogical64:
|
||||
return ShiftRightLogical64To32(block, inst);
|
||||
case IR::Opcode::ShiftRightArithmetic64:
|
||||
return ShiftRightArithmetic64To32(block, inst);
|
||||
case IR::Opcode::SharedAtomicExchange64:
|
||||
return inst.ReplaceOpcode(IR::Opcode::SharedAtomicExchange32x2);
|
||||
case IR::Opcode::GlobalAtomicIAdd64:
|
||||
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicIAdd32x2);
|
||||
case IR::Opcode::GlobalAtomicSMin64:
|
||||
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicSMin32x2);
|
||||
case IR::Opcode::GlobalAtomicUMin64:
|
||||
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicUMin32x2);
|
||||
case IR::Opcode::GlobalAtomicSMax64:
|
||||
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicSMax32x2);
|
||||
case IR::Opcode::GlobalAtomicUMax64:
|
||||
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicUMax32x2);
|
||||
case IR::Opcode::GlobalAtomicAnd64:
|
||||
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicAnd32x2);
|
||||
case IR::Opcode::GlobalAtomicOr64:
|
||||
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicOr32x2);
|
||||
case IR::Opcode::GlobalAtomicXor64:
|
||||
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicXor32x2);
|
||||
case IR::Opcode::GlobalAtomicExchange64:
|
||||
return inst.ReplaceOpcode(IR::Opcode::GlobalAtomicExchange32x2);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
} // Anonymous namespace
|
||||
|
||||
void LowerInt64ToInt32(IR::Program& program) {
|
||||
const auto end{program.post_order_blocks.rend()};
|
||||
for (auto it = program.post_order_blocks.rbegin(); it != end; ++it) {
|
||||
IR::Block* const block{*it};
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
Lower(*block, inst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
38
gpu/third_party/suyu/shader_recompiler/ir_opt/passes.h
vendored
Normal file
38
gpu/third_party/suyu/shader_recompiler/ir_opt/passes.h
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "shader_recompiler/environment.h"
|
||||
#include "shader_recompiler/frontend/ir/program.h"
|
||||
|
||||
namespace Shader {
|
||||
struct HostTranslateInfo;
|
||||
}
|
||||
|
||||
namespace Shader::Optimization {
|
||||
|
||||
void CollectShaderInfoPass(Environment& env, IR::Program& program);
|
||||
void ConditionalBarrierPass(IR::Program& program);
|
||||
void ConstantPropagationPass(Environment& env, IR::Program& program);
|
||||
void DeadCodeEliminationPass(IR::Program& program);
|
||||
void GlobalMemoryToStorageBufferPass(IR::Program& program, const HostTranslateInfo& host_info);
|
||||
void IdentityRemovalPass(IR::Program& program);
|
||||
void LowerFp64ToFp32(IR::Program& program);
|
||||
void LowerFp16ToFp32(IR::Program& program);
|
||||
void LowerInt64ToInt32(IR::Program& program);
|
||||
void RescalingPass(IR::Program& program);
|
||||
void SsaRewritePass(IR::Program& program);
|
||||
void PositionPass(Environment& env, IR::Program& program);
|
||||
void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo& host_info);
|
||||
void LayerPass(IR::Program& program, const HostTranslateInfo& host_info);
|
||||
void VendorWorkaroundPass(IR::Program& program);
|
||||
void VerificationPass(const IR::Program& program);
|
||||
|
||||
// Dual Vertex
|
||||
void VertexATransformPass(IR::Program& program);
|
||||
void VertexBTransformPass(IR::Program& program);
|
||||
void JoinTextureInfo(Info& base, Info& source);
|
||||
void JoinStorageInfo(Info& base, Info& source);
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
77
gpu/third_party/suyu/shader_recompiler/ir_opt/position_pass.cpp
vendored
Normal file
77
gpu/third_party/suyu/shader_recompiler/ir_opt/position_pass.cpp
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
|
||||
namespace {
|
||||
struct PositionInst {
|
||||
IR::Inst* inst;
|
||||
IR::Block* block;
|
||||
IR::Attribute attr;
|
||||
};
|
||||
using PositionInstVector = boost::container::small_vector<PositionInst, 24>;
|
||||
} // Anonymous namespace
|
||||
|
||||
void PositionPass(Environment& env, IR::Program& program) {
|
||||
if (env.ShaderStage() != Stage::VertexB || env.ReadViewportTransformState()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Info& info{program.info};
|
||||
info.uses_render_area = true;
|
||||
|
||||
PositionInstVector to_replace;
|
||||
for (IR::Block* const block : program.post_order_blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::SetAttribute: {
|
||||
const IR::Attribute attr{inst.Arg(0).Attribute()};
|
||||
switch (attr) {
|
||||
case IR::Attribute::PositionX:
|
||||
case IR::Attribute::PositionY: {
|
||||
to_replace.push_back(PositionInst{.inst = &inst, .block = block, .attr = attr});
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (PositionInst& position_inst : to_replace) {
|
||||
IR::IREmitter ir{*position_inst.block,
|
||||
IR::Block::InstructionList::s_iterator_to(*position_inst.inst)};
|
||||
const IR::F32 value(position_inst.inst->Arg(1));
|
||||
const IR::F32F64 scale(ir.Imm32(2.f));
|
||||
const IR::F32 negative_one{ir.Imm32(-1.f)};
|
||||
switch (position_inst.attr) {
|
||||
case IR::Attribute::PositionX: {
|
||||
position_inst.inst->SetArg(
|
||||
1,
|
||||
ir.FPFma(value, ir.FPMul(ir.FPRecip(ir.RenderAreaWidth()), scale), negative_one));
|
||||
break;
|
||||
}
|
||||
case IR::Attribute::PositionY: {
|
||||
position_inst.inst->SetArg(
|
||||
1,
|
||||
ir.FPFma(value, ir.FPMul(ir.FPRecip(ir.RenderAreaHeight()), scale), negative_one));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace Shader::Optimization
|
||||
366
gpu/third_party/suyu/shader_recompiler/ir_opt/rescaling_pass.cpp
vendored
Normal file
366
gpu/third_party/suyu/shader_recompiler/ir_opt/rescaling_pass.cpp
vendored
Normal file
@@ -0,0 +1,366 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "common/settings.h"
|
||||
#include "shader_recompiler/environment.h"
|
||||
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
||||
#include "shader_recompiler/frontend/ir/modifiers.h"
|
||||
#include "shader_recompiler/frontend/ir/program.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
#include "shader_recompiler/shader_info.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
namespace {
|
||||
[[nodiscard]] bool IsTextureTypeRescalable(TextureType type) {
|
||||
switch (type) {
|
||||
case TextureType::Color2D:
|
||||
case TextureType::ColorArray2D:
|
||||
case TextureType::Color2DRect:
|
||||
return true;
|
||||
case TextureType::Color1D:
|
||||
case TextureType::ColorArray1D:
|
||||
case TextureType::Color3D:
|
||||
case TextureType::ColorCube:
|
||||
case TextureType::ColorArrayCube:
|
||||
case TextureType::Buffer:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void VisitMark(IR::Block& block, IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::ShuffleIndex:
|
||||
case IR::Opcode::ShuffleUp:
|
||||
case IR::Opcode::ShuffleDown:
|
||||
case IR::Opcode::ShuffleButterfly: {
|
||||
const IR::Value shfl_arg{inst.Arg(0)};
|
||||
if (shfl_arg.IsImmediate()) {
|
||||
break;
|
||||
}
|
||||
const IR::Inst* const arg_inst{shfl_arg.InstRecursive()};
|
||||
if (arg_inst->GetOpcode() != IR::Opcode::BitCastU32F32) {
|
||||
break;
|
||||
}
|
||||
const IR::Value bitcast_arg{arg_inst->Arg(0)};
|
||||
if (bitcast_arg.IsImmediate()) {
|
||||
break;
|
||||
}
|
||||
IR::Inst* const bitcast_inst{bitcast_arg.InstRecursive()};
|
||||
bool must_patch_outside = false;
|
||||
if (bitcast_inst->GetOpcode() == IR::Opcode::GetAttribute) {
|
||||
const IR::Attribute attr{bitcast_inst->Arg(0).Attribute()};
|
||||
switch (attr) {
|
||||
case IR::Attribute::PositionX:
|
||||
case IR::Attribute::PositionY:
|
||||
bitcast_inst->SetFlags<u32>(0xDEADBEEF);
|
||||
must_patch_outside = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (must_patch_outside) {
|
||||
const auto it{IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
IR::IREmitter ir{block, it};
|
||||
if (Settings::values.rescale_hack.GetValue()) {
|
||||
const IR::F32 new_inst{&*block.PrependNewInst(it, inst)};
|
||||
const IR::F32 up_factor{ir.FPRecip(ir.ResolutionDownFactor())};
|
||||
const IR::Value converted{ir.FPMul(new_inst, up_factor)};
|
||||
inst.ReplaceUsesWith(converted);
|
||||
} else {
|
||||
IR::Inst* const new_inst{&*block.PrependNewInst(it, inst)};
|
||||
const IR::F32 new_bitcast{ir.ConvertUToF(32, 32, IR::Value{new_inst})};
|
||||
const IR::F32 up_factor{ir.FPRecip(ir.ResolutionDownFactor())};
|
||||
const IR::Value converted{ir.FPMul(new_bitcast, up_factor)};
|
||||
inst.ReplaceUsesWith(converted);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void PatchFragCoord(IR::Block& block, IR::Inst& inst) {
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
const IR::F32 down_factor{ir.ResolutionDownFactor()};
|
||||
const IR::F32 frag_coord{ir.GetAttribute(inst.Arg(0).Attribute())};
|
||||
const IR::F32 downscaled_frag_coord{ir.FPMul(frag_coord, down_factor)};
|
||||
inst.ReplaceUsesWith(downscaled_frag_coord);
|
||||
}
|
||||
|
||||
void PatchPointSize(IR::Block& block, IR::Inst& inst) {
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
const IR::F32 point_value{inst.Arg(1)};
|
||||
const IR::F32 up_factor{ir.FPRecip(ir.ResolutionDownFactor())};
|
||||
const IR::F32 upscaled_point_value{ir.FPMul(point_value, up_factor)};
|
||||
inst.SetArg(1, upscaled_point_value);
|
||||
}
|
||||
|
||||
[[nodiscard]] IR::U32 Scale(IR::IREmitter& ir, const IR::U1& is_scaled, const IR::U32& value) {
|
||||
IR::U32 scaled_value{value};
|
||||
if (const u32 up_scale = Settings::values.resolution_info.up_scale; up_scale != 1) {
|
||||
scaled_value = ir.IMul(scaled_value, ir.Imm32(up_scale));
|
||||
}
|
||||
if (const u32 down_shift = Settings::values.resolution_info.down_shift; down_shift != 0) {
|
||||
scaled_value = ir.ShiftRightArithmetic(scaled_value, ir.Imm32(down_shift));
|
||||
}
|
||||
return IR::U32{ir.Select(is_scaled, scaled_value, value)};
|
||||
}
|
||||
|
||||
[[nodiscard]] IR::U32 SubScale(IR::IREmitter& ir, const IR::U1& is_scaled, const IR::U32& value,
|
||||
const IR::Attribute attrib) {
|
||||
const IR::F32 up_factor{ir.Imm32(Settings::values.resolution_info.up_factor)};
|
||||
const IR::F32 base{ir.FPMul(ir.ConvertUToF(32, 32, value), up_factor)};
|
||||
const IR::F32 frag_coord{ir.GetAttribute(attrib)};
|
||||
const IR::F32 down_factor{ir.Imm32(Settings::values.resolution_info.down_factor)};
|
||||
const IR::F32 floor{ir.FPMul(up_factor, ir.FPFloor(ir.FPMul(frag_coord, down_factor)))};
|
||||
const IR::F16F32F64 deviation{ir.FPAdd(base, ir.FPAdd(frag_coord, ir.FPNeg(floor)))};
|
||||
return IR::U32{ir.Select(is_scaled, ir.ConvertFToU(32, deviation), value)};
|
||||
}
|
||||
|
||||
[[nodiscard]] IR::U32 DownScale(IR::IREmitter& ir, const IR::U1& is_scaled, const IR::U32& value) {
|
||||
IR::U32 scaled_value{value};
|
||||
if (const u32 down_shift = Settings::values.resolution_info.down_shift; down_shift != 0) {
|
||||
scaled_value = ir.ShiftLeftLogical(scaled_value, ir.Imm32(down_shift));
|
||||
}
|
||||
if (const u32 up_scale = Settings::values.resolution_info.up_scale; up_scale != 1) {
|
||||
scaled_value = ir.IDiv(scaled_value, ir.Imm32(up_scale));
|
||||
}
|
||||
return IR::U32{ir.Select(is_scaled, scaled_value, value)};
|
||||
}
|
||||
|
||||
void PatchImageQueryDimensions(IR::Block& block, IR::Inst& inst) {
|
||||
const auto it{IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
const auto info{inst.Flags<IR::TextureInstInfo>()};
|
||||
const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))};
|
||||
switch (info.type) {
|
||||
case TextureType::Color2D:
|
||||
case TextureType::ColorArray2D:
|
||||
case TextureType::Color2DRect: {
|
||||
const IR::Value new_inst{&*block.PrependNewInst(it, inst)};
|
||||
const IR::U32 width{DownScale(ir, is_scaled, IR::U32{ir.CompositeExtract(new_inst, 0)})};
|
||||
const IR::U32 height{DownScale(ir, is_scaled, IR::U32{ir.CompositeExtract(new_inst, 1)})};
|
||||
const IR::Value replacement{ir.CompositeConstruct(
|
||||
width, height, ir.CompositeExtract(new_inst, 2), ir.CompositeExtract(new_inst, 3))};
|
||||
inst.ReplaceUsesWith(replacement);
|
||||
break;
|
||||
}
|
||||
case TextureType::Color1D:
|
||||
case TextureType::ColorArray1D:
|
||||
case TextureType::Color3D:
|
||||
case TextureType::ColorCube:
|
||||
case TextureType::ColorArrayCube:
|
||||
case TextureType::Buffer:
|
||||
// Nothing to patch here
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ScaleIntegerComposite(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled,
|
||||
size_t index) {
|
||||
const IR::Value composite{inst.Arg(index)};
|
||||
if (composite.IsEmpty()) {
|
||||
return;
|
||||
}
|
||||
const auto info{inst.Flags<IR::TextureInstInfo>()};
|
||||
const IR::U32 x{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 0)})};
|
||||
const IR::U32 y{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 1)})};
|
||||
switch (info.type) {
|
||||
case TextureType::Color2D:
|
||||
case TextureType::Color2DRect:
|
||||
inst.SetArg(index, ir.CompositeConstruct(x, y));
|
||||
break;
|
||||
case TextureType::ColorArray2D: {
|
||||
const IR::U32 z{ir.CompositeExtract(composite, 2)};
|
||||
inst.SetArg(index, ir.CompositeConstruct(x, y, z));
|
||||
break;
|
||||
}
|
||||
case TextureType::Color1D:
|
||||
case TextureType::ColorArray1D:
|
||||
case TextureType::Color3D:
|
||||
case TextureType::ColorCube:
|
||||
case TextureType::ColorArrayCube:
|
||||
case TextureType::Buffer:
|
||||
// Nothing to patch here
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ScaleIntegerOffsetComposite(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled,
|
||||
size_t index) {
|
||||
const IR::Value composite{inst.Arg(index)};
|
||||
if (composite.IsEmpty()) {
|
||||
return;
|
||||
}
|
||||
const auto info{inst.Flags<IR::TextureInstInfo>()};
|
||||
const IR::U32 x{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 0)})};
|
||||
const IR::U32 y{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(composite, 1)})};
|
||||
switch (info.type) {
|
||||
case TextureType::ColorArray2D:
|
||||
case TextureType::Color2D:
|
||||
case TextureType::Color2DRect:
|
||||
inst.SetArg(index, ir.CompositeConstruct(x, y));
|
||||
break;
|
||||
case TextureType::Color1D:
|
||||
case TextureType::ColorArray1D:
|
||||
case TextureType::Color3D:
|
||||
case TextureType::ColorCube:
|
||||
case TextureType::ColorArrayCube:
|
||||
case TextureType::Buffer:
|
||||
// Nothing to patch here
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SubScaleCoord(IR::IREmitter& ir, IR::Inst& inst, const IR::U1& is_scaled) {
|
||||
const auto info{inst.Flags<IR::TextureInstInfo>()};
|
||||
const IR::Value coord{inst.Arg(1)};
|
||||
const IR::U32 coord_x{ir.CompositeExtract(coord, 0)};
|
||||
const IR::U32 coord_y{ir.CompositeExtract(coord, 1)};
|
||||
|
||||
const IR::U32 scaled_x{SubScale(ir, is_scaled, coord_x, IR::Attribute::PositionX)};
|
||||
const IR::U32 scaled_y{SubScale(ir, is_scaled, coord_y, IR::Attribute::PositionY)};
|
||||
switch (info.type) {
|
||||
case TextureType::Color2D:
|
||||
case TextureType::Color2DRect:
|
||||
inst.SetArg(1, ir.CompositeConstruct(scaled_x, scaled_y));
|
||||
break;
|
||||
case TextureType::ColorArray2D: {
|
||||
const IR::U32 z{ir.CompositeExtract(coord, 2)};
|
||||
inst.SetArg(1, ir.CompositeConstruct(scaled_x, scaled_y, z));
|
||||
break;
|
||||
}
|
||||
case TextureType::Color1D:
|
||||
case TextureType::ColorArray1D:
|
||||
case TextureType::Color3D:
|
||||
case TextureType::ColorCube:
|
||||
case TextureType::ColorArrayCube:
|
||||
case TextureType::Buffer:
|
||||
// Nothing to patch here
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SubScaleImageFetch(IR::Block& block, IR::Inst& inst) {
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
const auto info{inst.Flags<IR::TextureInstInfo>()};
|
||||
if (!IsTextureTypeRescalable(info.type)) {
|
||||
return;
|
||||
}
|
||||
const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))};
|
||||
SubScaleCoord(ir, inst, is_scaled);
|
||||
// Scale ImageFetch offset
|
||||
ScaleIntegerOffsetComposite(ir, inst, is_scaled, 2);
|
||||
}
|
||||
|
||||
void SubScaleImageRead(IR::Block& block, IR::Inst& inst) {
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
const auto info{inst.Flags<IR::TextureInstInfo>()};
|
||||
if (!IsTextureTypeRescalable(info.type)) {
|
||||
return;
|
||||
}
|
||||
const IR::U1 is_scaled{ir.IsImageScaled(ir.Imm32(info.descriptor_index))};
|
||||
SubScaleCoord(ir, inst, is_scaled);
|
||||
}
|
||||
|
||||
void PatchImageFetch(IR::Block& block, IR::Inst& inst) {
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
const auto info{inst.Flags<IR::TextureInstInfo>()};
|
||||
if (!IsTextureTypeRescalable(info.type)) {
|
||||
return;
|
||||
}
|
||||
const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))};
|
||||
ScaleIntegerComposite(ir, inst, is_scaled, 1);
|
||||
// Scale ImageFetch offset
|
||||
ScaleIntegerOffsetComposite(ir, inst, is_scaled, 2);
|
||||
}
|
||||
|
||||
void PatchImageRead(IR::Block& block, IR::Inst& inst) {
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
const auto info{inst.Flags<IR::TextureInstInfo>()};
|
||||
if (!IsTextureTypeRescalable(info.type)) {
|
||||
return;
|
||||
}
|
||||
const IR::U1 is_scaled{ir.IsImageScaled(ir.Imm32(info.descriptor_index))};
|
||||
ScaleIntegerComposite(ir, inst, is_scaled, 1);
|
||||
}
|
||||
|
||||
void Visit(const IR::Program& program, IR::Block& block, IR::Inst& inst) {
|
||||
const bool is_fragment_shader{program.stage == Stage::Fragment};
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::GetAttribute: {
|
||||
const IR::Attribute attr{inst.Arg(0).Attribute()};
|
||||
switch (attr) {
|
||||
case IR::Attribute::PositionX:
|
||||
case IR::Attribute::PositionY:
|
||||
if (is_fragment_shader && inst.Flags<u32>() != 0xDEADBEEF) {
|
||||
PatchFragCoord(block, inst);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::SetAttribute: {
|
||||
const IR::Attribute attr{inst.Arg(0).Attribute()};
|
||||
switch (attr) {
|
||||
case IR::Attribute::PointSize:
|
||||
if (inst.Flags<u32>() != 0xDEADBEEF) {
|
||||
PatchPointSize(block, inst);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::ImageQueryDimensions:
|
||||
PatchImageQueryDimensions(block, inst);
|
||||
break;
|
||||
case IR::Opcode::ImageFetch:
|
||||
if (is_fragment_shader) {
|
||||
SubScaleImageFetch(block, inst);
|
||||
} else {
|
||||
PatchImageFetch(block, inst);
|
||||
}
|
||||
break;
|
||||
case IR::Opcode::ImageRead:
|
||||
if (is_fragment_shader) {
|
||||
SubScaleImageRead(block, inst);
|
||||
} else {
|
||||
PatchImageRead(block, inst);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
} // Anonymous namespace
|
||||
|
||||
void RescalingPass(IR::Program& program) {
|
||||
const bool is_fragment_shader{program.stage == Stage::Fragment};
|
||||
if (is_fragment_shader) {
|
||||
for (IR::Block* const block : program.post_order_blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
VisitMark(*block, inst);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (IR::Block* const block : program.post_order_blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
Visit(program, *block, inst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
417
gpu/third_party/suyu/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
vendored
Normal file
417
gpu/third_party/suyu/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
vendored
Normal file
@@ -0,0 +1,417 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
// This file implements the SSA rewriting algorithm proposed in
|
||||
//
|
||||
// Simple and Efficient Construction of Static Single Assignment Form.
|
||||
// Braun M., Buchwald S., Hack S., Leiba R., Mallon C., Zwinkau A. (2013)
|
||||
// In: Jhala R., De Bosschere K. (eds)
|
||||
// Compiler Construction. CC 2013.
|
||||
// Lecture Notes in Computer Science, vol 7791.
|
||||
// Springer, Berlin, Heidelberg
|
||||
//
|
||||
// https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
|
||||
//
|
||||
|
||||
#include <deque>
|
||||
#include <map>
|
||||
#include <span>
|
||||
#include <ankerl/unordered_dense.h>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/opcodes.h"
|
||||
#include "shader_recompiler/frontend/ir/pred.h"
|
||||
#include "shader_recompiler/frontend/ir/reg.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
namespace {
|
||||
struct FlagTag {
|
||||
auto operator<=>(const FlagTag&) const noexcept = default;
|
||||
};
|
||||
struct ZeroFlagTag : FlagTag {};
|
||||
struct SignFlagTag : FlagTag {};
|
||||
struct CarryFlagTag : FlagTag {};
|
||||
struct OverflowFlagTag : FlagTag {};
|
||||
|
||||
struct GotoVariable : FlagTag {
|
||||
GotoVariable() = default;
|
||||
explicit GotoVariable(u32 index_) : index{index_} {}
|
||||
|
||||
auto operator<=>(const GotoVariable&) const noexcept = default;
|
||||
|
||||
u32 index;
|
||||
};
|
||||
|
||||
struct IndirectBranchVariable {
|
||||
auto operator<=>(const IndirectBranchVariable&) const noexcept = default;
|
||||
};
|
||||
|
||||
using Variant = std::variant<IR::Reg, IR::Pred, ZeroFlagTag, SignFlagTag, CarryFlagTag, OverflowFlagTag, GotoVariable, IndirectBranchVariable>;
|
||||
// TODO: majority of these require stable iterators, test with XC beforehand
|
||||
using ValueMap = std::unordered_map<IR::Block*, IR::Value>;
|
||||
|
||||
struct DefTable {
|
||||
const IR::Value& Def(IR::Block* block, IR::Reg variable) {
|
||||
return block->SsaRegValue(variable);
|
||||
}
|
||||
void SetDef(IR::Block* block, IR::Reg variable, const IR::Value& value) {
|
||||
block->SetSsaRegValue(variable, value);
|
||||
}
|
||||
|
||||
const IR::Value& Def(IR::Block* block, IR::Pred variable) {
|
||||
return preds[IR::PredIndex(variable)][block];
|
||||
}
|
||||
void SetDef(IR::Block* block, IR::Pred variable, const IR::Value& value) {
|
||||
preds[IR::PredIndex(variable)].insert_or_assign(block, value);
|
||||
}
|
||||
|
||||
const IR::Value& Def(IR::Block* block, GotoVariable variable) {
|
||||
return goto_vars[variable.index][block];
|
||||
}
|
||||
void SetDef(IR::Block* block, GotoVariable variable, const IR::Value& value) {
|
||||
goto_vars[variable.index].insert_or_assign(block, value);
|
||||
}
|
||||
|
||||
const IR::Value& Def(IR::Block* block, IndirectBranchVariable) {
|
||||
return indirect_branch_var[block];
|
||||
}
|
||||
void SetDef(IR::Block* block, IndirectBranchVariable, const IR::Value& value) {
|
||||
indirect_branch_var.insert_or_assign(block, value);
|
||||
}
|
||||
|
||||
const IR::Value& Def(IR::Block* block, ZeroFlagTag) {
|
||||
return zero_flag[block];
|
||||
}
|
||||
void SetDef(IR::Block* block, ZeroFlagTag, const IR::Value& value) {
|
||||
zero_flag.insert_or_assign(block, value);
|
||||
}
|
||||
|
||||
const IR::Value& Def(IR::Block* block, SignFlagTag) {
|
||||
return sign_flag[block];
|
||||
}
|
||||
void SetDef(IR::Block* block, SignFlagTag, const IR::Value& value) {
|
||||
sign_flag.insert_or_assign(block, value);
|
||||
}
|
||||
|
||||
const IR::Value& Def(IR::Block* block, CarryFlagTag) {
|
||||
return carry_flag[block];
|
||||
}
|
||||
void SetDef(IR::Block* block, CarryFlagTag, const IR::Value& value) {
|
||||
carry_flag.insert_or_assign(block, value);
|
||||
}
|
||||
|
||||
const IR::Value& Def(IR::Block* block, OverflowFlagTag) {
|
||||
return overflow_flag[block];
|
||||
}
|
||||
void SetDef(IR::Block* block, OverflowFlagTag, const IR::Value& value) {
|
||||
overflow_flag.insert_or_assign(block, value);
|
||||
}
|
||||
|
||||
std::array<ValueMap, IR::NUM_USER_PREDS> preds;
|
||||
// TODO: Requires stable iterators
|
||||
std::unordered_map<u32, ValueMap> goto_vars;
|
||||
ValueMap indirect_branch_var;
|
||||
ValueMap zero_flag;
|
||||
ValueMap sign_flag;
|
||||
ValueMap carry_flag;
|
||||
ValueMap overflow_flag;
|
||||
};
|
||||
|
||||
IR::Opcode UndefOpcode(IR::Reg) noexcept {
|
||||
return IR::Opcode::UndefU32;
|
||||
}
|
||||
|
||||
IR::Opcode UndefOpcode(IR::Pred) noexcept {
|
||||
return IR::Opcode::UndefU1;
|
||||
}
|
||||
|
||||
IR::Opcode UndefOpcode(const FlagTag&) noexcept {
|
||||
return IR::Opcode::UndefU1;
|
||||
}
|
||||
|
||||
IR::Opcode UndefOpcode(IndirectBranchVariable) noexcept {
|
||||
return IR::Opcode::UndefU32;
|
||||
}
|
||||
|
||||
enum class Status {
|
||||
Start,
|
||||
SetValue,
|
||||
PreparePhiArgument,
|
||||
PushPhiArgument,
|
||||
};
|
||||
|
||||
template <typename Type>
|
||||
struct ReadState {
|
||||
ReadState(IR::Block* block_) : block{block_} {}
|
||||
ReadState() = default;
|
||||
|
||||
IR::Block* block{};
|
||||
IR::Value result{};
|
||||
IR::Inst* phi{};
|
||||
IR::Block* const* pred_it{};
|
||||
IR::Block* const* pred_end{};
|
||||
Status pc{Status::Start};
|
||||
};
|
||||
|
||||
class Pass {
|
||||
public:
|
||||
template <typename Type>
|
||||
void WriteVariable(Type variable, IR::Block* block, const IR::Value& value) {
|
||||
current_def.SetDef(block, variable, value);
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
IR::Value ReadVariable(Type variable, IR::Block* root_block) {
|
||||
// TODO: Windows commits sodoku if you use small_vector
|
||||
std::vector<ReadState<Type>> stack{
|
||||
ReadState<Type>(nullptr),
|
||||
ReadState<Type>(root_block),
|
||||
};
|
||||
const auto prepare_phi_operand{[&] {
|
||||
if (stack.back().pred_it == stack.back().pred_end) {
|
||||
IR::Inst* const phi{stack.back().phi};
|
||||
IR::Block* const block{stack.back().block};
|
||||
const IR::Value result{TryRemoveTrivialPhi(*phi, block, UndefOpcode(variable))};
|
||||
stack.pop_back();
|
||||
stack.back().result = result;
|
||||
WriteVariable(variable, block, result);
|
||||
} else {
|
||||
IR::Block* const imm_pred{*stack.back().pred_it};
|
||||
stack.back().pc = Status::PushPhiArgument;
|
||||
stack.emplace_back(imm_pred);
|
||||
}
|
||||
}};
|
||||
do {
|
||||
IR::Block* const block{stack.back().block};
|
||||
switch (stack.back().pc) {
|
||||
case Status::Start: {
|
||||
if (const IR::Value& def = current_def.Def(block, variable); !def.IsEmpty()) {
|
||||
stack.back().result = def;
|
||||
} else if (!block->IsSsaSealed()) {
|
||||
// Incomplete CFG
|
||||
IR::Inst* phi{&*block->PrependNewInst(block->begin(), IR::Opcode::Phi)};
|
||||
phi->SetFlags(IR::TypeOf(UndefOpcode(variable)));
|
||||
|
||||
incomplete_phis[block].insert_or_assign(variable, phi);
|
||||
stack.back().result = IR::Value{&*phi};
|
||||
} else if (const std::span imm_preds = block->ImmPredecessors();
|
||||
imm_preds.size() == 1) {
|
||||
// Optimize the common case of one predecessor: no phi needed
|
||||
stack.back().pc = Status::SetValue;
|
||||
stack.emplace_back(imm_preds.front());
|
||||
break;
|
||||
} else {
|
||||
// Break potential cycles with operandless phi
|
||||
IR::Inst* const phi{&*block->PrependNewInst(block->begin(), IR::Opcode::Phi)};
|
||||
phi->SetFlags(IR::TypeOf(UndefOpcode(variable)));
|
||||
|
||||
WriteVariable(variable, block, IR::Value{phi});
|
||||
|
||||
stack.back().phi = phi;
|
||||
stack.back().pred_it = imm_preds.data();
|
||||
stack.back().pred_end = imm_preds.data() + imm_preds.size();
|
||||
prepare_phi_operand();
|
||||
break;
|
||||
}
|
||||
}
|
||||
[[fallthrough]];
|
||||
case Status::SetValue: {
|
||||
const IR::Value result{stack.back().result};
|
||||
WriteVariable(variable, block, result);
|
||||
stack.pop_back();
|
||||
stack.back().result = result;
|
||||
break;
|
||||
}
|
||||
case Status::PushPhiArgument: {
|
||||
IR::Inst* const phi{stack.back().phi};
|
||||
phi->AddPhiOperand(*stack.back().pred_it, stack.back().result);
|
||||
++stack.back().pred_it;
|
||||
}
|
||||
[[fallthrough]];
|
||||
case Status::PreparePhiArgument:
|
||||
prepare_phi_operand();
|
||||
break;
|
||||
}
|
||||
} while (stack.size() > 1);
|
||||
return stack.back().result;
|
||||
}
|
||||
|
||||
void SealBlock(IR::Block* block) {
|
||||
const auto it{incomplete_phis.find(block)};
|
||||
if (it != incomplete_phis.end()) {
|
||||
for (auto& pair : it->second) {
|
||||
auto& variant{pair.first};
|
||||
auto& phi{pair.second};
|
||||
std::visit([&](auto& variable) { AddPhiOperands(variable, *phi, block); }, variant);
|
||||
}
|
||||
}
|
||||
block->SsaSeal();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename Type>
|
||||
IR::Value AddPhiOperands(Type variable, IR::Inst& phi, IR::Block* block) {
|
||||
for (IR::Block* const imm_pred : block->ImmPredecessors()) {
|
||||
phi.AddPhiOperand(imm_pred, ReadVariable(variable, imm_pred));
|
||||
}
|
||||
return TryRemoveTrivialPhi(phi, block, UndefOpcode(variable));
|
||||
}
|
||||
|
||||
IR::Value TryRemoveTrivialPhi(IR::Inst& phi, IR::Block* block, IR::Opcode undef_opcode) {
|
||||
IR::Value same;
|
||||
const size_t num_args{phi.NumArgs()};
|
||||
for (size_t arg_index = 0; arg_index < num_args; ++arg_index) {
|
||||
const IR::Value& op{phi.Arg(arg_index)};
|
||||
if (op.Resolve() == same.Resolve() || op == IR::Value{&phi}) {
|
||||
// Unique value or self-reference
|
||||
continue;
|
||||
}
|
||||
if (!same.IsEmpty()) {
|
||||
// The phi merges at least two values: not trivial
|
||||
return IR::Value{&phi};
|
||||
}
|
||||
same = op;
|
||||
}
|
||||
// Remove the phi node from the block, it will be reinserted
|
||||
IR::Block::InstructionList& list{block->Instructions()};
|
||||
list.erase(IR::Block::InstructionList::s_iterator_to(phi));
|
||||
|
||||
// Find the first non-phi instruction and use it as an insertion point
|
||||
IR::Block::iterator reinsert_point{std::ranges::find_if_not(list, IR::IsPhi)};
|
||||
if (same.IsEmpty()) {
|
||||
// The phi is unreachable or in the start block
|
||||
// Insert an undefined instruction and make it the phi node replacement
|
||||
// The "phi" node reinsertion point is specified after this instruction
|
||||
reinsert_point = block->PrependNewInst(reinsert_point, undef_opcode);
|
||||
same = IR::Value{&*reinsert_point};
|
||||
++reinsert_point;
|
||||
}
|
||||
// Reinsert the phi node and reroute all its uses to the "same" value
|
||||
list.insert(reinsert_point, phi);
|
||||
phi.ReplaceUsesWith(same);
|
||||
// TODO: Try to recursively remove all phi users, which might have become trivial
|
||||
return same;
|
||||
}
|
||||
|
||||
// TODO: Windows dies with stack exhaustion?
|
||||
std::unordered_map<IR::Block*, std::map<Variant, IR::Inst*>> incomplete_phis;
|
||||
DefTable current_def;
|
||||
};
|
||||
|
||||
void VisitInst(Pass& pass, IR::Block* block, IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::SetRegister:
|
||||
if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
|
||||
pass.WriteVariable(reg, block, inst.Arg(1));
|
||||
}
|
||||
break;
|
||||
case IR::Opcode::SetPred:
|
||||
if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
|
||||
pass.WriteVariable(pred, block, inst.Arg(1));
|
||||
}
|
||||
break;
|
||||
case IR::Opcode::SetGotoVariable:
|
||||
pass.WriteVariable(GotoVariable{inst.Arg(0).U32()}, block, inst.Arg(1));
|
||||
break;
|
||||
case IR::Opcode::SetIndirectBranchVariable:
|
||||
pass.WriteVariable(IndirectBranchVariable{}, block, inst.Arg(0));
|
||||
break;
|
||||
case IR::Opcode::SetZFlag:
|
||||
pass.WriteVariable(ZeroFlagTag{}, block, inst.Arg(0));
|
||||
break;
|
||||
case IR::Opcode::SetSFlag:
|
||||
pass.WriteVariable(SignFlagTag{}, block, inst.Arg(0));
|
||||
break;
|
||||
case IR::Opcode::SetCFlag:
|
||||
pass.WriteVariable(CarryFlagTag{}, block, inst.Arg(0));
|
||||
break;
|
||||
case IR::Opcode::SetOFlag:
|
||||
pass.WriteVariable(OverflowFlagTag{}, block, inst.Arg(0));
|
||||
break;
|
||||
case IR::Opcode::GetRegister:
|
||||
if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
|
||||
inst.ReplaceUsesWith(pass.ReadVariable(reg, block));
|
||||
}
|
||||
break;
|
||||
case IR::Opcode::GetPred:
|
||||
if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
|
||||
inst.ReplaceUsesWith(pass.ReadVariable(pred, block));
|
||||
}
|
||||
break;
|
||||
case IR::Opcode::GetGotoVariable:
|
||||
inst.ReplaceUsesWith(pass.ReadVariable(GotoVariable{inst.Arg(0).U32()}, block));
|
||||
break;
|
||||
case IR::Opcode::GetIndirectBranchVariable:
|
||||
inst.ReplaceUsesWith(pass.ReadVariable(IndirectBranchVariable{}, block));
|
||||
break;
|
||||
case IR::Opcode::GetZFlag:
|
||||
inst.ReplaceUsesWith(pass.ReadVariable(ZeroFlagTag{}, block));
|
||||
break;
|
||||
case IR::Opcode::GetSFlag:
|
||||
inst.ReplaceUsesWith(pass.ReadVariable(SignFlagTag{}, block));
|
||||
break;
|
||||
case IR::Opcode::GetCFlag:
|
||||
inst.ReplaceUsesWith(pass.ReadVariable(CarryFlagTag{}, block));
|
||||
break;
|
||||
case IR::Opcode::GetOFlag:
|
||||
inst.ReplaceUsesWith(pass.ReadVariable(OverflowFlagTag{}, block));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void VisitBlock(Pass& pass, IR::Block* block) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
VisitInst(pass, block, inst);
|
||||
}
|
||||
pass.SealBlock(block);
|
||||
}
|
||||
|
||||
IR::Type GetConcreteType(IR::Inst* inst) {
|
||||
std::deque<IR::Inst*> queue;
|
||||
queue.push_back(inst);
|
||||
while (!queue.empty()) {
|
||||
IR::Inst* current = queue.front();
|
||||
queue.pop_front();
|
||||
const size_t num_args{current->NumArgs()};
|
||||
for (size_t i = 0; i < num_args; ++i) {
|
||||
const auto set_type = current->Arg(i).Type();
|
||||
if (set_type != IR::Type::Opaque) {
|
||||
return set_type;
|
||||
}
|
||||
if (!current->Arg(i).IsImmediate()) {
|
||||
queue.push_back(current->Arg(i).Inst());
|
||||
}
|
||||
}
|
||||
}
|
||||
return IR::Type::Opaque;
|
||||
}
|
||||
} // Anonymous namespace
|
||||
|
||||
void SsaRewritePass(IR::Program& program) {
|
||||
Pass pass;
|
||||
const auto end{program.post_order_blocks.rend()};
|
||||
for (auto block = program.post_order_blocks.rbegin(); block != end; ++block) {
|
||||
VisitBlock(pass, *block);
|
||||
}
|
||||
for (auto block = program.post_order_blocks.rbegin(); block != end; ++block) {
|
||||
for (IR::Inst& inst : (*block)->Instructions()) {
|
||||
if (inst.GetOpcode() == IR::Opcode::Phi) {
|
||||
if (inst.Type() == IR::Type::Opaque) {
|
||||
inst.SetFlags(GetConcreteType(&inst));
|
||||
}
|
||||
inst.OrderPhiArgs();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
877
gpu/third_party/suyu/shader_recompiler/ir_opt/texture_pass.cpp
vendored
Normal file
877
gpu/third_party/suyu/shader_recompiler/ir_opt/texture_pass.cpp
vendored
Normal file
@@ -0,0 +1,877 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 suyu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
#include <optional>
|
||||
#include <ankerl/unordered_dense.h>
|
||||
#include <tuple>
|
||||
#include <limits>
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include "common/settings.h"
|
||||
#include "shader_recompiler/environment.h"
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/breadth_first_search.h"
|
||||
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
||||
#include "shader_recompiler/host_translate_info.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
#include "shader_recompiler/shader_info.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
namespace {
|
||||
struct TextureInst {
|
||||
ConstBufferAddr cbuf;
|
||||
IR::Inst* inst;
|
||||
IR::Block* block;
|
||||
};
|
||||
|
||||
using TextureInstVector = boost::container::small_vector<TextureInst, 24>;
|
||||
|
||||
constexpr u32 DESCRIPTOR_SIZE = 8;
|
||||
constexpr u32 DESCRIPTOR_SIZE_SHIFT = u32(std::countr_zero(DESCRIPTOR_SIZE));
|
||||
constexpr u32 DESCRIPTOR_MAX_COUNT = 1024;
|
||||
|
||||
u32 DynamicDescriptorSizeShift(const IR::U32& dynamic_offset) {
|
||||
const IR::Inst* const inst = dynamic_offset.InstRecursive();
|
||||
if (!inst || inst->GetOpcode() != IR::Opcode::ShiftLeftLogical32)
|
||||
return DESCRIPTOR_SIZE_SHIFT;
|
||||
const IR::Value shift = inst->Arg(1);
|
||||
if (!shift.IsImmediate())
|
||||
return DESCRIPTOR_SIZE_SHIFT;
|
||||
const u32 size_shift = shift.U32();
|
||||
return size_shift >= DESCRIPTOR_SIZE_SHIFT && size_shift < 31 ? size_shift : DESCRIPTOR_SIZE_SHIFT;
|
||||
}
|
||||
|
||||
u32 DynamicDescriptorCount(u32 base_offset, u32 size_shift, u32 max_descriptors) {
|
||||
auto const descriptor_limit = (std::max)(1U, max_descriptors);
|
||||
auto const max_cbuf_bytes = 16 * descriptor_limit;
|
||||
if (size_shift >= 31 || base_offset >= max_cbuf_bytes)
|
||||
return 1;
|
||||
auto const stride = 1U << size_shift;
|
||||
auto const available = max_cbuf_bytes - base_offset;
|
||||
if (available < DESCRIPTOR_SIZE)
|
||||
return 1;
|
||||
auto const available_count = 1U + (available - DESCRIPTOR_SIZE) / stride;
|
||||
return std::min(descriptor_limit, available_count);
|
||||
}
|
||||
|
||||
u32 SaturatingSub(u32 lhs, u32 rhs) {
|
||||
return lhs > rhs ? lhs - rhs : 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] u32 StaticDescriptorCount(T const& descriptors) noexcept {
|
||||
return std::accumulate(descriptors.cbegin(), descriptors.cend(), 0U, [](auto const& acc, auto const& e) {
|
||||
return acc + (e.count <= 1 ? e.count : 0);
|
||||
});
|
||||
}
|
||||
|
||||
u32 DynamicSampledTextureCap(const Info& info, const HostTranslateInfo& host_info, u32 dynamic_arrays) {
|
||||
auto const sampled_limit = (std::max)(1U, std::min(host_info.max_per_stage_descriptor_sampled_images,
|
||||
host_info.max_descriptor_set_sampled_images));
|
||||
auto const resource_limit = (std::max)(1U, host_info.max_per_stage_resources);
|
||||
if (dynamic_arrays > 0) {
|
||||
auto const sampled_static_count = StaticDescriptorCount(info.texture_buffer_descriptors) + StaticDescriptorCount(info.texture_descriptors);
|
||||
auto const resource_static_count =
|
||||
NumDescriptors(info.constant_buffer_descriptors)
|
||||
+ NumDescriptors(info.storage_buffers_descriptors)
|
||||
+ sampled_static_count + NumDescriptors(info.image_buffer_descriptors)
|
||||
+ NumDescriptors(info.image_descriptors);
|
||||
auto const sampled_budget = SaturatingSub(sampled_limit, sampled_static_count);
|
||||
auto const resource_budget = SaturatingSub(resource_limit, resource_static_count);
|
||||
auto const sampled_cap = sampled_budget / dynamic_arrays;
|
||||
auto const resource_cap = resource_budget / dynamic_arrays;
|
||||
return (std::max)(1U, (std::min)(sampled_cap, resource_cap));
|
||||
}
|
||||
return (std::min)({DESCRIPTOR_MAX_COUNT, sampled_limit, resource_limit});
|
||||
}
|
||||
|
||||
IR::Opcode IndexedInstruction(const IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::BindlessImageSampleImplicitLod:
|
||||
case IR::Opcode::BoundImageSampleImplicitLod:
|
||||
return IR::Opcode::ImageSampleImplicitLod;
|
||||
case IR::Opcode::BoundImageSampleExplicitLod:
|
||||
case IR::Opcode::BindlessImageSampleExplicitLod:
|
||||
return IR::Opcode::ImageSampleExplicitLod;
|
||||
case IR::Opcode::BoundImageSampleDrefImplicitLod:
|
||||
case IR::Opcode::BindlessImageSampleDrefImplicitLod:
|
||||
return IR::Opcode::ImageSampleDrefImplicitLod;
|
||||
case IR::Opcode::BoundImageSampleDrefExplicitLod:
|
||||
case IR::Opcode::BindlessImageSampleDrefExplicitLod:
|
||||
return IR::Opcode::ImageSampleDrefExplicitLod;
|
||||
case IR::Opcode::BindlessImageGather:
|
||||
case IR::Opcode::BoundImageGather:
|
||||
return IR::Opcode::ImageGather;
|
||||
case IR::Opcode::BindlessImageGatherDref:
|
||||
case IR::Opcode::BoundImageGatherDref:
|
||||
return IR::Opcode::ImageGatherDref;
|
||||
case IR::Opcode::BindlessImageFetch:
|
||||
case IR::Opcode::BoundImageFetch:
|
||||
return IR::Opcode::ImageFetch;
|
||||
case IR::Opcode::BoundImageQueryDimensions:
|
||||
case IR::Opcode::BindlessImageQueryDimensions:
|
||||
return IR::Opcode::ImageQueryDimensions;
|
||||
case IR::Opcode::BoundImageQueryLod:
|
||||
case IR::Opcode::BindlessImageQueryLod:
|
||||
return IR::Opcode::ImageQueryLod;
|
||||
case IR::Opcode::BoundImageGradient:
|
||||
case IR::Opcode::BindlessImageGradient:
|
||||
return IR::Opcode::ImageGradient;
|
||||
case IR::Opcode::BoundImageRead:
|
||||
case IR::Opcode::BindlessImageRead:
|
||||
return IR::Opcode::ImageRead;
|
||||
case IR::Opcode::BoundImageWrite:
|
||||
case IR::Opcode::BindlessImageWrite:
|
||||
return IR::Opcode::ImageWrite;
|
||||
case IR::Opcode::BoundImageAtomicIAdd32:
|
||||
case IR::Opcode::BindlessImageAtomicIAdd32:
|
||||
return IR::Opcode::ImageAtomicIAdd32;
|
||||
case IR::Opcode::BoundImageAtomicSMin32:
|
||||
case IR::Opcode::BindlessImageAtomicSMin32:
|
||||
return IR::Opcode::ImageAtomicSMin32;
|
||||
case IR::Opcode::BoundImageAtomicUMin32:
|
||||
case IR::Opcode::BindlessImageAtomicUMin32:
|
||||
return IR::Opcode::ImageAtomicUMin32;
|
||||
case IR::Opcode::BoundImageAtomicSMax32:
|
||||
case IR::Opcode::BindlessImageAtomicSMax32:
|
||||
return IR::Opcode::ImageAtomicSMax32;
|
||||
case IR::Opcode::BoundImageAtomicUMax32:
|
||||
case IR::Opcode::BindlessImageAtomicUMax32:
|
||||
return IR::Opcode::ImageAtomicUMax32;
|
||||
case IR::Opcode::BoundImageAtomicInc32:
|
||||
case IR::Opcode::BindlessImageAtomicInc32:
|
||||
return IR::Opcode::ImageAtomicInc32;
|
||||
case IR::Opcode::BoundImageAtomicDec32:
|
||||
case IR::Opcode::BindlessImageAtomicDec32:
|
||||
return IR::Opcode::ImageAtomicDec32;
|
||||
case IR::Opcode::BoundImageAtomicAnd32:
|
||||
case IR::Opcode::BindlessImageAtomicAnd32:
|
||||
return IR::Opcode::ImageAtomicAnd32;
|
||||
case IR::Opcode::BoundImageAtomicOr32:
|
||||
case IR::Opcode::BindlessImageAtomicOr32:
|
||||
return IR::Opcode::ImageAtomicOr32;
|
||||
case IR::Opcode::BoundImageAtomicXor32:
|
||||
case IR::Opcode::BindlessImageAtomicXor32:
|
||||
return IR::Opcode::ImageAtomicXor32;
|
||||
case IR::Opcode::BoundImageAtomicExchange32:
|
||||
case IR::Opcode::BindlessImageAtomicExchange32:
|
||||
return IR::Opcode::ImageAtomicExchange32;
|
||||
default:
|
||||
return IR::Opcode::Void;
|
||||
}
|
||||
}
|
||||
|
||||
bool IsStorageImageOpcode(IR::Opcode opcode) {
|
||||
switch (opcode) {
|
||||
case IR::Opcode::ImageRead:
|
||||
case IR::Opcode::ImageAtomicIAdd32:
|
||||
case IR::Opcode::ImageAtomicSMin32:
|
||||
case IR::Opcode::ImageAtomicUMin32:
|
||||
case IR::Opcode::ImageAtomicSMax32:
|
||||
case IR::Opcode::ImageAtomicUMax32:
|
||||
case IR::Opcode::ImageAtomicInc32:
|
||||
case IR::Opcode::ImageAtomicDec32:
|
||||
case IR::Opcode::ImageAtomicAnd32:
|
||||
case IR::Opcode::ImageAtomicOr32:
|
||||
case IR::Opcode::ImageAtomicXor32:
|
||||
case IR::Opcode::ImageAtomicExchange32:
|
||||
case IR::Opcode::ImageWrite:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
u32 DynamicSampledTextureArrayCount(const TextureInstVector& to_replace) {
|
||||
u32 count{};
|
||||
for (const TextureInst& inst : to_replace) {
|
||||
const auto flags{inst.inst->Flags<IR::TextureInstInfo>()};
|
||||
if (inst.cbuf.count > 1 && !IsStorageImageOpcode(IndexedInstruction(*inst.inst)) &&
|
||||
flags.type != TextureType::Buffer) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
bool IsBindless(const IR::Inst& inst) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::BindlessImageSampleImplicitLod:
|
||||
case IR::Opcode::BindlessImageSampleExplicitLod:
|
||||
case IR::Opcode::BindlessImageSampleDrefImplicitLod:
|
||||
case IR::Opcode::BindlessImageSampleDrefExplicitLod:
|
||||
case IR::Opcode::BindlessImageGather:
|
||||
case IR::Opcode::BindlessImageGatherDref:
|
||||
case IR::Opcode::BindlessImageFetch:
|
||||
case IR::Opcode::BindlessImageQueryDimensions:
|
||||
case IR::Opcode::BindlessImageQueryLod:
|
||||
case IR::Opcode::BindlessImageGradient:
|
||||
case IR::Opcode::BindlessImageRead:
|
||||
case IR::Opcode::BindlessImageWrite:
|
||||
case IR::Opcode::BindlessImageAtomicIAdd32:
|
||||
case IR::Opcode::BindlessImageAtomicSMin32:
|
||||
case IR::Opcode::BindlessImageAtomicUMin32:
|
||||
case IR::Opcode::BindlessImageAtomicSMax32:
|
||||
case IR::Opcode::BindlessImageAtomicUMax32:
|
||||
case IR::Opcode::BindlessImageAtomicInc32:
|
||||
case IR::Opcode::BindlessImageAtomicDec32:
|
||||
case IR::Opcode::BindlessImageAtomicAnd32:
|
||||
case IR::Opcode::BindlessImageAtomicOr32:
|
||||
case IR::Opcode::BindlessImageAtomicXor32:
|
||||
case IR::Opcode::BindlessImageAtomicExchange32:
|
||||
return true;
|
||||
case IR::Opcode::BoundImageSampleImplicitLod:
|
||||
case IR::Opcode::BoundImageSampleExplicitLod:
|
||||
case IR::Opcode::BoundImageSampleDrefImplicitLod:
|
||||
case IR::Opcode::BoundImageSampleDrefExplicitLod:
|
||||
case IR::Opcode::BoundImageGather:
|
||||
case IR::Opcode::BoundImageGatherDref:
|
||||
case IR::Opcode::BoundImageFetch:
|
||||
case IR::Opcode::BoundImageQueryDimensions:
|
||||
case IR::Opcode::BoundImageQueryLod:
|
||||
case IR::Opcode::BoundImageGradient:
|
||||
case IR::Opcode::BoundImageRead:
|
||||
case IR::Opcode::BoundImageWrite:
|
||||
case IR::Opcode::BoundImageAtomicIAdd32:
|
||||
case IR::Opcode::BoundImageAtomicSMin32:
|
||||
case IR::Opcode::BoundImageAtomicUMin32:
|
||||
case IR::Opcode::BoundImageAtomicSMax32:
|
||||
case IR::Opcode::BoundImageAtomicUMax32:
|
||||
case IR::Opcode::BoundImageAtomicInc32:
|
||||
case IR::Opcode::BoundImageAtomicDec32:
|
||||
case IR::Opcode::BoundImageAtomicAnd32:
|
||||
case IR::Opcode::BoundImageAtomicOr32:
|
||||
case IR::Opcode::BoundImageAtomicXor32:
|
||||
case IR::Opcode::BoundImageAtomicExchange32:
|
||||
return false;
|
||||
default:
|
||||
throw InvalidArgument("Invalid opcode {}", inst.GetOpcode());
|
||||
}
|
||||
}
|
||||
|
||||
bool IsTextureInstruction(const IR::Inst& inst) {
|
||||
return IndexedInstruction(inst) != IR::Opcode::Void;
|
||||
}
|
||||
// Per-pass caches
|
||||
|
||||
static inline u32 ReadCbufCached(Environment& env, u32 index, u32 offset) {
|
||||
const CbufWordKey k{index, offset};
|
||||
if (auto it = env.cbuf_word_cache.find(k); it != env.cbuf_word_cache.end())
|
||||
return it->second;
|
||||
const u32 v = env.ReadCbufValue(index, offset);
|
||||
env.cbuf_word_cache.emplace(k, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
static inline u32 GetTextureHandleCached(Environment& env, const ConstBufferAddr& cbuf) {
|
||||
// Must all be uniquely different variables
|
||||
// If has secondary, then it will be cbuf.secondary_{index|offset}, else its 0.
|
||||
// So we can just hand it out the raw variable without using sec_idx or sec_off
|
||||
// because comparing 0 against 0 will yield true.
|
||||
const HandleKey hk{cbuf.index, cbuf.offset, cbuf.shift_left, cbuf.secondary_index, cbuf.secondary_offset, cbuf.secondary_shift_left, cbuf.count, cbuf.has_secondary};
|
||||
if (auto it = env.handle_cache.find(hk); it != env.handle_cache.end())
|
||||
return it->second;
|
||||
const u32 sec_idx = cbuf.has_secondary ? cbuf.secondary_index : cbuf.index;
|
||||
const u32 sec_off = cbuf.has_secondary ? cbuf.secondary_offset : cbuf.offset;
|
||||
const u32 lhs = ReadCbufCached(env, cbuf.index, cbuf.offset) << cbuf.shift_left;
|
||||
const u32 rhs = ReadCbufCached(env, sec_idx, sec_off) << cbuf.secondary_shift_left;
|
||||
const u32 handle = lhs | rhs;
|
||||
env.handle_cache.emplace(hk, handle);
|
||||
return handle;
|
||||
}
|
||||
|
||||
// Cached variants of existing helpers
|
||||
static inline TextureType ReadTextureTypeCached(Environment& env, const ConstBufferAddr& cbuf) {
|
||||
return env.ReadTextureType(GetTextureHandleCached(env, cbuf));
|
||||
}
|
||||
static inline TexturePixelFormat ReadTexturePixelFormatCached(Environment& env,
|
||||
const ConstBufferAddr& cbuf) {
|
||||
return env.ReadTexturePixelFormat(GetTextureHandleCached(env, cbuf));
|
||||
}
|
||||
static inline bool IsTexturePixelFormatIntegerCached(Environment& env,
|
||||
const ConstBufferAddr& cbuf) {
|
||||
return env.IsTexturePixelFormatInteger(GetTextureHandleCached(env, cbuf));
|
||||
}
|
||||
|
||||
|
||||
std::optional<ConstBufferAddr> Track(const IR::Value& value, Environment& env, const HostTranslateInfo& host_info);
|
||||
static inline std::optional<ConstBufferAddr> TrackCached(const IR::Value& v, Environment& env, const HostTranslateInfo& host_info) {
|
||||
if (const IR::Inst* key = v.InstRecursive()) {
|
||||
if (auto it = env.track_cache.find(key); it != env.track_cache.end()) return it->second;
|
||||
auto found = Track(v, env, host_info);
|
||||
if (found) env.track_cache.emplace(key, *found);
|
||||
return found;
|
||||
}
|
||||
return Track(v, env, host_info);
|
||||
}
|
||||
|
||||
std::optional<ConstBufferAddr> TryGetConstBuffer(const IR::Inst* inst, Environment& env, const HostTranslateInfo& host_info);
|
||||
|
||||
std::optional<ConstBufferAddr> Track(const IR::Value& value, Environment& env, const HostTranslateInfo& host_info) {
|
||||
return IR::BreadthFirstSearch(value, [&env, &host_info](const IR::Inst* inst) {
|
||||
return TryGetConstBuffer(inst, env, host_info);
|
||||
});
|
||||
}
|
||||
|
||||
std::optional<u32> TryGetConstant(IR::Value& value, Environment& env) {
|
||||
const IR::Inst* inst = value.InstRecursive();
|
||||
if (inst->GetOpcode() != IR::Opcode::GetCbufU32) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const IR::Value index{inst->Arg(0)};
|
||||
const IR::Value offset{inst->Arg(1)};
|
||||
if (!index.IsImmediate()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!offset.IsImmediate()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto index_number = index.U32();
|
||||
if (index_number != 1) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto offset_number = offset.U32();
|
||||
return ReadCbufCached(env, index_number, offset_number);
|
||||
}
|
||||
|
||||
std::optional<ConstBufferAddr> TryGetConstBuffer(const IR::Inst* inst, Environment& env, const HostTranslateInfo& host_info) {
|
||||
switch (inst->GetOpcode()) {
|
||||
default:
|
||||
return std::nullopt;
|
||||
case IR::Opcode::BitwiseOr32: {
|
||||
std::optional lhs{TrackCached(inst->Arg(0), env, host_info)};
|
||||
std::optional rhs{TrackCached(inst->Arg(1), env, host_info)};
|
||||
if (!lhs || !rhs) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (lhs->has_secondary || rhs->has_secondary) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (lhs->count > 1 || rhs->count > 1) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (lhs->shift_left > 0 || lhs->index > rhs->index || lhs->offset > rhs->offset) {
|
||||
std::swap(lhs, rhs);
|
||||
}
|
||||
return ConstBufferAddr{
|
||||
.index = lhs->index,
|
||||
.offset = lhs->offset,
|
||||
.shift_left = lhs->shift_left,
|
||||
.secondary_index = rhs->index,
|
||||
.secondary_offset = rhs->offset,
|
||||
.secondary_shift_left = rhs->shift_left,
|
||||
.dynamic_offset = {},
|
||||
.count = 1,
|
||||
.has_secondary = true,
|
||||
};
|
||||
}
|
||||
case IR::Opcode::ShiftLeftLogical32: {
|
||||
const IR::Value shift{inst->Arg(1)};
|
||||
if (!shift.IsImmediate()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
std::optional lhs{TrackCached(inst->Arg(0), env, host_info)};
|
||||
if (lhs) {
|
||||
lhs->shift_left = shift.U32();
|
||||
}
|
||||
return lhs;
|
||||
}
|
||||
case IR::Opcode::BitwiseAnd32: {
|
||||
IR::Value op1{inst->Arg(0)};
|
||||
IR::Value op2{inst->Arg(1)};
|
||||
if (op1.IsImmediate()) {
|
||||
std::swap(op1, op2);
|
||||
}
|
||||
if (!op2.IsImmediate() && !op1.IsImmediate()) {
|
||||
do {
|
||||
auto try_index = TryGetConstant(op1, env);
|
||||
if (try_index) {
|
||||
op1 = op2;
|
||||
op2 = IR::Value{*try_index};
|
||||
break;
|
||||
}
|
||||
auto try_index_2 = TryGetConstant(op2, env);
|
||||
if (try_index_2) {
|
||||
op2 = IR::Value{*try_index_2};
|
||||
break;
|
||||
}
|
||||
return std::nullopt;
|
||||
} while (false);
|
||||
}
|
||||
std::optional lhs{TrackCached(op1, env, host_info)};
|
||||
if (lhs) {
|
||||
lhs->shift_left = static_cast<u32>(std::countr_zero(op2.U32()));
|
||||
}
|
||||
return lhs;
|
||||
break;
|
||||
}
|
||||
case IR::Opcode::GetCbufU32x2:
|
||||
case IR::Opcode::GetCbufU32:
|
||||
break;
|
||||
}
|
||||
const IR::Value index{inst->Arg(0)};
|
||||
const IR::Value offset{inst->Arg(1)};
|
||||
if (!index.IsImmediate()) {
|
||||
// Reading a bindless texture from variable indices is valid
|
||||
// but not supported here at the moment
|
||||
return std::nullopt;
|
||||
}
|
||||
if (offset.IsImmediate()) {
|
||||
return ConstBufferAddr{
|
||||
.index = index.U32(),
|
||||
.offset = offset.U32(),
|
||||
.shift_left = 0,
|
||||
.secondary_index = 0,
|
||||
.secondary_offset = 0,
|
||||
.secondary_shift_left = 0,
|
||||
.dynamic_offset = {},
|
||||
.count = 1,
|
||||
.has_secondary = false,
|
||||
};
|
||||
}
|
||||
IR::Inst* const offset_inst{offset.InstRecursive()};
|
||||
if (offset_inst->GetOpcode() != IR::Opcode::IAdd32) {
|
||||
return std::nullopt;
|
||||
}
|
||||
u32 base_offset{};
|
||||
IR::U32 dynamic_offset;
|
||||
if (offset_inst->Arg(0).IsImmediate()) {
|
||||
base_offset = offset_inst->Arg(0).U32();
|
||||
dynamic_offset = IR::U32{offset_inst->Arg(1)};
|
||||
} else if (offset_inst->Arg(1).IsImmediate()) {
|
||||
base_offset = offset_inst->Arg(1).U32();
|
||||
dynamic_offset = IR::U32{offset_inst->Arg(0)};
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto const size_shift = DynamicDescriptorSizeShift(dynamic_offset);
|
||||
auto const sampled_limit = (std::max)(1U, (std::min)(host_info.max_per_stage_descriptor_sampled_images,
|
||||
host_info.max_descriptor_set_sampled_images));
|
||||
auto const resource_limit = (std::max)(1U, host_info.max_per_stage_resources);
|
||||
return ConstBufferAddr{
|
||||
.index = index.U32(),
|
||||
.offset = base_offset,
|
||||
.shift_left = 0,
|
||||
.secondary_index = 0,
|
||||
.secondary_offset = 0,
|
||||
.secondary_shift_left = 0,
|
||||
.dynamic_offset = dynamic_offset,
|
||||
.count = DynamicDescriptorCount(base_offset, size_shift, (std::min)({DESCRIPTOR_MAX_COUNT, sampled_limit, resource_limit})),
|
||||
.has_secondary = false,
|
||||
};
|
||||
}
|
||||
|
||||
TextureInst MakeInst(Environment& env, IR::Block* block, IR::Inst& inst, const HostTranslateInfo& host_info) {
|
||||
ConstBufferAddr addr;
|
||||
if (IsBindless(inst)) {
|
||||
const std::optional<ConstBufferAddr> track_addr{TrackCached(inst.Arg(0), env, host_info)};
|
||||
|
||||
if (!track_addr) {
|
||||
throw NotImplementedException("Failed to track bindless texture constant buffer");
|
||||
} else {
|
||||
addr = *track_addr;
|
||||
}
|
||||
} else {
|
||||
addr = ConstBufferAddr{
|
||||
.index = env.TextureBoundBuffer(),
|
||||
.offset = inst.Arg(0).U32(),
|
||||
.shift_left = 0,
|
||||
.secondary_index = 0,
|
||||
.secondary_offset = 0,
|
||||
.secondary_shift_left = 0,
|
||||
.dynamic_offset = {},
|
||||
.count = 1,
|
||||
.has_secondary = false,
|
||||
};
|
||||
}
|
||||
return TextureInst{
|
||||
.cbuf = addr,
|
||||
.inst = &inst,
|
||||
.block = block,
|
||||
};
|
||||
}
|
||||
|
||||
u32 GetTextureHandle(Environment& env, const ConstBufferAddr& cbuf) {
|
||||
const u32 secondary_index{cbuf.has_secondary ? cbuf.secondary_index : cbuf.index};
|
||||
const u32 secondary_offset{cbuf.has_secondary ? cbuf.secondary_offset : cbuf.offset};
|
||||
const u32 lhs_raw{env.ReadCbufValue(cbuf.index, cbuf.offset) << cbuf.shift_left};
|
||||
const u32 rhs_raw{env.ReadCbufValue(secondary_index, secondary_offset)
|
||||
<< cbuf.secondary_shift_left};
|
||||
return lhs_raw | rhs_raw;
|
||||
}
|
||||
|
||||
[[maybe_unused]] TextureType ReadTextureType(Environment& env, const ConstBufferAddr& cbuf) {
|
||||
return env.ReadTextureType(GetTextureHandle(env, cbuf));
|
||||
}
|
||||
|
||||
[[maybe_unused]] TexturePixelFormat ReadTexturePixelFormat(Environment& env, const ConstBufferAddr& cbuf) {
|
||||
return env.ReadTexturePixelFormat(GetTextureHandle(env, cbuf));
|
||||
}
|
||||
|
||||
[[maybe_unused]] bool IsTexturePixelFormatInteger(Environment& env, const ConstBufferAddr& cbuf) {
|
||||
return env.IsTexturePixelFormatInteger(GetTextureHandle(env, cbuf));
|
||||
}
|
||||
|
||||
class Descriptors {
|
||||
public:
|
||||
explicit Descriptors(TextureBufferDescriptors& texture_buffer_descriptors_,
|
||||
ImageBufferDescriptors& image_buffer_descriptors_,
|
||||
TextureDescriptors& texture_descriptors_,
|
||||
ImageDescriptors& image_descriptors_)
|
||||
: texture_buffer_descriptors{texture_buffer_descriptors_},
|
||||
image_buffer_descriptors{image_buffer_descriptors_},
|
||||
texture_descriptors{texture_descriptors_}, image_descriptors{image_descriptors_} {}
|
||||
|
||||
u32 Add(const TextureBufferDescriptor& desc) {
|
||||
return Add(texture_buffer_descriptors, desc, [&desc](const auto& existing) {
|
||||
return desc.cbuf_index == existing.cbuf_index &&
|
||||
desc.cbuf_offset == existing.cbuf_offset &&
|
||||
desc.shift_left == existing.shift_left &&
|
||||
desc.secondary_cbuf_index == existing.secondary_cbuf_index &&
|
||||
desc.secondary_cbuf_offset == existing.secondary_cbuf_offset &&
|
||||
desc.secondary_shift_left == existing.secondary_shift_left &&
|
||||
desc.count == existing.count && desc.size_shift == existing.size_shift &&
|
||||
desc.has_secondary == existing.has_secondary;
|
||||
});
|
||||
}
|
||||
|
||||
u32 Add(const ImageBufferDescriptor& desc) {
|
||||
const u32 index{Add(image_buffer_descriptors, desc, [&desc](const auto& existing) {
|
||||
return desc.format == existing.format && desc.cbuf_index == existing.cbuf_index &&
|
||||
desc.cbuf_offset == existing.cbuf_offset && desc.count == existing.count &&
|
||||
desc.size_shift == existing.size_shift;
|
||||
})};
|
||||
image_buffer_descriptors[index].is_written |= desc.is_written;
|
||||
image_buffer_descriptors[index].is_read |= desc.is_read;
|
||||
image_buffer_descriptors[index].is_integer |= desc.is_integer;
|
||||
return index;
|
||||
}
|
||||
|
||||
u32 Add(const TextureDescriptor& desc) {
|
||||
const u32 index{Add(texture_descriptors, desc, [&desc](const auto& existing) {
|
||||
return desc.type == existing.type && desc.is_depth == existing.is_depth &&
|
||||
desc.has_secondary == existing.has_secondary &&
|
||||
desc.cbuf_index == existing.cbuf_index &&
|
||||
desc.cbuf_offset == existing.cbuf_offset &&
|
||||
desc.shift_left == existing.shift_left &&
|
||||
desc.secondary_cbuf_index == existing.secondary_cbuf_index &&
|
||||
desc.secondary_cbuf_offset == existing.secondary_cbuf_offset &&
|
||||
desc.secondary_shift_left == existing.secondary_shift_left &&
|
||||
desc.count == existing.count && desc.size_shift == existing.size_shift;
|
||||
})};
|
||||
// TODO: Read this from TIC
|
||||
texture_descriptors[index].is_multisample |= desc.is_multisample;
|
||||
texture_descriptors[index].is_integer |= desc.is_integer;
|
||||
return index;
|
||||
}
|
||||
|
||||
u32 Add(const ImageDescriptor& desc) {
|
||||
const u32 index{Add(image_descriptors, desc, [&desc](const auto& existing) {
|
||||
return desc.type == existing.type && desc.format == existing.format &&
|
||||
desc.cbuf_index == existing.cbuf_index &&
|
||||
desc.cbuf_offset == existing.cbuf_offset && desc.count == existing.count &&
|
||||
desc.size_shift == existing.size_shift;
|
||||
})};
|
||||
image_descriptors[index].is_written |= desc.is_written;
|
||||
image_descriptors[index].is_read |= desc.is_read;
|
||||
image_descriptors[index].is_integer |= desc.is_integer;
|
||||
return index;
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename Descriptors, typename Descriptor, typename Func>
|
||||
static u32 Add(Descriptors& descriptors, const Descriptor& desc, Func&& pred) {
|
||||
// TODO: Handle arrays
|
||||
const auto it{std::ranges::find_if(descriptors, pred)};
|
||||
if (it != descriptors.end()) {
|
||||
return static_cast<u32>(std::distance(descriptors.begin(), it));
|
||||
}
|
||||
descriptors.push_back(desc);
|
||||
return static_cast<u32>(descriptors.size()) - 1;
|
||||
}
|
||||
|
||||
TextureBufferDescriptors& texture_buffer_descriptors;
|
||||
ImageBufferDescriptors& image_buffer_descriptors;
|
||||
TextureDescriptors& texture_descriptors;
|
||||
ImageDescriptors& image_descriptors;
|
||||
};
|
||||
|
||||
void PatchImageSampleImplicitLod(IR::Block& block, IR::Inst& inst) {
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
const auto info{inst.Flags<IR::TextureInstInfo>()};
|
||||
const IR::Value coord(inst.Arg(1));
|
||||
const IR::Value handle(ir.Imm32(0));
|
||||
const IR::U32 lod{ir.Imm32(0)};
|
||||
const IR::U1 skip_mips{ir.Imm1(true)};
|
||||
const IR::Value texture_size = ir.ImageQueryDimension(handle, lod, skip_mips, info);
|
||||
inst.SetArg(
|
||||
1, ir.CompositeConstruct(
|
||||
ir.FPMul(IR::F32(ir.CompositeExtract(coord, 0)),
|
||||
ir.FPRecip(ir.ConvertUToF(32, 32, ir.CompositeExtract(texture_size, 0)))),
|
||||
ir.FPMul(IR::F32(ir.CompositeExtract(coord, 1)),
|
||||
ir.FPRecip(ir.ConvertUToF(32, 32, ir.CompositeExtract(texture_size, 1))))));
|
||||
}
|
||||
|
||||
bool IsPixelFormatSNorm(TexturePixelFormat pixel_format) {
|
||||
switch (pixel_format) {
|
||||
case TexturePixelFormat::A8B8G8R8_SNORM:
|
||||
case TexturePixelFormat::R8G8_SNORM:
|
||||
case TexturePixelFormat::R8_SNORM:
|
||||
case TexturePixelFormat::R16G16B16A16_SNORM:
|
||||
case TexturePixelFormat::R16G16_SNORM:
|
||||
case TexturePixelFormat::R16_SNORM:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void PatchTexelFetch(IR::Block& block, IR::Inst& inst, TexturePixelFormat pixel_format) {
|
||||
const auto it{IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
auto get_max_value = [pixel_format]() -> float {
|
||||
switch (pixel_format) {
|
||||
case TexturePixelFormat::A8B8G8R8_SNORM:
|
||||
case TexturePixelFormat::R8G8_SNORM:
|
||||
case TexturePixelFormat::R8_SNORM:
|
||||
return 1.f / (std::numeric_limits<char>::max)();
|
||||
case TexturePixelFormat::R16G16B16A16_SNORM:
|
||||
case TexturePixelFormat::R16G16_SNORM:
|
||||
case TexturePixelFormat::R16_SNORM:
|
||||
return 1.f / (std::numeric_limits<short>::max)();
|
||||
default:
|
||||
throw InvalidArgument("Invalid texture pixel format");
|
||||
}
|
||||
};
|
||||
|
||||
const IR::Value new_inst{&*block.PrependNewInst(it, inst)};
|
||||
const IR::F32 x(ir.CompositeExtract(new_inst, 0));
|
||||
const IR::F32 y(ir.CompositeExtract(new_inst, 1));
|
||||
const IR::F32 z(ir.CompositeExtract(new_inst, 2));
|
||||
const IR::F32 w(ir.CompositeExtract(new_inst, 3));
|
||||
const IR::F16F32F64 max_value(ir.Imm32(get_max_value()));
|
||||
const IR::Value converted =
|
||||
ir.CompositeConstruct(ir.FPMul(ir.ConvertSToF(32, 32, ir.BitCast<IR::U32>(x)), max_value),
|
||||
ir.FPMul(ir.ConvertSToF(32, 32, ir.BitCast<IR::U32>(y)), max_value),
|
||||
ir.FPMul(ir.ConvertSToF(32, 32, ir.BitCast<IR::U32>(z)), max_value),
|
||||
ir.FPMul(ir.ConvertSToF(32, 32, ir.BitCast<IR::U32>(w)), max_value));
|
||||
inst.ReplaceUsesWith(converted);
|
||||
}
|
||||
} // Anonymous namespace
|
||||
|
||||
void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo& host_info) {
|
||||
// reset per-pass caches
|
||||
env.cbuf_word_cache.clear();
|
||||
env.handle_cache.clear();
|
||||
env.track_cache.clear();
|
||||
TextureInstVector to_replace;
|
||||
for (IR::Block* const block : program.post_order_blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
if (!IsTextureInstruction(inst)) {
|
||||
continue;
|
||||
}
|
||||
to_replace.push_back(MakeInst(env, block, inst, host_info));
|
||||
}
|
||||
}
|
||||
// Sort instructions to visit textures by constant buffer index, then by offset
|
||||
std::sort(to_replace.begin(), to_replace.end(), [](const auto& a, const auto& b) {
|
||||
if (a.cbuf.index != b.cbuf.index) return a.cbuf.index < b.cbuf.index;
|
||||
return a.cbuf.offset < b.cbuf.offset;
|
||||
});
|
||||
Descriptors descriptors{
|
||||
program.info.texture_buffer_descriptors,
|
||||
program.info.image_buffer_descriptors,
|
||||
program.info.texture_descriptors,
|
||||
program.info.image_descriptors,
|
||||
};
|
||||
const u32 sampled_dynamic_cap = DynamicSampledTextureCap(program.info, host_info, DynamicSampledTextureArrayCount(to_replace));
|
||||
bool has_last_is_integer{false};
|
||||
u32 last_cbuf_index{};
|
||||
u32 last_cbuf_offset{};
|
||||
bool last_is_integer{false};
|
||||
const auto is_texture_pixel_format_integer{[&](const ConstBufferAddr& cbuf_addr) {
|
||||
if (has_last_is_integer && last_cbuf_index == cbuf_addr.index &&
|
||||
last_cbuf_offset == cbuf_addr.offset) {
|
||||
return last_is_integer;
|
||||
}
|
||||
last_is_integer = IsTexturePixelFormatIntegerCached(env, cbuf_addr);
|
||||
last_cbuf_index = cbuf_addr.index;
|
||||
last_cbuf_offset = cbuf_addr.offset;
|
||||
has_last_is_integer = true;
|
||||
return last_is_integer;
|
||||
}};
|
||||
for (TextureInst& texture_inst : to_replace) {
|
||||
// TODO: Handle arrays
|
||||
IR::Inst* const inst{texture_inst.inst};
|
||||
inst->ReplaceOpcode(IndexedInstruction(*inst));
|
||||
|
||||
const auto& cbuf{texture_inst.cbuf};
|
||||
auto flags{inst->Flags<IR::TextureInstInfo>()};
|
||||
bool is_multisample{false};
|
||||
switch (inst->GetOpcode()) {
|
||||
case IR::Opcode::ImageQueryDimensions:
|
||||
flags.type.Assign(ReadTextureTypeCached(env, cbuf));
|
||||
inst->SetFlags(flags);
|
||||
break;
|
||||
case IR::Opcode::ImageSampleImplicitLod:
|
||||
if (flags.type != TextureType::Color2D) {
|
||||
break;
|
||||
}
|
||||
if (ReadTextureTypeCached(env, cbuf) == TextureType::Color2DRect) {
|
||||
PatchImageSampleImplicitLod(*texture_inst.block, *texture_inst.inst);
|
||||
}
|
||||
break;
|
||||
case IR::Opcode::ImageFetch:
|
||||
if (flags.type == TextureType::Color2D || flags.type == TextureType::Color2DRect ||
|
||||
flags.type == TextureType::ColorArray2D) {
|
||||
is_multisample = !inst->Arg(4).IsEmpty();
|
||||
} else {
|
||||
inst->SetArg(4, IR::U32{});
|
||||
}
|
||||
if (flags.type != TextureType::Color1D) {
|
||||
break;
|
||||
}
|
||||
if (ReadTextureTypeCached(env, cbuf) == TextureType::Buffer) {
|
||||
// Replace with the bound texture type only when it's a texture buffer
|
||||
// If the instruction is 1D and the bound type is 2D, don't change the code and let
|
||||
// the rasterizer robustness handle it
|
||||
// This happens on Fire Emblem: Three Houses
|
||||
flags.type.Assign(TextureType::Buffer);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
u32 index;
|
||||
u32 size_shift = cbuf.count > 1 ? DynamicDescriptorSizeShift(cbuf.dynamic_offset) : DESCRIPTOR_SIZE_SHIFT;
|
||||
u32 count = cbuf.count;
|
||||
switch (inst->GetOpcode()) {
|
||||
case IR::Opcode::ImageRead:
|
||||
case IR::Opcode::ImageAtomicIAdd32:
|
||||
case IR::Opcode::ImageAtomicSMin32:
|
||||
case IR::Opcode::ImageAtomicUMin32:
|
||||
case IR::Opcode::ImageAtomicSMax32:
|
||||
case IR::Opcode::ImageAtomicUMax32:
|
||||
case IR::Opcode::ImageAtomicInc32:
|
||||
case IR::Opcode::ImageAtomicDec32:
|
||||
case IR::Opcode::ImageAtomicAnd32:
|
||||
case IR::Opcode::ImageAtomicOr32:
|
||||
case IR::Opcode::ImageAtomicXor32:
|
||||
case IR::Opcode::ImageAtomicExchange32:
|
||||
case IR::Opcode::ImageWrite: {
|
||||
if (cbuf.has_secondary) {
|
||||
throw NotImplementedException("Unexpected separate sampler");
|
||||
}
|
||||
const bool is_written{inst->GetOpcode() != IR::Opcode::ImageRead};
|
||||
const bool is_read{inst->GetOpcode() != IR::Opcode::ImageWrite};
|
||||
const bool is_integer{is_texture_pixel_format_integer(cbuf)};
|
||||
if (flags.type == TextureType::Buffer) {
|
||||
index = descriptors.Add(ImageBufferDescriptor{
|
||||
.format = flags.image_format,
|
||||
.is_written = is_written,
|
||||
.is_read = is_read,
|
||||
.is_integer = is_integer,
|
||||
.cbuf_index = cbuf.index,
|
||||
.cbuf_offset = cbuf.offset,
|
||||
.count = count,
|
||||
.size_shift = size_shift,
|
||||
});
|
||||
} else {
|
||||
index = descriptors.Add(ImageDescriptor{
|
||||
.type = flags.type,
|
||||
.format = flags.image_format,
|
||||
.is_written = is_written,
|
||||
.is_read = is_read,
|
||||
.is_integer = is_integer,
|
||||
.cbuf_index = cbuf.index,
|
||||
.cbuf_offset = cbuf.offset,
|
||||
.count = count,
|
||||
.size_shift = size_shift,
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if (flags.type == TextureType::Buffer) {
|
||||
index = descriptors.Add(TextureBufferDescriptor{
|
||||
.has_secondary = cbuf.has_secondary,
|
||||
.cbuf_index = cbuf.index,
|
||||
.cbuf_offset = cbuf.offset,
|
||||
.shift_left = cbuf.shift_left,
|
||||
.secondary_cbuf_index = cbuf.secondary_index,
|
||||
.secondary_cbuf_offset = cbuf.secondary_offset,
|
||||
.secondary_shift_left = cbuf.secondary_shift_left,
|
||||
.count = count,
|
||||
.size_shift = size_shift,
|
||||
});
|
||||
} else {
|
||||
count = std::min(count, sampled_dynamic_cap);
|
||||
const bool is_integer{is_texture_pixel_format_integer(cbuf)};
|
||||
index = descriptors.Add(TextureDescriptor{
|
||||
.type = flags.type,
|
||||
.is_depth = flags.is_depth != 0,
|
||||
.is_multisample = is_multisample,
|
||||
.is_integer = is_integer,
|
||||
.has_secondary = cbuf.has_secondary,
|
||||
.cbuf_index = cbuf.index,
|
||||
.cbuf_offset = cbuf.offset,
|
||||
.shift_left = cbuf.shift_left,
|
||||
.secondary_cbuf_index = cbuf.secondary_index,
|
||||
.secondary_cbuf_offset = cbuf.secondary_offset,
|
||||
.secondary_shift_left = cbuf.secondary_shift_left,
|
||||
.count = count,
|
||||
.size_shift = size_shift,
|
||||
});
|
||||
flags.is_integer.Assign(is_integer ? 1 : 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
flags.descriptor_index.Assign(index);
|
||||
inst->SetFlags(flags);
|
||||
|
||||
if (count > 1) {
|
||||
const auto insert_point{IR::Block::InstructionList::s_iterator_to(*inst)};
|
||||
IR::IREmitter ir{*texture_inst.block, insert_point};
|
||||
const IR::U32 shift{ir.Imm32(size_shift)};
|
||||
inst->SetArg(0, ir.UMin(ir.ShiftRightLogical(cbuf.dynamic_offset, shift), ir.Imm32(count - 1)));
|
||||
} else {
|
||||
inst->SetArg(0, IR::Value{});
|
||||
}
|
||||
|
||||
if (!host_info.support_snorm_render_buffer && inst->GetOpcode() == IR::Opcode::ImageFetch &&
|
||||
flags.type == TextureType::Buffer) {
|
||||
const auto pixel_format = ReadTexturePixelFormatCached(env, cbuf);
|
||||
if (IsPixelFormatSNorm(pixel_format)) {
|
||||
PatchTexelFetch(*texture_inst.block, *texture_inst.inst, pixel_format);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void JoinTextureInfo(Info& base, Info& source) {
|
||||
Descriptors descriptors{
|
||||
base.texture_buffer_descriptors,
|
||||
base.image_buffer_descriptors,
|
||||
base.texture_descriptors,
|
||||
base.image_descriptors,
|
||||
};
|
||||
for (auto& desc : source.texture_buffer_descriptors) {
|
||||
descriptors.Add(desc);
|
||||
}
|
||||
for (auto& desc : source.image_buffer_descriptors) {
|
||||
descriptors.Add(desc);
|
||||
}
|
||||
for (auto& desc : source.texture_descriptors) {
|
||||
descriptors.Add(desc);
|
||||
}
|
||||
for (auto& desc : source.image_descriptors) {
|
||||
descriptors.Add(desc);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
79
gpu/third_party/suyu/shader_recompiler/ir_opt/vendor_workaround_pass.cpp
vendored
Normal file
79
gpu/third_party/suyu/shader_recompiler/ir_opt/vendor_workaround_pass.cpp
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/ir_emitter.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
|
||||
namespace {
|
||||
void AddingByteSwapsWorkaround(IR::Block& block, IR::Inst& inst) {
|
||||
/*
|
||||
* Workaround for an NVIDIA bug seen in Super Mario RPG
|
||||
*
|
||||
* We are looking for this pattern:
|
||||
* %lhs_bfe = BitFieldUExtract %factor_a, #0, #16
|
||||
* %lhs_mul = IMul32 %lhs_bfe, %factor_b // potentially optional?
|
||||
* %lhs_shl = ShiftLeftLogical32 %lhs_mul, #16
|
||||
* %rhs_bfe = BitFieldUExtract %factor_a, #16, #16
|
||||
* %result = IAdd32 %lhs_shl, %rhs_bfe
|
||||
*
|
||||
* And replacing the IAdd32 with a BitwiseOr32
|
||||
* %result = BitwiseOr32 %lhs_shl, %rhs_bfe
|
||||
*
|
||||
*/
|
||||
IR::Inst* const lhs_shl{inst.Arg(0).TryInstRecursive()};
|
||||
IR::Inst* const rhs_bfe{inst.Arg(1).TryInstRecursive()};
|
||||
if (!lhs_shl || !rhs_bfe) {
|
||||
return;
|
||||
}
|
||||
if (lhs_shl->GetOpcode() != IR::Opcode::ShiftLeftLogical32 ||
|
||||
lhs_shl->Arg(1) != IR::Value{16U}) {
|
||||
return;
|
||||
}
|
||||
if (rhs_bfe->GetOpcode() != IR::Opcode::BitFieldUExtract || rhs_bfe->Arg(1) != IR::Value{16U} ||
|
||||
rhs_bfe->Arg(2) != IR::Value{16U}) {
|
||||
return;
|
||||
}
|
||||
IR::Inst* const lhs_mul{lhs_shl->Arg(0).TryInstRecursive()};
|
||||
if (!lhs_mul) {
|
||||
return;
|
||||
}
|
||||
const bool lhs_mul_optional{lhs_mul->GetOpcode() == IR::Opcode::BitFieldUExtract};
|
||||
if (lhs_mul->GetOpcode() != IR::Opcode::IMul32 &&
|
||||
lhs_mul->GetOpcode() != IR::Opcode::BitFieldUExtract) {
|
||||
return;
|
||||
}
|
||||
IR::Inst* const lhs_bfe{lhs_mul_optional ? lhs_mul : lhs_mul->Arg(0).TryInstRecursive()};
|
||||
if (!lhs_bfe) {
|
||||
return;
|
||||
}
|
||||
if (lhs_bfe->GetOpcode() != IR::Opcode::BitFieldUExtract) {
|
||||
return;
|
||||
}
|
||||
if (lhs_bfe->Arg(1) != IR::Value{0U} || lhs_bfe->Arg(2) != IR::Value{16U}) {
|
||||
return;
|
||||
}
|
||||
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
|
||||
inst.ReplaceUsesWith(ir.BitwiseOr(IR::U32{inst.Arg(0)}, IR::U32{inst.Arg(1)}));
|
||||
}
|
||||
|
||||
} // Anonymous namespace
|
||||
|
||||
void VendorWorkaroundPass(IR::Program& program) {
|
||||
for (IR::Block* const block : program.post_order_blocks) {
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
switch (inst.GetOpcode()) {
|
||||
case IR::Opcode::IAdd32:
|
||||
AddingByteSwapsWorkaround(*block, inst);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
97
gpu/third_party/suyu/shader_recompiler/ir_opt/verification_pass.cpp
vendored
Normal file
97
gpu/third_party/suyu/shader_recompiler/ir_opt/verification_pass.cpp
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include "shader_recompiler/exception.h"
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/value.h"
|
||||
#include "shader_recompiler/ir_opt/passes.h"
|
||||
|
||||
namespace Shader::Optimization {
|
||||
|
||||
static void ValidateTypes(const IR::Program& program) {
|
||||
for (const auto& block : program.blocks) {
|
||||
for (const IR::Inst& inst : *block) {
|
||||
if (inst.GetOpcode() == IR::Opcode::Phi) {
|
||||
// Skip validation on phi nodes
|
||||
continue;
|
||||
}
|
||||
const size_t num_args{inst.NumArgs()};
|
||||
for (size_t i = 0; i < num_args; ++i) {
|
||||
const IR::Type t1{inst.Arg(i).Type()};
|
||||
const IR::Type t2{IR::ArgTypeOf(inst.GetOpcode(), i)};
|
||||
if (!IR::AreTypesCompatible(t1, t2)) {
|
||||
throw LogicError("Invalid types in block:\n{}", IR::DumpBlock(*block));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void ValidateUses(const IR::Program& program) {
|
||||
std::map<IR::Inst*, int> actual_uses;
|
||||
for (const auto& block : program.blocks) {
|
||||
for (const IR::Inst& inst : *block) {
|
||||
const size_t num_args{inst.NumArgs()};
|
||||
for (size_t i = 0; i < num_args; ++i) {
|
||||
const IR::Value arg{inst.Arg(i)};
|
||||
if (!arg.IsImmediate()) {
|
||||
++actual_uses[arg.Inst()];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto& [inst, uses] : actual_uses) {
|
||||
if (inst->UseCount() != uses) {
|
||||
throw LogicError("Invalid uses in block: {}", IR::DumpProgram(program));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void ValidateForwardDeclarations(const IR::Program& program) {
|
||||
std::set<const IR::Inst*> definitions;
|
||||
for (const IR::Block* const block : program.blocks) {
|
||||
for (const IR::Inst& inst : *block) {
|
||||
definitions.emplace(&inst);
|
||||
if (inst.GetOpcode() == IR::Opcode::Phi) {
|
||||
// Phi nodes can have forward declarations
|
||||
continue;
|
||||
}
|
||||
const size_t num_args{inst.NumArgs()};
|
||||
for (size_t arg = 0; arg < num_args; ++arg) {
|
||||
if (inst.Arg(arg).IsImmediate()) {
|
||||
continue;
|
||||
}
|
||||
if (!definitions.contains(inst.Arg(arg).Inst())) {
|
||||
throw LogicError("Forward declaration in block: {}", IR::DumpBlock(*block));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void ValidatePhiNodes(const IR::Program& program) {
|
||||
for (const IR::Block* const block : program.blocks) {
|
||||
bool no_more_phis{false};
|
||||
for (const IR::Inst& inst : *block) {
|
||||
if (inst.GetOpcode() == IR::Opcode::Phi) {
|
||||
if (no_more_phis) {
|
||||
throw LogicError("Interleaved phi nodes: {}", IR::DumpBlock(*block));
|
||||
}
|
||||
} else {
|
||||
no_more_phis = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void VerificationPass(const IR::Program& program) {
|
||||
ValidateTypes(program);
|
||||
ValidateUses(program);
|
||||
ValidateForwardDeclarations(program);
|
||||
ValidatePhiNodes(program);
|
||||
}
|
||||
|
||||
} // namespace Shader::Optimization
|
||||
Reference in New Issue
Block a user