Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b005a34

Browse files
authored
[flutter_tool] Pass --input-type to impellerc (#106845)
1 parent eb3cf24 commit b005a34

File tree

4 files changed

+77
-20
lines changed

4 files changed

+77
-20
lines changed

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

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

77
import '../../artifacts.dart';
8-
import '../../base/common.dart';
98
import '../../base/file_system.dart';
109
import '../../base/io.dart';
1110
import '../../base/logger.dart';
@@ -43,17 +42,13 @@ class ShaderCompiler {
4342
///
4443
/// All parameters are required.
4544
///
46-
/// If the input file is not a fragment shader, this returns false.
47-
/// If the shader compiler subprocess fails, it will [throwToolExit].
48-
/// Otherwise, it will return true.
45+
/// If the shader compiler subprocess fails, it will print the stdout and
46+
/// stderr to the log and throw a [ShaderCompilerException]. Otherwise, it
47+
/// will return true.
4948
Future<bool> compileShader({
5049
required File input,
5150
required String outputPath,
5251
}) async {
53-
if (!input.path.endsWith('.frag')) {
54-
return false;
55-
}
56-
5752
final File impellerc = _fs.file(
5853
_artifacts.getHostArtifact(HostArtifact.impellerc),
5954
);
@@ -72,6 +67,7 @@ class ShaderCompiler {
7267
'--flutter-spirv',
7368
'--spirv=$outputPath',
7469
'--input=${input.path}',
70+
'--input-type=frag',
7571
];
7672
final Process impellercProcess = await _processManager.start(cmd);
7773
final int code = await impellercProcess.exitCode;

packages/flutter_tools/lib/src/bundle_builder.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,25 @@ Future<void> writeBundle(
169169
// platform channels in the framework will URI encode these values,
170170
// and the native APIs will look for files this way.
171171
final File file = globals.fs.file(globals.fs.path.join(bundleDir.path, entry.key));
172+
final AssetKind assetKind = entryKinds[entry.key] ?? AssetKind.regular;
172173
file.parent.createSync(recursive: true);
173174
final DevFSContent devFSContent = entry.value;
174175
if (devFSContent is DevFSFileContent) {
175176
final File input = devFSContent.file as File;
176-
if (!await shaderCompiler.compileShader(input: input, outputPath: file.path)) {
177+
bool doCopy = true;
178+
switch (assetKind) {
179+
case AssetKind.regular:
180+
break;
181+
case AssetKind.font:
182+
break;
183+
case AssetKind.shader:
184+
doCopy = !await shaderCompiler.compileShader(
185+
input: input,
186+
outputPath: file.path,
187+
);
188+
break;
189+
}
190+
if (doCopy) {
177191
input.copySync(file.path);
178192
}
179193
} else {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ flutter:
444444
'--flutter-spirv',
445445
'--spirv=$outputPath',
446446
'--input=/$shaderPath',
447+
'--input-type=frag',
447448
],
448449
onRun: () {
449450
fileSystem.file(outputPath).createSync(recursive: true);

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

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import 'package:flutter_tools/src/build_system/targets/shader_compiler.dart';
1010
import '../../../src/common.dart';
1111
import '../../../src/fake_process_manager.dart';
1212

13-
const String shaderPath = '/shaders/my_shader.frag';
14-
const String notShaderPath = '/shaders/not_a_shader.file';
13+
const String fragPath = '/shaders/my_shader.frag';
14+
const String notFragPath = '/shaders/not_a_frag.file';
1515
const String outputPath = '/output/shaders/my_shader.spv';
1616

1717
void main() {
@@ -27,35 +27,51 @@ void main() {
2727
impellerc = artifacts.getHostArtifact(HostArtifact.impellerc).path;
2828

2929
fileSystem.file(impellerc).createSync(recursive: true);
30-
fileSystem.file(shaderPath).createSync(recursive: true);
31-
fileSystem.file(notShaderPath).createSync(recursive: true);
30+
fileSystem.file(fragPath).createSync(recursive: true);
31+
fileSystem.file(notFragPath).createSync(recursive: true);
3232
});
3333

34-
testWithoutContext('compileShader returns false for non-shader files', () async {
34+
testWithoutContext('compileShader invokes impellerc for .frag files', () async {
35+
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
36+
FakeCommand(
37+
command: <String>[
38+
impellerc,
39+
'--flutter-spirv',
40+
'--spirv=$outputPath',
41+
'--input=$fragPath',
42+
'--input-type=frag',
43+
],
44+
onRun: () {
45+
fileSystem.file(outputPath).createSync(recursive: true);
46+
},
47+
),
48+
]);
3549
final ShaderCompiler shaderCompiler = ShaderCompiler(
36-
processManager: FakeProcessManager.empty(),
50+
processManager: processManager,
3751
logger: logger,
3852
fileSystem: fileSystem,
3953
artifacts: artifacts,
4054
);
4155

4256
expect(
4357
await shaderCompiler.compileShader(
44-
input: fileSystem.file(notShaderPath),
58+
input: fileSystem.file(fragPath),
4559
outputPath: outputPath,
4660
),
47-
false,
61+
true,
4862
);
63+
expect(fileSystem.file(outputPath).existsSync(), true);
4964
});
5065

51-
testWithoutContext('compileShader returns true for shader files', () async {
66+
testWithoutContext('compileShader invokes impellerc for non-.frag files', () async {
5267
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
5368
FakeCommand(
5469
command: <String>[
5570
impellerc,
5671
'--flutter-spirv',
5772
'--spirv=$outputPath',
58-
'--input=$shaderPath',
73+
'--input=$notFragPath',
74+
'--input-type=frag',
5975
],
6076
onRun: () {
6177
fileSystem.file(outputPath).createSync(recursive: true);
@@ -71,11 +87,41 @@ void main() {
7187

7288
expect(
7389
await shaderCompiler.compileShader(
74-
input: fileSystem.file(shaderPath),
90+
input: fileSystem.file(notFragPath),
7591
outputPath: outputPath,
7692
),
7793
true,
7894
);
7995
expect(fileSystem.file(outputPath).existsSync(), true);
8096
});
97+
98+
testWithoutContext('compileShader throws an exception when impellerc fails', () async {
99+
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
100+
FakeCommand(
101+
command: <String>[
102+
impellerc,
103+
'--flutter-spirv',
104+
'--spirv=$outputPath',
105+
'--input=$notFragPath',
106+
'--input-type=frag',
107+
],
108+
exitCode: 1,
109+
),
110+
]);
111+
final ShaderCompiler shaderCompiler = ShaderCompiler(
112+
processManager: processManager,
113+
logger: logger,
114+
fileSystem: fileSystem,
115+
artifacts: artifacts,
116+
);
117+
118+
await expectLater(
119+
() => shaderCompiler.compileShader(
120+
input: fileSystem.file(notFragPath),
121+
outputPath: outputPath,
122+
),
123+
throwsA(isA<ShaderCompilerException>()),
124+
);
125+
expect(fileSystem.file(outputPath).existsSync(), false);
126+
});
81127
}

0 commit comments

Comments
 (0)