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