diff --git a/crates/xcl/src/gpu/desc.rs b/crates/xcl/src/gpu/desc.rs index 26a396cca..157bb1e18 100644 --- a/crates/xcl/src/gpu/desc.rs +++ b/crates/xcl/src/gpu/desc.rs @@ -365,6 +365,16 @@ pub fn buffer(w: &[u32; 4]) -> Buffer { mod tests { use super::*; + #[test] + fn cbv_is_float4_rows() { + let v = buffer(&cbv_words(0x4_4E10_DC00, 0x600)); + assert_eq!((v.address, v.stride, v.num_records, v.format), (0x4_4E10_DC00, 16, 0x60, 77)); + // an idxen load of row n reads base + 16 n; the scalar-load limit stays the view's size + assert_eq!(v.num_records * v.stride, 0x600); + // a size that is not a whole number of rows still covers its last bytes + assert_eq!(buffer(&cbv_words(0x1000, 0x608)).num_records, 0x61); + } + /// T# of a 2D BGRA-swizzled RGBA8 texture, 256×128, 8 mips, view of mips 1..3. fn tsharp() -> [u32; 8] { let (w, h) = (256u32 - 1, 128u32 - 1); @@ -655,6 +665,16 @@ pub fn format_bytes(f: u32) -> u32 { } } +/// A V# for a constant buffer view: 16-byte elements (one float4 row each), num_records in +/// rows, as the console writes it (a title that writes this descriptor itself writes stride 16 +/// and size / 16). Console shaders index constant-buffer arrays with `buffer_load_* … idxen` +/// and the row as the index, which a stride of 0 ignores (every row would read row 0). Loads by +/// byte offset read the same addresses as with a raw V#, and scalar loads keep their byte limit +/// (num_records × stride). +pub fn cbv_words(address: u64, size: u32) -> [u32; 4] { + buffer_words(address, 16, size.div_ceil(16), 77) +} + /// A V# for a buffer view: `stride` 0 = raw (num_records in bytes), else structured / typed /// (num_records in elements); `format` = GFX10 format for typed loads (raw: 32_FLOAT). pub fn buffer_words(address: u64, stride: u32, num_records: u32, format: u32) -> [u32; 4] { diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 94aeb8b3c..26019155e 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -1752,8 +1752,9 @@ fn note_buffer_view() { } } -/// #17 CreateConstantBufferView: a raw V# of the buffer's console address (unified memory; -/// anything else gets an empty V#). No PC CBV: translated shaders read buffers through `mem`. +/// #17 CreateConstantBufferView: a V# of float4 rows at the buffer's console address +/// (`desc::cbv_words`; unified memory, anything else gets an empty V#). No PC CBV: translated +/// shaders read buffers through `mem`. unsafe extern "system" fn dev_create_cbv_x(this: *mut c_void, desc: *const D3D12_CONSTANT_BUFFER_VIEW_DESC, cpu: u64) { let Some(slot) = super::console_heap::slot(cpu).filter(|s| !s.sampler) else { let f: unsafe extern "system" fn(*mut c_void, *const D3D12_CONSTANT_BUFFER_VIEW_DESC, u64) = std::mem::transmute(orig_fn(this, 17)); @@ -1765,7 +1766,7 @@ unsafe extern "system" fn dev_create_cbv_x(this: *mut c_void, desc: *const D3D12 note_buffer_view(); } let (va, size) = if in_uma { (va, size) } else { (0, 0) }; - super::console_heap::write(&slot, &crate::gpu::desc::buffer_words(va, 0, size, 22)); + super::console_heap::write(&slot, &crate::gpu::desc::cbv_words(va, size)); } /// #22 CreateSampler: the PC sampler at the slot's index, the S# in XCL ABI form (word 3