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
61 changes: 37 additions & 24 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4557,40 +4557,53 @@ fn out_struct(h: &mut String, name: &str, outputs: &[SigElement]) {
/// two is not always the same vertex: Mesa's radeonsi blits send (x1,y1), (x1,y2), (x2,y1) – v0
/// – and xf86-video-ati's r600 EXA (x1,y1), (x1,y2), (x2,y2) – v1. The corner is the vertex
/// sharing x with one of the others and y with the other; the fourth vertex is the opposite
/// corner, a + b - corner, with every output taken the same way (both drivers' texture
/// corner, a + b - corner, with every float output taken the same way (both drivers' texture
/// coordinates rely on the implied corner continuing them linearly). Only for three vertices
/// with one positive w – then a + b - corner is the same in clip and screen space and every
/// interpolation mode agrees; three that are not such corners, or differ in w, stay the one
/// triangle they make. Refused (Err): integer outputs, which are flat – which vertex of a rect
/// gives a flat value is not established, so neither is how the two halves would carry it
/// (the PS side: `pso::ps_reads_flat`).
pub fn rect_list_gs(outputs: &[SigElement]) -> Result<String, String> {
if let Some(e) = ranked(outputs, true).iter().find(|e| hlsl_type(e) != "float") {
return Err(format!("{} is an integer output (flat)", e.semantic()));
}
/// triangle they make.
///
/// Flat values are the rect's first vertex's: both triangles start with v[0], which is where
/// the PC takes a flat input from (an integer input, or what the translation declares
/// `nointerpolation` for `v_interp_mov_f32`), and the fourth vertex's integer outputs are
/// v[0]'s. On the console the first vertex is the provoking one of every primitive under
/// D3D's setting – AMD's register references (R6xx, SI, CIK), PA_SU_SC_MODE_CNTL
/// .PROVOKING_VTX_LAST: "Defines which vertex of a primitive is used for attribute components
/// when flat shading is enabled", "0 = First Vtx (D3D)"; the Evergreen / Cayman programming
/// guide, 5.2: a flat attribute is the provoking vertex's value with gradients of 0. Neither
/// says so of a rect in particular, and it is not measured on one.
pub fn rect_list_gs(outputs: &[SigElement]) -> String {
let mut h = String::new();
h.push_str("// generated by XCL: a console rect list's rectangles (three corners, the fourth implied)\n");
out_struct(&mut h, "XclOut", outputs);
h.push_str("[maxvertexcount(6)]\nvoid main(triangle XclOut v[3], inout TriangleStream<XclOut> s)\n{\n");
h.push_str(" s.Append(v[0]);\n s.Append(v[1]);\n s.Append(v[2]);\n");
h.push_str(" if (!(v[0].pos.w > 0.0 && v[1].pos.w == v[0].pos.w && v[2].pos.w == v[0].pos.w))\n return;\n");
// the corner c: x shared with a and y with b, or the other way round (a = c + 1, b = c + 2);
// one w: clip-space x and y compare as the screen's do
h.push_str(" float2 q[3] = { v[0].pos.xy, v[1].pos.xy, v[2].pos.xy };\n");
// the corner c: x shared with a and y with b, or the other way round (a = c + 1, b = c + 2)
h.push_str(" uint c = 3u;\n [unroll] for (uint i = 0u; i < 3u; i++) {\n");
h.push_str(" float2 p = q[i], a = q[(i + 1u) % 3u], b = q[(i + 2u) % 3u];\n");
h.push_str(" bool corner = (p.x == a.x && p.y == b.y) || (p.x == b.x && p.y == a.y);\n");
h.push_str(" bool degenerate = all(p == a) || all(p == b) || all(a == b);\n");
h.push_str(" if (c == 3u && corner && !degenerate)\n c = i;\n }\n");
h.push_str(" if (c == 3u)\n return;\n");
h.push_str(" XclOut k = v[c], a = v[(c + 1u) % 3u], b = v[(c + 2u) % 3u];\n XclOut d = a;\n");
h.push_str(" uint c = 3u;\n");
h.push_str(" if (v[0].pos.w > 0.0 && v[1].pos.w == v[0].pos.w && v[2].pos.w == v[0].pos.w) {\n");
h.push_str(" float2 q[3] = { v[0].pos.xy, v[1].pos.xy, v[2].pos.xy };\n");
h.push_str(" [unroll] for (uint i = 0u; i < 3u; i++) {\n");
h.push_str(" float2 p = q[i], a = q[(i + 1u) % 3u], b = q[(i + 2u) % 3u];\n");
h.push_str(" bool corner = (p.x == a.x && p.y == b.y) || (p.x == b.x && p.y == a.y);\n");
h.push_str(" bool degenerate = all(p == a) || all(p == b) || all(a == b);\n");
h.push_str(" if (c == 3u && corner && !degenerate)\n c = i;\n }\n }\n");
h.push_str(" if (c == 3u) {\n s.Append(v[0]);\n s.Append(v[1]);\n s.Append(v[2]);\n return;\n }\n");
h.push_str(" XclOut k = v[c], a = v[(c + 1u) % 3u], b = v[(c + 2u) % 3u];\n XclOut d = v[0];\n");
h.push_str(" d.pos = a.pos + b.pos - k.pos;\n");
for k in 0..ranked(outputs, true).len() {
let _ = writeln!(h, " d.p{k} = a.p{k} + b.p{k} - k.p{k};");
for (k, e) in ranked(outputs, true).iter().enumerate() {
if hlsl_type(e) == "float" {
let _ = writeln!(h, " d.p{k} = a.p{k} + b.p{k} - k.p{k};");
}
}
// (c, a, b) is the first triangle's winding; (a, d, b) – its point reflection – keeps it
h.push_str(" s.RestartStrip();\n s.Append(a);\n s.Append(d);\n s.Append(b);\n}\n");
Ok(h)
// (corner, a, b) is v0 v1 v2 in its order or rotated; (corner, a, d), (corner, d, b) and
// (a, d, b) wind as it does. Of those, the two halves that both hold v0, v0 first:
// v0 v1 d, v0 d v2 (corner v0) – v0 v1 v2, v0 v2 d (v1) – v0 v1 v2, v0 d v1 (v2)
h.push_str(" s.Append(v[0]);\n s.Append(v[1]);\n");
h.push_str(" if (c == 0u)\n s.Append(d);\n else\n s.Append(v[2]);\n");
h.push_str(" s.RestartStrip();\n s.Append(v[0]);\n");
h.push_str(" if (c == 1u) {\n s.Append(v[2]);\n s.Append(d);\n } else {\n s.Append(d);\n");
h.push_str(" if (c == 0u)\n s.Append(v[2]);\n else\n s.Append(v[1]);\n }\n}\n");
h
}

/// Second pass of the mesh mode: a mesh shader that turns the records written by the
Expand Down
31 changes: 1 addition & 30 deletions crates/xcl/src/gpu/recomp/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,36 +589,7 @@ pub fn rect_list_gs(vs: &[u8]) -> Result<Vec<u8>, String> {
if outputs.is_empty() {
return Err("the VS has no output signature".into());
}
Ok(dxc::compile_cached(&super::emit::rect_list_gs(&outputs)?, "main", "gs_6_6")?.0)
}

/// Whether the console PS `ps` (its container) reads an attribute flat: an integer input, or a
/// `v_interp_mov_f32` – what the translation declares `nointerpolation` (`emit`, `XclIn`). A rect
/// list's flat values are not carried by `rect_list_gs` (see there).
pub fn ps_reads_flat(ps: &[u8]) -> Result<bool, String> {
let sh = xbox::parse(ps).ok_or("not a console shader container")?;
let f = sh.fssm.iter().find(|f| f.stages.first().is_some_and(|s| s.hw == 5)).ok_or("no PS hardware variant")?;
let Some(Mode::Pixel(psi)) = interface(ps, &sh, f) else {
return Err("unknown PS interface".into());
};
if super::ranked(&psi.inputs, false).iter().any(|e| e.component_type != 3) {
return Ok(true);
}
let p = f.programs.first().ok_or("FSSM without a program")?;
reads_flat(&f.data[p.offset..p.offset + p.len])
}

/// Whether PS code has a `v_interp_mov_f32`.
pub fn reads_flat(code: &[u8]) -> Result<bool, String> {
let mut off = 0usize;
while off + 4 <= code.len() {
let d = crate::gpu::rdna2::decode(&code[off..], off as u64).map_err(|e| format!("+{off:#x}: cannot decode ({e:?})"))?;
if d.inst.name.eq_ignore_ascii_case("v_interp_mov_f32") {
return Ok(true);
}
off += d.len;
}
Ok(false)
Ok(dxc::compile_cached(&super::emit::rect_list_gs(&outputs), "main", "gs_6_6")?.0)
}

/// XCL shader ABI root signature (see the module docs); `graphics` allows an input layout.
Expand Down
60 changes: 56 additions & 4 deletions crates/xcl/src/gpu/recomp/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,23 @@ pub fn draw_gs(
size: u32,
clear: [f32; 4],
) -> Result<Vec<[f32; 4]>, String> {
draw_gs_views(vs, gs, ps, layout, vertices, stride, sgprs, mem, &[], &[], size, clear)
draw_gs_views(vs, gs, ps, layout, vertices, stride, &[], sgprs, mem, &[], &[], size, clear)
}

/// `draw_gs` of an indexed triangle list (`indices`).
#[allow(clippy::too_many_arguments)]
pub fn draw_gs_indexed(
vs: &[u8],
gs: Option<&[u8]>,
ps: &[u8],
layout: &[VertexElement],
vertices: &[f32],
stride: u32,
indices: &[u32],
size: u32,
clear: [f32; 4],
) -> Result<Vec<[f32; 4]>, String> {
draw_gs_views(vs, gs, ps, layout, vertices, stride, indices, &[], &[], &[], &[], size, clear)
}

/// `draw` with textures and samplers in the directly indexed heaps (see `ViewTexture`).
Expand All @@ -666,7 +682,7 @@ pub fn draw_views(
size: u32,
clear: [f32; 4],
) -> Result<Vec<[f32; 4]>, String> {
draw_gs_views(vs, None, ps, layout, vertices, stride, sgprs, mem, textures, samplers, size, clear)
draw_gs_views(vs, None, ps, layout, vertices, stride, &[], sgprs, mem, textures, samplers, size, clear)
}

#[allow(clippy::too_many_arguments)]
Expand All @@ -677,6 +693,7 @@ fn draw_gs_views(
layout: &[VertexElement],
vertices: &[f32],
stride: u32,
indices: &[u32],
sgprs: &[u32],
mem: &[u32],
textures: &[ViewTexture],
Expand Down Expand Up @@ -722,7 +739,7 @@ fn draw_gs_views(
pd.RTVFormats[0] = DXGI_FORMAT_R32G32B32A32_FLOAT;
let pso: ID3D12PipelineState = dev.CreateGraphicsPipelineState(&pd).map_err(|x| format!("CreateGraphicsPipelineState: {x}"))?;
let draw = Draw { sgprs: sgprs.to_vec(), scissor: [0, 0, size as i32, size as i32] };
draw_on_multi(dev, &pso, &rs, vertices, stride, &[draw], mem, textures, samplers, size, clear)
draw_indexed_on(dev, &pso, &rs, vertices, stride, indices, &[draw], mem, textures, samplers, size, clear)
}
}

Expand Down Expand Up @@ -842,6 +859,25 @@ pub fn draw_on_multi(
samplers: &[D3D12_SAMPLER_DESC],
size: u32,
clear: [f32; 4],
) -> Result<Vec<[f32; 4]>, String> {
draw_indexed_on(dev, pso, rs, vertices, stride, &[], draws, mem, textures, samplers, size, clear)
}

/// `draw_on_multi`, the draws indexed when there are `indices` (one index buffer for all).
#[allow(clippy::too_many_arguments)]
fn draw_indexed_on(
dev: &ID3D12Device,
pso: &ID3D12PipelineState,
rs: &ID3D12RootSignature,
vertices: &[f32],
stride: u32,
indices: &[u32],
draws: &[Draw],
mem: &[u32],
textures: &[ViewTexture],
samplers: &[D3D12_SAMPLER_DESC],
size: u32,
clear: [f32; 4],
) -> Result<Vec<[f32; 4]>, String> {
unsafe {
let queue: ID3D12CommandQueue =
Expand Down Expand Up @@ -928,11 +964,27 @@ pub fn draw_on_multi(
list.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
let vbv = D3D12_VERTEX_BUFFER_VIEW { BufferLocation: upload.GetGPUVirtualAddress(), SizeInBytes: vb_bytes as u32, StrideInBytes: stride };
list.IASetVertexBuffers(0, Some(&[vbv]));
let _index_buf = if indices.is_empty() {
None
} else {
let bytes = (indices.len() * 4) as u64;
let b = buffer(dev, bytes, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, false)?;
let mut p = std::ptr::null_mut();
b.Map(0, None, Some(&mut p)).map_err(e)?;
std::ptr::copy_nonoverlapping(indices.as_ptr(), p as *mut u32, indices.len());
b.Unmap(0, None);
list.IASetIndexBuffer(Some(&D3D12_INDEX_BUFFER_VIEW { BufferLocation: b.GetGPUVirtualAddress(), SizeInBytes: bytes as u32, Format: DXGI_FORMAT_R32_UINT }));
Some(b)
};
for (k, d) in draws.iter().enumerate() {
list.SetGraphicsRootUnorderedAccessView(0, io_buf.GetGPUVirtualAddress() + (k * KERNEL_SGPRS * 4) as u64);
let [left, top, right, bottom] = d.scissor;
list.RSSetScissorRects(&[windows::Win32::Foundation::RECT { left, top, right, bottom }]);
list.DrawInstanced((vertices.len() as u32 * 4) / stride, 1, 0, 0);
if indices.is_empty() {
list.DrawInstanced((vertices.len() as u32 * 4) / stride, 1, 0, 0);
} else {
list.DrawIndexedInstanced(indices.len() as u32, 1, 0, 0, 0);
}
}
list.ResourceBarrier(&[transition(&rt, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE)]);
let dst = D3D12_TEXTURE_COPY_LOCATION {
Expand Down
Loading
Loading