light_storage.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. /**************************************************************************/
  2. /* light_storage.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifdef GLES3_ENABLED
  31. #include "light_storage.h"
  32. #include "../rasterizer_gles3.h"
  33. #include "../rasterizer_scene_gles3.h"
  34. #include "core/config/project_settings.h"
  35. #include "texture_storage.h"
  36. using namespace GLES3;
  37. LightStorage *LightStorage::singleton = nullptr;
  38. LightStorage *LightStorage::get_singleton() {
  39. return singleton;
  40. }
  41. LightStorage::LightStorage() {
  42. singleton = this;
  43. directional_shadow.size = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/size");
  44. directional_shadow.use_16_bits = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/16_bits");
  45. // lightmap_probe_capture_update_speed = GLOBAL_GET("rendering/lightmapping/probe_capture/update_speed");
  46. }
  47. LightStorage::~LightStorage() {
  48. singleton = nullptr;
  49. }
  50. /* Light API */
  51. void LightStorage::_light_initialize(RID p_light, RS::LightType p_type) {
  52. Light light;
  53. light.type = p_type;
  54. light.param[RS::LIGHT_PARAM_ENERGY] = 1.0;
  55. light.param[RS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  56. light.param[RS::LIGHT_PARAM_VOLUMETRIC_FOG_ENERGY] = 1.0;
  57. light.param[RS::LIGHT_PARAM_SPECULAR] = 0.5;
  58. light.param[RS::LIGHT_PARAM_RANGE] = 1.0;
  59. light.param[RS::LIGHT_PARAM_SIZE] = 0.0;
  60. light.param[RS::LIGHT_PARAM_ATTENUATION] = 1.0;
  61. light.param[RS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  62. light.param[RS::LIGHT_PARAM_SPOT_ATTENUATION] = 1.0;
  63. light.param[RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  64. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  65. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  66. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  67. light.param[RS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
  68. light.param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 1.0;
  69. light.param[RS::LIGHT_PARAM_SHADOW_OPACITY] = 1.0;
  70. light.param[RS::LIGHT_PARAM_SHADOW_BIAS] = 0.02;
  71. light.param[RS::LIGHT_PARAM_SHADOW_BLUR] = 0;
  72. light.param[RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE] = 20.0;
  73. light.param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] = 0.05;
  74. light.param[RS::LIGHT_PARAM_INTENSITY] = p_type == RS::LIGHT_DIRECTIONAL ? 100000.0 : 1000.0;
  75. light_owner.initialize_rid(p_light, light);
  76. }
  77. RID LightStorage::directional_light_allocate() {
  78. return light_owner.allocate_rid();
  79. }
  80. void LightStorage::directional_light_initialize(RID p_rid) {
  81. _light_initialize(p_rid, RS::LIGHT_DIRECTIONAL);
  82. }
  83. RID LightStorage::omni_light_allocate() {
  84. return light_owner.allocate_rid();
  85. }
  86. void LightStorage::omni_light_initialize(RID p_rid) {
  87. _light_initialize(p_rid, RS::LIGHT_OMNI);
  88. }
  89. RID LightStorage::spot_light_allocate() {
  90. return light_owner.allocate_rid();
  91. }
  92. void LightStorage::spot_light_initialize(RID p_rid) {
  93. _light_initialize(p_rid, RS::LIGHT_SPOT);
  94. }
  95. void LightStorage::light_free(RID p_rid) {
  96. light_set_projector(p_rid, RID()); //clear projector
  97. // delete the texture
  98. Light *light = light_owner.get_or_null(p_rid);
  99. light->dependency.deleted_notify(p_rid);
  100. light_owner.free(p_rid);
  101. }
  102. void LightStorage::light_set_color(RID p_light, const Color &p_color) {
  103. Light *light = light_owner.get_or_null(p_light);
  104. ERR_FAIL_NULL(light);
  105. light->color = p_color;
  106. }
  107. void LightStorage::light_set_param(RID p_light, RS::LightParam p_param, float p_value) {
  108. Light *light = light_owner.get_or_null(p_light);
  109. ERR_FAIL_NULL(light);
  110. ERR_FAIL_INDEX(p_param, RS::LIGHT_PARAM_MAX);
  111. if (light->param[p_param] == p_value) {
  112. return;
  113. }
  114. switch (p_param) {
  115. case RS::LIGHT_PARAM_RANGE:
  116. case RS::LIGHT_PARAM_SPOT_ANGLE:
  117. case RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  118. case RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  119. case RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  120. case RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  121. case RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  122. case RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE:
  123. case RS::LIGHT_PARAM_SHADOW_BIAS: {
  124. light->version++;
  125. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  126. } break;
  127. case RS::LIGHT_PARAM_SIZE: {
  128. if ((light->param[p_param] > CMP_EPSILON) != (p_value > CMP_EPSILON)) {
  129. //changing from no size to size and the opposite
  130. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
  131. }
  132. } break;
  133. default: {
  134. }
  135. }
  136. light->param[p_param] = p_value;
  137. }
  138. void LightStorage::light_set_shadow(RID p_light, bool p_enabled) {
  139. Light *light = light_owner.get_or_null(p_light);
  140. ERR_FAIL_NULL(light);
  141. light->shadow = p_enabled;
  142. light->version++;
  143. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  144. }
  145. void LightStorage::light_set_projector(RID p_light, RID p_texture) {
  146. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  147. Light *light = light_owner.get_or_null(p_light);
  148. ERR_FAIL_NULL(light);
  149. if (light->projector == p_texture) {
  150. return;
  151. }
  152. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  153. texture_storage->texture_remove_from_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  154. }
  155. light->projector = p_texture;
  156. if (light->type != RS::LIGHT_DIRECTIONAL) {
  157. if (light->projector.is_valid()) {
  158. texture_storage->texture_add_to_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  159. }
  160. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR);
  161. }
  162. }
  163. void LightStorage::light_set_negative(RID p_light, bool p_enable) {
  164. Light *light = light_owner.get_or_null(p_light);
  165. ERR_FAIL_NULL(light);
  166. light->negative = p_enable;
  167. }
  168. void LightStorage::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  169. Light *light = light_owner.get_or_null(p_light);
  170. ERR_FAIL_NULL(light);
  171. light->cull_mask = p_mask;
  172. light->version++;
  173. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_CULL_MASK);
  174. }
  175. void LightStorage::light_set_shadow_caster_mask(RID p_light, uint32_t p_caster_mask) {
  176. Light *light = light_owner.get_or_null(p_light);
  177. ERR_FAIL_NULL(light);
  178. light->shadow_caster_mask = p_caster_mask;
  179. light->version++;
  180. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  181. }
  182. uint32_t LightStorage::light_get_shadow_caster_mask(RID p_light) const {
  183. Light *light = light_owner.get_or_null(p_light);
  184. ERR_FAIL_NULL_V(light, 0);
  185. return light->shadow_caster_mask;
  186. }
  187. void LightStorage::light_set_distance_fade(RID p_light, bool p_enabled, float p_begin, float p_shadow, float p_length) {
  188. Light *light = light_owner.get_or_null(p_light);
  189. ERR_FAIL_NULL(light);
  190. light->distance_fade = p_enabled;
  191. light->distance_fade_begin = p_begin;
  192. light->distance_fade_shadow = p_shadow;
  193. light->distance_fade_length = p_length;
  194. }
  195. void LightStorage::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  196. Light *light = light_owner.get_or_null(p_light);
  197. ERR_FAIL_NULL(light);
  198. light->reverse_cull = p_enabled;
  199. light->version++;
  200. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  201. }
  202. void LightStorage::light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) {
  203. Light *light = light_owner.get_or_null(p_light);
  204. ERR_FAIL_NULL(light);
  205. light->bake_mode = p_bake_mode;
  206. light->version++;
  207. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  208. }
  209. void LightStorage::light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) {
  210. Light *light = light_owner.get_or_null(p_light);
  211. ERR_FAIL_NULL(light);
  212. light->omni_shadow_mode = p_mode;
  213. light->version++;
  214. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  215. }
  216. RS::LightOmniShadowMode LightStorage::light_omni_get_shadow_mode(RID p_light) {
  217. const Light *light = light_owner.get_or_null(p_light);
  218. ERR_FAIL_NULL_V(light, RS::LIGHT_OMNI_SHADOW_CUBE);
  219. return light->omni_shadow_mode;
  220. }
  221. void LightStorage::light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) {
  222. Light *light = light_owner.get_or_null(p_light);
  223. ERR_FAIL_NULL(light);
  224. light->directional_shadow_mode = p_mode;
  225. light->version++;
  226. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  227. }
  228. void LightStorage::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  229. Light *light = light_owner.get_or_null(p_light);
  230. ERR_FAIL_NULL(light);
  231. light->directional_blend_splits = p_enable;
  232. light->version++;
  233. light->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_LIGHT);
  234. }
  235. bool LightStorage::light_directional_get_blend_splits(RID p_light) const {
  236. const Light *light = light_owner.get_or_null(p_light);
  237. ERR_FAIL_NULL_V(light, false);
  238. return light->directional_blend_splits;
  239. }
  240. void LightStorage::light_directional_set_sky_mode(RID p_light, RS::LightDirectionalSkyMode p_mode) {
  241. Light *light = light_owner.get_or_null(p_light);
  242. ERR_FAIL_NULL(light);
  243. light->directional_sky_mode = p_mode;
  244. }
  245. RS::LightDirectionalSkyMode LightStorage::light_directional_get_sky_mode(RID p_light) const {
  246. const Light *light = light_owner.get_or_null(p_light);
  247. ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_AND_SKY);
  248. return light->directional_sky_mode;
  249. }
  250. RS::LightDirectionalShadowMode LightStorage::light_directional_get_shadow_mode(RID p_light) {
  251. const Light *light = light_owner.get_or_null(p_light);
  252. ERR_FAIL_NULL_V(light, RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  253. return light->directional_shadow_mode;
  254. }
  255. RS::LightBakeMode LightStorage::light_get_bake_mode(RID p_light) {
  256. const Light *light = light_owner.get_or_null(p_light);
  257. ERR_FAIL_NULL_V(light, RS::LIGHT_BAKE_DISABLED);
  258. return light->bake_mode;
  259. }
  260. uint64_t LightStorage::light_get_version(RID p_light) const {
  261. const Light *light = light_owner.get_or_null(p_light);
  262. ERR_FAIL_NULL_V(light, 0);
  263. return light->version;
  264. }
  265. uint32_t LightStorage::light_get_cull_mask(RID p_light) const {
  266. const Light *light = light_owner.get_or_null(p_light);
  267. ERR_FAIL_NULL_V(light, 0);
  268. return light->cull_mask;
  269. }
  270. AABB LightStorage::light_get_aabb(RID p_light) const {
  271. const Light *light = light_owner.get_or_null(p_light);
  272. ERR_FAIL_NULL_V(light, AABB());
  273. switch (light->type) {
  274. case RS::LIGHT_SPOT: {
  275. float len = light->param[RS::LIGHT_PARAM_RANGE];
  276. float size = Math::tan(Math::deg_to_rad(light->param[RS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  277. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  278. };
  279. case RS::LIGHT_OMNI: {
  280. float r = light->param[RS::LIGHT_PARAM_RANGE];
  281. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  282. };
  283. case RS::LIGHT_DIRECTIONAL: {
  284. return AABB();
  285. };
  286. }
  287. ERR_FAIL_V(AABB());
  288. }
  289. /* LIGHT INSTANCE API */
  290. RID LightStorage::light_instance_create(RID p_light) {
  291. RID li = light_instance_owner.make_rid(LightInstance());
  292. LightInstance *light_instance = light_instance_owner.get_or_null(li);
  293. light_instance->self = li;
  294. light_instance->light = p_light;
  295. light_instance->light_type = light_get_type(p_light);
  296. return li;
  297. }
  298. void LightStorage::light_instance_free(RID p_light_instance) {
  299. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  300. ERR_FAIL_NULL(light_instance);
  301. // Remove from shadow atlases.
  302. for (const RID &E : light_instance->shadow_atlases) {
  303. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(E);
  304. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_light_instance));
  305. uint32_t key = shadow_atlas->shadow_owners[p_light_instance];
  306. uint32_t q = (key >> QUADRANT_SHIFT) & 0x3;
  307. uint32_t s = key & SHADOW_INDEX_MASK;
  308. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  309. shadow_atlas->shadow_owners.erase(p_light_instance);
  310. }
  311. light_instance_owner.free(p_light_instance);
  312. }
  313. void LightStorage::light_instance_set_transform(RID p_light_instance, const Transform3D &p_transform) {
  314. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  315. ERR_FAIL_NULL(light_instance);
  316. light_instance->transform = p_transform;
  317. }
  318. void LightStorage::light_instance_set_aabb(RID p_light_instance, const AABB &p_aabb) {
  319. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  320. ERR_FAIL_NULL(light_instance);
  321. light_instance->aabb = p_aabb;
  322. }
  323. void LightStorage::light_instance_set_shadow_transform(RID p_light_instance, const Projection &p_projection, const Transform3D &p_transform, float p_far, float p_split, int p_pass, float p_shadow_texel_size, float p_bias_scale, float p_range_begin, const Vector2 &p_uv_scale) {
  324. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  325. ERR_FAIL_NULL(light_instance);
  326. ERR_FAIL_INDEX(p_pass, 6);
  327. light_instance->shadow_transform[p_pass].camera = p_projection;
  328. light_instance->shadow_transform[p_pass].transform = p_transform;
  329. light_instance->shadow_transform[p_pass].farplane = p_far;
  330. light_instance->shadow_transform[p_pass].split = p_split;
  331. light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
  332. light_instance->shadow_transform[p_pass].range_begin = p_range_begin;
  333. light_instance->shadow_transform[p_pass].shadow_texel_size = p_shadow_texel_size;
  334. light_instance->shadow_transform[p_pass].uv_scale = p_uv_scale;
  335. }
  336. void LightStorage::light_instance_mark_visible(RID p_light_instance) {
  337. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  338. ERR_FAIL_NULL(light_instance);
  339. light_instance->last_scene_pass = RasterizerSceneGLES3::get_singleton()->get_scene_pass();
  340. }
  341. /* PROBE API */
  342. RID LightStorage::reflection_probe_allocate() {
  343. return reflection_probe_owner.allocate_rid();
  344. }
  345. void LightStorage::reflection_probe_initialize(RID p_rid) {
  346. ReflectionProbe probe;
  347. reflection_probe_owner.initialize_rid(p_rid, probe);
  348. }
  349. void LightStorage::reflection_probe_free(RID p_rid) {
  350. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_rid);
  351. reflection_probe->dependency.deleted_notify(p_rid);
  352. reflection_probe_owner.free(p_rid);
  353. }
  354. void LightStorage::reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) {
  355. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  356. ERR_FAIL_NULL(reflection_probe);
  357. reflection_probe->update_mode = p_mode;
  358. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  359. }
  360. void LightStorage::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  361. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  362. ERR_FAIL_NULL(reflection_probe);
  363. reflection_probe->intensity = p_intensity;
  364. }
  365. void LightStorage::reflection_probe_set_blend_distance(RID p_probe, float p_blend_distance) {
  366. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  367. ERR_FAIL_NULL(reflection_probe);
  368. reflection_probe->blend_distance = p_blend_distance;
  369. }
  370. void LightStorage::reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) {
  371. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  372. ERR_FAIL_NULL(reflection_probe);
  373. reflection_probe->ambient_mode = p_mode;
  374. }
  375. void LightStorage::reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) {
  376. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  377. ERR_FAIL_NULL(reflection_probe);
  378. reflection_probe->ambient_color = p_color;
  379. }
  380. void LightStorage::reflection_probe_set_ambient_energy(RID p_probe, float p_energy) {
  381. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  382. ERR_FAIL_NULL(reflection_probe);
  383. reflection_probe->ambient_color_energy = p_energy;
  384. }
  385. void LightStorage::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  386. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  387. ERR_FAIL_NULL(reflection_probe);
  388. reflection_probe->max_distance = p_distance;
  389. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  390. }
  391. void LightStorage::reflection_probe_set_size(RID p_probe, const Vector3 &p_size) {
  392. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  393. ERR_FAIL_NULL(reflection_probe);
  394. reflection_probe->size = p_size;
  395. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  396. }
  397. void LightStorage::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  398. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  399. ERR_FAIL_NULL(reflection_probe);
  400. reflection_probe->origin_offset = p_offset;
  401. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  402. }
  403. void LightStorage::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  404. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  405. ERR_FAIL_NULL(reflection_probe);
  406. reflection_probe->interior = p_enable;
  407. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  408. }
  409. void LightStorage::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  410. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  411. ERR_FAIL_NULL(reflection_probe);
  412. reflection_probe->box_projection = p_enable;
  413. }
  414. void LightStorage::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  415. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  416. ERR_FAIL_NULL(reflection_probe);
  417. reflection_probe->enable_shadows = p_enable;
  418. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  419. }
  420. void LightStorage::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  421. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  422. ERR_FAIL_NULL(reflection_probe);
  423. reflection_probe->cull_mask = p_layers;
  424. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  425. }
  426. void LightStorage::reflection_probe_set_reflection_mask(RID p_probe, uint32_t p_layers) {
  427. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  428. ERR_FAIL_NULL(reflection_probe);
  429. reflection_probe->reflection_mask = p_layers;
  430. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  431. }
  432. void LightStorage::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  433. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  434. ERR_FAIL_NULL(reflection_probe);
  435. reflection_probe->resolution = p_resolution;
  436. }
  437. AABB LightStorage::reflection_probe_get_aabb(RID p_probe) const {
  438. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  439. ERR_FAIL_NULL_V(reflection_probe, AABB());
  440. AABB aabb;
  441. aabb.position = -reflection_probe->size / 2;
  442. aabb.size = reflection_probe->size;
  443. return aabb;
  444. }
  445. RS::ReflectionProbeUpdateMode LightStorage::reflection_probe_get_update_mode(RID p_probe) const {
  446. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  447. ERR_FAIL_NULL_V(reflection_probe, RenderingServer::REFLECTION_PROBE_UPDATE_ONCE);
  448. return reflection_probe->update_mode;
  449. }
  450. uint32_t LightStorage::reflection_probe_get_cull_mask(RID p_probe) const {
  451. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  452. ERR_FAIL_NULL_V(reflection_probe, 0);
  453. return reflection_probe->cull_mask;
  454. }
  455. uint32_t LightStorage::reflection_probe_get_reflection_mask(RID p_probe) const {
  456. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  457. ERR_FAIL_NULL_V(reflection_probe, 0);
  458. return reflection_probe->reflection_mask;
  459. }
  460. Vector3 LightStorage::reflection_probe_get_size(RID p_probe) const {
  461. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  462. ERR_FAIL_NULL_V(reflection_probe, Vector3());
  463. return reflection_probe->size;
  464. }
  465. Vector3 LightStorage::reflection_probe_get_origin_offset(RID p_probe) const {
  466. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  467. ERR_FAIL_NULL_V(reflection_probe, Vector3());
  468. return reflection_probe->origin_offset;
  469. }
  470. float LightStorage::reflection_probe_get_origin_max_distance(RID p_probe) const {
  471. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  472. ERR_FAIL_NULL_V(reflection_probe, 0.0);
  473. return reflection_probe->max_distance;
  474. }
  475. bool LightStorage::reflection_probe_renders_shadows(RID p_probe) const {
  476. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  477. ERR_FAIL_NULL_V(reflection_probe, false);
  478. return reflection_probe->enable_shadows;
  479. }
  480. void LightStorage::reflection_probe_set_mesh_lod_threshold(RID p_probe, float p_ratio) {
  481. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  482. ERR_FAIL_NULL(reflection_probe);
  483. reflection_probe->mesh_lod_threshold = p_ratio;
  484. reflection_probe->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_REFLECTION_PROBE);
  485. }
  486. float LightStorage::reflection_probe_get_mesh_lod_threshold(RID p_probe) const {
  487. const ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  488. ERR_FAIL_NULL_V(reflection_probe, 0.0);
  489. return reflection_probe->mesh_lod_threshold;
  490. }
  491. Dependency *LightStorage::reflection_probe_get_dependency(RID p_probe) const {
  492. ReflectionProbe *reflection_probe = reflection_probe_owner.get_or_null(p_probe);
  493. ERR_FAIL_NULL_V(reflection_probe, nullptr);
  494. return &reflection_probe->dependency;
  495. }
  496. /* REFLECTION ATLAS */
  497. RID LightStorage::reflection_atlas_create() {
  498. ReflectionAtlas ra;
  499. ra.count = GLOBAL_GET("rendering/reflections/reflection_atlas/reflection_count");
  500. ra.size = GLOBAL_GET("rendering/reflections/reflection_atlas/reflection_size");
  501. return reflection_atlas_owner.make_rid(ra);
  502. }
  503. void LightStorage::reflection_atlas_free(RID p_ref_atlas) {
  504. reflection_atlas_set_size(p_ref_atlas, 0, 0);
  505. reflection_atlas_owner.free(p_ref_atlas);
  506. }
  507. int LightStorage::reflection_atlas_get_size(RID p_ref_atlas) const {
  508. ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(p_ref_atlas);
  509. ERR_FAIL_NULL_V(ra, 0);
  510. return ra->size;
  511. }
  512. void LightStorage::reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) {
  513. ReflectionAtlas *ra = reflection_atlas_owner.get_or_null(p_ref_atlas);
  514. ERR_FAIL_NULL(ra);
  515. if (ra->size == p_reflection_size && ra->count == p_reflection_count) {
  516. return; //no changes
  517. }
  518. ra->size = p_reflection_size;
  519. ra->count = p_reflection_count;
  520. if (ra->depth != 0) {
  521. //clear and invalidate everything
  522. for (int i = 0; i < ra->reflections.size(); i++) {
  523. for (int j = 0; j < 7; j++) {
  524. if (ra->reflections[i].fbos[j] != 0) {
  525. glDeleteFramebuffers(1, &ra->reflections[i].fbos[j]);
  526. ra->reflections.write[i].fbos[j] = 0;
  527. }
  528. }
  529. GLES3::Utilities::get_singleton()->texture_free_data(ra->reflections[i].color);
  530. ra->reflections.write[i].color = 0;
  531. GLES3::Utilities::get_singleton()->texture_free_data(ra->reflections[i].radiance);
  532. ra->reflections.write[i].radiance = 0;
  533. if (ra->reflections[i].owner.is_null()) {
  534. continue;
  535. }
  536. reflection_probe_release_atlas_index(ra->reflections[i].owner);
  537. //rp->atlasindex clear
  538. }
  539. ra->reflections.clear();
  540. GLES3::Utilities::get_singleton()->texture_free_data(ra->depth);
  541. ra->depth = 0;
  542. }
  543. if (ra->render_buffers.is_valid()) {
  544. ra->render_buffers->free_render_buffer_data();
  545. }
  546. }
  547. /* REFLECTION PROBE INSTANCE */
  548. RID LightStorage::reflection_probe_instance_create(RID p_probe) {
  549. ReflectionProbeInstance rpi;
  550. rpi.probe = p_probe;
  551. return reflection_probe_instance_owner.make_rid(rpi);
  552. }
  553. void LightStorage::reflection_probe_instance_free(RID p_instance) {
  554. reflection_probe_release_atlas_index(p_instance);
  555. reflection_probe_instance_owner.free(p_instance);
  556. }
  557. void LightStorage::reflection_probe_instance_set_transform(RID p_instance, const Transform3D &p_transform) {
  558. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  559. ERR_FAIL_NULL(rpi);
  560. rpi->transform = p_transform;
  561. rpi->dirty = true;
  562. }
  563. bool LightStorage::reflection_probe_has_atlas_index(RID p_instance) {
  564. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  565. ERR_FAIL_NULL_V(rpi, false);
  566. if (rpi->atlas.is_null()) {
  567. return false;
  568. }
  569. return rpi->atlas_index >= 0;
  570. }
  571. void LightStorage::reflection_probe_release_atlas_index(RID p_instance) {
  572. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  573. ERR_FAIL_NULL(rpi);
  574. if (rpi->atlas.is_null()) {
  575. return; //nothing to release
  576. }
  577. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
  578. ERR_FAIL_NULL(atlas);
  579. ERR_FAIL_INDEX(rpi->atlas_index, atlas->reflections.size());
  580. atlas->reflections.write[rpi->atlas_index].owner = RID();
  581. if (rpi->rendering) {
  582. // We were cancelled mid rendering, trigger refresh.
  583. rpi->rendering = false;
  584. rpi->dirty = true;
  585. rpi->processing_layer = 0;
  586. }
  587. rpi->atlas_index = -1;
  588. rpi->atlas = RID();
  589. }
  590. bool LightStorage::reflection_probe_instance_needs_redraw(RID p_instance) {
  591. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  592. ERR_FAIL_NULL_V(rpi, false);
  593. if (rpi->rendering) {
  594. return false;
  595. }
  596. if (rpi->dirty) {
  597. return true;
  598. }
  599. if (reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  600. return true;
  601. }
  602. return rpi->atlas_index == -1;
  603. }
  604. bool LightStorage::reflection_probe_instance_has_reflection(RID p_instance) {
  605. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  606. ERR_FAIL_NULL_V(rpi, false);
  607. return rpi->atlas.is_valid();
  608. }
  609. bool LightStorage::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
  610. TextureStorage *texture_storage = TextureStorage::get_singleton();
  611. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(p_reflection_atlas);
  612. ERR_FAIL_NULL_V(atlas, false);
  613. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  614. ERR_FAIL_NULL_V(rpi, false);
  615. if (atlas->render_buffers.is_null()) {
  616. atlas->render_buffers.instantiate();
  617. atlas->render_buffers->configure_for_probe(Size2i(atlas->size, atlas->size));
  618. }
  619. // First we check if our atlas is initialized.
  620. // Not making an exception for update_mode = REFLECTION_PROBE_UPDATE_ALWAYS, we are using
  621. // the same render techniques regardless of realtime or update once (for now).
  622. if (atlas->depth == 0) {
  623. // We need to create our textures
  624. atlas->mipmap_count = Image::get_image_required_mipmaps(atlas->size, atlas->size, Image::FORMAT_RGBAH) - 1;
  625. atlas->mipmap_count = MIN(atlas->mipmap_count, 8); // No more than 8 please..
  626. glActiveTexture(GL_TEXTURE0);
  627. {
  628. // We create one set of 6 layers for depth, we can reuse this when rendering.
  629. glGenTextures(1, &atlas->depth);
  630. glBindTexture(GL_TEXTURE_2D_ARRAY, atlas->depth);
  631. glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_DEPTH_COMPONENT24, atlas->size, atlas->size, 6, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  632. GLES3::Utilities::get_singleton()->texture_allocated_data(atlas->depth, atlas->size * atlas->size * 6 * 3, "Reflection probe atlas (depth)");
  633. }
  634. // Make room for our atlas entries
  635. atlas->reflections.resize(atlas->count);
  636. for (int i = 0; i < atlas->count; i++) {
  637. // Create a cube map for this atlas entry
  638. GLuint color = 0;
  639. glGenTextures(1, &color);
  640. glBindTexture(GL_TEXTURE_CUBE_MAP, color);
  641. atlas->reflections.write[i].color = color;
  642. #ifdef GL_API_ENABLED
  643. if (RasterizerGLES3::is_gles_over_gl()) {
  644. for (int s = 0; s < 6; s++) {
  645. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + s, 0, GL_RGB10_A2, atlas->size, atlas->size, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
  646. }
  647. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  648. }
  649. #endif
  650. #ifdef GLES_API_ENABLED
  651. if (!RasterizerGLES3::is_gles_over_gl()) {
  652. glTexStorage2D(GL_TEXTURE_CUBE_MAP, atlas->mipmap_count, GL_RGB10_A2, atlas->size, atlas->size);
  653. }
  654. #endif // GLES_API_ENABLED
  655. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  656. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  657. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  658. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  659. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
  660. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, atlas->mipmap_count - 1);
  661. // Setup sizes and calculate how much memory we're using.
  662. int mipmap_size = atlas->size;
  663. uint32_t data_size = 0;
  664. for (int m = 0; m < atlas->mipmap_count; m++) {
  665. atlas->mipmap_size[m] = mipmap_size;
  666. data_size += mipmap_size * mipmap_size * 6 * 4;
  667. mipmap_size = MAX(mipmap_size >> 1, 1);
  668. }
  669. GLES3::Utilities::get_singleton()->texture_allocated_data(color, data_size, String("Reflection probe atlas (") + String::num_int64(i) + String(", color)"));
  670. // Create a radiance map for this atlas entry
  671. GLuint radiance = 0;
  672. glGenTextures(1, &radiance);
  673. glBindTexture(GL_TEXTURE_CUBE_MAP, radiance);
  674. atlas->reflections.write[i].radiance = radiance;
  675. #ifdef GL_API_ENABLED
  676. if (RasterizerGLES3::is_gles_over_gl()) {
  677. for (int s = 0; s < 6; s++) {
  678. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + s, 0, GL_RGB10_A2, atlas->size, atlas->size, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
  679. }
  680. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  681. }
  682. #endif
  683. #ifdef GLES_API_ENABLED
  684. if (!RasterizerGLES3::is_gles_over_gl()) {
  685. glTexStorage2D(GL_TEXTURE_CUBE_MAP, atlas->mipmap_count, GL_RGB10_A2, atlas->size, atlas->size);
  686. }
  687. #endif // GLES_API_ENABLED
  688. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  689. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  690. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  691. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  692. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
  693. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, atlas->mipmap_count - 1);
  694. // Same data size as our color buffer
  695. GLES3::Utilities::get_singleton()->texture_allocated_data(radiance, data_size, String("Reflection probe atlas (") + String::num_int64(i) + String(", radiance)"));
  696. // Create our framebuffers so we can draw to all sides
  697. for (int side = 0; side < 6; side++) {
  698. GLuint fbo = 0;
  699. glGenFramebuffers(1, &fbo);
  700. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  701. // We use glFramebufferTexture2D for the color buffer as glFramebufferTextureLayer doesn't always work with cubemaps.
  702. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + side, color, 0);
  703. glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, atlas->depth, 0, side);
  704. // Validate framebuffer
  705. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  706. if (status != GL_FRAMEBUFFER_COMPLETE) {
  707. WARN_PRINT("Could not create reflections framebuffer, status: " + texture_storage->get_framebuffer_error(status));
  708. }
  709. atlas->reflections.write[i].fbos[side] = fbo;
  710. }
  711. // Create an extra framebuffer for building our radiance
  712. {
  713. GLuint fbo = 0;
  714. glGenFramebuffers(1, &fbo);
  715. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  716. atlas->reflections.write[i].fbos[6] = fbo;
  717. }
  718. }
  719. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  720. glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
  721. glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
  722. }
  723. // Then we find a free slot for our reflection probe
  724. if (rpi->atlas_index == -1) {
  725. for (int i = 0; i < atlas->reflections.size(); i++) {
  726. if (atlas->reflections[i].owner.is_null()) {
  727. rpi->atlas_index = i;
  728. break;
  729. }
  730. }
  731. //find the one used last
  732. if (rpi->atlas_index == -1) {
  733. //everything is in use, find the one least used via LRU
  734. uint64_t pass_min = 0;
  735. for (int i = 0; i < atlas->reflections.size(); i++) {
  736. ReflectionProbeInstance *rpi2 = reflection_probe_instance_owner.get_or_null(atlas->reflections[i].owner);
  737. if (rpi2->last_pass < pass_min) {
  738. pass_min = rpi2->last_pass;
  739. rpi->atlas_index = i;
  740. }
  741. }
  742. }
  743. }
  744. if (rpi->atlas_index != -1) { // should we fail if this is still -1 ?
  745. atlas->reflections.write[rpi->atlas_index].owner = p_instance;
  746. }
  747. rpi->atlas = p_reflection_atlas;
  748. rpi->rendering = true;
  749. rpi->dirty = false;
  750. rpi->processing_layer = 0;
  751. return true;
  752. }
  753. Ref<RenderSceneBuffers> LightStorage::reflection_probe_atlas_get_render_buffers(RID p_reflection_atlas) {
  754. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(p_reflection_atlas);
  755. ERR_FAIL_NULL_V(atlas, Ref<RenderSceneBuffersGLES3>());
  756. return atlas->render_buffers;
  757. }
  758. bool LightStorage::reflection_probe_instance_postprocess_step(RID p_instance) {
  759. GLES3::CubemapFilter *cubemap_filter = GLES3::CubemapFilter::get_singleton();
  760. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  761. ERR_FAIL_NULL_V(rpi, false);
  762. ERR_FAIL_COND_V(!rpi->rendering, false);
  763. ERR_FAIL_COND_V(rpi->atlas.is_null(), false);
  764. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
  765. if (!atlas || rpi->atlas_index == -1) {
  766. //does not belong to an atlas anymore, cancel (was removed from atlas or atlas changed while rendering)
  767. rpi->rendering = false;
  768. rpi->processing_layer = 0;
  769. return false;
  770. }
  771. if (LightStorage::get_singleton()->reflection_probe_get_update_mode(rpi->probe) == RS::REFLECTION_PROBE_UPDATE_ALWAYS) {
  772. // Using real time reflections, all roughness is done in one step
  773. for (int m = 0; m < atlas->mipmap_count; m++) {
  774. const GLES3::ReflectionAtlas::Reflection &reflection = atlas->reflections[rpi->atlas_index];
  775. cubemap_filter->filter_radiance(reflection.color, reflection.radiance, reflection.fbos[6], atlas->size, atlas->mipmap_count, m);
  776. }
  777. rpi->rendering = false;
  778. rpi->processing_layer = 0;
  779. return true;
  780. } else {
  781. const GLES3::ReflectionAtlas::Reflection &reflection = atlas->reflections[rpi->atlas_index];
  782. cubemap_filter->filter_radiance(reflection.color, reflection.radiance, reflection.fbos[6], atlas->size, atlas->mipmap_count, rpi->processing_layer);
  783. rpi->processing_layer++;
  784. if (rpi->processing_layer == atlas->mipmap_count) {
  785. rpi->rendering = false;
  786. rpi->processing_layer = 0;
  787. return true;
  788. }
  789. }
  790. return false;
  791. }
  792. GLuint LightStorage::reflection_probe_instance_get_texture(RID p_instance) {
  793. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  794. ERR_FAIL_NULL_V(rpi, 0);
  795. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
  796. ERR_FAIL_NULL_V(atlas, 0);
  797. ERR_FAIL_COND_V(rpi->atlas_index < 0, 0);
  798. return atlas->reflections[rpi->atlas_index].radiance;
  799. }
  800. GLuint LightStorage::reflection_probe_instance_get_framebuffer(RID p_instance, int p_index) {
  801. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.get_or_null(p_instance);
  802. ERR_FAIL_NULL_V(rpi, 0);
  803. ERR_FAIL_INDEX_V(p_index, 6, 0);
  804. ReflectionAtlas *atlas = reflection_atlas_owner.get_or_null(rpi->atlas);
  805. ERR_FAIL_NULL_V(atlas, 0);
  806. ERR_FAIL_COND_V(rpi->atlas_index < 0, 0);
  807. return atlas->reflections[rpi->atlas_index].fbos[p_index];
  808. }
  809. /* LIGHTMAP CAPTURE */
  810. RID LightStorage::lightmap_allocate() {
  811. return lightmap_owner.allocate_rid();
  812. }
  813. void LightStorage::lightmap_initialize(RID p_rid) {
  814. lightmap_owner.initialize_rid(p_rid, Lightmap());
  815. }
  816. void LightStorage::lightmap_free(RID p_rid) {
  817. Lightmap *lightmap = lightmap_owner.get_or_null(p_rid);
  818. ERR_FAIL_NULL(lightmap);
  819. lightmap->dependency.deleted_notify(p_rid);
  820. lightmap_owner.free(p_rid);
  821. }
  822. void LightStorage::lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) {
  823. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  824. ERR_FAIL_NULL(lightmap);
  825. lightmap->light_texture = p_light;
  826. lightmap->uses_spherical_harmonics = p_uses_spherical_haromics;
  827. Vector3i light_texture_size = GLES3::TextureStorage::get_singleton()->texture_get_size(lightmap->light_texture);
  828. lightmap->light_texture_size = Vector2i(light_texture_size.x, light_texture_size.y);
  829. GLuint tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(lightmap->light_texture);
  830. glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
  831. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  832. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  833. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  834. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  835. glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
  836. }
  837. void LightStorage::lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) {
  838. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  839. ERR_FAIL_NULL(lightmap);
  840. lightmap->bounds = p_bounds;
  841. }
  842. void LightStorage::lightmap_set_probe_interior(RID p_lightmap, bool p_interior) {
  843. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  844. ERR_FAIL_NULL(lightmap);
  845. lightmap->interior = p_interior;
  846. }
  847. void LightStorage::lightmap_set_probe_capture_data(RID p_lightmap, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree) {
  848. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  849. ERR_FAIL_NULL(lightmap);
  850. if (p_points.size()) {
  851. ERR_FAIL_COND(p_points.size() * 9 != p_point_sh.size());
  852. ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
  853. ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
  854. }
  855. lightmap->points = p_points;
  856. lightmap->point_sh = p_point_sh;
  857. lightmap->tetrahedra = p_tetrahedra;
  858. lightmap->bsp_tree = p_bsp_tree;
  859. }
  860. void LightStorage::lightmap_set_baked_exposure_normalization(RID p_lightmap, float p_exposure) {
  861. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  862. ERR_FAIL_NULL(lightmap);
  863. lightmap->baked_exposure = p_exposure;
  864. }
  865. PackedVector3Array LightStorage::lightmap_get_probe_capture_points(RID p_lightmap) const {
  866. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  867. ERR_FAIL_NULL_V(lightmap, PackedVector3Array());
  868. return lightmap->points;
  869. }
  870. PackedColorArray LightStorage::lightmap_get_probe_capture_sh(RID p_lightmap) const {
  871. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  872. ERR_FAIL_NULL_V(lightmap, PackedColorArray());
  873. return lightmap->point_sh;
  874. }
  875. PackedInt32Array LightStorage::lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const {
  876. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  877. ERR_FAIL_NULL_V(lightmap, PackedInt32Array());
  878. return lightmap->tetrahedra;
  879. }
  880. PackedInt32Array LightStorage::lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const {
  881. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  882. ERR_FAIL_NULL_V(lightmap, PackedInt32Array());
  883. return lightmap->bsp_tree;
  884. }
  885. AABB LightStorage::lightmap_get_aabb(RID p_lightmap) const {
  886. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  887. ERR_FAIL_NULL_V(lightmap, AABB());
  888. return lightmap->bounds;
  889. }
  890. void LightStorage::lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) {
  891. Lightmap *lm = lightmap_owner.get_or_null(p_lightmap);
  892. ERR_FAIL_NULL(lm);
  893. for (int i = 0; i < 9; i++) {
  894. r_sh[i] = Color(0, 0, 0, 0);
  895. }
  896. if (!lm->points.size() || !lm->bsp_tree.size() || !lm->tetrahedra.size()) {
  897. return;
  898. }
  899. static_assert(sizeof(Lightmap::BSP) == 24);
  900. const Lightmap::BSP *bsp = (const Lightmap::BSP *)lm->bsp_tree.ptr();
  901. int32_t node = 0;
  902. while (node >= 0) {
  903. if (Plane(bsp[node].plane[0], bsp[node].plane[1], bsp[node].plane[2], bsp[node].plane[3]).is_point_over(p_point)) {
  904. #ifdef DEBUG_ENABLED
  905. ERR_FAIL_COND(bsp[node].over >= 0 && bsp[node].over < node);
  906. #endif
  907. node = bsp[node].over;
  908. } else {
  909. #ifdef DEBUG_ENABLED
  910. ERR_FAIL_COND(bsp[node].under >= 0 && bsp[node].under < node);
  911. #endif
  912. node = bsp[node].under;
  913. }
  914. }
  915. if (node == Lightmap::BSP::EMPTY_LEAF) {
  916. return; // Nothing could be done.
  917. }
  918. node = Math::abs(node) - 1;
  919. uint32_t *tetrahedron = (uint32_t *)&lm->tetrahedra[node * 4];
  920. Vector3 points[4] = { lm->points[tetrahedron[0]], lm->points[tetrahedron[1]], lm->points[tetrahedron[2]], lm->points[tetrahedron[3]] };
  921. const Color *sh_colors[4]{ &lm->point_sh[tetrahedron[0] * 9], &lm->point_sh[tetrahedron[1] * 9], &lm->point_sh[tetrahedron[2] * 9], &lm->point_sh[tetrahedron[3] * 9] };
  922. Color barycentric = Geometry3D::tetrahedron_get_barycentric_coords(points[0], points[1], points[2], points[3], p_point);
  923. for (int i = 0; i < 4; i++) {
  924. float c = CLAMP(barycentric[i], 0.0, 1.0);
  925. for (int j = 0; j < 9; j++) {
  926. r_sh[j] += sh_colors[i][j] * c;
  927. }
  928. }
  929. }
  930. bool LightStorage::lightmap_is_interior(RID p_lightmap) const {
  931. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  932. ERR_FAIL_NULL_V(lightmap, false);
  933. return lightmap->interior;
  934. }
  935. void LightStorage::lightmap_set_probe_capture_update_speed(float p_speed) {
  936. lightmap_probe_capture_update_speed = p_speed;
  937. }
  938. float LightStorage::lightmap_get_probe_capture_update_speed() const {
  939. return lightmap_probe_capture_update_speed;
  940. }
  941. void LightStorage::lightmap_set_shadowmask_textures(RID p_lightmap, RID p_shadow) {
  942. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  943. ERR_FAIL_NULL(lightmap);
  944. lightmap->shadow_texture = p_shadow;
  945. GLuint tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(lightmap->shadow_texture);
  946. glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
  947. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  948. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  949. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  950. glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  951. glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
  952. }
  953. RS::ShadowmaskMode LightStorage::lightmap_get_shadowmask_mode(RID p_lightmap) {
  954. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  955. ERR_FAIL_NULL_V(lightmap, RS::SHADOWMASK_MODE_NONE);
  956. return lightmap->shadowmask_mode;
  957. }
  958. void LightStorage::lightmap_set_shadowmask_mode(RID p_lightmap, RS::ShadowmaskMode p_mode) {
  959. Lightmap *lightmap = lightmap_owner.get_or_null(p_lightmap);
  960. ERR_FAIL_NULL(lightmap);
  961. lightmap->shadowmask_mode = p_mode;
  962. }
  963. /* LIGHTMAP INSTANCE */
  964. RID LightStorage::lightmap_instance_create(RID p_lightmap) {
  965. LightmapInstance li;
  966. li.lightmap = p_lightmap;
  967. return lightmap_instance_owner.make_rid(li);
  968. }
  969. void LightStorage::lightmap_instance_free(RID p_lightmap) {
  970. lightmap_instance_owner.free(p_lightmap);
  971. }
  972. void LightStorage::lightmap_instance_set_transform(RID p_lightmap, const Transform3D &p_transform) {
  973. LightmapInstance *li = lightmap_instance_owner.get_or_null(p_lightmap);
  974. ERR_FAIL_NULL(li);
  975. li->transform = p_transform;
  976. }
  977. /* SHADOW ATLAS API */
  978. RID LightStorage::shadow_atlas_create() {
  979. return shadow_atlas_owner.make_rid(ShadowAtlas());
  980. }
  981. void LightStorage::shadow_atlas_free(RID p_atlas) {
  982. shadow_atlas_set_size(p_atlas, 0);
  983. shadow_atlas_owner.free(p_atlas);
  984. }
  985. void LightStorage::shadow_atlas_set_size(RID p_atlas, int p_size, bool p_16_bits) {
  986. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
  987. ERR_FAIL_NULL(shadow_atlas);
  988. ERR_FAIL_COND(p_size < 0);
  989. p_size = next_power_of_2((uint32_t)p_size);
  990. if (p_size == shadow_atlas->size && p_16_bits == shadow_atlas->use_16_bits) {
  991. return;
  992. }
  993. for (uint32_t i = 0; i < 4; i++) {
  994. // Clear all subdivisions and free shadows.
  995. for (uint32_t j = 0; j < shadow_atlas->quadrants[i].textures.size(); j++) {
  996. glDeleteTextures(1, &shadow_atlas->quadrants[i].textures[j]);
  997. glDeleteFramebuffers(1, &shadow_atlas->quadrants[i].fbos[j]);
  998. }
  999. shadow_atlas->quadrants[i].textures.clear();
  1000. shadow_atlas->quadrants[i].fbos.clear();
  1001. shadow_atlas->quadrants[i].shadows.clear();
  1002. shadow_atlas->quadrants[i].shadows.resize(shadow_atlas->quadrants[i].subdivision * shadow_atlas->quadrants[i].subdivision);
  1003. }
  1004. // Erase shadow atlas reference from lights.
  1005. for (const KeyValue<RID, uint32_t> &E : shadow_atlas->shadow_owners) {
  1006. LightInstance *li = light_instance_owner.get_or_null(E.key);
  1007. ERR_CONTINUE(!li);
  1008. li->shadow_atlases.erase(p_atlas);
  1009. }
  1010. if (shadow_atlas->debug_texture != 0) {
  1011. glDeleteTextures(1, &shadow_atlas->debug_texture);
  1012. }
  1013. if (shadow_atlas->debug_fbo != 0) {
  1014. glDeleteFramebuffers(1, &shadow_atlas->debug_fbo);
  1015. }
  1016. // Clear owners.
  1017. shadow_atlas->shadow_owners.clear();
  1018. shadow_atlas->size = p_size;
  1019. shadow_atlas->use_16_bits = p_16_bits;
  1020. }
  1021. void LightStorage::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
  1022. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
  1023. ERR_FAIL_NULL(shadow_atlas);
  1024. ERR_FAIL_INDEX(p_quadrant, 4);
  1025. ERR_FAIL_INDEX(p_subdivision, 16384);
  1026. uint32_t subdiv = next_power_of_2((uint32_t)p_subdivision);
  1027. if (subdiv & 0xaaaaaaaa) { // sqrt(subdiv) must be integer.
  1028. subdiv <<= 1;
  1029. }
  1030. subdiv = int(Math::sqrt((float)subdiv));
  1031. if (shadow_atlas->quadrants[p_quadrant].subdivision == subdiv) {
  1032. return;
  1033. }
  1034. // Erase all data from quadrant.
  1035. for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
  1036. if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
  1037. shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  1038. LightInstance *li = light_instance_owner.get_or_null(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  1039. ERR_CONTINUE(!li);
  1040. li->shadow_atlases.erase(p_atlas);
  1041. }
  1042. }
  1043. for (uint32_t j = 0; j < shadow_atlas->quadrants[p_quadrant].textures.size(); j++) {
  1044. glDeleteTextures(1, &shadow_atlas->quadrants[p_quadrant].textures[j]);
  1045. glDeleteFramebuffers(1, &shadow_atlas->quadrants[p_quadrant].fbos[j]);
  1046. }
  1047. shadow_atlas->quadrants[p_quadrant].textures.clear();
  1048. shadow_atlas->quadrants[p_quadrant].fbos.clear();
  1049. shadow_atlas->quadrants[p_quadrant].shadows.clear();
  1050. shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv * subdiv);
  1051. shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
  1052. // Cache the smallest subdiv (for faster allocation in light update).
  1053. shadow_atlas->smallest_subdiv = 1 << 30;
  1054. for (int i = 0; i < 4; i++) {
  1055. if (shadow_atlas->quadrants[i].subdivision) {
  1056. shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
  1057. }
  1058. }
  1059. if (shadow_atlas->smallest_subdiv == 1 << 30) {
  1060. shadow_atlas->smallest_subdiv = 0;
  1061. }
  1062. // Re-sort the size orders, simple bubblesort for 4 elements.
  1063. int swaps = 0;
  1064. do {
  1065. swaps = 0;
  1066. for (int i = 0; i < 3; i++) {
  1067. if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
  1068. SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
  1069. swaps++;
  1070. }
  1071. }
  1072. } while (swaps > 0);
  1073. }
  1074. bool LightStorage::shadow_atlas_update_light(RID p_atlas, RID p_light_instance, float p_coverage, uint64_t p_light_version) {
  1075. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get_or_null(p_atlas);
  1076. ERR_FAIL_NULL_V(shadow_atlas, false);
  1077. LightInstance *li = light_instance_owner.get_or_null(p_light_instance);
  1078. ERR_FAIL_NULL_V(li, false);
  1079. if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
  1080. return false;
  1081. }
  1082. uint32_t quad_size = shadow_atlas->size >> 1;
  1083. int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(uint32_t(quad_size * p_coverage)));
  1084. int valid_quadrants[4];
  1085. int valid_quadrant_count = 0;
  1086. int best_size = -1; // Best size found.
  1087. int best_subdiv = -1; // Subdiv for the best size.
  1088. // Find the quadrants this fits into, and the best possible size it can fit into.
  1089. for (int i = 0; i < 4; i++) {
  1090. int q = shadow_atlas->size_order[i];
  1091. int sd = shadow_atlas->quadrants[q].subdivision;
  1092. if (sd == 0) {
  1093. continue; // Unused.
  1094. }
  1095. int max_fit = quad_size / sd;
  1096. if (best_size != -1 && max_fit > best_size) {
  1097. break; // Too large.
  1098. }
  1099. valid_quadrants[valid_quadrant_count++] = q;
  1100. best_subdiv = sd;
  1101. if (max_fit >= desired_fit) {
  1102. best_size = max_fit;
  1103. }
  1104. }
  1105. ERR_FAIL_COND_V(valid_quadrant_count == 0, false);
  1106. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  1107. uint32_t old_key = SHADOW_INVALID;
  1108. uint32_t old_quadrant = SHADOW_INVALID;
  1109. uint32_t old_shadow = SHADOW_INVALID;
  1110. int old_subdivision = -1;
  1111. bool should_realloc = false;
  1112. bool should_redraw = false;
  1113. if (shadow_atlas->shadow_owners.has(p_light_instance)) {
  1114. old_key = shadow_atlas->shadow_owners[p_light_instance];
  1115. old_quadrant = (old_key >> QUADRANT_SHIFT) & 0x3;
  1116. old_shadow = old_key & SHADOW_INDEX_MASK;
  1117. // Only re-allocate if a better option is available, and enough time has passed.
  1118. should_realloc = shadow_atlas->quadrants[old_quadrant].subdivision != (uint32_t)best_subdiv && (tick - shadow_atlas->quadrants[old_quadrant].shadows[old_shadow].alloc_tick > shadow_atlas_realloc_tolerance_msec);
  1119. should_redraw = shadow_atlas->quadrants[old_quadrant].shadows[old_shadow].version != p_light_version;
  1120. if (!should_realloc) {
  1121. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].version = p_light_version;
  1122. // Already existing, see if it should redraw or it's just OK.
  1123. return should_redraw;
  1124. }
  1125. old_subdivision = shadow_atlas->quadrants[old_quadrant].subdivision;
  1126. }
  1127. bool is_omni = li->light_type == RS::LIGHT_OMNI;
  1128. bool found_shadow = false;
  1129. int new_quadrant = -1;
  1130. int new_shadow = -1;
  1131. found_shadow = _shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, old_subdivision, tick, is_omni, new_quadrant, new_shadow);
  1132. // For new shadows if we found an atlas.
  1133. // Or for existing shadows that found a better atlas.
  1134. if (found_shadow) {
  1135. if (old_quadrant != SHADOW_INVALID) {
  1136. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].version = 0;
  1137. shadow_atlas->quadrants[old_quadrant].shadows.write[old_shadow].owner = RID();
  1138. }
  1139. uint32_t new_key = new_quadrant << QUADRANT_SHIFT;
  1140. new_key |= new_shadow;
  1141. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  1142. _shadow_atlas_invalidate_shadow(sh, p_atlas, shadow_atlas, new_quadrant, new_shadow);
  1143. sh->owner = p_light_instance;
  1144. sh->owner_is_omni = is_omni;
  1145. sh->alloc_tick = tick;
  1146. sh->version = p_light_version;
  1147. li->shadow_atlases.insert(p_atlas);
  1148. // Update it in map.
  1149. shadow_atlas->shadow_owners[p_light_instance] = new_key;
  1150. // Make it dirty, as it should redraw anyway.
  1151. return true;
  1152. }
  1153. return should_redraw;
  1154. }
  1155. bool LightStorage::_shadow_atlas_find_shadow(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, bool is_omni, int &r_quadrant, int &r_shadow) {
  1156. for (int i = p_quadrant_count - 1; i >= 0; i--) {
  1157. int qidx = p_in_quadrants[i];
  1158. if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
  1159. return false;
  1160. }
  1161. // Look for an empty space.
  1162. int sc = shadow_atlas->quadrants[qidx].shadows.size();
  1163. const ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptr();
  1164. // We have a free space in this quadrant, allocate a texture and use it.
  1165. if (sc > (int)shadow_atlas->quadrants[qidx].textures.size()) {
  1166. GLuint fbo_id = 0;
  1167. glGenFramebuffers(1, &fbo_id);
  1168. glBindFramebuffer(GL_FRAMEBUFFER, fbo_id);
  1169. GLuint texture_id = 0;
  1170. glGenTextures(1, &texture_id);
  1171. glActiveTexture(GL_TEXTURE0);
  1172. int size = (shadow_atlas->size >> 1) / shadow_atlas->quadrants[qidx].subdivision;
  1173. GLenum format = shadow_atlas->use_16_bits ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24;
  1174. GLenum type = shadow_atlas->use_16_bits ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
  1175. if (is_omni) {
  1176. glBindTexture(GL_TEXTURE_CUBE_MAP, texture_id);
  1177. for (int id = 0; id < 6; id++) {
  1178. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + id, 0, format, size / 2, size / 2, 0, GL_DEPTH_COMPONENT, type, nullptr);
  1179. }
  1180. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1181. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1182. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1183. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1184. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  1185. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  1186. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
  1187. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X, texture_id, 0);
  1188. #ifdef DEBUG_ENABLED
  1189. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1190. if (status != GL_FRAMEBUFFER_COMPLETE) {
  1191. ERR_PRINT("Could not create omni light shadow framebuffer, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
  1192. }
  1193. #endif
  1194. glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
  1195. } else {
  1196. glBindTexture(GL_TEXTURE_2D, texture_id);
  1197. glTexImage2D(GL_TEXTURE_2D, 0, format, size, size, 0, GL_DEPTH_COMPONENT, type, nullptr);
  1198. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1199. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1200. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1201. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1202. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  1203. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
  1204. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture_id, 0);
  1205. glBindTexture(GL_TEXTURE_2D, 0);
  1206. }
  1207. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  1208. r_quadrant = qidx;
  1209. r_shadow = shadow_atlas->quadrants[qidx].textures.size();
  1210. shadow_atlas->quadrants[qidx].textures.push_back(texture_id);
  1211. shadow_atlas->quadrants[qidx].fbos.push_back(fbo_id);
  1212. return true;
  1213. }
  1214. int found_used_idx = -1; // Found existing one, must steal it.
  1215. uint64_t min_pass = 0; // Pass of the existing one, try to use the least recently used one (LRU fashion).
  1216. for (int j = 0; j < sc; j++) {
  1217. if (sarr[j].owner_is_omni != is_omni) {
  1218. // Existing light instance type doesn't match new light instance type skip.
  1219. continue;
  1220. }
  1221. LightInstance *sli = light_instance_owner.get_or_null(sarr[j].owner);
  1222. if (!sli) {
  1223. // Found a released light instance.
  1224. found_used_idx = j;
  1225. break;
  1226. }
  1227. if (sli->last_scene_pass != RasterizerSceneGLES3::get_singleton()->get_scene_pass()) {
  1228. // Was just allocated, don't kill it so soon, wait a bit.
  1229. if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
  1230. continue;
  1231. }
  1232. if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
  1233. found_used_idx = j;
  1234. min_pass = sli->last_scene_pass;
  1235. }
  1236. }
  1237. }
  1238. if (found_used_idx != -1) {
  1239. r_quadrant = qidx;
  1240. r_shadow = found_used_idx;
  1241. return true;
  1242. }
  1243. }
  1244. return false;
  1245. }
  1246. void LightStorage::_shadow_atlas_invalidate_shadow(ShadowAtlas::Quadrant::Shadow *p_shadow, RID p_atlas, ShadowAtlas *p_shadow_atlas, uint32_t p_quadrant, uint32_t p_shadow_idx) {
  1247. if (p_shadow->owner.is_valid()) {
  1248. LightInstance *sli = light_instance_owner.get_or_null(p_shadow->owner);
  1249. p_shadow_atlas->shadow_owners.erase(p_shadow->owner);
  1250. p_shadow->version = 0;
  1251. p_shadow->owner = RID();
  1252. sli->shadow_atlases.erase(p_atlas);
  1253. }
  1254. }
  1255. void LightStorage::shadow_atlas_update(RID p_atlas) {
  1256. // Do nothing as there is no shadow atlas texture.
  1257. }
  1258. /* DIRECTIONAL SHADOW */
  1259. // Create if necessary and clear.
  1260. void LightStorage::update_directional_shadow_atlas() {
  1261. if (directional_shadow.depth == 0 && directional_shadow.size > 0) {
  1262. glGenFramebuffers(1, &directional_shadow.fbo);
  1263. glBindFramebuffer(GL_FRAMEBUFFER, directional_shadow.fbo);
  1264. glGenTextures(1, &directional_shadow.depth);
  1265. glActiveTexture(GL_TEXTURE0);
  1266. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  1267. GLenum format = directional_shadow.use_16_bits ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24;
  1268. GLenum type = directional_shadow.use_16_bits ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
  1269. glTexImage2D(GL_TEXTURE_2D, 0, format, directional_shadow.size, directional_shadow.size, 0, GL_DEPTH_COMPONENT, type, nullptr);
  1270. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1271. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1272. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1273. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1274. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  1275. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
  1276. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, directional_shadow.depth, 0);
  1277. }
  1278. glUseProgram(0);
  1279. glDepthMask(GL_TRUE);
  1280. glBindFramebuffer(GL_FRAMEBUFFER, directional_shadow.fbo);
  1281. RasterizerGLES3::clear_depth(0.0);
  1282. glClear(GL_DEPTH_BUFFER_BIT);
  1283. glBindTexture(GL_TEXTURE_2D, 0);
  1284. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  1285. }
  1286. void LightStorage::directional_shadow_atlas_set_size(int p_size, bool p_16_bits) {
  1287. p_size = nearest_power_of_2_templated(p_size);
  1288. if (directional_shadow.size == p_size && directional_shadow.use_16_bits == p_16_bits) {
  1289. return;
  1290. }
  1291. directional_shadow.size = p_size;
  1292. directional_shadow.use_16_bits = p_16_bits;
  1293. if (directional_shadow.depth != 0) {
  1294. glDeleteTextures(1, &directional_shadow.depth);
  1295. directional_shadow.depth = 0;
  1296. glDeleteFramebuffers(1, &directional_shadow.fbo);
  1297. directional_shadow.fbo = 0;
  1298. }
  1299. }
  1300. void LightStorage::set_directional_shadow_count(int p_count) {
  1301. directional_shadow.light_count = p_count;
  1302. directional_shadow.current_light = 0;
  1303. }
  1304. static Rect2i _get_directional_shadow_rect(int p_size, int p_shadow_count, int p_shadow_index) {
  1305. int split_h = 1;
  1306. int split_v = 1;
  1307. while (split_h * split_v < p_shadow_count) {
  1308. if (split_h == split_v) {
  1309. split_h <<= 1;
  1310. } else {
  1311. split_v <<= 1;
  1312. }
  1313. }
  1314. Rect2i rect(0, 0, p_size, p_size);
  1315. rect.size.width /= split_h;
  1316. rect.size.height /= split_v;
  1317. rect.position.x = rect.size.width * (p_shadow_index % split_h);
  1318. rect.position.y = rect.size.height * (p_shadow_index / split_h);
  1319. return rect;
  1320. }
  1321. Rect2i LightStorage::get_directional_shadow_rect() {
  1322. return _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, directional_shadow.current_light);
  1323. }
  1324. int LightStorage::get_directional_light_shadow_size(RID p_light_instance) {
  1325. ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
  1326. Rect2i r = _get_directional_shadow_rect(directional_shadow.size, directional_shadow.light_count, 0);
  1327. LightInstance *light_instance = light_instance_owner.get_or_null(p_light_instance);
  1328. ERR_FAIL_NULL_V(light_instance, 0);
  1329. switch (light_directional_get_shadow_mode(light_instance->light)) {
  1330. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  1331. break; //none
  1332. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  1333. r.size.height /= 2;
  1334. break;
  1335. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  1336. r.size /= 2;
  1337. break;
  1338. }
  1339. return MAX(r.size.width, r.size.height);
  1340. }
  1341. #endif // !GLES3_ENABLED