Blender V5.3
eevee_gbuffer_types.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
23#include "eevee_defines.hh"
24
26#include "gpu_shader_codegen_lib.glsl"
30
32 /* Data 0 is always used. */
33 /* Data 1 is always used. */
34 CLOSURE_DATA_2 = 1u << 0u,
35 CLOSURE_DATA_3 = 1u << 1u,
36 CLOSURE_DATA_4 = 1u << 2u,
37 CLOSURE_DATA_5 = 1u << 3u,
38
39 /* Normal 0 is always used. */
40 NORMAL_DATA_1 = 1u << 4u,
41 NORMAL_DATA_2 = 1u << 5u,
42
43 ADDITIONAL_DATA = 1u << 6u,
44
45 OBJECT_ID = 1u << 7u,
46};
47
48static constexpr int GBUFFER_LAYER_MAX = 3;
49
50namespace gbuffer {
51
52/* -------------------------------------------------------------------- */
56
87
89{
90 switch (type) {
92 return GBUF_DIFFUSE;
94 return GBUF_TRANSLUCENT;
95 case CLOSURE_BSDF_MICROFACET_GGX_REFLECTION_ID:
96 return is_grayscale ? GBUF_REFLECTION_COLORLESS : GBUF_REFLECTION;
98 return is_grayscale ? GBUF_REFRACTION_COLORLESS : GBUF_REFRACTION;
102 return GBUF_SUBSURFACE;
103 default:
104 return GBUF_NONE;
105 }
106}
107
109{
110 switch (mode) {
111 case GBUF_DIFFUSE:
113 case GBUF_TRANSLUCENT:
115 case GBUF_SUBSURFACE:
118 case GBUF_REFLECTION:
119 return ClosureType(CLOSURE_BSDF_MICROFACET_GGX_REFLECTION_ID);
121 case GBUF_REFRACTION:
126 default:
128 }
129}
130
132{
133 /* This tests is R == G == B. */
134 return all(equal(color.rgb, color.gbr));
135}
136
137bool closure_is_empty(ClosureUndetermined cl)
138{
139 return cl.weight() <= CLOSURE_WEIGHT_CUTOFF || cl.type == CLOSURE_NONE_ID;
140}
141
143
144/* -------------------------------------------------------------------- */
148
150{
151 N /= length_manhattan(N);
152 float2 _sign = sign(N.xy);
153 _sign.x = _sign.x == 0.0f ? 1.0f : _sign.x;
154 _sign.y = _sign.y == 0.0f ? 1.0f : _sign.y;
155 N.xy = (N.z >= 0.0f) ? N.xy : ((1.0f - abs(N.yx)) * _sign);
156 N.xy = N.xy * 0.5f + 0.5f;
157 return N.xy;
158}
160{
161 N_packed = N_packed * 2.0f - 1.0f;
162 float3 N = float3(N_packed.x, N_packed.y, 1.0f - abs(N_packed.x) - abs(N_packed.y));
163 float t = clamp(-N.z, 0.0f, 1.0f);
164 N.x += (N.x >= 0.0f) ? -t : t;
165 N.y += (N.y >= 0.0f) ? -t : t;
166 return normalize(N);
167}
168
169/* Keep IOR 1.0 stable across 10-bit quantization. */
170static constexpr float closure_10bit_unit = (1.0f / 1023.0f);
171static constexpr float ior_packed_lt_1_scale = (511.0f * closure_10bit_unit);
172static constexpr float ior_packed_eq_1 = (512.0f * closure_10bit_unit);
173static constexpr float ior_packed_gt_1_bias = (510.0f * closure_10bit_unit);
174
175float ior_pack(float ior)
176{
177 if (ior == 1.0f) {
178 return ior_packed_eq_1;
179 }
180 return (ior > 1.0f) ? (1.0f - ior_packed_gt_1_bias / ior) : (ior_packed_lt_1_scale * ior);
181}
182float ior_unpack(float ior_packed)
183{
184 if (ior_packed == ior_packed_eq_1) {
185 return 1.0f;
186 }
187 return (ior_packed > ior_packed_eq_1) ? (ior_packed_gt_1_bias / (1.0f - ior_packed)) :
188 (ior_packed / ior_packed_lt_1_scale);
189}
190
191float thickness_pack(Thickness thickness)
192{
193 /* TODO(fclem): If needed, we could increase precision by defining a ceiling value like the view
194 * distance and remap to it. Or tweak the hyperbole equality. */
195 /* NOTE: Sign encodes the thickness mode. */
196 /* Remap [0..+inf) to [0..1/2]. */
197 float thickness_packed = thickness.value() / (1.0f + 2.0f * thickness.value());
198 /* Mirror the negative from [0..1/2] to [1..1/2]. O is mapped to 0 for precision. */
199 return thickness.mode() == ThicknessMode::Slab ? 1.0f - thickness_packed : thickness_packed;
200}
201
202Thickness thickness_unpack(float thickness_packed)
203{
204 /* Undo mirroring. */
205 float thickness = (thickness_packed > 0.5f) ? 1.0f - thickness_packed : thickness_packed;
206 /* Remap [0..1/2] to [0..+inf). */
207 thickness = thickness / (1.0f - 2.0f * thickness);
208 /* Retrieve mode. */
209 return (thickness_packed > 0.5f) ? Thickness::from(thickness, ThicknessMode::Slab) :
211}
212
220{
221 float max_comp = max(color.x, max(color.y, color.z));
222 float exponent = (max_comp > 1) ? ((max_comp > 2) ? ((max_comp > 4) ? 3.0f : 2.0f) : 1.0f) :
223 0.0f;
224 /* TODO(fclem): Could try dithering to avoid banding artifacts on higher exponents. */
225 return float4(color / exp2(exponent), exponent / 3.0f);
226}
228{
229 float exponent = color_packed.a * 3.0f;
230 return color_packed.rgb * exp2(exponent);
231}
232
234{
235 /* TODO(fclem): Something better. */
236 return closure_color_pack(
237 float3(ior_pack(sss_radii.x), ior_pack(sss_radii.y), ior_pack(sss_radii.z)));
238}
240{
241 /* TODO(fclem): Something better. */
242 float3 radii_packed = closure_color_unpack(sss_radii_packed);
243 return float3(
244 ior_unpack(radii_packed.x), ior_unpack(radii_packed.y), ior_unpack(radii_packed.z));
245}
246
247float object_id_f16_pack(uint object_id)
248{
249 /* TODO(fclem): Make use of all the 16 bits in a half float.
250 * This here only correctly represent values up to 1024. */
251 return float(object_id);
252}
253
254uint object_id_f16_unpack(float object_id_packed)
255{
256 return uint(object_id_packed);
257}
258
259/* Quantize geometric normal to 6 bits. */
261{
262 /* This is a threshold that minimizes the error over the sphere. */
263 constexpr float quantization_multiplier = 1.360f;
264 /* Normalize for comparison. */
265 float3 Ng_quantize = normalize(round(quantization_multiplier * Ng));
266 /* Note: Comparing the error using cosines. The greater the cosine value, the lower the error. */
267 if (dot(N, Ng) > dot(Ng, Ng_quantize)) {
268 /* If the error between the default shading normal and the geometric normal is smaller than the
269 * compression error, we do not encode the geometric normal and use the shading normal for
270 * biasing the shadow rays. This avoid precision issues that comes with the quantization. */
271 return 0;
272 }
273 uint data;
274 data = (Ng_quantize.x > 0.0f) ? (1u << 0u) : 0u;
275 data |= (Ng_quantize.y > 0.0f) ? (1u << 1u) : 0u;
276 data |= (Ng_quantize.z > 0.0f) ? (1u << 2u) : 0u;
277 data |= (Ng_quantize.x < 0.0f) ? (1u << 3u) : 0u;
278 data |= (Ng_quantize.y < 0.0f) ? (1u << 4u) : 0u;
279 data |= (Ng_quantize.z < 0.0f) ? (1u << 5u) : 0u;
280 return data << 20u;
281}
282
284{
285 /* If data is 0 it means the shading normal is representative enough. */
286 if ((data & (63u << 20u)) == 0u) {
287 return N;
288 }
289 float3 Ng = float3((uint3(data) >> (uint3(0, 1, 2) + 20u)) & 1u) -
290 float3((uint3(data) >> (uint3(3, 4, 5) + 20u)) & 1u);
291 return normalize(Ng);
292}
293
295
296/* -------------------------------------------------------------------- */
300
305struct Header {
306 static constexpr uint GBUFFER_NORMAL_BITS_SHIFT = 12u;
307 static constexpr uint GBUFFER_GEOMETRIC_NORMAL_BITS_SHIFT = 20u;
308 static constexpr int GBUFFER_HEADER_BITS_PER_BIN = 4;
309
310 private:
328 uint header_;
329
330 public:
331 static Header from_data(uint data)
332 {
333 Header header;
334 header.header_ = data;
335 return header;
336 }
337
338 static Header zero()
339 {
340 return Header::from_data(0u);
341 }
342
343 bool is_empty() const
344 {
345 return this->header_ == 0;
346 }
347
348 void closure_set(uint bin, GBufferMode mode)
349 {
350 this->header_ |= (mode << (GBUFFER_HEADER_BITS_PER_BIN * bin));
351 }
352 GBufferMode closure(uint bin) const
353 {
354 return GBufferMode((this->header_ >> (GBUFFER_HEADER_BITS_PER_BIN * bin)) & 15u);
355 }
356
357 /* If this flag is set, the header texture has a second layer containing the ObjectIDs. */
358 bool use_object_id() const
359 {
360 return flag_test(this->header_, 1u << 31u);
361 }
362 void use_object_id_set(bool value)
363 {
364 set_flag_from_test(this->header_, value, 1u << 31u);
365 }
366
372 void tangent_space_id_set(uint layer_id, uint normal_id)
373 {
374 /* Layer 0 will always have normal id 0. It doesn't have to be encoded. Skip it. */
375 assert(layer_id > 0u);
376 /* Note: Keep this in the if statement as it compiles faster somehow. */
377 /* -2 is to skip the layer_id 0 and start encoding for layer_id 1. This keeps the FMA. */
378 this->header_ |= normal_id << ((GBUFFER_NORMAL_BITS_SHIFT - 2u) + layer_id * 2u);
379 }
380 uchar tangent_space_id(uint layer_id) const
381 {
382 /* Layer 0 will always have normal id 0. */
383 if (layer_id == 0u) {
384 return 0u;
385 }
386 /* -2 is to skip the layer_id 0 and start encoding for layer_id 1. This keeps the FMA. */
387 return uchar(3u & (this->header_ >> ((GBUFFER_NORMAL_BITS_SHIFT - 2u) + layer_id * 2u)));
388 }
389
390 /* Pack geometric normal into the header if needed. */
392 {
393 this->header_ |= geometry_normal_pack(Ng, N);
394 }
396 {
397 return geometry_normal_unpack(this->header_, surface_N);
398 }
399
400 /* Return a vector of GBufferMode. */
402 {
403 /* NOTE: Need to be adjusted for different global GBUFFER_LAYER_MAX. */
404 constexpr uint bits_per_bin = uint(GBUFFER_HEADER_BITS_PER_BIN);
405 uint3 types = (uint3(this->header_) >> (uint3(0u, 1u, 2u) * bits_per_bin)) &
406 ((1u << bits_per_bin) - 1);
407 return types;
408 }
409
411 {
412 constexpr uint bits_per_bin = uint(GBUFFER_HEADER_BITS_PER_BIN);
413 return GBufferMode((this->header_ >> (bin * bits_per_bin)) & ((1u << bits_per_bin) - 1));
414 }
415
416 /* Return a vector of GBufferMode.
417 * Same as bin_types() but skip empty bins. */
419 {
420 uint3 modes = this->bin_types();
421 if (modes.y == GBUF_NONE) {
422 modes = modes.xzy;
423 }
424 if (modes.x == GBUF_NONE) {
425 modes = modes.yzx;
426 }
427 return modes;
428 }
429
431 {
432 uint3 modes = this->bin_types();
433 uint3 bins = uint3(0, 1, 2);
434 if (modes.y == GBUF_NONE) {
435 bins = bins.xzy;
436 }
437 if (modes.x == GBUF_NONE) {
438 bins = bins.yzx;
439 }
440 return bins;
441 }
442
443 /* Return which closures are empty (equal to GBUF_NONE). */
445 {
446 return equal(this->bin_types(), uint3(GBUF_NONE));
447 }
448
450 {
451 return uchar(reduce_add(int3(not(this->empty_bins()))));
452 }
453
455 {
456 if (this->header_ == 0u) {
457 return 0;
458 }
459 /* Count implicit first layer. */
460 uchar count = 1u;
461 count += uchar(((this->header_ >> 12u) & 3u) != 0);
462 count += uchar(((this->header_ >> 14u) & 3u) != 0);
463 return count;
464 }
465
466 uint raw() const
467 {
468 return this->header_;
469 }
470
471 bool has_transmission() const
472 {
473 /* NOTE: Need to be adjusted for different global GBUFFER_LAYER_MAX. */
474 constexpr uint bits_per_layer = uint(GBUFFER_HEADER_BITS_PER_BIN);
475 /* TODO(@fclem): Compiler should allow uchar to uint cast in constexpr. */
476 /*constexpr*/ uint header_mask = (uint(GBUF_TRANSMISSION_BIT) << (bits_per_layer * 0)) |
477 (uint(GBUF_TRANSMISSION_BIT) << (bits_per_layer * 1)) |
478 (uint(GBUF_TRANSMISSION_BIT) << (bits_per_layer * 2));
479 return (this->header_ & header_mask) != 0;
480 }
481
483 {
484 /* For now, this is true. Only the transmission closures use the thickness data. */
485 return this->has_transmission();
486 }
487
488 /* For a given bin index, return the associated layer index.
489 * Result is undefined if bin has no valid closure. */
491 {
492 uint3 types = this->bin_types();
493 switch (bin_id) {
494 case 2u:
495 return uchar(types[0] != GBUF_NONE) + uchar(types[1] != GBUF_NONE);
496 case 1u:
497 return uchar(types[0] != GBUF_NONE);
498 }
499 /* Default, bin_id == 0 case. */
500 return 0u;
501 }
502};
503
504/* Added data inside the Tangent Space layers. */
507
509 {
510 return float2(thickness_pack(thickness), 0.0f /* UNUSED */);
511 }
512
514 {
515 return AdditionalInfo{thickness_unpack(data.x)};
516 }
517};
518
519/* Almost 1:1 match with ClosureUndetermined but has packed data. */
521 /* Packed data layer 0. Always used. */
523 /* Packed data layer 1. Might not be used. */
525 /* Unpacked normal. Normal packing happens later. */
527 /* Gbuffer packing mode. */
529
531 {
533 cl.mode = GBUF_UNLIT;
534 cl.data0 = float4(0);
535 cl.data1 = float4(0);
536 cl.N = surface_N;
537 return cl;
538 }
539
540 bool use_data1() const
541 {
542 return this->mode == GBUF_REFLECTION || this->mode == GBUF_REFRACTION ||
543 this->mode == GBUF_THIN_REFRACTION || this->mode == GBUF_SUBSURFACE;
544 }
545
546 bool is_empty() const
547 {
548 return this->mode == GBUF_NONE;
549 }
550};
551
553
554} // namespace gbuffer
555
556namespace gbuffer {
557
559
560/* -------------------------------------------------------------------- */
564
566 static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
567 {
568 cl_packed.data1 = gbuffer::sss_radii_pack(cl.data.xyz);
569 }
570
571 static void unpack_additional(ClosureUndetermined &cl, float4 data1)
572 {
573 cl.data.rgb = gbuffer::sss_radii_unpack(data1);
574 }
575};
576
578 static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
579 {
580 cl_packed.data1 = float4(cl.data.x, 0.0f, 0.0f, 0.0f);
581 }
582
583 static void unpack_additional(ClosureUndetermined &cl, float4 data1)
584 {
585 cl.data.x = data1.x; /* Roughness. */
586 }
587};
588
590 static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
591 {
592 cl_packed.data1 = float4(cl.data.x, gbuffer::ior_pack(cl.data.y), 0.0f, 0.0f);
593 }
594
595 static void unpack_additional(ClosureUndetermined &cl, float4 data1)
596 {
597 cl.data.x = data1.x; /* Roughness. */
598 cl.data.y = gbuffer::ior_unpack(data1.y);
599 }
600};
601
603 static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
604 {
605 cl_packed.data1 = float4(cl.data.x, 0.0f, 0.0f, 0.0f);
606 }
607
608 static void unpack_additional(ClosureUndetermined &cl, float4 data1)
609 {
610 cl.data.x = data1.x; /* Roughness. */
611 }
612};
613
614/* Special case where we can save 1 data layers per closure. */
616 static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
617 {
618 cl_packed.data0 = float4(cl.data.x, 0.0f, cl_packed.data0.zw);
619 }
620
621 static void unpack_additional(ClosureUndetermined &cl, float4 data0)
622 {
623 cl.color = cl.color.zzz;
624 cl.data.x = data0.x; /* Roughness. */
625 }
626};
627
628/* Special case where we can save 1 data layers per closure. */
630 static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
631 {
632 cl_packed.data0 = float4(cl.data.x, gbuffer::ior_pack(cl.data.y), cl_packed.data0.zw);
633 }
634
635 static void unpack_additional(ClosureUndetermined &cl, float4 data0)
636 {
637 cl.color = cl.color.zzz;
638 cl.data.x = data0.x; /* Roughness. */
639 cl.data.y = gbuffer::ior_unpack(data0.y);
640 }
641};
642
643/* Special case where we can save 1 data layers per closure. */
645 static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
646 {
647 cl_packed.data0 = float4(cl.data.x, 0.0f, cl_packed.data0.zw);
648 }
649
650 static void unpack_additional(ClosureUndetermined &cl, float4 data0)
651 {
652 cl.color = cl.color.zzz;
653 cl.data.x = data0.x; /* Roughness. */
654 }
655};
656
658
659} // namespace gbuffer
btScalar max
static Thickness from(float value, ThicknessMode mode)
float value() const
ThicknessMode mode() const
#define CLOSURE_WEIGHT_CUTOFF
static constexpr int GBUFFER_LAYER_MAX
#define assert(assertion)
#define exp2
#define round
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
constexpr T sign(T) RET
#define abs
bool all(VecOp< bool, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
unsigned int uchar
VecBase< bool, 3 > bool3
float length_manhattan(VecT a)
float reduce_add(float2 a)
void set_flag_from_test(uint &value, bool test, uint flag)
bool flag_test(uint flag, uint val)
ClosureType
@ CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID
@ CLOSURE_BSDF_DIFFUSE_ID
@ CLOSURE_BSSRDF_BURLEY_ID
@ CLOSURE_NONE_ID
@ CLOSURE_BSDF_THIN_GLASS_TRANSMISSION_ID
@ CLOSURE_BSDF_TRANSLUCENT_ID
#define N
float3 closure_color_unpack(float4 color_packed)
float3 geometry_normal_unpack(uint data, float3 N)
float ior_unpack(float ior_packed)
bool closure_is_empty(ClosureUndetermined cl)
uint object_id_f16_unpack(float object_id_packed)
uint geometry_normal_pack(float3 Ng, float3 N)
float object_id_f16_pack(uint object_id)
bool color_is_grayscale(float3 color)
float thickness_pack(Thickness thickness)
static constexpr float ior_packed_lt_1_scale
static constexpr float closure_10bit_unit
ClosureType mode_to_closure_type(uint mode)
float2 normal_pack(float3 N)
float4 closure_color_pack(float3 color)
static constexpr float ior_packed_gt_1_bias
Thickness thickness_unpack(float thickness_packed)
static constexpr float ior_packed_eq_1
float3 normal_unpack(float2 N_packed)
float ior_pack(float ior)
GBufferMode closure_type_to_mode(ClosureType type, bool is_grayscale)
float3 sss_radii_unpack(float4 sss_radii_packed)
float4 sss_radii_pack(float3 sss_radii)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
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 x
Definition sky_math.h:225
static float2 pack(Thickness thickness)
static AdditionalInfo unpack(float2 data)
static ClosurePacking fallback(float3 surface_N)
uchar bin_to_layer(uchar bin_id) const
uint3 bin_index_per_layer() const
void tangent_space_id_set(uint layer_id, uint normal_id)
void use_object_id_set(bool value)
static constexpr int GBUFFER_HEADER_BITS_PER_BIN
uint3 bin_types_per_layer() const
static Header from_data(uint data)
GBufferMode bin_type(uchar bin) const
GBufferMode closure(uint bin) const
void geometry_normal_set(float3 Ng, float3 N)
void closure_set(uint bin, GBufferMode mode)
static constexpr uint GBUFFER_GEOMETRIC_NORMAL_BITS_SHIFT
uchar tangent_space_id(uint layer_id) const
float3 geometry_normal(float3 surface_N) const
static constexpr uint GBUFFER_NORMAL_BITS_SHIFT
static void unpack_additional(ClosureUndetermined &cl, float4 data0)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void unpack_additional(ClosureUndetermined &cl, float4 data1)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void unpack_additional(ClosureUndetermined &cl, float4 data0)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void unpack_additional(ClosureUndetermined &cl, float4 data1)
static void unpack_additional(ClosureUndetermined &cl, float4 data1)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void unpack_additional(ClosureUndetermined &cl, float4 data0)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void pack_additional(ClosurePacking &cl_packed, ClosureUndetermined cl)
static void unpack_additional(ClosureUndetermined &cl, float4 data1)
uint y
Definition types_uint3.h:13
uint x
Definition types_uint3.h:13