lightmapper_rd.cpp 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  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/io/dir_access.h"
  36. #include "core/math/geometry_2d.h"
  37. #include "editor/editor_paths.h"
  38. #include "editor/editor_settings.h"
  39. #include "servers/rendering/rendering_device_binds.h"
  40. #if defined(VULKAN_ENABLED)
  41. #include "drivers/vulkan/rendering_context_driver_vulkan.h"
  42. #endif
  43. #if defined(METAL_ENABLED)
  44. #include "drivers/metal/rendering_context_driver_metal.h"
  45. #endif
  46. //uncomment this if you want to see textures from all the process saved
  47. //#define DEBUG_TEXTURES
  48. void LightmapperRD::add_mesh(const MeshData &p_mesh) {
  49. ERR_FAIL_COND(p_mesh.albedo_on_uv2.is_null() || p_mesh.albedo_on_uv2->is_empty());
  50. ERR_FAIL_COND(p_mesh.emission_on_uv2.is_null() || p_mesh.emission_on_uv2->is_empty());
  51. ERR_FAIL_COND(p_mesh.albedo_on_uv2->get_width() != p_mesh.emission_on_uv2->get_width());
  52. ERR_FAIL_COND(p_mesh.albedo_on_uv2->get_height() != p_mesh.emission_on_uv2->get_height());
  53. ERR_FAIL_COND(p_mesh.points.is_empty());
  54. MeshInstance mi;
  55. mi.data = p_mesh;
  56. mesh_instances.push_back(mi);
  57. }
  58. void LightmapperRD::add_directional_light(bool p_static, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_angular_distance, float p_shadow_blur) {
  59. Light l;
  60. l.type = LIGHT_TYPE_DIRECTIONAL;
  61. l.direction[0] = p_direction.x;
  62. l.direction[1] = p_direction.y;
  63. l.direction[2] = p_direction.z;
  64. l.color[0] = p_color.r;
  65. l.color[1] = p_color.g;
  66. l.color[2] = p_color.b;
  67. l.energy = p_energy;
  68. l.indirect_energy = p_indirect_energy;
  69. l.static_bake = p_static;
  70. l.size = Math::tan(Math::deg_to_rad(p_angular_distance));
  71. l.shadow_blur = p_shadow_blur;
  72. lights.push_back(l);
  73. }
  74. void LightmapperRD::add_omni_light(bool p_static, const Vector3 &p_position, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_size, float p_shadow_blur) {
  75. Light l;
  76. l.type = LIGHT_TYPE_OMNI;
  77. l.position[0] = p_position.x;
  78. l.position[1] = p_position.y;
  79. l.position[2] = p_position.z;
  80. l.range = p_range;
  81. l.attenuation = p_attenuation;
  82. l.color[0] = p_color.r;
  83. l.color[1] = p_color.g;
  84. l.color[2] = p_color.b;
  85. l.energy = p_energy;
  86. l.indirect_energy = p_indirect_energy;
  87. l.static_bake = p_static;
  88. l.size = p_size;
  89. l.shadow_blur = p_shadow_blur;
  90. lights.push_back(l);
  91. }
  92. 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_indirect_energy, float p_range, float p_attenuation, float p_spot_angle, float p_spot_attenuation, float p_size, float p_shadow_blur) {
  93. Light l;
  94. l.type = LIGHT_TYPE_SPOT;
  95. l.position[0] = p_position.x;
  96. l.position[1] = p_position.y;
  97. l.position[2] = p_position.z;
  98. l.direction[0] = p_direction.x;
  99. l.direction[1] = p_direction.y;
  100. l.direction[2] = p_direction.z;
  101. l.range = p_range;
  102. l.attenuation = p_attenuation;
  103. l.cos_spot_angle = Math::cos(Math::deg_to_rad(p_spot_angle));
  104. l.inv_spot_attenuation = 1.0f / p_spot_attenuation;
  105. l.color[0] = p_color.r;
  106. l.color[1] = p_color.g;
  107. l.color[2] = p_color.b;
  108. l.energy = p_energy;
  109. l.indirect_energy = p_indirect_energy;
  110. l.static_bake = p_static;
  111. l.size = p_size;
  112. l.shadow_blur = p_shadow_blur;
  113. lights.push_back(l);
  114. }
  115. void LightmapperRD::add_probe(const Vector3 &p_position) {
  116. Probe probe;
  117. probe.position[0] = p_position.x;
  118. probe.position[1] = p_position.y;
  119. probe.position[2] = p_position.z;
  120. probe.position[3] = 0;
  121. probe_positions.push_back(probe);
  122. }
  123. void LightmapperRD::_plot_triangle_into_triangle_index_list(int p_size, const Vector3i &p_ofs, const AABB &p_bounds, const Vector3 p_points[3], uint32_t p_triangle_index, LocalVector<TriangleSort> &p_triangles_sort, uint32_t p_grid_size) {
  124. int half_size = p_size / 2;
  125. for (int i = 0; i < 8; i++) {
  126. AABB aabb = p_bounds;
  127. aabb.size *= 0.5;
  128. Vector3i n = p_ofs;
  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. Vector3 qsize = aabb.size * 0.5; //quarter size, for fast aabb test
  143. if (!Geometry3D::triangle_box_overlap(aabb.position + qsize, qsize, p_points)) {
  144. //does not fit in child, go on
  145. continue;
  146. }
  147. }
  148. if (half_size == 1) {
  149. //got to the end
  150. TriangleSort ts;
  151. ts.cell_index = n.x + (n.y * p_grid_size) + (n.z * p_grid_size * p_grid_size);
  152. ts.triangle_index = p_triangle_index;
  153. ts.triangle_aabb.position = p_points[0];
  154. ts.triangle_aabb.size = Vector3();
  155. ts.triangle_aabb.expand_to(p_points[1]);
  156. ts.triangle_aabb.expand_to(p_points[2]);
  157. p_triangles_sort.push_back(ts);
  158. } else {
  159. _plot_triangle_into_triangle_index_list(half_size, n, aabb, p_points, p_triangle_index, p_triangles_sort, p_grid_size);
  160. }
  161. }
  162. }
  163. void LightmapperRD::_sort_triangle_clusters(uint32_t p_cluster_size, uint32_t p_cluster_index, uint32_t p_index_start, uint32_t p_count, LocalVector<TriangleSort> &p_triangle_sort, LocalVector<ClusterAABB> &p_cluster_aabb) {
  164. if (p_count == 0) {
  165. return;
  166. }
  167. // Compute AABB for all triangles in the range.
  168. SortArray<TriangleSort, TriangleSortAxis<0>> triangle_sorter_x;
  169. SortArray<TriangleSort, TriangleSortAxis<1>> triangle_sorter_y;
  170. SortArray<TriangleSort, TriangleSortAxis<2>> triangle_sorter_z;
  171. AABB cluster_aabb = p_triangle_sort[p_index_start].triangle_aabb;
  172. for (uint32_t i = 1; i < p_count; i++) {
  173. cluster_aabb.merge_with(p_triangle_sort[p_index_start + i].triangle_aabb);
  174. }
  175. if (p_count > p_cluster_size) {
  176. int longest_axis_index = cluster_aabb.get_longest_axis_index();
  177. switch (longest_axis_index) {
  178. case 0:
  179. triangle_sorter_x.sort(&p_triangle_sort[p_index_start], p_count);
  180. break;
  181. case 1:
  182. triangle_sorter_y.sort(&p_triangle_sort[p_index_start], p_count);
  183. break;
  184. case 2:
  185. triangle_sorter_z.sort(&p_triangle_sort[p_index_start], p_count);
  186. break;
  187. default:
  188. DEV_ASSERT(false && "Invalid axis returned by AABB.");
  189. break;
  190. }
  191. uint32_t left_cluster_count = next_power_of_2(p_count / 2);
  192. left_cluster_count = MAX(left_cluster_count, p_cluster_size);
  193. left_cluster_count = MIN(left_cluster_count, p_count);
  194. _sort_triangle_clusters(p_cluster_size, p_cluster_index, p_index_start, left_cluster_count, p_triangle_sort, p_cluster_aabb);
  195. if (left_cluster_count < p_count) {
  196. uint32_t cluster_index_right = p_cluster_index + (left_cluster_count / p_cluster_size);
  197. _sort_triangle_clusters(p_cluster_size, cluster_index_right, p_index_start + left_cluster_count, p_count - left_cluster_count, p_triangle_sort, p_cluster_aabb);
  198. }
  199. } else {
  200. ClusterAABB &aabb = p_cluster_aabb[p_cluster_index];
  201. Vector3 aabb_end = cluster_aabb.get_end();
  202. aabb.min_bounds[0] = cluster_aabb.position.x;
  203. aabb.min_bounds[1] = cluster_aabb.position.y;
  204. aabb.min_bounds[2] = cluster_aabb.position.z;
  205. aabb.max_bounds[0] = aabb_end.x;
  206. aabb.max_bounds[1] = aabb_end.y;
  207. aabb.max_bounds[2] = aabb_end.z;
  208. }
  209. }
  210. Lightmapper::BakeError LightmapperRD::_blit_meshes_into_atlas(int p_max_texture_size, int p_denoiser_range, Vector<Ref<Image>> &albedo_images, Vector<Ref<Image>> &emission_images, AABB &bounds, Size2i &atlas_size, int &atlas_slices, BakeStepFunc p_step_function, void *p_bake_userdata) {
  211. Vector<Size2i> sizes;
  212. for (int m_i = 0; m_i < mesh_instances.size(); m_i++) {
  213. MeshInstance &mi = mesh_instances.write[m_i];
  214. Size2i s = Size2i(mi.data.albedo_on_uv2->get_width(), mi.data.albedo_on_uv2->get_height());
  215. sizes.push_back(s);
  216. atlas_size = atlas_size.max(s + Size2i(2, 2).maxi(p_denoiser_range));
  217. }
  218. int max = nearest_power_of_2_templated(atlas_size.width);
  219. max = MAX(max, nearest_power_of_2_templated(atlas_size.height));
  220. if (max > p_max_texture_size) {
  221. return BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE;
  222. }
  223. if (p_step_function) {
  224. if (p_step_function(0.1, RTR("Determining optimal atlas size"), p_bake_userdata, true)) {
  225. return BAKE_ERROR_USER_ABORTED;
  226. }
  227. }
  228. atlas_size = Size2i(max, max);
  229. Size2i best_atlas_size;
  230. int best_atlas_slices = 0;
  231. int best_atlas_memory = 0x7FFFFFFF;
  232. Vector<Vector3i> best_atlas_offsets;
  233. // Determine best texture array atlas size by bruteforce fitting.
  234. while (atlas_size.x <= p_max_texture_size && atlas_size.y <= p_max_texture_size) {
  235. Vector<Vector2i> source_sizes;
  236. Vector<int> source_indices;
  237. source_sizes.resize(sizes.size());
  238. source_indices.resize(sizes.size());
  239. for (int i = 0; i < source_indices.size(); i++) {
  240. source_sizes.write[i] = sizes[i] + Vector2i(2, 2).maxi(p_denoiser_range); // Add padding between lightmaps.
  241. source_indices.write[i] = i;
  242. }
  243. Vector<Vector3i> atlas_offsets;
  244. atlas_offsets.resize(source_sizes.size());
  245. // Ensure the sizes can all fit into a single atlas layer.
  246. // This should always happen, and this check is only in place to prevent an infinite loop.
  247. for (int i = 0; i < source_sizes.size(); i++) {
  248. if (source_sizes[i] > atlas_size) {
  249. return BAKE_ERROR_ATLAS_TOO_SMALL;
  250. }
  251. }
  252. int slices = 0;
  253. while (source_sizes.size() > 0) {
  254. Vector<Vector3i> offsets = Geometry2D::partial_pack_rects(source_sizes, atlas_size);
  255. Vector<int> new_indices;
  256. Vector<Vector2i> new_sources;
  257. for (int i = 0; i < offsets.size(); i++) {
  258. Vector3i ofs = offsets[i];
  259. int sidx = source_indices[i];
  260. if (ofs.z > 0) {
  261. //valid
  262. ofs.z = slices;
  263. atlas_offsets.write[sidx] = ofs + Vector3i(1, 1, 0); // Center lightmap in the reserved oversized region
  264. } else {
  265. new_indices.push_back(sidx);
  266. new_sources.push_back(source_sizes[i]);
  267. }
  268. }
  269. source_sizes = new_sources;
  270. source_indices = new_indices;
  271. slices++;
  272. }
  273. int mem_used = atlas_size.x * atlas_size.y * slices;
  274. if (mem_used < best_atlas_memory) {
  275. best_atlas_size = atlas_size;
  276. best_atlas_offsets = atlas_offsets;
  277. best_atlas_slices = slices;
  278. best_atlas_memory = mem_used;
  279. }
  280. if (atlas_size.width == atlas_size.height) {
  281. atlas_size.width *= 2;
  282. } else {
  283. atlas_size.height *= 2;
  284. }
  285. }
  286. atlas_size = best_atlas_size;
  287. atlas_slices = best_atlas_slices;
  288. // apply the offsets and slice to all images, and also blit albedo and emission
  289. albedo_images.resize(atlas_slices);
  290. emission_images.resize(atlas_slices);
  291. if (p_step_function) {
  292. if (p_step_function(0.2, RTR("Blitting albedo and emission"), p_bake_userdata, true)) {
  293. return BAKE_ERROR_USER_ABORTED;
  294. }
  295. }
  296. for (int i = 0; i < atlas_slices; i++) {
  297. Ref<Image> albedo = Image::create_empty(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBA8);
  298. albedo->set_as_black();
  299. albedo_images.write[i] = albedo;
  300. Ref<Image> emission = Image::create_empty(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH);
  301. emission->set_as_black();
  302. emission_images.write[i] = emission;
  303. }
  304. //assign uv positions
  305. for (int m_i = 0; m_i < mesh_instances.size(); m_i++) {
  306. MeshInstance &mi = mesh_instances.write[m_i];
  307. mi.offset.x = best_atlas_offsets[m_i].x;
  308. mi.offset.y = best_atlas_offsets[m_i].y;
  309. mi.slice = best_atlas_offsets[m_i].z;
  310. albedo_images.write[mi.slice]->blit_rect(mi.data.albedo_on_uv2, Rect2i(Vector2i(), mi.data.albedo_on_uv2->get_size()), mi.offset);
  311. emission_images.write[mi.slice]->blit_rect(mi.data.emission_on_uv2, Rect2(Vector2i(), mi.data.emission_on_uv2->get_size()), mi.offset);
  312. }
  313. return BAKE_OK;
  314. }
  315. void LightmapperRD::_create_acceleration_structures(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, AABB &bounds, int grid_size, uint32_t p_cluster_size, Vector<Probe> &p_probe_positions, GenerateProbes p_generate_probes, Vector<int> &slice_triangle_count, Vector<int> &slice_seam_count, RID &vertex_buffer, RID &triangle_buffer, RID &lights_buffer, RID &r_triangle_indices_buffer, RID &r_cluster_indices_buffer, RID &r_cluster_aabbs_buffer, RID &probe_positions_buffer, RID &grid_texture, RID &seams_buffer, BakeStepFunc p_step_function, void *p_bake_userdata) {
  316. HashMap<Vertex, uint32_t, VertexHash> vertex_map;
  317. //fill triangles array and vertex array
  318. LocalVector<Triangle> triangles;
  319. LocalVector<Vertex> vertex_array;
  320. LocalVector<Seam> seams;
  321. slice_triangle_count.resize(atlas_slices);
  322. slice_seam_count.resize(atlas_slices);
  323. for (int i = 0; i < atlas_slices; i++) {
  324. slice_triangle_count.write[i] = 0;
  325. slice_seam_count.write[i] = 0;
  326. }
  327. bounds = AABB();
  328. for (int m_i = 0; m_i < mesh_instances.size(); m_i++) {
  329. if (p_step_function) {
  330. float p = float(m_i + 1) / MAX(1, mesh_instances.size()) * 0.1;
  331. 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);
  332. }
  333. HashMap<Edge, EdgeUV2, EdgeHash> edges;
  334. MeshInstance &mi = mesh_instances.write[m_i];
  335. Vector2 uv_scale = Vector2(mi.data.albedo_on_uv2->get_width(), mi.data.albedo_on_uv2->get_height()) / Vector2(atlas_size);
  336. Vector2 uv_offset = Vector2(mi.offset) / Vector2(atlas_size);
  337. if (m_i == 0) {
  338. bounds.position = mi.data.points[0];
  339. }
  340. for (int i = 0; i < mi.data.points.size(); i += 3) {
  341. Vector3 vtxs[3] = { mi.data.points[i + 0], mi.data.points[i + 1], mi.data.points[i + 2] };
  342. 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 };
  343. Vector3 normal[3] = { mi.data.normal[i + 0], mi.data.normal[i + 1], mi.data.normal[i + 2] };
  344. AABB taabb;
  345. Triangle t;
  346. t.slice = mi.slice;
  347. for (int k = 0; k < 3; k++) {
  348. bounds.expand_to(vtxs[k]);
  349. Vertex v;
  350. v.position[0] = vtxs[k].x;
  351. v.position[1] = vtxs[k].y;
  352. v.position[2] = vtxs[k].z;
  353. v.uv[0] = uvs[k].x;
  354. v.uv[1] = uvs[k].y;
  355. v.normal_xy[0] = normal[k].x;
  356. v.normal_xy[1] = normal[k].y;
  357. v.normal_z = normal[k].z;
  358. uint32_t *indexptr = vertex_map.getptr(v);
  359. if (indexptr) {
  360. t.indices[k] = *indexptr;
  361. } else {
  362. uint32_t new_index = vertex_map.size();
  363. t.indices[k] = new_index;
  364. vertex_map[v] = new_index;
  365. vertex_array.push_back(v);
  366. }
  367. if (k == 0) {
  368. taabb.position = vtxs[k];
  369. } else {
  370. taabb.expand_to(vtxs[k]);
  371. }
  372. }
  373. //compute seams that will need to be blended later
  374. for (int k = 0; k < 3; k++) {
  375. int n = (k + 1) % 3;
  376. Edge edge(vtxs[k], vtxs[n], normal[k], normal[n]);
  377. Vector2i edge_indices(t.indices[k], t.indices[n]);
  378. EdgeUV2 uv2(uvs[k], uvs[n], edge_indices);
  379. if (edge.b == edge.a) {
  380. continue; //degenerate, somehow
  381. }
  382. if (edge.b < edge.a) {
  383. SWAP(edge.a, edge.b);
  384. SWAP(edge.na, edge.nb);
  385. SWAP(uv2.a, uv2.b);
  386. SWAP(uv2.indices.x, uv2.indices.y);
  387. SWAP(edge_indices.x, edge_indices.y);
  388. }
  389. EdgeUV2 *euv2 = edges.getptr(edge);
  390. if (!euv2) {
  391. edges[edge] = uv2;
  392. } else {
  393. if (*euv2 == uv2) {
  394. continue; // seam shared UV space, no need to blend
  395. }
  396. if (euv2->seam_found) {
  397. continue; //bad geometry
  398. }
  399. Seam seam;
  400. seam.a = edge_indices;
  401. seam.b = euv2->indices;
  402. seam.slice = mi.slice;
  403. seams.push_back(seam);
  404. slice_seam_count.write[mi.slice]++;
  405. euv2->seam_found = true;
  406. }
  407. }
  408. t.min_bounds[0] = taabb.position.x;
  409. t.min_bounds[1] = taabb.position.y;
  410. t.min_bounds[2] = taabb.position.z;
  411. t.max_bounds[0] = taabb.position.x + MAX(taabb.size.x, 0.0001);
  412. t.max_bounds[1] = taabb.position.y + MAX(taabb.size.y, 0.0001);
  413. t.max_bounds[2] = taabb.position.z + MAX(taabb.size.z, 0.0001);
  414. t.pad0 = t.pad1 = 0; //make valgrind not complain
  415. triangles.push_back(t);
  416. slice_triangle_count.write[t.slice]++;
  417. }
  418. }
  419. //also consider probe positions for bounds
  420. for (int i = 0; i < p_probe_positions.size(); i++) {
  421. Vector3 pp(p_probe_positions[i].position[0], p_probe_positions[i].position[1], p_probe_positions[i].position[2]);
  422. bounds.expand_to(pp);
  423. }
  424. bounds.grow_by(0.1); //grow a bit to avoid numerical error
  425. triangles.sort(); //sort by slice
  426. seams.sort();
  427. if (p_step_function) {
  428. p_step_function(0.4, RTR("Optimizing acceleration structure"), p_bake_userdata, true);
  429. }
  430. //fill list of triangles in grid
  431. LocalVector<TriangleSort> triangle_sort;
  432. for (uint32_t i = 0; i < triangles.size(); i++) {
  433. const Triangle &t = triangles[i];
  434. Vector3 face[3] = {
  435. Vector3(vertex_array[t.indices[0]].position[0], vertex_array[t.indices[0]].position[1], vertex_array[t.indices[0]].position[2]),
  436. Vector3(vertex_array[t.indices[1]].position[0], vertex_array[t.indices[1]].position[1], vertex_array[t.indices[1]].position[2]),
  437. Vector3(vertex_array[t.indices[2]].position[0], vertex_array[t.indices[2]].position[1], vertex_array[t.indices[2]].position[2])
  438. };
  439. _plot_triangle_into_triangle_index_list(grid_size, Vector3i(), bounds, face, i, triangle_sort, grid_size);
  440. }
  441. //sort it
  442. triangle_sort.sort();
  443. LocalVector<uint32_t> cluster_indices;
  444. LocalVector<ClusterAABB> cluster_aabbs;
  445. Vector<uint32_t> triangle_indices;
  446. triangle_indices.resize(triangle_sort.size());
  447. Vector<uint32_t> grid_indices;
  448. grid_indices.resize(grid_size * grid_size * grid_size * 2);
  449. memset(grid_indices.ptrw(), 0, grid_indices.size() * sizeof(uint32_t));
  450. {
  451. // Fill grid with cell indices.
  452. uint32_t last_cell = 0xFFFFFFFF;
  453. uint32_t *giw = grid_indices.ptrw();
  454. uint32_t cluster_count = 0;
  455. uint32_t solid_cell_count = 0;
  456. for (uint32_t i = 0; i < triangle_sort.size(); i++) {
  457. uint32_t cell = triangle_sort[i].cell_index;
  458. if (cell != last_cell) {
  459. giw[cell * 2 + 1] = solid_cell_count;
  460. solid_cell_count++;
  461. }
  462. if ((giw[cell * 2] % p_cluster_size) == 0) {
  463. // Add an extra cluster every time the triangle counter reaches a multiple of the cluster size.
  464. cluster_count++;
  465. }
  466. giw[cell * 2]++;
  467. last_cell = cell;
  468. }
  469. // Build fixed-size triangle clusters for all the cells to speed up the traversal. A cell can hold multiple clusters that each contain a fixed
  470. // amount of triangles and an AABB. The tracer will check against the AABBs first to know whether it needs to visit the cell's triangles.
  471. //
  472. // The building algorithm will divide the triangles recursively contained inside each cell, sorting by the longest axis of the AABB on each step.
  473. //
  474. // - If the amount of triangles is less or equal to the cluster size, the AABB will be stored and the algorithm stops.
  475. //
  476. // - The division by two is increased to the next power of two of half the amount of triangles (with cluster size as the minimum value) to
  477. // ensure the first half always fills the cluster.
  478. cluster_indices.resize(solid_cell_count * 2);
  479. cluster_aabbs.resize(cluster_count);
  480. uint32_t i = 0;
  481. uint32_t cluster_index = 0;
  482. uint32_t solid_cell_index = 0;
  483. uint32_t *tiw = triangle_indices.ptrw();
  484. while (i < triangle_sort.size()) {
  485. cluster_indices[solid_cell_index * 2] = cluster_index;
  486. cluster_indices[solid_cell_index * 2 + 1] = i;
  487. uint32_t cell = triangle_sort[i].cell_index;
  488. uint32_t triangle_count = giw[cell * 2];
  489. uint32_t cell_cluster_count = (triangle_count + p_cluster_size - 1) / p_cluster_size;
  490. _sort_triangle_clusters(p_cluster_size, cluster_index, i, triangle_count, triangle_sort, cluster_aabbs);
  491. for (uint32_t j = 0; j < triangle_count; j++) {
  492. tiw[i + j] = triangle_sort[i + j].triangle_index;
  493. }
  494. i += triangle_count;
  495. cluster_index += cell_cluster_count;
  496. solid_cell_index++;
  497. }
  498. }
  499. #if 0
  500. for (int i = 0; i < grid_size; i++) {
  501. for (int j = 0; j < grid_size; j++) {
  502. for (int k = 0; k < grid_size; k++) {
  503. uint32_t index = i * (grid_size * grid_size) + j * grid_size + k;
  504. grid_indices.write[index * 2] = float(i) / grid_size * 255;
  505. grid_indices.write[index * 2 + 1] = float(j) / grid_size * 255;
  506. }
  507. }
  508. }
  509. #endif
  510. #if 0
  511. for (int i = 0; i < grid_size; i++) {
  512. Vector<uint8_t> grid_usage;
  513. grid_usage.resize(grid_size * grid_size);
  514. for (int j = 0; j < grid_usage.size(); j++) {
  515. uint32_t ofs = i * grid_size * grid_size + j;
  516. uint32_t count = grid_indices[ofs * 2];
  517. grid_usage.write[j] = count > 0 ? 255 : 0;
  518. }
  519. Ref<Image> img = Image::create_from_data(grid_size, grid_size, false, Image::FORMAT_L8, grid_usage);
  520. img->save_png("res://grid_layer_" + itos(1000 + i).substr(1, 3) + ".png");
  521. }
  522. #endif
  523. /*****************************/
  524. /*** CREATE GPU STRUCTURES ***/
  525. /*****************************/
  526. lights.sort();
  527. Vector<Vector2i> seam_buffer_vec;
  528. seam_buffer_vec.resize(seams.size() * 2);
  529. for (uint32_t i = 0; i < seams.size(); i++) {
  530. seam_buffer_vec.write[i * 2 + 0] = seams[i].a;
  531. seam_buffer_vec.write[i * 2 + 1] = seams[i].b;
  532. }
  533. { //buffers
  534. Vector<uint8_t> vb = vertex_array.to_byte_array();
  535. vertex_buffer = rd->storage_buffer_create(vb.size(), vb);
  536. Vector<uint8_t> tb = triangles.to_byte_array();
  537. triangle_buffer = rd->storage_buffer_create(tb.size(), tb);
  538. Vector<uint8_t> tib = triangle_indices.to_byte_array();
  539. r_triangle_indices_buffer = rd->storage_buffer_create(tib.size(), tib);
  540. Vector<uint8_t> cib = cluster_indices.to_byte_array();
  541. r_cluster_indices_buffer = rd->storage_buffer_create(cib.size(), cib);
  542. Vector<uint8_t> cab = cluster_aabbs.to_byte_array();
  543. r_cluster_aabbs_buffer = rd->storage_buffer_create(cab.size(), cab);
  544. Vector<uint8_t> lb = lights.to_byte_array();
  545. if (lb.size() == 0) {
  546. lb.resize(sizeof(Light)); //even if no lights, the buffer must exist
  547. }
  548. lights_buffer = rd->storage_buffer_create(lb.size(), lb);
  549. Vector<uint8_t> sb = seam_buffer_vec.to_byte_array();
  550. if (sb.size() == 0) {
  551. sb.resize(sizeof(Vector2i) * 2); //even if no seams, the buffer must exist
  552. }
  553. seams_buffer = rd->storage_buffer_create(sb.size(), sb);
  554. Vector<uint8_t> pb = p_probe_positions.to_byte_array();
  555. if (pb.size() == 0) {
  556. pb.resize(sizeof(Probe));
  557. }
  558. probe_positions_buffer = rd->storage_buffer_create(pb.size(), pb);
  559. }
  560. { //grid
  561. RD::TextureFormat tf;
  562. tf.width = grid_size;
  563. tf.height = grid_size;
  564. tf.depth = grid_size;
  565. tf.texture_type = RD::TEXTURE_TYPE_3D;
  566. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  567. Vector<Vector<uint8_t>> texdata;
  568. texdata.resize(1);
  569. //grid and indices
  570. tf.format = RD::DATA_FORMAT_R32G32_UINT;
  571. texdata.write[0] = grid_indices.to_byte_array();
  572. grid_texture = rd->texture_create(tf, RD::TextureView(), texdata);
  573. }
  574. }
  575. 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) {
  576. Vector<RID> framebuffers;
  577. for (int i = 0; i < atlas_slices; i++) {
  578. RID slice_pos_tex = rd->texture_create_shared_from_slice(RD::TextureView(), position_tex, i, 0);
  579. RID slice_unoc_tex = rd->texture_create_shared_from_slice(RD::TextureView(), unocclude_tex, i, 0);
  580. RID slice_norm_tex = rd->texture_create_shared_from_slice(RD::TextureView(), normal_tex, i, 0);
  581. Vector<RID> fb;
  582. fb.push_back(slice_pos_tex);
  583. fb.push_back(slice_norm_tex);
  584. fb.push_back(slice_unoc_tex);
  585. fb.push_back(raster_depth_buffer);
  586. framebuffers.push_back(rd->framebuffer_create(fb));
  587. }
  588. RD::PipelineDepthStencilState ds;
  589. ds.enable_depth_test = true;
  590. ds.enable_depth_write = true;
  591. ds.depth_compare_operator = RD::COMPARE_OP_LESS; //so it does render same pixel twice
  592. 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);
  593. RID raster_pipeline_wire;
  594. {
  595. RD::PipelineRasterizationState rw;
  596. rw.wireframe = true;
  597. 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);
  598. }
  599. uint32_t triangle_offset = 0;
  600. Vector<Color> clear_colors;
  601. clear_colors.push_back(Color(0, 0, 0, 0));
  602. clear_colors.push_back(Color(0, 0, 0, 0));
  603. clear_colors.push_back(Color(0, 0, 0, 0));
  604. for (int i = 0; i < atlas_slices; i++) {
  605. RasterPushConstant raster_push_constant;
  606. raster_push_constant.atlas_size[0] = atlas_size.x;
  607. raster_push_constant.atlas_size[1] = atlas_size.y;
  608. raster_push_constant.base_triangle = triangle_offset;
  609. raster_push_constant.to_cell_offset[0] = bounds.position.x;
  610. raster_push_constant.to_cell_offset[1] = bounds.position.y;
  611. raster_push_constant.to_cell_offset[2] = bounds.position.z;
  612. raster_push_constant.bias = p_bias;
  613. raster_push_constant.to_cell_size[0] = (1.0 / bounds.size.x) * float(grid_size);
  614. raster_push_constant.to_cell_size[1] = (1.0 / bounds.size.y) * float(grid_size);
  615. raster_push_constant.to_cell_size[2] = (1.0 / bounds.size.z) * float(grid_size);
  616. raster_push_constant.grid_size[0] = grid_size;
  617. raster_push_constant.grid_size[1] = grid_size;
  618. raster_push_constant.grid_size[2] = grid_size;
  619. // Half pixel offset is required so the rasterizer doesn't output face edges directly aligned into pixels.
  620. // This fixes artifacts where the pixel would be traced from the edge of a face, causing half the rays to
  621. // be outside of the boundaries of the geometry. See <https://github.com/godotengine/godot/issues/69126>.
  622. raster_push_constant.uv_offset[0] = -0.5f / float(atlas_size.x);
  623. raster_push_constant.uv_offset[1] = -0.5f / float(atlas_size.y);
  624. RD::DrawListID draw_list = rd->draw_list_begin(framebuffers[i], RD::DRAW_CLEAR_ALL, clear_colors, 1.0f, 0, Rect2(), RDD::BreadcrumbMarker::LIGHTMAPPER_PASS);
  625. //draw opaque
  626. rd->draw_list_bind_render_pipeline(draw_list, raster_pipeline);
  627. rd->draw_list_bind_uniform_set(draw_list, raster_base_uniform, 0);
  628. rd->draw_list_set_push_constant(draw_list, &raster_push_constant, sizeof(RasterPushConstant));
  629. rd->draw_list_draw(draw_list, false, 1, slice_triangle_count[i] * 3);
  630. //draw wire
  631. rd->draw_list_bind_render_pipeline(draw_list, raster_pipeline_wire);
  632. rd->draw_list_bind_uniform_set(draw_list, raster_base_uniform, 0);
  633. rd->draw_list_set_push_constant(draw_list, &raster_push_constant, sizeof(RasterPushConstant));
  634. rd->draw_list_draw(draw_list, false, 1, slice_triangle_count[i] * 3);
  635. rd->draw_list_end();
  636. triangle_offset += slice_triangle_count[i];
  637. }
  638. }
  639. static Vector<RD::Uniform> dilate_or_denoise_common_uniforms(RID &p_source_light_tex, RID &p_dest_light_tex) {
  640. Vector<RD::Uniform> uniforms;
  641. {
  642. RD::Uniform u;
  643. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  644. u.binding = 0;
  645. u.append_id(p_dest_light_tex);
  646. uniforms.push_back(u);
  647. }
  648. {
  649. RD::Uniform u;
  650. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  651. u.binding = 1;
  652. u.append_id(p_source_light_tex);
  653. uniforms.push_back(u);
  654. }
  655. return uniforms;
  656. }
  657. 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) {
  658. Vector<RD::Uniform> uniforms = dilate_or_denoise_common_uniforms(source_light_tex, dest_light_tex);
  659. RID compute_shader_dilate = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("dilate"));
  660. ERR_FAIL_COND_V(compute_shader_dilate.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen
  661. RID compute_shader_dilate_pipeline = rd->compute_pipeline_create(compute_shader_dilate);
  662. RID dilate_uniform_set = rd->uniform_set_create(uniforms, compute_shader_dilate, 1);
  663. RD::ComputeListID compute_list = rd->compute_list_begin();
  664. rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_dilate_pipeline);
  665. rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
  666. rd->compute_list_bind_uniform_set(compute_list, dilate_uniform_set, 1);
  667. push_constant.region_ofs[0] = 0;
  668. push_constant.region_ofs[1] = 0;
  669. Vector3i group_size(Math::division_round_up(atlas_size.x, 8), Math::division_round_up(atlas_size.y, 8), 1); //restore group size
  670. for (int i = 0; i < atlas_slices; i++) {
  671. push_constant.atlas_slice = i;
  672. rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  673. rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
  674. //no barrier, let them run all together
  675. }
  676. rd->compute_list_end();
  677. rd->free(compute_shader_dilate);
  678. #ifdef DEBUG_TEXTURES
  679. for (int i = 0; i < atlas_slices; i++) {
  680. Vector<uint8_t> s = rd->texture_get_data(source_light_tex, i);
  681. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  682. img->convert(Image::FORMAT_RGBA8);
  683. img->save_png("res://5_dilated_" + itos(i) + ".png");
  684. }
  685. #endif
  686. return BAKE_OK;
  687. }
  688. LightmapperRD::BakeError LightmapperRD::_pack_l1(RenderingDevice *rd, Ref<RDShaderFile> &compute_shader, RID &compute_base_uniform_set, PushConstant &push_constant, RID &source_light_tex, RID &dest_light_tex, const Size2i &atlas_size, int atlas_slices) {
  689. Vector<RD::Uniform> uniforms = dilate_or_denoise_common_uniforms(source_light_tex, dest_light_tex);
  690. RID compute_shader_pack = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("pack_coeffs"));
  691. ERR_FAIL_COND_V(compute_shader_pack.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen
  692. RID compute_shader_pack_pipeline = rd->compute_pipeline_create(compute_shader_pack);
  693. RID dilate_uniform_set = rd->uniform_set_create(uniforms, compute_shader_pack, 1);
  694. RD::ComputeListID compute_list = rd->compute_list_begin();
  695. rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_pack_pipeline);
  696. rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
  697. rd->compute_list_bind_uniform_set(compute_list, dilate_uniform_set, 1);
  698. push_constant.region_ofs[0] = 0;
  699. push_constant.region_ofs[1] = 0;
  700. Vector3i group_size(Math::division_round_up(atlas_size.x, 8), Math::division_round_up(atlas_size.y, 8), 1); //restore group size
  701. for (int i = 0; i < atlas_slices; i++) {
  702. push_constant.atlas_slice = i;
  703. rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  704. rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
  705. //no barrier, let them run all together
  706. }
  707. rd->compute_list_end();
  708. rd->free(compute_shader_pack);
  709. return BAKE_OK;
  710. }
  711. Error LightmapperRD::_store_pfm(RenderingDevice *p_rd, RID p_atlas_tex, int p_index, const Size2i &p_atlas_size, const String &p_name) {
  712. Vector<uint8_t> data = p_rd->texture_get_data(p_atlas_tex, p_index);
  713. Ref<Image> img = Image::create_from_data(p_atlas_size.width, p_atlas_size.height, false, Image::FORMAT_RGBAH, data);
  714. img->convert(Image::FORMAT_RGBF);
  715. Vector<uint8_t> data_float = img->get_data();
  716. Error err = OK;
  717. Ref<FileAccess> file = FileAccess::open(p_name, FileAccess::WRITE, &err);
  718. ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save PFN at path: '%s'.", p_name));
  719. file->store_line("PF");
  720. file->store_line(vformat("%d %d", img->get_width(), img->get_height()));
  721. #ifdef BIG_ENDIAN_ENABLED
  722. file->store_line("1.0");
  723. #else
  724. file->store_line("-1.0");
  725. #endif
  726. file->store_buffer(data_float);
  727. file->close();
  728. return OK;
  729. }
  730. Ref<Image> LightmapperRD::_read_pfm(const String &p_name) {
  731. Error err = OK;
  732. Ref<FileAccess> file = FileAccess::open(p_name, FileAccess::READ, &err);
  733. ERR_FAIL_COND_V_MSG(err, Ref<Image>(), vformat("Can't load PFM at path: '%s'.", p_name));
  734. ERR_FAIL_COND_V(file->get_line() != "PF", Ref<Image>());
  735. Vector<String> new_size = file->get_line().split(" ");
  736. ERR_FAIL_COND_V(new_size.size() != 2, Ref<Image>());
  737. int new_width = new_size[0].to_int();
  738. int new_height = new_size[1].to_int();
  739. float endian = file->get_line().to_float();
  740. Vector<uint8_t> new_data = file->get_buffer(file->get_length() - file->get_position());
  741. file->close();
  742. #ifdef BIG_ENDIAN_ENABLED
  743. if (unlikely(endian < 0.0)) {
  744. uint32_t count = new_data.size() / 4;
  745. uint16_t *dst = (uint16_t *)new_data.ptrw();
  746. for (uint32_t j = 0; j < count; j++) {
  747. dst[j * 4] = BSWAP32(dst[j * 4]);
  748. }
  749. }
  750. #else
  751. if (unlikely(endian > 0.0)) {
  752. uint32_t count = new_data.size() / 4;
  753. uint16_t *dst = (uint16_t *)new_data.ptrw();
  754. for (uint32_t j = 0; j < count; j++) {
  755. dst[j * 4] = BSWAP32(dst[j * 4]);
  756. }
  757. }
  758. #endif
  759. Ref<Image> img = Image::create_from_data(new_width, new_height, false, Image::FORMAT_RGBF, new_data);
  760. img->convert(Image::FORMAT_RGBAH);
  761. return img;
  762. }
  763. LightmapperRD::BakeError LightmapperRD::_denoise_oidn(RenderingDevice *p_rd, RID p_source_light_tex, RID p_source_normal_tex, RID p_dest_light_tex, const Size2i &p_atlas_size, int p_atlas_slices, bool p_bake_sh, const String &p_exe) {
  764. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  765. for (int i = 0; i < p_atlas_slices; i++) {
  766. String fname_norm_in = EditorPaths::get_singleton()->get_cache_dir().path_join(vformat("temp_norm_%d.pfm", i));
  767. _store_pfm(p_rd, p_source_normal_tex, i, p_atlas_size, fname_norm_in);
  768. for (int j = 0; j < (p_bake_sh ? 4 : 1); j++) {
  769. int index = i * (p_bake_sh ? 4 : 1) + j;
  770. String fname_light_in = EditorPaths::get_singleton()->get_cache_dir().path_join(vformat("temp_light_%d.pfm", index));
  771. String fname_out = EditorPaths::get_singleton()->get_cache_dir().path_join(vformat("temp_denoised_%d.pfm", index));
  772. _store_pfm(p_rd, p_source_light_tex, index, p_atlas_size, fname_light_in);
  773. List<String> args;
  774. args.push_back("--device");
  775. args.push_back("default");
  776. args.push_back("--filter");
  777. args.push_back("RTLightmap");
  778. args.push_back("--hdr");
  779. args.push_back(fname_light_in);
  780. args.push_back("--nrm");
  781. args.push_back(fname_norm_in);
  782. args.push_back("--output");
  783. args.push_back(fname_out);
  784. String str;
  785. int exitcode = 0;
  786. Error err = OS::get_singleton()->execute(p_exe, args, &str, &exitcode, true);
  787. da->remove(fname_light_in);
  788. if (err != OK || exitcode != 0) {
  789. da->remove(fname_out);
  790. print_verbose(str);
  791. ERR_FAIL_V_MSG(BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES, vformat("OIDN denoiser failed, return code: %d", exitcode));
  792. }
  793. Ref<Image> img = _read_pfm(fname_out);
  794. da->remove(fname_out);
  795. ERR_FAIL_COND_V(img.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  796. Vector<uint8_t> old_data = p_rd->texture_get_data(p_source_light_tex, index);
  797. Vector<uint8_t> new_data = img->get_data();
  798. img.unref(); // Avoid copy on write.
  799. uint32_t count = old_data.size() / 2;
  800. const uint16_t *src = (const uint16_t *)old_data.ptr();
  801. uint16_t *dst = (uint16_t *)new_data.ptrw();
  802. for (uint32_t k = 0; k < count; k += 4) {
  803. dst[k + 3] = src[k + 3];
  804. }
  805. p_rd->texture_update(p_dest_light_tex, index, new_data);
  806. }
  807. da->remove(fname_norm_in);
  808. }
  809. return BAKE_OK;
  810. }
  811. 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, int p_denoiser_range, const Size2i &p_atlas_size, int p_atlas_slices, bool p_bake_sh, BakeStepFunc p_step_function, void *p_bake_userdata) {
  812. RID denoise_params_buffer = p_rd->uniform_buffer_create(sizeof(DenoiseParams));
  813. DenoiseParams denoise_params;
  814. denoise_params.spatial_bandwidth = 5.0f;
  815. denoise_params.light_bandwidth = p_denoiser_strength;
  816. denoise_params.albedo_bandwidth = 1.0f;
  817. denoise_params.normal_bandwidth = 0.1f;
  818. denoise_params.filter_strength = 10.0f;
  819. denoise_params.half_search_window = p_denoiser_range;
  820. p_rd->buffer_update(denoise_params_buffer, 0, sizeof(DenoiseParams), &denoise_params);
  821. Vector<RD::Uniform> uniforms = dilate_or_denoise_common_uniforms(p_source_light_tex, p_dest_light_tex);
  822. {
  823. RD::Uniform u;
  824. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  825. u.binding = 2;
  826. u.append_id(p_source_normal_tex);
  827. uniforms.push_back(u);
  828. }
  829. {
  830. RD::Uniform u;
  831. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  832. u.binding = 3;
  833. u.append_id(denoise_params_buffer);
  834. uniforms.push_back(u);
  835. }
  836. RID compute_shader_denoise = p_rd->shader_create_from_spirv(p_compute_shader->get_spirv_stages("denoise"));
  837. ERR_FAIL_COND_V(compute_shader_denoise.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  838. RID compute_shader_denoise_pipeline = p_rd->compute_pipeline_create(compute_shader_denoise);
  839. RID denoise_uniform_set = p_rd->uniform_set_create(uniforms, compute_shader_denoise, 1);
  840. // We denoise in fixed size regions and synchronize execution to avoid GPU timeouts.
  841. // We use a region with 1/4 the amount of pixels if we're denoising SH lightmaps, as
  842. // all four of them are denoised in the shader in one dispatch.
  843. const int max_region_size = p_bake_sh ? 512 : 1024;
  844. int x_regions = Math::division_round_up(p_atlas_size.width, max_region_size);
  845. int y_regions = Math::division_round_up(p_atlas_size.height, max_region_size);
  846. for (int s = 0; s < p_atlas_slices; s++) {
  847. p_push_constant.atlas_slice = s;
  848. for (int i = 0; i < x_regions; i++) {
  849. for (int j = 0; j < y_regions; j++) {
  850. int x = i * max_region_size;
  851. int y = j * max_region_size;
  852. int w = MIN((i + 1) * max_region_size, p_atlas_size.width) - x;
  853. int h = MIN((j + 1) * max_region_size, p_atlas_size.height) - y;
  854. p_push_constant.region_ofs[0] = x;
  855. p_push_constant.region_ofs[1] = y;
  856. RD::ComputeListID compute_list = p_rd->compute_list_begin();
  857. p_rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_denoise_pipeline);
  858. p_rd->compute_list_bind_uniform_set(compute_list, p_compute_base_uniform_set, 0);
  859. p_rd->compute_list_bind_uniform_set(compute_list, denoise_uniform_set, 1);
  860. p_rd->compute_list_set_push_constant(compute_list, &p_push_constant, sizeof(PushConstant));
  861. p_rd->compute_list_dispatch(compute_list, Math::division_round_up(w, 8), Math::division_round_up(h, 8), 1);
  862. p_rd->compute_list_end();
  863. p_rd->submit();
  864. p_rd->sync();
  865. }
  866. }
  867. if (p_step_function) {
  868. int percent = (s + 1) * 100 / p_atlas_slices;
  869. float p = float(s) / p_atlas_slices * 0.1;
  870. if (p_step_function(0.8 + p, vformat(RTR("Denoising %d%%"), percent), p_bake_userdata, false)) {
  871. return BAKE_ERROR_USER_ABORTED;
  872. }
  873. }
  874. }
  875. p_rd->free(compute_shader_denoise);
  876. p_rd->free(denoise_params_buffer);
  877. return BAKE_OK;
  878. }
  879. LightmapperRD::BakeError LightmapperRD::bake(BakeQuality p_quality, bool p_use_denoiser, float p_denoiser_strength, int p_denoiser_range, int p_bounces, float p_bounce_indirect_energy, float p_bias, int p_max_texture_size, bool p_bake_sh, bool p_texture_for_bounces, GenerateProbes p_generate_probes, const Ref<Image> &p_environment_panorama, const Basis &p_environment_transform, BakeStepFunc p_step_function, void *p_bake_userdata, float p_exposure_normalization) {
  880. int denoiser = GLOBAL_GET("rendering/lightmapping/denoising/denoiser");
  881. String oidn_path = EDITOR_GET("filesystem/tools/oidn/oidn_denoise_path");
  882. if (p_use_denoiser && denoiser == 1) {
  883. // OIDN (external).
  884. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  885. if (da->dir_exists(oidn_path)) {
  886. if (OS::get_singleton()->get_name() == "Windows") {
  887. oidn_path = oidn_path.path_join("oidnDenoise.exe");
  888. } else {
  889. oidn_path = oidn_path.path_join("oidnDenoise");
  890. }
  891. }
  892. ERR_FAIL_COND_V_MSG(oidn_path.is_empty() || !da->file_exists(oidn_path), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES, "OIDN denoiser is selected in the project settings, but no or invalid OIDN executable path is configured in the editor settings.");
  893. }
  894. if (p_step_function) {
  895. p_step_function(0.0, RTR("Begin Bake"), p_bake_userdata, true);
  896. }
  897. bake_textures.clear();
  898. int grid_size = 128;
  899. /* STEP 1: Fetch material textures and compute the bounds */
  900. AABB bounds;
  901. Size2i atlas_size;
  902. int atlas_slices;
  903. Vector<Ref<Image>> albedo_images;
  904. Vector<Ref<Image>> emission_images;
  905. BakeError bake_error = _blit_meshes_into_atlas(p_max_texture_size, p_denoiser_range, albedo_images, emission_images, bounds, atlas_size, atlas_slices, p_step_function, p_bake_userdata);
  906. if (bake_error != BAKE_OK) {
  907. return bake_error;
  908. }
  909. #ifdef DEBUG_TEXTURES
  910. for (int i = 0; i < atlas_slices; i++) {
  911. albedo_images[i]->save_png("res://0_albedo_" + itos(i) + ".png");
  912. emission_images[i]->save_png("res://0_emission_" + itos(i) + ".png");
  913. }
  914. #endif
  915. // Attempt to create a local device by requesting it from rendering server first.
  916. // If that fails because the current renderer is not implemented on top of RD, we fall back to creating
  917. // a local rendering device manually depending on the current platform.
  918. Error err;
  919. RenderingContextDriver *rcd = nullptr;
  920. RenderingDevice *rd = RenderingServer::get_singleton()->create_local_rendering_device();
  921. if (rd == nullptr) {
  922. #if defined(RD_ENABLED)
  923. #if defined(METAL_ENABLED)
  924. rcd = memnew(RenderingContextDriverMetal);
  925. rd = memnew(RenderingDevice);
  926. #endif
  927. #if defined(VULKAN_ENABLED)
  928. if (rcd == nullptr) {
  929. rcd = memnew(RenderingContextDriverVulkan);
  930. rd = memnew(RenderingDevice);
  931. }
  932. #endif
  933. #endif
  934. if (rcd != nullptr && rd != nullptr) {
  935. err = rcd->initialize();
  936. if (err == OK) {
  937. err = rd->initialize(rcd);
  938. }
  939. if (err != OK) {
  940. memdelete(rd);
  941. memdelete(rcd);
  942. rd = nullptr;
  943. rcd = nullptr;
  944. }
  945. }
  946. }
  947. ERR_FAIL_NULL_V(rd, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  948. RID albedo_array_tex;
  949. RID emission_array_tex;
  950. RID normal_tex;
  951. RID position_tex;
  952. RID unocclude_tex;
  953. RID light_source_tex;
  954. RID light_dest_tex;
  955. RID light_accum_tex;
  956. RID light_accum_tex2;
  957. RID light_environment_tex;
  958. #define FREE_TEXTURES \
  959. rd->free(albedo_array_tex); \
  960. rd->free(emission_array_tex); \
  961. rd->free(normal_tex); \
  962. rd->free(position_tex); \
  963. rd->free(unocclude_tex); \
  964. rd->free(light_source_tex); \
  965. rd->free(light_accum_tex2); \
  966. rd->free(light_accum_tex); \
  967. rd->free(light_environment_tex);
  968. { // create all textures
  969. Vector<Vector<uint8_t>> albedo_data;
  970. Vector<Vector<uint8_t>> emission_data;
  971. for (int i = 0; i < atlas_slices; i++) {
  972. albedo_data.push_back(albedo_images[i]->get_data());
  973. emission_data.push_back(emission_images[i]->get_data());
  974. }
  975. RD::TextureFormat tf;
  976. tf.width = atlas_size.width;
  977. tf.height = atlas_size.height;
  978. tf.array_layers = atlas_slices;
  979. tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY;
  980. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  981. tf.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  982. albedo_array_tex = rd->texture_create(tf, RD::TextureView(), albedo_data);
  983. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  984. emission_array_tex = rd->texture_create(tf, RD::TextureView(), emission_data);
  985. //this will be rastered to
  986. 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;
  987. normal_tex = rd->texture_create(tf, RD::TextureView());
  988. tf.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  989. position_tex = rd->texture_create(tf, RD::TextureView());
  990. unocclude_tex = rd->texture_create(tf, RD::TextureView());
  991. tf.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  992. 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;
  993. light_source_tex = rd->texture_create(tf, RD::TextureView());
  994. rd->texture_clear(light_source_tex, Color(0, 0, 0, 0), 0, 1, 0, atlas_slices);
  995. if (p_bake_sh) {
  996. tf.array_layers *= 4;
  997. }
  998. light_accum_tex = rd->texture_create(tf, RD::TextureView());
  999. rd->texture_clear(light_accum_tex, Color(0, 0, 0, 0), 0, 1, 0, tf.array_layers);
  1000. light_dest_tex = rd->texture_create(tf, RD::TextureView());
  1001. rd->texture_clear(light_dest_tex, Color(0, 0, 0, 0), 0, 1, 0, tf.array_layers);
  1002. light_accum_tex2 = light_dest_tex;
  1003. //env
  1004. {
  1005. Ref<Image> panorama_tex;
  1006. if (p_environment_panorama.is_valid()) {
  1007. panorama_tex = p_environment_panorama;
  1008. panorama_tex->convert(Image::FORMAT_RGBAF);
  1009. } else {
  1010. panorama_tex.instantiate();
  1011. panorama_tex->initialize_data(8, 8, false, Image::FORMAT_RGBAF);
  1012. panorama_tex->fill(Color(0, 0, 0, 1));
  1013. }
  1014. RD::TextureFormat tfp;
  1015. tfp.width = panorama_tex->get_width();
  1016. tfp.height = panorama_tex->get_height();
  1017. tfp.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  1018. tfp.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  1019. Vector<Vector<uint8_t>> tdata;
  1020. tdata.push_back(panorama_tex->get_data());
  1021. light_environment_tex = rd->texture_create(tfp, RD::TextureView(), tdata);
  1022. #ifdef DEBUG_TEXTURES
  1023. panorama_tex->save_exr("res://0_panorama.exr", false);
  1024. #endif
  1025. }
  1026. }
  1027. /* STEP 2: create the acceleration structure for the GPU*/
  1028. Vector<int> slice_triangle_count;
  1029. RID bake_parameters_buffer;
  1030. RID vertex_buffer;
  1031. RID triangle_buffer;
  1032. RID lights_buffer;
  1033. RID triangle_indices_buffer;
  1034. RID cluster_indices_buffer;
  1035. RID cluster_aabbs_buffer;
  1036. RID grid_texture;
  1037. RID seams_buffer;
  1038. RID probe_positions_buffer;
  1039. Vector<int> slice_seam_count;
  1040. #define FREE_BUFFERS \
  1041. rd->free(bake_parameters_buffer); \
  1042. rd->free(vertex_buffer); \
  1043. rd->free(triangle_buffer); \
  1044. rd->free(lights_buffer); \
  1045. rd->free(triangle_indices_buffer); \
  1046. rd->free(cluster_indices_buffer); \
  1047. rd->free(cluster_aabbs_buffer); \
  1048. rd->free(grid_texture); \
  1049. rd->free(seams_buffer); \
  1050. rd->free(probe_positions_buffer);
  1051. const uint32_t cluster_size = 16;
  1052. _create_acceleration_structures(rd, atlas_size, atlas_slices, bounds, grid_size, cluster_size, probe_positions, p_generate_probes, slice_triangle_count, slice_seam_count, vertex_buffer, triangle_buffer, lights_buffer, triangle_indices_buffer, cluster_indices_buffer, cluster_aabbs_buffer, probe_positions_buffer, grid_texture, seams_buffer, p_step_function, p_bake_userdata);
  1053. // Create global bake parameters buffer.
  1054. BakeParameters bake_parameters;
  1055. bake_parameters.world_size[0] = bounds.size.x;
  1056. bake_parameters.world_size[1] = bounds.size.y;
  1057. bake_parameters.world_size[2] = bounds.size.z;
  1058. bake_parameters.bias = p_bias;
  1059. bake_parameters.to_cell_offset[0] = bounds.position.x;
  1060. bake_parameters.to_cell_offset[1] = bounds.position.y;
  1061. bake_parameters.to_cell_offset[2] = bounds.position.z;
  1062. bake_parameters.grid_size = grid_size;
  1063. bake_parameters.to_cell_size[0] = (1.0 / bounds.size.x) * float(grid_size);
  1064. bake_parameters.to_cell_size[1] = (1.0 / bounds.size.y) * float(grid_size);
  1065. bake_parameters.to_cell_size[2] = (1.0 / bounds.size.z) * float(grid_size);
  1066. bake_parameters.light_count = lights.size();
  1067. bake_parameters.env_transform[0] = p_environment_transform.rows[0][0];
  1068. bake_parameters.env_transform[1] = p_environment_transform.rows[1][0];
  1069. bake_parameters.env_transform[2] = p_environment_transform.rows[2][0];
  1070. bake_parameters.env_transform[3] = 0.0f;
  1071. bake_parameters.env_transform[4] = p_environment_transform.rows[0][1];
  1072. bake_parameters.env_transform[5] = p_environment_transform.rows[1][1];
  1073. bake_parameters.env_transform[6] = p_environment_transform.rows[2][1];
  1074. bake_parameters.env_transform[7] = 0.0f;
  1075. bake_parameters.env_transform[8] = p_environment_transform.rows[0][2];
  1076. bake_parameters.env_transform[9] = p_environment_transform.rows[1][2];
  1077. bake_parameters.env_transform[10] = p_environment_transform.rows[2][2];
  1078. bake_parameters.env_transform[11] = 0.0f;
  1079. bake_parameters.atlas_size[0] = atlas_size.width;
  1080. bake_parameters.atlas_size[1] = atlas_size.height;
  1081. bake_parameters.exposure_normalization = p_exposure_normalization;
  1082. bake_parameters.bounces = p_bounces;
  1083. bake_parameters.bounce_indirect_energy = p_bounce_indirect_energy;
  1084. bake_parameters_buffer = rd->uniform_buffer_create(sizeof(BakeParameters));
  1085. rd->buffer_update(bake_parameters_buffer, 0, sizeof(BakeParameters), &bake_parameters);
  1086. if (p_step_function) {
  1087. if (p_step_function(0.47, RTR("Preparing shaders"), p_bake_userdata, true)) {
  1088. FREE_TEXTURES
  1089. FREE_BUFFERS
  1090. memdelete(rd);
  1091. if (rcd != nullptr) {
  1092. memdelete(rcd);
  1093. }
  1094. return BAKE_ERROR_USER_ABORTED;
  1095. }
  1096. }
  1097. //shaders
  1098. Ref<RDShaderFile> raster_shader;
  1099. raster_shader.instantiate();
  1100. err = raster_shader->parse_versions_from_text(lm_raster_shader_glsl);
  1101. if (err != OK) {
  1102. raster_shader->print_errors("raster_shader");
  1103. FREE_TEXTURES
  1104. FREE_BUFFERS
  1105. memdelete(rd);
  1106. if (rcd != nullptr) {
  1107. memdelete(rcd);
  1108. }
  1109. }
  1110. ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  1111. RID rasterize_shader = rd->shader_create_from_spirv(raster_shader->get_spirv_stages());
  1112. ERR_FAIL_COND_V(rasterize_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //this is a bug check, though, should not happen
  1113. RID sampler;
  1114. {
  1115. RD::SamplerState s;
  1116. s.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1117. s.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1118. s.max_lod = 0;
  1119. sampler = rd->sampler_create(s);
  1120. }
  1121. Vector<RD::Uniform> base_uniforms;
  1122. {
  1123. {
  1124. RD::Uniform u;
  1125. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1126. u.binding = 0;
  1127. u.append_id(bake_parameters_buffer);
  1128. base_uniforms.push_back(u);
  1129. }
  1130. {
  1131. RD::Uniform u;
  1132. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1133. u.binding = 1;
  1134. u.append_id(vertex_buffer);
  1135. base_uniforms.push_back(u);
  1136. }
  1137. {
  1138. RD::Uniform u;
  1139. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1140. u.binding = 2;
  1141. u.append_id(triangle_buffer);
  1142. base_uniforms.push_back(u);
  1143. }
  1144. {
  1145. RD::Uniform u;
  1146. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1147. u.binding = 3;
  1148. u.append_id(triangle_indices_buffer);
  1149. base_uniforms.push_back(u);
  1150. }
  1151. {
  1152. RD::Uniform u;
  1153. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1154. u.binding = 4;
  1155. u.append_id(lights_buffer);
  1156. base_uniforms.push_back(u);
  1157. }
  1158. {
  1159. RD::Uniform u;
  1160. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1161. u.binding = 5;
  1162. u.append_id(seams_buffer);
  1163. base_uniforms.push_back(u);
  1164. }
  1165. {
  1166. RD::Uniform u;
  1167. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1168. u.binding = 6;
  1169. u.append_id(probe_positions_buffer);
  1170. base_uniforms.push_back(u);
  1171. }
  1172. {
  1173. RD::Uniform u;
  1174. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1175. u.binding = 7;
  1176. u.append_id(grid_texture);
  1177. base_uniforms.push_back(u);
  1178. }
  1179. {
  1180. RD::Uniform u;
  1181. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1182. u.binding = 8;
  1183. u.append_id(albedo_array_tex);
  1184. base_uniforms.push_back(u);
  1185. }
  1186. {
  1187. RD::Uniform u;
  1188. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1189. u.binding = 9;
  1190. u.append_id(emission_array_tex);
  1191. base_uniforms.push_back(u);
  1192. }
  1193. {
  1194. RD::Uniform u;
  1195. u.uniform_type = RD::UNIFORM_TYPE_SAMPLER;
  1196. u.binding = 10;
  1197. u.append_id(sampler);
  1198. base_uniforms.push_back(u);
  1199. }
  1200. {
  1201. RD::Uniform u;
  1202. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1203. u.binding = 11;
  1204. u.append_id(cluster_indices_buffer);
  1205. base_uniforms.push_back(u);
  1206. }
  1207. {
  1208. RD::Uniform u;
  1209. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1210. u.binding = 12;
  1211. u.append_id(cluster_aabbs_buffer);
  1212. base_uniforms.push_back(u);
  1213. }
  1214. }
  1215. RID raster_base_uniform = rd->uniform_set_create(base_uniforms, rasterize_shader, 0);
  1216. RID raster_depth_buffer;
  1217. {
  1218. RD::TextureFormat tf;
  1219. tf.width = atlas_size.width;
  1220. tf.height = atlas_size.height;
  1221. tf.depth = 1;
  1222. tf.texture_type = RD::TEXTURE_TYPE_2D;
  1223. tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  1224. tf.format = RD::DATA_FORMAT_D32_SFLOAT;
  1225. tf.is_discardable = true;
  1226. raster_depth_buffer = rd->texture_create(tf, RD::TextureView());
  1227. }
  1228. rd->submit();
  1229. rd->sync();
  1230. /* STEP 3: Raster the geometry to UV2 coords in the atlas textures GPU*/
  1231. _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);
  1232. #ifdef DEBUG_TEXTURES
  1233. for (int i = 0; i < atlas_slices; i++) {
  1234. Vector<uint8_t> s = rd->texture_get_data(position_tex, i);
  1235. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAF, s);
  1236. img->save_exr("res://1_position_" + itos(i) + ".exr", false);
  1237. s = rd->texture_get_data(normal_tex, i);
  1238. img->set_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1239. img->save_exr("res://1_normal_" + itos(i) + ".exr", false);
  1240. }
  1241. #endif
  1242. #define FREE_RASTER_RESOURCES \
  1243. rd->free(rasterize_shader); \
  1244. rd->free(sampler); \
  1245. rd->free(raster_depth_buffer);
  1246. /* Plot direct light */
  1247. Ref<RDShaderFile> compute_shader;
  1248. String defines = "";
  1249. defines += "\n#define CLUSTER_SIZE " + uitos(cluster_size) + "\n";
  1250. if (p_bake_sh) {
  1251. defines += "\n#define USE_SH_LIGHTMAPS\n";
  1252. }
  1253. if (p_texture_for_bounces) {
  1254. defines += "\n#define USE_LIGHT_TEXTURE_FOR_BOUNCES\n";
  1255. }
  1256. compute_shader.instantiate();
  1257. err = compute_shader->parse_versions_from_text(lm_compute_shader_glsl, defines);
  1258. if (err != OK) {
  1259. FREE_TEXTURES
  1260. FREE_BUFFERS
  1261. FREE_RASTER_RESOURCES
  1262. memdelete(rd);
  1263. if (rcd != nullptr) {
  1264. memdelete(rcd);
  1265. }
  1266. compute_shader->print_errors("compute_shader");
  1267. }
  1268. ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  1269. // Unoccluder
  1270. RID compute_shader_unocclude = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("unocclude"));
  1271. ERR_FAIL_COND_V(compute_shader_unocclude.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); // internal check, should not happen
  1272. RID compute_shader_unocclude_pipeline = rd->compute_pipeline_create(compute_shader_unocclude);
  1273. // Direct light
  1274. RID compute_shader_primary = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("primary"));
  1275. ERR_FAIL_COND_V(compute_shader_primary.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); // internal check, should not happen
  1276. RID compute_shader_primary_pipeline = rd->compute_pipeline_create(compute_shader_primary);
  1277. // Indirect light
  1278. RID compute_shader_secondary = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("secondary"));
  1279. ERR_FAIL_COND_V(compute_shader_secondary.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen
  1280. RID compute_shader_secondary_pipeline = rd->compute_pipeline_create(compute_shader_secondary);
  1281. // Light probes
  1282. RID compute_shader_light_probes = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("light_probes"));
  1283. ERR_FAIL_COND_V(compute_shader_light_probes.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen
  1284. RID compute_shader_light_probes_pipeline = rd->compute_pipeline_create(compute_shader_light_probes);
  1285. RID compute_base_uniform_set = rd->uniform_set_create(base_uniforms, compute_shader_primary, 0);
  1286. #define FREE_COMPUTE_RESOURCES \
  1287. rd->free(compute_shader_unocclude); \
  1288. rd->free(compute_shader_primary); \
  1289. rd->free(compute_shader_secondary); \
  1290. rd->free(compute_shader_light_probes);
  1291. Vector3i group_size(Math::division_round_up(atlas_size.x, 8), Math::division_round_up(atlas_size.y, 8), 1);
  1292. rd->submit();
  1293. rd->sync();
  1294. if (p_step_function) {
  1295. if (p_step_function(0.49, RTR("Un-occluding geometry"), p_bake_userdata, true)) {
  1296. FREE_TEXTURES
  1297. FREE_BUFFERS
  1298. FREE_RASTER_RESOURCES
  1299. FREE_COMPUTE_RESOURCES
  1300. memdelete(rd);
  1301. if (rcd != nullptr) {
  1302. memdelete(rcd);
  1303. }
  1304. return BAKE_ERROR_USER_ABORTED;
  1305. }
  1306. }
  1307. PushConstant push_constant;
  1308. /* UNOCCLUDE */
  1309. {
  1310. Vector<RD::Uniform> uniforms;
  1311. {
  1312. {
  1313. RD::Uniform u;
  1314. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1315. u.binding = 0;
  1316. u.append_id(position_tex);
  1317. uniforms.push_back(u);
  1318. }
  1319. {
  1320. RD::Uniform u;
  1321. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1322. u.binding = 1;
  1323. u.append_id(unocclude_tex); //will be unused
  1324. uniforms.push_back(u);
  1325. }
  1326. }
  1327. RID unocclude_uniform_set = rd->uniform_set_create(uniforms, compute_shader_unocclude, 1);
  1328. RD::ComputeListID compute_list = rd->compute_list_begin();
  1329. rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_unocclude_pipeline);
  1330. rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
  1331. rd->compute_list_bind_uniform_set(compute_list, unocclude_uniform_set, 1);
  1332. for (int i = 0; i < atlas_slices; i++) {
  1333. push_constant.atlas_slice = i;
  1334. rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  1335. rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
  1336. //no barrier, let them run all together
  1337. }
  1338. rd->compute_list_end(); //done
  1339. }
  1340. if (p_step_function) {
  1341. if (p_step_function(0.5, RTR("Plot direct lighting"), p_bake_userdata, true)) {
  1342. FREE_TEXTURES
  1343. FREE_BUFFERS
  1344. FREE_RASTER_RESOURCES
  1345. FREE_COMPUTE_RESOURCES
  1346. memdelete(rd);
  1347. if (rcd != nullptr) {
  1348. memdelete(rcd);
  1349. }
  1350. return BAKE_ERROR_USER_ABORTED;
  1351. }
  1352. }
  1353. // Set ray count to the quality used for direct light and bounces.
  1354. switch (p_quality) {
  1355. case BAKE_QUALITY_LOW: {
  1356. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/low_quality_ray_count");
  1357. } break;
  1358. case BAKE_QUALITY_MEDIUM: {
  1359. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/medium_quality_ray_count");
  1360. } break;
  1361. case BAKE_QUALITY_HIGH: {
  1362. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/high_quality_ray_count");
  1363. } break;
  1364. case BAKE_QUALITY_ULTRA: {
  1365. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/ultra_quality_ray_count");
  1366. } break;
  1367. }
  1368. push_constant.ray_count = CLAMP(push_constant.ray_count, 16u, 8192u);
  1369. /* PRIMARY (direct) LIGHT PASS */
  1370. {
  1371. Vector<RD::Uniform> uniforms;
  1372. {
  1373. {
  1374. RD::Uniform u;
  1375. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1376. u.binding = 0;
  1377. u.append_id(light_source_tex);
  1378. uniforms.push_back(u);
  1379. }
  1380. {
  1381. RD::Uniform u;
  1382. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1383. u.binding = 1;
  1384. u.append_id(light_dest_tex); //will be unused
  1385. uniforms.push_back(u);
  1386. }
  1387. {
  1388. RD::Uniform u;
  1389. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1390. u.binding = 2;
  1391. u.append_id(position_tex);
  1392. uniforms.push_back(u);
  1393. }
  1394. {
  1395. RD::Uniform u;
  1396. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1397. u.binding = 3;
  1398. u.append_id(normal_tex);
  1399. uniforms.push_back(u);
  1400. }
  1401. {
  1402. RD::Uniform u;
  1403. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1404. u.binding = 4;
  1405. u.append_id(light_accum_tex);
  1406. uniforms.push_back(u);
  1407. }
  1408. }
  1409. RID light_uniform_set = rd->uniform_set_create(uniforms, compute_shader_primary, 1);
  1410. RD::ComputeListID compute_list = rd->compute_list_begin();
  1411. rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_primary_pipeline);
  1412. rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
  1413. rd->compute_list_bind_uniform_set(compute_list, light_uniform_set, 1);
  1414. for (int i = 0; i < atlas_slices; i++) {
  1415. push_constant.atlas_slice = i;
  1416. rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  1417. rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
  1418. //no barrier, let them run all together
  1419. }
  1420. rd->compute_list_end(); //done
  1421. }
  1422. #ifdef DEBUG_TEXTURES
  1423. for (int i = 0; i < atlas_slices; i++) {
  1424. Vector<uint8_t> s = rd->texture_get_data(light_source_tex, i);
  1425. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1426. img->save_exr("res://2_light_primary_" + itos(i) + ".exr", false);
  1427. }
  1428. if (p_bake_sh) {
  1429. for (int i = 0; i < atlas_slices * 4; i++) {
  1430. Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
  1431. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1432. img->save_exr("res://2_light_primary_accum_" + itos(i) + ".exr", false);
  1433. }
  1434. }
  1435. #endif
  1436. /* SECONDARY (indirect) LIGHT PASS(ES) */
  1437. if (p_bounces > 0) {
  1438. Vector<RD::Uniform> uniforms;
  1439. {
  1440. {
  1441. // Unused.
  1442. RD::Uniform u;
  1443. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1444. u.binding = 0;
  1445. u.append_id(light_dest_tex);
  1446. uniforms.push_back(u);
  1447. }
  1448. {
  1449. RD::Uniform u;
  1450. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1451. u.binding = 1;
  1452. u.append_id(light_source_tex);
  1453. uniforms.push_back(u);
  1454. }
  1455. {
  1456. RD::Uniform u;
  1457. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1458. u.binding = 2;
  1459. u.append_id(position_tex);
  1460. uniforms.push_back(u);
  1461. }
  1462. {
  1463. RD::Uniform u;
  1464. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1465. u.binding = 3;
  1466. u.append_id(normal_tex);
  1467. uniforms.push_back(u);
  1468. }
  1469. {
  1470. RD::Uniform u;
  1471. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1472. u.binding = 4;
  1473. u.append_id(light_accum_tex);
  1474. uniforms.push_back(u);
  1475. }
  1476. {
  1477. RD::Uniform u;
  1478. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1479. u.binding = 5;
  1480. u.append_id(light_environment_tex);
  1481. uniforms.push_back(u);
  1482. }
  1483. }
  1484. RID secondary_uniform_set;
  1485. secondary_uniform_set = rd->uniform_set_create(uniforms, compute_shader_secondary, 1);
  1486. int max_region_size = nearest_power_of_2_templated(int(GLOBAL_GET("rendering/lightmapping/bake_performance/region_size")));
  1487. int max_rays = GLOBAL_GET("rendering/lightmapping/bake_performance/max_rays_per_pass");
  1488. int x_regions = Math::division_round_up(atlas_size.width, max_region_size);
  1489. int y_regions = Math::division_round_up(atlas_size.height, max_region_size);
  1490. int ray_iterations = Math::division_round_up((int32_t)push_constant.ray_count, max_rays);
  1491. rd->submit();
  1492. rd->sync();
  1493. if (p_step_function) {
  1494. if (p_step_function(0.6, RTR("Integrate indirect lighting"), p_bake_userdata, true)) {
  1495. FREE_TEXTURES
  1496. FREE_BUFFERS
  1497. FREE_RASTER_RESOURCES
  1498. FREE_COMPUTE_RESOURCES
  1499. memdelete(rd);
  1500. if (rcd != nullptr) {
  1501. memdelete(rcd);
  1502. }
  1503. return BAKE_ERROR_USER_ABORTED;
  1504. }
  1505. }
  1506. int count = 0;
  1507. for (int s = 0; s < atlas_slices; s++) {
  1508. push_constant.atlas_slice = s;
  1509. for (int i = 0; i < x_regions; i++) {
  1510. for (int j = 0; j < y_regions; j++) {
  1511. int x = i * max_region_size;
  1512. int y = j * max_region_size;
  1513. int w = MIN((i + 1) * max_region_size, atlas_size.width) - x;
  1514. int h = MIN((j + 1) * max_region_size, atlas_size.height) - y;
  1515. push_constant.region_ofs[0] = x;
  1516. push_constant.region_ofs[1] = y;
  1517. group_size = Vector3i(Math::division_round_up(w, 8), Math::division_round_up(h, 8), 1);
  1518. for (int k = 0; k < ray_iterations; k++) {
  1519. RD::ComputeListID compute_list = rd->compute_list_begin();
  1520. rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_secondary_pipeline);
  1521. rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
  1522. rd->compute_list_bind_uniform_set(compute_list, secondary_uniform_set, 1);
  1523. push_constant.ray_from = k * max_rays;
  1524. push_constant.ray_to = MIN((k + 1) * max_rays, int32_t(push_constant.ray_count));
  1525. rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  1526. rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
  1527. rd->compute_list_end();
  1528. rd->submit();
  1529. rd->sync();
  1530. count++;
  1531. if (p_step_function) {
  1532. int total = (atlas_slices * x_regions * y_regions * ray_iterations);
  1533. int percent = count * 100 / total;
  1534. float p = float(count) / total * 0.1;
  1535. if (p_step_function(0.6 + p, vformat(RTR("Integrate indirect lighting %d%%"), percent), p_bake_userdata, false)) {
  1536. FREE_TEXTURES
  1537. FREE_BUFFERS
  1538. FREE_RASTER_RESOURCES
  1539. FREE_COMPUTE_RESOURCES
  1540. memdelete(rd);
  1541. if (rcd != nullptr) {
  1542. memdelete(rcd);
  1543. }
  1544. return BAKE_ERROR_USER_ABORTED;
  1545. }
  1546. }
  1547. }
  1548. }
  1549. }
  1550. }
  1551. }
  1552. /* LIGHTPROBES */
  1553. RID light_probe_buffer;
  1554. if (probe_positions.size()) {
  1555. light_probe_buffer = rd->storage_buffer_create(sizeof(float) * 4 * 9 * probe_positions.size());
  1556. if (p_step_function) {
  1557. if (p_step_function(0.7, RTR("Baking light probes"), p_bake_userdata, true)) {
  1558. FREE_TEXTURES
  1559. FREE_BUFFERS
  1560. FREE_RASTER_RESOURCES
  1561. FREE_COMPUTE_RESOURCES
  1562. if (probe_positions.size() > 0) {
  1563. rd->free(light_probe_buffer);
  1564. }
  1565. memdelete(rd);
  1566. if (rcd != nullptr) {
  1567. memdelete(rcd);
  1568. }
  1569. return BAKE_ERROR_USER_ABORTED;
  1570. }
  1571. }
  1572. Vector<RD::Uniform> uniforms;
  1573. {
  1574. {
  1575. RD::Uniform u;
  1576. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1577. u.binding = 0;
  1578. u.append_id(light_probe_buffer);
  1579. uniforms.push_back(u);
  1580. }
  1581. {
  1582. RD::Uniform u;
  1583. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1584. u.binding = 1;
  1585. u.append_id(light_source_tex);
  1586. uniforms.push_back(u);
  1587. }
  1588. {
  1589. RD::Uniform u;
  1590. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1591. u.binding = 2;
  1592. u.append_id(light_environment_tex);
  1593. uniforms.push_back(u);
  1594. }
  1595. }
  1596. RID light_probe_uniform_set = rd->uniform_set_create(uniforms, compute_shader_light_probes, 1);
  1597. switch (p_quality) {
  1598. case BAKE_QUALITY_LOW: {
  1599. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/low_quality_probe_ray_count");
  1600. } break;
  1601. case BAKE_QUALITY_MEDIUM: {
  1602. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/medium_quality_probe_ray_count");
  1603. } break;
  1604. case BAKE_QUALITY_HIGH: {
  1605. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/high_quality_probe_ray_count");
  1606. } break;
  1607. case BAKE_QUALITY_ULTRA: {
  1608. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/ultra_quality_probe_ray_count");
  1609. } break;
  1610. }
  1611. push_constant.ray_count = CLAMP(push_constant.ray_count, 16u, 8192u);
  1612. push_constant.probe_count = probe_positions.size();
  1613. int max_rays = GLOBAL_GET("rendering/lightmapping/bake_performance/max_rays_per_probe_pass");
  1614. int ray_iterations = Math::division_round_up((int32_t)push_constant.ray_count, max_rays);
  1615. for (int i = 0; i < ray_iterations; i++) {
  1616. RD::ComputeListID compute_list = rd->compute_list_begin();
  1617. rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_light_probes_pipeline);
  1618. rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
  1619. rd->compute_list_bind_uniform_set(compute_list, light_probe_uniform_set, 1);
  1620. push_constant.ray_from = i * max_rays;
  1621. push_constant.ray_to = MIN((i + 1) * max_rays, int32_t(push_constant.ray_count));
  1622. rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  1623. rd->compute_list_dispatch(compute_list, Math::division_round_up((int)probe_positions.size(), 64), 1, 1);
  1624. rd->compute_list_end(); //done
  1625. rd->submit();
  1626. rd->sync();
  1627. if (p_step_function) {
  1628. int percent = i * 100 / ray_iterations;
  1629. float p = float(i) / ray_iterations * 0.1;
  1630. if (p_step_function(0.7 + p, vformat(RTR("Integrating light probes %d%%"), percent), p_bake_userdata, false)) {
  1631. FREE_TEXTURES
  1632. FREE_BUFFERS
  1633. FREE_RASTER_RESOURCES
  1634. FREE_COMPUTE_RESOURCES
  1635. if (probe_positions.size() > 0) {
  1636. rd->free(light_probe_buffer);
  1637. }
  1638. memdelete(rd);
  1639. if (rcd != nullptr) {
  1640. memdelete(rcd);
  1641. }
  1642. return BAKE_ERROR_USER_ABORTED;
  1643. }
  1644. }
  1645. }
  1646. }
  1647. #if 0
  1648. for (int i = 0; i < probe_positions.size(); i++) {
  1649. Ref<Image> img = Image::create_empty(6, 4, false, Image::FORMAT_RGB8);
  1650. for (int j = 0; j < 6; j++) {
  1651. Vector<uint8_t> s = rd->texture_get_data(lightprobe_tex, i * 6 + j);
  1652. Ref<Image> img2 = Image::create_from_data(2, 2, false, Image::FORMAT_RGBAF, s);
  1653. img2->convert(Image::FORMAT_RGB8);
  1654. img->blit_rect(img2, Rect2i(0, 0, 2, 2), Point2i((j % 3) * 2, (j / 3) * 2));
  1655. }
  1656. img->save_png("res://3_light_probe_" + itos(i) + ".png");
  1657. }
  1658. #endif
  1659. /* DENOISE */
  1660. if (p_use_denoiser) {
  1661. if (p_step_function) {
  1662. if (p_step_function(0.8, RTR("Denoising"), p_bake_userdata, true)) {
  1663. FREE_TEXTURES
  1664. FREE_BUFFERS
  1665. FREE_RASTER_RESOURCES
  1666. FREE_COMPUTE_RESOURCES
  1667. if (probe_positions.size() > 0) {
  1668. rd->free(light_probe_buffer);
  1669. }
  1670. memdelete(rd);
  1671. if (rcd != nullptr) {
  1672. memdelete(rcd);
  1673. }
  1674. return BAKE_ERROR_USER_ABORTED;
  1675. }
  1676. }
  1677. {
  1678. BakeError error;
  1679. if (denoiser == 1) {
  1680. // OIDN (external).
  1681. error = _denoise_oidn(rd, light_accum_tex, normal_tex, light_accum_tex, atlas_size, atlas_slices, p_bake_sh, oidn_path);
  1682. } else {
  1683. // JNLM (built-in).
  1684. SWAP(light_accum_tex, light_accum_tex2);
  1685. error = _denoise(rd, compute_shader, compute_base_uniform_set, push_constant, light_accum_tex2, normal_tex, light_accum_tex, p_denoiser_strength, p_denoiser_range, atlas_size, atlas_slices, p_bake_sh, p_step_function, p_bake_userdata);
  1686. }
  1687. if (unlikely(error != BAKE_OK)) {
  1688. return error;
  1689. }
  1690. }
  1691. }
  1692. {
  1693. SWAP(light_accum_tex, light_accum_tex2);
  1694. 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));
  1695. if (unlikely(error != BAKE_OK)) {
  1696. return error;
  1697. }
  1698. }
  1699. #ifdef DEBUG_TEXTURES
  1700. for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
  1701. Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
  1702. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1703. img->save_exr("res://4_light_secondary_" + itos(i) + ".exr", false);
  1704. }
  1705. #endif
  1706. /* BLEND SEAMS */
  1707. //shaders
  1708. Ref<RDShaderFile> blendseams_shader;
  1709. blendseams_shader.instantiate();
  1710. err = blendseams_shader->parse_versions_from_text(lm_blendseams_shader_glsl);
  1711. if (err != OK) {
  1712. FREE_TEXTURES
  1713. FREE_BUFFERS
  1714. FREE_RASTER_RESOURCES
  1715. FREE_COMPUTE_RESOURCES
  1716. memdelete(rd);
  1717. if (rcd != nullptr) {
  1718. memdelete(rcd);
  1719. }
  1720. blendseams_shader->print_errors("blendseams_shader");
  1721. }
  1722. ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  1723. RID blendseams_line_raster_shader = rd->shader_create_from_spirv(blendseams_shader->get_spirv_stages("lines"));
  1724. ERR_FAIL_COND_V(blendseams_line_raster_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  1725. RID blendseams_triangle_raster_shader = rd->shader_create_from_spirv(blendseams_shader->get_spirv_stages("triangles"));
  1726. ERR_FAIL_COND_V(blendseams_triangle_raster_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  1727. #define FREE_BLENDSEAMS_RESOURCES \
  1728. rd->free(blendseams_line_raster_shader); \
  1729. rd->free(blendseams_triangle_raster_shader);
  1730. {
  1731. //pre copy
  1732. for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
  1733. rd->texture_copy(light_accum_tex, light_accum_tex2, Vector3(), Vector3(), Vector3(atlas_size.width, atlas_size.height, 1), 0, 0, i, i);
  1734. }
  1735. Vector<RID> framebuffers;
  1736. for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
  1737. RID slice_tex = rd->texture_create_shared_from_slice(RD::TextureView(), light_accum_tex, i, 0);
  1738. Vector<RID> fb;
  1739. fb.push_back(slice_tex);
  1740. fb.push_back(raster_depth_buffer);
  1741. framebuffers.push_back(rd->framebuffer_create(fb));
  1742. }
  1743. Vector<RD::Uniform> uniforms;
  1744. {
  1745. {
  1746. RD::Uniform u;
  1747. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1748. u.binding = 0;
  1749. u.append_id(light_accum_tex2);
  1750. uniforms.push_back(u);
  1751. }
  1752. }
  1753. RID blendseams_raster_uniform = rd->uniform_set_create(uniforms, blendseams_line_raster_shader, 1);
  1754. bool debug = false;
  1755. RD::PipelineColorBlendState bs = RD::PipelineColorBlendState::create_blend(1);
  1756. bs.attachments.write[0].src_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;
  1757. bs.attachments.write[0].dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1758. RD::PipelineDepthStencilState ds;
  1759. ds.enable_depth_test = true;
  1760. ds.enable_depth_write = true;
  1761. ds.depth_compare_operator = RD::COMPARE_OP_LESS; //so it does not render same pixel twice, this avoids wrong blending
  1762. 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);
  1763. 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);
  1764. uint32_t seam_offset = 0;
  1765. uint32_t triangle_offset = 0;
  1766. for (int i = 0; i < atlas_slices; i++) {
  1767. int subslices = (p_bake_sh ? 4 : 1);
  1768. if (slice_seam_count[i] == 0) {
  1769. continue;
  1770. }
  1771. for (int k = 0; k < subslices; k++) {
  1772. RasterSeamsPushConstant seams_push_constant;
  1773. seams_push_constant.slice = uint32_t(i * subslices + k);
  1774. seams_push_constant.debug = debug;
  1775. // Store the current subslice in the breadcrumb.
  1776. RD::DrawListID draw_list = rd->draw_list_begin(framebuffers[i * subslices + k], RD::DRAW_CLEAR_DEPTH, Vector<Color>(), 1.0f, 0, Rect2(), RDD::BreadcrumbMarker::LIGHTMAPPER_PASS | seams_push_constant.slice);
  1777. rd->draw_list_bind_uniform_set(draw_list, raster_base_uniform, 0);
  1778. rd->draw_list_bind_uniform_set(draw_list, blendseams_raster_uniform, 1);
  1779. const int uv_offset_count = 9;
  1780. static const Vector3 uv_offsets[uv_offset_count] = {
  1781. Vector3(0, 0, 0.5), //using zbuffer, so go inwards-outwards
  1782. Vector3(0, 1, 0.2),
  1783. Vector3(0, -1, 0.2),
  1784. Vector3(1, 0, 0.2),
  1785. Vector3(-1, 0, 0.2),
  1786. Vector3(-1, -1, 0.1),
  1787. Vector3(1, -1, 0.1),
  1788. Vector3(1, 1, 0.1),
  1789. Vector3(-1, 1, 0.1),
  1790. };
  1791. /* step 1 use lines to blend the edges */
  1792. {
  1793. seams_push_constant.base_index = seam_offset;
  1794. rd->draw_list_bind_render_pipeline(draw_list, blendseams_line_raster_pipeline);
  1795. seams_push_constant.uv_offset[0] = (uv_offsets[0].x - 0.5f) / float(atlas_size.width);
  1796. seams_push_constant.uv_offset[1] = (uv_offsets[0].y - 0.5f) / float(atlas_size.height);
  1797. seams_push_constant.blend = uv_offsets[0].z;
  1798. rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant));
  1799. rd->draw_list_draw(draw_list, false, 1, slice_seam_count[i] * 4);
  1800. }
  1801. /* step 2 use triangles to mask the interior */
  1802. {
  1803. seams_push_constant.base_index = triangle_offset;
  1804. rd->draw_list_bind_render_pipeline(draw_list, blendseams_triangle_raster_pipeline);
  1805. seams_push_constant.blend = 0; //do not draw them, just fill the z-buffer so its used as a mask
  1806. rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant));
  1807. rd->draw_list_draw(draw_list, false, 1, slice_triangle_count[i] * 3);
  1808. }
  1809. /* step 3 blend around the triangle */
  1810. rd->draw_list_bind_render_pipeline(draw_list, blendseams_line_raster_pipeline);
  1811. for (int j = 1; j < uv_offset_count; j++) {
  1812. seams_push_constant.base_index = seam_offset;
  1813. seams_push_constant.uv_offset[0] = (uv_offsets[j].x - 0.5f) / float(atlas_size.width);
  1814. seams_push_constant.uv_offset[1] = (uv_offsets[j].y - 0.5f) / float(atlas_size.height);
  1815. seams_push_constant.blend = uv_offsets[0].z;
  1816. rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant));
  1817. rd->draw_list_draw(draw_list, false, 1, slice_seam_count[i] * 4);
  1818. }
  1819. rd->draw_list_end();
  1820. }
  1821. seam_offset += slice_seam_count[i];
  1822. triangle_offset += slice_triangle_count[i];
  1823. }
  1824. }
  1825. if (p_bake_sh) {
  1826. SWAP(light_accum_tex, light_accum_tex2);
  1827. BakeError error = _pack_l1(rd, compute_shader, compute_base_uniform_set, push_constant, light_accum_tex2, light_accum_tex, atlas_size, atlas_slices);
  1828. if (unlikely(error != BAKE_OK)) {
  1829. return error;
  1830. }
  1831. }
  1832. #ifdef DEBUG_TEXTURES
  1833. for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
  1834. Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
  1835. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1836. img->save_exr("res://5_blendseams" + itos(i) + ".exr", false);
  1837. }
  1838. #endif
  1839. if (p_step_function) {
  1840. p_step_function(0.9, RTR("Retrieving textures"), p_bake_userdata, true);
  1841. }
  1842. for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
  1843. Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
  1844. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1845. img->convert(Image::FORMAT_RGBH); //remove alpha
  1846. bake_textures.push_back(img);
  1847. }
  1848. if (probe_positions.size() > 0) {
  1849. probe_values.resize(probe_positions.size() * 9);
  1850. Vector<uint8_t> probe_data = rd->buffer_get_data(light_probe_buffer);
  1851. memcpy(probe_values.ptrw(), probe_data.ptr(), probe_data.size());
  1852. rd->free(light_probe_buffer);
  1853. #ifdef DEBUG_TEXTURES
  1854. {
  1855. Ref<Image> img2 = Image::create_from_data(probe_values.size(), 1, false, Image::FORMAT_RGBAF, probe_data);
  1856. img2->save_exr("res://6_lightprobes.exr", false);
  1857. }
  1858. #endif
  1859. }
  1860. FREE_TEXTURES
  1861. FREE_BUFFERS
  1862. FREE_RASTER_RESOURCES
  1863. FREE_COMPUTE_RESOURCES
  1864. FREE_BLENDSEAMS_RESOURCES
  1865. memdelete(rd);
  1866. if (rcd != nullptr) {
  1867. memdelete(rcd);
  1868. }
  1869. return BAKE_OK;
  1870. }
  1871. int LightmapperRD::get_bake_texture_count() const {
  1872. return bake_textures.size();
  1873. }
  1874. Ref<Image> LightmapperRD::get_bake_texture(int p_index) const {
  1875. ERR_FAIL_INDEX_V(p_index, bake_textures.size(), Ref<Image>());
  1876. return bake_textures[p_index];
  1877. }
  1878. int LightmapperRD::get_bake_mesh_count() const {
  1879. return mesh_instances.size();
  1880. }
  1881. Variant LightmapperRD::get_bake_mesh_userdata(int p_index) const {
  1882. ERR_FAIL_INDEX_V(p_index, mesh_instances.size(), Variant());
  1883. return mesh_instances[p_index].data.userdata;
  1884. }
  1885. Rect2 LightmapperRD::get_bake_mesh_uv_scale(int p_index) const {
  1886. ERR_FAIL_COND_V(bake_textures.is_empty(), Rect2());
  1887. Rect2 uv_ofs;
  1888. Vector2 atlas_size = Vector2(bake_textures[0]->get_width(), bake_textures[0]->get_height());
  1889. uv_ofs.position = Vector2(mesh_instances[p_index].offset) / atlas_size;
  1890. 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;
  1891. return uv_ofs;
  1892. }
  1893. int LightmapperRD::get_bake_mesh_texture_slice(int p_index) const {
  1894. ERR_FAIL_INDEX_V(p_index, mesh_instances.size(), Variant());
  1895. return mesh_instances[p_index].slice;
  1896. }
  1897. int LightmapperRD::get_bake_probe_count() const {
  1898. return probe_positions.size();
  1899. }
  1900. Vector3 LightmapperRD::get_bake_probe_point(int p_probe) const {
  1901. ERR_FAIL_INDEX_V(p_probe, probe_positions.size(), Variant());
  1902. return Vector3(probe_positions[p_probe].position[0], probe_positions[p_probe].position[1], probe_positions[p_probe].position[2]);
  1903. }
  1904. Vector<Color> LightmapperRD::get_bake_probe_sh(int p_probe) const {
  1905. ERR_FAIL_INDEX_V(p_probe, probe_positions.size(), Vector<Color>());
  1906. Vector<Color> ret;
  1907. ret.resize(9);
  1908. memcpy(ret.ptrw(), &probe_values[p_probe * 9], sizeof(Color) * 9);
  1909. return ret;
  1910. }
  1911. LightmapperRD::LightmapperRD() {
  1912. }