-
Notifications
You must be signed in to change notification settings - Fork 30.6k
Expand file tree
/
Copy pathgpu_test.dart
More file actions
2309 lines (2011 loc) · 87.9 KB
/
Copy pathgpu_test.dart
File metadata and controls
2309 lines (2011 loc) · 87.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flutter GPU API tests.
// The flutter_gpu package is located at //flutter/impeller/lib/gpu.
// ignore_for_file: avoid_relative_lib_imports
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:test/test.dart';
import 'package:vector_math/vector_math.dart';
import '../../lib/gpu/lib/gpu.dart' as gpu;
import 'goldens.dart';
import 'impeller_enabled.dart';
ByteData float32(List<double> values) {
return Float32List.fromList(values).buffer.asByteData();
}
ByteData unlitUBO(Matrix4 mvp, Vector4 color) {
return float32(<double>[
mvp[0], mvp[1], mvp[2], mvp[3], //
mvp[4], mvp[5], mvp[6], mvp[7], //
mvp[8], mvp[9], mvp[10], mvp[11], //
mvp[12], mvp[13], mvp[14], mvp[15], //
color.r, color.g, color.b, color.a,
]);
}
ByteData mvpUBO(Matrix4 mvp) {
return float32(<double>[
mvp[0], mvp[1], mvp[2], mvp[3], //
mvp[4], mvp[5], mvp[6], mvp[7], //
mvp[8], mvp[9], mvp[10], mvp[11], //
mvp[12], mvp[13], mvp[14], mvp[15], //
]);
}
Future<void> submitAndWait(gpu.CommandBuffer commandBuffer) {
final completer = Completer<void>();
commandBuffer.submit(
completionCallback: (bool success) {
if (success) {
completer.complete();
} else {
completer.completeError(Exception('CommandBuffer submit failed'));
}
},
);
return completer.future;
}
Future<ByteData> readTextureBytes(gpu.Texture texture) async {
final ui.Image image = texture.asImage();
final ByteData? bytes = await image.toByteData();
expect(bytes, isNotNull);
return bytes!;
}
Future<gpu.RenderPipeline> createUnlitRenderPipeline() async {
final gpu.ShaderLibrary? library = await gpu.ShaderLibrary.fromAsset('test.shaderbundle');
assert(library != null);
final gpu.Shader? vertex = library!['UnlitVertex'];
assert(vertex != null);
final gpu.Shader? fragment = library['UnlitFragment'];
assert(fragment != null);
return gpu.gpuContext.createRenderPipeline(vertex!, fragment!);
}
Future<gpu.RenderPipeline> createTextureRenderPipeline() async {
final gpu.ShaderLibrary? library = await gpu.ShaderLibrary.fromAsset('test.shaderbundle');
assert(library != null);
final gpu.Shader? vertex = library!['TextureVertex'];
assert(vertex != null);
final gpu.Shader? fragment = library['TextureFragment'];
assert(fragment != null);
return gpu.gpuContext.createRenderPipeline(vertex!, fragment!);
}
class RenderPassState {
RenderPassState(this.renderTexture, this.commandBuffer, this.renderPass);
final gpu.Texture renderTexture;
final gpu.CommandBuffer commandBuffer;
final gpu.RenderPass renderPass;
}
/// Create a simple RenderPass with simple color and depth-stencil attachments.
RenderPassState createSimpleRenderPass({Vector4? clearColor}) {
final gpu.Texture renderTexture = gpu.gpuContext.createTexture(
gpu.StorageMode.devicePrivate,
100,
100,
);
final gpu.Texture depthStencilTexture = gpu.gpuContext.createTexture(
gpu.StorageMode.deviceTransient,
100,
100,
format: gpu.gpuContext.defaultDepthStencilFormat,
);
final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer();
final renderTarget = gpu.RenderTarget.singleColor(
gpu.ColorAttachment(texture: renderTexture, clearValue: clearColor),
depthStencilAttachment: gpu.DepthStencilAttachment(texture: depthStencilTexture),
);
final gpu.RenderPass renderPass = commandBuffer.createRenderPass(renderTarget);
return RenderPassState(renderTexture, commandBuffer, renderPass);
}
RenderPassState createSimpleRenderPassWithMSAA() {
// Create transient MSAA attachments, which will live entirely in tile memory
// for most GPUs.
final gpu.Texture renderTexture = gpu.gpuContext.createTexture(
gpu.StorageMode.deviceTransient,
100,
100,
format: gpu.gpuContext.defaultColorFormat,
sampleCount: 4,
);
final gpu.Texture depthStencilTexture = gpu.gpuContext.createTexture(
gpu.StorageMode.deviceTransient,
100,
100,
format: gpu.gpuContext.defaultDepthStencilFormat,
sampleCount: 4,
);
// Create the single-sample resolve texture that live in DRAM and will be
// drawn to the screen.
final gpu.Texture resolveTexture = gpu.gpuContext.createTexture(
gpu.StorageMode.devicePrivate,
100,
100,
format: gpu.gpuContext.defaultColorFormat,
);
final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer();
final renderTarget = gpu.RenderTarget.singleColor(
gpu.ColorAttachment(
texture: renderTexture,
resolveTexture: resolveTexture,
storeAction: gpu.StoreAction.multisampleResolve,
),
depthStencilAttachment: gpu.DepthStencilAttachment(texture: depthStencilTexture),
);
final gpu.RenderPass renderPass = commandBuffer.createRenderPass(renderTarget);
return RenderPassState(resolveTexture, commandBuffer, renderPass);
}
Future<void> drawTriangle(RenderPassState state, Vector4 color) async {
final gpu.RenderPipeline pipeline = await createUnlitRenderPipeline();
state.renderPass.bindPipeline(pipeline);
final gpu.HostBuffer transients = gpu.gpuContext.createHostBuffer();
final gpu.BufferView vertices = transients.emplace(
float32(<double>[
-0.5, 0.5, //
0.0, -0.5, //
0.5, 0.5, //
]),
);
final gpu.BufferView vertInfoData = transients.emplace(unlitUBO(Matrix4.identity(), color));
state.renderPass.bindVertexBuffer(vertices);
final gpu.UniformSlot vertInfo = pipeline.vertexShader.getUniformSlot('VertInfo');
state.renderPass.bindUniform(vertInfo, vertInfoData);
state.renderPass.draw(3);
}
void main() async {
final ImageComparer comparer = await ImageComparer.create();
test('gpu.context throws exception when Impeller is not enabled', () async {
try {
// ignore: unnecessary_statements
gpu.gpuContext; // Force the context to instantiate.
if (!impellerEnabled) {
fail('Exception not thrown, but Impeller is not enabled.');
}
} catch (e) {
if (impellerEnabled && flutterGpuEnabled) {
fail('Exception thrown even though Impeller and Flutter GPU are both enabled: $e');
}
if (!impellerEnabled) {
expect(e.toString(), contains('Flutter GPU requires the Impeller rendering backend'));
}
}
});
test('gpu.context throws exception when Flutter GPU is not enabled', () async {
try {
// ignore: unnecessary_statements
gpu.gpuContext; // Force the context to instantiate.
if (!flutterGpuEnabled) {
fail('Exception not thrown, but Flutter GPU is not enabled.');
}
} catch (e) {
if (flutterGpuEnabled && impellerEnabled) {
fail('Exception thrown even though Flutter GPU and Impeller are both enabled: $e');
}
if (impellerEnabled) {
expect(
e.toString(),
contains('Flutter GPU must be enabled via the Flutter GPU manifest setting'),
);
}
}
});
test('gpu.context is available when Impeller and Flutter GPU are enabled', () async {
try {
// ignore: unnecessary_statements
gpu.gpuContext; // Force the context to instantiate.
} catch (e) {
if (impellerEnabled && flutterGpuEnabled) {
fail('Exception thrown even though Impeller and Flutter GPU are enabled: $e');
}
}
});
test('GpuContext.minimumUniformByteAlignment', () async {
final int alignment = gpu.gpuContext.minimumUniformByteAlignment;
expect(alignment, greaterThanOrEqualTo(16));
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('HostBuffer.emplace', () async {
final gpu.HostBuffer hostBuffer = gpu.gpuContext.createHostBuffer();
final gpu.BufferView view0 = hostBuffer.emplace(
Int8List.fromList(<int>[0, 1, 2, 3]).buffer.asByteData(),
);
expect(view0.offsetInBytes, 0);
expect(view0.lengthInBytes, 4);
final gpu.BufferView view1 = hostBuffer.emplace(
Int8List.fromList(<int>[0, 1, 2, 3]).buffer.asByteData(),
);
expect(view1.offsetInBytes, equals(gpu.gpuContext.minimumUniformByteAlignment));
expect(view1.lengthInBytes, 4);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('HostBuffer.reset', () async {
final gpu.HostBuffer hostBuffer = gpu.gpuContext.createHostBuffer();
final gpu.BufferView view0 = hostBuffer.emplace(
Int8List.fromList(<int>[0, 1, 2, 3]).buffer.asByteData(),
);
expect(view0.offsetInBytes, 0);
expect(view0.lengthInBytes, 4);
hostBuffer.reset();
final gpu.BufferView view1 = hostBuffer.emplace(
Int8List.fromList(<int>[0, 1, 2, 3]).buffer.asByteData(),
);
expect(view1.offsetInBytes, 0);
expect(view1.lengthInBytes, 4);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('HostBuffer reuses DeviceBuffers after N frames', () async {
final gpu.HostBuffer hostBuffer = gpu.gpuContext.createHostBuffer();
final gpu.BufferView view0 = hostBuffer.emplace(
Int8List.fromList(<int>[0, 1, 2, 3]).buffer.asByteData(),
);
for (var i = 0; i < hostBuffer.frameCount; i++) {
hostBuffer.reset();
}
final gpu.BufferView view1 = hostBuffer.emplace(
Int8List.fromList(<int>[0, 1, 2, 3]).buffer.asByteData(),
);
expect(view0.buffer, equals(view1.buffer));
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuContext.createDeviceBuffer', () async {
final gpu.DeviceBuffer deviceBuffer = gpu.gpuContext.createDeviceBuffer(
gpu.StorageMode.hostVisible,
4,
);
expect(deviceBuffer.sizeInBytes, 4);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('DeviceBuffer.overwrite', () async {
final gpu.DeviceBuffer deviceBuffer = gpu.gpuContext.createDeviceBuffer(
gpu.StorageMode.hostVisible,
4,
);
final bool success = deviceBuffer.overwrite(
Int8List.fromList(<int>[0, 1, 2, 3]).buffer.asByteData(),
);
deviceBuffer.flush();
expect(success, true);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('DeviceBuffer.overwrite fails when out of bounds', () async {
final gpu.DeviceBuffer deviceBuffer = gpu.gpuContext.createDeviceBuffer(
gpu.StorageMode.hostVisible,
4,
);
final bool success = deviceBuffer.overwrite(
Int8List.fromList(<int>[0, 1, 2, 3]).buffer.asByteData(),
destinationOffsetInBytes: 1,
);
deviceBuffer.flush();
expect(success, false);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('DeviceBuffer.overwrite throws for negative destination offset', () async {
final gpu.DeviceBuffer deviceBuffer = gpu.gpuContext.createDeviceBuffer(
gpu.StorageMode.hostVisible,
4,
);
try {
deviceBuffer.overwrite(
Int8List.fromList(<int>[0, 1, 2, 3]).buffer.asByteData(),
destinationOffsetInBytes: -1,
);
deviceBuffer.flush();
fail('Exception not thrown for negative destination offset.');
} catch (e) {
expect(e.toString(), contains('destinationOffsetInBytes must be positive'));
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuContext.createTexture', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 100, 100);
// Check the defaults.
expect(texture.width, 100);
expect(texture.height, 100);
expect(texture.storageMode, gpu.StorageMode.hostVisible);
expect(texture.sampleCount, 1);
expect(texture.textureType, gpu.TextureType.texture2D);
expect(texture.format, gpu.PixelFormat.r8g8b8a8UNormInt);
expect(texture.enableRenderTargetUsage, true);
expect(texture.enableShaderReadUsage, true);
expect(!texture.enableShaderWriteUsage, true);
expect(texture.format.bytesPerBlock, 4);
expect(texture.getBaseMipLevelSizeInBytes(), 40000);
expect(texture.mipLevelCount, 1);
expect(texture.sliceCount, 1);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuContext.createTexture allocates an r32Float texture', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
4,
4,
format: gpu.PixelFormat.r32Float,
);
expect(texture.format, gpu.PixelFormat.r32Float);
expect(texture.format.bytesPerBlock, 4);
expect(texture.getBaseMipLevelSizeInBytes(), 4 * 4 * 4);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('PixelFormat block introspection matches the format table', () async {
// Uncompressed formats report block dims of 1.
expect(gpu.PixelFormat.r8g8b8a8UNormInt.isCompressed, false);
expect(gpu.PixelFormat.r8g8b8a8UNormInt.blockWidth, 1);
expect(gpu.PixelFormat.r8g8b8a8UNormInt.blockHeight, 1);
expect(gpu.PixelFormat.r8g8b8a8UNormInt.bytesPerBlock, 4);
expect(gpu.PixelFormat.r8g8b8a8UNormInt.compressionFamily, isNull);
expect(gpu.PixelFormat.r32Float.bytesPerBlock, 4);
// BC1 / ETC2 RGB8: 4x4 blocks, 8 bytes per block.
expect(gpu.PixelFormat.bc1RGBAUNormInt.isCompressed, true);
expect(gpu.PixelFormat.bc1RGBAUNormInt.blockWidth, 4);
expect(gpu.PixelFormat.bc1RGBAUNormInt.blockHeight, 4);
expect(gpu.PixelFormat.bc1RGBAUNormInt.bytesPerBlock, 8);
expect(gpu.PixelFormat.bc1RGBAUNormInt.compressionFamily, gpu.TextureCompressionFamily.bc);
expect(gpu.PixelFormat.etc2RGB8UNormInt.bytesPerBlock, 8);
expect(gpu.PixelFormat.etc2RGB8UNormInt.compressionFamily, gpu.TextureCompressionFamily.etc2);
// BC7 / ETC2 RGBA / ASTC 4x4: 4x4 blocks, 16 bytes per block.
expect(gpu.PixelFormat.bc7RGBAUNormInt.bytesPerBlock, 16);
expect(gpu.PixelFormat.etc2RGBA8UNormInt.bytesPerBlock, 16);
expect(gpu.PixelFormat.astc4x4LDR.bytesPerBlock, 16);
expect(gpu.PixelFormat.astc4x4LDR.compressionFamily, gpu.TextureCompressionFamily.astc);
// ASTC 8x8: 8x8 blocks, 16 bytes per block.
expect(gpu.PixelFormat.astc8x8LDR.blockWidth, 8);
expect(gpu.PixelFormat.astc8x8LDR.blockHeight, 8);
expect(gpu.PixelFormat.astc8x8LDR.bytesPerBlock, 16);
// All ASTC LDR variants map to the astc family.
expect(gpu.PixelFormat.astc4x4LDRSRGB.compressionFamily, gpu.TextureCompressionFamily.astc);
expect(gpu.PixelFormat.astc8x8LDR.compressionFamily, gpu.TextureCompressionFamily.astc);
expect(gpu.PixelFormat.astc8x8LDRSRGB.compressionFamily, gpu.TextureCompressionFamily.astc);
// ASTC HDR shares geometry with ASTC LDR but is its own family.
expect(gpu.PixelFormat.astc4x4HDR.blockWidth, 4);
expect(gpu.PixelFormat.astc4x4HDR.blockHeight, 4);
expect(gpu.PixelFormat.astc4x4HDR.bytesPerBlock, 16);
expect(gpu.PixelFormat.astc4x4HDR.compressionFamily, gpu.TextureCompressionFamily.astcHdr);
expect(gpu.PixelFormat.astc8x8HDR.blockWidth, 8);
expect(gpu.PixelFormat.astc8x8HDR.blockHeight, 8);
expect(gpu.PixelFormat.astc8x8HDR.bytesPerBlock, 16);
expect(gpu.PixelFormat.astc8x8HDR.compressionFamily, gpu.TextureCompressionFamily.astcHdr);
});
test('GpuContext.supportsTextureCompression returns a bool per family', () async {
// The exact answer depends on the device, but each query must return.
expect(gpu.gpuContext.supportsTextureCompression(gpu.TextureCompressionFamily.bc), isA<bool>());
expect(
gpu.gpuContext.supportsTextureCompression(gpu.TextureCompressionFamily.etc2),
isA<bool>(),
);
expect(
gpu.gpuContext.supportsTextureCompression(gpu.TextureCompressionFamily.astc),
isA<bool>(),
);
expect(
gpu.gpuContext.supportsTextureCompression(gpu.TextureCompressionFamily.astcHdr),
isA<bool>(),
);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuContext.supportsTextureFormat rejects render-target on compressed', () async {
// Sample-only is always permitted to be queried (subject to the family
// capability); render-target and shader-write are always rejected for
// compressed formats.
expect(
gpu.gpuContext.supportsTextureFormat(gpu.PixelFormat.bc7RGBAUNormInt, renderTarget: true),
false,
);
expect(
gpu.gpuContext.supportsTextureFormat(gpu.PixelFormat.bc7RGBAUNormInt, shaderWrite: true),
false,
);
// Uncompressed formats are not gated by per-format usage today.
expect(
gpu.gpuContext.supportsTextureFormat(gpu.PixelFormat.r8g8b8a8UNormInt, renderTarget: true),
true,
);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.getMipLevelSizeInBytes is block-aware for compressed formats', () async {
if (!gpu.gpuContext.supportsTextureCompression(gpu.TextureCompressionFamily.bc)) {
markTestSkipped('BC texture compression is not supported on this device.');
return;
}
final gpu.Texture texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
16,
16,
format: gpu.PixelFormat.bc1RGBAUNormInt,
enableRenderTargetUsage: false,
mipLevelCount: 3,
);
// 16x16 -> 4x4 blocks * 8 bytes = 128.
expect(texture.getMipLevelSizeInBytes(0), 128);
// 8x8 -> 2x2 blocks * 8 bytes = 32.
expect(texture.getMipLevelSizeInBytes(1), 32);
// 4x4 -> 1x1 block * 8 bytes = 8.
expect(texture.getMipLevelSizeInBytes(2), 8);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuContext.createTexture allocates a compressed texture when supported', () async {
if (!gpu.gpuContext.supportsTextureCompression(gpu.TextureCompressionFamily.bc)) {
markTestSkipped('BC texture compression is not supported on this device.');
return;
}
final gpu.Texture texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
8,
8,
format: gpu.PixelFormat.bc1RGBAUNormInt,
enableRenderTargetUsage: false,
);
expect(texture.format, gpu.PixelFormat.bc1RGBAUNormInt);
expect(texture.format.isCompressed, true);
// 8x8 -> 2x2 blocks * 8 bytes = 32.
expect(texture.getBaseMipLevelSizeInBytes(), 32);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.overwrite uploads block-aligned compressed data', () async {
if (!gpu.gpuContext.supportsTextureCompression(gpu.TextureCompressionFamily.bc)) {
markTestSkipped('BC texture compression is not supported on this device.');
return;
}
final gpu.Texture texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
8,
8,
format: gpu.PixelFormat.bc1RGBAUNormInt,
enableRenderTargetUsage: false,
);
final ByteData bytes = Uint8List(32).buffer.asByteData();
texture.overwrite(bytes);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.overwrite rejects wrong size for compressed format', () async {
if (!gpu.gpuContext.supportsTextureCompression(gpu.TextureCompressionFamily.bc)) {
markTestSkipped('BC texture compression is not supported on this device.');
return;
}
final gpu.Texture texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
8,
8,
format: gpu.PixelFormat.bc1RGBAUNormInt,
enableRenderTargetUsage: false,
);
try {
// 8x8 BC1 needs 32 bytes; 16 is wrong.
texture.overwrite(Uint8List(16).buffer.asByteData());
fail('Exception not thrown for wrong compressed buffer size.');
} catch (e) {
expect(e.toString(), contains('must exactly match the size of mip level 0'));
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuContext.createTexture rejects compressed format as render target', () async {
try {
gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
8,
8,
format: gpu.PixelFormat.bc1RGBAUNormInt,
);
fail('Exception not thrown for compressed render target.');
} catch (e) {
expect(e.toString(), contains('sample-only'));
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuContext.createTexture rejects compressed format with shader write', () async {
try {
gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
8,
8,
format: gpu.PixelFormat.bc1RGBAUNormInt,
enableRenderTargetUsage: false,
enableShaderWriteUsage: true,
);
fail('Exception not thrown for compressed shader write.');
} catch (e) {
expect(e.toString(), contains('sample-only'));
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuContext.createTexture rejects compressed format with MSAA', () async {
try {
gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
8,
8,
format: gpu.PixelFormat.bc1RGBAUNormInt,
enableRenderTargetUsage: false,
sampleCount: 4,
);
fail('Exception not thrown for compressed MSAA.');
} catch (e) {
expect(e.toString(), contains('sample-only'));
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuContext.createTexture rejects non-block-aligned compressed dimensions', () async {
try {
// 5 is not a multiple of the 4x4 BC1 block.
gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
5,
8,
format: gpu.PixelFormat.bc1RGBAUNormInt,
enableRenderTargetUsage: false,
);
fail('Exception not thrown for non-block-aligned compressed dimensions.');
} catch (e) {
expect(e.toString(), contains('multiple of the block size'));
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.fullMipCount', () async {
// Matches Impeller's `ISize::MipCount`: `floor(log2(min(w, h)))`,
// clamped to a minimum of 1.
expect(gpu.Texture.fullMipCount(1, 1), 1);
expect(gpu.Texture.fullMipCount(2, 1), 1);
expect(gpu.Texture.fullMipCount(2, 2), 1);
expect(gpu.Texture.fullMipCount(4, 4), 2);
expect(gpu.Texture.fullMipCount(8, 8), 3);
expect(gpu.Texture.fullMipCount(100, 100), 6);
expect(gpu.Texture.fullMipCount(1024, 1024), 10);
// Non-square: count uses the smaller dimension.
expect(gpu.Texture.fullMipCount(1024, 1), 1);
expect(gpu.Texture.fullMipCount(1, 256), 1);
});
test('GpuContext.createTexture with mipLevelCount allocates a mip chain', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
8,
8,
mipLevelCount: 3,
);
expect(texture.mipLevelCount, 3);
// Per-level sizes: 8*8*4=256, 4*4*4=64, 2*2*4=16.
expect(texture.getMipLevelSizeInBytes(0), 256);
expect(texture.getMipLevelSizeInBytes(1), 64);
expect(texture.getMipLevelSizeInBytes(2), 16);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuContext.createTexture rejects out-of-range mipLevelCount', () async {
try {
gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 8, 8, mipLevelCount: 0);
fail('Exception not thrown for mipLevelCount=0.');
} catch (e) {
expect(e.toString(), contains('mipLevelCount'));
}
try {
// Max for 8x8 is 3.
gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 8, 8, mipLevelCount: 4);
fail('Exception not thrown for mipLevelCount above the maximum.');
} catch (e) {
expect(e.toString(), contains('mipLevelCount'));
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test(
'GpuContext.createTexture fails if invalid sampleCount and texture type is passed.',
() async {
try {
gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
100,
100,
sampleCount: 4,
textureType: gpu.TextureType.texture2D,
);
fail('Exception not thrown when creating an invalid texture.');
} catch (e) {
expect(e.toString(), contains('Texture creation failed'));
}
},
skip: !(impellerEnabled && flutterGpuEnabled),
);
test('Texture.overwrite', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 2, 2);
const red = ui.Color.fromARGB(0xFF, 0xFF, 0, 0);
const green = ui.Color.fromARGB(0xFF, 0, 0xFF, 0);
texture.overwrite(
Int32List.fromList(<int>[red.value, green.value, green.value, red.value]).buffer.asByteData(),
);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('CommandBuffer.copyBufferToTexture writes tightly packed texture regions', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 2, 2);
final ByteData pixels = Uint8List.fromList(<int>[
0xFF, 0x00, 0x00, 0xFF, // red
0x00, 0xFF, 0x00, 0xFF, // green
0x00, 0x00, 0xFF, 0xFF, // blue
0xFF, 0xFF, 0x00, 0xFF, // yellow
]).buffer.asByteData();
final gpu.DeviceBuffer source = gpu.gpuContext.createDeviceBufferWithCopy(pixels);
final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer();
commandBuffer.copyBufferToTexture(
gpu.BufferView(source, offsetInBytes: 0, lengthInBytes: 4),
gpu.TextureRegion(texture, width: 1, height: 1),
);
commandBuffer.copyBufferToTexture(
gpu.BufferView(source, offsetInBytes: 4, lengthInBytes: 4),
gpu.TextureRegion(texture, x: 1, width: 1, height: 1),
);
commandBuffer.copyBufferToTexture(
gpu.BufferView(source, offsetInBytes: 8, lengthInBytes: 8),
gpu.TextureRegion(texture, y: 1, width: 2, height: 1),
);
await submitAndWait(commandBuffer);
final ByteData bytes = await readTextureBytes(texture);
expect(bytes.getUint8(0), 0xFF);
expect(bytes.getUint8(1), 0x00);
expect(bytes.getUint8(2), 0x00);
expect(bytes.getUint8(3), 0xFF);
final int bottomRightOffset = (1 + 1 * texture.width) * 4;
expect(bytes.getUint8(bottomRightOffset), 0xFF);
expect(bytes.getUint8(bottomRightOffset + 1), 0xFF);
expect(bytes.getUint8(bottomRightOffset + 2), 0x00);
expect(bytes.getUint8(bottomRightOffset + 3), 0xFF);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('CommandBuffer.copyTextureToTexture copies a texture region', () async {
final gpu.Texture sourceTexture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
2,
2,
);
final gpu.Texture destinationTexture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
2,
2,
);
final ByteData pixels = Uint8List.fromList(<int>[
0x00,
0x00,
0x00,
0xFF,
0x11,
0x22,
0x33,
0xFF,
0x44,
0x55,
0x66,
0xFF,
0xAA,
0xBB,
0xCC,
0xFF,
]).buffer.asByteData();
sourceTexture.overwrite(pixels);
final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer();
commandBuffer.copyTextureToTexture(
gpu.TextureRegion(sourceTexture, x: 1, y: 1, width: 1, height: 1),
gpu.TextureDestinationRegion(destinationTexture),
);
await submitAndWait(commandBuffer);
final ByteData bytes = await readTextureBytes(destinationTexture);
expect(bytes.getUint8(0), 0xAA);
expect(bytes.getUint8(1), 0xBB);
expect(bytes.getUint8(2), 0xCC);
expect(bytes.getUint8(3), 0xFF);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('CommandBuffer.copyTextureToBuffer appends successfully', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 4, 4);
final ByteData pixels = Uint8List.fromList(
List<int>.filled(4 * 4 * 4, 0xFF),
).buffer.asByteData();
texture.overwrite(pixels);
final gpu.DeviceBuffer destination = gpu.gpuContext.createDeviceBuffer(
gpu.StorageMode.hostVisible,
2 * 2 * texture.format.bytesPerBlock,
);
final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer();
commandBuffer.copyTextureToBuffer(
gpu.TextureRegion(texture, width: 2, height: 2),
gpu.BufferView(
destination,
offsetInBytes: 0,
lengthInBytes: 2 * 2 * texture.format.bytesPerBlock,
),
);
await submitAndWait(commandBuffer);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('CommandBuffer.copyBufferToTexture validates copy size', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 2, 2);
final gpu.DeviceBuffer source = gpu.gpuContext.createDeviceBufferWithCopy(
Uint8List.fromList(<int>[0xFF, 0x00, 0x00, 0xFF]).buffer.asByteData(),
);
final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer();
try {
commandBuffer.copyBufferToTexture(
gpu.BufferView(source, offsetInBytes: 0, lengthInBytes: 4),
gpu.TextureRegion(texture),
);
fail('Exception not thrown for wrong copy size.');
} catch (e) {
expect(e.toString(), contains('must match the destination texture region size'));
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.overwrite throws for wrong buffer size', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 100, 100);
const red = ui.Color.fromARGB(0xFF, 0xFF, 0, 0);
try {
texture.overwrite(
Int32List.fromList(<int>[red.value, red.value, red.value, red.value]).buffer.asByteData(),
);
fail('Exception not thrown for wrong buffer size.');
} catch (e) {
expect(
e.toString(),
contains(
'The length of sourceBytes (bytes: 16) must exactly match the size of mip level 0 (bytes: 40000)',
),
);
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.overwrite writes to a non-zero mip level', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
8,
8,
mipLevelCount: 3,
);
const red = ui.Color.fromARGB(0xFF, 0xFF, 0, 0);
const blue = ui.Color.fromARGB(0xFF, 0, 0, 0xFF);
// Mip 0: 8x8 = 64 texels.
texture.overwrite(Int32List.fromList(List<int>.filled(64, red.value)).buffer.asByteData());
// Mip 1: 4x4 = 16 texels.
texture.overwrite(
Int32List.fromList(List<int>.filled(16, blue.value)).buffer.asByteData(),
mipLevel: 1,
);
// Mip 2: 2x2 = 4 texels.
texture.overwrite(
Int32List.fromList(List<int>.filled(4, red.value)).buffer.asByteData(),
mipLevel: 2,
);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.overwrite throws for an out-of-range mipLevel', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
4,
4,
mipLevelCount: 2,
);
const red = ui.Color.fromARGB(0xFF, 0xFF, 0, 0);
try {
texture.overwrite(Int32List.fromList(<int>[red.value]).buffer.asByteData(), mipLevel: 2);
fail('Exception not thrown for out-of-range mipLevel.');
} catch (e) {
expect(e.toString(), contains('mipLevel (2) must be in the range [0, 1]'));
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.overwrite throws for an out-of-range slice', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 2, 2);
const red = ui.Color.fromARGB(0xFF, 0xFF, 0, 0);
try {
texture.overwrite(
Int32List.fromList(List<int>.filled(4, red.value)).buffer.asByteData(),
slice: 1,
);
fail('Exception not thrown for out-of-range slice.');
} catch (e) {
expect(e.toString(), contains('slice (1) must be in the range [0, 0]'));
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.overwrite writes each face of a cubemap', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
2,
2,
textureType: gpu.TextureType.textureCube,
);
expect(texture.sliceCount, 6);
const colors = <ui.Color>[
ui.Color.fromARGB(0xFF, 0xFF, 0, 0),
ui.Color.fromARGB(0xFF, 0, 0xFF, 0),
ui.Color.fromARGB(0xFF, 0, 0, 0xFF),
ui.Color.fromARGB(0xFF, 0xFF, 0xFF, 0),
ui.Color.fromARGB(0xFF, 0xFF, 0, 0xFF),
ui.Color.fromARGB(0xFF, 0, 0xFF, 0xFF),
];
for (var slice = 0; slice < 6; slice++) {
final int v = colors[slice].value;
texture.overwrite(Int32List.fromList(<int>[v, v, v, v]).buffer.asByteData(), slice: slice);
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.asImage returns a valid ui.Image handle', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 100, 100);
final ui.Image image = texture.asImage();
expect(image.width, 100);
expect(image.height, 100);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('Texture.asImage throws when not shader readable', () async {
final gpu.Texture texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
100,
100,
enableShaderReadUsage: false,
);
try {
texture.asImage();
fail('Exception not thrown when not shader readable.');
} catch (e) {
expect(
e.toString(),
contains('Only shader readable Flutter GPU textures can be used as UI Images'),
);
}
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuImageSurface acquireNextFrame returns a presentable color texture', () async {
final gpu.GpuImageSurface surface = gpu.gpuContext.createImageSurface(17, 19);
expect(surface.width, 17);
expect(surface.height, 19);
expect(surface.format, gpu.gpuContext.defaultColorFormat);
expect(surface.currentImage, isNull);
expect(surface.debugBackingTextureCount, 0);
final gpu.GpuImageSurfaceFrame frame = surface.acquireNextFrame();
expect(frame.colorTexture.width, 17);
expect(frame.colorTexture.height, 19);
expect(frame.colorTexture.format, gpu.gpuContext.defaultColorFormat);
expect(frame.colorTexture.enableRenderTargetUsage, isTrue);
expect(frame.colorTexture.enableShaderReadUsage, isTrue);
expect(frame.colorTexture.isValid, isTrue);
expect(surface.debugBackingTextureCount, 1);
frame.discard();
expect(frame.colorTexture.isValid, isFalse);
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuImageSurface present updates currentImage', () async {
final gpu.GpuImageSurface surface = gpu.gpuContext.createImageSurface(17, 19);
final gpu.GpuImageSurfaceFrame frame = surface.acquireNextFrame();
final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer();
final gpu.GpuPresentStatus status = frame.present(commandBuffer);
expect(status, gpu.GpuPresentStatus.success);
expect(frame.colorTexture.isValid, isFalse);
final ui.Image? currentImage = surface.currentImage;
expect(currentImage, isNotNull);
expect(currentImage!.width, 17);
expect(currentImage.height, 19);
commandBuffer.submit();
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuImageSurface present must happen before command buffer submit', () async {
final gpu.GpuImageSurface surface = gpu.gpuContext.createImageSurface(17, 19);
final gpu.GpuImageSurfaceFrame frame = surface.acquireNextFrame();
final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer();
commandBuffer.submit();
try {
frame.present(commandBuffer);
fail('Exception not thrown for presenting after command buffer submit.');
} catch (e) {
expect(e.toString(), contains('SurfaceFrame.present must be called before submitting'));
}
frame.discard();
}, skip: !(impellerEnabled && flutterGpuEnabled));
test('GpuImageSurface cannot present or discard the same frame twice', () async {
final gpu.GpuImageSurface surface = gpu.gpuContext.createImageSurface(17, 19);
final gpu.GpuImageSurfaceFrame frame = surface.acquireNextFrame();
final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer();
frame.present(commandBuffer);
commandBuffer.submit();
expect(() => frame.present(gpu.gpuContext.createCommandBuffer()), throwsA(isA<StateError>()));
// Discarding an inactive frame is intentionally a no-op.
frame.discard();
}, skip: !(impellerEnabled && flutterGpuEnabled));
test(
'GpuImageSurface acquireNextFrame throws if the previous present was never submitted',
() async {
final gpu.GpuImageSurface surface = gpu.gpuContext.createImageSurface(17, 19);
final gpu.GpuImageSurfaceFrame frame = surface.acquireNextFrame();
final gpu.CommandBuffer commandBuffer = gpu.gpuContext.createCommandBuffer();
frame.present(commandBuffer);
// Intentionally skip commandBuffer.submit() to simulate the footgun.
expect(surface.acquireNextFrame, throwsA(isA<StateError>()));
// Submitting clears the pending state so acquisition can resume.