Skip to content
Draft
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
7 changes: 7 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -18473,6 +18473,13 @@ Linux controls (including exhaustive verification against `fractions.Fraction` a
Independent Windows 11 (`26200.9974`, DXC `1.9.2602.17`, `debug_layer=unavailable`) measurements on `bd8f6f42` ([comment 1](https://github.com/R0Y00/Xbox2Windows/pull/2857#issuecomment-7196200555)) and `3100f363` ([comment 2](https://github.com/R0Y00/Xbox2Windows/pull/2857#issuecomment-7200844836)) passed `pixel_aot_f16_cvt_norm_hardware` (`AMD Radeon RX 9070 XT` `1002:7550`, driver `32.0.32029.2010`) and `pixel_aot_f16_cvt_norm_warp` with `max_absolute_error=0`, all 20 mutation draws mismatching, and DXIL disassembly confirming the `-Od` pass retains the `mul`/`lshr`/`shl` integer path. Scope limits: under `#839`'s static finite-proof contract (`half16_arith_source` / `half16_dpp_source`), operands are statically known per-lane uniform values, so device draw readbacks do not distinguish lane-routing changes on `dpp16`/`dpp8` (the permutation-to-intrinsic mapping is pinned statically in `check_f16_cvt_norm_controls`), mutation draws compile optimized rather than `-Od`, and the exhaustive 63,488-value check runs in Python rather than on-device.


## Native pixel VCC shortcuts

The direct DXIL path can lower a forward varying VCCZ shortcut when every live
write keeps the old value for lanes with a clear VCC bit. The
[source-built fixtures](experiments/native-pixel-vcc-shortcuts.md) check both
CNDMASK and matching SAVEEXEC bodies.

## Native raw pixel buffer UAV binding

The native graphics cache and draw stand-in support a single raw pixel UAV from a tracked descriptor table for a direct non-indexed draw without SRVs. The manifest records the table parameter and byte offset; the runtime requires an aligned raw R32_TYPELESS buffer view with valid bounds, UAV resource flags and compatible state, retains the resource, and orders consecutive UAV uses. A structured UAV window is also eligible when its `StructureByteStride` is at least 4 and a multiple of 4, `Format` is `UNKNOWN`, `Flags` is `NONE`, and `CounterOffsetInBytes` is zero. Its byte offset must be 16-byte aligned and its non-empty window must fit within the resource and the D3D12 raw UAV element limit. The runtime rewrites eligible windows to `R32_TYPELESS | FLAG_RAW`. Structured views with a non-4-byte stride, unaligned byte offset or counter are refused. Typed views, mixed views and other draw paths are outside this slice (#3212, #3215; separate from #3094/#2981).
Expand Down
29 changes: 29 additions & 0 deletions docs/experiments/native-pixel-vcc-shortcuts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Native pixel VCC shortcuts

A guest `s_cbranch_vccz` skips the body when every VCC bit is clear. In a mixed
wave the guest enters the body, but a plain `v_cndmask_b32 dst, dst, candidate, vcc`
keeps the incoming `dst` for clear lanes. The same lanes keep their VGPRs in a
body bracketed by `s_and_saveexec` on the same VCC and a restore of the saved
EXEC. In these cases the direct pixel translator can branch on each invocation's
bit. It requires a closed forward body with local register arithmetic and
expires unguarded scratch writes at the join. The saved EXEC pair and SCC also
expire. Values selected by the new branch retain the condition's input
dependencies at the join.

`s_cbranch_vccnz` skips the body for the entire wave when any bit is set. A
body with an inverted CNDMASK would update clear lanes when executed. In a mixed
wave the guest skips those updates, so the translator still refuses that
projection. It accepts VCCNZ when the condition is wave-uniform. A flat
attribute may differ between primitives in the same wave, and a floating
self-compare can see a NaN. Both keep their input dependencies.

```powershell
ctest --preset pr --no-tests=error --output-on-failure -R '^native_pixel_varying_vcc_branch_controls$'
ctest --preset dev --no-tests=error -R '^native_pixel_varying_vcc_branch_' -V
ctest --preset release --no-tests=error -R '^native_pixel_varying_vcc_branch_' -V
```

The fixture validates source-built gfx1013 pixel containers through DXC. Each
draw checks all 16 pixels and requires the opposite reference to report a pixel
mismatch. The controls also exercise scratch reads, mask clobbers, wrong
polarity, incoming branches, exports, stores, implicit LOD and DPP.
164 changes: 148 additions & 16 deletions src/gfx1013_direct_graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,86 @@ struct GraphicsEdge {
std::map<unsigned, unsigned> exports;
};

void join_graphics(GraphicsStage &g, std::vector<GraphicsEdge> &edges, const GraphicsMaskRegions &regions) {
struct PixelVccSkip {
std::set<unsigned> scratch;
std::optional<unsigned> save, restore;
};

std::optional<PixelVccSkip> pixel_vcc_skip(const Program &program, const Inst &skip,
const std::multiset<unsigned> &branch_targets, bool wave32) {
// VCCZ may run the body for VCC-clear lanes whenever another lane sets
// its bit. VCCNZ has no analogous lane projection: a single set bit
// skips the body for *all* lanes, including the clear ones.
if (skip.op != 6 || skip.target() <= skip.next || branch_targets.count(skip.target()) != 1)
return std::nullopt;
const size_t k = program.at.at(skip.pc), end = program.at.at(skip.target());
if (!k || branch_targets.count(skip.pc))
return std::nullopt;
const auto &producer = program.code[k - 1];
const bool compare = producer.enc == Encoding::Vopc && producer.dst == 106 &&
producer.name.substr(0, 6) == "v_cmp_" && !producer.dpp_control && !producer.dpp8_selectors;
// The WQM emitter already represents a tracked compare pair intersected
// with EXEC as the current invocation's VCC bit.
const bool intersection = !wave32 && producer.name == "s_and_b64" && producer.dst == 106 &&
(producer.src[0] == 126 || producer.src[1] == 126);
if (!compare && !intersection)
return std::nullopt;

PixelVccSkip result;
size_t begin = k + 1, limit = end;
const auto &save = program.code[begin];
if (save.name == (wave32 ? "s_and_saveexec_b32" : "s_and_saveexec_b64")) {
const auto &restore = program.code[end - 1];
if (begin + 1 >= end || save.src[0] != 106 || save.dst > (wave32 ? 105u : 104u) ||
(!wave32 && (save.dst & 1)) ||
restore.name != (wave32 ? "s_mov_b32" : "s_mov_b64") ||
restore.dst != 126 || restore.src[0] != save.dst)
return std::nullopt;
result.save = save.pc;
result.restore = restore.pc;
result.scratch.insert(save.dst);
if (!wave32) result.scratch.insert(save.dst + 1);
result.scratch.insert(128); // SCC holds the nonzero result of SAVEEXEC.
++begin;
--limit;
}
auto entry = branch_targets.lower_bound(skip.next);
if (entry != branch_targets.end() && *entry < skip.target())
return std::nullopt;

bool guarded_write = false;
for (size_t j = begin; j < limit; ++j) {
const auto &op = program.code[j];
if ((op.name == "s_nop" && op.a >> 16 == 0xbf80) || op.name == "s_waitcnt" || op.name == "s_clause")
continue;
if (!result.save && op.name == "s_mov_b32" && op.dst <= 105 &&
(op.src[0] <= 105 || constant32(op, op.src[0]))) {
result.scratch.insert(op.dst);
continue;
}
if (!GraphicsMaskRegions::local_alu(op) || op.dpp8_selectors || op.subword || carry_destination(op))
return std::nullopt;
if (op.name == "v_cndmask_b32") {
if (op.src[2] != 106 || op.abs || op.neg || op.scale || op.clamp || (op.wide && (op.a & 0x7800)))
return std::nullopt;
if (!result.save && (op.src[0] != op.dst || result.scratch.count(op.dst)))
return std::nullopt;
guarded_write = true;
} else if (alu_rules.count(op.name)) {
// This table, with local_alu's lane-op exclusion, writes one
// VGPR. Outside SAVEEXEC its output must be dead at the join.
if (!result.save) result.scratch.insert(op.dst);
} else {
return std::nullopt;
}
}
if (!result.save && !guarded_write)
return std::nullopt;
return result;
}

void join_graphics(GraphicsStage &g, std::vector<GraphicsEdge> &edges, const GraphicsMaskRegions &regions,
const PixelVccSkip *vcc_skip) {
auto &alu = g.alu;
alu.guarded_ij.clear();
if (edges.size() == 1) {
Expand Down Expand Up @@ -1511,7 +1590,11 @@ void join_graphics(GraphicsStage &g, std::vector<GraphicsEdge> &edges, const Gra
for (const auto &edge : edges)
control_inputs |= alu.ir.dependencies[edge.values.at(merge->second)];
}
if (vcc_skip)
control_inputs |= alu.ir.dependencies[edges.front().values.at(106)];
for (unsigned r : g.overwritten_inputs) {
if (vcc_skip && vcc_skip->scratch.count(r))
continue;
if (g.partial.count(r))
continue;
string first, incoming;
Expand Down Expand Up @@ -1541,6 +1624,8 @@ void join_graphics(GraphicsStage &g, std::vector<GraphicsEdge> &edges, const Gra
if (defined && !same)
alu.ir.dependencies[alu.graphics_registers[r]] |= control_inputs;
}
if (vcc_skip)
for (unsigned reg : vcc_skip->scratch) alu.write(reg, {});
}

// A loop may consume a VGPR defined on a backedge. Give each requested
Expand Down Expand Up @@ -1987,6 +2072,10 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
}
}
}
std::multiset<unsigned> branch_targets;
if (pixel)
for (const auto &i : program.code)
if (i.branch()) branch_targets.insert(i.target());
PixelWaterfalls waterfalls;
if (pixel) waterfalls.find(program,stage.wave32);
PixelWqm wqm;
Expand All @@ -1997,6 +2086,8 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
wqm.numeric_guards(program,stage.wave32);
}
GraphicsMaskRegions regions(program, stage.wave32, !pixel);
for (const auto &edge : regions.then_fallthroughs)
branch_targets.insert(edge.second);
const auto first_branch = std::find_if(program.code.begin(), program.code.end(),
[](const auto &i) { return i.branch(); });
const bool straight = first_branch == program.code.end();
Expand Down Expand Up @@ -2093,6 +2184,8 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
bool scalar_entry = true;
std::set<string> uniform_vector_values;
std::set<string> uniform_vcc_values;
std::map<unsigned, PixelVccSkip> vcc_skip_joins;
std::set<unsigned> vcc_skip_saves, vcc_skip_restores;
// Follow the helper count within one quad: h + swap_x(h) compared
// against -swap_y(h + swap_x(h)). This is a quad vote, whereas the
// original SOPP branch joins that vote across the entire guest wave.
Expand All @@ -2104,6 +2197,12 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
struct IndexRange { unsigned first, last, zero_bits; };
std::map<string,IndexRange> heap_ranges;
auto scalar_operand = [](unsigned s) { return s <= 105 || (s >= 128 && s <= 248) || s == 255; };
auto uniform_operand = [&](unsigned reg) {
if (scalar_operand(reg)) return true;
const auto source = alu.graphics_registers.find(reg);
return reg >= 256 && source != alu.graphics_registers.end() &&
uniform_vector_values.count(source->second) != 0;
};
auto covered_lane = [&] {
auto helper = alu.ir.invoke("i1","isHelperLane.i1",221,{},"nounwind readonly");
alu.ir.dependencies[helper].set(128);
Expand Down Expand Up @@ -2372,6 +2471,16 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =

if (auto it = regions.expired.find(pc); it != regions.expired.end())
for (auto reg : it->second) alu.write(reg, {});
if (vcc_skip_saves.count(pc)) {
// The incoming DXIL edge already selects the lanes in VCC.
saved_exec.erase(i.dst);
alu.write(i.dst, {});
if (!stage.wave32) alu.write(i.dst + 1, {});
alu.write(128, {});
return;
}
if (vcc_skip_restores.count(pc))
return;
if (regions.vertex_anchors.count(pc) || regions.captures.count(pc)) {
// A single invocation observes one bit of this saved wave mask.
// Never substitute that bit for a numeric 64-bit SGPR read.
Expand Down Expand Up @@ -2756,6 +2865,11 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
// Keep the invocation bit for CNDMASK and predicated regions.
// A whole-wave VCC branch also requires a uniform condition.
auto pred = alu.comparison(i);
const bool integer_eq = i.name == "v_cmp_eq_i32" || i.name == "v_cmp_eq_u32";
const bool integer_ne = i.name == "v_cmp_ne_i32" || i.name == "v_cmp_ne_u32";
const bool self_compare = pixel && !alu.ir.loop_dependencies && !i.subword &&
i.src[0] == i.src[1] && (integer_eq || integer_ne);
if (self_compare) pred = integer_eq ? "true" : "false";
if (!alu.graphics_predicate.empty())
pred = alu.ir.value("and i1 " + alu.graphics_predicate + ", " + pred);
auto bit = alu.ir.value("zext i1 " + pred + " to i32");
Expand All @@ -2781,7 +2895,8 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
mask_release.claimed = true;
}
}
if (i.dst == 106 && alu.graphics_predicate.empty() && scalar_operand(i.src[0]) && scalar_operand(i.src[1]) &&
if (i.dst == 106 && alu.graphics_predicate.empty() &&
(self_compare || (uniform_operand(i.src[0]) && uniform_operand(i.src[1]))) &&
alu.ir.dependencies[bit].none()) uniform_vcc_values.insert(bit);
return;
}
Expand Down Expand Up @@ -2980,21 +3095,16 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
alu.image_io(i, state);
} else {
// SGPR values and inline constants are common to the wave.
// Propagate that fact through pure VS arithmetic before a write
// Propagate that fact through pure vector arithmetic before a write
// can replace an overlapping source. An FMAC also reads its dst.
bool uniform_vertex = false;
bool uniform_alu = false;
auto rule = alu_rules.find(i.name);
if (!alu.ir.loop_dependencies && alu.graphics_predicate.empty() &&
!i.subword && GraphicsMaskRegions::local_alu(i) && rule != alu_rules.end()) {
auto uniform = [&](unsigned r) {
if (scalar_operand(r)) return true;
auto source = alu.graphics_registers.find(r);
return r >= 256 && source != alu.graphics_registers.end() &&
uniform_vector_values.count(source->second) != 0;
};
!i.subword && !i.dpp8_selectors && GraphicsMaskRegions::local_alu(i) && rule != alu_rules.end()) {
const bool mac = i.name == "v_fmac_f32";
const unsigned count = mac || i.name == "v_fmamk_f32" || i.name == "v_fmaak_f32" ? 2 : rule->second.arity;
uniform_vertex = std::all_of(i.src.begin(),i.src.begin()+count,uniform) && (!mac || uniform(i.dst));
uniform_alu = std::all_of(i.src.begin(),i.src.begin()+count,uniform_operand) &&
(!mac || uniform_operand(i.dst));
}
// Two complementary packed FMAs under the same EXEC define
// the entire VGPR. Its incoming bits are dead when neither operation
Expand Down Expand Up @@ -3051,7 +3161,17 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
if (peer!=vertical_peer.end()) negative_sum_source=peer->second;
}
}
const bool fold_integer = pixel && !alu.ir.loop_dependencies && alu.graphics_predicate.empty() &&
!i.subword && !i.dpp_control && !i.dpp8_selectors && !i.abs && !i.neg && !i.scale && !i.clamp &&
!(i.wide && (i.a & 0x7800));
const bool and_zero = fold_integer && i.name == "v_and_b32" &&
(constant32(i, i.src[0]) == 0u || constant32(i, i.src[1]) == 0u);
alu.arithmetic(i, state);
if (and_zero) alu.write(i.dst, "0");
if (fold_integer && i.enc == Encoding::Sopc && i.src[0] == i.src[1]) {
if (i.name == "s_cmp_eq_i32" || i.name == "s_cmp_eq_u32") alu.write(128, "1");
if (i.name == "s_cmp_lg_i32" || i.name == "s_cmp_lg_u32") alu.write(128, "0");
}
if (!horizontal_sum_source.empty()) horizontal_counts.insert(alu.read(i.dst));
if (!negative_sum_source.empty()) negative_peer.emplace(alu.read(i.dst),negative_sum_source);
if (heap_range) heap_ranges[alu.read(i.dst)]=*heap_range;
Expand All @@ -3060,7 +3180,7 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
((straight && i.name=="v_cvt_i32_f32" && i.src[0]<=105) ||
(i.name=="v_cvt_f32_f16" && scalar_operand(i.src[0])) ||
(i.name=="v_fma_mix_f32" && std::all_of(i.src.begin(),i.src.end(),scalar_operand)));
if (uniform_vertex || uniform_conversion || (pixel && i.name == "v_med3_f32" && alu.graphics_predicate.empty() &&
if (uniform_alu || and_zero || uniform_conversion || (pixel && i.name == "v_med3_f32" && alu.graphics_predicate.empty() &&
std::all_of(i.src.begin(), i.src.end(), scalar_operand))) {
auto value = alu.read(i.dst);
if (alu.ir.dependencies[value].none()) uniform_vector_values.insert(value);
Expand Down Expand Up @@ -3095,7 +3215,9 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
alu.ir.body << "g" << label << ":\n";
graphics.pc = label;
if (label) {
join_graphics(graphics, edges.at(label), regions);
const auto vcc_skip = vcc_skip_joins.find(label);
join_graphics(graphics, edges.at(label), regions,
vcc_skip == vcc_skip_joins.end() ? nullptr : &vcc_skip->second);
edges.erase(label);
}
for (size_t k = block.begin; k < block.end; ++k) {
Expand Down Expand Up @@ -3133,8 +3255,18 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
// take the other edge: safe only while nothing after the branch
// reads another lane (raster_vote keeps the tail lane-local).
if (branch == regions.lane_branches.end() && alu.ir.dependencies[condition].any() &&
last.pc != wqm.vote_skip && !quad_vote)
refuse(last.pc, "graphics scalar condition depends on a varying input");
last.pc != wqm.vote_skip && !quad_vote) {
const auto &body = blocks.at(last.next);
const bool closed = block.successors[1] == last.next && body.successors.size() == 1 &&
body.successors.front() == last.target();
auto proof = pixel && closed && alu.graphics_predicate.empty() && !exact_after_wqm ?
pixel_vcc_skip(program, last, branch_targets, stage.wave32) : std::nullopt;
if (!proof)
refuse(last.pc, "graphics scalar condition depends on a varying input");
if (proof->save) vcc_skip_saves.insert(*proof->save);
if (proof->restore) vcc_skip_restores.insert(*proof->restore);
vcc_skip_joins.emplace(last.target(), std::move(*proof));
}
alu.ir.body << " br i1 " << condition << ", label %g" << last.target() << ", label %g" << last.next
<< "\n";
} else
Expand Down
Loading
Loading