rasterizer_scene_gles3.cpp 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770
  1. /**************************************************************************/
  2. /* rasterizer_scene_gles3.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "rasterizer_scene_gles3.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/templates/sort_array.h"
  33. #include "servers/rendering/rendering_server_default.h"
  34. #include "servers/rendering/rendering_server_globals.h"
  35. #include "storage/config.h"
  36. #include "storage/mesh_storage.h"
  37. #include "storage/particles_storage.h"
  38. #include "storage/texture_storage.h"
  39. #ifdef GLES3_ENABLED
  40. RasterizerSceneGLES3 *RasterizerSceneGLES3::singleton = nullptr;
  41. RenderGeometryInstance *RasterizerSceneGLES3::geometry_instance_create(RID p_base) {
  42. RS::InstanceType type = RSG::utilities->get_base_type(p_base);
  43. ERR_FAIL_COND_V(!((1 << type) & RS::INSTANCE_GEOMETRY_MASK), nullptr);
  44. GeometryInstanceGLES3 *ginstance = geometry_instance_alloc.alloc();
  45. ginstance->data = memnew(GeometryInstanceGLES3::Data);
  46. ginstance->data->base = p_base;
  47. ginstance->data->base_type = type;
  48. ginstance->data->dependency_tracker.userdata = ginstance;
  49. ginstance->data->dependency_tracker.changed_callback = _geometry_instance_dependency_changed;
  50. ginstance->data->dependency_tracker.deleted_callback = _geometry_instance_dependency_deleted;
  51. ginstance->_mark_dirty();
  52. return ginstance;
  53. }
  54. uint32_t RasterizerSceneGLES3::geometry_instance_get_pair_mask() {
  55. return (1 << RS::INSTANCE_LIGHT);
  56. }
  57. void RasterizerSceneGLES3::GeometryInstanceGLES3::pair_light_instances(const RID *p_light_instances, uint32_t p_light_instance_count) {
  58. GLES3::Config *config = GLES3::Config::get_singleton();
  59. omni_light_count = 0;
  60. spot_light_count = 0;
  61. omni_lights.clear();
  62. spot_lights.clear();
  63. for (uint32_t i = 0; i < p_light_instance_count; i++) {
  64. RS::LightType type = GLES3::LightStorage::get_singleton()->light_instance_get_type(p_light_instances[i]);
  65. switch (type) {
  66. case RS::LIGHT_OMNI: {
  67. if (omni_light_count < (uint32_t)config->max_lights_per_object) {
  68. omni_lights.push_back(p_light_instances[i]);
  69. omni_light_count++;
  70. }
  71. } break;
  72. case RS::LIGHT_SPOT: {
  73. if (spot_light_count < (uint32_t)config->max_lights_per_object) {
  74. spot_lights.push_back(p_light_instances[i]);
  75. spot_light_count++;
  76. }
  77. } break;
  78. default:
  79. break;
  80. }
  81. }
  82. }
  83. void RasterizerSceneGLES3::geometry_instance_free(RenderGeometryInstance *p_geometry_instance) {
  84. GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance);
  85. ERR_FAIL_COND(!ginstance);
  86. GeometryInstanceSurface *surf = ginstance->surface_caches;
  87. while (surf) {
  88. GeometryInstanceSurface *next = surf->next;
  89. geometry_instance_surface_alloc.free(surf);
  90. surf = next;
  91. }
  92. memdelete(ginstance->data);
  93. geometry_instance_alloc.free(ginstance);
  94. }
  95. void RasterizerSceneGLES3::GeometryInstanceGLES3::_mark_dirty() {
  96. if (dirty_list_element.in_list()) {
  97. return;
  98. }
  99. //clear surface caches
  100. GeometryInstanceSurface *surf = surface_caches;
  101. while (surf) {
  102. GeometryInstanceSurface *next = surf->next;
  103. RasterizerSceneGLES3::get_singleton()->geometry_instance_surface_alloc.free(surf);
  104. surf = next;
  105. }
  106. surface_caches = nullptr;
  107. RasterizerSceneGLES3::get_singleton()->geometry_instance_dirty_list.add(&dirty_list_element);
  108. }
  109. void RasterizerSceneGLES3::GeometryInstanceGLES3::set_use_lightmap(RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) {
  110. }
  111. void RasterizerSceneGLES3::GeometryInstanceGLES3::set_lightmap_capture(const Color *p_sh9) {
  112. }
  113. void RasterizerSceneGLES3::_update_dirty_geometry_instances() {
  114. while (geometry_instance_dirty_list.first()) {
  115. _geometry_instance_update(geometry_instance_dirty_list.first()->self());
  116. }
  117. }
  118. void RasterizerSceneGLES3::_geometry_instance_dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *p_tracker) {
  119. switch (p_notification) {
  120. case Dependency::DEPENDENCY_CHANGED_MATERIAL:
  121. case Dependency::DEPENDENCY_CHANGED_MESH:
  122. case Dependency::DEPENDENCY_CHANGED_PARTICLES:
  123. case Dependency::DEPENDENCY_CHANGED_MULTIMESH:
  124. case Dependency::DEPENDENCY_CHANGED_SKELETON_DATA: {
  125. static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty();
  126. static_cast<GeometryInstanceGLES3 *>(p_tracker->userdata)->data->dirty_dependencies = true;
  127. } break;
  128. case Dependency::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES: {
  129. GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_tracker->userdata);
  130. if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) {
  131. ginstance->instance_count = GLES3::MeshStorage::get_singleton()->multimesh_get_instances_to_draw(ginstance->data->base);
  132. }
  133. } break;
  134. default: {
  135. //rest of notifications of no interest
  136. } break;
  137. }
  138. }
  139. void RasterizerSceneGLES3::_geometry_instance_dependency_deleted(const RID &p_dependency, DependencyTracker *p_tracker) {
  140. static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty();
  141. static_cast<GeometryInstanceGLES3 *>(p_tracker->userdata)->data->dirty_dependencies = true;
  142. }
  143. void RasterizerSceneGLES3::_geometry_instance_add_surface_with_material(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, GLES3::SceneMaterialData *p_material, uint32_t p_material_id, uint32_t p_shader_id, RID p_mesh) {
  144. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  145. bool has_read_screen_alpha = p_material->shader_data->uses_screen_texture || p_material->shader_data->uses_depth_texture || p_material->shader_data->uses_normal_texture;
  146. bool has_base_alpha = ((p_material->shader_data->uses_alpha && !p_material->shader_data->uses_alpha_clip) || has_read_screen_alpha);
  147. bool has_blend_alpha = p_material->shader_data->uses_blend_alpha;
  148. bool has_alpha = has_base_alpha || has_blend_alpha;
  149. uint32_t flags = 0;
  150. if (p_material->shader_data->uses_screen_texture) {
  151. flags |= GeometryInstanceSurface::FLAG_USES_SCREEN_TEXTURE;
  152. }
  153. if (p_material->shader_data->uses_depth_texture) {
  154. flags |= GeometryInstanceSurface::FLAG_USES_DEPTH_TEXTURE;
  155. }
  156. if (p_material->shader_data->uses_normal_texture) {
  157. flags |= GeometryInstanceSurface::FLAG_USES_NORMAL_TEXTURE;
  158. }
  159. if (ginstance->data->cast_double_sided_shadows) {
  160. flags |= GeometryInstanceSurface::FLAG_USES_DOUBLE_SIDED_SHADOWS;
  161. }
  162. if (has_alpha || has_read_screen_alpha || p_material->shader_data->depth_draw == GLES3::SceneShaderData::DEPTH_DRAW_DISABLED || p_material->shader_data->depth_test == GLES3::SceneShaderData::DEPTH_TEST_DISABLED) {
  163. //material is only meant for alpha pass
  164. flags |= GeometryInstanceSurface::FLAG_PASS_ALPHA;
  165. if (p_material->shader_data->uses_depth_prepass_alpha && !(p_material->shader_data->depth_draw == GLES3::SceneShaderData::DEPTH_DRAW_DISABLED || p_material->shader_data->depth_test == GLES3::SceneShaderData::DEPTH_TEST_DISABLED)) {
  166. flags |= GeometryInstanceSurface::FLAG_PASS_DEPTH;
  167. flags |= GeometryInstanceSurface::FLAG_PASS_SHADOW;
  168. }
  169. } else {
  170. flags |= GeometryInstanceSurface::FLAG_PASS_OPAQUE;
  171. flags |= GeometryInstanceSurface::FLAG_PASS_DEPTH;
  172. flags |= GeometryInstanceSurface::FLAG_PASS_SHADOW;
  173. }
  174. GLES3::SceneMaterialData *material_shadow = nullptr;
  175. void *surface_shadow = nullptr;
  176. if (!p_material->shader_data->uses_particle_trails && !p_material->shader_data->writes_modelview_or_projection && !p_material->shader_data->uses_vertex && !p_material->shader_data->uses_discard && !p_material->shader_data->uses_depth_prepass_alpha && !p_material->shader_data->uses_alpha_clip) {
  177. flags |= GeometryInstanceSurface::FLAG_USES_SHARED_SHADOW_MATERIAL;
  178. material_shadow = static_cast<GLES3::SceneMaterialData *>(GLES3::MaterialStorage::get_singleton()->material_get_data(scene_globals.default_material, RS::SHADER_SPATIAL));
  179. RID shadow_mesh = mesh_storage->mesh_get_shadow_mesh(p_mesh);
  180. if (shadow_mesh.is_valid()) {
  181. surface_shadow = mesh_storage->mesh_get_surface(shadow_mesh, p_surface);
  182. }
  183. } else {
  184. material_shadow = p_material;
  185. }
  186. GeometryInstanceSurface *sdcache = geometry_instance_surface_alloc.alloc();
  187. sdcache->flags = flags;
  188. sdcache->shader = p_material->shader_data;
  189. sdcache->material = p_material;
  190. sdcache->surface = mesh_storage->mesh_get_surface(p_mesh, p_surface);
  191. sdcache->primitive = mesh_storage->mesh_surface_get_primitive(sdcache->surface);
  192. sdcache->surface_index = p_surface;
  193. if (ginstance->data->dirty_dependencies) {
  194. RSG::utilities->base_update_dependency(p_mesh, &ginstance->data->dependency_tracker);
  195. }
  196. //shadow
  197. sdcache->shader_shadow = material_shadow->shader_data;
  198. sdcache->material_shadow = material_shadow;
  199. sdcache->surface_shadow = surface_shadow ? surface_shadow : sdcache->surface;
  200. sdcache->owner = ginstance;
  201. sdcache->next = ginstance->surface_caches;
  202. ginstance->surface_caches = sdcache;
  203. //sortkey
  204. sdcache->sort.sort_key1 = 0;
  205. sdcache->sort.sort_key2 = 0;
  206. sdcache->sort.surface_index = p_surface;
  207. sdcache->sort.material_id_low = p_material_id & 0x0000FFFF;
  208. sdcache->sort.material_id_hi = p_material_id >> 16;
  209. sdcache->sort.shader_id = p_shader_id;
  210. sdcache->sort.geometry_id = p_mesh.get_local_index();
  211. sdcache->sort.priority = p_material->priority;
  212. }
  213. void RasterizerSceneGLES3::_geometry_instance_add_surface_with_material_chain(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, GLES3::SceneMaterialData *p_material_data, RID p_mat_src, RID p_mesh) {
  214. GLES3::SceneMaterialData *material_data = p_material_data;
  215. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  216. _geometry_instance_add_surface_with_material(ginstance, p_surface, material_data, p_mat_src.get_local_index(), material_storage->material_get_shader_id(p_mat_src), p_mesh);
  217. while (material_data->next_pass.is_valid()) {
  218. RID next_pass = material_data->next_pass;
  219. material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(next_pass, RS::SHADER_SPATIAL));
  220. if (!material_data || !material_data->shader_data->valid) {
  221. break;
  222. }
  223. if (ginstance->data->dirty_dependencies) {
  224. material_storage->material_update_dependency(next_pass, &ginstance->data->dependency_tracker);
  225. }
  226. _geometry_instance_add_surface_with_material(ginstance, p_surface, material_data, next_pass.get_local_index(), material_storage->material_get_shader_id(next_pass), p_mesh);
  227. }
  228. }
  229. void RasterizerSceneGLES3::_geometry_instance_add_surface(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, RID p_material, RID p_mesh) {
  230. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  231. RID m_src;
  232. m_src = ginstance->data->material_override.is_valid() ? ginstance->data->material_override : p_material;
  233. GLES3::SceneMaterialData *material_data = nullptr;
  234. if (m_src.is_valid()) {
  235. material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(m_src, RS::SHADER_SPATIAL));
  236. if (!material_data || !material_data->shader_data->valid) {
  237. material_data = nullptr;
  238. }
  239. }
  240. if (material_data) {
  241. if (ginstance->data->dirty_dependencies) {
  242. material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker);
  243. }
  244. } else {
  245. material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(scene_globals.default_material, RS::SHADER_SPATIAL));
  246. m_src = scene_globals.default_material;
  247. }
  248. ERR_FAIL_COND(!material_data);
  249. _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material_data, m_src, p_mesh);
  250. if (ginstance->data->material_overlay.is_valid()) {
  251. m_src = ginstance->data->material_overlay;
  252. material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(m_src, RS::SHADER_SPATIAL));
  253. if (material_data && material_data->shader_data->valid) {
  254. if (ginstance->data->dirty_dependencies) {
  255. material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker);
  256. }
  257. _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material_data, m_src, p_mesh);
  258. }
  259. }
  260. }
  261. void RasterizerSceneGLES3::_geometry_instance_update(RenderGeometryInstance *p_geometry_instance) {
  262. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  263. GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
  264. GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance);
  265. if (ginstance->data->dirty_dependencies) {
  266. ginstance->data->dependency_tracker.update_begin();
  267. }
  268. //add geometry for drawing
  269. switch (ginstance->data->base_type) {
  270. case RS::INSTANCE_MESH: {
  271. const RID *materials = nullptr;
  272. uint32_t surface_count;
  273. RID mesh = ginstance->data->base;
  274. materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
  275. if (materials) {
  276. //if no materials, no surfaces.
  277. const RID *inst_materials = ginstance->data->surface_materials.ptr();
  278. uint32_t surf_mat_count = ginstance->data->surface_materials.size();
  279. for (uint32_t j = 0; j < surface_count; j++) {
  280. RID material = (j < surf_mat_count && inst_materials[j].is_valid()) ? inst_materials[j] : materials[j];
  281. _geometry_instance_add_surface(ginstance, j, material, mesh);
  282. }
  283. }
  284. ginstance->instance_count = -1;
  285. } break;
  286. case RS::INSTANCE_MULTIMESH: {
  287. RID mesh = mesh_storage->multimesh_get_mesh(ginstance->data->base);
  288. if (mesh.is_valid()) {
  289. const RID *materials = nullptr;
  290. uint32_t surface_count;
  291. materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
  292. if (materials) {
  293. for (uint32_t j = 0; j < surface_count; j++) {
  294. _geometry_instance_add_surface(ginstance, j, materials[j], mesh);
  295. }
  296. }
  297. ginstance->instance_count = mesh_storage->multimesh_get_instances_to_draw(ginstance->data->base);
  298. }
  299. } break;
  300. case RS::INSTANCE_PARTICLES: {
  301. int draw_passes = particles_storage->particles_get_draw_passes(ginstance->data->base);
  302. for (int j = 0; j < draw_passes; j++) {
  303. RID mesh = particles_storage->particles_get_draw_pass_mesh(ginstance->data->base, j);
  304. if (!mesh.is_valid()) {
  305. continue;
  306. }
  307. const RID *materials = nullptr;
  308. uint32_t surface_count;
  309. materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
  310. if (materials) {
  311. for (uint32_t k = 0; k < surface_count; k++) {
  312. _geometry_instance_add_surface(ginstance, k, materials[k], mesh);
  313. }
  314. }
  315. }
  316. ginstance->instance_count = particles_storage->particles_get_amount(ginstance->data->base);
  317. } break;
  318. default: {
  319. }
  320. }
  321. bool store_transform = true;
  322. ginstance->base_flags = 0;
  323. if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) {
  324. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH;
  325. if (mesh_storage->multimesh_get_transform_format(ginstance->data->base) == RS::MULTIMESH_TRANSFORM_2D) {
  326. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D;
  327. }
  328. if (mesh_storage->multimesh_uses_colors(ginstance->data->base)) {
  329. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR;
  330. }
  331. if (mesh_storage->multimesh_uses_custom_data(ginstance->data->base)) {
  332. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA;
  333. }
  334. } else if (ginstance->data->base_type == RS::INSTANCE_PARTICLES) {
  335. ginstance->base_flags |= INSTANCE_DATA_FLAG_PARTICLES;
  336. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH;
  337. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR;
  338. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA;
  339. if (!particles_storage->particles_is_using_local_coords(ginstance->data->base)) {
  340. store_transform = false;
  341. }
  342. } else if (ginstance->data->base_type == RS::INSTANCE_MESH) {
  343. if (mesh_storage->skeleton_is_valid(ginstance->data->skeleton)) {
  344. if (ginstance->data->dirty_dependencies) {
  345. mesh_storage->skeleton_update_dependency(ginstance->data->skeleton, &ginstance->data->dependency_tracker);
  346. }
  347. }
  348. }
  349. ginstance->store_transform_cache = store_transform;
  350. if (ginstance->data->dirty_dependencies) {
  351. ginstance->data->dependency_tracker.update_end();
  352. ginstance->data->dirty_dependencies = false;
  353. }
  354. ginstance->dirty_list_element.remove_from_list();
  355. }
  356. /* SKY API */
  357. void RasterizerSceneGLES3::_free_sky_data(Sky *p_sky) {
  358. if (p_sky->radiance != 0) {
  359. glDeleteTextures(1, &p_sky->radiance);
  360. p_sky->radiance = 0;
  361. glDeleteFramebuffers(1, &p_sky->radiance_framebuffer);
  362. p_sky->radiance_framebuffer = 0;
  363. }
  364. }
  365. RID RasterizerSceneGLES3::sky_allocate() {
  366. return sky_owner.allocate_rid();
  367. }
  368. void RasterizerSceneGLES3::sky_initialize(RID p_rid) {
  369. sky_owner.initialize_rid(p_rid);
  370. }
  371. void RasterizerSceneGLES3::sky_set_radiance_size(RID p_sky, int p_radiance_size) {
  372. Sky *sky = sky_owner.get_or_null(p_sky);
  373. ERR_FAIL_COND(!sky);
  374. ERR_FAIL_COND_MSG(p_radiance_size < 32 || p_radiance_size > 2048, "Sky radiance size must be between 32 and 2048");
  375. if (sky->radiance_size == p_radiance_size) {
  376. return; // No need to update
  377. }
  378. sky->radiance_size = p_radiance_size;
  379. _free_sky_data(sky);
  380. _invalidate_sky(sky);
  381. }
  382. void RasterizerSceneGLES3::sky_set_mode(RID p_sky, RS::SkyMode p_mode) {
  383. Sky *sky = sky_owner.get_or_null(p_sky);
  384. ERR_FAIL_COND(!sky);
  385. if (sky->mode == p_mode) {
  386. return;
  387. }
  388. sky->mode = p_mode;
  389. _invalidate_sky(sky);
  390. }
  391. void RasterizerSceneGLES3::sky_set_material(RID p_sky, RID p_material) {
  392. Sky *sky = sky_owner.get_or_null(p_sky);
  393. ERR_FAIL_COND(!sky);
  394. if (sky->material == p_material) {
  395. return;
  396. }
  397. sky->material = p_material;
  398. _invalidate_sky(sky);
  399. }
  400. float RasterizerSceneGLES3::sky_get_baked_exposure(RID p_sky) const {
  401. Sky *sky = sky_owner.get_or_null(p_sky);
  402. ERR_FAIL_COND_V(!sky, 1.0);
  403. return sky->baked_exposure;
  404. }
  405. void RasterizerSceneGLES3::_invalidate_sky(Sky *p_sky) {
  406. if (!p_sky->dirty) {
  407. p_sky->dirty = true;
  408. p_sky->dirty_list = dirty_sky_list;
  409. dirty_sky_list = p_sky;
  410. }
  411. }
  412. void RasterizerSceneGLES3::_update_dirty_skys() {
  413. Sky *sky = dirty_sky_list;
  414. while (sky) {
  415. if (sky->radiance == 0) {
  416. sky->mipmap_count = Image::get_image_required_mipmaps(sky->radiance_size, sky->radiance_size, Image::FORMAT_RGBA8) - 1;
  417. // Left uninitialized, will attach a texture at render time
  418. glGenFramebuffers(1, &sky->radiance_framebuffer);
  419. GLenum internal_format = GL_RGB10_A2;
  420. glGenTextures(1, &sky->radiance);
  421. glBindTexture(GL_TEXTURE_CUBE_MAP, sky->radiance);
  422. #ifdef GLES_OVER_GL
  423. GLenum format = GL_RGBA;
  424. GLenum type = GL_UNSIGNED_INT_2_10_10_10_REV;
  425. //TODO, on low-end compare this to allocating each face of each mip individually
  426. // see: https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexStorage2D.xhtml
  427. for (int i = 0; i < 6; i++) {
  428. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internal_format, sky->radiance_size, sky->radiance_size, 0, format, type, nullptr);
  429. }
  430. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  431. #else
  432. glTexStorage2D(GL_TEXTURE_CUBE_MAP, sky->mipmap_count, internal_format, sky->radiance_size, sky->radiance_size);
  433. #endif
  434. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  435. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  436. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  437. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  438. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
  439. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, sky->mipmap_count - 1);
  440. glGenTextures(1, &sky->raw_radiance);
  441. glBindTexture(GL_TEXTURE_CUBE_MAP, sky->raw_radiance);
  442. #ifdef GLES_OVER_GL
  443. //TODO, on low-end compare this to allocating each face of each mip individually
  444. // see: https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexStorage2D.xhtml
  445. for (int i = 0; i < 6; i++) {
  446. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internal_format, sky->radiance_size, sky->radiance_size, 0, format, type, nullptr);
  447. }
  448. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  449. #else
  450. glTexStorage2D(GL_TEXTURE_CUBE_MAP, sky->mipmap_count, internal_format, sky->radiance_size, sky->radiance_size);
  451. #endif
  452. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  453. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  454. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  455. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  456. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
  457. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, sky->mipmap_count - 1);
  458. glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
  459. }
  460. sky->reflection_dirty = true;
  461. sky->processing_layer = 0;
  462. Sky *next = sky->dirty_list;
  463. sky->dirty_list = nullptr;
  464. sky->dirty = false;
  465. sky = next;
  466. }
  467. dirty_sky_list = nullptr;
  468. }
  469. void RasterizerSceneGLES3::_setup_sky(const RenderDataGLES3 *p_render_data, const PagedArray<RID> &p_lights, const Projection &p_projection, const Transform3D &p_transform, const Size2i p_screen_size) {
  470. GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
  471. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  472. ERR_FAIL_COND(p_render_data->environment.is_null());
  473. GLES3::SkyMaterialData *material = nullptr;
  474. Sky *sky = sky_owner.get_or_null(environment_get_sky(p_render_data->environment));
  475. RID sky_material;
  476. GLES3::SkyShaderData *shader_data = nullptr;
  477. if (sky) {
  478. sky_material = sky->material;
  479. if (sky_material.is_valid()) {
  480. material = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  481. if (!material || !material->shader_data->valid) {
  482. material = nullptr;
  483. }
  484. }
  485. if (!material) {
  486. sky_material = sky_globals.default_material;
  487. material = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  488. }
  489. ERR_FAIL_COND(!material);
  490. shader_data = material->shader_data;
  491. ERR_FAIL_COND(!shader_data);
  492. if (shader_data->uses_time && time - sky->prev_time > 0.00001) {
  493. sky->prev_time = time;
  494. sky->reflection_dirty = true;
  495. RenderingServerDefault::redraw_request();
  496. }
  497. if (material != sky->prev_material) {
  498. sky->prev_material = material;
  499. sky->reflection_dirty = true;
  500. }
  501. if (material->uniform_set_updated) {
  502. material->uniform_set_updated = false;
  503. sky->reflection_dirty = true;
  504. }
  505. if (!p_transform.origin.is_equal_approx(sky->prev_position) && shader_data->uses_position) {
  506. sky->prev_position = p_transform.origin;
  507. sky->reflection_dirty = true;
  508. }
  509. glBindBufferBase(GL_UNIFORM_BUFFER, SKY_DIRECTIONAL_LIGHT_UNIFORM_LOCATION, sky_globals.directional_light_buffer);
  510. if (shader_data->uses_light) {
  511. sky_globals.directional_light_count = 0;
  512. for (int i = 0; i < (int)p_lights.size(); i++) {
  513. GLES3::LightInstance *li = GLES3::LightStorage::get_singleton()->get_light_instance(p_lights[i]);
  514. if (!li) {
  515. continue;
  516. }
  517. RID base = li->light;
  518. ERR_CONTINUE(base.is_null());
  519. RS::LightType type = light_storage->light_get_type(base);
  520. if (type == RS::LIGHT_DIRECTIONAL && light_storage->light_directional_get_sky_mode(base) != RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_ONLY) {
  521. DirectionalLightData &sky_light_data = sky_globals.directional_lights[sky_globals.directional_light_count];
  522. Transform3D light_transform = li->transform;
  523. Vector3 world_direction = light_transform.basis.xform(Vector3(0, 0, 1)).normalized();
  524. sky_light_data.direction[0] = world_direction.x;
  525. sky_light_data.direction[1] = world_direction.y;
  526. sky_light_data.direction[2] = world_direction.z;
  527. float sign = light_storage->light_is_negative(base) ? -1 : 1;
  528. sky_light_data.energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY);
  529. if (is_using_physical_light_units()) {
  530. sky_light_data.energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
  531. }
  532. if (p_render_data->camera_attributes.is_valid()) {
  533. sky_light_data.energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  534. }
  535. Color linear_col = light_storage->light_get_color(base);
  536. sky_light_data.color[0] = linear_col.r;
  537. sky_light_data.color[1] = linear_col.g;
  538. sky_light_data.color[2] = linear_col.b;
  539. sky_light_data.enabled = true;
  540. float angular_diameter = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  541. if (angular_diameter > 0.0) {
  542. angular_diameter = Math::tan(Math::deg_to_rad(angular_diameter));
  543. } else {
  544. angular_diameter = 0.0;
  545. }
  546. sky_light_data.size = angular_diameter;
  547. sky_globals.directional_light_count++;
  548. if (sky_globals.directional_light_count >= sky_globals.max_directional_lights) {
  549. break;
  550. }
  551. }
  552. }
  553. // Check whether the directional_light_buffer changes
  554. bool light_data_dirty = false;
  555. // Light buffer is dirty if we have fewer or more lights
  556. // If we have fewer lights, make sure that old lights are disabled
  557. if (sky_globals.directional_light_count != sky_globals.last_frame_directional_light_count) {
  558. light_data_dirty = true;
  559. for (uint32_t i = sky_globals.directional_light_count; i < sky_globals.max_directional_lights; i++) {
  560. sky_globals.directional_lights[i].enabled = false;
  561. sky_globals.last_frame_directional_lights[i].enabled = false;
  562. }
  563. }
  564. if (!light_data_dirty) {
  565. for (uint32_t i = 0; i < sky_globals.directional_light_count; i++) {
  566. if (sky_globals.directional_lights[i].direction[0] != sky_globals.last_frame_directional_lights[i].direction[0] ||
  567. sky_globals.directional_lights[i].direction[1] != sky_globals.last_frame_directional_lights[i].direction[1] ||
  568. sky_globals.directional_lights[i].direction[2] != sky_globals.last_frame_directional_lights[i].direction[2] ||
  569. sky_globals.directional_lights[i].energy != sky_globals.last_frame_directional_lights[i].energy ||
  570. sky_globals.directional_lights[i].color[0] != sky_globals.last_frame_directional_lights[i].color[0] ||
  571. sky_globals.directional_lights[i].color[1] != sky_globals.last_frame_directional_lights[i].color[1] ||
  572. sky_globals.directional_lights[i].color[2] != sky_globals.last_frame_directional_lights[i].color[2] ||
  573. sky_globals.directional_lights[i].enabled != sky_globals.last_frame_directional_lights[i].enabled ||
  574. sky_globals.directional_lights[i].size != sky_globals.last_frame_directional_lights[i].size) {
  575. light_data_dirty = true;
  576. break;
  577. }
  578. }
  579. }
  580. if (light_data_dirty) {
  581. glBufferData(GL_UNIFORM_BUFFER, sizeof(DirectionalLightData) * sky_globals.max_directional_lights, sky_globals.directional_lights, GL_STREAM_DRAW);
  582. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  583. DirectionalLightData *temp = sky_globals.last_frame_directional_lights;
  584. sky_globals.last_frame_directional_lights = sky_globals.directional_lights;
  585. sky_globals.directional_lights = temp;
  586. sky_globals.last_frame_directional_light_count = sky_globals.directional_light_count;
  587. sky->reflection_dirty = true;
  588. }
  589. }
  590. if (p_render_data->view_count > 1) {
  591. glBindBufferBase(GL_UNIFORM_BUFFER, SKY_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
  592. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  593. }
  594. if (!sky->radiance) {
  595. _invalidate_sky(sky);
  596. _update_dirty_skys();
  597. }
  598. }
  599. }
  600. void RasterizerSceneGLES3::_draw_sky(RID p_env, const Projection &p_projection, const Transform3D &p_transform, float p_luminance_multiplier, bool p_use_multiview, bool p_flip_y) {
  601. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  602. ERR_FAIL_COND(p_env.is_null());
  603. Sky *sky = sky_owner.get_or_null(environment_get_sky(p_env));
  604. ERR_FAIL_COND(!sky);
  605. GLES3::SkyMaterialData *material_data = nullptr;
  606. RID sky_material;
  607. uint64_t spec_constants = p_use_multiview ? SkyShaderGLES3::USE_MULTIVIEW : 0;
  608. if (p_flip_y) {
  609. spec_constants |= SkyShaderGLES3::USE_INVERTED_Y;
  610. }
  611. RS::EnvironmentBG background = environment_get_background(p_env);
  612. if (sky) {
  613. sky_material = sky->material;
  614. if (sky_material.is_valid()) {
  615. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  616. if (!material_data || !material_data->shader_data->valid) {
  617. material_data = nullptr;
  618. }
  619. }
  620. if (!material_data) {
  621. sky_material = sky_globals.default_material;
  622. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  623. }
  624. } else if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  625. sky_material = sky_globals.fog_material;
  626. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  627. }
  628. ERR_FAIL_COND(!material_data);
  629. material_data->bind_uniforms();
  630. GLES3::SkyShaderData *shader_data = material_data->shader_data;
  631. ERR_FAIL_COND(!shader_data);
  632. // Camera
  633. Projection camera;
  634. if (environment_get_sky_custom_fov(p_env)) {
  635. float near_plane = p_projection.get_z_near();
  636. float far_plane = p_projection.get_z_far();
  637. float aspect = p_projection.get_aspect();
  638. camera.set_perspective(environment_get_sky_custom_fov(p_env), aspect, near_plane, far_plane);
  639. } else {
  640. camera = p_projection;
  641. }
  642. Basis sky_transform = environment_get_sky_orientation(p_env);
  643. sky_transform.invert();
  644. sky_transform = sky_transform * p_transform.basis;
  645. bool success = material_storage->shaders.sky_shader.version_bind_shader(shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  646. if (!success) {
  647. return;
  648. }
  649. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::ORIENTATION, sky_transform, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  650. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::PROJECTION, camera.columns[2][0], camera.columns[0][0], camera.columns[2][1], camera.columns[1][1], shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  651. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::POSITION, p_transform.origin, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  652. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::TIME, time, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  653. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::LUMINANCE_MULTIPLIER, p_luminance_multiplier, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  654. if (p_use_multiview) {
  655. glBindBufferBase(GL_UNIFORM_BUFFER, SKY_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
  656. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  657. }
  658. glBindVertexArray(sky_globals.screen_triangle_array);
  659. glDrawArrays(GL_TRIANGLES, 0, 3);
  660. }
  661. void RasterizerSceneGLES3::_update_sky_radiance(RID p_env, const Projection &p_projection, const Transform3D &p_transform, float p_luminance_multiplier) {
  662. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  663. ERR_FAIL_COND(p_env.is_null());
  664. Sky *sky = sky_owner.get_or_null(environment_get_sky(p_env));
  665. ERR_FAIL_COND(!sky);
  666. GLES3::SkyMaterialData *material_data = nullptr;
  667. RID sky_material;
  668. RS::EnvironmentBG background = environment_get_background(p_env);
  669. if (sky) {
  670. ERR_FAIL_COND(!sky);
  671. sky_material = sky->material;
  672. if (sky_material.is_valid()) {
  673. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  674. if (!material_data || !material_data->shader_data->valid) {
  675. material_data = nullptr;
  676. }
  677. }
  678. if (!material_data) {
  679. sky_material = sky_globals.default_material;
  680. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  681. }
  682. } else if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  683. sky_material = sky_globals.fog_material;
  684. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  685. }
  686. ERR_FAIL_COND(!material_data);
  687. material_data->bind_uniforms();
  688. GLES3::SkyShaderData *shader_data = material_data->shader_data;
  689. ERR_FAIL_COND(!shader_data);
  690. bool update_single_frame = sky->mode == RS::SKY_MODE_REALTIME || sky->mode == RS::SKY_MODE_QUALITY;
  691. RS::SkyMode sky_mode = sky->mode;
  692. if (sky_mode == RS::SKY_MODE_AUTOMATIC) {
  693. if (shader_data->uses_time || shader_data->uses_position) {
  694. update_single_frame = true;
  695. sky_mode = RS::SKY_MODE_REALTIME;
  696. } else if (shader_data->uses_light || shader_data->ubo_size > 0) {
  697. update_single_frame = false;
  698. sky_mode = RS::SKY_MODE_INCREMENTAL;
  699. } else {
  700. update_single_frame = true;
  701. sky_mode = RS::SKY_MODE_QUALITY;
  702. }
  703. }
  704. if (sky->processing_layer == 0 && sky_mode == RS::SKY_MODE_INCREMENTAL) {
  705. // On the first frame after creating sky, rebuild in single frame
  706. update_single_frame = true;
  707. sky_mode = RS::SKY_MODE_QUALITY;
  708. }
  709. int max_processing_layer = sky->mipmap_count;
  710. // Update radiance cubemap
  711. if (sky->reflection_dirty && (sky->processing_layer > max_processing_layer || update_single_frame)) {
  712. static const Vector3 view_normals[6] = {
  713. Vector3(+1, 0, 0),
  714. Vector3(-1, 0, 0),
  715. Vector3(0, +1, 0),
  716. Vector3(0, -1, 0),
  717. Vector3(0, 0, +1),
  718. Vector3(0, 0, -1)
  719. };
  720. static const Vector3 view_up[6] = {
  721. Vector3(0, -1, 0),
  722. Vector3(0, -1, 0),
  723. Vector3(0, 0, +1),
  724. Vector3(0, 0, -1),
  725. Vector3(0, -1, 0),
  726. Vector3(0, -1, 0)
  727. };
  728. Projection cm;
  729. cm.set_perspective(90, 1, 0.01, 10.0);
  730. Projection correction;
  731. correction.columns[1][1] = -1.0;
  732. cm = correction * cm;
  733. bool success = material_storage->shaders.sky_shader.version_bind_shader(shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  734. if (!success) {
  735. return;
  736. }
  737. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::POSITION, p_transform.origin, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  738. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::TIME, time, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  739. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::PROJECTION, cm.columns[2][0], cm.columns[0][0], cm.columns[2][1], cm.columns[1][1], shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  740. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::LUMINANCE_MULTIPLIER, p_luminance_multiplier, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  741. glBindVertexArray(sky_globals.screen_triangle_array);
  742. glViewport(0, 0, sky->radiance_size, sky->radiance_size);
  743. glBindFramebuffer(GL_FRAMEBUFFER, sky->radiance_framebuffer);
  744. for (int i = 0; i < 6; i++) {
  745. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  746. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::ORIENTATION, local_view, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  747. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, sky->raw_radiance, 0);
  748. glDrawArrays(GL_TRIANGLES, 0, 3);
  749. }
  750. if (update_single_frame) {
  751. for (int i = 0; i < max_processing_layer; i++) {
  752. _filter_sky_radiance(sky, i);
  753. }
  754. } else {
  755. _filter_sky_radiance(sky, 0); //Just copy over the first mipmap
  756. }
  757. sky->processing_layer = 1;
  758. sky->baked_exposure = p_luminance_multiplier;
  759. sky->reflection_dirty = false;
  760. } else {
  761. if (sky_mode == RS::SKY_MODE_INCREMENTAL && sky->processing_layer < max_processing_layer) {
  762. _filter_sky_radiance(sky, sky->processing_layer);
  763. sky->processing_layer++;
  764. }
  765. }
  766. }
  767. // Helper functions for IBL filtering
  768. Vector3 importance_sample_GGX(Vector2 xi, float roughness4) {
  769. // Compute distribution direction
  770. float phi = 2.0 * Math_PI * xi.x;
  771. float cos_theta = sqrt((1.0 - xi.y) / (1.0 + (roughness4 - 1.0) * xi.y));
  772. float sin_theta = sqrt(1.0 - cos_theta * cos_theta);
  773. // Convert to spherical direction
  774. Vector3 half_vector;
  775. half_vector.x = sin_theta * cos(phi);
  776. half_vector.y = sin_theta * sin(phi);
  777. half_vector.z = cos_theta;
  778. return half_vector;
  779. }
  780. float distribution_GGX(float NdotH, float roughness4) {
  781. float NdotH2 = NdotH * NdotH;
  782. float denom = (NdotH2 * (roughness4 - 1.0) + 1.0);
  783. denom = Math_PI * denom * denom;
  784. return roughness4 / denom;
  785. }
  786. float radical_inverse_vdC(uint32_t bits) {
  787. bits = (bits << 16) | (bits >> 16);
  788. bits = ((bits & 0x55555555) << 1) | ((bits & 0xAAAAAAAA) >> 1);
  789. bits = ((bits & 0x33333333) << 2) | ((bits & 0xCCCCCCCC) >> 2);
  790. bits = ((bits & 0x0F0F0F0F) << 4) | ((bits & 0xF0F0F0F0) >> 4);
  791. bits = ((bits & 0x00FF00FF) << 8) | ((bits & 0xFF00FF00) >> 8);
  792. return float(bits) * 2.3283064365386963e-10;
  793. }
  794. Vector2 hammersley(uint32_t i, uint32_t N) {
  795. return Vector2(float(i) / float(N), radical_inverse_vdC(i));
  796. }
  797. void RasterizerSceneGLES3::_filter_sky_radiance(Sky *p_sky, int p_base_layer) {
  798. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  799. glActiveTexture(GL_TEXTURE0);
  800. glBindTexture(GL_TEXTURE_CUBE_MAP, p_sky->raw_radiance);
  801. glBindFramebuffer(GL_FRAMEBUFFER, p_sky->radiance_framebuffer);
  802. CubemapFilterShaderGLES3::ShaderVariant mode = CubemapFilterShaderGLES3::MODE_DEFAULT;
  803. if (p_base_layer == 0) {
  804. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  805. // Copy over base layer without filtering.
  806. mode = CubemapFilterShaderGLES3::MODE_COPY;
  807. }
  808. int size = p_sky->radiance_size >> p_base_layer;
  809. glViewport(0, 0, size, size);
  810. glBindVertexArray(sky_globals.screen_triangle_array);
  811. bool success = material_storage->shaders.cubemap_filter_shader.version_bind_shader(scene_globals.cubemap_filter_shader_version, mode);
  812. if (!success) {
  813. return;
  814. }
  815. if (p_base_layer > 0) {
  816. const uint32_t sample_counts[4] = { 1, sky_globals.ggx_samples / 4, sky_globals.ggx_samples / 2, sky_globals.ggx_samples };
  817. uint32_t sample_count = sample_counts[MIN(3, p_base_layer)];
  818. float roughness = float(p_base_layer) / (p_sky->mipmap_count);
  819. float roughness4 = roughness * roughness;
  820. roughness4 *= roughness4;
  821. float solid_angle_texel = 4.0 * Math_PI / float(6 * size * size);
  822. LocalVector<float> sample_directions;
  823. sample_directions.resize(4 * sample_count);
  824. uint32_t index = 0;
  825. float weight = 0.0;
  826. for (uint32_t i = 0; i < sample_count; i++) {
  827. Vector2 xi = hammersley(i, sample_count);
  828. Vector3 dir = importance_sample_GGX(xi, roughness4);
  829. Vector3 light_vec = (2.0 * dir.z * dir - Vector3(0.0, 0.0, 1.0));
  830. if (light_vec.z < 0.0) {
  831. continue;
  832. }
  833. sample_directions[index * 4] = light_vec.x;
  834. sample_directions[index * 4 + 1] = light_vec.y;
  835. sample_directions[index * 4 + 2] = light_vec.z;
  836. float D = distribution_GGX(dir.z, roughness4);
  837. float pdf = D * dir.z / (4.0 * dir.z) + 0.0001;
  838. float solid_angle_sample = 1.0 / (float(sample_count) * pdf + 0.0001);
  839. float mip_level = MAX(0.5 * log2(solid_angle_sample / solid_angle_texel) + float(MAX(1, p_base_layer - 3)), 1.0);
  840. sample_directions[index * 4 + 3] = mip_level;
  841. weight += light_vec.z;
  842. index++;
  843. }
  844. glUniform4fv(material_storage->shaders.cubemap_filter_shader.version_get_uniform(CubemapFilterShaderGLES3::SAMPLE_DIRECTIONS_MIP, scene_globals.cubemap_filter_shader_version, mode), sample_count, sample_directions.ptr());
  845. material_storage->shaders.cubemap_filter_shader.version_set_uniform(CubemapFilterShaderGLES3::WEIGHT, weight, scene_globals.cubemap_filter_shader_version, mode);
  846. material_storage->shaders.cubemap_filter_shader.version_set_uniform(CubemapFilterShaderGLES3::SAMPLE_COUNT, index, scene_globals.cubemap_filter_shader_version, mode);
  847. }
  848. for (int i = 0; i < 6; i++) {
  849. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, p_sky->radiance, p_base_layer);
  850. #ifdef DEBUG_ENABLED
  851. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  852. if (status != GL_FRAMEBUFFER_COMPLETE) {
  853. WARN_PRINT("Could not bind sky radiance face: " + itos(i) + ", status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
  854. }
  855. #endif
  856. material_storage->shaders.cubemap_filter_shader.version_set_uniform(CubemapFilterShaderGLES3::FACE_ID, i, scene_globals.cubemap_filter_shader_version, mode);
  857. glDrawArrays(GL_TRIANGLES, 0, 3);
  858. }
  859. glBindVertexArray(0);
  860. glViewport(0, 0, p_sky->screen_size.x, p_sky->screen_size.y);
  861. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  862. }
  863. Ref<Image> RasterizerSceneGLES3::sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) {
  864. return Ref<Image>();
  865. }
  866. /* ENVIRONMENT API */
  867. void RasterizerSceneGLES3::environment_glow_set_use_bicubic_upscale(bool p_enable) {
  868. glow_bicubic_upscale = p_enable;
  869. }
  870. void RasterizerSceneGLES3::environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) {
  871. }
  872. void RasterizerSceneGLES3::environment_set_ssao_quality(RS::EnvironmentSSAOQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to) {
  873. }
  874. void RasterizerSceneGLES3::environment_set_ssil_quality(RS::EnvironmentSSILQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to) {
  875. }
  876. void RasterizerSceneGLES3::environment_set_sdfgi_ray_count(RS::EnvironmentSDFGIRayCount p_ray_count) {
  877. }
  878. void RasterizerSceneGLES3::environment_set_sdfgi_frames_to_converge(RS::EnvironmentSDFGIFramesToConverge p_frames) {
  879. }
  880. void RasterizerSceneGLES3::environment_set_sdfgi_frames_to_update_light(RS::EnvironmentSDFGIFramesToUpdateLight p_update) {
  881. }
  882. void RasterizerSceneGLES3::environment_set_volumetric_fog_volume_size(int p_size, int p_depth) {
  883. }
  884. void RasterizerSceneGLES3::environment_set_volumetric_fog_filter_active(bool p_enable) {
  885. }
  886. Ref<Image> RasterizerSceneGLES3::environment_bake_panorama(RID p_env, bool p_bake_irradiance, const Size2i &p_size) {
  887. return Ref<Image>();
  888. }
  889. void RasterizerSceneGLES3::positional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) {
  890. }
  891. void RasterizerSceneGLES3::directional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) {
  892. }
  893. RID RasterizerSceneGLES3::fog_volume_instance_create(RID p_fog_volume) {
  894. return RID();
  895. }
  896. void RasterizerSceneGLES3::fog_volume_instance_set_transform(RID p_fog_volume_instance, const Transform3D &p_transform) {
  897. }
  898. void RasterizerSceneGLES3::fog_volume_instance_set_active(RID p_fog_volume_instance, bool p_active) {
  899. }
  900. RID RasterizerSceneGLES3::fog_volume_instance_get_volume(RID p_fog_volume_instance) const {
  901. return RID();
  902. }
  903. Vector3 RasterizerSceneGLES3::fog_volume_instance_get_position(RID p_fog_volume_instance) const {
  904. return Vector3();
  905. }
  906. RID RasterizerSceneGLES3::voxel_gi_instance_create(RID p_voxel_gi) {
  907. return RID();
  908. }
  909. void RasterizerSceneGLES3::voxel_gi_instance_set_transform_to_data(RID p_probe, const Transform3D &p_xform) {
  910. }
  911. bool RasterizerSceneGLES3::voxel_gi_needs_update(RID p_probe) const {
  912. return false;
  913. }
  914. void RasterizerSceneGLES3::voxel_gi_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RenderGeometryInstance *> &p_dynamic_objects) {
  915. }
  916. void RasterizerSceneGLES3::voxel_gi_set_quality(RS::VoxelGIQuality) {
  917. }
  918. void RasterizerSceneGLES3::_fill_render_list(RenderListType p_render_list, const RenderDataGLES3 *p_render_data, PassMode p_pass_mode, bool p_append) {
  919. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  920. if (p_render_list == RENDER_LIST_OPAQUE) {
  921. scene_state.used_screen_texture = false;
  922. scene_state.used_normal_texture = false;
  923. scene_state.used_depth_texture = false;
  924. }
  925. Plane near_plane;
  926. if (p_render_data->cam_orthogonal) {
  927. near_plane = Plane(-p_render_data->cam_transform.basis.get_column(Vector3::AXIS_Z), p_render_data->cam_transform.origin);
  928. near_plane.d += p_render_data->cam_projection.get_z_near();
  929. }
  930. float z_max = p_render_data->cam_projection.get_z_far() - p_render_data->cam_projection.get_z_near();
  931. RenderList *rl = &render_list[p_render_list];
  932. // Parse any updates on our geometry, updates surface caches and such
  933. _update_dirty_geometry_instances();
  934. if (!p_append) {
  935. rl->clear();
  936. if (p_render_list == RENDER_LIST_OPAQUE) {
  937. render_list[RENDER_LIST_ALPHA].clear(); //opaque fills alpha too
  938. }
  939. }
  940. //fill list
  941. for (int i = 0; i < (int)p_render_data->instances->size(); i++) {
  942. GeometryInstanceGLES3 *inst = static_cast<GeometryInstanceGLES3 *>((*p_render_data->instances)[i]);
  943. Vector3 center = inst->transform.origin;
  944. if (p_render_data->cam_orthogonal) {
  945. if (inst->use_aabb_center) {
  946. center = inst->transformed_aabb.get_support(-near_plane.normal);
  947. }
  948. inst->depth = near_plane.distance_to(center) - inst->sorting_offset;
  949. } else {
  950. if (inst->use_aabb_center) {
  951. center = inst->transformed_aabb.position + (inst->transformed_aabb.size * 0.5);
  952. }
  953. inst->depth = p_render_data->cam_transform.origin.distance_to(center) - inst->sorting_offset;
  954. }
  955. uint32_t depth_layer = CLAMP(int(inst->depth * 16 / z_max), 0, 15);
  956. uint32_t flags = inst->base_flags; //fill flags if appropriate
  957. if (inst->non_uniform_scale) {
  958. flags |= INSTANCE_DATA_FLAGS_NON_UNIFORM_SCALE;
  959. }
  960. // Sets the index values for lookup in the shader
  961. // This has to be done after _setup_lights was called this frame
  962. // TODO, check shadow status of lights here, if using shadows, skip here and add below
  963. if (p_pass_mode == PASS_MODE_COLOR) {
  964. if (inst->omni_light_count) {
  965. inst->omni_light_gl_cache.resize(inst->omni_light_count);
  966. for (uint32_t j = 0; j < inst->omni_light_count; j++) {
  967. inst->omni_light_gl_cache[j] = GLES3::LightStorage::get_singleton()->light_instance_get_gl_id(inst->omni_lights[j]);
  968. }
  969. }
  970. if (inst->spot_light_count) {
  971. inst->spot_light_gl_cache.resize(inst->spot_light_count);
  972. for (uint32_t j = 0; j < inst->spot_light_count; j++) {
  973. inst->spot_light_gl_cache[j] = GLES3::LightStorage::get_singleton()->light_instance_get_gl_id(inst->spot_lights[j]);
  974. }
  975. }
  976. }
  977. inst->flags_cache = flags;
  978. GeometryInstanceSurface *surf = inst->surface_caches;
  979. while (surf) {
  980. // LOD
  981. if (p_render_data->screen_mesh_lod_threshold > 0.0 && mesh_storage->mesh_surface_has_lod(surf->surface)) {
  982. // Get the LOD support points on the mesh AABB.
  983. Vector3 lod_support_min = inst->transformed_aabb.get_support(p_render_data->cam_transform.basis.get_column(Vector3::AXIS_Z));
  984. Vector3 lod_support_max = inst->transformed_aabb.get_support(-p_render_data->cam_transform.basis.get_column(Vector3::AXIS_Z));
  985. // Get the distances to those points on the AABB from the camera origin.
  986. float distance_min = (float)p_render_data->cam_transform.origin.distance_to(lod_support_min);
  987. float distance_max = (float)p_render_data->cam_transform.origin.distance_to(lod_support_max);
  988. float distance = 0.0;
  989. if (distance_min * distance_max < 0.0) {
  990. //crossing plane
  991. distance = 0.0;
  992. } else if (distance_min >= 0.0) {
  993. distance = distance_min;
  994. } else if (distance_max <= 0.0) {
  995. distance = -distance_max;
  996. }
  997. if (p_render_data->cam_orthogonal) {
  998. distance = 1.0;
  999. }
  1000. uint32_t indices = 0;
  1001. surf->lod_index = mesh_storage->mesh_surface_get_lod(surf->surface, inst->lod_model_scale * inst->lod_bias, distance * p_render_data->lod_distance_multiplier, p_render_data->screen_mesh_lod_threshold, indices);
  1002. /*
  1003. if (p_render_data->render_info) {
  1004. indices = _indices_to_primitives(surf->primitive, indices);
  1005. if (p_render_list == RENDER_LIST_OPAQUE) { //opaque
  1006. p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices;
  1007. } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow
  1008. p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices;
  1009. }
  1010. }
  1011. */
  1012. } else {
  1013. surf->lod_index = 0;
  1014. /*
  1015. if (p_render_data->render_info) {
  1016. uint32_t to_draw = mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface);
  1017. to_draw = _indices_to_primitives(surf->primitive, to_draw);
  1018. to_draw *= inst->instance_count;
  1019. if (p_render_list == RENDER_LIST_OPAQUE) { //opaque
  1020. p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface);
  1021. } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow
  1022. p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface);
  1023. }
  1024. }
  1025. */
  1026. }
  1027. // ADD Element
  1028. if (p_pass_mode == PASS_MODE_COLOR) {
  1029. #ifdef DEBUG_ENABLED
  1030. bool force_alpha = unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW);
  1031. #else
  1032. bool force_alpha = false;
  1033. #endif
  1034. if (!force_alpha && (surf->flags & GeometryInstanceSurface::FLAG_PASS_OPAQUE)) {
  1035. rl->add_element(surf);
  1036. }
  1037. if (force_alpha || (surf->flags & GeometryInstanceSurface::FLAG_PASS_ALPHA)) {
  1038. render_list[RENDER_LIST_ALPHA].add_element(surf);
  1039. }
  1040. if (surf->flags & GeometryInstanceSurface::FLAG_USES_SCREEN_TEXTURE) {
  1041. scene_state.used_screen_texture = true;
  1042. }
  1043. if (surf->flags & GeometryInstanceSurface::FLAG_USES_NORMAL_TEXTURE) {
  1044. scene_state.used_normal_texture = true;
  1045. }
  1046. if (surf->flags & GeometryInstanceSurface::FLAG_USES_DEPTH_TEXTURE) {
  1047. scene_state.used_depth_texture = true;
  1048. }
  1049. /*
  1050. Add elements here if there are shadows
  1051. */
  1052. } else if (p_pass_mode == PASS_MODE_SHADOW) {
  1053. if (surf->flags & GeometryInstanceSurface::FLAG_PASS_SHADOW) {
  1054. rl->add_element(surf);
  1055. }
  1056. } else {
  1057. if (surf->flags & (GeometryInstanceSurface::FLAG_PASS_DEPTH | GeometryInstanceSurface::FLAG_PASS_OPAQUE)) {
  1058. rl->add_element(surf);
  1059. }
  1060. }
  1061. surf->sort.depth_layer = depth_layer;
  1062. surf = surf->next;
  1063. }
  1064. }
  1065. }
  1066. // Needs to be called after _setup_lights so that directional_light_count is accurate.
  1067. void RasterizerSceneGLES3::_setup_environment(const RenderDataGLES3 *p_render_data, bool p_no_fog, const Size2i &p_screen_size, bool p_flip_y, const Color &p_default_bg_color, bool p_pancake_shadows) {
  1068. Projection correction;
  1069. correction.columns[1][1] = p_flip_y ? -1.0 : 1.0;
  1070. Projection projection = correction * p_render_data->cam_projection;
  1071. //store camera into ubo
  1072. GLES3::MaterialStorage::store_camera(projection, scene_state.ubo.projection_matrix);
  1073. GLES3::MaterialStorage::store_camera(projection.inverse(), scene_state.ubo.inv_projection_matrix);
  1074. GLES3::MaterialStorage::store_transform(p_render_data->cam_transform, scene_state.ubo.inv_view_matrix);
  1075. GLES3::MaterialStorage::store_transform(p_render_data->inv_cam_transform, scene_state.ubo.view_matrix);
  1076. scene_state.ubo.camera_visible_layers = p_render_data->camera_visible_layers;
  1077. if (p_render_data->view_count > 1) {
  1078. for (uint32_t v = 0; v < p_render_data->view_count; v++) {
  1079. projection = correction * p_render_data->view_projection[v];
  1080. GLES3::MaterialStorage::store_camera(projection, scene_state.multiview_ubo.projection_matrix_view[v]);
  1081. GLES3::MaterialStorage::store_camera(projection.inverse(), scene_state.multiview_ubo.inv_projection_matrix_view[v]);
  1082. scene_state.multiview_ubo.eye_offset[v][0] = p_render_data->view_eye_offset[v].x;
  1083. scene_state.multiview_ubo.eye_offset[v][1] = p_render_data->view_eye_offset[v].y;
  1084. scene_state.multiview_ubo.eye_offset[v][2] = p_render_data->view_eye_offset[v].z;
  1085. scene_state.multiview_ubo.eye_offset[v][3] = 0.0;
  1086. }
  1087. }
  1088. scene_state.ubo.directional_light_count = p_render_data->directional_light_count;
  1089. scene_state.ubo.z_far = p_render_data->z_far;
  1090. scene_state.ubo.z_near = p_render_data->z_near;
  1091. scene_state.ubo.viewport_size[0] = p_screen_size.x;
  1092. scene_state.ubo.viewport_size[1] = p_screen_size.y;
  1093. Size2 screen_pixel_size = Vector2(1.0, 1.0) / Size2(p_screen_size);
  1094. scene_state.ubo.screen_pixel_size[0] = screen_pixel_size.x;
  1095. scene_state.ubo.screen_pixel_size[1] = screen_pixel_size.y;
  1096. //time global variables
  1097. scene_state.ubo.time = time;
  1098. if (is_environment(p_render_data->environment)) {
  1099. RS::EnvironmentBG env_bg = environment_get_background(p_render_data->environment);
  1100. RS::EnvironmentAmbientSource ambient_src = environment_get_ambient_source(p_render_data->environment);
  1101. float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_render_data->environment);
  1102. scene_state.ubo.ambient_light_color_energy[3] = bg_energy_multiplier;
  1103. scene_state.ubo.ambient_color_sky_mix = environment_get_ambient_sky_contribution(p_render_data->environment);
  1104. //ambient
  1105. if (ambient_src == RS::ENV_AMBIENT_SOURCE_BG && (env_bg == RS::ENV_BG_CLEAR_COLOR || env_bg == RS::ENV_BG_COLOR)) {
  1106. Color color = env_bg == RS::ENV_BG_CLEAR_COLOR ? p_default_bg_color : environment_get_bg_color(p_render_data->environment);
  1107. color = color.srgb_to_linear();
  1108. scene_state.ubo.ambient_light_color_energy[0] = color.r * bg_energy_multiplier;
  1109. scene_state.ubo.ambient_light_color_energy[1] = color.g * bg_energy_multiplier;
  1110. scene_state.ubo.ambient_light_color_energy[2] = color.b * bg_energy_multiplier;
  1111. scene_state.ubo.use_ambient_light = true;
  1112. scene_state.ubo.use_ambient_cubemap = false;
  1113. } else {
  1114. float energy = environment_get_ambient_light_energy(p_render_data->environment);
  1115. Color color = environment_get_ambient_light(p_render_data->environment);
  1116. color = color.srgb_to_linear();
  1117. scene_state.ubo.ambient_light_color_energy[0] = color.r * energy;
  1118. scene_state.ubo.ambient_light_color_energy[1] = color.g * energy;
  1119. scene_state.ubo.ambient_light_color_energy[2] = color.b * energy;
  1120. Basis sky_transform = environment_get_sky_orientation(p_render_data->environment);
  1121. sky_transform = sky_transform.inverse() * p_render_data->cam_transform.basis;
  1122. GLES3::MaterialStorage::store_transform_3x3(sky_transform, scene_state.ubo.radiance_inverse_xform);
  1123. scene_state.ubo.use_ambient_cubemap = (ambient_src == RS::ENV_AMBIENT_SOURCE_BG && env_bg == RS::ENV_BG_SKY) || ambient_src == RS::ENV_AMBIENT_SOURCE_SKY;
  1124. scene_state.ubo.use_ambient_light = scene_state.ubo.use_ambient_cubemap || ambient_src == RS::ENV_AMBIENT_SOURCE_COLOR;
  1125. }
  1126. //specular
  1127. RS::EnvironmentReflectionSource ref_src = environment_get_reflection_source(p_render_data->environment);
  1128. if ((ref_src == RS::ENV_REFLECTION_SOURCE_BG && env_bg == RS::ENV_BG_SKY) || ref_src == RS::ENV_REFLECTION_SOURCE_SKY) {
  1129. scene_state.ubo.use_reflection_cubemap = true;
  1130. } else {
  1131. scene_state.ubo.use_reflection_cubemap = false;
  1132. }
  1133. scene_state.ubo.fog_enabled = environment_get_fog_enabled(p_render_data->environment);
  1134. scene_state.ubo.fog_density = environment_get_fog_density(p_render_data->environment);
  1135. scene_state.ubo.fog_height = environment_get_fog_height(p_render_data->environment);
  1136. scene_state.ubo.fog_height_density = environment_get_fog_height_density(p_render_data->environment);
  1137. scene_state.ubo.fog_aerial_perspective = environment_get_fog_aerial_perspective(p_render_data->environment);
  1138. Color fog_color = environment_get_fog_light_color(p_render_data->environment).srgb_to_linear();
  1139. float fog_energy = environment_get_fog_light_energy(p_render_data->environment);
  1140. scene_state.ubo.fog_light_color[0] = fog_color.r * fog_energy;
  1141. scene_state.ubo.fog_light_color[1] = fog_color.g * fog_energy;
  1142. scene_state.ubo.fog_light_color[2] = fog_color.b * fog_energy;
  1143. scene_state.ubo.fog_sun_scatter = environment_get_fog_sun_scatter(p_render_data->environment);
  1144. } else {
  1145. }
  1146. if (p_render_data->camera_attributes.is_valid()) {
  1147. scene_state.ubo.emissive_exposure_normalization = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  1148. scene_state.ubo.IBL_exposure_normalization = 1.0;
  1149. if (is_environment(p_render_data->environment)) {
  1150. RID sky_rid = environment_get_sky(p_render_data->environment);
  1151. if (sky_rid.is_valid()) {
  1152. float current_exposure = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes) * environment_get_bg_intensity(p_render_data->environment);
  1153. scene_state.ubo.IBL_exposure_normalization = current_exposure / MAX(0.001, sky_get_baked_exposure(sky_rid));
  1154. }
  1155. }
  1156. } else if (scene_state.ubo.emissive_exposure_normalization > 0.0) {
  1157. // This branch is triggered when using render_material().
  1158. // Emissive is set outside the function, so don't set it.
  1159. // IBL isn't used don't set it.
  1160. } else {
  1161. scene_state.ubo.emissive_exposure_normalization = 1.0;
  1162. scene_state.ubo.IBL_exposure_normalization = 1.0;
  1163. }
  1164. if (scene_state.ubo_buffer == 0) {
  1165. glGenBuffers(1, &scene_state.ubo_buffer);
  1166. }
  1167. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DATA_UNIFORM_LOCATION, scene_state.ubo_buffer);
  1168. glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::UBO), &scene_state.ubo, GL_STREAM_DRAW);
  1169. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1170. if (p_render_data->view_count > 1) {
  1171. if (scene_state.multiview_buffer == 0) {
  1172. glGenBuffers(1, &scene_state.multiview_buffer);
  1173. }
  1174. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
  1175. glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::MultiviewUBO), &scene_state.multiview_ubo, GL_STREAM_DRAW);
  1176. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1177. }
  1178. }
  1179. // Puts lights into Uniform Buffers. Needs to be called before _fill_list as this caches the index of each light in the Uniform Buffer
  1180. void RasterizerSceneGLES3::_setup_lights(const RenderDataGLES3 *p_render_data, bool p_using_shadows, uint32_t &r_directional_light_count, uint32_t &r_omni_light_count, uint32_t &r_spot_light_count) {
  1181. GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
  1182. GLES3::Config *config = GLES3::Config::get_singleton();
  1183. const Transform3D inverse_transform = p_render_data->inv_cam_transform;
  1184. const PagedArray<RID> &lights = *p_render_data->lights;
  1185. r_directional_light_count = 0;
  1186. r_omni_light_count = 0;
  1187. r_spot_light_count = 0;
  1188. int num_lights = lights.size();
  1189. for (int i = 0; i < num_lights; i++) {
  1190. GLES3::LightInstance *li = GLES3::LightStorage::get_singleton()->get_light_instance(lights[i]);
  1191. if (!li) {
  1192. continue;
  1193. }
  1194. RID base = li->light;
  1195. ERR_CONTINUE(base.is_null());
  1196. RS::LightType type = light_storage->light_get_type(base);
  1197. switch (type) {
  1198. case RS::LIGHT_DIRECTIONAL: {
  1199. if (r_directional_light_count >= RendererSceneRender::MAX_DIRECTIONAL_LIGHTS || light_storage->light_directional_get_sky_mode(base) == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
  1200. continue;
  1201. }
  1202. DirectionalLightData &light_data = scene_state.directional_lights[r_directional_light_count];
  1203. Transform3D light_transform = li->transform;
  1204. Vector3 direction = inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, 1))).normalized();
  1205. light_data.direction[0] = direction.x;
  1206. light_data.direction[1] = direction.y;
  1207. light_data.direction[2] = direction.z;
  1208. float sign = light_storage->light_is_negative(base) ? -1 : 1;
  1209. light_data.energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY);
  1210. if (is_using_physical_light_units()) {
  1211. light_data.energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
  1212. } else {
  1213. light_data.energy *= Math_PI;
  1214. }
  1215. if (p_render_data->camera_attributes.is_valid()) {
  1216. light_data.energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  1217. }
  1218. Color linear_col = light_storage->light_get_color(base).srgb_to_linear();
  1219. light_data.color[0] = linear_col.r;
  1220. light_data.color[1] = linear_col.g;
  1221. light_data.color[2] = linear_col.b;
  1222. float size = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  1223. light_data.size = 1.0 - Math::cos(Math::deg_to_rad(size)); //angle to cosine offset
  1224. light_data.specular = light_storage->light_get_param(base, RS::LIGHT_PARAM_SPECULAR);
  1225. r_directional_light_count++;
  1226. } break;
  1227. case RS::LIGHT_OMNI: {
  1228. if (r_omni_light_count >= (uint32_t)config->max_renderable_lights) {
  1229. continue;
  1230. }
  1231. const real_t distance = p_render_data->cam_transform.origin.distance_to(li->transform.origin);
  1232. if (light_storage->light_is_distance_fade_enabled(li->light)) {
  1233. const float fade_begin = light_storage->light_get_distance_fade_begin(li->light);
  1234. const float fade_length = light_storage->light_get_distance_fade_length(li->light);
  1235. if (distance > fade_begin) {
  1236. if (distance > fade_begin + fade_length) {
  1237. // Out of range, don't draw this light to improve performance.
  1238. continue;
  1239. }
  1240. }
  1241. }
  1242. scene_state.omni_light_sort[r_omni_light_count].instance = li;
  1243. scene_state.omni_light_sort[r_omni_light_count].depth = distance;
  1244. r_omni_light_count++;
  1245. } break;
  1246. case RS::LIGHT_SPOT: {
  1247. if (r_spot_light_count >= (uint32_t)config->max_renderable_lights) {
  1248. continue;
  1249. }
  1250. const real_t distance = p_render_data->cam_transform.origin.distance_to(li->transform.origin);
  1251. if (light_storage->light_is_distance_fade_enabled(li->light)) {
  1252. const float fade_begin = light_storage->light_get_distance_fade_begin(li->light);
  1253. const float fade_length = light_storage->light_get_distance_fade_length(li->light);
  1254. if (distance > fade_begin) {
  1255. if (distance > fade_begin + fade_length) {
  1256. // Out of range, don't draw this light to improve performance.
  1257. continue;
  1258. }
  1259. }
  1260. }
  1261. scene_state.spot_light_sort[r_spot_light_count].instance = li;
  1262. scene_state.spot_light_sort[r_spot_light_count].depth = distance;
  1263. r_spot_light_count++;
  1264. } break;
  1265. }
  1266. }
  1267. if (r_omni_light_count) {
  1268. SortArray<InstanceSort<GLES3::LightInstance>> sorter;
  1269. sorter.sort(scene_state.omni_light_sort, r_omni_light_count);
  1270. }
  1271. if (r_spot_light_count) {
  1272. SortArray<InstanceSort<GLES3::LightInstance>> sorter;
  1273. sorter.sort(scene_state.spot_light_sort, r_spot_light_count);
  1274. }
  1275. for (uint32_t i = 0; i < (r_omni_light_count + r_spot_light_count); i++) {
  1276. uint32_t index = (i < r_omni_light_count) ? i : i - (r_omni_light_count);
  1277. LightData &light_data = (i < r_omni_light_count) ? scene_state.omni_lights[index] : scene_state.spot_lights[index];
  1278. RS::LightType type = (i < r_omni_light_count) ? RS::LIGHT_OMNI : RS::LIGHT_SPOT;
  1279. GLES3::LightInstance *li = (i < r_omni_light_count) ? scene_state.omni_light_sort[index].instance : scene_state.spot_light_sort[index].instance;
  1280. real_t distance = (i < r_omni_light_count) ? scene_state.omni_light_sort[index].depth : scene_state.spot_light_sort[index].depth;
  1281. RID base = li->light;
  1282. li->gl_id = index;
  1283. Transform3D light_transform = li->transform;
  1284. Vector3 pos = inverse_transform.xform(light_transform.origin);
  1285. light_data.position[0] = pos.x;
  1286. light_data.position[1] = pos.y;
  1287. light_data.position[2] = pos.z;
  1288. float radius = MAX(0.001, light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE));
  1289. light_data.inv_radius = 1.0 / radius;
  1290. Vector3 direction = inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, -1))).normalized();
  1291. light_data.direction[0] = direction.x;
  1292. light_data.direction[1] = direction.y;
  1293. light_data.direction[2] = direction.z;
  1294. float size = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  1295. light_data.size = size;
  1296. float sign = light_storage->light_is_negative(base) ? -1 : 1;
  1297. Color linear_col = light_storage->light_get_color(base).srgb_to_linear();
  1298. // Reuse fade begin, fade length and distance for shadow LOD determination later.
  1299. float fade_begin = 0.0;
  1300. float fade_length = 0.0;
  1301. float fade = 1.0;
  1302. if (light_storage->light_is_distance_fade_enabled(li->light)) {
  1303. fade_begin = light_storage->light_get_distance_fade_begin(li->light);
  1304. fade_length = light_storage->light_get_distance_fade_length(li->light);
  1305. if (distance > fade_begin) {
  1306. // Use `smoothstep()` to make opacity changes more gradual and less noticeable to the player.
  1307. fade = Math::smoothstep(0.0f, 1.0f, 1.0f - float(distance - fade_begin) / fade_length);
  1308. }
  1309. }
  1310. float energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY) * fade;
  1311. if (is_using_physical_light_units()) {
  1312. energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
  1313. // Convert from Luminous Power to Luminous Intensity
  1314. if (type == RS::LIGHT_OMNI) {
  1315. energy *= 1.0 / (Math_PI * 4.0);
  1316. } else {
  1317. // Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
  1318. // We make this assumption to keep them easy to control.
  1319. energy *= 1.0 / Math_PI;
  1320. }
  1321. } else {
  1322. energy *= Math_PI;
  1323. }
  1324. if (p_render_data->camera_attributes.is_valid()) {
  1325. energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  1326. }
  1327. light_data.color[0] = linear_col.r * energy;
  1328. light_data.color[1] = linear_col.g * energy;
  1329. light_data.color[2] = linear_col.b * energy;
  1330. light_data.attenuation = light_storage->light_get_param(base, RS::LIGHT_PARAM_ATTENUATION);
  1331. light_data.inv_spot_attenuation = 1.0f / light_storage->light_get_param(base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  1332. float spot_angle = light_storage->light_get_param(base, RS::LIGHT_PARAM_SPOT_ANGLE);
  1333. light_data.cos_spot_angle = Math::cos(Math::deg_to_rad(spot_angle));
  1334. light_data.specular_amount = light_storage->light_get_param(base, RS::LIGHT_PARAM_SPECULAR) * 2.0;
  1335. light_data.shadow_opacity = 0.0;
  1336. }
  1337. // TODO, to avoid stalls, should rotate between 3 buffers based on frame index.
  1338. // TODO, consider mapping the buffer as in 2D
  1339. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_OMNILIGHT_UNIFORM_LOCATION, scene_state.omni_light_buffer);
  1340. if (r_omni_light_count) {
  1341. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(LightData) * r_omni_light_count, scene_state.omni_lights);
  1342. }
  1343. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_SPOTLIGHT_UNIFORM_LOCATION, scene_state.spot_light_buffer);
  1344. if (r_spot_light_count) {
  1345. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(LightData) * r_spot_light_count, scene_state.spot_lights);
  1346. }
  1347. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DIRECTIONAL_LIGHT_UNIFORM_LOCATION, scene_state.directional_light_buffer);
  1348. if (r_directional_light_count) {
  1349. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(DirectionalLightData) * r_directional_light_count, scene_state.directional_lights);
  1350. }
  1351. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1352. }
  1353. void RasterizerSceneGLES3::render_scene(const Ref<RenderSceneBuffers> &p_render_buffers, const CameraData *p_camera_data, const CameraData *p_prev_camera_data, const PagedArray<RenderGeometryInstance *> &p_instances, const PagedArray<RID> &p_lights, const PagedArray<RID> &p_reflection_probes, const PagedArray<RID> &p_voxel_gi_instances, const PagedArray<RID> &p_decals, const PagedArray<RID> &p_lightmaps, const PagedArray<RID> &p_fog_volumes, RID p_environment, RID p_camera_attributes, RID p_shadow_atlas, RID p_occluder_debug_tex, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass, float p_screen_mesh_lod_threshold, const RenderShadowData *p_render_shadows, int p_render_shadow_count, const RenderSDFGIData *p_render_sdfgi_regions, int p_render_sdfgi_region_count, const RenderSDFGIUpdateData *p_sdfgi_update_data, RenderingMethod::RenderInfo *r_render_info) {
  1354. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1355. GLES3::Config *config = GLES3::Config::get_singleton();
  1356. RENDER_TIMESTAMP("Setup 3D Scene");
  1357. Ref<RenderSceneBuffersGLES3> rb;
  1358. if (p_render_buffers.is_valid()) {
  1359. rb = p_render_buffers;
  1360. ERR_FAIL_COND(rb.is_null());
  1361. }
  1362. GLES3::RenderTarget *rt = texture_storage->get_render_target(rb->render_target);
  1363. ERR_FAIL_COND(!rt);
  1364. // Assign render data
  1365. // Use the format from rendererRD
  1366. RenderDataGLES3 render_data;
  1367. {
  1368. render_data.render_buffers = rb;
  1369. render_data.transparent_bg = rb.is_valid() ? rb->is_transparent : false;
  1370. // Our first camera is used by default
  1371. render_data.cam_transform = p_camera_data->main_transform;
  1372. render_data.inv_cam_transform = render_data.cam_transform.affine_inverse();
  1373. render_data.cam_projection = p_camera_data->main_projection;
  1374. render_data.cam_orthogonal = p_camera_data->is_orthogonal;
  1375. render_data.camera_visible_layers = p_camera_data->visible_layers;
  1376. render_data.view_count = p_camera_data->view_count;
  1377. for (uint32_t v = 0; v < p_camera_data->view_count; v++) {
  1378. render_data.view_eye_offset[v] = p_camera_data->view_offset[v].origin;
  1379. render_data.view_projection[v] = p_camera_data->view_projection[v];
  1380. }
  1381. render_data.z_near = p_camera_data->main_projection.get_z_near();
  1382. render_data.z_far = p_camera_data->main_projection.get_z_far();
  1383. render_data.instances = &p_instances;
  1384. render_data.lights = &p_lights;
  1385. render_data.reflection_probes = &p_reflection_probes;
  1386. render_data.environment = p_environment;
  1387. render_data.camera_attributes = p_camera_attributes;
  1388. render_data.reflection_probe = p_reflection_probe;
  1389. render_data.reflection_probe_pass = p_reflection_probe_pass;
  1390. // this should be the same for all cameras..
  1391. render_data.lod_distance_multiplier = p_camera_data->main_projection.get_lod_multiplier();
  1392. if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) {
  1393. render_data.screen_mesh_lod_threshold = 0.0;
  1394. } else {
  1395. render_data.screen_mesh_lod_threshold = p_screen_mesh_lod_threshold;
  1396. }
  1397. render_data.render_info = r_render_info;
  1398. }
  1399. PagedArray<RID> empty;
  1400. if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
  1401. render_data.lights = &empty;
  1402. render_data.reflection_probes = &empty;
  1403. }
  1404. bool reverse_cull = render_data.cam_transform.basis.determinant() < 0;
  1405. ///////////
  1406. // Fill Light lists here
  1407. //////////
  1408. GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
  1409. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer);
  1410. Color clear_color;
  1411. if (p_render_buffers.is_valid()) {
  1412. clear_color = texture_storage->render_target_get_clear_request_color(rb->render_target);
  1413. } else {
  1414. clear_color = texture_storage->get_default_clear_color();
  1415. }
  1416. bool fb_cleared = false;
  1417. Size2i screen_size;
  1418. screen_size.x = rb->width;
  1419. screen_size.y = rb->height;
  1420. bool use_wireframe = get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_WIREFRAME;
  1421. SceneState::TonemapUBO tonemap_ubo;
  1422. if (render_data.environment.is_valid()) {
  1423. tonemap_ubo.exposure = environment_get_exposure(render_data.environment);
  1424. tonemap_ubo.white = environment_get_white(render_data.environment);
  1425. tonemap_ubo.tonemapper = int32_t(environment_get_tone_mapper(render_data.environment));
  1426. }
  1427. if (scene_state.tonemap_buffer == 0) {
  1428. // Only create if using 3D
  1429. glGenBuffers(1, &scene_state.tonemap_buffer);
  1430. }
  1431. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_TONEMAP_UNIFORM_LOCATION, scene_state.tonemap_buffer);
  1432. glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::TonemapUBO), &tonemap_ubo, GL_STREAM_DRAW);
  1433. scene_state.ubo.emissive_exposure_normalization = -1.0; // Use default exposure normalization.
  1434. bool flip_y = !render_data.reflection_probe.is_valid();
  1435. if (rt->overridden.color.is_valid()) {
  1436. // If we've overridden the render target's color texture, then don't render upside down.
  1437. // We're probably rendering directly to an XR device.
  1438. flip_y = false;
  1439. }
  1440. if (!flip_y) {
  1441. // If we're rendering right-side up, then we need to change the winding order.
  1442. glFrontFace(GL_CW);
  1443. }
  1444. _setup_lights(&render_data, false, render_data.directional_light_count, render_data.omni_light_count, render_data.spot_light_count);
  1445. _setup_environment(&render_data, render_data.reflection_probe.is_valid(), screen_size, flip_y, clear_color, false);
  1446. _fill_render_list(RENDER_LIST_OPAQUE, &render_data, PASS_MODE_COLOR);
  1447. render_list[RENDER_LIST_OPAQUE].sort_by_key();
  1448. render_list[RENDER_LIST_ALPHA].sort_by_reverse_depth_and_priority();
  1449. bool draw_sky = false;
  1450. bool draw_sky_fog_only = false;
  1451. bool keep_color = false;
  1452. float sky_energy_multiplier = 1.0;
  1453. if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW) {
  1454. clear_color = Color(0, 0, 0, 1); //in overdraw mode, BG should always be black
  1455. } else if (render_data.environment.is_valid()) {
  1456. RS::EnvironmentBG bg_mode = environment_get_background(render_data.environment);
  1457. float bg_energy_multiplier = environment_get_bg_energy_multiplier(render_data.environment);
  1458. bg_energy_multiplier *= environment_get_bg_intensity(render_data.environment);
  1459. if (render_data.camera_attributes.is_valid()) {
  1460. bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(render_data.camera_attributes);
  1461. }
  1462. switch (bg_mode) {
  1463. case RS::ENV_BG_CLEAR_COLOR: {
  1464. clear_color.r *= bg_energy_multiplier;
  1465. clear_color.g *= bg_energy_multiplier;
  1466. clear_color.b *= bg_energy_multiplier;
  1467. if (environment_get_fog_enabled(render_data.environment)) {
  1468. draw_sky_fog_only = true;
  1469. GLES3::MaterialStorage::get_singleton()->material_set_param(sky_globals.fog_material, "clear_color", Variant(clear_color));
  1470. }
  1471. } break;
  1472. case RS::ENV_BG_COLOR: {
  1473. clear_color = environment_get_bg_color(render_data.environment);
  1474. clear_color.r *= bg_energy_multiplier;
  1475. clear_color.g *= bg_energy_multiplier;
  1476. clear_color.b *= bg_energy_multiplier;
  1477. if (environment_get_fog_enabled(render_data.environment)) {
  1478. draw_sky_fog_only = true;
  1479. GLES3::MaterialStorage::get_singleton()->material_set_param(sky_globals.fog_material, "clear_color", Variant(clear_color));
  1480. }
  1481. } break;
  1482. case RS::ENV_BG_SKY: {
  1483. draw_sky = true;
  1484. } break;
  1485. case RS::ENV_BG_CANVAS: {
  1486. keep_color = true;
  1487. } break;
  1488. case RS::ENV_BG_KEEP: {
  1489. keep_color = true;
  1490. } break;
  1491. case RS::ENV_BG_CAMERA_FEED: {
  1492. } break;
  1493. default: {
  1494. }
  1495. }
  1496. // setup sky if used for ambient, reflections, or background
  1497. if (draw_sky || draw_sky_fog_only || environment_get_reflection_source(render_data.environment) == RS::ENV_REFLECTION_SOURCE_SKY || environment_get_ambient_source(render_data.environment) == RS::ENV_AMBIENT_SOURCE_SKY) {
  1498. RENDER_TIMESTAMP("Setup Sky");
  1499. Projection projection = render_data.cam_projection;
  1500. if (render_data.reflection_probe.is_valid()) {
  1501. Projection correction;
  1502. correction.columns[1][1] = -1.0;
  1503. projection = correction * render_data.cam_projection;
  1504. }
  1505. sky_energy_multiplier *= bg_energy_multiplier;
  1506. _setup_sky(&render_data, *render_data.lights, projection, render_data.cam_transform, screen_size);
  1507. if (environment_get_sky(render_data.environment).is_valid()) {
  1508. if (environment_get_reflection_source(render_data.environment) == RS::ENV_REFLECTION_SOURCE_SKY || environment_get_ambient_source(render_data.environment) == RS::ENV_AMBIENT_SOURCE_SKY || (environment_get_reflection_source(render_data.environment) == RS::ENV_REFLECTION_SOURCE_BG && environment_get_background(render_data.environment) == RS::ENV_BG_SKY)) {
  1509. _update_sky_radiance(render_data.environment, projection, render_data.cam_transform, sky_energy_multiplier);
  1510. }
  1511. } else {
  1512. // do not try to draw sky if invalid
  1513. draw_sky = false;
  1514. }
  1515. }
  1516. }
  1517. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  1518. glViewport(0, 0, rb->width, rb->height);
  1519. glCullFace(GL_BACK);
  1520. glEnable(GL_CULL_FACE);
  1521. scene_state.cull_mode = GLES3::SceneShaderData::CULL_BACK;
  1522. // Do depth prepass if it's explicitly enabled
  1523. bool use_depth_prepass = config->use_depth_prepass;
  1524. // Don't do depth prepass we are rendering overdraw
  1525. use_depth_prepass = use_depth_prepass && get_debug_draw_mode() != RS::VIEWPORT_DEBUG_DRAW_OVERDRAW;
  1526. if (use_depth_prepass) {
  1527. RENDER_TIMESTAMP("Depth Prepass");
  1528. //pre z pass
  1529. glDisable(GL_BLEND);
  1530. glDepthMask(GL_TRUE);
  1531. glEnable(GL_DEPTH_TEST);
  1532. glDepthFunc(GL_LEQUAL);
  1533. glDisable(GL_SCISSOR_TEST);
  1534. glColorMask(0, 0, 0, 0);
  1535. glClearDepth(1.0f);
  1536. glClear(GL_DEPTH_BUFFER_BIT);
  1537. uint64_t spec_constant = SceneShaderGLES3::DISABLE_FOG | SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL |
  1538. SceneShaderGLES3::DISABLE_LIGHTMAP | SceneShaderGLES3::DISABLE_LIGHT_OMNI |
  1539. SceneShaderGLES3::DISABLE_LIGHT_SPOT;
  1540. RenderListParameters render_list_params(render_list[RENDER_LIST_OPAQUE].elements.ptr(), render_list[RENDER_LIST_OPAQUE].elements.size(), reverse_cull, spec_constant, use_wireframe);
  1541. _render_list_template<PASS_MODE_DEPTH>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_OPAQUE].elements.size());
  1542. glColorMask(1, 1, 1, 1);
  1543. fb_cleared = true;
  1544. scene_state.used_depth_prepass = true;
  1545. } else {
  1546. scene_state.used_depth_prepass = false;
  1547. }
  1548. glBlendEquation(GL_FUNC_ADD);
  1549. if (render_data.transparent_bg) {
  1550. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1551. glEnable(GL_BLEND);
  1552. } else {
  1553. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1554. glDisable(GL_BLEND);
  1555. }
  1556. scene_state.current_blend_mode = GLES3::SceneShaderData::BLEND_MODE_MIX;
  1557. glEnable(GL_DEPTH_TEST);
  1558. glDepthFunc(GL_LEQUAL);
  1559. glDepthMask(GL_TRUE);
  1560. scene_state.current_depth_test = GLES3::SceneShaderData::DEPTH_TEST_ENABLED;
  1561. scene_state.current_depth_draw = GLES3::SceneShaderData::DEPTH_DRAW_ALWAYS;
  1562. if (!fb_cleared) {
  1563. glClearDepth(1.0f);
  1564. glClear(GL_DEPTH_BUFFER_BIT);
  1565. }
  1566. if (!keep_color) {
  1567. glClearBufferfv(GL_COLOR, 0, clear_color.components);
  1568. }
  1569. RENDER_TIMESTAMP("Render Opaque Pass");
  1570. uint64_t spec_constant_base_flags = 0;
  1571. {
  1572. // Specialization Constants that apply for entire rendering pass.
  1573. if (render_data.directional_light_count == 0) {
  1574. spec_constant_base_flags |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
  1575. }
  1576. if (render_data.environment.is_null() || (render_data.environment.is_valid() && !environment_get_fog_enabled(render_data.environment))) {
  1577. spec_constant_base_flags |= SceneShaderGLES3::DISABLE_FOG;
  1578. }
  1579. }
  1580. // Render Opaque Objects.
  1581. RenderListParameters render_list_params(render_list[RENDER_LIST_OPAQUE].elements.ptr(), render_list[RENDER_LIST_OPAQUE].elements.size(), reverse_cull, spec_constant_base_flags, use_wireframe);
  1582. _render_list_template<PASS_MODE_COLOR>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_OPAQUE].elements.size());
  1583. glDepthMask(GL_FALSE);
  1584. scene_state.current_depth_draw = GLES3::SceneShaderData::DEPTH_DRAW_DISABLED;
  1585. if (draw_sky) {
  1586. RENDER_TIMESTAMP("Render Sky");
  1587. glEnable(GL_DEPTH_TEST);
  1588. glDisable(GL_BLEND);
  1589. glEnable(GL_CULL_FACE);
  1590. glCullFace(GL_BACK);
  1591. scene_state.current_depth_test = GLES3::SceneShaderData::DEPTH_TEST_ENABLED;
  1592. scene_state.cull_mode = GLES3::SceneShaderData::CULL_BACK;
  1593. _draw_sky(render_data.environment, render_data.cam_projection, render_data.cam_transform, sky_energy_multiplier, p_camera_data->view_count > 1, flip_y);
  1594. }
  1595. if (scene_state.used_screen_texture || scene_state.used_depth_texture) {
  1596. texture_storage->copy_scene_to_backbuffer(rt, scene_state.used_screen_texture, scene_state.used_depth_texture);
  1597. glBindFramebuffer(GL_READ_FRAMEBUFFER, rt->fbo);
  1598. glReadBuffer(GL_COLOR_ATTACHMENT0);
  1599. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, rt->backbuffer_fbo);
  1600. if (scene_state.used_screen_texture) {
  1601. glBlitFramebuffer(0, 0, rt->size.x, rt->size.y,
  1602. 0, 0, rt->size.x, rt->size.y,
  1603. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1604. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 5);
  1605. glBindTexture(GL_TEXTURE_2D, rt->backbuffer);
  1606. }
  1607. if (scene_state.used_depth_texture) {
  1608. glBlitFramebuffer(0, 0, rt->size.x, rt->size.y,
  1609. 0, 0, rt->size.x, rt->size.y,
  1610. GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  1611. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 6);
  1612. glBindTexture(GL_TEXTURE_2D, rt->backbuffer_depth);
  1613. }
  1614. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  1615. }
  1616. RENDER_TIMESTAMP("Render 3D Transparent Pass");
  1617. glEnable(GL_BLEND);
  1618. //Render transparent pass
  1619. RenderListParameters render_list_params_alpha(render_list[RENDER_LIST_ALPHA].elements.ptr(), render_list[RENDER_LIST_ALPHA].elements.size(), reverse_cull, spec_constant_base_flags, use_wireframe);
  1620. _render_list_template<PASS_MODE_COLOR_TRANSPARENT>(&render_list_params_alpha, &render_data, 0, render_list[RENDER_LIST_ALPHA].elements.size(), true);
  1621. if (!flip_y) {
  1622. // Restore the default winding order.
  1623. glFrontFace(GL_CCW);
  1624. }
  1625. if (rb.is_valid()) {
  1626. _render_buffers_debug_draw(rb, p_shadow_atlas, p_occluder_debug_tex);
  1627. }
  1628. glDisable(GL_BLEND);
  1629. texture_storage->render_target_disable_clear_request(rb->render_target);
  1630. }
  1631. template <PassMode p_pass_mode>
  1632. void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params, const RenderDataGLES3 *p_render_data, uint32_t p_from_element, uint32_t p_to_element, bool p_alpha_pass) {
  1633. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  1634. GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
  1635. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  1636. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1637. GLES3::Config *config = GLES3::Config::get_singleton();
  1638. GLuint prev_vertex_array_gl = 0;
  1639. GLuint prev_index_array_gl = 0;
  1640. GLES3::SceneMaterialData *prev_material_data = nullptr;
  1641. GLES3::SceneShaderData *prev_shader = nullptr;
  1642. GeometryInstanceGLES3 *prev_inst = nullptr;
  1643. SceneShaderGLES3::ShaderVariant prev_variant = SceneShaderGLES3::ShaderVariant::MODE_COLOR;
  1644. SceneShaderGLES3::ShaderVariant shader_variant = SceneShaderGLES3::MODE_COLOR; // Assigned to silence wrong -Wmaybe-initialized
  1645. uint64_t prev_spec_constants = 0;
  1646. // Specializations constants used by all instances in the scene.
  1647. uint64_t base_spec_constants = p_params->spec_constant_base_flags;
  1648. if (p_render_data->view_count > 1) {
  1649. base_spec_constants |= SceneShaderGLES3::USE_MULTIVIEW;
  1650. }
  1651. switch (p_pass_mode) {
  1652. case PASS_MODE_COLOR:
  1653. case PASS_MODE_COLOR_TRANSPARENT: {
  1654. } break;
  1655. case PASS_MODE_COLOR_ADDITIVE: {
  1656. shader_variant = SceneShaderGLES3::MODE_ADDITIVE;
  1657. } break;
  1658. case PASS_MODE_SHADOW:
  1659. case PASS_MODE_DEPTH: {
  1660. shader_variant = SceneShaderGLES3::MODE_DEPTH;
  1661. } break;
  1662. }
  1663. if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
  1664. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 2);
  1665. GLuint texture_to_bind = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_CUBEMAP_BLACK))->tex_id;
  1666. if (p_render_data->environment.is_valid()) {
  1667. Sky *sky = sky_owner.get_or_null(environment_get_sky(p_render_data->environment));
  1668. if (sky && sky->radiance != 0) {
  1669. texture_to_bind = sky->radiance;
  1670. base_spec_constants |= SceneShaderGLES3::USE_RADIANCE_MAP;
  1671. }
  1672. glBindTexture(GL_TEXTURE_CUBE_MAP, texture_to_bind);
  1673. }
  1674. }
  1675. bool should_request_redraw = false;
  1676. for (uint32_t i = p_from_element; i < p_to_element; i++) {
  1677. const GeometryInstanceSurface *surf = p_params->elements[i];
  1678. GeometryInstanceGLES3 *inst = surf->owner;
  1679. if (p_pass_mode == PASS_MODE_COLOR && !(surf->flags & GeometryInstanceSurface::FLAG_PASS_OPAQUE)) {
  1680. continue; // Objects with "Depth-prepass" transparency are included in both render lists, but should only be rendered in the transparent pass
  1681. }
  1682. if (inst->instance_count == 0) {
  1683. continue;
  1684. }
  1685. GLES3::SceneShaderData *shader;
  1686. GLES3::SceneMaterialData *material_data;
  1687. void *mesh_surface;
  1688. if constexpr (p_pass_mode == PASS_MODE_SHADOW) {
  1689. shader = surf->shader_shadow;
  1690. material_data = surf->material_shadow;
  1691. mesh_surface = surf->surface_shadow;
  1692. } else {
  1693. shader = surf->shader;
  1694. material_data = surf->material;
  1695. mesh_surface = surf->surface;
  1696. }
  1697. if (!mesh_surface) {
  1698. continue;
  1699. }
  1700. //request a redraw if one of the shaders uses TIME
  1701. if (shader->uses_time) {
  1702. should_request_redraw = true;
  1703. }
  1704. if constexpr (p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
  1705. if (scene_state.current_depth_test != shader->depth_test) {
  1706. if (shader->depth_test == GLES3::SceneShaderData::DEPTH_TEST_DISABLED) {
  1707. glDisable(GL_DEPTH_TEST);
  1708. } else {
  1709. glEnable(GL_DEPTH_TEST);
  1710. }
  1711. scene_state.current_depth_test = shader->depth_test;
  1712. }
  1713. }
  1714. if (scene_state.current_depth_draw != shader->depth_draw) {
  1715. switch (shader->depth_draw) {
  1716. case GLES3::SceneShaderData::DEPTH_DRAW_OPAQUE: {
  1717. glDepthMask(p_pass_mode == PASS_MODE_COLOR);
  1718. } break;
  1719. case GLES3::SceneShaderData::DEPTH_DRAW_ALWAYS: {
  1720. glDepthMask(GL_TRUE);
  1721. } break;
  1722. case GLES3::SceneShaderData::DEPTH_DRAW_DISABLED: {
  1723. glDepthMask(GL_FALSE);
  1724. } break;
  1725. }
  1726. scene_state.current_depth_draw = shader->depth_draw;
  1727. }
  1728. if constexpr (p_pass_mode == PASS_MODE_COLOR_TRANSPARENT || p_pass_mode == PASS_MODE_COLOR_ADDITIVE) {
  1729. GLES3::SceneShaderData::BlendMode desired_blend_mode;
  1730. if constexpr (p_pass_mode == PASS_MODE_COLOR_ADDITIVE) {
  1731. desired_blend_mode = GLES3::SceneShaderData::BLEND_MODE_ADD;
  1732. } else {
  1733. desired_blend_mode = shader->blend_mode;
  1734. }
  1735. if (desired_blend_mode != scene_state.current_blend_mode) {
  1736. switch (desired_blend_mode) {
  1737. case GLES3::SceneShaderData::BLEND_MODE_MIX: {
  1738. glBlendEquation(GL_FUNC_ADD);
  1739. if (p_render_data->transparent_bg) {
  1740. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1741. } else {
  1742. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1743. }
  1744. } break;
  1745. case GLES3::SceneShaderData::BLEND_MODE_ADD: {
  1746. glBlendEquation(GL_FUNC_ADD);
  1747. glBlendFunc(p_pass_mode == PASS_MODE_COLOR_TRANSPARENT ? GL_SRC_ALPHA : GL_ONE, GL_ONE);
  1748. } break;
  1749. case GLES3::SceneShaderData::BLEND_MODE_SUB: {
  1750. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  1751. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  1752. } break;
  1753. case GLES3::SceneShaderData::BLEND_MODE_MUL: {
  1754. glBlendEquation(GL_FUNC_ADD);
  1755. if (p_render_data->transparent_bg) {
  1756. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  1757. } else {
  1758. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  1759. }
  1760. } break;
  1761. case GLES3::SceneShaderData::BLEND_MODE_ALPHA_TO_COVERAGE: {
  1762. // Do nothing for now.
  1763. } break;
  1764. }
  1765. scene_state.current_blend_mode = desired_blend_mode;
  1766. }
  1767. }
  1768. //find cull variant
  1769. GLES3::SceneShaderData::Cull cull_mode = shader->cull_mode;
  1770. if ((surf->flags & GeometryInstanceSurface::FLAG_USES_DOUBLE_SIDED_SHADOWS)) {
  1771. cull_mode = GLES3::SceneShaderData::CULL_DISABLED;
  1772. } else {
  1773. bool mirror = inst->mirror;
  1774. if (p_params->reverse_cull) {
  1775. mirror = !mirror;
  1776. }
  1777. if (cull_mode == GLES3::SceneShaderData::CULL_FRONT && mirror) {
  1778. cull_mode = GLES3::SceneShaderData::CULL_BACK;
  1779. } else if (cull_mode == GLES3::SceneShaderData::CULL_BACK && mirror) {
  1780. cull_mode = GLES3::SceneShaderData::CULL_FRONT;
  1781. }
  1782. }
  1783. if (scene_state.cull_mode != cull_mode) {
  1784. if (cull_mode == GLES3::SceneShaderData::CULL_DISABLED) {
  1785. glDisable(GL_CULL_FACE);
  1786. } else {
  1787. if (scene_state.cull_mode == GLES3::SceneShaderData::CULL_DISABLED) {
  1788. // Last time was disabled, so enable and set proper face.
  1789. glEnable(GL_CULL_FACE);
  1790. }
  1791. glCullFace(cull_mode == GLES3::SceneShaderData::CULL_FRONT ? GL_FRONT : GL_BACK);
  1792. }
  1793. scene_state.cull_mode = cull_mode;
  1794. }
  1795. RS::PrimitiveType primitive = surf->primitive;
  1796. if (shader->uses_point_size) {
  1797. primitive = RS::PRIMITIVE_POINTS;
  1798. }
  1799. static const GLenum prim[5] = { GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP };
  1800. GLenum primitive_gl = prim[int(primitive)];
  1801. GLuint vertex_array_gl = 0;
  1802. GLuint index_array_gl = 0;
  1803. //skeleton and blend shape
  1804. if (surf->owner->mesh_instance.is_valid()) {
  1805. mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(surf->owner->mesh_instance, surf->surface_index, shader->vertex_input_mask, vertex_array_gl);
  1806. } else {
  1807. mesh_storage->mesh_surface_get_vertex_arrays_and_format(mesh_surface, shader->vertex_input_mask, vertex_array_gl);
  1808. }
  1809. index_array_gl = mesh_storage->mesh_surface_get_index_buffer(mesh_surface, surf->lod_index);
  1810. if (prev_vertex_array_gl != vertex_array_gl) {
  1811. if (vertex_array_gl != 0) {
  1812. glBindVertexArray(vertex_array_gl);
  1813. }
  1814. prev_vertex_array_gl = vertex_array_gl;
  1815. // Invalidate the previous index array
  1816. prev_index_array_gl = 0;
  1817. }
  1818. bool use_index_buffer = index_array_gl != 0;
  1819. if (prev_index_array_gl != index_array_gl) {
  1820. if (index_array_gl != 0) {
  1821. // Bind index each time so we can use LODs
  1822. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_array_gl);
  1823. }
  1824. prev_index_array_gl = index_array_gl;
  1825. }
  1826. Transform3D world_transform;
  1827. if (inst->store_transform_cache) {
  1828. world_transform = inst->transform;
  1829. }
  1830. if (prev_material_data != material_data) {
  1831. material_data->bind_uniforms();
  1832. prev_material_data = material_data;
  1833. }
  1834. SceneShaderGLES3::ShaderVariant instance_variant = shader_variant;
  1835. if (inst->instance_count > 0) {
  1836. // Will need to use instancing to draw (either MultiMesh or Particles).
  1837. instance_variant = SceneShaderGLES3::ShaderVariant(1 + int(shader_variant));
  1838. }
  1839. uint64_t spec_constants = base_spec_constants;
  1840. if (inst->omni_light_count == 0) {
  1841. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_OMNI;
  1842. }
  1843. if (inst->spot_light_count == 0) {
  1844. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_SPOT;
  1845. }
  1846. if (prev_shader != shader || prev_variant != instance_variant || spec_constants != prev_spec_constants) {
  1847. bool success = material_storage->shaders.scene_shader.version_bind_shader(shader->version, instance_variant, spec_constants);
  1848. if (!success) {
  1849. continue;
  1850. }
  1851. float opaque_prepass_threshold = 0.0;
  1852. if constexpr (p_pass_mode == PASS_MODE_DEPTH) {
  1853. opaque_prepass_threshold = 0.99;
  1854. } else if constexpr (p_pass_mode == PASS_MODE_SHADOW) {
  1855. opaque_prepass_threshold = 0.1;
  1856. }
  1857. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::OPAQUE_PREPASS_THRESHOLD, opaque_prepass_threshold, shader->version, instance_variant, spec_constants);
  1858. prev_shader = shader;
  1859. prev_variant = instance_variant;
  1860. prev_spec_constants = spec_constants;
  1861. }
  1862. if (prev_inst != inst || prev_shader != shader || prev_variant != instance_variant) {
  1863. // Rebind the light indices.
  1864. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::OMNI_LIGHT_COUNT, inst->omni_light_count, shader->version, instance_variant, spec_constants);
  1865. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::SPOT_LIGHT_COUNT, inst->spot_light_count, shader->version, instance_variant, spec_constants);
  1866. if (inst->omni_light_count) {
  1867. glUniform1uiv(material_storage->shaders.scene_shader.version_get_uniform(SceneShaderGLES3::OMNI_LIGHT_INDICES, shader->version, instance_variant, spec_constants), inst->omni_light_count, inst->omni_light_gl_cache.ptr());
  1868. }
  1869. if (inst->spot_light_count) {
  1870. glUniform1uiv(material_storage->shaders.scene_shader.version_get_uniform(SceneShaderGLES3::SPOT_LIGHT_INDICES, shader->version, instance_variant, spec_constants), inst->spot_light_count, inst->spot_light_gl_cache.ptr());
  1871. }
  1872. prev_inst = inst;
  1873. }
  1874. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::WORLD_TRANSFORM, world_transform, shader->version, instance_variant, spec_constants);
  1875. if (inst->instance_count > 0) {
  1876. // Using MultiMesh or Particles.
  1877. // Bind instance buffers.
  1878. GLuint instance_buffer = 0;
  1879. uint32_t stride = 0;
  1880. if (inst->flags_cache & INSTANCE_DATA_FLAG_PARTICLES) {
  1881. instance_buffer = particles_storage->particles_get_gl_buffer(inst->data->base);
  1882. stride = 16; // 12 bytes for instance transform and 4 bytes for packed color and custom.
  1883. } else {
  1884. instance_buffer = mesh_storage->multimesh_get_gl_buffer(inst->data->base);
  1885. stride = mesh_storage->multimesh_get_stride(inst->data->base);
  1886. }
  1887. if (instance_buffer == 0) {
  1888. // Instance buffer not initialized yet. Skip rendering for now.
  1889. continue;
  1890. }
  1891. glBindBuffer(GL_ARRAY_BUFFER, instance_buffer);
  1892. glEnableVertexAttribArray(12);
  1893. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(0));
  1894. glVertexAttribDivisor(12, 1);
  1895. glEnableVertexAttribArray(13);
  1896. glVertexAttribPointer(13, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4));
  1897. glVertexAttribDivisor(13, 1);
  1898. if (!(inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D)) {
  1899. glEnableVertexAttribArray(14);
  1900. glVertexAttribPointer(14, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(sizeof(float) * 8));
  1901. glVertexAttribDivisor(14, 1);
  1902. }
  1903. if ((inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR) || (inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA)) {
  1904. uint32_t color_custom_offset = inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D ? 8 : 12;
  1905. glEnableVertexAttribArray(15);
  1906. glVertexAttribIPointer(15, 4, GL_UNSIGNED_INT, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(color_custom_offset * sizeof(float)));
  1907. glVertexAttribDivisor(15, 1);
  1908. }
  1909. if (use_index_buffer) {
  1910. glDrawElementsInstanced(primitive_gl, mesh_storage->mesh_surface_get_vertices_drawn_count(mesh_surface), mesh_storage->mesh_surface_get_index_type(mesh_surface), 0, inst->instance_count);
  1911. } else {
  1912. glDrawArraysInstanced(primitive_gl, 0, mesh_storage->mesh_surface_get_vertices_drawn_count(mesh_surface), inst->instance_count);
  1913. }
  1914. } else {
  1915. // Using regular Mesh.
  1916. if (use_index_buffer) {
  1917. glDrawElements(primitive_gl, mesh_storage->mesh_surface_get_vertices_drawn_count(mesh_surface), mesh_storage->mesh_surface_get_index_type(mesh_surface), 0);
  1918. } else {
  1919. glDrawArrays(primitive_gl, 0, mesh_storage->mesh_surface_get_vertices_drawn_count(mesh_surface));
  1920. }
  1921. }
  1922. if (inst->instance_count > 0) {
  1923. glDisableVertexAttribArray(12);
  1924. glDisableVertexAttribArray(13);
  1925. glDisableVertexAttribArray(14);
  1926. glDisableVertexAttribArray(15);
  1927. }
  1928. }
  1929. // Make the actual redraw request
  1930. if (should_request_redraw) {
  1931. RenderingServerDefault::redraw_request();
  1932. }
  1933. }
  1934. void RasterizerSceneGLES3::render_material(const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal, const PagedArray<RenderGeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) {
  1935. }
  1936. void RasterizerSceneGLES3::render_particle_collider_heightfield(RID p_collider, const Transform3D &p_transform, const PagedArray<RenderGeometryInstance *> &p_instances) {
  1937. GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
  1938. ERR_FAIL_COND(!particles_storage->particles_collision_is_heightfield(p_collider));
  1939. Vector3 extents = particles_storage->particles_collision_get_extents(p_collider) * p_transform.basis.get_scale();
  1940. Projection cm;
  1941. cm.set_orthogonal(-extents.x, extents.x, -extents.z, extents.z, 0, extents.y * 2.0);
  1942. Vector3 cam_pos = p_transform.origin;
  1943. cam_pos.y += extents.y;
  1944. Transform3D cam_xform;
  1945. cam_xform.set_look_at(cam_pos, cam_pos - p_transform.basis.get_column(Vector3::AXIS_Y), -p_transform.basis.get_column(Vector3::AXIS_Z).normalized());
  1946. GLuint fb = particles_storage->particles_collision_get_heightfield_framebuffer(p_collider);
  1947. Size2i fb_size = particles_storage->particles_collision_get_heightfield_size(p_collider);
  1948. RENDER_TIMESTAMP("Setup GPUParticlesCollisionHeightField3D");
  1949. RenderDataGLES3 render_data;
  1950. render_data.cam_projection = cm;
  1951. render_data.cam_transform = cam_xform;
  1952. render_data.view_projection[0] = cm;
  1953. render_data.inv_cam_transform = render_data.cam_transform.affine_inverse();
  1954. render_data.cam_orthogonal = true;
  1955. render_data.z_near = 0.0;
  1956. render_data.z_far = cm.get_z_far();
  1957. render_data.instances = &p_instances;
  1958. _setup_environment(&render_data, true, Vector2(fb_size), true, Color(), false);
  1959. PassMode pass_mode = PASS_MODE_SHADOW;
  1960. _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
  1961. render_list[RENDER_LIST_SECONDARY].sort_by_key();
  1962. RENDER_TIMESTAMP("Render Collider Heightfield");
  1963. glBindFramebuffer(GL_FRAMEBUFFER, fb);
  1964. glViewport(0, 0, fb_size.width, fb_size.height);
  1965. GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
  1966. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer);
  1967. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1968. glDisable(GL_BLEND);
  1969. glDepthMask(GL_TRUE);
  1970. glEnable(GL_DEPTH_TEST);
  1971. glDepthFunc(GL_LESS);
  1972. glDisable(GL_SCISSOR_TEST);
  1973. glCullFace(GL_BACK);
  1974. glEnable(GL_CULL_FACE);
  1975. scene_state.cull_mode = GLES3::SceneShaderData::CULL_BACK;
  1976. glColorMask(0, 0, 0, 0);
  1977. glClearDepth(1.0f);
  1978. glClear(GL_DEPTH_BUFFER_BIT);
  1979. RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), false, 31, false);
  1980. _render_list_template<PASS_MODE_SHADOW>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());
  1981. glColorMask(1, 1, 1, 1);
  1982. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1983. }
  1984. void RasterizerSceneGLES3::set_time(double p_time, double p_step) {
  1985. time = p_time;
  1986. time_step = p_step;
  1987. }
  1988. void RasterizerSceneGLES3::set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) {
  1989. debug_draw = p_debug_draw;
  1990. }
  1991. Ref<RenderSceneBuffers> RasterizerSceneGLES3::render_buffers_create() {
  1992. Ref<RenderSceneBuffersGLES3> rb;
  1993. rb.instantiate();
  1994. return rb;
  1995. }
  1996. //clear render buffers
  1997. /*
  1998. if (rt->copy_screen_effect.color) {
  1999. glDeleteFramebuffers(1, &rt->copy_screen_effect.fbo);
  2000. rt->copy_screen_effect.fbo = 0;
  2001. glDeleteTextures(1, &rt->copy_screen_effect.color);
  2002. rt->copy_screen_effect.color = 0;
  2003. }
  2004. if (rt->multisample_active) {
  2005. glDeleteFramebuffers(1, &rt->multisample_fbo);
  2006. rt->multisample_fbo = 0;
  2007. glDeleteRenderbuffers(1, &rt->multisample_depth);
  2008. rt->multisample_depth = 0;
  2009. glDeleteRenderbuffers(1, &rt->multisample_color);
  2010. rt->multisample_color = 0;
  2011. }
  2012. */
  2013. void RasterizerSceneGLES3::_render_buffers_debug_draw(Ref<RenderSceneBuffersGLES3> p_render_buffers, RID p_shadow_atlas, RID p_occlusion_buffer) {
  2014. }
  2015. void RasterizerSceneGLES3::gi_set_use_half_resolution(bool p_enable) {
  2016. }
  2017. void RasterizerSceneGLES3::screen_space_roughness_limiter_set_active(bool p_enable, float p_amount, float p_curve) {
  2018. }
  2019. bool RasterizerSceneGLES3::screen_space_roughness_limiter_is_active() const {
  2020. return false;
  2021. }
  2022. void RasterizerSceneGLES3::sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality) {
  2023. }
  2024. void RasterizerSceneGLES3::sub_surface_scattering_set_scale(float p_scale, float p_depth_scale) {
  2025. }
  2026. TypedArray<Image> RasterizerSceneGLES3::bake_render_uv2(RID p_base, const TypedArray<RID> &p_material_overrides, const Size2i &p_image_size) {
  2027. return TypedArray<Image>();
  2028. }
  2029. bool RasterizerSceneGLES3::free(RID p_rid) {
  2030. if (is_environment(p_rid)) {
  2031. environment_free(p_rid);
  2032. } else if (sky_owner.owns(p_rid)) {
  2033. Sky *sky = sky_owner.get_or_null(p_rid);
  2034. ERR_FAIL_COND_V(!sky, false);
  2035. _free_sky_data(sky);
  2036. sky_owner.free(p_rid);
  2037. } else if (GLES3::LightStorage::get_singleton()->owns_light_instance(p_rid)) {
  2038. GLES3::LightStorage::get_singleton()->light_instance_free(p_rid);
  2039. } else if (RSG::camera_attributes->owns_camera_attributes(p_rid)) {
  2040. //not much to delete, just free it
  2041. RSG::camera_attributes->camera_attributes_free(p_rid);
  2042. } else {
  2043. return false;
  2044. }
  2045. return true;
  2046. }
  2047. void RasterizerSceneGLES3::update() {
  2048. _update_dirty_skys();
  2049. }
  2050. void RasterizerSceneGLES3::sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) {
  2051. }
  2052. void RasterizerSceneGLES3::decals_set_filter(RS::DecalFilter p_filter) {
  2053. }
  2054. void RasterizerSceneGLES3::light_projectors_set_filter(RS::LightProjectorFilter p_filter) {
  2055. }
  2056. RasterizerSceneGLES3::RasterizerSceneGLES3() {
  2057. singleton = this;
  2058. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  2059. GLES3::Config *config = GLES3::Config::get_singleton();
  2060. // Quality settings.
  2061. use_physical_light_units = GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units");
  2062. {
  2063. // Setup Lights
  2064. config->max_renderable_lights = MIN(config->max_renderable_lights, config->max_uniform_buffer_size / (int)sizeof(RasterizerSceneGLES3::LightData));
  2065. config->max_lights_per_object = MIN(config->max_lights_per_object, config->max_renderable_lights);
  2066. uint32_t light_buffer_size = config->max_renderable_lights * sizeof(LightData);
  2067. scene_state.omni_lights = memnew_arr(LightData, config->max_renderable_lights);
  2068. scene_state.omni_light_sort = memnew_arr(InstanceSort<GLES3::LightInstance>, config->max_renderable_lights);
  2069. glGenBuffers(1, &scene_state.omni_light_buffer);
  2070. glBindBuffer(GL_UNIFORM_BUFFER, scene_state.omni_light_buffer);
  2071. glBufferData(GL_UNIFORM_BUFFER, light_buffer_size, nullptr, GL_STREAM_DRAW);
  2072. scene_state.spot_lights = memnew_arr(LightData, config->max_renderable_lights);
  2073. scene_state.spot_light_sort = memnew_arr(InstanceSort<GLES3::LightInstance>, config->max_renderable_lights);
  2074. glGenBuffers(1, &scene_state.spot_light_buffer);
  2075. glBindBuffer(GL_UNIFORM_BUFFER, scene_state.spot_light_buffer);
  2076. glBufferData(GL_UNIFORM_BUFFER, light_buffer_size, nullptr, GL_STREAM_DRAW);
  2077. uint32_t directional_light_buffer_size = MAX_DIRECTIONAL_LIGHTS * sizeof(DirectionalLightData);
  2078. scene_state.directional_lights = memnew_arr(DirectionalLightData, MAX_DIRECTIONAL_LIGHTS);
  2079. glGenBuffers(1, &scene_state.directional_light_buffer);
  2080. glBindBuffer(GL_UNIFORM_BUFFER, scene_state.directional_light_buffer);
  2081. glBufferData(GL_UNIFORM_BUFFER, directional_light_buffer_size, nullptr, GL_STREAM_DRAW);
  2082. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2083. }
  2084. {
  2085. sky_globals.max_directional_lights = 4;
  2086. uint32_t directional_light_buffer_size = sky_globals.max_directional_lights * sizeof(DirectionalLightData);
  2087. sky_globals.directional_lights = memnew_arr(DirectionalLightData, sky_globals.max_directional_lights);
  2088. sky_globals.last_frame_directional_lights = memnew_arr(DirectionalLightData, sky_globals.max_directional_lights);
  2089. sky_globals.last_frame_directional_light_count = sky_globals.max_directional_lights + 1;
  2090. glGenBuffers(1, &sky_globals.directional_light_buffer);
  2091. glBindBuffer(GL_UNIFORM_BUFFER, sky_globals.directional_light_buffer);
  2092. glBufferData(GL_UNIFORM_BUFFER, directional_light_buffer_size, nullptr, GL_STREAM_DRAW);
  2093. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2094. }
  2095. {
  2096. String global_defines;
  2097. global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now
  2098. global_defines += "\n#define MAX_LIGHT_DATA_STRUCTS " + itos(config->max_renderable_lights) + "\n";
  2099. global_defines += "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(MAX_DIRECTIONAL_LIGHTS) + "\n";
  2100. global_defines += "\n#define MAX_FORWARD_LIGHTS uint(" + itos(config->max_lights_per_object) + ")\n";
  2101. material_storage->shaders.scene_shader.initialize(global_defines);
  2102. scene_globals.shader_default_version = material_storage->shaders.scene_shader.version_create();
  2103. material_storage->shaders.scene_shader.version_bind_shader(scene_globals.shader_default_version, SceneShaderGLES3::MODE_COLOR);
  2104. }
  2105. {
  2106. //default material and shader
  2107. scene_globals.default_shader = material_storage->shader_allocate();
  2108. material_storage->shader_initialize(scene_globals.default_shader);
  2109. material_storage->shader_set_code(scene_globals.default_shader, R"(
  2110. // Default 3D material shader.
  2111. shader_type spatial;
  2112. void vertex() {
  2113. ROUGHNESS = 0.8;
  2114. }
  2115. void fragment() {
  2116. ALBEDO = vec3(0.6);
  2117. ROUGHNESS = 0.8;
  2118. METALLIC = 0.2;
  2119. }
  2120. )");
  2121. scene_globals.default_material = material_storage->material_allocate();
  2122. material_storage->material_initialize(scene_globals.default_material);
  2123. material_storage->material_set_shader(scene_globals.default_material, scene_globals.default_shader);
  2124. }
  2125. {
  2126. // Initialize Sky stuff
  2127. sky_globals.roughness_layers = GLOBAL_GET("rendering/reflections/sky_reflections/roughness_layers");
  2128. sky_globals.ggx_samples = GLOBAL_GET("rendering/reflections/sky_reflections/ggx_samples");
  2129. String global_defines;
  2130. global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now
  2131. global_defines += "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(sky_globals.max_directional_lights) + "\n";
  2132. material_storage->shaders.sky_shader.initialize(global_defines);
  2133. sky_globals.shader_default_version = material_storage->shaders.sky_shader.version_create();
  2134. }
  2135. {
  2136. String global_defines;
  2137. global_defines += "\n#define MAX_SAMPLE_COUNT " + itos(sky_globals.ggx_samples) + "\n";
  2138. material_storage->shaders.cubemap_filter_shader.initialize(global_defines);
  2139. scene_globals.cubemap_filter_shader_version = material_storage->shaders.cubemap_filter_shader.version_create();
  2140. }
  2141. {
  2142. sky_globals.default_shader = material_storage->shader_allocate();
  2143. material_storage->shader_initialize(sky_globals.default_shader);
  2144. material_storage->shader_set_code(sky_globals.default_shader, R"(
  2145. // Default sky shader.
  2146. shader_type sky;
  2147. void sky() {
  2148. COLOR = vec3(0.0);
  2149. }
  2150. )");
  2151. sky_globals.default_material = material_storage->material_allocate();
  2152. material_storage->material_initialize(sky_globals.default_material);
  2153. material_storage->material_set_shader(sky_globals.default_material, sky_globals.default_shader);
  2154. }
  2155. {
  2156. sky_globals.fog_shader = material_storage->shader_allocate();
  2157. material_storage->shader_initialize(sky_globals.fog_shader);
  2158. material_storage->shader_set_code(sky_globals.fog_shader, R"(
  2159. // Default clear color sky shader.
  2160. shader_type sky;
  2161. uniform vec4 clear_color;
  2162. void sky() {
  2163. COLOR = clear_color.rgb;
  2164. }
  2165. )");
  2166. sky_globals.fog_material = material_storage->material_allocate();
  2167. material_storage->material_initialize(sky_globals.fog_material);
  2168. material_storage->material_set_shader(sky_globals.fog_material, sky_globals.fog_shader);
  2169. }
  2170. {
  2171. glGenBuffers(1, &sky_globals.screen_triangle);
  2172. glBindBuffer(GL_ARRAY_BUFFER, sky_globals.screen_triangle);
  2173. const float qv[6] = {
  2174. -1.0f,
  2175. -1.0f,
  2176. 3.0f,
  2177. -1.0f,
  2178. -1.0f,
  2179. 3.0f,
  2180. };
  2181. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, qv, GL_STATIC_DRAW);
  2182. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  2183. glGenVertexArrays(1, &sky_globals.screen_triangle_array);
  2184. glBindVertexArray(sky_globals.screen_triangle_array);
  2185. glBindBuffer(GL_ARRAY_BUFFER, sky_globals.screen_triangle);
  2186. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);
  2187. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  2188. glBindVertexArray(0);
  2189. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  2190. }
  2191. #ifdef GLES_OVER_GL
  2192. glEnable(_EXT_TEXTURE_CUBE_MAP_SEAMLESS);
  2193. #endif
  2194. // MultiMesh may read from color when color is disabled, so make sure that the color defaults to white instead of black;
  2195. glVertexAttrib4f(RS::ARRAY_COLOR, 1.0, 1.0, 1.0, 1.0);
  2196. }
  2197. RasterizerSceneGLES3::~RasterizerSceneGLES3() {
  2198. glDeleteBuffers(1, &scene_state.directional_light_buffer);
  2199. glDeleteBuffers(1, &scene_state.omni_light_buffer);
  2200. glDeleteBuffers(1, &scene_state.spot_light_buffer);
  2201. memdelete_arr(scene_state.directional_lights);
  2202. memdelete_arr(scene_state.omni_lights);
  2203. memdelete_arr(scene_state.spot_lights);
  2204. memdelete_arr(scene_state.omni_light_sort);
  2205. memdelete_arr(scene_state.spot_light_sort);
  2206. // Scene Shader
  2207. GLES3::MaterialStorage::get_singleton()->shaders.scene_shader.version_free(scene_globals.shader_default_version);
  2208. GLES3::MaterialStorage::get_singleton()->shaders.cubemap_filter_shader.version_free(scene_globals.cubemap_filter_shader_version);
  2209. RSG::material_storage->material_free(scene_globals.default_material);
  2210. RSG::material_storage->shader_free(scene_globals.default_shader);
  2211. // Sky Shader
  2212. GLES3::MaterialStorage::get_singleton()->shaders.sky_shader.version_free(sky_globals.shader_default_version);
  2213. RSG::material_storage->material_free(sky_globals.default_material);
  2214. RSG::material_storage->shader_free(sky_globals.default_shader);
  2215. RSG::material_storage->material_free(sky_globals.fog_material);
  2216. RSG::material_storage->shader_free(sky_globals.fog_shader);
  2217. glDeleteBuffers(1, &sky_globals.screen_triangle);
  2218. glDeleteVertexArrays(1, &sky_globals.screen_triangle_array);
  2219. glDeleteTextures(1, &sky_globals.radical_inverse_vdc_cache_tex);
  2220. glDeleteBuffers(1, &sky_globals.directional_light_buffer);
  2221. memdelete_arr(sky_globals.directional_lights);
  2222. memdelete_arr(sky_globals.last_frame_directional_lights);
  2223. singleton = nullptr;
  2224. }
  2225. #endif // GLES3_ENABLED