Fix glyph sampling overlap (flutter/engine#37764)
This commit is contained in:
@@ -13,22 +13,16 @@ vec2 IPVec2TransformPosition(mat4 matrix, vec2 point) {
|
|||||||
|
|
||||||
// Returns the transformed gl_Position for a given glyph position in a glyph
|
// Returns the transformed gl_Position for a given glyph position in a glyph
|
||||||
// atlas.
|
// atlas.
|
||||||
vec4 IPPositionForGlyphPosition(mat4 mvp, vec2 unit_vertex, vec2 glyph_position, vec2 glyph_size) {
|
vec4 IPPositionForGlyphPosition(mat4 mvp,
|
||||||
vec4 translate = mvp[0] * glyph_position.x
|
vec2 unit_position,
|
||||||
+ mvp[1] * glyph_position.y
|
vec2 glyph_position,
|
||||||
+ mvp[3];
|
vec2 glyph_size) {
|
||||||
mat4 translated_mvp = mat4(
|
vec4 translate =
|
||||||
mvp[0],
|
mvp[0] * glyph_position.x + mvp[1] * glyph_position.y + mvp[3];
|
||||||
mvp[1],
|
mat4 translated_mvp =
|
||||||
mvp[2],
|
mat4(mvp[0], mvp[1], mvp[2], vec4(translate.xyz, mvp[3].w));
|
||||||
vec4(
|
return translated_mvp * vec4(unit_position.x * glyph_size.x,
|
||||||
translate.xyz,
|
unit_position.y * glyph_size.y, 0.0, 1.0);
|
||||||
mvp[3].w
|
|
||||||
)
|
|
||||||
);
|
|
||||||
return translated_mvp *
|
|
||||||
vec4(unit_vertex.x * glyph_size.x,
|
|
||||||
unit_vertex.y * glyph_size.y, 0.0, 1.0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ static bool CommonRender(
|
|||||||
SamplerDescriptor sampler_desc;
|
SamplerDescriptor sampler_desc;
|
||||||
sampler_desc.min_filter = MinMagFilter::kLinear;
|
sampler_desc.min_filter = MinMagFilter::kLinear;
|
||||||
sampler_desc.mag_filter = MinMagFilter::kLinear;
|
sampler_desc.mag_filter = MinMagFilter::kLinear;
|
||||||
|
sampler_desc.mip_filter = MipFilter::kNone;
|
||||||
|
|
||||||
typename FS::FragInfo frag_info;
|
typename FS::FragInfo frag_info;
|
||||||
frag_info.text_color = ToVector(color.Premultiply());
|
frag_info.text_color = ToVector(color.Premultiply());
|
||||||
@@ -103,9 +104,9 @@ static bool CommonRender(
|
|||||||
// interpolated vertex information is also used in the fragment shader to
|
// interpolated vertex information is also used in the fragment shader to
|
||||||
// sample from the glyph atlas.
|
// sample from the glyph atlas.
|
||||||
|
|
||||||
const std::vector<Point> unit_vertex_points = {
|
const std::array<Point, 4> unit_points = {Point{0, 0}, Point{1, 0},
|
||||||
{0, 0}, {1, 0}, {0, 1}, {1, 1}};
|
Point{0, 1}, Point{1, 1}};
|
||||||
const std::vector<uint32_t> indices = {0, 1, 2, 1, 2, 3};
|
const std::array<uint32_t, 6> indices = {0, 1, 2, 1, 2, 3};
|
||||||
|
|
||||||
VertexBufferBuilder<typename VS::PerVertexData> vertex_builder;
|
VertexBufferBuilder<typename VS::PerVertexData> vertex_builder;
|
||||||
|
|
||||||
@@ -127,7 +128,7 @@ static bool CommonRender(
|
|||||||
|
|
||||||
for (const auto& run : frame.GetRuns()) {
|
for (const auto& run : frame.GetRuns()) {
|
||||||
auto font = run.GetFont();
|
auto font = run.GetFont();
|
||||||
auto glyph_size_ = ISize::Ceil(font.GetMetrics().GetBoundingBox().size);
|
auto glyph_size_ = font.GetMetrics().GetBoundingBox().size;
|
||||||
auto glyph_size = Point{static_cast<Scalar>(glyph_size_.width),
|
auto glyph_size = Point{static_cast<Scalar>(glyph_size_.width),
|
||||||
static_cast<Scalar>(glyph_size_.height)};
|
static_cast<Scalar>(glyph_size_.height)};
|
||||||
auto metrics_offset =
|
auto metrics_offset =
|
||||||
@@ -141,22 +142,20 @@ static bool CommonRender(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto atlas_position =
|
auto atlas_position = atlas_glyph_pos->origin;
|
||||||
atlas_glyph_pos->origin + Point{1 / atlas_glyph_pos->size.width,
|
|
||||||
1 / atlas_glyph_pos->size.height};
|
|
||||||
auto atlas_glyph_size =
|
auto atlas_glyph_size =
|
||||||
Point{atlas_glyph_pos->size.width, atlas_glyph_pos->size.height};
|
Point{atlas_glyph_pos->size.width, atlas_glyph_pos->size.height};
|
||||||
auto offset_glyph_position = glyph_position.position + metrics_offset;
|
auto offset_glyph_position = glyph_position.position + metrics_offset;
|
||||||
|
|
||||||
for (const auto& point : unit_vertex_points) {
|
for (const auto& point : unit_points) {
|
||||||
typename VS::PerVertexData vtx;
|
typename VS::PerVertexData vtx;
|
||||||
vtx.unit_vertex = point;
|
vtx.unit_position = point;
|
||||||
vtx.glyph_position = offset_glyph_position;
|
vtx.destination_position = offset_glyph_position + Point(0.5, 0.5);
|
||||||
vtx.glyph_size = glyph_size;
|
vtx.destination_size = glyph_size - Point(1.0, 1.0);
|
||||||
vtx.atlas_position = atlas_position;
|
vtx.source_position = atlas_position + Point(0.5, 0.5);
|
||||||
vtx.atlas_glyph_size = atlas_glyph_size;
|
vtx.source_glyph_size = atlas_glyph_size - Point(1.0, 1.0);
|
||||||
if constexpr (std::is_same_v<TPipeline, GlyphAtlasPipeline>) {
|
if constexpr (std::is_same_v<TPipeline, GlyphAtlasPipeline>) {
|
||||||
vtx.color_glyph =
|
vtx.has_color =
|
||||||
glyph_position.glyph.type == Glyph::Type::kBitmap ? 1.0 : 0.0;
|
glyph_position.glyph.type == Glyph::Type::kBitmap ? 1.0 : 0.0;
|
||||||
}
|
}
|
||||||
vertex_builder.AppendVertex(std::move(vtx));
|
vertex_builder.AppendVertex(std::move(vtx));
|
||||||
|
|||||||
@@ -7,27 +7,25 @@ uniform sampler2D glyph_atlas_sampler;
|
|||||||
uniform FragInfo {
|
uniform FragInfo {
|
||||||
vec2 atlas_size;
|
vec2 atlas_size;
|
||||||
vec4 text_color;
|
vec4 text_color;
|
||||||
} frag_info;
|
}
|
||||||
|
frag_info;
|
||||||
|
|
||||||
in vec2 v_unit_vertex;
|
in vec2 v_unit_position;
|
||||||
in vec2 v_atlas_position;
|
in vec2 v_source_position;
|
||||||
in vec2 v_atlas_glyph_size;
|
in vec2 v_source_glyph_size;
|
||||||
in float v_color_glyph;
|
in float v_has_color;
|
||||||
|
|
||||||
out vec4 frag_color;
|
out vec4 frag_color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec2 scale_perspective = v_atlas_glyph_size / frag_info.atlas_size;
|
vec2 uv_size = v_source_glyph_size / frag_info.atlas_size;
|
||||||
vec2 offset = v_atlas_position / frag_info.atlas_size;
|
vec2 offset = v_source_position / frag_info.atlas_size;
|
||||||
if (v_color_glyph == 1.0) {
|
if (v_has_color == 1.0) {
|
||||||
frag_color = texture(
|
frag_color =
|
||||||
glyph_atlas_sampler,
|
texture(glyph_atlas_sampler, v_unit_position * uv_size + offset);
|
||||||
v_unit_vertex * scale_perspective + offset
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
frag_color = texture(
|
frag_color =
|
||||||
glyph_atlas_sampler,
|
texture(glyph_atlas_sampler, v_unit_position * uv_size + offset).aaaa *
|
||||||
v_unit_vertex * scale_perspective + offset
|
frag_info.text_color;
|
||||||
).aaaa * frag_info.text_color;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,24 +6,26 @@
|
|||||||
|
|
||||||
uniform FrameInfo {
|
uniform FrameInfo {
|
||||||
mat4 mvp;
|
mat4 mvp;
|
||||||
} frame_info;
|
}
|
||||||
|
frame_info;
|
||||||
|
|
||||||
in vec2 unit_vertex;
|
in vec2 unit_position;
|
||||||
in vec2 glyph_position;
|
in vec2 destination_position;
|
||||||
in vec2 glyph_size;
|
in vec2 destination_size;
|
||||||
in vec2 atlas_position;
|
in vec2 source_position;
|
||||||
in vec2 atlas_glyph_size;
|
in vec2 source_glyph_size;
|
||||||
in float color_glyph;
|
in float has_color;
|
||||||
|
|
||||||
out vec2 v_unit_vertex;
|
out vec2 v_unit_position;
|
||||||
out vec2 v_atlas_position;
|
out vec2 v_source_position;
|
||||||
out vec2 v_atlas_glyph_size;
|
out vec2 v_source_glyph_size;
|
||||||
out float v_color_glyph;
|
out float v_has_color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = IPPositionForGlyphPosition(frame_info.mvp, unit_vertex, glyph_position, glyph_size);
|
gl_Position = IPPositionForGlyphPosition(
|
||||||
v_unit_vertex = unit_vertex;
|
frame_info.mvp, unit_position, destination_position, destination_size);
|
||||||
v_atlas_position = atlas_position;
|
v_unit_position = unit_position;
|
||||||
v_atlas_glyph_size = atlas_glyph_size;
|
v_source_position = source_position;
|
||||||
v_color_glyph = color_glyph;
|
v_source_glyph_size = source_glyph_size;
|
||||||
|
v_has_color = has_color;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,28 +7,35 @@ uniform sampler2D glyph_atlas_sampler;
|
|||||||
uniform FragInfo {
|
uniform FragInfo {
|
||||||
vec2 atlas_size;
|
vec2 atlas_size;
|
||||||
vec4 text_color;
|
vec4 text_color;
|
||||||
} frag_info;
|
}
|
||||||
|
frag_info;
|
||||||
|
|
||||||
in vec2 v_unit_vertex;
|
in vec2 v_unit_position;
|
||||||
in vec2 v_atlas_position;
|
in vec2 v_source_position;
|
||||||
in vec2 v_atlas_glyph_size;
|
in vec2 v_source_glyph_size;
|
||||||
|
|
||||||
out vec4 frag_color;
|
out vec4 frag_color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec2 scale_perspective = v_atlas_glyph_size / frag_info.atlas_size;
|
vec2 scale_perspective = v_source_glyph_size / frag_info.atlas_size;
|
||||||
vec2 offset = v_atlas_position / frag_info.atlas_size;
|
vec2 offset = v_source_position / frag_info.atlas_size;
|
||||||
|
|
||||||
// Inspired by Metal by Example's SDF text rendering shader:
|
// Inspired by Metal by Example's SDF text rendering shader:
|
||||||
// https://github.com/metal-by-example/sample-code/blob/master/objc/12-TextRendering/TextRendering/Shaders.metal
|
// https://github.com/metal-by-example/sample-code/blob/master/objc/12-TextRendering/TextRendering/Shaders.metal
|
||||||
|
|
||||||
// Outline of glyph is the isocontour with value 50%
|
// Outline of glyph is the isocontour with value 50%
|
||||||
float edge_distance = 0.5;
|
float edge_distance = 0.5;
|
||||||
// Sample the signed-distance field to find distance from this fragment to the glyph outline
|
// Sample the signed-distance field to find distance from this fragment to the
|
||||||
float sample_distance = texture(glyph_atlas_sampler, v_unit_vertex * scale_perspective + offset).a;
|
// glyph outline
|
||||||
// Use local automatic gradients to find anti-aliased anisotropic edge width, cf. Gustavson 2012
|
float sample_distance =
|
||||||
|
texture(glyph_atlas_sampler, v_unit_position * scale_perspective + offset)
|
||||||
|
.a;
|
||||||
|
// Use local automatic gradients to find anti-aliased anisotropic edge width,
|
||||||
|
// cf. Gustavson 2012
|
||||||
float edge_width = length(vec2(dFdx(sample_distance), dFdy(sample_distance)));
|
float edge_width = length(vec2(dFdx(sample_distance), dFdy(sample_distance)));
|
||||||
// Smooth the glyph edge by interpolating across the boundary in a band with the width determined above
|
// Smooth the glyph edge by interpolating across the boundary in a band with
|
||||||
float insideness = smoothstep(edge_distance - edge_width, edge_distance + edge_width, sample_distance);
|
// the width determined above
|
||||||
|
float insideness = smoothstep(edge_distance - edge_width,
|
||||||
|
edge_distance + edge_width, sample_distance);
|
||||||
frag_color = frag_info.text_color * insideness;
|
frag_color = frag_info.text_color * insideness;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,21 +6,23 @@
|
|||||||
|
|
||||||
uniform FrameInfo {
|
uniform FrameInfo {
|
||||||
mat4 mvp;
|
mat4 mvp;
|
||||||
} frame_info;
|
}
|
||||||
|
frame_info;
|
||||||
|
|
||||||
in vec2 unit_vertex;
|
in vec2 unit_position;
|
||||||
in vec2 glyph_position;
|
in vec2 destination_position;
|
||||||
in vec2 glyph_size;
|
in vec2 destination_size;
|
||||||
in vec2 atlas_position;
|
in vec2 source_position;
|
||||||
in vec2 atlas_glyph_size;
|
in vec2 source_glyph_size;
|
||||||
|
|
||||||
out vec2 v_unit_vertex;
|
out vec2 v_unit_position;
|
||||||
out vec2 v_atlas_position;
|
out vec2 v_source_position;
|
||||||
out vec2 v_atlas_glyph_size;
|
out vec2 v_source_glyph_size;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = IPPositionForGlyphPosition(frame_info.mvp, unit_vertex, glyph_position, glyph_size);
|
gl_Position = IPPositionForGlyphPosition(
|
||||||
v_unit_vertex = unit_vertex;
|
frame_info.mvp, unit_position, destination_position, destination_size);
|
||||||
v_atlas_position = atlas_position;
|
v_unit_position = unit_position;
|
||||||
v_atlas_glyph_size = atlas_glyph_size;
|
v_source_position = source_position;
|
||||||
|
v_source_glyph_size = source_glyph_size;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,25 +10,25 @@ uniform FrameInfo {
|
|||||||
|
|
||||||
in vec2 unit_vertex;
|
in vec2 unit_vertex;
|
||||||
in mat4 glyph_position; // <--- Causes multiple slots to be used and is a failure.
|
in mat4 glyph_position; // <--- Causes multiple slots to be used and is a failure.
|
||||||
in vec2 glyph_size;
|
in vec2 destination_size;
|
||||||
in vec2 atlas_position;
|
in vec2 source_position;
|
||||||
in vec2 atlas_glyph_size;
|
in vec2 source_glyph_size;
|
||||||
|
|
||||||
out vec2 v_unit_vertex;
|
out vec2 v_unit_vertex;
|
||||||
out vec2 v_atlas_position;
|
out vec2 v_source_position;
|
||||||
out vec2 v_atlas_glyph_size;
|
out vec2 v_source_glyph_size;
|
||||||
out vec2 v_atlas_size;
|
out vec2 v_atlas_size;
|
||||||
out vec4 v_text_color;
|
out vec4 v_text_color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = frame_info.mvp
|
gl_Position = frame_info.mvp
|
||||||
* glyph_position
|
* glyph_position
|
||||||
* vec4(unit_vertex.x * glyph_size.x,
|
* vec4(unit_vertex.x * destination_size.x,
|
||||||
unit_vertex.y * glyph_size.y, 0.0, 1.0);
|
unit_vertex.y * destination_size.y, 0.0, 1.0);
|
||||||
|
|
||||||
v_unit_vertex = unit_vertex;
|
v_unit_vertex = unit_vertex;
|
||||||
v_atlas_position = atlas_position;
|
v_source_position = source_position;
|
||||||
v_atlas_glyph_size = atlas_glyph_size;
|
v_source_glyph_size = source_glyph_size;
|
||||||
v_atlas_size = frame_info.atlas_size;
|
v_atlas_size = frame_info.atlas_size;
|
||||||
v_text_color = frame_info.text_color;
|
v_text_color = frame_info.text_color;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,15 +71,19 @@ static size_t PairsFitInAtlasOfSize(const FontGlyphPair::Vector& pairs,
|
|||||||
glyph_positions.clear();
|
glyph_positions.clear();
|
||||||
glyph_positions.reserve(pairs.size());
|
glyph_positions.reserve(pairs.size());
|
||||||
|
|
||||||
|
// TODO(114563): We might be able to remove this per-glyph padding if we fix
|
||||||
|
// the underlying causes of the overlap.
|
||||||
|
constexpr auto padding = 2;
|
||||||
|
|
||||||
for (size_t i = 0; i < pairs.size(); i++) {
|
for (size_t i = 0; i < pairs.size(); i++) {
|
||||||
const auto& pair = pairs[i];
|
const auto& pair = pairs[i];
|
||||||
const auto glyph_size =
|
const auto glyph_size =
|
||||||
ISize::Ceil(pair.font.GetMetrics().GetBoundingBox().size *
|
ISize::Ceil(pair.font.GetMetrics().GetBoundingBox().size *
|
||||||
pair.font.GetMetrics().scale);
|
pair.font.GetMetrics().scale);
|
||||||
SkIPoint16 location_in_atlas;
|
SkIPoint16 location_in_atlas;
|
||||||
if (!rect_packer->addRect(glyph_size.width, //
|
if (!rect_packer->addRect(glyph_size.width + padding, //
|
||||||
glyph_size.height, //
|
glyph_size.height + padding, //
|
||||||
&location_in_atlas //
|
&location_in_atlas //
|
||||||
)) {
|
)) {
|
||||||
return pairs.size() - i;
|
return pairs.size() - i;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user