From efb606d64a447f0ce46ae2e45cc694dd823c97c7 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 16 Dec 2022 14:34:29 -0800 Subject: [PATCH] [CP] Add remap-sampler option to impellerc (#38228) * [Impeller] order metal samplers according to declared order and not usage order (#38115) * [Impeller] order metal samplers according to declared order and not use order * ++ * always enabl remapping * Revert "always enabl remapping" This reverts commit 2fffb05aeea9cfcbd0df051540054ca0d6c337c0. * ++ * add test * ++ * ++ * only run on mac * Fix sampler offsets (#38170) * [impeller] dont remap floats * ++ * Update fragment_shader_test.dart * ++: * ++ --- ci/licenses_golden/tool_signature | 2 +- impeller/compiler/compiler.cc | 39 ++++++++ impeller/compiler/impellerc_main.cc | 1 + impeller/compiler/source_options.h | 1 + impeller/compiler/switches.cc | 4 + impeller/compiler/switches.h | 1 + .../ordering/shader_with_samplers.frag | 15 +++ impeller/tools/impeller.gni | 9 ++ lib/ui/fixtures/shaders/BUILD.gn | 13 +++ testing/dart/fragment_shader_test.dart | 33 +++++++ third_party/test_shaders/selman/LICENSE | 7 ++ .../test_shaders/selman/glow_shader.frag | 93 +++++++++++++++++++ tools/licenses/lib/main.dart | 1 + 13 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 impeller/fixtures/ordering/shader_with_samplers.frag create mode 100644 third_party/test_shaders/selman/LICENSE create mode 100644 third_party/test_shaders/selman/glow_shader.frag diff --git a/ci/licenses_golden/tool_signature b/ci/licenses_golden/tool_signature index bcc22302b6d60..5ab989a0f4fa8 100644 --- a/ci/licenses_golden/tool_signature +++ b/ci/licenses_golden/tool_signature @@ -1,2 +1,2 @@ -Signature: f6d8146c82d268e2e2549bf5019ebf07 +Signature: 9d0f7a4c4f53b80e33da848062bef937 diff --git a/impeller/compiler/compiler.cc b/impeller/compiler/compiler.cc index c8fe2f3890404..fdb9ebd8f9130 100644 --- a/impeller/compiler/compiler.cc +++ b/impeller/compiler/compiler.cc @@ -35,6 +35,45 @@ static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir, sl_options.msl_version = spirv_cross::CompilerMSL::Options::make_msl_version(1, 2); sl_compiler->set_msl_options(sl_options); + + // Set metal resource mappings to be consistent with location based mapping + // used on other backends when creating fragment shaders. This doesn't seem + // to work with the generated bindings for compute shaders, nor for certain + // shaders in the flutter/engine tree. + if (source_options.remap_samplers) { + std::vector sampler_offsets; + ir.for_each_typed_id( + [&](uint32_t, const spirv_cross::SPIRVariable& var) { + if (var.storage != spv::StorageClassUniformConstant) { + return; + } + const auto spir_type = sl_compiler->get_type(var.basetype); + auto location = sl_compiler->get_decoration( + var.self, spv::Decoration::DecorationLocation); + if (spir_type.basetype == + spirv_cross::SPIRType::BaseType::SampledImage) { + sampler_offsets.push_back(location); + } + }); + if (sampler_offsets.size() > 0) { + auto start_offset = + *std::min_element(sampler_offsets.begin(), sampler_offsets.end()); + for (auto offset : sampler_offsets) { + sl_compiler->add_msl_resource_binding({ + .stage = spv::ExecutionModel::ExecutionModelFragment, + .basetype = spirv_cross::SPIRType::BaseType::SampledImage, + .binding = offset, + .count = 1u, + // A sampled image is both an image and a sampler, so both + // offsets need to be set or depending on the partiular shader + // the bindings may be incorrect. + .msl_texture = offset - start_offset, + .msl_sampler = offset - start_offset, + }); + } + } + } + return CompilerBackend(sl_compiler); } diff --git a/impeller/compiler/impellerc_main.cc b/impeller/compiler/impellerc_main.cc index f25ecd3f86509..f412c21d86495 100644 --- a/impeller/compiler/impellerc_main.cc +++ b/impeller/compiler/impellerc_main.cc @@ -73,6 +73,7 @@ bool Main(const fml::CommandLine& command_line) { switches.source_file_name, options.type, options.source_language, switches.entry_point); options.json_format = switches.json_format; + options.remap_samplers = switches.remap_samplers; options.gles_language_version = switches.gles_language_version; Reflector::Options reflector_options; diff --git a/impeller/compiler/source_options.h b/impeller/compiler/source_options.h index ccd264562a3d0..100462ea0d095 100644 --- a/impeller/compiler/source_options.h +++ b/impeller/compiler/source_options.h @@ -27,6 +27,7 @@ struct SourceOptions { uint32_t gles_language_version = 100; std::vector defines; bool json_format = false; + bool remap_samplers = false; SourceOptions(); diff --git a/impeller/compiler/switches.cc b/impeller/compiler/switches.cc index 7618b0ed0ad73..f37bf41ac5054 100644 --- a/impeller/compiler/switches.cc +++ b/impeller/compiler/switches.cc @@ -71,6 +71,9 @@ void Switches::PrintHelp(std::ostream& stream) { stream << "[optional] --depfile=" << std::endl; stream << "[optional] --gles-language-verision=" << std::endl; stream << "[optional] --json" << std::endl; + stream << "[optional] --remap-samplers (force metal sampler index to match " + "declared order)" + << std::endl; } Switches::Switches() = default; @@ -125,6 +128,7 @@ Switches::Switches(const fml::CommandLine& command_line) command_line.GetOptionValueWithDefault("reflection-cc", "")), depfile_path(command_line.GetOptionValueWithDefault("depfile", "")), json_format(command_line.HasOption("json")), + remap_samplers(command_line.HasOption("remap-samplers")), gles_language_version( stoi(command_line.GetOptionValueWithDefault("gles-language-version", "0"))), diff --git a/impeller/compiler/switches.h b/impeller/compiler/switches.h index 01774285e7f27..198c67590443e 100644 --- a/impeller/compiler/switches.h +++ b/impeller/compiler/switches.h @@ -32,6 +32,7 @@ struct Switches { std::string depfile_path; std::vector defines; bool json_format; + bool remap_samplers; SourceLanguage source_language = SourceLanguage::kUnknown; uint32_t gles_language_version; std::string entry_point; diff --git a/impeller/fixtures/ordering/shader_with_samplers.frag b/impeller/fixtures/ordering/shader_with_samplers.frag new file mode 100644 index 0000000000000..2900797d22d00 --- /dev/null +++ b/impeller/fixtures/ordering/shader_with_samplers.frag @@ -0,0 +1,15 @@ +// Copyright 2013 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. + +// Declare samplers in different order than usage. +uniform sampler2D textureA; +uniform sampler2D textureB; + +out vec4 frag_color; + +void main() { + vec4 sample_1 = texture(textureB, vec2(1.0)); + vec4 sample_2 = texture(textureA, vec2(1.0)); + frag_color = sample_1 + sample_2; +} diff --git a/impeller/tools/impeller.gni b/impeller/tools/impeller.gni index 320efc9fcb29c..e6e71edfbeae6 100644 --- a/impeller/tools/impeller.gni +++ b/impeller/tools/impeller.gni @@ -233,9 +233,13 @@ template("impellerc") { iplr = invoker.iplr } json = false + remap_samplers = false if (defined(invoker.json) && invoker.json) { json = invoker.json } + if (defined(invoker.remap_samplers) && invoker.remap_samplers) { + remap_samplers = invoker.remap_samplers + } # Not needed on every path. not_needed([ @@ -248,6 +252,8 @@ template("impellerc") { # Optional: invoker.intermediates_subdir specifies the subdirectory in which # to put intermediates. # Optional: invoker.json Causes output format to be JSON instead of flatbuffer. + # Optional: invoker.remap_samplers Output metal samplers according to + # declaration order instead of usage order. _impellerc(target_name) { sources = invoker.shaders @@ -284,6 +290,9 @@ template("impellerc") { if (json) { args += [ "--json" ] } + if (remap_samplers) { + args += [ "--remap-samplers" ] + } if (sksl) { sl_intermediate = diff --git a/lib/ui/fixtures/shaders/BUILD.gn b/lib/ui/fixtures/shaders/BUILD.gn index ff4b9b458e7cb..37d881e3539f8 100644 --- a/lib/ui/fixtures/shaders/BUILD.gn +++ b/lib/ui/fixtures/shaders/BUILD.gn @@ -33,10 +33,23 @@ if (enable_unittests) { json = true } + impellerc("sampler_order_fixture") { + shaders = [ + "//flutter/impeller/fixtures/ordering/shader_with_samplers.frag", + "//flutter/third_party/test_shaders/selman/glow_shader.frag", + ] + shader_target_flag = "--runtime-stage-metal" + intermediates_subdir = "iplr-remap" + sl_file_extension = "iplr" + iplr = true + remap_samplers = true + } + test_fixtures("fixtures") { deps = [ ":ink_sparkle", ":ink_sparkle_web", + ":sampler_order_fixture", ] fixtures = get_target_outputs(":ink_sparkle") dest = "$root_gen_dir/flutter/lib/ui" diff --git a/testing/dart/fragment_shader_test.dart b/testing/dart/fragment_shader_test.dart index 9fd025ab04085..7ce3edbe389b6 100644 --- a/testing/dart/fragment_shader_test.dart +++ b/testing/dart/fragment_shader_test.dart @@ -332,6 +332,34 @@ void main() async { shader.dispose(); }); + // This test can't rely on actual pixels rendered since it needs to run on a + // metal shader on iOS. instead parse the source code. + test('impellerc orders samplers in metal shader according to declaration and not usage', () async { + if (!Platform.isMacOS) { + return; + } + final Directory directory = shaderDirectory('iplr-remap'); + final String data = readAsStringLossy(File(path.join(directory.path, 'shader_with_samplers.frag.iplr'))); + + const String expected = 'texture2d textureA [[texture(0)]],' + ' texture2d textureB [[texture(1)]]'; + + expect(data, contains(expected)); + }); + + test('impellerc orders samplers in metal shader according to declaration and not usage in glow', () async { + if (!Platform.isMacOS) { + return; + } + final Directory directory = shaderDirectory('iplr-remap'); + final String data = readAsStringLossy(File(path.join(directory.path, 'glow_shader.frag.iplr'))); + + const String expected = 'texture2d tInput [[texture(0)]], texture2d tNoise [[texture(1)]], ' + 'sampler tInputSmplr [[sampler(0)]], sampler tNoiseSmplr [[sampler(1)]]'; + + expect(data, contains(expected)); + }); + // Test all supported GLSL ops. See lib/spirv/lib/src/constants.dart final Map iplrSupportedGLSLOpShaders = await _loadShaderAssets( path.join('supported_glsl_op_shaders', 'iplr'), @@ -484,3 +512,8 @@ Image _createBlueGreenImageSync() { picture.dispose(); } } + +// Ignore invalid utf8 since file is not actually text. +String readAsStringLossy(File file) { + return convert.utf8.decode(file.readAsBytesSync(), allowMalformed: true); +} diff --git a/third_party/test_shaders/selman/LICENSE b/third_party/test_shaders/selman/LICENSE new file mode 100644 index 0000000000000..25145d88bd421 --- /dev/null +++ b/third_party/test_shaders/selman/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2022 by Selman Ay (https://codepen.io/selmanays/pen/yLVmEqY) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/third_party/test_shaders/selman/glow_shader.frag b/third_party/test_shaders/selman/glow_shader.frag new file mode 100644 index 0000000000000..4aae339f3843a --- /dev/null +++ b/third_party/test_shaders/selman/glow_shader.frag @@ -0,0 +1,93 @@ +// Directional glow GLSL fragment shader. +// Based on the "Let it Glow!" pen by "Selman Ay" +// (https://codepen.io/selmanays/pen/yLVmEqY) +precision highp float; + +// Float uniforms +uniform float width; // Width of the canvas +uniform float height; // Height of the canvas +uniform float sourceX; // X position of the light source +uniform float + scrollFraction; // Scroll fraction of the page in relation to the canvas +uniform float density; // Density of the "smoke" effect +uniform float lightStrength; // Strength of the light +uniform float weight; // Weight of the "smoke" effect + +// Sampler uniforms +uniform sampler2D tInput; // Input texture (the application canvas) +uniform sampler2D tNoise; // Some texture + +out vec4 fragColor; + +float sourceY = scrollFraction; +vec2 resolution = vec2(width, height); +vec2 lightSource = vec2(sourceX, sourceY); + +const int samples = 20; // The number of "copies" of the canvas made to emulate + // the "smoke" effect +const float decay = 0.88; // Decay of the light in each sample +const float exposure = .9; // The exposure to the light + +float random2d(vec2 uv) { + uv /= 256.; + vec4 tex = texture(tNoise, uv); + return mix(tex.r, tex.g, tex.a); +} + +float random(vec3 xyz) { + return fract(sin(dot(xyz, vec3(12.9898, 78.233, 151.7182))) * 43758.5453); +} + +vec4 sampleTexture(vec2 uv) { + vec4 textColor = texture(tInput, uv); + return textColor; +} + +vec4 occlusion(vec2 uv, vec2 lightpos, vec4 objects) { + return (1. - smoothstep(0.0, lightStrength, length(lightpos - uv))) * + (objects); +} + +vec4 fragment(vec2 uv, vec2 fragCoord) { + vec3 colour = vec3(0); + + vec4 obj = sampleTexture(uv); + vec4 map = occlusion(uv, lightSource, obj); + + float random = random(vec3(fragCoord, 1.0)); + ; + + float exposure = exposure + (sin(random) * .5 + 1.) * .05; + + vec2 _uv = uv; + vec2 distance = (_uv - lightSource) * (1. / float(samples) * density); + + float illumination_decay = 1.; + for (int i = 0; i < samples; i++) { + _uv -= distance; + + float movement = random * 20. * float(i + 1); + float dither = + random2d(uv + + mod(vec2(movement * sin(random * .5), -movement), 1000.)) * + 2.; + + vec4 stepped_map = + occlusion(uv, lightSource, sampleTexture(_uv + distance * dither)); + stepped_map *= illumination_decay * weight; + + illumination_decay *= decay; + map += stepped_map; + } + + float lum = dot(map.rgb, vec3(0.2126, 0.7152, 0.0722)); + + colour += vec3(map.rgb * exposure); + return vec4(colour, lum); +} + +void main() { + vec2 pos = gl_FragCoord.xy; + vec2 uv = pos / vec2(width, height); + fragColor = fragment(uv, pos); +} diff --git a/tools/licenses/lib/main.dart b/tools/licenses/lib/main.dart index e9cea77d9eacc..f308860b43b9d 100644 --- a/tools/licenses/lib/main.dart +++ b/tools/licenses/lib/main.dart @@ -1025,6 +1025,7 @@ class _RepositoryDirectory extends _RepositoryEntry implements LicenseSource { !entry.fullName.endsWith('third_party/json/docs') && !entry.fullName.endsWith('third_party/ninja') && !entry.fullName.endsWith('third_party/tinygltf') && + !entry.fullName.endsWith('third_party/test_shaders') && entry.name != '.ccls-cache' && entry.name != '.cipd' && entry.name != '.git' &&