diff options
Diffstat (limited to 'servers/rendering/renderer_rd/cluster_builder_rd.h')
-rw-r--r-- | servers/rendering/renderer_rd/cluster_builder_rd.h | 95 |
1 files changed, 54 insertions, 41 deletions
diff --git a/servers/rendering/renderer_rd/cluster_builder_rd.h b/servers/rendering/renderer_rd/cluster_builder_rd.h index 0b20a5d7ee..a13e6c8172 100644 --- a/servers/rendering/renderer_rd/cluster_builder_rd.h +++ b/servers/rendering/renderer_rd/cluster_builder_rd.h @@ -43,13 +43,13 @@ class ClusterBuilderSharedDataRD { RID sphere_vertex_array; RID sphere_index_buffer; RID sphere_index_array; - float sphere_overfit = 0.0; //because an icosphere is not a perfect sphere, we need to enlarge it to cover the sphere area + float sphere_overfit = 0.0; // Because an icosphere is not a perfect sphere, we need to enlarge it to cover the sphere area. RID cone_vertex_buffer; RID cone_vertex_array; RID cone_index_buffer; RID cone_index_array; - float cone_overfit = 0.0; //because an cone mesh is not a perfect sphere, we need to enlarge it to cover the actual cone area + float cone_overfit = 0.0; // Because an cone mesh is not a perfect cone, we need to enlarge it to cover the actual cone area. RID box_vertex_buffer; RID box_vertex_array; @@ -73,6 +73,7 @@ class ClusterBuilderSharedDataRD { ClusterRenderShaderRD cluster_render_shader; RID shader_version; RID shader; + enum PipelineVersion { PIPELINE_NORMAL, PIPELINE_MSAA, @@ -85,10 +86,11 @@ class ClusterBuilderSharedDataRD { struct ClusterStore { struct PushConstant { uint32_t cluster_render_data_size; // how much data for a single cluster takes - uint32_t max_render_element_count_div_32; //divided by 32 + uint32_t max_render_element_count_div_32; // divided by 32 uint32_t cluster_screen_size[2]; - uint32_t render_element_count_div_32; //divided by 32 - uint32_t max_cluster_element_count_div_32; //divided by 32 + uint32_t render_element_count_div_32; // divided by 32 + uint32_t max_cluster_element_count_div_32; // divided by 32 + uint32_t pad1; uint32_t pad2; }; @@ -111,6 +113,7 @@ class ClusterBuilderSharedDataRD { uint32_t orthogonal; uint32_t max_cluster_element_count_div_32; + uint32_t pad1; uint32_t pad2; }; @@ -128,6 +131,8 @@ public: class ClusterBuilderRD { public: + static constexpr float WIDE_SPOT_ANGLE_THRESHOLD_DEG = 60.0f; + enum LightType { LIGHT_TYPE_OMNI, LIGHT_TYPE_SPOT @@ -144,21 +149,20 @@ public: ELEMENT_TYPE_DECAL, ELEMENT_TYPE_REFLECTION_PROBE, ELEMENT_TYPE_MAX, - }; private: ClusterBuilderSharedDataRD *shared = nullptr; struct RenderElementData { - uint32_t type; //0-4 + uint32_t type; // 0-4 uint32_t touches_near; uint32_t touches_far; uint32_t original_index; - float transform_inv[12]; //transposed transform for less space + float transform_inv[12]; // Transposed transform for less space. float scale[3]; - uint32_t pad; - }; + uint32_t has_wide_spot_angle; + }; // Keep aligned to 32 bytes. uint32_t cluster_count_by_type[ELEMENT_TYPE_MAX] = {}; uint32_t max_elements_by_type = 0; @@ -172,7 +176,7 @@ private: Projection projection; float z_far = 0; float z_near = 0; - bool orthogonal = false; + bool camera_orthogonal = false; enum Divisor { DIVISOR_1, @@ -188,26 +192,27 @@ private: Size2i cluster_screen_size; RID framebuffer; - RID cluster_render_buffer; //used for creating - RID cluster_buffer; //used for rendering - RID element_buffer; //used for storing, to hint element touches far plane or near plane + RID cluster_render_buffer; // Used for creating. + RID cluster_buffer; // Used for rendering. + RID element_buffer; // Used for storing, to hint element touches far plane or near plane. uint32_t cluster_render_buffer_size = 0; uint32_t cluster_buffer_size = 0; RID cluster_render_uniform_set; RID cluster_store_uniform_set; - //persistent data + // Persistent data. void _clear(); struct StateUniform { float projection[16]; float inv_z_far; - uint32_t screen_to_clusters_shift; // shift to obtain coordinates in block indices - uint32_t cluster_screen_width; // - uint32_t cluster_data_size; // how much data for a single cluster takes + uint32_t screen_to_clusters_shift; // Shift to obtain coordinates in block indices. + uint32_t cluster_screen_width; + uint32_t cluster_data_size; // How much data is needed for a single cluster. uint32_t cluster_depth_offset; + uint32_t pad0; uint32_t pad1; uint32_t pad2; @@ -224,10 +229,10 @@ public: _FORCE_INLINE_ void add_light(LightType p_type, const Transform3D &p_transform, float p_radius, float p_spot_aperture) { if (p_type == LIGHT_TYPE_OMNI && cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT] == max_elements_by_type) { - return; //max number elements reached + return; // Max number elements reached. } if (p_type == LIGHT_TYPE_SPOT && cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT] == max_elements_by_type) { - return; //max number elements reached + return; // Max number elements reached. } RenderElementData &e = render_elements[render_element_count]; @@ -242,15 +247,14 @@ public: radius *= p_radius; if (p_type == LIGHT_TYPE_OMNI) { - radius *= shared->sphere_overfit; // overfit icosphere + radius *= shared->sphere_overfit; // Overfit icosphere. - //omni float depth = -xform.origin.z; - if (orthogonal) { + if (camera_orthogonal) { e.touches_near = (depth - radius) < z_near; } else { - //contains camera inside light - float radius2 = radius * shared->sphere_overfit; // overfit again for outer size (camera may be outside actual sphere but behind an icosphere vertex) + // Contains camera inside light. + float radius2 = radius * shared->sphere_overfit; // Overfit again for outer size (camera may be outside actual sphere but behind an icosphere vertex) e.touches_near = xform.origin.length_squared() < radius2 * radius2; } @@ -265,12 +269,11 @@ public: cluster_count_by_type[ELEMENT_TYPE_OMNI_LIGHT]++; - } else { - //spot - radius *= shared->cone_overfit; // overfit icosphere + } else /*LIGHT_TYPE_SPOT */ { + radius *= shared->cone_overfit; // Overfit icosphere real_t len = Math::tan(Math::deg_to_rad(p_spot_aperture)) * radius; - //approximate, probably better to use a cone support function + // Approximate, probably better to use a cone support function. float max_d = -1e20; float min_d = 1e20; #define CONE_MINMAX(m_x, m_y) \ @@ -285,14 +288,13 @@ public: CONE_MINMAX(-1, -1); CONE_MINMAX(1, -1); - if (orthogonal) { + if (camera_orthogonal) { e.touches_near = min_d < z_near; } else { - //contains camera inside light Plane base_plane(-xform.basis.get_column(Vector3::AXIS_Z), xform.origin); float dist = base_plane.distance_to(Vector3()); if (dist >= 0 && dist < radius) { - //inside, check angle + // Contains camera inside light, check angle. float angle = Math::rad_to_deg(Math::acos((-xform.origin.normalized()).dot(-xform.basis.get_column(Vector3::AXIS_Z)))); e.touches_near = angle < p_spot_aperture * 1.05; //overfit aperture a little due to cone overfit } else { @@ -302,12 +304,23 @@ public: e.touches_far = max_d > z_far; - e.scale[0] = len * shared->cone_overfit; - e.scale[1] = len * shared->cone_overfit; - e.scale[2] = radius; + // If the spot angle is above the threshold, use a sphere instead of a cone for building the clusters + // since the cone gets too flat/large (spot angle close to 90 degrees) or + // can't even cover the affected area of the light (spot angle above 90 degrees). + if (p_spot_aperture > WIDE_SPOT_ANGLE_THRESHOLD_DEG) { + e.scale[0] = radius; + e.scale[1] = radius; + e.scale[2] = radius; + e.has_wide_spot_angle = true; + } else { + e.scale[0] = len * shared->cone_overfit; + e.scale[1] = len * shared->cone_overfit; + e.scale[2] = radius; + e.has_wide_spot_angle = false; + } e.type = ELEMENT_TYPE_SPOT_LIGHT; - e.original_index = cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT]; //use omni since they share index + e.original_index = cluster_count_by_type[ELEMENT_TYPE_SPOT_LIGHT]; // Use omni light since they share index. RendererRD::MaterialStorage::store_transform_transposed_3x4(xform, e.transform_inv); @@ -319,16 +332,16 @@ public: _FORCE_INLINE_ void add_box(BoxType p_box_type, const Transform3D &p_transform, const Vector3 &p_half_extents) { if (p_box_type == BOX_TYPE_DECAL && cluster_count_by_type[ELEMENT_TYPE_DECAL] == max_elements_by_type) { - return; //max number elements reached + return; // Max number elements reached. } if (p_box_type == BOX_TYPE_REFLECTION_PROBE && cluster_count_by_type[ELEMENT_TYPE_REFLECTION_PROBE] == max_elements_by_type) { - return; //max number elements reached + return; // Max number elements reached. } RenderElementData &e = render_elements[render_element_count]; Transform3D xform = view_xform * p_transform; - //extract scale and scale the matrix by it, makes things simpler + // Extract scale and scale the matrix by it, makes things simpler. Vector3 scale = p_half_extents; for (uint32_t i = 0; i < 3; i++) { float s = xform.basis.rows[i].length(); @@ -339,10 +352,10 @@ public: float box_depth = Math::abs(xform.basis.xform_inv(Vector3(0, 0, -1)).dot(scale)); float depth = -xform.origin.z; - if (orthogonal) { + if (camera_orthogonal) { e.touches_near = depth - box_depth < z_near; } else { - //contains camera inside box + // Contains camera inside box. Vector3 inside = xform.xform_inv(Vector3(0, 0, 0)).abs(); e.touches_near = inside.x < scale.x && inside.y < scale.y && inside.z < scale.z; } |