Blender V5.0
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 ccl_private ShaderData *sd,
31{
32#ifdef __PASSES__
33 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
34
35 if (!(path_flag & PATH_RAY_TRANSPARENT_BACKGROUND)) {
36 return;
37 }
38
39 /* Don't write data passes for paths that were split off for shadow catchers
40 * to avoid double-counting. */
41 if (path_flag & PATH_RAY_SHADOW_CATCHER_PASS) {
42 return;
43 }
44
45 const int flag = kernel_data.film.pass_flag;
46
47 if (!(flag & PASS_ANY)) {
48 return;
49 }
50
52
53 if (!(path_flag & PATH_RAY_SINGLE_PASS_DONE)) {
54 if (INTEGRATOR_STATE(state, path, sample) == 0) {
55 if (flag & PASSMASK(DEPTH)) {
56 const float depth = camera_z_depth(kg, sd->P);
57 film_overwrite_pass_float(buffer + kernel_data.film.pass_depth, depth);
58 }
59 if (flag & PASSMASK(OBJECT_ID)) {
60 const float id = object_pass_id(kg, sd->object);
61 film_overwrite_pass_float(buffer + kernel_data.film.pass_object_id, id);
62 }
63 if (flag & PASSMASK(MATERIAL_ID)) {
64 const float id = shader_pass_id(kg, sd);
65 film_overwrite_pass_float(buffer + kernel_data.film.pass_material_id, id);
66 }
67 if (flag & PASSMASK(POSITION)) {
68 const float3 position = sd->P;
69 film_overwrite_pass_float3(buffer + kernel_data.film.pass_position, position);
70 }
71 }
72
73 if (!(sd->flag & (SD_TRANSPARENT | SD_RAY_PORTAL)) ||
74 kernel_data.film.pass_alpha_threshold == 0.0f ||
75 average(surface_shader_alpha(sd)) >= kernel_data.film.pass_alpha_threshold)
76 {
77 if (flag & PASSMASK(NORMAL)) {
78 const float3 normal = surface_shader_average_normal(sd);
79 film_write_pass_float3(buffer + kernel_data.film.pass_normal, normal);
80 }
81 if (flag & PASSMASK(ROUGHNESS)) {
82 const float roughness = surface_shader_average_roughness(sd);
83 film_write_pass_float(buffer + kernel_data.film.pass_roughness, roughness);
84 }
85 if (flag & PASSMASK(UV)) {
86 const float3 uv = primitive_uv(kg, sd);
87 film_write_pass_float3(buffer + kernel_data.film.pass_uv, uv);
88 }
89 if (flag & PASSMASK(MOTION)) {
90 const float4 speed = primitive_motion_vector(kg, sd);
91 film_write_pass_float4(buffer + kernel_data.film.pass_motion, speed);
92 film_write_pass_float(buffer + kernel_data.film.pass_motion_weight, 1.0f);
93 }
94
96 }
97 }
98
99 if (kernel_data.film.cryptomatte_passes) {
100 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
101 const float matte_weight = average(throughput) *
103 if (matte_weight > 0.0f) {
104 ccl_global float *cryptomatte_buffer = buffer + kernel_data.film.pass_cryptomatte;
105 if (kernel_data.film.cryptomatte_passes & CRYPT_OBJECT) {
106 const float id = object_cryptomatte_id(kg, sd->object);
107 cryptomatte_buffer += film_write_cryptomatte_pass(
108 cryptomatte_buffer, kernel_data.film.cryptomatte_depth, id, matte_weight);
109 }
110 if (kernel_data.film.cryptomatte_passes & CRYPT_MATERIAL) {
111 const float id = kernel_data_fetch(shaders, (sd->shader & SHADER_MASK)).cryptomatte_id;
112 cryptomatte_buffer += film_write_cryptomatte_pass(
113 cryptomatte_buffer, kernel_data.film.cryptomatte_depth, id, matte_weight);
114 }
115 if (kernel_data.film.cryptomatte_passes & CRYPT_ASSET) {
116 const float id = object_cryptomatte_asset_id(kg, sd->object);
117 cryptomatte_buffer += film_write_cryptomatte_pass(
118 cryptomatte_buffer, kernel_data.film.cryptomatte_depth, id, matte_weight);
119 }
120 }
121 }
122
123 if (flag & PASSMASK(DIFFUSE_COLOR)) {
124 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
125 film_write_pass_spectrum(buffer + kernel_data.film.pass_diffuse_color,
126 surface_shader_diffuse(kg, sd) * throughput);
127 }
128 if (flag & PASSMASK(GLOSSY_COLOR)) {
129 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
130 film_write_pass_spectrum(buffer + kernel_data.film.pass_glossy_color,
131 surface_shader_glossy(kg, sd) * throughput);
132 }
133 if (flag & PASSMASK(TRANSMISSION_COLOR)) {
134 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
135 film_write_pass_spectrum(buffer + kernel_data.film.pass_transmission_color,
136 surface_shader_transmission(kg, sd) * throughput);
137 }
138 if (flag & PASSMASK(MIST)) {
139 /* Bring depth into 0..1 range. */
140 const float mist_start = kernel_data.film.mist_start;
141 const float mist_inv_depth = kernel_data.film.mist_inv_depth;
142
143 const float depth = camera_distance(kg, sd->P);
144 float mist = saturatef((depth - mist_start) * mist_inv_depth);
145
146 /* Falloff */
147 const float mist_falloff = kernel_data.film.mist_falloff;
148
149 if (mist_falloff == 1.0f) {
150 ;
151 }
152 else if (mist_falloff == 2.0f) {
153 mist = mist * mist;
154 }
155 else if (mist_falloff == 0.5f) {
156 mist = sqrtf(mist);
157 }
158 else {
159 mist = powf(mist, mist_falloff);
160 }
161
162 /* Modulate by transparency */
163 const Spectrum throughput = INTEGRATOR_STATE(state, path, throughput);
164 const Spectrum alpha = surface_shader_alpha(sd);
165 const float mist_output = (1.0f - mist) * average(throughput * alpha);
166
167 /* Note that the final value in the render buffer we want is 1 - mist_output,
168 * to avoid having to tracking this in the Integrator state we do the negation
169 * after rendering. */
170 film_write_pass_float(buffer + kernel_data.film.pass_mist, mist_output);
171 }
172#endif
173}
174
177{
178#ifdef __PASSES__
179 const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
180
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_background(KernelGlobals kg, IntegratorState state, ccl_global float *ccl_restrict render_buffer)
ccl_device_inline void film_write_data_passes(KernelGlobals kg, IntegratorState state, const ccl_private ShaderData *sd, ccl_global float *ccl_restrict render_buffer)
Definition data_passes.h:27
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 ccl_restrict
#define kernel_data_fetch(name, index)
#define ccl_private
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define PASSMASK(pass)
#define ccl_global
#define PASS_ANY
#define powf(x, y)
#define CCL_NAMESPACE_END
#define saturatef(x)
ccl_device_inline float camera_z_depth(KernelGlobals kg, const float3 P)
ccl_device_inline float camera_distance(KernelGlobals kg, const float3 P)
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)
@ SD_RAY_PORTAL
@ SD_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
static ulong state[N]
float average(point a)
Definition node_math.h:144
ccl_device_forceinline float3 primitive_uv(KernelGlobals kg, const ccl_private ShaderData *sd)
Definition primitive.h:87
ccl_device_forceinline float4 primitive_motion_vector(KernelGlobals kg, const ccl_private ShaderData *sd)
Definition primitive.h:156
#define sqrtf
#define INTEGRATOR_STATE_WRITE(state, nested_struct, member)
Definition state.h:236
#define INTEGRATOR_STATE(state, nested_struct, member)
Definition state.h:235
IntegratorStateCPU * IntegratorState
Definition state.h:228
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
uint8_t flag
Definition wm_window.cc:145
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