lightmapper_rd.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  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::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors, 1.0, 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. raster_depth_buffer = rd->texture_create(tf, RD::TextureView());
  1226. }
  1227. rd->submit();
  1228. rd->sync();
  1229. /* STEP 3: Raster the geometry to UV2 coords in the atlas textures GPU*/
  1230. _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);
  1231. #ifdef DEBUG_TEXTURES
  1232. for (int i = 0; i < atlas_slices; i++) {
  1233. Vector<uint8_t> s = rd->texture_get_data(position_tex, i);
  1234. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAF, s);
  1235. img->save_exr("res://1_position_" + itos(i) + ".exr", false);
  1236. s = rd->texture_get_data(normal_tex, i);
  1237. img->set_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1238. img->save_exr("res://1_normal_" + itos(i) + ".exr", false);
  1239. }
  1240. #endif
  1241. #define FREE_RASTER_RESOURCES \
  1242. rd->free(rasterize_shader); \
  1243. rd->free(sampler); \
  1244. rd->free(raster_depth_buffer);
  1245. /* Plot direct light */
  1246. Ref<RDShaderFile> compute_shader;
  1247. String defines = "";
  1248. defines += "\n#define CLUSTER_SIZE " + uitos(cluster_size) + "\n";
  1249. if (p_bake_sh) {
  1250. defines += "\n#define USE_SH_LIGHTMAPS\n";
  1251. }
  1252. if (p_texture_for_bounces) {
  1253. defines += "\n#define USE_LIGHT_TEXTURE_FOR_BOUNCES\n";
  1254. }
  1255. compute_shader.instantiate();
  1256. err = compute_shader->parse_versions_from_text(lm_compute_shader_glsl, defines);
  1257. if (err != OK) {
  1258. FREE_TEXTURES
  1259. FREE_BUFFERS
  1260. FREE_RASTER_RESOURCES
  1261. memdelete(rd);
  1262. if (rcd != nullptr) {
  1263. memdelete(rcd);
  1264. }
  1265. compute_shader->print_errors("compute_shader");
  1266. }
  1267. ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  1268. // Unoccluder
  1269. RID compute_shader_unocclude = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("unocclude"));
  1270. ERR_FAIL_COND_V(compute_shader_unocclude.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); // internal check, should not happen
  1271. RID compute_shader_unocclude_pipeline = rd->compute_pipeline_create(compute_shader_unocclude);
  1272. // Direct light
  1273. RID compute_shader_primary = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("primary"));
  1274. ERR_FAIL_COND_V(compute_shader_primary.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); // internal check, should not happen
  1275. RID compute_shader_primary_pipeline = rd->compute_pipeline_create(compute_shader_primary);
  1276. // Indirect light
  1277. RID compute_shader_secondary = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("secondary"));
  1278. ERR_FAIL_COND_V(compute_shader_secondary.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen
  1279. RID compute_shader_secondary_pipeline = rd->compute_pipeline_create(compute_shader_secondary);
  1280. // Light probes
  1281. RID compute_shader_light_probes = rd->shader_create_from_spirv(compute_shader->get_spirv_stages("light_probes"));
  1282. ERR_FAIL_COND_V(compute_shader_light_probes.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES); //internal check, should not happen
  1283. RID compute_shader_light_probes_pipeline = rd->compute_pipeline_create(compute_shader_light_probes);
  1284. RID compute_base_uniform_set = rd->uniform_set_create(base_uniforms, compute_shader_primary, 0);
  1285. #define FREE_COMPUTE_RESOURCES \
  1286. rd->free(compute_shader_unocclude); \
  1287. rd->free(compute_shader_primary); \
  1288. rd->free(compute_shader_secondary); \
  1289. rd->free(compute_shader_light_probes);
  1290. Vector3i group_size(Math::division_round_up(atlas_size.x, 8), Math::division_round_up(atlas_size.y, 8), 1);
  1291. rd->submit();
  1292. rd->sync();
  1293. if (p_step_function) {
  1294. if (p_step_function(0.49, RTR("Un-occluding geometry"), p_bake_userdata, true)) {
  1295. FREE_TEXTURES
  1296. FREE_BUFFERS
  1297. FREE_RASTER_RESOURCES
  1298. FREE_COMPUTE_RESOURCES
  1299. memdelete(rd);
  1300. if (rcd != nullptr) {
  1301. memdelete(rcd);
  1302. }
  1303. return BAKE_ERROR_USER_ABORTED;
  1304. }
  1305. }
  1306. PushConstant push_constant;
  1307. /* UNOCCLUDE */
  1308. {
  1309. Vector<RD::Uniform> uniforms;
  1310. {
  1311. {
  1312. RD::Uniform u;
  1313. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1314. u.binding = 0;
  1315. u.append_id(position_tex);
  1316. uniforms.push_back(u);
  1317. }
  1318. {
  1319. RD::Uniform u;
  1320. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1321. u.binding = 1;
  1322. u.append_id(unocclude_tex); //will be unused
  1323. uniforms.push_back(u);
  1324. }
  1325. }
  1326. RID unocclude_uniform_set = rd->uniform_set_create(uniforms, compute_shader_unocclude, 1);
  1327. RD::ComputeListID compute_list = rd->compute_list_begin();
  1328. rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_unocclude_pipeline);
  1329. rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
  1330. rd->compute_list_bind_uniform_set(compute_list, unocclude_uniform_set, 1);
  1331. for (int i = 0; i < atlas_slices; i++) {
  1332. push_constant.atlas_slice = i;
  1333. rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  1334. rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
  1335. //no barrier, let them run all together
  1336. }
  1337. rd->compute_list_end(); //done
  1338. }
  1339. if (p_step_function) {
  1340. if (p_step_function(0.5, RTR("Plot direct lighting"), p_bake_userdata, true)) {
  1341. FREE_TEXTURES
  1342. FREE_BUFFERS
  1343. FREE_RASTER_RESOURCES
  1344. FREE_COMPUTE_RESOURCES
  1345. memdelete(rd);
  1346. if (rcd != nullptr) {
  1347. memdelete(rcd);
  1348. }
  1349. return BAKE_ERROR_USER_ABORTED;
  1350. }
  1351. }
  1352. // Set ray count to the quality used for direct light and bounces.
  1353. switch (p_quality) {
  1354. case BAKE_QUALITY_LOW: {
  1355. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/low_quality_ray_count");
  1356. } break;
  1357. case BAKE_QUALITY_MEDIUM: {
  1358. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/medium_quality_ray_count");
  1359. } break;
  1360. case BAKE_QUALITY_HIGH: {
  1361. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/high_quality_ray_count");
  1362. } break;
  1363. case BAKE_QUALITY_ULTRA: {
  1364. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/ultra_quality_ray_count");
  1365. } break;
  1366. }
  1367. push_constant.ray_count = CLAMP(push_constant.ray_count, 16u, 8192u);
  1368. /* PRIMARY (direct) LIGHT PASS */
  1369. {
  1370. Vector<RD::Uniform> uniforms;
  1371. {
  1372. {
  1373. RD::Uniform u;
  1374. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1375. u.binding = 0;
  1376. u.append_id(light_source_tex);
  1377. uniforms.push_back(u);
  1378. }
  1379. {
  1380. RD::Uniform u;
  1381. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1382. u.binding = 1;
  1383. u.append_id(light_dest_tex); //will be unused
  1384. uniforms.push_back(u);
  1385. }
  1386. {
  1387. RD::Uniform u;
  1388. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1389. u.binding = 2;
  1390. u.append_id(position_tex);
  1391. uniforms.push_back(u);
  1392. }
  1393. {
  1394. RD::Uniform u;
  1395. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1396. u.binding = 3;
  1397. u.append_id(normal_tex);
  1398. uniforms.push_back(u);
  1399. }
  1400. {
  1401. RD::Uniform u;
  1402. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1403. u.binding = 4;
  1404. u.append_id(light_accum_tex);
  1405. uniforms.push_back(u);
  1406. }
  1407. }
  1408. RID light_uniform_set = rd->uniform_set_create(uniforms, compute_shader_primary, 1);
  1409. RD::ComputeListID compute_list = rd->compute_list_begin();
  1410. rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_primary_pipeline);
  1411. rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
  1412. rd->compute_list_bind_uniform_set(compute_list, light_uniform_set, 1);
  1413. for (int i = 0; i < atlas_slices; i++) {
  1414. push_constant.atlas_slice = i;
  1415. rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  1416. rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
  1417. //no barrier, let them run all together
  1418. }
  1419. rd->compute_list_end(); //done
  1420. }
  1421. #ifdef DEBUG_TEXTURES
  1422. for (int i = 0; i < atlas_slices; i++) {
  1423. Vector<uint8_t> s = rd->texture_get_data(light_source_tex, i);
  1424. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1425. img->save_exr("res://2_light_primary_" + itos(i) + ".exr", false);
  1426. }
  1427. if (p_bake_sh) {
  1428. for (int i = 0; i < atlas_slices * 4; i++) {
  1429. Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
  1430. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1431. img->save_exr("res://2_light_primary_accum_" + itos(i) + ".exr", false);
  1432. }
  1433. }
  1434. #endif
  1435. /* SECONDARY (indirect) LIGHT PASS(ES) */
  1436. if (p_bounces > 0) {
  1437. Vector<RD::Uniform> uniforms;
  1438. {
  1439. {
  1440. // Unused.
  1441. RD::Uniform u;
  1442. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1443. u.binding = 0;
  1444. u.append_id(light_dest_tex);
  1445. uniforms.push_back(u);
  1446. }
  1447. {
  1448. RD::Uniform u;
  1449. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1450. u.binding = 1;
  1451. u.append_id(light_source_tex);
  1452. uniforms.push_back(u);
  1453. }
  1454. {
  1455. RD::Uniform u;
  1456. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1457. u.binding = 2;
  1458. u.append_id(position_tex);
  1459. uniforms.push_back(u);
  1460. }
  1461. {
  1462. RD::Uniform u;
  1463. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1464. u.binding = 3;
  1465. u.append_id(normal_tex);
  1466. uniforms.push_back(u);
  1467. }
  1468. {
  1469. RD::Uniform u;
  1470. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  1471. u.binding = 4;
  1472. u.append_id(light_accum_tex);
  1473. uniforms.push_back(u);
  1474. }
  1475. {
  1476. RD::Uniform u;
  1477. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1478. u.binding = 5;
  1479. u.append_id(light_environment_tex);
  1480. uniforms.push_back(u);
  1481. }
  1482. }
  1483. RID secondary_uniform_set;
  1484. secondary_uniform_set = rd->uniform_set_create(uniforms, compute_shader_secondary, 1);
  1485. int max_region_size = nearest_power_of_2_templated(int(GLOBAL_GET("rendering/lightmapping/bake_performance/region_size")));
  1486. int max_rays = GLOBAL_GET("rendering/lightmapping/bake_performance/max_rays_per_pass");
  1487. int x_regions = Math::division_round_up(atlas_size.width, max_region_size);
  1488. int y_regions = Math::division_round_up(atlas_size.height, max_region_size);
  1489. int ray_iterations = Math::division_round_up((int32_t)push_constant.ray_count, max_rays);
  1490. rd->submit();
  1491. rd->sync();
  1492. if (p_step_function) {
  1493. if (p_step_function(0.6, RTR("Integrate indirect lighting"), p_bake_userdata, true)) {
  1494. FREE_TEXTURES
  1495. FREE_BUFFERS
  1496. FREE_RASTER_RESOURCES
  1497. FREE_COMPUTE_RESOURCES
  1498. memdelete(rd);
  1499. if (rcd != nullptr) {
  1500. memdelete(rcd);
  1501. }
  1502. return BAKE_ERROR_USER_ABORTED;
  1503. }
  1504. }
  1505. int count = 0;
  1506. for (int s = 0; s < atlas_slices; s++) {
  1507. push_constant.atlas_slice = s;
  1508. for (int i = 0; i < x_regions; i++) {
  1509. for (int j = 0; j < y_regions; j++) {
  1510. int x = i * max_region_size;
  1511. int y = j * max_region_size;
  1512. int w = MIN((i + 1) * max_region_size, atlas_size.width) - x;
  1513. int h = MIN((j + 1) * max_region_size, atlas_size.height) - y;
  1514. push_constant.region_ofs[0] = x;
  1515. push_constant.region_ofs[1] = y;
  1516. group_size = Vector3i(Math::division_round_up(w, 8), Math::division_round_up(h, 8), 1);
  1517. for (int k = 0; k < ray_iterations; k++) {
  1518. RD::ComputeListID compute_list = rd->compute_list_begin();
  1519. rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_secondary_pipeline);
  1520. rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
  1521. rd->compute_list_bind_uniform_set(compute_list, secondary_uniform_set, 1);
  1522. push_constant.ray_from = k * max_rays;
  1523. push_constant.ray_to = MIN((k + 1) * max_rays, int32_t(push_constant.ray_count));
  1524. rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  1525. rd->compute_list_dispatch(compute_list, group_size.x, group_size.y, group_size.z);
  1526. rd->compute_list_end();
  1527. rd->submit();
  1528. rd->sync();
  1529. count++;
  1530. if (p_step_function) {
  1531. int total = (atlas_slices * x_regions * y_regions * ray_iterations);
  1532. int percent = count * 100 / total;
  1533. float p = float(count) / total * 0.1;
  1534. if (p_step_function(0.6 + p, vformat(RTR("Integrate indirect lighting %d%%"), percent), p_bake_userdata, false)) {
  1535. FREE_TEXTURES
  1536. FREE_BUFFERS
  1537. FREE_RASTER_RESOURCES
  1538. FREE_COMPUTE_RESOURCES
  1539. memdelete(rd);
  1540. if (rcd != nullptr) {
  1541. memdelete(rcd);
  1542. }
  1543. return BAKE_ERROR_USER_ABORTED;
  1544. }
  1545. }
  1546. }
  1547. }
  1548. }
  1549. }
  1550. }
  1551. /* LIGHTPROBES */
  1552. RID light_probe_buffer;
  1553. if (probe_positions.size()) {
  1554. light_probe_buffer = rd->storage_buffer_create(sizeof(float) * 4 * 9 * probe_positions.size());
  1555. if (p_step_function) {
  1556. if (p_step_function(0.7, RTR("Baking light probes"), p_bake_userdata, true)) {
  1557. FREE_TEXTURES
  1558. FREE_BUFFERS
  1559. FREE_RASTER_RESOURCES
  1560. FREE_COMPUTE_RESOURCES
  1561. if (probe_positions.size() > 0) {
  1562. rd->free(light_probe_buffer);
  1563. }
  1564. memdelete(rd);
  1565. if (rcd != nullptr) {
  1566. memdelete(rcd);
  1567. }
  1568. return BAKE_ERROR_USER_ABORTED;
  1569. }
  1570. }
  1571. Vector<RD::Uniform> uniforms;
  1572. {
  1573. {
  1574. RD::Uniform u;
  1575. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1576. u.binding = 0;
  1577. u.append_id(light_probe_buffer);
  1578. uniforms.push_back(u);
  1579. }
  1580. {
  1581. RD::Uniform u;
  1582. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1583. u.binding = 1;
  1584. u.append_id(light_source_tex);
  1585. uniforms.push_back(u);
  1586. }
  1587. {
  1588. RD::Uniform u;
  1589. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1590. u.binding = 2;
  1591. u.append_id(light_environment_tex);
  1592. uniforms.push_back(u);
  1593. }
  1594. }
  1595. RID light_probe_uniform_set = rd->uniform_set_create(uniforms, compute_shader_light_probes, 1);
  1596. switch (p_quality) {
  1597. case BAKE_QUALITY_LOW: {
  1598. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/low_quality_probe_ray_count");
  1599. } break;
  1600. case BAKE_QUALITY_MEDIUM: {
  1601. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/medium_quality_probe_ray_count");
  1602. } break;
  1603. case BAKE_QUALITY_HIGH: {
  1604. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/high_quality_probe_ray_count");
  1605. } break;
  1606. case BAKE_QUALITY_ULTRA: {
  1607. push_constant.ray_count = GLOBAL_GET("rendering/lightmapping/bake_quality/ultra_quality_probe_ray_count");
  1608. } break;
  1609. }
  1610. push_constant.ray_count = CLAMP(push_constant.ray_count, 16u, 8192u);
  1611. push_constant.probe_count = probe_positions.size();
  1612. int max_rays = GLOBAL_GET("rendering/lightmapping/bake_performance/max_rays_per_probe_pass");
  1613. int ray_iterations = Math::division_round_up((int32_t)push_constant.ray_count, max_rays);
  1614. for (int i = 0; i < ray_iterations; i++) {
  1615. RD::ComputeListID compute_list = rd->compute_list_begin();
  1616. rd->compute_list_bind_compute_pipeline(compute_list, compute_shader_light_probes_pipeline);
  1617. rd->compute_list_bind_uniform_set(compute_list, compute_base_uniform_set, 0);
  1618. rd->compute_list_bind_uniform_set(compute_list, light_probe_uniform_set, 1);
  1619. push_constant.ray_from = i * max_rays;
  1620. push_constant.ray_to = MIN((i + 1) * max_rays, int32_t(push_constant.ray_count));
  1621. rd->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  1622. rd->compute_list_dispatch(compute_list, Math::division_round_up((int)probe_positions.size(), 64), 1, 1);
  1623. rd->compute_list_end(); //done
  1624. rd->submit();
  1625. rd->sync();
  1626. if (p_step_function) {
  1627. int percent = i * 100 / ray_iterations;
  1628. float p = float(i) / ray_iterations * 0.1;
  1629. if (p_step_function(0.7 + p, vformat(RTR("Integrating light probes %d%%"), percent), p_bake_userdata, false)) {
  1630. FREE_TEXTURES
  1631. FREE_BUFFERS
  1632. FREE_RASTER_RESOURCES
  1633. FREE_COMPUTE_RESOURCES
  1634. if (probe_positions.size() > 0) {
  1635. rd->free(light_probe_buffer);
  1636. }
  1637. memdelete(rd);
  1638. if (rcd != nullptr) {
  1639. memdelete(rcd);
  1640. }
  1641. return BAKE_ERROR_USER_ABORTED;
  1642. }
  1643. }
  1644. }
  1645. }
  1646. #if 0
  1647. for (int i = 0; i < probe_positions.size(); i++) {
  1648. Ref<Image> img = Image::create_empty(6, 4, false, Image::FORMAT_RGB8);
  1649. for (int j = 0; j < 6; j++) {
  1650. Vector<uint8_t> s = rd->texture_get_data(lightprobe_tex, i * 6 + j);
  1651. Ref<Image> img2 = Image::create_from_data(2, 2, false, Image::FORMAT_RGBAF, s);
  1652. img2->convert(Image::FORMAT_RGB8);
  1653. img->blit_rect(img2, Rect2i(0, 0, 2, 2), Point2i((j % 3) * 2, (j / 3) * 2));
  1654. }
  1655. img->save_png("res://3_light_probe_" + itos(i) + ".png");
  1656. }
  1657. #endif
  1658. /* DENOISE */
  1659. if (p_use_denoiser) {
  1660. if (p_step_function) {
  1661. if (p_step_function(0.8, RTR("Denoising"), p_bake_userdata, true)) {
  1662. FREE_TEXTURES
  1663. FREE_BUFFERS
  1664. FREE_RASTER_RESOURCES
  1665. FREE_COMPUTE_RESOURCES
  1666. if (probe_positions.size() > 0) {
  1667. rd->free(light_probe_buffer);
  1668. }
  1669. memdelete(rd);
  1670. if (rcd != nullptr) {
  1671. memdelete(rcd);
  1672. }
  1673. return BAKE_ERROR_USER_ABORTED;
  1674. }
  1675. }
  1676. {
  1677. BakeError error;
  1678. if (denoiser == 1) {
  1679. // OIDN (external).
  1680. error = _denoise_oidn(rd, light_accum_tex, normal_tex, light_accum_tex, atlas_size, atlas_slices, p_bake_sh, oidn_path);
  1681. } else {
  1682. // JNLM (built-in).
  1683. SWAP(light_accum_tex, light_accum_tex2);
  1684. 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);
  1685. }
  1686. if (unlikely(error != BAKE_OK)) {
  1687. return error;
  1688. }
  1689. }
  1690. }
  1691. {
  1692. SWAP(light_accum_tex, light_accum_tex2);
  1693. 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));
  1694. if (unlikely(error != BAKE_OK)) {
  1695. return error;
  1696. }
  1697. }
  1698. #ifdef DEBUG_TEXTURES
  1699. for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
  1700. Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
  1701. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1702. img->save_exr("res://4_light_secondary_" + itos(i) + ".exr", false);
  1703. }
  1704. #endif
  1705. /* BLEND SEAMS */
  1706. //shaders
  1707. Ref<RDShaderFile> blendseams_shader;
  1708. blendseams_shader.instantiate();
  1709. err = blendseams_shader->parse_versions_from_text(lm_blendseams_shader_glsl);
  1710. if (err != OK) {
  1711. FREE_TEXTURES
  1712. FREE_BUFFERS
  1713. FREE_RASTER_RESOURCES
  1714. FREE_COMPUTE_RESOURCES
  1715. memdelete(rd);
  1716. if (rcd != nullptr) {
  1717. memdelete(rcd);
  1718. }
  1719. blendseams_shader->print_errors("blendseams_shader");
  1720. }
  1721. ERR_FAIL_COND_V(err != OK, BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  1722. RID blendseams_line_raster_shader = rd->shader_create_from_spirv(blendseams_shader->get_spirv_stages("lines"));
  1723. ERR_FAIL_COND_V(blendseams_line_raster_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  1724. RID blendseams_triangle_raster_shader = rd->shader_create_from_spirv(blendseams_shader->get_spirv_stages("triangles"));
  1725. ERR_FAIL_COND_V(blendseams_triangle_raster_shader.is_null(), BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES);
  1726. #define FREE_BLENDSEAMS_RESOURCES \
  1727. rd->free(blendseams_line_raster_shader); \
  1728. rd->free(blendseams_triangle_raster_shader);
  1729. {
  1730. //pre copy
  1731. for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
  1732. rd->texture_copy(light_accum_tex, light_accum_tex2, Vector3(), Vector3(), Vector3(atlas_size.width, atlas_size.height, 1), 0, 0, i, i);
  1733. }
  1734. Vector<RID> framebuffers;
  1735. for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
  1736. RID slice_tex = rd->texture_create_shared_from_slice(RD::TextureView(), light_accum_tex, i, 0);
  1737. Vector<RID> fb;
  1738. fb.push_back(slice_tex);
  1739. fb.push_back(raster_depth_buffer);
  1740. framebuffers.push_back(rd->framebuffer_create(fb));
  1741. }
  1742. Vector<RD::Uniform> uniforms;
  1743. {
  1744. {
  1745. RD::Uniform u;
  1746. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1747. u.binding = 0;
  1748. u.append_id(light_accum_tex2);
  1749. uniforms.push_back(u);
  1750. }
  1751. }
  1752. RID blendseams_raster_uniform = rd->uniform_set_create(uniforms, blendseams_line_raster_shader, 1);
  1753. bool debug = false;
  1754. RD::PipelineColorBlendState bs = RD::PipelineColorBlendState::create_blend(1);
  1755. bs.attachments.write[0].src_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;
  1756. bs.attachments.write[0].dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
  1757. RD::PipelineDepthStencilState ds;
  1758. ds.enable_depth_test = true;
  1759. ds.enable_depth_write = true;
  1760. ds.depth_compare_operator = RD::COMPARE_OP_LESS; //so it does not render same pixel twice, this avoids wrong blending
  1761. 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);
  1762. 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);
  1763. uint32_t seam_offset = 0;
  1764. uint32_t triangle_offset = 0;
  1765. Vector<Color> clear_colors;
  1766. clear_colors.push_back(Color(0, 0, 0, 1));
  1767. for (int i = 0; i < atlas_slices; i++) {
  1768. int subslices = (p_bake_sh ? 4 : 1);
  1769. if (slice_seam_count[i] == 0) {
  1770. continue;
  1771. }
  1772. for (int k = 0; k < subslices; k++) {
  1773. RasterSeamsPushConstant seams_push_constant;
  1774. seams_push_constant.slice = uint32_t(i * subslices + k);
  1775. seams_push_constant.debug = debug;
  1776. // Store the current subslice in the breadcrumb.
  1777. RD::DrawListID draw_list = rd->draw_list_begin(framebuffers[i * subslices + k], RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, clear_colors, 1.0, 0, Rect2(), RDD::BreadcrumbMarker::LIGHTMAPPER_PASS | seams_push_constant.slice);
  1778. rd->draw_list_bind_uniform_set(draw_list, raster_base_uniform, 0);
  1779. rd->draw_list_bind_uniform_set(draw_list, blendseams_raster_uniform, 1);
  1780. const int uv_offset_count = 9;
  1781. static const Vector3 uv_offsets[uv_offset_count] = {
  1782. Vector3(0, 0, 0.5), //using zbuffer, so go inwards-outwards
  1783. Vector3(0, 1, 0.2),
  1784. Vector3(0, -1, 0.2),
  1785. Vector3(1, 0, 0.2),
  1786. Vector3(-1, 0, 0.2),
  1787. Vector3(-1, -1, 0.1),
  1788. Vector3(1, -1, 0.1),
  1789. Vector3(1, 1, 0.1),
  1790. Vector3(-1, 1, 0.1),
  1791. };
  1792. /* step 1 use lines to blend the edges */
  1793. {
  1794. seams_push_constant.base_index = seam_offset;
  1795. rd->draw_list_bind_render_pipeline(draw_list, blendseams_line_raster_pipeline);
  1796. seams_push_constant.uv_offset[0] = (uv_offsets[0].x - 0.5f) / float(atlas_size.width);
  1797. seams_push_constant.uv_offset[1] = (uv_offsets[0].y - 0.5f) / float(atlas_size.height);
  1798. seams_push_constant.blend = uv_offsets[0].z;
  1799. rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant));
  1800. rd->draw_list_draw(draw_list, false, 1, slice_seam_count[i] * 4);
  1801. }
  1802. /* step 2 use triangles to mask the interior */
  1803. {
  1804. seams_push_constant.base_index = triangle_offset;
  1805. rd->draw_list_bind_render_pipeline(draw_list, blendseams_triangle_raster_pipeline);
  1806. seams_push_constant.blend = 0; //do not draw them, just fill the z-buffer so its used as a mask
  1807. rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant));
  1808. rd->draw_list_draw(draw_list, false, 1, slice_triangle_count[i] * 3);
  1809. }
  1810. /* step 3 blend around the triangle */
  1811. rd->draw_list_bind_render_pipeline(draw_list, blendseams_line_raster_pipeline);
  1812. for (int j = 1; j < uv_offset_count; j++) {
  1813. seams_push_constant.base_index = seam_offset;
  1814. seams_push_constant.uv_offset[0] = (uv_offsets[j].x - 0.5f) / float(atlas_size.width);
  1815. seams_push_constant.uv_offset[1] = (uv_offsets[j].y - 0.5f) / float(atlas_size.height);
  1816. seams_push_constant.blend = uv_offsets[0].z;
  1817. rd->draw_list_set_push_constant(draw_list, &seams_push_constant, sizeof(RasterSeamsPushConstant));
  1818. rd->draw_list_draw(draw_list, false, 1, slice_seam_count[i] * 4);
  1819. }
  1820. rd->draw_list_end();
  1821. }
  1822. seam_offset += slice_seam_count[i];
  1823. triangle_offset += slice_triangle_count[i];
  1824. }
  1825. }
  1826. if (p_bake_sh) {
  1827. SWAP(light_accum_tex, light_accum_tex2);
  1828. BakeError error = _pack_l1(rd, compute_shader, compute_base_uniform_set, push_constant, light_accum_tex2, light_accum_tex, atlas_size, atlas_slices);
  1829. if (unlikely(error != BAKE_OK)) {
  1830. return error;
  1831. }
  1832. }
  1833. #ifdef DEBUG_TEXTURES
  1834. for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
  1835. Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
  1836. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1837. img->save_exr("res://5_blendseams" + itos(i) + ".exr", false);
  1838. }
  1839. #endif
  1840. if (p_step_function) {
  1841. p_step_function(0.9, RTR("Retrieving textures"), p_bake_userdata, true);
  1842. }
  1843. for (int i = 0; i < atlas_slices * (p_bake_sh ? 4 : 1); i++) {
  1844. Vector<uint8_t> s = rd->texture_get_data(light_accum_tex, i);
  1845. Ref<Image> img = Image::create_from_data(atlas_size.width, atlas_size.height, false, Image::FORMAT_RGBAH, s);
  1846. img->convert(Image::FORMAT_RGBH); //remove alpha
  1847. bake_textures.push_back(img);
  1848. }
  1849. if (probe_positions.size() > 0) {
  1850. probe_values.resize(probe_positions.size() * 9);
  1851. Vector<uint8_t> probe_data = rd->buffer_get_data(light_probe_buffer);
  1852. memcpy(probe_values.ptrw(), probe_data.ptr(), probe_data.size());
  1853. rd->free(light_probe_buffer);
  1854. #ifdef DEBUG_TEXTURES
  1855. {
  1856. Ref<Image> img2 = Image::create_from_data(probe_values.size(), 1, false, Image::FORMAT_RGBAF, probe_data);
  1857. img2->save_exr("res://6_lightprobes.exr", false);
  1858. }
  1859. #endif
  1860. }
  1861. FREE_TEXTURES
  1862. FREE_BUFFERS
  1863. FREE_RASTER_RESOURCES
  1864. FREE_COMPUTE_RESOURCES
  1865. FREE_BLENDSEAMS_RESOURCES
  1866. memdelete(rd);
  1867. if (rcd != nullptr) {
  1868. memdelete(rcd);
  1869. }
  1870. return BAKE_OK;
  1871. }
  1872. int LightmapperRD::get_bake_texture_count() const {
  1873. return bake_textures.size();
  1874. }
  1875. Ref<Image> LightmapperRD::get_bake_texture(int p_index) const {
  1876. ERR_FAIL_INDEX_V(p_index, bake_textures.size(), Ref<Image>());
  1877. return bake_textures[p_index];
  1878. }
  1879. int LightmapperRD::get_bake_mesh_count() const {
  1880. return mesh_instances.size();
  1881. }
  1882. Variant LightmapperRD::get_bake_mesh_userdata(int p_index) const {
  1883. ERR_FAIL_INDEX_V(p_index, mesh_instances.size(), Variant());
  1884. return mesh_instances[p_index].data.userdata;
  1885. }
  1886. Rect2 LightmapperRD::get_bake_mesh_uv_scale(int p_index) const {
  1887. ERR_FAIL_COND_V(bake_textures.is_empty(), Rect2());
  1888. Rect2 uv_ofs;
  1889. Vector2 atlas_size = Vector2(bake_textures[0]->get_width(), bake_textures[0]->get_height());
  1890. uv_ofs.position = Vector2(mesh_instances[p_index].offset) / atlas_size;
  1891. 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;
  1892. return uv_ofs;
  1893. }
  1894. int LightmapperRD::get_bake_mesh_texture_slice(int p_index) const {
  1895. ERR_FAIL_INDEX_V(p_index, mesh_instances.size(), Variant());
  1896. return mesh_instances[p_index].slice;
  1897. }
  1898. int LightmapperRD::get_bake_probe_count() const {
  1899. return probe_positions.size();
  1900. }
  1901. Vector3 LightmapperRD::get_bake_probe_point(int p_probe) const {
  1902. ERR_FAIL_INDEX_V(p_probe, probe_positions.size(), Variant());
  1903. return Vector3(probe_positions[p_probe].position[0], probe_positions[p_probe].position[1], probe_positions[p_probe].position[2]);
  1904. }
  1905. Vector<Color> LightmapperRD::get_bake_probe_sh(int p_probe) const {
  1906. ERR_FAIL_INDEX_V(p_probe, probe_positions.size(), Vector<Color>());
  1907. Vector<Color> ret;
  1908. ret.resize(9);
  1909. memcpy(ret.ptrw(), &probe_values[p_probe * 9], sizeof(Color) * 9);
  1910. return ret;
  1911. }
  1912. LightmapperRD::LightmapperRD() {
  1913. }