Adds support for uniform arrays to FragmentPrograms (flutter/engine#35209)

This commit is contained in:
Zachary Anderson
2022-08-08 11:56:11 -07:00
committed by GitHub
parent 39cfb76a40
commit f615fba929
12 changed files with 92 additions and 0 deletions

View File

@@ -354,11 +354,24 @@ std::shared_ptr<RuntimeStageData> 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:

View File

@@ -138,6 +138,8 @@ class Reflector {
std::vector<StructMember> ReadStructMembers(
const spirv_cross::TypeID& type_id) const;
uint32_t GetArrayElements(const spirv_cross::SPIRType& type) const;
FML_DISALLOW_COPY_AND_ASSIGN(Reflector);
};

View File

@@ -157,6 +157,7 @@ std::shared_ptr<fml::Mapping> 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));
}

View File

@@ -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 {

View File

@@ -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";

View File

@@ -85,6 +85,7 @@ RuntimeStage::RuntimeStage(std::shared_ptr<fml::Mapping> payload)
desc.dimensions = RuntimeUniformDimensions{
static_cast<size_t>(i->rows()), static_cast<size_t>(i->columns())};
desc.bit_width = i->bit_width();
desc.array_elements = i->array_elements();
uniforms_.emplace_back(std::move(desc));
}
}

View File

@@ -41,6 +41,7 @@ table UniformDescription {
bit_width: uint64;
rows: uint64;
columns: uint64;
array_elements: uint64;
}
table RuntimeStage {

View File

@@ -41,6 +41,7 @@ struct RuntimeUniformDescription {
RuntimeUniformType type = kFloat;
RuntimeUniformDimensions dimensions;
size_t bit_width;
size_t array_elements;
};
size_t SizeOfRuntimeUniformType(RuntimeUniformType type);

View File

@@ -16,6 +16,7 @@ if (enable_unittests) {
"simple.frag",
"uniforms.frag",
"uniforms_sorted.frag",
"uniform_arrays.frag",
]
group("general_shaders") {

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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<double> floatArray = List<double>.generate(
24, (int i) => i.toDouble(),
);
final Shader shader = program.shader(
floatUniforms: Float32List.fromList(<double>[
...floatArray,
]));
await _expectShaderRendersGreen(shader);
});
test('The ink_sparkle shader is accepted', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'ink_sparkle.frag.iplr',