loadArcaneShaders function

Future<void> loadArcaneShaders(
  1. List<ArcaneShader> shaders
)

Implementation

Future<void> loadArcaneShaders(List<ArcaneShader> shaders) async {
  WidgetsFlutterBinding.ensureInitialized();
  _wallClock = PrecisionStopwatch.start();

  void _processTimings(List<FrameTiming> timings) {
    for (final frame in timings) {
      Duration totalFrameTime = frame.rasterDuration;
      double frameTime = totalFrameTime.inMicroseconds / 1000;
      double frameUsage = frameTime / targetShaderFrameTime;
      mRenderUsage = frameUsage;
      double tfps = cShaderFPS;

      if (mRenderUsage > 0.9) {
        double intensity = mRenderUsage - 0.9;
        tfps -= 1.2 * intensity;
        cShaderRS -= 0.001 * intensity;
      } else {
        double intensity = 0.9 - mRenderUsage;
        tfps += 0.1 * intensity;
        cShaderRS += 0.0003 * intensity;
      }

      cShaderRS = cShaderRS.clamp(mMinShaderRS, mMaxShaderRS);
      cShaderFPS = tfps.clamp(mMinShaderFPS, mMaxShaderFPS);
    }
  }

  SchedulerBinding.instance.addTimingsCallback(_processTimings);
  List<String> failures = [];
  List<Future> shaderFutures = [];
  PrecisionStopwatch p = PrecisionStopwatch.start();
  for (ArcaneShader shader in shaders) {
    Future<FragmentProgram> f = shader.programProvider().thenRun((i) {
      verbose(
          "Compiled shader: ${shader.name} to 0x${i.identityHash.toRadixString(16)}");
    }).catchError((e, es) {
      error("Failed to compile shader: ${shader.name}");
      error(es);
      failures.add(shader.name);
    });
    shaderFutures.add(f);
    arcaneShaderPrograms[shader.name] = f;
    arcaneShaderRegistry[shader.name] = shader;
  }

  try {
    await Future.wait(shaderFutures);
  } catch (e) {
    error("Failed to compile shaders: $e");
  }
  if (failures.isNotEmpty) {
    warningAnnounce(
        "Arcane failed to compile ${failures.length} ${failures.length.plural("Shader")}\n${failures.join("\n")}\n\nCheck tome.arcane.art setup page for more information.");

    info("""
Did you forget to add the shaders to the pubspec.yaml?

@(#FFcfc7b6)&(#FF24180d)flutter:
  shaders:
${failures.map((i) => "    - &(#FF106b00)packages/arcane/resources/shaders/${i}.frag").join("&(#FF24180d)\n")}
&r
    """
        .chatColor);
  }
  actioned(
      "Compiled ${shaders.length - failures.length} shaders in ${p.getMilliseconds()}ms");
}