Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6e85c38

Browse files
committed
FragmentProgram constructed asynchronously
1 parent caf6d29 commit 6e85c38

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

lib/ui/painting.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3773,8 +3773,15 @@ class FragmentProgram extends NativeFieldWrapperClass1 {
37733773
/// well-specified. Because of this, it is reccommended to construct
37743774
/// `FragmentProgram` asynchronously, outside of a widget's `build`
37753775
/// method; this will minimize the chance of UI jank.
3776+
static Future<FragmentProgram> compile({
3777+
required ByteBuffer spirv,
3778+
bool debugPrint = false,
3779+
}) async {
3780+
return FragmentProgram._(spirv: spirv, debugPrint: debugPrint);
3781+
}
3782+
37763783
@pragma('vm:entry-point')
3777-
FragmentProgram({
3784+
FragmentProgram._({
37783785
required ByteBuffer spirv,
37793786
bool debugPrint = false,
37803787
}) {

lib/web_ui/lib/src/ui/painting.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,14 @@ class ImageDescriptor {
790790
}
791791

792792
class FragmentProgram {
793-
FragmentProgram({
793+
static Future<FragmentProgram> compile({
794+
required ByteBuffer spirv,
795+
bool debugPrint = false,
796+
}) async {
797+
throw UnsupportedError('FragmentProgram is not supported for the CanvasKit or HTML renderers.');
798+
}
799+
800+
FragmentProgram._({
794801
required ByteBuffer spirv, // ignore: avoid_unused_constructor_parameters
795802
bool debugPrint = false, // ignore: avoid_unused_constructor_parameters
796803
}) {

testing/dart/fragment_shader_test.dart

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ import 'package:path/path.dart' as path;
1414
import 'shader_test_file_utils.dart';
1515

1616
void main() {
17-
test('throws exception for invalid shader', () {
17+
test('throws exception for invalid shader', () async {
1818
final ByteBuffer invalidBytes = Uint8List.fromList(<int>[1, 2, 3, 4, 5]).buffer;
19-
expect(() => FragmentProgram(spirv: invalidBytes), throws);
19+
try {
20+
await FragmentProgram.compile(spirv: invalidBytes);
21+
fail('expected compile to throw an exception');
22+
} catch (_) {
23+
}
2024
});
2125

2226
test('simple shader renders correctly', () async {
2327
final Uint8List shaderBytes = await spvFile('general_shaders', 'functions.spv').readAsBytes();
24-
final FragmentProgram program = FragmentProgram(
28+
final FragmentProgram program = await FragmentProgram.compile(
2529
spirv: shaderBytes.buffer,
2630
);
2731
final Shader shader = program.shader(
@@ -30,9 +34,9 @@ void main() {
3034
_expectShaderRendersGreen(shader);
3135
});
3236

33-
test('shader with functions renders green', () {
37+
test('shader with functions renders green', () async {
3438
final ByteBuffer spirv = spvFile('general_shaders', 'functions.spv').readAsBytesSync().buffer;
35-
final FragmentProgram program = FragmentProgram(
39+
final FragmentProgram program = await FragmentProgram.compile(
3640
spirv: spirv,
3741
);
3842
final Shader shader = program.shader(
@@ -43,7 +47,7 @@ void main() {
4347

4448
test('shader with uniforms renders correctly', () async {
4549
final Uint8List shaderBytes = await spvFile('general_shaders', 'uniforms.spv').readAsBytes();
46-
final FragmentProgram program = FragmentProgram(spirv: shaderBytes.buffer);
50+
final FragmentProgram program = await FragmentProgram.compile(spirv: shaderBytes.buffer);
4751

4852
final Shader shader = program.shader(
4953
floatUniforms: Float32List.fromList(<double>[
@@ -80,10 +84,10 @@ void main() {
8084
_expectShadersRenderGreen(supportedOpShaders);
8185
_expectShadersHaveOp(supportedOpShaders, false /* glsl ops */);
8286

83-
test('equality depends on floatUniforms', () {
87+
test('equality depends on floatUniforms', () async {
8488
final ByteBuffer spirv = spvFile('general_shaders', 'simple.spv')
8589
.readAsBytesSync().buffer;
86-
final FragmentProgram program = FragmentProgram(spirv: spirv);
90+
final FragmentProgram program = await FragmentProgram.compile(spirv: spirv);
8791
final Float32List ones = Float32List.fromList(<double>[1]);
8892
final Float32List zeroes = Float32List.fromList(<double>[0]);
8993

@@ -102,13 +106,13 @@ void main() {
102106
}
103107
});
104108

105-
test('equality depends on spirv', () {
109+
test('equality depends on spirv', () async {
106110
final ByteBuffer spirvA = spvFile('general_shaders', 'simple.spv')
107111
.readAsBytesSync().buffer;
108112
final ByteBuffer spirvB = spvFile('general_shaders', 'uniforms.spv')
109113
.readAsBytesSync().buffer;
110-
final FragmentProgram programA = FragmentProgram(spirv: spirvA);
111-
final FragmentProgram programB = FragmentProgram(spirv: spirvB);
114+
final FragmentProgram programA = await FragmentProgram.compile(spirv: spirvA);
115+
final FragmentProgram programB = await FragmentProgram.compile(spirv: spirvB);
112116
final a = programA.shader();
113117
final b = programB.shader();
114118

@@ -122,8 +126,8 @@ void main() {
122126
// of the file name within the test case.
123127
void _expectShadersRenderGreen(Map<String, ByteBuffer> shaders) {
124128
for (final String key in shaders.keys) {
125-
test('$key renders green', () {
126-
final FragmentProgram program = FragmentProgram(
129+
test('$key renders green', () async {
130+
final FragmentProgram program = await FragmentProgram.compile(
127131
spirv: shaders[key]!,
128132
);
129133
final Shader shader = program.shader(

0 commit comments

Comments
 (0)