Blender V5.3
gpu_shader_text.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016-2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
9
10namespace builtin::text {
11
12bool is_inside_box(int2 box_size, int2 v)
13{
14 return all(greaterThanEqual(v, int2(0))) && all(lessThan(v, box_size));
15}
16
17struct Resources {
21
23
24 [[storage(0, read)]] GlyphQuad (&glyphs)[];
25
26 /* Font texture is conceptually laid out like a big 1D buffer: each glyph
27 * rectangle is flattened in row-major order into a "pixel strip". Inside
28 * the texture, glyphs strips are put one after another. The texture pixel
29 * rows can conceptually be treated as a really wide 1D texture.
30 *
31 * Because of all this, texture filtering has to be implemented manually,
32 * as well as checks for whether filtering samples fall outside of the
33 * glyph rectangle. */
34
35 float texel_fetch(int index) const
36 {
37 int2 texel = int2(index & this->glyph_tex_width_mask, index >> this->glyph_tex_width_shift);
38 return texelFetch(glyph, texel, 0).r;
39 }
40
41 float sample_glyph_bilinear(int2 glyph_size, int glyph_ofs, float2 bilin_f, float2 uv) const
42 {
43 int2 texel = int2(floor(uv)) - 1;
44 int index = glyph_ofs + texel.y * glyph_size.x + texel.x;
45
46 /* Fetch 2x2 texels for filtering. */
47 int offset_x = 1;
48 int offset_y = glyph_size.x;
49 float tl = texel_fetch(index);
50 float tr = texel_fetch(index + offset_x);
51 float bl = texel_fetch(index + offset_y);
52 float br = texel_fetch(index + offset_x + offset_y);
53
54 /* Texels outside of glyph box: zero. */
55 if (!is_inside_box(glyph_size, texel)) {
56 tl = 0.0f;
57 }
58 if (!is_inside_box(glyph_size, texel + int2(1, 0))) {
59 tr = 0.0f;
60 }
61 if (!is_inside_box(glyph_size, texel + int2(0, 1))) {
62 bl = 0.0f;
63 }
64 if (!is_inside_box(glyph_size, texel + int2(1, 1))) {
65 br = 0.0f;
66 }
67
68 /* Bilinear filter. */
69 float tA = mix(tl, tr, bilin_f.x);
70 float tB = mix(bl, br, bilin_f.x);
71 return mix(tA, tB, bilin_f.y);
72 }
73
74 float4 sample_glyph_rgba(int2 glyph_size, int glyph_ofs, float2 uv) const
75 {
76 int2 texel = int2(round(uv)) - 1;
77
78 float4 col = float4(0.0f);
79 if (is_inside_box(glyph_size, texel)) {
80 int index = glyph_ofs + (texel.y * glyph_size.x + texel.x) * 4;
81 col.r = texel_fetch(index);
82 col.g = texel_fetch(index + 1);
83 col.b = texel_fetch(index + 2);
84 col.a = texel_fetch(index + 3);
85 }
86 return col;
87 }
88};
89
97
98[[vertex]] void main_vert([[resource_table]] const Resources &srt,
99 [[instance_index]] const int inst_index,
100 [[vertex_id]] const int vert_id,
101 [[out]] VertOut &v_out,
102 [[position]] float4 &out_pos)
103{
104 int glyph_index = inst_index;
105
106 v_out.color_flat = srt.glyphs[glyph_index].glyph_color;
107 v_out.glyph_offset = srt.glyphs[glyph_index].offset;
108 v_out.glyph_dim = srt.glyphs[glyph_index].glyph_size;
109 v_out.glyph_flags = srt.glyphs[glyph_index].flags;
110
111 /* Depending on shadow outline / blur level, we might need to expand the quad. */
112 uint shadow_type = v_out.glyph_flags & 0xFu;
113 int interp_size = shadow_type > 4 ? 2 : (shadow_type > 0 ? 1 : 0);
114
115 /* Quad expansion using instanced rendering. */
116 float x = float(vert_id % 2);
117 float y = float(vert_id / 2);
118 float2 quad = float2(x, y);
119
120 float4 pos = float4(srt.glyphs[glyph_index].position);
121 float2 interp_offset = float(interp_size) / abs(pos.zw - pos.xy);
122 v_out.uv_interp = mix(-interp_offset, 1.0f + interp_offset, quad) * float2(v_out.glyph_dim) +
123 float2(0.5f);
124
125 float2 final_pos = mix(float2(int2(pos.xy) + int2(-interp_size, interp_size)),
126 float2(int2(pos.zw) + int2(interp_size, -interp_size)),
127 quad);
128
129 out_pos = srt.ModelViewProjectionMatrix * float4(final_pos, 0.0f, 1.0f);
130}
131
132struct FragOut {
134};
135
138 [[in]] const VertOut &v_out,
139 [[out]] FragOut &frag_out)
140{
141 float2 uv_base = v_out.uv_interp;
142 uint num_channels = (v_out.glyph_flags >> 4) & 0xFu;
143 uint shadow_type = v_out.glyph_flags & 0xFu;
144
145 /* Colored glyphs: do not do filtering or blurring. */
146 if (num_channels == 4) {
147 frag_out.color = srt.sample_glyph_rgba(v_out.glyph_dim, v_out.glyph_offset, uv_base).rgba;
148 frag_out.color.a *= v_out.color_flat.a;
149 return;
150 }
151
152 float2 bilin_f = fract(uv_base);
153
154 frag_out.color.rgb = v_out.color_flat.rgb;
155
156 if (shadow_type == 0) {
157 /* No blurring: just a bilinear sample. */
158 frag_out.color.a = srt.sample_glyph_bilinear(
159 v_out.glyph_dim, v_out.glyph_offset, bilin_f, uv_base);
160 }
161 else {
162
163 /* Blurring or dilation: will fetch (N+1)x(N+1) are of glyph texels,
164 * shifting the filter kernel weights by bilinear fraction. */
165 frag_out.color.a = 0.0f;
166
167 int2 texel = int2(floor(uv_base)) - 1;
168 int frag_offset = v_out.glyph_offset + texel.y * v_out.glyph_dim.x + texel.x;
169
170 if (shadow_type == 6) {
171 /* 3x3 outline by dilation */
172
173 float maxval = 0.0f;
174 for (int iy = 0; iy < 4; ++iy) {
175 int ofsy = iy - 1;
176 for (int ix = 0; ix < 4; ++ix) {
177 int ofsx = ix - 1;
178 float v = srt.texel_fetch(frag_offset + ofsy * v_out.glyph_dim.x + ofsx);
179 if (!is_inside_box(v_out.glyph_dim, texel + int2(ofsx, ofsy))) {
180 v = 0.0f;
181 }
182
183 /* Bilinearly compute weight for this sample. */
184 float w00 = ix < 3 && iy < 3 ? 1.0f : 0.0f;
185 float w10 = ix > 0 && iy < 3 ? 1.0f : 0.0f;
186 float w01 = ix < 3 && iy > 0 ? 1.0f : 0.0f;
187 float w11 = ix > 0 && iy > 0 ? 1.0f : 0.0f;
188 float w = mix(mix(w00, w10, bilin_f.x), mix(w01, w11, bilin_f.x), bilin_f.y);
189
190 maxval = max(maxval, v * w);
191 }
192 }
193 frag_out.color.a = maxval;
194 }
195 else if (shadow_type <= 4) {
196 /* 3x3 blur */
197
198 /* clang-format off */
199 const float weights3x3[16] = {
200 1.0f, 2.0f, 1.0f, 0.0f,
201 2.0f, 4.0f, 2.0f, 0.0f,
202 1.0f, 2.0f, 1.0f, 0.0f,
203 0.0f, 0.0f, 0.0f, 0.0f
204 };
205 /* clang-format on */
206
207 float sum = 0.0f;
208 int idx = 0;
209 for (int iy = 0; iy < 4; ++iy) {
210 int ofsy = iy - 1;
211 for (int ix = 0; ix < 4; ++ix) {
212 int ofsx = ix - 1;
213 float v = srt.texel_fetch(frag_offset + ofsy * v_out.glyph_dim.x + ofsx);
214 if (!is_inside_box(v_out.glyph_dim, texel + int2(ofsx, ofsy))) {
215 v = 0.0f;
216 }
217
218 /* Bilinearly compute filter weight for this sample. */
219 float w00 = weights3x3[idx];
220 float w10 = ix > 0 ? weights3x3[idx - 1] : 0.0f;
221 float w01 = iy > 0 ? weights3x3[idx - 4] : 0.0f;
222 float w11 = ix > 0 && iy > 0 ? weights3x3[idx - 5] : 0.0f;
223 float w = mix(mix(w00, w10, bilin_f.x), mix(w01, w11, bilin_f.x), bilin_f.y);
224
225 sum += v * w;
226 ++idx;
227 }
228 }
229 frag_out.color.a = sum * (1.0f / 16.0f);
230 }
231 else {
232 /* 5x5 blur */
233
234 /* clang-format off */
235 const float weights5x5[36] = {
236 1.0f, 2.0f, 2.0f, 2.0f, 1.0f, 0.0f,
237 2.0f, 5.0f, 6.0f, 5.0f, 2.0f, 0.0f,
238 2.0f, 6.0f, 8.0f, 6.0f, 2.0f, 0.0f,
239 2.0f, 5.0f, 6.0f, 5.0f, 2.0f, 0.0f,
240 1.0f, 2.0f, 2.0f, 2.0f, 1.0f, 0.0f,
241 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
242 };
243 /* clang-format on */
244
245 float sum = 0.0f;
246 int idx = 0;
247 for (int iy = 0; iy < 6; ++iy) {
248 int ofsy = iy - 2;
249 for (int ix = 0; ix < 6; ++ix) {
250 int ofsx = ix - 2;
251 float v = srt.texel_fetch(frag_offset + ofsy * v_out.glyph_dim.x + ofsx);
252 if (!is_inside_box(v_out.glyph_dim, texel + int2(ofsx, ofsy))) {
253 v = 0.0f;
254 }
255
256 /* Bilinearly compute filter weight for this sample. */
257 float w00 = weights5x5[idx];
258 float w10 = ix > 0 ? weights5x5[idx - 1] : 0.0f;
259 float w01 = iy > 0 ? weights5x5[idx - 6] : 0.0f;
260 float w11 = ix > 0 && iy > 0 ? weights5x5[idx - 7] : 0.0f;
261 float w = mix(mix(w00, w10, bilin_f.x), mix(w01, w11, bilin_f.x), bilin_f.y);
262
263 sum += v * w;
264 ++idx;
265 }
266 }
267 frag_out.color.a = sum * (1.0f / 80.0f);
268 }
269 }
270
271 frag_out.color.a *= v_out.color_flat.a;
272 frag_out.color = colorspace.rec709_srgb_to_output_space(frag_out.color);
273}
274
275} // namespace builtin::text
276
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define instance_index
#define storage(slot, qualifiers)
#define position
#define index(i)
#define flat
#define push_constant
#define resource_table
#define sampler(slot)
#define fragment
#define vertex_id
#define frequency(freq)
#define no_perspective
#define vertex
#define frag_color(slot)
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
#define round
T fract(T) RET
#define abs
#define floor
bool all(VecOp< bool, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< float, 2 > sampler2D
PipelineGraphic gpu_shader_text(builtin::text::main_vert, builtin::text::main_frag)
MatBase< float, 4, 4 > float4x4
bool is_inside_box(int2 box_size, int2 v)
void main_frag(const Resources &srt, const ColorSpace &colorspace, const VertOut &v_out, FragOut &frag_out)
void main_vert(const Resources &srt, const int inst_index, const int vert_id, VertOut &v_out, float4 &out_pos)
#define mix
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float texel_fetch(int index) const
float sample_glyph_bilinear(int2 glyph_size, int glyph_ofs, float2 bilin_f, float2 uv) const
float4 sample_glyph_rgba(int2 glyph_size, int glyph_ofs, float2 uv) const
float x
float y