text
stringlengths
1
372
given in the pubspec.yaml file.
the FragmentProgram object can be used to create
one or more FragmentShader instances.
a FragmentShader object represents a fragment program
along with a particular set of uniforms (configuration parameters).
the available uniforms depends on how the shader was defined.
<topic_end>
<topic_start>
canvas API
fragment shaders can be used with most canvas APIs
by setting paint.shader.
for example, when using Canvas.drawRect
the shader is evaluated for all fragments within the rectangle.
for an API like Canvas.drawPath with a stroked path,
the shader is evaluated for all fragments within the stroked line.
some APIs, such as Canvas.drawImage, ignore the value of the shader.
<topic_end>
<topic_start>
authoring shaders
fragment shaders are authored as GLSL source files.
by convention, these files have the .frag extension.
(flutter doesn’t support vertex shaders,
which would have the .vert extension.)
any GLSL version from 460 down to 100 is supported,
though some available features are restricted.
the rest of the examples in this document use version 460 core.
shaders are subject to the following limitations
when used with flutter:
<topic_end>
<topic_start>
uniforms
a fragment program can be configured by defining
uniform values in the GLSL shader source
and then setting these values in dart for
each fragment shader instance.
floating point uniforms with the GLSL types
float, vec2, vec3, and vec4
are set using the FragmentShader.setFloat method.
GLSL sampler values, which use the sampler2D type,
are set using the FragmentShader.setImageSampler method.
the correct index for each uniform value is determined by the order
that the uniform values are defined in the fragment program.
for data types composed of multiple floats, such as a vec4,
you must call FragmentShader.setFloat once for each value.
for example, given the following uniforms declarations in a GLSL fragment program:
the corresponding dart code to initialize these uniform values is as follows:
observe that the indices used with FragmentShader.setFloat
do not count the sampler2D uniform.
this uniform is set separately with FragmentShader.setImageSampler,
with the index starting over at 0.
any float uniforms that are left uninitialized will default to 0.0.
<topic_end>
<topic_start>
current position
the shader has access to a varying value that contains the local coordinates for
the particular fragment being evaluated. use this feature to compute
effects that depend on the current position, which can be accessed by
importing the flutter/runtime_effect.glsl library and calling the
FlutterFragCoord function. for example:
the value returned from FlutterFragCoord is distinct from gl_FragCoord.
gl_FragCoord provides the screen space coordinates and should generally be
avoided to ensure that shaders are consistent across backends.
when targeting a skia backend,
the calls to gl_FragCoord are rewritten to access local
coordinates but this rewriting isn’t possible with impeller.
<topic_end>
<topic_start>
colors
there isn’t a built-in data type for colors.
instead they are commonly represented as a vec4
with each component corresponding to one of the RGBA
color channels.
the single output fragColor expects that the color value
is normalized to be in the range of 0.0 to 1.0
and that it has premultiplied alpha.
this is different than typical flutter colors which use
a 0-255 value encoding and have unpremultipled alpha.
<topic_end>
<topic_start>
samplers
a sampler provides access to a dart:ui image object.
this image can be acquired either from a decoded image
or from part of the application using
Scene.toImageSync or Picture.toImageSync.
by default, the image uses
TileMode.clamp to determine how values outside
of the range of [0, 1] behave.
customization of the tile mode is not
supported and needs to be emulated in the shader.
<topic_end>
<topic_start>
performance considerations
when targeting the skia backend,
loading the shader might be expensive since it
must be compiled to the appropriate
platform-specific shader at runtime.
if you intend to use one or more shaders during an animation,
consider precaching the fragment program objects before
starting the animation.
you can reuse a FragmentShader object across frames;