Blender V5.3
eevee_gbuffer_read.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
24
25namespace gbuffer::detail {
26
27/* -------------------------------------------------------------------- */
30
31ClosureUndetermined unpack_closure(ClosurePacking cl_in)
32{
33 ClosureUndetermined cl;
34 cl.type = gbuffer::mode_to_closure_type(cl_in.mode);
35 /* Common to all configs. */
36 cl.color = gbuffer::closure_color_unpack(cl_in.data0);
37 cl.N = cl_in.N;
38 /* Some closures require additional unpacking. */
39 switch (cl_in.mode) {
40 case GBUF_REFLECTION:
42 break;
45 break;
46 case GBUF_REFRACTION:
48 break;
51 break;
54 break;
57 break;
58 case GBUF_SUBSURFACE:
60 break;
61 default:
62 break;
63 }
64 return cl;
65}
66
67} // namespace gbuffer::detail
68
69namespace gbuffer {
70
72
73/* Result of reading the GBuffer. Data are to be indexed by layers.
74 * Note that the normal of the first closure is always guaranteed to be valid even if the closure
75 * has invalid type.*/
76struct Layers {
77 ClosureUndetermined layer[GBUFFER_LAYER_MAX];
79
81 {
82 return this->layer[0].N;
83 }
84
85 bool has_any_closure() const
86 {
87 return this->layer[0].type != CLOSURE_NONE_ID;
88 }
89 bool has_no_closure() const
90 {
91 return this->layer[0].type == CLOSURE_NONE_ID;
92 }
93};
94
95struct Reader {
99
100 uint fetch_object_id(int2 texel) const
101 {
102 return texelFetch(gbuf_header_tx, int3(texel, 1), 0).r;
103 }
104
106 {
107#if defined(GPU_NVIDIA) && defined(GPU_OPENGL)
108 /* WORKAROUND: Compiler bug where the loads are somehow invalid inside the ray tile
109 * classification shader. */
110 layer = min(layer, 9999u);
111#endif
112 return texelFetch(gbuf_closure_tx, int3(texel, int(layer)), 0);
113 }
114
116 {
117#if defined(GPU_NVIDIA) && defined(GPU_OPENGL)
118 /* WORKAROUND: Compiler bug where the loads are somehow invalid inside the ray tile
119 * classification shader. */
120 layer = min(layer, 9999u);
121#endif
122 return texelFetch(gbuf_normal_tx, int3(texel, int(layer)), 0);
123 }
124
126 {
127 auto &samp = gbuf_normal_tx;
128 return texelFetch(samp, int3(texel, textureSize(samp, 0).z - 1), 0);
129 }
130
131 /* Read only one layer from the GBuffer. */
132 ClosureUndetermined read_layer(
133 uchar normal_id, uchar closure_len, GBufferMode bin_mode, int2 texel, uchar layer_id) const
134 {
135 if (bin_mode == GBUF_NONE) {
136 return closure_new(ClosureType(CLOSURE_NONE_ID));
137 }
138
139 ClosurePacking cl_in;
140 cl_in.mode = bin_mode;
141
142 float2 packed_N = fetch_normal(texel, normal_id).xy;
143 cl_in.N = normal_unpack(packed_N);
144
145 cl_in.data0 = fetch_data(texel, layer_id);
146 if (cl_in.use_data1()) {
147 cl_in.data1 = fetch_data(texel, layer_id + closure_len);
148 }
149 return detail::unpack_closure(cl_in);
150 }
151
153 {
154 return Header::from_data(texelFetch(gbuf_header_tx, int3(texel, 0), 0).r);
155 }
156
157 /* Read the entirety of the GBuffer by layer. */
159 {
160 Layers layers;
161
162 layers.header = read_header(texel);
163 uint3 layer_types = layers.header.bin_types_per_layer();
164 uchar closure_count = layers.header.closure_len();
165
166 for (int i = 0; i < 3 /* GBUFFER_LAYER_MAX */; i++) [[unroll]] {
167 layers.layer[i] = read_layer(
168 layers.header.tangent_space_id(i), closure_count, GBufferMode(layer_types[i]), texel, i);
169 }
170 return layers;
171 }
172
173 /* Read only one bin from the GBuffer. */
174 ClosureUndetermined read_bin(Header header, int2 texel, uchar bin_index) const
175 {
176 GBufferMode bin_mode = header.bin_type(bin_index);
177
178 uchar layer_id = header.bin_to_layer(bin_index);
179 uchar normal_id = header.tangent_space_id(layer_id);
180 uchar closure_count = header.closure_len();
181
182 return read_layer(normal_id, closure_count, bin_mode, texel, layer_id);
183 }
184 ClosureUndetermined read_bin(int2 texel, uchar bin_index) const
185 {
186 return read_bin(read_header(texel), texel, bin_index);
187 }
188
189 /* Load thickness data only if available. Return 0 otherwise. */
190 Thickness read_thickness(Header header, int2 texel) const
191 {
192 if (!header.has_additional_data()) {
193 return Thickness::zero();
194 }
195 float2 data_packed = fetch_additional_data(texel).rg;
196 return AdditionalInfo::unpack(data_packed).thickness;
197 }
198
199 /* Returns the first world normal stored in the gbuffer. Assume gbuffer header is non-null. */
201 {
202 return normal_unpack(fetch_normal(texel, 0).rg);
203 }
204
205 /* Returns the object id stored in the gbuffer. Assume gbuffer header is non-null and object id
206 * is stored. */
207 uint read_object_id(int2 texel) const
208 {
209 return fetch_object_id(texel);
210 }
211};
212
214
215} // namespace gbuffer
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
static Thickness zero()
#define GBUF_NORMAL_TEX_SLOT
#define GBUF_CLOSURE_TEX_SLOT
#define GBUF_HEADER_TEX_SLOT
#define sampler(slot)
#define unroll
#define layer
SamplerBase< uint, 2, false, true > usampler2DArray
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< float, 2, false, true > sampler2DArray
TEX_TEMPLATE SizeVec textureSize(T, int) RET
unsigned int uchar
ClosureType
@ CLOSURE_NONE_ID
ClosureUndetermined unpack_closure(ClosurePacking cl_in)
float3 closure_color_unpack(float4 color_packed)
ClosureType mode_to_closure_type(uint mode)
float3 normal_unpack(float2 N_packed)
#define min(a, b)
Definition sort.cc:38
static AdditionalInfo unpack(float2 data)
uchar bin_to_layer(uchar bin_id) const
uint3 bin_types_per_layer() const
static Header from_data(uint data)
GBufferMode bin_type(uchar bin) const
uchar tangent_space_id(uint layer_id) const
ClosureUndetermined layer[GBUFFER_LAYER_MAX]
float4 fetch_data(int2 texel, uchar layer) const
float3 read_normal(int2 texel) const
ClosureUndetermined read_bin(int2 texel, uchar bin_index) const
float4 fetch_normal(int2 texel, uchar layer) const
Thickness read_thickness(Header header, int2 texel) const
Layers read_layers(int2 texel) const
float4 fetch_additional_data(int2 texel) const
Header read_header(int2 texel) const
uint read_object_id(int2 texel) const
sampler2DArray gbuf_closure_tx
ClosureUndetermined read_bin(Header header, int2 texel, uchar bin_index) const
uint fetch_object_id(int2 texel) const
sampler2DArray gbuf_normal_tx
usampler2DArray gbuf_header_tx
ClosureUndetermined read_layer(uchar normal_id, uchar closure_len, GBufferMode bin_mode, int2 texel, uchar layer_id) const
static void unpack_additional(ClosureUndetermined &cl, float4 data0)
static void unpack_additional(ClosureUndetermined &cl, float4 data1)
static void unpack_additional(ClosureUndetermined &cl, float4 data0)
static void unpack_additional(ClosureUndetermined &cl, float4 data1)
static void unpack_additional(ClosureUndetermined &cl, float4 data1)
static void unpack_additional(ClosureUndetermined &cl, float4 data0)
static void unpack_additional(ClosureUndetermined &cl, float4 data1)