diff --git a/crates/xcl/src/gpu/desc.rs b/crates/xcl/src/gpu/desc.rs index a400a70..26a396c 100644 --- a/crates/xcl/src/gpu/desc.rs +++ b/crates/xcl/src/gpu/desc.rs @@ -259,15 +259,13 @@ pub fn image_d3d12(t: &Image) -> Option<(D3D12_RESOURCE_DESC, D3D12_SHADER_RESOU v.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE3D; v.Anonymous.Texture3D = D3D12_TEX3D_SRV { MostDetailedMip: t.base_level, MipLevels: levels, ResourceMinLODClamp: lod }; } - ImageType::Cube if depth > 6 => { + // always a cube array (of one cube, too): translated shaders declare TextureCubeArray, + // as a cube T# says nothing at compile time about how many cubes it has + ImageType::Cube => { v.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBEARRAY; v.Anonymous.TextureCubeArray = D3D12_TEXCUBE_ARRAY_SRV { MostDetailedMip: t.base_level, MipLevels: levels, First2DArrayFace: t.base_array, NumCubes: slices / 6, ResourceMinLODClamp: lod }; } - ImageType::Cube => { - v.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBE; - v.Anonymous.TextureCube = D3D12_TEXCUBE_SRV { MostDetailedMip: t.base_level, MipLevels: levels, ResourceMinLODClamp: lod }; - } ImageType::D2Msaa => v.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DMS, ImageType::D2MsaaArray => { v.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY; diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 82724ee..5d60a4a 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -2130,7 +2130,8 @@ impl Em { 0 => ("Texture1D", coords[0].clone()), 1 => ("Texture2D", format!("float2({}, {})", coords[0], coords[1])), 2 => ("Texture3D", format!("float3({}, {}, {})", coords[0], coords[1], coords[2])), - 3 => ("TextureCube", format!("cube_dir({}, {}, {})", coords[0], coords[1], coords[2])), + // every cube T# is a cube array view; the face coordinate is face + 8 · slice + 3 => ("TextureCubeArray", format!("float4(cube_dir({0}, {1}, {2}), cube_slice({2}))", coords[0], coords[1], coords[2])), 4 => ("Texture1DArray", format!("float2({}, round({}))", coords[0], coords[1])), 5 => ("Texture2DArray", format!("float3({}, {}, round({}))", coords[0], coords[1], coords[2])), _ => return Err("MIMG dim"), @@ -2185,7 +2186,7 @@ impl Em { (None, Some((dx, dy)), _, _) if dim == 3 => { let len = |x: &[String]| format!("length(float2({}, {}))", x[0], x.get(1).cloned().unwrap_or("0.0f".into())); let size = format!("{t}_cw"); - self.line(&format!("uint {size} = 1u, {t}_ch = 1u, {t}_cn = 1u; {t}_T.GetDimensions(0u, {size}, {t}_ch, {t}_cn);")); + self.line(&format!("uint {size} = 1u, {t}_ch = 1u, {t}_ce = 1u, {t}_cn = 1u; {t}_T.GetDimensions(0u, {size}, {t}_ch, {t}_ce, {t}_cn);")); let lod = format!("log2(max(max({}, {}) * (float){size}, 1e-8f))", len(dx), len(dy)); format!("{t}_T.SampleLevel({t}_S, {coord}, {})", floor(lod)) } @@ -2217,7 +2218,8 @@ impl Em { 0 => ("Texture1D", coords[0].clone()), 1 => ("Texture2D", format!("float2({}, {})", coords[0], coords[1])), 2 => ("Texture3D", format!("float3({}, {}, {})", coords[0], coords[1], coords[2])), - 3 => ("TextureCube", format!("cube_dir({}, {}, {})", coords[0], coords[1], coords[2])), + // a cube array's level of detail takes the direction only + 3 => ("TextureCubeArray", format!("cube_dir({}, {}, {})", coords[0], coords[1], coords[2])), 4 => ("Texture1DArray", format!("float2({}, round({}))", coords[0], coords[1])), 5 => ("Texture2DArray", format!("float3({}, {}, round({}))", coords[0], coords[1], coords[2])), _ => return Err("MIMG dim"), @@ -2240,7 +2242,8 @@ impl Em { 0 => ("Texture1D", format!("{t}_T.GetDimensions({mipv}, {t}_w, {t}_n); {t}_h = 1u; {t}_d = 1u;")), 1 => ("Texture2D", format!("{t}_T.GetDimensions({mipv}, {t}_w, {t}_h, {t}_n); {t}_d = 1u;")), 2 => ("Texture3D", format!("{t}_T.GetDimensions({mipv}, {t}_w, {t}_h, {t}_d, {t}_n);")), - 3 => ("TextureCube", format!("{t}_T.GetDimensions({mipv}, {t}_w, {t}_h, {t}_n); {t}_d = 1u;")), + // the hardware returns the layers (6 per cube), as for a 2D array + 3 => ("TextureCubeArray", format!("{t}_T.GetDimensions({mipv}, {t}_w, {t}_h, {t}_d, {t}_n); {t}_d *= 6u;")), 4 => ("Texture1DArray", format!("{t}_T.GetDimensions({mipv}, {t}_w, {t}_d, {t}_n); {t}_h = 1u;")), 5 => ("Texture2DArray", format!("{t}_T.GetDimensions({mipv}, {t}_w, {t}_h, {t}_d, {t}_n);")), _ => return Err("get_resinfo dim"), @@ -5157,6 +5160,8 @@ float3 cube_dir(float s, float t, float face) { if (f == 4u) return float3(u, -v, 1.0f); return float3(-u, -v, -1.0f); } +// the cube of a cube array the face coordinate (face + 8 · slice) is in +float cube_slice(float face) { return (float)(((uint)round(face)) / 8u); } uint first_lane(uint lo, uint hi) { return select(lo != 0u, firstbitlow(lo), select(hi != 0u, 32u + firstbitlow(hi), 0u)); } uint lanemask_lt_lo() { return lane_id >= 32u ? 0xFFFFFFFFu : (lane_id == 0u ? 0u : (0xFFFFFFFFu >> (32u - lane_id))); } uint lanemask_lt_hi() { return lane_id <= 32u ? 0u : (0xFFFFFFFFu >> (64u - lane_id)); } diff --git a/crates/xcl/src/gpu/recomp/runner.rs b/crates/xcl/src/gpu/recomp/runner.rs index dae6ca9..95abceb 100644 --- a/crates/xcl/src/gpu/recomp/runner.rs +++ b/crates/xcl/src/gpu/recomp/runner.rs @@ -142,7 +142,7 @@ pub struct TestTexture { pub format: DXGI_FORMAT, pub width: u32, pub height: u32, - /// array slices (cube: 6) or depth + /// array slices (cube: 6 per cube) or depth pub depth: u32, pub texel_bytes: u32, /// tightly packed texels, slice after slice @@ -288,10 +288,11 @@ unsafe fn bind_resources(dev: &ID3D12Device, list: &ID3D12GraphicsCommandList, t uv.Anonymous.Texture3D = D3D12_TEX3D_UAV { WSize: t.depth, ..Default::default() }; } TexDim::Cube => { - sv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBE; - sv.Anonymous.TextureCube = D3D12_TEXCUBE_SRV { MipLevels: 1, ..Default::default() }; + // a cube array, as translated shaders declare it (depth = 6 per cube) + sv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBEARRAY; + sv.Anonymous.TextureCubeArray = D3D12_TEXCUBE_ARRAY_SRV { MipLevels: 1, NumCubes: t.depth / 6, ..Default::default() }; uv.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY; - uv.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_UAV { ArraySize: 6, ..Default::default() }; + uv.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_UAV { ArraySize: t.depth, ..Default::default() }; } } if !t.uav { diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 2bcfc2c..2906575 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -1545,6 +1545,54 @@ fn tsharp(index: u32, format: u32, ty: u32) -> [u32; 8] { [index, format << 20, 0, ty << 28, 0, 0, 0, 0] } +/// A cube T# of two cubes: the face coordinate (face + 8 · cube, as v_cubeid and the console's +/// compiler give it) picks the cube, and image_get_resinfo gives the layers (6 per cube). +#[test] +fn gpu_cube_array_slices() { + if !gpu_ready() { + return; + } + use runner::{TestSampler, TestTexture, TexDim}; + use windows::Win32::Graphics::Direct3D12::*; + use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32B32A32_FLOAT; + let mimg = |name: &'static str, f: &[(&str, u64)]| -> Vec { encode(name, "ENC_MIMG", f, None).unwrap() }; + let mut code = Vec::new(); + // v[0:2] = (s, t, face + 8 · cube), T# in s[16:23], S# point in s[24:27] + code.extend(mimg("IMAGE_SAMPLE_LZ", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0xF), ("DIM", 3)])); + code.extend(mimg("IMAGE_GET_RESINFO", &[("VDATA", 20), ("VADDR", 3), ("SRSRC", 4), ("DMASK", 0xF), ("DIM", 3)])); + code.extend(encode("S_ENDPGM", "ENC_SOPP", &[], None).unwrap()); + let mut st = KernelState::default(); + st.sgpr[16..24].copy_from_slice(&tsharp(0, 77, 11)); + for (l, r) in st.vgpr.iter_mut().enumerate() { + let (face, cube) = (l % 6, l / 6 % 2); + r[0] = f(1.5); // the middle of the face + r[1] = f(1.5); + r[2] = f((face + 8 * cube) as f32); + r[3] = 0; // mip + } + // 2 x 2 faces, every texel of layer (cube, face) = (face, cube, 6 · cube + face, 1) + let mut data = Vec::new(); + for cube in 0..2 { + for face in 0..6 { + for _ in 0..4 { + for v in [face as f32, cube as f32, (6 * cube + face) as f32, 1.0] { + data.extend_from_slice(&v.to_le_bytes()); + } + } + } + } + let tex = [TestTexture { dim: TexDim::Cube, format: DXGI_FORMAT_R32G32B32A32_FLOAT, width: 2, height: 2, depth: 12, texel_bytes: 16, data, uav: false }]; + let samp = [TestSampler { filter: D3D12_FILTER_MIN_MAG_MIP_POINT, address: D3D12_TEXTURE_ADDRESS_MODE_CLAMP, comparison: None }]; + let r = runner::run_code_with(&code, &st, &[], &tex, &samp).unwrap(); + for l in 0..64 { + let v = &r.vgpr[l]; + let (face, cube) = ((l % 6) as f32, (l / 6 % 2) as f32); + let fl = |i: usize| f32::from_bits(v[i]); + assert_eq!([fl(10), fl(11), fl(12), fl(13)], [face, cube, 6.0 * cube + face, 1.0], "lane {l}: cube {cube} face {face}"); + assert_eq!(&v[20..24], &[2, 2, 12, 1], "lane {l} resinfo (w, h, layers, mips)"); + } +} + #[test] fn gpu_images_sample_load_store_atomic() { if !gpu_ready() { diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 190868c..33121fb 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -969,7 +969,8 @@ unsafe fn view_shape(res: &ID3D12Resource, format: windows::Win32::Graphics::Dxg let depth = match ty { T::D3 => d, T::D1Array | T::D2Array | T::D2MsaaArray => array.max(1), - T::Cube => 6, + // a cube view: its faces (6 per cube) + T::Cube => array.max(6), _ => 1, }; Some((fmt, ty, w, h, depth, mips)) @@ -1670,6 +1671,7 @@ unsafe extern "system" fn dev_create_srv_x(this: *mut c_void, res: *mut c_void, 7 => (a.Texture2DMSArray.ArraySize, 1), 8 => (1, a.Texture3D.MipLevels), 9 => (6, a.TextureCube.MipLevels), + 10 => (a.TextureCubeArray.NumCubes.max(1) * 6, a.TextureCubeArray.MipLevels), _ => (1, 1), }; (v.Format, v.ViewDimension.0, array, mips)