Blender V5.3
gpencil_vfx.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "gpencil_common_lib.glsl"
10
12
13/* Match DNA enum. */
21
22enum FxMode : int {
25 BLUR = 2,
27 GLOW = 4,
28 RIM = 5,
29 SHADOW = 6,
31};
32
33float gaussian_weight(float x)
34{
35 return exp(-x * x / (2.0f * 0.35f * 0.35f));
36}
37
43
44struct FragOut {
45 /* Reminder: This is considered SRC color in blend equations.
46 * Same operation on all buffers. */
49};
50
51struct Composite {
53
54 void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
55 {
56 if (is_first_pass) {
57 /* Blend mode is multiply. */
58 frag_out.color.rgb = frag_out.revealage.rgb = texture(srt.reveal_buf, screen_uv).rgb;
59 frag_out.color.a = frag_out.revealage.a = 1.0f;
60 }
61 else {
62 /* Blend mode is additive. */
63 frag_out.revealage = float4(0.0f);
64 frag_out.color.rgb = texture(srt.color_buf, screen_uv).rgb;
65 frag_out.color.a = 0.0f;
66 }
67 }
68};
69
70struct Colorize {
75
76 void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
77 {
78 frag_out.color = texture(srt.color_buf, screen_uv);
79 frag_out.revealage = texture(srt.reveal_buf, screen_uv);
80
81 float luma = dot(frag_out.color.rgb, float3(0.2126f, 0.7152f, 0.723f));
82
83 float3x3 sepia_mat = float3x3(float3(0.393f, 0.349f, 0.272f),
84 float3(0.769f, 0.686f, 0.534f),
85 float3(0.189f, 0.168f, 0.131f));
86 /* No blending. */
87 switch (mode) {
89 frag_out.color.rgb = mix(frag_out.color.rgb, float3(luma), factor);
90 break;
92 frag_out.color.rgb = mix(frag_out.color.rgb, sepia_mat * frag_out.color.rgb, factor);
93 break;
95 frag_out.color.rgb = luma * ((luma <= factor) ? low_color : high_color);
96 break;
98 frag_out.color.rgb = mix(frag_out.color.rgb, luma * low_color, factor);
99 break;
101 default:
102 frag_out.color.rgb *= factor;
103 frag_out.revealage.rgb = mix(float3(1.0f), frag_out.revealage.rgb, factor);
104 break;
105 }
106 }
107};
108
109struct Blur {
112
113 void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
114 {
115 float2 pixel_size = 1.0f / float2(textureSize(srt.reveal_buf, 0).xy);
116 float2 ofs = offset * pixel_size;
117
118 frag_out.color = float4(0.0f);
119 frag_out.revealage = float4(0.0f);
120
121 /* No blending. */
122 float weight_accum = 0.0f;
123 for (int i = -samp_count; i <= samp_count; i++) {
124 float x = float(i) / float(samp_count);
125 float weight = gaussian_weight(x);
126 weight_accum += weight;
127 float2 uv = screen_uv + ofs * x;
128 frag_out.color.rgb += texture(srt.color_buf, uv).rgb * weight;
129 frag_out.revealage.rgb += texture(srt.reveal_buf, uv).rgb * weight;
130 }
131
132 frag_out.color /= weight_accum;
133 frag_out.revealage /= weight_accum;
134 }
135};
136
137struct Transform {
145
146 void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
147 {
148 float2 uv = (screen_uv - 0.5f) * axis_flip + 0.5f;
149
150 /* Wave deform. */
151 float wave_time = dot(uv, wave_dir.xy);
152 uv += sin(wave_time + wave_phase) * wave_offset;
153 /* Swirl deform. */
154 if (swirl_radius > 0.0f) {
155 float2 tex_size = float2(textureSize(srt.color_buf, 0).xy);
156 float2 pix_coord = uv * tex_size - swirl_center;
157 float dist = length(pix_coord);
158 float percent = clamp((swirl_radius - dist) / swirl_radius, 0.0f, 1.0f);
159 float theta = percent * percent * swirl_angle;
160 float s = sin(theta);
161 float c = cos(theta);
162 float2x2 rot = float2x2(float2(c, -s), float2(s, c));
163 uv = (rot * pix_coord + swirl_center) / tex_size;
164 }
165
166 frag_out.color = texture(srt.color_buf, uv);
167 frag_out.revealage = texture(srt.reveal_buf, uv);
168 }
169};
170
171struct Glow {
179
180 void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
181 {
182 float2 pixel_size = 1.0f / float2(textureSize(srt.reveal_buf, 0).xy);
183 float2 ofs = offset * pixel_size;
184
185 frag_out.color = float4(0.0f);
186 frag_out.revealage = float4(0.0f);
187
188 float weight_accum = 0.0f;
189 for (int i = -samp_count; i <= samp_count; i++) {
190 float x = float(i) / float(samp_count);
191 float weight = gaussian_weight(x);
192 weight_accum += weight;
193 float2 uv = screen_uv + ofs * x;
194 float3 col = texture(srt.color_buf, uv).rgb;
195 float3 rev = texture(srt.reveal_buf, uv).rgb;
196 if (threshold.x > -1.0f) {
197 if (threshold.y > -1.0f) {
198 if (any(greaterThan(abs(col - threshold.xyz), float3(threshold.w)))) {
199 weight = 0.0f;
200 }
201 }
202 else {
203 if (dot(col, float3(1.0f / 3.0f)) < threshold.x) {
204 weight = 0.0f;
205 }
206 }
207 }
208 frag_out.color.rgb += col * weight;
209 frag_out.revealage.rgb += (1.0f - rev) * weight;
210 }
211
212 if (weight_accum > 0.0f) {
213 frag_out.color *= glow_color.rgbb / weight_accum;
214 frag_out.revealage = frag_out.revealage / weight_accum;
215 }
216 frag_out.revealage = 1.0f - frag_out.revealage;
217
218 if (glow_under) {
219 if (first_pass) {
220 /* In first pass we copy the revealage buffer in the alpha channel.
221 * This let us do the alpha under in second pass. */
222 float3 original_revealage = texture(srt.reveal_buf, screen_uv).rgb;
223 frag_out.revealage.a = clamp(dot(original_revealage.rgb, float3(0.333334f)), 0.0f, 1.0f);
224 }
225 else {
226 /* Recover original revealage. */
227 frag_out.revealage.a = texture(srt.reveal_buf, screen_uv).a;
228 }
229 }
230 if (!first_pass) {
231 frag_out.color.a = clamp(1.0f - dot(frag_out.revealage.rgb, float3(0.333334f)), 0.0f, 1.0f);
232 frag_out.revealage.a *= glow_color.a;
233 blend_mode_output(
234 blend_mode, frag_out.color, frag_out.revealage.a, frag_out.color, frag_out.revealage);
235 }
236 }
237};
238
239struct Rim {
247
248 void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
249 {
250 /* Blur revealage buffer. */
251 frag_out.revealage = float4(0.0f);
252 float weight_accum = 0.0f;
253 for (int i = -samp_count; i <= samp_count; i++) {
254 float x = float(i) / float(samp_count);
255 float weight = gaussian_weight(x);
256 weight_accum += weight;
257 float2 uv = screen_uv + blur_dir * x + uv_offset;
258 float3 col = texture(srt.reveal_buf, uv).rgb;
259 if (any(not(equal(float2(0.0f), floor(uv))))) {
260 col = float3(0.0f);
261 }
262 frag_out.revealage.rgb += col * weight;
263 }
264 frag_out.revealage /= weight_accum;
265
266 if (is_first_pass) {
267 /* In first pass we copy the reveal buffer. This let us do alpha masking in second pass. */
268 frag_out.color = texture(srt.reveal_buf, screen_uv);
269 /* Also add the masked color to the reveal buffer. */
270 float3 col = texture(srt.color_buf, screen_uv).rgb;
271 if (all(lessThan(abs(col - mask_color), float3(0.05f)))) {
272 frag_out.color = float4(1.0f);
273 }
274 }
275 else {
276 /* Pre-multiply by foreground alpha (alpha mask). */
277 float mask = 1.0f - clamp(dot(float3(0.333334f), texture(srt.color_buf, screen_uv).rgb),
278 0.0f,
279 1.0f);
280
281 /* frag_out.revealage is blurred shadow. */
282 float rim = clamp(dot(float3(0.333334f), frag_out.revealage.rgb), 0.0f, 1.0f);
283
284 float4 color = float4(rim_color, 1.0f);
285
286 blend_mode_output(blend_mode, color, rim * mask, frag_out.color, frag_out.revealage);
287 }
288 }
289};
290
291struct Shadow {
302
303 float2 compute_uvs(float2 screen_uv, float x) const
304 {
305 float2 uv = screen_uv;
306 /* Transform UV (loc, rot, scale) */
307 uv = uv.x * uv_rot_x + uv.y * uv_rot_y + uv_offset;
308 uv += blur_dir * x;
309 /* Wave deform. */
310 float wave_time = dot(uv, wave_dir.xy);
311 uv += sin(wave_time + wave_phase) * wave_offset;
312 return uv;
313 }
314
315 void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
316 {
317 /* Blur revealage buffer. */
318 frag_out.revealage = float4(0.0f);
319 float weight_accum = 0.0f;
320 for (int i = -samp_count; i <= samp_count; i++) {
321 float x = float(i) / float(samp_count);
322 float weight = gaussian_weight(x);
323 weight_accum += weight;
324 float2 uv = compute_uvs(screen_uv, x);
325 float3 col = texture(srt.reveal_buf, uv).rgb;
326 if (any(not(equal(float2(0.0f), floor(uv))))) {
327 col = float3(1.0f);
328 }
329 frag_out.revealage.rgb += col * weight;
330 }
331 frag_out.revealage /= weight_accum;
332
333 /* No blending in first pass, alpha over pre-multiply in second pass. */
334 if (is_first_pass) {
335 /* In first pass we copy the reveal buffer. This let us do alpha under in second pass. */
336 frag_out.color = texture(srt.reveal_buf, screen_uv);
337 }
338 else {
339 /* frag_out.revealage is blurred shadow. */
340 float shadow_fac = 1.0f - clamp(dot(float3(0.333334f), frag_out.revealage.rgb), 0.0f, 1.0f);
341 /* Pre-multiply by foreground revealage (alpha under). */
342 float3 original_revealage = texture(srt.color_buf, screen_uv).rgb;
343 shadow_fac *= clamp(dot(float3(0.333334f), original_revealage), 0.0f, 1.0f);
344 /* Modulate by opacity */
345 shadow_fac *= shadow_color.a;
346 /* Apply shadow color. */
347 frag_out.color.rgb = mix(float3(0.0f), shadow_color.rgb, shadow_fac);
348 /* Alpha over (mask behind the shadow). */
349 frag_out.color.a = shadow_fac;
350
351 frag_out.revealage.rgb = original_revealage * (1.0f - shadow_fac);
352 /* Replace the whole revealage buffer. */
353 frag_out.revealage.a = 1.0f;
354 }
355 }
356};
357
358struct Pixelize {
363
364 void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
365 {
366 float2 pixel = floor((screen_uv - target_pixel_offset) / target_pixel_size);
367 float2 uv = (pixel + 0.5f) * target_pixel_size + target_pixel_offset;
368
369 frag_out.color = float4(0.0f);
370 frag_out.revealage = float4(0.0f);
371
372 for (int i = -samp_count; i <= samp_count; i++) {
373 float x = float(i) / float(samp_count + 1);
374 float2 uv_ofs = uv + accum_offset * 0.5f * x;
375 frag_out.color += texture(srt.color_buf, uv_ofs);
376 frag_out.revealage += texture(srt.reveal_buf, uv_ofs);
377 }
378
379 frag_out.color /= float(samp_count) * 2.0f + 1.0f;
380 frag_out.revealage /= float(samp_count) * 2.0f + 1.0f;
381 }
382};
383
384[[fragment]]
385void frag([[resource_table]] const Vfx &srt,
386 [[resource_table]] [[condition(vfx_mode == COMPOSITE)]] const Composite &composite,
387 [[resource_table]] [[condition(vfx_mode == COLORIZE)]] const Colorize &colorize,
388 [[resource_table]] [[condition(vfx_mode == BLUR)]] const Blur &blur,
389 [[resource_table]] [[condition(vfx_mode == TRANSFORM)]] const Transform &transform,
390 [[resource_table]] [[condition(vfx_mode == GLOW)]] const Glow &glow,
391 [[resource_table]] [[condition(vfx_mode == RIM)]] const Rim &rim,
392 [[resource_table]] [[condition(vfx_mode == SHADOW)]] const Shadow &shadow,
393 [[resource_table]] [[condition(vfx_mode == PIXELIZE)]] const Pixelize &pixelize,
394 [[frag_coord]] const float4 frag_co,
395 [[out]] FragOut &frag_out)
396{
397 float2 screen_uv = frag_co.xy / float2(textureSize(srt.color_buf, 0).xy);
398
399 if (srt.vfx_mode == COMPOSITE) [[static_branch]] {
400 composite.process(srt, screen_uv, frag_out);
401 }
402 else if (srt.vfx_mode == COLORIZE) [[static_branch]] {
403 colorize.process(srt, screen_uv, frag_out);
404 }
405 else if (srt.vfx_mode == BLUR) [[static_branch]] {
406 blur.process(srt, screen_uv, frag_out);
407 }
408 else if (srt.vfx_mode == TRANSFORM) [[static_branch]] {
409 transform.process(srt, screen_uv, frag_out);
410 }
411 else if (srt.vfx_mode == GLOW) [[static_branch]] {
412 glow.process(srt, screen_uv, frag_out);
413 }
414 else if (srt.vfx_mode == RIM) [[static_branch]] {
415 rim.process(srt, screen_uv, frag_out);
416 }
417 else if (srt.vfx_mode == SHADOW) [[static_branch]] {
418 shadow.process(srt, screen_uv, frag_out);
419 }
420 else if (srt.vfx_mode == PIXELIZE) [[static_branch]] {
421 pixelize.process(srt, screen_uv, frag_out);
422 }
423}
424
425[[vertex]]
426void vert([[vertex_id]] const int vert_id, [[position]] float4 &position)
427{
428 fullscreen_vertex(vert_id, position);
429}
430
431} // namespace grease_pencil::vfx
432
435 grease_pencil::vfx::Vfx{.vfx_mode = 0 /* COMPOSITE */});
438 grease_pencil::vfx::Vfx{.vfx_mode = 1 /* COLORIZE */});
441 grease_pencil::vfx::Vfx{.vfx_mode = 2 /* BLUR */});
444 grease_pencil::vfx::Vfx{.vfx_mode = 3 /* TRANSFORM */});
447 grease_pencil::vfx::Vfx{.vfx_mode = 4 /* GLOW */});
450 grease_pencil::vfx::Vfx{.vfx_mode = 5 /* RIM */});
453 grease_pencil::vfx::Vfx{.vfx_mode = 6 /* SHADOW */});
456 grease_pencil::vfx::Vfx{.vfx_mode = 7 /* PIXELIZE */});
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
#define rot(x, k)
PipelineGraphic gpencil_fx_transform(grease_pencil::vfx::vert, grease_pencil::vfx::frag, grease_pencil::vfx::Vfx{.vfx_mode=3 })
PipelineGraphic gpencil_fx_composite(grease_pencil::vfx::vert, grease_pencil::vfx::frag, grease_pencil::vfx::Vfx{.vfx_mode=0 })
PipelineGraphic gpencil_fx_pixelize(grease_pencil::vfx::vert, grease_pencil::vfx::frag, grease_pencil::vfx::Vfx{.vfx_mode=7 })
PipelineGraphic gpencil_fx_glow(grease_pencil::vfx::vert, grease_pencil::vfx::frag, grease_pencil::vfx::Vfx{.vfx_mode=4 })
PipelineGraphic gpencil_fx_blur(grease_pencil::vfx::vert, grease_pencil::vfx::frag, grease_pencil::vfx::Vfx{.vfx_mode=2 })
PipelineGraphic gpencil_fx_shadow(grease_pencil::vfx::vert, grease_pencil::vfx::frag, grease_pencil::vfx::Vfx{.vfx_mode=6 })
PipelineGraphic gpencil_fx_colorize(grease_pencil::vfx::vert, grease_pencil::vfx::frag, grease_pencil::vfx::Vfx{.vfx_mode=1 })
PipelineGraphic gpencil_fx_rim(grease_pencil::vfx::vert, grease_pencil::vfx::frag, grease_pencil::vfx::Vfx{.vfx_mode=5 })
#define position
#define frag_coord
#define push_constant
#define compilation_constant
#define resource_table
#define sampler(slot)
#define static_branch
#define fragment
#define vertex_id
#define condition(cond)
#define vertex
#define frag_color(slot)
#define sin
bool any(VecOp< bool, D >) RET
#define exp
#define cos
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
#define floor
bool all(VecOp< bool, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
void fullscreen_vertex(int vertex_id, float4 &out_position)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
MatBase< float, 2, 2 > float2x2
void vert(const int vert_id, float4 &position)
void frag(const Vfx &srt, const Composite &composite, const Colorize &colorize, const Blur &blur, const Transform &transform, const Glow &glow, const Rim &rim, const Shadow &shadow, const Pixelize &pixelize, const float4 frag_co, FragOut &frag_out)
float gaussian_weight(float x)
#define mix
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float x
float y
float y
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const
float2 compute_uvs(float2 screen_uv, float x) const
void process(const Vfx &srt, float2 screen_uv, FragOut &frag_out) const