From f615fba929cd1fb747d170493258891c0e7e0ca6 Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Mon, 8 Aug 2022 11:56:11 -0700 Subject: [PATCH] Adds support for uniform arrays to FragmentPrograms (flutter/engine#35209) --- .../flutter/impeller/compiler/reflector.cc | 13 +++++ .../src/flutter/impeller/compiler/reflector.h | 2 + .../impeller/compiler/runtime_stage_data.cc | 1 + .../impeller/compiler/runtime_stage_data.h | 1 + .../flutter/impeller/compiler/spirv_sksl.cc | 1 + .../impeller/runtime_stage/runtime_stage.cc | 1 + .../impeller/runtime_stage/runtime_stage.fbs | 1 + .../impeller/runtime_stage/runtime_stage.h | 1 + .../fixtures/shaders/general_shaders/BUILD.gn | 1 + .../general_shaders/uniform_arrays.frag | 51 +++++++++++++++++++ .../lib/ui/painting/fragment_program.cc | 3 ++ .../testing/dart/fragment_shader_test.dart | 16 ++++++ 12 files changed, 92 insertions(+) create mode 100644 engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/uniform_arrays.frag diff --git a/engine/src/flutter/impeller/compiler/reflector.cc b/engine/src/flutter/impeller/compiler/reflector.cc index 69d61719e0..0df6098086 100644 --- a/engine/src/flutter/impeller/compiler/reflector.cc +++ b/engine/src/flutter/impeller/compiler/reflector.cc @@ -354,11 +354,24 @@ std::shared_ptr Reflector::GenerateRuntimeStageData() const { uniform_description.rows = spir_type.vecsize; uniform_description.columns = spir_type.columns; uniform_description.bit_width = spir_type.width; + uniform_description.array_elements = GetArrayElements(spir_type); data->AddUniformDescription(std::move(uniform_description)); }); return data; } +uint32_t Reflector::GetArrayElements(const spirv_cross::SPIRType& type) const { + if (type.array.empty()) { + return 0; + } + uint32_t elements = 1; + for (size_t i = 0; i < type.array.size(); i++) { + FML_CHECK(type.array_size_literal[i]); + elements *= type.array[i]; + } + return elements; +} + static std::string ToString(CompilerBackend::Type type) { switch (type) { case CompilerBackend::Type::kMSL: diff --git a/engine/src/flutter/impeller/compiler/reflector.h b/engine/src/flutter/impeller/compiler/reflector.h index 5a5be69503..808ce56d3b 100644 --- a/engine/src/flutter/impeller/compiler/reflector.h +++ b/engine/src/flutter/impeller/compiler/reflector.h @@ -138,6 +138,8 @@ class Reflector { std::vector ReadStructMembers( const spirv_cross::TypeID& type_id) const; + uint32_t GetArrayElements(const spirv_cross::SPIRType& type) const; + FML_DISALLOW_COPY_AND_ASSIGN(Reflector); }; diff --git a/engine/src/flutter/impeller/compiler/runtime_stage_data.cc b/engine/src/flutter/impeller/compiler/runtime_stage_data.cc index af4c611185..70ebb3079a 100644 --- a/engine/src/flutter/impeller/compiler/runtime_stage_data.cc +++ b/engine/src/flutter/impeller/compiler/runtime_stage_data.cc @@ -157,6 +157,7 @@ std::shared_ptr RuntimeStageData::CreateMapping() const { } desc->type = uniform_type.value(); desc->bit_width = uniform.bit_width; + desc->array_elements = uniform.array_elements; 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 b0022a1402..d5da09dd02 100644 --- a/engine/src/flutter/impeller/compiler/runtime_stage_data.h +++ b/engine/src/flutter/impeller/compiler/runtime_stage_data.h @@ -22,6 +22,7 @@ struct UniformDescription { size_t rows = 0u; size_t columns = 0u; size_t bit_width = 0u; + size_t array_elements = 0u; }; class RuntimeStageData { diff --git a/engine/src/flutter/impeller/compiler/spirv_sksl.cc b/engine/src/flutter/impeller/compiler/spirv_sksl.cc index 42fbeeb4a5..ab5727ded9 100644 --- a/engine/src/flutter/impeller/compiler/spirv_sksl.cc +++ b/engine/src/flutter/impeller/compiler/spirv_sksl.cc @@ -22,6 +22,7 @@ std::string CompilerSkSL::compile() { options.version = 100; options.vulkan_semantics = false; options.enable_420pack_extension = false; + options.flatten_multidimensional_arrays = true; backend.allow_precision_qualifiers = false; backend.basic_int16_type = "short"; diff --git a/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc b/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc index 01955ee373..353287d6a0 100644 --- a/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc +++ b/engine/src/flutter/impeller/runtime_stage/runtime_stage.cc @@ -85,6 +85,7 @@ RuntimeStage::RuntimeStage(std::shared_ptr payload) desc.dimensions = RuntimeUniformDimensions{ static_cast(i->rows()), static_cast(i->columns())}; desc.bit_width = i->bit_width(); + desc.array_elements = i->array_elements(); 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 1391fc565a..bb3d04e00e 100644 --- a/engine/src/flutter/impeller/runtime_stage/runtime_stage.fbs +++ b/engine/src/flutter/impeller/runtime_stage/runtime_stage.fbs @@ -41,6 +41,7 @@ table UniformDescription { bit_width: uint64; rows: uint64; columns: uint64; + array_elements: uint64; } table RuntimeStage { diff --git a/engine/src/flutter/impeller/runtime_stage/runtime_stage.h b/engine/src/flutter/impeller/runtime_stage/runtime_stage.h index 4f8f9b8148..2bbcf83d37 100644 --- a/engine/src/flutter/impeller/runtime_stage/runtime_stage.h +++ b/engine/src/flutter/impeller/runtime_stage/runtime_stage.h @@ -41,6 +41,7 @@ struct RuntimeUniformDescription { RuntimeUniformType type = kFloat; RuntimeUniformDimensions dimensions; size_t bit_width; + size_t array_elements; }; size_t SizeOfRuntimeUniformType(RuntimeUniformType type); diff --git a/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/BUILD.gn b/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/BUILD.gn index c99d06320b..640823715e 100644 --- a/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/BUILD.gn +++ b/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/BUILD.gn @@ -16,6 +16,7 @@ if (enable_unittests) { "simple.frag", "uniforms.frag", "uniforms_sorted.frag", + "uniform_arrays.frag", ] group("general_shaders") { diff --git a/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/uniform_arrays.frag b/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/uniform_arrays.frag new file mode 100644 index 0000000000..64878088a9 --- /dev/null +++ b/engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/uniform_arrays.frag @@ -0,0 +1,51 @@ +#version 100 core + +// 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. + +precision highp float; + +layout ( location = 0 ) out vec4 oColor; + +layout ( location = 1 ) uniform float floatArray[2]; +layout ( location = 3 ) uniform vec2 vec2Array[2]; +layout ( location = 7 ) uniform vec3 vec3Array[2]; +layout ( location = 13 ) uniform mat2 mat2Array[2]; +layout ( location = 21 ) uniform float float2dArray[2][2]; + +void main() { + vec4 badColor = vec4(1.0, 0, 0, 1.0); + vec4 goodColor = vec4(0, 1.0, 0, 1.0); + + // The test populates the uniforms with strictly increasing values, so if + // out-of-order values are read out of the uniforms, then the bad color that + // causes the test to fail is returned. + if (floatArray[0] >= floatArray[1] || + floatArray[1] >= vec2Array[0].x || + vec2Array[0].x >= vec2Array[0].y || + vec2Array[0].y >= vec2Array[1].x || + vec2Array[1].x >= vec2Array[1].y || + vec2Array[1].y >= vec3Array[0].x || + vec3Array[0].x >= vec3Array[0].y || + vec3Array[0].y >= vec3Array[0].z || + vec3Array[0].z >= vec3Array[1].x || + vec3Array[1].x >= vec3Array[1].y || + vec3Array[1].y >= vec3Array[1].z || + vec3Array[1].z >= mat2Array[0][0][0] || + mat2Array[0][0][0] >= mat2Array[0][0][1] || + mat2Array[0][0][1] >= mat2Array[0][1][0] || + mat2Array[0][1][0] >= mat2Array[0][1][1] || + mat2Array[0][1][1] >= mat2Array[1][0][0] || + mat2Array[1][0][0] >= mat2Array[1][0][1] || + mat2Array[1][0][1] >= mat2Array[1][1][0] || + mat2Array[1][1][0] >= mat2Array[1][1][1] || + mat2Array[1][1][1] >= float2dArray[0][0] || + float2dArray[0][0] >= float2dArray[0][1] || + float2dArray[0][1] >= float2dArray[1][0] || + float2dArray[1][0] >= float2dArray[1][1]) { + oColor = badColor; + } else { + oColor = goodColor; + } +} diff --git a/engine/src/flutter/lib/ui/painting/fragment_program.cc b/engine/src/flutter/lib/ui/painting/fragment_program.cc index a20e813167..bcd2626d28 100644 --- a/engine/src/flutter/lib/ui/painting/fragment_program.cc +++ b/engine/src/flutter/lib/ui/painting/fragment_program.cc @@ -65,6 +65,9 @@ std::string FragmentProgram::initFromAsset(std::string asset_name) { size_t size = uniform_description.dimensions.rows * uniform_description.dimensions.cols * uniform_description.bit_width / 8u; + if (uniform_description.array_elements > 0) { + size *= uniform_description.array_elements; + } other_uniforms_bytes += size; } } diff --git a/engine/src/flutter/testing/dart/fragment_shader_test.dart b/engine/src/flutter/testing/dart/fragment_shader_test.dart index d19a59c017..7fdcdf2742 100644 --- a/engine/src/flutter/testing/dart/fragment_shader_test.dart +++ b/engine/src/flutter/testing/dart/fragment_shader_test.dart @@ -64,6 +64,22 @@ void main() async { expect(toFloat(renderedBytes.getUint8(3)), closeTo(1.0, epsilon)); }); + test('shader with array uniforms renders correctly', () async { + final FragmentProgram program = await FragmentProgram.fromAsset( + 'uniform_arrays.frag.iplr', + ); + + final List floatArray = List.generate( + 24, (int i) => i.toDouble(), + ); + final Shader shader = program.shader( + floatUniforms: Float32List.fromList([ + ...floatArray, + ])); + + await _expectShaderRendersGreen(shader); + }); + test('The ink_sparkle shader is accepted', () async { final FragmentProgram program = await FragmentProgram.fromAsset( 'ink_sparkle.frag.iplr',