Skip to content

Commit

Permalink
When Impeller is enabled for flutter tester choose correct shader tar…
Browse files Browse the repository at this point in the history
…get. (#141391)

When compiling shaders for flutter tester, include Vulkan shaders when targeting Impeller.
  • Loading branch information
jonahwilliams authored Jan 12, 2024
1 parent 2da87e6 commit f2745e9
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import '../build_system.dart';
enum ShaderTarget {
impellerAndroid(<String>['--runtime-stage-gles', '--runtime-stage-vulkan']),
impelleriOS(<String>['--runtime-stage-metal']),
impellerSwiftShader(<String>['--runtime-stage-vulkan']),
sksl(<String>['--sksl']);

const ShaderTarget(this.stages);
Expand Down Expand Up @@ -71,8 +72,9 @@ class DevelopmentShaderCompiler {
case TargetPlatform.fuchsia_arm64:
case TargetPlatform.fuchsia_x64:
case TargetPlatform.tester:
assert(impellerStatus != ImpellerStatus.enabled);
_shaderTarget = ShaderTarget.sksl;
_shaderTarget = impellerStatus == ImpellerStatus.enabled
? ShaderTarget.impellerSwiftShader
: ShaderTarget.sksl;
case TargetPlatform.web_javascript:
assert(impellerStatus != ImpellerStatus.enabled);
_shaderTarget = ShaderTarget.sksl;
Expand Down
9 changes: 8 additions & 1 deletion packages/flutter_tools/lib/src/bundle_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'build_system/targets/shader_compiler.dart';
import 'bundle.dart';
import 'cache.dart';
import 'devfs.dart';
import 'device.dart';
import 'globals.dart' as globals;
import 'project.dart';

Expand Down Expand Up @@ -140,6 +141,7 @@ Future<void> writeBundle(
Map<String, AssetKind> entryKinds, {
Logger? loggerOverride,
required TargetPlatform targetPlatform,
required ImpellerStatus impellerStatus,
}) async {
loggerOverride ??= globals.logger;
if (bundleDir.existsSync()) {
Expand Down Expand Up @@ -168,6 +170,11 @@ Future<void> writeBundle(
artifacts: globals.artifacts!,
);

ShaderTarget shaderTarget = ShaderTarget.sksl;
if (targetPlatform == TargetPlatform.tester && impellerStatus == ImpellerStatus.enabled) {
shaderTarget = ShaderTarget.impellerSwiftShader;
}

// Limit number of open files to avoid running out of file descriptors.
final Pool pool = Pool(64);
await Future.wait<void>(
Expand Down Expand Up @@ -195,7 +202,7 @@ Future<void> writeBundle(
doCopy = !await shaderCompiler.compileShader(
input: input,
outputPath: file.path,
target: ShaderTarget.sksl, // TODO(zanderso): configure impeller target when enabled.
target: shaderTarget,
json: targetPlatform == TargetPlatform.web_javascript,
);
case AssetKind.model:
Expand Down
42 changes: 22 additions & 20 deletions packages/flutter_tools/lib/src/commands/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,6 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
);
}

String? testAssetDirectory;
if (buildTestAssets) {
await _buildTestAsset(flavor: buildInfo.flavor);
testAssetDirectory = globals.fs.path.
join(flutterProject.directory.path, 'build', 'unit_test_assets');
}

final bool startPaused = boolArg('start-paused');
if (startPaused && _testFileUris.length != 1) {
throwToolExit(
Expand All @@ -360,6 +353,26 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
);
}

final DebuggingOptions debuggingOptions = DebuggingOptions.enabled(
buildInfo,
startPaused: startPaused,
disableServiceAuthCodes: boolArg('disable-service-auth-codes'),
serveObservatory: boolArg('serve-observatory'),
// On iOS >=14, keeping this enabled will leave a prompt on the screen.
disablePortPublication: true,
enableDds: enableDds,
nullAssertions: boolArg(FlutterOptions.kNullAssertions),
usingCISystem: usingCISystem,
enableImpeller: ImpellerStatus.fromBool(argResults!['enable-impeller'] as bool?),
);

String? testAssetDirectory;
if (buildTestAssets) {
await _buildTestAsset(flavor: buildInfo.flavor, impellerStatus: debuggingOptions.enableImpeller);
testAssetDirectory = globals.fs.path.
join(flutterProject.directory.path, 'build', 'unit_test_assets');
}

final String? concurrencyString = stringArg('concurrency');
int? jobs = concurrencyString == null ? null : int.tryParse(concurrencyString);
if (jobs != null && (jobs <= 0 || !jobs.isFinite)) {
Expand Down Expand Up @@ -427,19 +440,6 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
watcher = collector;
}

final DebuggingOptions debuggingOptions = DebuggingOptions.enabled(
buildInfo,
startPaused: startPaused,
disableServiceAuthCodes: boolArg('disable-service-auth-codes'),
serveObservatory: boolArg('serve-observatory'),
// On iOS >=14, keeping this enabled will leave a prompt on the screen.
disablePortPublication: true,
enableDds: enableDds,
nullAssertions: boolArg(FlutterOptions.kNullAssertions),
usingCISystem: usingCISystem,
enableImpeller: ImpellerStatus.fromBool(argResults!['enable-impeller'] as bool?),
);

Device? integrationTestDevice;
if (_isIntegrationTest) {
integrationTestDevice = await findTargetDevice();
Expand Down Expand Up @@ -569,6 +569,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {

Future<void> _buildTestAsset({
required String? flavor,
required ImpellerStatus impellerStatus,
}) async {
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
final int build = await assetBundle.build(
Expand All @@ -584,6 +585,7 @@ class TestCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
assetBundle.entries,
assetBundle.entryKinds,
targetPlatform: TargetPlatform.tester,
impellerStatus: impellerStatus,
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter_tools/lib/src/isolated/devfs_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import '../compile.dart';
import '../convert.dart';
import '../dart/package_map.dart';
import '../devfs.dart';
import '../device.dart';
import '../globals.dart' as globals;
import '../html_utils.dart';
import '../project.dart';
Expand Down Expand Up @@ -885,6 +886,7 @@ class WebDevFS implements DevFS {
bundle.entries,
bundle.entryKinds,
targetPlatform: TargetPlatform.web_javascript,
impellerStatus: ImpellerStatus.disabled,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/bundle_builder.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/devfs.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/project.dart';
import 'package:standard_message_codec/standard_message_codec.dart';
Expand Down Expand Up @@ -622,6 +623,7 @@ flutter:
<String, AssetKind>{},
loggerOverride: testLogger,
targetPlatform: TargetPlatform.android,
impellerStatus: ImpellerStatus.disabled,
);

expect(testLogger.warningText, contains('Expected Error Text'));
Expand Down Expand Up @@ -744,6 +746,7 @@ flutter:
bundle.entryKinds,
loggerOverride: testLogger,
targetPlatform: TargetPlatform.android,
impellerStatus: ImpellerStatus.disabled,
);

}, overrides: <Type, Generator>{
Expand Down Expand Up @@ -790,6 +793,7 @@ flutter:
bundle.entryKinds,
loggerOverride: testLogger,
targetPlatform: TargetPlatform.web_javascript,
impellerStatus: ImpellerStatus.disabled,
);

}, overrides: <Type, Generator>{
Expand Down Expand Up @@ -873,6 +877,7 @@ flutter:
bundle.entryKinds,
loggerOverride: testLogger,
targetPlatform: TargetPlatform.web_javascript,
impellerStatus: ImpellerStatus.disabled,
);
expect((globals.processManager as FakeProcessManager).hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,53 @@ void main() {
expect(fileSystem.file('/.tmp_rand0/0.8255140718871702.temp'), isNot(exists));
});

testWithoutContext('DevelopmentShaderCompiler can compile for Flutter Tester with Impeller and Vulkan', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
FakeCommand(
command: <String>[
impellerc,
'--runtime-stage-vulkan',
'--iplr',
'--sl=/.tmp_rand0/0.8255140718871702.temp',
'--spirv=/.tmp_rand0/0.8255140718871702.temp.spirv',
'--input=$fragPath',
'--input-type=frag',
'--include=$fragDir',
'--include=$shaderLibDir',
],
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.tester,
impellerStatus: ImpellerStatus.enabled,
);

final DevFSContent? content = await developmentShaderCompiler
.recompileShader(DevFSFileContent(fileSystem.file(fragPath)));

expect(await content!.contentsAsBytes(), <int>[1, 2, 3, 4]);
expect(processManager.hasRemainingExpectations, false);
});

testWithoutContext('DevelopmentShaderCompiler can compile for android with impeller', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
FakeCommand(
Expand Down

0 comments on commit f2745e9

Please sign in to comment.