Blender V5.3
data_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
10
12
14#include "kernel/film/write.h"
15
17
19 const size_t depth,
20 const float id,
21 const float matte_weight)
22{
23 film_write_cryptomatte_slots(buffer, depth * 2, id, matte_weight);
24 return depth * 4;
25}
26
29 const uint32_t path_flag,
30 const ccl_private ShaderData *sd,
32{
33#ifdef __PASSES__
34 if (!(path_flag & PATH_RAY_TRANSPARENT_BACKGROUND)) {
35 return;
36 }
37
38 /* Don't write data passes for paths that were split off for shadow catchers
39 * to avoid double-counting. */
40 if (path_flag & PATH_RAY_SHADOW_CATCHER_PASS) {
41 return;
42 }
43
44 const int flag = kernel_data.film.pass_flag;
45
46 if (!(flag & PASS_ANY) && kernel_data.film.pass_denoising_backward_motion == PASS_UNUSED) {
47 return;
48 }
49
51
52 if (!(path_flag & PATH_RAY_SINGLE_PASS_DONE)) {
53 if (INTEGRATOR_STATE(state, path, sample) == 0) {
54 if (flag & PASSMASK(DEPTH)) {
55 const float depth = camera_z_depth(kg, sd->P);
56 film_overwrite_pass_float(buffer + kernel_data.film.pass_depth, depth);
57 }
58 if (flag & PASSMASK(OBJECT_ID)) {
59 const float id = object_pass_id(kg, sd->object);
60 film_overwrite_pass_float(buffer + kernel_data.film.pass_object_id, id);
61 }
62 if (flag & PASSMASK(MATERIAL_ID)) {
63 const float id = shader_pass_id(kg, sd);
64 film_overwrite_pass_float(buffer + kernel_data.film.pass_material_id, id);
65 }
66 if (flag & PASSMASK(POSITION)) {
67 const float3 position = sd->P;
68 film_overwrite_pass_float3(buffer + kernel_data.film.pass_position, position);
69 }
70 }
71
72 if (!(sd->runtime_flag & (SR_TRANSPARENT | SR_RAY_PORTAL)) ||
73 kernel_data.film.pass_alpha_threshold == 0.0f ||
74 average(surface_shader_alpha(sd)) >= kernel_data.film.pass_alpha_threshold)
75 {
76 if (flag & PASSMASK(NORMAL)) {
77 const float3 normal = surface_shader_average_normal(sd);
78 film_write_pass_float3(buffer + kernel_data.film.pass_normal, normal);
79 }
80 if (flag & PASSMASK(ROUGHNESS)) {
81 const float roughness = surface_shader_average_roughness(sd);
82 film_write_pass_float(buffer + kernel_data.film.pass_roughness, roughness);
83 }
84 if (flag & PASSMASK(UV)) {
85 const float3 uv = primitive_uv(kg, sd);
86 film_write_pass_float3(buffer + kernel_data.film.pass_uv, uv);
87 }
88 if (flag & PASSMASK(MOTION)) {
89 const float4 speed = primitive_motion_vector(kg, sd);
90 film_write_pass_float4(buffer + kernel_data.film.pass_motion, speed);
91 film_write_pass_float(buffer + kernel_data.film.pass_motion_weight, 1.0f);
92 }
93
95 }
96 }
97
98 if (kernel_data.film.cryptomatte_passes) {
99 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
100 const float matte_weight = average(throughput) *
102 if (matte_weight > 0.0f) {
103 ccl_global float *cryptomatte_buffer = buffer + kernel_data.film.pass_cryptomatte;
104 if (kernel_data.film.cryptomatte_passes & CRYPT_OBJECT) {
105 const float id = object_cryptomatte_id(kg, sd->object);
106 cryptomatte_buffer += film_write_cryptomatte_pass(
107 cryptomatte_buffer, kernel_data.film.cryptomatte_depth, id, matte_weight);
108 }
109 if (kernel_data.film.cryptomatte_passes & CRYPT_MATERIAL) {
110 const float id = kernel_data_fetch(shaders, (sd->shader & SHADER_MASK)).cryptomatte_id;
111 cryptomatte_buffer += film_write_cryptomatte_pass(
112 cryptomatte_buffer, kernel_data.film.cryptomatte_depth, id, matte_weight);
113 }
114 if (kernel_data.film.cryptomatte_passes & CRYPT_ASSET) {
115 const float id = object_cryptomatte_asset_id(kg, sd->object);
116 cryptomatte_buffer += film_write_cryptomatte_pass(
117 cryptomatte_buffer, kernel_data.film.cryptomatte_depth, id, matte_weight);
118 }
119 }
120 }
121
122 if (flag & PASSMASK(DIFFUSE_COLOR)) {
123 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
124 film_write_pass_spectrum(buffer + kernel_data.film.pass_diffuse_color,
125 surface_shader_diffuse(kg, sd) * throughput);
126 }
127 if (flag & PASSMASK(GLOSSY_COLOR)) {
128 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
129 film_write_pass_spectrum(buffer + kernel_data.film.pass_glossy_color,
130 surface_shader_glossy(kg, sd) * throughput);
131 }
132 if (flag & PASSMASK(TRANSMISSION_COLOR)) {
133 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
134 film_write_pass_spectrum(buffer + kernel_data.film.pass_transmission_color,
135 surface_shader_transmission(kg, sd) * throughput);
136 }
137 if (flag & PASSMASK(MIST)) {
138 /* Bring depth into 0..1 range. */
139 const float mist_start = kernel_data.film.mist_start;
140 const float mist_inv_depth = kernel_data.film.mist_inv_depth;
141
142 const float depth = camera_distance(kg, sd->P);
143 float mist = saturatef((depth - mist_start) * mist_inv_depth);
144
145 /* Falloff */
146 const float mist_falloff = kernel_data.film.mist_falloff;
147
148 if (mist_falloff == 1.0f) {
149 ;
150 }
151 else if (mist_falloff == 2.0f) {
152 mist = mist * mist;
153 }
154 else if (mist_falloff == 0.5f) {
155 mist = sqrtf(mist);
156 }
157 else {
158 mist = powf(mist, mist_falloff);
159 }
160
161 /* Modulate by transparency */
162 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
163 const Spectrum alpha = surface_shader_alpha(sd);
164 const float mist_output = (1.0f - mist) * average(throughput * alpha);
165
166 /* Note that the final value in the render buffer we want is 1 - mist_output,
167 * to avoid having to tracking this in the Integrator state we do the negation
168 * after rendering. */
169 film_write_pass_float(buffer + kernel_data.film.pass_mist, mist_output);
170 }
171#endif
172}
173
176 const uint32_t path_flag,
179{
180#ifdef __PASSES__
181 if (!(path_flag & PATH_RAY_TRANSPARENT_BACKGROUND)) {
182 return;
183 }
184
185 /* Don't write data passes for paths that were split off for shadow catchers
186 * to avoid double-counting. */
187 if (path_flag & PATH_RAY_SHADOW_CATCHER_PASS) {
188 return;
189 }
190
191 const int flag = kernel_data.film.pass_flag;
192
193 if (!(flag & PASS_ANY)) {
194 return;
195 }
196
197 if (!(path_flag & PATH_RAY_SINGLE_PASS_DONE)) {
199
200 if (INTEGRATOR_STATE(state, path, sample) == 0) {
201 if (flag & PASSMASK(DEPTH)) {
202 film_overwrite_pass_float(buffer + kernel_data.film.pass_depth, 0.0f);
203 }
204 if (flag & PASSMASK(OBJECT_ID)) {
205 film_overwrite_pass_float(buffer + kernel_data.film.pass_object_id, 0.0f);
206 }
207 if (flag & PASSMASK(MATERIAL_ID)) {
208 film_overwrite_pass_float(buffer + kernel_data.film.pass_material_id, 0.0f);
209 }
210 if (flag & PASSMASK(POSITION)) {
211 film_overwrite_pass_float3(buffer + kernel_data.film.pass_position, zero_float3());
212 }
213 }
214 }
215#endif
216}
217
ccl_device_inline void film_write_cryptomatte_slots(ccl_global float *buffer, const int num_slots, const float id, const float weight)
ccl_device_inline void film_write_data_passes(KernelGlobals kg, IntegratorState state, const uint32_t path_flag, const ccl_private ShaderData *sd, ccl_global float *ccl_restrict render_buffer)
Definition data_passes.h:27
ccl_device_inline void film_write_data_passes_background(KernelGlobals kg, IntegratorState state, const uint32_t path_flag, ccl_global float *ccl_restrict render_buffer)
CCL_NAMESPACE_BEGIN ccl_device_inline size_t film_write_cryptomatte_pass(ccl_global float *ccl_restrict buffer, const size_t depth, const float id, const float matte_weight)
Definition data_passes.h:18
#define kernel_data
#define PASS_UNUSED
#define ccl_restrict
#define kernel_data_fetch(name, index)
#define ccl_private
#define ccl_device_inline
#define PASSMASK(pass)
#define ccl_global
#define PASS_ANY
#define powf(x, y)
#define CCL_NAMESPACE_END
#define saturatef(x)
#define position
float average(float2 a)
ccl_device_inline float camera_z_depth(KernelGlobals kg, const float3 P)
ccl_device_inline float camera_distance(KernelGlobals kg, const float3 P)
const int state
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
ccl_device int shader_pass_id(KernelGlobals kg, const ccl_private ShaderData *sd)
ccl_device_inline float object_cryptomatte_id(KernelGlobals kg, const int object)
ccl_device_inline float object_cryptomatte_asset_id(KernelGlobals kg, const int object)
ccl_device_inline float object_pass_id(KernelGlobals kg, const int object)
@ SR_RAY_PORTAL
@ SR_TRANSPARENT
@ PATH_RAY_SHADOW_CATCHER_PASS
@ PATH_RAY_SINGLE_PASS_DONE
@ PATH_RAY_TRANSPARENT_BACKGROUND
@ CRYPT_ASSET
@ CRYPT_OBJECT
@ CRYPT_MATERIAL
@ SHADER_MASK
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
ccl_device_forceinline float3 primitive_uv(KernelGlobals kg, const ccl_private ShaderData *sd)
Definition primitive.h:111
ccl_device_forceinline float4 primitive_motion_vector(KernelGlobals kg, const ccl_private ShaderData *sd)
Definition primitive.h:282
#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
ccl_device float surface_shader_average_roughness(const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_transmission(KernelGlobals kg, const ccl_private ShaderData *sd)
ccl_device float3 surface_shader_average_normal(const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_transparency(const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_glossy(KernelGlobals kg, const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_alpha(const ccl_private ShaderData *sd)
ccl_device Spectrum surface_shader_diffuse(KernelGlobals kg, const ccl_private ShaderData *sd)
float3 Spectrum
ccl_device_inline void film_write_pass_float4(ccl_global float *ccl_restrict buffer, const float4 value)
Definition write.h:92
ccl_device_inline void film_overwrite_pass_float3(ccl_global float *ccl_restrict buffer, const float3 value)
Definition write.h:128
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