Skip to content

Commit 6434c3d

Browse files
committed
LightmapGI: Search for shadowmask light index only after sorting the lights
1 parent 978b387 commit 6434c3d

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

modules/lightmapper_rd/lightmapper_rd.cpp

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ void LightmapperRD::add_directional_light(const String &p_name, bool p_static, c
7979
l.size = Math::tan(Math::deg_to_rad(p_angular_distance));
8080
l.shadow_blur = p_shadow_blur;
8181
lights.push_back(l);
82-
light_names.push_back(p_name);
82+
83+
LightMetadata md;
84+
md.name = p_name;
85+
md.type = LIGHT_TYPE_DIRECTIONAL;
86+
light_metadata.push_back(md);
8387
}
8488

8589
void LightmapperRD::add_omni_light(const String &p_name, bool p_static, const Vector3 &p_position, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_size, float p_shadow_blur) {
@@ -99,7 +103,11 @@ void LightmapperRD::add_omni_light(const String &p_name, bool p_static, const Ve
99103
l.size = p_size;
100104
l.shadow_blur = p_shadow_blur;
101105
lights.push_back(l);
102-
light_names.push_back(p_name);
106+
107+
LightMetadata md;
108+
md.name = p_name;
109+
md.type = LIGHT_TYPE_OMNI;
110+
light_metadata.push_back(md);
103111
}
104112

105113
void LightmapperRD::add_spot_light(const String &p_name, bool p_static, const Vector3 &p_position, const Vector3 p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_spot_angle, float p_spot_attenuation, float p_size, float p_shadow_blur) {
@@ -124,7 +132,11 @@ void LightmapperRD::add_spot_light(const String &p_name, bool p_static, const Ve
124132
l.size = p_size;
125133
l.shadow_blur = p_shadow_blur;
126134
lights.push_back(l);
127-
light_names.push_back(p_name);
135+
136+
LightMetadata md;
137+
md.name = p_name;
138+
md.type = LIGHT_TYPE_SPOT;
139+
light_metadata.push_back(md);
128140
}
129141

130142
void LightmapperRD::add_probe(const Vector3 &p_position) {
@@ -620,6 +632,7 @@ void LightmapperRD::_create_acceleration_structures(RenderingDevice *rd, Size2i
620632
/*****************************/
621633

622634
lights.sort();
635+
light_metadata.sort();
623636

624637
Vector<Vector2i> seam_buffer_vec;
625638
seam_buffer_vec.resize(seams.size() * 2);
@@ -1083,32 +1096,19 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
10831096
return bake_error;
10841097
}
10851098

1086-
// The index of the directional light used for shadowmasking.
1087-
int shadowmask_light_idx = -1;
1088-
1089-
// If there are no valid directional lights for shadowmasking, the entire
1090-
// scene would be shadowed and this saves baking time.
1099+
// Find any directional light suitable for shadowmasking.
10911100
if (p_bake_shadowmask) {
1092-
int shadowmask_lights_count = 0;
1093-
1101+
bool found = false;
10941102
for (int i = 0; i < lights.size(); i++) {
10951103
if (lights[i].type == LightType::LIGHT_TYPE_DIRECTIONAL && !lights[i].static_bake) {
1096-
if (shadowmask_light_idx < 0) {
1097-
shadowmask_light_idx = i;
1098-
}
1099-
1100-
shadowmask_lights_count += 1;
1104+
found = true;
1105+
break;
11011106
}
11021107
}
11031108

1104-
if (shadowmask_light_idx < 0) {
1109+
if (!found) {
11051110
p_bake_shadowmask = false;
11061111
WARN_PRINT("Shadowmask disabled: no directional light with their bake mode set to dynamic exists.");
1107-
1108-
} else if (shadowmask_lights_count > 1) {
1109-
WARN_PRINT(
1110-
vformat("%d directional lights detected for shadowmask baking. Only %s will be used.",
1111-
shadowmask_lights_count, light_names[shadowmask_light_idx]));
11121112
}
11131113
}
11141114

@@ -1300,6 +1300,29 @@ LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_d
13001300
const uint32_t cluster_size = 16;
13011301
_create_acceleration_structures(rd, atlas_size, atlas_slices, bounds, grid_size, cluster_size, probe_positions, p_generate_probes, slice_triangle_count, slice_seam_count, vertex_buffer, triangle_buffer, lights_buffer, triangle_indices_buffer, cluster_indices_buffer, cluster_aabbs_buffer, probe_positions_buffer, grid_texture, seams_buffer, p_step_function, p_bake_userdata);
13021302

1303+
// The index of the directional light used for shadowmasking.
1304+
int shadowmask_light_idx = -1;
1305+
1306+
// Find the directional light index in the sorted lights array.
1307+
if (p_bake_shadowmask) {
1308+
int shadowmask_lights_count = 0;
1309+
1310+
for (int i = 0; i < lights.size(); i++) {
1311+
if (lights[i].type == LightType::LIGHT_TYPE_DIRECTIONAL && !lights[i].static_bake) {
1312+
if (shadowmask_light_idx < 0) {
1313+
shadowmask_light_idx = i;
1314+
}
1315+
shadowmask_lights_count += 1;
1316+
}
1317+
}
1318+
1319+
if (shadowmask_lights_count > 1) {
1320+
WARN_PRINT(
1321+
vformat("%d directional lights detected for shadowmask baking. Only %s will be used.",
1322+
shadowmask_lights_count, light_metadata[shadowmask_light_idx].name));
1323+
}
1324+
}
1325+
13031326
// Create global bake parameters buffer.
13041327
BakeParameters bake_parameters;
13051328
bake_parameters.world_size[0] = bounds.size.x;

modules/lightmapper_rd/lightmapper_rd.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ class LightmapperRD : public Lightmapper {
8888
}
8989
};
9090

91+
struct LightMetadata {
92+
String name;
93+
uint32_t type = LIGHT_TYPE_DIRECTIONAL;
94+
95+
bool operator<(const LightMetadata &p_light) const {
96+
return type < p_light.type;
97+
}
98+
};
99+
91100
struct Vertex {
92101
float position[3] = {};
93102
float normal_z = 0.0;
@@ -203,7 +212,7 @@ class LightmapperRD : public Lightmapper {
203212
Vector<MeshInstance> mesh_instances;
204213

205214
Vector<Light> lights;
206-
Vector<String> light_names;
215+
Vector<LightMetadata> light_metadata;
207216

208217
struct TriangleSort {
209218
uint32_t cell_index = 0;

0 commit comments

Comments
 (0)