35 short blender_area_type,
36 bool use_soft_falloff)
38 switch (blender_light_type) {
92 const bool diffuse_visibility = (visibility_flag &
OB_HIDE_DIFFUSE) == 0;
93 const bool glossy_visibility = (visibility_flag &
OB_HIDE_GLOSSY) == 0;
97 this->
shape_power = base_power * shape_radiance_get();
98 this->
point_power = base_power * point_radiance_get();
105 this->
lod_min = shadow_lod_min_get(la);
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;
155 else if (this->
punctual ==
nullptr) {
161 float light_threshold,
170 return sqrtf(light_power / light_threshold);
176 const float threshold,
177 const bool use_jitter)
182 if (is_local_light(this->
type)) {
186 const float surface_max_power =
max(
max(la->diff_fac, la->spec_fac), la->transmission_fac) *
188 const float volume_max_power = la->volume_fac * max_power;
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);
202 la->shadow_jitter_overblur / 100.0f :
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;
209 l_sun.
shadow_angle = sun_half_angle * trace_scaling_fac;
219 else if (is_area_light(this->
type)) {
220 LightAreaData &l_area = this->area;
222 l_area.
size =
float2(la->area_size, is_irregular ? la->area_sizey : la->area_size);
230 if (l_area.
size.x * l_area.
size.y < 0.00001f) {
239 else if (is_point_light(this->
type)) {
240 LightSpotData &l_spot = this->spot;
241 LightLocalData &l_local = this->local;
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;
272float Light::shape_radiance_get()
274 using namespace blender::math;
277 switch (this->
type) {
281 float area = this->area.size.x * this->area.size.y * 4.0f;
294 float area =
float(4.0f *
M_PI) *
square(this->local.local.shape_radius);
300 float inv_sin_sq = 1.0f + 1.0f /
square(this->sun.shape_radius);
309float Light::point_radiance_get()
312 switch (this->
type) {
317 float area = this->area.size.x * this->area.size.y * 4.0f;
320 float mrp_scaling = tmp + (1.0f - tmp) *
M_1_PI;
343 this->local.local.influence_radius_max,
344 float4(0.8f, 0.3f, 0.0f, 1.0f));
356 for (
Light &light : light_map_.values()) {
357 light.shadow_discard_safe(inst_.shadows);
361void LightModule::add_world_sun_light(
const ObjectKey &key,
bool use_diffuse,
bool use_glossy)
368 la.r = la.g = la.b = -1.0f;
376 int visibility_flag = 0;
381 Light &light = light_map_.lookup_or_add_default(key);
388 draw::LIGHT_LINK_MASK_ALL,
389 draw::LIGHT_LINK_MASK_ALL,
392 sun_lights_len_ += 1;
398 if (inst_.is_viewport()) {
400 inst_.sampling.reset();
406 use_sun_lights_ = (inst_.world.has_volume_absorption() ==
false);
411 for (
Light &light : light_map_.values()) {
419 local_lights_len_ = 0;
421 if (use_sun_lights_ && inst_.world.sun_threshold() > 0.0f) {
422 if (inst_.pipelines.world.use_lightpath_node()) {
435 if (use_scene_lights_ ==
false) {
439 if (use_sun_lights_ ==
false) {
445 Light &light = light_map_.lookup_or_add_default(
ObjectKey(ob_ref));
448 inst_.manager->extract_all_object_attributes(handle, ob_ref);
450 if (inst_.get_recalc_flags(ob_ref) != 0 || !light.
initialized) {
452 light.
sync(inst_.shadows,
460 sun_lights_len_ += int(is_sun_light(light.
type));
461 local_lights_len_ += int(!is_sun_light(light.
type));
472 light_buf_.resize(lights_allocated);
476 int sun_lights_idx = 0;
477 int local_lights_idx = sun_lights_len_;
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;
484 if (!light.
used && !inst_.is_baking()) {
485 light_map_.remove(it);
489 int dst_idx = is_sun_light(light.
type) ? sun_lights_idx++ : local_lights_idx++;
491 light_buf_[dst_idx] = light;
497 light_buf_.push_update();
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.");
505 lights_len_ = sun_lights_len_ + local_lights_len_;
509 culling_key_buf_.resize(lights_allocated);
510 culling_zdist_buf_.resize(lights_allocated);
511 culling_light_buf_.resize(lights_allocated);
514 int2 render_extent = inst_.film.render_extent_get();
515 int2 probe_extent =
int2(inst_.sphere_probes.probe_render_extent());
523 bool tile_size_valid =
false;
527 uint tile_count = tiles_extent.x * tiles_extent.y;
528 if (tile_count > max_tile_count_threshold) {
531 total_word_count_ = tile_count * word_per_tile;
532 tile_size_valid =
true;
534 }
while (total_word_count_ > max_word_count_threshold || !tile_size_valid);
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_;
546 culling_tile_buf_.resize(total_word_count_);
550 shape_display_pass_sync();
554void LightModule::shape_display_pass_sync()
556 shape_display_ps_.
init();
558 if (lights_len_ == 0) {
571void LightModule::culling_pass_sync()
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);
582 auto &sub = culling_ps_.
sub(
"Select");
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));
594 auto &sub = culling_ps_.sub(
"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));
605 auto &sub = culling_ps_.sub(
"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));
614 auto &sub = culling_ps_.sub(
"Tiles");
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));
624void LightModule::update_pass_sync()
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);
631 auto &pass = update_ps_;
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));
644void LightModule::debug_pass_sync()
647 debug_draw_ps_.init();
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);
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();
673 inst_.manager->submit(culling_ps_, view);
674 inst_.manager->submit(update_ps_, view);
680 inst_.info_append(
"Debug Mode: Light Culling Validation");
681 inst_.hiz_buffer.update();
683 inst_.manager->submit(debug_draw_ps_, view);
690 inst_.manager->submit(shape_display_ps_, view);
General operations, lookup, etc. for blender lights.
#define BLI_assert_unreachable()
#define SET_FLAG_FROM_TEST(value, test, flag)
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
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
float near_clip(int view_id=0) const
float far_clip(int view_id=0) const
void draw_procedural(GPUPrimType primitive, uint instance_len, uint vertex_len, uint vertex_first=-1, ResourceIDRange res_id={}, uint custom_id=0)
void bind_resources(U &resources)
void shader_set(gpu::Shader *shader)
PassBase< DrawCommandBufType > & sub(const char *name)
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()
float global_lod_bias() const
Pool< ShadowDirectional > directional_pool
Pool< ShadowPunctual > punctual_pool
struct blender::eevee::VolumeModule::@235034362354336005016350043217325022302275164354 result
bool use_sun_shadow_jitter()
float sun_shadow_filter_radius()
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.
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
float3x3 scale(float3x3 mat, float2 scale)
float3x3 normalize_and_get_size(float3x3 mat, float3 &r_size)
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)
@ LIGHT_USE_SHADOW_JITTER
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
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
@ DRW_STATE_CLIP_CONTROL_UNIT_RANGE
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)
VecBase< float, 3 > float3
MINLINE int min_ii(int a, int b)
MINLINE int float_as_int(float f)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static MatBase identity()
eObject_VisibilityFlag visibility_flag
struct LightLocalCommon local
struct Transform object_to_world
uint2 light_set_membership
float influence_radius_invsqr_volume
packed_float3 shadow_position
float influence_radius_max
float influence_radius_invsqr_surface
struct LightLocalCommon local
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)
uint2 shadow_set_membership
ShadowPunctual * punctual