Blender V5.3
eevee_subsurface.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
8#include "draw_view.bsl.hh"
9#include "eevee_defines.hh"
14#include "gpu_shader_codegen_lib.glsl"
19
21
22/* -------------------------------------------------------------------- */
28
44
45[[compute, local_size(SUBSURFACE_GROUP_SIZE, SUBSURFACE_GROUP_SIZE)]]
47 [[resource_table]] const draw::View &views,
48 [[resource_table]] const gbuffer::Reader &reader,
49 [[work_group_id]] const uint3 group_id,
50 [[global_invocation_id]] const uint3 global_thread_id,
51 [[local_invocation_index]] const uint local_thread_index)
52{
53 int2 texel = int2(global_thread_id.xy);
54
55 if (local_thread_index == 0u) {
56 srt.has_visible_sss = 0u;
57 }
58
59 barrier();
60
61 const ViewMatrices view = views.get(0);
62 const gbuffer::Layers gbuf = reader.read_layers(texel);
63
64 ClosureUndetermined cl = gbuf.layer[0];
65
66 if (cl.type == CLOSURE_BSSRDF_BURLEY_ID) {
67 float3 direct = rgb9e5_decode(imageLoadFast(srt.direct_light_img, int3(texel, 0)).r);
68 float3 indirect = imageLoadFast(srt.indirect_light_img, texel).rgb;
69
70 ClosureSubsurface closure = to_closure_subsurface(cl);
71 float max_radius = reduce_max(closure.sss_radius);
72
73 uint object_id = reader.read_object_id(texel);
74
75 if (srt.use_split_radiance) {
76 imageStoreFast(srt.radiance_img, int3(texel, 0), float4(direct, 0.0f));
77 imageStoreFast(srt.radiance_img, int3(texel, 1), float4(indirect, 0.0f));
78 }
79 else {
80 imageStoreFast(srt.radiance_img, int3(texel, 0), float4(direct + indirect, 0.0f));
81 }
82 imageStoreFast(srt.object_id_img, texel, uint4(object_id));
83
84 float depth = reverse_z::read(texelFetch(srt.depth_tx, texel, 0).r);
85 /* TODO(fclem): Check if this simplifies. */
86 float vPz = view.depth_screen_to_view(depth);
87 float homogenous_coord = view.winmat[2][3] * vPz + view.winmat[3][3];
88 float sample_scale = view.winmat[0][0] * (0.5f * max_radius / homogenous_coord);
89 float pixel_footprint = sample_scale * float(textureSize(reader.gbuf_header_tx, 0).x);
90 if (pixel_footprint > 1.0f) {
91 /* Race condition doesn't matter here. */
92 srt.has_visible_sss = 1u;
93 }
94 }
95 else {
96 if (srt.use_split_radiance) {
97 /* The indirect radiance is not part of the convolve stage shared memory cache. A cache hit
98 * and a similar indirect radiance texture fetch can differ slightly and lead to
99 * uninitialized reads that are not masked by the object ID. To avoid this, we clear the
100 * indirect radiance to 0. */
101 imageStoreFast(srt.radiance_img, int3(texel, 1), float4(0));
102 }
103 /* No need to write radiance_img (except for the case above) since the radiance won't be used
104 * at all. */
105 imageStore(srt.object_id_img, texel, uint4(0));
106 }
107
108 barrier();
109
110 if (local_thread_index == 0u) {
111 if (srt.has_visible_sss > 0u) {
112 uint tile_id = atomicAdd(srt.convolve_dispatch_buf.num_groups_x, 1u);
113 srt.convolve_tile_buf[tile_id] = packUvec2x16(group_id.xy);
114 /* Race condition doesn't matter here. */
115 srt.convolve_dispatch_buf.num_groups_y = 1;
116 srt.convolve_dispatch_buf.num_groups_z = 1;
117 }
118 }
119}
120
122
123/* -------------------------------------------------------------------- */
135
141
142struct Convolve {
144
148
151
152 [[uniform(SUBSURFACE_BUF_SLOT)]] const SubsurfaceData &subsurface_buf;
153
154 [[storage(0, read)]] const uint (&tiles_coord_buf)[];
155
156 [[shared]] float3 cached_radiance[SUBSURFACE_GROUP_SIZE][SUBSURFACE_GROUP_SIZE];
157 [[shared]] uint cached_sss_id[SUBSURFACE_GROUP_SIZE][SUBSURFACE_GROUP_SIZE];
158 [[shared]] float cached_depth[SUBSURFACE_GROUP_SIZE][SUBSURFACE_GROUP_SIZE];
159
160 void cache_populate(float2 local_uv, uint2 texel)
161 {
162 cached_radiance[texel.y][texel.x] = textureLod(radiance_tx, float3(local_uv, 0.0f), 0.0f).rgb;
163 cached_sss_id[texel.y][texel.x] = texture(object_id_tx, local_uv).r;
164 cached_depth[texel.y][texel.x] = reverse_z::read(texture(depth_tx, local_uv).r);
165 }
166
167 bool cache_sample(uint2 texel, uint3 group_id, SubSurfaceSample &samp) const
168 {
169 uint2 tile_coord = unpackUvec2x16(tiles_coord_buf[group_id.x]);
170 /* This can underflow and allow us to only do one upper bound check. */
171 texel -= tile_coord * SUBSURFACE_GROUP_SIZE;
172 if (any(greaterThanEqual(texel, uint2(SUBSURFACE_GROUP_SIZE)))) {
173 return false;
174 }
175 samp.radiance = cached_radiance[texel.y][texel.x];
176 samp.sss_id = cached_sss_id[texel.y][texel.x];
177 samp.depth = cached_depth[texel.y][texel.x];
178 return true;
179 }
180
182 {
183 SubSurfaceSample samp;
184 uint2 sample_texel = uint2(sample_uv * float2(textureSize(depth_tx, 0)));
185 if (cache_sample(sample_texel, group_id, samp)) {
186 return samp;
187 }
188 samp.depth = reverse_z::read(texture(depth_tx, sample_uv).r);
189 samp.sss_id = texture(object_id_tx, sample_uv).r;
190 samp.radiance = textureLod(radiance_tx, float3(sample_uv, 0.0f), 0.0f).rgb;
191 return samp;
192 }
193};
194
195[[compute, local_size(SUBSURFACE_GROUP_SIZE, SUBSURFACE_GROUP_SIZE)]]
197 [[resource_table]] const draw::View &views,
198 [[resource_table]] const gbuffer::Reader &reader,
199 [[work_group_id]] const uint3 group_id,
200 [[local_invocation_id]] const uint3 local_thread_id)
201{
202 constexpr uint tile_size = uint(SUBSURFACE_GROUP_SIZE);
203 uint2 tile_coord = unpackUvec2x16(srt.tiles_coord_buf[group_id.x]);
204 int2 texel = int2(local_thread_id.xy + tile_coord * tile_size);
205
206 float2 center_uv = (float2(texel) + 0.5f) / float2(textureSize(reader.gbuf_header_tx, 0).xy);
207
208 srt.cache_populate(center_uv, local_thread_id.xy);
209 barrier();
210
211 const ViewMatrices view = views.get(0);
212
213 float depth = reverse_z::read(texelFetch(srt.depth_tx, texel, 0).r);
214 float3 vP = view.point_screen_to_view(float3(center_uv, depth));
215
216 const gbuffer::Layers gbuf = reader.read_layers(texel);
217 if (gbuf.layer[0].type != CLOSURE_BSSRDF_BURLEY_ID) {
218 return;
219 }
220
221 const uint object_id = reader.read_object_id(texel);
222
223 const ClosureSubsurface closure = to_closure_subsurface(gbuf.layer[0]);
224 float max_radius = reduce_max(closure.sss_radius);
225
226 float homogenous_coord = view.winmat[2][3] * vP.z + view.winmat[3][3];
227 float2 sample_scale = float2(view.winmat[0][0], view.winmat[1][1]) *
228 (0.5f * max_radius / homogenous_coord);
229
230 float pixel_footprint = sample_scale.x * textureSize(srt.depth_tx, 0).x;
231 if (pixel_footprint <= 1.0f) {
232 /* Early out, avoid divisions by zero. */
233 return;
234 }
235
236 /* Avoid too small radii that have float imprecision. */
237 float3 clamped_sss_radius = max(float3(srt.subsurface_buf.min_radius),
238 closure.sss_radius / max_radius) *
239 max_radius;
240 /* Scale albedo because we can have HDR value caused by BSDF sampling. */
241 float3 albedo = closure.color / max(1e-6f, reduce_max(closure.color));
242 float3 d = burley_setup(clamped_sss_radius, albedo);
243
244 /* Do not rotate too much to avoid too much cache misses. */
245 float golden_angle = M_PI * (3.0f - sqrt(5.0f));
246 float theta = random::interleaved_gradient(float2(texel), 0, 0.0f) * golden_angle;
247
248 float2x2 sample_space = from_scale(sample_scale) * from_rotation(AngleRadian{theta});
249
250 float3 accum_weight = float3(0.0f);
251 float3 accum_radiance = float3(0.0f);
252 float3 accum_radiance_indirect = float3(0.0f);
253
254 for (int i = 0; i < srt.subsurface_buf.sample_len; i++) {
255 float2 sample_uv = center_uv + sample_space * srt.subsurface_buf.samples[i].xy;
256 float pdf_inv = srt.subsurface_buf.samples[i].z;
257
258 SubSurfaceSample samp = srt.sample_neighborhood(sample_uv, group_id);
259 /* Reject radiance from other surfaces. Avoids light leak between objects. */
260 if (samp.sss_id != object_id) {
261 continue;
262 }
263 /* Slide 34. */
264 float3 sample_vP = view.point_screen_to_view(float3(sample_uv, samp.depth));
265 float r = distance(sample_vP, vP);
266 float3 weight = burley_eval(d, r) * pdf_inv;
267
268 accum_radiance += samp.radiance * weight;
269 accum_weight += weight;
270
271 if (srt.use_split_radiance) {
272 accum_radiance_indirect += textureLod(srt.radiance_tx, float3(sample_uv, 1.0f), 0.0f).rgb *
273 weight;
274 }
275 }
276 /* Normalize the sum (slide 34). */
277 float3 accum_weight_inv = safe_rcp(accum_weight);
278 accum_radiance *= accum_weight_inv;
279 accum_radiance_indirect *= accum_weight_inv;
280
281 /* Put result in direct diffuse. */
282 imageStoreFast(srt.out_direct_light_img, int3(texel, 0), uint4(rgb9e5_encode(accum_radiance)));
283 /* Note that if we don't use split radiance, this clears the indirect pass since its content has
284 * been merged and convolved with direct light.*/
285 imageStoreFast(srt.out_indirect_light_img, texel, float4(accum_radiance_indirect, 0.0f));
286}
287
289
292
293} // namespace eevee::subsurface
#define M_PI
btScalar max
#define SUBSURFACE_RADIANCE_FORMAT
#define RAYTRACE_RADIANCE_FORMAT
#define SUBSURFACE_BUF_SLOT
#define DEFERRED_RADIANCE_FORMAT
#define SUBSURFACE_OBJECT_ID_FORMAT
#define shared
#define uniform(slot)
#define local_invocation_index
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define work_group_id
#define resource_table
#define sampler(slot)
#define specialization_constant(default_value)
#define global_invocation_id
#define local_invocation_id
#define compute
#define local_size(...)
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
bool any(VecOp< bool, D >) RET
float distance(float, float) RET
int atomicAdd(int &, int) RET
#define sqrt
void barrier()
ImageBase< uint, 2 > uimage2D
ImageBase< uint, 2, true > uimage2DArray
ImageBase< float, 2, true > image2DArray
ImageBase< float, 2 > image2D
#define imageLoadFast
#define imageStoreFast
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
SamplerBase< float, 2, false, false, false, true > sampler2DDepth
SamplerBase< float, 2, false, true > sampler2DArray
TEX_TEMPLATE SizeVec textureSize(T, int) RET
SamplerBase< uint, 2 > usampler2D
float2x2 from_scale(float2 scale)
float2x2 from_rotation(AngleRadian rotation)
float safe_rcp(float a)
float reduce_max(float2 a)
uint rgb9e5_encode(float3 color)
float3 rgb9e5_decode(uint data)
uint2 unpackUvec2x16(uint a)
uint packUvec2x16(uint2 a)
@ CLOSURE_BSSRDF_BURLEY_ID
MatBase< float, 2, 2 > float2x2
PipelineCompute setup(setup_main)
void setup_main(Setup &srt, const draw::View &views, const gbuffer::Reader &reader, const uint3 group_id, const uint3 global_thread_id, const uint local_thread_index)
void convolve_main(Convolve &srt, const draw::View &views, const gbuffer::Reader &reader, const uint3 group_id, const uint3 local_thread_id)
PipelineCompute convolve(convolve_main)
float interleaved_gradient(float2 pixel, float seed, float offset)
T read(T depth_buffer_value)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float depth_screen_to_view(float ss_depth) const
float3 point_screen_to_view(float3 ssP) const
float3 cached_radiance[SUBSURFACE_GROUP_SIZE][SUBSURFACE_GROUP_SIZE]
SubSurfaceSample sample_neighborhood(float2 sample_uv, uint3 group_id) const
uint cached_sss_id[SUBSURFACE_GROUP_SIZE][SUBSURFACE_GROUP_SIZE]
bool cache_sample(uint2 texel, uint3 group_id, SubSurfaceSample &samp) const
void cache_populate(float2 local_uv, uint2 texel)
float cached_depth[SUBSURFACE_GROUP_SIZE][SUBSURFACE_GROUP_SIZE]
const SubsurfaceData & subsurface_buf
DispatchCommand & convolve_dispatch_buf
const uimage2DArray direct_light_img
float x
float z
Definition sky_math.h:136
ClosureUndetermined layer[GBUFFER_LAYER_MAX]
uint x
Definition types_uint2.h:13
uint y
Definition types_uint2.h:13
uint x
Definition types_uint3.h:13