lightmapper_rd.cpp 61 KB

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