Blender V5.3
denoising_passes.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8
9#include "kernel/film/write.h"
10
12
13#ifdef __DENOISING_FEATURES__
14ccl_device_forceinline float denoising_depth_compute(KernelGlobals kg,
16 const ccl_private ShaderData *sd,
18 const bool follow_reflections)
19{
20 float depth;
21 const float d = sd->ray_length - INTEGRATOR_STATE(state, ray, tmin);
22 if (follow_reflections) {
23 /* Write the ray length minus tmin. */
24 depth = d;
25 }
26 else {
27 /* Write the camera z depth. */
28 const float3 prev_P = sd->P + sd->wi * d;
29 const float prev_depth = camera_z_depth(kg, prev_P);
30 const float new_depth = camera_z_depth(kg, sd->P);
31 depth = new_depth - prev_depth;
32 }
33
34 /* Include nearclip distance for the first bounce only to make sure that the depth value will
35 * never be lower than the near clip plane. OIDN expects the denoising depth to be in the range
36 * [near, far]. For orthographic cameras this is skipped, as `nearclip = -farclip / 2` is used
37 * and OIDN does not accept negative near clip planes.*/
38 if (INTEGRATOR_STATE(state, path, bounce) == 0 &&
39 INTEGRATOR_STATE(state, path, transparent_bounce) == 0 &&
40 INTEGRATOR_STATE(state, path, volume_bounds_bounce) == 0 && kernel_data.cam.nearclip > 0.0f)
41 {
42 if (follow_reflections && kernel_data.cam.type == CAMERA_PERSPECTIVE) {
43 /* Depth is a ray length here, so add the ray length from the camera to the near clip plane
44 * rather than the near clip z-depth. */
45 const Transform cameratoworld = kernel_data.cam.cameratoworld;
46 const float3 camD = make_float3(cameratoworld.x.z, cameratoworld.y.z, cameratoworld.z.z);
47 depth += kernel_data.cam.nearclip / dot(-sd->wi, camD);
48 }
49 else {
50 depth += kernel_data.cam.nearclip;
51 }
52 }
53
55}
56
57ccl_device_forceinline void film_write_denoising_features_surface(KernelGlobals kg,
59 const uint32_t path_flag,
60 const ccl_private ShaderData *sd,
63{
64 if (!(path_flag & PATH_RAY_DENOISING_FEATURES)) {
65 return;
66 }
67
68 /* Don't write denoising passes for paths that were split off for shadow catchers
69 * to avoid double-counting. */
70 if (path_flag & PATH_RAY_SHADOW_CATCHER_PASS) {
71 return;
72 }
73
74 const bool use_albedo_roughness_weighting = (kernel_data.film.denoising_pass_options_flag &
76
78
79 float3 normal = zero_float3();
80 Spectrum diffuse_albedo = zero_spectrum();
81 Spectrum specular_albedo = zero_spectrum();
82 Spectrum transparent_albedo = zero_spectrum();
83 float specular_roughness = 0.0f;
84 float sum_weight = 0.0f;
85 float sum_nonspecular_weight = 0.0f;
86
87 for (int i = 0; i < sd->num_closure; i++) {
88 const ccl_private ShaderClosure *sc = &sd->closure[i];
89
90 if (!CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
91 continue;
92 }
93
94 /* Transparency always passes through. */
95 if (CLOSURE_IS_BSDF_TRANSPARENT(sc->type)) {
96 transparent_albedo += sc->weight;
97 continue;
98 }
99
100 const Spectrum albedo = closure_albedo(kg, sd, sc, true, true);
101 const float closure_weight = average(albedo);
102
103 /* All closures contribute to the normal feature, but only diffuse-like ones to the albedo. */
104 /* If far-field hair, use fiber tangent as feature instead of normal. */
105 normal += (sc->type == CLOSURE_BSDF_HAIR_HUANG_ID ? safe_normalize(sd->dPdu) : sc->N) *
106 closure_weight;
107
108 /* bsdf_get_specular_roughness_squared returns GGX alpha squared (alpha_x*alpha_y). Use sqrtf
109 * to get GGX alpha. */
110 const float roughness = sqrtf(bsdf_get_specular_roughness_squared(sc));
111
112 /* Transition smoothly from specular to diffuse between 0.0 and 0.15 roughness. */
113 const float diffuse_weight = (sc->type == CLOSURE_BSDF_HAIR_HUANG_ID) ?
114 1.0f :
115 smoothstep(0.0f, 0.15f, roughness);
116
117 if (use_albedo_roughness_weighting) {
118 diffuse_albedo += albedo * diffuse_weight;
119 specular_albedo += albedo * (1.0f - diffuse_weight);
120 }
121 else if (CLOSURE_IS_BSDF_DIFFUSE(sc->type) || CLOSURE_IS_BSSRDF(sc->type)) {
122 diffuse_albedo += albedo;
123 }
124 else if (CLOSURE_IS_BSDF_GLOSSY(sc->type) || CLOSURE_IS_GLASS(sc->type)) {
125 specular_albedo += albedo;
126 }
127 /* Apply sqrtf again to convert GGX alpha to perceptual roughness. */
128 specular_roughness += sqrtf(roughness) * closure_weight;
129
130 sum_weight += closure_weight;
131 sum_nonspecular_weight += closure_weight * diffuse_weight;
132 }
133
134 /* Fraction of non-transparent closures, for smooth blending at transparent surfaces. */
135 const float transparent_weight = average(transparent_albedo);
136 const float total_weight = sum_weight + transparent_weight;
137
138 /* Blend between writing features at this bounce vs. deferring to the next bounce based
139 * on the proportion of diffuse closures. Smoothly transition between 0.0 and 0.5 diffuse
140 * fraction. */
141 float feature_weight = 0.0f;
142 if (sum_weight > 0.0f) {
143 normal /= sum_weight;
144 specular_roughness /= sum_weight;
145
146 feature_weight = smoothstep(0.0f, 0.5f, sum_nonspecular_weight / sum_weight);
147 }
148
149 float deferred_feature_weight = 1.0f - feature_weight;
150
151 /* Whether to defer features to the next bounce for individual passes. */
152 const bool follow_reflections = (kernel_data.film.denoising_pass_options_flag &
154 if (!follow_reflections) {
155 feature_weight = 1.0f;
156 }
157
158 const bool is_first_bounce = INTEGRATOR_STATE(state, path, bounce) == 0;
159
162
163 if (is_first_bounce || follow_reflections) {
164 if (kernel_data.film.pass_denoising_depth != PASS_UNUSED) {
165 const float denoising_depth = denoising_depth_compute(
166 kg, state, sd, denoising_feature_throughput, follow_reflections);
167 film_write_pass_float(buffer + kernel_data.film.pass_denoising_depth, denoising_depth);
168 }
169
170 if (kernel_data.film.pass_denoising_normal != PASS_UNUSED && feature_weight > 0.0f) {
171 /* Transform normal into camera space. */
172 const Transform worldtocamera = kernel_data.cam.worldtocamera;
173 float3 denoising_normal = transform_direction(&worldtocamera, normal);
174 const float opaque_fraction = (total_weight > 0.0f) ? (sum_weight / total_weight) : 1.0f;
175
176 denoising_normal = ensure_finite(denoising_normal * opaque_fraction * feature_weight *
178 film_write_pass_float3(buffer + kernel_data.film.pass_denoising_normal, denoising_normal);
179 }
180
181 if (kernel_data.film.pass_denoising_albedo != PASS_UNUSED && feature_weight > 0.0f) {
182 const Spectrum denoising_albedo = ensure_finite(diffuse_albedo * feature_weight *
184 film_write_pass_spectrum(buffer + kernel_data.film.pass_denoising_albedo, denoising_albedo);
185 }
186 }
187
188 if (is_first_bounce) {
189 if (kernel_data.film.pass_denoising_roughness != PASS_UNUSED) {
190 const float denoising_roughness = ensure_finite(specular_roughness *
192 film_write_pass_float(buffer + kernel_data.film.pass_denoising_roughness,
193 denoising_roughness);
194 }
195
196 if (kernel_data.film.pass_denoising_specular_albedo != PASS_UNUSED) {
197 const Spectrum denoising_specular_albedo = ensure_finite(specular_albedo *
199 film_write_pass_spectrum(buffer + kernel_data.film.pass_denoising_specular_albedo,
200 denoising_specular_albedo);
201 }
202
203 if (kernel_data.film.pass_denoising_backward_motion != PASS_UNUSED &&
204 /* Only write motion for a single hit, using the same logic as film_write_data_passes
205 * (which also sets PATH_RAY_SINGLE_PASS_DONE afterwards). */
206 !(path_flag & PATH_RAY_SINGLE_PASS_DONE) &&
207 (!(sd->runtime_flag & (SR_TRANSPARENT | SR_RAY_PORTAL)) ||
208 kernel_data.film.pass_alpha_threshold == 0.0f ||
209 average(surface_shader_alpha(sd)) >= kernel_data.film.pass_alpha_threshold))
210 {
211 const float3 backward_motion = primitive_motion_vector_backward_depth_delta(kg, sd);
212 film_write_pass_float3(buffer + kernel_data.film.pass_denoising_backward_motion,
213 backward_motion);
214 }
215 }
216 else if (INTEGRATOR_STATE(state, path, bounce) == 1 && (path_flag & PATH_RAY_REFLECT)) {
217 if (kernel_data.film.pass_denoising_specular_motion != PASS_UNUSED) {
218 const float3 reflector_P = INTEGRATOR_STATE(state, ray, P);
219 const float3 reflector_N = INTEGRATOR_STATE(state, path, mis_origin_n);
220
221 const float4 denoising_specular_motion = primitive_motion_vector_reflection(
222 kg, reflector_P, reflector_N, sd);
223 film_write_pass_float(buffer + kernel_data.film.pass_denoising_specular_motion + 0,
224 denoising_specular_motion.x);
225 film_write_pass_float(buffer + kernel_data.film.pass_denoising_specular_motion + 1,
226 denoising_specular_motion.y);
227 }
228 }
229
230 /* Portion deferred to the next bounce. Specularity uses the feature weight, transparent
231 * always passes through. */
232 Spectrum deferred_albedo = specular_albedo * deferred_feature_weight + transparent_albedo;
233
234 /* When not following reflections, but the specular motion pass is enabled, still need to
235 * continue to the first bounce, but with no weight for the albedo pass. */
236 if (!follow_reflections && kernel_data.film.pass_denoising_specular_motion == PASS_UNUSED) {
237 deferred_albedo = transparent_albedo;
238 }
239 if (reduce_max(fabs(deferred_albedo)) > 1e-4f) {
240 if (!follow_reflections) {
241 deferred_albedo = transparent_albedo;
242 }
245 deferred_albedo;
246 }
247 else {
249 }
250}
251
252ccl_device_forceinline void film_write_denoising_features_surface_volume(
253 KernelGlobals kg,
255 const ccl_private ShaderData *sd,
257{
259
260 const bool follow_reflections = (kernel_data.film.denoising_pass_options_flag &
262 const bool is_first_bounce = INTEGRATOR_STATE(state, path, bounce) == 0;
263
266
267 if (is_first_bounce || follow_reflections) {
268 if (kernel_data.film.pass_denoising_depth != PASS_UNUSED) {
269 const float denoising_depth = denoising_depth_compute(
270 kg, state, sd, denoising_feature_throughput, follow_reflections);
271 film_write_pass_float(buffer + kernel_data.film.pass_denoising_depth, denoising_depth);
272 }
273 }
274}
275
276ccl_device_forceinline void film_write_denoising_features_volume(KernelGlobals kg,
278 const Spectrum albedo,
279 const bool scatter,
282{
284
287
288 if (scatter && kernel_data.film.pass_denoising_normal != PASS_UNUSED) {
289 /* Assume scatter is sufficiently diffuse to stop writing denoising features. */
291
292 /* Write view direction as normal. */
293 const float3 denoising_normal = make_float3(0.0f, 0.0f, -1.0f);
294 film_write_pass_float3(buffer + kernel_data.film.pass_denoising_normal, denoising_normal);
295 }
296
297 if (kernel_data.film.pass_denoising_albedo != PASS_UNUSED) {
298 /* Write albedo. */
299 const Spectrum denoising_albedo = ensure_finite(denoising_feature_throughput * albedo);
300 film_write_pass_spectrum(buffer + kernel_data.film.pass_denoising_albedo, denoising_albedo);
301 }
302}
303
304ccl_device_forceinline void film_write_denoising_features_background(KernelGlobals kg,
306 const uint32_t path_flag,
309{
310 /* Don't write denoising passes for paths that were split off for shadow catchers
311 * to avoid double-counting. */
312 if (!(path_flag & PATH_RAY_DENOISING_FEATURES) || (path_flag & PATH_RAY_SHADOW_CATCHER_PASS)) {
313 return;
314 }
315
316 /* Do not write default background denoising data for secondary paths. */
317 if (INTEGRATOR_STATE(state, path, bounce) != 0) {
318 return;
319 }
320
322
323 if (kernel_data.film.pass_denoising_depth != PASS_UNUSED) {
326
327 /* Transparent surfaces and volumes keep `bounce == 0` and the first of them already wrote the
328 * near clip distance as part of their depth segment. */
329 const bool wrote_first_segment = INTEGRATOR_STATE(state, path, transparent_bounce) != 0 ||
330 INTEGRATOR_STATE(state, path, volume_bounds_bounce) != 0;
331
332 float farclip_depth;
333 /* Add nearclip only when the depth is measured from the camera and nothing was written yet.
334 * Skip it for orthographic cameras (nearclip < 0.0f), when cliplength is FLT_MAX (to avoid
335 * numerical issues), or once the first segment already covered the near clip distance. */
336 if (kernel_data.cam.nearclip < 0.0f || kernel_data.cam.cliplength == FLT_MAX ||
337 wrote_first_segment)
338 {
339 farclip_depth = kernel_data.cam.cliplength;
340 }
341 else {
342 farclip_depth = kernel_data.cam.nearclip + kernel_data.cam.cliplength;
343 }
344
345 if (kernel_data.cam.cliplength == FLT_MAX) {
346 film_overwrite_pass_float(buffer + kernel_data.film.pass_denoising_depth, farclip_depth);
347 }
348 else {
349 /* No shading point at the background, so use the ray directly. Transparent surfaces and
350 * volume boundaries keep `bounce == 0` and only advance `tmin`, leaving the ray origin and
351 * direction unchanged, so `ray_P` is still the camera near clip point. */
352 const float3 ray_P = INTEGRATOR_STATE(state, ray, P);
353 const float3 ray_D = INTEGRATOR_STATE(state, ray, D);
354
355 /* Only cover the distance not already written for this path, so the accumulated value stays
356 * within [near, far]. A ray reaching the background directly has not written any depth. */
357 if (wrote_first_segment) {
358 const float3 prev_P = ray_P + ray_D * INTEGRATOR_STATE(state, ray, tmin);
359 farclip_depth -= camera_z_depth(kg, prev_P) - camera_z_depth(kg, ray_P);
360 }
361
362 /* Convert the camera z distance to a ray length if necessary. */
363 const bool follow_reflections = (kernel_data.film.denoising_pass_options_flag &
365 if (follow_reflections && kernel_data.cam.type == CAMERA_PERSPECTIVE) {
366 const Transform cameratoworld = kernel_data.cam.cameratoworld;
367 const float3 camD = make_float3(cameratoworld.x.z, cameratoworld.y.z, cameratoworld.z.z);
368 farclip_depth = farclip_depth / dot(ray_D, camD);
369 }
370
371 farclip_depth = ensure_finite(farclip_depth * average(denoising_feature_throughput));
372 film_write_pass_float(buffer + kernel_data.film.pass_denoising_depth, farclip_depth);
373 }
374 }
375
376 if (kernel_data.film.pass_denoising_backward_motion != PASS_UNUSED &&
377 !(path_flag & PATH_RAY_SINGLE_PASS_DONE))
378 {
380 kg, INTEGRATOR_STATE(state, ray, P), INTEGRATOR_STATE(state, ray, D));
381 const float3 backward_motion = make_float3(speed.x, speed.y, 0.0f);
382 film_write_pass_float3(buffer + kernel_data.film.pass_denoising_backward_motion,
383 backward_motion);
384 }
385
386 /* 'pass_denoising_albedo' is written by 'film_write_emission_or_background_pass' */
387}
388#endif /* __DENOISING_FEATURES__ */
389
#define D
CCL_NAMESPACE_BEGIN ccl_device_inline float bsdf_get_specular_roughness_squared(const ccl_private ShaderClosure *sc)
Definition bsdf.h:29
ccl_device_inline Spectrum closure_albedo(KernelGlobals kg, const ccl_private ShaderData *sd, const ccl_private ShaderClosure *sc, const bool reflection, const bool transmission)
Definition bsdf.h:732
#define CLOSURE_IS_GLASS(type)
#define kernel_data
#define PASS_UNUSED
#define ccl_restrict
#define ccl_device_forceinline
#define CLOSURE_IS_BSDF_TRANSPARENT(type)
#define zero_spectrum
#define CLOSURE_IS_BSDF_GLOSSY(type)
#define ccl_private
#define CLOSURE_IS_BSDF_DIFFUSE(type)
#define ccl_global
#define CLOSURE_IS_BSDF_OR_BSSRDF(type)
#define CLOSURE_IS_BSSRDF(type)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define FLT_MAX
T smoothstep(T, T, T) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
float3 transform_direction(float3x3 mat, float3 direction)
float average(float2 a)
float reduce_max(float2 a)
VecT safe_normalize(VecT vector)
ccl_device_forceinline float4 camera_motion_vector_direction(KernelGlobals kg, const float3 P, const float3 D)
ccl_device_inline float camera_z_depth(KernelGlobals kg, const float3 P)
if(state< num_states)
const int state
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
@ CLOSURE_BSDF_HAIR_HUANG_ID
@ SR_RAY_PORTAL
@ SR_TRANSPARENT
@ PATH_RAY_REFLECT
@ PATH_RAY_SHADOW_CATCHER_PASS
@ PATH_RAY_SINGLE_PASS_DONE
@ PATH_RAY_DENOISING_FEATURES
@ DENOISING_PASS_FOLLOW_REFLECTIONS
@ DENOISING_PASS_USE_ALBEDO_ROUGHNESS_WEIGHTING
@ CAMERA_PERSPECTIVE
ccl_device_inline float ensure_finite(const float v)
Definition math_base.h:370
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define N
closure color scatter(string phase, float Anisotropy, float IOR, float Backscatter, float Alpha, float Diameter)
ccl_device_forceinline float3 primitive_motion_vector_backward_depth_delta(KernelGlobals kg, const ccl_private ShaderData *sd)
Definition primitive.h:294
ccl_device_forceinline float4 primitive_motion_vector_reflection(KernelGlobals kg, const float3 reflector_P, const float3 reflector_N, const ccl_private ShaderData *sd)
Definition primitive.h:340
#define sqrtf
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition state.h:235
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:234
IntegratorStateCPU * IntegratorState
Definition state.h:227
KERNEL_FEATURE_LIGHT_PASSES denoising_feature_throughput
float4 y
Definition transform.h:53
float4 x
Definition transform.h:53
float4 z
Definition transform.h:53
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
ccl_device Spectrum surface_shader_alpha(const ccl_private ShaderData *sd)
float3 Spectrum
ccl_device_inline void film_write_pass_float3(ccl_global float *ccl_restrict buffer, const float3 value)
Definition write.h:68
ccl_device_inline void film_write_pass_spectrum(ccl_global float *ccl_restrict buffer, Spectrum value)
Definition write.h:86
ccl_device_inline void film_write_pass_float(ccl_global float *ccl_restrict buffer, const float value)
Definition write.h:58
CCL_NAMESPACE_BEGIN ccl_device_forceinline ccl_global float * film_pass_pixel_render_buffer(KernelGlobals kg, ConstIntegratorState state, ccl_global float *ccl_restrict render_buffer)
Definition write.h:24
ccl_device_inline void film_overwrite_pass_float(ccl_global float *ccl_restrict buffer, const float value)
Definition write.h:122