[flutter_tools] Add support for compiling shaders to JSON bundle for web (#114295)
This commit is contained in:
@@ -322,6 +322,13 @@ void main() {
|
||||
fileSystem.path.join('/out', 'host_debug_unopt', 'dart-sdk', 'bin',
|
||||
'snapshots', 'frontend_server.dart.snapshot')
|
||||
);
|
||||
|
||||
|
||||
fileSystem.file(fileSystem.path.join('/out', 'host_debug_unopt', 'impellerc'))
|
||||
.createSync(recursive: true);
|
||||
fileSystem.file(fileSystem.path.join('/out', 'host_debug_unopt', 'libtessellator.so'))
|
||||
.createSync(recursive: true);
|
||||
|
||||
expect(
|
||||
artifacts.getHostArtifact(HostArtifact.impellerc).path,
|
||||
fileSystem.path.join('/out', 'host_debug_unopt', 'impellerc'),
|
||||
@@ -332,6 +339,17 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
testWithoutContext('falls back to bundled impeller artifacts if the files do not exist in the local engine', () {
|
||||
expect(
|
||||
artifacts.getHostArtifact(HostArtifact.impellerc).path,
|
||||
fileSystem.path.join('root', 'bin', 'cache', 'artifacts', 'engine', 'linux-x64', 'impellerc'),
|
||||
);
|
||||
expect(
|
||||
artifacts.getHostArtifact(HostArtifact.libtessellator).path,
|
||||
fileSystem.path.join('root', 'bin', 'cache', 'artifacts', 'engine', 'linux-x64', 'libtessellator.so'),
|
||||
);
|
||||
});
|
||||
|
||||
testWithoutContext('falls back to prebuilt dart sdk', () {
|
||||
final String failureMessage = 'Unable to find a built dart sdk at:'
|
||||
' "${fileSystem.path.join('/out', 'host_debug_unopt', 'dart-sdk')}"'
|
||||
|
||||
@@ -313,6 +313,7 @@ flutter:
|
||||
<String, DevFSContent>{},
|
||||
<String, AssetKind>{},
|
||||
loggerOverride: testLogger,
|
||||
targetPlatform: TargetPlatform.android,
|
||||
);
|
||||
|
||||
expect(testLogger.warningText, contains('Expected Error Text'));
|
||||
@@ -434,6 +435,7 @@ flutter:
|
||||
bundle.entries,
|
||||
bundle.entryKinds,
|
||||
loggerOverride: testLogger,
|
||||
targetPlatform: TargetPlatform.android,
|
||||
);
|
||||
|
||||
}, overrides: <Type, Generator>{
|
||||
@@ -459,7 +461,7 @@ flutter:
|
||||
]),
|
||||
});
|
||||
|
||||
testUsingContext('Included shaders are not compiled for the web', () async {
|
||||
testUsingContext('Included shaders are compiled for the web', () async {
|
||||
fileSystem.file('.packages').createSync();
|
||||
fileSystem.file('pubspec.yaml')
|
||||
..createSync()
|
||||
@@ -478,18 +480,34 @@ flutter:
|
||||
bundle.entries,
|
||||
bundle.entryKinds,
|
||||
loggerOverride: testLogger,
|
||||
targetPlatform: TargetPlatform.web_javascript,
|
||||
);
|
||||
|
||||
}, overrides: <Type, Generator>{
|
||||
Artifacts: () => artifacts,
|
||||
FileSystem: () => fileSystem,
|
||||
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
|
||||
// No impeller commands are expected here because shader compilation is
|
||||
// not supposed to happen for the web.
|
||||
FakeCommand(
|
||||
command: <String>[
|
||||
impellerc,
|
||||
'--sksl',
|
||||
'--iplr',
|
||||
'--json',
|
||||
'--sl=$outputPath',
|
||||
'--spirv=$outputPath.spirv',
|
||||
'--input=/$shaderPath',
|
||||
'--input-type=frag',
|
||||
'--include=/$assetsPath',
|
||||
],
|
||||
onRun: () {
|
||||
fileSystem.file(outputPath).createSync(recursive: true);
|
||||
fileSystem.file('$outputPath.spirv').createSync(recursive: true);
|
||||
},
|
||||
),
|
||||
]),
|
||||
});
|
||||
|
||||
testUsingContext('Material shaders are not compiled for the web', () async {
|
||||
testUsingContext('Material shaders are compiled for the web', () async {
|
||||
fileSystem.file('.packages').createSync();
|
||||
|
||||
final String materialIconsPath = fileSystem.path.join(
|
||||
@@ -508,6 +526,25 @@ flutter:
|
||||
materialDir.childFile(shader).createSync(recursive: true);
|
||||
}
|
||||
|
||||
(globals.processManager as FakeProcessManager)
|
||||
.addCommand(FakeCommand(
|
||||
command: <String>[
|
||||
impellerc,
|
||||
'--sksl',
|
||||
'--iplr',
|
||||
'--json',
|
||||
'--sl=${fileSystem.path.join(output.path, 'shaders', 'ink_sparkle.frag')}',
|
||||
'--spirv=${fileSystem.path.join(output.path, 'shaders', 'ink_sparkle.frag.spirv')}',
|
||||
'--input=${fileSystem.path.join(materialDir.path, 'shaders', 'ink_sparkle.frag')}',
|
||||
'--input-type=frag',
|
||||
'--include=${fileSystem.path.join(materialDir.path, 'shaders')}',
|
||||
],
|
||||
onRun: () {
|
||||
fileSystem.file(outputPath).createSync(recursive: true);
|
||||
fileSystem.file('$outputPath.spirv').createSync(recursive: true);
|
||||
},
|
||||
));
|
||||
|
||||
fileSystem.file('pubspec.yaml')
|
||||
..createSync()
|
||||
..writeAsStringSync(r'''
|
||||
@@ -524,15 +561,13 @@ flutter:
|
||||
bundle.entries,
|
||||
bundle.entryKinds,
|
||||
loggerOverride: testLogger,
|
||||
targetPlatform: TargetPlatform.web_javascript,
|
||||
);
|
||||
|
||||
}, overrides: <Type, Generator>{
|
||||
Artifacts: () => artifacts,
|
||||
FileSystem: () => fileSystem,
|
||||
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
|
||||
// No impeller commands are expected here because shader compilation is
|
||||
// not supposed to happen for the web.
|
||||
]),
|
||||
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[]),
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@ void main() {
|
||||
input: fileSystem.file(fragPath),
|
||||
outputPath: outputPath,
|
||||
target: ShaderTarget.sksl,
|
||||
json: false,
|
||||
),
|
||||
true,
|
||||
);
|
||||
@@ -106,6 +107,7 @@ void main() {
|
||||
input: fileSystem.file(fragPath),
|
||||
outputPath: outputPath,
|
||||
target: ShaderTarget.impelleriOS,
|
||||
json: false,
|
||||
),
|
||||
true,
|
||||
);
|
||||
@@ -142,6 +144,7 @@ void main() {
|
||||
input: fileSystem.file(fragPath),
|
||||
outputPath: outputPath,
|
||||
target: ShaderTarget.impellerAndroid,
|
||||
json: false,
|
||||
),
|
||||
true,
|
||||
);
|
||||
@@ -179,6 +182,7 @@ void main() {
|
||||
input: fileSystem.file(notFragPath),
|
||||
outputPath: outputPath,
|
||||
target: ShaderTarget.sksl,
|
||||
json: false,
|
||||
),
|
||||
true,
|
||||
);
|
||||
@@ -216,6 +220,7 @@ void main() {
|
||||
input: fileSystem.file(notFragPath),
|
||||
outputPath: outputPath,
|
||||
target: ShaderTarget.sksl,
|
||||
json: false,
|
||||
);
|
||||
fail('unreachable');
|
||||
} on ShaderCompilerException catch (e) {
|
||||
@@ -313,4 +318,49 @@ void main() {
|
||||
expect(fileSystem.file('/.tmp_rand0/0.8255140718871702.temp.spirv'), isNot(exists));
|
||||
expect(fileSystem.file('/.tmp_rand0/0.8255140718871702.temp'), isNot(exists));
|
||||
});
|
||||
|
||||
testWithoutContext('DevelopmentShaderCompiler can compile JSON for web targts', () async {
|
||||
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
|
||||
FakeCommand(
|
||||
command: <String>[
|
||||
impellerc,
|
||||
'--sksl',
|
||||
'--iplr',
|
||||
'--json',
|
||||
'--sl=/.tmp_rand0/0.8255140718871702.temp',
|
||||
'--spirv=/.tmp_rand0/0.8255140718871702.temp.spirv',
|
||||
'--input=$fragPath',
|
||||
'--input-type=frag',
|
||||
'--include=$fragDir',
|
||||
],
|
||||
onRun: () {
|
||||
fileSystem.file('/.tmp_rand0/0.8255140718871702.temp.spirv').createSync();
|
||||
fileSystem.file('/.tmp_rand0/0.8255140718871702.temp')
|
||||
..createSync()
|
||||
..writeAsBytesSync(<int>[1, 2, 3, 4]);
|
||||
}
|
||||
),
|
||||
]);
|
||||
fileSystem.file(fragPath).writeAsBytesSync(<int>[1, 2, 3, 4]);
|
||||
final ShaderCompiler shaderCompiler = ShaderCompiler(
|
||||
processManager: processManager,
|
||||
logger: logger,
|
||||
fileSystem: fileSystem,
|
||||
artifacts: artifacts,
|
||||
);
|
||||
final DevelopmentShaderCompiler developmentShaderCompiler = DevelopmentShaderCompiler(
|
||||
shaderCompiler: shaderCompiler,
|
||||
fileSystem: fileSystem,
|
||||
random: math.Random(0),
|
||||
);
|
||||
|
||||
developmentShaderCompiler.configureCompiler(TargetPlatform.web_javascript, enableImpeller: false);
|
||||
|
||||
final DevFSContent? content = await developmentShaderCompiler
|
||||
.recompileShader(DevFSFileContent(fileSystem.file(fragPath)));
|
||||
|
||||
expect(await content!.contentsAsBytes(), <int>[1, 2, 3, 4]);
|
||||
expect(fileSystem.file('/.tmp_rand0/0.8255140718871702.temp.spirv'), isNot(exists));
|
||||
expect(fileSystem.file('/.tmp_rand0/0.8255140718871702.temp'), isNot(exists));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ void main() {
|
||||
input: globals.fs.file(inkSparklePath),
|
||||
outputPath: inkSparkleOutputPath,
|
||||
target: ShaderTarget.sksl,
|
||||
json: false,
|
||||
);
|
||||
final File resultFile = globals.fs.file(inkSparkleOutputPath);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user