Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1101,11 +1101,11 @@ uint32_t LoadIndirectScalarBuffer(ValueEmitContext& ctx, const IR::Inst& inst) {
Unary(state, spv::OpUConvert, TypeU64(state),
EmitUMax32(state, stride, ConstantU32(state, 1))),
Unary(state, spv::OpUConvert, TypeU64(state), ctx.Arg(handle, 2)));
const auto offset = Binary(state, spv::OpIAdd, TypeU64(state),
Unary(state, spv::OpUConvert, TypeU64(state),
Binary(state, spv::OpBitwiseAnd, TypeU32(state),
ctx.Arg(inst, 1), ConstantU32(state, ~3u))),
ConstantU64(state, ctx.Memory(inst).offset & ~3u));
const auto offset = Binary(state, spv::OpBitwiseAnd, TypeU64(state),
Binary(state, spv::OpIAdd, TypeU64(state),
Unary(state, spv::OpUConvert, TypeU64(state), ctx.Arg(inst, 1)),
ConstantU64(state, ctx.Memory(inst).offset)),
ConstantU64(state, ~uint64_t {3}));
const auto end =
Binary(state, spv::OpIAdd, TypeU64(state), offset, ConstantU64(state, sizeof(uint32_t)));
const auto scalar_address = [&]() {
Expand Down Expand Up @@ -2608,19 +2608,34 @@ uint32_t EmitReadConst(ValueEmitContext& ctx, const IR::Inst& inst) {
void EmitReadConstBuffer(ValueEmitContext& ctx, const IR::Inst& inst) {
auto mem = ctx.Memory(inst);
if (mem.planning_only) return;
if (static_cast<int32_t>(mem.offset) < 0) ctx.Fail(inst, "has a negative scalar buffer immediate offset");
if (mem.kind == IR::ResourceKind::IndirectBuffer) {
ctx.Define(inst, LoadIndirectScalarBuffer(ctx, inst));
return;
}
auto& state = ctx.state;
mem.kind = IR::ResourceKind::ScalarBuffer;
const auto offset = inst.Arg(1).Resolve();
auto index = offset.IsImmediate()
? ConstantU32(state, (offset.U32() >> 2u) + (mem.offset >> 2u))
: Binary(state, spv::OpShiftRightLogical, TypeU32(state),
ctx.Arg(inst, 1), ConstantU32(state, 2));
if (!offset.IsImmediate() && mem.offset >= 4u) {
index = Binary(state, spv::OpIAdd, TypeU32(state), index, ConstantU32(state, mem.offset >> 2u));
const auto static_index = offset.IsImmediate()
? static_cast<uint32_t>((uint64_t {offset.U32()} + mem.offset) >> 2u)
: 0u;
auto index = offset.IsImmediate()
? ConstantU32(state, static_index)
: Binary(state, spv::OpShiftRightLogical, TypeU32(state), ctx.Arg(inst, 1),
ConstantU32(state, 2));
if (!offset.IsImmediate() && mem.offset != 0u) {
if ((mem.offset & 3u) != 0u) {
const auto low = Binary(state, spv::OpBitwiseAnd, TypeU32(state), ctx.Arg(inst, 1),
ConstantU32(state, 3));
const auto carry = Binary(state, spv::OpIAdd, TypeU32(state), low,
ConstantU32(state, mem.offset));
index = Binary(state, spv::OpIAdd, TypeU32(state), index,
Binary(state, spv::OpShiftRightLogical, TypeU32(state), carry,
ConstantU32(state, 2)));
} else {
index = Binary(state, spv::OpIAdd, TypeU32(state), index,
ConstantU32(state, mem.offset >> 2u));
}
}
const auto storage_load = [&]() {
const auto access = PrepareMemoryResourceAccess(state, mem);
Expand All @@ -2629,7 +2644,7 @@ void EmitReadConstBuffer(ValueEmitContext& ctx, const IR::Inst& inst) {
ConstantU32(state, 2)));
constexpr uint32_t last_element = UINT32_MAX >> 2u;
if (state.profile.robust_buffer_access &&
(!offset.IsImmediate() || (offset.U32() >> 2u) + (mem.offset >> 2u) > last_element - 63u)) {
(!offset.IsImmediate() || static_index > last_element - 63u)) {
element = EmitGlsl<GLSLstd450UMin, IR::Type::U32>(state, element, ConstantU32(state, last_element));
}
const auto condition = EmitStorageElementInBounds(state, access, element);
Expand Down Expand Up @@ -2662,7 +2677,7 @@ void EmitReadConstBuffer(ValueEmitContext& ctx, const IR::Inst& inst) {
};
constexpr uint32_t uniform_words = IR::UniformBufferBytes / sizeof(uint32_t);
constexpr uint32_t base_words = IR::UniformBufferBaseSlack / sizeof(uint32_t);
if (offset.IsImmediate() && (offset.U32() >> 2u) + (mem.offset >> 2u) < uniform_words - base_words) {
if (offset.IsImmediate() && static_index < uniform_words - base_words) {
ctx.Define(inst, bounded_load());
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,14 @@ bool ResolveSelectorKeys(const ResourcePlan& program, const DescriptorSource::In
} else {
ShaderBufferResource material;
if (!DecodeBufferDescriptor(material_value, material) || selector->stride < 4u ||
!std::has_single_bit(selector->stride) || (selector->offset & 3u) != 0u)
!std::has_single_bit(selector->stride) ||
((selector->offset & 3u) != 0u && indirect.selector_byte_bias.IsEmpty()))
return false;
const auto domain_count = (uint64_t {1} << 32u) / selector->stride;
uint32_t bias = 0;
if (!indirect.selector_byte_bias.IsEmpty() &&
!WalkedValue(program, indirect.selector_byte_bias, bias)) return false;
const auto first = uint64_t {selector->offset} + ((bias % selector->stride) & ~3u);
const auto first = (uint64_t {selector->offset} + (bias % selector->stride)) & ~uint64_t {3};
const auto available = material.GetSize() > first ? material.GetSize() - first : 0u;
const auto count = std::min(domain_count,
available < 4u ? 0u : (available - 4u) / selector->stride + 1u);
Expand Down Expand Up @@ -660,6 +661,9 @@ bool MaterializeIndirectDescriptor(const ResourcePlan& p
"read_address={:#x} requested_bytes={}", row, child, base, offset,
(base & AddressMask & ~uint64_t {3}) + (offset & ~uint64_t {3}), bytes));
};
const auto scalar_buffer_offset = [&](uint64_t offset) {
return (offset + indirect.table_immediate) & ~uint64_t {3};
};
const auto normalization_failed = [&](uint64_t offset, uint32_t key, uint64_t step,
std::string_view key_kind) {
return failed("candidate", fmt::format("candidate_offset={:#x} {}={} stride={} step={}",
Expand All @@ -672,7 +676,7 @@ bool MaterializeIndirectDescriptor(const ResourcePlan& p
table_base = table.Base48();
table_size = table.GetSize();
descriptors[resource_index] = {.dword_count = dword_count};
const uint64_t first = uint64_t {indirect.table_offset & ~3u} + (indirect.table_immediate & ~3u);
const uint64_t first = scalar_buffer_offset(indirect.table_offset);
const uint64_t rows = table_size >= first + 8u
? (table_size - first - 8u) / indirect.table_stride + 1u : 0u;
std::vector<uint32_t> words(table_size / 4u);
Expand All @@ -684,8 +688,9 @@ bool MaterializeIndirectDescriptor(const ResourcePlan& p
};
std::optional<uint64_t> function_slot;
if (pointer.function != 0u) {
const auto pointer_slot = first - (indirect.table_offset & ~3u);
for (uint64_t slot = 0; slot + 8u <= indirect.table_stride && !function_slot; slot += 8u) {
if (slot == (indirect.table_immediate & ~7u)) continue;
if (slot < pointer_slot + 8u && pointer_slot < slot + 8u) continue;
for (uint64_t row = 0; row < rows; ++row) {
if (((indirect.table_offset & ~3u) + row * indirect.table_stride + slot + 8u) > table_size) break;
if ((row_word(row, (indirect.table_offset & ~3u) + slot) & AddressMask) ==
Expand Down Expand Up @@ -943,8 +948,8 @@ bool MaterializeIndirectDescriptor(const ResourcePlan& p
: 4u;
uint64_t first = 0u;
if (table_value.dword_count != 2u)
first = ((indirect.table_maximum ? indirect.table_offset : indirect.table_offset % step) &
~uint64_t {3}) + (indirect.table_immediate & ~3u);
first = scalar_buffer_offset(indirect.table_maximum ? indirect.table_offset
: indirect.table_offset % step);
if (!indirect.table_maximum) domain /= step;
const auto count = table_size >= first + 4u
? std::min((table_size - first - 4u) / step + 1u, domain)
Expand Down Expand Up @@ -1131,8 +1136,10 @@ bool MaterializeIndirectDescriptor(const ResourcePlan& p
byte_offsets.clear();
byte_offsets.reserve(keys.size());
for (const auto key: keys) {
const auto offset = uint64_t {(key * indirect.table_stride + indirect.table_offset) & ~3u} +
(indirect.table_immediate & ~3u);
const auto indexed_offset = uint64_t {key * indirect.table_stride + indirect.table_offset};
const auto offset = table_value.dword_count == 4u
? scalar_buffer_offset(indexed_offset)
: (indexed_offset & ~uint64_t {3}) + (indirect.table_immediate & ~3u);
byte_offsets.push_back(offset <= UINT32_MAX ? static_cast<uint32_t>(offset) : UINT32_MAX);
}
std::ranges::sort(byte_offsets);
Expand All @@ -1157,9 +1164,11 @@ bool MaterializeIndirectDescriptor(const ResourcePlan& p
DescriptorValue candidate;
candidate.dword_count = dword_count;
if (sources.empty()) {
const auto indexed_offset = uint64_t {key * indirect.table_stride + indirect.table_offset};
table_offset = byte_keys ? key
: uint64_t {(key * indirect.table_stride + indirect.table_offset) & ~3u} +
(indirect.table_immediate & ~3u);
: table_value.dword_count == 4u ? scalar_buffer_offset(indexed_offset)
: (indexed_offset & ~uint64_t {3}) +
(indirect.table_immediate & ~3u);
if (!(byte_keys && key == UINT32_MAX) && table_offset <= UINT32_MAX &&
!ReadScalarTable(table_base, table_size, table_offset, runtime,
std::span(candidate.dwords).first(read_dwords))) {
Expand Down
35 changes: 25 additions & 10 deletions core/kyty/graphics/shader/recompiler/ir/passes/ResourceTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,9 @@ class Tracker {
offset = emit(ValueOpcode::ISub32, {dynamic, Value(indirect.table_offset)});
valid = emit(ValueOpcode::ULessThanEqual32, {Value(indirect.table_offset), dynamic});
} else {
offset = emit(ValueOpcode::IAdd32, {dynamic, Value(memory.offset & ~3u)});
valid = emit(ValueOpcode::ULessThanEqual32, {dynamic, offset});
const auto sum = emit(ValueOpcode::IAdd32, {read.Arg(1), Value(memory.offset)});
offset = emit(ValueOpcode::BitwiseAnd32, {sum, Value(~3u)});
valid = emit(ValueOpcode::ULessThanEqual32, {read.Arg(1), sum});
}
}
key = emit(ValueOpcode::SelectU32, {valid, offset, Value(UINT32_MAX)});
Expand Down Expand Up @@ -2765,9 +2766,14 @@ class Tracker {
}
if (require_bound && count.IsEmpty() && workgroup_axis == UINT32_MAX && !rows) return false;
if (!MakeRuntimeTableSource(*read, material_source)) return false;
if (memory->kind == ResourceKind::ScalarBuffer &&
uint64_t {offset} + memory->offset > UINT32_MAX) return false;
selector.source = InternSource(material_source);
selector.stride = stride;
selector.offset = offset + (memory->offset & ~3u);
selector.offset = memory->kind == ResourceKind::ScalarBuffer
? static_cast<uint32_t>((uint64_t {offset} + memory->offset) &
(byte_bias.IsEmpty() ? ~uint64_t {3} : UINT32_MAX))
: offset + (memory->offset & ~3u);
indirect.selector = std::move(selector);
indirect.selector_byte_bias = byte_bias;
indirect.key_count = count;
Expand Down Expand Up @@ -3210,6 +3216,11 @@ class Tracker {
Inst* table_handle = nullptr;
Value table_guard;
const uint32_t alignment_mask = allow_fixed ? ~3u : UINT32_MAX;
const auto physical_offset = [&](uint32_t offset, uint32_t immediate, ResourceKind kind) {
return allow_fixed && kind == ResourceKind::ScalarBuffer
? (uint64_t {offset} + immediate) & ~uint64_t {3}
: uint64_t {offset & alignment_mask} + (immediate & alignment_mask);
};
for (uint32_t dword = 0; dword < plan.read_dwords; ++dword) {
auto* read = descriptor.dwords[dword].Resolve().TryInstruction();
if (read == nullptr) {
Expand Down Expand Up @@ -3276,10 +3287,11 @@ class Tracker {
table_stride = stride;
if (table_mask != nullptr) *table_mask = mask;
} else if (plan.table_indexed != indexed || table_stride != stride ||
memory->kind != m_program.memory_info[plan.memory[0]].kind ||
!EquivalentValue(m_program, key, current_key) ||
uint64_t {table_offset & alignment_mask} + (table_immediate & alignment_mask) +
physical_offset(table_offset, table_immediate, memory->kind) +
dword * sizeof(uint32_t) !=
uint64_t {offset & alignment_mask} + (memory->offset & alignment_mask)) {
physical_offset(offset, memory->offset, memory->kind)) {
return false;
}
plan.split_offsets |= !indexed && offset != table_offset;
Expand Down Expand Up @@ -3357,16 +3369,17 @@ class Tracker {
low_memory->kind != ResourceKind::ScalarBuffer ||
high_memory->kind != ResourceKind::ScalarBuffer ||
low_memory->offset > INT32_MAX || high_memory->offset > INT32_MAX ||
(low_memory->offset & ~3u) + 4u != (high_memory->offset & ~3u) ||
!EquivalentValue(m_program, low->Arg(0), high->Arg(0)) ||
!EquivalentValue(m_program, low->Arg(1), high->Arg(1)) ||
!AvailableBefore(*low, *pointer) || !AvailableBefore(*high, *pointer) ||
!MatchTableOffset(low->Arg(1), key, indirect.table_offset, indirect.table_stride) ||
((uint64_t {indirect.table_offset} + low_memory->offset) & ~uint64_t {3}) + 4u !=
((uint64_t {indirect.table_offset} + high_memory->offset) & ~uint64_t {3}) ||
indirect.table_stride < 8u || (indirect.table_stride & 3u) != 0u ||
!SetExactTableMaximum(key, indirect) ||
(key.TryInstruction() != nullptr && !AvailableBefore(*key.Instruction(), handle)) ||
!MakeRuntimeTableSource(*low, table_source)) return false;
indirect.table_immediate = low_memory->offset & ~3u;
indirect.table_immediate = low_memory->offset;
} else {
if (handle.GetOpcode() != ValueOpcode::GetImageResource ||
!ImmediateU32(UnsignedResourceBound(child_key, handle), child.count) || child.count == 0u ||
Expand Down Expand Up @@ -3598,13 +3611,14 @@ class Tracker {
low_memory->kind != ResourceKind::ScalarBuffer ||
high_memory->kind != ResourceKind::ScalarBuffer ||
low_memory->offset > INT32_MAX || high_memory->offset > INT32_MAX ||
(low_memory->offset & ~3u) + 4u != (high_memory->offset & ~3u) ||
!EquivalentValue(m_program, low->Arg(0), high->Arg(0)) ||
!EquivalentValue(m_program, low->Arg(1), high->Arg(1)) ||
!MatchTableOffset(low->Arg(1), row_key, indirect.table_offset, indirect.table_stride) ||
((uint64_t {indirect.table_offset} + low_memory->offset) & ~uint64_t {3}) + 4u !=
((uint64_t {indirect.table_offset} + high_memory->offset) & ~uint64_t {3}) ||
row_key.IsEmpty() || indirect.table_stride < 8u || (indirect.table_stride & 3u) != 0u ||
!MakeRuntimeTableSource(*low, table_source) || table_source.dword_count != 4u) return false;
indirect.table_immediate = low_memory->offset & ~3u;
indirect.table_immediate = low_memory->offset;
indirect.table_source = InternSource(table_source);
indirect.table_scalar = true;
uint64_t function = 0;
Expand All @@ -3615,11 +3629,12 @@ class Tracker {
functions = body.aliases;
}
}
if (uint64_t {element_offset} + element_immediate > UINT32_MAX) return false;
indirect.pointer_table = DescriptorSource::IndirectDescriptor::PointerTable {
.offset = record_offset,
.buffer = true,
.element_stride = element_stride,
.element_offset = (element_offset & ~3u) + (element_immediate & ~3u),
.element_offset = (element_offset + element_immediate) & ~3u,
.function = function,
.functions = std::move(functions)};
DescriptorSource source;
Expand Down
22 changes: 16 additions & 6 deletions core/kyty/graphics/shader/recompiler/ir/passes/SrtWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1309,21 +1309,31 @@ void WalkerCompiler::EmitRead(const Node& node, uint32_t index) {
}
if (!args.empty()) {
for (const auto& arg: args) Branch(arg, m_ret);
const auto immediate =
static_cast<int64_t>(static_cast<int32_t>(m_plan.memory_info[inst.Flags<MemoryFlags>().index].offset)) &
~int64_t {3};
const auto constant = int64_t {static_cast<uint32_t>(args[2].value) & ~3u} + immediate;
const auto raw_immediate =
static_cast<int64_t>(static_cast<int32_t>(m_plan.memory_info[inst.Flags<MemoryFlags>().index].offset));
const auto scalar_buffer = op == ValueOpcode::ReadConstBuffer && (raw_immediate & 3) != 0;
const auto immediate = raw_immediate & ~int64_t {3};
const auto constant = scalar_buffer
? (int64_t {static_cast<uint32_t>(args[2].value)} + raw_immediate) & ~int64_t {3}
: int64_t {static_cast<uint32_t>(args[2].value) & ~3u} + immediate;
const bool folded = args[2].kind == Kind::Constant && constant + 4 <= INT32_MAX;
const auto offset = folded ? Xbyak::RegExp(static_cast<size_t>(constant))
: c.r9 + static_cast<size_t>(immediate);
: scalar_buffer ? Xbyak::RegExp(c.r9)
: c.r9 + static_cast<size_t>(immediate);
if (args[0].kind == Kind::UserData && args[1].kind == Kind::UserData) {
c.mov(c.rax, c.qword[At(m_bases.at({args[0].value, args[1].value}))]);
} else {
Base(args[0], args[1]);
}
if (!folded) {
Load(c.r9, args[2]);
c.and_(c.r9d, ~3u);
if (scalar_buffer) {
c.mov(c.r9d, c.r9d);
c.add(c.r9, static_cast<uint32_t>(raw_immediate));
c.and_(c.r9, ~3u);
} else {
c.and_(c.r9d, ~3u);
}
}
c.lea(c.r10, c.ptr[c.rax + offset]);
if (op == ValueOpcode::LoadAddressU32) {
Expand Down
Loading
Loading