lightmapper_rd.cpp 73 KB

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