Time a replayed page whose commands each bind their own argument buffer and texture

The host-replayed step a consumer measured costs about 14 s a frame on a page of 5001
GPU-written commands, each drawing with a texture of its own, under a residency of 15018
buffers and 5001 textures; replayed_frames' page binds no buffer per command and keeps one
kind of resident. The owned fixture icb_vertex_encode_textured has each command set its
fragment buffer to an argument buffer of its own (through a slot table of addresses), whose
entry names its texture, and replayed_textured_frames times that page's frames as
replayed_frames does, with REPLAYED_TEXTURED_DRAWS and REPLAYED_TEXTURED_BUFFERS setting the
counts. replayed_frames' timing loop is now time_frames, which both use.

Ignored measurement; not run yet (built and run on the M3 next).

(cherry picked from commit 71890e856e34894733ef68fe61526a2ea2ffd177)
This commit is contained in:
Anees Iqbal committed 2026-11-05 02:08:24 -08:00
1 parent 44ae958d95
commit 3550112c29
7 files changed
+650 -23

No files matched your search

@@ -165,6 +165,7 @@ fixture icb_routed_writes: metal: Apple metal version 32023.922 (metalfe-32023.9
fixture icb_stage_in_encode: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture icb_stage_in_template: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture icb_vertex_encode_indexed: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture icb_vertex_encode_textured: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture ido_draw_order: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture kernel_bit_manipulation: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture kernel_frexp_ilogb: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
@@ -0,0 +1,157 @@
; Generated by scripts/regen-air-fixtures from icb_vertex_encode_textured.metal (typed pointers; xcrun metal-as reassembles it). Do not edit.
; ModuleID = '-'
source_filename = "-"
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32"
target triple = "air64_v28-apple-macosx26.0.0"
%struct.Encoder = type { %"struct.metal::command_buffer" }
%"struct.metal::command_buffer" = type { %struct._command_buffer_t addrspace(1)* }
%struct._command_buffer_t = type opaque
%struct.Slot = type { %struct.Entry addrspace(1)* }
%struct.Entry = type { %"struct.metal::texture2d" }
%"struct.metal::texture2d" = type { %struct._texture_2d_t addrspace(1)* }
%struct._texture_2d_t = type opaque
%struct._sampler_t = type opaque
; Function Attrs: mustprogress nounwind willreturn
define void @ivt_encode(%struct.Encoder addrspace(1)* nocapture noundef readonly align 8 dereferenceable(8) "air-buffer-no-alias" %0, i16 addrspace(1)* noundef "air-buffer-no-alias" %1, %struct.Slot addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %2, i32 noundef %3) local_unnamed_addr #0 {
%5 = getelementptr inbounds %struct.Encoder, %struct.Encoder addrspace(1)* %0, i64 0, i32 0, i32 0
%6 = load %struct._command_buffer_t addrspace(1)*, %struct._command_buffer_t addrspace(1)* addrspace(1)* %5, align 8, !tbaa.struct !39, !alias.scope !44, !noalias !47
%7 = zext i32 %3 to i64
%8 = getelementptr inbounds %struct.Slot, %struct.Slot addrspace(1)* %2, i64 %7, i32 0
%9 = bitcast %struct.Entry addrspace(1)* addrspace(1)* %8 to i8 addrspace(1)* addrspace(1)*
%10 = load i8 addrspace(1)*, i8 addrspace(1)* addrspace(1)* %9, align 8, !tbaa !50, !alias.scope !53, !noalias !54
tail call void @air.set_fragment_buffer_render_command.p1i8(%struct._command_buffer_t addrspace(1)* nocapture %6, i32 %3, i8 addrspace(1)* %10, i32 0) #7
%11 = urem i32 %3, 5
%12 = and i32 %3, 63
%13 = mul nuw nsw i32 %12, 6
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i16, i16 addrspace(1)* %1, i64 %14
tail call void @air.draw_indexed_primitives_render_command.p1i16(%struct._command_buffer_t addrspace(1)* nocapture %6, i32 %3, i32 %11, i32 6, i16 addrspace(1)* %15, i32 1, i32 0, i32 0) #7
ret void
}
; Function Attrs: mustprogress nofree nosync nounwind readnone willreturn
define <{ <4 x float>, float }> @ivt_vertex(i32 noundef %0) local_unnamed_addr #1 {
%2 = and i32 %0, 7
%3 = tail call fast float @air.convert.f.f32.u.i32(i32 %2) #8
%4 = fmul fast float %3, 2.500000e-01
%5 = fadd fast float %4, -8.750000e-01
%6 = lshr i32 %0, 3
%7 = and i32 %6, 7
%8 = tail call fast float @air.convert.f.f32.u.i32(i32 %7) #8
%9 = fmul fast float %8, 2.500000e-01
%10 = fadd fast float %9, -8.750000e-01
%11 = insertelement <4 x float> <float poison, float poison, float 0.000000e+00, float 1.000000e+00>, float %5, i64 0
%12 = insertelement <4 x float> %11, float %10, i64 1
%13 = insertvalue <{ <4 x float>, float }> undef, <4 x float> %12, 0
%14 = insertvalue <{ <4 x float>, float }> %13, float 1.000000e+00, 1
ret <{ <4 x float>, float }> %14
}
; Function Attrs: mustprogress nofree nosync nounwind readnone willreturn
declare float @air.convert.f.f32.u.i32(i32) local_unnamed_addr #2
; Function Attrs: mustprogress nofree nounwind readonly willreturn
define <4 x half> @ivt_fragment(%struct.Entry addrspace(1)* nocapture noundef readonly align 8 dereferenceable(8) "air-buffer-no-alias" %0) local_unnamed_addr #3 {
%2 = getelementptr inbounds %struct.Entry, %struct.Entry addrspace(1)* %0, i64 0, i32 0, i32 0
%3 = load %struct._texture_2d_t addrspace(1)*, %struct._texture_2d_t addrspace(1)* addrspace(1)* %2, align 8, !tbaa !55, !alias.scope !58
%4 = tail call %struct._sampler_t addrspace(2)* @air.get_read_sampler() #9
%5 = tail call { <4 x half>, i8 } @air.read_texture_2d.v4f16(%struct._texture_2d_t addrspace(1)* nocapture readonly %3, %struct._sampler_t addrspace(2)* %4, <2 x i32> zeroinitializer, <2 x i32> zeroinitializer, i32 0, i32 0) #10
%6 = extractvalue { <4 x half>, i8 } %5, 0
ret <4 x half> %6
}
; Function Attrs: mustprogress nounwind willreturn
declare void @air.set_fragment_buffer_render_command.p1i8(%struct._command_buffer_t addrspace(1)* nocapture, i32, i8 addrspace(1)*, i32) local_unnamed_addr #4
; Function Attrs: mustprogress nounwind willreturn
declare void @air.draw_indexed_primitives_render_command.p1i16(%struct._command_buffer_t addrspace(1)* nocapture, i32, i32, i32, i16 addrspace(1)*, i32, i32, i32) local_unnamed_addr #4
; Function Attrs: inaccessiblememonly mustprogress nofree nounwind readonly willreturn
declare %struct._sampler_t addrspace(2)* @air.get_read_sampler() local_unnamed_addr #5
; Function Attrs: argmemonly mustprogress nofree nounwind readonly willreturn
declare { <4 x half>, i8 } @air.read_texture_2d.v4f16(%struct._texture_2d_t addrspace(1)* nocapture readonly, %struct._sampler_t addrspace(2)*, <2 x i32>, <2 x i32>, i32, i32) local_unnamed_addr #6
attributes #0 = { mustprogress nounwind willreturn "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #1 = { mustprogress nofree nosync nounwind readnone willreturn "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #2 = { mustprogress nofree nosync nounwind readnone willreturn }
attributes #3 = { mustprogress nofree nounwind readonly willreturn "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="64" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #4 = { mustprogress nounwind willreturn }
attributes #5 = { inaccessiblememonly mustprogress nofree nounwind readonly willreturn }
attributes #6 = { argmemonly mustprogress nofree nounwind readonly willreturn }
attributes #7 = { nounwind willreturn }
attributes #8 = { nounwind readnone willreturn }
attributes #9 = { inaccessiblememonly nounwind readonly willreturn }
attributes #10 = { argmemonly nounwind readonly willreturn }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!air.vertex = !{!9, !22}
!air.fragment = !{!28}
!air.compile_options = !{!33, !34, !35}
!llvm.ident = !{!36}
!air.version = !{!37}
!air.language_version = !{!38}
!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]}
!1 = !{i32 1, !"wchar_size", i32 4}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{i32 7, !"air.max_device_buffers", i32 31}
!4 = !{i32 7, !"air.max_constant_buffers", i32 31}
!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31}
!6 = !{i32 7, !"air.max_textures", i32 128}
!7 = !{i32 7, !"air.max_read_write_textures", i32 8}
!8 = !{i32 7, !"air.max_samplers", i32 16}
!9 = !{void (%struct.Encoder addrspace(1)*, i16 addrspace(1)*, %struct.Slot addrspace(1)*, i32)* @ivt_encode, !10, !11}
!10 = !{}
!11 = !{!12, !15, !16, !21}
!12 = !{i32 0, !"air.indirect_buffer", !"air.buffer_size", i32 8, !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.struct_type_info", !13, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Encoder", !"air.arg_name", !"encoder"}
!13 = !{i32 0, i32 8, i32 0, !"command_buffer", !"commands", !"air.indirect_argument", !14}
!14 = !{i32 0, !"air.command_buffer", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"command_buffer", !"air.arg_name", !"commands"}
!15 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 2, !"air.arg_type_align_size", i32 2, !"air.arg_type_name", !"ushort", !"air.arg_name", !"indices"}
!16 = !{i32 2, !"air.indirect_buffer", !"air.location_index", i32 2, i32 1, !"air.read", !"air.address_space", i32 1, !"air.struct_type_info", !17, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Slot", !"air.arg_name", !"slots"}
!17 = !{i32 0, i32 8, i32 0, !"Entry", !"entry", !"air.indirect_argument", !18}
!18 = !{i32 0, !"air.indirect_buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.struct_type_info", !19, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Entry", !"air.arg_name", !"entry"}
!19 = !{i32 0, i32 8, i32 0, !"texture2d<half, sample>", !"texture", !"air.indirect_argument", !20}
!20 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d<half, sample>", !"air.arg_name", !"texture"}
!21 = !{i32 3, !"air.instance_id", !"air.arg_type_name", !"uint", !"air.arg_name", !"iid"}
!22 = !{<{ <4 x float>, float }> (i32)* @ivt_vertex, !23, !26}
!23 = !{!24, !25}
!24 = !{!"air.position", !"air.arg_type_name", !"float4", !"air.arg_name", !"position"}
!25 = !{!"air.point_size", !"air.arg_type_name", !"float", !"air.arg_name", !"size"}
!26 = !{!27}
!27 = !{i32 0, !"air.vertex_id", !"air.arg_type_name", !"uint", !"air.arg_name", !"vid"}
!28 = !{<4 x half> (%struct.Entry addrspace(1)*)* @ivt_fragment, !29, !31}
!29 = !{!30}
!30 = !{!"air.render_target", i32 0, i32 0, !"air.arg_type_name", !"half4"}
!31 = !{!32}
!32 = !{i32 0, !"air.indirect_buffer", !"air.buffer_size", i32 8, !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.struct_type_info", !19, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Entry", !"air.arg_name", !"entry"}
!33 = !{!"air.compile.denorms_disable"}
!34 = !{!"air.compile.fast_math_enable"}
!35 = !{!"air.compile.framebuffer_fetch_enable"}
!36 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"}
!37 = !{i32 2, i32 8, i32 0}
!38 = !{!"Metal", i32 4, i32 0, i32 0}
!39 = !{i64 0, i64 8, !40}
!40 = !{!41, !41, i64 0}
!41 = !{!"__metal_command_buffer_t", !42, i64 0}
!42 = !{!"omnipotent char", !43, i64 0}
!43 = !{!"Simple C++ TBAA"}
!44 = !{!45}
!45 = distinct !{!45, !46, !"air-alias-scope-arg(0)"}
!46 = distinct !{!46, !"air-alias-scopes(ivt_encode)"}
!47 = !{!48, !49}
!48 = distinct !{!48, !46, !"air-alias-scope-arg(1)"}
!49 = distinct !{!49, !46, !"air-alias-scope-arg(2)"}
!50 = !{!51, !52, i64 0}
!51 = !{!"_ZTS4Slot", !52, i64 0}
!52 = !{!"any pointer", !42, i64 0}
!53 = !{!49}
!54 = !{!45, !48}
!55 = !{!56, !57, i64 0}
!56 = !{!"_ZTSN5metal9texture2dIDhLNS_6accessE0ELNS_16memory_coherenceE0EvEE", !57, i64 0}
!57 = !{!"__metal_texture_2d_t", !42, i64 0}
!58 = !{!59}
!59 = distinct !{!59, !60, !"air-alias-scope-arg(0)"}
!60 = distinct !{!60, !"air-alias-scopes(ivt_fragment)"}
@@ -0,0 +1,154 @@
; Generated by scripts/regen-air-fixtures from icb_vertex_encode_textured.metal (llvm-dis of icb_vertex_encode_textured.air). Do not edit.
; ModuleID = '<stdin>'
source_filename = "-"
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32"
target triple = "air64_v28-apple-macosx26.0.0"
%struct.Encoder = type { %"struct.metal::command_buffer" }
%"struct.metal::command_buffer" = type { ptr addrspace(1) }
%struct.Slot = type { ptr addrspace(1) }
%struct.Entry = type { %"struct.metal::texture2d" }
%"struct.metal::texture2d" = type { ptr addrspace(1) }
; Function Attrs: mustprogress nounwind willreturn
define void @ivt_encode(ptr addrspace(1) noundef readonly align 8 captures(none) dereferenceable(8) "air-buffer-no-alias" %0, ptr addrspace(1) noundef "air-buffer-no-alias" %1, ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %2, i32 noundef %3) local_unnamed_addr #0 {
%5 = getelementptr inbounds %struct.Encoder, ptr addrspace(1) %0, i64 0, i32 0, i32 0
%6 = load ptr addrspace(1), ptr addrspace(1) %5, align 8, !tbaa.struct !39, !alias.scope !44, !noalias !47
%7 = zext i32 %3 to i64
%8 = getelementptr inbounds %struct.Slot, ptr addrspace(1) %2, i64 %7, i32 0
%9 = bitcast ptr addrspace(1) %8 to ptr addrspace(1)
%10 = load ptr addrspace(1), ptr addrspace(1) %9, align 8, !tbaa !50, !alias.scope !53, !noalias !54
tail call void @air.set_fragment_buffer_render_command.p1i8(ptr addrspace(1) captures(none) %6, i32 %3, ptr addrspace(1) %10, i32 0) #7
%11 = urem i32 %3, 5
%12 = and i32 %3, 63
%13 = mul nuw nsw i32 %12, 6
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i16, ptr addrspace(1) %1, i64 %14
tail call void @air.draw_indexed_primitives_render_command.p1i16(ptr addrspace(1) captures(none) %6, i32 %3, i32 %11, i32 6, ptr addrspace(1) %15, i32 1, i32 0, i32 0) #7
ret void
}
; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
define <{ <4 x float>, float }> @ivt_vertex(i32 noundef %0) local_unnamed_addr #1 {
%2 = and i32 %0, 7
%3 = tail call fast float @air.convert.f.f32.u.i32(i32 %2) #8
%4 = fmul fast float %3, 2.500000e-01
%5 = fadd fast float %4, -8.750000e-01
%6 = lshr i32 %0, 3
%7 = and i32 %6, 7
%8 = tail call fast float @air.convert.f.f32.u.i32(i32 %7) #8
%9 = fmul fast float %8, 2.500000e-01
%10 = fadd fast float %9, -8.750000e-01
%11 = insertelement <4 x float> <float poison, float poison, float 0.000000e+00, float 1.000000e+00>, float %5, i64 0
%12 = insertelement <4 x float> %11, float %10, i64 1
%13 = insertvalue <{ <4 x float>, float }> undef, <4 x float> %12, 0
%14 = insertvalue <{ <4 x float>, float }> %13, float 1.000000e+00, 1
ret <{ <4 x float>, float }> %14
}
; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare float @air.convert.f.f32.u.i32(i32) local_unnamed_addr #2
; Function Attrs: mustprogress nofree nounwind willreturn memory(read)
define <4 x half> @ivt_fragment(ptr addrspace(1) noundef readonly align 8 captures(none) dereferenceable(8) "air-buffer-no-alias" %0) local_unnamed_addr #3 {
%2 = getelementptr inbounds %struct.Entry, ptr addrspace(1) %0, i64 0, i32 0, i32 0
%3 = load ptr addrspace(1), ptr addrspace(1) %2, align 8, !tbaa !55, !alias.scope !58
%4 = tail call ptr addrspace(2) @air.get_read_sampler() #9
%5 = tail call { <4 x half>, i8 } @air.read_texture_2d.v4f16(ptr addrspace(1) readonly captures(none) %3, ptr addrspace(2) %4, <2 x i32> zeroinitializer, <2 x i32> zeroinitializer, i32 0, i32 0) #10
%6 = extractvalue { <4 x half>, i8 } %5, 0
ret <4 x half> %6
}
; Function Attrs: mustprogress nounwind willreturn
declare void @air.set_fragment_buffer_render_command.p1i8(ptr addrspace(1) captures(none), i32, ptr addrspace(1), i32) local_unnamed_addr #4
; Function Attrs: mustprogress nounwind willreturn
declare void @air.draw_indexed_primitives_render_command.p1i16(ptr addrspace(1) captures(none), i32, i32, i32, ptr addrspace(1), i32, i32, i32) local_unnamed_addr #4
; Function Attrs: mustprogress nofree nounwind willreturn memory(inaccessiblemem: read)
declare ptr addrspace(2) @air.get_read_sampler() local_unnamed_addr #5
; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare { <4 x half>, i8 } @air.read_texture_2d.v4f16(ptr addrspace(1) readonly captures(none), ptr addrspace(2), <2 x i32>, <2 x i32>, i32, i32) local_unnamed_addr #6
attributes #0 = { mustprogress nounwind willreturn "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #1 = { mustprogress nofree nosync nounwind willreturn memory(none) "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #2 = { mustprogress nofree nosync nounwind willreturn memory(none) }
attributes #3 = { mustprogress nofree nounwind willreturn memory(read) "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="64" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #4 = { mustprogress nounwind willreturn }
attributes #5 = { mustprogress nofree nounwind willreturn memory(inaccessiblemem: read) }
attributes #6 = { mustprogress nofree nounwind willreturn memory(argmem: read) }
attributes #7 = { nounwind willreturn }
attributes #8 = { nounwind willreturn memory(none) }
attributes #9 = { nounwind willreturn memory(inaccessiblemem: read) }
attributes #10 = { nounwind willreturn memory(argmem: read) }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!air.vertex = !{!9, !22}
!air.fragment = !{!28}
!air.compile_options = !{!33, !34, !35}
!llvm.ident = !{!36}
!air.version = !{!37}
!air.language_version = !{!38}
!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]}
!1 = !{i32 1, !"wchar_size", i32 4}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{i32 7, !"air.max_device_buffers", i32 31}
!4 = !{i32 7, !"air.max_constant_buffers", i32 31}
!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31}
!6 = !{i32 7, !"air.max_textures", i32 128}
!7 = !{i32 7, !"air.max_read_write_textures", i32 8}
!8 = !{i32 7, !"air.max_samplers", i32 16}
!9 = !{ptr @ivt_encode, !10, !11}
!10 = !{}
!11 = !{!12, !15, !16, !21}
!12 = !{i32 0, !"air.indirect_buffer", !"air.buffer_size", i32 8, !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.struct_type_info", !13, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Encoder", !"air.arg_name", !"encoder"}
!13 = !{i32 0, i32 8, i32 0, !"command_buffer", !"commands", !"air.indirect_argument", !14}
!14 = !{i32 0, !"air.command_buffer", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"command_buffer", !"air.arg_name", !"commands"}
!15 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 2, !"air.arg_type_align_size", i32 2, !"air.arg_type_name", !"ushort", !"air.arg_name", !"indices"}
!16 = !{i32 2, !"air.indirect_buffer", !"air.location_index", i32 2, i32 1, !"air.read", !"air.address_space", i32 1, !"air.struct_type_info", !17, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Slot", !"air.arg_name", !"slots"}
!17 = !{i32 0, i32 8, i32 0, !"Entry", !"entry", !"air.indirect_argument", !18}
!18 = !{i32 0, !"air.indirect_buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.struct_type_info", !19, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Entry", !"air.arg_name", !"entry"}
!19 = !{i32 0, i32 8, i32 0, !"texture2d<half, sample>", !"texture", !"air.indirect_argument", !20}
!20 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d<half, sample>", !"air.arg_name", !"texture"}
!21 = !{i32 3, !"air.instance_id", !"air.arg_type_name", !"uint", !"air.arg_name", !"iid"}
!22 = !{ptr @ivt_vertex, !23, !26}
!23 = !{!24, !25}
!24 = !{!"air.position", !"air.arg_type_name", !"float4", !"air.arg_name", !"position"}
!25 = !{!"air.point_size", !"air.arg_type_name", !"float", !"air.arg_name", !"size"}
!26 = !{!27}
!27 = !{i32 0, !"air.vertex_id", !"air.arg_type_name", !"uint", !"air.arg_name", !"vid"}
!28 = !{ptr @ivt_fragment, !29, !31}
!29 = !{!30}
!30 = !{!"air.render_target", i32 0, i32 0, !"air.arg_type_name", !"half4"}
!31 = !{!32}
!32 = !{i32 0, !"air.indirect_buffer", !"air.buffer_size", i32 8, !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.struct_type_info", !19, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Entry", !"air.arg_name", !"entry"}
!33 = !{!"air.compile.denorms_disable"}
!34 = !{!"air.compile.fast_math_enable"}
!35 = !{!"air.compile.framebuffer_fetch_enable"}
!36 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"}
!37 = !{i32 2, i32 8, i32 0}
!38 = !{!"Metal", i32 4, i32 0, i32 0}
!39 = !{i64 0, i64 8, !40}
!40 = !{!41, !41, i64 0}
!41 = !{!"__metal_command_buffer_t", !42, i64 0}
!42 = !{!"omnipotent char", !43, i64 0}
!43 = !{!"Simple C++ TBAA"}
!44 = !{!45}
!45 = distinct !{!45, !46, !"air-alias-scope-arg(0)"}
!46 = distinct !{!46, !"air-alias-scopes(ivt_encode)"}
!47 = !{!48, !49}
!48 = distinct !{!48, !46, !"air-alias-scope-arg(1)"}
!49 = distinct !{!49, !46, !"air-alias-scope-arg(2)"}
!50 = !{!51, !52, i64 0}
!51 = !{!"_ZTS4Slot", !52, i64 0}
!52 = !{!"any pointer", !42, i64 0}
!53 = !{!49}
!54 = !{!45, !48}
!55 = !{!56, !57, i64 0}
!56 = !{!"_ZTSN5metal9texture2dIDhLNS_6accessE0ELNS_16memory_coherenceE0EvEE", !57, i64 0}
!57 = !{!"__metal_texture_2d_t", !42, i64 0}
!58 = !{!59}
!59 = distinct !{!59, !60, !"air-alias-scope-arg(0)"}
!60 = distinct !{!60, !"air-alias-scopes(ivt_fragment)"}
@@ -0,0 +1,55 @@
// Owned fixture: as icb_vertex_encode_indexed, a vertex function whose only effect is the indexed
// draw it encodes, as a pass with rasterization off encodes one a draw. The draw of instance
// `iid` (its base instance) writes command `iid`: a draw of 6 indices from the 16-bit `indices`,
// from index 6 * (iid % 64), once, of primitive type iid % 5 (point, line, line strip, triangle,
// triangle strip), at base vertex 0, whose fragment buffer 0 is the buffer slot `iid` of `slots`
// points at: an argument buffer whose entry names a texture, one buffer and one texture a command
// as a page's bind groups are. The commands inherit the executing encoder's pipeline state, and
// set their buffers themselves. `ivt_vertex` places vertex `vid` on a grid of 8 by 8 points, one
// pixel wide, and `ivt_fragment` colours it with texel (0, 0) of its command's entry's texture.
// Written for this repository; no third-party source. Loop-free.
#include <metal_stdlib>
using namespace metal;
struct Encoder {
command_buffer commands;
};
struct Entry {
texture2d<half> texture;
};
struct Slot {
device Entry *entry;
};
vertex void ivt_encode(device const Encoder &encoder [[buffer(0)]],
const device ushort *indices [[buffer(1)]],
device const Slot *slots [[buffer(2)]],
uint iid [[instance_id]])
{
render_command command(encoder.commands, iid);
command.set_fragment_buffer(slots[iid].entry, 0);
command.draw_indexed_primitives(static_cast<primitive_type>(iid % 5), 6,
indices + 6 * (iid % 64), 1, 0, 0);
}
struct GridPoint {
float4 position [[position]];
float size [[point_size]];
};
vertex GridPoint ivt_vertex(uint vid [[vertex_id]])
{
GridPoint point;
float x = float(vid % 8) / 4.0 - 0.875;
float y = float(vid / 8 % 8) / 4.0 - 0.875;
point.position = float4(x, y, 0.0, 1.0);
point.size = 1.0;
return point;
}
fragment half4 ivt_fragment(device const Entry &entry [[buffer(0)]])
{
return entry.texture.read(uint2(0, 0));
}
@@ -747,6 +747,8 @@ mod renders;
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
mod replayed_frames;
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
mod replayed_textured;
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
mod resident_draws;
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
mod resolve_rounding;
@@ -83,7 +83,7 @@ const DRAWS: u32 = 5000;
const CHECKED_DRAWS: u32 = 256;
/// The target's width and height.
const SIDE: u64 = 64;
pub(super) const SIDE: u64 = 64;
/// The most bind shapes a warm frame makes or finds among those its drawing states settled: a
/// few for each pass (the encoding pass's one state, the execution's five primitive types), not
@@ -138,7 +138,7 @@ fn library(device: &Device) -> Library {
}
/// `name` of `library`, as a stage's source.
fn function(library: &Library, name: &str) -> StageSource {
pub(super) fn function(library: &Library, name: &str) -> StageSource {
let function = library.new_function_with_name(name).unwrap();
StageSource::Function(function.function_descriptor())
}
@@ -411,14 +411,50 @@ fn shapes(before: &WorkCounts, after: &WorkCounts) -> u64 {
fn replayed_frames() {
let cache = ScratchCache::new().unwrap();
let messages = Messages::default();
let device = validated_device_with(TEST_DEVICE, &messages, cache.path(), |options| {
let device = measured_device(&messages, &cache);
let page = Page::new(&device, DRAWS);
let changed = Inputs {
indices: Some((&page.indices, ZEROS_AT)),
..Inputs::default()
};
let change_at = change_at();
let each = std::env::var_os(EACH_VAR).is_some();
let frame = |queue: &CommandQueue, frame: usize| {
let changes = change_at.filter(|&at| frame >= at);
let inputs = match changes.is_some_and(|at| !each || (frame - at) % 2 == 0) {
true => &changed,
false => &Inputs::default(),
};
page.frame_with(queue, inputs)
};
let pixels = || page.pixels();
time_frames(&device, &page.surface, Some(WARM_SHAPES), frame, pixels);
}
/// A device as a consumer's: without the validation layer, keeping no execution record.
pub(super) fn measured_device(messages: &Messages, cache: &ScratchCache) -> Device {
validated_device_with(TEST_DEVICE, messages, cache.path(), |options| {
options.layers.clear();
// A consumer's device keeps no execution record.
options.record_executions = false;
})
.unwrap();
.unwrap()
}
/// Times [`frames`] frames on `device`, each the command buffer `frame` commits on a queue of its
/// own (given the frame's index), waited for, while a presenter queue commits a blit that copies
/// out `surface` and a bystander queue one that fills a buffer of its own, each one at a time,
/// again as each completes, until the frame completes. Prints a `REPLAYED-FRAMES` line for each
/// (the module docs), `pixels`' hash after it last; each warm frame makes or finds at most
/// `warm_shapes` bind shapes, where set.
pub(super) fn time_frames(
device: &Device,
surface: &Texture,
warm_shapes: Option<u64>,
frame: impl Fn(&CommandQueue, usize) -> CommandBuffer,
pixels: impl Fn() -> Vec<u8>,
) {
let name = device.name().to_owned();
let page = Page::new(&device, DRAWS);
let (page_queue, presenter, bystander) = (
device.new_command_queue().unwrap(),
device.new_command_queue().unwrap(),
@@ -430,18 +466,7 @@ fn replayed_frames() {
let own = device
.new_buffer(1 << 16, ResourceOptions::shared())
.unwrap();
let changed = Inputs {
indices: Some((&page.indices, ZEROS_AT)),
..Inputs::default()
};
let change_at = change_at();
let each = std::env::var_os(EACH_VAR).is_some();
for frame in 0..frames() {
let changes = change_at.filter(|&at| frame >= at);
let inputs = match changes.is_some_and(|at| !each || (frame - at) % 2 == 0) {
true => &changed,
false => &Inputs::default(),
};
for index in 0..frames() {
let done = AtomicBool::new(false);
let before = device.work_counts();
let (took, presented, stood) = thread::scope(|scope| {
@@ -450,7 +475,7 @@ fn replayed_frames() {
let mut blit = command_buffer.blit_command_encoder().unwrap();
let size = Size::new(SIDE, SIDE, 1);
blit.copy_from_texture_to_buffer(
&page.surface,
surface,
0,
0,
Origin::new(0, 0, 0),
@@ -473,7 +498,7 @@ fn replayed_frames() {
})
});
let start = Instant::now();
let command_buffer = page.frame_with(&page_queue, inputs);
let command_buffer = frame(&page_queue, index);
command_buffer.wait_until_completed_for(WAIT).unwrap();
let took = start.elapsed();
assert_eq!(command_buffer.status(), CommandBufferStatus::Completed);
@@ -482,7 +507,7 @@ fn replayed_frames() {
});
let after = device.work_counts();
println!(
"REPLAYED-FRAMES {name} frame {frame}: {:.1} ms (encoding to completion), {}; \
"REPLAYED-FRAMES {name} frame {index}: {:.1} ms (encoding to completion), {}; \
presenter {} command buffers, largest {:.1} ms, mean {:.1} ms; bystander {}, \
largest {:.1} ms, mean {:.1} ms; pixels {:016x}",
took.as_secs_f64() * 1e3,
@@ -493,12 +518,12 @@ fn replayed_frames() {
stood.count,
stood.largest.as_secs_f64() * 1e3,
stood.total.as_secs_f64() * 1e3 / stood.count.max(1) as f64,
pixels_hash(&page.pixels()),
pixels_hash(&pixels()),
);
let shapes = shapes(&before, &after);
assert!(
frame == 0 || shapes <= WARM_SHAPES,
"warm frame {frame} on {name} made or found {shapes} bind shapes"
index == 0 || warm_shapes.is_none_or(|warm| shapes <= warm),
"warm frame {index} on {name} made or found {shapes} bind shapes"
);
}
}
@@ -0,0 +1,233 @@
//! The page of `replayed_frames.rs` as a page's bind groups draw it: each command the GPU writes
//! sets its own fragment buffer, an argument buffer whose entry names a texture of its own, and
//! the executing pass makes many buffers and every command's texture resident.
//!
//! The owned fixture `icb_vertex_encode_textured`: a pass with rasterization off draws one point
//! per instance [`DRAWS`] times, setting `ivt_encode`'s state before each; the draw of instance
//! `i` encodes command `i`, a draw of 6 16-bit indices of primitive type `i % 5` whose fragment
//! buffer 0 is the argument buffer slot `i` of the slots buffer points at, its entry naming
//! texture `i`. A second pass makes [`BUFFERS`] buffers resident (the commands' argument buffers,
//! the rest buffers no command binds), and the [`DRAWS`] textures, then executes the commands,
//! which inherit its state (`ivt_vertex`, `ivt_fragment`): the GPU may write the records, so a
//! host step records the execution from them, finding each command's fragment buffer by its
//! address among the resident ones. [`replayed_textured_frames`] prints the frames as
//! `replayed_frames` does (`REPLAYED-FRAMES` lines); the step's time should grow with the
//! records, not with them times the resident resources. [`DRAWS_VAR`] and [`BUFFERS_VAR`] set
//! other counts. It is ignored by default: run it by name with `--run-ignored all`.
use super::replayed_frames::{function, indices, measured_device, time_frames, SIDE};
use super::{buffer_with, read_buffer, Messages, ScratchCache};
use metal2vulkan::metal::{
IndirectCommandType, PixelFormat, PrimitiveType, RenderStages, ResourceUsage, TextureUsage,
};
use metal2vulkan::pipeline::{ColorAttachmentDescriptor, RenderPipelineDescriptor};
use metal2vulkan_devtools::{engine_dir, llvm_assemble};
use metal2vulkan_runtime::{
BlitOption, Buffer, CommandBuffer, CommandQueue, Device, IndirectCommandBuffer,
IndirectCommandBufferDescriptor, LoadAction, Origin, PipelineOptions, Region,
RenderPassDescriptor, RenderPipelineState, ResourceOptions, Size, StoreAction, Texture,
TextureDescriptor,
};
/// The variable that sets the commands the pass encodes, each with a texture of its own.
const DRAWS_VAR: &str = "REPLAYED_TEXTURED_DRAWS";
/// The variable that sets the buffers the execution's pass makes resident, the commands'
/// argument buffers among them (at least one a command).
const BUFFERS_VAR: &str = "REPLAYED_TEXTURED_BUFFERS";
/// The commands the pass encodes by default.
const DRAWS: usize = 5001;
/// The buffers the execution's pass makes resident by default: three a command and a few more.
const BUFFERS: usize = 15018;
/// The fixture's states and resources on a device.
struct Page {
draws: usize,
encode: RenderPipelineState,
draw: RenderPipelineState,
icb: IndirectCommandBuffer,
table: Buffer,
indices: Buffer,
/// The address of each command's argument buffer.
slots: Buffer,
/// The buffers the execution's pass makes resident: the commands' argument buffers, one a
/// command, then buffers no command binds.
buffers: Vec<Buffer>,
/// Each command's texture, which its argument buffer's entry names.
textures: Vec<Texture>,
target: Texture,
surface: Texture,
pixels: Buffer,
}
/// A count [`DRAWS_VAR`] or [`BUFFERS_VAR`] sets, `default` where it is unset.
fn count(var: &str, default: usize) -> usize {
std::env::var(var).map_or(default, |count| count.trim().parse().expect(var))
}
impl Page {
fn new(device: &Device, draws: usize, buffers: usize) -> Page {
let bitcode = llvm_assemble(&engine_dir().join("icb_vertex_encode_textured.ll")).unwrap();
let library = device.new_library_with_air(&bitcode).unwrap();
let rgba = PixelFormat::RGBA8Unorm;
let options = PipelineOptions::default();
let mut descriptor = RenderPipelineDescriptor::new(function(&library, "ivt_encode"));
descriptor.rasterization_enabled = false;
descriptor.color_attachments[0] = Some(ColorAttachmentDescriptor::new(rgba));
let encode = device
.new_render_pipeline_state(&descriptor, &options)
.unwrap();
let mut descriptor = RenderPipelineDescriptor::new(function(&library, "ivt_vertex"));
descriptor.fragment = Some(function(&library, "ivt_fragment"));
descriptor.color_attachments[0] = Some(ColorAttachmentDescriptor::new(rgba));
descriptor.support_indirect_command_buffers = true;
let draw = device
.new_render_pipeline_state(&descriptor, &options)
.unwrap();
let mut commands = IndirectCommandBufferDescriptor::new();
commands.command_types = IndirectCommandType::DRAW_INDEXED;
commands.inherit_pipeline_state = true;
commands.inherit_buffers = false;
commands.max_fragment_buffer_bind_count = 1;
let icb = (device.new_indirect_command_buffer(
&commands,
draws as u64,
ResourceOptions::shared(),
))
.unwrap();
let table = buffer_with(device, &icb.gpu_resource_id().0.to_le_bytes()).unwrap();
let textures: Vec<_> = (0..draws)
.map(|at| {
let mut descriptor = TextureDescriptor::texture_2d(rgba, 4, 4, false);
descriptor.usage = TextureUsage::SHADER_READ;
descriptor.resource_options = ResourceOptions::shared();
let texture = device.new_texture(&descriptor).unwrap();
let texel = [(at % 251) as u8, (at / 251 % 251) as u8, 0x80, 0xff];
let texels = texel.repeat(16);
(texture.replace_region(Region::new_2d(0, 0, 4, 4), 0, 0, &texels, 16, 0)).unwrap();
texture
})
.collect();
let entries = textures.iter().map(|texture| {
buffer_with(device, &texture.gpu_resource_id().0.to_le_bytes()).unwrap()
});
let unbound = (draws..buffers.max(draws))
.map(|_| device.new_buffer(256, ResourceOptions::shared()).unwrap());
let buffers: Vec<_> = entries.chain(unbound).collect();
let slots = (buffers[..draws].iter()).flat_map(|entry| entry.gpu_address().0.to_le_bytes());
let slots = buffer_with(device, &slots.collect::<Vec<_>>()).unwrap();
let texture = |usage: TextureUsage| {
let mut descriptor = TextureDescriptor::texture_2d(rgba, SIDE, SIDE, false);
descriptor.usage = usage;
descriptor.resource_options = ResourceOptions::shared();
device.new_texture(&descriptor).unwrap()
};
Page {
draws,
encode,
draw,
icb,
table,
indices: buffer_with(device, &indices(37)).unwrap(),
slots,
buffers,
textures,
target: texture(TextureUsage::RENDER_TARGET),
surface: texture(TextureUsage::SHADER_READ),
pixels: device
.new_buffer(SIDE * SIDE * 4, ResourceOptions::shared())
.unwrap(),
}
}
fn pass(&self) -> RenderPassDescriptor {
let mut pass = RenderPassDescriptor::new();
let color = &mut pass.color_attachments[0];
color.texture = Some(self.target.clone());
color.load_action = LoadAction::Clear;
color.store_action = StoreAction::Store;
pass
}
/// One frame on `queue`, committed: the encoding pass, then the execution's, then a copy of
/// the target's pixels.
fn frame(&self, queue: &CommandQueue) -> CommandBuffer {
let command_buffer = queue.command_buffer().unwrap();
let mut encoder = command_buffer.render_command_encoder(&self.pass()).unwrap();
let vertex = RenderStages::VERTEX;
let uses = [
(self.icb.as_resource(), ResourceUsage::WRITE),
(self.table.as_resource(), ResourceUsage::READ),
(self.indices.as_resource(), ResourceUsage::READ),
(self.slots.as_resource(), ResourceUsage::READ),
(self.surface.as_resource(), ResourceUsage::READ),
];
for (resource, usage) in uses {
encoder.use_resource(resource, usage, vertex).unwrap();
}
encoder.set_vertex_buffer(Some(&self.table), 0, 0).unwrap();
encoder
.set_vertex_buffer(Some(&self.indices), 0, 1)
.unwrap();
encoder.set_vertex_buffer(Some(&self.slots), 0, 2).unwrap();
for draw in 0..self.draws as u64 {
encoder.set_render_pipeline_state(&self.encode).unwrap();
encoder
.draw_primitives(PrimitiveType::Point, 0, 1, 1, draw)
.unwrap();
}
encoder.end_encoding().unwrap();
let mut encoder = command_buffer.render_command_encoder(&self.pass()).unwrap();
encoder.set_render_pipeline_state(&self.draw).unwrap();
let stages = RenderStages::VERTEX | RenderStages::FRAGMENT;
let buffers = self.buffers.iter().map(Buffer::as_resource);
for resource in buffers.chain(self.textures.iter().map(Texture::as_resource)) {
encoder
.use_resource(resource, ResourceUsage::READ, stages)
.unwrap();
}
encoder
.execute_commands_in_buffer(&self.icb, 0..self.draws as u64)
.unwrap();
encoder.end_encoding().unwrap();
let mut blit = command_buffer.blit_command_encoder().unwrap();
let size = Size::new(SIDE, SIDE, 1);
let (row, image) = (SIDE * 4, SIDE * SIDE * 4);
let origin = Origin::new(0, 0, 0);
let none = BlitOption::None;
(blit.copy_from_texture_to_buffer(
&self.target,
0,
0,
origin,
size,
&self.pixels,
0,
row,
image,
none,
))
.unwrap();
blit.end_encoding().unwrap();
command_buffer.commit().unwrap();
command_buffer
}
}
#[test]
#[ignore = "a measurement, not a check: run by name with --run-ignored all"]
fn replayed_textured_frames() {
let cache = ScratchCache::new().unwrap();
let messages = Messages::default();
let device = measured_device(&messages, &cache);
let page = Page::new(
&device,
count(DRAWS_VAR, DRAWS),
count(BUFFERS_VAR, BUFFERS),
);
let pixels = || read_buffer(&page.pixels, 0, SIDE * SIDE * 4).unwrap();
let frame = |queue: &CommandQueue, _| page.frame(queue);
time_frames(&device, &page.surface, None, frame, pixels);
}