Blender V5.3
eevee_gbuffer_write.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
22
25
26namespace gbuffer {
27
38
39/* -------------------------------------------------------------------- */
42
45
46ClosurePacking pack_closure(const PackParameters &srt, ClosureUndetermined cl)
47{
48 ClosurePacking cl_packed;
50
51 if (cl.weight() <= CLOSURE_WEIGHT_CUTOFF) {
52 cl_packed.mode = GBUF_NONE;
53 }
54 /* Common to all configs. */
55 cl_packed.N = cl.N;
56 cl_packed.data0 = gbuffer::closure_color_pack(cl.color);
57 /* Some closures require additional packing. */
58 switch (cl_packed.mode) {
59 case GBUF_REFLECTION:
62 /* Material is colored, but the flag is set to colorless. */
63 assert(false);
64 }
65 else {
67 }
68 }
69 break;
73 }
74 break;
75 case GBUF_REFRACTION:
78 /* Material is colored, but the flag is set to colorless. */
79 assert(false);
80 }
81 else {
83 }
84 }
85 break;
89 }
90 break;
94 /* Material is colored, but the flag is set to colorless. */
95 assert(false);
96 }
97 else {
99 }
100 }
101 break;
105 }
106 break;
107 case GBUF_SUBSURFACE:
110 }
111 break;
112 default:
113 break;
114 }
115 return cl_packed;
116}
117
118/* Data laid-out as stored in the gbuffer. */
119struct Packed {
120 [[capacity(gbuffer_layer_max * 2)]] float4 closure[GBUFFER_LAYER_MAX * 2];
121 [[capacity(gbuffer_layer_max)]] float2 normal[GBUFFER_LAYER_MAX];
123 uint header;
126};
127
128/* ROP writes round to nearest. */
130{
131 constexpr float quantization_step = 1.0f / 1023.0f;
132 return saturate(data + (noise - 0.5f) * quantization_step);
133}
134
139
140/* Image writes to UNORM flush toward zero. */
142{
143 constexpr float quantization_step = 1.0f / 1023.0f;
144 return saturate(data + noise * quantization_step);
145}
146
151
152float3 closure_dither_noise(float2 texel, uint layer_id, float3 offset)
153{
154 float seed = float(layer_id) * 3.0f;
155 return random::interleaved_gradient_3d(texel, float3(seed, seed + 1.0f, seed + 2.0f), offset);
156}
157
159 float2 texel,
160 uint layer_id,
161 float3 offset)
162{
163 return closure_data_dither_round_to_nearest(data, closure_dither_noise(texel, layer_id, offset));
164}
165
167 float2 texel,
168 uint layer_id,
169 float3 offset)
170{
171 return closure_data_dither_flush_to_zero(data, closure_dither_noise(texel, layer_id, offset));
172}
173
174/* Transient data used during packing. */
175struct Packer {
176 /* Packed GBuffer data in layer indexing. */
177 [[capacity(gbuffer_layer_max)]] ClosurePacking closures[GBUFFER_LAYER_MAX];
178 /* Additional info to be stored inside the normal stack. */
180 /* Header containing which closures are encoded and which normals are used. */
182
183 /* Swap closures to avoid gap in data. Closures are then in layer order. */
185 {
186#if 0 /* NOTE: 4 closures mode are not yet supported but might be in the future. */
187 if (srt.gbuffer_layer_max > 3) [[static_branch]] {
188 if (this->closures[2].is_empty()) {
189 this->closures[2] = this->closures[3];
190 this->closures[3].mode = GBUF_NONE;
191 }
192 }
193#endif
194 if (srt.gbuffer_layer_max > 2) [[static_branch]] {
195 if (this->closures[1].is_empty()) {
196 this->closures[1] = this->closures[2];
197 this->closures[2].mode = GBUF_NONE;
198#if 0 /* NOTE: 4 closures mode are not yet supported but might be in the future. */
199 if (srt.gbuffer_layer_max > 3) [[static_branch]] {
200 this->closures[2] = this->closures[3];
201 this->closures[3].mode = GBUF_NONE;
202 }
203#endif
204 }
205 }
206 if (srt.gbuffer_layer_max > 1) [[static_branch]] {
207 if (this->closures[0].is_empty()) {
208 this->closures[0] = this->closures[1];
209 this->closures[1].mode = GBUF_NONE;
210 if (srt.gbuffer_layer_max > 2) [[static_branch]] {
211 this->closures[1] = this->closures[2];
212 this->closures[2].mode = GBUF_NONE;
213#if 0 /* NOTE: 4 closures mode are not yet supported but might be in the future. */
214 if (srt.gbuffer_layer_max > 3) [[static_branch]] {
215 this->closures[2] = this->closures[3];
216 this->closures[3].mode = GBUF_NONE;
217 }
218#endif
219 }
220 }
221 }
222 }
223
224 /* Needs to happen in layer order. */
226 {
227 /* Assume that the header was cleared to 0 and all layers point to the 1st tangent (0 id). */
228 /* Since this function runs in layer ordering (after compaction) each layer (if non-empty) can
229 * rely on the previous one to also be non-empty. */
230 if (srt.gbuffer_layer_max > 1) [[static_branch]] {
231 if (!this->closures[1].is_empty()) {
232 if (!all(equal(this->closures[0].N, this->closures[1].N))) {
233 /* Unique tangent space. */
234 this->header.tangent_space_id_set(1, 1);
235 }
236 else {
237 /* Reuse layer 0 tangent space. */
238 }
239 }
240 }
241 if (srt.gbuffer_layer_max > 2) [[static_branch]] {
242 if (!this->closures[2].is_empty()) {
243 if (!all(equal(this->closures[0].N, this->closures[2].N))) {
244 if (!all(equal(this->closures[1].N, this->closures[2].N))) {
245 /* Unique tangent space. */
246 this->header.tangent_space_id_set(2, 2);
247 }
248 else {
249 /* Reuse layer 1 tangent space. */
250 this->header.tangent_space_id_set(2, 1);
251 }
252 }
253 else {
254 /* Reuse layer 0 tangent space. */
255 }
256 }
257 }
258 }
259
261 {
262 uchar flag = 0;
263 if (this->header.tangent_space_id(1) == 1u) {
264 flag |= NORMAL_DATA_1;
265 }
266 if (this->header.tangent_space_id(2) == 2u) {
267 flag |= NORMAL_DATA_2;
268 }
269 return UsedLayerFlag(flag);
270 }
271
273 {
274 Packed data;
275 /* Note: Normals are not interleaved or packed together.
276 * Even if they are packed, only the first one is required to be written in the gbuffer.
277 * The other ones are optional. This means layer's tangent space are indexed using layer id. */
278 for (int i = 0; i < 3 /* GBUFFER_LAYER_MAX */; i++) [[unroll]] {
279 if (srt.gbuffer_layer_max > i) [[static_branch]] {
280 data.normal[i] = gbuffer::normal_pack(this->closures[i].N);
281 }
282 }
283
284 uint used_layers = this->get_used_normal_layers();
285
286 uint closure_count = this->header.closure_len();
287
288 /* Interleave data to simplify loading code and keep packed storage. */
289 switch (closure_count) {
290 case 1u:
291 data.closure[0] = this->closures[0].data0;
292 data.closure[1] = this->closures[0].data1;
293 break;
294 case 2u:
295 if (srt.gbuffer_layer_max > 1) [[static_branch]] {
296 data.closure[0] = this->closures[0].data0;
297 data.closure[1] = this->closures[1].data0;
299 data.closure[2] = this->closures[0].data1;
300 data.closure[3] = this->closures[1].data1;
301 set_flag_from_test(used_layers, this->closures[0].use_data1(), CLOSURE_DATA_2);
302 set_flag_from_test(used_layers, this->closures[1].use_data1(), CLOSURE_DATA_3);
303 }
304 }
305 break;
306 case 3u:
307 if (srt.gbuffer_layer_max > 2) [[static_branch]] {
308 data.closure[0] = this->closures[0].data0;
309 data.closure[1] = this->closures[1].data0;
310 data.closure[2] = this->closures[2].data0;
311 data.closure[3] = this->closures[0].data1;
312 data.closure[4] = this->closures[1].data1;
313 data.closure[5] = this->closures[2].data1;
314 set_flag_from_test(used_layers, true, CLOSURE_DATA_2);
315 set_flag_from_test(used_layers, this->closures[0].use_data1(), CLOSURE_DATA_3);
316 set_flag_from_test(used_layers, this->closures[1].use_data1(), CLOSURE_DATA_4);
317 set_flag_from_test(used_layers, this->closures[2].use_data1(), CLOSURE_DATA_5);
318 }
319 break;
320 }
321
322 if (this->header.has_additional_data()) {
323 data.additional_info = float2(this->additional_info);
324 set_flag_from_test(used_layers, true, ADDITIONAL_DATA);
325 }
326
327 if (this->header.use_object_id()) {
328 set_flag_from_test(used_layers, true, OBJECT_ID);
329 }
330
331 data.used_layers = UsedLayerFlag(used_layers);
332 data.header = this->header.raw();
333
334 return data;
335 }
336};
337
339 [[capacity(gbuffer_layer_max)]] ClosureUndetermined closure[GBUFFER_LAYER_MAX];
340};
341
350 InputClosures cl_data,
351 float3 Ng,
352 packed_float3 surface_N,
353 Thickness thickness,
354 bool use_object_id)
355{
356 Packer packer;
357 packer.header = Header::zero();
358 packer.header.use_object_id_set(use_object_id);
359
360 for (int i = 0; i < 3 /* GBUFFER_LAYER_MAX */; i++) [[unroll]] {
361 if (srt.gbuffer_layer_max > i) [[static_branch]] {
362 packer.closures[i] = pack_closure(srt, cl_data.closure[i]);
363 }
364 }
365
366 for (int i = 0; i < 3 /* GBUFFER_LAYER_MAX */; i++) [[unroll]] {
367 if (srt.gbuffer_layer_max > i) [[static_branch]] {
368 packer.header.closure_set(uchar(i), packer.closures[i].mode);
369 }
370 }
371
372 if (packer.header.has_additional_data()) {
374 }
375
376 /* ---- Switch from Bin to Layer order. ---- */
377 packer.closures_to_layer_order(srt);
378
379 /* This is correct in layer order. */
380 bool has_any_closure = packer.closures[0].mode != GBUF_NONE;
381 if (!has_any_closure) {
382 /* Output dummy closure in the case of unlit materials for correct render passes data. */
383 packer.closures[0] = ClosurePacking::fallback(surface_N);
384 packer.header.closure_set(0, packer.closures[0].mode);
385 }
386
387 packer.reuse_tangent_spaces(srt);
388
389 /* Needs to happen in layer order and after normal fallback. */
390 packer.header.geometry_normal_set(Ng, packer.closures[0].N);
391
392 return packer.result_get(srt);
393}
394
396
397} // namespace gbuffer
#define CLOSURE_WEIGHT_CUTOFF
#define assert(assertion)
#define capacity(const)
#define compilation_constant
#define static_branch
#define unroll
bool all(VecOp< bool, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
unsigned int uchar
float noise(float p)
void set_flag_from_test(uint &value, bool test, uint flag)
#define N
float3 closure_data_dither_round_to_nearest(float3 data, float3 noise)
float3 closure_data_dither_flush_to_zero(float3 data, float3 noise)
float4 closure_data_layer_dither_flush_to_zero(float4 data, float2 texel, uint layer_id, float3 offset)
ClosurePacking pack_closure(const PackParameters &srt, ClosureUndetermined cl)
float3 closure_dither_noise(float2 texel, uint layer_id, float3 offset)
bool color_is_grayscale(float3 color)
float4 closure_data_layer_dither_round_to_nearest(float4 data, float2 texel, uint layer_id, float3 offset)
float2 normal_pack(float3 N)
float4 closure_color_pack(float3 color)
GBufferMode closure_type_to_mode(ClosureType type, bool is_grayscale)
Packed pack(const PackParameters &srt, InputClosures cl_data, float3 Ng, packed_float3 surface_N, Thickness thickness, bool use_object_id)
float3 interleaved_gradient_3d(float2 pixel, float3 seed, float3 offset)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
float x
static float2 pack(Thickness thickness)
static ClosurePacking fallback(float3 surface_N)
void tangent_space_id_set(uint layer_id, uint normal_id)
void use_object_id_set(bool value)
void geometry_normal_set(float3 Ng, float3 N)
void closure_set(uint bin, GBufferMode mode)
uchar tangent_space_id(uint layer_id) const
ClosureUndetermined closure[GBUFFER_LAYER_MAX]
float2 normal[GBUFFER_LAYER_MAX]
float4 closure[GBUFFER_LAYER_MAX *2]
UsedLayerFlag get_used_normal_layers()
ClosurePacking closures[GBUFFER_LAYER_MAX]
Packed result_get(const PackParameters &srt)
void closures_to_layer_order(const PackParameters &srt)
void reuse_tangent_spaces(const PackParameters &srt)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)