Removes FragmentProgram.shader() (flutter/engine#35904)

This commit is contained in:
Zachary Anderson
2022-09-21 07:14:12 -07:00
committed by GitHub
parent 37a90d9bdd
commit 16fbee37b3
8 changed files with 3 additions and 394 deletions

View File

@@ -172,7 +172,6 @@ typedef CanvasPath Path;
V(ColorFilter, initSrgbToLinearGamma, 1) \
V(EngineLayer, dispose, 1) \
V(FragmentProgram, initFromAsset, 2) \
V(FragmentProgram, shader, 4) \
V(ReusableFragmentShader, Dispose, 1) \
V(ReusableFragmentShader, SetSampler, 3) \
V(Gradient, initLinear, 6) \

View File

@@ -4214,87 +4214,6 @@ class FragmentProgram extends NativeFieldWrapperClass1 {
/// Returns a fresh instance of [FragmentShader].
FragmentShader fragmentShader() => FragmentShader._(this);
/// Constructs a [Shader] object suitable for use by [Paint.shader] with
/// the given uniforms.
///
/// This method is suitable to be called synchronously within a widget's
/// `build` method or from [CustomPainter.paint].
///
/// `floatUniforms` can be passed optionally to initialize the shader's
/// uniforms. If they are not set they will each default to 0.
///
/// When initializing `floatUniforms`, the length of float uniforms must match
/// the total number of floats defined as uniforms in the shader, or an
/// [ArgumentError] will be thrown. Details are below.
///
/// Consider the following snippit of GLSL code.
///
/// ```glsl
/// layout (location = 0) uniform float a;
/// layout (location = 1) uniform vec2 b;
/// layout (location = 2) uniform vec3 c;
/// layout (location = 3) uniform mat2x2 d;
/// ```
///
/// When compiled to SPIR-V and provided to the constructor, `floatUniforms`
/// must have a length of 10. One per float-component of each uniform.
///
/// `program.shader(floatUniforms: Float32List.fromList([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));`
///
/// The uniforms will be set as follows:
///
/// a: 1
/// b: [2, 3]
/// c: [4, 5, 6]
/// d: [7, 8, 9, 10] // 2x2 matrix in column-major order
///
/// `imageSamplers` must also be sized correctly, matching the number of UniformConstant
/// variables of type SampledImage specified in the SPIR-V code.
///
/// Consider the following snippit of GLSL code.
///
/// ```glsl
/// layout (location = 0) uniform sampler2D a;
/// layout (location = 1) uniform sampler2D b;
/// ```
///
/// After being compiled to SPIR-V `imageSamplers` must have a length
/// of 2.
///
/// Once a [Shader] is built, uniform values cannot be changed. Instead,
/// [shader] must be called again with new uniform values.
Shader shader({
Float32List? floatUniforms,
List<ImageShader>? samplerUniforms,
}) {
floatUniforms ??= Float32List(_uniformFloatCount);
if (floatUniforms.length != _uniformFloatCount) {
throw ArgumentError(
'floatUniforms size: ${floatUniforms.length} must match given shader '
'uniform count: $_uniformFloatCount.',
);
}
if (_samplerCount > 0 &&
(samplerUniforms == null || samplerUniforms.length != _samplerCount)) {
throw ArgumentError('samplerUniforms must have length $_samplerCount');
}
if (samplerUniforms == null) {
samplerUniforms = <ImageShader>[];
} else {
samplerUniforms = <ImageShader>[...samplerUniforms];
}
final _FragmentShader shader = _FragmentShader(
this,
Float32List.fromList(floatUniforms),
samplerUniforms,
);
_shader(shader, floatUniforms, samplerUniforms);
return shader;
}
@FfiNative<Handle Function(Pointer<Void>, Handle, Handle, Handle)>('FragmentProgram::shader')
external Handle _shader(_FragmentShader shader, Float32List floatUniforms, List<ImageShader> samplerUniforms);
}
/// A [Shader] generated from a [FragmentProgram].
@@ -4360,40 +4279,6 @@ class FragmentShader extends Shader {
external void _dispose();
}
@pragma('vm:entry-point')
class _FragmentShader extends Shader {
/// This class is created by the engine and should not be instantiated
/// or extended directly.
///
/// To create a [_FragmentShader], use a [FragmentProgram].
_FragmentShader(
this._builder,
this._floatUniforms,
this._samplerUniforms,
) : super._();
final FragmentProgram _builder;
final Float32List _floatUniforms;
final List<ImageShader> _samplerUniforms;
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is _FragmentShader
&& other._builder == _builder
&& _listEquals<double>(other._floatUniforms, _floatUniforms)
&& _listEquals<ImageShader>(other._samplerUniforms, _samplerUniforms);
}
@override
int get hashCode => Object.hash(_builder, Object.hashAll(_floatUniforms), Object.hashAll(_samplerUniforms));
}
/// Defines how a list of points is interpreted when drawing a set of triangles.
///
/// Used by [Canvas.drawVertices].

View File

@@ -95,44 +95,6 @@ std::string FragmentProgram::initFromAsset(std::string asset_name) {
return "";
}
fml::RefPtr<FragmentShader> FragmentProgram::shader(Dart_Handle shader,
Dart_Handle uniforms_handle,
Dart_Handle samplers) {
auto sampler_shaders =
tonic::DartConverter<std::vector<ImageShader*>>::FromDart(samplers);
tonic::Float32List uniforms(uniforms_handle);
size_t uniform_count = uniforms.num_elements();
size_t uniform_data_size =
(uniform_count + 2 * sampler_shaders.size()) * sizeof(float);
sk_sp<SkData> uniform_data = SkData::MakeUninitialized(uniform_data_size);
// uniform_floats must only be referenced BEFORE the call to makeShader below.
auto* uniform_floats =
reinterpret_cast<float*>(uniform_data->writable_data());
for (size_t i = 0; i < uniform_count; i++) {
uniform_floats[i] = uniforms[i];
}
uniforms.Release();
std::vector<std::shared_ptr<DlColorSource>> dl_samplers(
sampler_shaders.size());
for (size_t i = 0; i < sampler_shaders.size(); i++) {
DlImageSampling sampling = DlImageSampling::kNearestNeighbor;
ImageShader* image_shader = sampler_shaders[i];
// ImageShaders can hold a preferred value for sampling options and
// developers are encouraged to use that value or the value will be supplied
// by "the environment where it is used". The environment here does not
// contain a value to be used if the developer did not specify a preference
// when they constructed the ImageShader, so we will use kNearest which is
// the default filterQuality in a Paint object.
dl_samplers[i] = image_shader->shader(sampling);
uniform_floats[uniform_count + 2 * i] = image_shader->width();
uniform_floats[uniform_count + 2 * i + 1] = image_shader->height();
}
return FragmentShader::Create(
shader,
DlColorSource::MakeRuntimeEffect(runtime_effect_, std::move(dl_samplers),
std::move(uniform_data)));
}
std::shared_ptr<DlColorSource> FragmentProgram::MakeDlColorSource(
sk_sp<SkData> float_uniforms,
const std::vector<std::shared_ptr<DlColorSource>>& children) {

View File

@@ -18,36 +18,6 @@
namespace flutter {
// Since _FragmentShader is a private class, we can't use
// IMPLEMENT_WRAPPERTYPEINFO
static const tonic::DartWrapperInfo kDartWrapperInfoUIFragmentShader = {
"ui",
"_FragmentShader",
};
const tonic::DartWrapperInfo& FragmentShader::dart_wrapper_info_ =
kDartWrapperInfoUIFragmentShader;
std::shared_ptr<DlColorSource> FragmentShader::shader(
DlImageSampling sampling) {
// Sampling options are ignored, since sampling options don't make sense for
// generative shaders.
return source_;
}
fml::RefPtr<FragmentShader> FragmentShader::Create(
Dart_Handle dart_handle,
std::shared_ptr<DlRuntimeEffectColorSource> shader) {
auto fragment_shader = fml::MakeRefCounted<FragmentShader>(std::move(shader));
fragment_shader->AssociateWithDartWrapper(dart_handle);
return fragment_shader;
}
FragmentShader::FragmentShader(
std::shared_ptr<DlRuntimeEffectColorSource> shader)
: source_(std::move(shader)) {}
FragmentShader::~FragmentShader() = default;
IMPLEMENT_WRAPPERTYPEINFO(ui, ReusableFragmentShader);
ReusableFragmentShader::ReusableFragmentShader(

View File

@@ -22,25 +22,6 @@ namespace flutter {
class FragmentProgram;
class FragmentShader : public Shader {
DEFINE_WRAPPERTYPEINFO();
FML_FRIEND_MAKE_REF_COUNTED(FragmentShader);
public:
~FragmentShader() override;
static fml::RefPtr<FragmentShader> Create(
Dart_Handle dart_handle,
std::shared_ptr<DlRuntimeEffectColorSource> shader);
// |Shader|
std::shared_ptr<DlColorSource> shader(DlImageSampling) override;
private:
explicit FragmentShader(std::shared_ptr<DlRuntimeEffectColorSource> shader);
std::shared_ptr<DlRuntimeEffectColorSource> source_;
};
class ReusableFragmentShader : public Shader {
DEFINE_WRAPPERTYPEINFO();
FML_FRIEND_MAKE_REF_COUNTED(ReusableFragmentShader);

View File

@@ -813,11 +813,6 @@ class FragmentProgram {
FragmentShader fragmentShader() {
throw UnsupportedError('FragmentProgram is not supported for the CanvasKit or HTML renderers.');
}
Shader shader({
Float32List? floatUniforms,
List<ImageShader>? samplerUniforms,
}) => throw UnsupportedError('FragmentProgram is not supported for the CanvasKit or HTML renderers.');
}
class FragmentShader extends Shader {

View File

@@ -140,16 +140,6 @@ void main() async {
}
});
test('simple shader renders correctly', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'functions.frag.iplr',
);
final Shader shader = program.shader(
floatUniforms: Float32List.fromList(<double>[1]),
);
await _expectShaderRendersGreen(shader);
});
test('FragmentShader simple shader renders correctly', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'functions.frag.iplr',
@@ -174,22 +164,6 @@ void main() async {
shader.dispose();
});
test('blue-green image renders green', () async {
final FragmentProgram program = await 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(<double>[]),
samplerUniforms: <ImageShader>[imageShader],
);
await _expectShaderRendersGreen(shader);
imageShader.dispose();
blueGreenImage.dispose();
});
test('FragmentShader blue-green image renders green', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'blue_green_sampler.frag.iplr',
@@ -205,22 +179,6 @@ void main() async {
blueGreenImage.dispose();
});
test('blue-green image renders green - GPU image', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'blue_green_sampler.frag.iplr',
);
final Image blueGreenImage = _createBlueGreenImageSync();
final ImageShader imageShader = ImageShader(
blueGreenImage, TileMode.clamp, TileMode.clamp, _identityMatrix);
final Shader shader = program.shader(
floatUniforms: Float32List.fromList(<double>[]),
samplerUniforms: <ImageShader>[imageShader],
);
await _expectShaderRendersGreen(shader);
imageShader.dispose();
blueGreenImage.dispose();
});
test('FragmentShader blue-green image renders green - GPU image', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'blue_green_sampler.frag.iplr',
@@ -236,32 +194,6 @@ void main() async {
blueGreenImage.dispose();
});
test('shader with uniforms renders correctly', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'uniforms.frag.iplr',
);
final Shader shader = program.shader(
floatUniforms: Float32List.fromList(<double>[
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('FragmentShader with uniforms renders correctly', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'uniforms.frag.iplr',
@@ -288,22 +220,6 @@ void main() async {
shader.dispose();
});
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('FragmentShader shader with array uniforms renders correctly', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'uniform_arrays.frag.iplr',
@@ -318,20 +234,6 @@ void main() async {
shader.dispose();
});
test('The ink_sparkle shader is accepted', () async {
final FragmentProgram program = await 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('FragmentShader The ink_sparkle shader is accepted', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'ink_sparkle.frag.iplr',
@@ -345,22 +247,6 @@ void main() async {
shader.dispose();
});
test('Uniforms are sorted correctly', () async {
final FragmentProgram program = await 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<double>.generate(32, (int i) => i.toDouble()),
),
);
await _expectShaderRendersGreen(shader);
});
test('FragmentShader Uniforms are sorted correctly', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'uniforms_sorted.frag.iplr',
@@ -402,16 +288,6 @@ void main() async {
expect(throws, equals(true));
});
test('user defined functions do not redefine builtins', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'no_builtin_redefinition.frag.iplr',
);
final Shader shader = program.shader(
floatUniforms: Float32List.fromList(<double>[1.0]),
);
await _expectShaderRendersGreen(shader);
});
test('FragmentShader user defined functions do not redefine builtins', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'no_builtin_redefinition.frag.iplr',
@@ -422,14 +298,6 @@ void main() async {
shader.dispose();
});
test('fromAsset accepts a shader with no uniforms', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'no_uniforms.frag.iplr',
);
final Shader shader = program.shader();
await _expectShaderRendersGreen(shader);
});
test('FragmentShader fromAsset accepts a shader with no uniforms', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'no_uniforms.frag.iplr',
@@ -445,7 +313,6 @@ void main() async {
'.iplr',
);
expect(iplrSupportedGLSLOpShaders.isNotEmpty, true);
_expectIplrShadersRenderGreen(iplrSupportedGLSLOpShaders);
_expectFragmentShadersRenderGreen(iplrSupportedGLSLOpShaders);
// Test all supported instructions. See lib/spirv/lib/src/constants.dart
@@ -454,61 +321,12 @@ void main() async {
'.iplr',
);
expect(iplrSupportedOpShaders.isNotEmpty, true);
_expectIplrShadersRenderGreen(iplrSupportedOpShaders);
_expectFragmentShadersRenderGreen(iplrSupportedOpShaders);
test('Equality depends on floatUniforms', () async {
final FragmentProgram program = await FragmentProgram.fromAsset(
'simple.frag.iplr',
);
final Float32List ones = Float32List.fromList(<double>[1]);
final Float32List zeroes = Float32List.fromList(<double>[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 data', () async {
final FragmentProgram programA = await FragmentProgram.fromAsset(
'simple.frag.iplr',
);
final FragmentProgram programB = await 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));
});
}
// Expect that all of the shaders in this folder render green.
// Keeping the outer loop of the test synchronous allows for easy printing
// of the file name within the test case.
void _expectIplrShadersRenderGreen(Map<String, FragmentProgram> 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(<double>[1]),
);
await _expectShaderRendersGreen(shader);
});
}
}
void _expectFragmentShadersRenderGreen(Map<String, FragmentProgram> programs) {
for (final String key in programs.keys) {
test('FragmentProgram $key renders green', () async {

View File

@@ -3,7 +3,6 @@
// found in the LICENSE file.
import 'dart:developer' as developer;
import 'dart:typed_data';
import 'dart:ui';
import 'package:litetest/litetest.dart';
@@ -13,13 +12,12 @@ import 'package:vm_service/vm_service_io.dart';
void main() {
test('simple iplr shader can be re-initialized', () async {
vms.VmService? vmService;
FragmentShader? shader;
try {
final FragmentProgram program = await FragmentProgram.fromAsset(
'functions.frag.iplr',
);
final Shader shader = program.shader(
floatUniforms: Float32List.fromList(<double>[1]),
);
shader = program.fragmentShader()..setFloat(0, 1.0);
_use(shader);
final developer.ServiceProtocolInfo info = await developer.Service.getInfo();
@@ -46,6 +44,7 @@ void main() {
}
} finally {
await vmService?.dispose();
shader?.dispose();
}
});
}