shader_compiler_test.dart 4.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/build_system/targets/shader_compiler.dart';
import 'package:flutter_tools/src/globals.dart' as globals;

import '../src/common.dart';
import '../src/context.dart';

void main() {
  late BufferLogger logger;

  setUp(() {
    logger = BufferLogger.test();
  });

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  Future<void> testCompileShader(String source) async {
    final Directory tmpDir = globals.fs.systemTempDirectory.createTempSync(
      'shader_compiler_test.',
    );
    final File file = tmpDir.childFile('test_shader.frag')
      ..writeAsStringSync(source);
    final ShaderCompiler shaderCompiler = ShaderCompiler(
      processManager: globals.processManager,
      logger: logger,
      fileSystem: globals.fs,
      artifacts: globals.artifacts!,
    );
    await shaderCompiler.compileShader(
      input: file,
      outputPath: tmpDir.childFile('test_shader.frag.out').path,
      target: ShaderTarget.sksl,
      json: false,
    );
  }

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  testUsingContext('impellerc .iplr output has correct permissions', () async {
    if (globals.platform.isWindows) {
      return;
    }

    final String flutterRoot = getFlutterRoot();
    final String inkSparklePath = globals.fs.path.join(flutterRoot,
      'packages', 'flutter', 'lib', 'src', 'material', 'shaders',
      'ink_sparkle.frag');
    final Directory tmpDir = globals.fs.systemTempDirectory.createTempSync(
      'shader_compiler_test.',
    );
    final String inkSparkleOutputPath = globals.fs.path.join(
      tmpDir.path, 'ink_sparkle.frag',
    );

    final ShaderCompiler shaderCompiler = ShaderCompiler(
      processManager: globals.processManager,
      logger: logger,
      fileSystem: globals.fs,
      artifacts: globals.artifacts!,
    );
    final bool compileResult = await shaderCompiler.compileShader(
      input: globals.fs.file(inkSparklePath),
      outputPath: inkSparkleOutputPath,
      target: ShaderTarget.sksl,
66
      json: false,
67 68 69 70 71 72 73 74 75 76
    );
    final File resultFile = globals.fs.file(inkSparkleOutputPath);


    expect(compileResult, true);
    expect(resultFile.existsSync(), true);

    final int expectedMode = int.parse('644', radix: 8);
    expect(resultFile.statSync().mode & expectedMode, equals(expectedMode));
  });
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

  testUsingContext('Compilation error with in storage', () async {
    const String kShaderWithInput = '''
in float foo;

out vec4 fragColor;

void main() {
  fragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
''';

    expect(() => testCompileShader(kShaderWithInput), throwsA(isA<ShaderCompilerException>()
      .having(
        (ShaderCompilerException exception) => exception.message,
        'message',
        contains('SkSL does not support inputs'),
      ),
    ));
  });

  testUsingContext('Compilation error with UBO', () async {
    const String kShaderWithInput = '''
uniform Data {
  vec4 foo;
} data;

out vec4 fragColor;

void main() {
  fragColor = data.foo;
}
''';

    expect(() => testCompileShader(kShaderWithInput), throwsA(isA<ShaderCompilerException>()
      .having(
        (ShaderCompilerException exception) => exception.message,
        'message',
        contains('SkSL does not support UBOs or SSBOs'),
      ),
    ));
  });

  testUsingContext('Compilation error with texture arguments besides position or sampler', () async {
    const String kShaderWithInput = '''
uniform sampler2D tex;

out vec4 fragColor;

void main() {
  fragColor = texture(tex, vec2(0.5, 0.3), 0.5);
}
''';

    expect(() => testCompileShader(kShaderWithInput), throwsA(isA<ShaderCompilerException>()
      .having(
        (ShaderCompilerException exception) => exception.message,
        'message',
        contains('Only sampler and position arguments are supported in texture() calls'),
      ),
    ));
  });
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

  testUsingContext('Compilation error with uint8 uniforms', () async {
    const String kShaderWithInput = '''
#version 310 es

layout(location = 0) uniform uint foo;
layout(location = 0) out vec4 fragColor;

void main() {}
''';

    expect(() => testCompileShader(kShaderWithInput), throwsA(isA<ShaderCompilerException>()
      .having(
        (ShaderCompilerException exception) => exception.message,
        'message',
        contains('SkSL does not support unsigned integers'),
      ),
    ));
  });
158
}