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 "lm_blendseams.glsl.gen.h" |
34 | #include "lm_compute.glsl.gen.h" |
35 | #include "lm_raster.glsl.gen.h" |
36 | |
37 | #include "core/config/project_settings.h" |
38 | #include "core/math/geometry_2d.h" |
39 | #include "servers/rendering/rendering_device_binds.h" |
40 | |
41 | //uncomment this if you want to see textures from all the process saved |
42 | //#define DEBUG_TEXTURES |
43 | |
44 | void LightmapperRD::add_mesh(const MeshData &p_mesh) { |
45 | ERR_FAIL_COND(p_mesh.albedo_on_uv2.is_null() || p_mesh.albedo_on_uv2->is_empty()); |
46 | ERR_FAIL_COND(p_mesh.emission_on_uv2.is_null() || p_mesh.emission_on_uv2->is_empty()); |
47 | ERR_FAIL_COND(p_mesh.albedo_on_uv2->get_width() != p_mesh.emission_on_uv2->get_width()); |
48 | ERR_FAIL_COND(p_mesh.albedo_on_uv2->get_height() != p_mesh.emission_on_uv2->get_height()); |
49 | ERR_FAIL_COND(p_mesh.points.size() == 0); |
50 | MeshInstance mi; |
51 | mi.data = p_mesh; |
52 | mesh_instances.push_back(mi); |
53 | } |
54 | |
55 | void LightmapperRD::add_directional_light(bool p_static, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_angular_distance, float p_shadow_blur) { |
56 | Light l; |
57 | l.type = LIGHT_TYPE_DIRECTIONAL; |
58 | l.direction[0] = p_direction.x; |
59 | l.direction[1] = p_direction.y; |
60 | l.direction[2] = p_direction.z; |
61 | l.color[0] = p_color.r; |
62 | l.color[1] = p_color.g; |
63 | l.color[2] = p_color.b; |
64 | l.energy = p_energy; |
65 | l.static_bake = p_static; |
66 | l.size = Math::tan(Math::deg_to_rad(p_angular_distance)); |
67 | l.shadow_blur = p_shadow_blur; |
68 | lights.push_back(l); |
69 | } |
70 | |
71 | void LightmapperRD::add_omni_light(bool p_static, const Vector3 &p_position, const Color &p_color, float p_energy, float p_range, float p_attenuation, float p_size, float p_shadow_blur) { |
72 | Light l; |
73 | l.type = LIGHT_TYPE_OMNI; |
74 | l.position[0] = p_position.x; |
75 | l.position[1] = p_position.y; |
76 | l.position[2] = p_position.z; |
77 | l.range = p_range; |
78 | l.attenuation = p_attenuation; |
79 | l.color[0] = p_color.r; |
80 | l.color[1] = p_color.g; |
81 | l.color[2] = p_color.b; |
82 | l.energy = p_energy; |
83 | l.static_bake = p_static; |
84 | l.size = p_size; |
85 | l.shadow_blur = p_shadow_blur; |
86 | lights.push_back(l); |
87 | } |
88 | |
89 | void LightmapperRD::add_spot_light(bool p_static, const Vector3 &p_position, const Vector3 p_direction, const Color &p_color, float p_energy, float p_range, float p_attenuation, float p_spot_angle, float p_spot_attenuation, float p_size, float p_shadow_blur) { |
90 | Light l; |
91 | l.type = LIGHT_TYPE_SPOT; |
92 | l.position[0] = p_position.x; |
93 | l.position[1] = p_position.y; |
94 | l.position[2] = p_position.z; |
95 | l.direction[0] = p_direction.x; |
96 | l.direction[1] = p_direction.y; |
97 | l.direction[2] = p_direction.z; |
98 | l.range = p_range; |
99 | l.attenuation = p_attenuation; |
100 | l.cos_spot_angle = Math::cos(Math::deg_to_rad(p_spot_angle)); |
101 | l.inv_spot_attenuation = 1.0f / p_spot_attenuation; |
102 | l.color[0] = p_color.r; |
103 | l.color[1] = p_color.g; |
104 | l.color[2] = p_color.b; |
105 | l.energy = p_energy; |
106 | l.static_bake = p_static; |
107 | l.size = p_size; |
108 | l.shadow_blur = p_shadow_blur; |
109 | lights.push_back(l); |
110 | } |
111 | |
112 | void LightmapperRD::add_probe(const Vector3 &p_position) { |
113 | Probe probe; |
114 | probe.position[0] = p_position.x; |
115 | probe.position[1] = p_position.y; |
116 | probe.position[2] = p_position.z; |
117 | probe.position[3] = 0; |
118 | probe_positions.push_back(probe); |
119 | } |
120 | |
121 | 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> &triangles, uint32_t p_grid_size) { |
122 | int half_size = p_size / 2; |
123 | |
124 | for (int i = 0; i < 8; i++) { |
125 | AABB aabb = p_bounds; |
126 | aabb.size *= 0.5; |
127 | Vector3i n = p_ofs; |
128 | |
129 | if (i & 1) { |
130 | aabb.position.x += aabb.size.x; |
131 | n.x += half_size; |
132 | } |
133 | if (i & 2) { |
134 | aabb.position.y += aabb.size.y; |
135 | n.y += half_size; |
136 | } |
137 | if (i & 4) { |
138 | aabb.position.z += aabb.size.z; |
139 | n.z += half_size; |
140 | } |
141 | |
142 | { |
143 | Vector3 qsize = aabb.size * 0.5; //quarter size, for fast aabb test |
144 | |
145 | if (!Geometry3D::triangle_box_overlap(aabb.position + qsize, qsize, p_points)) { |
146 | //does not fit in child, go on |
147 | continue; |
148 | } |
149 | } |
150 | |
151 | if (half_size == 1) { |
152 | //got to the end |
153 | TriangleSort ts; |
154 | ts.cell_index = n.x + (n.y * p_grid_size) + (n.z * p_grid_size * p_grid_size); |
155 | ts.triangle_index = p_triangle_index; |
156 | triangles.push_back(ts); |
157 | } else { |
158 | _plot_triangle_into_triangle_index_list(half_size, n, aabb, p_points, p_triangle_index, triangles, p_grid_size); |
159 | } |
160 | } |
161 | } |
162 | |
163 | Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_size, Vector<Ref<Image>> &albedo_images, Vector<Ref<Image>> &emission_images, AABB &bounds, Size2i &atlas_size, int &atlas_slices, BakeStepFunc p_step_function, void *p_bake_userdata) { |
164 | Vector<Size2i> sizes; |
165 | |
166 | for (int m_i = 0; m_i < mesh_instances.size(); m_i++) { |
167 | MeshInstance &mi = mesh_instances.write[m_i]; |
168 | Size2i s = Size2i(mi.data.albedo_on_uv2->get_width(), mi.data.albedo_on_uv2->get_height()); |
169 | sizes.push_back(s); |
170 | atlas_size.width = MAX(atlas_size.width, s.width + 2); |
171 | atlas_size.height = MAX(atlas_size.height, s.height + 2); |
172 | } |
173 | |
174 | int max = nearest_power_of_2_templated(atlas_size.width); |
175 | max = MAX(max, nearest_power_of_2_templated(atlas_size.height)); |
176 | |
177 | if (max > p_max_texture_size) { |
178 | return BAKE_ERROR_LIGHTMAP_TOO_SMALL; |
179 | } |
180 | |
181 | if (p_step_function) { |
182 | p_step_function(0.1, RTR("Determining optimal atlas size" ), p_bake_userdata, true); |
183 | } |
184 | |
185 | atlas_size = Size2i(max, max); |
186 | |
187 | Size2i best_atlas_size; |
188 | int best_atlas_slices = 0; |
189 | int best_atlas_memory = 0x7FFFFFFF; |
190 | Vector<Vector3i> best_atlas_offsets; |
191 | |
192 | //determine best texture array atlas size by bruteforce fitting |
193 | while (atlas_size.x <= p_max_texture_size && atlas_size.y <= p_max_texture_size) { |
194 | Vector<Vector2i> source_sizes; |
195 | Vector<int> source_indices; |
196 | source_sizes.resize(sizes.size()); |
197 | source_indices.resize(sizes.size()); |
198 | for (int i = 0; i < source_indices.size(); i++) { |
199 | source_sizes.write[i] = sizes[i] + Vector2i(2, 2); // Add padding between lightmaps |
200 | source_indices.write[i] = i; |
201 | } |
202 | Vector<Vector3i> atlas_offsets; |
203 | atlas_offsets.resize(source_sizes.size()); |
204 | |
205 | int slices = 0; |
206 | |
207 | while (source_sizes.size() > 0) { |
208 | Vector<Vector3i> offsets = Geometry2D::partial_pack_rects(source_sizes, atlas_size); |
209 | Vector<int> new_indices; |
210 | Vector<Vector2i> new_sources; |
211 | for (int i = 0; i < offsets.size(); i++) { |
212 | Vector3i ofs = offsets[i]; |
213 | int sidx = source_indices[i]; |
214 | if (ofs.z > 0) { |
215 | //valid |
216 | ofs.z = slices; |
217 | atlas_offsets.write[sidx] = ofs + Vector3i(1, 1, 0); // Center lightmap in the reserved oversized region |
218 | } else { |
219 | new_indices.push_back(sidx); |
220 | new_sources.push_back(source_sizes[i]); |
221 | } |
222 | } |
223 | |
224 | source_sizes = new_sources; |
225 | source_indices = new_indices; |
226 | slices++; |
227 | } |
228 | |
229 | int mem_used = atlas_size.x * atlas_size.y * slices; |
230 | if (mem_used < best_atlas_memory) { |
231 | best_atlas_size = atlas_size; |
232 | best_atlas_offsets = atlas_offsets; |
233 | best_atlas_slices = slices; |
234 | best_atlas_memory = mem_used; |
235 | } |
236 | |
237 | if (atlas_size.width == atlas_size.height) { |
238 | atlas_size.width *= 2; |
239 | } else { |
240 | atlas_size.height *= 2; |
241 | } |
242 | } |
243 | atlas_size = best_atlas_size; |
244 | atlas_slices = best_atlas_slices; |
245 | |
246 | // apply the offsets and slice to all images, and also blit albedo and emission |
247 | albedo_images.resize(atlas_slices); |
248 | emission_images.resize(atlas_slices); |
249 | |
250 | if (p_step_function) { |
251 | p_step_function(0.2, RTR("Blitting albedo and emission" ), p_bake_userdata, true); |
252 | } |
253 | |
254 | for (int i = 0; i < atlas_slices; i++) { |
255 | Ref<Image> albedo = Image::create_empty(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBA8); |
256 | albedo->set_as_black(); |
257 | albedo_images.write[i] = albedo; |
258 | |
259 | Ref<Image> emission = Image::create_empty(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH); |
260 | emission->set_as_black(); |
261 | emission_images.write[i] = emission; |
262 | } |
263 | |
264 | //assign uv positions |
265 | |
266 | for (int m_i = 0; m_i < mesh_instances.size(); m_i++) { |
267 | MeshInstance &mi = mesh_instances.write[m_i]; |
268 | mi.offset.x = best_atlas_offsets[m_i].x; |
269 | mi.offset.y = best_atlas_offsets[m_i].y; |
270 | mi.slice = best_atlas_offsets[m_i].z; |
271 | albedo_images.write[mi.slice]->blit_rect(mi.data.albedo_on_uv2, Rect2i(Vector2i(), mi.data.albedo_on_uv2->get_size()), mi.offset); |
272 | emission_images.write[mi.slice]->blit_rect(mi.data.emission_on_uv2, Rect2(Vector2i(), mi.data.emission_on_uv2->get_size()), mi.offset); |
273 | } |
274 | |
275 | return BAKE_OK; |
276 | } |
277 | |
278 | void LightmapperRD::_create_acceleration_structures(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, AABB &bounds, int grid_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 &triangle_cell_indices_buffer, RID &probe_positions_buffer, RID &grid_texture, RID &seams_buffer, BakeStepFunc p_step_function, void *p_bake_userdata) { |
279 | HashMap<Vertex, uint32_t, VertexHash> vertex_map; |
280 | |
281 | //fill triangles array and vertex array |
282 | LocalVector<Triangle> triangles; |
283 | LocalVector<Vertex> vertex_array; |
284 | LocalVector<Seam> seams; |
285 | |
286 | slice_triangle_count.resize(atlas_slices); |
287 | slice_seam_count.resize(atlas_slices); |
288 | |
289 | for (int i = 0; i < atlas_slices; i++) { |
290 | slice_triangle_count.write[i] = 0; |
291 | slice_seam_count.write[i] = 0; |
292 | } |
293 | |
294 | bounds = AABB(); |
295 | |
296 | for (int m_i = 0; m_i < mesh_instances.size(); m_i++) { |
297 | if (p_step_function) { |
298 | float p = float(m_i + 1) / mesh_instances.size() * 0.1; |
299 | 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); |
300 | } |
301 | |
302 | HashMap<Edge, EdgeUV2, EdgeHash> edges; |
303 | |
304 | MeshInstance &mi = mesh_instances.write[m_i]; |
305 | |
306 | Vector2 uv_scale = Vector2(mi.data.albedo_on_uv2->get_width(), mi.data.albedo_on_uv2->get_height()) / Vector2(atlas_size); |
307 | Vector2 uv_offset = Vector2(mi.offset) / Vector2(atlas_size); |
308 | if (m_i == 0) { |
309 | bounds.position = mi.data.points[0]; |
310 | } |
311 | |
312 | for (int i = 0; i < mi.data.points.size(); i += 3) { |
313 | Vector3 vtxs[3] = { mi.data.points[i + 0], mi.data.points[i + 1], mi.data.points[i + 2] }; |
314 | 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 }; |
315 | Vector3 normal[3] = { mi.data.normal[i + 0], mi.data.normal[i + 1], mi.data.normal[i + 2] }; |
316 | |
317 | AABB taabb; |
318 | Triangle t; |
319 | t.slice = mi.slice; |
320 | for (int k = 0; k < 3; k++) { |
321 | bounds.expand_to(vtxs[k]); |
322 | |
323 | Vertex v; |
324 | v.position[0] = vtxs[k].x; |
325 | v.position[1] = vtxs[k].y; |
326 | v.position[2] = vtxs[k].z; |
327 | v.uv[0] = uvs[k].x; |
328 | v.uv[1] = uvs[k].y; |
329 | v.normal_xy[0] = normal[k].x; |
330 | v.normal_xy[1] = normal[k].y; |
331 | v.normal_z = normal[k].z; |
332 | |
333 | uint32_t *indexptr = vertex_map.getptr(v); |
334 | |
335 | if (indexptr) { |
336 | t.indices[k] = *indexptr; |
337 | } else { |
338 | uint32_t new_index = vertex_map.size(); |
339 | t.indices[k] = new_index; |
340 | vertex_map[v] = new_index; |
341 | vertex_array.push_back(v); |
342 | } |
343 | |
344 | if (k == 0) { |
345 | taabb.position = vtxs[k]; |
346 | } else { |
347 | taabb.expand_to(vtxs[k]); |
348 | } |
349 | } |
350 | |
351 | //compute seams that will need to be blended later |
352 | for (int k = 0; k < 3; k++) { |
353 | int n = (k + 1) % 3; |
354 | |
355 | Edge edge(vtxs[k], vtxs[n], normal[k], normal[n]); |
356 | Vector2i edge_indices(t.indices[k], t.indices[n]); |
357 | EdgeUV2 uv2(uvs[k], uvs[n], edge_indices); |
358 | |
359 | if (edge.b == edge.a) { |
360 | continue; //degenerate, somehow |
361 | } |
362 | if (edge.b < edge.a) { |
363 | SWAP(edge.a, edge.b); |
364 | SWAP(edge.na, edge.nb); |
365 | SWAP(uv2.a, uv2.b); |
366 | SWAP(edge_indices.x, edge_indices.y); |
367 | } |
368 | |
369 | EdgeUV2 *euv2 = edges.getptr(edge); |
370 | if (!euv2) { |
371 | edges[edge] = uv2; |
372 | } else { |
373 | if (*euv2 == uv2) { |
374 | continue; // seam shared UV space, no need to blend |
375 | } |
376 | if (euv2->seam_found) { |
377 | continue; //bad geometry |
378 | } |
379 | |
380 | Seam seam; |
381 | seam.a = edge_indices; |
382 | seam.b = euv2->indices; |
383 | seam.slice = mi.slice; |
384 | seams.push_back(seam); |
385 | slice_seam_count.write[mi.slice]++; |
386 | euv2->seam_found = true; |
387 | } |
388 | } |
389 | |
390 | t.min_bounds[0] = taabb.position.x; |
391 | t.min_bounds[1] = taabb.position.y; |
392 | t.min_bounds[2] = taabb.position.z; |
393 | t.max_bounds[0] = taabb.position.x + MAX(taabb.size.x, 0.0001); |
394 | t.max_bounds[1] = taabb.position.y + MAX(taabb.size.y, 0.0001); |
395 | t.max_bounds[2] = taabb.position.z + MAX(taabb.size.z, 0.0001); |
396 | t.pad0 = t.pad1 = 0; //make valgrind not complain |
397 | triangles.push_back(t); |
398 | slice_triangle_count.write[t.slice]++; |
399 | } |
400 | } |
401 | |
402 | //also consider probe positions for bounds |
403 | for (int i = 0; i < p_probe_positions.size(); i++) { |
404 | Vector3 pp(p_probe_positions[i].position[0], p_probe_positions[i].position[1], p_probe_positions[i].position[2]); |
405 | bounds.expand_to(pp); |
406 | } |
407 | bounds.grow_by(0.1); //grow a bit to avoid numerical error |
408 | |
409 | triangles.sort(); //sort by slice |
410 | seams.sort(); |
411 | |
412 | if (p_step_function) { |
413 | p_step_function(0.4, RTR("Optimizing acceleration structure" ), p_bake_userdata, true); |
414 | } |
415 | |
416 | //fill list of triangles in grid |
417 | LocalVector<TriangleSort> triangle_sort; |
418 | for (uint32_t i = 0; i < triangles.size(); i++) { |
419 | const Triangle &t = triangles[i]; |
420 | Vector3 face[3] = { |
421 | Vector3(vertex_array[t.indices[0]].position[0], vertex_array[t.indices[0]].position[1], vertex_array[t.indices[0]].position[2]), |
422 | Vector3(vertex_array[t.indices[1]].position[0], vertex_array[t.indices[1]].position[1], vertex_array[t.indices[1]].position[2]), |
423 | Vector3(vertex_array[t.indices[2]].position[0], vertex_array[t.indices[2]].position[1], vertex_array[t.indices[2]].position[2]) |
424 | }; |
425 | _plot_triangle_into_triangle_index_list(grid_size, Vector3i(), bounds, face, i, triangle_sort, grid_size); |
426 | } |
427 | //sort it |
428 | triangle_sort.sort(); |
429 | |
430 | Vector<uint32_t> triangle_indices; |
431 | triangle_indices.resize(triangle_sort.size()); |
432 | Vector<uint32_t> grid_indices; |
433 | grid_indices.resize(grid_size * grid_size * grid_size * 2); |
434 | memset(grid_indices.ptrw(), 0, grid_indices.size() * sizeof(uint32_t)); |
435 | Vector<bool> solid; |
436 | solid.resize(grid_size * grid_size * grid_size); |
437 | memset(solid.ptrw(), 0, solid.size() * sizeof(bool)); |
438 | |
439 | { |
440 | uint32_t *tiw = triangle_indices.ptrw(); |
441 | uint32_t last_cell = 0xFFFFFFFF; |
442 | uint32_t *giw = grid_indices.ptrw(); |
443 | bool *solidw = solid.ptrw(); |
444 | for (uint32_t i = 0; i < triangle_sort.size(); i++) { |
445 | uint32_t cell = triangle_sort[i].cell_index; |
446 | if (cell != last_cell) { |
447 | //cell changed, update pointer to indices |
448 | giw[cell * 2 + 1] = i; |
449 | solidw[cell] = true; |
450 | } |
451 | tiw[i] = triangle_sort[i].triangle_index; |
452 | giw[cell * 2]++; //update counter |
453 | last_cell = cell; |
454 | } |
455 | } |
456 | #if 0 |
457 | for (int i = 0; i < grid_size; i++) { |
458 | for (int j = 0; j < grid_size; j++) { |
459 | for (int k = 0; k < grid_size; k++) { |
460 | uint32_t index = i * (grid_size * grid_size) + j * grid_size + k; |
461 | grid_indices.write[index * 2] = float(i) / grid_size * 255; |
462 | grid_indices.write[index * 2 + 1] = float(j) / grid_size * 255; |
463 | } |
464 | } |
465 | } |
466 | #endif |
467 | |
468 | #if 0 |
469 | for (int i = 0; i < grid_size; i++) { |
470 | Vector<uint8_t> grid_usage; |
471 | grid_usage.resize(grid_size * grid_size); |
472 | for (int j = 0; j < grid_usage.size(); j++) { |
473 | uint32_t ofs = i * grid_size * grid_size + j; |
474 | uint32_t count = grid_indices[ofs * 2]; |
475 | grid_usage.write[j] = count > 0 ? 255 : 0; |
476 | } |
477 | |
478 | Ref<Image> img = Image::create_from_data(grid_size, grid_size, false, Image::FORMAT_L8, grid_usage); |
479 | img->save_png("res://grid_layer_" + itos(1000 + i).substr(1, 3) + ".png" ); |
480 | } |
481 | #endif |
482 | |
483 | /*****************************/ |
484 | /*** CREATE GPU STRUCTURES ***/ |
485 | /*****************************/ |
486 | |
487 | lights.sort(); |
488 | |
489 | Vector<Vector2i> seam_buffer_vec; |
490 | seam_buffer_vec.resize(seams.size() * 2); |
491 | for (uint32_t i = 0; i < seams.size(); i++) { |
492 | seam_buffer_vec.write[i * 2 + 0] = seams[i].a; |
493 | seam_buffer_vec.write[i * 2 + 1] = seams[i].b; |
494 | } |
495 | |
496 | { //buffers |
497 | Vector<uint8_t> vb = vertex_array.to_byte_array(); |
498 | vertex_buffer = rd->storage_buffer_create(vb.size(), vb); |
499 | |
500 | Vector<uint8_t> tb = triangles.to_byte_array(); |
501 | triangle_buffer = rd->storage_buffer_create(tb.size(), tb); |
502 | |
503 | Vector<uint8_t> tib = triangle_indices.to_byte_array(); |
504 | triangle_cell_indices_buffer = rd->storage_buffer_create(tib.size(), tib); |
505 | |
506 | Vector<uint8_t> lb = lights.to_byte_array(); |
507 | if (lb.size() == 0) { |
508 | lb.resize(sizeof(Light)); //even if no lights, the buffer must exist |
509 | } |
510 | lights_buffer = rd->storage_buffer_create(lb.size(), lb); |
511 | |
512 | Vector<uint8_t> sb = seam_buffer_vec.to_byte_array(); |
513 | if (sb.size() == 0) { |
514 | sb.resize(sizeof(Vector2i) * 2); //even if no seams, the buffer must exist |
515 | } |
516 | seams_buffer = rd->storage_buffer_create(sb.size(), sb); |
517 | |
518 | Vector<uint8_t> pb = p_probe_positions.to_byte_array(); |
519 | if (pb.size() == 0) { |
520 | pb.resize(sizeof(Probe)); |
521 | } |
522 | probe_positions_buffer = rd->storage_buffer_create(pb.size(), pb); |
523 | } |
524 | |
525 | { //grid |
526 | |
527 | RD::TextureFormat tf; |
528 | tf.width = grid_size; |
529 | tf.height = grid_size; |
530 | tf.depth = grid_size; |
531 | tf.texture_type = RD::TEXTURE_TYPE_3D; |
532 | tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT; |
533 | |
534 | Vector<Vector<uint8_t>> texdata; |
535 | texdata.resize(1); |
536 | //grid and indices |
537 | tf.format = RD::DATA_FORMAT_R32G32_UINT; |
538 | texdata.write[0] = grid_indices.to_byte_array(); |
539 | grid_texture = rd->texture_create(tf, RD::TextureView(), texdata); |
540 | } |
541 | } |
542 | |
543 | 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) { |
544 | Vector<RID> framebuffers; |
545 | |
546 | for (int i = 0; i < atlas_slices; i++) { |
547 | RID slice_pos_tex = rd->texture_create_shared_from_slice(RD::TextureView(), position_tex, i, 0); |
548 | RID slice_unoc_tex = rd->texture_create_shared_from_slice(RD::TextureView(), unocclude_tex, i, 0); |
549 | RID slice_norm_tex = rd->texture_create_shared_from_slice(RD::TextureView(), normal_tex, i, 0); |
550 | Vector<RID> fb; |
551 | fb.push_back(slice_pos_tex); |
552 | fb.push_back(slice_norm_tex); |
553 | fb.push_back(slice_unoc_tex); |
554 | fb.push_back(raster_depth_buffer); |
555 | framebuffers.push_back(rd->framebuffer_create(fb)); |
556 | } |
557 | |
558 | RD::PipelineDepthStencilState ds; |
559 | ds.enable_depth_test = true; |
560 | ds.enable_depth_write = true; |
561 | ds.depth_compare_operator = RD::COMPARE_OP_LESS; //so it does render same pixel twice |
562 | |
563 | 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); |
564 | RID raster_pipeline_wire; |
565 | { |
566 | RD::PipelineRasterizationState rw; |
567 | rw.wireframe = true; |
568 | 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); |
569 | } |
570 | |
571 | uint32_t triangle_offset = 0; |
572 | Vector<Color> clear_colors; |
573 | clear_colors.push_back(Color(0, 0, 0, 0)); |
574 | clear_colors.push_back(Color(0, 0, 0, 0)); |
575 | clear_colors.push_back(Color(0, 0, 0, 0)); |
576 | |
577 | for (int i = 0; i < atlas_slices; i++) { |
578 | RasterPushConstant raster_push_constant; |
579 | raster_push_constant.atlas_size[0] = atlas_size.x; |
580 | raster_push_constant.atlas_size[1] = atlas_size.y; |
581 | raster_push_constant.base_triangle = triangle_offset; |
582 | raster_push_constant.to_cell_offset[0] = bounds.position.x; |
583 | raster_push_constant.to_cell_offset[1] = bounds.position.y; |
584 | raster_push_constant.to_cell_offset[2] = bounds.position.z; |
585 | raster_push_constant.bias = p_bias; |
586 | raster_push_constant.to_cell_size[0] = (1.0 / bounds.size.x) * float(grid_size); |
587 | raster_push_constant.to_cell_size[1] = (1.0 / bounds.size.y) * float(grid_size); |
588 | raster_push_constant.to_cell_size[2] = (1.0 / bounds.size.z) * float(grid_size); |
589 | raster_push_constant.grid_size[0] = grid_size; |
590 | raster_push_constant.grid_size[1] = grid_size; |
591 | raster_push_constant.grid_size[2] = grid_size; |
592 | raster_push_constant.uv_offset[0] = 0; |
593 | raster_push_constant.uv_offset[1] = 0; |
594 | |
595 | RD::DrawListID draw_list = rd->draw_list_begin(framebuffers[i], RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors); |
596 | //draw opaque |
597 | rd->draw_list_bind_render_pipeline(draw_list, raster_pipeline); |
598 | rd->draw_list_bind_uniform_set(draw_list, raster_base_uniform, 0); |
599 | rd->draw_list_set_push_constant(draw_list, &raster_push_constant, sizeof(RasterPushConstant)); |
600 | rd->draw_list_draw(draw_list, false, 1, slice_triangle_count[i] * 3); |
601 | //draw wire |
602 | rd->draw_list_bind_render_pipeline(draw_list, raster_pipeline_wire); |
603 | rd->draw_list_bind_uniform_set(draw_list, raster_base_uniform, 0); |
604 | rd->draw_list_set_push_constant(draw_list, &raster_push_constant, sizeof(RasterPushConstant)); |
605 | rd->draw_list_draw(draw_list, false, 1, slice_triangle_count[i] * 3); |
606 | |
607 | rd->draw_list_end(); |
608 | |
609 | triangle_offset += slice_triangle_count[i]; |
610 | } |
611 | } |
612 | |
613 | 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) { |
614 | Vector<RD::Uniform> uniforms; |
615 | { |
616 | { |
617 | RD::Uniform u; |
618 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
619 | u.binding = 0; |
620 | u.append_id(dest_light_tex); |
621 | uniforms.push_back(u); |
622 | } |
623 | { |
624 | RD::Uniform u; |
625 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
626 | u.binding = 1; |
627 | u.append_id(source_light_tex); |
628 | uniforms.push_back(u); |
629 | } |
630 | } |
631 | |
632 | RID compute_shader_dilate = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("dilate" )); |
633 | ERR_FAIL_COND_V(compute_shader_dilate.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen |
634 | RID compute_shader_dilate_pipeline = rd->compute_pipeline_create(compute_shader_dilate); |
635 | |
636 | RID dilate_uniform_set = rd->uniform_set_create(uniforms, compute_shader_dilate, 1); |
637 | |
638 | RD::ComputeListID compute_list = rd->compute_list_begin(); |
639 | rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_dilate_pipeline); |
640 | rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0); |
641 | rd->compute_list_bind_uniform_set(compute_list, dilate_uniform_set, 1); |
642 | push_constant.region_ofs[0] = 0; |
643 | push_constant.region_ofs[1] = 0; |
644 | Vector3i group_size((atlas_size.x - 1) / 8 + 1, (atlas_size.y - 1) / 8 + 1, 1); //restore group size |
645 | |
646 | for (int i = 0; i < atlas_slices; i++) { |
647 | push_constant.atlas_slice = i; |
648 | rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant)); |
649 | rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z); |
650 | //no barrier, let them run all together |
651 | } |
652 | rd->compute_list_end(); |
653 | rd->free(compute_shader_dilate); |
654 | |
655 | #ifdef DEBUG_TEXTURES |
656 | for (int i = 0; i < atlas_slices; i++) { |
657 | Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i); |
658 | Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s); |
659 | img->convert(Image::FORMAT_RGBA8); |
660 | img->save_png("res://5_dilated_" + itos(i) + ".png" ); |
661 | } |
662 | #endif |
663 | return BAKE_OK; |
664 | } |
665 | |
666 | LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_denoiser, int p_bounces, float p_bias, int p_max_texture_size, bool p_bake_sh, 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) { |
667 | if (p_step_function) { |
668 | p_step_function(0.0, RTR("Begin Bake" ), p_bake_userdata, true); |
669 | } |
670 | bake_textures.clear(); |
671 | int grid_size = 128; |
672 | |
673 | /* STEP 1: Fetch material textures and compute the bounds */ |
674 | |
675 | AABB bounds; |
676 | Size2i atlas_size; |
677 | int atlas_slices; |
678 | Vector<Ref<Image>> albedo_images; |
679 | Vector<Ref<Image>> emission_images; |
680 | |
681 | BakeError bake_error = _blit_meshes_into_atlas(p_max_texture_size, albedo_images, emission_images, bounds, atlas_size, atlas_slices, p_step_function, p_bake_userdata); |
682 | if (bake_error != BAKE_OK) { |
683 | return bake_error; |
684 | } |
685 | |
686 | #ifdef DEBUG_TEXTURES |
687 | for (int i = 0; i < atlas_slices; i++) { |
688 | albedo_images[i]->save_png("res://0_albedo_" + itos(i) + ".png" ); |
689 | emission_images[i]->save_png("res://0_emission_" + itos(i) + ".png" ); |
690 | } |
691 | #endif |
692 | |
693 | RenderingDevice *rd = RenderingDevice::get_singleton()->create_local_device(); |
694 | |
695 | RID albedo_array_tex; |
696 | RID emission_array_tex; |
697 | RID normal_tex; |
698 | RID position_tex; |
699 | RID unocclude_tex; |
700 | RID light_source_tex; |
701 | RID light_dest_tex; |
702 | RID light_accum_tex; |
703 | RID light_accum_tex2; |
704 | RID light_primary_dynamic_tex; |
705 | RID light_environment_tex; |
706 | |
707 | #define FREE_TEXTURES \ |
708 | rd->free(albedo_array_tex); \ |
709 | rd->free(emission_array_tex); \ |
710 | rd->free(normal_tex); \ |
711 | rd->free(position_tex); \ |
712 | rd->free(unocclude_tex); \ |
713 | rd->free(light_source_tex); \ |
714 | rd->free(light_accum_tex2); \ |
715 | rd->free(light_accum_tex); \ |
716 | rd->free(light_primary_dynamic_tex); \ |
717 | rd->free(light_environment_tex); |
718 | |
719 | { // create all textures |
720 | |
721 | Vector<Vector<uint8_t>> albedo_data; |
722 | Vector<Vector<uint8_t>> emission_data; |
723 | for (int i = 0; i < atlas_slices; i++) { |
724 | albedo_data.push_back(albedo_images[i]->get_data()); |
725 | emission_data.push_back(emission_images[i]->get_data()); |
726 | } |
727 | |
728 | RD::TextureFormat tf; |
729 | tf.width = atlas_size.width; |
730 | tf.height = atlas_size.height; |
731 | tf.array_layers = atlas_slices; |
732 | tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY; |
733 | tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT; |
734 | tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM; |
735 | |
736 | albedo_array_tex = rd->texture_create(tf, RD::TextureView(), albedo_data); |
737 | |
738 | tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT; |
739 | |
740 | emission_array_tex = rd->texture_create(tf, RD::TextureView(), emission_data); |
741 | |
742 | //this will be rastered to |
743 | 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; |
744 | normal_tex = rd->texture_create(tf, RD::TextureView()); |
745 | tf.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
746 | position_tex = rd->texture_create(tf, RD::TextureView()); |
747 | unocclude_tex = rd->texture_create(tf, RD::TextureView()); |
748 | |
749 | tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT; |
750 | 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; |
751 | |
752 | light_source_tex = rd->texture_create(tf, RD::TextureView()); |
753 | rd->texture_clear(light_source_tex, Color(0, 0, 0, 0), 0, 1, 0, atlas_slices); |
754 | light_primary_dynamic_tex = rd->texture_create(tf, RD::TextureView()); |
755 | rd->texture_clear(light_primary_dynamic_tex, Color(0, 0, 0, 0), 0, 1, 0, atlas_slices); |
756 | |
757 | if (p_bake_sh) { |
758 | tf.array_layers *= 4; |
759 | } |
760 | light_accum_tex = rd->texture_create(tf, RD::TextureView()); |
761 | rd->texture_clear(light_accum_tex, Color(0, 0, 0, 0), 0, 1, 0, tf.array_layers); |
762 | light_dest_tex = rd->texture_create(tf, RD::TextureView()); |
763 | rd->texture_clear(light_dest_tex, Color(0, 0, 0, 0), 0, 1, 0, tf.array_layers); |
764 | light_accum_tex2 = light_dest_tex; |
765 | |
766 | //env |
767 | { |
768 | Ref<Image> panorama_tex; |
769 | if (p_environment_panorama.is_valid()) { |
770 | panorama_tex = p_environment_panorama; |
771 | panorama_tex->convert(Image::FORMAT_RGBAF); |
772 | } else { |
773 | panorama_tex.instantiate(); |
774 | panorama_tex->initialize_data(8, 8, false, Image::FORMAT_RGBAF); |
775 | panorama_tex->fill(Color(0, 0, 0, 1)); |
776 | } |
777 | |
778 | RD::TextureFormat tfp; |
779 | tfp.width = panorama_tex->get_width(); |
780 | tfp.height = panorama_tex->get_height(); |
781 | tfp.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT; |
782 | tfp.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
783 | |
784 | Vector<Vector<uint8_t>> tdata; |
785 | tdata.push_back(panorama_tex->get_data()); |
786 | light_environment_tex = rd->texture_create(tfp, RD::TextureView(), tdata); |
787 | |
788 | #ifdef DEBUG_TEXTURES |
789 | panorama_tex->save_exr("res://0_panorama.exr" , false); |
790 | #endif |
791 | } |
792 | } |
793 | |
794 | /* STEP 2: create the acceleration structure for the GPU*/ |
795 | |
796 | Vector<int> slice_triangle_count; |
797 | RID vertex_buffer; |
798 | RID triangle_buffer; |
799 | RID lights_buffer; |
800 | RID triangle_cell_indices_buffer; |
801 | RID grid_texture; |
802 | RID seams_buffer; |
803 | RID probe_positions_buffer; |
804 | |
805 | Vector<int> slice_seam_count; |
806 | |
807 | #define FREE_BUFFERS \ |
808 | rd->free(vertex_buffer); \ |
809 | rd->free(triangle_buffer); \ |
810 | rd->free(lights_buffer); \ |
811 | rd->free(triangle_cell_indices_buffer); \ |
812 | rd->free(grid_texture); \ |
813 | rd->free(seams_buffer); \ |
814 | rd->free(probe_positions_buffer); |
815 | |
816 | _create_acceleration_structures(rd, atlas_size, atlas_slices, bounds, grid_size, probe_positions, p_generate_probes, slice_triangle_count, slice_seam_count, vertex_buffer, triangle_buffer, lights_buffer, triangle_cell_indices_buffer, probe_positions_buffer, grid_texture, seams_buffer, p_step_function, p_bake_userdata); |
817 | |
818 | if (p_step_function) { |
819 | p_step_function(0.47, RTR("Preparing shaders" ), p_bake_userdata, true); |
820 | } |
821 | |
822 | //shaders |
823 | Ref<RDShaderFile> raster_shader; |
824 | raster_shader.instantiate(); |
825 | Error err = raster_shader->parse_versions_from_text(lm_raster_shader_glsl); |
826 | if (err != OK) { |
827 | raster_shader->print_errors("raster_shader" ); |
828 | |
829 | FREE_TEXTURES |
830 | FREE_BUFFERS |
831 | |
832 | memdelete(rd); |
833 | } |
834 | ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); |
835 | |
836 | RID rasterize_shader = rd->shader_create_from_spirv(raster_shader->get_spirv_stages()); |
837 | |
838 | ERR_FAIL_COND_V(rasterize_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //this is a bug check, though, should not happen |
839 | |
840 | RID sampler; |
841 | { |
842 | RD::SamplerState s; |
843 | s.mag_filter = RD::SAMPLER_FILTER_LINEAR; |
844 | s.min_filter = RD::SAMPLER_FILTER_LINEAR; |
845 | s.max_lod = 0; |
846 | |
847 | sampler = rd->sampler_create(s); |
848 | } |
849 | |
850 | Vector<RD::Uniform> base_uniforms; |
851 | { |
852 | { |
853 | RD::Uniform u; |
854 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
855 | u.binding = 1; |
856 | u.append_id(vertex_buffer); |
857 | base_uniforms.push_back(u); |
858 | } |
859 | { |
860 | RD::Uniform u; |
861 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
862 | u.binding = 2; |
863 | u.append_id(triangle_buffer); |
864 | base_uniforms.push_back(u); |
865 | } |
866 | { |
867 | RD::Uniform u; |
868 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
869 | u.binding = 3; |
870 | u.append_id(triangle_cell_indices_buffer); |
871 | base_uniforms.push_back(u); |
872 | } |
873 | { |
874 | RD::Uniform u; |
875 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
876 | u.binding = 4; |
877 | u.append_id(lights_buffer); |
878 | base_uniforms.push_back(u); |
879 | } |
880 | { |
881 | RD::Uniform u; |
882 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
883 | u.binding = 5; |
884 | u.append_id(seams_buffer); |
885 | base_uniforms.push_back(u); |
886 | } |
887 | { |
888 | RD::Uniform u; |
889 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
890 | u.binding = 6; |
891 | u.append_id(probe_positions_buffer); |
892 | base_uniforms.push_back(u); |
893 | } |
894 | { |
895 | RD::Uniform u; |
896 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
897 | u.binding = 7; |
898 | u.append_id(grid_texture); |
899 | base_uniforms.push_back(u); |
900 | } |
901 | { |
902 | RD::Uniform u; |
903 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
904 | u.binding = 8; |
905 | u.append_id(albedo_array_tex); |
906 | base_uniforms.push_back(u); |
907 | } |
908 | { |
909 | RD::Uniform u; |
910 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
911 | u.binding = 9; |
912 | u.append_id(emission_array_tex); |
913 | base_uniforms.push_back(u); |
914 | } |
915 | { |
916 | RD::Uniform u; |
917 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER; |
918 | u.binding = 10; |
919 | u.append_id(sampler); |
920 | base_uniforms.push_back(u); |
921 | } |
922 | } |
923 | |
924 | RID raster_base_uniform = rd->uniform_set_create(base_uniforms, rasterize_shader, 0); |
925 | RID raster_depth_buffer; |
926 | { |
927 | RD::TextureFormat tf; |
928 | tf.width = atlas_size.width; |
929 | tf.height = atlas_size.height; |
930 | tf.depth = 1; |
931 | tf.texture_type = RD::TEXTURE_TYPE_2D; |
932 | tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
933 | tf.format = RD::DATA_FORMAT_D32_SFLOAT; |
934 | |
935 | raster_depth_buffer = rd->texture_create(tf, RD::TextureView()); |
936 | } |
937 | |
938 | rd->submit(); |
939 | rd->sync(); |
940 | |
941 | /* STEP 3: Raster the geometry to UV2 coords in the atlas textures GPU*/ |
942 | |
943 | _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); |
944 | |
945 | #ifdef DEBUG_TEXTURES |
946 | |
947 | for (int i = 0; i < atlas_slices; i++) { |
948 | Vector<uint8_t> s = rd->texture_get_data(position_tex, i); |
949 | Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAF, s); |
950 | img->save_exr("res://1_position_" + itos(i) + ".exr" , false); |
951 | |
952 | s = rd->texture_get_data(normal_tex, i); |
953 | img->set_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s); |
954 | img->save_exr("res://1_normal_" + itos(i) + ".exr" , false); |
955 | } |
956 | #endif |
957 | |
958 | #define FREE_RASTER_RESOURCES \ |
959 | rd->free(rasterize_shader); \ |
960 | rd->free(sampler); \ |
961 | rd->free(raster_depth_buffer); |
962 | |
963 | /* Plot direct light */ |
964 | |
965 | Ref<RDShaderFile> compute_shader; |
966 | compute_shader.instantiate(); |
967 | err = compute_shader->parse_versions_from_text(lm_compute_shader_glsl, p_bake_sh ? "\n#define USE_SH_LIGHTMAPS\n" : "" ); |
968 | if (err != OK) { |
969 | FREE_TEXTURES |
970 | FREE_BUFFERS |
971 | FREE_RASTER_RESOURCES |
972 | memdelete(rd); |
973 | compute_shader->print_errors("compute_shader" ); |
974 | } |
975 | ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); |
976 | |
977 | // Unoccluder |
978 | RID compute_shader_unocclude = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("unocclude" )); |
979 | ERR_FAIL_COND_V(compute_shader_unocclude.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); // internal check, should not happen |
980 | RID compute_shader_unocclude_pipeline = rd->compute_pipeline_create(compute_shader_unocclude); |
981 | |
982 | // Direct light |
983 | RID compute_shader_primary = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("primary" )); |
984 | ERR_FAIL_COND_V(compute_shader_primary.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); // internal check, should not happen |
985 | RID compute_shader_primary_pipeline = rd->compute_pipeline_create(compute_shader_primary); |
986 | |
987 | // Indirect light |
988 | RID compute_shader_secondary = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("secondary" )); |
989 | ERR_FAIL_COND_V(compute_shader_secondary.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen |
990 | RID compute_shader_secondary_pipeline = rd->compute_pipeline_create(compute_shader_secondary); |
991 | |
992 | // Light probes |
993 | RID compute_shader_light_probes = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("light_probes" )); |
994 | ERR_FAIL_COND_V(compute_shader_light_probes.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen |
995 | RID compute_shader_light_probes_pipeline = rd->compute_pipeline_create(compute_shader_light_probes); |
996 | |
997 | RID compute_base_uniform_set = rd->uniform_set_create(base_uniforms, compute_shader_primary, 0); |
998 | |
999 | #define FREE_COMPUTE_RESOURCES \ |
1000 | rd->free(compute_shader_unocclude); \ |
1001 | rd->free(compute_shader_primary); \ |
1002 | rd->free(compute_shader_secondary); \ |
1003 | rd->free(compute_shader_light_probes); |
1004 | |
1005 | PushConstant push_constant; |
1006 | { |
1007 | //set defaults |
1008 | push_constant.atlas_size[0] = atlas_size.width; |
1009 | push_constant.atlas_size[1] = atlas_size.height; |
1010 | push_constant.world_size[0] = bounds.size.x; |
1011 | push_constant.world_size[1] = bounds.size.y; |
1012 | push_constant.world_size[2] = bounds.size.z; |
1013 | push_constant.to_cell_offset[0] = bounds.position.x; |
1014 | push_constant.to_cell_offset[1] = bounds.position.y; |
1015 | push_constant.to_cell_offset[2] = bounds.position.z; |
1016 | push_constant.bias = p_bias; |
1017 | push_constant.to_cell_size[0] = (1.0 / bounds.size.x) * float(grid_size); |
1018 | push_constant.to_cell_size[1] = (1.0 / bounds.size.y) * float(grid_size); |
1019 | push_constant.to_cell_size[2] = (1.0 / bounds.size.z) * float(grid_size); |
1020 | push_constant.light_count = lights.size(); |
1021 | push_constant.grid_size = grid_size; |
1022 | push_constant.atlas_slice = 0; |
1023 | push_constant.region_ofs[0] = 0; |
1024 | push_constant.region_ofs[1] = 0; |
1025 | push_constant.environment_xform[0] = p_environment_transform.rows[0][0]; |
1026 | push_constant.environment_xform[1] = p_environment_transform.rows[1][0]; |
1027 | push_constant.environment_xform[2] = p_environment_transform.rows[2][0]; |
1028 | push_constant.environment_xform[3] = 0; |
1029 | push_constant.environment_xform[4] = p_environment_transform.rows[0][1]; |
1030 | push_constant.environment_xform[5] = p_environment_transform.rows[1][1]; |
1031 | push_constant.environment_xform[6] = p_environment_transform.rows[2][1]; |
1032 | push_constant.environment_xform[7] = 0; |
1033 | push_constant.environment_xform[8] = p_environment_transform.rows[0][2]; |
1034 | push_constant.environment_xform[9] = p_environment_transform.rows[1][2]; |
1035 | push_constant.environment_xform[10] = p_environment_transform.rows[2][2]; |
1036 | push_constant.environment_xform[11] = 0; |
1037 | } |
1038 | |
1039 | Vector3i group_size((atlas_size.x - 1) / 8 + 1, (atlas_size.y - 1) / 8 + 1, 1); |
1040 | rd->submit(); |
1041 | rd->sync(); |
1042 | |
1043 | if (p_step_function) { |
1044 | p_step_function(0.49, RTR("Un-occluding geometry" ), p_bake_userdata, true); |
1045 | } |
1046 | |
1047 | /* UNOCCLUDE */ |
1048 | { |
1049 | Vector<RD::Uniform> uniforms; |
1050 | { |
1051 | { |
1052 | RD::Uniform u; |
1053 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
1054 | u.binding = 0; |
1055 | u.append_id(position_tex); |
1056 | uniforms.push_back(u); |
1057 | } |
1058 | { |
1059 | RD::Uniform u; |
1060 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
1061 | u.binding = 1; |
1062 | u.append_id(unocclude_tex); //will be unused |
1063 | uniforms.push_back(u); |
1064 | } |
1065 | } |
1066 | |
1067 | RID unocclude_uniform_set = rd->uniform_set_create(uniforms, compute_shader_unocclude, 1); |
1068 | |
1069 | RD::ComputeListID compute_list = rd->compute_list_begin(); |
1070 | rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_unocclude_pipeline); |
1071 | rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0); |
1072 | rd->compute_list_bind_uniform_set(compute_list, unocclude_uniform_set, 1); |
1073 | |
1074 | for (int i = 0; i < atlas_slices; i++) { |
1075 | push_constant.atlas_slice = i; |
1076 | rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant)); |
1077 | rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z); |
1078 | //no barrier, let them run all together |
1079 | } |
1080 | rd->compute_list_end(); //done |
1081 | } |
1082 | |
1083 | if (p_step_function) { |
1084 | p_step_function(0.5, RTR("Plot direct lighting" ), p_bake_userdata, true); |
1085 | } |
1086 | |
1087 | /* PRIMARY (direct) LIGHT PASS */ |
1088 | { |
1089 | Vector<RD::Uniform> uniforms; |
1090 | { |
1091 | { |
1092 | RD::Uniform u; |
1093 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
1094 | u.binding = 0; |
1095 | u.append_id(light_source_tex); |
1096 | uniforms.push_back(u); |
1097 | } |
1098 | { |
1099 | RD::Uniform u; |
1100 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1101 | u.binding = 1; |
1102 | u.append_id(light_dest_tex); //will be unused |
1103 | uniforms.push_back(u); |
1104 | } |
1105 | { |
1106 | RD::Uniform u; |
1107 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1108 | u.binding = 2; |
1109 | u.append_id(position_tex); |
1110 | uniforms.push_back(u); |
1111 | } |
1112 | { |
1113 | RD::Uniform u; |
1114 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1115 | u.binding = 3; |
1116 | u.append_id(normal_tex); |
1117 | uniforms.push_back(u); |
1118 | } |
1119 | { |
1120 | RD::Uniform u; |
1121 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
1122 | u.binding = 4; |
1123 | u.append_id(light_accum_tex); |
1124 | uniforms.push_back(u); |
1125 | } |
1126 | { |
1127 | RD::Uniform u; |
1128 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
1129 | u.binding = 5; |
1130 | u.append_id(light_primary_dynamic_tex); |
1131 | uniforms.push_back(u); |
1132 | } |
1133 | } |
1134 | |
1135 | RID light_uniform_set = rd->uniform_set_create(uniforms, compute_shader_primary, 1); |
1136 | |
1137 | switch (p_quality) { |
1138 | case BAKE_QUALITY_LOW: { |
1139 | push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/low_quality_ray_count" ); |
1140 | } break; |
1141 | case BAKE_QUALITY_MEDIUM: { |
1142 | push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/medium_quality_ray_count" ); |
1143 | } break; |
1144 | case BAKE_QUALITY_HIGH: { |
1145 | push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/high_quality_ray_count" ); |
1146 | } break; |
1147 | case BAKE_QUALITY_ULTRA: { |
1148 | push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/ultra_quality_ray_count" ); |
1149 | } break; |
1150 | } |
1151 | |
1152 | push_constant.ray_count = CLAMP(push_constant.ray_count, 16u, 8192u); |
1153 | |
1154 | RD::ComputeListID compute_list = rd->compute_list_begin(); |
1155 | rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_primary_pipeline); |
1156 | rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0); |
1157 | rd->compute_list_bind_uniform_set(compute_list, light_uniform_set, 1); |
1158 | |
1159 | push_constant.environment_xform[11] = p_exposure_normalization; |
1160 | |
1161 | for (int i = 0; i < atlas_slices; i++) { |
1162 | push_constant.atlas_slice = i; |
1163 | rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant)); |
1164 | rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z); |
1165 | //no barrier, let them run all together |
1166 | } |
1167 | rd->compute_list_end(); //done |
1168 | |
1169 | push_constant.environment_xform[11] = 0.0; |
1170 | } |
1171 | |
1172 | #ifdef DEBUG_TEXTURES |
1173 | |
1174 | for (int i = 0; i < atlas_slices; i++) { |
1175 | Vector<uint8_t> s = rd->texture_get_data(light_source_tex, i); |
1176 | Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s); |
1177 | img->save_exr("res://2_light_primary_" + itos(i) + ".exr" , false); |
1178 | } |
1179 | #endif |
1180 | |
1181 | /* SECONDARY (indirect) LIGHT PASS(ES) */ |
1182 | if (p_step_function) { |
1183 | p_step_function(0.6, RTR("Integrate indirect lighting" ), p_bake_userdata, true); |
1184 | } |
1185 | |
1186 | if (p_bounces > 0) { |
1187 | Vector<RD::Uniform> uniforms; |
1188 | { |
1189 | { |
1190 | RD::Uniform u; |
1191 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
1192 | u.binding = 0; |
1193 | u.append_id(light_dest_tex); |
1194 | uniforms.push_back(u); |
1195 | } |
1196 | { |
1197 | RD::Uniform u; |
1198 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1199 | u.binding = 1; |
1200 | u.append_id(light_source_tex); |
1201 | uniforms.push_back(u); |
1202 | } |
1203 | { |
1204 | RD::Uniform u; |
1205 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1206 | u.binding = 2; |
1207 | u.append_id(position_tex); |
1208 | uniforms.push_back(u); |
1209 | } |
1210 | { |
1211 | RD::Uniform u; |
1212 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1213 | u.binding = 3; |
1214 | u.append_id(normal_tex); |
1215 | uniforms.push_back(u); |
1216 | } |
1217 | { |
1218 | RD::Uniform u; |
1219 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
1220 | u.binding = 4; |
1221 | u.append_id(light_accum_tex); |
1222 | uniforms.push_back(u); |
1223 | } |
1224 | { |
1225 | RD::Uniform u; |
1226 | u.uniform_type = RD::UNIFORM_TYPE_IMAGE; |
1227 | u.binding = 5; |
1228 | u.append_id(unocclude_tex); //reuse unocclude tex |
1229 | uniforms.push_back(u); |
1230 | } |
1231 | { |
1232 | RD::Uniform u; |
1233 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1234 | u.binding = 6; |
1235 | u.append_id(light_environment_tex); |
1236 | uniforms.push_back(u); |
1237 | } |
1238 | } |
1239 | |
1240 | RID secondary_uniform_set[2]; |
1241 | secondary_uniform_set[0] = rd->uniform_set_create(uniforms, compute_shader_secondary, 1); |
1242 | uniforms.write[0].set_id(0, light_source_tex); |
1243 | uniforms.write[1].set_id(0, light_dest_tex); |
1244 | secondary_uniform_set[1] = rd->uniform_set_create(uniforms, compute_shader_secondary, 1); |
1245 | |
1246 | int max_region_size = nearest_power_of_2_templated(int(GLOBAL_GET("rendering/lightmapping/bake_performance/region_size" ))); |
1247 | int max_rays = GLOBAL_GET("rendering/lightmapping/bake_performance/max_rays_per_pass" ); |
1248 | |
1249 | int x_regions = (atlas_size.width - 1) / max_region_size + 1; |
1250 | int y_regions = (atlas_size.height - 1) / max_region_size + 1; |
1251 | int ray_iterations = (push_constant.ray_count - 1) / max_rays + 1; |
1252 | |
1253 | rd->submit(); |
1254 | rd->sync(); |
1255 | |
1256 | for (int b = 0; b < p_bounces; b++) { |
1257 | int count = 0; |
1258 | if (b > 0) { |
1259 | SWAP(light_source_tex, light_dest_tex); |
1260 | SWAP(secondary_uniform_set[0], secondary_uniform_set[1]); |
1261 | } |
1262 | |
1263 | for (int s = 0; s < atlas_slices; s++) { |
1264 | push_constant.atlas_slice = s; |
1265 | |
1266 | for (int i = 0; i < x_regions; i++) { |
1267 | for (int j = 0; j < y_regions; j++) { |
1268 | int x = i * max_region_size; |
1269 | int y = j * max_region_size; |
1270 | int w = MIN((i + 1) * max_region_size, atlas_size.width) - x; |
1271 | int h = MIN((j + 1) * max_region_size, atlas_size.height) - y; |
1272 | |
1273 | push_constant.region_ofs[0] = x; |
1274 | push_constant.region_ofs[1] = y; |
1275 | |
1276 | group_size = Vector3i((w - 1) / 8 + 1, (h - 1) / 8 + 1, 1); |
1277 | |
1278 | for (int k = 0; k < ray_iterations; k++) { |
1279 | RD::ComputeListID compute_list = rd->compute_list_begin(); |
1280 | rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_secondary_pipeline); |
1281 | rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0); |
1282 | rd->compute_list_bind_uniform_set(compute_list, secondary_uniform_set[0], 1); |
1283 | |
1284 | push_constant.ray_from = k * max_rays; |
1285 | push_constant.ray_to = MIN((k + 1) * max_rays, int32_t(push_constant.ray_count)); |
1286 | rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant)); |
1287 | rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z); |
1288 | |
1289 | rd->compute_list_end(); //done |
1290 | rd->submit(); |
1291 | rd->sync(); |
1292 | |
1293 | count++; |
1294 | if (p_step_function) { |
1295 | int total = (atlas_slices * x_regions * y_regions * ray_iterations); |
1296 | int percent = count * 100 / total; |
1297 | float p = float(count) / total * 0.1; |
1298 | p_step_function(0.6 + p, vformat(RTR("Bounce %d/%d: Integrate indirect lighting %d%%" ), b + 1, p_bounces, percent), p_bake_userdata, false); |
1299 | } |
1300 | } |
1301 | } |
1302 | } |
1303 | } |
1304 | |
1305 | if (b == 0) { |
1306 | // This disables the environment for subsequent bounces |
1307 | push_constant.environment_xform[3] = -99.0f; |
1308 | } |
1309 | } |
1310 | |
1311 | // Restore the correct environment transform |
1312 | push_constant.environment_xform[3] = 0.0f; |
1313 | } |
1314 | |
1315 | /* LIGHTPROBES */ |
1316 | |
1317 | RID light_probe_buffer; |
1318 | |
1319 | if (probe_positions.size()) { |
1320 | light_probe_buffer = rd->storage_buffer_create(sizeof(float) * 4 * 9 * probe_positions.size()); |
1321 | |
1322 | if (p_step_function) { |
1323 | p_step_function(0.7, RTR("Baking lightprobes" ), p_bake_userdata, true); |
1324 | } |
1325 | |
1326 | Vector<RD::Uniform> uniforms; |
1327 | { |
1328 | { |
1329 | RD::Uniform u; |
1330 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
1331 | u.binding = 0; |
1332 | u.append_id(light_probe_buffer); |
1333 | uniforms.push_back(u); |
1334 | } |
1335 | { |
1336 | RD::Uniform u; |
1337 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1338 | u.binding = 1; |
1339 | u.append_id(light_dest_tex); |
1340 | uniforms.push_back(u); |
1341 | } |
1342 | { |
1343 | RD::Uniform u; |
1344 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1345 | u.binding = 2; |
1346 | u.append_id(light_primary_dynamic_tex); |
1347 | uniforms.push_back(u); |
1348 | } |
1349 | { |
1350 | RD::Uniform u; |
1351 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1352 | u.binding = 3; |
1353 | u.append_id(light_environment_tex); |
1354 | uniforms.push_back(u); |
1355 | } |
1356 | } |
1357 | RID light_probe_uniform_set = rd->uniform_set_create(uniforms, compute_shader_light_probes, 1); |
1358 | |
1359 | switch (p_quality) { |
1360 | case BAKE_QUALITY_LOW: { |
1361 | push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/low_quality_probe_ray_count" ); |
1362 | } break; |
1363 | case BAKE_QUALITY_MEDIUM: { |
1364 | push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/medium_quality_probe_ray_count" ); |
1365 | } break; |
1366 | case BAKE_QUALITY_HIGH: { |
1367 | push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/high_quality_probe_ray_count" ); |
1368 | } break; |
1369 | case BAKE_QUALITY_ULTRA: { |
1370 | push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/ultra_quality_probe_ray_count" ); |
1371 | } break; |
1372 | } |
1373 | |
1374 | push_constant.atlas_size[0] = probe_positions.size(); |
1375 | push_constant.ray_count = CLAMP(push_constant.ray_count, 16u, 8192u); |
1376 | |
1377 | int max_rays = GLOBAL_GET("rendering/lightmapping/bake_performance/max_rays_per_probe_pass" ); |
1378 | int ray_iterations = (push_constant.ray_count - 1) / max_rays + 1; |
1379 | |
1380 | for (int i = 0; i < ray_iterations; i++) { |
1381 | RD::ComputeListID compute_list = rd->compute_list_begin(); |
1382 | rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_light_probes_pipeline); |
1383 | rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0); |
1384 | rd->compute_list_bind_uniform_set(compute_list, light_probe_uniform_set, 1); |
1385 | |
1386 | push_constant.ray_from = i * max_rays; |
1387 | push_constant.ray_to = MIN((i + 1) * max_rays, int32_t(push_constant.ray_count)); |
1388 | rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant)); |
1389 | rd->compute_list_dispatch(compute_list, (probe_positions.size() - 1) / 64 + 1, 1, 1); |
1390 | |
1391 | rd->compute_list_end(); //done |
1392 | rd->submit(); |
1393 | rd->sync(); |
1394 | |
1395 | if (p_step_function) { |
1396 | int percent = i * 100 / ray_iterations; |
1397 | float p = float(i) / ray_iterations * 0.1; |
1398 | p_step_function(0.7 + p, vformat(RTR("Integrating light probes %d%%" ), percent), p_bake_userdata, false); |
1399 | } |
1400 | } |
1401 | |
1402 | push_constant.atlas_size[0] = atlas_size.x; //restore |
1403 | } |
1404 | |
1405 | #if 0 |
1406 | for (int i = 0; i < probe_positions.size(); i++) { |
1407 | Ref<Image> img = Image::create_empty(6, 4, false, Image::FORMAT_RGB8); |
1408 | for (int j = 0; j < 6; j++) { |
1409 | Vector<uint8_t> s = rd->texture_get_data(lightprobe_tex, i * 6 + j); |
1410 | Ref<Image> img2 = Image::create_from_data(2, 2, false, Image::FORMAT_RGBAF, s); |
1411 | img2->convert(Image::FORMAT_RGB8); |
1412 | img->blit_rect(img2, Rect2i(0, 0, 2, 2), Point2i((j % 3) * 2, (j / 3) * 2)); |
1413 | } |
1414 | img->save_png("res://3_light_probe_" + itos(i) + ".png" ); |
1415 | } |
1416 | #endif |
1417 | |
1418 | { |
1419 | SWAP(light_accum_tex, light_accum_tex2); |
1420 | 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)); |
1421 | if (unlikely(error != BAKE_OK)) { |
1422 | return error; |
1423 | } |
1424 | } |
1425 | |
1426 | /* DENOISE */ |
1427 | |
1428 | if (p_use_denoiser) { |
1429 | if (p_step_function) { |
1430 | p_step_function(0.8, RTR("Denoising" ), p_bake_userdata, true); |
1431 | } |
1432 | |
1433 | Ref<LightmapDenoiser> denoiser = LightmapDenoiser::create(); |
1434 | if (denoiser.is_valid()) { |
1435 | for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) { |
1436 | Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i); |
1437 | Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s); |
1438 | |
1439 | Ref<Image> denoised = denoiser->denoise_image(img); |
1440 | if (denoised != img) { |
1441 | denoised->convert(Image::FORMAT_RGBAH); |
1442 | Vector<uint8_t> ds = denoised->get_data(); |
1443 | denoised.unref(); //avoid copy on write |
1444 | { //restore alpha |
1445 | uint32_t count = s.size() / 2; //uint16s |
1446 | const uint16_t *src = (const uint16_t *)s.ptr(); |
1447 | uint16_t *dst = (uint16_t *)ds.ptrw(); |
1448 | for (uint32_t j = 0; j < count; j += 4) { |
1449 | dst[j + 3] = src[j + 3]; |
1450 | } |
1451 | } |
1452 | rd->texture_update(light_accum_tex, i, ds); |
1453 | } |
1454 | } |
1455 | } |
1456 | |
1457 | { |
1458 | SWAP(light_accum_tex, light_accum_tex2); |
1459 | 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)); |
1460 | if (unlikely(error != BAKE_OK)) { |
1461 | return error; |
1462 | } |
1463 | } |
1464 | } |
1465 | |
1466 | #ifdef DEBUG_TEXTURES |
1467 | |
1468 | for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) { |
1469 | Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i); |
1470 | Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s); |
1471 | img->save_exr("res://4_light_secondary_" + itos(i) + ".exr" , false); |
1472 | } |
1473 | #endif |
1474 | |
1475 | /* BLEND SEAMS */ |
1476 | //shaders |
1477 | Ref<RDShaderFile> blendseams_shader; |
1478 | blendseams_shader.instantiate(); |
1479 | err = blendseams_shader->parse_versions_from_text(lm_blendseams_shader_glsl); |
1480 | if (err != OK) { |
1481 | FREE_TEXTURES |
1482 | FREE_BUFFERS |
1483 | FREE_RASTER_RESOURCES |
1484 | FREE_COMPUTE_RESOURCES |
1485 | memdelete(rd); |
1486 | blendseams_shader->print_errors("blendseams_shader" ); |
1487 | } |
1488 | ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); |
1489 | |
1490 | RID blendseams_line_raster_shader = rd->shader_create_from_spirv(blendseams_shader->get_spirv_stages("lines" )); |
1491 | |
1492 | ERR_FAIL_COND_V(blendseams_line_raster_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); |
1493 | |
1494 | RID blendseams_triangle_raster_shader = rd->shader_create_from_spirv(blendseams_shader->get_spirv_stages("triangles" )); |
1495 | |
1496 | ERR_FAIL_COND_V(blendseams_triangle_raster_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); |
1497 | |
1498 | #define FREE_BLENDSEAMS_RESOURCES \ |
1499 | rd->free(blendseams_line_raster_shader); \ |
1500 | rd->free(blendseams_triangle_raster_shader); |
1501 | |
1502 | { |
1503 | //pre copy |
1504 | for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) { |
1505 | rd->texture_copy(light_accum_tex, light_accum_tex2, Vector3(), Vector3(), Vector3(atlas_size.width, atlas_size.height, 1), 0, 0, i, i); |
1506 | } |
1507 | |
1508 | Vector<RID> framebuffers; |
1509 | for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) { |
1510 | RID slice_tex = rd->texture_create_shared_from_slice(RD::TextureView(), light_accum_tex, i, 0); |
1511 | Vector<RID> fb; |
1512 | fb.push_back(slice_tex); |
1513 | fb.push_back(raster_depth_buffer); |
1514 | framebuffers.push_back(rd->framebuffer_create(fb)); |
1515 | } |
1516 | |
1517 | Vector<RD::Uniform> uniforms; |
1518 | { |
1519 | { |
1520 | RD::Uniform u; |
1521 | u.uniform_type = RD::UNIFORM_TYPE_TEXTURE; |
1522 | u.binding = 0; |
1523 | u.append_id(light_accum_tex2); |
1524 | uniforms.push_back(u); |
1525 | } |
1526 | } |
1527 | |
1528 | RID blendseams_raster_uniform = rd->uniform_set_create(uniforms, blendseams_line_raster_shader, 1); |
1529 | |
1530 | bool debug = false; |
1531 | RD::PipelineColorBlendState bs = RD::PipelineColorBlendState::create_blend(1); |
1532 | bs.attachments.write[0].src_alpha_blend_factor = RD::BLEND_FACTOR_ZERO; |
1533 | bs.attachments.write[0].dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE; |
1534 | |
1535 | RD::PipelineDepthStencilState ds; |
1536 | ds.enable_depth_test = true; |
1537 | ds.enable_depth_write = true; |
1538 | ds.depth_compare_operator = RD::COMPARE_OP_LESS; //so it does not render same pixel twice, this avoids wrong blending |
1539 | |
1540 | 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); |
1541 | 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); |
1542 | |
1543 | uint32_t seam_offset = 0; |
1544 | uint32_t triangle_offset = 0; |
1545 | |
1546 | Vector<Color> clear_colors; |
1547 | clear_colors.push_back(Color(0, 0, 0, 1)); |
1548 | for (int i = 0; i < atlas_slices; i++) { |
1549 | int subslices = (p_bake_sh ? 4 : 1); |
1550 | |
1551 | if (slice_seam_count[i] == 0) { |
1552 | continue; |
1553 | } |
1554 | |
1555 | for (int k = 0; k < subslices; k++) { |
1556 | RasterSeamsPushConstant seams_push_constant; |
1557 | seams_push_constant.slice = uint32_t(i * subslices + k); |
1558 | seams_push_constant.debug = debug; |
1559 | |
1560 | RD::DrawListID draw_list = rd->draw_list_begin(framebuffers[i], RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors); |
1561 | |
1562 | rd->draw_list_bind_uniform_set(draw_list, raster_base_uniform, 0); |
1563 | rd->draw_list_bind_uniform_set(draw_list, blendseams_raster_uniform, 1); |
1564 | |
1565 | const int uv_offset_count = 9; |
1566 | static const Vector3 uv_offsets[uv_offset_count] = { |
1567 | Vector3(0, 0, 0.5), //using zbuffer, so go inwards-outwards |
1568 | Vector3(0, 1, 0.2), |
1569 | Vector3(0, -1, 0.2), |
1570 | Vector3(1, 0, 0.2), |
1571 | Vector3(-1, 0, 0.2), |
1572 | Vector3(-1, -1, 0.1), |
1573 | Vector3(1, -1, 0.1), |
1574 | Vector3(1, 1, 0.1), |
1575 | Vector3(-1, 1, 0.1), |
1576 | }; |
1577 | |
1578 | /* step 1 use lines to blend the edges */ |
1579 | { |
1580 | seams_push_constant.base_index = seam_offset; |
1581 | rd->draw_list_bind_render_pipeline(draw_list, blendseams_line_raster_pipeline); |
1582 | seams_push_constant.uv_offset[0] = uv_offsets[0].x / float(atlas_size.width); |
1583 | seams_push_constant.uv_offset[1] = uv_offsets[0].y / float(atlas_size.height); |
1584 | seams_push_constant.blend = uv_offsets[0].z; |
1585 | |
1586 | rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant)); |
1587 | rd->draw_list_draw(draw_list, false, 1, slice_seam_count[i] * 4); |
1588 | } |
1589 | |
1590 | /* step 2 use triangles to mask the interior */ |
1591 | |
1592 | { |
1593 | seams_push_constant.base_index = triangle_offset; |
1594 | rd->draw_list_bind_render_pipeline(draw_list, blendseams_triangle_raster_pipeline); |
1595 | seams_push_constant.blend = 0; //do not draw them, just fill the z-buffer so its used as a mask |
1596 | |
1597 | rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant)); |
1598 | rd->draw_list_draw(draw_list, false, 1, slice_triangle_count[i] * 3); |
1599 | } |
1600 | /* step 3 blend around the triangle */ |
1601 | |
1602 | rd->draw_list_bind_render_pipeline(draw_list, blendseams_line_raster_pipeline); |
1603 | |
1604 | for (int j = 1; j < uv_offset_count; j++) { |
1605 | seams_push_constant.base_index = seam_offset; |
1606 | seams_push_constant.uv_offset[0] = uv_offsets[j].x / float(atlas_size.width); |
1607 | seams_push_constant.uv_offset[1] = uv_offsets[j].y / float(atlas_size.height); |
1608 | seams_push_constant.blend = uv_offsets[0].z; |
1609 | |
1610 | rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant)); |
1611 | rd->draw_list_draw(draw_list, false, 1, slice_seam_count[i] * 4); |
1612 | } |
1613 | rd->draw_list_end(); |
1614 | } |
1615 | seam_offset += slice_seam_count[i]; |
1616 | triangle_offset += slice_triangle_count[i]; |
1617 | } |
1618 | } |
1619 | |
1620 | #ifdef DEBUG_TEXTURES |
1621 | |
1622 | for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) { |
1623 | Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i); |
1624 | Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s); |
1625 | img->save_exr("res://5_blendseams" + itos(i) + ".exr" , false); |
1626 | } |
1627 | #endif |
1628 | if (p_step_function) { |
1629 | p_step_function(0.9, RTR("Retrieving textures" ), p_bake_userdata, true); |
1630 | } |
1631 | |
1632 | for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) { |
1633 | Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i); |
1634 | Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s); |
1635 | img->convert(Image::FORMAT_RGBH); //remove alpha |
1636 | bake_textures.push_back(img); |
1637 | } |
1638 | |
1639 | if (probe_positions.size() > 0) { |
1640 | probe_values.resize(probe_positions.size() * 9); |
1641 | Vector<uint8_t> probe_data = rd->buffer_get_data(light_probe_buffer); |
1642 | memcpy(probe_values.ptrw(), probe_data.ptr(), probe_data.size()); |
1643 | rd->free(light_probe_buffer); |
1644 | |
1645 | #ifdef DEBUG_TEXTURES |
1646 | { |
1647 | Ref<Image> img2 = Image::create_from_data(probe_values.size(), 1, false, Image::FORMAT_RGBAF, probe_data); |
1648 | img2->save_exr("res://6_lightprobes.exr" , false); |
1649 | } |
1650 | #endif |
1651 | } |
1652 | |
1653 | FREE_TEXTURES |
1654 | FREE_BUFFERS |
1655 | FREE_RASTER_RESOURCES |
1656 | FREE_COMPUTE_RESOURCES |
1657 | FREE_BLENDSEAMS_RESOURCES |
1658 | |
1659 | memdelete(rd); |
1660 | |
1661 | return BAKE_OK; |
1662 | } |
1663 | |
1664 | int LightmapperRD::get_bake_texture_count() const { |
1665 | return bake_textures.size(); |
1666 | } |
1667 | |
1668 | Ref<Image> LightmapperRD::get_bake_texture(int p_index) const { |
1669 | ERR_FAIL_INDEX_V(p_index, bake_textures.size(), Ref<Image>()); |
1670 | return bake_textures[p_index]; |
1671 | } |
1672 | |
1673 | int LightmapperRD::get_bake_mesh_count() const { |
1674 | return mesh_instances.size(); |
1675 | } |
1676 | |
1677 | Variant LightmapperRD::get_bake_mesh_userdata(int p_index) const { |
1678 | ERR_FAIL_INDEX_V(p_index, mesh_instances.size(), Variant()); |
1679 | return mesh_instances[p_index].data.userdata; |
1680 | } |
1681 | |
1682 | Rect2 LightmapperRD::get_bake_mesh_uv_scale(int p_index) const { |
1683 | ERR_FAIL_COND_V(bake_textures.size() == 0, Rect2()); |
1684 | Rect2 uv_ofs; |
1685 | Vector2 atlas_size = Vector2(bake_textures[0]->get_width(), bake_textures[0]->get_height()); |
1686 | uv_ofs.position = Vector2(mesh_instances[p_index].offset) / atlas_size; |
1687 | 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; |
1688 | return uv_ofs; |
1689 | } |
1690 | |
1691 | int LightmapperRD::get_bake_mesh_texture_slice(int p_index) const { |
1692 | ERR_FAIL_INDEX_V(p_index, mesh_instances.size(), Variant()); |
1693 | return mesh_instances[p_index].slice; |
1694 | } |
1695 | |
1696 | int LightmapperRD::get_bake_probe_count() const { |
1697 | return probe_positions.size(); |
1698 | } |
1699 | |
1700 | Vector3 LightmapperRD::get_bake_probe_point(int p_probe) const { |
1701 | ERR_FAIL_INDEX_V(p_probe, probe_positions.size(), Variant()); |
1702 | return Vector3(probe_positions[p_probe].position[0], probe_positions[p_probe].position[1], probe_positions[p_probe].position[2]); |
1703 | } |
1704 | |
1705 | Vector<Color> LightmapperRD::get_bake_probe_sh(int p_probe) const { |
1706 | ERR_FAIL_INDEX_V(p_probe, probe_positions.size(), Vector<Color>()); |
1707 | Vector<Color> ret; |
1708 | ret.resize(9); |
1709 | memcpy(ret.ptrw(), &probe_values[p_probe * 9], sizeof(Color) * 9); |
1710 | return ret; |
1711 | } |
1712 | |
1713 | LightmapperRD::LightmapperRD() { |
1714 | } |
1715 | |