Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/lightmapper_rd/lightmapper_rd.cpp
20884 views
1
/**************************************************************************/
2
/* lightmapper_rd.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "lightmapper_rd.h"
32
33
#include "core/string/print_string.h"
34
#include "lm_blendseams.glsl.gen.h"
35
#include "lm_compute.glsl.gen.h"
36
#include "lm_raster.glsl.gen.h"
37
38
#include "core/config/project_settings.h"
39
#include "core/io/dir_access.h"
40
#include "core/math/geometry_2d.h"
41
#include "core/math/geometry_3d.h"
42
#include "editor/file_system/editor_paths.h"
43
#include "editor/settings/editor_settings.h"
44
#include "servers/rendering/rendering_device_binds.h"
45
#include "servers/rendering/rendering_server_globals.h"
46
47
#if defined(VULKAN_ENABLED)
48
#include "drivers/vulkan/rendering_context_driver_vulkan.h"
49
#endif
50
#if defined(METAL_ENABLED)
51
#include "drivers/metal/rendering_context_driver_metal.h"
52
#endif
53
54
//uncomment this if you want to see textures from all the process saved
55
//#define DEBUG_TEXTURES
56
57
void LightmapperRD::add_mesh(const MeshData &p_mesh) {
58
ERR_FAIL_COND(p_mesh.albedo_on_uv2.is_null() || p_mesh.albedo_on_uv2->is_empty());
59
ERR_FAIL_COND(p_mesh.emission_on_uv2.is_null() || p_mesh.emission_on_uv2->is_empty());
60
ERR_FAIL_COND(p_mesh.albedo_on_uv2->get_width() != p_mesh.emission_on_uv2->get_width());
61
ERR_FAIL_COND(p_mesh.albedo_on_uv2->get_height() != p_mesh.emission_on_uv2->get_height());
62
ERR_FAIL_COND(p_mesh.points.is_empty());
63
MeshInstance mi;
64
mi.data = p_mesh;
65
mesh_instances.push_back(mi);
66
}
67
68
void LightmapperRD::add_directional_light(const String &p_name, bool p_static, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_angular_distance, float p_shadow_blur) {
69
Light l;
70
l.type = LIGHT_TYPE_DIRECTIONAL;
71
l.direction[0] = p_direction.x;
72
l.direction[1] = p_direction.y;
73
l.direction[2] = p_direction.z;
74
l.color[0] = p_color.r;
75
l.color[1] = p_color.g;
76
l.color[2] = p_color.b;
77
l.energy = p_energy;
78
l.indirect_energy = p_indirect_energy;
79
l.static_bake = p_static;
80
l.size = Math::tan(Math::deg_to_rad(p_angular_distance));
81
l.shadow_blur = p_shadow_blur;
82
lights.push_back(l);
83
84
LightMetadata md;
85
md.name = p_name;
86
md.type = LIGHT_TYPE_DIRECTIONAL;
87
light_metadata.push_back(md);
88
}
89
90
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) {
91
Light l;
92
l.type = LIGHT_TYPE_OMNI;
93
l.position[0] = p_position.x;
94
l.position[1] = p_position.y;
95
l.position[2] = p_position.z;
96
l.range = p_range;
97
l.attenuation = p_attenuation;
98
l.color[0] = p_color.r;
99
l.color[1] = p_color.g;
100
l.color[2] = p_color.b;
101
l.energy = p_energy;
102
l.indirect_energy = p_indirect_energy;
103
l.static_bake = p_static;
104
l.size = p_size;
105
l.shadow_blur = p_shadow_blur;
106
lights.push_back(l);
107
108
LightMetadata md;
109
md.name = p_name;
110
md.type = LIGHT_TYPE_OMNI;
111
light_metadata.push_back(md);
112
}
113
114
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) {
115
Light l;
116
l.type = LIGHT_TYPE_SPOT;
117
l.position[0] = p_position.x;
118
l.position[1] = p_position.y;
119
l.position[2] = p_position.z;
120
l.direction[0] = p_direction.x;
121
l.direction[1] = p_direction.y;
122
l.direction[2] = p_direction.z;
123
l.range = p_range;
124
l.attenuation = p_attenuation;
125
l.cos_spot_angle = Math::cos(Math::deg_to_rad(p_spot_angle));
126
l.inv_spot_attenuation = 1.0f / p_spot_attenuation;
127
l.color[0] = p_color.r;
128
l.color[1] = p_color.g;
129
l.color[2] = p_color.b;
130
l.energy = p_energy;
131
l.indirect_energy = p_indirect_energy;
132
l.static_bake = p_static;
133
l.size = p_size;
134
l.shadow_blur = p_shadow_blur;
135
lights.push_back(l);
136
137
LightMetadata md;
138
md.name = p_name;
139
md.type = LIGHT_TYPE_SPOT;
140
light_metadata.push_back(md);
141
}
142
143
void LightmapperRD::add_probe(const Vector3 &p_position) {
144
Probe probe;
145
probe.position[0] = p_position.x;
146
probe.position[1] = p_position.y;
147
probe.position[2] = p_position.z;
148
probe.position[3] = 0;
149
probe_positions.push_back(probe);
150
}
151
152
void LightmapperRD::_plot_triangle_into_triangle_index_list(int p_size, const Vector3i &p_ofs, const AABB &p_bounds, const Vector3 p_points[3], uint32_t p_triangle_index, LocalVector<TriangleSort> &p_triangles_sort, uint32_t p_grid_size) {
153
int half_size = p_size / 2;
154
155
for (int i = 0; i < 8; i++) {
156
AABB aabb = p_bounds;
157
aabb.size *= 0.5;
158
Vector3i n = p_ofs;
159
160
if (i & 1) {
161
aabb.position.x += aabb.size.x;
162
n.x += half_size;
163
}
164
if (i & 2) {
165
aabb.position.y += aabb.size.y;
166
n.y += half_size;
167
}
168
if (i & 4) {
169
aabb.position.z += aabb.size.z;
170
n.z += half_size;
171
}
172
173
{
174
Vector3 qsize = aabb.size * 0.5; //quarter size, for fast aabb test
175
176
if (!Geometry3D::triangle_box_overlap(aabb.position + qsize, qsize, p_points)) {
177
//does not fit in child, go on
178
continue;
179
}
180
}
181
182
if (half_size == 1) {
183
//got to the end
184
TriangleSort ts;
185
ts.cell_index = n.x + (n.y * p_grid_size) + (n.z * p_grid_size * p_grid_size);
186
ts.triangle_index = p_triangle_index;
187
ts.triangle_aabb.position = p_points[0];
188
ts.triangle_aabb.size = Vector3();
189
ts.triangle_aabb.expand_to(p_points[1]);
190
ts.triangle_aabb.expand_to(p_points[2]);
191
p_triangles_sort.push_back(ts);
192
} else {
193
_plot_triangle_into_triangle_index_list(half_size, n, aabb, p_points, p_triangle_index, p_triangles_sort, p_grid_size);
194
}
195
}
196
}
197
198
void LightmapperRD::_sort_triangle_clusters(uint32_t p_cluster_size, uint32_t p_cluster_index, uint32_t p_index_start, uint32_t p_count, LocalVector<TriangleSort> &p_triangle_sort, LocalVector<ClusterAABB> &p_cluster_aabb) {
199
if (p_count == 0) {
200
return;
201
}
202
203
// Compute AABB for all triangles in the range.
204
SortArray<TriangleSort, TriangleSortAxis<0>> triangle_sorter_x;
205
SortArray<TriangleSort, TriangleSortAxis<1>> triangle_sorter_y;
206
SortArray<TriangleSort, TriangleSortAxis<2>> triangle_sorter_z;
207
AABB cluster_aabb = p_triangle_sort[p_index_start].triangle_aabb;
208
for (uint32_t i = 1; i < p_count; i++) {
209
cluster_aabb.merge_with(p_triangle_sort[p_index_start + i].triangle_aabb);
210
}
211
212
if (p_count > p_cluster_size) {
213
int longest_axis_index = cluster_aabb.get_longest_axis_index();
214
switch (longest_axis_index) {
215
case 0:
216
triangle_sorter_x.sort(&p_triangle_sort[p_index_start], p_count);
217
break;
218
case 1:
219
triangle_sorter_y.sort(&p_triangle_sort[p_index_start], p_count);
220
break;
221
case 2:
222
triangle_sorter_z.sort(&p_triangle_sort[p_index_start], p_count);
223
break;
224
default:
225
DEV_ASSERT(false && "Invalid axis returned by AABB.");
226
break;
227
}
228
229
uint32_t left_cluster_count = next_power_of_2(p_count / 2);
230
left_cluster_count = MAX(left_cluster_count, p_cluster_size);
231
left_cluster_count = MIN(left_cluster_count, p_count);
232
_sort_triangle_clusters(p_cluster_size, p_cluster_index, p_index_start, left_cluster_count, p_triangle_sort, p_cluster_aabb);
233
234
if (left_cluster_count < p_count) {
235
uint32_t cluster_index_right = p_cluster_index + (left_cluster_count / p_cluster_size);
236
_sort_triangle_clusters(p_cluster_size, cluster_index_right, p_index_start + left_cluster_count, p_count - left_cluster_count, p_triangle_sort, p_cluster_aabb);
237
}
238
} else {
239
ClusterAABB &aabb = p_cluster_aabb[p_cluster_index];
240
Vector3 aabb_end = cluster_aabb.get_end();
241
aabb.min_bounds[0] = cluster_aabb.position.x;
242
aabb.min_bounds[1] = cluster_aabb.position.y;
243
aabb.min_bounds[2] = cluster_aabb.position.z;
244
aabb.max_bounds[0] = aabb_end.x;
245
aabb.max_bounds[1] = aabb_end.y;
246
aabb.max_bounds[2] = aabb_end.z;
247
}
248
}
249
250
Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_size, int p_denoiser_range, Vector<Ref<Image>> &albedo_images, Vector<Ref<Image>> &emission_images, AABB &bounds, Size2i &atlas_size, int &atlas_slices, float p_supersampling_factor, BakeStepFunc p_step_function, void *p_bake_userdata) {
251
Vector<Size2i> sizes;
252
253
for (int m_i = 0; m_i < mesh_instances.size(); m_i++) {
254
MeshInstance &mi = mesh_instances.write[m_i];
255
Size2i s = Size2i(mi.data.albedo_on_uv2->get_width(), mi.data.albedo_on_uv2->get_height());
256
sizes.push_back(s);
257
atlas_size = atlas_size.max(s + Size2i(2, 2).maxi(p_denoiser_range) * p_supersampling_factor);
258
}
259
260
int max = nearest_power_of_2_templated(atlas_size.width);
261
max = MAX(max, nearest_power_of_2_templated(atlas_size.height));
262
263
if (max > p_max_texture_size) {
264
return BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE;
265
}
266
267
if (p_step_function) {
268
if (p_step_function(0.1, RTR("Determining optimal atlas size"), p_bake_userdata, true)) {
269
return BAKE_ERROR_USER_ABORTED;
270
}
271
}
272
273
atlas_size = Size2i(max, max);
274
275
Size2i best_atlas_size;
276
int best_atlas_slices = 0;
277
int best_atlas_memory = 0x7FFFFFFF;
278
Vector<Vector3i> best_atlas_offsets;
279
280
// Determine best texture array atlas size by bruteforce fitting.
281
while (atlas_size.x <= p_max_texture_size && atlas_size.y <= p_max_texture_size) {
282
Vector<Vector2i> source_sizes;
283
Vector<int> source_indices;
284
source_sizes.resize(sizes.size());
285
source_indices.resize(sizes.size());
286
for (int i = 0; i < source_indices.size(); i++) {
287
// Add padding between lightmaps.
288
// Scale the padding if the lightmap will be downsampled at the end of the baking process
289
// Otherwise the padding would be insufficient.
290
source_sizes.write[i] = sizes[i] + Vector2i(2, 2).maxi(p_denoiser_range) * p_supersampling_factor;
291
source_indices.write[i] = i;
292
}
293
Vector<Vector3i> atlas_offsets;
294
atlas_offsets.resize(source_sizes.size());
295
296
// Ensure the sizes can all fit into a single atlas layer.
297
// This should always happen, and this check is only in place to prevent an infinite loop.
298
for (int i = 0; i < source_sizes.size(); i++) {
299
if (source_sizes[i] > atlas_size) {
300
return BAKE_ERROR_ATLAS_TOO_SMALL;
301
}
302
}
303
304
int slices = 0;
305
306
while (source_sizes.size() > 0) {
307
Vector<Vector3i> offsets = Geometry2D::partial_pack_rects(source_sizes, atlas_size);
308
Vector<int> new_indices;
309
Vector<Vector2i> new_sources;
310
for (int i = 0; i < offsets.size(); i++) {
311
Vector3i ofs = offsets[i];
312
int sidx = source_indices[i];
313
if (ofs.z > 0) {
314
//valid
315
ofs.z = slices;
316
atlas_offsets.write[sidx] = ofs + Vector3i(1, 1, 0); // Center lightmap in the reserved oversized region
317
} else {
318
new_indices.push_back(sidx);
319
new_sources.push_back(source_sizes[i]);
320
}
321
}
322
323
source_sizes = new_sources;
324
source_indices = new_indices;
325
slices++;
326
}
327
328
int mem_used = atlas_size.x * atlas_size.y * slices;
329
if (mem_used < best_atlas_memory) {
330
best_atlas_size = atlas_size;
331
best_atlas_offsets = atlas_offsets;
332
best_atlas_slices = slices;
333
best_atlas_memory = mem_used;
334
}
335
336
if (atlas_size.width == atlas_size.height) {
337
atlas_size.width *= 2;
338
} else {
339
atlas_size.height *= 2;
340
}
341
}
342
atlas_size = best_atlas_size;
343
atlas_slices = best_atlas_slices;
344
345
// apply the offsets and slice to all images, and also blit albedo and emission
346
albedo_images.resize(atlas_slices);
347
emission_images.resize(atlas_slices);
348
349
if (p_step_function) {
350
if (p_step_function(0.2, RTR("Blitting albedo and emission"), p_bake_userdata, true)) {
351
return BAKE_ERROR_USER_ABORTED;
352
}
353
}
354
355
for (int i = 0; i < atlas_slices; i++) {
356
Ref<Image> albedo = Image::create_empty(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBA8);
357
albedo->set_as_black();
358
albedo_images.write[i] = albedo;
359
360
Ref<Image> emission = Image::create_empty(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH);
361
emission->set_as_black();
362
emission_images.write[i] = emission;
363
}
364
365
//assign uv positions
366
367
for (int m_i = 0; m_i < mesh_instances.size(); m_i++) {
368
MeshInstance &mi = mesh_instances.write[m_i];
369
mi.offset.x = best_atlas_offsets[m_i].x;
370
mi.offset.y = best_atlas_offsets[m_i].y;
371
mi.slice = best_atlas_offsets[m_i].z;
372
albedo_images.write[mi.slice]->blit_rect(mi.data.albedo_on_uv2, Rect2i(Vector2i(), mi.data.albedo_on_uv2->get_size()), mi.offset);
373
emission_images.write[mi.slice]->blit_rect(mi.data.emission_on_uv2, Rect2(Vector2i(), mi.data.emission_on_uv2->get_size()), mi.offset);
374
}
375
376
return BAKE_OK;
377
}
378
379
void LightmapperRD::_create_acceleration_structures(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, AABB &bounds, int grid_size, uint32_t p_cluster_size, Vector<Probe> &p_probe_positions, GenerateProbes p_generate_probes, Vector<int> &slice_triangle_count, Vector<int> &slice_seam_count, RID &vertex_buffer, RID &triangle_buffer, RID &lights_buffer, RID &r_triangle_indices_buffer, RID &r_cluster_indices_buffer, RID &r_cluster_aabbs_buffer, RID &probe_positions_buffer, RID &grid_texture, RID &seams_buffer, BakeStepFunc p_step_function, void *p_bake_userdata) {
380
HashMap<Vertex, uint32_t, VertexHash> vertex_map;
381
382
//fill triangles array and vertex array
383
LocalVector<Triangle> triangles;
384
LocalVector<Vertex> vertex_array;
385
LocalVector<Seam> seams;
386
387
slice_triangle_count.resize(atlas_slices);
388
slice_seam_count.resize(atlas_slices);
389
390
for (int i = 0; i < atlas_slices; i++) {
391
slice_triangle_count.write[i] = 0;
392
slice_seam_count.write[i] = 0;
393
}
394
395
bounds = AABB();
396
397
for (int m_i = 0; m_i < mesh_instances.size(); m_i++) {
398
if (p_step_function) {
399
float p = float(m_i + 1) / MAX(1, mesh_instances.size()) * 0.1;
400
p_step_function(0.3 + p, vformat(RTR("Plotting mesh into acceleration structure %d/%d"), m_i + 1, mesh_instances.size()), p_bake_userdata, false);
401
}
402
403
HashMap<Edge, EdgeUV2, EdgeHash> edges;
404
405
MeshInstance &mi = mesh_instances.write[m_i];
406
407
Vector2 uv_scale = Vector2(mi.data.albedo_on_uv2->get_width(), mi.data.albedo_on_uv2->get_height()) / Vector2(atlas_size);
408
Vector2 uv_offset = Vector2(mi.offset) / Vector2(atlas_size);
409
if (m_i == 0) {
410
bounds.position = mi.data.points[0];
411
}
412
413
for (int i = 0; i < mi.data.points.size(); i += 3) {
414
Vector3 vtxs[3] = { mi.data.points[i + 0], mi.data.points[i + 1], mi.data.points[i + 2] };
415
Vector2 uvs[3] = { mi.data.uv2[i + 0] * uv_scale + uv_offset, mi.data.uv2[i + 1] * uv_scale + uv_offset, mi.data.uv2[i + 2] * uv_scale + uv_offset };
416
Vector3 normal[3] = { mi.data.normal[i + 0], mi.data.normal[i + 1], mi.data.normal[i + 2] };
417
418
AABB taabb;
419
Triangle t;
420
t.slice = mi.slice;
421
for (int k = 0; k < 3; k++) {
422
bounds.expand_to(vtxs[k]);
423
424
Vertex v;
425
v.position[0] = vtxs[k].x;
426
v.position[1] = vtxs[k].y;
427
v.position[2] = vtxs[k].z;
428
v.uv[0] = uvs[k].x;
429
v.uv[1] = uvs[k].y;
430
v.normal_xy[0] = normal[k].x;
431
v.normal_xy[1] = normal[k].y;
432
v.normal_z = normal[k].z;
433
434
uint32_t *indexptr = vertex_map.getptr(v);
435
436
if (indexptr) {
437
t.indices[k] = *indexptr;
438
} else {
439
uint32_t new_index = vertex_map.size();
440
t.indices[k] = new_index;
441
vertex_map[v] = new_index;
442
vertex_array.push_back(v);
443
}
444
445
if (k == 0) {
446
taabb.position = vtxs[k];
447
} else {
448
taabb.expand_to(vtxs[k]);
449
}
450
}
451
452
//compute seams that will need to be blended later
453
for (int k = 0; k < 3; k++) {
454
int n = (k + 1) % 3;
455
456
Edge edge(vtxs[k], vtxs[n], normal[k], normal[n]);
457
Vector2i edge_indices(t.indices[k], t.indices[n]);
458
EdgeUV2 uv2(uvs[k], uvs[n], edge_indices);
459
460
if (edge.b == edge.a) {
461
continue; //degenerate, somehow
462
}
463
if (edge.b < edge.a) {
464
SWAP(edge.a, edge.b);
465
SWAP(edge.na, edge.nb);
466
SWAP(uv2.a, uv2.b);
467
SWAP(uv2.indices.x, uv2.indices.y);
468
SWAP(edge_indices.x, edge_indices.y);
469
}
470
471
EdgeUV2 *euv2 = edges.getptr(edge);
472
if (!euv2) {
473
edges[edge] = uv2;
474
} else {
475
if (*euv2 == uv2) {
476
continue; // seam shared UV space, no need to blend
477
}
478
if (euv2->seam_found) {
479
continue; //bad geometry
480
}
481
482
Seam seam;
483
seam.a = edge_indices;
484
seam.b = euv2->indices;
485
seam.slice = mi.slice;
486
seams.push_back(seam);
487
slice_seam_count.write[mi.slice]++;
488
euv2->seam_found = true;
489
}
490
}
491
492
t.min_bounds[0] = taabb.position.x;
493
t.min_bounds[1] = taabb.position.y;
494
t.min_bounds[2] = taabb.position.z;
495
t.max_bounds[0] = taabb.position.x + MAX(taabb.size.x, 0.0001);
496
t.max_bounds[1] = taabb.position.y + MAX(taabb.size.y, 0.0001);
497
t.max_bounds[2] = taabb.position.z + MAX(taabb.size.z, 0.0001);
498
499
t.cull_mode = RS::CULL_MODE_BACK;
500
501
RID material = mi.data.material[i];
502
if (material.is_valid()) {
503
t.cull_mode = RSG::material_storage->material_get_cull_mode(material);
504
}
505
t.pad1 = 0; //make valgrind not complain
506
triangles.push_back(t);
507
slice_triangle_count.write[t.slice]++;
508
}
509
}
510
511
//also consider probe positions for bounds
512
for (int i = 0; i < p_probe_positions.size(); i++) {
513
Vector3 pp(p_probe_positions[i].position[0], p_probe_positions[i].position[1], p_probe_positions[i].position[2]);
514
bounds.expand_to(pp);
515
}
516
bounds.grow_by(0.1); //grow a bit to avoid numerical error
517
518
triangles.sort(); //sort by slice
519
seams.sort();
520
521
if (p_step_function) {
522
p_step_function(0.4, RTR("Optimizing acceleration structure"), p_bake_userdata, true);
523
}
524
525
//fill list of triangles in grid
526
LocalVector<TriangleSort> triangle_sort;
527
for (uint32_t i = 0; i < triangles.size(); i++) {
528
const Triangle &t = triangles[i];
529
Vector3 face[3] = {
530
Vector3(vertex_array[t.indices[0]].position[0], vertex_array[t.indices[0]].position[1], vertex_array[t.indices[0]].position[2]),
531
Vector3(vertex_array[t.indices[1]].position[0], vertex_array[t.indices[1]].position[1], vertex_array[t.indices[1]].position[2]),
532
Vector3(vertex_array[t.indices[2]].position[0], vertex_array[t.indices[2]].position[1], vertex_array[t.indices[2]].position[2])
533
};
534
_plot_triangle_into_triangle_index_list(grid_size, Vector3i(), bounds, face, i, triangle_sort, grid_size);
535
}
536
//sort it
537
triangle_sort.sort();
538
539
LocalVector<uint32_t> cluster_indices;
540
LocalVector<ClusterAABB> cluster_aabbs;
541
Vector<uint32_t> triangle_indices;
542
triangle_indices.resize(triangle_sort.size());
543
Vector<uint32_t> grid_indices;
544
grid_indices.resize(grid_size * grid_size * grid_size * 2);
545
memset(grid_indices.ptrw(), 0, grid_indices.size() * sizeof(uint32_t));
546
547
{
548
// Fill grid with cell indices.
549
uint32_t last_cell = 0xFFFFFFFF;
550
uint32_t *giw = grid_indices.ptrw();
551
uint32_t cluster_count = 0;
552
uint32_t solid_cell_count = 0;
553
for (uint32_t i = 0; i < triangle_sort.size(); i++) {
554
uint32_t cell = triangle_sort[i].cell_index;
555
if (cell != last_cell) {
556
giw[cell * 2 + 1] = solid_cell_count;
557
solid_cell_count++;
558
}
559
560
if ((giw[cell * 2] % p_cluster_size) == 0) {
561
// Add an extra cluster every time the triangle counter reaches a multiple of the cluster size.
562
cluster_count++;
563
}
564
565
giw[cell * 2]++;
566
last_cell = cell;
567
}
568
569
// Build fixed-size triangle clusters for all the cells to speed up the traversal. A cell can hold multiple clusters that each contain a fixed
570
// amount of triangles and an AABB. The tracer will check against the AABBs first to know whether it needs to visit the cell's triangles.
571
//
572
// The building algorithm will divide the triangles recursively contained inside each cell, sorting by the longest axis of the AABB on each step.
573
//
574
// - If the amount of triangles is less or equal to the cluster size, the AABB will be stored and the algorithm stops.
575
//
576
// - The division by two is increased to the next power of two of half the amount of triangles (with cluster size as the minimum value) to
577
// ensure the first half always fills the cluster.
578
579
cluster_indices.resize(solid_cell_count * 2);
580
cluster_aabbs.resize(cluster_count);
581
582
uint32_t i = 0;
583
uint32_t cluster_index = 0;
584
uint32_t solid_cell_index = 0;
585
uint32_t *tiw = triangle_indices.ptrw();
586
while (i < triangle_sort.size()) {
587
cluster_indices[solid_cell_index * 2] = cluster_index;
588
cluster_indices[solid_cell_index * 2 + 1] = i;
589
590
uint32_t cell = triangle_sort[i].cell_index;
591
uint32_t triangle_count = giw[cell * 2];
592
uint32_t cell_cluster_count = (triangle_count + p_cluster_size - 1) / p_cluster_size;
593
_sort_triangle_clusters(p_cluster_size, cluster_index, i, triangle_count, triangle_sort, cluster_aabbs);
594
595
for (uint32_t j = 0; j < triangle_count; j++) {
596
tiw[i + j] = triangle_sort[i + j].triangle_index;
597
}
598
599
i += triangle_count;
600
cluster_index += cell_cluster_count;
601
solid_cell_index++;
602
}
603
}
604
#if 0
605
for (int i = 0; i < grid_size; i++) {
606
for (int j = 0; j < grid_size; j++) {
607
for (int k = 0; k < grid_size; k++) {
608
uint32_t index = i * (grid_size * grid_size) + j * grid_size + k;
609
grid_indices.write[index * 2] = float(i) / grid_size * 255;
610
grid_indices.write[index * 2 + 1] = float(j) / grid_size * 255;
611
}
612
}
613
}
614
#endif
615
616
#if 0
617
for (int i = 0; i < grid_size; i++) {
618
Vector<uint8_t> grid_usage;
619
grid_usage.resize(grid_size * grid_size);
620
for (int j = 0; j < grid_usage.size(); j++) {
621
uint32_t ofs = i * grid_size * grid_size + j;
622
uint32_t count = grid_indices[ofs * 2];
623
grid_usage.write[j] = count > 0 ? 255 : 0;
624
}
625
626
Ref<Image> img = Image::create_from_data(grid_size, grid_size, false, Image::FORMAT_L8, grid_usage);
627
img->save_png("res://grid_layer_" + itos(1000 + i).substr(1, 3) + ".png");
628
}
629
#endif
630
631
/*****************************/
632
/*** CREATE GPU STRUCTURES ***/
633
/*****************************/
634
635
lights.sort();
636
light_metadata.sort();
637
638
Vector<Vector2i> seam_buffer_vec;
639
seam_buffer_vec.resize(seams.size() * 2);
640
for (uint32_t i = 0; i < seams.size(); i++) {
641
seam_buffer_vec.write[i * 2 + 0] = seams[i].a;
642
seam_buffer_vec.write[i * 2 + 1] = seams[i].b;
643
}
644
645
{ //buffers
646
vertex_buffer = rd->storage_buffer_create(vertex_array.size() * sizeof(Vertex), vertex_array.span().reinterpret<uint8_t>());
647
648
triangle_buffer = rd->storage_buffer_create(triangles.size() * sizeof(Triangle), triangles.span().reinterpret<uint8_t>());
649
650
r_triangle_indices_buffer = rd->storage_buffer_create(triangle_indices.size() * sizeof(uint32_t), triangle_indices.span().reinterpret<uint8_t>());
651
652
r_cluster_indices_buffer = rd->storage_buffer_create(cluster_indices.size() * sizeof(uint32_t), cluster_indices.span().reinterpret<uint8_t>());
653
654
r_cluster_aabbs_buffer = rd->storage_buffer_create(cluster_aabbs.size() * sizeof(ClusterAABB), cluster_aabbs.span().reinterpret<uint8_t>());
655
656
// Even when there are no lights, the buffer must exist.
657
static const Light empty_lights[1];
658
Span<uint8_t> lb = (lights.is_empty() ? Span(empty_lights) : lights.span()).reinterpret<uint8_t>();
659
lights_buffer = rd->storage_buffer_create(lb.size(), lb);
660
661
// Even when there are no seams, the buffer must exist.
662
static const Vector2i empty_seams[2];
663
Span<uint8_t> sb = (seam_buffer_vec.is_empty() ? Span(empty_seams) : seam_buffer_vec.span()).reinterpret<uint8_t>();
664
seams_buffer = rd->storage_buffer_create(sb.size(), sb);
665
666
// Even when there are no probes, the buffer must exist.
667
static const Probe empty_probes[1];
668
Span<uint8_t> pb = (p_probe_positions.is_empty() ? Span(empty_probes) : p_probe_positions.span()).reinterpret<uint8_t>();
669
probe_positions_buffer = rd->storage_buffer_create(pb.size(), pb);
670
}
671
672
{ //grid
673
674
RD::TextureFormat tf;
675
tf.width = grid_size;
676
tf.height = grid_size;
677
tf.depth = grid_size;
678
tf.texture_type = RD::TEXTURE_TYPE_3D;
679
tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
680
681
Vector<Vector<uint8_t>> texdata;
682
texdata.resize(1);
683
//grid and indices
684
tf.format = RD::DATA_FORMAT_R32G32_UINT;
685
texdata.write[0] = grid_indices.to_byte_array();
686
grid_texture = rd->texture_create(tf, RD::TextureView(), texdata);
687
}
688
}
689
690
void LightmapperRD::_raster_geometry(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, int grid_size, AABB bounds, float p_bias, Vector<int> slice_triangle_count, RID position_tex, RID unocclude_tex, RID normal_tex, RID raster_depth_buffer, RID rasterize_shader, RID raster_base_uniform) {
691
Vector<RID> framebuffers;
692
693
for (int i = 0; i < atlas_slices; i++) {
694
RID slice_pos_tex = rd->texture_create_shared_from_slice(RD::TextureView(), position_tex, i, 0);
695
RID slice_unoc_tex = rd->texture_create_shared_from_slice(RD::TextureView(), unocclude_tex, i, 0);
696
RID slice_norm_tex = rd->texture_create_shared_from_slice(RD::TextureView(), normal_tex, i, 0);
697
Vector<RID> fb;
698
fb.push_back(slice_pos_tex);
699
fb.push_back(slice_norm_tex);
700
fb.push_back(slice_unoc_tex);
701
fb.push_back(raster_depth_buffer);
702
framebuffers.push_back(rd->framebuffer_create(fb));
703
}
704
705
RD::PipelineDepthStencilState ds;
706
ds.enable_depth_test = true;
707
ds.enable_depth_write = true;
708
ds.depth_compare_operator = RD::COMPARE_OP_LESS; //so it does render same pixel twice
709
710
RID raster_pipeline = rd->render_pipeline_create(rasterize_shader, rd->framebuffer_get_format(framebuffers[0]), RD::INVALID_FORMAT_ID, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(3), 0);
711
RID raster_pipeline_wire;
712
{
713
RD::PipelineRasterizationState rw;
714
rw.wireframe = true;
715
raster_pipeline_wire = rd->render_pipeline_create(rasterize_shader, rd->framebuffer_get_format(framebuffers[0]), RD::INVALID_FORMAT_ID, RD::RENDER_PRIMITIVE_TRIANGLES, rw, RD::PipelineMultisampleState(), ds, RD::PipelineColorBlendState::create_disabled(3), 0);
716
}
717
718
uint32_t triangle_offset = 0;
719
Vector<Color> clear_colors;
720
clear_colors.push_back(Color(0, 0, 0, 0));
721
clear_colors.push_back(Color(0, 0, 0, 0));
722
clear_colors.push_back(Color(0, 0, 0, 0));
723
724
for (int i = 0; i < atlas_slices; i++) {
725
RasterPushConstant raster_push_constant;
726
raster_push_constant.atlas_size[0] = atlas_size.x;
727
raster_push_constant.atlas_size[1] = atlas_size.y;
728
raster_push_constant.base_triangle = triangle_offset;
729
raster_push_constant.to_cell_offset[0] = bounds.position.x;
730
raster_push_constant.to_cell_offset[1] = bounds.position.y;
731
raster_push_constant.to_cell_offset[2] = bounds.position.z;
732
raster_push_constant.bias = p_bias;
733
raster_push_constant.to_cell_size[0] = (1.0 / bounds.size.x) * float(grid_size);
734
raster_push_constant.to_cell_size[1] = (1.0 / bounds.size.y) * float(grid_size);
735
raster_push_constant.to_cell_size[2] = (1.0 / bounds.size.z) * float(grid_size);
736
raster_push_constant.grid_size[0] = grid_size;
737
raster_push_constant.grid_size[1] = grid_size;
738
raster_push_constant.grid_size[2] = grid_size;
739
740
// Half pixel offset is required so the rasterizer doesn't output face edges directly aligned into pixels.
741
// This fixes artifacts where the pixel would be traced from the edge of a face, causing half the rays to
742
// be outside of the boundaries of the geometry. See <https://github.com/godotengine/godot/issues/69126>.
743
raster_push_constant.uv_offset[0] = -0.5f / float(atlas_size.x);
744
raster_push_constant.uv_offset[1] = -0.5f / float(atlas_size.y);
745
746
RD::DrawListID draw_list = rd->draw_list_begin(framebuffers[i], RD::DRAW_CLEAR_ALL, clear_colors, 1.0f, 0, Rect2(), RDD::BreadcrumbMarker::LIGHTMAPPER_PASS);
747
//draw opaque
748
rd->draw_list_bind_render_pipeline(draw_list, raster_pipeline);
749
rd->draw_list_bind_uniform_set(draw_list, raster_base_uniform, 0);
750
rd->draw_list_set_push_constant(draw_list, &raster_push_constant, sizeof(RasterPushConstant));
751
rd->draw_list_draw(draw_list, false, 1, slice_triangle_count[i] * 3);
752
//draw wire
753
rd->draw_list_bind_render_pipeline(draw_list, raster_pipeline_wire);
754
rd->draw_list_bind_uniform_set(draw_list, raster_base_uniform, 0);
755
rd->draw_list_set_push_constant(draw_list, &raster_push_constant, sizeof(RasterPushConstant));
756
rd->draw_list_draw(draw_list, false, 1, slice_triangle_count[i] * 3);
757
758
rd->draw_list_end();
759
760
triangle_offset += slice_triangle_count[i];
761
}
762
}
763
764
static Vector<RD::Uniform> dilate_or_denoise_common_uniforms(RID &p_source_light_tex, RID &p_dest_light_tex) {
765
Vector<RD::Uniform> uniforms;
766
{
767
RD::Uniform u;
768
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
769
u.binding = 0;
770
u.append_id(p_dest_light_tex);
771
uniforms.push_back(u);
772
}
773
{
774
RD::Uniform u;
775
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
776
u.binding = 1;
777
u.append_id(p_source_light_tex);
778
uniforms.push_back(u);
779
}
780
781
return uniforms;
782
}
783
784
LightmapperRD::BakeError LightmapperRD::_dilate(RenderingDevice *rd, Ref<RDShaderFile> &compute_shader, RID &compute_base_uniform_set, PushConstant &push_constant, RID &source_light_tex, RID &dest_light_tex, const Size2i &atlas_size, int atlas_slices) {
785
Vector<RD::Uniform> uniforms = dilate_or_denoise_common_uniforms(source_light_tex, dest_light_tex);
786
787
RID compute_shader_dilate = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("dilate"));
788
ERR_FAIL_COND_V(compute_shader_dilate.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen
789
RID compute_shader_dilate_pipeline = rd->compute_pipeline_create(compute_shader_dilate);
790
791
RID dilate_uniform_set = rd->uniform_set_create(uniforms, compute_shader_dilate, 1);
792
793
RD::ComputeListID compute_list = rd->compute_list_begin();
794
rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_dilate_pipeline);
795
rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
796
rd->compute_list_bind_uniform_set(compute_list, dilate_uniform_set, 1);
797
push_constant.region_ofs[0] = 0;
798
push_constant.region_ofs[1] = 0;
799
Vector3i group_size(Math::division_round_up(atlas_size.x, 8), Math::division_round_up(atlas_size.y, 8), 1); //restore group size
800
801
for (int i = 0; i < atlas_slices; i++) {
802
push_constant.atlas_slice = i;
803
rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
804
rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
805
//no barrier, let them run all together
806
}
807
rd->compute_list_end();
808
rd->free_rid(compute_shader_dilate);
809
810
#ifdef DEBUG_TEXTURES
811
for (int i = 0; i < atlas_slices; i++) {
812
Vector<uint8_t> s = rd->texture_get_data(source_light_tex, i);
813
Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
814
img->convert(Image::FORMAT_RGBA8);
815
img->save_png("res://5_dilated_" + itos(i) + ".png");
816
}
817
#endif
818
return BAKE_OK;
819
}
820
821
LightmapperRD::BakeError LightmapperRD::_pack_l1(RenderingDevice *rd, Ref<RDShaderFile> &compute_shader, RID &compute_base_uniform_set, PushConstant &push_constant, RID &source_light_tex, RID &dest_light_tex, const Size2i &atlas_size, int atlas_slices) {
822
Vector<RD::Uniform> uniforms = dilate_or_denoise_common_uniforms(source_light_tex, dest_light_tex);
823
824
RID compute_shader_pack = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("pack_coeffs"));
825
ERR_FAIL_COND_V(compute_shader_pack.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen
826
RID compute_shader_pack_pipeline = rd->compute_pipeline_create(compute_shader_pack);
827
828
RID dilate_uniform_set = rd->uniform_set_create(uniforms, compute_shader_pack, 1);
829
830
RD::ComputeListID compute_list = rd->compute_list_begin();
831
rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_pack_pipeline);
832
rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
833
rd->compute_list_bind_uniform_set(compute_list, dilate_uniform_set, 1);
834
push_constant.region_ofs[0] = 0;
835
push_constant.region_ofs[1] = 0;
836
Vector3i group_size(Math::division_round_up(atlas_size.x, 8), Math::division_round_up(atlas_size.y, 8), 1); //restore group size
837
838
for (int i = 0; i < atlas_slices; i++) {
839
push_constant.atlas_slice = i;
840
rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
841
rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
842
//no barrier, let them run all together
843
}
844
rd->compute_list_end();
845
rd->free_rid(compute_shader_pack);
846
847
return BAKE_OK;
848
}
849
850
Error LightmapperRD::_store_pfm(RenderingDevice *p_rd, RID p_atlas_tex, int p_index, const Size2i &p_atlas_size, const String &p_name, bool p_shadowmask) {
851
Vector<uint8_t> data = p_rd->texture_get_data(p_atlas_tex, p_index);
852
Ref<Image> img = Image::create_from_data(p_atlas_size.width, p_atlas_size.height, false, p_shadowmask ? Image::FORMAT_RGBA8 : Image::FORMAT_RGBAH, data);
853
img->convert(Image::FORMAT_RGBF);
854
Vector<uint8_t> data_float = img->get_data();
855
856
Error err = OK;
857
Ref<FileAccess> file = FileAccess::open(p_name, FileAccess::WRITE, &err);
858
ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save PFN at path: '%s'.", p_name));
859
file->store_line("PF");
860
file->store_line(vformat("%d %d", img->get_width(), img->get_height()));
861
#ifdef BIG_ENDIAN_ENABLED
862
file->store_line("1.0");
863
#else
864
file->store_line("-1.0");
865
#endif
866
file->store_buffer(data_float);
867
file->close();
868
869
return OK;
870
}
871
872
Ref<Image> LightmapperRD::_read_pfm(const String &p_name, bool p_shadowmask) {
873
Error err = OK;
874
Ref<FileAccess> file = FileAccess::open(p_name, FileAccess::READ, &err);
875
ERR_FAIL_COND_V_MSG(err, Ref<Image>(), vformat("Can't load PFM at path: '%s'.", p_name));
876
ERR_FAIL_COND_V(file->get_line() != "PF", Ref<Image>());
877
878
Vector<String> new_size = file->get_line().split(" ");
879
ERR_FAIL_COND_V(new_size.size() != 2, Ref<Image>());
880
int new_width = new_size[0].to_int();
881
int new_height = new_size[1].to_int();
882
883
float endian = file->get_line().to_float();
884
Vector<uint8_t> new_data = file->get_buffer(file->get_length() - file->get_position());
885
file->close();
886
887
#ifdef BIG_ENDIAN_ENABLED
888
if (unlikely(endian < 0.0)) {
889
uint32_t count = new_data.size() / 4;
890
uint16_t *dst = (uint16_t *)new_data.ptrw();
891
for (uint32_t j = 0; j < count; j++) {
892
dst[j * 4] = BSWAP32(dst[j * 4]);
893
}
894
}
895
#else
896
if (unlikely(endian > 0.0)) {
897
uint32_t count = new_data.size() / 4;
898
uint16_t *dst = (uint16_t *)new_data.ptrw();
899
for (uint32_t j = 0; j < count; j++) {
900
dst[j * 4] = BSWAP32(dst[j * 4]);
901
}
902
}
903
#endif
904
Ref<Image> img = Image::create_from_data(new_width, new_height, false, Image::FORMAT_RGBF, new_data);
905
img->convert(p_shadowmask ? Image::FORMAT_RGBA8 : Image::FORMAT_RGBAH);
906
return img;
907
}
908
909
LightmapperRD::BakeError LightmapperRD::_denoise_oidn(RenderingDevice *p_rd, RID p_source_light_tex, RID p_source_normal_tex, RID p_dest_light_tex, const Size2i &p_atlas_size, int p_atlas_slices, bool p_bake_sh, bool p_shadowmask, const String &p_exe) {
910
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
911
912
for (int i = 0; i < p_atlas_slices; i++) {
913
String fname_norm_in = EditorPaths::get_singleton()->get_cache_dir().path_join(vformat("temp_norm_%d.pfm", i));
914
_store_pfm(p_rd, p_source_normal_tex, i, p_atlas_size, fname_norm_in, false);
915
916
for (int j = 0; j < (p_bake_sh ? 4 : 1); j++) {
917
int index = i * (p_bake_sh ? 4 : 1) + j;
918
String fname_light_in = EditorPaths::get_singleton()->get_cache_dir().path_join(vformat("temp_light_%d.pfm", index));
919
String fname_out = EditorPaths::get_singleton()->get_cache_dir().path_join(vformat("temp_denoised_%d.pfm", index));
920
921
_store_pfm(p_rd, p_source_light_tex, index, p_atlas_size, fname_light_in, p_shadowmask);
922
923
List<String> args;
924
args.push_back("--device");
925
args.push_back("default");
926
927
args.push_back("--filter");
928
args.push_back("RTLightmap");
929
930
args.push_back(p_shadowmask ? "--ldr" : "--hdr");
931
args.push_back(fname_light_in);
932
933
args.push_back("--nrm");
934
args.push_back(fname_norm_in);
935
936
args.push_back("--output");
937
args.push_back(fname_out);
938
939
String str;
940
int exitcode = 0;
941
942
Error err = OS::get_singleton()->execute(p_exe, args, &str, &exitcode, true);
943
944
da->remove(fname_light_in);
945
946
if (err != OK || exitcode != 0) {
947
da->remove(fname_out);
948
print_verbose(str);
949
ERR_FAIL_V_MSG(BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES, vformat("OIDN denoiser failed, return code: %d", exitcode));
950
}
951
952
Ref<Image> img = _read_pfm(fname_out, p_shadowmask);
953
da->remove(fname_out);
954
955
ERR_FAIL_COND_V(img.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
956
957
Vector<uint8_t> old_data = p_rd->texture_get_data(p_source_light_tex, index);
958
Vector<uint8_t> new_data = img->get_data();
959
img.unref(); // Avoid copy on write.
960
961
uint32_t count = old_data.size() / 2;
962
const uint16_t *src = (const uint16_t *)old_data.ptr();
963
uint16_t *dst = (uint16_t *)new_data.ptrw();
964
for (uint32_t k = 0; k < count; k += 4) {
965
dst[k + 3] = src[k + 3];
966
}
967
968
p_rd->texture_update(p_dest_light_tex, index, new_data);
969
}
970
da->remove(fname_norm_in);
971
}
972
return BAKE_OK;
973
}
974
975
LightmapperRD::BakeError LightmapperRD::_denoise(RenderingDevice *p_rd, Ref<RDShaderFile> &p_compute_shader, const RID &p_compute_base_uniform_set, PushConstant &p_push_constant, RID p_source_light_tex, RID p_source_normal_tex, RID p_dest_light_tex, RID p_unocclude_tex, float p_denoiser_strength, int p_denoiser_range, const Size2i &p_atlas_size, int p_atlas_slices, bool p_bake_sh, BakeStepFunc p_step_function, void *p_bake_userdata) {
976
RID denoise_params_buffer = p_rd->uniform_buffer_create(sizeof(DenoiseParams));
977
DenoiseParams denoise_params;
978
denoise_params.spatial_bandwidth = 5.0f;
979
denoise_params.light_bandwidth = p_denoiser_strength;
980
denoise_params.albedo_bandwidth = 1.0f;
981
denoise_params.normal_bandwidth = 0.1f;
982
denoise_params.filter_strength = 10.0f;
983
denoise_params.half_search_window = p_denoiser_range;
984
denoise_params.slice_count = p_bake_sh ? 4 : 1;
985
p_rd->buffer_update(denoise_params_buffer, 0, sizeof(DenoiseParams), &denoise_params);
986
987
Vector<RD::Uniform> uniforms = dilate_or_denoise_common_uniforms(p_source_light_tex, p_dest_light_tex);
988
{
989
RD::Uniform u;
990
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
991
u.binding = 2;
992
u.append_id(p_source_normal_tex);
993
uniforms.push_back(u);
994
}
995
{
996
RD::Uniform u;
997
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
998
u.binding = 3;
999
u.append_id(p_unocclude_tex);
1000
uniforms.push_back(u);
1001
}
1002
{
1003
RD::Uniform u;
1004
u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
1005
u.binding = 4;
1006
u.append_id(denoise_params_buffer);
1007
uniforms.push_back(u);
1008
}
1009
1010
RID compute_shader_denoise = p_rd->shader_create_from_spirv(p_compute_shader->get_spirv_stages("denoise"));
1011
ERR_FAIL_COND_V(compute_shader_denoise.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
1012
1013
RID compute_shader_denoise_pipeline = p_rd->compute_pipeline_create(compute_shader_denoise);
1014
RID denoise_uniform_set = p_rd->uniform_set_create(uniforms, compute_shader_denoise, 1);
1015
1016
// We denoise in fixed size regions and synchronize execution to avoid GPU timeouts.
1017
// We use a region with 1/4 the amount of pixels if we're denoising SH lightmaps, as
1018
// all four of them are denoised in the shader in one dispatch.
1019
const int user_region_size = nearest_power_of_2_templated(int(GLOBAL_GET("rendering/lightmapping/bake_performance/region_size")));
1020
const int max_region_size = p_bake_sh ? user_region_size / 2 : user_region_size;
1021
int x_regions = Math::division_round_up(p_atlas_size.width, max_region_size);
1022
int y_regions = Math::division_round_up(p_atlas_size.height, max_region_size);
1023
for (int s = 0; s < p_atlas_slices; s++) {
1024
p_push_constant.atlas_slice = s;
1025
1026
for (int i = 0; i < x_regions; i++) {
1027
for (int j = 0; j < y_regions; j++) {
1028
int x = i * max_region_size;
1029
int y = j * max_region_size;
1030
int w = MIN((i + 1) * max_region_size, p_atlas_size.width) - x;
1031
int h = MIN((j + 1) * max_region_size, p_atlas_size.height) - y;
1032
p_push_constant.region_ofs[0] = x;
1033
p_push_constant.region_ofs[1] = y;
1034
1035
RD::ComputeListID compute_list = p_rd->compute_list_begin();
1036
p_rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_denoise_pipeline);
1037
p_rd->compute_list_bind_uniform_set(compute_list, p_compute_base_uniform_set, 0);
1038
p_rd->compute_list_bind_uniform_set(compute_list, denoise_uniform_set, 1);
1039
p_rd->compute_list_set_push_constant(compute_list, &p_push_constant, sizeof(PushConstant));
1040
p_rd->compute_list_dispatch(compute_list, Math::division_round_up(w, 8), Math::division_round_up(h, 8), 1);
1041
p_rd->compute_list_end();
1042
1043
p_rd->submit();
1044
p_rd->sync();
1045
}
1046
}
1047
if (p_step_function) {
1048
int percent = (s + 1) * 100 / p_atlas_slices;
1049
float p = float(s) / p_atlas_slices * 0.1;
1050
if (p_step_function(0.8 + p, vformat(RTR("Denoising %d%%"), percent), p_bake_userdata, false)) {
1051
return BAKE_ERROR_USER_ABORTED;
1052
}
1053
}
1054
}
1055
1056
p_rd->free_rid(compute_shader_denoise);
1057
p_rd->free_rid(denoise_params_buffer);
1058
1059
return BAKE_OK;
1060
}
1061
1062
LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_denoiser, float p_denoiser_strength, int p_denoiser_range, int p_bounces, float p_bounce_indirect_energy, float p_bias, int p_max_texture_size, bool p_bake_sh, bool p_bake_shadowmask, bool p_texture_for_bounces, GenerateProbes p_generate_probes, const Ref<Image> &p_environment_panorama, const Basis &p_environment_transform, BakeStepFunc p_step_function, void *p_bake_userdata, float p_exposure_normalization, float p_supersampling_factor) {
1063
int denoiser = GLOBAL_GET("rendering/lightmapping/denoising/denoiser");
1064
String oidn_path = EDITOR_GET("filesystem/tools/oidn/oidn_denoise_path");
1065
1066
if (p_use_denoiser && denoiser == 1) {
1067
// OIDN (external).
1068
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
1069
1070
if (da->dir_exists(oidn_path)) {
1071
if (OS::get_singleton()->get_name() == "Windows") {
1072
oidn_path = oidn_path.path_join("oidnDenoise.exe");
1073
} else {
1074
oidn_path = oidn_path.path_join("oidnDenoise");
1075
}
1076
}
1077
ERR_FAIL_COND_V_MSG(oidn_path.is_empty() || !da->file_exists(oidn_path), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES, "OIDN denoiser is selected in the project settings, but no or invalid OIDN executable path is configured in the editor settings.");
1078
}
1079
1080
if (p_step_function) {
1081
p_step_function(0.0, RTR("Begin Bake"), p_bake_userdata, true);
1082
}
1083
lightmap_textures.clear();
1084
shadowmask_textures.clear();
1085
int grid_size = 128;
1086
1087
/* STEP 1: Fetch material textures and compute the bounds */
1088
1089
AABB bounds;
1090
Size2i atlas_size;
1091
int atlas_slices;
1092
Vector<Ref<Image>> albedo_images;
1093
Vector<Ref<Image>> emission_images;
1094
1095
BakeError bake_error = _blit_meshes_into_atlas(p_max_texture_size, p_denoiser_range, albedo_images, emission_images, bounds, atlas_size, atlas_slices, p_supersampling_factor, p_step_function, p_bake_userdata);
1096
if (bake_error != BAKE_OK) {
1097
return bake_error;
1098
}
1099
1100
// Find any directional light suitable for shadowmasking.
1101
if (p_bake_shadowmask) {
1102
bool found = false;
1103
for (int i = 0; i < lights.size(); i++) {
1104
if (lights[i].type == LightType::LIGHT_TYPE_DIRECTIONAL && !lights[i].static_bake) {
1105
found = true;
1106
break;
1107
}
1108
}
1109
1110
if (!found) {
1111
p_bake_shadowmask = false;
1112
WARN_PRINT("Shadowmask disabled: no directional light with their bake mode set to dynamic exists.");
1113
}
1114
}
1115
1116
#ifdef DEBUG_TEXTURES
1117
for (int i = 0; i < atlas_slices; i++) {
1118
albedo_images[i]->save_png("res://0_albedo_" + itos(i) + ".png");
1119
emission_images[i]->save_png("res://0_emission_" + itos(i) + ".png");
1120
}
1121
#endif
1122
1123
// Attempt to create a local device by requesting it from rendering server first.
1124
// If that fails because the current renderer is not implemented on top of RD, we fall back to creating
1125
// a local rendering device manually depending on the current platform.
1126
Error err;
1127
RenderingContextDriver *rcd = nullptr;
1128
RenderingDevice *rd = RenderingServer::get_singleton()->create_local_rendering_device();
1129
if (rd == nullptr) {
1130
#if defined(RD_ENABLED)
1131
#if defined(METAL_ENABLED)
1132
rcd = memnew(RenderingContextDriverMetal);
1133
rd = memnew(RenderingDevice);
1134
#endif
1135
#if defined(VULKAN_ENABLED)
1136
if (rcd == nullptr) {
1137
rcd = memnew(RenderingContextDriverVulkan);
1138
rd = memnew(RenderingDevice);
1139
}
1140
#endif
1141
#endif
1142
if (rcd != nullptr && rd != nullptr) {
1143
err = rcd->initialize();
1144
if (err == OK) {
1145
err = rd->initialize(rcd);
1146
}
1147
1148
if (err != OK) {
1149
memdelete(rd);
1150
memdelete(rcd);
1151
rd = nullptr;
1152
rcd = nullptr;
1153
}
1154
}
1155
}
1156
1157
ERR_FAIL_NULL_V(rd, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
1158
1159
RID albedo_array_tex;
1160
RID emission_array_tex;
1161
RID normal_tex;
1162
RID position_tex;
1163
RID unocclude_tex;
1164
RID light_source_tex;
1165
RID light_dest_tex;
1166
RID light_accum_tex;
1167
RID light_accum_tex2;
1168
RID light_environment_tex;
1169
RID shadowmask_tex;
1170
RID shadowmask_tex2;
1171
1172
#define FREE_TEXTURES \
1173
rd->free_rid(albedo_array_tex); \
1174
rd->free_rid(emission_array_tex); \
1175
rd->free_rid(normal_tex); \
1176
rd->free_rid(position_tex); \
1177
rd->free_rid(unocclude_tex); \
1178
rd->free_rid(light_source_tex); \
1179
rd->free_rid(light_accum_tex2); \
1180
rd->free_rid(light_accum_tex); \
1181
rd->free_rid(light_environment_tex); \
1182
if (p_bake_shadowmask) { \
1183
rd->free_rid(shadowmask_tex); \
1184
rd->free_rid(shadowmask_tex2); \
1185
}
1186
1187
{ // create all textures
1188
1189
Vector<Vector<uint8_t>> albedo_data;
1190
Vector<Vector<uint8_t>> emission_data;
1191
for (int i = 0; i < atlas_slices; i++) {
1192
albedo_data.push_back(albedo_images[i]->get_data());
1193
emission_data.push_back(emission_images[i]->get_data());
1194
}
1195
1196
RD::TextureFormat tf;
1197
tf.width = atlas_size.width;
1198
tf.height = atlas_size.height;
1199
tf.array_layers = atlas_slices;
1200
tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
1201
tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
1202
tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
1203
1204
albedo_array_tex = rd->texture_create(tf, RD::TextureView(), albedo_data);
1205
1206
tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
1207
1208
emission_array_tex = rd->texture_create(tf, RD::TextureView(), emission_data);
1209
1210
//this will be rastered to
1211
tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
1212
normal_tex = rd->texture_create(tf, RD::TextureView());
1213
tf.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
1214
position_tex = rd->texture_create(tf, RD::TextureView());
1215
unocclude_tex = rd->texture_create(tf, RD::TextureView());
1216
1217
tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
1218
1219
// shadowmask
1220
if (p_bake_shadowmask) {
1221
tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
1222
1223
shadowmask_tex = rd->texture_create(tf, RD::TextureView());
1224
rd->texture_clear(shadowmask_tex, Color(0, 0, 0, 0), 0, 1, 0, atlas_slices);
1225
1226
shadowmask_tex2 = rd->texture_create(tf, RD::TextureView());
1227
rd->texture_clear(shadowmask_tex2, Color(0, 0, 0, 0), 0, 1, 0, atlas_slices);
1228
}
1229
1230
// lightmap
1231
tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
1232
1233
light_source_tex = rd->texture_create(tf, RD::TextureView());
1234
rd->texture_clear(light_source_tex, Color(0, 0, 0, 0), 0, 1, 0, atlas_slices);
1235
1236
if (p_bake_sh) {
1237
tf.array_layers *= 4;
1238
}
1239
light_accum_tex = rd->texture_create(tf, RD::TextureView());
1240
rd->texture_clear(light_accum_tex, Color(0, 0, 0, 0), 0, 1, 0, tf.array_layers);
1241
light_dest_tex = rd->texture_create(tf, RD::TextureView());
1242
rd->texture_clear(light_dest_tex, Color(0, 0, 0, 0), 0, 1, 0, tf.array_layers);
1243
light_accum_tex2 = light_dest_tex;
1244
1245
//env
1246
{
1247
Ref<Image> panorama_tex;
1248
if (p_environment_panorama.is_valid()) {
1249
panorama_tex = p_environment_panorama;
1250
panorama_tex->convert(Image::FORMAT_RGBAF);
1251
} else {
1252
panorama_tex.instantiate();
1253
panorama_tex->initialize_data(8, 8, false, Image::FORMAT_RGBAF);
1254
panorama_tex->fill(Color(0, 0, 0, 1));
1255
}
1256
1257
RD::TextureFormat tfp;
1258
tfp.width = panorama_tex->get_width();
1259
tfp.height = panorama_tex->get_height();
1260
tfp.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
1261
tfp.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
1262
1263
Vector<Vector<uint8_t>> tdata;
1264
tdata.push_back(panorama_tex->get_data());
1265
light_environment_tex = rd->texture_create(tfp, RD::TextureView(), tdata);
1266
1267
#ifdef DEBUG_TEXTURES
1268
panorama_tex->save_exr("res://0_panorama.exr", false);
1269
#endif
1270
}
1271
}
1272
1273
/* STEP 2: create the acceleration structure for the GPU*/
1274
1275
Vector<int> slice_triangle_count;
1276
RID bake_parameters_buffer;
1277
RID vertex_buffer;
1278
RID triangle_buffer;
1279
RID lights_buffer;
1280
RID triangle_indices_buffer;
1281
RID cluster_indices_buffer;
1282
RID cluster_aabbs_buffer;
1283
RID grid_texture;
1284
RID seams_buffer;
1285
RID probe_positions_buffer;
1286
1287
Vector<int> slice_seam_count;
1288
1289
#define FREE_BUFFERS \
1290
rd->free_rid(bake_parameters_buffer); \
1291
rd->free_rid(vertex_buffer); \
1292
rd->free_rid(triangle_buffer); \
1293
rd->free_rid(lights_buffer); \
1294
rd->free_rid(triangle_indices_buffer); \
1295
rd->free_rid(cluster_indices_buffer); \
1296
rd->free_rid(cluster_aabbs_buffer); \
1297
rd->free_rid(grid_texture); \
1298
rd->free_rid(seams_buffer); \
1299
rd->free_rid(probe_positions_buffer);
1300
1301
const uint32_t cluster_size = 16;
1302
_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);
1303
1304
// The index of the directional light used for shadowmasking.
1305
int shadowmask_light_idx = -1;
1306
1307
// Find the directional light index in the sorted lights array.
1308
if (p_bake_shadowmask) {
1309
int shadowmask_lights_count = 0;
1310
1311
for (int i = 0; i < lights.size(); i++) {
1312
if (lights[i].type == LightType::LIGHT_TYPE_DIRECTIONAL && !lights[i].static_bake) {
1313
if (shadowmask_light_idx < 0) {
1314
shadowmask_light_idx = i;
1315
}
1316
shadowmask_lights_count += 1;
1317
}
1318
}
1319
1320
if (shadowmask_lights_count > 1) {
1321
WARN_PRINT(
1322
vformat("%d directional lights detected for shadowmask baking. Only %s will be used.",
1323
shadowmask_lights_count, light_metadata[shadowmask_light_idx].name));
1324
}
1325
}
1326
1327
// Create global bake parameters buffer.
1328
BakeParameters bake_parameters;
1329
bake_parameters.world_size[0] = bounds.size.x;
1330
bake_parameters.world_size[1] = bounds.size.y;
1331
bake_parameters.world_size[2] = bounds.size.z;
1332
bake_parameters.bias = p_bias;
1333
bake_parameters.to_cell_offset[0] = bounds.position.x;
1334
bake_parameters.to_cell_offset[1] = bounds.position.y;
1335
bake_parameters.to_cell_offset[2] = bounds.position.z;
1336
bake_parameters.grid_size = grid_size;
1337
bake_parameters.to_cell_size[0] = (1.0 / bounds.size.x) * float(grid_size);
1338
bake_parameters.to_cell_size[1] = (1.0 / bounds.size.y) * float(grid_size);
1339
bake_parameters.to_cell_size[2] = (1.0 / bounds.size.z) * float(grid_size);
1340
bake_parameters.light_count = lights.size();
1341
bake_parameters.env_transform[0] = p_environment_transform.rows[0][0];
1342
bake_parameters.env_transform[1] = p_environment_transform.rows[1][0];
1343
bake_parameters.env_transform[2] = p_environment_transform.rows[2][0];
1344
bake_parameters.env_transform[3] = 0.0f;
1345
bake_parameters.env_transform[4] = p_environment_transform.rows[0][1];
1346
bake_parameters.env_transform[5] = p_environment_transform.rows[1][1];
1347
bake_parameters.env_transform[6] = p_environment_transform.rows[2][1];
1348
bake_parameters.env_transform[7] = 0.0f;
1349
bake_parameters.env_transform[8] = p_environment_transform.rows[0][2];
1350
bake_parameters.env_transform[9] = p_environment_transform.rows[1][2];
1351
bake_parameters.env_transform[10] = p_environment_transform.rows[2][2];
1352
bake_parameters.env_transform[11] = 0.0f;
1353
bake_parameters.atlas_size[0] = atlas_size.width;
1354
bake_parameters.atlas_size[1] = atlas_size.height;
1355
bake_parameters.exposure_normalization = p_exposure_normalization;
1356
bake_parameters.bounces = p_bounces;
1357
bake_parameters.bounce_indirect_energy = p_bounce_indirect_energy;
1358
bake_parameters.shadowmask_light_idx = shadowmask_light_idx;
1359
// Same number of rays for transparency regardless of quality (it's more of a retry rather than shooting new ones).
1360
bake_parameters.transparency_rays = GLOBAL_GET("rendering/lightmapping/bake_performance/max_transparency_rays");
1361
bake_parameters.supersampling_factor = p_supersampling_factor;
1362
1363
bake_parameters_buffer = rd->uniform_buffer_create(sizeof(BakeParameters));
1364
rd->buffer_update(bake_parameters_buffer, 0, sizeof(BakeParameters), &bake_parameters);
1365
1366
if (p_step_function) {
1367
if (p_step_function(0.47, RTR("Preparing shaders"), p_bake_userdata, true)) {
1368
FREE_TEXTURES
1369
FREE_BUFFERS
1370
memdelete(rd);
1371
if (rcd != nullptr) {
1372
memdelete(rcd);
1373
}
1374
return BAKE_ERROR_USER_ABORTED;
1375
}
1376
}
1377
1378
//shaders
1379
Ref<RDShaderFile> raster_shader;
1380
raster_shader.instantiate();
1381
err = raster_shader->parse_versions_from_text(lm_raster_shader_glsl);
1382
if (err != OK) {
1383
raster_shader->print_errors("raster_shader");
1384
1385
FREE_TEXTURES
1386
FREE_BUFFERS
1387
1388
memdelete(rd);
1389
1390
if (rcd != nullptr) {
1391
memdelete(rcd);
1392
}
1393
}
1394
ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
1395
1396
RID rasterize_shader = rd->shader_create_from_spirv(raster_shader->get_spirv_stages());
1397
1398
ERR_FAIL_COND_V(rasterize_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //this is a bug check, though, should not happen
1399
1400
RID sampler;
1401
{
1402
RD::SamplerState s;
1403
s.mag_filter = RD::SAMPLER_FILTER_LINEAR;
1404
s.min_filter = RD::SAMPLER_FILTER_LINEAR;
1405
s.max_lod = 0;
1406
1407
sampler = rd->sampler_create(s);
1408
}
1409
1410
Vector<RD::Uniform> base_uniforms;
1411
{
1412
{
1413
RD::Uniform u;
1414
u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
1415
u.binding = 0;
1416
u.append_id(bake_parameters_buffer);
1417
base_uniforms.push_back(u);
1418
}
1419
{
1420
RD::Uniform u;
1421
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
1422
u.binding = 1;
1423
u.append_id(vertex_buffer);
1424
base_uniforms.push_back(u);
1425
}
1426
{
1427
RD::Uniform u;
1428
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
1429
u.binding = 2;
1430
u.append_id(triangle_buffer);
1431
base_uniforms.push_back(u);
1432
}
1433
{
1434
RD::Uniform u;
1435
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
1436
u.binding = 3;
1437
u.append_id(triangle_indices_buffer);
1438
base_uniforms.push_back(u);
1439
}
1440
{
1441
RD::Uniform u;
1442
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
1443
u.binding = 4;
1444
u.append_id(lights_buffer);
1445
base_uniforms.push_back(u);
1446
}
1447
{
1448
RD::Uniform u;
1449
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
1450
u.binding = 5;
1451
u.append_id(seams_buffer);
1452
base_uniforms.push_back(u);
1453
}
1454
{
1455
RD::Uniform u;
1456
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
1457
u.binding = 6;
1458
u.append_id(probe_positions_buffer);
1459
base_uniforms.push_back(u);
1460
}
1461
{
1462
RD::Uniform u;
1463
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1464
u.binding = 7;
1465
u.append_id(grid_texture);
1466
base_uniforms.push_back(u);
1467
}
1468
{
1469
RD::Uniform u;
1470
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1471
u.binding = 8;
1472
u.append_id(albedo_array_tex);
1473
base_uniforms.push_back(u);
1474
}
1475
{
1476
RD::Uniform u;
1477
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1478
u.binding = 9;
1479
u.append_id(emission_array_tex);
1480
base_uniforms.push_back(u);
1481
}
1482
{
1483
RD::Uniform u;
1484
u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
1485
u.binding = 10;
1486
u.append_id(sampler);
1487
base_uniforms.push_back(u);
1488
}
1489
{
1490
RD::Uniform u;
1491
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
1492
u.binding = 11;
1493
u.append_id(cluster_indices_buffer);
1494
base_uniforms.push_back(u);
1495
}
1496
{
1497
RD::Uniform u;
1498
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
1499
u.binding = 12;
1500
u.append_id(cluster_aabbs_buffer);
1501
base_uniforms.push_back(u);
1502
}
1503
}
1504
1505
RID raster_base_uniform = rd->uniform_set_create(base_uniforms, rasterize_shader, 0);
1506
RID raster_depth_buffer;
1507
{
1508
RD::TextureFormat tf;
1509
tf.width = atlas_size.width;
1510
tf.height = atlas_size.height;
1511
tf.depth = 1;
1512
tf.texture_type = RD::TEXTURE_TYPE_2D;
1513
tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
1514
tf.format = RD::DATA_FORMAT_D32_SFLOAT;
1515
tf.is_discardable = true;
1516
1517
raster_depth_buffer = rd->texture_create(tf, RD::TextureView());
1518
}
1519
1520
rd->submit();
1521
rd->sync();
1522
1523
/* STEP 3: Raster the geometry to UV2 coords in the atlas textures GPU*/
1524
1525
_raster_geometry(rd, atlas_size, atlas_slices, grid_size, bounds, p_bias, slice_triangle_count, position_tex, unocclude_tex, normal_tex, raster_depth_buffer, rasterize_shader, raster_base_uniform);
1526
1527
#ifdef DEBUG_TEXTURES
1528
1529
for (int i = 0; i < atlas_slices; i++) {
1530
Vector<uint8_t> s = rd->texture_get_data(position_tex, i);
1531
Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAF, s);
1532
img->save_exr("res://1_position_" + itos(i) + ".exr", false);
1533
1534
s = rd->texture_get_data(normal_tex, i);
1535
img->set_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
1536
img->save_exr("res://1_normal_" + itos(i) + ".exr", false);
1537
}
1538
#endif
1539
1540
#define FREE_RASTER_RESOURCES \
1541
rd->free_rid(rasterize_shader); \
1542
rd->free_rid(sampler); \
1543
rd->free_rid(raster_depth_buffer);
1544
1545
/* Plot direct light */
1546
1547
Ref<RDShaderFile> compute_shader;
1548
String defines = "";
1549
defines += "\n#define CLUSTER_SIZE " + uitos(cluster_size) + "\n";
1550
1551
if (p_bake_sh) {
1552
defines += "\n#define USE_SH_LIGHTMAPS\n";
1553
}
1554
1555
if (p_texture_for_bounces) {
1556
defines += "\n#define USE_LIGHT_TEXTURE_FOR_BOUNCES\n";
1557
}
1558
1559
if (p_bake_shadowmask) {
1560
defines += "\n#define USE_SHADOWMASK\n";
1561
}
1562
1563
compute_shader.instantiate();
1564
err = compute_shader->parse_versions_from_text(lm_compute_shader_glsl, defines);
1565
if (err != OK) {
1566
FREE_TEXTURES
1567
FREE_BUFFERS
1568
FREE_RASTER_RESOURCES
1569
memdelete(rd);
1570
1571
if (rcd != nullptr) {
1572
memdelete(rcd);
1573
}
1574
1575
compute_shader->print_errors("compute_shader");
1576
}
1577
ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
1578
1579
// Unoccluder
1580
RID compute_shader_unocclude = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("unocclude"));
1581
ERR_FAIL_COND_V(compute_shader_unocclude.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); // internal check, should not happen
1582
RID compute_shader_unocclude_pipeline = rd->compute_pipeline_create(compute_shader_unocclude);
1583
1584
// Direct light
1585
RID compute_shader_primary = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("primary"));
1586
ERR_FAIL_COND_V(compute_shader_primary.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); // internal check, should not happen
1587
RID compute_shader_primary_pipeline = rd->compute_pipeline_create(compute_shader_primary);
1588
1589
// Indirect light
1590
RID compute_shader_secondary = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("secondary"));
1591
ERR_FAIL_COND_V(compute_shader_secondary.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen
1592
RID compute_shader_secondary_pipeline = rd->compute_pipeline_create(compute_shader_secondary);
1593
1594
// Light probes
1595
RID compute_shader_light_probes = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("light_probes"));
1596
ERR_FAIL_COND_V(compute_shader_light_probes.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen
1597
RID compute_shader_light_probes_pipeline = rd->compute_pipeline_create(compute_shader_light_probes);
1598
1599
RID compute_base_uniform_set = rd->uniform_set_create(base_uniforms, compute_shader_primary, 0);
1600
1601
#define FREE_COMPUTE_RESOURCES \
1602
rd->free_rid(compute_shader_unocclude); \
1603
rd->free_rid(compute_shader_primary); \
1604
rd->free_rid(compute_shader_secondary); \
1605
rd->free_rid(compute_shader_light_probes);
1606
1607
Vector3i group_size(Math::division_round_up(atlas_size.x, 8), Math::division_round_up(atlas_size.y, 8), 1);
1608
rd->submit();
1609
rd->sync();
1610
1611
if (p_step_function) {
1612
if (p_step_function(0.49, RTR("Un-occluding geometry"), p_bake_userdata, true)) {
1613
FREE_TEXTURES
1614
FREE_BUFFERS
1615
FREE_RASTER_RESOURCES
1616
FREE_COMPUTE_RESOURCES
1617
memdelete(rd);
1618
if (rcd != nullptr) {
1619
memdelete(rcd);
1620
}
1621
return BAKE_ERROR_USER_ABORTED;
1622
}
1623
}
1624
1625
PushConstant push_constant;
1626
push_constant.denoiser_range = p_use_denoiser ? p_denoiser_range : 1.0;
1627
1628
/* UNOCCLUDE */
1629
{
1630
Vector<RD::Uniform> uniforms;
1631
{
1632
{
1633
RD::Uniform u;
1634
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1635
u.binding = 0;
1636
u.append_id(position_tex);
1637
uniforms.push_back(u);
1638
}
1639
{
1640
RD::Uniform u;
1641
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1642
u.binding = 1;
1643
u.append_id(unocclude_tex);
1644
uniforms.push_back(u);
1645
}
1646
}
1647
1648
RID unocclude_uniform_set = rd->uniform_set_create(uniforms, compute_shader_unocclude, 1);
1649
1650
RD::ComputeListID compute_list = rd->compute_list_begin();
1651
rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_unocclude_pipeline);
1652
rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
1653
rd->compute_list_bind_uniform_set(compute_list, unocclude_uniform_set, 1);
1654
1655
for (int i = 0; i < atlas_slices; i++) {
1656
push_constant.atlas_slice = i;
1657
rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
1658
rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
1659
//no barrier, let them run all together
1660
}
1661
rd->compute_list_end(); //done
1662
}
1663
1664
#ifdef DEBUG_TEXTURES
1665
for (int i = 0; i < atlas_slices; i++) {
1666
Vector<uint8_t> s = rd->texture_get_data(unocclude_tex, i);
1667
Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAF, s);
1668
img->save_exr("res://1_unocclude_" + itos(i) + ".exr", false);
1669
}
1670
#endif
1671
1672
if (p_step_function) {
1673
if (p_step_function(0.5, RTR("Plot direct lighting"), p_bake_userdata, true)) {
1674
FREE_TEXTURES
1675
FREE_BUFFERS
1676
FREE_RASTER_RESOURCES
1677
FREE_COMPUTE_RESOURCES
1678
memdelete(rd);
1679
if (rcd != nullptr) {
1680
memdelete(rcd);
1681
}
1682
return BAKE_ERROR_USER_ABORTED;
1683
}
1684
}
1685
1686
const int max_region_size = nearest_power_of_2_templated(int(GLOBAL_GET("rendering/lightmapping/bake_performance/region_size")));
1687
const int x_regions = Math::division_round_up(atlas_size.width, max_region_size);
1688
const int y_regions = Math::division_round_up(atlas_size.height, max_region_size);
1689
1690
// Set ray count to the quality used for direct light and bounces.
1691
switch (p_quality) {
1692
case BAKE_QUALITY_LOW: {
1693
push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/low_quality_ray_count");
1694
} break;
1695
case BAKE_QUALITY_MEDIUM: {
1696
push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/medium_quality_ray_count");
1697
} break;
1698
case BAKE_QUALITY_HIGH: {
1699
push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/high_quality_ray_count");
1700
} break;
1701
case BAKE_QUALITY_ULTRA: {
1702
push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/ultra_quality_ray_count");
1703
} break;
1704
}
1705
1706
push_constant.ray_count = CLAMP(push_constant.ray_count, 16u, 8192u);
1707
1708
/* PRIMARY (direct) LIGHT PASS */
1709
{
1710
Vector<RD::Uniform> uniforms;
1711
{
1712
{
1713
RD::Uniform u;
1714
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1715
u.binding = 0;
1716
u.append_id(light_source_tex);
1717
uniforms.push_back(u);
1718
}
1719
{
1720
RD::Uniform u;
1721
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1722
u.binding = 1;
1723
u.append_id(light_dest_tex); //will be unused
1724
uniforms.push_back(u);
1725
}
1726
{
1727
RD::Uniform u;
1728
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1729
u.binding = 2;
1730
u.append_id(position_tex);
1731
uniforms.push_back(u);
1732
}
1733
{
1734
RD::Uniform u;
1735
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1736
u.binding = 3;
1737
u.append_id(normal_tex);
1738
uniforms.push_back(u);
1739
}
1740
{
1741
RD::Uniform u;
1742
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1743
u.binding = 4;
1744
u.append_id(light_accum_tex);
1745
uniforms.push_back(u);
1746
}
1747
1748
if (p_bake_shadowmask) {
1749
RD::Uniform u;
1750
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1751
u.binding = 5;
1752
u.append_id(shadowmask_tex);
1753
uniforms.push_back(u);
1754
}
1755
}
1756
1757
RID light_uniform_set = rd->uniform_set_create(uniforms, compute_shader_primary, 1);
1758
1759
int count = 0;
1760
for (int s = 0; s < atlas_slices; s++) {
1761
push_constant.atlas_slice = s;
1762
1763
for (int i = 0; i < x_regions; i++) {
1764
for (int j = 0; j < y_regions; j++) {
1765
int x = i * max_region_size;
1766
int y = j * max_region_size;
1767
int w = MIN((i + 1) * max_region_size, atlas_size.width) - x;
1768
int h = MIN((j + 1) * max_region_size, atlas_size.height) - y;
1769
1770
push_constant.region_ofs[0] = x;
1771
push_constant.region_ofs[1] = y;
1772
1773
group_size = Vector3i(Math::division_round_up(w, 8), Math::division_round_up(h, 8), 1);
1774
RD::ComputeListID compute_list = rd->compute_list_begin();
1775
rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_primary_pipeline);
1776
rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
1777
rd->compute_list_bind_uniform_set(compute_list, light_uniform_set, 1);
1778
rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
1779
rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
1780
rd->compute_list_end();
1781
1782
rd->submit();
1783
rd->sync();
1784
1785
count++;
1786
if (p_step_function) {
1787
int total = (atlas_slices * x_regions * y_regions);
1788
int percent = count * 100 / total;
1789
float p = float(count) / total * 0.1;
1790
if (p_step_function(0.5 + p, vformat(RTR("Plot direct lighting %d%%"), percent), p_bake_userdata, false)) {
1791
FREE_TEXTURES
1792
FREE_BUFFERS
1793
FREE_RASTER_RESOURCES
1794
FREE_COMPUTE_RESOURCES
1795
memdelete(rd);
1796
if (rcd != nullptr) {
1797
memdelete(rcd);
1798
}
1799
return BAKE_ERROR_USER_ABORTED;
1800
}
1801
}
1802
}
1803
}
1804
}
1805
}
1806
1807
#ifdef DEBUG_TEXTURES
1808
1809
for (int i = 0; i < atlas_slices; i++) {
1810
Vector<uint8_t> s = rd->texture_get_data(light_source_tex, i);
1811
Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
1812
img->save_exr("res://2_light_primary_" + itos(i) + ".exr", false);
1813
}
1814
1815
if (p_bake_sh) {
1816
for (int i = 0; i < atlas_slices * 4; i++) {
1817
Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
1818
Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
1819
img->save_exr("res://2_light_primary_accum_" + itos(i) + ".exr", false);
1820
}
1821
}
1822
#endif
1823
1824
/* SECONDARY (indirect) LIGHT PASS(ES) */
1825
1826
if (p_bounces > 0) {
1827
Vector<RD::Uniform> uniforms;
1828
{
1829
{
1830
// Unused.
1831
RD::Uniform u;
1832
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1833
u.binding = 0;
1834
u.append_id(light_dest_tex);
1835
uniforms.push_back(u);
1836
}
1837
{
1838
RD::Uniform u;
1839
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1840
u.binding = 1;
1841
u.append_id(light_source_tex);
1842
uniforms.push_back(u);
1843
}
1844
{
1845
RD::Uniform u;
1846
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1847
u.binding = 2;
1848
u.append_id(position_tex);
1849
uniforms.push_back(u);
1850
}
1851
{
1852
RD::Uniform u;
1853
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1854
u.binding = 3;
1855
u.append_id(normal_tex);
1856
uniforms.push_back(u);
1857
}
1858
{
1859
RD::Uniform u;
1860
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1861
u.binding = 4;
1862
u.append_id(light_accum_tex);
1863
uniforms.push_back(u);
1864
}
1865
{
1866
RD::Uniform u;
1867
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1868
u.binding = 5;
1869
u.append_id(light_environment_tex);
1870
uniforms.push_back(u);
1871
}
1872
}
1873
1874
RID secondary_uniform_set;
1875
secondary_uniform_set = rd->uniform_set_create(uniforms, compute_shader_secondary, 1);
1876
1877
const int max_rays = GLOBAL_GET("rendering/lightmapping/bake_performance/max_rays_per_pass");
1878
int ray_iterations = Math::division_round_up((int32_t)push_constant.ray_count, max_rays);
1879
1880
if (p_step_function) {
1881
if (p_step_function(0.6, RTR("Integrate indirect lighting"), p_bake_userdata, true)) {
1882
FREE_TEXTURES
1883
FREE_BUFFERS
1884
FREE_RASTER_RESOURCES
1885
FREE_COMPUTE_RESOURCES
1886
memdelete(rd);
1887
if (rcd != nullptr) {
1888
memdelete(rcd);
1889
}
1890
return BAKE_ERROR_USER_ABORTED;
1891
}
1892
}
1893
1894
int count = 0;
1895
for (int s = 0; s < atlas_slices; s++) {
1896
push_constant.atlas_slice = s;
1897
1898
for (int i = 0; i < x_regions; i++) {
1899
for (int j = 0; j < y_regions; j++) {
1900
int x = i * max_region_size;
1901
int y = j * max_region_size;
1902
int w = MIN((i + 1) * max_region_size, atlas_size.width) - x;
1903
int h = MIN((j + 1) * max_region_size, atlas_size.height) - y;
1904
1905
push_constant.region_ofs[0] = x;
1906
push_constant.region_ofs[1] = y;
1907
1908
group_size = Vector3i(Math::division_round_up(w, 8), Math::division_round_up(h, 8), 1);
1909
1910
for (int k = 0; k < ray_iterations; k++) {
1911
RD::ComputeListID compute_list = rd->compute_list_begin();
1912
rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_secondary_pipeline);
1913
rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
1914
rd->compute_list_bind_uniform_set(compute_list, secondary_uniform_set, 1);
1915
1916
push_constant.ray_from = k * max_rays;
1917
push_constant.ray_to = MIN((k + 1) * max_rays, int32_t(push_constant.ray_count));
1918
rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
1919
rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
1920
1921
rd->compute_list_end();
1922
rd->submit();
1923
rd->sync();
1924
1925
count++;
1926
if (p_step_function) {
1927
int total = (atlas_slices * x_regions * y_regions * ray_iterations);
1928
int percent = count * 100 / total;
1929
float p = float(count) / total * 0.1;
1930
if (p_step_function(0.6 + p, vformat(RTR("Integrate indirect lighting %d%%"), percent), p_bake_userdata, false)) {
1931
FREE_TEXTURES
1932
FREE_BUFFERS
1933
FREE_RASTER_RESOURCES
1934
FREE_COMPUTE_RESOURCES
1935
memdelete(rd);
1936
if (rcd != nullptr) {
1937
memdelete(rcd);
1938
}
1939
return BAKE_ERROR_USER_ABORTED;
1940
}
1941
}
1942
}
1943
}
1944
}
1945
}
1946
}
1947
1948
/* LIGHTPROBES */
1949
1950
RID light_probe_buffer;
1951
1952
if (probe_positions.size()) {
1953
light_probe_buffer = rd->storage_buffer_create(sizeof(float) * 4 * 9 * probe_positions.size());
1954
1955
if (p_step_function) {
1956
if (p_step_function(0.7, RTR("Baking light probes"), p_bake_userdata, true)) {
1957
FREE_TEXTURES
1958
FREE_BUFFERS
1959
FREE_RASTER_RESOURCES
1960
FREE_COMPUTE_RESOURCES
1961
if (probe_positions.size() > 0) {
1962
rd->free_rid(light_probe_buffer);
1963
}
1964
memdelete(rd);
1965
if (rcd != nullptr) {
1966
memdelete(rcd);
1967
}
1968
return BAKE_ERROR_USER_ABORTED;
1969
}
1970
}
1971
1972
Vector<RD::Uniform> uniforms;
1973
{
1974
{
1975
RD::Uniform u;
1976
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
1977
u.binding = 0;
1978
u.append_id(light_probe_buffer);
1979
uniforms.push_back(u);
1980
}
1981
{
1982
RD::Uniform u;
1983
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1984
u.binding = 1;
1985
u.append_id(light_source_tex);
1986
uniforms.push_back(u);
1987
}
1988
{
1989
RD::Uniform u;
1990
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
1991
u.binding = 2;
1992
u.append_id(light_environment_tex);
1993
uniforms.push_back(u);
1994
}
1995
}
1996
RID light_probe_uniform_set = rd->uniform_set_create(uniforms, compute_shader_light_probes, 1);
1997
1998
switch (p_quality) {
1999
case BAKE_QUALITY_LOW: {
2000
push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/low_quality_probe_ray_count");
2001
} break;
2002
case BAKE_QUALITY_MEDIUM: {
2003
push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/medium_quality_probe_ray_count");
2004
} break;
2005
case BAKE_QUALITY_HIGH: {
2006
push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/high_quality_probe_ray_count");
2007
} break;
2008
case BAKE_QUALITY_ULTRA: {
2009
push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/ultra_quality_probe_ray_count");
2010
} break;
2011
}
2012
2013
push_constant.ray_count = CLAMP(push_constant.ray_count, 16u, 8192u);
2014
push_constant.probe_count = probe_positions.size();
2015
2016
int max_rays = GLOBAL_GET("rendering/lightmapping/bake_performance/max_rays_per_probe_pass");
2017
int ray_iterations = Math::division_round_up((int32_t)push_constant.ray_count, max_rays);
2018
2019
for (int i = 0; i < ray_iterations; i++) {
2020
RD::ComputeListID compute_list = rd->compute_list_begin();
2021
rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_light_probes_pipeline);
2022
rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
2023
rd->compute_list_bind_uniform_set(compute_list, light_probe_uniform_set, 1);
2024
2025
push_constant.ray_from = i * max_rays;
2026
push_constant.ray_to = MIN((i + 1) * max_rays, int32_t(push_constant.ray_count));
2027
rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
2028
rd->compute_list_dispatch(compute_list, Math::division_round_up((int)probe_positions.size(), 64), 1, 1);
2029
2030
rd->compute_list_end(); //done
2031
rd->submit();
2032
rd->sync();
2033
2034
if (p_step_function) {
2035
int percent = i * 100 / ray_iterations;
2036
float p = float(i) / ray_iterations * 0.1;
2037
if (p_step_function(0.7 + p, vformat(RTR("Integrating light probes %d%%"), percent), p_bake_userdata, false)) {
2038
FREE_TEXTURES
2039
FREE_BUFFERS
2040
FREE_RASTER_RESOURCES
2041
FREE_COMPUTE_RESOURCES
2042
if (probe_positions.size() > 0) {
2043
rd->free_rid(light_probe_buffer);
2044
}
2045
memdelete(rd);
2046
if (rcd != nullptr) {
2047
memdelete(rcd);
2048
}
2049
return BAKE_ERROR_USER_ABORTED;
2050
}
2051
}
2052
}
2053
}
2054
2055
#if 0
2056
for (int i = 0; i < probe_positions.size(); i++) {
2057
Ref<Image> img = Image::create_empty(6, 4, false, Image::FORMAT_RGB8);
2058
for (int j = 0; j < 6; j++) {
2059
Vector<uint8_t> s = rd->texture_get_data(lightprobe_tex, i * 6 + j);
2060
Ref<Image> img2 = Image::create_from_data(2, 2, false, Image::FORMAT_RGBAF, s);
2061
img2->convert(Image::FORMAT_RGB8);
2062
img->blit_rect(img2, Rect2i(0, 0, 2, 2), Point2i((j % 3) * 2, (j / 3) * 2));
2063
}
2064
img->save_png("res://3_light_probe_" + itos(i) + ".png");
2065
}
2066
#endif
2067
2068
/* DENOISE */
2069
2070
if (p_bake_sh) {
2071
SWAP(light_accum_tex, light_accum_tex2);
2072
BakeError error = _pack_l1(rd, compute_shader, compute_base_uniform_set, push_constant, light_accum_tex2, light_accum_tex, atlas_size, atlas_slices);
2073
if (unlikely(error != BAKE_OK)) {
2074
return error;
2075
}
2076
}
2077
2078
if (p_use_denoiser) {
2079
if (p_step_function) {
2080
if (p_step_function(0.8, RTR("Denoising"), p_bake_userdata, true)) {
2081
FREE_TEXTURES
2082
FREE_BUFFERS
2083
FREE_RASTER_RESOURCES
2084
FREE_COMPUTE_RESOURCES
2085
if (probe_positions.size() > 0) {
2086
rd->free_rid(light_probe_buffer);
2087
}
2088
memdelete(rd);
2089
if (rcd != nullptr) {
2090
memdelete(rcd);
2091
}
2092
return BAKE_ERROR_USER_ABORTED;
2093
}
2094
}
2095
2096
{
2097
BakeError error;
2098
if (denoiser == 1) {
2099
// OIDN (external).
2100
error = _denoise_oidn(rd, light_accum_tex, normal_tex, light_accum_tex, atlas_size, atlas_slices, p_bake_sh, false, oidn_path);
2101
} else {
2102
// JNLM (built-in).
2103
SWAP(light_accum_tex, light_accum_tex2);
2104
error = _denoise(rd, compute_shader, compute_base_uniform_set, push_constant, light_accum_tex2, normal_tex, light_accum_tex, unocclude_tex, p_denoiser_strength, p_denoiser_range, atlas_size, atlas_slices, p_bake_sh, p_step_function, p_bake_userdata);
2105
}
2106
if (unlikely(error != BAKE_OK)) {
2107
return error;
2108
}
2109
}
2110
2111
if (p_bake_shadowmask) {
2112
BakeError error;
2113
if (denoiser == 1) {
2114
// OIDN (external).
2115
error = _denoise_oidn(rd, shadowmask_tex, normal_tex, shadowmask_tex, atlas_size, atlas_slices, false, true, oidn_path);
2116
} else {
2117
// JNLM (built-in).
2118
SWAP(shadowmask_tex, shadowmask_tex2);
2119
error = _denoise(rd, compute_shader, compute_base_uniform_set, push_constant, shadowmask_tex2, normal_tex, shadowmask_tex, unocclude_tex, p_denoiser_strength, p_denoiser_range, atlas_size, atlas_slices, false, p_step_function, p_bake_userdata);
2120
}
2121
if (unlikely(error != BAKE_OK)) {
2122
return error;
2123
}
2124
}
2125
}
2126
2127
/* DILATE */
2128
2129
{
2130
SWAP(light_accum_tex, light_accum_tex2);
2131
BakeError error = _dilate(rd, compute_shader, compute_base_uniform_set, push_constant, light_accum_tex2, light_accum_tex, atlas_size, atlas_slices * (p_bake_sh ? 4 : 1));
2132
if (unlikely(error != BAKE_OK)) {
2133
return error;
2134
}
2135
2136
if (p_bake_shadowmask) {
2137
SWAP(shadowmask_tex, shadowmask_tex2);
2138
error = _dilate(rd, compute_shader, compute_base_uniform_set, push_constant, shadowmask_tex2, shadowmask_tex, atlas_size, atlas_slices);
2139
if (unlikely(error != BAKE_OK)) {
2140
return error;
2141
}
2142
}
2143
}
2144
2145
#ifdef DEBUG_TEXTURES
2146
2147
for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
2148
Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
2149
Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
2150
img->save_exr("res://4_light_secondary_" + itos(i) + ".exr", false);
2151
}
2152
#endif
2153
2154
/* BLEND SEAMS */
2155
//shaders
2156
Ref<RDShaderFile> blendseams_shader;
2157
blendseams_shader.instantiate();
2158
err = blendseams_shader->parse_versions_from_text(lm_blendseams_shader_glsl);
2159
if (err != OK) {
2160
FREE_TEXTURES
2161
FREE_BUFFERS
2162
FREE_RASTER_RESOURCES
2163
FREE_COMPUTE_RESOURCES
2164
memdelete(rd);
2165
2166
if (rcd != nullptr) {
2167
memdelete(rcd);
2168
}
2169
2170
blendseams_shader->print_errors("blendseams_shader");
2171
}
2172
ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
2173
2174
RID blendseams_line_raster_shader = rd->shader_create_from_spirv(blendseams_shader->get_spirv_stages("lines"));
2175
2176
ERR_FAIL_COND_V(blendseams_line_raster_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
2177
2178
RID blendseams_triangle_raster_shader = rd->shader_create_from_spirv(blendseams_shader->get_spirv_stages("triangles"));
2179
2180
ERR_FAIL_COND_V(blendseams_triangle_raster_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
2181
2182
#define FREE_BLENDSEAMS_RESOURCES \
2183
rd->free_rid(blendseams_line_raster_shader); \
2184
rd->free_rid(blendseams_triangle_raster_shader);
2185
2186
{
2187
//pre copy
2188
for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
2189
rd->texture_copy(light_accum_tex, light_accum_tex2, Vector3(), Vector3(), Vector3(atlas_size.width, atlas_size.height, 1), 0, 0, i, i);
2190
}
2191
2192
Vector<RID> framebuffers;
2193
for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
2194
RID slice_tex = rd->texture_create_shared_from_slice(RD::TextureView(), light_accum_tex, i, 0);
2195
Vector<RID> fb;
2196
fb.push_back(slice_tex);
2197
fb.push_back(raster_depth_buffer);
2198
framebuffers.push_back(rd->framebuffer_create(fb));
2199
}
2200
2201
Vector<RD::Uniform> uniforms;
2202
{
2203
{
2204
RD::Uniform u;
2205
u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
2206
u.binding = 0;
2207
u.append_id(light_accum_tex2);
2208
uniforms.push_back(u);
2209
}
2210
}
2211
2212
RID blendseams_raster_uniform = rd->uniform_set_create(uniforms, blendseams_line_raster_shader, 1);
2213
2214
bool debug = false;
2215
RD::PipelineColorBlendState bs = RD::PipelineColorBlendState::create_blend(1);
2216
bs.attachments.write[0].src_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;
2217
bs.attachments.write[0].dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
2218
2219
RD::PipelineDepthStencilState ds;
2220
ds.enable_depth_test = true;
2221
ds.enable_depth_write = true;
2222
ds.depth_compare_operator = RD::COMPARE_OP_LESS; //so it does not render same pixel twice, this avoids wrong blending
2223
2224
RID blendseams_line_raster_pipeline = rd->render_pipeline_create(blendseams_line_raster_shader, rd->framebuffer_get_format(framebuffers[0]), RD::INVALID_FORMAT_ID, RD::RENDER_PRIMITIVE_LINES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), ds, bs, 0);
2225
RID blendseams_triangle_raster_pipeline = rd->render_pipeline_create(blendseams_triangle_raster_shader, rd->framebuffer_get_format(framebuffers[0]), RD::INVALID_FORMAT_ID, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), ds, bs, 0);
2226
2227
uint32_t seam_offset = 0;
2228
uint32_t triangle_offset = 0;
2229
2230
for (int i = 0; i < atlas_slices; i++) {
2231
int subslices = (p_bake_sh ? 4 : 1);
2232
2233
if (slice_seam_count[i] == 0) {
2234
continue;
2235
}
2236
2237
for (int k = 0; k < subslices; k++) {
2238
RasterSeamsPushConstant seams_push_constant;
2239
seams_push_constant.slice = uint32_t(i * subslices + k);
2240
seams_push_constant.debug = debug;
2241
2242
// Store the current subslice in the breadcrumb.
2243
RD::DrawListID draw_list = rd->draw_list_begin(framebuffers[i * subslices + k], RD::DRAW_CLEAR_DEPTH, Vector<Color>(), 1.0f, 0, Rect2(), RDD::BreadcrumbMarker::LIGHTMAPPER_PASS | seams_push_constant.slice);
2244
2245
rd->draw_list_bind_uniform_set(draw_list, raster_base_uniform, 0);
2246
rd->draw_list_bind_uniform_set(draw_list, blendseams_raster_uniform, 1);
2247
2248
const int uv_offset_count = 9;
2249
static const Vector3 uv_offsets[uv_offset_count] = {
2250
Vector3(0, 0, 0.5), //using zbuffer, so go inwards-outwards
2251
Vector3(0, 1, 0.2),
2252
Vector3(0, -1, 0.2),
2253
Vector3(1, 0, 0.2),
2254
Vector3(-1, 0, 0.2),
2255
Vector3(-1, -1, 0.1),
2256
Vector3(1, -1, 0.1),
2257
Vector3(1, 1, 0.1),
2258
Vector3(-1, 1, 0.1),
2259
};
2260
2261
/* step 1 use lines to blend the edges */
2262
{
2263
seams_push_constant.base_index = seam_offset;
2264
rd->draw_list_bind_render_pipeline(draw_list, blendseams_line_raster_pipeline);
2265
seams_push_constant.uv_offset[0] = (uv_offsets[0].x - 0.5f) / float(atlas_size.width);
2266
seams_push_constant.uv_offset[1] = (uv_offsets[0].y - 0.5f) / float(atlas_size.height);
2267
seams_push_constant.blend = uv_offsets[0].z;
2268
2269
rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant));
2270
rd->draw_list_draw(draw_list, false, 1, slice_seam_count[i] * 4);
2271
}
2272
2273
/* step 2 use triangles to mask the interior */
2274
2275
{
2276
seams_push_constant.base_index = triangle_offset;
2277
rd->draw_list_bind_render_pipeline(draw_list, blendseams_triangle_raster_pipeline);
2278
seams_push_constant.blend = 0; //do not draw them, just fill the z-buffer so its used as a mask
2279
2280
rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant));
2281
rd->draw_list_draw(draw_list, false, 1, slice_triangle_count[i] * 3);
2282
}
2283
/* step 3 blend around the triangle */
2284
2285
rd->draw_list_bind_render_pipeline(draw_list, blendseams_line_raster_pipeline);
2286
2287
for (int j = 1; j < uv_offset_count; j++) {
2288
seams_push_constant.base_index = seam_offset;
2289
seams_push_constant.uv_offset[0] = (uv_offsets[j].x - 0.5f) / float(atlas_size.width);
2290
seams_push_constant.uv_offset[1] = (uv_offsets[j].y - 0.5f) / float(atlas_size.height);
2291
seams_push_constant.blend = uv_offsets[0].z;
2292
2293
rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant));
2294
rd->draw_list_draw(draw_list, false, 1, slice_seam_count[i] * 4);
2295
}
2296
rd->draw_list_end();
2297
}
2298
seam_offset += slice_seam_count[i];
2299
triangle_offset += slice_triangle_count[i];
2300
}
2301
}
2302
2303
#ifdef DEBUG_TEXTURES
2304
2305
for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
2306
Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
2307
Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
2308
img->save_exr("res://5_blendseams" + itos(i) + ".exr", false);
2309
}
2310
#endif
2311
2312
if (p_step_function) {
2313
p_step_function(0.9, RTR("Retrieving textures"), p_bake_userdata, true);
2314
}
2315
2316
for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
2317
Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
2318
Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
2319
img->convert(Image::FORMAT_RGBH); //remove alpha
2320
lightmap_textures.push_back(img);
2321
}
2322
2323
if (p_bake_shadowmask) {
2324
for (int i = 0; i < atlas_slices; i++) {
2325
Vector<uint8_t> s = rd->texture_get_data(shadowmask_tex, i);
2326
Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBA8, s);
2327
img->convert(Image::FORMAT_R8);
2328
shadowmask_textures.push_back(img);
2329
}
2330
}
2331
2332
if (probe_positions.size() > 0) {
2333
probe_values.resize(probe_positions.size() * 9);
2334
Vector<uint8_t> probe_data = rd->buffer_get_data(light_probe_buffer);
2335
memcpy(probe_values.ptrw(), probe_data.ptr(), probe_data.size());
2336
rd->free_rid(light_probe_buffer);
2337
2338
#ifdef DEBUG_TEXTURES
2339
{
2340
Ref<Image> img2 = Image::create_from_data(probe_values.size(), 1, false, Image::FORMAT_RGBAF, probe_data);
2341
img2->save_exr("res://6_lightprobes.exr", false);
2342
}
2343
#endif
2344
}
2345
2346
FREE_TEXTURES
2347
FREE_BUFFERS
2348
FREE_RASTER_RESOURCES
2349
FREE_COMPUTE_RESOURCES
2350
FREE_BLENDSEAMS_RESOURCES
2351
2352
memdelete(rd);
2353
2354
if (rcd != nullptr) {
2355
memdelete(rcd);
2356
}
2357
2358
return BAKE_OK;
2359
}
2360
2361
int LightmapperRD::get_bake_texture_count() const {
2362
return lightmap_textures.size();
2363
}
2364
2365
Ref<Image> LightmapperRD::get_bake_texture(int p_index) const {
2366
ERR_FAIL_INDEX_V(p_index, lightmap_textures.size(), Ref<Image>());
2367
return lightmap_textures[p_index];
2368
}
2369
2370
int LightmapperRD::get_shadowmask_texture_count() const {
2371
return shadowmask_textures.size();
2372
}
2373
2374
Ref<Image> LightmapperRD::get_shadowmask_texture(int p_index) const {
2375
ERR_FAIL_INDEX_V(p_index, shadowmask_textures.size(), Ref<Image>());
2376
return shadowmask_textures[p_index];
2377
}
2378
2379
int LightmapperRD::get_bake_mesh_count() const {
2380
return mesh_instances.size();
2381
}
2382
2383
Variant LightmapperRD::get_bake_mesh_userdata(int p_index) const {
2384
ERR_FAIL_INDEX_V(p_index, mesh_instances.size(), Variant());
2385
return mesh_instances[p_index].data.userdata;
2386
}
2387
2388
Rect2 LightmapperRD::get_bake_mesh_uv_scale(int p_index) const {
2389
ERR_FAIL_COND_V(lightmap_textures.is_empty(), Rect2());
2390
Rect2 uv_ofs;
2391
Vector2 atlas_size = Vector2(lightmap_textures[0]->get_width(), lightmap_textures[0]->get_height());
2392
uv_ofs.position = Vector2(mesh_instances[p_index].offset) / atlas_size;
2393
uv_ofs.size = Vector2(mesh_instances[p_index].data.albedo_on_uv2->get_width(), mesh_instances[p_index].data.albedo_on_uv2->get_height()) / atlas_size;
2394
return uv_ofs;
2395
}
2396
2397
int LightmapperRD::get_bake_mesh_texture_slice(int p_index) const {
2398
ERR_FAIL_INDEX_V(p_index, mesh_instances.size(), Variant());
2399
return mesh_instances[p_index].slice;
2400
}
2401
2402
int LightmapperRD::get_bake_probe_count() const {
2403
return probe_positions.size();
2404
}
2405
2406
Vector3 LightmapperRD::get_bake_probe_point(int p_probe) const {
2407
ERR_FAIL_INDEX_V(p_probe, probe_positions.size(), Variant());
2408
return Vector3(probe_positions[p_probe].position[0], probe_positions[p_probe].position[1], probe_positions[p_probe].position[2]);
2409
}
2410
2411
Vector<Color> LightmapperRD::get_bake_probe_sh(int p_probe) const {
2412
ERR_FAIL_INDEX_V(p_probe, probe_positions.size(), Vector<Color>());
2413
Vector<Color> ret;
2414
ret.resize(9);
2415
memcpy(ret.ptrw(), &probe_values[p_probe * 9], sizeof(Color) * 9);
2416
return ret;
2417
}
2418
2419
LightmapperRD::LightmapperRD() {
2420
}
2421
2422