Unify tiling functionality in shader lib (flutter/engine#35245)

This commit is contained in:
Brandon DeRosier
2022-08-08 16:25:50 -07:00
committed by GitHub
parent a015114478
commit 0ac321bc81
7 changed files with 54 additions and 30 deletions

View File

@@ -6,6 +6,11 @@
#define CONSTANTS_GLSL_
const float kEhCloseEnough = 0.000001;
// 1 / (2 * pi)
const float k1Over2Pi = 0.1591549430918;
// sqrt(2 * pi)
const float kSqrtTwoPi = 2.50662827463;
#endif

View File

@@ -19,16 +19,6 @@ vec4 IPSample(sampler2D texture_sampler, vec2 coords, float y_coord_scale) {
return texture(texture_sampler, coords);
}
/// Sample a texture, emulating SamplerAddressMode::ClampToBorder.
///
/// This is useful for Impeller graphics backend that don't support
/// ClampToBorder.
vec4 IPSampleClampToBorder(sampler2D tex, vec2 uv) {
float within_bounds = float(uv.x > 0 && uv.y > 0 && uv.x < 1 && uv.y < 1);
return texture(tex, uv) * within_bounds;
}
// These values must correspond to the order of the items in the
// 'Entity::TileMode' enum class.
const float kTileModeClamp = 0;
@@ -36,10 +26,13 @@ const float kTileModeRepeat = 1;
const float kTileModeMirror = 2;
const float kTileModeDecal = 3;
/// Compute an interpolant value "t".
/// Remap a float using a tiling mode.
///
/// The domain appears to be any value and the range is [0 to 1].
float IPTileTextureCoords(float t, float tile_mode) {
/// When `tile_mode` is `kTileModeDecal`, no tiling is applied and `t` is
/// returned. In all other cases, a value between 0 and 1 is returned by tiling
/// `t`.
/// When `t` is between [0 to 1), the original unchanged `t` is always returned.
float IPFloatTile(float t, float tile_mode) {
if (tile_mode == kTileModeClamp) {
t = clamp(t, 0.0, 1.0);
} else if (tile_mode == kTileModeRepeat) {
@@ -52,4 +45,24 @@ float IPTileTextureCoords(float t, float tile_mode) {
return t;
}
/// Remap a vec2 using a tiling mode.
///
/// Runs each component of the vec2 through `IPFloatTile`.
vec2 IPVec2Tile(vec2 uv, float tile_mode) {
return vec2(IPFloatTile(uv.x, tile_mode), IPFloatTile(uv.y, tile_mode));
}
/// Sample a texture, emulating a specific tile mode.
///
/// This is useful for Impeller graphics backend that don't have native support
/// for Decal.
vec4 IPSampleWithTileMode(sampler2D tex, vec2 uv, float tile_mode) {
if (tile_mode == kTileModeDecal &&
(uv.x < 0 || uv.y < 0 || uv.x >= 1 || uv.y >= 1)) {
return vec4(0);
}
return texture(tex, IPVec2Tile(uv, tile_mode));
}
#endif

View File

@@ -21,12 +21,13 @@ in vec2 v_src_texture_coords;
out vec4 frag_color;
void main() {
vec4 dst = IPUnpremultiply(
IPSampleClampToBorder(texture_sampler_dst, v_dst_texture_coords));
vec4 src = blend_info.color_factor > 0
? blend_info.color
: IPUnpremultiply(IPSampleClampToBorder(texture_sampler_src,
v_src_texture_coords));
vec4 dst = IPUnpremultiply(IPSampleWithTileMode(
texture_sampler_dst, v_dst_texture_coords, kTileModeDecal));
vec4 src =
blend_info.color_factor > 0
? blend_info.color
: IPUnpremultiply(IPSampleWithTileMode(
texture_sampler_src, v_src_texture_coords, kTileModeDecal));
vec3 blended = Blend(dst.rgb, src.rgb);

View File

@@ -7,8 +7,13 @@
// Paths for future optimization:
// * Remove the uv bounds multiplier in SampleColor by adding optional
// support for SamplerAddressMode::ClampToBorder in the texture sampler.
// * Sample from higher mipmap levels when the blur radius is high enough.
// * Render both blur passes into a smaller texture than the source image
// (~1/radius size).
// * If doing the small texture render optimization, cache misses can be
// reduced in the first pass by sampling the source textures with a mip
// level of log2(min_radius).
#include <impeller/constants.glsl>
#include <impeller/texture.glsl>
uniform sampler2D texture_sampler;
@@ -23,15 +28,14 @@ uniform FragInfo {
float src_factor;
float inner_blur_factor;
float outer_blur_factor;
} frag_info;
}
frag_info;
in vec2 v_texture_coords;
in vec2 v_src_texture_coords;
out vec4 frag_color;
const float kSqrtTwoPi = 2.50662827463;
float Gaussian(float x) {
float variance = frag_info.blur_sigma * frag_info.blur_sigma;
return exp(-0.5 * x * x / variance) / (kSqrtTwoPi * frag_info.blur_sigma);
@@ -46,14 +50,15 @@ void main() {
float gaussian = Gaussian(i);
gaussian_integral += gaussian;
total_color +=
gaussian * IPSampleClampToBorder(texture_sampler,
v_texture_coords + blur_uv_offset * i);
gaussian * IPSampleWithTileMode(texture_sampler,
v_texture_coords + blur_uv_offset * i,
kTileModeDecal);
}
vec4 blur_color = total_color / gaussian_integral;
vec4 src_color =
IPSampleClampToBorder(alpha_mask_sampler, v_src_texture_coords);
vec4 src_color = IPSampleWithTileMode(alpha_mask_sampler,
v_src_texture_coords, kTileModeDecal);
float blur_factor = frag_info.inner_blur_factor * float(src_color.a > 0) +
frag_info.outer_blur_factor * float(src_color.a == 0);

View File

@@ -28,6 +28,6 @@ void main() {
return;
}
t = IPTileTextureCoords(t, gradient_info.tile_mode);
t = IPFloatTile(t, gradient_info.tile_mode);
frag_color = mix(gradient_info.start_color, gradient_info.end_color, t);
}

View File

@@ -24,6 +24,6 @@ void main() {
return;
}
t = IPTileTextureCoords(t, gradient_info.tile_mode);
t = IPFloatTile(t, gradient_info.tile_mode);
frag_color = mix(gradient_info.center_color, gradient_info.edge_color, t);
}

View File

@@ -28,6 +28,6 @@ void main() {
return;
}
t = IPTileTextureCoords(t, gradient_info.tile_mode);
t = IPFloatTile(t, gradient_info.tile_mode);
frag_color = mix(gradient_info.start_color, gradient_info.end_color, t);
}