UI: Improved overlay text contrast with new outline text decoration
Overlay texts were previously drawn with two sets of shadows:
- 3px blur,
- 5px blur, slightly offset
But since the shadow color was always set to black, it was still
causing legibility issues when the text itself was dark (set
via theme for example).
This PR adds a new "outline" BLF text decoration, and uses that
for the overlays. And it picks text/outline color depending
on the "background" color of the view.
Details:
- Instead of "shadow level" integer where the only valid options
are 0, 3 or 5, have a FontShadowType enum.
- Add a new FontShadowType::Outline enum entry, that does a 1px
outline by doing a 3x3 dilation in the font shader.
- BLF_draw_default_shadowed is changed to do outline, instead of
drawing the shadow twice.
- In the font shader, instead of encoding shadow type in signs of
the glyph_size, pass that as a "flags" vertex attribute. Put
font texture channel count into the same flags, so that the
vertex size stays the same.
- Well actually, vertex size becomes smaller by 4 bytes, since turns
out glyph_mode vertex attribute was not used for anything at all.
Images in the PR.
Co-authored-by: Harley Acheson <harley.acheson@gmail.com>
Pull Request: https://projects.blender.org/blender/blender/pulls/121383
2024-05-10 21:06:44 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2016-2024 Blender Authors
|
2023-08-24 10:54:59 +10:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2016-09-17 13:33:02 +02:00
|
|
|
|
2025-09-25 10:57:02 +02:00
|
|
|
#include "infos/gpu_shader_text_infos.hh"
|
2024-11-12 18:53:34 +01:00
|
|
|
|
|
|
|
|
VERTEX_SHADER_CREATE_INFO(gpu_shader_text)
|
|
|
|
|
|
2016-09-17 13:33:02 +02:00
|
|
|
void main()
|
|
|
|
|
{
|
2025-08-27 14:52:19 +02:00
|
|
|
int glyph_index = gl_InstanceID;
|
|
|
|
|
|
|
|
|
|
color_flat = glyphs[glyph_index].glyph_color;
|
|
|
|
|
glyph_offset = glyphs[glyph_index].offset;
|
|
|
|
|
glyph_dim = glyphs[glyph_index].glyph_size;
|
|
|
|
|
glyph_flags = glyphs[glyph_index].flags;
|
UI: Improved overlay text contrast with new outline text decoration
Overlay texts were previously drawn with two sets of shadows:
- 3px blur,
- 5px blur, slightly offset
But since the shadow color was always set to black, it was still
causing legibility issues when the text itself was dark (set
via theme for example).
This PR adds a new "outline" BLF text decoration, and uses that
for the overlays. And it picks text/outline color depending
on the "background" color of the view.
Details:
- Instead of "shadow level" integer where the only valid options
are 0, 3 or 5, have a FontShadowType enum.
- Add a new FontShadowType::Outline enum entry, that does a 1px
outline by doing a 3x3 dilation in the font shader.
- BLF_draw_default_shadowed is changed to do outline, instead of
drawing the shadow twice.
- In the font shader, instead of encoding shadow type in signs of
the glyph_size, pass that as a "flags" vertex attribute. Put
font texture channel count into the same flags, so that the
vertex size stays the same.
- Well actually, vertex size becomes smaller by 4 bytes, since turns
out glyph_mode vertex attribute was not used for anything at all.
Images in the PR.
Co-authored-by: Harley Acheson <harley.acheson@gmail.com>
Pull Request: https://projects.blender.org/blender/blender/pulls/121383
2024-05-10 21:06:44 +02:00
|
|
|
|
|
|
|
|
/* Depending on shadow outline / blur level, we might need to expand the quad. */
|
2025-08-27 14:52:19 +02:00
|
|
|
uint shadow_type = glyph_flags & 0xFu;
|
UI: Improved overlay text contrast with new outline text decoration
Overlay texts were previously drawn with two sets of shadows:
- 3px blur,
- 5px blur, slightly offset
But since the shadow color was always set to black, it was still
causing legibility issues when the text itself was dark (set
via theme for example).
This PR adds a new "outline" BLF text decoration, and uses that
for the overlays. And it picks text/outline color depending
on the "background" color of the view.
Details:
- Instead of "shadow level" integer where the only valid options
are 0, 3 or 5, have a FontShadowType enum.
- Add a new FontShadowType::Outline enum entry, that does a 1px
outline by doing a 3x3 dilation in the font shader.
- BLF_draw_default_shadowed is changed to do outline, instead of
drawing the shadow twice.
- In the font shader, instead of encoding shadow type in signs of
the glyph_size, pass that as a "flags" vertex attribute. Put
font texture channel count into the same flags, so that the
vertex size stays the same.
- Well actually, vertex size becomes smaller by 4 bytes, since turns
out glyph_mode vertex attribute was not used for anything at all.
Images in the PR.
Co-authored-by: Harley Acheson <harley.acheson@gmail.com>
Pull Request: https://projects.blender.org/blender/blender/pulls/121383
2024-05-10 21:06:44 +02:00
|
|
|
int interp_size = shadow_type > 4 ? 2 : (shadow_type > 0 ? 1 : 0);
|
BLF: Optimize text rendering and caching
The current code allocates and transfers a lot of memory to the GPU,
but only a small portion of this memory is actually used.
In addition, the code calls many costly gl operations during the
caching process.
This commit significantly reduce the amount of memory by allocating
and transferring a flat array without pads to the GPU.
It also calls as little as possible the gl operations during the cache.
This code also simulate a billinear filter `GL_LINEAR` using a 1D texture.
**Average drawing time:**
|before:|0.00003184 sec
|now:|0.00001943 sec
|fac:|1.6385156675048407
**5 worst times:**
|before:|[0.001075, 0.001433, 0.002143, 0.002915, 0.003242]
|now:|[0.00094, 0.000993, 0.001502, 0.002284, 0.002328]
Differential Revision: https://developer.blender.org/D6886
2020-02-23 17:30:27 -03:00
|
|
|
|
2022-10-06 12:12:09 +11:00
|
|
|
/* Quad expansion using instanced rendering. */
|
2019-12-02 01:40:58 +01:00
|
|
|
float x = float(gl_VertexID % 2);
|
|
|
|
|
float y = float(gl_VertexID / 2);
|
2025-04-14 13:46:41 +02:00
|
|
|
float2 quad = float2(x, y);
|
2019-12-02 01:40:58 +01:00
|
|
|
|
2025-08-27 14:52:19 +02:00
|
|
|
float4 pos = float4(glyphs[glyph_index].position);
|
2025-04-14 13:46:41 +02:00
|
|
|
float2 interp_offset = float(interp_size) / abs(pos.zw - pos.xy);
|
|
|
|
|
texCoord_interp = mix(-interp_offset, 1.0f + interp_offset, quad) * float2(glyph_dim) +
|
|
|
|
|
float2(0.5f);
|
BLF: Optimize text rendering and caching
The current code allocates and transfers a lot of memory to the GPU,
but only a small portion of this memory is actually used.
In addition, the code calls many costly gl operations during the
caching process.
This commit significantly reduce the amount of memory by allocating
and transferring a flat array without pads to the GPU.
It also calls as little as possible the gl operations during the cache.
This code also simulate a billinear filter `GL_LINEAR` using a 1D texture.
**Average drawing time:**
|before:|0.00003184 sec
|now:|0.00001943 sec
|fac:|1.6385156675048407
**5 worst times:**
|before:|[0.001075, 0.001433, 0.002143, 0.002915, 0.003242]
|now:|[0.00094, 0.000993, 0.001502, 0.002284, 0.002328]
Differential Revision: https://developer.blender.org/D6886
2020-02-23 17:30:27 -03:00
|
|
|
|
2025-04-14 13:46:41 +02:00
|
|
|
float2 final_pos = mix(float2(int2(pos.xy) + int2(-interp_size, interp_size)),
|
|
|
|
|
float2(int2(pos.zw) + int2(interp_size, -interp_size)),
|
|
|
|
|
quad);
|
BLF: Optimize text rendering and caching
The current code allocates and transfers a lot of memory to the GPU,
but only a small portion of this memory is actually used.
In addition, the code calls many costly gl operations during the
caching process.
This commit significantly reduce the amount of memory by allocating
and transferring a flat array without pads to the GPU.
It also calls as little as possible the gl operations during the cache.
This code also simulate a billinear filter `GL_LINEAR` using a 1D texture.
**Average drawing time:**
|before:|0.00003184 sec
|now:|0.00001943 sec
|fac:|1.6385156675048407
**5 worst times:**
|before:|[0.001075, 0.001433, 0.002143, 0.002915, 0.003242]
|now:|[0.00094, 0.000993, 0.001502, 0.002284, 0.002328]
Differential Revision: https://developer.blender.org/D6886
2020-02-23 17:30:27 -03:00
|
|
|
|
2025-04-14 13:46:41 +02:00
|
|
|
gl_Position = ModelViewProjectionMatrix * float4(final_pos, 0.0f, 1.0f);
|
2016-09-17 13:33:02 +02:00
|
|
|
}
|