ImageFilter.shader constructor

ImageFilter.shader(
  1. FragmentShader shader
)

Creates an image filter from a FragmentShader.

The fragment shader provided here has additional requirements to be used by the engine for filtering. The first uniform value must be a vec2, this will be set by the engine to the size of the bound texture. There must also be at least one sampler2D uniform, the first of which will be set by the engine to contain the filter input.

For example, the following is a valid fragment shader that can be used with this API. Note that the uniform names are not required to have any particular value.

#include <flutter/runtime_effect.glsl>

uniform vec2 u_size;
uniform float u_time;

uniform sampler2D u_texture_input;

out vec4 frag_color;

void main() {
  frag_color = texture(u_texture_input, FlutterFragCoord().xy / u_size) * u_time;

}

This API is only supported when using the Impeller rendering engine. On other backends a UnsupportedError will be thrown. To check at runtime whether this API is suppored use isShaderFilterSupported.

Implementation

factory ImageFilter.shader(FragmentShader shader) {
  if (!_impellerEnabled) {
    throw UnsupportedError('ImageFilter.shader only supported with Impeller rendering engine.');
  }
  final bool invalidFloats = shader._floats.length < 2;
  final bool invalidSampler = !shader._validateImageFilter();
  if (invalidFloats || invalidSampler) {
    final StringBuffer buffer = StringBuffer(
      'ImageFilter.shader requires that the first uniform is a vec2 and at '
      'least one sampler uniform is present.\n',
    );
    if (invalidFloats) {
      buffer.write('The shader has fewer than two float uniforms.\n');
    }
    if (invalidSampler) {
      buffer.write('The shader is missing a sampler uniform.\n');
    }
  }
  return _FragmentShaderImageFilter(shader);
}