diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index a8d099d042..ae0f294bb3 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -331,6 +331,7 @@ std::shared_ptr Reflector::GenerateRuntimeStageData() const { uniform_description.type = spir_type.basetype; uniform_description.rows = spir_type.vecsize; uniform_description.columns = spir_type.columns; + uniform_description.bit_width = spir_type.width; data->AddUniformDescription(std::move(uniform_description)); }); return data; diff --git a/engine/src/flutter/impeller/compiler/runtime_stage_data.cc b/engine/src/flutter/impeller/compiler/runtime_stage_data.cc index f78fbc2fc3..af4c611185 100644 --- a/engine/src/flutter/impeller/compiler/runtime_stage_data.cc +++ b/engine/src/flutter/impeller/compiler/runtime_stage_data.cc @@ -55,11 +55,12 @@ static std::optional ToTargetPlatform( case TargetPlatform::kMetalDesktop: case TargetPlatform::kMetalIOS: case TargetPlatform::kFlutterSPIRV: - case TargetPlatform::kSkSL: case TargetPlatform::kOpenGLES: case TargetPlatform::kOpenGLDesktop: case TargetPlatform::kVulkan: return std::nullopt; + case TargetPlatform::kSkSL: + return fb::TargetPlatform::kSkSL; case TargetPlatform::kRuntimeStageMetal: return fb::TargetPlatform::kMetal; case TargetPlatform::kRuntimeStageGLES: @@ -155,6 +156,7 @@ std::shared_ptr RuntimeStageData::CreateMapping() const { return nullptr; } desc->type = uniform_type.value(); + desc->bit_width = uniform.bit_width; runtime_stage.uniforms.emplace_back(std::move(desc)); } diff --git a/engine/src/flutter/impeller/compiler/runtime_stage_data.h b/engine/src/flutter/impeller/compiler/runtime_stage_data.h index 056a5ef8ed..b0022a1402 100644 --- a/engine/src/flutter/impeller/compiler/runtime_stage_data.h +++ b/engine/src/flutter/impeller/compiler/runtime_stage_data.h @@ -21,6 +21,7 @@ struct UniformDescription { spirv_cross::SPIRType::BaseType type = spirv_cross::SPIRType::BaseType::Float; size_t rows = 0u; size_t columns = 0u; + size_t bit_width = 0u; }; class RuntimeStageData { diff --git a/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc b/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc index 31f8a00cfb..05571fdcee 100644 --- a/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc +++ b/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc @@ -85,7 +85,9 @@ RuntimeStage::RuntimeStage(std::shared_ptr payload) desc.name = i->name()->str(); desc.location = i->location(); desc.type = ToType(i->type()); - desc.dimensions = RuntimeUniformDimensions{i->rows(), i->columns()}; + desc.dimensions = RuntimeUniformDimensions{ + static_cast(i->rows()), static_cast(i->columns())}; + desc.bit_width = i->bit_width(); uniforms_.emplace_back(std::move(desc)); } diff --git a/engine/src/flutter/impeller/runtime_stage/runtime_stage.fbs b/engine/src/flutter/impeller/runtime_stage/runtime_stage.fbs index f4ff7a7f89..1391fc565a 100644 --- a/engine/src/flutter/impeller/runtime_stage/runtime_stage.fbs +++ b/engine/src/flutter/impeller/runtime_stage/runtime_stage.fbs @@ -15,6 +15,7 @@ enum Stage:byte { enum TargetPlatform:byte { kMetal, kOpenGLES, + kSkSL, } enum UniformDataType:uint32 { @@ -37,6 +38,7 @@ table UniformDescription { name: string; location: uint64; type: UniformDataType; + bit_width: uint64; rows: uint64; columns: uint64; } diff --git a/engine/src/flutter/impeller/runtime_stage/runtime_stage.h b/engine/src/flutter/impeller/runtime_stage/runtime_stage.h index bdfabf462c..4f8f9b8148 100644 --- a/engine/src/flutter/impeller/runtime_stage/runtime_stage.h +++ b/engine/src/flutter/impeller/runtime_stage/runtime_stage.h @@ -40,8 +40,11 @@ struct RuntimeUniformDescription { size_t location = 0u; RuntimeUniformType type = kFloat; RuntimeUniformDimensions dimensions; + size_t bit_width; }; +size_t SizeOfRuntimeUniformType(RuntimeUniformType type); + class RuntimeStage { public: RuntimeStage(std::shared_ptr payload); diff --git a/engine/src/flutter/lib/spirv/test/BUILD.gn b/engine/src/flutter/lib/spirv/test/BUILD.gn index 667e696c31..e9aaedb4c5 100644 --- a/engine/src/flutter/lib/spirv/test/BUILD.gn +++ b/engine/src/flutter/lib/spirv/test/BUILD.gn @@ -21,7 +21,9 @@ if (enable_unittests) { } group("test") { + testonly = true deps = [ + ":fixtures", ":sksl_ink_sparkle", ":spirv_ink_sparkle", "//flutter/lib/spirv/test/exception_shaders:spirv_compile_exception_shaders", @@ -43,4 +45,17 @@ if (enable_unittests) { intermediates_subdir = "sksl" sl_file_extension = "sksl" } + + impellerc("iplr_ink_sparkle") { + shaders = [ "//flutter/impeller/fixtures/ink_sparkle.frag" ] + shader_target_flag = "--sksl" + intermediates_subdir = "iplr" + sl_file_extension = "iplr" + } + + test_fixtures("fixtures") { + deps = [ ":iplr_ink_sparkle" ] + fixtures = get_target_outputs(":iplr_ink_sparkle") + dest = "$root_gen_dir/flutter/lib/ui" + } } diff --git a/engine/src/flutter/lib/spirv/test/general_shaders/BUILD.gn b/engine/src/flutter/lib/spirv/test/general_shaders/BUILD.gn index 2c53b4ee71..596856fac8 100644 --- a/engine/src/flutter/lib/spirv/test/general_shaders/BUILD.gn +++ b/engine/src/flutter/lib/spirv/test/general_shaders/BUILD.gn @@ -17,7 +17,9 @@ if (enable_unittests) { ] group("general_shaders") { + testonly = true deps = [ + ":fixtures", ":sksl_compile_general_shaders", ":spirv_compile_general_shaders", ] @@ -35,4 +37,17 @@ if (enable_unittests) { intermediates_subdir = "sksl" sl_file_extension = "sksl" } + + impellerc("sksl_compile_general_shaders_iplr") { + shaders = test_shaders + shader_target_flag = "--sksl" + intermediates_subdir = "iplr" + sl_file_extension = "iplr" + } + + test_fixtures("fixtures") { + deps = [ ":sksl_compile_general_shaders_iplr" ] + fixtures = get_target_outputs(":sksl_compile_general_shaders_iplr") + dest = "$root_gen_dir/flutter/lib/ui" + } } diff --git a/engine/src/flutter/lib/spirv/test/supported_glsl_op_shaders/BUILD.gn b/engine/src/flutter/lib/spirv/test/supported_glsl_op_shaders/BUILD.gn index 3f8b442af6..bd7b16d3f5 100644 --- a/engine/src/flutter/lib/spirv/test/supported_glsl_op_shaders/BUILD.gn +++ b/engine/src/flutter/lib/spirv/test/supported_glsl_op_shaders/BUILD.gn @@ -44,7 +44,9 @@ if (enable_unittests) { ] group("supported_glsl_op_shaders") { + testonly = true deps = [ + ":fixtures", ":sksl_compile_supported_glsl_shaders", ":spirv_compile_supported_glsl_shaders", ] @@ -62,4 +64,17 @@ if (enable_unittests) { intermediates_subdir = "sksl" sl_file_extension = "sksl" } + + impellerc("iplr_compile_supported_glsl_shaders") { + shaders = test_shaders + shader_target_flag = "--sksl" + intermediates_subdir = "iplr" + sl_file_extension = "iplr" + } + + test_fixtures("fixtures") { + deps = [ ":iplr_compile_supported_glsl_shaders" ] + fixtures = get_target_outputs(":iplr_compile_supported_glsl_shaders") + dest = "$root_gen_dir/flutter/lib/ui" + } } diff --git a/engine/src/flutter/lib/spirv/test/supported_op_shaders/BUILD.gn b/engine/src/flutter/lib/spirv/test/supported_op_shaders/BUILD.gn index 13e3ba55fc..bf23954143 100644 --- a/engine/src/flutter/lib/spirv/test/supported_op_shaders/BUILD.gn +++ b/engine/src/flutter/lib/spirv/test/supported_op_shaders/BUILD.gn @@ -40,7 +40,9 @@ if (enable_unittests) { ] group("supported_op_shaders") { + testonly = true deps = [ + ":fixtures", ":sksl_compile_supported_op_shaders", ":spirv_compile_supported_op_shaders", ] @@ -58,4 +60,17 @@ if (enable_unittests) { intermediates_subdir = "sksl" sl_file_extension = "sksl" } + + impellerc("iplr_compile_supported_op_shaders") { + shaders = test_shaders + shader_target_flag = "--sksl" + intermediates_subdir = "iplr" + sl_file_extension = "iplr" + } + + test_fixtures("fixtures") { + deps = [ ":iplr_compile_supported_op_shaders" ] + fixtures = get_target_outputs(":iplr_compile_supported_op_shaders") + dest = "$root_gen_dir/flutter/lib/ui" + } } diff --git a/engine/src/flutter/lib/ui/BUILD.gn b/engine/src/flutter/lib/ui/BUILD.gn index 80b4f773f9..e5ca4e36cb 100644 --- a/engine/src/flutter/lib/ui/BUILD.gn +++ b/engine/src/flutter/lib/ui/BUILD.gn @@ -148,6 +148,7 @@ source_set("ui") { "//flutter/common", "//flutter/display_list", "//flutter/fml", + "//flutter/impeller/runtime_stage", "//flutter/runtime:dart_plugin_registrant", "//flutter/runtime:test_font", "//flutter/third_party/tonic", diff --git a/engine/src/flutter/lib/ui/dart_ui.cc b/engine/src/flutter/lib/ui/dart_ui.cc index 33215756ad..818e99a618 100644 --- a/engine/src/flutter/lib/ui/dart_ui.cc +++ b/engine/src/flutter/lib/ui/dart_ui.cc @@ -162,6 +162,7 @@ typedef CanvasPath Path; V(ColorFilter, initSrgbToLinearGamma, 1) \ V(EngineLayer, dispose, 1) \ V(FragmentProgram, init, 3) \ + V(FragmentProgram, initFromAsset, 2) \ V(FragmentProgram, shader, 4) \ V(Gradient, initLinear, 6) \ V(Gradient, initRadial, 8) \ diff --git a/engine/src/flutter/lib/ui/natives.dart b/engine/src/flutter/lib/ui/natives.dart index a38c9a19b4..5c0ca3a7aa 100644 --- a/engine/src/flutter/lib/ui/natives.dart +++ b/engine/src/flutter/lib/ui/natives.dart @@ -47,9 +47,9 @@ const Endian _kFakeHostEndian = Endian.little; // A service protocol extension to schedule a frame to be rendered into the // window. Future _scheduleFrame( - String method, - Map parameters - ) async { + String method, + Map parameters, +) async { // Schedule the frame. PlatformDispatcher.instance.scheduleFrame(); // Always succeed. @@ -58,11 +58,32 @@ Future _scheduleFrame( })); } +Future _reinitializeShader( + String method, + Map parameters, +) async { + final String? assetKey = parameters['assetKey']; + if (assetKey != null) { + FragmentProgram._reinitializeShader(assetKey); + } + + // Always succeed. + return developer.ServiceExtensionResponse.result(json.encode({ + 'type': 'Success', + })); +} + @pragma('vm:entry-point') void _setupHooks() { assert(() { // In debug mode, register the schedule frame extension. developer.registerExtension('ext.ui.window.scheduleFrame', _scheduleFrame); + + // In debug mode, allow shaders to be reinitialized. + developer.registerExtension( + 'ext.ui.window.reinitializeShader', + _reinitializeShader, + ); return true; }()); } diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index b2cc49befb..03f08b2030 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -4079,6 +4079,26 @@ class FragmentProgram extends NativeFieldWrapperClass1 { )); } + static Map> _shaderRegistry = + >{}; + + // TODO(zra): Document custom shaders on the website and add a link to it + // here. https://github.com/flutter/flutter/issues/107929. + /// Creates a fragment program from the asset with key [assetKey]. + /// + /// The asset must be a file produced as the output of the `impellerc` + /// compiler. The constructed object should then be reused via the [shader] + /// method to create [Shader] objects that can be used by [Shader.paint]. + static FragmentProgram fromAsset(String assetKey) { + FragmentProgram? program = _shaderRegistry[assetKey]?.target; + if (program == null) { + program = FragmentProgram._fromAsset(assetKey); + _shaderRegistry[assetKey] = WeakReference(program); + } + + return program; + } + @pragma('vm:entry-point') FragmentProgram._({ ByteBuffer? spirv, @@ -4103,6 +4123,31 @@ class FragmentProgram extends NativeFieldWrapperClass1 { } } + @pragma('vm:entry-point') + FragmentProgram._fromAsset(String assetKey) { + _constructor(); + _initFromAsset(assetKey); + } + + static void _reinitializeShader(String assetKey) { + // If a shader for the assent isn't already registered, then there's no + // need to reinitialize it. The new shader will be loaded and initialized + // the next time the program access it. + final WeakReference? programRef = _shaderRegistry == null + ? null + : _shaderRegistry[assetKey]; + if (programRef == null) { + return; + } + + final FragmentProgram? program = programRef.target; + if (program == null) { + return; + } + + program._initFromAsset(assetKey); + } + late final int _uniformFloatCount; late final int _samplerCount; @@ -4112,6 +4157,9 @@ class FragmentProgram extends NativeFieldWrapperClass1 { @FfiNative, Handle, Bool)>('FragmentProgram::init') external void _init(String sksl, bool debugPrint); + @FfiNative, Handle)>('FragmentProgram::initFromAsset') + external void _initFromAsset(String assetKey); + /// Constructs a [Shader] object suitable for use by [Paint.shader] with /// the given uniforms. /// diff --git a/engine/src/flutter/lib/ui/painting/fragment_program.cc b/engine/src/flutter/lib/ui/painting/fragment_program.cc index 9b1f94fcbe..1a2e0e9ede 100644 --- a/engine/src/flutter/lib/ui/painting/fragment_program.cc +++ b/engine/src/flutter/lib/ui/painting/fragment_program.cc @@ -6,8 +6,11 @@ #include "flutter/lib/ui/painting/fragment_program.h" +#include "flutter/assets/asset_manager.h" +#include "flutter/impeller/runtime_stage/runtime_stage.h" #include "flutter/lib/ui/dart_wrapper.h" #include "flutter/lib/ui/ui_dart_state.h" +#include "flutter/lib/ui/window/platform_configuration.h" #include "third_party/skia/include/core/SkString.h" #include "third_party/tonic/converter/dart_converter.h" #include "third_party/tonic/dart_args.h" @@ -19,6 +22,70 @@ namespace flutter { IMPLEMENT_WRAPPERTYPEINFO(ui, FragmentProgram); +void FragmentProgram::initFromAsset(std::string asset_name) { + std::shared_ptr asset_manager = UIDartState::Current() + ->platform_configuration() + ->client() + ->GetAssetManager(); + std::unique_ptr data = asset_manager->GetAsMapping(asset_name); + if (data == nullptr) { + Dart_ThrowException(tonic::ToDart("Asset '" + asset_name + "' not found")); + return; + } + + auto runtime_stage = impeller::RuntimeStage(std::move(data)); + { + auto code_mapping = runtime_stage.GetCodeMapping(); + auto code_size = code_mapping->GetSize(); + const char* sksl = + reinterpret_cast(code_mapping->GetMapping()); + // SkString makes a copy. + SkRuntimeEffect::Result result = + SkRuntimeEffect::MakeForShader(SkString(sksl, code_size)); + if (result.effect == nullptr) { + Dart_ThrowException(tonic::ToDart(std::string("Invalid SkSL:\n") + sksl + + std::string("\nSkSL Error:\n") + + result.errorText.c_str())); + return; + } + runtime_effect_ = result.effect; + } + + int sampled_image_count = 0; + size_t other_uniforms_bytes = 0; + for (const auto& uniform_description : runtime_stage.GetUniforms()) { + if (uniform_description.type == + impeller::RuntimeUniformType::kSampledImage) { + sampled_image_count++; + } else { + size_t size = uniform_description.dimensions.rows * + uniform_description.dimensions.cols * + uniform_description.bit_width / 8u; + other_uniforms_bytes += size; + } + } + + Dart_Handle ths = Dart_HandleFromWeakPersistent(dart_wrapper()); + if (Dart_IsError(ths)) { + Dart_PropagateError(ths); + } + + Dart_Handle result = Dart_SetField(ths, tonic::ToDart("_samplerCount"), + Dart_NewInteger(sampled_image_count)); + if (Dart_IsError(result)) { + Dart_PropagateError(result); + } + + size_t rounded_uniform_bytes = + (other_uniforms_bytes + sizeof(float) - 1) & ~(sizeof(float) - 1); + size_t float_count = rounded_uniform_bytes / sizeof(float); + result = Dart_SetField(ths, tonic::ToDart("_uniformFloatCount"), + Dart_NewInteger(float_count)); + if (Dart_IsError(result)) { + Dart_PropagateError(result); + } +} + void FragmentProgram::init(std::string sksl, bool debugPrintSksl) { SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForShader(SkString(sksl)); diff --git a/engine/src/flutter/lib/ui/painting/fragment_program.h b/engine/src/flutter/lib/ui/painting/fragment_program.h index c53f4e3d81..d984410adb 100644 --- a/engine/src/flutter/lib/ui/painting/fragment_program.h +++ b/engine/src/flutter/lib/ui/painting/fragment_program.h @@ -24,6 +24,8 @@ class FragmentProgram : public RefCountedDartWrappable { ~FragmentProgram() override; static void Create(Dart_Handle wrapper); + void initFromAsset(std::string asset_name); + void init(std::string sksl, bool debugPrintSksl); fml::RefPtr shader(Dart_Handle shader, diff --git a/engine/src/flutter/lib/ui/painting/image_decoder.cc b/engine/src/flutter/lib/ui/painting/image_decoder.cc index ebbad15d4e..7fabd46115 100644 --- a/engine/src/flutter/lib/ui/painting/image_decoder.cc +++ b/engine/src/flutter/lib/ui/painting/image_decoder.cc @@ -6,9 +6,9 @@ #include "flutter/lib/ui/painting/image_decoder_skia.h" -#if IMPELLER_SUPPORTS_PLATFORM +#if IMPELLER_SUPPORTS_RENDERING #include "flutter/lib/ui/painting/image_decoder_impeller.h" -#endif // IMPELLER_SUPPORTS_PLATFORM +#endif // IMPELLER_SUPPORTS_RENDERING namespace flutter { @@ -17,7 +17,7 @@ std::unique_ptr ImageDecoder::Make( TaskRunners runners, std::shared_ptr concurrent_task_runner, fml::WeakPtr io_manager) { -#if IMPELLER_SUPPORTS_PLATFORM +#if IMPELLER_SUPPORTS_RENDERING if (settings.enable_impeller) { return std::make_unique( std::move(runners), // @@ -25,7 +25,7 @@ std::unique_ptr ImageDecoder::Make( std::move(io_manager) // ); } -#endif // IMPELLER_SUPPORTS_PLATFORM +#endif // IMPELLER_SUPPORTS_RENDERING return std::make_unique( std::move(runners), // std::move(concurrent_task_runner), // diff --git a/engine/src/flutter/lib/web_ui/lib/painting.dart b/engine/src/flutter/lib/web_ui/lib/painting.dart index ad9def4c95..47a119d876 100644 --- a/engine/src/flutter/lib/web_ui/lib/painting.dart +++ b/engine/src/flutter/lib/web_ui/lib/painting.dart @@ -845,6 +845,10 @@ class FragmentProgram { throw UnsupportedError('FragmentProgram is not supported for the CanvasKit or HTML renderers.'); } + static FragmentProgram fromAsset(String assetKey) { + throw UnsupportedError('FragmentProgram is not supported for the CanvasKit or HTML renderers.'); + } + FragmentProgram._(); Shader shader({ diff --git a/engine/src/flutter/testing/dart/fragment_shader_test.dart b/engine/src/flutter/testing/dart/fragment_shader_test.dart index 602440f607..4d4d79a1ae 100644 --- a/engine/src/flutter/testing/dart/fragment_shader_test.dart +++ b/engine/src/flutter/testing/dart/fragment_shader_test.dart @@ -54,28 +54,9 @@ void main() { _expectShaderRendersGreen(shader); }); - test('shader with functions renders green', () async { - final ByteBuffer spirv = shaderFile( - path.join('general_shaders', 'spirv'), - 'functions.frag.spirv', - ).readAsBytesSync().buffer; - final FragmentProgram program = await FragmentProgram.compile( - spirv: spirv, - ); - final Shader shader = program.shader( - floatUniforms: Float32List.fromList([1]), - ); - _expectShaderRendersGreen(shader); - }); - - test('sksl shader with functions renders green', () async { - final ByteBuffer sksl = shaderFile( - path.join('general_shaders', 'sksl'), - 'functions.frag.sksl', - ).readAsBytesSync().buffer; - final FragmentProgram program = await FragmentProgram.compile( - raw: sksl, - uniformFloatCount: 1, + test('simple iplr shader renders correctly', () async { + final FragmentProgram program = FragmentProgram.fromAsset( + 'functions.frag.iplr', ); final Shader shader = program.shader( floatUniforms: Float32List.fromList([1]), @@ -123,6 +104,20 @@ void main() { await _expectShaderRendersGreen(shader); }); + test('iplr blue-green image renders green', () async { + final FragmentProgram program = FragmentProgram.fromAsset( + 'blue_green_sampler.frag.iplr', + ); + final Image blueGreenImage = await _createBlueGreenImage(); + final ImageShader imageShader = ImageShader( + blueGreenImage, TileMode.clamp, TileMode.clamp, _identityMatrix); + final Shader shader = program.shader( + floatUniforms: Float32List.fromList([]), + samplerUniforms: [imageShader], + ); + await _expectShaderRendersGreen(shader); + }); + test('shader with uniforms renders correctly', () async { final Uint8List shaderBytes = await shaderFile( path.join('general_shaders', 'spirv'), @@ -184,6 +179,32 @@ void main() { expect(toFloat(renderedBytes.getUint8(3)), closeTo(1.0, epsilon)); }); + test('iplr shader with uniforms renders correctly', () async { + final FragmentProgram program = FragmentProgram.fromAsset( + 'uniforms.frag.iplr', + ); + + final Shader shader = program.shader( + floatUniforms: Float32List.fromList([ + 0.0, // iFloatUniform + 0.25, // iVec2Uniform.x + 0.75, // iVec2Uniform.y + 0.0, // iMat2Uniform[0][0] + 0.0, // iMat2Uniform[0][1] + 0.0, // iMat2Uniform[1][0] + 1.0, // iMat2Uniform[1][1] + ])); + + final ByteData renderedBytes = (await _imageByteDataFromShader( + shader: shader, + ))!; + + expect(toFloat(renderedBytes.getUint8(0)), closeTo(0.0, epsilon)); + expect(toFloat(renderedBytes.getUint8(1)), closeTo(0.25, epsilon)); + expect(toFloat(renderedBytes.getUint8(2)), closeTo(0.75, epsilon)); + expect(toFloat(renderedBytes.getUint8(3)), closeTo(1.0, epsilon)); + }); + test('spirv for the ink_sparkle shader is accepted', () async { final Uint8List spirv = await shaderFile( path.join('spirv'), @@ -244,6 +265,36 @@ void main() { await _expectShaderRendersGreen(shader); }); + test('iplr for the ink_sparkle shader is accepted', () async { + final FragmentProgram program = FragmentProgram.fromAsset( + 'ink_sparkle.frag.iplr', + ); + final Shader shader = program.shader( + floatUniforms: Float32List(32), + ); + + await _imageByteDataFromShader(shader: shader); + + // Testing that no exceptions are thrown. Tests that the ink_sparkle shader + // produces the correct pixels are in the framework. + }); + + test('iplr uniforms are sorted correctly', () async { + final FragmentProgram program = FragmentProgram.fromAsset( + 'uniforms_sorted.frag.iplr', + ); + + // The shader will not render green if the compiler doesn't keep the + // uniforms in the right order. + final Shader shader = program.shader( + floatUniforms: Float32List.fromList( + List.generate(32, (int i) => i.toDouble()), + ), + ); + + await _expectShaderRendersGreen(shader); + }); + // Test all supported GLSL ops. See lib/spirv/lib/src/constants.dart final Map supportedGLSLOpShaders = _loadShaders( path.join('supported_glsl_op_shaders', 'spirv'), @@ -260,6 +311,14 @@ void main() { expect(skslSupportedGLSLOpShaders.isNotEmpty, true); _expectSkSLShadersRenderGreen(skslSupportedGLSLOpShaders); + + final Map iplrSupportedGLSLOpShaders = _loadShaderAssets( + path.join('supported_glsl_op_shaders', 'iplr'), + '.iplr', + ); + expect(iplrSupportedGLSLOpShaders.isNotEmpty, true); + _expectIplrShadersRenderGreen(iplrSupportedGLSLOpShaders); + // Test all supported instructions. See lib/spirv/lib/src/constants.dart final Map supportedOpShaders = _loadShaders( path.join('supported_op_shaders', 'spirv'), @@ -276,6 +335,13 @@ void main() { expect(skslSupportedOpShaders.isNotEmpty, true); _expectSkSLShadersRenderGreen(skslSupportedOpShaders); + final Map iplrSupportedOpShaders = _loadShaderAssets( + path.join('supported_op_shaders', 'iplr'), + '.iplr', + ); + expect(iplrSupportedOpShaders.isNotEmpty, true); + _expectIplrShadersRenderGreen(iplrSupportedOpShaders); + test('equality depends on floatUniforms', () async { final ByteBuffer spirv = shaderFile( path.join('general_shaders', 'spirv'), @@ -327,6 +393,28 @@ void main() { } }); + test('iplr equality depends on floatUniforms', () { + final FragmentProgram program = FragmentProgram.fromAsset( + 'simple.frag.iplr', + ); + final Float32List ones = Float32List.fromList([1]); + final Float32List zeroes = Float32List.fromList([0]); + + { + final Shader a = program.shader(floatUniforms: ones); + final Shader b = program.shader(floatUniforms: ones); + expect(a, b); + expect(a.hashCode, b.hashCode); + } + + { + final Shader a = program.shader(floatUniforms: ones); + final Shader b = program.shader(floatUniforms: zeroes); + expect(a, notEquals(b)); + expect(a.hashCode, notEquals(b.hashCode)); + } + }); + test('equality depends on spirv', () async { final ByteBuffer spirvA = shaderFile( path.join('general_shaders', 'spirv'), @@ -369,6 +457,20 @@ void main() { expect(a.hashCode, notEquals(b.hashCode)); }); + test('iplr equality depends on data', () async { + final FragmentProgram programA = FragmentProgram.fromAsset( + 'simple.frag.iplr', + ); + final FragmentProgram programB = FragmentProgram.fromAsset( + 'uniforms.frag.iplr', + ); + final Shader a = programA.shader(); + final Shader b = programB.shader(); + + expect(a, notEquals(b)); + expect(a.hashCode, notEquals(b.hashCode)); + }); + test('Compilation does not create a Timer object', () async { final ByteBuffer spirvA = shaderFile( path.join('general_shaders', 'spirv'), @@ -446,6 +548,18 @@ void _expectSkSLShadersRenderGreen(Map shaders) { } } +void _expectIplrShadersRenderGreen(Map shaders) { + for (final String key in shaders.keys) { + test('iplr $key renders green', () async { + final FragmentProgram program = shaders[key]!; + final Shader shader = program.shader( + floatUniforms: Float32List.fromList([1]), + ); + _expectShaderRendersGreen(shader); + }); + } +} + void _expectShadersHaveOp(Map shaders, bool glsl) { for (final String key in shaders.keys) { test('$key contains opcode', () { @@ -540,6 +654,24 @@ Map _loadShaders(String leafFolderName, String ext) { return out; } +Map _loadShaderAssets(String leafFolderName, String ext) { + final Map out = SplayTreeMap(); + + final Directory directory = shaderDirectory(leafFolderName); + if (!directory.existsSync()) { + return out; + } + + directory + .listSync() + .where((FileSystemEntity entry) => path.extension(entry.path) == ext) + .forEach((FileSystemEntity entry) { + final String key = path.basenameWithoutExtension(entry.path); + out[key] = FragmentProgram.fromAsset(path.basename(entry.path)); + }); + return out; +} + // Arbitrary, but needs to be greater than 1 for frag coord tests. const int _shaderImageDimension = 4; @@ -588,4 +720,3 @@ final Float64List _identityMatrix = Float64List.fromList([ 0, 0, 1, 0, 0, 0, 0, 1, ]); - diff --git a/engine/src/flutter/testing/testing.gni b/engine/src/flutter/testing/testing.gni index db1bd241a3..3fadcbbc71 100644 --- a/engine/src/flutter/testing/testing.gni +++ b/engine/src/flutter/testing/testing.gni @@ -246,11 +246,20 @@ template("dart_snapshot") { # Arguments: # # fixtures (required): The list of fixtures to copy. +# +# dest (optional): When specified, the fixtures are placed under an +# 'assets' subdirectory of this path rather than an +# 'assets' subdirectory of target_gen_dir. template("copy_fixtures") { testonly = true assert(defined(invoker.fixtures), "The test fixtures must be specified.") + dest = target_gen_dir + if (defined(invoker.dest)) { + dest = invoker.dest + } + has_fixtures = false foreach(fixture, invoker.fixtures) { has_fixtures = true @@ -259,7 +268,7 @@ template("copy_fixtures") { if (has_fixtures) { copy(target_name) { sources = invoker.fixtures - outputs = [ "$target_gen_dir/assets/{{source_file_part}}" ] + outputs = [ "$dest/assets/{{source_file_part}}" ] forward_variables_from(invoker, [ "deps" ]) } } else { @@ -289,8 +298,21 @@ template("copy_fixtures") { # dart_main (optional): The path to the main Dart file. If specified, it is # snapshotted. # -# use_target_as_artifact_prefix(optional): If true, adds the target name as prefix of the kernel and AOT ELF snapshot filename. +# use_target_as_artifact_prefix(optional): If true, adds the target name as +# prefix of the kernel and AOT ELF snapshot filename. +# +# dest (optional): When specified, the fixtures are placed under an +# 'assets' subdirectory of this path rather than an +# 'assets' subdirectory of target_gen_dir. template("test_fixtures") { + dest = target_gen_dir + if (defined(invoker.dest)) { + dest = invoker.dest + } + + # Not all paths use 'dest' + not_needed([ "dest" ]) + # Even if no fixtures are present, the location of the fixtures directory # must always be known to tests. fixtures_location_target_name = "_fl_$target_name" @@ -298,7 +320,7 @@ template("test_fixtures") { if (is_fuchsia) { assets_dir = "/pkg/data/assets" } else { - assets_dir = "$target_gen_dir/assets" + assets_dir = "$dest/assets" } } test_deps = [ ":$fixtures_location_target_name" ] @@ -309,6 +331,7 @@ template("test_fixtures") { copy_fixtures_target_name = "_cf_$target_name" copy_fixtures(copy_fixtures_target_name) { fixtures = invoker.fixtures + dest = dest forward_variables_from(invoker, [ "deps" ]) } test_public_deps += [ ":$copy_fixtures_target_name" ]