[Impeller] move most texture flips to vertex shader (flutter/engine#39872)

[Impeller] move most texture flips to vertex shader
This commit is contained in:
Jonah Williams 2023-02-27 12:07:05 -08:00 committed by GitHub
parent e52501822b
commit 2fb0258c8c
23 changed files with 71 additions and 84 deletions

View File

@ -204,9 +204,9 @@ bool AtlasTextureContents::Render(const ContentContext& renderer,
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
entity.GetTransformation();
frame_info.texture_sampler_y_coord_scale = texture->GetYCoordScale();
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale = texture->GetYCoordScale();
frag_info.alpha = alpha_;
auto options = OptionsFromPassAndEntity(pass, entity);

View File

@ -102,15 +102,17 @@ std::optional<Entity> BorderMaskBlurFilterContents::RenderFilter(
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
frame_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
auto sigma = effect_transform * Vector2(sigma_x_.sigma, sigma_y_.sigma);
frame_info.sigma_uv = sigma.Abs() / input_snapshot->texture->GetSize();
frame_info.src_factor = src_color_factor_;
frame_info.inner_blur_factor = inner_blur_factor_;
frame_info.outer_blur_factor = outer_blur_factor_;
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
frag_info.sigma_uv = sigma.Abs() / input_snapshot->texture->GetSize();
frag_info.src_factor = src_color_factor_;
frag_info.inner_blur_factor = inner_blur_factor_;
frag_info.outer_blur_factor = outer_blur_factor_;
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));

View File

@ -73,12 +73,12 @@ std::optional<Entity> ColorMatrixFilterContents::RenderFilter(
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
frame_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
FS::FragInfo frag_info;
const float* matrix = matrix_.array;
frag_info.color_v = Vector4(matrix[4], matrix[9], matrix[14], matrix[19]);
frag_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
// clang-format off
frag_info.color_m = Matrix(
matrix[0], matrix[5], matrix[10], matrix[15],

View File

@ -60,10 +60,10 @@ std::optional<Entity> LinearToSrgbFilterContents::RenderFilter(
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
frame_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
frag_info.input_alpha = GetAbsorbOpacity() ? input_snapshot->opacity : 1.0f;
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});

View File

@ -90,6 +90,9 @@ std::optional<Entity> DirectionalMorphologyFilterContents::RenderFilter(
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
frame_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
auto transform = entity.GetTransformation() * effect_transform;
auto transformed_radius =
transform.TransformDirection(direction_ * radius_.radius);
@ -104,8 +107,6 @@ std::optional<Entity> DirectionalMorphologyFilterContents::RenderFilter(
transformed_texture_vertices[2]);
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
frag_info.radius = std::round(transformed_radius.GetLength());
frag_info.direction = input_snapshot->transform.Invert()
.TransformDirection(transformed_radius)

View File

@ -60,10 +60,10 @@ std::optional<Entity> SrgbToLinearFilterContents::RenderFilter(
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
frame_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
frag_info.input_alpha = GetAbsorbOpacity() ? input_snapshot->opacity : 1.0f;
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});

View File

@ -87,10 +87,10 @@ std::optional<Entity> YUVToRGBFilterContents::RenderFilter(
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
frame_info.texture_sampler_y_coord_scale =
y_input_snapshot->texture->GetYCoordScale();
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale =
y_input_snapshot->texture->GetYCoordScale();
frag_info.yuv_color_space = static_cast<Scalar>(yuv_color_space_);
switch (yuv_color_space_) {
case YUVColorSpace::kBT601LimitedRange:

View File

@ -162,9 +162,9 @@ bool TextureContents::Render(const ContentContext& renderer,
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
entity.GetTransformation();
frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
frag_info.alpha = opacity_;
Command cmd;

View File

@ -18,37 +18,38 @@
uniform sampler2D texture_sampler;
uniform FragInfo {
float texture_sampler_y_coord_scale;
float src_factor;
float inner_blur_factor;
float outer_blur_factor;
vec2 sigma_uv;
}
frag_info;
in vec2 v_texture_coords;
in vec2 v_sigma_uv;
in float v_src_factor;
in float v_inner_blur_factor;
in float v_outer_blur_factor;
out vec4 frag_color;
float BoxBlurMask(vec2 uv) {
// LTRB
return IPGaussianIntegral(uv.x, v_sigma_uv.x) * //
IPGaussianIntegral(uv.y, v_sigma_uv.y) * //
IPGaussianIntegral(1 - uv.x, v_sigma_uv.x) * //
IPGaussianIntegral(1 - uv.y, v_sigma_uv.y);
return IPGaussianIntegral(uv.x, frag_info.sigma_uv.x) * //
IPGaussianIntegral(uv.y, frag_info.sigma_uv.y) * //
IPGaussianIntegral(1 - uv.x, frag_info.sigma_uv.x) * //
IPGaussianIntegral(1 - uv.y, frag_info.sigma_uv.y);
}
void main() {
vec4 image_color = IPSample(texture_sampler, v_texture_coords,
frag_info.texture_sampler_y_coord_scale);
vec4 image_color = texture(texture_sampler, v_texture_coords);
float blur_factor = BoxBlurMask(v_texture_coords);
float within_bounds =
float(v_texture_coords.x >= 0 && v_texture_coords.y >= 0 &&
v_texture_coords.x < 1 && v_texture_coords.y < 1);
float inner_factor =
(v_inner_blur_factor * blur_factor + v_src_factor) * within_bounds;
float outer_factor = v_outer_blur_factor * blur_factor * (1 - within_bounds);
(frag_info.inner_blur_factor * blur_factor + frag_info.src_factor) *
within_bounds;
float outer_factor =
frag_info.outer_blur_factor * blur_factor * (1 - within_bounds);
float mask_factor = inner_factor + outer_factor;
frag_color = image_color * mask_factor;

View File

@ -2,16 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
uniform FrameInfo {
mat4 mvp;
vec2 sigma_uv;
float src_factor;
float inner_blur_factor;
float outer_blur_factor;
float texture_sampler_y_coord_scale;
}
frame_info;
@ -19,16 +16,9 @@ in vec2 vertices;
in vec2 texture_coords;
out vec2 v_texture_coords;
out vec2 v_sigma_uv;
out float v_src_factor;
out float v_inner_blur_factor;
out float v_outer_blur_factor;
void main() {
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
v_texture_coords = texture_coords;
v_sigma_uv = frame_info.sigma_uv;
v_src_factor = frame_info.src_factor;
v_inner_blur_factor = frame_info.inner_blur_factor;
v_outer_blur_factor = frame_info.outer_blur_factor;
v_texture_coords =
IPRemapCoords(texture_coords, frame_info.texture_sampler_y_coord_scale);
}

View File

@ -32,7 +32,6 @@
uniform FragInfo {
mat4 color_m;
vec4 color_v;
float texture_sampler_y_coord_scale;
float input_alpha;
}
frag_info;
@ -43,9 +42,7 @@ in vec2 v_position;
out vec4 frag_color;
void main() {
vec4 input_color = IPSample(input_texture, v_position,
frag_info.texture_sampler_y_coord_scale) *
frag_info.input_alpha;
vec4 input_color = texture(input_texture, v_position) * frag_info.input_alpha;
// unpremultiply first, as filter inputs are premultiplied.
vec4 color = IPUnpremultiply(input_color);

View File

@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
uniform FrameInfo {
mat4 mvp;
float texture_sampler_y_coord_scale;
}
frame_info;
@ -13,6 +15,7 @@ in vec2 position;
out vec2 v_position;
void main() {
v_position = position;
v_position =
IPRemapCoords(position, frame_info.texture_sampler_y_coord_scale);
gl_Position = frame_info.mvp * vec4(position, 0.0, 1.0);
}

View File

@ -3,7 +3,6 @@
// found in the LICENSE file.
#include <impeller/color.glsl>
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
// A color filter that applies the sRGB gamma curve to the color.
@ -13,7 +12,6 @@
uniform sampler2D input_texture;
uniform FragInfo {
float texture_sampler_y_coord_scale;
float input_alpha;
}
frag_info;
@ -22,9 +20,7 @@ in vec2 v_position;
out vec4 frag_color;
void main() {
vec4 input_color = IPSample(input_texture, v_position,
frag_info.texture_sampler_y_coord_scale) *
frag_info.input_alpha;
vec4 input_color = texture(input_texture, v_position) * frag_info.input_alpha;
vec4 color = IPUnpremultiply(input_color);
for (int i = 0; i < 3; i++) {

View File

@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
uniform FrameInfo {
mat4 mvp;
float texture_sampler_y_coord_scale;
}
frame_info;
@ -13,6 +15,7 @@ in vec2 position;
out vec2 v_position;
void main() {
v_position = position;
v_position =
IPRemapCoords(position, frame_info.texture_sampler_y_coord_scale);
gl_Position = frame_info.mvp * vec4(position, 0.0, 1.0);
}

View File

@ -14,7 +14,6 @@ const float kMorphTypeErode = 1;
uniform sampler2D texture_sampler;
uniform FragInfo {
float texture_sampler_y_coord_scale;
vec2 texture_size;
vec2 direction;
float radius;
@ -31,12 +30,7 @@ void main() {
for (float i = -frag_info.radius; i <= frag_info.radius; i++) {
vec2 texture_coords = v_texture_coords + uv_offset * i;
vec4 color;
color = IPSampleWithTileMode(
texture_sampler, // sampler
texture_coords, // texture coordinates
frag_info.texture_sampler_y_coord_scale, // y coordinate scale
kTileModeDecal // tile mode
);
color = IPSampleDecal(texture_sampler, texture_coords);
if (frag_info.morph_type == kMorphTypeDilate) {
result = max(color, result);
} else {

View File

@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
uniform FrameInfo {
mat4 mvp;
float texture_sampler_y_coord_scale;
}
frame_info;
@ -16,5 +18,6 @@ out vec2 v_texture_coords;
void main() {
gl_Position = frame_info.mvp * vec4(position, 0.0, 1.0);
v_texture_coords = texture_coords;
v_texture_coords =
IPRemapCoords(texture_coords, frame_info.texture_sampler_y_coord_scale);
}

View File

@ -3,7 +3,6 @@
// found in the LICENSE file.
#include <impeller/color.glsl>
#include <impeller/texture.glsl>
// Creates a color filter that applies the inverse of the sRGB gamma curve
// to the RGB channels.
@ -13,7 +12,6 @@
uniform sampler2D input_texture;
uniform FragInfo {
float texture_sampler_y_coord_scale;
float input_alpha;
}
frag_info;
@ -22,9 +20,7 @@ in vec2 v_position;
out vec4 frag_color;
void main() {
vec4 input_color = IPSample(input_texture, v_position,
frag_info.texture_sampler_y_coord_scale) *
frag_info.input_alpha;
vec4 input_color = texture(input_texture, v_position) * frag_info.input_alpha;
vec4 color = IPUnpremultiply(input_color);
for (int i = 0; i < 3; i++) {

View File

@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
uniform FrameInfo {
mat4 mvp;
float texture_sampler_y_coord_scale;
}
frame_info;
@ -13,6 +15,7 @@ in vec2 position;
out vec2 v_position;
void main() {
v_position = position;
v_position =
IPRemapCoords(position, frame_info.texture_sampler_y_coord_scale);
gl_Position = frame_info.mvp * vec4(position, 0.0, 1.0);
}

View File

@ -2,13 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
uniform sampler2D texture_sampler;
uniform FragInfo {
float texture_sampler_y_coord_scale;
float alpha;
}
frag_info;
@ -18,7 +16,6 @@ in vec2 v_texture_coords;
out vec4 frag_color;
void main() {
vec4 sampled = IPSample(texture_sampler, v_texture_coords,
frag_info.texture_sampler_y_coord_scale);
vec4 sampled = texture(texture_sampler, v_texture_coords);
frag_color = sampled * frag_info.alpha;
}

View File

@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
uniform FrameInfo {
mat4 mvp;
float texture_sampler_y_coord_scale;
}
frame_info;
@ -16,5 +18,6 @@ out vec2 v_texture_coords;
void main() {
gl_Position = frame_info.mvp * vec4(position, 0.0, 1.0);
v_texture_coords = texture_coords;
v_texture_coords =
IPRemapCoords(texture_coords, frame_info.texture_sampler_y_coord_scale);
}

View File

@ -15,7 +15,6 @@ const float kBT601LimitedRange = 0;
const float kBT601FullRange = 1;
uniform FragInfo {
float texture_sampler_y_coord_scale;
mat4 matrix;
float yuv_color_space;
}
@ -31,11 +30,7 @@ void main() {
yuv_offset.x = 16.0 / 255.0;
}
yuv.x =
IPSample(y_texture, v_position, frag_info.texture_sampler_y_coord_scale)
.r;
yuv.yz =
IPSample(uv_texture, v_position, frag_info.texture_sampler_y_coord_scale)
.rg;
yuv.x = texture(y_texture, v_position).r;
yuv.yz = texture(uv_texture, v_position).rg;
frag_color = frag_info.matrix * vec4(yuv - yuv_offset, 1);
}

View File

@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <impeller/texture.glsl>
#include <impeller/types.glsl>
uniform FrameInfo {
mat4 mvp;
float texture_sampler_y_coord_scale;
}
frame_info;
@ -13,6 +15,7 @@ in vec2 position;
out vec2 v_position;
void main() {
v_position = position;
v_position =
IPRemapCoords(position, frame_info.texture_sampler_y_coord_scale);
gl_Position = frame_info.mvp * vec4(position, 0.0, 1.0);
}

File diff suppressed because one or more lines are too long