lightmapper_rd.cpp 63 KB

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