From 0b560fe199ffcb693318b9bb71e01496b89f28ce Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 31 Oct 2026 02:44:49 +0200 Subject: [PATCH] :new: Answer texture-buffer alignment and sparse tiles per pixel format The bundle answered neither minimumTextureBufferAlignmentForPixelFormat: nor either sparseTileSizeWithTextureType: form, and IOGPUMetalDevice has none of them. A call fell through to the zero catch-all, whose signature returns a scalar where both tile queries return an MTLSize. The blob now carries, for each pixel format of the SDK, its texture-buffer alignment and its 2D sparse tile at 1, 2 and 4 samples on 16, 64 and 256 KiB pages. The tile is one answer for every texture type, as Metal gives it on an M3 Max. Metal asks the alignment only of the 56 formats it answers; on every other one it fails an assertion, measured on the M3 Max. The Vulkan platform answers with the runtime's linear alignment, which Metal answers alike for every format, and with the runtime's tiles. --- .../include/reims_graphics_wire.h | 29 ++++++- crates/reims-graphics/src/wire.rs | 42 +++++++++- crates/reims-vgpu-core/src/platform/caps.rs | 48 ++++++++++++ .../src/testkit/impls/device.rs | 25 +++++- crates/reims-vgpu-metal/src/gpu/device.rs | 58 +++++++++++++- crates/reims-vgpu-vulkan/src/gpu/identity.rs | 76 ++++++++++++++++++- crates/reims-vgpu/src/graphics/device_info.rs | 50 +++++++++++- guest/bundle/src/RgfxCaps.cpp | 39 ++++++++++ guest/bundle/src/RgfxCaps.hpp | 8 ++ guest/bundle/src/RgfxMetalDevice.mm | 29 +++++++ guest/bundle/src/RgfxWireLayout.cpp | 4 +- guest/bundle/tests/serializer_tests.cpp | 21 +++++ 12 files changed, 411 insertions(+), 18 deletions(-) diff --git a/crates/reims-graphics/include/reims_graphics_wire.h b/crates/reims-graphics/include/reims_graphics_wire.h index 260b2909e..abb7771b1 100644 --- a/crates/reims-graphics/include/reims_graphics_wire.h +++ b/crates/reims-graphics/include/reims_graphics_wire.h @@ -1022,6 +1022,13 @@ // The counter sampling points [`RgfxDeviceInfo::counter_sampling`] answers. #define RGFX_DEVICE_INFO_COUNTER_POINTS 5 +// The most pixel formats [`RgfxDeviceInfo::formats`] answers. +#define RGFX_DEVICE_INFO_FORMATS 160 + +// The sparse tiles [`RgfxFormatAnswers::sparse_tiles`] holds: sample counts 1, 2 and 4, each +// at pages of 16, 64 and 256 KiB. +#define RGFX_DEVICE_INFO_SPARSE_TILES 9 + // A pipeline reflection reply's status: the pipeline was created. #define RGFX_PIPELINE_OK 0 @@ -2114,6 +2121,21 @@ typedef struct { RgfxStencilDescriptor back; } RgfxDepthStencilCreate; +// One pixel format's answers in [`RgfxDeviceInfo`]. +typedef struct { + // The raw `MTLPixelFormat`. + uint32_t format; + // `minimumTextureBufferAlignmentForPixelFormat:`; 0 for a format the host has no answer + // of (one Metal asserts on, among them). + uint32_t texture_buffer_alignment; + // `sparseTileSizeWithTextureType:pixelFormat:sampleCount:sparsePageSize:`, the same for + // every texture type: width, height and depth, at sample counts 1, 2 and 4 (major) and + // pages of 16, 64 and 256 KiB; all 0 where the host has no answer. + uint16_t sparse_tiles[RGFX_DEVICE_INFO_SPARSE_TILES][3]; + // Zero. + uint8_t reserved_3e[2]; +} RgfxFormatAnswers; + // The device-info caps blob the host writes once at device init, read by the // guest bundle before it makes any queue. It holds every answer of the guest's // `MTLDevice` that does not change while the device lives: the host device's @@ -2135,8 +2157,8 @@ typedef struct { uint32_t family_count; // The entries of `feature_set_values` and `feature_set_answers` in use. uint32_t feature_set_count; - // Zero. - uint32_t reserved_14; + // The entries of `formats` in use. + uint32_t format_count; // `registryID`. uint64_t registry_id; // `gpuCoreCount`, or 0 when the device does not answer it. @@ -2192,6 +2214,9 @@ typedef struct { // The device's `architecture.name`, UTF-8, `architecture_name_len` bytes // used, the rest 0. uint8_t architecture_name[RGFX_DEVICE_ARCHITECTURE_NAME_LEN]; + // Each pixel format's answers, `format_count` in use; a format the blob does + // not list has none. + RgfxFormatAnswers formats[RGFX_DEVICE_INFO_FORMATS]; } RgfxDeviceInfo; // `DISPLAY_CHANGED`: what the display offers has changed; the guest asks `GET_DISPLAYS` again. diff --git a/crates/reims-graphics/src/wire.rs b/crates/reims-graphics/src/wire.rs index 29210a289..1c22a14fc 100644 --- a/crates/reims-graphics/src/wire.rs +++ b/crates/reims-graphics/src/wire.rs @@ -1347,6 +1347,32 @@ pub const RGFX_DEVICE_INFO_IDENTITY_FLAGS: usize = 19; /// The counter sampling points [`RgfxDeviceInfo::counter_sampling`] answers. pub const RGFX_DEVICE_INFO_COUNTER_POINTS: usize = 5; +/// The most pixel formats [`RgfxDeviceInfo::formats`] answers. +pub const RGFX_DEVICE_INFO_FORMATS: usize = 160; + +/// The sparse tiles [`RgfxFormatAnswers::sparse_tiles`] holds: sample counts 1, 2 and 4, each +/// at pages of 16, 64 and 256 KiB. +pub const RGFX_DEVICE_INFO_SPARSE_TILES: usize = 9; + +/// One pixel format's answers in [`RgfxDeviceInfo`]. +#[repr(C)] +#[derive( + Clone, Copy, Debug, Default, PartialEq, Eq, FromBytes, IntoBytes, Immutable, KnownLayout, +)] +pub struct RgfxFormatAnswers { + /// The raw `MTLPixelFormat`. + pub format: u32, + /// `minimumTextureBufferAlignmentForPixelFormat:`; 0 for a format the host has no answer + /// of (one Metal asserts on, among them). + pub texture_buffer_alignment: u32, + /// `sparseTileSizeWithTextureType:pixelFormat:sampleCount:sparsePageSize:`, the same for + /// every texture type: width, height and depth, at sample counts 1, 2 and 4 (major) and + /// pages of 16, 64 and 256 KiB; all 0 where the host has no answer. + pub sparse_tiles: [[u16; 3]; RGFX_DEVICE_INFO_SPARSE_TILES], + /// Zero. + pub reserved_3e: [u8; 2], +} + /// The device-info caps blob the host writes once at device init, read by the /// guest bundle before it makes any queue. It holds every answer of the guest's /// `MTLDevice` that does not change while the device lives: the host device's @@ -1370,8 +1396,8 @@ pub struct RgfxDeviceInfo { pub family_count: u32, /// The entries of `feature_set_values` and `feature_set_answers` in use. pub feature_set_count: u32, - /// Zero. - pub reserved_14: u32, + /// The entries of `formats` in use. + pub format_count: u32, /// `registryID`. pub registry_id: u64, /// `gpuCoreCount`, or 0 when the device does not answer it. @@ -1427,6 +1453,9 @@ pub struct RgfxDeviceInfo { /// The device's `architecture.name`, UTF-8, `architecture_name_len` bytes /// used, the rest 0. pub architecture_name: [u8; RGFX_DEVICE_ARCHITECTURE_NAME_LEN], + /// Each pixel format's answers, `format_count` in use; a format the blob does + /// not list has none. + pub formats: [RgfxFormatAnswers; RGFX_DEVICE_INFO_FORMATS], } impl Default for RgfxDeviceInfo { @@ -1740,7 +1769,9 @@ const _: () = { assert!(size_of::() == 112); assert!(size_of::() == 56); assert!(RGFX_LIBRARY_BYTES_OFFSET as usize == size_of::()); - assert!(size_of::() == 1400); + assert!(size_of::() == 64); + assert!(size_of::() == 1400 + 64 * RGFX_DEVICE_INFO_FORMATS); + assert!(core::mem::offset_of!(RgfxDeviceInfo, formats) == 1400); assert!(core::mem::offset_of!(RgfxDeviceInfo, family_answers) == 944); assert!(core::mem::offset_of!(RgfxDeviceInfo, name) == 1080); // The device-info blob sits past the completion ring and before the allocations. @@ -1919,7 +1950,10 @@ mod tests { let name = b"NVIDIA GeForce RTX 5080 Laptop GPU"; info.name[..name.len()].copy_from_slice(name); info.name_len = name.len() as u32; - assert_eq!(info.as_bytes().len(), 1400); + info.format_count = 1; + info.formats[0].format = 80; + info.formats[0].sparse_tiles[0] = [64, 64, 1]; + assert_eq!(info.as_bytes().len(), 1400 + 64 * RGFX_DEVICE_INFO_FORMATS); assert_eq!( RgfxDeviceInfo::read_from_bytes(info.as_bytes()).unwrap(), info diff --git a/crates/reims-vgpu-core/src/platform/caps.rs b/crates/reims-vgpu-core/src/platform/caps.rs index 5d67e1517..2267d2df3 100644 --- a/crates/reims-vgpu-core/src/platform/caps.rs +++ b/crates/reims-vgpu-core/src/platform/caps.rs @@ -152,6 +152,52 @@ pub const COUNTER_SAMPLING_POINTS: [u64; 5] = [0, 1, 2, 3, 4]; /// [`IdentitySizes`] is the largest count up to it the query takes. pub const LARGEST_COUNT_ASKED: u64 = 16; +/// Every `MTLPixelFormat` of the macOS 27 SDK but Invalid, in its order. [`IdentityCaps`] answers +/// `sparseTileSizeWithTextureType:pixelFormat:sampleCount:sparsePageSize:` of each. +pub const SDK_PIXEL_FORMATS: [u64; 151] = [ + 1, 10, 11, 12, 13, 14, 20, 22, 23, 24, 25, 30, 31, 32, 33, 34, 40, 41, 42, 43, 45, 46, 47, 48, + 53, 54, 55, 60, 62, 63, 64, 65, 70, 71, 72, 73, 74, 80, 81, 90, 91, 92, 93, 94, 95, 96, 97, 98, + 99, 103, 104, 105, 110, 112, 113, 114, 115, 120, 121, 122, 123, 124, 125, 130, 131, 132, 133, + 134, 135, 140, 141, 142, 143, 150, 151, 152, 153, 160, 161, 162, 163, 164, 165, 166, 167, 170, + 172, 174, 176, 178, 179, 180, 181, 182, 183, 186, 187, 188, 189, 190, 192, 193, 194, 195, 196, + 197, 198, 199, 200, 204, 205, 206, 207, 208, 210, 211, 212, 213, 214, 215, 216, 217, 218, 222, + 223, 224, 225, 226, 228, 229, 230, 231, 232, 233, 234, 235, 236, 240, 241, 250, 252, 253, 255, + 260, 261, 262, 263, 552, 553, 554, 555, +]; + +/// The formats of [`SDK_PIXEL_FORMATS`] Metal answers `minimumTextureBufferAlignmentForPixelFormat:` +/// of, as it does `minimumLinearTextureAlignmentForPixelFormat:`: the uncompressed colour +/// formats that are neither packed nor YUV (measured on an Apple M3 Max, macOS 27.2: 16 for each, +/// and a failed assertion, which ends the process, for every other format of the SDK, so no +/// other is asked). +pub const TEXTURE_BUFFER_FORMATS: [u64; 56] = [ + 1, 10, 11, 12, 13, 14, 20, 22, 23, 24, 25, 30, 31, 32, 33, 34, 40, 41, 42, 43, 53, 54, 55, 60, + 62, 63, 64, 65, 70, 71, 72, 73, 74, 80, 81, 90, 91, 92, 93, 94, 103, 104, 105, 110, 112, 113, + 114, 115, 123, 124, 125, 263, 552, 553, 554, 555, +]; + +/// The sample counts [`FormatAnswers::sparse_tiles`] answers, in order. +pub const SPARSE_SAMPLE_COUNTS: [u64; 3] = [1, 2, 4]; + +/// The `MTLSparsePageSize`s [`FormatAnswers::sparse_tiles`] answers, in order: 16, 64 and 256 +/// KiB. The query without a page size answers the first's. +pub const SPARSE_PAGE_SIZES: [i64; 3] = [101, 102, 103]; + +/// One pixel format's answers in [`IdentityCaps`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct FormatAnswers { + /// The raw `MTLPixelFormat`. + pub format: u64, + /// `minimumTextureBufferAlignmentForPixelFormat:`; `None` for a format Metal does not answer + /// (it asserts) or where the platform has no answer. + pub texture_buffer_alignment: Option, + /// `sparseTileSizeWithTextureType:pixelFormat:sampleCount:sparsePageSize:` of a 2D texture, + /// width, height and depth, at each of [`SPARSE_SAMPLE_COUNTS`] (major) and each of + /// [`SPARSE_PAGE_SIZES`]: the same for every texture type (measured on the M3 Max); `None` + /// where the platform has no answer. + pub sparse_tiles: Option<[[u64; 3]; 9]>, +} + caps_struct! { /// The device's identity and size answers [`IdentityCaps`] holds, one field per selector: /// `None` where the platform has no answer of its own. @@ -218,6 +264,8 @@ pub struct IdentityCaps { pub counter_sampling: [Option; 5], /// `architecture.name`. pub architecture_name: Option, + /// The answers of each of [`SDK_PIXEL_FORMATS`], in its order. + pub formats: Vec, /// The integer answers. pub sizes: IdentitySizes, /// The `BOOL` answers. diff --git a/crates/reims-vgpu-core/src/testkit/impls/device.rs b/crates/reims-vgpu-core/src/testkit/impls/device.rs index 4c295b2ee..9c6d5c4df 100644 --- a/crates/reims-vgpu-core/src/testkit/impls/device.rs +++ b/crates/reims-vgpu-core/src/testkit/impls/device.rs @@ -18,7 +18,9 @@ use super::rate_map::RateMapModel; use super::resource::PipelineModel; use super::{Backing, ResolveIds, common, new_buffer, new_state, new_texture}; use crate::num; -use crate::platform::caps::{self, DeviceCaps, IdentityCaps, IdentityFlags, IdentitySizes}; +use crate::platform::caps::{ + self, DeviceCaps, FormatAnswers, IdentityCaps, IdentityFlags, IdentitySizes, +}; use crate::platform::device::*; use crate::platform::types::{ MtlSize, NsErrorText, PixelFormatInfo, SizeAndAlign, TextureLevelInfo, TextureLevelQuery, @@ -805,6 +807,27 @@ pub fn fake_identity_caps() -> IdentityCaps { feature_sets, counter_sampling: caps::COUNTER_SAMPLING_POINTS.map(|p| Some(p == 0)), architecture_name: Some(String::from("fake_gpu")), + // 16 for each texture-buffer format; a 4-byte texel's tiles for every format. + formats: caps::SDK_PIXEL_FORMATS + .iter() + .map(|&format| FormatAnswers { + format, + texture_buffer_alignment: caps::TEXTURE_BUFFER_FORMATS + .contains(&format) + .then_some(16), + sparse_tiles: Some([ + [64, 64, 1], + [128, 128, 1], + [256, 256, 1], + [64, 32, 1], + [128, 64, 1], + [256, 128, 1], + [32, 32, 1], + [64, 64, 1], + [128, 128, 1], + ]), + }) + .collect(), sizes: IdentitySizes { recommended_max_working_set_size: Some(0xcf5c_2c000), location: Some(0), diff --git a/crates/reims-vgpu-metal/src/gpu/device.rs b/crates/reims-vgpu-metal/src/gpu/device.rs index 9044734e2..552c4e079 100644 --- a/crates/reims-vgpu-metal/src/gpu/device.rs +++ b/crates/reims-vgpu-metal/src/gpu/device.rs @@ -30,9 +30,10 @@ use objc2_metal::{MTLDevice, MTLGPUFamily, MTLPixelFormat, MTLResourceOptions}; use reims_vgpu_core::num::{ToU64, ToUsize}; use reims_vgpu_core::platform::HostRange; use reims_vgpu_core::platform::caps::{ - COUNTER_SAMPLING_POINTS, DeviceCaps, FAMILIES, FEATURE_SETS, Flags, HEAP_QUERY_OPTIONS, - IdentityCaps, IdentityFlags, IdentitySizes, LARGEST_COUNT_ASKED, LINEAR_ALIGNMENT_FORMAT, - Limits, SAMPLE_COUNTS, SDK_FAMILIES, WRITE_ROUNDING_MODES, + COUNTER_SAMPLING_POINTS, DeviceCaps, FAMILIES, FEATURE_SETS, Flags, FormatAnswers, + HEAP_QUERY_OPTIONS, IdentityCaps, IdentityFlags, IdentitySizes, LARGEST_COUNT_ASKED, + LINEAR_ALIGNMENT_FORMAT, Limits, SAMPLE_COUNTS, SDK_FAMILIES, SDK_PIXEL_FORMATS, + SPARSE_PAGE_SIZES, SPARSE_SAMPLE_COUNTS, TEXTURE_BUFFER_FORMATS, WRITE_ROUNDING_MODES, }; use reims_vgpu_core::platform::device::{FunctionResolver, GpuDevice, NoCopyDeallocator}; use reims_vgpu_core::platform::types::{ @@ -1006,6 +1007,46 @@ fn largest(accepts: impl Fn(u64) -> bool) -> u64 { .unwrap_or(0) } +/// The answers of `format` [`FormatAnswers`] holds: its texture buffers' alignment where Metal +/// answers it ([`TEXTURE_BUFFER_FORMATS`]; it asserts on every other format), and its 2D sparse +/// tiles. +fn format_answers(d: &ProtocolObject, format: u64) -> FormatAnswers { + let pf = MTLPixelFormat(format.to_usize()); + let texture_buffer_alignment = TEXTURE_BUFFER_FORMATS.contains(&format).then(|| { + msg::get("minimumTextureBufferAlignmentForPixelFormat:", || { + d.minimumTextureBufferAlignmentForPixelFormat(pf) + }) + .to_u64() + }); + let mut tiles = [[0; 3]; 9]; + let pairs = SPARSE_SAMPLE_COUNTS + .iter() + .flat_map(|&samples| SPARSE_PAGE_SIZES.iter().map(move |&page| (samples, page))); + for (tile, (samples, page)) in tiles.iter_mut().zip(pairs) { + // `NSInteger` is `i64` on arm64: lossless. + let page = objc2_metal::MTLSparsePageSize(page as isize); + let samples = samples.to_usize(); + // SAFETY: any format, count and page; Metal answers a tile for each of the SDK's. + let size = msg::get( + "sparseTileSizeWithTextureType:pixelFormat:sampleCount:sparsePageSize:", + || unsafe { + d.sparseTileSizeWithTextureType_pixelFormat_sampleCount_sparsePageSize( + objc2_metal::MTLTextureType::Type2D, + pf, + samples, + page, + ) + }, + ); + *tile = [size.width, size.height, size.depth].map(ToU64::to_u64); + } + FormatAnswers { + format, + texture_buffer_alignment, + sparse_tiles: Some(tiles), + } +} + /// Every identity and capability answer of `device` [`IdentityCaps`] holds, each Metal's getter /// or query, sent once. Metal answers each of them, so none is `None`. #[allow(deprecated)] // `supportsFeatureSet:`, `location` and the peer getters, deprecated in the SDK @@ -1048,6 +1089,10 @@ fn read_identity_caps(device: &MetalDevice) -> IdentityCaps { })) }), architecture_name: Some(msg::string_text(&architecture_name)), + formats: SDK_PIXEL_FORMATS + .iter() + .map(|&format| format_answers(d, format)) + .collect(), sizes: IdentitySizes { recommended_max_working_set_size: Some(get!(recommendedMaxWorkingSetSize)), location: Some(get!(location).0.to_u64()), @@ -1612,6 +1657,13 @@ pub(crate) mod tests { identity.architecture_name, Some(d.get().architecture().name().to_string()) ); + // BGRA8Unorm (80): 16-byte texture buffers, and a 4-byte texel's 64x64 tile in 16 KiB. + let bgra8 = identity.formats.iter().find(|f| f.format == 80).unwrap(); + assert_eq!(bgra8.texture_buffer_alignment, Some(16)); + assert_eq!(bgra8.sparse_tiles.unwrap()[0], [64, 64, 1]); + // BC1_RGBA (130), which Metal asserts on, is not asked. + let bc1 = identity.formats.iter().find(|f| f.format == 130).unwrap(); + assert_eq!(bc1.texture_buffer_alignment, None); } /// Garbage is the NSError; a compiled metallib (the suite's one-kernel fixture) is a library diff --git a/crates/reims-vgpu-vulkan/src/gpu/identity.rs b/crates/reims-vgpu-vulkan/src/gpu/identity.rs index 2fba99384..a75e4c1c5 100644 --- a/crates/reims-vgpu-vulkan/src/gpu/identity.rs +++ b/crates/reims-vgpu-vulkan/src/gpu/identity.rs @@ -4,8 +4,11 @@ //! Each answer comes from one of these sources, and is `None` where none gives it: //! - **the runtime's answer**: `name`'s companions in `DeviceProperties` (`hasUnifiedMemory`, //! `maxBufferLength`, the argument-buffer and read-write texture tiers, -//! `supports32BitFloatFiltering`), `sparseTileSizeInBytes`, `supportsFamily:`, -//! `supportsVertexAmplificationCount:` and `supportsRasterizationRateMapWithLayerCount:`; +//! `supports32BitFloatFiltering`), `sparseTileSizeInBytes`, each format's sparse tiles, +//! `supportsFamily:`, `supportsVertexAmplificationCount:` and +//! `supportsRasterizationRateMapWithLayerCount:`, and, for +//! `minimumTextureBufferAlignmentForPixelFormat:`, its +//! `minimumLinearTextureAlignmentForPixelFormat:`, which Metal answers alike (measured); //! - **the runtime's audit of a feature row** of Apple's tables, in the Apple9 column, which lists //! every row read here, as [`crate::gpu::caps`]' SIMD flags are read ([`ROWS`]): a getter is yes //! where the row its feature is is exact on the device; @@ -32,12 +35,16 @@ //! set the heaps' sum), and a query of the runtime's to come. use ash::vk; +use metal2vulkan::metal::TextureType; use metal2vulkan_runtime as rt; use reims_vgpu_core::platform::caps::{ - DeviceCaps, FEATURE_SETS, IdentityCaps, IdentityFlags, IdentitySizes, LARGEST_COUNT_ASKED, - SDK_FAMILIES, + DeviceCaps, FEATURE_SETS, FormatAnswers, IdentityCaps, IdentityFlags, IdentitySizes, + LARGEST_COUNT_ASKED, SDK_FAMILIES, SDK_PIXEL_FORMATS, SPARSE_PAGE_SIZES, SPARSE_SAMPLE_COUNTS, + TEXTURE_BUFFER_FORMATS, }; +use crate::gpu::runtime_pixel_format; + /// The feature rows of Apple's tables the getters are read from, each with its getter's field /// in [`IdentityFlags`], in the order `flags` reads them. const ROWS: [rt::FeatureRow; 9] = [ @@ -158,6 +165,56 @@ fn largest(accepts: impl Fn(u64) -> Result) -> u64 { .unwrap_or(0) } +/// The runtime's page of Metal's raw `MTLSparsePageSize` `page`. +fn sparse_page(page: i64) -> Option { + [ + rt::SparsePageSize::Size16, + rt::SparsePageSize::Size64, + rt::SparsePageSize::Size256, + ] + .into_iter() + .zip(SPARSE_PAGE_SIZES) + .find_map(|(p, raw)| (raw == page).then_some(p)) +} + +/// The answers of `format` [`FormatAnswers`] holds: the runtime's sparse tiles, and, of a format +/// Metal answers it of ([`TEXTURE_BUFFER_FORMATS`]), the alignment of its texture buffers: the +/// runtime's `minimumLinearTextureAlignmentForPixelFormat:`, which Metal answers alike for +/// every format of the SDK (measured, an M3 Max under macOS 27.2). `None` for a format the +/// runtime has no enumerator of. +fn format_answers(device: &rt::Device, format: u64) -> FormatAnswers { + let Some(pf) = runtime_pixel_format(format) else { + return FormatAnswers { + format, + ..FormatAnswers::default() + }; + }; + let texture_buffer_alignment = TEXTURE_BUFFER_FORMATS + .contains(&format) + .then(|| { + device + .properties() + .minimum_linear_texture_alignment_for_pixel_format(pf) + .ok() + }) + .flatten(); + let mut tiles = [[0; 3]; 9]; + let pairs = SPARSE_SAMPLE_COUNTS + .iter() + .flat_map(|&samples| SPARSE_PAGE_SIZES.iter().map(move |&page| (samples, page))); + for (tile, (samples, page)) in tiles.iter_mut().zip(pairs) { + if let Some(page) = sparse_page(page) { + let size = device.sparse_tile_size_for_page(TextureType::Type2D, pf, samples, page); + *tile = [size.width, size.height, size.depth]; + } + } + FormatAnswers { + format, + texture_buffer_alignment, + sparse_tiles: Some(tiles), + } +} + /// The sum of `device`'s device-local memory heaps. fn device_local_bytes(device: &rt::Device) -> u64 { // SAFETY: the instance and physical device are the runtime's, read only, while `device` @@ -194,6 +251,10 @@ pub fn read(device: &rt::Device, caps: &DeviceCaps) -> IdentityCaps { feature_sets: FEATURE_SETS.map(|set| Some(feature_set(set, &families))), counter_sampling: [None; 5], architecture_name: None, + formats: SDK_PIXEL_FORMATS + .iter() + .map(|&format| format_answers(device, format)) + .collect(), sizes: IdentitySizes { recommended_max_working_set_size: Some(device_local_bytes(device)), max_buffer_length: Some(props.max_buffer_length), @@ -305,5 +366,12 @@ mod tests { .is_some_and(|bytes| bytes > 0) ); assert_eq!(identity.flags.is_low_power, None); + // BGRA8Unorm (80): 16-byte texture buffers, and a 4-byte texel's 64x64 tile in 16 KiB, + // as Metal answers on the M3 Max; a format Metal asserts on (BC1_RGBA, 130) has none. + let bgra8 = identity.formats.iter().find(|f| f.format == 80).unwrap(); + assert_eq!(bgra8.texture_buffer_alignment, Some(16)); + assert_eq!(bgra8.sparse_tiles.unwrap()[0], [64, 64, 1]); + let bc1 = identity.formats.iter().find(|f| f.format == 130).unwrap(); + assert_eq!(bc1.texture_buffer_alignment, None); } } diff --git a/crates/reims-vgpu/src/graphics/device_info.rs b/crates/reims-vgpu/src/graphics/device_info.rs index 7cb2f588c..b40a084d9 100644 --- a/crates/reims-vgpu/src/graphics/device_info.rs +++ b/crates/reims-vgpu/src/graphics/device_info.rs @@ -15,11 +15,13 @@ use reims_graphics::wire::{ RGFX_DEVICE_ARCHITECTURE_NAME_LEN, RGFX_DEVICE_INFO_COUNTER_POINTS, RGFX_DEVICE_INFO_FAMILIES, - RGFX_DEVICE_INFO_FEATURE_SETS, RGFX_DEVICE_INFO_IDENTITY_FLAGS, RGFX_DEVICE_INFO_SIZES, - RGFX_DEVICE_INFO_VERSION, RGFX_DEVICE_NAME_LEN, RgfxDeviceInfo, + RGFX_DEVICE_INFO_FEATURE_SETS, RGFX_DEVICE_INFO_FORMATS, RGFX_DEVICE_INFO_IDENTITY_FLAGS, + RGFX_DEVICE_INFO_SIZES, RGFX_DEVICE_INFO_SPARSE_TILES, RGFX_DEVICE_INFO_VERSION, + RGFX_DEVICE_NAME_LEN, RgfxDeviceInfo, RgfxFormatAnswers, }; use reims_vgpu_core::platform::caps::{ - COUNTER_SAMPLING_POINTS, DeviceCaps, FAMILIES, FEATURE_SETS, IdentityCaps, SDK_FAMILIES, + COUNTER_SAMPLING_POINTS, DeviceCaps, FAMILIES, FEATURE_SETS, FormatAnswers, IdentityCaps, + SDK_FAMILIES, SDK_PIXEL_FORMATS, SPARSE_PAGE_SIZES, SPARSE_SAMPLE_COUNTS, }; use reims_vgpu_core::raise::RaiseResult; @@ -48,6 +50,31 @@ fn put_text(out: &mut [u8], text: &str) -> u32 { len as u32 } +/// `answers` as the blob holds them; a value the blob's field cannot hold (no such answer is +/// known) is 0, no answer. +fn format_answers(answers: &FormatAnswers) -> RgfxFormatAnswers { + let mut out = RgfxFormatAnswers { + format: u32::try_from(answers.format).unwrap_or(0), + texture_buffer_alignment: answers + .texture_buffer_alignment + .and_then(|a| u32::try_from(a).ok()) + .unwrap_or(0), + ..RgfxFormatAnswers::default() + }; + if let Some(tiles) = answers.sparse_tiles { + for (out, tile) in out.sparse_tiles.iter_mut().zip(tiles) { + if let (Ok(w), Ok(h), Ok(d)) = ( + u16::try_from(tile[0]), + u16::try_from(tile[1]), + u16::try_from(tile[2]), + ) { + *out = [w, h, d]; + } + } + } + out +} + /// The guest's device's answers where they are not the platform's: what the lane does not /// carry yet. The guest's `MTLDevice` is the lane's own, which can do only what the wire, the /// bundle and the decoder carry, so it answers no to a feature they lack whatever the host GPU @@ -251,6 +278,12 @@ pub fn device_info( ]; info.identity_flags = identity_flags.map(known); info.counter_sampling = identity.counter_sampling.map(known); + let mut format_count = 0; + for (out, answers) in info.formats.iter_mut().zip(&identity.formats) { + *out = format_answers(answers); + format_count += 1; + } + info.format_count = format_count; info } @@ -260,6 +293,9 @@ const _: () = assert!(FEATURE_SETS.len() <= RGFX_DEVICE_INFO_FEATURE_SETS); const _: () = assert!(COUNTER_SAMPLING_POINTS.len() == RGFX_DEVICE_INFO_COUNTER_POINTS); const _: () = assert!(RGFX_DEVICE_NAME_LEN <= 256 && RGFX_DEVICE_ARCHITECTURE_NAME_LEN <= 256); const _: () = assert!(FAMILIES[6] == -1009); +const _: () = assert!(SDK_PIXEL_FORMATS.len() <= RGFX_DEVICE_INFO_FORMATS); +const _: () = + assert!(SPARSE_SAMPLE_COUNTS.len() * SPARSE_PAGE_SIZES.len() == RGFX_DEVICE_INFO_SPARSE_TILES); #[cfg(test)] mod tests { @@ -388,6 +424,14 @@ mod tests { assert_eq!(info.sizes[12], 4); assert_eq!(info.sizes[13], 8); assert_eq!(info.sizes[14], 0); + // Every SDK format, each the platform's: BGRA8Unorm (80) 16-byte texture buffers and + // 64x64 tiles; BC1_RGBA (130) no texture buffers. + assert_eq!(info.format_count as usize, SDK_PIXEL_FORMATS.len()); + let at = |f: u32| info.formats.iter().find(|a| a.format == f).unwrap(); + assert_eq!(at(80).texture_buffer_alignment, 16); + assert_eq!(at(80).sparse_tiles[0], [64, 64, 1]); + assert_eq!(at(80).sparse_tiles[8], [128, 128, 1]); + assert_eq!(at(130).texture_buffer_alignment, 0); } /// A present no reads 0, a present yes reads 1, and a platform gap (an `Err` or `None`) diff --git a/guest/bundle/src/RgfxCaps.cpp b/guest/bundle/src/RgfxCaps.cpp index b04a8ba13..31c4062f2 100644 --- a/guest/bundle/src/RgfxCaps.cpp +++ b/guest/bundle/src/RgfxCaps.cpp @@ -9,6 +9,7 @@ // The enum counts match the blob's arrays: a drift in either is caught here, at // the guest's own compile, beside wire.rs's assertions on the host side. +static_assert(RGFX_DEVICE_INFO_SPARSE_TILES == 9, "3 sample counts at 3 pages"); static_assert(kRgfxSizeCount == RGFX_DEVICE_INFO_SIZES, "size count must match RgfxDeviceInfo::sizes"); static_assert(kRgfxIdentityCount == RGFX_DEVICE_INFO_IDENTITY_FLAGS, @@ -58,6 +59,8 @@ bool RgfxCaps::load(const RgfxDeviceInfo &info) m_info.family_count = RGFX_DEVICE_INFO_FAMILIES; if (m_info.feature_set_count > RGFX_DEVICE_INFO_FEATURE_SETS) m_info.feature_set_count = RGFX_DEVICE_INFO_FEATURE_SETS; + if (m_info.format_count > RGFX_DEVICE_INFO_FORMATS) + m_info.format_count = RGFX_DEVICE_INFO_FORMATS; m_valid = true; return true; } @@ -87,6 +90,42 @@ bool RgfxCaps::supportsCounterSampling(uint64_t point) const return m_info.counter_sampling[point] != 0; } +namespace { +// The blob's answers of `format`, or null where it lists none. +const RgfxFormatAnswers *formatAnswers(const RgfxDeviceInfo &info, uint64_t format) +{ + for (uint32_t k = 0; k < info.format_count; k++) { + if (info.formats[k].format == format) + return &info.formats[k]; + } + return nullptr; +} +} // namespace + +uint64_t RgfxCaps::textureBufferAlignment(uint64_t format) const +{ + const RgfxFormatAnswers *a = formatAnswers(m_info, format); + return a ? a->texture_buffer_alignment : 0; +} + +bool RgfxCaps::sparseTile(uint64_t format, uint64_t samples, int64_t page, uint64_t out[3]) const +{ + out[0] = out[1] = out[2] = 0; + // Sample counts 1, 2 and 4 (major), pages 101, 102 and 103, as the blob holds them. + int s = samples == 1 ? 0 : samples == 2 ? 1 : samples == 4 ? 2 : -1; + int p = page >= 101 && page <= 103 ? static_cast(page - 101) : -1; + const RgfxFormatAnswers *a = formatAnswers(m_info, format); + if (a == nullptr || s < 0 || p < 0) + return false; + const uint16_t *tile = a->sparse_tiles[s * 3 + p]; + if (tile[0] == 0 && tile[1] == 0 && tile[2] == 0) + return false; + out[0] = tile[0]; + out[1] = tile[1]; + out[2] = tile[2]; + return true; +} + uint64_t RgfxCaps::size(RgfxSize which) const { if (which < 0 || which >= kRgfxSizeCount) diff --git a/guest/bundle/src/RgfxCaps.hpp b/guest/bundle/src/RgfxCaps.hpp index a751a842f..3b5c1a2be 100644 --- a/guest/bundle/src/RgfxCaps.hpp +++ b/guest/bundle/src/RgfxCaps.hpp @@ -150,6 +150,14 @@ public: /// `supportsCounterSampling:` of the raw `MTLCounterSamplingPoint` `point` (0..4; any /// other is no). bool supportsCounterSampling(uint64_t point) const; + /// `minimumTextureBufferAlignmentForPixelFormat:` of the raw `MTLPixelFormat` `format`; 0 + /// where the blob has no answer (a format Metal asserts on, among them). + uint64_t textureBufferAlignment(uint64_t format) const; + /// `sparseTileSizeWithTextureType:pixelFormat:sampleCount:sparsePageSize:` of `format` at + /// `samples` (1, 2 or 4) in pages of the raw `MTLSparsePageSize` `page` (101, 102 or 103), + /// the same for every texture type, into `out` (width, height, depth); false, with `out` + /// all 0, where the blob has no answer. + bool sparseTile(uint64_t format, uint64_t samples, int64_t page, uint64_t out[3]) const; /// One of the 15 integer identity answers. uint64_t size(RgfxSize which) const; /// One of the 19 BOOL identity answers. diff --git a/guest/bundle/src/RgfxMetalDevice.mm b/guest/bundle/src/RgfxMetalDevice.mm index 0a713220d..f0c2f8446 100644 --- a/guest/bundle/src/RgfxMetalDevice.mm +++ b/guest/bundle/src/RgfxMetalDevice.mm @@ -568,6 +568,35 @@ void rgfxDeviceDoorbell(void *context, uint32_t ring_id) return _caps.size(kRgfxSizeSparseTileSizeInBytes); } +- (NSUInteger)minimumTextureBufferAlignmentForPixelFormat:(MTLPixelFormat)format +{ + return _caps.textureBufferAlignment(static_cast(format)); +} + +- (MTLSize)sparseTileSizeWithTextureType:(MTLTextureType)textureType + pixelFormat:(MTLPixelFormat)pixelFormat + sampleCount:(NSUInteger)sampleCount + sparsePageSize:(MTLSparsePageSize)sparsePageSize +{ + // The host answers one tile for every texture type, as Metal does. + (void)textureType; + uint64_t tile[3]; + _caps.sparseTile(static_cast(pixelFormat), sampleCount, + static_cast(sparsePageSize), tile); + return MTLSizeMake(tile[0], tile[1], tile[2]); +} + +- (MTLSize)sparseTileSizeWithTextureType:(MTLTextureType)textureType + pixelFormat:(MTLPixelFormat)pixelFormat + sampleCount:(NSUInteger)sampleCount +{ + // Pages of 16 KiB, as Metal's query without a page size. + return [self sparseTileSizeWithTextureType:textureType + pixelFormat:pixelFormat + sampleCount:sampleCount + sparsePageSize:MTLSparsePageSize16]; +} + // --- feature flags (RgfxCaps) ------------------------------------------------ - (BOOL)areProgrammableSamplePositionsSupported diff --git a/guest/bundle/src/RgfxWireLayout.cpp b/guest/bundle/src/RgfxWireLayout.cpp index e8c1b9898..d70b23c21 100644 --- a/guest/bundle/src/RgfxWireLayout.cpp +++ b/guest/bundle/src/RgfxWireLayout.cpp @@ -42,7 +42,9 @@ static_assert(sizeof(RgfxLibraryCreate) == 72, "RgfxLibraryCreate"); static_assert(RGFX_LIBRARY_BYTES_OFFSET == sizeof(RgfxLibraryCreate), "library bytes offset"); static_assert(sizeof(RgfxLibraryFromRegion) == 112, "RgfxLibraryFromRegion"); static_assert(sizeof(RgfxLibraryLookup) == 64, "RgfxLibraryLookup"); -static_assert(sizeof(RgfxDeviceInfo) == 1400, "RgfxDeviceInfo"); +static_assert(sizeof(RgfxFormatAnswers) == 64, "RgfxFormatAnswers"); +static_assert(sizeof(RgfxDeviceInfo) == 1400 + 64 * RGFX_DEVICE_INFO_FORMATS, "RgfxDeviceInfo"); +static_assert(__builtin_offsetof(RgfxDeviceInfo, formats) == 1400, "RgfxDeviceInfo formats"); static_assert(__builtin_offsetof(RgfxDeviceInfo, family_answers) == 944, "RgfxDeviceInfo family_answers"); static_assert(__builtin_offsetof(RgfxDeviceInfo, name) == 1080, "RgfxDeviceInfo name"); diff --git a/guest/bundle/tests/serializer_tests.cpp b/guest/bundle/tests/serializer_tests.cpp index a6de6b509..fbe7c2c40 100644 --- a/guest/bundle/tests/serializer_tests.cpp +++ b/guest/bundle/tests/serializer_tests.cpp @@ -218,6 +218,17 @@ void test_caps() info.identity_flags[kRgfxIdentityIsLowPower] = 1; // first by position info.identity_flags[kRgfxIdentitySupportsPlacementSparse] = 1; // last by position info.counter_sampling[0] = 1; // at stage boundaries + info.format_count = 1; + info.formats[0].format = 80; // BGRA8Unorm + info.formats[0].texture_buffer_alignment = 16; + info.formats[0].sparse_tiles[0][0] = 64; // 1 sample, 16 KiB pages + info.formats[0].sparse_tiles[0][1] = 64; + info.formats[0].sparse_tiles[0][2] = 1; + info.formats[0].sparse_tiles[8][0] = 128; // 4 samples, 256 KiB pages + info.formats[0].sparse_tiles[8][1] = 128; + info.formats[0].sparse_tiles[8][2] = 1; + info.formats[1].format = 70; // past format_count: not read + info.formats[1].texture_buffer_alignment = 16; const char *arch = "test_arch"; info.architecture_name_len = static_cast(std::strlen(arch)); std::memcpy(info.architecture_name, arch, info.architecture_name_len); @@ -259,6 +270,16 @@ void test_caps() CHECK(!caps.supportsCounterSampling(1)); CHECK(!caps.supportsCounterSampling(5)); CHECK(std::strcmp(caps.architectureName(), "test_arch") == 0); + CHECK(caps.textureBufferAlignment(80) == 16); + CHECK(caps.textureBufferAlignment(70) == 0); // listed past the count + CHECK(caps.textureBufferAlignment(130) == 0); // not listed + uint64_t tile[3]; + CHECK(caps.sparseTile(80, 1, 101, tile) && tile[0] == 64 && tile[1] == 64 && tile[2] == 1); + CHECK(caps.sparseTile(80, 4, 103, tile) && tile[0] == 128 && tile[2] == 1); + CHECK(!caps.sparseTile(80, 2, 102, tile) && tile[0] == 0); // a zero tile is no answer + CHECK(!caps.sparseTile(80, 8, 101, tile)); // no such sample count + CHECK(!caps.sparseTile(80, 1, 104, tile)); // no such page + CHECK(!caps.sparseTile(130, 1, 101, tile)); // not listed CHECK(caps.supportsSampleCount(1)); CHECK(caps.supportsSampleCount(4)); CHECK(!caps.supportsSampleCount(2));