Skip to content
Merged
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::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 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 end =
Binary(state, spv::OpIAdd, TypeU64(state), offset, ConstantU64(state, sizeof(uint32_t)));
const auto scalar_address = [&]() {
Expand Down Expand Up @@ -2617,25 +2617,15 @@ void EmitReadConstBuffer(ValueEmitContext& ctx, const IR::Inst& inst) {
mem.kind = IR::ResourceKind::ScalarBuffer;
const auto offset = inst.Arg(1).Resolve();
const auto static_index = offset.IsImmediate()
? static_cast<uint32_t>((uint64_t {offset.U32()} + mem.offset) >> 2u)
? (offset.U32() >> 2u) + (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));
}
if (!offset.IsImmediate() && mem.offset >= 4u) {
index = Binary(state, spv::OpIAdd, TypeU32(state), index,
ConstantU32(state, mem.offset >> 2u));
}
const auto storage_load = [&]() {
const auto access = PrepareMemoryResourceAccess(state, mem);
Expand Down
15 changes: 11 additions & 4 deletions core/kyty/graphics/shader/recompiler/ir/passes/RayQueryResults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,10 @@ class Machine {
const auto* buffer = inst.Arg(0).Resolve().TryInstruction();
if (buffer == nullptr) return "a constant buffer read without resource";
for (uint32_t lane = 0; lane < m_wave; ++lane) {
const auto base = (a(0, lane, 0) & 0xffffffffu) | ((a(0, lane, 1) & 0xffffu) << 32u);
const auto address = base + u32(a(1, lane)) + memory.offset;
const auto raw_base = (a(0, lane, 0) & 0xffffffffu) | ((a(0, lane, 1) & 0xffffu) << 32u);
const auto base = memory.kind == ResourceKind::IndirectBuffer ? raw_base & ~uint64_t {3} : raw_base;
const auto address = base + (u32(a(1, lane)) & ~3u) +
(memory.offset & ~3u);
At(out, lane) = Memory(address);
if (m_tracking && m_walk) {
if (lane == 0) m_taint[out].reset();
Expand Down Expand Up @@ -967,9 +969,14 @@ class Machine {
continue;
}
const auto& memory = m_program.memory_info[inst.Flags<MemoryFlags>().index];
const bool scalar = memory.kind == ResourceKind::ScalarAddress;
const auto low = u32(a(1, lane));
const auto base_low = memory.address_is_full ? low : u32(a(0, lane, 0));
const auto base_high = memory.address_is_full ? u32(a(2, lane)) : u32(a(0, lane, 1));
const auto base = uint64_t {scalar ? base_low & ~3u : base_low} | (uint64_t {base_high} << 32u);
const auto dynamic = memory.address_is_full ? 0u : scalar ? low & ~3u : low;
const auto offset = static_cast<uint64_t>(static_cast<int64_t>(static_cast<int32_t>(memory.offset)));
const auto address = memory.address_is_full ? ((a(1, lane) & 0xffffffffu) | (a(2, lane) & 0xffffffffu) << 32u) + offset
: ((a(0, lane, 0) & 0xffffffffu) | (a(0, lane, 1) << 32u)) + (a(1, lane) & 0xffffffffu) + offset;
const auto address = base + dynamic + (scalar ? offset & ~uint64_t {3} : offset);
auto value = static_cast<uint64_t>(Memory(address));
value >>= (address & 3u) * 8u;
if (op == Op::LoadAddressU16) value &= 0xffffu;
Expand Down
30 changes: 28 additions & 2 deletions core/kyty/graphics/shader/recompiler/ir/passes/RayQuerySource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,24 @@ class Evaluator {
return value;
}

uint32_t AlignDword(uint32_t value) {
auto form = m_store.F(value);
if (std::ranges::all_of(form.terms, [](const auto& term) { return (term.second & 3u) == 0u; })) {
form.constant &= ~uint64_t {3};
return m_store.Make(std::move(form));
}
return m_store.Op(32, {static_cast<uint64_t>(ValueOpcode::BitwiseAnd32), value, Literal | 0xfffffffcu});
}

uint32_t AlignAddress(uint32_t value) {
auto form = m_store.F(value);
if (std::ranges::all_of(form.terms, [](const auto& term) { return (term.second & 3u) == 0u; })) {
form.constant &= ~uint64_t {3};
return m_store.Make(std::move(form));
}
return m_store.Join(AlignDword(m_store.Lo(value)), m_store.Hi(value));
}

Choices Word(Value value) {
value = value.Resolve();
if (value.IsImmediate()) {
Expand Down Expand Up @@ -530,15 +548,22 @@ class Evaluator {
Choices Address(const Inst& load) {
const auto& memory = m_program.memory_info[load.Flags<MemoryFlags>().index];
auto offset = static_cast<uint64_t>(static_cast<int64_t>(static_cast<int32_t>(memory.offset)));
const bool scalar = memory.kind == ResourceKind::ScalarAddress;
if (scalar) offset &= ~uint64_t {3};
Choices bases;
auto& ev = load.NumArgs() > 3 ? Given(load.Arg(3)) : *this;
if (memory.address_is_full) {
bases = ev.Pair(load.Arg(1), load.Arg(2));
if (scalar) {
for (auto& base: bases) base = AlignAddress(base);
}
} else {
const auto* handle = ProducerOf(load.Arg(0), ValueOpcode::GetAddressResource);
if (handle == nullptr) return {};
for (const auto base: ev.Pair(handle->Arg(0), handle->Arg(1))) {
for (const auto index: ev.Word(load.Arg(1))) bases.push_back(m_store.Add(base, m_store.Widen(index)));
for (const auto index: ev.Word(load.Arg(1)))
bases.push_back(m_store.Add(scalar ? AlignAddress(base) : base,
m_store.Widen(scalar ? AlignDword(index) : index)));
}
}
Choices result;
Expand All @@ -555,7 +580,8 @@ class Evaluator {
for (const auto base: Pair(buffer->Arg(0), buffer->Arg(1))) {
const auto start = m_store.Op(64, {Base48, base});
for (const auto index: Word(read.Arg(1))) {
result.push_back(m_store.Add(m_store.Add(start, m_store.Widen(index)), m_store.Constant(64, memory.offset)));
result.push_back(m_store.Add(m_store.Add(start, m_store.Widen(AlignDword(index))),
m_store.Constant(64, memory.offset & ~3u)));
}
}
Cap(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,40 @@ std::optional<uint32_t> Immediate(Value value) {
return value.U32();
}

std::optional<uint32_t> LowThreeBits(Value value, uint32_t depth = 0u) {
if (depth >= 16u) return std::nullopt;
if (const auto immediate = Immediate(value)) return *immediate & 7u;
const auto* inst = value.Resolve().TryInstruction();
if (inst == nullptr) return std::nullopt;
switch (inst->GetOpcode()) {
case ValueOpcode::IAdd32: {
const auto left = LowThreeBits(inst->Arg(0), depth + 1u);
const auto right = LowThreeBits(inst->Arg(1), depth + 1u);
if (left && right) return (*left + *right) & 7u;
return std::nullopt;
}
case ValueOpcode::ShiftLeftLogical32: {
const auto shift = Immediate(inst->Arg(1));
if (!shift) return std::nullopt;
const auto count = *shift & 31u;
if (count >= 3u) return 0u;
const auto low = LowThreeBits(inst->Arg(0), depth + 1u);
if (low) return (*low << count) & 7u;
return std::nullopt;
}
case ValueOpcode::BitwiseAnd32: {
const auto left = Immediate(inst->Arg(0));
const auto right = Immediate(inst->Arg(1));
if ((left && (*left & 7u) == 0u) || (right && (*right & 7u) == 0u)) return 0u;
const auto low_left = LowThreeBits(inst->Arg(0), depth + 1u);
const auto low_right = LowThreeBits(inst->Arg(1), depth + 1u);
if (low_left && low_right) return *low_left & *low_right;
return std::nullopt;
}
default: return std::nullopt;
}
}

std::optional<uint32_t> Slot(Value value) {
const auto* read = Producer(value, ValueOpcode::ReadConst);
return read != nullptr ? Immediate(read->Arg(1)) : std::nullopt;
Expand Down Expand Up @@ -1456,6 +1490,18 @@ uint32_t TraceTraversalsWithRayQueries(Program& program) {
return traced;
}

std::optional<RayQueryResultKind> WaveScalarReadKind(const Program& program, const Inst& read) {
if (read.GetOpcode() != ValueOpcode::ReadConstBuffer || read.NumArgs() < 2u) return std::nullopt;
const auto index = read.Flags<MemoryFlags>().index;
if (index >= program.memory_info.size()) return std::nullopt;
const auto low = LowThreeBits(read.Arg(1));
if (!low) return std::nullopt;
const auto at = ((*low & ~3u) + (program.memory_info[index].offset & ~3u)) & 7u;
return at == 0u ? std::optional(RayQueryResultKind::Node)
: at == 4u ? std::optional(RayQueryResultKind::Word)
: std::nullopt;
}

namespace {


Expand Down Expand Up @@ -3348,11 +3394,11 @@ bool DeriveLaneAlpha(const Program& program, const PacketWalk& w, const BlasWalk
if (m2_load == nullptr || !std::ranges::all_of(textures->first, descriptor_defined)) return false;
const auto material_element = [&](const Inst* load) -> std::optional<std::pair<uint32_t, Value>> {
if (!a.scalar) return ElementOfThree(load->Arg(1));
auto at = MemoryOf(program, *load).offset;
auto at = MemoryOf(program, *load).offset & ~3u;
auto index = Canonical(load->Arg(1));
const auto plus = Operands(index, Op::IAdd32, [](Value side) { return Immediate(side).has_value(); });
if (plus) {
at += *Immediate(plus->first);
at += *Immediate(plus->first) & ~3u;
index = Canonical(plus->second);
}
const auto* lsl = Producer(index, Op::ShiftLeftLogical32);
Expand Down Expand Up @@ -3993,13 +4039,7 @@ std::optional<RayQueryResultKind> WaveResultKind(const Program& program, const L
(base->GetOpcode() == ValueOpcode::CompositeConstructU64 &&
resource->Arg(0).Resolve() == base->Arg(0).Resolve()));
if (!blas || !InLoop(program, value->Parent(), s.wave_header, s.wave_latch)) return std::nullopt;
auto at = MemoryOf(program, *value).offset;
if (const auto* add = Producer(value->Arg(1), ValueOpcode::IAdd32); add != nullptr)
at += Immediate(add->Arg(0)).value_or(Immediate(add->Arg(1)).value_or(0u));
at &= 7u;
return at == 0u ? std::optional(RayQueryResultKind::Node)
: at == 4u ? std::optional(RayQueryResultKind::Word)
: std::nullopt;
return WaveScalarReadKind(program, *value);
}
const auto* mul = Producer(Canonical(Value(const_cast<Inst*>(value))), ValueOpcode::FPMul32);
for (uint32_t side = 0; mul != nullptr && side < 2; ++side) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include "graphics/shader/recompiler/ir/RayQuery.h"
#include "graphics/shader/recompiler/ir/ShaderIR.h"

#include <array>
#include <optional>
#include <utility>
#include <vector>

Expand All @@ -14,6 +16,8 @@ namespace Libs::Graphics::ShaderRecompiler::IR {

[[nodiscard]] uint32_t TraceTraversalsWithRayQueries(Program& program);

[[nodiscard]] std::optional<RayQueryResultKind> WaveScalarReadKind(const Program& program, const Inst& read);

constexpr uint32_t OracleImprovementMask = 1u << 1;

struct OracleImprovement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ bool MaterializeIndirectDescriptor(const ResourcePlan& p
(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};
return (offset & ~uint64_t {3}) + (indirect.table_immediate & ~3u);
};
const auto normalization_failed = [&](uint64_t offset, uint32_t key, uint64_t step,
std::string_view key_kind) {
Expand Down Expand Up @@ -1108,10 +1108,12 @@ bool MaterializeIndirectDescriptor(const ResourcePlan& p
if (!EvaluateIndirectCount(program, indirect, runtime, key_count)) return false;
if (!indirect.selector_first.IsEmpty() && !WalkedValue(program, indirect.selector_first, first_key))
return false;
const auto first_offset = indirect.indexed
? uint64_t {indirect.table_offset} + indirect.table_immediate
: scalar_buffer_offset(indirect.table_offset);
if (indirect.table_stride == 0u || key_count > MaxIndirectDescriptorProbes ||
(indirect.selector_first.IsEmpty() && key_count != 0u &&
uint64_t {indirect.table_offset} + indirect.table_immediate +
uint64_t {key_count - 1u} * indirect.table_stride + descriptor_bytes >
first_offset + uint64_t {key_count - 1u} * indirect.table_stride + descriptor_bytes >
UINT32_MAX + 1ull)) {
return false;
}
Expand Down
Loading
Loading