forked from firka/flutter
[flutter_tools] Add support for compiling shaders to JSON bundle for web (#114295)
This commit is contained in:
@@ -751,7 +751,8 @@ class CachedLocalEngineArtifacts implements LocalEngineArtifacts {
|
||||
_cache = cache,
|
||||
_processManager = processManager,
|
||||
_platform = platform,
|
||||
_operatingSystemUtils = operatingSystemUtils;
|
||||
_operatingSystemUtils = operatingSystemUtils,
|
||||
_backupCache = CachedArtifacts(fileSystem: fileSystem, platform: platform, cache: cache, operatingSystemUtils: operatingSystemUtils);
|
||||
|
||||
@override
|
||||
final String engineOutPath;
|
||||
@@ -765,7 +766,7 @@ class CachedLocalEngineArtifacts implements LocalEngineArtifacts {
|
||||
final ProcessManager _processManager;
|
||||
final Platform _platform;
|
||||
final OperatingSystemUtils _operatingSystemUtils;
|
||||
|
||||
final CachedArtifacts _backupCache;
|
||||
|
||||
@override
|
||||
FileSystemEntity getHostArtifact(HostArtifact artifact) {
|
||||
@@ -838,7 +839,11 @@ class CachedLocalEngineArtifacts implements LocalEngineArtifacts {
|
||||
case HostArtifact.impellerc:
|
||||
case HostArtifact.libtessellator:
|
||||
final String artifactFileName = _hostArtifactToFileName(artifact, _platform);
|
||||
return _fileSystem.file(_fileSystem.path.join(_hostEngineOutPath, artifactFileName));
|
||||
final File file = _fileSystem.file(_fileSystem.path.join(_hostEngineOutPath, artifactFileName));
|
||||
if (!file.existsSync()) {
|
||||
return _backupCache.getHostArtifact(artifact);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -409,11 +409,10 @@ class ManifestAssetBundle implements AssetBundle {
|
||||
final List<_Asset> materialAssets = <_Asset>[
|
||||
if (flutterManifest.usesMaterialDesign)
|
||||
..._getMaterialFonts(),
|
||||
// For non-web platforms, include the shaders unconditionally. They are
|
||||
// For all platforms, include the shaders unconditionally. They are
|
||||
// small, and whether they're used is determined only by the app source
|
||||
// code and not by the Flutter manifest.
|
||||
if (targetPlatform != TargetPlatform.web_javascript)
|
||||
..._getMaterialShaders(),
|
||||
..._getMaterialShaders(),
|
||||
];
|
||||
for (final _Asset asset in materialAssets) {
|
||||
final File assetFile = asset.lookupAssetFile(_fileSystem);
|
||||
@@ -754,23 +753,19 @@ class ManifestAssetBundle implements AssetBundle {
|
||||
}
|
||||
}
|
||||
|
||||
// No shader compilation for the web.
|
||||
if (targetPlatform != TargetPlatform.web_javascript) {
|
||||
for (final Uri shaderUri in flutterManifest.shaders) {
|
||||
_parseAssetFromFile(
|
||||
packageConfig,
|
||||
flutterManifest,
|
||||
assetBase,
|
||||
cache,
|
||||
result,
|
||||
shaderUri,
|
||||
packageName: packageName,
|
||||
attributedPackage: attributedPackage,
|
||||
assetKind: AssetKind.shader,
|
||||
);
|
||||
}
|
||||
for (final Uri shaderUri in flutterManifest.shaders) {
|
||||
_parseAssetFromFile(
|
||||
packageConfig,
|
||||
flutterManifest,
|
||||
assetBase,
|
||||
cache,
|
||||
result,
|
||||
shaderUri,
|
||||
packageName: packageName,
|
||||
attributedPackage: attributedPackage,
|
||||
assetKind: AssetKind.shader,
|
||||
);
|
||||
}
|
||||
|
||||
// Add assets referenced in the fonts section of the manifest.
|
||||
for (final Font font in flutterManifest.fonts) {
|
||||
for (final FontAsset fontAsset in font.fontAssets) {
|
||||
|
||||
@@ -128,6 +128,7 @@ Future<Depfile> copyAssets(
|
||||
input: content.file as File,
|
||||
outputPath: file.path,
|
||||
target: shaderTarget,
|
||||
json: targetPlatform == TargetPlatform.web_javascript,
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ class DevelopmentShaderCompiler {
|
||||
|
||||
late ShaderTarget _shaderTarget;
|
||||
bool _debugConfigured = false;
|
||||
bool _jsonMode = false;
|
||||
|
||||
/// Configure the output format of the shader compiler for a particular
|
||||
/// flutter device.
|
||||
@@ -69,9 +70,13 @@ class DevelopmentShaderCompiler {
|
||||
case TargetPlatform.fuchsia_arm64:
|
||||
case TargetPlatform.fuchsia_x64:
|
||||
case TargetPlatform.tester:
|
||||
assert(!enableImpeller);
|
||||
_shaderTarget = ShaderTarget.sksl;
|
||||
break;
|
||||
case TargetPlatform.web_javascript:
|
||||
assert(!enableImpeller);
|
||||
_shaderTarget = ShaderTarget.sksl;
|
||||
_jsonMode = true;
|
||||
break;
|
||||
case null:
|
||||
return;
|
||||
@@ -102,6 +107,7 @@ class DevelopmentShaderCompiler {
|
||||
outputPath: output.path,
|
||||
target: _shaderTarget,
|
||||
fatal: false,
|
||||
json: _jsonMode,
|
||||
);
|
||||
if (!success) {
|
||||
return null;
|
||||
@@ -157,6 +163,7 @@ class ShaderCompiler {
|
||||
required String outputPath,
|
||||
required ShaderTarget target,
|
||||
bool fatal = true,
|
||||
required bool json,
|
||||
}) async {
|
||||
final File impellerc = _fs.file(
|
||||
_artifacts.getHostArtifact(HostArtifact.impellerc),
|
||||
@@ -172,6 +179,8 @@ class ShaderCompiler {
|
||||
impellerc.path,
|
||||
target.target,
|
||||
'--iplr',
|
||||
if (json)
|
||||
'--json',
|
||||
'--sl=$outputPath',
|
||||
'--spirv=$outputPath.spirv',
|
||||
'--input=${input.path}',
|
||||
|
||||
@@ -134,9 +134,10 @@ Future<AssetBundle?> buildAssets({
|
||||
Future<void> writeBundle(
|
||||
Directory bundleDir,
|
||||
Map<String, DevFSContent> assetEntries,
|
||||
Map<String, AssetKind> entryKinds,
|
||||
{ Logger? loggerOverride }
|
||||
) async {
|
||||
Map<String, AssetKind> entryKinds, {
|
||||
Logger? loggerOverride,
|
||||
required TargetPlatform targetPlatform,
|
||||
}) async {
|
||||
loggerOverride ??= globals.logger;
|
||||
if (bundleDir.existsSync()) {
|
||||
try {
|
||||
@@ -185,6 +186,7 @@ Future<void> writeBundle(
|
||||
input: input,
|
||||
outputPath: file.path,
|
||||
target: ShaderTarget.sksl, // TODO(zanderso): configure impeller target when enabled.
|
||||
json: targetPlatform == TargetPlatform.web_javascript,
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -502,8 +502,12 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
|
||||
throwToolExit('Error: Failed to build asset bundle');
|
||||
}
|
||||
if (_needRebuild(assetBundle.entries)) {
|
||||
await writeBundle(globals.fs.directory(globals.fs.path.join('build', 'unit_test_assets')),
|
||||
assetBundle.entries, assetBundle.entryKinds);
|
||||
await writeBundle(
|
||||
globals.fs.directory(globals.fs.path.join('build', 'unit_test_assets')),
|
||||
assetBundle.entries,
|
||||
assetBundle.entryKinds,
|
||||
targetPlatform: TargetPlatform.tester,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -868,6 +868,7 @@ class WebDevFS implements DevFS {
|
||||
globals.fs.directory(getAssetBuildDirectory()),
|
||||
bundle.entries,
|
||||
bundle.entryKinds,
|
||||
targetPlatform: TargetPlatform.web_javascript,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user