StageInterface.fromGlsl constructor

StageInterface.fromGlsl(
  1. String glsl, {
  2. required String name,
  3. required bool fragment,
})

Reads the declarations of glsl — a stage's source with its #includes already resolved — as the engine's GLSL writes them: in and out at the top level, with or without a layout(location = N), and uniform Name { … } blocks. Comments are skipped; anything else (a function's own locals, a #define) is not a declaration here.

Implementation

factory StageInterface.fromGlsl(
  String glsl, {
  required String name,
  required bool fragment,
}) {
  final text = glsl
      .replaceAll(RegExp(r'/\*[\s\S]*?\*/'), ' ')
      .replaceAll(RegExp(r'//[^\n]*'), '');
  final inputs = <StageVariable>[];
  final outputs = <StageVariable>[];
  for (final match in _variable.allMatches(text)) {
    final location = match.group(1);
    final variable = StageVariable(
      match.group(4)!,
      match.group(3)!,
      location: location == null ? null : int.parse(location),
    );
    (match.group(2) == 'in' ? inputs : outputs).add(variable);
  }
  final blocks = <String, List<StageBlockMember>>{
    for (final match in _block.allMatches(text))
      match.group(1)!: <StageBlockMember>[
        for (final member in _member.allMatches(match.group(2)!))
          StageBlockMember(member.group(2)!, member.group(1)!),
      ],
  };
  return StageInterface(
    name: name,
    fragment: fragment,
    inputs: inputs,
    outputs: outputs,
    blocks: blocks,
  );
}