loadBaseShaderLibrary function Assets and loading

Future<void> loadBaseShaderLibrary()

Asynchronously loads and caches the base shader bundle. Idempotent. Called by Scene.initializeStaticResources so the synchronous baseShaderLibrary getter has a cached library to return (shader assets can't be read synchronously on any backend).

Implementation

Future<void> loadBaseShaderLibrary() async {
  if (_baseShaderLibrary != null) {
    return;
  }
  final key = await resolveBaseShaderBundleKey();
  if (key == null) {
    final built = (await loadGeneratedAssetIndex()).targetsOf(
      GeneratedAssetFamily.shaderBundle,
      'base',
      package: 'flutter_scene',
    );
    throw Exception(
      built.isEmpty
          ? baseShaderBundleMissingMessage
          : baseShaderBundleWrongTargetMessage(built),
    );
  }
  final lib = await gpu.loadShaderLibraryAsync(key);
  if (lib == null) {
    throw Exception(baseShaderBundleLoadFailureMessage(key));
  }
  // A bundle the engine cannot unpack still loads; every lookup in it just
  // returns null, which without this check leaves the scene "ready" and drawing
  // nothing. Probe one shader that is always present.
  if (lib[baseShaderBundleProbeName] == null) {
    throw Exception(baseShaderBundleUnusableMessage(key));
  }
  _baseShaderLibrary = lib;
}