Blender V5.3
eevee_light.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "draw_debug.hh"
12
13#include "eevee_instance.hh"
14
15#include "eevee_light.hh"
16
17#include "DNA_light_types.h"
18#include "DNA_sdna_type_ids.hh"
19
20#include "BKE_light.h"
21
22namespace blender::eevee {
23
24/* Convert by putting the least significant bits in the first component. */
26{
27 return {uint(data), uint(data >> uint64_t(32))};
28}
29
30/* -------------------------------------------------------------------- */
33
34static eLightType to_light_type(short blender_light_type,
35 short blender_area_type,
36 bool use_soft_falloff)
37{
38 switch (blender_light_type) {
39 default:
40 case LA_LOCAL:
41 return use_soft_falloff ? LIGHT_OMNI_DISK : LIGHT_OMNI_SPHERE;
42 case LA_SUN:
43 return LIGHT_SUN;
44 case LA_SPOT:
45 return use_soft_falloff ? LIGHT_SPOT_DISK : LIGHT_SPOT_SPHERE;
46 case LA_AREA:
47 return ELEM(blender_area_type, LA_AREA_DISK, LA_AREA_ELLIPSE) ? LIGHT_ELLIPSE : LIGHT_RECT;
48 }
49}
50
52
53/* -------------------------------------------------------------------- */
56
59 char visibility_flag,
60 const blender::Light *la,
63 float threshold)
64{
65 using namespace blender::math;
66
67 eLightType new_type = to_light_type(la->type, la->area_shape, la->mode & LA_USE_SOFT_FALLOFF);
68 if (assign_if_different(this->type, new_type)) {
69 shadow_discard_safe(shadows);
70 }
71
72 this->color = BKE_light_color(*la);
73 float base_power = BKE_light_power(*la);
74 if (la->mode & LA_UNNORMALIZED) {
75 base_power *= BKE_light_area(*la, object_to_world);
76 }
77
79 object_to_world.view<3, 3>() = normalize_and_get_size(object_to_world.view<3, 3>(), scale);
80
81 /* Make sure we have consistent handedness (in case of negatively scaled Z axis). */
82 float3 back = cross(float3(object_to_world.x_axis()), float3(object_to_world.y_axis()));
83 if (dot(back, float3(object_to_world.z_axis())) < 0.0f) {
84 negate_v3(object_to_world.y_axis());
85 }
86
87 this->object_to_world = object_to_world;
88
89 shape_parameters_set(
90 la, scale, object_to_world.z_axis(), threshold, shadows.get_data().use_jitter);
91
92 const bool diffuse_visibility = (visibility_flag & OB_HIDE_DIFFUSE) == 0;
93 const bool glossy_visibility = (visibility_flag & OB_HIDE_GLOSSY) == 0;
94 const bool transmission_visibility = (visibility_flag & OB_HIDE_TRANSMISSION) == 0;
95 const bool volume_visibility = (visibility_flag & OB_HIDE_VOLUME_SCATTER) == 0;
96
97 this->shape_power = base_power * shape_radiance_get();
98 this->point_power = base_power * point_radiance_get();
99 this->power_factor[LIGHT_DIFFUSE] = la->diff_fac * diffuse_visibility;
100 this->power_factor[LIGHT_SPECULAR] = la->spec_fac * glossy_visibility;
101 this->power_factor[LIGHT_TRANSMISSION] = la->transmission_fac * transmission_visibility;
102 this->power_factor[LIGHT_VOLUME] = la->volume_fac * volume_visibility;
103
104 this->lod_bias = shadows.global_lod_bias();
105 this->lod_min = shadow_lod_min_get(la);
106 this->filter_radius = la->shadow_filter_radius;
107 this->flags = LightFlag(0u);
108 if (la->mode & LA_SHADOW_JITTER) {
110 }
111 if (visibility_flag & OB_HIDE_CAMERA) {
112 this->flags = LightFlag(this->flags | LIGHT_CAMERA_HIDDEN);
113 }
114
115 if (la->mode & LA_SHADOW) {
116 shadow_ensure(shadows);
117 }
118 else {
119 shadow_discard_safe(shadows);
120 }
121
122 this->light_set_membership = uint64_to_uint2(light_set_membership);
123 this->shadow_set_membership = uint64_to_uint2(shadow_set_membership);
124 this->initialized = true;
125}
126
127float Light::shadow_lod_min_get(const blender::Light *la)
128{
129 /* Property is in mm. Convert to unit. */
130 float max_res_unit = la->shadow_maximum_resolution;
131 if (is_sun_light(this->type)) {
132 return log2f(max_res_unit * SHADOW_MAP_MAX_RES) - 1.0f;
133 }
134 /* Store absolute mode as negative. */
135 return (la->mode & LA_SHAD_RES_ABSOLUTE) ? -max_res_unit : max_res_unit;
136}
137
139{
140 if (this->directional != nullptr) {
141 shadows.directional_pool.destruct(*directional);
142 this->directional = nullptr;
143 }
144 if (this->punctual != nullptr) {
145 shadows.punctual_pool.destruct(*punctual);
146 this->punctual = nullptr;
147 }
148}
149
151{
152 if (is_sun_light(this->type) && this->directional == nullptr) {
153 this->directional = &shadows.directional_pool.construct(shadows);
154 }
155 else if (this->punctual == nullptr) {
156 this->punctual = &shadows.punctual_pool.construct(shadows);
157 }
158}
159
160float Light::attenuation_radius_get(const blender::Light *la,
161 float light_threshold,
162 float light_power)
163{
164 if (la->mode & LA_CUSTOM_ATTENUATION) {
165 return la->att_dist;
166 }
167 /* Compute the distance (using the inverse square law)
168 * at which the light power reaches the light_threshold. */
169 /* TODO take area light scale into account. */
170 return sqrtf(light_power / light_threshold);
171}
172
173void Light::shape_parameters_set(const blender::Light *la,
174 const float3 &scale,
175 const float3 &z_axis,
176 const float threshold,
177 const bool use_jitter)
178{
179 using namespace blender::math;
180
181 /* Compute influence radius first. Can be amended by shape later. */
182 if (is_local_light(this->type)) {
183 LightLocalData &l_local = this->local;
184 const float max_power = reduce_max(BKE_light_color(*la)) *
185 fabsf(BKE_light_power(*la) / 100.0f);
186 const float surface_max_power = max(max(la->diff_fac, la->spec_fac), la->transmission_fac) *
187 max_power;
188 const float volume_max_power = la->volume_fac * max_power;
189
190 float influence_radius_surface = attenuation_radius_get(la, threshold, surface_max_power);
191 float influence_radius_volume = attenuation_radius_get(la, threshold, volume_max_power);
192
193 l_local.local.influence_radius_max = max(influence_radius_surface, influence_radius_volume);
194 l_local.local.influence_radius_invsqr_surface = safe_rcp(square(influence_radius_surface));
195 l_local.local.influence_radius_invsqr_volume = safe_rcp(square(influence_radius_volume));
196 /* TODO(fclem): This is just duplicating a member for local lights. */
198 this->clip_near = float_as_int(l_local.local.influence_radius_max / 4000.0f);
199 }
200
201 float trace_scaling_fac = (use_jitter && (la->mode & LA_SHADOW_JITTER)) ?
202 la->shadow_jitter_overblur / 100.0f :
203 1.0f;
204
205 if (is_sun_light(this->type)) {
206 LightSunData &l_sun = this->sun;
207 float sun_half_angle = min_ff(la->sun_angle, DEG2RADF(179.9f)) / 2.0f;
208 /* Use non-clamped radius for soft shadows. Avoid having a minimum blur. */
209 l_sun.shadow_angle = sun_half_angle * trace_scaling_fac;
210 /* Clamp to a minimum to distinguish between point lights and area light shadow. */
211 l_sun.shadow_angle = (sun_half_angle > 0.0f) ? max_ff(1e-8f, l_sun.shadow_angle) : 0.0f;
212 /* Precompute this cosine on CPU to avoid differences in shadow tracing between platforms. */
213 l_sun.shadow_angle_cos = cosf(l_sun.shadow_angle);
214 /* Clamp to minimum value before float imprecision artifacts appear. */
215 l_sun.shape_radius = clamp(tanf(sun_half_angle), 0.001f, 20.0f);
216 /* Stable shading direction. */
217 l_sun.direction = z_axis;
218 }
219 else if (is_area_light(this->type)) {
220 LightAreaData &l_area = this->area;
221 const bool is_irregular = ELEM(la->area_shape, LA_AREA_RECT, LA_AREA_ELLIPSE);
222 l_area.size = float2(la->area_size, is_irregular ? la->area_sizey : la->area_size);
223 /* Scale and clamp to minimum value before float imprecision artifacts appear. */
224 l_area.size *= scale.xy() / 2.0f;
225 l_area.shadow_scale = trace_scaling_fac;
226 l_area.local.shadow_radius = length(l_area.size) * trace_scaling_fac;
227 /* Set to default position. */
228 l_area.local.shadow_position = float3(0.0f);
229 /* Do not render lights that have no area. */
230 if (l_area.size.x * l_area.size.y < 0.00001f) {
231 /* Forces light to be culled. */
232 l_area.local.influence_radius_max = 0.0f;
233 }
234 /* Clamp to minimum value before float imprecision artifacts appear. */
235 l_area.size = max(float2(0.003f), l_area.size);
236 /* For volume point lighting. */
237 l_area.local.shape_radius = max(0.001f, length(l_area.size) / 2.0f);
238 }
239 else if (is_point_light(this->type)) {
240 LightSpotData &l_spot = this->spot;
241 LightLocalData &l_local = this->local;
242 /* Spot size & blend */
243 if (is_spot_light(this->type)) {
244 const float spot_size = cosf(la->spotsize * 0.5f);
245 const float spot_blend = (1.0f - spot_size) * la->spotblend;
246 l_spot.spot_size_inv = scale.z / max(scale.xy(), float2(1e-8f));
247 l_spot.spot_mul = 1.0f / max(1e-8f, spot_blend);
248 l_spot.spot_bias = -spot_size * l_spot.spot_mul;
249 l_spot.spot_tan = tanf(min(la->spotsize * 0.5f, float(M_PI_2 - 0.0001f)));
250 }
251 else {
252 /* Point light could access it. Make sure to avoid Undefined Behavior.
253 * In practice it is only ever used. */
254 l_spot.spot_size_inv = float2(1.0f);
255 l_spot.spot_mul = 0.0f;
256 l_spot.spot_bias = 1.0f;
257 l_spot.spot_tan = 0.0f;
258 }
259 /* Use unclamped radius for soft shadows. Avoid having a minimum blur. */
260 l_local.local.shadow_radius = max(0.0f, la->radius) * trace_scaling_fac;
261 /* Clamp to a minimum to distinguish between point lights and area light shadow. */
262 l_local.local.shadow_radius = (la->radius > 0.0f) ? max_ff(1e-8f, local.local.shadow_radius) :
263 0.0f;
264 /* Set to default position. */
265 l_local.local.shadow_position = float3(0.0f);
266 l_local.local.shape_radius = la->radius;
267 /* Clamp to minimum value before float imprecision artifacts appear. */
268 l_local.local.shape_radius = max(0.001f, l_local.local.shape_radius);
269 }
270}
271
272float Light::shape_radiance_get()
273{
274 using namespace blender::math;
275
276 /* Make illumination power constant. */
277 switch (this->type) {
278 case LIGHT_RECT:
279 case LIGHT_ELLIPSE: {
280 /* Rectangle area. */
281 float area = this->area.size.x * this->area.size.y * 4.0f;
282 /* Scale for the lower area of the ellipse compared to the surrounding rectangle. */
283 if (this->type == LIGHT_ELLIPSE) {
284 area *= M_PI / 4.0f;
285 }
286 /* Convert radiant flux to radiance. */
287 return float(M_1_PI) / area;
288 }
290 case LIGHT_OMNI_DISK:
292 case LIGHT_SPOT_DISK: {
293 /* Sphere area. */
294 float area = float(4.0f * M_PI) * square(this->local.local.shape_radius);
295 /* Convert radiant flux to radiance. */
296 return 1.0f / (area * float(M_PI));
297 }
298 case LIGHT_SUN_ORTHO:
299 case LIGHT_SUN: {
300 float inv_sin_sq = 1.0f + 1.0f / square(this->sun.shape_radius);
301 /* Convert irradiance to radiance. */
302 return float(M_1_PI) * inv_sin_sq;
303 }
304 }
306 return 0.0f;
307}
308
309float Light::point_radiance_get()
310{
311 /* Volume light is evaluated as point lights. */
312 switch (this->type) {
313 case LIGHT_RECT:
314 case LIGHT_ELLIPSE: {
315 /* This corrects for area light most representative point trick.
316 * The fit was found by reducing the average error compared to cycles. */
317 float area = this->area.size.x * this->area.size.y * 4.0f;
318 float tmp = M_PI_2 / (M_PI_2 + sqrtf(area));
319 /* Lerp between 1.0 and the limit (1 / pi). */
320 float mrp_scaling = tmp + (1.0f - tmp) * M_1_PI;
321 return float(M_1_PI) * mrp_scaling;
322 }
324 case LIGHT_OMNI_DISK:
326 case LIGHT_SPOT_DISK: {
327 /* Convert radiant flux to intensity. */
328 /* Inverse of sphere solid angle. */
329 return float(1.0 / (4.0 * M_PI));
330 }
331 case LIGHT_SUN_ORTHO:
332 case LIGHT_SUN: {
333 return 1.0f;
334 }
335 }
337 return 0.0f;
338}
339
341{
343 this->local.local.influence_radius_max,
344 float4(0.8f, 0.3f, 0.0f, 1.0f));
345}
346
348
349/* -------------------------------------------------------------------- */
352
354{
355 /* WATCH: Destructor order. Expect shadow module to be destructed later. */
356 for (Light &light : light_map_.values()) {
357 light.shadow_discard_safe(inst_.shadows);
358 }
359};
360
361void LightModule::add_world_sun_light(const ObjectKey &key, bool use_diffuse, bool use_glossy)
362{
363 /* Create a placeholder light to be fed by the GPU after sunlight extraction.
364 * Sunlight is disabled if power is zero. */
365 blender::Light la = {};
366 la.type = LA_SUN;
367 /* Set on the GPU. */
368 la.r = la.g = la.b = -1.0f; /* Tag as world sun light. */
369 la.energy = 1.0f;
370 la.sun_angle = inst_.world.sun_angle();
371 la.shadow_filter_radius = inst_.world.sun_shadow_filter_radius();
372 la.shadow_jitter_overblur = inst_.world.sun_shadow_jitter_overblur();
373 la.shadow_maximum_resolution = inst_.world.sun_shadow_max_resolution();
376 int visibility_flag = 0;
377 SET_FLAG_FROM_TEST(visibility_flag, !use_diffuse, OB_HIDE_DIFFUSE);
378 SET_FLAG_FROM_TEST(visibility_flag, !use_glossy, OB_HIDE_GLOSSY);
379 visibility_flag |= OB_HIDE_CAMERA;
380
381 Light &light = light_map_.lookup_or_add_default(key);
382 light.used = true;
383 light.resource_id = 0;
384 light.sync(inst_.shadows,
386 visibility_flag,
387 &la,
388 draw::LIGHT_LINK_MASK_ALL,
389 draw::LIGHT_LINK_MASK_ALL,
390 light_threshold_);
391
392 sun_lights_len_ += 1;
393}
394
396{
397 if (assign_if_different(use_scene_lights_, inst_.use_scene_lights())) {
398 if (inst_.is_viewport()) {
399 /* Catch lookdev viewport properties updates. */
400 inst_.sampling.reset();
401 }
402 }
403
404 /* Disable sunlight if world has a volume shader as we consider the light cannot go through an
405 * infinite opaque medium. */
406 use_sun_lights_ = (inst_.world.has_volume_absorption() == false);
407
408 /* In begin_sync so it can be animated. */
409 if (assign_if_different(light_threshold_, max_ff(1e-16f, inst_.scene->eevee.light_threshold))) {
410 /* All local lights need to be re-sync. */
411 for (Light &light : light_map_.values()) {
412 if (!ELEM(light.type, LIGHT_SUN, LIGHT_SUN_ORTHO)) {
413 light.initialized = false;
414 }
415 }
416 }
417
418 sun_lights_len_ = 0;
419 local_lights_len_ = 0;
420
421 if (use_sun_lights_ && inst_.world.sun_threshold() > 0.0f) {
422 if (inst_.pipelines.world.use_lightpath_node()) {
423 add_world_sun_light(world_sunlight_key_[WORLD_SUN_DIFFUSE], true, false);
424 add_world_sun_light(world_sunlight_key_[WORLD_SUN_GLOSSY], false, true);
425 }
426 else {
427 add_world_sun_light(world_sunlight_key_[WORLD_SUN_COMBINED], true, true);
428 }
429 }
430}
431
433{
435 if (use_scene_lights_ == false) {
436 return;
437 }
438
439 if (use_sun_lights_ == false) {
440 if (la.type == LA_SUN) {
441 return;
442 }
443 }
444
445 Light &light = light_map_.lookup_or_add_default(ObjectKey(ob_ref));
446 light.used = true;
447 ResourceHandleRange handle = inst_.manager->unique_handle(ob_ref);
448 inst_.manager->extract_all_object_attributes(handle, ob_ref);
449 light.resource_id = handle.index();
450 if (inst_.get_recalc_flags(ob_ref) != 0 || !light.initialized) {
451 light.initialized = true;
452 light.sync(inst_.shadows,
453 ob_ref.object_to_world(),
454 ob_ref.object->visibility_flag,
455 &la,
458 light_threshold_);
459 }
460 sun_lights_len_ += int(is_sun_light(light.type));
461 local_lights_len_ += int(!is_sun_light(light.type));
462}
463
465{
466 /* IMPORTANT: We cannot add new lights here since the shadow module already executed its
467 * `end_sync`. Doing so ends up in very bad data access since the shadow data of the new light
468 * will not exists on the GPU. */
469
470 /* NOTE: We resize this buffer before removing deleted lights. */
471 int lights_allocated = ceil_to_multiple_u(max_ii(light_map_.size(), 1), LIGHT_CHUNK);
472 light_buf_.resize(lights_allocated);
473
474 /* Track light deletion. */
475 /* Indices inside GPU data array. */
476 int sun_lights_idx = 0;
477 int local_lights_idx = sun_lights_len_;
478
479 /* Fill GPU data with scene data. */
480 auto it_end = light_map_.items().end();
481 for (auto it = light_map_.items().begin(); it != it_end; ++it) {
482 Light &light = (*it).value;
483 /* Do not discard casters in baking mode. See WORKAROUND in `surfels_create`. */
484 if (!light.used && !inst_.is_baking()) {
485 light_map_.remove(it);
486 continue;
487 }
488
489 int dst_idx = is_sun_light(light.type) ? sun_lights_idx++ : local_lights_idx++;
490 /* Put all light data into global data SSBO. */
491 light_buf_[dst_idx] = light;
492
493 /* Untag for next sync. */
494 light.used = false;
495 }
496 /* This scene data buffer is then immutable after this point. */
497 light_buf_.push_update();
498
499 /* If exceeding the limit, just trim off the excess to avoid glitchy rendering. */
500 if (sun_lights_len_ + local_lights_len_ > CULLING_MAX_ITEM) {
501 sun_lights_len_ = min_ii(sun_lights_len_, CULLING_MAX_ITEM);
502 local_lights_len_ = min_ii(local_lights_len_, CULLING_MAX_ITEM - sun_lights_len_);
503 inst_.info_append_i18n("Error: Too many lights in the scene.");
504 }
505 lights_len_ = sun_lights_len_ + local_lights_len_;
506
507 /* Resize to the actual number of lights after pruning. */
508 lights_allocated = ceil_to_multiple_u(max_ii(lights_len_, 1), LIGHT_CHUNK);
509 culling_key_buf_.resize(lights_allocated);
510 culling_zdist_buf_.resize(lights_allocated);
511 culling_light_buf_.resize(lights_allocated);
512
513 {
514 int2 render_extent = inst_.film.render_extent_get();
515 int2 probe_extent = int2(inst_.sphere_probes.probe_render_extent());
516 int2 max_extent = math::max(render_extent, probe_extent);
517 /* Compute tile size and total word count. */
518 uint word_per_tile = divide_ceil_u(max_ii(lights_len_, 1), 32);
519 int2 tiles_extent;
520 /* Default to 32 as this is likely to be the maximum
521 * tile size used by hardware or compute shading. */
522 uint tile_size = 16;
523 bool tile_size_valid = false;
524 do {
525 tile_size *= 2;
526 tiles_extent = math::divide_ceil(max_extent, int2(tile_size));
527 uint tile_count = tiles_extent.x * tiles_extent.y;
528 if (tile_count > max_tile_count_threshold) {
529 continue;
530 }
531 total_word_count_ = tile_count * word_per_tile;
532 tile_size_valid = true;
533
534 } while (total_word_count_ > max_word_count_threshold || !tile_size_valid);
535 /* Keep aligned with storage buffer requirements. */
536 total_word_count_ = ceil_to_multiple_u(total_word_count_, 32);
537
538 culling_data_buf_.tile_word_len = word_per_tile;
539 culling_data_buf_.tile_size = tile_size;
540 culling_data_buf_.tile_x_len = tiles_extent.x;
541 culling_data_buf_.tile_y_len = tiles_extent.y;
542 culling_data_buf_.items_count = lights_len_;
543 culling_data_buf_.local_lights_len = local_lights_len_;
544 culling_data_buf_.sun_lights_len = sun_lights_len_;
545 }
546 culling_tile_buf_.resize(total_word_count_);
547
548 culling_pass_sync();
549 update_pass_sync();
550 shape_display_pass_sync();
551 debug_pass_sync();
552}
553
554void LightModule::shape_display_pass_sync()
555{
556 shape_display_ps_.init();
557
558 if (lights_len_ == 0) {
559 return;
560 }
561
565 shape_display_ps_.bind_resources(inst_.uniform_data);
566 shape_display_ps_.bind_resources(inst_.volume.result);
567 shape_display_ps_.bind_resources(inst_.lights);
568 shape_display_ps_.draw_procedural(GPU_PRIM_TRIS, 1, lights_len_ * 6);
569}
570
571void LightModule::culling_pass_sync()
572{
573 uint safe_lights_len = max_ii(lights_len_, 1);
574 uint culling_select_dispatch_size = divide_ceil_u(safe_lights_len, CULLING_SELECT_GROUP_SIZE);
575 uint culling_sort_dispatch_size = divide_ceil_u(safe_lights_len, CULLING_SORT_GROUP_SIZE);
576 uint culling_tile_dispatch_size = divide_ceil_u(total_word_count_, CULLING_TILE_GROUP_SIZE);
577
578 /* NOTE: We reference the buffers that may be resized or updated later. */
579
580 culling_ps_.init();
581 {
582 auto &sub = culling_ps_.sub("Select");
583 sub.shader_set(inst_.shaders.static_shader_get(LIGHT_CULLING_SELECT));
584 sub.bind_ubo("sunlight_buf", &inst_.world.sunlight);
585 sub.bind_ssbo("light_cull_buf", &culling_data_buf_);
586 sub.bind_ssbo("in_light_buf", light_buf_);
587 sub.bind_ssbo("out_light_buf", culling_light_buf_);
588 sub.bind_ssbo("out_zdist_buf", culling_zdist_buf_);
589 sub.bind_ssbo("out_key_buf", culling_key_buf_);
590 sub.dispatch(int3(culling_select_dispatch_size, 1, 1));
591 sub.barrier(GPU_BARRIER_SHADER_STORAGE);
592 }
593 {
594 auto &sub = culling_ps_.sub("Sort");
595 sub.shader_set(inst_.shaders.static_shader_get(LIGHT_CULLING_SORT));
596 sub.bind_ssbo("light_cull_buf", &culling_data_buf_);
597 sub.bind_ssbo("in_light_buf", light_buf_);
598 sub.bind_ssbo("out_light_buf", culling_light_buf_);
599 sub.bind_ssbo("in_zdist_buf", culling_zdist_buf_);
600 sub.bind_ssbo("in_key_buf", culling_key_buf_);
601 sub.dispatch(int3(culling_sort_dispatch_size, 1, 1));
602 sub.barrier(GPU_BARRIER_SHADER_STORAGE);
603 }
604 {
605 auto &sub = culling_ps_.sub("Zbin");
606 sub.shader_set(inst_.shaders.static_shader_get(LIGHT_CULLING_ZBIN));
607 sub.bind_ssbo("light_cull_buf", &culling_data_buf_);
608 sub.bind_ssbo("light_buf", culling_light_buf_);
609 sub.bind_ssbo("out_zbin_buf", culling_zbin_buf_);
610 sub.dispatch(int3(1, 1, 1));
611 sub.barrier(GPU_BARRIER_SHADER_STORAGE);
612 }
613 {
614 auto &sub = culling_ps_.sub("Tiles");
615 sub.shader_set(inst_.shaders.static_shader_get(LIGHT_CULLING_TILE));
616 sub.bind_ssbo("light_cull_buf", &culling_data_buf_);
617 sub.bind_ssbo("light_buf", culling_light_buf_);
618 sub.bind_ssbo("out_light_tile_buf", culling_tile_buf_);
619 sub.dispatch(int3(culling_tile_dispatch_size, 1, 1));
620 sub.barrier(GPU_BARRIER_SHADER_STORAGE);
621 }
622}
623
624void LightModule::update_pass_sync()
625{
626 /* TODO(fclem): This dispatch for all light before culling. This could be made better by
627 * only running on lights that survive culling using an indirect dispatch. */
628 uint safe_lights_len = max_ii(lights_len_, 1);
629 uint shadow_setup_dispatch_size = divide_ceil_u(safe_lights_len, CULLING_SELECT_GROUP_SIZE);
630
631 auto &pass = update_ps_;
632 pass.init();
633 pass.shader_set(inst_.shaders.static_shader_get(LIGHT_SHADOW_SETUP));
634 pass.bind_ssbo("light_buf", &culling_light_buf_);
635 pass.bind_ssbo("light_cull_buf", &culling_data_buf_);
636 pass.bind_ssbo("tilemaps_buf", &inst_.shadows.tilemap_pool.tilemaps_data);
637 pass.bind_ssbo("tilemaps_clip_buf", &inst_.shadows.tilemap_pool.tilemaps_clip);
638 pass.bind_resources(inst_.uniform_data);
639 pass.bind_resources(inst_.sampling);
640 pass.dispatch(int3(shadow_setup_dispatch_size, 1, 1));
641 pass.barrier(GPU_BARRIER_SHADER_STORAGE);
642}
643
644void LightModule::debug_pass_sync()
645{
646 if (inst_.debug_mode == eDebugMode::DEBUG_LIGHT_CULLING) {
647 debug_draw_ps_.init();
648 debug_draw_ps_.state_set(DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_CUSTOM);
649 debug_draw_ps_.shader_set(inst_.shaders.static_shader_get(LIGHT_CULLING_DEBUG));
650 debug_draw_ps_.bind_resources(inst_.uniform_data);
651 debug_draw_ps_.bind_resources(inst_.hiz_buffer.front);
652 debug_draw_ps_.bind_ssbo("light_buf", &culling_light_buf_);
653 debug_draw_ps_.bind_ssbo("light_cull_buf", &culling_data_buf_);
654 debug_draw_ps_.bind_ssbo("light_zbin_buf", &culling_zbin_buf_);
655 debug_draw_ps_.bind_ssbo("light_tile_buf", &culling_tile_buf_);
656 debug_draw_ps_.bind_texture("depth_tx", &inst_.render_buffers.depth_tx);
657 debug_draw_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
658 }
659}
660
661void LightModule::set_view(View &view, const int2 extent)
662{
663 float far_z = view.far_clip();
664 float near_z = view.near_clip();
665
666 culling_data_buf_.zbin_scale = -CULLING_ZBIN_COUNT / fabsf(far_z - near_z);
667 culling_data_buf_.zbin_bias = -near_z * culling_data_buf_.zbin_scale;
668 culling_data_buf_.tile_to_uv_fac = (culling_data_buf_.tile_size / float2(extent));
669 culling_data_buf_.visible_count = 0;
670 culling_data_buf_.view_is_flipped = view.is_inverted();
671 culling_data_buf_.push_update();
672
673 inst_.manager->submit(culling_ps_, view);
674 inst_.manager->submit(update_ps_, view);
675}
676
678{
679 if (inst_.debug_mode == eDebugMode::DEBUG_LIGHT_CULLING) {
680 inst_.info_append("Debug Mode: Light Culling Validation");
681 inst_.hiz_buffer.update();
682 GPU_framebuffer_bind(view_fb);
683 inst_.manager->submit(debug_draw_ps_, view);
684 }
685}
686
688{
689 GPU_framebuffer_bind(view_fb);
690 inst_.manager->submit(shape_display_ps_, view);
691}
692
694
695} // namespace blender::eevee
General operations, lookup, etc. for blender lights.
#define M_1_PI
#define DEG2RADF(_deg)
#define M_PI_2
#define M_PI
#define BLI_assert_unreachable()
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
btScalar max
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
uint64_t get_light_set_membership() const
uint64_t get_shadow_set_membership() const
float4x4 object_to_world(int instance_index) const
bool is_inverted(int view_id=0) const
Definition draw_view.hh:99
float near_clip(int view_id=0) const
Definition draw_view.hh:115
float far_clip(int view_id=0) const
Definition draw_view.hh:106
void draw_procedural(GPUPrimType primitive, uint instance_len, uint vertex_len, uint vertex_first=-1, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:976
void bind_resources(U &resources)
Definition draw_pass.hh:459
void shader_set(gpu::Shader *shader)
PassBase< DrawCommandBufType > & sub(const char *name)
Definition draw_pass.hh:701
void state_set(DRWState state, int clip_plane_count=0)
struct blender::eevee::Film::DepthState depth
UniformDataModule uniform_data
void sync_light(const ObjectRef &ob_ref)
void set_view(View &view, const int2 extent)
void shape_display_draw(View &view, gpu::FrameBuffer *view_fb)
void debug_draw(View &view, gpu::FrameBuffer *view_fb)
gpu::Shader * static_shader_get(eShaderType shader_type)
const ShadowSceneData & get_data()
Pool< ShadowDirectional > directional_pool
Pool< ShadowPunctual > punctual_pool
struct blender::eevee::VolumeModule::@235034362354336005016350043217325022302275164354 result
float sun_shadow_jitter_overblur()
float sun_shadow_max_resolution()
UniformArrayBuffer< LightData, 2 > sunlight
Simple API to draw debug shapes and log in the viewport.
#define LIGHT_CHUNK
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
constexpr T clamp(T, U, U) RET
float square(float v)
float3x3 scale(float3x3 mat, float2 scale)
float3x3 normalize_and_get_size(float3x3 mat, float3 &r_size)
float safe_rcp(float a)
float reduce_max(float2 a)
void drw_debug_sphere(const float3 center, float radius, const float4 color, const uint lifetime)
static uint2 uint64_to_uint2(uint64_t data)
static eLightType to_light_type(short blender_light_type, short blender_area_type, bool use_soft_falloff)
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T max(const T &a, const T &b)
MINLINE float min_ff(float a, float b)
VecBase< uint32_t, 2 > uint2
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
bool assign_if_different(T &old_value, T &&new_value)
MINLINE int max_ii(int a, int b)
float BKE_light_power(const Light &light)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
T & DRW_object_get_data_for_drawing(const Object &object)
BMesh const char void * data
MINLINE void negate_v3(float r[3])
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
@ DRW_STATE_CLIP_CONTROL_UNIT_RANGE
Definition draw_state.hh:71
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_CUSTOM
Definition draw_state.hh:65
@ DRW_STATE_BLEND_ADD
Definition draw_state.hh:53
float3 BKE_light_color(const Light &light)
MINLINE float max_ff(float a, float b)
MINLINE uint divide_ceil_u(uint a, uint b)
float BKE_light_area(const Light &light, const float4x4 &object_to_world)
MINLINE uint ceil_to_multiple_u(uint a, uint b)
@ LA_CUSTOM_ATTENUATION
@ LA_SHAD_RES_ABSOLUTE
@ LA_USE_SOFT_FALLOFF
VecBase< float, 3 > float3
MINLINE int min_ii(int a, int b)
MINLINE int float_as_int(float f)
#define fabsf
#define sqrtf
#define tanf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
eObject_VisibilityFlag visibility_flag
void sync(ShadowModule &shadows, float4x4 object_to_world, char visibility_flag, const blender::Light *la, uint64_t light_set_membership, uint64_t shadow_set_membership, float threshold)
void shadow_discard_safe(ShadowModule &shadows)
ShadowDirectional * directional
void shadow_ensure(ShadowModule &shadows)
ShadowPunctual * punctual