diff --git a/crates/reims-vgpu/src/graphics/device_info.rs b/crates/reims-vgpu/src/graphics/device_info.rs index a75b7273b..62e02cf1b 100644 --- a/crates/reims-vgpu/src/graphics/device_info.rs +++ b/crates/reims-vgpu/src/graphics/device_info.rs @@ -20,8 +20,9 @@ use reims_graphics::wire::{ RGFX_DEVICE_NAME_LEN, RgfxDeviceInfo, RgfxFormatAnswers, }; use reims_vgpu_core::platform::caps::{ - COUNTER_SAMPLING_POINTS, DeviceCaps, FAMILIES, FEATURE_SETS, FormatAnswers, IdentityCaps, - SDK_FAMILIES, SDK_PIXEL_FORMATS, SPARSE_PAGE_SIZES, SPARSE_SAMPLE_COUNTS, feature_set_family, + COUNTER_SAMPLING_POINTS, DeviceCaps, FAMILIES, FEATURE_SETS, Flags, FormatAnswers, + IdentityCaps, Limits, SDK_FAMILIES, SDK_PIXEL_FORMATS, SPARSE_PAGE_SIZES, SPARSE_SAMPLE_COUNTS, + feature_set_family, }; use reims_vgpu_core::raise::RaiseResult; @@ -278,6 +279,13 @@ fn cap_apple_families(identity: &mut IdentityCaps, wired: &[Piece]) { /// | `supportsCounterSampling:` | no, at every point | counter sample buffers | /// | `supportsRasterizationRateMapWithLayerCount:` | no, of every count | rasterization rate maps | /// | `maximumConcurrentCompilationTaskCount` | the device's compile threads, which run its pipeline compiles | nothing: the device's own answer | +/// | `maxBufferLength` | the platform's, but at most three quarters of `recommendedMaxWorkingSetSize`, rounded down to 16 KiB ([`lane_max_buffer_length`]) | nothing: a choice | +/// | `minimumLinearTextureAlignmentForPixelFormat:` and the SPI `linearTextureAlignmentBytes` and `deviceLinearTextureAlignmentBytes` | 256 bytes, or the platform's where larger ([`LANE_LINEAR_TEXTURE_ALIGNMENT`]) | nothing: a choice | +/// | `isFramebufferReadSupported` (SPI) | no ([`lane_flags`]) | nothing: a choice | +/// | `supportsTileShaders`, `supportsNonSquareTileShaders`, `supportsImageBlocks` (SPI) | no ([`lane_flags`]) | tile pipelines, their non-square dispatch and imageblocks, Apple4's and Apple5's pieces | +/// +/// The rows marked "a choice" are answers real Macs give, picked from those the lane can keep for +/// what they cost the host (docs/graphics-kext/dgpu-oracle.md, "Reading the oracles"). /// /// `hasUnifiedMemory` is the platform's: the decoder keeps `Managed` resources in one copy or two /// by the same answer (docs/graphics-kext/design.md, Decided 4). @@ -300,9 +308,68 @@ fn lane(mut identity: IdentityCaps, compile_threads: u64) -> IdentityCaps { identity.counter_sampling = [Some(false); 5]; identity.sizes.largest_rasterization_rate_map_layer_count = Some(0); identity.sizes.maximum_concurrent_compilation_task_count = Some(compile_threads); + let z = &mut identity.sizes; + z.max_buffer_length = + lane_max_buffer_length(z.max_buffer_length, z.recommended_max_working_set_size); identity } +/// `maxBufferLength`: the platform's, but at most three quarters of `recommended_max_working_set`, +/// rounded down to 16 KiB; the platform's alone where it gives no working set. That is the rule +/// both Macs measured with GPU memory of their own answer by: an AMD Radeon Pro 5300M (macOS +/// 26.7.1) 3208642560 of 4278190080 bytes, three quarters exactly, and an Apple M3 Max (macOS +/// 27.0.1) 41747087360 of 55662788608, three quarters rounded down to its 16 KiB pages. It keeps the +/// answer to a buffer the host's memory can hold: the Vulkan platform answers the driver's +/// `maxBufferSize` (a terabyte on an RTX 5080), which no allocation reaches, and its working set is +/// the GPU's device-local memory. Never above the platform's answer. +fn lane_max_buffer_length(platform: Option, working_set: Option) -> Option { + const PAGE: u64 = 16 << 10; + match (platform, working_set) { + (Some(length), Some(set)) => Some(length.min(set / 4 * 3 / PAGE * PAGE)), + (length, _) => length, + } +} + +/// `minimumLinearTextureAlignmentForPixelFormat:` of every format a buffer can back, and the SPI +/// `linearTextureAlignmentBytes` and `deviceLinearTextureAlignmentBytes`: 256 bytes, as an AMD +/// Radeon Pro 5300M and an Intel UHD 630 answer (macOS 26.7.1; an Apple M3 Max answers 16), or +/// the platform's where it needs more. A guest lays a texture's rows out in its buffer at a +/// multiple of this alignment. The Vulkan platform on RADV takes a texture over a buffer's +/// dma-buf pages in place only with its rows a multiple of 256 bytes apart (metal2vulkan's +/// `dma_buf.rs`, measured on Mesa 26.0.8 and 26.2.4), and shadows one at any other pitch; how many +/// of a guest's textures this moves in place is unmeasured. Any 256-byte layout is one the +/// platform's 16 takes too. +pub const LANE_LINEAR_TEXTURE_ALIGNMENT: u64 = 256; + +/// The device's `NSUInteger` SPI answers where the lane's differ from the platform's: the linear +/// texture alignments, at least [`LANE_LINEAR_TEXTURE_ALIGNMENT`]. +fn lane_limits(mut limits: Limits) -> Limits { + for align in [ + &mut limits.linear_texture_alignment_bytes, + &mut limits.device_linear_texture_alignment_bytes, + ] { + *align = (*align).max(LANE_LINEAR_TEXTURE_ALIGNMENT); + } + limits +} + +/// The device's `BOOL` SPI answers where the lane's differ from the platform's: +/// - `isFramebufferReadSupported` no, as an AMD Radeon Pro 5300M answers (macOS 26.7.1; an Intel +/// UHD 630 and an Apple M3 Max answer yes): it steers the guest's frameworks off reading their +/// attachments in a fragment function, which costs the Vulkan platform most (an input +/// attachment, or the pixel interlock), onto the paths they take on an AMD Mac; +/// - `supportsTileShaders`, `supportsNonSquareTileShaders` and `supportsImageBlocks` no, as the +/// 5300M and the UHD 630 answer: the lane carries neither tile pipelines, nor their non-square +/// dispatch, nor imageblocks ([`Piece::TilePipelines`], [`Piece::NonsquareTileDispatch`], +/// [`Piece::Imageblocks`]). +fn lane_flags(mut flags: Flags) -> Flags { + flags.is_framebuffer_read_supported = false; + flags.supports_tile_shaders = false; + flags.supports_non_square_tile_shaders = false; + flags.supports_image_blocks = false; + flags +} + /// The device-info caps blob for `caps` and `identity`, of a device that runs `compile_threads` /// pipeline compiles at once: every value the guest bundle reads once at init, the host device's /// answers but where the lane's own differ ([`lane`]). @@ -332,7 +399,7 @@ pub fn device_info( let t = caps.max_threads_per_threadgroup; info.max_threads_per_threadgroup = [t.width, t.height, t.depth]; - let l = &caps.limits; + let l = &lane_limits(caps.limits); // In the host `Limits` field order; the bundle reads them in this order. info.limits = [ l.device_linear_texture_alignment_bytes, @@ -411,7 +478,7 @@ pub fn device_info( info.sample_counts = caps.sample_counts.each_ref().map(bit); info.primitive_types = caps.primitive_types.each_ref().map(bit); info.write_rounding_modes = caps.write_rounding_modes.each_ref().map(bit); - let f = &caps.flags; + let f = &lane_flags(caps.flags); // In the host `Flags` field order; the bundle reads them in this order. info.flags = [ f.are_raster_order_groups_supported, @@ -734,6 +801,61 @@ mod tests { assert_eq!(at(130).texture_buffer_alignment, 0); } + /// `maxBufferLength` is three quarters of the working set rounded down to 16 KiB where that is + /// below the platform's answer, as the 5300M's and the M3 Max's answers are of theirs, and the + /// platform's otherwise; the linear texture alignments are 256 bytes, or the platform's where + /// larger; the framebuffer read, tile shader and imageblock answers are no, whatever the + /// platform's, and the other SPI flags the platform's. + #[test] + fn the_lanes_buffer_length_alignments_and_spi_flags() { + // An RTX 5080 on the Vulkan platform: a terabyte against 17094934528 bytes of VRAM. + assert_eq!( + lane_max_buffer_length(Some(1 << 40), Some(17_094_934_528)), + Some(12_821_200_896) + ); + // The 5300M's and the M3 Max's own answers, from their working sets. + assert_eq!( + lane_max_buffer_length(Some(u64::MAX), Some(4_278_190_080)), + Some(3_208_642_560) + ); + assert_eq!( + lane_max_buffer_length(Some(u64::MAX), Some(55_662_788_608)), + Some(41_747_087_360) + ); + // Never above the platform's; the platform's where it gives no working set. + assert_eq!( + lane_max_buffer_length(Some(4096), Some(1 << 34)), + Some(4096) + ); + assert_eq!(lane_max_buffer_length(Some(4096), None), Some(4096)); + assert_eq!(lane_max_buffer_length(None, Some(1 << 34)), None); + + let p = Arc::new(FakePlatform::new()); + let mut caps = (*p.device().caps()).clone(); + let mut identity = p.device().identity_caps(); + identity.sizes.max_buffer_length = Some(1 << 40); + identity.sizes.recommended_max_working_set_size = Some(17_094_934_528); + // The fake answers 16 for both alignments, and yes for every SPI flag. + let info = device_info(&caps, identity.clone(), 4); + assert_eq!(info.sizes[7], 12_821_200_896); + // device_linear_texture_alignment_bytes first, linear_texture_alignment_bytes fourth. + assert_eq!(info.limits[0], 256); + assert_eq!(info.limits[3], 256); + // is_framebuffer_read_supported (2), supports_image_blocks (14), + // supports_non_square_tile_shaders (17) and supports_tile_shaders (24) no; the others + // the platform's, all yes on the fake but is_depth24_stencil8_pixel_format_supported (1). + for (k, &flag) in info.flags.iter().enumerate() { + let want = !matches!(k, 1 | 2 | 14 | 17 | 24); + assert_eq!(flag, u8::from(want), "flag {k}"); + } + // A platform that needs more than 256 bytes keeps its own. + caps.limits.linear_texture_alignment_bytes = 512; + caps.limits.device_linear_texture_alignment_bytes = 1024; + let info = device_info(&caps, identity, 4); + assert_eq!(info.limits[0], 1024); + assert_eq!(info.limits[3], 512); + } + /// A present no reads 0, a present yes reads 1, and a platform gap (an `Err` or `None`) /// reads 0 — never a spurious yes; a name longer than the blob's is cut on a character #[test] diff --git a/guest/bundle/src/RgfxCaps.cpp b/guest/bundle/src/RgfxCaps.cpp index 0b603e69c..3b694778e 100644 --- a/guest/bundle/src/RgfxCaps.cpp +++ b/guest/bundle/src/RgfxCaps.cpp @@ -120,6 +120,11 @@ uint64_t RgfxCaps::linearTextureAlignment() const return align; } +uint64_t RgfxCaps::minimumLinearTextureAlignment(uint64_t format) const +{ + return textureBufferAlignment(format) != 0 ? limit(kRgfxLimitLinearTextureAlignmentBytes) : 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; diff --git a/guest/bundle/src/RgfxCaps.hpp b/guest/bundle/src/RgfxCaps.hpp index eb0651090..c22808714 100644 --- a/guest/bundle/src/RgfxCaps.hpp +++ b/guest/bundle/src/RgfxCaps.hpp @@ -161,6 +161,11 @@ public: /// `minimumLinearTextureAlignmentForPixelFormat:`(BGRA8Unorm) and every format's /// texture-buffer alignment, the blob's answers; 0 where it has no linear answer. uint64_t linearTextureAlignment() const; + /// `minimumLinearTextureAlignmentForPixelFormat:` of the raw `MTLPixelFormat` `format`: the + /// blob's `linearTextureAlignmentBytes` for every format the host makes textures over a + /// buffer of (one with a texture-buffer answer), as an Intel, AMD or Apple GPU answers one + /// value for every format it makes linear textures of; 0 for any other, which Metal asserts on. + uint64_t minimumLinearTextureAlignment(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` diff --git a/guest/bundle/src/RgfxMetalDevice.mm b/guest/bundle/src/RgfxMetalDevice.mm index 512501b7e..4b49eb19a 100644 --- a/guest/bundle/src/RgfxMetalDevice.mm +++ b/guest/bundle/src/RgfxMetalDevice.mm @@ -734,6 +734,21 @@ RGFX_LOG_UNRECOGNIZED return _caps.textureBufferAlignment(static_cast(format)); } +- (NSUInteger)minimumLinearTextureAlignmentForPixelFormat:(MTLPixelFormat)format +{ + return _caps.minimumLinearTextureAlignment(static_cast(format)); +} + +- (NSUInteger)linearTextureAlignmentBytes +{ + return _caps.limit(kRgfxLimitLinearTextureAlignmentBytes); +} + +- (NSUInteger)deviceLinearTextureAlignmentBytes +{ + return _caps.limit(kRgfxLimitDeviceLinearTextureAlignmentBytes); +} + - (MTLSize)sparseTileSizeWithTextureType:(MTLTextureType)textureType pixelFormat:(MTLPixelFormat)pixelFormat sampleCount:(NSUInteger)sampleCount @@ -775,6 +790,26 @@ RGFX_LOG_UNRECOGNIZED return _caps.valid() && _caps.flag(kRgfxFlagIsDepth24Stencil8PixelFormatSupported); } +- (BOOL)isFramebufferReadSupported +{ + return _caps.valid() && _caps.flag(kRgfxFlagIsFramebufferReadSupported); +} + +- (BOOL)supportsTileShaders +{ + return _caps.valid() && _caps.flag(kRgfxFlagSupportsTileShaders); +} + +- (BOOL)supportsNonSquareTileShaders +{ + return _caps.valid() && _caps.flag(kRgfxFlagSupportsNonSquareTileShaders); +} + +- (BOOL)supportsImageBlocks +{ + return _caps.valid() && _caps.flag(kRgfxFlagSupportsImageBlocks); +} + - (BOOL)supports32BitFloatFiltering { return _caps.identityFlag(kRgfxIdentitySupports32BitFloatFiltering); diff --git a/guest/bundle/src/RgfxMetalPrivate.h b/guest/bundle/src/RgfxMetalPrivate.h index 37dd46dea..cdccc2339 100644 --- a/guest/bundle/src/RgfxMetalPrivate.h +++ b/guest/bundle/src/RgfxMetalPrivate.h @@ -104,6 +104,15 @@ NS_ASSUME_NONNULL_END - (NSUInteger)maxComputeTextures; - (NSUInteger)maxVertexAmplificationCount; - (BOOL)supportsPrimitiveType:(NSUInteger)primitiveType; +// measured: macOS 27 Metal.framework's x86-64 slice, _MTLDevice's, `Q16@0:8` and `c16@0:8`: +// the linear texture alignments, and the framebuffer read, tile shader and imageblock answers +// the host's device-info blob carries. +- (NSUInteger)linearTextureAlignmentBytes; +- (NSUInteger)deviceLinearTextureAlignmentBytes; +- (BOOL)isFramebufferReadSupported; +- (BOOL)supportsTileShaders; +- (BOOL)supportsNonSquareTileShaders; +- (BOOL)supportsImageBlocks; // measured: macOS 26 Metal.framework, required, `@32@0:8Q16Q24`: a texture over a legacy // IOAccel surface, by its id and buffer (CoreDisplay's display surfaces). - (nullable id)newTextureWithSurface:(NSUInteger)surfaceId buffer:(NSUInteger)buffer; diff --git a/guest/bundle/tests/serializer_tests.cpp b/guest/bundle/tests/serializer_tests.cpp index c69988a01..28f5cae9a 100644 --- a/guest/bundle/tests/serializer_tests.cpp +++ b/guest/bundle/tests/serializer_tests.cpp @@ -333,6 +333,7 @@ void test_caps() info.primitive_types[3] = 1; info.write_rounding_modes[1] = 1; // mode 2 info.limits[kRgfxLimitDeviceLinearTextureAlignmentBytes] = 64; + info.limits[kRgfxLimitLinearTextureAlignmentBytes] = 256; info.limits[kRgfxLimitMaxViewportCount] = 16; // last by position info.flags[kRgfxFlagAreRasterOrderGroupsSupported] = 1; // first by position info.flags[kRgfxFlagSupportsTileShaders] = 1; // last by position @@ -369,6 +370,10 @@ void test_caps() CHECK(caps.textureBufferAlignment(80) == 16); CHECK(caps.textureBufferAlignment(70) == 0); // listed past the count CHECK(caps.textureBufferAlignment(130) == 0); // not listed + // The linear alignment is the one limit for every format with a texture-buffer answer. + CHECK(caps.minimumLinearTextureAlignment(80) == 256); + CHECK(caps.minimumLinearTextureAlignment(70) == 0); // listed past the count + CHECK(caps.minimumLinearTextureAlignment(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);