Skip to content

Commit dd20919

Browse files
authored
[flutter_tool] Build shaders as .iplr and use FragmentProgram.fromAsset for ink_sparkle (#108071)
1 parent cc62a5b commit dd20919

4 files changed

Lines changed: 30 additions & 19 deletions

File tree

packages/flutter/lib/src/material/ink_sparkle.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'dart:math' as math;
77
import 'dart:typed_data';
88
import 'dart:ui' as ui;
99

10-
import 'package:flutter/services.dart';
1110
import 'package:flutter/widgets.dart';
1211
import 'package:vector_math/vector_math_64.dart';
1312

@@ -522,16 +521,10 @@ class FragmentShaderManager {
522521
/// Creates an [FragmentShaderManager] with an [InkSparkle] effect.
523522
static Future<FragmentShaderManager> inkSparkle() async {
524523
final FragmentShaderManager manager = FragmentShaderManager._();
525-
await manager._compile();
524+
_program = ui.FragmentProgram.fromAsset('shaders/ink_sparkle.frag');
526525
return manager;
527526
}
528527

529-
/// Compiles the spir-v bytecode into a shader program.
530-
Future<void> _compile() async {
531-
final ByteData data = await rootBundle.load('shaders/ink_sparkle.frag');
532-
_program = await ui.FragmentProgram.compile(spirv: data.buffer);
533-
}
534-
535528
/// Creates a shader with the original program and optional uniforms.
536529
///
537530
/// A new shader must be made whenever the uniforms are updated.

packages/flutter_tools/lib/src/build_system/targets/shader_compiler.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:process/process.dart';
66

77
import '../../artifacts.dart';
8+
import '../../base/error_handling_io.dart';
89
import '../../base/file_system.dart';
910
import '../../base/io.dart';
1011
import '../../base/logger.dart';
@@ -64,8 +65,10 @@ class ShaderCompiler {
6465
// TODO(zanderso): When impeller is enabled, the correct flags for the
6566
// target backend will need to be passed.
6667
// https://github.com/flutter/flutter/issues/102853
67-
'--flutter-spirv',
68-
'--spirv=$outputPath',
68+
'--sksl',
69+
'--iplr',
70+
'--sl=$outputPath',
71+
'--spirv=$outputPath.spirv',
6972
'--input=${input.path}',
7073
'--input-type=frag',
7174
'--include=${input.parent.path}',
@@ -81,6 +84,7 @@ class ShaderCompiler {
8184
);
8285
}
8386

87+
ErrorHandlingFileSystem.deleteIfExists(_fs.file('$outputPath.spirv'));
8488
return true;
8589
}
8690
}

packages/flutter_tools/test/general.shard/asset_bundle_test.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,17 @@ flutter:
443443
FakeCommand(
444444
command: <String>[
445445
impellerc,
446-
'--flutter-spirv',
447-
'--spirv=$outputPath',
446+
'--sksl',
447+
'--iplr',
448+
'--sl=$outputPath',
449+
'--spirv=$outputPath.spirv',
448450
'--input=/$shaderPath',
449451
'--input-type=frag',
450452
'--include=/$assetsPath',
451453
],
452454
onRun: () {
453455
fileSystem.file(outputPath).createSync(recursive: true);
456+
fileSystem.file('$outputPath.spirv').createSync(recursive: true);
454457
},
455458
),
456459
]),

packages/flutter_tools/test/general.shard/build_system/targets/shader_compiler_test.dart

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import '../../../src/fake_process_manager.dart';
1313
const String fragDir = '/shaders';
1414
const String fragPath = '/shaders/my_shader.frag';
1515
const String notFragPath = '/shaders/not_a_frag.file';
16-
const String outputPath = '/output/shaders/my_shader.spv';
16+
const String outputSpirvPath = '/output/shaders/my_shader.frag.spirv';
17+
const String outputPath = '/output/shaders/my_shader.frag';
1718

1819
void main() {
1920
late BufferLogger logger;
@@ -37,14 +38,17 @@ void main() {
3738
FakeCommand(
3839
command: <String>[
3940
impellerc,
40-
'--flutter-spirv',
41-
'--spirv=$outputPath',
41+
'--sksl',
42+
'--iplr',
43+
'--sl=$outputPath',
44+
'--spirv=$outputSpirvPath',
4245
'--input=$fragPath',
4346
'--input-type=frag',
4447
'--include=$fragDir',
4548
],
4649
onRun: () {
4750
fileSystem.file(outputPath).createSync(recursive: true);
51+
fileSystem.file(outputSpirvPath).createSync(recursive: true);
4852
},
4953
),
5054
]);
@@ -63,21 +67,25 @@ void main() {
6367
true,
6468
);
6569
expect(fileSystem.file(outputPath).existsSync(), true);
70+
expect(fileSystem.file(outputSpirvPath).existsSync(), false);
6671
});
6772

6873
testWithoutContext('compileShader invokes impellerc for non-.frag files', () async {
6974
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
7075
FakeCommand(
7176
command: <String>[
7277
impellerc,
73-
'--flutter-spirv',
74-
'--spirv=$outputPath',
78+
'--sksl',
79+
'--iplr',
80+
'--sl=$outputPath',
81+
'--spirv=$outputSpirvPath',
7582
'--input=$notFragPath',
7683
'--input-type=frag',
7784
'--include=$fragDir',
7885
],
7986
onRun: () {
8087
fileSystem.file(outputPath).createSync(recursive: true);
88+
fileSystem.file(outputSpirvPath).createSync(recursive: true);
8189
},
8290
),
8391
]);
@@ -96,15 +104,18 @@ void main() {
96104
true,
97105
);
98106
expect(fileSystem.file(outputPath).existsSync(), true);
107+
expect(fileSystem.file(outputSpirvPath).existsSync(), false);
99108
});
100109

101110
testWithoutContext('compileShader throws an exception when impellerc fails', () async {
102111
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
103112
FakeCommand(
104113
command: <String>[
105114
impellerc,
106-
'--flutter-spirv',
107-
'--spirv=$outputPath',
115+
'--sksl',
116+
'--iplr',
117+
'--sl=$outputPath',
118+
'--spirv=$outputSpirvPath',
108119
'--input=$notFragPath',
109120
'--input-type=frag',
110121
'--include=$fragDir',

0 commit comments

Comments
 (0)