Rect lists: retry a refused GS pipeline without it, map untranslated stream rect lists

From the review of #199:
- translated_graphics_pso: when the driver refuses a pipeline with the
  generated rect list GS, it is made once more without it
  (retry_without_rect_gs) and a note is logged, so each rect is drawn as
  its first triangle, as before, rather than by the dummy shaders.
- A stream pipeline of type 19 that is not translated (HS/DS, a
  translation error) gets the PC's topology type before the driver sees it
  (untranslated_stream_desc), as at 956a476, where the stream path mapped
  it. dev_create_graphics_pso's body is now graphics_pso, with a flag
  for descs from a stream; a direct CreateGraphicsPipelineState is
  unchanged.
- rect_list_gs compares clip-space x and y directly: w is one already.

Tests (CPU): a_refused_rect_list_gs_is_retried_without_it (injected
refusal), untranslated_stream_rect_lists_get_the_pcs_topology_type.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kalmarv
2026-11-23 15:31:47 -05:00
parent c217b1fbec
commit 938fdd2e65
2 changed files with 104 additions and 5 deletions
+2 -1
View File
@@ -4531,7 +4531,8 @@ pub fn rect_list_gs(outputs: &[SigElement]) -> Result<String, String> {
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");
h.push_str(" float2 q[3] = { v[0].pos.xy / v[0].pos.w, v[1].pos.xy / v[1].pos.w, v[2].pos.xy / v[2].pos.w };\n");
// 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");
+102 -4
View File
@@ -3274,7 +3274,7 @@ unsafe fn translated_graphics_pso(this: *mut c_void, f: PsoFn, desc: *const c_vo
if debug_pso_switch("XCL_DEBUG_NO_CULL", ps_hash) {
copy.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
}
let hr = finish_pso(this, f, &copy as *const _ as *const c_void, riid, ppv);
let hr = retry_without_rect_gs(&mut copy, rect_gs.is_some(), |c| finish_pso(this, f, c as *const _ as *const c_void, riid, ppv));
if hr < 0 {
note_translation_error(format!(
"the driver rejected the PSO with the translated shaders ({hr:#x}; topology type {}, {} render targets, DSV format {}, HS {}, GS {})",
@@ -3538,6 +3538,11 @@ unsafe fn build_stream(dev: Dev, d: &OwnedDesc, out: *mut *mut c_void) -> bool {
}
pub(super) unsafe extern "system" fn dev_create_graphics_pso(this: *mut c_void, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT {
graphics_pso(this, desc, riid, ppv, false)
}
/// `dev_create_graphics_pso`; `stream`: the desc is a pipeline stream's (`stream_pipeline`).
unsafe fn graphics_pso(this: *mut c_void, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void, stream: bool) -> HRESULT {
super::null::capture_graphics_desc(desc as *const u8);
let f: PsoFn = std::mem::transmute(orig_fn(this, slot_of(this, "CreateGraphicsPipelineState")));
let (translated, missed) = if super::pso_async::enabled() { attempt(|| translated_graphics_pso(this, f, desc, riid, ppv)) } else { (translated_graphics_pso(this, f, desc, riid, ppv), false) };
@@ -3551,7 +3556,8 @@ pub(super) unsafe extern "system" fn dev_create_graphics_pso(this: *mut c_void,
} else if let Some(hr) = translated {
return hr;
}
let hr = finish_pso(this, f, desc, riid, ppv);
let pc = untranslated_stream_desc(desc, stream);
let hr = finish_pso(this, f, pc.as_ref().map_or(desc, |g| g as *const _ as *const c_void), riid, ppv);
if hr >= 0 || !crate::gpu::shader::readable(desc as usize, std::mem::size_of::<D3D12_GRAPHICS_PIPELINE_STATE_DESC>()) {
return hr;
}
@@ -3958,6 +3964,34 @@ unsafe fn fallback_stream(desc: *const u8) -> Option<(Vec<u8>, [usize; 2])> {
/// The console's pipeline topology type of rect lists (draws of topology 17, `list_x::RECT_LIST`).
const RECT_LIST_TYPE: i32 = 19;
/// `create(copy)`, and once more without the generated rect list GS (`rect_gs`) when the driver
/// refuses the pipeline with it: each rect is then drawn as the triangle of its three vertices,
/// as without the GS, rather than not at all (the untranslated fallback's dummy shaders).
fn retry_without_rect_gs(copy: &mut D3D12_GRAPHICS_PIPELINE_STATE_DESC, rect_gs: bool, mut create: impl FnMut(&D3D12_GRAPHICS_PIPELINE_STATE_DESC) -> HRESULT) -> HRESULT {
let hr = create(copy);
if hr >= 0 || !rect_gs {
return hr;
}
note_translation_error(format!("the driver rejected the PSO with the rect list GS ({hr:#x}): rect list drawn as triangles"));
copy.GS = D3D12_SHADER_BYTECODE::default();
create(copy)
}
/// What an untranslated pipeline from a stream is made from: a rect list's (type 19, which
/// `stream_pipeline` keeps for `translated_graphics_pso`) with the PC's topology type, as the
/// stream path made it before; None: `desc` as it is.
unsafe fn untranslated_stream_desc(desc: *const c_void, stream: bool) -> Option<D3D12_GRAPHICS_PIPELINE_STATE_DESC> {
if !stream || !crate::gpu::shader::readable(desc as usize, std::mem::size_of::<D3D12_GRAPHICS_PIPELINE_STATE_DESC>()) {
return None;
}
let mut g = std::ptr::read(desc as *const D3D12_GRAPHICS_PIPELINE_STATE_DESC);
if g.PrimitiveTopologyType.0 != RECT_LIST_TYPE {
return None;
}
pc_topology_type(&mut g);
Some(g)
}
/// The GS that completes a rect list's rects behind the console VS `vs` and the PS `ps`; refused
/// for a PS that reads flat or is not a console one (its inputs' modes are not known).
fn rect_list_gs(vs: &[u8], ps: Option<&[u8]>) -> Result<Vec<u8>, String> {
@@ -4114,7 +4148,8 @@ unsafe fn stream_pipeline(desc: *const c_void) -> StreamPipeline {
_ => {}
}
}
// a rect list stays one for translated_graphics_pso, which gives it its GS
// a rect list stays one for translated_graphics_pso, which gives it its GS (untranslated, it
// becomes the PC's in graphics_pso)
if g.PrimitiveTopologyType.0 != RECT_LIST_TYPE {
pc_topology_type(&mut g);
}
@@ -4142,7 +4177,7 @@ pub(super) unsafe extern "system" fn dev_create_pipeline_state(this: *mut c_void
return hr;
}
match stream_pipeline(desc) {
StreamPipeline::Graphics(d) => return dev_create_graphics_pso(this, &*d as *const _ as *const c_void, riid, ppv),
StreamPipeline::Graphics(d) => return graphics_pso(this, &*d as *const _ as *const c_void, riid, ppv, true),
StreamPipeline::Compute(d) => return dev_create_compute_pso(this, &*d as *const _ as *const c_void, riid, ppv),
StreamPipeline::Unknown => {}
}
@@ -4679,6 +4714,69 @@ pub fn stop_if_device_removed() {
}
}
#[cfg(test)]
mod rect_list_pso_tests {
use super::*;
const REFUSED: HRESULT = 0x8007_0057u32 as i32;
fn with_gs(gs: &[u8]) -> D3D12_GRAPHICS_PIPELINE_STATE_DESC {
D3D12_GRAPHICS_PIPELINE_STATE_DESC { GS: D3D12_SHADER_BYTECODE { pShaderBytecode: gs.as_ptr() as _, BytecodeLength: gs.len() }, ..Default::default() }
}
/// A pipeline the driver refuses with the rect list GS is made again without it, once; one
/// it accepts keeps the GS, and a refused pipeline without a rect list GS is not retried.
#[test]
fn a_refused_rect_list_gs_is_retried_without_it() {
let gs = [0u8; 4];
let mut copy = with_gs(&gs);
let mut calls = Vec::new();
let hr = retry_without_rect_gs(&mut copy, true, |c| {
calls.push(c.GS.pShaderBytecode.is_null());
if c.GS.pShaderBytecode.is_null() {
0
} else {
REFUSED
}
});
assert_eq!((hr, calls), (0, vec![false, true]));
let mut copy = with_gs(&gs);
let mut n = 0;
assert_eq!(
retry_without_rect_gs(&mut copy, true, |_| {
n += 1;
0
}),
0
);
assert!(n == 1 && !copy.GS.pShaderBytecode.is_null());
let mut n = 0;
assert_eq!(
retry_without_rect_gs(&mut copy, false, |_| {
n += 1;
REFUSED
}),
REFUSED
);
assert!(n == 1 && !copy.GS.pShaderBytecode.is_null());
}
/// An untranslated rect list pipeline from a stream gets the PC's topology type (TRIANGLE),
/// as at 956a476; other types, and a direct CreateGraphicsPipelineState, are left as they are.
#[test]
fn untranslated_stream_rect_lists_get_the_pcs_topology_type() {
let at = |t: i32| D3D12_GRAPHICS_PIPELINE_STATE_DESC { PrimitiveTopologyType: D3D12_PRIMITIVE_TOPOLOGY_TYPE(t), ..Default::default() };
let desc = |g: &D3D12_GRAPHICS_PIPELINE_STATE_DESC| g as *const _ as *const c_void;
let (rect, tri) = (at(RECT_LIST_TYPE), at(3));
unsafe {
let g = untranslated_stream_desc(desc(&rect), true).expect("a stream's rect list");
assert_eq!(g.PrimitiveTopologyType, D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE);
assert!(untranslated_stream_desc(desc(&tri), true).is_none());
assert!(untranslated_stream_desc(desc(&rect), false).is_none());
}
}
}
#[cfg(test)]
mod cache_report_tests {
use super::*;