Blender V5.3
eevee_film.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include "draw_math_geom.bsl.hh"
17#include "eevee_uniform.bsl.hh"
18#include "eevee_velocity.bsl.hh"
25
26namespace eevee::film {
27
28/* Return scene linear Z depth from the camera or radial depth for panoramic cameras. */
30 float depth,
31 float2 uv,
32 bool is_panoramic_camera)
33{
34 if (is_panoramic_camera) {
35 return length(view.point_screen_to_view(float3(uv, depth)));
36 }
37 return -view.depth_screen_to_view(depth);
38}
39
40ePanoramicFace panoramic_face_index(float3 direction)
41{
42 float3 abs_direction = abs(direction);
43 if (abs_direction.x >= abs_direction.y && abs_direction.x >= abs_direction.z) {
44 return (direction.x >= 0.0f) ? PANORAMIC_FACE_POS_X : PANORAMIC_FACE_NEG_X;
45 }
46 if (abs_direction.y >= abs_direction.z) {
47 return (direction.y >= 0.0f) ? PANORAMIC_FACE_POS_Y : PANORAMIC_FACE_NEG_Y;
48 }
49 return (direction.z >= 0.0f) ? PANORAMIC_FACE_POS_Z : PANORAMIC_FACE_NEG_Z;
50}
51
52float display_depth_amend(float depth)
53{
54 /* This effectively offsets the depth of the whole 2x2 region to the lowest value of the region
55 * twice. One for X and one for Y direction. */
56 /* TODO(fclem): This could be improved as it gives flickering result at depth discontinuity.
57 * But this is the quickest stable result I could come with for now. */
58 depth += gpu_fwidth(depth);
59 /* Small offset to avoid depth test lessEqual failing because of all the conversions loss. */
60 depth += 2.4e-7f * 4.0f;
61 return saturate(depth);
62}
63
64/* Load a texture sample in a specific format. Combined pass needs to use this. */
66{
67 float4 color = texelFetch(tx, texel, 0);
68 /* Convert transmittance to opacity. */
69 color.a = saturate(1.0f - color.a);
70 /* Transform to YCoCg for accumulation. */
71 color.rgb = colorspace::YCoCg_from_scene_linear(color.rgb);
72 return color;
73}
74
75/* Returns a weight based on Luma to reduce the flickering introduced by high energy pixels. */
76float luma_weight(const Uniform &uni, float luma)
77{
78 /* Slide 20 of "High Quality Temporal Supersampling" by Brian Karis at SIGGRAPH 2014. */
79 /* To preserve more details in dark areas, we use a bigger bias. */
80 return 1.0f / (4.0f + luma * uni.uniform_buf.film.exposure_scale);
81}
82
94{
95 return uintBitsToFloat(floatBitsToUint(color) + uint4(dither * 0x2000));
96}
97float patch_float_for_16f_storage(float value, float dither)
98{
99 return uintBitsToFloat(floatBitsToUint(value) + uint(dither * 0x2000));
100}
101
103{
104 a *= safe_rcp(b);
105 /* Try to get gray even if b is zero. */
106 if (b.x == 0.0f) {
107 if (b.y == 0.0f) {
108 a.x = a.z;
109 a.y = a.z;
110 }
111 else if (b.z == 0.0f) {
112 a.x = a.y;
113 a.z = a.y;
114 }
115 else {
116 a.x = 0.5f * (a.y + a.z);
117 }
118 }
119 else if (b.y == 0.0f) {
120 if (b.z == 0.0f) {
121 a.y = a.x;
122 a.z = a.x;
123 }
124 else {
125 a.y = 0.5f * (a.x + a.z);
126 }
127 }
128 else if (b.z == 0.0f) {
129 a.z = 0.5f * (a.x + a.y);
130 }
131 return a;
132}
133
137
138struct Film {
140
146
154
155 /* Sample inputs. Data freshly rendered. */
162
163 /* Color History for TAA needs to be sampler to leverage bilinear sampling. */
165
168
169 /* Accumulation buffers. */
171 [[image(3, read_write, SFLOAT_32)]] image2D depth_img;
175
176 /* Same dither value for every pixel and every buffer, so render passes reconstruct correctly. */
178 {
179 return sampling.rng_1D_get(SAMPLING_FILM_U);
180 }
181
182 /* -------------------------------------------------------------------- */
185
186 /* Returns a null vector outside the projection. */
188 {
189 const CameraData cam = uniforms.uniform_buf.camera;
190 const float2 film_uv = film_co * uniforms.uniform_buf.film.extent_inv;
191 return eevee::camera::view_from_uv(cam, film_uv);
192 }
193
195 {
196 return panoramic_direction_get(float2(texel_film) + 0.5f);
197 }
198
199 bool panoramic_texel_is_owned_by_view(float3 camera_direction, ePanoramicFace view_id)
200 {
201 if (is_zero(camera_direction)) {
202 return false;
203 }
204 return panoramic_face_index(camera_direction) == view_id;
205 }
206
207 ePanoramicFace panoramic_view_id_get()
208 {
209
210 const ViewMatrices view = views.get(0);
211 const float4x4 face_mat = view.viewmat * uniforms.uniform_buf.camera.viewinv;
212 const float3 direction = normalize(transpose(to_float3x3(face_mat)) *
213 float3(0.0f, 0.0f, -1.0f));
214 return panoramic_face_index(direction);
215 }
216
217 /* Panoramic samples outside the projection or on another face have an invalid texel. */
218 bool sample_is_black(FilmSample samp)
219 {
221 return samp.texel.x < 0;
222 }
223 else {
224 return false;
225 }
226 }
227
233 FilmSample panoramic_sample_get(int sample_n, int2 texel_film)
234 {
235 const CameraData cam = uniforms.uniform_buf.camera;
236 const float filter_radius = uniforms.uniform_buf.film.filter_radius;
237 const ePanoramicFace view_id = panoramic_view_id_get();
238
239 FilmSample film_sample = {.texel = int2(-1), .weight = 0.0f, .weight_sum_inv = 0.0f};
240
241 /* Per pixel jitter. */
242 const float seed = float(sample_n) * 2.0f;
244 float2(texel_film), float2(seed, seed + 1.0f), sampling.rng_2D_get(SAMPLING_PANORAMIC_U));
245 const float2 jitter = sample_disk(rand) * filter_radius;
246
247 const float3 camera_direction = panoramic_direction_get(float2(texel_film) + 0.5f + jitter);
248 if (is_zero(camera_direction)) {
249 /* Only accumulate once in the first rendered face. */
250 if (int(view_id) == findLSB(cam.panoramic_view_mask)) {
251 film_sample.weight = film_filter_weight(filter_radius, length_squared(jitter));
252 }
253 return film_sample;
254 }
255 if (!panoramic_texel_is_owned_by_view(camera_direction, view_id)) {
256 return film_sample;
257 }
258
259 /* The face projection contains the render jitter. */
260 const ViewMatrices view = views.get(0);
261 const int2 render_extent = textureSize(depth_tx, 0).xy;
262
263 const float3 face_direction = to_float3x3(view.viewmat) *
264 (to_float3x3(cam.viewinv) * camera_direction);
265 const float3 ndc = project_point(view.winmat, face_direction);
266 const float2 render_co = (ndc.xy * 0.5f + 0.5f) * float2(render_extent);
267 film_sample.texel = clamp(int2(floor(render_co)), int2(0), render_extent - 1);
268
269 const float2 render_uv = (float2(film_sample.texel) + 0.5f) / float2(render_extent);
270 const float3 sample_direction = to_float3x3(cam.viewmat) *
271 (to_float3x3(view.viewinv) *
272 eevee::camera::view_from_uv(view.wininv, render_uv));
273 /* Relative to the sample position to avoid the seam of periodic projections. */
274 const float2 film_offset = (eevee::camera::uv_from_view(cam, normalize(sample_direction)) -
275 eevee::camera::uv_from_view(cam, normalize(camera_direction))) *
276 float2(uniforms.uniform_buf.film.extent);
277
278 const float distance_sqr = length_squared(jitter + film_offset);
279 /* Ensure a minimum weight to avoid missing data when the render pixel is far away. */
280 film_sample.weight = max(film_filter_weight(filter_radius, distance_sqr), 1e-8f);
281 return film_sample;
282 }
283
284 FilmSample sample_get(int sample_n, int2 texel_film)
285 {
286
287 FilmSample film_sample = uniforms.uniform_buf.film.samples[sample_n];
288
289 if (scaling_factor > 1) {
290 /* We are working in the render pixel region on the film. We use film pixel units. */
291
292 float2 film_coord = 0.5f + float2(texel_film % scaling_factor);
293 /* Sample position inside the render pixel region. */
294 float2 jittered_sample_coord = (0.5f - uniforms.uniform_buf.film.subpixel_offset) *
295 float(scaling_factor);
296 /* Offset the film samples to always sample the 4 nearest neighbors in the render target.
297 * `film_sample.texel` is set to visit all 4 neighbors in [0..1] region. */
298 int2 quad_offset = -int2(lessThan(film_coord, jittered_sample_coord));
299 /* Select correct sample depending on which quadrant the film pixel lies. */
300 film_sample.texel += quad_offset;
301 jittered_sample_coord += float2(film_sample.texel * scaling_factor);
302
303 float sample_dist_sqr = length_squared(jittered_sample_coord - film_coord);
304 film_sample.weight = film_filter_weight(uniforms.uniform_buf.film.filter_radius,
305 sample_dist_sqr);
306 /* Ensure a minimum weight for each sample to avoid missing data at 4x or 8x up-scaling. */
307 film_sample.weight = max(film_sample.weight, 1e-8f);
308 }
309
310 film_sample.texel += (texel_film / scaling_factor) + uniforms.uniform_buf.film.overscan;
311
312 /* Use extend on borders. */
313 film_sample.texel = clamp(
314 film_sample.texel, int2(0, 0), uniforms.uniform_buf.film.render_extent - 1);
315
316 return film_sample;
317 }
318
319 FilmSample sample_get(int sample_n,
320 int2 texel_film,
321 const FilmSample (&panoramic_samples)[FILM_PANORAMIC_SAMPLE_LEN])
322 {
324 return panoramic_samples[sample_n];
325 }
326 else {
327 return sample_get(sample_n, texel_film);
328 }
329 }
330
331 /* Returns the combined weights of all samples affecting this film pixel. */
332 float weight_accumulation(int2 texel_film)
333 {
334 /* TODO(fclem): Reference implementation, also needed for panoramic cameras. */
335 if (scaling_factor > 1) {
336 float weight = 0.0f;
337 for (int i = 0; i < samples_len; i++) {
338 weight += sample_get(i, texel_film).weight;
339 }
340 return weight;
341 }
342 return uniforms.uniform_buf.film.samples_weight_total;
343 }
344
345 void sample_accum(FilmSample samp, int pass_id, int layer, sampler2DArray tex, float4 &accum)
346 {
347 if (pass_id < 0 || layer < 0) {
348 return;
349 }
350 float4 value = sample_is_black(samp) ? float4(0.0f) :
351 texelFetch(tex, int3(samp.texel, layer), 0);
352 accum += value * samp.weight;
353 }
354
355 void sample_accum(FilmSample samp, int pass_id, int layer, sampler2DArray tex, float &accum)
356 {
357 if (pass_id < 0 || layer < 0) {
358 return;
359 }
360 float value = sample_is_black(samp) ? 0.0f : texelFetch(tex, int3(samp.texel, layer), 0).x;
361 accum += value * samp.weight;
362 }
363
364 void sample_accum_mist(FilmSample samp, float &accum)
365 {
366
367 if (uniforms.uniform_buf.film.mist_id == -1 || sample_is_black(samp)) {
368 return;
369 }
370 const ViewMatrices view = views.get(0);
371 float depth = reverse_z::read(texelFetch(depth_tx, samp.texel, 0).x);
372 float2 uv = (float2(samp.texel) + 0.5f) / float2(textureSize(depth_tx, 0).xy);
373 float3 vP = view.point_screen_to_view(float3(uv, depth));
374 bool is_persp = view.winmat[3][3] == 0.0f;
375 float mist = (is_persp) ? length(vP) : abs(vP.z);
376 /* Remap to 0..1 range. */
377 mist = saturate(mist * uniforms.uniform_buf.film.mist_scale +
378 uniforms.uniform_buf.film.mist_bias);
379 /* Falloff. */
380 mist = pow(mist, uniforms.uniform_buf.film.mist_exponent);
381 accum += mist * samp.weight;
382 }
383
384 void sample_accum_combined(FilmSample samp, float4 &accum, float &weight_accum)
385 {
386 if (combined_id == -1) {
387 return;
388 }
389 /* Black samples are opaque. */
390 float4 color = sample_is_black(samp) ? float4(0.0f, 0.0f, 0.0f, 1.0f) :
392
393 /* Weight by luma to remove fireflies. */
394 float weight = luma_weight(uniforms, color.x) * samp.weight;
395
396 accum += color * weight;
397 weight_accum += weight;
398 }
399
400 void sample_cryptomatte_accum(FilmSample samp,
401 int layer,
402 sampler2D tex,
403 float2 (&crypto_samples)[4])
404 {
405 if (sample_is_black(samp)) {
406 return;
407 }
408 float hash = texelFetch(tex, samp.texel, 0)[layer];
409 /* Find existing entry. */
410 for (int i = 0; i < 4; i++) {
411 if (crypto_samples[i].x == hash) {
412 crypto_samples[i].y += samp.weight;
413 return;
414 }
415 }
416 /* Overwrite entry with less weight. */
417 for (int i = 0; i < 4; i++) {
418 if (crypto_samples[i].y < samp.weight) {
419 crypto_samples[i] = float2(hash, samp.weight);
420 return;
421 }
422 }
423 }
424
426 FilmSample dst,
427 int2 texel_film,
428 const FilmSample (&panoramic_samples)[FILM_PANORAMIC_SAMPLE_LEN],
429 int pass_id,
430 int layer_component,
431 float4 &out_color)
432 {
433 if (pass_id == -1) {
434 return;
435 }
436
437 /* x = hash, y = accumulated weight. Only keep track of 4 highest weighted samples. */
438 float2 crypto_samples[4] = {float2(0.0f), float2(0.0f), float2(0.0f), float2(0.0f)};
439 for (int i = 0; i < samples_len; i++) {
440 FilmSample src = sample_get(i, texel_film, panoramic_samples);
441 sample_cryptomatte_accum(src, layer_component, cryptomatte_tx, crypto_samples);
442 }
443 float4 display_color = float4(0.0f);
444 for (int i = 0; i < 4; i++) {
446 pass_id,
447 uniforms.uniform_buf.film.cryptomatte_samples_len,
448 crypto_samples[i],
449 display_color);
450 }
451
452 if (uniforms.uniform_buf.film.display_storage_type == PASS_STORAGE_CRYPTOMATTE) {
453 out_color = display_color;
454 }
455 }
456
458
459 /* -------------------------------------------------------------------- */
462
463 /* Returns the distance used to store nearest interpolation data. */
464 float distance_load(int2 texel)
465 {
466 /* Repeat texture coordinates as the weight can be optimized to a small portion of the film. */
467 texel = texel % imageSize(in_weight_img).xy;
468
469 if (!uniforms.uniform_buf.film.use_history || use_reprojection) {
470 return 0.0f;
471 }
472 return imageLoadFast(in_weight_img, int3(texel, FILM_WEIGHT_LAYER_DISTANCE)).x;
473 }
474
475 float weight_load(int2 texel)
476 {
477 /* Repeat texture coordinates as the weight can be optimized to a small portion of the film. */
478 texel = texel % imageSize(in_weight_img).xy;
479
480 if (!uniforms.uniform_buf.film.use_history || use_reprojection) {
481 return 0.0f;
482 }
483 return imageLoadFast(in_weight_img, int3(texel, FILM_WEIGHT_LAYER_ACCUMULATION)).x;
484 }
485
487 {
488
489 if (display_id == -1) {
490 return texelFetch(in_combined_tx, texel_film, 0);
491 }
492 if (uniforms.uniform_buf.film.display_storage_type == PASS_STORAGE_VALUE) {
493 return float4(imageLoadFast(value_accum_img, int3(texel_film, display_id)).rrr, 1.0f);
494 }
495 if (uniforms.uniform_buf.film.display_storage_type == PASS_STORAGE_COLOR) {
496 return imageLoadFast(color_accum_img, int3(texel_film, display_id));
497 }
500 }
501
502 void copy_history(int2 texel_film, float4 &out_color, float &out_depth)
503 {
504 float weight = weight_load(texel_film);
505 float distance = distance_load(texel_film);
507 out_weight_img, int3(texel_film, FILM_WEIGHT_LAYER_ACCUMULATION), float4(weight));
508 imageStoreFast(out_weight_img, int3(texel_film, FILM_WEIGHT_LAYER_DISTANCE), float4(distance));
509
510 if (combined_id != -1) {
511 float4 color = texelFetch(in_combined_tx, texel_film, 0);
512 imageStoreFast(out_combined_img, texel_film, color);
513 }
514
515 out_color = display_color_load(texel_film);
516 out_depth = imageLoadFast(depth_img, texel_film).r;
517 }
518
519 /* Returns motion in pixel space to retrieve the pixel history. */
521 {
522
527 const int2 corners[4] = {int2(-2, -2), int2(2, -2), int2(-2, 2), int2(2, 2)};
528 float min_depth = reverse_z::read(texelFetch(depth_tx, texel_sample, 0).x);
529 int2 nearest_texel = texel_sample;
530 for (int i = 0; i < 4; i++) {
531 int2 texel = clamp(texel_sample + corners[i], int2(0), textureSize(depth_tx, 0).xy - 1);
532 float depth = reverse_z::read(texelFetch(depth_tx, texel, 0).x);
533 if (min_depth > depth) {
534 min_depth = depth;
535 nearest_texel = texel;
536 }
537 }
538
539 /* Only ever called through use_reprojection, which implies a non-panoramic camera. */
540 float4 vector = camera.resolve_local(views, vector_tx, nearest_texel, min_depth);
541
542 /* Transform to pixel space. */
543 vector.xy *= float2(uniforms.uniform_buf.film.extent);
544
545 return vector.xy;
546 }
547
548 /* \a t is inter-pixel position. 0 means perfectly on a pixel center.
549 * Returns weights in both dimensions.
550 * Multiply each dimension weights to get final pixel weights. */
551 void get_catmull_rom_weights(float2 t, float2 (&weights)[4])
552 {
553 float2 t2 = t * t;
554 float2 t3 = t2 * t;
555 float fc = 0.5f; /* Catmull-Rom. */
556
557 float2 fct = t * fc;
558 float2 fct2 = t2 * fc;
559 float2 fct3 = t3 * fc;
560 weights[0] = (fct2 * 2.0f - fct3) - fct;
561 weights[1] = (t3 * 2.0f - fct3) + (-t2 * 3.0f + fct2) + 1.0f;
562 weights[2] = (-t3 * 2.0f + fct3) + (t2 * 3.0f - (2.0f * fct2)) + fct;
563 weights[3] = fct3 - fct2;
564 }
565
566 /* Load color using a special filter to avoid losing detail.
567 * \a texel is sample position with subpixel accuracy. */
569 {
570
571 float2 center_texel;
572 float2 inter_texel = modf(input_texel, center_texel);
573 float2 weights[4];
574 get_catmull_rom_weights(inter_texel, weights);
575
576#if 0 /* Reference. 16 Taps. */
577 float4 color = float4(0.0f);
578 for (int y = 0; y < 4; y++) {
579 for (int x = 0; x < 4; x++) {
580 int2 texel = int2(center_texel) + int2(x, y) - 1;
581 texel = clamp(texel, int2(0), textureSize(color_tx, 0).xy - 1);
582 color += texelFetch(color_tx, texel, 0) * weights[x].x * weights[y].y;
583 }
584 }
585 return color;
586
587#elif 1 /* Optimize version. 5 Bilinear Taps. */
594 center_texel += 0.5f;
595
596 /* Slide 92. */
597 float2 weight_12 = weights[1] + weights[2];
598 float2 uv_12 = (center_texel + weights[2] / weight_12) * uniforms.uniform_buf.film.extent_inv;
599 float2 uv_0 = (center_texel - 1.0f) * uniforms.uniform_buf.film.extent_inv;
600 float2 uv_3 = (center_texel + 2.0f) * uniforms.uniform_buf.film.extent_inv;
601
602 float4 color;
603 float4 weight_cross = weight_12.xyyx * float4(weights[0].yx, weights[3].xy);
604 float weight_center = weight_12.x * weight_12.y;
605
606 color = textureLod(color_tx, uv_12, 0.0f) * weight_center;
607 color += textureLod(color_tx, float2(uv_12.x, uv_0.y), 0.0f) * weight_cross.x;
608 color += textureLod(color_tx, float2(uv_0.x, uv_12.y), 0.0f) * weight_cross.y;
609 color += textureLod(color_tx, float2(uv_3.x, uv_12.y), 0.0f) * weight_cross.z;
610 color += textureLod(color_tx, float2(uv_12.x, uv_3.y), 0.0f) * weight_cross.w;
611 /* Re-normalize for the removed corners. */
612 return color / (weight_center + reduce_add(weight_cross));
613
614#else /* Nearest interpolation for debugging. 1 Tap. */
615 int2 texel = int2(center_texel) + int2(greaterThan(inter_texel, float2(0.5f)));
616 texel = clamp(texel, int2(0), textureSize(color_tx, 0).xy - 1);
617 return texelFetch(color_tx, texel, 0);
618#endif
619 }
620
621 /* Return history clipping bounding box in YCoCg color space. */
622 void combined_neighbor_boundbox(int2 texel, float4 &min_c, float4 &max_c)
623 {
624 /* Plus (+) shape offsets. */
625 const int2 plus_offsets[5] = {int2(0, 0), /* Center */
626 int2(-1, 0),
627 int2(0, -1),
628 int2(1, 0),
629 int2(0, 1)};
630#if 0
637
638 /* First 2 moments. */
639 float4 mu1 = float4(0), mu2 = float4(0);
640 for (int i = 0; i < 5; i++) {
641 float4 color = texelfetch_as_YCoCg_opacity(combined_tx, texel + plus_offsets[i]);
642 mu1 += color;
643 mu2 += square(color);
644 }
645 mu1 *= (1.0f / 5.0f);
646 mu2 *= (1.0f / 5.0f);
647
648 /* Extent scaling. Range [0.75..1.25].
649 * Balance between more flickering (0.75) or more ghosting (1.25). */
650 constexpr float gamma = 1.25f;
651 /* Standard deviation. */
652 float4 sigma = sqrt(abs(mu2 - square(mu1)));
653 /* eq. 6 in "A Survey of Temporal Anti-aliasing Techniques". */
654 min_c = mu1 - gamma * sigma;
655 max_c = mu1 + gamma * sigma;
656#else
661 min_c = float4(1e16f);
662 max_c = float4(-1e16f);
663 for (int i = 0; i < 5; i++) {
664 float4 color = texelfetch_as_YCoCg_opacity(combined_tx, texel + plus_offsets[i]);
665 min_c = min(min_c, color);
666 max_c = max(max_c, color);
667 }
668 /* (Slide 32) Simple clamp to min/max of 8 neighbors results in 3x3 box artifacts.
669 * Round bbox shape by averaging 2 different min/max from 2 different neighborhood. */
670 float4 min_c_3x3 = min_c;
671 float4 max_c_3x3 = max_c;
672 const int2 corners[4] = {int2(-1, -1), int2(1, -1), int2(-1, 1), int2(1, 1)};
673 for (int i = 0; i < 4; i++) {
674 float4 color = texelfetch_as_YCoCg_opacity(combined_tx, texel + corners[i]);
675 min_c_3x3 = min(min_c_3x3, color);
676 max_c_3x3 = max(max_c_3x3, color);
677 }
678 min_c = (min_c + min_c_3x3) * 0.5f;
679 max_c = (max_c + max_c_3x3) * 0.5f;
680#endif
681 }
682
683 /* 1D equivalent of line_aabb_clipping_dist(). */
684 float aabb_clipping_dist_alpha(float origin, float direction, float aabb_min, float aabb_max)
685 {
686 if (abs(direction) < 1e-5f) {
687 return 0.0f;
688 }
689 float nearest_plane = (direction > 0.0f) ? aabb_min : aabb_max;
690 return (nearest_plane - origin) / direction;
691 }
692
693 /* Modulate the history color to avoid ghosting artifact. */
695 float4 max_color,
696 float4 color_history,
697 float4 src_color)
698 {
699 /* Clip instead of clamping to avoid color accumulating in the AABB corners. */
700 float4 clip_dir = src_color - color_history;
701
702 float t = line_aabb_clipping_dist(
703 color_history.rgb, clip_dir.rgb, min_color.rgb, max_color.rgb);
704 color_history.rgb += clip_dir.rgb * saturate(t);
705
706 /* Clip alpha on its own to avoid interference with other channels. */
707 float t_a = aabb_clipping_dist_alpha(color_history.a, clip_dir.a, min_color.a, max_color.a);
708 color_history.a += clip_dir.a * saturate(t_a);
709
710 return color_history;
711 }
712
714 float velocity, float2 texel, float luma_min, float luma_max, float luma_history)
715 {
716 /* 5% of incoming color by default. */
717 float blend = 0.05f;
718 /* Blend less history if the pixel has substantial velocity. */
719 blend = mix(blend, 0.20f, saturate(velocity * 0.02f));
724 float distance_to_luma_clip = reduce_min(
725 float2(luma_history - luma_min, luma_max - luma_history));
726 /* Divide by bbox size to get a factor. 2 factor to compensate the line above. */
727 distance_to_luma_clip *= 2.0f * safe_rcp(luma_max - luma_min);
728 /* Linearly blend when history gets below to 25% of the bbox size. */
729 blend *= saturate(distance_to_luma_clip * 4.0f + 0.1f);
730 /* Discard out of view history. */
731 if (any(lessThan(texel, float2(0))) ||
732 any(greaterThanEqual(texel, float2(uniforms.uniform_buf.film.extent))))
733 {
734 blend = 1.0f;
735 }
736 /* Discard history if invalid. */
737 if (uniforms.uniform_buf.film.use_history == false) {
738 blend = 1.0f;
739 }
740 return blend;
741 }
742
744 {
745 /* Clamp negative values caused by float imprecision to 0.0f. This also covers the case of
746 * -0.0f, as (-0.0f > 0.0f) evaluates to false and therefore the whole ternary operator to
747 * 0.0f. This is important for certain compositor operations that work differently depending on
748 * the sign of the input. In theory, color = max(0.0f, color) could also be used for that,
749 * however, the output of max(0.0f, -0.0f) depends on both the exact wording of the
750 * specification of the max() function and the order of function parameters, which is why it is
751 * not used. */
752 for (int i = 0; i < 4; i++) [[unroll]] {
753 color[i] = (color[i] > 0.0f) ? color[i] : 0.0f;
754 }
755 return color;
756 }
757
758 /* Returns resolved final color. */
760 FilmSample dst, int2 src_texel, float4 color, float color_weight, float4 &display)
761 {
762 if (combined_id == -1) {
763 return;
764 }
765
766 float4 color_src, color_dst;
767 float weight_src, weight_dst;
768
769 /* Undo the weighting to get final spatially-filtered color. */
770 color_src = color / color_weight;
771
772 if (use_reprojection) {
773 /* Interactive accumulation. Do reprojection and Temporal Anti-Aliasing. */
774
775 /* Reproject by finding where this pixel was in the previous frame. */
776 float2 motion = pixel_history_motion_vector(src_texel);
777 float2 history_texel = float2(dst.texel) + motion;
778
779 float velocity = length(motion);
780
781 /* Load weight if it is not uniform across the whole buffer (i.e: upsampling, panoramic). */
782 // dst.weight = weight_load(texel_combined);
783
784 color_dst = sample_catmull_rom(in_combined_tx, history_texel);
785 color_dst.rgb = colorspace::YCoCg_from_scene_linear(color_dst.rgb);
786
787 /* Get local color bounding box of source neighborhood. */
788 float4 min_color, max_color;
789 combined_neighbor_boundbox(src_texel, min_color, max_color);
790
791 float blend = history_blend_factor(
792 velocity, history_texel, min_color.x, max_color.x, color_dst.x);
793
794 color_dst = amend_combined_history(min_color, max_color, color_dst, color_src);
795
796 /* Luma weighted blend to avoid flickering. */
797 weight_dst = luma_weight(uniforms, color_dst.x) * (1.0f - blend);
798 weight_src = luma_weight(uniforms, color_src.x) * (blend);
799 }
800 else {
801 /* Everything is static. Use render accumulation. */
802 color_dst = texelFetch(in_combined_tx, dst.texel, 0);
803 color_dst.rgb = colorspace::YCoCg_from_scene_linear(color_dst.rgb);
804
805 /* Luma weighted blend to avoid flickering. */
806 weight_dst = luma_weight(uniforms, color_dst.x) * dst.weight;
807 weight_src = color_weight;
808 }
809 /* Weighted blend. */
810 color = color_dst * weight_dst + color_src * weight_src;
811 color /= weight_src + weight_dst;
812
813 color.rgb = colorspace::scene_linear_from_YCoCg(color.rgb);
814
815 /* Fix alpha not accumulating to 1 because of float imprecision. */
816 if (color.a > 0.995f) {
817 color.a = 1.0f;
818 }
819
820 /* Filter NaNs. */
821 if (any(isnan(color))) {
822 color = float4(0.0f, 0.0f, 0.0f, 1.0f);
823 }
824
825 color = clamp_negative_values(color);
826
827 if (display_id == -1) {
828 display = color;
829 }
831 imageStoreFast(out_combined_img, dst.texel, color);
832 }
833
835 FilmSample dst, int pass_id, float4 color, float4 &display, bool do_clamp_negative_values)
836 {
837 /* Filter NaNs. */
838 if (any(isnan(color))) {
839 color = float4(0.0f, 0.0f, 0.0f, 1.0f);
840 }
841
842 if (do_clamp_negative_values) {
843 color = clamp_negative_values(color);
844 }
845
846 /* Fix alpha not accumulating to 1 because of float imprecision. But here we cannot assume that
847 * the alpha contains actual transparency and not user data. Only bias if very close to 1. */
848 if (color.a > 0.9999f && color.a < 1.0f) {
849 color.a = 1.0f;
850 }
851
852 if (display_id == pass_id) {
853 display = color;
854 }
856 imageStoreFast(color_accum_img, int3(dst.texel, pass_id), color);
857 }
858
859 void store_color(FilmSample dst,
860 int pass_id,
861 float4 color,
862 float4 &display,
863 bool do_clamp_negative_values = true)
864 {
865 if (pass_id == -1) {
866 return;
867 }
868
869 float4 data_film = imageLoadFast(color_accum_img, int3(dst.texel, pass_id));
870
871 color = (data_film * dst.weight + color) * dst.weight_sum_inv;
872
873 store_color_ex(dst, pass_id, color, display, do_clamp_negative_values);
874 }
875
876 void store_color_and_light(FilmSample dst,
877 int color_pass_id,
878 int light_pass_id,
879 float4 color,
881 float4 &display)
882 {
883 if (color_pass_id == -1) {
884 return;
885 }
886
887 float4 color_film = imageLoadFast(color_accum_img, int3(dst.texel, color_pass_id));
888 color = (color_film * dst.weight + color) * dst.weight_sum_inv;
889 store_color_ex(dst, color_pass_id, color, display, true);
890
891 if (light_pass_id == -1) {
892 return;
893 }
894
895 float4 light_film = imageLoadFast(color_accum_img, int3(dst.texel, light_pass_id));
896 /* Undivide. */
897 light_film *= color_film;
898 light = (light_film * dst.weight + light) * dst.weight_sum_inv;
900 store_color_ex(dst, light_pass_id, light, display, true);
901 }
902
903 void store_value(FilmSample dst, int pass_id, float value, float4 &display)
904 {
905 if (pass_id == -1) {
906 return;
907 }
908
909 float data_film = imageLoadFast(value_accum_img, int3(dst.texel, pass_id)).x;
910
911 value = (data_film * dst.weight + value) * dst.weight_sum_inv;
912
913 /* Filter NaNs. */
914 if (isnan(value)) {
915 value = 0.0f;
916 }
917
918 if (display_id == pass_id) {
919 display = float4(value, value, value, 1.0f);
920 }
922 imageStoreFast(value_accum_img, int3(dst.texel, pass_id), float4(value));
923 }
924
925 /* Nearest sample variant. Always stores the data. */
926 void store_data(int2 texel_film, int pass_id, float4 data_sample, float4 &display)
927 {
928 if (pass_id == -1) {
929 return;
930 }
931
932 if (display_id == pass_id) {
933 display = data_sample;
934 }
935 imageStoreFast(color_accum_img, int3(texel_film, pass_id), data_sample);
936 }
937
938 void store_depth(int2 texel_film, int2 texel_sample, float value, float &out_depth)
939 {
940
941 if (uniforms.uniform_buf.film.depth_id == -1) {
942 return;
943 }
944
945 float2 uv = (float2(texel_sample) + 0.5f) / float2(textureSize(depth_tx, 0).xy);
946 float depth_value = depth_convert_to_scene(views.get(0), value, uv, constants.use_panoramic);
947 out_depth = depth_value;
948
949 if (value == 1.0f) {
950 /* Match clear value in render_layer_allocate_pass. */
951 depth_value = 1e10f;
952 }
953
954 imageStoreFast(depth_img, texel_film, float4(depth_value));
955 }
956
957 void store_denoising_depth(FilmSample dst, float value, float4 &display)
958 {
959 if (uniforms.uniform_buf.film.denoising_depth_id == -1) {
960 return;
961 }
962
963 float data_film = imageLoadFast(denoising_depth_img, dst.texel).x;
964
965 value = (data_film * dst.weight + value) * dst.weight_sum_inv;
966
967 if (uniforms.uniform_buf.film.display_storage_type == PASS_STORAGE_DENOISING_DEPTH &&
968 display_id == uniforms.uniform_buf.film.denoising_depth_id)
969 {
970 display = float4(value, value, value, 1.0f);
971 }
972 imageStoreFast(denoising_depth_img, dst.texel, float4(value));
973 }
974
975 void store_distance(int2 texel, float value)
976 {
977 imageStoreFast(out_weight_img, int3(texel, FILM_WEIGHT_LAYER_DISTANCE), float4(value));
978 }
979
980 void store_weight(int2 texel, float value)
981 {
982 imageStoreFast(out_weight_img, int3(texel, FILM_WEIGHT_LAYER_ACCUMULATION), float4(value));
983 }
984
986
988 void process_render_sample(int2 texel_film, float4 &out_color, float &out_depth)
989 {
990 const UniformData &uni = uniforms.uniform_buf;
991
992 out_color = float4(0.0f);
993 out_depth = 0.0f;
994
995 FilmSample panoramic_samples[FILM_PANORAMIC_SAMPLE_LEN];
996 float panoramic_weight = 0.0f;
997
999 /* Each sample is accumulated by the face it projects to. */
1000 for (int i = 0; i < samples_len; i++) {
1001 panoramic_samples[i] = panoramic_sample_get(i, texel_film);
1002 panoramic_weight += panoramic_samples[i].weight;
1003 if (panoramic_samples[i].weight > panoramic_samples[0].weight) {
1004 /* Use the first sample as the closest one. */
1005 const FilmSample closest = panoramic_samples[i];
1006 panoramic_samples[i] = panoramic_samples[0];
1007 panoramic_samples[0] = closest;
1008 }
1009 }
1010 if (panoramic_weight == 0.0f) {
1011 /* TODO: Have a stencil mask of each sub-view to avoid computing weights for the whole
1012 * screen for each. Using a mesh to draw the region is not recommended due to quad
1013 * over-shading and the amount of tessellation needed. Using a mesh to mark the stencil
1014 * might be beneficial (passthrough fragment shader). */
1015 copy_history(texel_film, out_color, out_depth);
1016 return;
1017 }
1018 }
1019
1020 float weight_accum;
1022 weight_accum = panoramic_weight;
1023 }
1024 else {
1025 weight_accum = weight_accumulation(texel_film);
1026 }
1027 float film_weight = weight_load(texel_film);
1028 float weight_sum = film_weight + weight_accum;
1029 store_weight(texel_film, weight_sum);
1030
1031 FilmSample dst;
1032 dst.texel = texel_film;
1033 dst.weight = film_weight;
1034 dst.weight_sum_inv = 1.0f / weight_sum;
1035
1036 /* NOTE: We split the accumulations into separate loops to avoid using too much registers and
1037 * maximize occupancy. */
1038
1039 if (combined_id != -1) {
1040 /* NOTE: Do weight accumulation again since we use custom weights. */
1041 float weight_accum = 0.0f;
1042 float4 combined_accum = float4(0.0f);
1043
1044 FilmSample src;
1045 for (int i = samples_len - 1; i >= 0; i--) {
1046 src = sample_get(i, texel_film, panoramic_samples);
1047 sample_accum_combined(src, combined_accum, weight_accum);
1048 }
1049 /* NOTE: src.texel is center texel in incoming data buffer. */
1050 store_combined(dst, src.texel, combined_accum, weight_accum, out_color);
1051 }
1052
1053 if (flag_test(enabled_categories, PASS_CATEGORY_DATA)) {
1054 float film_distance = distance_load(texel_film);
1055
1056 /* Get sample closest to target texel. It is always sample 0. */
1057 FilmSample film_sample = sample_get(0, texel_film, panoramic_samples);
1058
1059 /* Using film weight as distance to the pixel. So the check is inverted. */
1060 if (film_sample.weight > film_distance) {
1061 if (sample_is_black(film_sample)) {
1062 /* No data outside the projection. */
1063 if (uni.film.depth_id != -1) {
1064 imageStoreFast(depth_img, texel_film, float4(1e10f));
1065 }
1066 out_depth = 1e10f;
1067 store_data(texel_film, normal_id, float4(0.0f), out_color);
1068 store_data(texel_film, uni.film.position_id, float4(0.0f), out_color);
1069 store_data(texel_film, uni.film.vector_id, float4(0.0f), out_color);
1070 }
1071 else {
1072 float depth = reverse_z::read(texelFetch(depth_tx, film_sample.texel, 0).x);
1073 float4 vector;
1075 vector = camera.resolve_panoramic(views, vector_tx, film_sample.texel, depth);
1076 }
1077 else {
1078 vector = camera.resolve_local(views, vector_tx, film_sample.texel, depth);
1079 }
1080 /* Transform to pixel space, matching Cycles format. */
1081 vector *= float4(float2(uni.film.render_extent), float2(uni.film.render_extent));
1082
1083 store_depth(texel_film, film_sample.texel, depth, out_depth);
1084 if (normal_id != -1) {
1085 float4 normal = texelFetch(
1086 rp_color_tx, int3(film_sample.texel, uni.render_pass.normal_id), 0);
1087 store_data(texel_film, normal_id, normal, out_color);
1088 }
1089 if (uni.film.position_id != -1) {
1091 rp_color_tx, int3(film_sample.texel, uni.render_pass.position_id), 0);
1092 store_data(texel_film, uni.film.position_id, position, out_color);
1093 }
1094 store_data(texel_film, uni.film.vector_id, vector, out_color);
1095 }
1096 store_distance(texel_film, film_sample.weight);
1097 }
1098 else {
1099 out_depth = imageLoadFast(depth_img, texel_film).r;
1100 if (display_id == -1) {
1101 /* NOP. */
1102 }
1103 else if (display_id == normal_id) {
1104 out_color = imageLoadFast(color_accum_img, int3(texel_film, display_id));
1105 }
1106 else if (display_id == uni.film.position_id) {
1107 out_color = imageLoadFast(color_accum_img, int3(texel_film, uni.film.position_id));
1108 }
1109 }
1110 }
1111
1112 if (flag_test(enabled_categories, PASS_CATEGORY_COLOR_1)) {
1113 float4 diffuse_color_accum = float4(0.0f);
1114 float4 specular_color_accum = float4(0.0f);
1115 float4 diffuse_light_accum = float4(0.0f);
1116 float4 specular_light_accum = float4(0.0f);
1117
1118 for (int i = 0; i < samples_len; i++) {
1119 FilmSample src = sample_get(i, texel_film, panoramic_samples);
1120 sample_accum(src,
1121 uni.film.diffuse_color_id,
1122 uni.render_pass.diffuse_color_id,
1124 diffuse_color_accum);
1125 sample_accum(src,
1126 uni.film.specular_color_id,
1127 uni.render_pass.specular_color_id,
1129 specular_color_accum);
1130 sample_accum(src,
1131 uni.film.diffuse_light_id,
1132 uni.render_pass.diffuse_light_id,
1134 diffuse_light_accum);
1135 sample_accum(src,
1136 uni.film.specular_light_id,
1137 uni.render_pass.specular_light_id,
1139 specular_light_accum);
1140 }
1141
1143 uni.film.diffuse_color_id,
1144 uni.film.diffuse_light_id,
1145 diffuse_color_accum,
1146 diffuse_light_accum,
1147 out_color);
1149 uni.film.specular_color_id,
1150 uni.film.specular_light_id,
1151 specular_color_accum,
1152 specular_light_accum,
1153 out_color);
1154 }
1155
1156 if (flag_test(enabled_categories, PASS_CATEGORY_COLOR_2)) {
1157 float4 environment_accum = float4(0.0f);
1158 float4 volume_light_accum = float4(0.0f);
1159 float4 emission_accum = float4(0.0f);
1160 float mist_accum = 0.0f;
1161 float shadow_accum = 0.0f;
1162 float ao_accum = 0.0f;
1163
1164 for (int i = 0; i < samples_len; i++) {
1165 FilmSample src = sample_get(i, texel_film, panoramic_samples);
1166 sample_accum(src,
1167 uni.film.volume_light_id,
1168 uni.render_pass.volume_light_id,
1170 volume_light_accum);
1172 src, uni.film.emission_id, uni.render_pass.emission_id, rp_color_tx, emission_accum);
1173 sample_accum(src,
1174 uni.film.environment_id,
1175 uni.render_pass.environment_id,
1177 environment_accum);
1179 src, uni.film.shadow_id, uni.render_pass.shadow_id, rp_value_tx, shadow_accum);
1180 sample_accum(src,
1181 uni.film.ambient_occlusion_id,
1182 uni.render_pass.ambient_occlusion_id,
1184 ao_accum);
1185 sample_accum_mist(src, mist_accum);
1186 }
1187 /* Monochrome render passes that have colored outputs. Set alpha to 1. */
1188 float4 shadow_accum_color = float4(float3(shadow_accum), weight_accum);
1189 float4 ao_accum_color = float4(float3(ao_accum), weight_accum);
1190
1191 store_color(dst, uni.film.volume_light_id, volume_light_accum, out_color);
1192 store_color(dst, uni.film.emission_id, emission_accum, out_color);
1193 store_color(dst, uni.film.environment_id, environment_accum, out_color);
1194 store_color(dst, uni.film.shadow_id, shadow_accum_color, out_color);
1195 store_color(dst, uni.film.ambient_occlusion_id, ao_accum_color, out_color);
1196 store_value(dst, uni.film.mist_id, mist_accum, out_color);
1197 }
1198
1199 if (flag_test(enabled_categories, PASS_CATEGORY_COLOR_3)) {
1200 float4 transparent_accum = float4(0.0f);
1201
1202 for (int i = 0; i < samples_len; i++) {
1203 FilmSample src = sample_get(i, texel_film, panoramic_samples);
1204 sample_accum(src,
1205 uni.film.transparent_id,
1206 uni.render_pass.transparent_id,
1208 transparent_accum);
1209 }
1210 /* Alpha stores transmittance for transparent pass. */
1211 transparent_accum.a = weight_accum - transparent_accum.a;
1212
1213 store_color(dst, uni.film.transparent_id, transparent_accum, out_color);
1214 }
1215
1216 if (flag_test(enabled_categories, PASS_CATEGORY_DENOISE)) {
1217 float denoising_depth_accum = 0.0f;
1218 float4 denoising_normal_accum = float4(0.0f);
1219 float denoising_roughness_accum = 0.0f;
1220 float4 denoising_diffuse_albedo_accum = float4(0.0f);
1221 float4 denoising_specular_albedo_accum = float4(0.0f);
1222
1223 for (int i = 0; i < samples_len; i++) {
1224 FilmSample src = sample_get(i, texel_film, panoramic_samples);
1225 if (uni.film.denoising_depth_id >= 0 && !sample_is_black(src)) {
1226 float depth = reverse_z::read(texelFetch(depth_tx, src.texel, 0).x);
1227 if (depth == 1.0f) {
1228 /* Match clear value of depth pass. */
1229 depth = 1e10f;
1230 }
1231 else {
1232 /* Average over view space z. */
1233 float2 uv = (float2(src.texel) + 0.5f) / float2(textureSize(depth_tx, 0).xy);
1234 depth = depth_convert_to_scene(views.get(0), depth, uv, constants.use_panoramic);
1235 }
1236 denoising_depth_accum += depth * src.weight;
1237 }
1238 sample_accum(src,
1239 uni.film.denoising_normal_id,
1240 uni.render_pass.denoising_normal_id,
1242 denoising_normal_accum);
1243 sample_accum(src,
1244 uni.film.denoising_roughness_id,
1245 uni.render_pass.denoising_roughness_id,
1247 denoising_roughness_accum);
1248 sample_accum(src,
1249 uni.film.denoising_diffuse_albedo_id,
1250 uni.render_pass.denoising_diffuse_albedo_id,
1252 denoising_diffuse_albedo_accum);
1253 sample_accum(src,
1254 uni.film.denoising_specular_albedo_id,
1255 uni.render_pass.denoising_specular_albedo_id,
1257 denoising_specular_albedo_accum);
1258 }
1259
1260 store_denoising_depth(dst, denoising_depth_accum, out_color);
1261 store_color(dst, uni.film.denoising_normal_id, denoising_normal_accum, out_color, false);
1262 store_value(dst, uni.film.denoising_roughness_id, denoising_roughness_accum, out_color);
1264 dst, uni.film.denoising_diffuse_albedo_id, denoising_diffuse_albedo_accum, out_color);
1266 dst, uni.film.denoising_specular_albedo_id, denoising_specular_albedo_accum, out_color);
1267 }
1268
1269 if (flag_test(enabled_categories, PASS_CATEGORY_AOV)) {
1270 for (int aov = 0; aov < uni.film.aov_color_len; aov++) {
1271 float4 aov_accum = float4(0.0f);
1272
1273 for (int i = 0; i < samples_len; i++) {
1274 FilmSample src = sample_get(i, texel_film, panoramic_samples);
1275 sample_accum(src, 0, uni.render_pass.color_len + aov, rp_color_tx, aov_accum);
1276 }
1277 store_color(dst, uni.film.aov_color_id + aov, aov_accum, out_color, false);
1278 }
1279
1280 for (int aov = 0; aov < uni.film.aov_value_len; aov++) {
1281 float aov_accum = 0.0f;
1282
1283 for (int i = 0; i < samples_len; i++) {
1284 FilmSample src = sample_get(i, texel_film, panoramic_samples);
1285 sample_accum(src, 0, uni.render_pass.value_len + aov, rp_value_tx, aov_accum);
1286 }
1287 store_value(dst, uni.film.aov_value_id + aov, aov_accum, out_color);
1288 }
1289 }
1290
1291 if (flag_test(enabled_categories, PASS_CATEGORY_CRYPTOMATTE)) {
1292 if (uni.film.cryptomatte_samples_len != 0) {
1293
1294 /* Cryptomatte passes cannot be cleared by a weighted store like other passes. */
1295 if (!uni.film.use_history || use_reprojection) {
1297 }
1298
1300 dst, texel_film, panoramic_samples, uni.film.cryptomatte_object_id, 0, out_color);
1302 dst, texel_film, panoramic_samples, uni.film.cryptomatte_asset_id, 1, out_color);
1304 dst, texel_film, panoramic_samples, uni.film.cryptomatte_material_id, 2, out_color);
1305 }
1306 }
1307 }
1308};
1309
1310[[vertex]] void fullscreen_vert([[vertex_id]] const int vert_id, [[position]] float4 &out_position)
1311{
1312 fullscreen_vertex(vert_id, out_position);
1313}
1314
1317};
1318
1320 /* True if we bypass the accumulation and directly output the accumulation buffer. */
1322};
1323
1324/* Accumulate and output to the render frame-buffer.
1325 * Used for viewport. */
1326[[fragment]]
1329 [[resource_table]] const Uniform &uni,
1330 [[resource_table]] const draw::View &views,
1331 [[out]] FilmFragOut &frag_out,
1332 [[frag_coord]] const float4 frag_co,
1333 [[frag_depth(any)]] float out_depth)
1334{
1335 [[resource_table]] Cryptomatte &cryptomatte = film.cryptomatte;
1336
1337 int2 texel_film = int2(frag_co.xy) - uni.uniform_buf.film.offset;
1338
1339 if (is_panoramic(uni.uniform_buf.camera.type) &&
1340 is_zero(film.panoramic_direction_get(texel_film)))
1341 {
1342 /* A zero direction means failed panoramic sampling, e.g. outside the fisheye lens.
1343 * Display as black with background depth so overlay can draw over them. */
1344 frag_out.color = float4(0.0f, 0.0f, 0.0f, 1.0f);
1345 out_depth = 1.0f;
1346 return;
1347 }
1348 else if (srt.display_only) {
1349 out_depth = imageLoadFast(film.depth_img, texel_film).r;
1350
1351 if (film.display_id == -1) {
1352 frag_out.color = texelFetch(film.in_combined_tx, texel_film, 0);
1353 }
1354 else if (uni.uniform_buf.film.display_storage_type == PASS_STORAGE_VALUE) {
1355 frag_out.color.rgb =
1356 imageLoadFast(film.value_accum_img, int3(texel_film, film.display_id)).rrr;
1357 frag_out.color.a = 1.0f;
1358 }
1359 else if (uni.uniform_buf.film.display_storage_type == PASS_STORAGE_COLOR) {
1360 frag_out.color = imageLoadFast(film.color_accum_img, int3(texel_film, film.display_id));
1361 }
1362 else if (uni.uniform_buf.film.display_storage_type == PASS_STORAGE_CRYPTOMATTE) {
1363 frag_out.color = cryptomatte::false_color(
1364 imageLoadFast(cryptomatte.cryptomatte_img, int3(texel_film, film.display_id)).r);
1365 }
1366 else /* PASS_STORAGE_DENOISING_DEPTH */ {
1367 frag_out.color.rgb = imageLoadFast(film.denoising_depth_img, texel_film).rrr;
1368 frag_out.color.a = 1.0f;
1369 }
1370 }
1371 else {
1372 film.process_render_sample(texel_film, frag_out.color, out_depth);
1373 }
1374
1375 out_depth = views.get(0).depth_view_to_screen(-out_depth);
1376
1377 out_depth = display_depth_amend(out_depth);
1378}
1379
1387[[fragment]]
1389 [[out]] FilmFragOut &frag_out,
1390 [[resource_table]] const Uniform &uni,
1391 [[resource_table]] const draw::View &views,
1392 [[frag_coord]] const float4 frag_co,
1393 [[frag_depth(any)]] float out_depth)
1394{
1395 [[resource_table]] Cryptomatte &cryptomatte = film.cryptomatte;
1396
1397 int2 texel = int2(frag_co.xy) - uni.uniform_buf.film.offset;
1398
1399 if (film.display_id == -1) {
1400 frag_out.color = texelFetch(film.in_combined_tx, texel, 0);
1401 }
1402 else if (uni.uniform_buf.film.display_storage_type == PASS_STORAGE_VALUE) {
1403 frag_out.color.rgb = imageLoadFast(film.value_accum_img, int3(texel, film.display_id)).rrr;
1404 frag_out.color.a = 1.0f;
1405 }
1406 else if (uni.uniform_buf.film.display_storage_type == PASS_STORAGE_COLOR) {
1407 frag_out.color = imageLoadFast(film.color_accum_img, int3(texel, film.display_id));
1408 }
1409 else if (uni.uniform_buf.film.display_storage_type == PASS_STORAGE_CRYPTOMATTE) {
1410 frag_out.color = cryptomatte::false_color(
1411 imageLoadFast(cryptomatte.cryptomatte_img, int3(texel, film.display_id)).r);
1412 }
1413 else /* PASS_STORAGE_DENOISING_DEPTH */ {
1414 frag_out.color = imageLoadFast(film.denoising_depth_img, texel);
1415 }
1416
1417 out_depth = imageLoadFast(film.depth_img, texel).r;
1418 out_depth = views.get(0).depth_view_to_screen(-out_depth);
1419 out_depth += 2.4e-7f * 4.0f + gpu_fwidth(out_depth);
1420 out_depth = saturate(out_depth);
1421}
1422
1423/* Accumulate sample result. */
1426 [[resource_table]] const Uniform &uni,
1427 [[global_invocation_id]] const uint3 global_id)
1428{
1429 int2 texel_film = int2(global_id.xy);
1430 /* Not used. */
1431 float4 out_color;
1432 float out_depth;
1433
1434 if (any(greaterThanEqual(texel_film, uni.uniform_buf.film.extent))) {
1435 return;
1436 }
1437
1438 film.process_render_sample(texel_film, out_color, out_depth);
1439}
1440
1441/* The combined pass is stored into its own 2D texture with a format of
1442 * SFLOAT_16_16_16_16. */
1448
1449/* The depth pass is stored into its own 2D texture with a format of SFLOAT_32. */
1455
1456/* Value passes are stored in a slice of a 2D texture array with a format of
1457 * SFLOAT_16. */
1463
1464/* Color passes are stored in a slice of a 2D texture array with a format of
1465 * SFLOAT_16_16_16_16. */
1471
1472/* Cryptomatte passes are stored in a slice of a 2D texture array with a format of
1473 * SFLOAT_32_32_32_32. */
1479
1480/* Used by the Viewport Compositor to copy EEVEE passes to the compositor DRW passes textures. The
1481 * output passes covert the entire display extent even when border rendering because that's what
1482 * the compositor expects, so areas outside of the border are zeroed. */
1483template<typename ResourceT, bool layered>
1485void pass_convert([[resource_table]] ResourceT &srt,
1486 [[global_invocation_id]] const uint3 global_id)
1487{
1488 int2 texel = int2(global_id.xy);
1489 if (any(greaterThan(texel, imageSize(srt.output_img) - int2(1)))) {
1490 return;
1491 }
1492 /* In case of border rendering, clear areas outside of the border. The offset is the lower left
1493 * corner of the border. */
1494 int2 input_bounds = textureSize(srt.input_tx, 0).xy - int2(1);
1495 if (any(lessThan(texel, srt.offset)) || any(greaterThan(texel, srt.offset + input_bounds))) {
1496 imageStoreFast(srt.output_img, texel, float4(0.0f));
1497 }
1498 else {
1499 if constexpr (layered) {
1501 srt.output_img, texel, texelFetch(srt.input_tx, int3(texel - srt.offset, 0), 0));
1502 }
1503 else {
1504 imageStoreFast(srt.output_img, texel, texelFetch(srt.input_tx, texel - srt.offset, 0));
1505 }
1506 }
1507}
1508
1514
1515} // namespace eevee::film
1516
1519 .use_panoramic = false,
1520 });
1523 .use_panoramic = true,
1524 });
1528 .use_panoramic = false,
1529 });
1533 .use_panoramic = false,
1534 });
1535
btScalar max
bool closest(btVector3 &v)
float line_aabb_clipping_dist(float3 line_origin, float3 line_direction, float3 aabb_min, float3 aabb_max)
#define FILM_GROUP_SIZE
PipelineCompute eevee_film_comp_panoramic(eevee::film::accumulate_comp, eevee::film::FilmConstants{ .use_panoramic=true, })
PipelineCompute eevee_film_pass_convert_value(eevee::film::pass_convert< eevee::film::ConvertValue, true >)
PipelineCompute eevee_film_comp(eevee::film::accumulate_comp, eevee::film::FilmConstants{ .use_panoramic=false, })
PipelineGraphic eevee_film_frag(eevee::film::fullscreen_vert, eevee::film::accumulate_or_display_frag, eevee::film::FilmConstants{ .use_panoramic=false, })
PipelineCompute eevee_film_pass_convert_cryptomatte(eevee::film::pass_convert< eevee::film::ConvertCryptomatte, true >)
PipelineCompute eevee_film_pass_convert_depth(eevee::film::pass_convert< eevee::film::ConvertDepth, false >)
PipelineGraphic eevee_film_copy_frag(eevee::film::fullscreen_vert, eevee::film::display_frag, eevee::film::FilmConstants{ .use_panoramic=false, })
PipelineCompute eevee_film_pass_convert_combined(eevee::film::pass_convert< eevee::film::ConvertCombined, false >)
PipelineCompute eevee_film_pass_convert_color(eevee::film::pass_convert< eevee::film::ConvertColor, true >)
float2 sample_disk(float2 rand)
@ SFLOAT_16_16_16_16
@ SFLOAT_32_32_32_32
#define position
#define image(slot, qualifiers, format)
#define frag_coord
#define push_constant
#define compilation_constant
#define resource_table
#define sampler(slot)
#define static_branch
#define specialization_constant(default_value)
#define frag_depth(mode)
#define global_invocation_id
#define fragment
#define vertex_id
#define unroll
#define compute
#define layer
#define vertex
#define frag_color(slot)
#define local_size(...)
#define isnan
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
bool any(VecOp< bool, D >) RET
#define pow
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
T gpu_fwidth(T) RET
#define modf
#define abs
#define floor
float distance(float, float) RET
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
#define sqrt
MatBase< R, C > transpose(MatBase< C, R >) RET
VecBase< float, D > uintBitsToFloat(VecOp< uint, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
VecBase< int, D > findLSB(VecOp< int, D >) RET
ImageBase< float, 2, true > image2DArray
ImageBase< float, 2 > image2D
IMG_TEMPLATE SizeVec imageSize(const T &) RET
#define imageLoadFast
#define imageStoreFast
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
SamplerBase< float, 2, false, false, false, true > sampler2DDepth
SamplerBase< float, 2, false, true > sampler2DArray
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
void fullscreen_vertex(int vertex_id, float4 &out_position)
float square(float v)
bool is_zero(float3x3 a)
float2 project_point(float3x3 mat, float2 point)
float safe_rcp(float a)
float length_squared(VecT a)
float reduce_add(float2 a)
float reduce_min(float2 a)
bool flag_test(uint flag, uint val)
MatBase< float, 4, 4 > float4x4
float3 scene_linear_from_YCoCg(float3 ycocg_color)
float3 YCoCg_from_scene_linear(float3 rgb_color)
float3 view_from_uv(float4x4 projmat, float2 uv)
float2 uv_from_view(float4x4 projmat, bool is_persp, float3 vV)
float4 false_color(float hash)
float luma_weight(const Uniform &uni, float luma)
void pass_convert(ResourceT &srt, const uint3 global_id)
float4 patch_float_for_16f_storage(float4 color, float dither)
float depth_convert_to_scene(const ViewMatrices view, float depth, float2 uv, bool is_panoramic_camera)
ePanoramicFace panoramic_face_index(float3 direction)
float display_depth_amend(float depth)
void accumulate_or_display_frag(const FilmDisplay &srt, Film &film, const Uniform &uni, const draw::View &views, FilmFragOut &frag_out, const float4 frag_co, float out_depth)
float4 texelfetch_as_YCoCg_opacity(sampler2D tx, int2 texel)
float4 safe_divide_even_color(float4 a, float4 b)
void fullscreen_vert(const int vert_id, float4 &out_position)
void display_frag(Film &film, FilmFragOut &frag_out, const Uniform &uni, const draw::View &views, const float4 frag_co, float out_depth)
void accumulate_comp(Film &film, const Uniform &uni, const uint3 global_id)
float2 interleaved_gradient_2d(float2 pixel, float2 seed, float2 offset)
T read(T depth_buffer_value)
#define hash
Definition noise_c.cc:156
#define mix
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
#define min(a, b)
Definition sort.cc:38
float depth_screen_to_view(float ss_depth) const
float3 point_screen_to_view(float3 ssP) const
ViewMatrices get(uint view_id) const
void clear_samples(FilmSample dst)
void store_film_sample(FilmSample dst, int cryptomatte_layer_id, int cryptomatte_samples_len, float2 crypto_sample, float4 &out_color)
float2 rng_2D_get(const eSamplingDimension dim) const
float rng_1D_get(const eSamplingDimension dim) const
const UniformData & uniform_buf
void sample_accum(FilmSample samp, int pass_id, int layer, sampler2DArray tex, float4 &accum)
sampler2DDepth depth_tx
void store_data(int2 texel_film, int pass_id, float4 data_sample, float4 &display)
void store_color_and_light(FilmSample dst, int color_pass_id, int light_pass_id, float4 color, float4 light, float4 &display)
FilmSample panoramic_sample_get(int sample_n, int2 texel_film)
float3 panoramic_direction_get(int2 texel_film)
void sample_cryptomatte_accum(FilmSample samp, int layer, sampler2D tex, float2(&crypto_samples)[4])
bool panoramic_texel_is_owned_by_view(float3 camera_direction, ePanoramicFace view_id)
void sample_accum_mist(FilmSample samp, float &accum)
void process_render_sample(int2 texel_film, float4 &out_color, float &out_depth)
void store_weight(int2 texel, float value)
const image2DArray in_weight_img
sampler2DArray rp_color_tx
float3 panoramic_direction_get(float2 film_co)
Cryptomatte cryptomatte
image2DArray value_accum_img
void store_color_ex(FilmSample dst, int pass_id, float4 color, float4 &display, bool do_clamp_negative_values)
void cryptomatte_layer_accum_and_store(FilmSample dst, int2 texel_film, const FilmSample(&panoramic_samples)[FILM_PANORAMIC_SAMPLE_LEN], int pass_id, int layer_component, float4 &out_color)
image2DArray out_weight_img
float weight_load(int2 texel)
FilmSample sample_get(int sample_n, int2 texel_film, const FilmSample(&panoramic_samples)[FILM_PANORAMIC_SAMPLE_LEN])
void sample_accum_combined(FilmSample samp, float4 &accum, float &weight_accum)
void combined_neighbor_boundbox(int2 texel, float4 &min_c, float4 &max_c)
void store_depth(int2 texel_film, int2 texel_sample, float value, float &out_depth)
float4 sample_catmull_rom(sampler2D color_tx, float2 input_texel)
float history_blend_factor(float velocity, float2 texel, float luma_min, float luma_max, float luma_history)
float distance_load(int2 texel)
float2 pixel_history_motion_vector(int2 texel_sample)
void store_denoising_depth(FilmSample dst, float value, float4 &display)
void copy_history(int2 texel_film, float4 &out_color, float &out_depth)
void store_value(FilmSample dst, int pass_id, float value, float4 &display)
CameraVelocity camera
FilmSample sample_get(int sample_n, int2 texel_film)
ePanoramicFace panoramic_view_id_get()
bool sample_is_black(FilmSample samp)
float4 amend_combined_history(float4 min_color, float4 max_color, float4 color_history, float4 src_color)
float aabb_clipping_dist_alpha(float origin, float direction, float aabb_min, float aabb_max)
void get_catmull_rom_weights(float2 t, float2(&weights)[4])
sampler2DArray rp_value_tx
float4 display_color_load(int2 texel_film)
float weight_accumulation(int2 texel_film)
void store_combined(FilmSample dst, int2 src_texel, float4 color, float color_weight, float4 &display)
float4 clamp_negative_values(float4 color)
image2DArray color_accum_img
void store_distance(int2 texel, float value)
void sample_accum(FilmSample samp, int pass_id, int layer, sampler2DArray tex, float &accum)
FilmConstants constants
void store_color(FilmSample dst, int pass_id, float4 color, float4 &display, bool do_clamp_negative_values=true)
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225