lightmapper_rd.cpp 60 KB

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