29void ShadowPass::ShadowView::setup(
View &view,
float3 light_direction,
bool force_fail_method)
31 force_fail_method_ = force_fail_method;
32 light_direction_ = light_direction;
33 sync(view.viewmat(), view.winmat());
62 const int3 corner_faces[8] = {
63 {x_neg, y_neg, z_pos},
64 {x_neg, y_neg, z_neg},
65 {x_neg, y_pos, z_neg},
66 {x_neg, y_pos, z_pos},
67 {x_pos, y_neg, z_pos},
68 {x_pos, y_neg, z_neg},
69 {x_pos, y_pos, z_neg},
70 {x_pos, y_pos, z_pos},
73 const int2 edge_faces[12] = {
88 const int2 edge_corners[12] = {
107 for (
float3 corner : frustum_corners) {
108 frustum_center += corner /
float(frustum_corners.size());
111 Vector<float4> faces_result;
112 Vector<float3> corners_result;
119 frustum_planes[i] *=
float4(-1, -1, -1, 1);
121 face_lit[i] =
math::dot(
float3(frustum_planes[i]), light_direction_) < 0;
123 faces_result.append(frustum_planes[i]);
130 int2 f = edge_faces[i];
131 bool a_lit = face_lit[f[0]];
132 bool b_lit = face_lit[f[1]];
133 if (a_lit != b_lit) {
135 float3 corner_a = frustum_corners[edge_corners[i][0]];
136 float3 corner_b = frustum_corners[edge_corners[i][1]];
140 if (
dot(corner_a - frustum_center, normal) < 0.0f) {
145 faces_result.append(extruded_face);
152 lit_faces += face_lit[corner_faces[i_corner][i_face]] ? 1 : 0;
156 corners_result.append(frustum_corners[i_corner]);
160 corners_result.append(
float3(frustum_corners[i_corner]) - (light_direction_ * 1e4f));
165 for (
int i : corners_result.index_range()) {
166 extruded_frustum_.corners[i] =
float4(corners_result[i], 1);
168 extruded_frustum_.corners_count = corners_result.size();
170 for (
int i : faces_result.index_range()) {
171 extruded_frustum_.planes[i] = faces_result[i];
173 extruded_frustum_.planes_count = faces_result.size();
175 extruded_frustum_.push_update();
178bool ShadowPass::ShadowView::debug_object_culling(Object *ob)
183 for (
int p :
IndexRange(extruded_frustum_.planes_count)) {
184 float4 plane = extruded_frustum_.planes[p];
185 bool separating_axis =
true;
186 for (
float3 corner : corners) {
189 if (signed_distance <= 0) {
190 separating_axis =
false;
194 if (separating_axis) {
195 printf(
"Separating Axis >>> x: %f, y: %f, z: %f, w: %f \n",
UNPACK4(plane));
202void ShadowPass::ShadowView::set_mode(ShadowPass::PassType type)
204 current_pass_type_ = type;
206 manager_fingerprint_ = 0;
209void ShadowPass::ShadowView::compute_visibility(
ObjectBoundsBuf &bounds,
218 uint word_per_draw = this->visibility_word_per_draw();
221 resource_len * word_per_draw;
225 if (current_pass_type_ == ShadowPass::PASS) {
227 pass_visibility_buf_.resize(words_len);
229 fail_visibility_buf_.resize(words_len);
232 else if (current_pass_type_ == ShadowPass::FAIL) {
238 visibility_buf_.resize(words_len);
242 if (do_visibility_) {
244 gpu::Shader *shader = current_pass_type_ == ShadowPass::FORCED_FAIL ?
255 if (current_pass_type_ == ShadowPass::FORCED_FAIL) {
274 switch (current_pass_type_) {
275 case ShadowPass::PASS:
276 return pass_visibility_buf_;
277 case ShadowPass::FAIL:
278 return fail_visibility_buf_;
279 case ShadowPass::FORCED_FAIL:
280 return visibility_buf_;
284 return visibility_buf_;
287PassMain::Sub *&ShadowPass::get_pass_ptr(PassType type,
bool manifold,
bool cap )
289 return passes_[type][manifold][cap];
296 needs_rt_update_ = use_raytracing_ && (!shadow_as_ || scene_state.
updated);
297 if (!use_raytracing_) {
298 shadow_as_ =
nullptr;
300 else if (needs_rt_update_) {
313 std::swap(direction_ws.y, direction_ws.z);
314 direction_ws *=
float3(-1, 1, -1);
318 pass_data_.light_direction_ws = direction_ws;
319 pass_data_.far_plane = planes[2] *
float4(-1, -1, -1, 1);
320 pass_data_.push_update();
341#if DEBUG_SHADOW_VOLUME
351 if (use_raytracing_) {
355 raytrace_ps_.state_stencil(0xFF, 0xFF, 0xFF);
357 raytrace_ps_.bind_texture(
"depth_tx", &resources.
depth_tx);
358 raytrace_ps_.bind_texture(
"normal_tx", &gbuffer_normal_ref);
359 raytrace_ps_.bind_ubo(
"pass_data", pass_data_);
360 raytrace_ps_.bind_tlas(
"shadow_as", shadow_as_.get());
367 pass_ps_.state_set(depth_pass_state);
368 pass_ps_.state_stencil(0xFF, 0xFF, 0xFF);
371 fail_ps_.state_set(depth_fail_state);
372 fail_ps_.state_stencil(0xFF, 0xFF, 0xFF);
374 forced_fail_ps_.init();
375 forced_fail_ps_.state_set(depth_fail_state);
376 forced_fail_ps_.state_stencil(0xFF, 0xFF, 0xFF);
379 for (
bool manifold : {
false,
true}) {
381 ps = &pass_ps_.
sub(manifold ?
"manifold" :
"non_manifold");
383 ps->
bind_ubo(
"pass_data", pass_data_);
385 for (PassType fail_type : {FAIL, FORCED_FAIL}) {
386 PassMain &ps_main = fail_type == FAIL ? fail_ps_ : forced_fail_ps_;
388 PassMain::Sub *&ps = get_pass_ptr(fail_type, manifold,
false);
389 ps = &ps_main.
sub(manifold ?
"NoCaps.manifold" :
"NoCaps.non_manifold");
391 ps->
bind_ubo(
"pass_data", pass_data_);
393 PassMain::Sub *&caps_ps = get_pass_ptr(fail_type, manifold,
true);
394 caps_ps = &ps_main.
sub(manifold ?
"Caps.manifold" :
"Caps.non_manifold");
396 caps_ps->
bind_ubo(
"pass_data", pass_data_);
404 const bool has_transp_mat)
411 if (use_raytracing_) {
412 if (needs_rt_update_) {
424 if (geom_shadow ==
nullptr) {
428#define DEBUG_CULLING 0
430 View view =
View(
"View", DRW_view_default_get());
431 ShadowView shadow_view = ShadowView(
"ShadowView", view, pass_data_.light_direction_ws);
433 "%s culling : %s\n", ob->
id.
name, shadow_view.debug_object_culling(ob) ?
"true" :
"false");
438 bool force_fail_pass = has_transp_mat || (!is_manifold && (scene_state.
cull_state != 0));
440 PassType fail_type = force_fail_pass ? FORCED_FAIL : FAIL;
446 int tri_len = is_manifold ? 2 : 4;
448 if (!force_fail_pass) {
450 ps.
draw_expand(geom_shadow, prim, tri_len, 1, handle);
455 get_pass_ptr(fail_type, is_manifold,
true)->draw_expand(geom_faces, prim, 2, 1, handle);
457 get_pass_ptr(fail_type, is_manifold,
false)->draw_expand(geom_shadow, prim, tri_len, 1, handle);
462 if (use_raytracing_) {
473 bool force_fail_method)
479 if (use_raytracing_) {
480 gbuffer_normal_ref = &normal_tx;
482 pass_data_.push_update();
485 manager.
submit(raytrace_ps_, view);
493 view_.setup(view, pass_data_.light_direction_ws, force_fail_method);
495 view_.set_mode(PASS);
496 manager.
submit(pass_ps_, view_);
497 view_.set_mode(FAIL);
498 manager.
submit(fail_ps_, view_);
499 view_.set_mode(FORCED_FAIL);
500 manager.
submit(forced_fail_ps_, view_);
General operations, lookup, etc. for blender objects.
#define GPU_ATTACHMENT_TEXTURE(_texture)
blender::gpu::TopLevelAS * GPU_ray_tracing_tlas_alloc(const char *name)
#define BLI_assert_unreachable()
constexpr IndexRange()=default
void submit(PassSimple &pass, View &view)
int instances_count() const
float4x4 object_to_world(int instance_index) const
View(const char *name, int view_len=1, bool procedural=false)
static View & default_get()
const float4x4 & viewmat(int view_id=0) const
static float screen_pixel_radius(const float4x4 &wininv, bool is_perspective, const int2 &extent)
virtual VisibilityBuf & get_visibility_buffer()
std::array< float3, 8 > frustum_corners_get(int view_id=0) const
std::array< float4, 6 > frustum_planes_get(int view_id=0) const
void sync(const float4x4 &view_mat, const float4x4 &win_mat, int view_id=0)
void shader_set(gpu::Shader *shader)
PassBase< DrawCommandBufType > & sub(const char *name)
void draw_expand(gpu::Batch *batch, GPUPrimType primitive_type, uint primitive_len, uint instance_len, uint vertex_len, uint vertex_first, ResourceIDRange res_id={}, uint custom_id=0)
void bind_ubo(const char *name, gpu::UniformBuf *buffer)
detail::PassBase< command::DrawMultiBuf > Sub
StaticShader shadow_visibility_static
StaticShader shadow_visibility_dynamic
static ShaderCache & get()
void init(const SceneState &scene_state, SceneResources &resources)
void object_sync(SceneState &scene_state, ObjectRef &ob_ref, ResourceHandleRange handle, const bool has_transp_mat)
void draw(Manager &manager, View &view, SceneResources &resources, gpu::Texture &depth_stencil_tx, gpu::Texture &normal_tx, int2 resolution, bool force_fail_method)
void sync(SceneResources &resources)
#define DRW_VISIBILITY_GROUP_SIZE
#define DRW_VIEW_UBO_SLOT
float dot(VecOp< float, D >, VecOp< float, D >) RET
std::array< VecBase< T, 3 >, 8 > corners(const Bounds< VecBase< T, 3 > > &bounds)
gpu::BottomLevelAS * DRW_cache_object_surface_blas_get(Object *ob)
detail::Pass< command::DrawMultiBuf > PassMain
gpu::Batch * DRW_cache_object_surface_get(Object *ob)
StorageArrayBuffer< ObjectBounds, 128 > ObjectBoundsBuf
gpu::Batch * DRW_cache_object_edge_detection_get(Object *ob, bool *r_is_manifold)
StorageArrayBuffer< ObjectInfos, 128 > ObjectInfosBuf
StorageArrayBuffer< uint, 4, true > VisibilityBuf
std::unique_ptr< TopLevelAS, TopLevelASDeleter > TopLevelASPtr
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > transform_direction(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
MINLINE int max_ii(int a, int b)
MatBase< float, 4, 4 > float4x4
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
int GPU_shader_get_ssbo_binding(gpu::Shader *shader, const char *name)
void GPU_storagebuf_clear(gpu::StorageBuf *ssbo, uint32_t clear_value)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
void GPU_memory_barrier(GPUBarrier barrier)
BMesh const char void * data
void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3])
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
@ GPU_BARRIER_SHADER_STORAGE
@ DRW_STATE_BLEND_ADD_FULL
@ DRW_STATE_STENCIL_ALWAYS
@ DRW_STATE_WRITE_STENCIL
@ DRW_STATE_DEPTH_GREATER_EQUAL
@ DRW_STATE_WRITE_STENCIL_SHADOW_FAIL
@ DRW_STATE_WRITE_STENCIL_SHADOW_PASS
void GPU_storagebuf_bind(gpu::StorageBuf *ssbo, int slot)
void GPU_uniformbuf_bind(gpu::UniformBuf *ubo, int slot)
void GPU_debug_group_end()
MINLINE uint divide_ceil_u(uint a, uint b)
int GPU_shader_get_ubo_binding(gpu::Shader *shader, const char *name)
std::optional< Bounds< float3 > > BKE_object_boundbox_get(const Object *ob)
MINLINE uint ceil_to_multiple_u(uint a, uint b)
VecBase< float, 3 > float3
void GPU_debug_group_begin(const char *name)
MINLINE float clamp_f(float value, float min, float max)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float4 shadow_direction_vs
struct SceneDisplay display
UniformBuffer< WorldData > world_buf
#define DEBUG_SHADOW_VOLUME