From d8b258a455e50035369e934b597131c00881bceb Mon Sep 17 00:00:00 2001 From: fxceless <36782933+fxceless@users.noreply.github.com> Date: Fri, 27 Nov 2026 22:43:13 +0300 Subject: [PATCH] GPU: a view moved to the first level with tiles keeps its minimum level `ResourceMinLODClamp` is measured from mip 0 of the resource, not from the view's first level, so moving a view's start must not shift it. The test now samples the same resident level through the adjusted and the original view instead of expecting the adjusted one to read level 1. Ported from fxceless/victory-fork 41f9857 (fork pull request #1210). (cherry picked from commit 4bdf2b31f95f099091aa192791685ff2ba0c14ce) --- crates/victory-gpu/src/tiles.rs | 45 ++++++++++++++++----------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/crates/victory-gpu/src/tiles.rs b/crates/victory-gpu/src/tiles.rs index b8ca6a8..e636932 100644 --- a/crates/victory-gpu/src/tiles.rs +++ b/crates/victory-gpu/src/tiles.rs @@ -649,13 +649,13 @@ impl Drop for Tiled { /// Keeps a two-dimensional shader view (2D or 2D array) from the levels /// before `first` of a texture of `levels` levels: it starts at `first` if /// it started before it, shows as many levels fewer (clamped to the -/// remaining levels), and reduces `ResourceMinLODClamp` by the same shift so -/// the effective minimum LOD stays at the same resource level. +/// remaining levels). `ResourceMinLODClamp` is measured from mip 0 of the +/// resource, so moving the start of the view preserves that field. pub(crate) fn show_from(desc: &mut D3D12_SHADER_RESOURCE_VIEW_DESC, first: u32, levels: u32) { if first >= levels { return; } - let clamp = |most: &mut u32, mips: &mut u32, min_lod: &mut f32| { + let clamp = |most: &mut u32, mips: &mut u32| { if first <= *most { return; } @@ -664,20 +664,17 @@ pub(crate) fn show_from(desc: &mut D3D12_SHADER_RESOURCE_VIEW_DESC, first: u32, if *mips != u32::MAX { *mips = mips.saturating_sub(fewer).clamp(1, levels - first); } - if *min_lod > 0.0 { - *min_lod = (*min_lod - fewer as f32).max(0.0); - } }; match desc.ViewDimension { D3D12_SRV_DIMENSION_TEXTURE2D => { // SAFETY: `ViewDimension` selects `Texture2D`. let view = unsafe { &mut desc.Anonymous.Texture2D }; - clamp(&mut view.MostDetailedMip, &mut view.MipLevels, &mut view.ResourceMinLODClamp); + clamp(&mut view.MostDetailedMip, &mut view.MipLevels); } D3D12_SRV_DIMENSION_TEXTURE2DARRAY => { // SAFETY: `ViewDimension` selects `Texture2DArray`. let view = unsafe { &mut desc.Anonymous.Texture2DArray }; - clamp(&mut view.MostDetailedMip, &mut view.MipLevels, &mut view.ResourceMinLODClamp); + clamp(&mut view.MostDetailedMip, &mut view.MipLevels); } _ => {} } @@ -1265,10 +1262,10 @@ void look() { give_back(singles[1].1, &singles[1].2); } - /// (v) A view made by [`show_from`] of a texture whose level 0 has no - /// tiles returns level 1's colour for a sample at level 0; the view as - /// the title asked returns zero. - fn the_view_starts_at_the_first_level_with_tiles(rig: &mut Rig) -> Option<()> { + /// (v) Sample LOD 0 through the adjusted and original views. Level 0 + /// has no tiles. A minimum LOD inside the resident range should select + /// the same resource level through either view. + fn sample_resident_views(rig: &mut Rig, min_lod: f32) -> Option<[u32; 2]> { rig.step = "step 5, the view from the first level with tiles"; let cs = compile(LOOK, s!("look"), s!("cs_5_1")).ok()?; let format = DXGI_FORMAT_R8G8B8A8_UNORM; @@ -1314,7 +1311,7 @@ void look() { MostDetailedMip: 0, MipLevels: u32::MAX, PlaneSlice: 0, - ResourceMinLODClamp: 0.0, + ResourceMinLODClamp: min_lod, }, }, }; @@ -1356,14 +1353,12 @@ void look() { let mut data = std::ptr::null_mut(); let range = D3D12_RANGE { Begin: 0, End: 16 }; back.Map(0, Some(&range), Some(&mut data)).unwrap(); - let read = std::slice::from_raw_parts(data.cast::(), 2).to_vec(); + let read = data.cast::<[u32; 2]>().read(); back.Unmap(0, None); read }; - eprintln!("a sample at level 0: {} through the view from level 1, {} through the view as asked", read[0], read[1]); - assert_eq!(read[0], 32, "the view from the first level with tiles reads level 1"); - assert_eq!(read[1], 0, "the view from level 0 reads a level without tiles"); - Some(()) + eprintln!("sample at LOD 0, minimum {min_lod}: adjusted and original {read:?}"); + Some(read) } fn a_full_card_keeps_new_levels_waiting(rig: &mut Rig) { @@ -1452,8 +1447,12 @@ void look() { a_level_on_several_ranges(&mut rig); a_full_card_keeps_new_levels_waiting(&mut rig); a_claim_given_up_is_made_again(&mut rig); - if the_view_starts_at_the_first_level_with_tiles(&mut rig).is_none() { - eprintln!("the view test's shader or objects could not be made; that part was skipped"); + for (min_lod, expected) in [(0.0, [32, 0]), (3.0, [64, 64])] { + let Some(read) = sample_resident_views(&mut rig, min_lod) else { + eprintln!("the view test's shader or objects could not be made; that part was skipped"); + break; + }; + assert_eq!(read, expected, "resource minimum LOD {min_lod}"); } rig.step = "the end"; rig.check("every step done"); @@ -1482,7 +1481,7 @@ void look() { } #[test] - fn show_from_clamps_2d_and_2d_array_and_adjusts_min_lod() { + fn show_from_preserves_resource_min_lod_for_2d_and_2d_array() { let mut srv2d = D3D12_SHADER_RESOURCE_VIEW_DESC { Format: DXGI_FORMAT_R8G8B8A8_UNORM, ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D, @@ -1502,7 +1501,7 @@ void look() { assert_eq!(v2d.MostDetailedMip, 3); // Clamped to remaining levels (11 - 3 = 8), not 12 - 3 = 9. assert_eq!(v2d.MipLevels, 8); - assert_eq!(v2d.ResourceMinLODClamp, 0.5); + assert_eq!(v2d.ResourceMinLODClamp, 3.5); let mut srv_arr = D3D12_SHADER_RESOURCE_VIEW_DESC { Format: DXGI_FORMAT_R8G8B8A8_UNORM, @@ -1524,7 +1523,7 @@ void look() { let varr = unsafe { srv_arr.Anonymous.Texture2DArray }; assert_eq!(varr.MostDetailedMip, 2); assert_eq!(varr.MipLevels, 4); - assert_eq!(varr.ResourceMinLODClamp, 0.0); + assert_eq!(varr.ResourceMinLODClamp, 1.5); } #[test]