Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/gles3/rasterizer_scene_gles3.cpp
9973 views
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
33
#include "drivers/gles3/effects/copy_effects.h"
34
#include "drivers/gles3/effects/feed_effects.h"
35
#include "drivers/gles3/storage/material_storage.h"
36
#include "rasterizer_gles3.h"
37
#include "storage/config.h"
38
#include "storage/mesh_storage.h"
39
#include "storage/particles_storage.h"
40
#include "storage/texture_storage.h"
41
42
#include "core/config/project_settings.h"
43
#include "core/templates/sort_array.h"
44
#include "servers/camera/camera_feed.h"
45
#include "servers/camera_server.h"
46
#include "servers/rendering/rendering_server_default.h"
47
#include "servers/rendering/rendering_server_globals.h"
48
49
#ifdef GLES3_ENABLED
50
51
RasterizerSceneGLES3 *RasterizerSceneGLES3::singleton = nullptr;
52
53
RenderGeometryInstance *RasterizerSceneGLES3::geometry_instance_create(RID p_base) {
54
RS::InstanceType type = RSG::utilities->get_base_type(p_base);
55
ERR_FAIL_COND_V(!((1 << type) & RS::INSTANCE_GEOMETRY_MASK), nullptr);
56
57
GeometryInstanceGLES3 *ginstance = geometry_instance_alloc.alloc();
58
ginstance->data = memnew(GeometryInstanceGLES3::Data);
59
60
ginstance->data->base = p_base;
61
ginstance->data->base_type = type;
62
ginstance->data->dependency_tracker.userdata = ginstance;
63
ginstance->data->dependency_tracker.changed_callback = _geometry_instance_dependency_changed;
64
ginstance->data->dependency_tracker.deleted_callback = _geometry_instance_dependency_deleted;
65
66
ginstance->_mark_dirty();
67
68
return ginstance;
69
}
70
71
uint32_t RasterizerSceneGLES3::geometry_instance_get_pair_mask() {
72
return ((1 << RS::INSTANCE_LIGHT) | (1 << RS::INSTANCE_REFLECTION_PROBE));
73
}
74
75
void RasterizerSceneGLES3::GeometryInstanceGLES3::pair_light_instances(const RID *p_light_instances, uint32_t p_light_instance_count) {
76
GLES3::Config *config = GLES3::Config::get_singleton();
77
78
paired_omni_light_count = 0;
79
paired_spot_light_count = 0;
80
paired_omni_lights.clear();
81
paired_spot_lights.clear();
82
83
for (uint32_t i = 0; i < p_light_instance_count; i++) {
84
RS::LightType type = GLES3::LightStorage::get_singleton()->light_instance_get_type(p_light_instances[i]);
85
switch (type) {
86
case RS::LIGHT_OMNI: {
87
if (paired_omni_light_count < (uint32_t)config->max_lights_per_object) {
88
paired_omni_lights.push_back(p_light_instances[i]);
89
paired_omni_light_count++;
90
}
91
} break;
92
case RS::LIGHT_SPOT: {
93
if (paired_spot_light_count < (uint32_t)config->max_lights_per_object) {
94
paired_spot_lights.push_back(p_light_instances[i]);
95
paired_spot_light_count++;
96
}
97
} break;
98
default:
99
break;
100
}
101
}
102
}
103
104
void RasterizerSceneGLES3::GeometryInstanceGLES3::pair_reflection_probe_instances(const RID *p_reflection_probe_instances, uint32_t p_reflection_probe_instance_count) {
105
paired_reflection_probes.clear();
106
107
for (uint32_t i = 0; i < p_reflection_probe_instance_count; i++) {
108
paired_reflection_probes.push_back(p_reflection_probe_instances[i]);
109
}
110
}
111
112
void RasterizerSceneGLES3::geometry_instance_free(RenderGeometryInstance *p_geometry_instance) {
113
GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance);
114
ERR_FAIL_NULL(ginstance);
115
GeometryInstanceSurface *surf = ginstance->surface_caches;
116
while (surf) {
117
GeometryInstanceSurface *next = surf->next;
118
geometry_instance_surface_alloc.free(surf);
119
surf = next;
120
}
121
memdelete(ginstance->data);
122
geometry_instance_alloc.free(ginstance);
123
}
124
125
void RasterizerSceneGLES3::GeometryInstanceGLES3::_mark_dirty() {
126
if (dirty_list_element.in_list()) {
127
return;
128
}
129
130
//clear surface caches
131
GeometryInstanceSurface *surf = surface_caches;
132
133
while (surf) {
134
GeometryInstanceSurface *next = surf->next;
135
RasterizerSceneGLES3::get_singleton()->geometry_instance_surface_alloc.free(surf);
136
surf = next;
137
}
138
139
surface_caches = nullptr;
140
141
RasterizerSceneGLES3::get_singleton()->geometry_instance_dirty_list.add(&dirty_list_element);
142
}
143
144
void RasterizerSceneGLES3::GeometryInstanceGLES3::set_use_lightmap(RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) {
145
lightmap_instance = p_lightmap_instance;
146
lightmap_uv_scale = p_lightmap_uv_scale;
147
lightmap_slice_index = p_lightmap_slice_index;
148
149
_mark_dirty();
150
}
151
152
void RasterizerSceneGLES3::GeometryInstanceGLES3::set_lightmap_capture(const Color *p_sh9) {
153
if (p_sh9) {
154
if (lightmap_sh == nullptr) {
155
lightmap_sh = memnew(GeometryInstanceLightmapSH);
156
}
157
158
memcpy(lightmap_sh->sh, p_sh9, sizeof(Color) * 9);
159
} else {
160
if (lightmap_sh != nullptr) {
161
memdelete(lightmap_sh);
162
lightmap_sh = nullptr;
163
}
164
}
165
_mark_dirty();
166
}
167
168
void RasterizerSceneGLES3::_update_dirty_geometry_instances() {
169
while (geometry_instance_dirty_list.first()) {
170
_geometry_instance_update(geometry_instance_dirty_list.first()->self());
171
}
172
}
173
174
void RasterizerSceneGLES3::_geometry_instance_dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *p_tracker) {
175
switch (p_notification) {
176
case Dependency::DEPENDENCY_CHANGED_MATERIAL:
177
case Dependency::DEPENDENCY_CHANGED_MESH:
178
case Dependency::DEPENDENCY_CHANGED_PARTICLES:
179
case Dependency::DEPENDENCY_CHANGED_MULTIMESH:
180
case Dependency::DEPENDENCY_CHANGED_SKELETON_DATA: {
181
static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty();
182
static_cast<GeometryInstanceGLES3 *>(p_tracker->userdata)->data->dirty_dependencies = true;
183
} break;
184
case Dependency::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES: {
185
GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_tracker->userdata);
186
if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) {
187
ginstance->instance_count = GLES3::MeshStorage::get_singleton()->multimesh_get_instances_to_draw(ginstance->data->base);
188
}
189
} break;
190
default: {
191
//rest of notifications of no interest
192
} break;
193
}
194
}
195
196
void RasterizerSceneGLES3::_geometry_instance_dependency_deleted(const RID &p_dependency, DependencyTracker *p_tracker) {
197
static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty();
198
static_cast<GeometryInstanceGLES3 *>(p_tracker->userdata)->data->dirty_dependencies = true;
199
}
200
201
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) {
202
GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
203
204
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;
205
bool has_base_alpha = ((p_material->shader_data->uses_alpha && !p_material->shader_data->uses_alpha_clip) || has_read_screen_alpha);
206
bool has_blend_alpha = p_material->shader_data->uses_blend_alpha;
207
bool has_alpha = has_base_alpha || has_blend_alpha;
208
209
uint32_t flags = 0;
210
211
if (p_material->shader_data->uses_screen_texture) {
212
flags |= GeometryInstanceSurface::FLAG_USES_SCREEN_TEXTURE;
213
}
214
215
if (p_material->shader_data->uses_depth_texture) {
216
flags |= GeometryInstanceSurface::FLAG_USES_DEPTH_TEXTURE;
217
}
218
219
if (p_material->shader_data->uses_normal_texture) {
220
flags |= GeometryInstanceSurface::FLAG_USES_NORMAL_TEXTURE;
221
}
222
223
if (ginstance->data->cast_double_sided_shadows) {
224
flags |= GeometryInstanceSurface::FLAG_USES_DOUBLE_SIDED_SHADOWS;
225
}
226
227
if (p_material->shader_data->stencil_enabled) {
228
flags |= GeometryInstanceSurface::FLAG_USES_STENCIL;
229
}
230
231
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_ENABLED) {
232
//material is only meant for alpha pass
233
flags |= GeometryInstanceSurface::FLAG_PASS_ALPHA;
234
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_ENABLED)) {
235
flags |= GeometryInstanceSurface::FLAG_PASS_DEPTH;
236
flags |= GeometryInstanceSurface::FLAG_PASS_SHADOW;
237
}
238
} else {
239
flags |= GeometryInstanceSurface::FLAG_PASS_OPAQUE;
240
flags |= GeometryInstanceSurface::FLAG_PASS_DEPTH;
241
flags |= GeometryInstanceSurface::FLAG_PASS_SHADOW;
242
}
243
244
if (p_material->shader_data->stencil_enabled) {
245
if (p_material->shader_data->stencil_flags & GLES3::SceneShaderData::STENCIL_FLAG_READ) {
246
// Stencil materials which read from the stencil buffer must be in the alpha pass.
247
// This is critical to preserve compatibility once we'll have the compositor.
248
if (!(flags & GeometryInstanceSurface::FLAG_PASS_ALPHA)) {
249
String shader_path = p_material->shader_data->path.is_empty() ? "" : "(" + p_material->shader_data->path + ")";
250
ERR_PRINT_ED(vformat("Attempting to use a shader %s that reads stencil but is not in the alpha queue. Ensure the material uses alpha blending or has depth_draw disabled or depth_test disabled.", shader_path));
251
}
252
}
253
}
254
255
GLES3::SceneMaterialData *material_shadow = nullptr;
256
void *surface_shadow = nullptr;
257
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 && !p_material->shader_data->uses_world_coordinates && !p_material->shader_data->wireframe) {
258
flags |= GeometryInstanceSurface::FLAG_USES_SHARED_SHADOW_MATERIAL;
259
material_shadow = static_cast<GLES3::SceneMaterialData *>(GLES3::MaterialStorage::get_singleton()->material_get_data(scene_globals.default_material, RS::SHADER_SPATIAL));
260
261
RID shadow_mesh = mesh_storage->mesh_get_shadow_mesh(p_mesh);
262
263
if (shadow_mesh.is_valid()) {
264
surface_shadow = mesh_storage->mesh_get_surface(shadow_mesh, p_surface);
265
}
266
267
} else {
268
material_shadow = p_material;
269
}
270
271
GeometryInstanceSurface *sdcache = geometry_instance_surface_alloc.alloc();
272
273
sdcache->flags = flags;
274
275
sdcache->shader = p_material->shader_data;
276
sdcache->material = p_material;
277
sdcache->surface = mesh_storage->mesh_get_surface(p_mesh, p_surface);
278
sdcache->primitive = mesh_storage->mesh_surface_get_primitive(sdcache->surface);
279
sdcache->surface_index = p_surface;
280
281
if (ginstance->data->dirty_dependencies) {
282
RSG::utilities->base_update_dependency(p_mesh, &ginstance->data->dependency_tracker);
283
}
284
285
//shadow
286
sdcache->shader_shadow = material_shadow->shader_data;
287
sdcache->material_shadow = material_shadow;
288
289
sdcache->surface_shadow = surface_shadow ? surface_shadow : sdcache->surface;
290
291
sdcache->owner = ginstance;
292
293
sdcache->next = ginstance->surface_caches;
294
ginstance->surface_caches = sdcache;
295
296
//sortkey
297
298
sdcache->sort.sort_key1 = 0;
299
sdcache->sort.sort_key2 = 0;
300
301
sdcache->sort.surface_index = p_surface;
302
sdcache->sort.material_id_low = p_material_id & 0x0000FFFF;
303
sdcache->sort.material_id_hi = p_material_id >> 16;
304
sdcache->sort.shader_id = p_shader_id;
305
sdcache->sort.geometry_id = p_mesh.get_local_index();
306
sdcache->sort.priority = p_material->priority;
307
308
GLES3::Mesh::Surface *s = reinterpret_cast<GLES3::Mesh::Surface *>(sdcache->surface);
309
if (p_material->shader_data->uses_tangent && !(s->format & RS::ARRAY_FORMAT_TANGENT)) {
310
String shader_path = p_material->shader_data->path.is_empty() ? "" : "(" + p_material->shader_data->path + ")";
311
String mesh_path = mesh_storage->mesh_get_path(p_mesh).is_empty() ? "" : "(" + mesh_storage->mesh_get_path(p_mesh) + ")";
312
WARN_PRINT_ED(vformat("Attempting to use a shader %s that requires tangents with a mesh %s that doesn't contain tangents. Ensure that meshes are imported with the 'ensure_tangents' option. If creating your own meshes, add an `ARRAY_TANGENT` array (when using ArrayMesh) or call `generate_tangents()` (when using SurfaceTool).", shader_path, mesh_path));
313
}
314
}
315
316
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) {
317
GLES3::SceneMaterialData *material_data = p_material_data;
318
GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
319
320
_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);
321
322
while (material_data->next_pass.is_valid()) {
323
RID next_pass = material_data->next_pass;
324
material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(next_pass, RS::SHADER_SPATIAL));
325
if (!material_data || !material_data->shader_data->valid) {
326
break;
327
}
328
if (ginstance->data->dirty_dependencies) {
329
material_storage->material_update_dependency(next_pass, &ginstance->data->dependency_tracker);
330
}
331
_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);
332
}
333
}
334
335
void RasterizerSceneGLES3::_geometry_instance_add_surface(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, RID p_material, RID p_mesh) {
336
GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
337
RID m_src;
338
339
m_src = ginstance->data->material_override.is_valid() ? ginstance->data->material_override : p_material;
340
341
GLES3::SceneMaterialData *material_data = nullptr;
342
343
if (m_src.is_valid()) {
344
material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(m_src, RS::SHADER_SPATIAL));
345
if (!material_data || !material_data->shader_data->valid) {
346
material_data = nullptr;
347
}
348
}
349
350
if (material_data) {
351
if (ginstance->data->dirty_dependencies) {
352
material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker);
353
}
354
} else {
355
material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(scene_globals.default_material, RS::SHADER_SPATIAL));
356
m_src = scene_globals.default_material;
357
}
358
359
ERR_FAIL_NULL(material_data);
360
361
_geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material_data, m_src, p_mesh);
362
363
if (ginstance->data->material_overlay.is_valid()) {
364
m_src = ginstance->data->material_overlay;
365
366
material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(m_src, RS::SHADER_SPATIAL));
367
if (material_data && material_data->shader_data->valid) {
368
if (ginstance->data->dirty_dependencies) {
369
material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker);
370
}
371
372
_geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material_data, m_src, p_mesh);
373
}
374
}
375
}
376
377
void RasterizerSceneGLES3::_geometry_instance_update(RenderGeometryInstance *p_geometry_instance) {
378
GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
379
GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
380
381
GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance);
382
383
if (ginstance->data->dirty_dependencies) {
384
ginstance->data->dependency_tracker.update_begin();
385
}
386
387
//add geometry for drawing
388
switch (ginstance->data->base_type) {
389
case RS::INSTANCE_MESH: {
390
const RID *materials = nullptr;
391
uint32_t surface_count;
392
RID mesh = ginstance->data->base;
393
394
materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
395
if (materials) {
396
//if no materials, no surfaces.
397
const RID *inst_materials = ginstance->data->surface_materials.ptr();
398
uint32_t surf_mat_count = ginstance->data->surface_materials.size();
399
400
for (uint32_t j = 0; j < surface_count; j++) {
401
RID material = (j < surf_mat_count && inst_materials[j].is_valid()) ? inst_materials[j] : materials[j];
402
_geometry_instance_add_surface(ginstance, j, material, mesh);
403
}
404
}
405
406
ginstance->instance_count = -1;
407
408
} break;
409
410
case RS::INSTANCE_MULTIMESH: {
411
RID mesh = mesh_storage->multimesh_get_mesh(ginstance->data->base);
412
if (mesh.is_valid()) {
413
const RID *materials = nullptr;
414
uint32_t surface_count;
415
416
materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
417
if (materials) {
418
for (uint32_t j = 0; j < surface_count; j++) {
419
_geometry_instance_add_surface(ginstance, j, materials[j], mesh);
420
}
421
}
422
423
ginstance->instance_count = mesh_storage->multimesh_get_instances_to_draw(ginstance->data->base);
424
}
425
426
} break;
427
case RS::INSTANCE_PARTICLES: {
428
int draw_passes = particles_storage->particles_get_draw_passes(ginstance->data->base);
429
430
for (int j = 0; j < draw_passes; j++) {
431
RID mesh = particles_storage->particles_get_draw_pass_mesh(ginstance->data->base, j);
432
if (!mesh.is_valid()) {
433
continue;
434
}
435
436
const RID *materials = nullptr;
437
uint32_t surface_count;
438
439
materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
440
if (materials) {
441
for (uint32_t k = 0; k < surface_count; k++) {
442
_geometry_instance_add_surface(ginstance, k, materials[k], mesh);
443
}
444
}
445
}
446
447
ginstance->instance_count = particles_storage->particles_get_amount(ginstance->data->base);
448
} break;
449
450
default: {
451
}
452
}
453
454
bool store_transform = true;
455
ginstance->base_flags = 0;
456
457
if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) {
458
ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH;
459
if (mesh_storage->multimesh_get_transform_format(ginstance->data->base) == RS::MULTIMESH_TRANSFORM_2D) {
460
ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D;
461
}
462
if (mesh_storage->multimesh_uses_colors(ginstance->data->base)) {
463
ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR;
464
}
465
if (mesh_storage->multimesh_uses_custom_data(ginstance->data->base)) {
466
ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA;
467
}
468
469
} else if (ginstance->data->base_type == RS::INSTANCE_PARTICLES) {
470
ginstance->base_flags |= INSTANCE_DATA_FLAG_PARTICLES;
471
ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH;
472
473
ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR;
474
ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA;
475
476
if (!particles_storage->particles_is_using_local_coords(ginstance->data->base)) {
477
store_transform = false;
478
}
479
480
} else if (ginstance->data->base_type == RS::INSTANCE_MESH) {
481
if (mesh_storage->skeleton_is_valid(ginstance->data->skeleton)) {
482
if (ginstance->data->dirty_dependencies) {
483
mesh_storage->skeleton_update_dependency(ginstance->data->skeleton, &ginstance->data->dependency_tracker);
484
}
485
}
486
}
487
488
ginstance->store_transform_cache = store_transform;
489
490
if (ginstance->data->dirty_dependencies) {
491
ginstance->data->dependency_tracker.update_end();
492
ginstance->data->dirty_dependencies = false;
493
}
494
495
ginstance->dirty_list_element.remove_from_list();
496
}
497
498
/* SKY API */
499
500
void RasterizerSceneGLES3::_free_sky_data(Sky *p_sky) {
501
if (p_sky->radiance != 0) {
502
GLES3::Utilities::get_singleton()->texture_free_data(p_sky->radiance);
503
p_sky->radiance = 0;
504
GLES3::Utilities::get_singleton()->texture_free_data(p_sky->raw_radiance);
505
p_sky->raw_radiance = 0;
506
glDeleteFramebuffers(1, &p_sky->radiance_framebuffer);
507
p_sky->radiance_framebuffer = 0;
508
}
509
}
510
511
RID RasterizerSceneGLES3::sky_allocate() {
512
return sky_owner.allocate_rid();
513
}
514
515
void RasterizerSceneGLES3::sky_initialize(RID p_rid) {
516
sky_owner.initialize_rid(p_rid);
517
}
518
519
void RasterizerSceneGLES3::sky_set_radiance_size(RID p_sky, int p_radiance_size) {
520
Sky *sky = sky_owner.get_or_null(p_sky);
521
ERR_FAIL_NULL(sky);
522
ERR_FAIL_COND_MSG(p_radiance_size < 32 || p_radiance_size > 2048, "Sky radiance size must be between 32 and 2048");
523
524
if (sky->radiance_size == p_radiance_size) {
525
return; // No need to update
526
}
527
528
sky->radiance_size = p_radiance_size;
529
530
_free_sky_data(sky);
531
_invalidate_sky(sky);
532
}
533
534
void RasterizerSceneGLES3::sky_set_mode(RID p_sky, RS::SkyMode p_mode) {
535
Sky *sky = sky_owner.get_or_null(p_sky);
536
ERR_FAIL_NULL(sky);
537
538
if (sky->mode == p_mode) {
539
return;
540
}
541
542
sky->mode = p_mode;
543
_invalidate_sky(sky);
544
}
545
546
void RasterizerSceneGLES3::sky_set_material(RID p_sky, RID p_material) {
547
Sky *sky = sky_owner.get_or_null(p_sky);
548
ERR_FAIL_NULL(sky);
549
550
if (sky->material == p_material) {
551
return;
552
}
553
554
sky->material = p_material;
555
_invalidate_sky(sky);
556
}
557
558
float RasterizerSceneGLES3::sky_get_baked_exposure(RID p_sky) const {
559
Sky *sky = sky_owner.get_or_null(p_sky);
560
ERR_FAIL_NULL_V(sky, 1.0);
561
562
return sky->baked_exposure;
563
}
564
565
void RasterizerSceneGLES3::_invalidate_sky(Sky *p_sky) {
566
if (!p_sky->dirty) {
567
p_sky->dirty = true;
568
p_sky->dirty_list = dirty_sky_list;
569
dirty_sky_list = p_sky;
570
}
571
}
572
573
GLuint _init_radiance_texture(int p_size, int p_mipmaps, String p_name) {
574
GLuint radiance_id = 0;
575
576
glGenTextures(1, &radiance_id);
577
glBindTexture(GL_TEXTURE_CUBE_MAP, radiance_id);
578
#ifdef GL_API_ENABLED
579
if (RasterizerGLES3::is_gles_over_gl()) {
580
//TODO, on low-end compare this to allocating each face of each mip individually
581
// see: https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexStorage2D.xhtml
582
for (int i = 0; i < 6; i++) {
583
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB10_A2, p_size, p_size, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
584
}
585
586
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
587
}
588
#endif // GL_API_ENABLED
589
#ifdef GLES_API_ENABLED
590
if (!RasterizerGLES3::is_gles_over_gl()) {
591
glTexStorage2D(GL_TEXTURE_CUBE_MAP, p_mipmaps, GL_RGB10_A2, p_size, p_size);
592
}
593
#endif // GLES_API_ENABLED
594
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
595
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
596
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
597
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
598
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
599
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, p_mipmaps - 1);
600
601
GLES3::Utilities::get_singleton()->texture_allocated_data(radiance_id, Image::get_image_data_size(p_size, p_size, Image::FORMAT_RGBA8, true), p_name);
602
return radiance_id;
603
}
604
605
void RasterizerSceneGLES3::_update_dirty_skys() {
606
Sky *sky = dirty_sky_list;
607
608
while (sky) {
609
if (sky->radiance == 0) {
610
sky->mipmap_count = Image::get_image_required_mipmaps(sky->radiance_size, sky->radiance_size, Image::FORMAT_RGBA8) - 1;
611
// Left uninitialized, will attach a texture at render time
612
glGenFramebuffers(1, &sky->radiance_framebuffer);
613
sky->radiance = _init_radiance_texture(sky->radiance_size, sky->mipmap_count, "Sky radiance texture");
614
sky->raw_radiance = _init_radiance_texture(sky->radiance_size, sky->mipmap_count, "Sky raw radiance texture");
615
}
616
617
sky->reflection_dirty = true;
618
sky->processing_layer = 0;
619
620
Sky *next = sky->dirty_list;
621
sky->dirty_list = nullptr;
622
sky->dirty = false;
623
sky = next;
624
}
625
626
dirty_sky_list = nullptr;
627
}
628
629
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) {
630
GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
631
GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
632
ERR_FAIL_COND(p_render_data->environment.is_null());
633
634
GLES3::SkyMaterialData *material = nullptr;
635
Sky *sky = sky_owner.get_or_null(environment_get_sky(p_render_data->environment));
636
637
RID sky_material;
638
639
GLES3::SkyShaderData *shader_data = nullptr;
640
641
if (sky) {
642
sky_material = sky->material;
643
644
if (sky_material.is_valid()) {
645
material = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
646
if (!material || !material->shader_data->valid) {
647
material = nullptr;
648
}
649
}
650
}
651
652
if (!material) {
653
sky_material = sky_globals.default_material;
654
material = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
655
}
656
657
ERR_FAIL_NULL(material);
658
659
shader_data = material->shader_data;
660
661
ERR_FAIL_NULL(shader_data);
662
663
if (sky) {
664
if (shader_data->uses_time && time - sky->prev_time > 0.00001) {
665
sky->prev_time = time;
666
sky->reflection_dirty = true;
667
RenderingServerDefault::redraw_request();
668
}
669
670
if (material != sky->prev_material) {
671
sky->prev_material = material;
672
sky->reflection_dirty = true;
673
}
674
675
if (material->uniform_set_updated) {
676
material->uniform_set_updated = false;
677
sky->reflection_dirty = true;
678
}
679
680
if (!p_transform.origin.is_equal_approx(sky->prev_position) && shader_data->uses_position) {
681
sky->prev_position = p_transform.origin;
682
sky->reflection_dirty = true;
683
}
684
}
685
686
glBindBufferBase(GL_UNIFORM_BUFFER, SKY_DIRECTIONAL_LIGHT_UNIFORM_LOCATION, sky_globals.directional_light_buffer);
687
if (shader_data->uses_light || (environment_get_fog_enabled(p_render_data->environment) && environment_get_fog_sun_scatter(p_render_data->environment) > 0.001)) {
688
sky_globals.directional_light_count = 0;
689
for (int i = 0; i < (int)p_lights.size(); i++) {
690
GLES3::LightInstance *li = GLES3::LightStorage::get_singleton()->get_light_instance(p_lights[i]);
691
if (!li) {
692
continue;
693
}
694
RID base = li->light;
695
696
ERR_CONTINUE(base.is_null());
697
698
RS::LightType type = light_storage->light_get_type(base);
699
if (type == RS::LIGHT_DIRECTIONAL && light_storage->light_directional_get_sky_mode(base) != RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_ONLY) {
700
DirectionalLightData &sky_light_data = sky_globals.directional_lights[sky_globals.directional_light_count];
701
Transform3D light_transform = li->transform;
702
Vector3 world_direction = light_transform.basis.xform(Vector3(0, 0, 1)).normalized();
703
704
sky_light_data.direction[0] = world_direction.x;
705
sky_light_data.direction[1] = world_direction.y;
706
sky_light_data.direction[2] = world_direction.z;
707
708
float sign = light_storage->light_is_negative(base) ? -1 : 1;
709
sky_light_data.energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY);
710
711
if (is_using_physical_light_units()) {
712
sky_light_data.energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
713
}
714
715
if (p_render_data->camera_attributes.is_valid()) {
716
sky_light_data.energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
717
}
718
719
Color srgb_col = light_storage->light_get_color(base);
720
sky_light_data.color[0] = srgb_col.r;
721
sky_light_data.color[1] = srgb_col.g;
722
sky_light_data.color[2] = srgb_col.b;
723
724
sky_light_data.enabled = true;
725
726
float angular_diameter = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
727
sky_light_data.size = Math::deg_to_rad(angular_diameter);
728
sky_globals.directional_light_count++;
729
if (sky_globals.directional_light_count >= sky_globals.max_directional_lights) {
730
break;
731
}
732
}
733
}
734
// Check whether the directional_light_buffer changes
735
bool light_data_dirty = false;
736
737
// Light buffer is dirty if we have fewer or more lights
738
// If we have fewer lights, make sure that old lights are disabled
739
if (sky_globals.directional_light_count != sky_globals.last_frame_directional_light_count) {
740
light_data_dirty = true;
741
for (uint32_t i = sky_globals.directional_light_count; i < sky_globals.max_directional_lights; i++) {
742
sky_globals.directional_lights[i].enabled = false;
743
sky_globals.last_frame_directional_lights[i].enabled = false;
744
}
745
}
746
747
if (!light_data_dirty) {
748
for (uint32_t i = 0; i < sky_globals.directional_light_count; i++) {
749
if (sky_globals.directional_lights[i].direction[0] != sky_globals.last_frame_directional_lights[i].direction[0] ||
750
sky_globals.directional_lights[i].direction[1] != sky_globals.last_frame_directional_lights[i].direction[1] ||
751
sky_globals.directional_lights[i].direction[2] != sky_globals.last_frame_directional_lights[i].direction[2] ||
752
sky_globals.directional_lights[i].energy != sky_globals.last_frame_directional_lights[i].energy ||
753
sky_globals.directional_lights[i].color[0] != sky_globals.last_frame_directional_lights[i].color[0] ||
754
sky_globals.directional_lights[i].color[1] != sky_globals.last_frame_directional_lights[i].color[1] ||
755
sky_globals.directional_lights[i].color[2] != sky_globals.last_frame_directional_lights[i].color[2] ||
756
sky_globals.directional_lights[i].enabled != sky_globals.last_frame_directional_lights[i].enabled ||
757
sky_globals.directional_lights[i].size != sky_globals.last_frame_directional_lights[i].size) {
758
light_data_dirty = true;
759
break;
760
}
761
}
762
}
763
764
if (light_data_dirty) {
765
glBufferData(GL_UNIFORM_BUFFER, sizeof(DirectionalLightData) * sky_globals.max_directional_lights, sky_globals.directional_lights, GL_STREAM_DRAW);
766
glBindBuffer(GL_UNIFORM_BUFFER, 0);
767
768
DirectionalLightData *temp = sky_globals.last_frame_directional_lights;
769
sky_globals.last_frame_directional_lights = sky_globals.directional_lights;
770
sky_globals.directional_lights = temp;
771
sky_globals.last_frame_directional_light_count = sky_globals.directional_light_count;
772
if (sky) {
773
sky->reflection_dirty = true;
774
}
775
}
776
}
777
778
if (p_render_data->view_count > 1) {
779
glBindBufferBase(GL_UNIFORM_BUFFER, SKY_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
780
glBindBuffer(GL_UNIFORM_BUFFER, 0);
781
}
782
783
if (sky && !sky->radiance) {
784
_invalidate_sky(sky);
785
_update_dirty_skys();
786
}
787
}
788
789
void RasterizerSceneGLES3::_draw_sky(RID p_env, const Projection &p_projection, const Transform3D &p_transform, float p_sky_energy_multiplier, float p_luminance_multiplier, bool p_use_multiview, bool p_flip_y, bool p_apply_color_adjustments_in_post) {
790
GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
791
ERR_FAIL_COND(p_env.is_null());
792
793
Sky *sky = sky_owner.get_or_null(environment_get_sky(p_env));
794
795
GLES3::SkyMaterialData *material_data = nullptr;
796
RID sky_material;
797
798
uint64_t spec_constants = p_use_multiview ? SkyShaderGLES3::USE_MULTIVIEW : 0;
799
if (p_flip_y) {
800
spec_constants |= SkyShaderGLES3::USE_INVERTED_Y;
801
}
802
if (!p_apply_color_adjustments_in_post) {
803
spec_constants |= SkyShaderGLES3::APPLY_TONEMAPPING;
804
}
805
806
RS::EnvironmentBG background = environment_get_background(p_env);
807
808
if (sky) {
809
sky_material = sky->material;
810
811
if (sky_material.is_valid()) {
812
material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
813
if (!material_data || !material_data->shader_data->valid) {
814
material_data = nullptr;
815
}
816
}
817
818
if (!material_data) {
819
sky_material = sky_globals.default_material;
820
material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
821
}
822
} else if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
823
sky_material = sky_globals.fog_material;
824
material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
825
}
826
827
ERR_FAIL_NULL(material_data);
828
material_data->bind_uniforms();
829
830
GLES3::SkyShaderData *shader_data = material_data->shader_data;
831
832
ERR_FAIL_NULL(shader_data);
833
834
// Camera
835
Projection camera;
836
837
if (environment_get_sky_custom_fov(p_env)) {
838
float near_plane = p_projection.get_z_near();
839
float far_plane = p_projection.get_z_far();
840
float aspect = p_projection.get_aspect();
841
842
camera.set_perspective(environment_get_sky_custom_fov(p_env), aspect, near_plane, far_plane);
843
} else {
844
camera = p_projection;
845
}
846
847
Projection correction;
848
correction.set_depth_correction(false, true, false);
849
camera = correction * camera;
850
851
Basis sky_transform = environment_get_sky_orientation(p_env);
852
sky_transform.invert();
853
sky_transform = sky_transform * p_transform.basis;
854
855
bool success = material_storage->shaders.sky_shader.version_bind_shader(shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
856
if (!success) {
857
return;
858
}
859
860
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::ORIENTATION, sky_transform, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
861
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);
862
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::POSITION, p_transform.origin, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
863
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::TIME, time, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
864
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::SKY_ENERGY_MULTIPLIER, p_sky_energy_multiplier, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
865
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::LUMINANCE_MULTIPLIER, p_luminance_multiplier, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
866
867
Color fog_color = environment_get_fog_light_color(p_env).srgb_to_linear() * environment_get_fog_light_energy(p_env);
868
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_ENABLED, environment_get_fog_enabled(p_env), shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
869
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_AERIAL_PERSPECTIVE, environment_get_fog_aerial_perspective(p_env), shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
870
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_LIGHT_COLOR, fog_color, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
871
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_SUN_SCATTER, environment_get_fog_sun_scatter(p_env), shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
872
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_DENSITY, environment_get_fog_density(p_env), shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
873
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_SKY_AFFECT, environment_get_fog_sky_affect(p_env), shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
874
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::DIRECTIONAL_LIGHT_COUNT, sky_globals.directional_light_count, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
875
876
if (p_use_multiview) {
877
glBindBufferBase(GL_UNIFORM_BUFFER, SKY_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
878
glBindBuffer(GL_UNIFORM_BUFFER, 0);
879
}
880
881
glBindVertexArray(sky_globals.screen_triangle_array);
882
glDrawArrays(GL_TRIANGLES, 0, 3);
883
}
884
885
void RasterizerSceneGLES3::_update_sky_radiance(RID p_env, const Projection &p_projection, const Transform3D &p_transform, float p_sky_energy_multiplier) {
886
GLES3::CubemapFilter *cubemap_filter = GLES3::CubemapFilter::get_singleton();
887
GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
888
ERR_FAIL_COND(p_env.is_null());
889
890
Sky *sky = sky_owner.get_or_null(environment_get_sky(p_env));
891
ERR_FAIL_NULL(sky);
892
893
GLES3::SkyMaterialData *material_data = nullptr;
894
RID sky_material;
895
896
RS::EnvironmentBG background = environment_get_background(p_env);
897
898
if (sky) {
899
ERR_FAIL_NULL(sky);
900
sky_material = sky->material;
901
902
if (sky_material.is_valid()) {
903
material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
904
if (!material_data || !material_data->shader_data->valid) {
905
material_data = nullptr;
906
}
907
}
908
909
if (!material_data) {
910
sky_material = sky_globals.default_material;
911
material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
912
}
913
} else if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
914
sky_material = sky_globals.fog_material;
915
material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
916
}
917
918
ERR_FAIL_NULL(material_data);
919
material_data->bind_uniforms();
920
921
GLES3::SkyShaderData *shader_data = material_data->shader_data;
922
923
ERR_FAIL_NULL(shader_data);
924
925
bool update_single_frame = sky->mode == RS::SKY_MODE_REALTIME || sky->mode == RS::SKY_MODE_QUALITY;
926
RS::SkyMode sky_mode = sky->mode;
927
928
if (sky_mode == RS::SKY_MODE_AUTOMATIC) {
929
if ((shader_data->uses_time || shader_data->uses_position) && sky->radiance_size == 256) {
930
update_single_frame = true;
931
sky_mode = RS::SKY_MODE_REALTIME;
932
} else if (shader_data->uses_light || shader_data->ubo_size > 0) {
933
update_single_frame = false;
934
sky_mode = RS::SKY_MODE_INCREMENTAL;
935
} else {
936
update_single_frame = true;
937
sky_mode = RS::SKY_MODE_QUALITY;
938
}
939
}
940
941
if (sky->processing_layer == 0 && sky_mode == RS::SKY_MODE_INCREMENTAL) {
942
// On the first frame after creating sky, rebuild in single frame
943
update_single_frame = true;
944
sky_mode = RS::SKY_MODE_QUALITY;
945
}
946
947
int max_processing_layer = sky->mipmap_count;
948
949
// Update radiance cubemap
950
if (sky->reflection_dirty && (sky->processing_layer >= max_processing_layer || update_single_frame)) {
951
static const Vector3 view_normals[6] = {
952
Vector3(+1, 0, 0),
953
Vector3(-1, 0, 0),
954
Vector3(0, +1, 0),
955
Vector3(0, -1, 0),
956
Vector3(0, 0, +1),
957
Vector3(0, 0, -1)
958
};
959
static const Vector3 view_up[6] = {
960
Vector3(0, -1, 0),
961
Vector3(0, -1, 0),
962
Vector3(0, 0, +1),
963
Vector3(0, 0, -1),
964
Vector3(0, -1, 0),
965
Vector3(0, -1, 0)
966
};
967
968
Projection cm;
969
cm.set_perspective(90, 1, 0.01, 10.0);
970
Projection correction;
971
correction.set_depth_correction(true, true, false);
972
cm = correction * cm;
973
974
bool success = material_storage->shaders.sky_shader.version_bind_shader(shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
975
if (!success) {
976
return;
977
}
978
979
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::POSITION, p_transform.origin, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
980
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::TIME, time, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
981
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);
982
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::SKY_ENERGY_MULTIPLIER, p_sky_energy_multiplier, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
983
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::LUMINANCE_MULTIPLIER, 1.0, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
984
985
glBindVertexArray(sky_globals.screen_triangle_array);
986
987
glViewport(0, 0, sky->radiance_size, sky->radiance_size);
988
glBindFramebuffer(GL_FRAMEBUFFER, sky->radiance_framebuffer);
989
990
scene_state.reset_gl_state();
991
scene_state.set_gl_cull_mode(RS::CULL_MODE_DISABLED);
992
scene_state.enable_gl_blend(false);
993
994
for (int i = 0; i < 6; i++) {
995
Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
996
material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::ORIENTATION, local_view, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
997
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, sky->raw_radiance, 0);
998
glDrawArrays(GL_TRIANGLES, 0, 3);
999
}
1000
1001
if (update_single_frame) {
1002
for (int i = 0; i < max_processing_layer; i++) {
1003
cubemap_filter->filter_radiance(sky->raw_radiance, sky->radiance, sky->radiance_framebuffer, sky->radiance_size, sky->mipmap_count, i);
1004
}
1005
} else {
1006
cubemap_filter->filter_radiance(sky->raw_radiance, sky->radiance, sky->radiance_framebuffer, sky->radiance_size, sky->mipmap_count, 0); // Just copy over the first mipmap.
1007
}
1008
sky->processing_layer = 1;
1009
sky->baked_exposure = p_sky_energy_multiplier;
1010
sky->reflection_dirty = false;
1011
} else {
1012
if (sky_mode == RS::SKY_MODE_INCREMENTAL && sky->processing_layer < max_processing_layer) {
1013
scene_state.reset_gl_state();
1014
scene_state.set_gl_cull_mode(RS::CULL_MODE_DISABLED);
1015
scene_state.enable_gl_blend(false);
1016
1017
cubemap_filter->filter_radiance(sky->raw_radiance, sky->radiance, sky->radiance_framebuffer, sky->radiance_size, sky->mipmap_count, sky->processing_layer);
1018
sky->processing_layer++;
1019
}
1020
}
1021
glViewport(0, 0, sky->screen_size.x, sky->screen_size.y);
1022
}
1023
1024
Ref<Image> RasterizerSceneGLES3::sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) {
1025
Sky *sky = sky_owner.get_or_null(p_sky);
1026
ERR_FAIL_NULL_V(sky, Ref<Image>());
1027
1028
_update_dirty_skys();
1029
1030
if (sky->radiance == 0) {
1031
return Ref<Image>();
1032
}
1033
1034
GLES3::CopyEffects *copy_effects = GLES3::CopyEffects::get_singleton();
1035
GLES3::Config *config = GLES3::Config::get_singleton();
1036
1037
GLuint rad_tex = 0;
1038
glGenTextures(1, &rad_tex);
1039
glBindTexture(GL_TEXTURE_2D, rad_tex);
1040
if (config->float_texture_supported) {
1041
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, p_size.width, p_size.height, 0, GL_RGBA, GL_FLOAT, nullptr);
1042
GLES3::Utilities::get_singleton()->texture_allocated_data(rad_tex, p_size.width * p_size.height * 16, "Temp sky panorama");
1043
} else {
1044
// Fallback to RGBA8 on devices that don't support rendering to floating point textures. This will look bad, but we have no choice.
1045
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, p_size.width, p_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
1046
GLES3::Utilities::get_singleton()->texture_allocated_data(rad_tex, p_size.width * p_size.height * 4, "Temp sky panorama");
1047
}
1048
1049
GLuint rad_fbo = 0;
1050
glGenFramebuffers(1, &rad_fbo);
1051
glBindFramebuffer(GL_FRAMEBUFFER, rad_fbo);
1052
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rad_tex, 0);
1053
glActiveTexture(GL_TEXTURE0);
1054
glBindTexture(GL_TEXTURE_CUBE_MAP, sky->radiance);
1055
glViewport(0, 0, p_size.width, p_size.height);
1056
1057
glClearColor(0.0, 0.0, 0.0, 1.0);
1058
glClear(GL_COLOR_BUFFER_BIT);
1059
1060
copy_effects->copy_cube_to_panorama(p_bake_irradiance ? float(sky->mipmap_count) : 0.0);
1061
1062
glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
1063
glDeleteFramebuffers(1, &rad_fbo);
1064
// Create a dummy texture so we can use texture_2d_get.
1065
RID tex_rid = GLES3::TextureStorage::get_singleton()->texture_allocate();
1066
{
1067
GLES3::Texture texture;
1068
texture.width = p_size.width;
1069
texture.height = p_size.height;
1070
texture.alloc_width = p_size.width;
1071
texture.alloc_height = p_size.height;
1072
texture.format = Image::FORMAT_RGBAF;
1073
texture.real_format = Image::FORMAT_RGBAF;
1074
texture.gl_format_cache = GL_RGBA;
1075
texture.gl_type_cache = GL_FLOAT;
1076
texture.type = GLES3::Texture::TYPE_2D;
1077
texture.target = GL_TEXTURE_2D;
1078
texture.active = true;
1079
texture.tex_id = rad_tex;
1080
texture.is_render_target = true; // HACK: Prevent TextureStorage from retaining a cached copy of the texture.
1081
GLES3::TextureStorage::get_singleton()->texture_2d_initialize_from_texture(tex_rid, texture);
1082
}
1083
1084
Ref<Image> img = GLES3::TextureStorage::get_singleton()->texture_2d_get(tex_rid);
1085
GLES3::Utilities::get_singleton()->texture_free_data(rad_tex);
1086
1087
GLES3::Texture &texture = *GLES3::TextureStorage::get_singleton()->get_texture(tex_rid);
1088
texture.is_render_target = false; // HACK: Avoid an error when freeing the texture.
1089
texture.tex_id = 0;
1090
GLES3::TextureStorage::get_singleton()->texture_free(tex_rid);
1091
1092
for (int i = 0; i < p_size.width; i++) {
1093
for (int j = 0; j < p_size.height; j++) {
1094
Color c = img->get_pixel(i, j);
1095
c.r *= p_energy;
1096
c.g *= p_energy;
1097
c.b *= p_energy;
1098
img->set_pixel(i, j, c);
1099
}
1100
}
1101
return img;
1102
}
1103
1104
/* ENVIRONMENT API */
1105
1106
void RasterizerSceneGLES3::environment_glow_set_use_bicubic_upscale(bool p_enable) {
1107
glow_bicubic_upscale = p_enable;
1108
}
1109
1110
void RasterizerSceneGLES3::environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) {
1111
}
1112
1113
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) {
1114
}
1115
1116
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) {
1117
}
1118
1119
void RasterizerSceneGLES3::environment_set_sdfgi_ray_count(RS::EnvironmentSDFGIRayCount p_ray_count) {
1120
}
1121
1122
void RasterizerSceneGLES3::environment_set_sdfgi_frames_to_converge(RS::EnvironmentSDFGIFramesToConverge p_frames) {
1123
}
1124
1125
void RasterizerSceneGLES3::environment_set_sdfgi_frames_to_update_light(RS::EnvironmentSDFGIFramesToUpdateLight p_update) {
1126
}
1127
1128
void RasterizerSceneGLES3::environment_set_volumetric_fog_volume_size(int p_size, int p_depth) {
1129
}
1130
1131
void RasterizerSceneGLES3::environment_set_volumetric_fog_filter_active(bool p_enable) {
1132
}
1133
1134
Ref<Image> RasterizerSceneGLES3::environment_bake_panorama(RID p_env, bool p_bake_irradiance, const Size2i &p_size) {
1135
ERR_FAIL_COND_V(p_env.is_null(), Ref<Image>());
1136
1137
RS::EnvironmentBG environment_background = environment_get_background(p_env);
1138
1139
if (environment_background == RS::ENV_BG_CAMERA_FEED || environment_background == RS::ENV_BG_CANVAS || environment_background == RS::ENV_BG_KEEP) {
1140
return Ref<Image>(); // Nothing to bake.
1141
}
1142
1143
RS::EnvironmentAmbientSource ambient_source = environment_get_ambient_source(p_env);
1144
1145
bool use_ambient_light = false;
1146
bool use_cube_map = false;
1147
if (ambient_source == RS::ENV_AMBIENT_SOURCE_BG && (environment_background == RS::ENV_BG_CLEAR_COLOR || environment_background == RS::ENV_BG_COLOR)) {
1148
use_ambient_light = true;
1149
} else {
1150
use_cube_map = (ambient_source == RS::ENV_AMBIENT_SOURCE_BG && environment_background == RS::ENV_BG_SKY) || ambient_source == RS::ENV_AMBIENT_SOURCE_SKY;
1151
use_ambient_light = use_cube_map || ambient_source == RS::ENV_AMBIENT_SOURCE_COLOR;
1152
}
1153
1154
use_cube_map = use_cube_map || (environment_background == RS::ENV_BG_SKY && environment_get_sky(p_env).is_valid());
1155
1156
Color ambient_color;
1157
float ambient_color_sky_mix = 0.0;
1158
if (use_ambient_light) {
1159
ambient_color_sky_mix = environment_get_ambient_sky_contribution(p_env);
1160
const float ambient_energy = environment_get_ambient_light_energy(p_env);
1161
ambient_color = environment_get_ambient_light(p_env);
1162
ambient_color = ambient_color.srgb_to_linear();
1163
ambient_color.r *= ambient_energy;
1164
ambient_color.g *= ambient_energy;
1165
ambient_color.b *= ambient_energy;
1166
}
1167
1168
if (use_cube_map) {
1169
Ref<Image> panorama = sky_bake_panorama(environment_get_sky(p_env), environment_get_bg_energy_multiplier(p_env), p_bake_irradiance, p_size);
1170
if (use_ambient_light) {
1171
for (int x = 0; x < p_size.width; x++) {
1172
for (int y = 0; y < p_size.height; y++) {
1173
panorama->set_pixel(x, y, ambient_color.lerp(panorama->get_pixel(x, y), ambient_color_sky_mix));
1174
}
1175
}
1176
}
1177
return panorama;
1178
} else {
1179
const float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_env);
1180
Color panorama_color = ((environment_background == RS::ENV_BG_CLEAR_COLOR) ? RSG::texture_storage->get_default_clear_color() : environment_get_bg_color(p_env));
1181
panorama_color = panorama_color.srgb_to_linear();
1182
panorama_color.r *= bg_energy_multiplier;
1183
panorama_color.g *= bg_energy_multiplier;
1184
panorama_color.b *= bg_energy_multiplier;
1185
1186
if (use_ambient_light) {
1187
panorama_color = ambient_color.lerp(panorama_color, ambient_color_sky_mix);
1188
}
1189
1190
Ref<Image> panorama = Image::create_empty(p_size.width, p_size.height, false, Image::FORMAT_RGBAF);
1191
panorama->fill(panorama_color);
1192
return panorama;
1193
}
1194
}
1195
1196
void RasterizerSceneGLES3::positional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) {
1197
scene_state.positional_shadow_quality = p_quality;
1198
}
1199
1200
void RasterizerSceneGLES3::directional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) {
1201
scene_state.directional_shadow_quality = p_quality;
1202
}
1203
1204
RID RasterizerSceneGLES3::fog_volume_instance_create(RID p_fog_volume) {
1205
return RID();
1206
}
1207
1208
void RasterizerSceneGLES3::fog_volume_instance_set_transform(RID p_fog_volume_instance, const Transform3D &p_transform) {
1209
}
1210
1211
void RasterizerSceneGLES3::fog_volume_instance_set_active(RID p_fog_volume_instance, bool p_active) {
1212
}
1213
1214
RID RasterizerSceneGLES3::fog_volume_instance_get_volume(RID p_fog_volume_instance) const {
1215
return RID();
1216
}
1217
1218
Vector3 RasterizerSceneGLES3::fog_volume_instance_get_position(RID p_fog_volume_instance) const {
1219
return Vector3();
1220
}
1221
1222
RID RasterizerSceneGLES3::voxel_gi_instance_create(RID p_voxel_gi) {
1223
return RID();
1224
}
1225
1226
void RasterizerSceneGLES3::voxel_gi_instance_set_transform_to_data(RID p_probe, const Transform3D &p_xform) {
1227
}
1228
1229
bool RasterizerSceneGLES3::voxel_gi_needs_update(RID p_probe) const {
1230
return false;
1231
}
1232
1233
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) {
1234
}
1235
1236
void RasterizerSceneGLES3::voxel_gi_set_quality(RS::VoxelGIQuality) {
1237
}
1238
1239
_FORCE_INLINE_ static uint32_t _indices_to_primitives(RS::PrimitiveType p_primitive, uint32_t p_indices) {
1240
static const uint32_t divisor[RS::PRIMITIVE_MAX] = { 1, 2, 1, 3, 1 };
1241
static const uint32_t subtractor[RS::PRIMITIVE_MAX] = { 0, 0, 1, 0, 2 };
1242
return (p_indices - subtractor[p_primitive]) / divisor[p_primitive];
1243
}
1244
void RasterizerSceneGLES3::_fill_render_list(RenderListType p_render_list, const RenderDataGLES3 *p_render_data, PassMode p_pass_mode, bool p_append) {
1245
GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
1246
GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
1247
1248
if (p_render_list == RENDER_LIST_OPAQUE) {
1249
scene_state.used_screen_texture = false;
1250
scene_state.used_normal_texture = false;
1251
scene_state.used_depth_texture = false;
1252
scene_state.used_opaque_stencil = false;
1253
}
1254
1255
Plane near_plane;
1256
if (p_render_data->cam_orthogonal) {
1257
near_plane = Plane(-p_render_data->cam_transform.basis.get_column(Vector3::AXIS_Z), p_render_data->cam_transform.origin);
1258
near_plane.d += p_render_data->cam_projection.get_z_near();
1259
}
1260
float z_max = p_render_data->cam_projection.get_z_far() - p_render_data->cam_projection.get_z_near();
1261
1262
RenderList *rl = &render_list[p_render_list];
1263
1264
// Parse any updates on our geometry, updates surface caches and such
1265
_update_dirty_geometry_instances();
1266
1267
if (!p_append) {
1268
rl->clear();
1269
if (p_render_list == RENDER_LIST_OPAQUE) {
1270
render_list[RENDER_LIST_ALPHA].clear(); //opaque fills alpha too
1271
}
1272
}
1273
1274
//fill list
1275
1276
for (int i = 0; i < (int)p_render_data->instances->size(); i++) {
1277
GeometryInstanceGLES3 *inst = static_cast<GeometryInstanceGLES3 *>((*p_render_data->instances)[i]);
1278
1279
Vector3 center = inst->transform.origin;
1280
if (p_render_data->cam_orthogonal) {
1281
if (inst->use_aabb_center) {
1282
center = inst->transformed_aabb.get_support(-near_plane.normal);
1283
}
1284
inst->depth = near_plane.distance_to(center) - inst->sorting_offset;
1285
} else {
1286
if (inst->use_aabb_center) {
1287
center = inst->transformed_aabb.position + (inst->transformed_aabb.size * 0.5);
1288
}
1289
inst->depth = p_render_data->cam_transform.origin.distance_to(center) - inst->sorting_offset;
1290
}
1291
uint32_t depth_layer = CLAMP(int(inst->depth * 16 / z_max), 0, 15);
1292
1293
uint32_t flags = inst->base_flags; //fill flags if appropriate
1294
1295
if (inst->non_uniform_scale) {
1296
flags |= INSTANCE_DATA_FLAGS_NON_UNIFORM_SCALE;
1297
}
1298
1299
// Sets the index values for lookup in the shader
1300
// This has to be done after _setup_lights was called this frame
1301
1302
if (p_pass_mode == PASS_MODE_COLOR) {
1303
inst->light_passes.clear();
1304
inst->spot_light_gl_cache.clear();
1305
inst->omni_light_gl_cache.clear();
1306
inst->reflection_probes_local_transform_cache.clear();
1307
inst->reflection_probe_rid_cache.clear();
1308
uint64_t current_frame = RSG::rasterizer->get_frame_number();
1309
1310
if (inst->paired_omni_light_count) {
1311
for (uint32_t j = 0; j < inst->paired_omni_light_count; j++) {
1312
RID light_instance = inst->paired_omni_lights[j];
1313
if (light_storage->light_instance_get_render_pass(light_instance) != current_frame) {
1314
continue;
1315
}
1316
RID light = light_storage->light_instance_get_base_light(light_instance);
1317
int32_t shadow_id = light_storage->light_instance_get_shadow_id(light_instance);
1318
1319
if (light_storage->light_has_shadow(light) && shadow_id >= 0) {
1320
GeometryInstanceGLES3::LightPass pass;
1321
pass.light_id = light_storage->light_instance_get_gl_id(light_instance);
1322
pass.shadow_id = shadow_id;
1323
pass.light_instance_rid = light_instance;
1324
pass.is_omni = true;
1325
inst->light_passes.push_back(pass);
1326
} else {
1327
// Lights without shadow can all go in base pass.
1328
inst->omni_light_gl_cache.push_back((uint32_t)light_storage->light_instance_get_gl_id(light_instance));
1329
}
1330
}
1331
}
1332
1333
if (inst->paired_spot_light_count) {
1334
for (uint32_t j = 0; j < inst->paired_spot_light_count; j++) {
1335
RID light_instance = inst->paired_spot_lights[j];
1336
if (light_storage->light_instance_get_render_pass(light_instance) != current_frame) {
1337
continue;
1338
}
1339
RID light = light_storage->light_instance_get_base_light(light_instance);
1340
int32_t shadow_id = light_storage->light_instance_get_shadow_id(light_instance);
1341
1342
if (light_storage->light_has_shadow(light) && shadow_id >= 0) {
1343
GeometryInstanceGLES3::LightPass pass;
1344
pass.light_id = light_storage->light_instance_get_gl_id(light_instance);
1345
pass.shadow_id = shadow_id;
1346
pass.light_instance_rid = light_instance;
1347
inst->light_passes.push_back(pass);
1348
} else {
1349
// Lights without shadow can all go in base pass.
1350
inst->spot_light_gl_cache.push_back((uint32_t)light_storage->light_instance_get_gl_id(light_instance));
1351
}
1352
}
1353
}
1354
1355
if (p_render_data->reflection_probe.is_null() && inst->paired_reflection_probes.size() > 0) {
1356
// Do not include if we're rendering reflection probes.
1357
// We only support two probes for now and we handle them first come, first serve.
1358
// This should be improved one day, at minimum the list should be sorted by priority.
1359
1360
for (uint32_t pi = 0; pi < inst->paired_reflection_probes.size(); pi++) {
1361
RID probe_instance = inst->paired_reflection_probes[pi];
1362
RID atlas = light_storage->reflection_probe_instance_get_atlas(probe_instance);
1363
RID probe = light_storage->reflection_probe_instance_get_probe(probe_instance);
1364
uint32_t reflection_mask = light_storage->reflection_probe_get_reflection_mask(probe);
1365
if (atlas.is_valid() && (inst->layer_mask & reflection_mask)) {
1366
Transform3D local_matrix = p_render_data->inv_cam_transform * light_storage->reflection_probe_instance_get_transform(probe_instance);
1367
inst->reflection_probes_local_transform_cache.push_back(local_matrix.affine_inverse());
1368
inst->reflection_probe_rid_cache.push_back(probe_instance);
1369
}
1370
}
1371
}
1372
}
1373
1374
inst->flags_cache = flags;
1375
1376
GeometryInstanceSurface *surf = inst->surface_caches;
1377
1378
float lod_distance = 0.0;
1379
1380
if (p_render_data->cam_orthogonal) {
1381
lod_distance = 1.0;
1382
} else {
1383
Vector3 aabb_min = inst->transformed_aabb.position;
1384
Vector3 aabb_max = inst->transformed_aabb.position + inst->transformed_aabb.size;
1385
Vector3 camera_position = p_render_data->main_cam_transform.origin;
1386
Vector3 surface_distance = Vector3(0.0, 0.0, 0.0).max(aabb_min - camera_position).max(camera_position - aabb_max);
1387
1388
lod_distance = surface_distance.length();
1389
}
1390
1391
while (surf) {
1392
// LOD
1393
1394
if (p_render_data->screen_mesh_lod_threshold > 0.0 && mesh_storage->mesh_surface_has_lod(surf->surface)) {
1395
uint32_t indices = 0;
1396
surf->lod_index = mesh_storage->mesh_surface_get_lod(surf->surface, inst->lod_model_scale * inst->lod_bias, lod_distance * p_render_data->lod_distance_multiplier, p_render_data->screen_mesh_lod_threshold, indices);
1397
surf->index_count = indices;
1398
1399
if (p_render_data->render_info) {
1400
indices = _indices_to_primitives(surf->primitive, indices);
1401
if (p_render_list == RENDER_LIST_OPAQUE) { //opaque
1402
p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices;
1403
} else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow
1404
p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices;
1405
}
1406
}
1407
1408
} else {
1409
surf->lod_index = 0;
1410
1411
if (p_render_data->render_info) {
1412
uint32_t to_draw = mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface);
1413
to_draw = _indices_to_primitives(surf->primitive, to_draw);
1414
to_draw *= inst->instance_count > 0 ? inst->instance_count : 1;
1415
if (p_render_list == RENDER_LIST_OPAQUE) { //opaque
1416
p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += to_draw;
1417
} else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow
1418
p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += to_draw;
1419
}
1420
}
1421
}
1422
1423
// ADD Element
1424
if (p_pass_mode == PASS_MODE_COLOR) {
1425
#ifdef DEBUG_ENABLED
1426
bool force_alpha = unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW);
1427
#else
1428
bool force_alpha = false;
1429
#endif
1430
if (!force_alpha && (surf->flags & (GeometryInstanceSurface::FLAG_PASS_DEPTH | GeometryInstanceSurface::FLAG_PASS_OPAQUE))) {
1431
rl->add_element(surf);
1432
}
1433
if (force_alpha || (surf->flags & GeometryInstanceSurface::FLAG_PASS_ALPHA)) {
1434
render_list[RENDER_LIST_ALPHA].add_element(surf);
1435
}
1436
1437
if (surf->flags & GeometryInstanceSurface::FLAG_USES_SCREEN_TEXTURE) {
1438
scene_state.used_screen_texture = true;
1439
}
1440
if (surf->flags & GeometryInstanceSurface::FLAG_USES_NORMAL_TEXTURE) {
1441
scene_state.used_normal_texture = true;
1442
}
1443
if (surf->flags & GeometryInstanceSurface::FLAG_USES_DEPTH_TEXTURE) {
1444
scene_state.used_depth_texture = true;
1445
}
1446
if ((surf->flags & GeometryInstanceSurface::FLAG_USES_STENCIL) && !force_alpha && (surf->flags & (GeometryInstanceSurface::FLAG_PASS_DEPTH | GeometryInstanceSurface::FLAG_PASS_OPAQUE))) {
1447
scene_state.used_opaque_stencil = true;
1448
}
1449
1450
} else if (p_pass_mode == PASS_MODE_SHADOW) {
1451
if (surf->flags & GeometryInstanceSurface::FLAG_PASS_SHADOW) {
1452
rl->add_element(surf);
1453
}
1454
} else if (p_pass_mode == PASS_MODE_MATERIAL) {
1455
if (surf->flags & (GeometryInstanceSurface::FLAG_PASS_DEPTH | GeometryInstanceSurface::FLAG_PASS_OPAQUE | GeometryInstanceSurface::FLAG_PASS_ALPHA)) {
1456
rl->add_element(surf);
1457
}
1458
} else {
1459
if (surf->flags & (GeometryInstanceSurface::FLAG_PASS_DEPTH | GeometryInstanceSurface::FLAG_PASS_OPAQUE)) {
1460
rl->add_element(surf);
1461
}
1462
}
1463
1464
surf->sort.depth_layer = depth_layer;
1465
surf->finished_base_pass = false;
1466
surf->light_pass_index = 0;
1467
1468
surf = surf->next;
1469
}
1470
}
1471
}
1472
1473
// Needs to be called after _setup_lights so that directional_light_count is accurate.
1474
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, float p_shadow_bias) {
1475
Projection correction;
1476
correction.set_depth_correction(p_flip_y, true, false);
1477
Projection projection = correction * p_render_data->cam_projection;
1478
//store camera into ubo
1479
GLES3::MaterialStorage::store_camera(projection, scene_state.ubo.projection_matrix);
1480
GLES3::MaterialStorage::store_camera(projection.inverse(), scene_state.ubo.inv_projection_matrix);
1481
GLES3::MaterialStorage::store_transform(p_render_data->cam_transform, scene_state.ubo.inv_view_matrix);
1482
GLES3::MaterialStorage::store_transform(p_render_data->inv_cam_transform, scene_state.ubo.view_matrix);
1483
GLES3::MaterialStorage::store_transform(p_render_data->main_cam_transform, scene_state.ubo.main_cam_inv_view_matrix);
1484
scene_state.ubo.camera_visible_layers = p_render_data->camera_visible_layers;
1485
1486
if (p_render_data->view_count > 1) {
1487
for (uint32_t v = 0; v < p_render_data->view_count; v++) {
1488
projection = correction * p_render_data->view_projection[v];
1489
GLES3::MaterialStorage::store_camera(projection, scene_state.multiview_ubo.projection_matrix_view[v]);
1490
GLES3::MaterialStorage::store_camera(projection.inverse(), scene_state.multiview_ubo.inv_projection_matrix_view[v]);
1491
1492
scene_state.multiview_ubo.eye_offset[v][0] = p_render_data->view_eye_offset[v].x;
1493
scene_state.multiview_ubo.eye_offset[v][1] = p_render_data->view_eye_offset[v].y;
1494
scene_state.multiview_ubo.eye_offset[v][2] = p_render_data->view_eye_offset[v].z;
1495
scene_state.multiview_ubo.eye_offset[v][3] = 0.0;
1496
}
1497
}
1498
1499
// Only render the lights without shadows in the base pass.
1500
scene_state.ubo.directional_light_count = p_render_data->directional_light_count - p_render_data->directional_shadow_count;
1501
1502
scene_state.ubo.z_far = p_render_data->z_far;
1503
scene_state.ubo.z_near = p_render_data->z_near;
1504
1505
scene_state.ubo.viewport_size[0] = p_screen_size.x;
1506
scene_state.ubo.viewport_size[1] = p_screen_size.y;
1507
1508
Size2 screen_pixel_size = Vector2(1.0, 1.0) / Size2(p_screen_size);
1509
scene_state.ubo.screen_pixel_size[0] = screen_pixel_size.x;
1510
scene_state.ubo.screen_pixel_size[1] = screen_pixel_size.y;
1511
1512
scene_state.ubo.luminance_multiplier = p_render_data->luminance_multiplier;
1513
1514
scene_state.ubo.shadow_bias = p_shadow_bias;
1515
scene_state.ubo.pancake_shadows = p_pancake_shadows;
1516
1517
//time global variables
1518
scene_state.ubo.time = time;
1519
1520
if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
1521
scene_state.ubo.use_ambient_light = true;
1522
scene_state.ubo.ambient_light_color_energy[0] = 1;
1523
scene_state.ubo.ambient_light_color_energy[1] = 1;
1524
scene_state.ubo.ambient_light_color_energy[2] = 1;
1525
scene_state.ubo.ambient_light_color_energy[3] = 1.0;
1526
scene_state.ubo.use_ambient_cubemap = false;
1527
scene_state.ubo.use_reflection_cubemap = false;
1528
} else if (is_environment(p_render_data->environment)) {
1529
RS::EnvironmentBG env_bg = environment_get_background(p_render_data->environment);
1530
RS::EnvironmentAmbientSource ambient_src = environment_get_ambient_source(p_render_data->environment);
1531
1532
float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_render_data->environment);
1533
1534
scene_state.ubo.ambient_light_color_energy[3] = bg_energy_multiplier;
1535
1536
scene_state.ubo.ambient_color_sky_mix = environment_get_ambient_sky_contribution(p_render_data->environment);
1537
1538
//ambient
1539
if (ambient_src == RS::ENV_AMBIENT_SOURCE_BG && (env_bg == RS::ENV_BG_CLEAR_COLOR || env_bg == RS::ENV_BG_COLOR)) {
1540
Color color = env_bg == RS::ENV_BG_CLEAR_COLOR ? p_default_bg_color : environment_get_bg_color(p_render_data->environment);
1541
color = color.srgb_to_linear();
1542
1543
scene_state.ubo.ambient_light_color_energy[0] = color.r * bg_energy_multiplier;
1544
scene_state.ubo.ambient_light_color_energy[1] = color.g * bg_energy_multiplier;
1545
scene_state.ubo.ambient_light_color_energy[2] = color.b * bg_energy_multiplier;
1546
scene_state.ubo.use_ambient_light = true;
1547
scene_state.ubo.use_ambient_cubemap = false;
1548
} else {
1549
float energy = environment_get_ambient_light_energy(p_render_data->environment);
1550
Color color = environment_get_ambient_light(p_render_data->environment);
1551
color = color.srgb_to_linear();
1552
scene_state.ubo.ambient_light_color_energy[0] = color.r * energy;
1553
scene_state.ubo.ambient_light_color_energy[1] = color.g * energy;
1554
scene_state.ubo.ambient_light_color_energy[2] = color.b * energy;
1555
1556
Basis sky_transform = environment_get_sky_orientation(p_render_data->environment);
1557
sky_transform = sky_transform.inverse() * p_render_data->cam_transform.basis;
1558
GLES3::MaterialStorage::store_transform_3x3(sky_transform, scene_state.ubo.radiance_inverse_xform);
1559
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;
1560
scene_state.ubo.use_ambient_light = scene_state.ubo.use_ambient_cubemap || ambient_src == RS::ENV_AMBIENT_SOURCE_COLOR;
1561
}
1562
1563
//specular
1564
RS::EnvironmentReflectionSource ref_src = environment_get_reflection_source(p_render_data->environment);
1565
if ((ref_src == RS::ENV_REFLECTION_SOURCE_BG && env_bg == RS::ENV_BG_SKY) || ref_src == RS::ENV_REFLECTION_SOURCE_SKY) {
1566
scene_state.ubo.use_reflection_cubemap = true;
1567
} else {
1568
scene_state.ubo.use_reflection_cubemap = false;
1569
}
1570
1571
scene_state.ubo.fog_enabled = environment_get_fog_enabled(p_render_data->environment);
1572
scene_state.ubo.fog_mode = environment_get_fog_mode(p_render_data->environment);
1573
scene_state.ubo.fog_density = environment_get_fog_density(p_render_data->environment);
1574
scene_state.ubo.fog_height = environment_get_fog_height(p_render_data->environment);
1575
scene_state.ubo.fog_depth_curve = environment_get_fog_depth_curve(p_render_data->environment);
1576
scene_state.ubo.fog_depth_end = environment_get_fog_depth_end(p_render_data->environment) > 0.0 ? environment_get_fog_depth_end(p_render_data->environment) : scene_state.ubo.z_far;
1577
scene_state.ubo.fog_depth_begin = MIN(environment_get_fog_depth_begin(p_render_data->environment), scene_state.ubo.fog_depth_end - 0.001);
1578
scene_state.ubo.fog_height_density = environment_get_fog_height_density(p_render_data->environment);
1579
scene_state.ubo.fog_aerial_perspective = environment_get_fog_aerial_perspective(p_render_data->environment);
1580
1581
Color fog_color = environment_get_fog_light_color(p_render_data->environment).srgb_to_linear();
1582
float fog_energy = environment_get_fog_light_energy(p_render_data->environment);
1583
1584
scene_state.ubo.fog_light_color[0] = fog_color.r * fog_energy;
1585
scene_state.ubo.fog_light_color[1] = fog_color.g * fog_energy;
1586
scene_state.ubo.fog_light_color[2] = fog_color.b * fog_energy;
1587
1588
scene_state.ubo.fog_sun_scatter = environment_get_fog_sun_scatter(p_render_data->environment);
1589
1590
} else {
1591
}
1592
1593
if (p_render_data->camera_attributes.is_valid()) {
1594
scene_state.ubo.emissive_exposure_normalization = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1595
scene_state.ubo.IBL_exposure_normalization = 1.0;
1596
if (is_environment(p_render_data->environment)) {
1597
RID sky_rid = environment_get_sky(p_render_data->environment);
1598
if (sky_rid.is_valid()) {
1599
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);
1600
scene_state.ubo.IBL_exposure_normalization = current_exposure / MAX(0.001, sky_get_baked_exposure(sky_rid));
1601
}
1602
}
1603
} else if (scene_state.ubo.emissive_exposure_normalization > 0.0) {
1604
// This branch is triggered when using render_material().
1605
// Emissive is set outside the function, so don't set it.
1606
// IBL isn't used don't set it.
1607
} else {
1608
scene_state.ubo.emissive_exposure_normalization = 1.0;
1609
scene_state.ubo.IBL_exposure_normalization = 1.0;
1610
}
1611
1612
if (scene_state.ubo_buffer == 0) {
1613
glGenBuffers(1, &scene_state.ubo_buffer);
1614
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DATA_UNIFORM_LOCATION, scene_state.ubo_buffer);
1615
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");
1616
glBindBuffer(GL_UNIFORM_BUFFER, 0);
1617
} else {
1618
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DATA_UNIFORM_LOCATION, scene_state.ubo_buffer);
1619
glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::UBO), &scene_state.ubo, GL_STREAM_DRAW);
1620
}
1621
1622
glBindBuffer(GL_UNIFORM_BUFFER, 0);
1623
1624
if (p_render_data->view_count > 1) {
1625
if (scene_state.multiview_buffer == 0) {
1626
glGenBuffers(1, &scene_state.multiview_buffer);
1627
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
1628
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");
1629
} else {
1630
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
1631
glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::MultiviewUBO), &scene_state.multiview_ubo, GL_STREAM_DRAW);
1632
}
1633
1634
glBindBuffer(GL_UNIFORM_BUFFER, 0);
1635
}
1636
}
1637
1638
// Puts lights into Uniform Buffers. Needs to be called before _fill_list as this caches the index of each light in the Uniform Buffer
1639
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, uint32_t &r_directional_shadow_count) {
1640
GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
1641
GLES3::Config *config = GLES3::Config::get_singleton();
1642
1643
const Transform3D inverse_transform = p_render_data->inv_cam_transform;
1644
1645
const PagedArray<RID> &lights = *p_render_data->lights;
1646
1647
r_directional_light_count = 0;
1648
r_omni_light_count = 0;
1649
r_spot_light_count = 0;
1650
r_directional_shadow_count = 0;
1651
1652
int num_lights = lights.size();
1653
1654
for (int i = 0; i < num_lights; i++) {
1655
GLES3::LightInstance *li = GLES3::LightStorage::get_singleton()->get_light_instance(lights[i]);
1656
if (!li) {
1657
continue;
1658
}
1659
RID base = li->light;
1660
1661
ERR_CONTINUE(base.is_null());
1662
1663
RS::LightType type = light_storage->light_get_type(base);
1664
switch (type) {
1665
case RS::LIGHT_DIRECTIONAL: {
1666
if (r_directional_light_count >= RendererSceneRender::MAX_DIRECTIONAL_LIGHTS || light_storage->light_directional_get_sky_mode(base) == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
1667
continue;
1668
}
1669
1670
// If a DirectionalLight has shadows, we will add it to the end of the array and work in.
1671
bool has_shadow = light_storage->light_has_shadow(base);
1672
1673
int index = r_directional_light_count - r_directional_shadow_count;
1674
1675
if (has_shadow) {
1676
// Lights with shadow are incremented from the end of the array.
1677
index = MAX_DIRECTIONAL_LIGHTS - 1 - r_directional_shadow_count;
1678
}
1679
DirectionalLightData &light_data = scene_state.directional_lights[index];
1680
1681
Transform3D light_transform = li->transform;
1682
1683
Vector3 direction = inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, 1))).normalized();
1684
1685
light_data.direction[0] = direction.x;
1686
light_data.direction[1] = direction.y;
1687
light_data.direction[2] = direction.z;
1688
1689
light_data.bake_mode = light_storage->light_get_bake_mode(base);
1690
1691
float sign = light_storage->light_is_negative(base) ? -1 : 1;
1692
1693
light_data.energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY);
1694
1695
if (is_using_physical_light_units()) {
1696
light_data.energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
1697
} else {
1698
light_data.energy *= Math::PI;
1699
}
1700
1701
if (p_render_data->camera_attributes.is_valid()) {
1702
light_data.energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1703
}
1704
1705
Color linear_col = light_storage->light_get_color(base).srgb_to_linear();
1706
light_data.color[0] = linear_col.r;
1707
light_data.color[1] = linear_col.g;
1708
light_data.color[2] = linear_col.b;
1709
1710
float size = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
1711
light_data.size = 1.0 - Math::cos(Math::deg_to_rad(size)); //angle to cosine offset
1712
1713
light_data.specular = light_storage->light_get_param(base, RS::LIGHT_PARAM_SPECULAR);
1714
1715
light_data.shadow_opacity = (p_using_shadows && light_storage->light_has_shadow(base))
1716
? light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_OPACITY)
1717
: 0.0;
1718
1719
if (has_shadow) {
1720
DirectionalShadowData &shadow_data = scene_state.directional_shadows[MAX_DIRECTIONAL_LIGHTS - 1 - r_directional_shadow_count];
1721
1722
RS::LightDirectionalShadowMode shadow_mode = light_storage->light_directional_get_shadow_mode(base);
1723
1724
int limit = shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL ? 0 : (shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS ? 1 : 3);
1725
1726
shadow_data.shadow_atlas_pixel_size = 1.0 / light_storage->directional_shadow_get_size();
1727
1728
shadow_data.blend_splits = uint32_t((shadow_mode != RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL) && light_storage->light_directional_get_blend_splits(base));
1729
for (int j = 0; j < 4; j++) {
1730
Rect2 atlas_rect = li->shadow_transform[j].atlas_rect;
1731
Projection correction;
1732
correction.set_depth_correction(false, true, false);
1733
Projection matrix = correction * li->shadow_transform[j].camera;
1734
float split = li->shadow_transform[MIN(limit, j)].split;
1735
1736
Projection bias;
1737
bias.set_light_bias();
1738
Projection rectm;
1739
rectm.set_light_atlas_rect(atlas_rect);
1740
1741
Transform3D modelview = (inverse_transform * li->shadow_transform[j].transform).inverse();
1742
1743
shadow_data.direction[0] = light_data.direction[0];
1744
shadow_data.direction[1] = light_data.direction[1];
1745
shadow_data.direction[2] = light_data.direction[2];
1746
1747
Projection shadow_mtx = rectm * bias * matrix * modelview;
1748
shadow_data.shadow_split_offsets[j] = split;
1749
shadow_data.shadow_normal_bias[j] = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * li->shadow_transform[j].shadow_texel_size;
1750
GLES3::MaterialStorage::store_camera(shadow_mtx, shadow_data.shadow_matrices[j]);
1751
}
1752
float fade_start = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_FADE_START);
1753
shadow_data.fade_from = -shadow_data.shadow_split_offsets[3] * MIN(fade_start, 0.999);
1754
shadow_data.fade_to = -shadow_data.shadow_split_offsets[3];
1755
1756
r_directional_shadow_count++;
1757
}
1758
1759
r_directional_light_count++;
1760
} break;
1761
case RS::LIGHT_OMNI: {
1762
if (r_omni_light_count >= (uint32_t)config->max_renderable_lights) {
1763
continue;
1764
}
1765
1766
const real_t distance = p_render_data->cam_transform.origin.distance_to(li->transform.origin);
1767
1768
if (light_storage->light_is_distance_fade_enabled(li->light)) {
1769
const float fade_begin = light_storage->light_get_distance_fade_begin(li->light);
1770
const float fade_length = light_storage->light_get_distance_fade_length(li->light);
1771
1772
if (distance > fade_begin) {
1773
if (distance > fade_begin + fade_length) {
1774
// Out of range, don't draw this light to improve performance.
1775
continue;
1776
}
1777
}
1778
}
1779
1780
scene_state.omni_light_sort[r_omni_light_count].instance = li;
1781
scene_state.omni_light_sort[r_omni_light_count].depth = distance;
1782
r_omni_light_count++;
1783
} break;
1784
case RS::LIGHT_SPOT: {
1785
if (r_spot_light_count >= (uint32_t)config->max_renderable_lights) {
1786
continue;
1787
}
1788
1789
const real_t distance = p_render_data->cam_transform.origin.distance_to(li->transform.origin);
1790
1791
if (light_storage->light_is_distance_fade_enabled(li->light)) {
1792
const float fade_begin = light_storage->light_get_distance_fade_begin(li->light);
1793
const float fade_length = light_storage->light_get_distance_fade_length(li->light);
1794
1795
if (distance > fade_begin) {
1796
if (distance > fade_begin + fade_length) {
1797
// Out of range, don't draw this light to improve performance.
1798
continue;
1799
}
1800
}
1801
}
1802
1803
scene_state.spot_light_sort[r_spot_light_count].instance = li;
1804
scene_state.spot_light_sort[r_spot_light_count].depth = distance;
1805
r_spot_light_count++;
1806
} break;
1807
}
1808
1809
li->last_pass = RSG::rasterizer->get_frame_number();
1810
}
1811
1812
if (r_omni_light_count) {
1813
SortArray<InstanceSort<GLES3::LightInstance>> sorter;
1814
sorter.sort(scene_state.omni_light_sort, r_omni_light_count);
1815
}
1816
1817
if (r_spot_light_count) {
1818
SortArray<InstanceSort<GLES3::LightInstance>> sorter;
1819
sorter.sort(scene_state.spot_light_sort, r_spot_light_count);
1820
}
1821
1822
int num_positional_shadows = 0;
1823
1824
for (uint32_t i = 0; i < (r_omni_light_count + r_spot_light_count); i++) {
1825
uint32_t index = (i < r_omni_light_count) ? i : i - (r_omni_light_count);
1826
LightData &light_data = (i < r_omni_light_count) ? scene_state.omni_lights[index] : scene_state.spot_lights[index];
1827
RS::LightType type = (i < r_omni_light_count) ? RS::LIGHT_OMNI : RS::LIGHT_SPOT;
1828
GLES3::LightInstance *li = (i < r_omni_light_count) ? scene_state.omni_light_sort[index].instance : scene_state.spot_light_sort[index].instance;
1829
real_t distance = (i < r_omni_light_count) ? scene_state.omni_light_sort[index].depth : scene_state.spot_light_sort[index].depth;
1830
RID base = li->light;
1831
1832
li->gl_id = index;
1833
1834
Transform3D light_transform = li->transform;
1835
Vector3 pos = inverse_transform.xform(light_transform.origin);
1836
1837
light_data.position[0] = pos.x;
1838
light_data.position[1] = pos.y;
1839
light_data.position[2] = pos.z;
1840
1841
light_data.bake_mode = light_storage->light_get_bake_mode(base);
1842
1843
float radius = MAX(0.001, light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE));
1844
light_data.inv_radius = 1.0 / radius;
1845
1846
Vector3 direction = inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, -1))).normalized();
1847
1848
light_data.direction[0] = direction.x;
1849
light_data.direction[1] = direction.y;
1850
light_data.direction[2] = direction.z;
1851
1852
float size = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
1853
1854
light_data.size = size;
1855
1856
float sign = light_storage->light_is_negative(base) ? -1 : 1;
1857
Color linear_col = light_storage->light_get_color(base).srgb_to_linear();
1858
1859
// Reuse fade begin, fade length and distance for shadow LOD determination later.
1860
float fade_begin = 0.0;
1861
float fade_shadow = 0.0;
1862
float fade_length = 0.0;
1863
1864
float fade = 1.0;
1865
float shadow_opacity_fade = 1.0;
1866
1867
if (light_storage->light_is_distance_fade_enabled(base)) {
1868
fade_begin = light_storage->light_get_distance_fade_begin(base);
1869
fade_shadow = light_storage->light_get_distance_fade_shadow(base);
1870
fade_length = light_storage->light_get_distance_fade_length(base);
1871
1872
if (distance > fade_begin) {
1873
// Use `smoothstep()` to make opacity changes more gradual and less noticeable to the player.
1874
fade = Math::smoothstep(0.0f, 1.0f, 1.0f - float(distance - fade_begin) / fade_length);
1875
}
1876
if (distance > fade_shadow) {
1877
shadow_opacity_fade = Math::smoothstep(0.0f, 1.0f, 1.0f - float(distance - fade_shadow) / fade_length);
1878
}
1879
}
1880
1881
float energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY) * fade;
1882
1883
if (is_using_physical_light_units()) {
1884
energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
1885
1886
// Convert from Luminous Power to Luminous Intensity
1887
if (type == RS::LIGHT_OMNI) {
1888
energy *= 1.0 / (Math::PI * 4.0);
1889
} else {
1890
// Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
1891
// We make this assumption to keep them easy to control.
1892
energy *= 1.0 / Math::PI;
1893
}
1894
} else {
1895
energy *= Math::PI;
1896
}
1897
1898
if (p_render_data->camera_attributes.is_valid()) {
1899
energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
1900
}
1901
1902
light_data.color[0] = linear_col.r * energy;
1903
light_data.color[1] = linear_col.g * energy;
1904
light_data.color[2] = linear_col.b * energy;
1905
1906
light_data.attenuation = light_storage->light_get_param(base, RS::LIGHT_PARAM_ATTENUATION);
1907
1908
light_data.inv_spot_attenuation = 1.0f / light_storage->light_get_param(base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
1909
1910
float spot_angle = light_storage->light_get_param(base, RS::LIGHT_PARAM_SPOT_ANGLE);
1911
light_data.cos_spot_angle = Math::cos(Math::deg_to_rad(spot_angle));
1912
1913
light_data.specular_amount = light_storage->light_get_param(base, RS::LIGHT_PARAM_SPECULAR) * 2.0;
1914
1915
// Setup shadows
1916
const bool needs_shadow =
1917
p_using_shadows &&
1918
light_storage->owns_shadow_atlas(p_render_data->shadow_atlas) &&
1919
light_storage->shadow_atlas_owns_light_instance(p_render_data->shadow_atlas, li->self) &&
1920
light_storage->light_has_shadow(base);
1921
1922
bool in_shadow_range = true;
1923
if (needs_shadow && light_storage->light_is_distance_fade_enabled(base)) {
1924
if (distance > fade_shadow + fade_length) {
1925
// Out of range, don't draw shadows to improve performance.
1926
in_shadow_range = false;
1927
}
1928
}
1929
1930
// Fill in the shadow information.
1931
if (needs_shadow && in_shadow_range) {
1932
if (num_positional_shadows >= config->max_renderable_lights) {
1933
continue;
1934
}
1935
ShadowData &shadow_data = scene_state.positional_shadows[num_positional_shadows];
1936
li->shadow_id = num_positional_shadows;
1937
num_positional_shadows++;
1938
1939
light_data.shadow_opacity = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_OPACITY) * shadow_opacity_fade;
1940
1941
float shadow_texel_size = light_storage->light_instance_get_shadow_texel_size(li->self, p_render_data->shadow_atlas);
1942
shadow_data.shadow_atlas_pixel_size = shadow_texel_size;
1943
shadow_data.shadow_normal_bias = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * shadow_texel_size * 10.0;
1944
1945
shadow_data.light_position[0] = light_data.position[0];
1946
shadow_data.light_position[1] = light_data.position[1];
1947
shadow_data.light_position[2] = light_data.position[2];
1948
1949
if (type == RS::LIGHT_OMNI) {
1950
Transform3D proj = (inverse_transform * light_transform).inverse();
1951
1952
GLES3::MaterialStorage::store_transform(proj, shadow_data.shadow_matrix);
1953
1954
} else if (type == RS::LIGHT_SPOT) {
1955
Transform3D modelview = (inverse_transform * light_transform).inverse();
1956
Projection bias;
1957
bias.set_light_bias();
1958
1959
Projection correction;
1960
correction.set_depth_correction(false, true, false);
1961
Projection cm = correction * li->shadow_transform[0].camera;
1962
Projection shadow_mtx = bias * cm * modelview;
1963
GLES3::MaterialStorage::store_camera(shadow_mtx, shadow_data.shadow_matrix);
1964
}
1965
}
1966
}
1967
1968
// TODO, to avoid stalls, should rotate between 3 buffers based on frame index.
1969
// TODO, consider mapping the buffer as in 2D
1970
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_OMNILIGHT_UNIFORM_LOCATION, scene_state.omni_light_buffer);
1971
if (r_omni_light_count) {
1972
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(LightData) * r_omni_light_count, scene_state.omni_lights);
1973
}
1974
1975
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_SPOTLIGHT_UNIFORM_LOCATION, scene_state.spot_light_buffer);
1976
if (r_spot_light_count) {
1977
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(LightData) * r_spot_light_count, scene_state.spot_lights);
1978
}
1979
1980
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DIRECTIONAL_LIGHT_UNIFORM_LOCATION, scene_state.directional_light_buffer);
1981
if (r_directional_light_count) {
1982
glBufferData(GL_UNIFORM_BUFFER, sizeof(DirectionalLightData) * MAX_DIRECTIONAL_LIGHTS, scene_state.directional_lights, GL_STREAM_DRAW);
1983
}
1984
1985
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_POSITIONAL_SHADOW_UNIFORM_LOCATION, scene_state.positional_shadow_buffer);
1986
if (num_positional_shadows) {
1987
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(ShadowData) * num_positional_shadows, scene_state.positional_shadows);
1988
}
1989
1990
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DIRECTIONAL_SHADOW_UNIFORM_LOCATION, scene_state.directional_shadow_buffer);
1991
if (r_directional_shadow_count) {
1992
glBufferData(GL_UNIFORM_BUFFER, sizeof(DirectionalShadowData) * MAX_DIRECTIONAL_LIGHTS, scene_state.directional_shadows, GL_STREAM_DRAW);
1993
}
1994
glBindBuffer(GL_UNIFORM_BUFFER, 0);
1995
}
1996
1997
// Render shadows
1998
void RasterizerSceneGLES3::_render_shadows(const RenderDataGLES3 *p_render_data, const Size2i &p_viewport_size) {
1999
GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
2000
2001
LocalVector<int> cube_shadows;
2002
LocalVector<int> shadows;
2003
LocalVector<int> directional_shadows;
2004
2005
float lod_distance_multiplier = p_render_data->cam_projection.get_lod_multiplier();
2006
2007
// Put lights into buckets for omni (cube shadows), directional, and spot.
2008
{
2009
for (int i = 0; i < p_render_data->render_shadow_count; i++) {
2010
RID li = p_render_data->render_shadows[i].light;
2011
RID base = light_storage->light_instance_get_base_light(li);
2012
2013
if (light_storage->light_get_type(base) == RS::LIGHT_DIRECTIONAL) {
2014
directional_shadows.push_back(i);
2015
} else if (light_storage->light_get_type(base) == RS::LIGHT_OMNI && light_storage->light_omni_get_shadow_mode(base) == RS::LIGHT_OMNI_SHADOW_CUBE) {
2016
cube_shadows.push_back(i);
2017
} else {
2018
shadows.push_back(i);
2019
}
2020
}
2021
if (directional_shadows.size()) {
2022
light_storage->update_directional_shadow_atlas();
2023
}
2024
}
2025
2026
bool render_shadows = directional_shadows.size() || shadows.size() || cube_shadows.size();
2027
2028
if (render_shadows) {
2029
RENDER_TIMESTAMP("Render Shadows");
2030
2031
// Render cubemap shadows.
2032
for (const int &index : cube_shadows) {
2033
_render_shadow_pass(p_render_data->render_shadows[index].light, p_render_data->shadow_atlas, p_render_data->render_shadows[index].pass, p_render_data->render_shadows[index].instances, lod_distance_multiplier, p_render_data->screen_mesh_lod_threshold, p_render_data->render_info, p_viewport_size, p_render_data->cam_transform);
2034
}
2035
// Render directional shadows.
2036
for (uint32_t i = 0; i < directional_shadows.size(); i++) {
2037
_render_shadow_pass(p_render_data->render_shadows[directional_shadows[i]].light, p_render_data->shadow_atlas, p_render_data->render_shadows[directional_shadows[i]].pass, p_render_data->render_shadows[directional_shadows[i]].instances, lod_distance_multiplier, p_render_data->screen_mesh_lod_threshold, p_render_data->render_info, p_viewport_size, p_render_data->cam_transform);
2038
}
2039
// Render positional shadows (Spotlight and Omnilight with dual-paraboloid).
2040
for (uint32_t i = 0; i < shadows.size(); i++) {
2041
_render_shadow_pass(p_render_data->render_shadows[shadows[i]].light, p_render_data->shadow_atlas, p_render_data->render_shadows[shadows[i]].pass, p_render_data->render_shadows[shadows[i]].instances, lod_distance_multiplier, p_render_data->screen_mesh_lod_threshold, p_render_data->render_info, p_viewport_size, p_render_data->cam_transform);
2042
}
2043
}
2044
}
2045
2046
void RasterizerSceneGLES3::_render_shadow_pass(RID p_light, RID p_shadow_atlas, int p_pass, const PagedArray<RenderGeometryInstance *> &p_instances, float p_lod_distance_multiplier, float p_screen_mesh_lod_threshold, RenderingMethod::RenderInfo *p_render_info, const Size2i &p_viewport_size, const Transform3D &p_main_cam_transform) {
2047
GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
2048
2049
ERR_FAIL_COND(!light_storage->owns_light_instance(p_light));
2050
2051
RID base = light_storage->light_instance_get_base_light(p_light);
2052
2053
float zfar = 0.0;
2054
bool use_pancake = false;
2055
float shadow_bias = 0.0;
2056
bool reverse_cull = false;
2057
bool needs_clear = false;
2058
2059
Projection light_projection;
2060
Transform3D light_transform;
2061
GLuint shadow_fb = 0;
2062
Rect2i atlas_rect;
2063
2064
if (light_storage->light_get_type(base) == RS::LIGHT_DIRECTIONAL) {
2065
// Set pssm stuff.
2066
uint64_t last_scene_shadow_pass = light_storage->light_instance_get_shadow_pass(p_light);
2067
if (last_scene_shadow_pass != get_scene_pass()) {
2068
light_storage->light_instance_set_directional_rect(p_light, light_storage->get_directional_shadow_rect());
2069
light_storage->directional_shadow_increase_current_light();
2070
light_storage->light_instance_set_shadow_pass(p_light, get_scene_pass());
2071
}
2072
2073
atlas_rect = light_storage->light_instance_get_directional_rect(p_light);
2074
2075
if (light_storage->light_directional_get_shadow_mode(base) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
2076
atlas_rect.size.width /= 2;
2077
atlas_rect.size.height /= 2;
2078
2079
if (p_pass == 1) {
2080
atlas_rect.position.x += atlas_rect.size.width;
2081
} else if (p_pass == 2) {
2082
atlas_rect.position.y += atlas_rect.size.height;
2083
} else if (p_pass == 3) {
2084
atlas_rect.position += atlas_rect.size;
2085
}
2086
} else if (light_storage->light_directional_get_shadow_mode(base) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
2087
atlas_rect.size.height /= 2;
2088
2089
if (p_pass == 0) {
2090
} else {
2091
atlas_rect.position.y += atlas_rect.size.height;
2092
}
2093
}
2094
2095
use_pancake = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE) > 0;
2096
light_projection = light_storage->light_instance_get_shadow_camera(p_light, p_pass);
2097
light_transform = light_storage->light_instance_get_shadow_transform(p_light, p_pass);
2098
2099
float directional_shadow_size = light_storage->directional_shadow_get_size();
2100
Rect2 atlas_rect_norm = atlas_rect;
2101
atlas_rect_norm.position /= directional_shadow_size;
2102
atlas_rect_norm.size /= directional_shadow_size;
2103
light_storage->light_instance_set_directional_shadow_atlas_rect(p_light, p_pass, atlas_rect_norm);
2104
2105
zfar = RSG::light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE);
2106
shadow_fb = light_storage->direction_shadow_get_fb();
2107
reverse_cull = !light_storage->light_get_reverse_cull_face_mode(base);
2108
2109
float bias_scale = light_storage->light_instance_get_shadow_bias_scale(p_light, p_pass);
2110
shadow_bias = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BIAS) / 100.0 * bias_scale;
2111
2112
} else {
2113
// Set from shadow atlas.
2114
2115
ERR_FAIL_COND(!light_storage->owns_shadow_atlas(p_shadow_atlas));
2116
ERR_FAIL_COND(!light_storage->shadow_atlas_owns_light_instance(p_shadow_atlas, p_light));
2117
2118
uint32_t key = light_storage->shadow_atlas_get_light_instance_key(p_shadow_atlas, p_light);
2119
2120
uint32_t quadrant = (key >> GLES3::LightStorage::QUADRANT_SHIFT) & 0x3;
2121
uint32_t shadow = key & GLES3::LightStorage::SHADOW_INDEX_MASK;
2122
2123
ERR_FAIL_INDEX((int)shadow, light_storage->shadow_atlas_get_quadrant_shadows_length(p_shadow_atlas, quadrant));
2124
2125
int shadow_size = light_storage->shadow_atlas_get_quadrant_shadow_size(p_shadow_atlas, quadrant);
2126
2127
shadow_fb = light_storage->shadow_atlas_get_quadrant_shadow_fb(p_shadow_atlas, quadrant, shadow);
2128
2129
zfar = light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE);
2130
reverse_cull = !light_storage->light_get_reverse_cull_face_mode(base);
2131
2132
if (light_storage->light_get_type(base) == RS::LIGHT_OMNI) {
2133
if (light_storage->light_omni_get_shadow_mode(base) == RS::LIGHT_OMNI_SHADOW_CUBE) {
2134
GLuint shadow_texture = light_storage->shadow_atlas_get_quadrant_shadow_texture(p_shadow_atlas, quadrant, shadow);
2135
glBindFramebuffer(GL_FRAMEBUFFER, shadow_fb);
2136
2137
static GLenum cube_map_faces[6] = {
2138
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
2139
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
2140
// Flipped order for Y to match what the RD renderer expects
2141
// (and thus what is given to us by the Rendering Server).
2142
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
2143
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
2144
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
2145
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
2146
};
2147
2148
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, cube_map_faces[p_pass], shadow_texture, 0);
2149
2150
light_projection = light_storage->light_instance_get_shadow_camera(p_light, p_pass);
2151
light_transform = light_storage->light_instance_get_shadow_transform(p_light, p_pass);
2152
shadow_size = shadow_size / 2;
2153
} else {
2154
ERR_FAIL_MSG("Dual paraboloid shadow mode not supported in the Compatibility renderer. Please use CubeMap shadow mode instead.");
2155
}
2156
2157
shadow_bias = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BIAS);
2158
2159
} else if (light_storage->light_get_type(base) == RS::LIGHT_SPOT) {
2160
light_projection = light_storage->light_instance_get_shadow_camera(p_light, 0);
2161
light_transform = light_storage->light_instance_get_shadow_transform(p_light, 0);
2162
2163
shadow_bias = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BIAS) / 10.0;
2164
// Prebake range into bias so we can scale based on distance easily.
2165
shadow_bias *= light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE);
2166
}
2167
atlas_rect.size.x = shadow_size;
2168
atlas_rect.size.y = shadow_size;
2169
2170
needs_clear = true;
2171
}
2172
2173
RenderDataGLES3 render_data;
2174
render_data.cam_projection = light_projection;
2175
render_data.cam_transform = light_transform;
2176
render_data.inv_cam_transform = light_transform.affine_inverse();
2177
render_data.z_far = zfar; // Only used by OmniLights.
2178
render_data.z_near = 0.0;
2179
render_data.lod_distance_multiplier = p_lod_distance_multiplier;
2180
render_data.main_cam_transform = p_main_cam_transform;
2181
2182
render_data.instances = &p_instances;
2183
render_data.render_info = p_render_info;
2184
2185
_setup_environment(&render_data, true, p_viewport_size, false, Color(), use_pancake, shadow_bias);
2186
2187
if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) {
2188
render_data.screen_mesh_lod_threshold = 0.0;
2189
} else {
2190
render_data.screen_mesh_lod_threshold = p_screen_mesh_lod_threshold;
2191
}
2192
2193
_fill_render_list(RENDER_LIST_SECONDARY, &render_data, PASS_MODE_SHADOW);
2194
render_list[RENDER_LIST_SECONDARY].sort_by_key();
2195
2196
glBindFramebuffer(GL_FRAMEBUFFER, shadow_fb);
2197
glViewport(atlas_rect.position.x, atlas_rect.position.y, atlas_rect.size.x, atlas_rect.size.y);
2198
2199
GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
2200
2201
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer);
2202
glBindBuffer(GL_UNIFORM_BUFFER, 0);
2203
2204
scene_state.reset_gl_state();
2205
scene_state.enable_gl_depth_test(true);
2206
scene_state.enable_gl_depth_draw(true);
2207
scene_state.set_gl_depth_func(GL_GREATER);
2208
2209
glColorMask(0, 0, 0, 0);
2210
glDrawBuffers(0, nullptr);
2211
RasterizerGLES3::clear_depth(0.0);
2212
if (needs_clear) {
2213
glClear(GL_DEPTH_BUFFER_BIT);
2214
}
2215
2216
uint64_t spec_constant_base_flags = SceneShaderGLES3::DISABLE_LIGHTMAP |
2217
SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL |
2218
SceneShaderGLES3::DISABLE_LIGHT_OMNI |
2219
SceneShaderGLES3::DISABLE_LIGHT_SPOT |
2220
SceneShaderGLES3::DISABLE_FOG |
2221
SceneShaderGLES3::RENDER_SHADOWS;
2222
2223
if (light_storage->light_get_type(base) == RS::LIGHT_OMNI) {
2224
spec_constant_base_flags |= SceneShaderGLES3::RENDER_SHADOWS_LINEAR;
2225
}
2226
2227
RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), reverse_cull, spec_constant_base_flags, false);
2228
2229
_render_list_template<PASS_MODE_SHADOW>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());
2230
2231
glColorMask(1, 1, 1, 1);
2232
scene_state.enable_gl_depth_test(false);
2233
scene_state.enable_gl_depth_draw(true);
2234
glDisable(GL_CULL_FACE);
2235
scene_state.cull_mode = RS::CULL_MODE_DISABLED;
2236
glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
2237
}
2238
2239
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_compositor, 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) {
2240
GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
2241
GLES3::Config *config = GLES3::Config::get_singleton();
2242
RENDER_TIMESTAMP("Setup 3D Scene");
2243
2244
bool apply_color_adjustments_in_post = false;
2245
bool is_reflection_probe = p_reflection_probe.is_valid();
2246
2247
Ref<RenderSceneBuffersGLES3> rb = p_render_buffers;
2248
ERR_FAIL_COND(rb.is_null());
2249
2250
if (rb->get_scaling_3d_mode() != RS::VIEWPORT_SCALING_3D_MODE_OFF) {
2251
// If we're scaling, we apply tonemapping etc. in post, so disable it during rendering
2252
apply_color_adjustments_in_post = true;
2253
}
2254
2255
GLES3::RenderTarget *rt = nullptr; // No render target for reflection probe
2256
if (!is_reflection_probe) {
2257
rt = texture_storage->get_render_target(rb->render_target);
2258
ERR_FAIL_NULL(rt);
2259
}
2260
2261
bool glow_enabled = false;
2262
if (p_environment.is_valid()) {
2263
glow_enabled = environment_get_glow_enabled(p_environment);
2264
if (glow_enabled) {
2265
// If glow is enabled, we apply tonemapping etc. in post, so disable it during rendering
2266
apply_color_adjustments_in_post = true;
2267
}
2268
}
2269
2270
// Assign render data
2271
// Use the format from rendererRD
2272
RenderDataGLES3 render_data;
2273
{
2274
render_data.render_buffers = rb;
2275
2276
if (rt) {
2277
render_data.transparent_bg = rt->is_transparent;
2278
render_data.render_region = rt->render_region;
2279
}
2280
2281
// Our first camera is used by default
2282
render_data.cam_transform = p_camera_data->main_transform;
2283
render_data.inv_cam_transform = render_data.cam_transform.affine_inverse();
2284
render_data.cam_projection = p_camera_data->main_projection;
2285
render_data.cam_orthogonal = p_camera_data->is_orthogonal;
2286
render_data.cam_frustum = p_camera_data->is_frustum;
2287
render_data.camera_visible_layers = p_camera_data->visible_layers;
2288
render_data.main_cam_transform = p_camera_data->main_transform;
2289
2290
render_data.view_count = p_camera_data->view_count;
2291
for (uint32_t v = 0; v < p_camera_data->view_count; v++) {
2292
render_data.view_eye_offset[v] = p_camera_data->view_offset[v].origin;
2293
render_data.view_projection[v] = p_camera_data->view_projection[v];
2294
}
2295
2296
render_data.z_near = p_camera_data->main_projection.get_z_near();
2297
render_data.z_far = p_camera_data->main_projection.get_z_far();
2298
2299
render_data.instances = &p_instances;
2300
render_data.lights = &p_lights;
2301
render_data.reflection_probes = &p_reflection_probes;
2302
render_data.environment = p_environment;
2303
render_data.camera_attributes = p_camera_attributes;
2304
render_data.shadow_atlas = p_shadow_atlas;
2305
render_data.reflection_probe = p_reflection_probe;
2306
render_data.reflection_probe_pass = p_reflection_probe_pass;
2307
2308
// this should be the same for all cameras..
2309
render_data.lod_distance_multiplier = p_camera_data->main_projection.get_lod_multiplier();
2310
2311
if (rt != nullptr && rt->color_type == GL_UNSIGNED_INT_2_10_10_10_REV && glow_enabled) {
2312
// As our output is in sRGB and we're using 10bit color space, we can fake a little HDR to do glow...
2313
render_data.luminance_multiplier = 0.25;
2314
} else {
2315
render_data.luminance_multiplier = 1.0;
2316
}
2317
2318
if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) {
2319
render_data.screen_mesh_lod_threshold = 0.0;
2320
} else {
2321
render_data.screen_mesh_lod_threshold = p_screen_mesh_lod_threshold;
2322
}
2323
render_data.render_info = r_render_info;
2324
render_data.render_shadows = p_render_shadows;
2325
render_data.render_shadow_count = p_render_shadow_count;
2326
}
2327
2328
PagedArray<RID> empty;
2329
2330
if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
2331
render_data.lights = &empty;
2332
render_data.reflection_probes = &empty;
2333
}
2334
2335
bool reverse_cull = render_data.cam_transform.basis.determinant() < 0;
2336
2337
///////////
2338
// Fill Light lists here
2339
//////////
2340
2341
GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
2342
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer);
2343
2344
Color clear_color;
2345
if (!is_reflection_probe && rb->render_target.is_valid()) {
2346
clear_color = texture_storage->render_target_get_clear_request_color(rb->render_target);
2347
} else {
2348
clear_color = texture_storage->get_default_clear_color();
2349
}
2350
2351
bool fb_cleared = false;
2352
2353
Size2i screen_size = rb->internal_size;
2354
2355
bool use_wireframe = get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_WIREFRAME;
2356
2357
SceneState::TonemapUBO tonemap_ubo;
2358
if (render_data.environment.is_valid()) {
2359
bool use_bcs = environment_get_adjustments_enabled(render_data.environment);
2360
if (use_bcs) {
2361
apply_color_adjustments_in_post = true;
2362
}
2363
2364
tonemap_ubo.exposure = environment_get_exposure(render_data.environment);
2365
tonemap_ubo.white = environment_get_white(render_data.environment);
2366
tonemap_ubo.tonemapper = int32_t(environment_get_tone_mapper(render_data.environment));
2367
2368
tonemap_ubo.brightness = environment_get_adjustments_brightness(render_data.environment);
2369
tonemap_ubo.contrast = environment_get_adjustments_contrast(render_data.environment);
2370
tonemap_ubo.saturation = environment_get_adjustments_saturation(render_data.environment);
2371
}
2372
2373
if (scene_state.tonemap_buffer == 0) {
2374
// Only create if using 3D
2375
glGenBuffers(1, &scene_state.tonemap_buffer);
2376
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_TONEMAP_UNIFORM_LOCATION, scene_state.tonemap_buffer);
2377
GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.tonemap_buffer, sizeof(SceneState::TonemapUBO), &tonemap_ubo, GL_STREAM_DRAW, "Tonemap UBO");
2378
} else {
2379
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_TONEMAP_UNIFORM_LOCATION, scene_state.tonemap_buffer);
2380
glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::TonemapUBO), &tonemap_ubo, GL_STREAM_DRAW);
2381
}
2382
2383
glBindBuffer(GL_UNIFORM_BUFFER, 0);
2384
2385
scene_state.ubo.emissive_exposure_normalization = -1.0; // Use default exposure normalization.
2386
2387
bool flip_y = !is_reflection_probe;
2388
2389
if (rt && rt->overridden.color.is_valid()) {
2390
// If we've overridden the render target's color texture, then don't render upside down.
2391
// We're probably rendering directly to an XR device.
2392
flip_y = false;
2393
}
2394
if (!flip_y) {
2395
// If we're rendering right-side up, then we need to change the winding order.
2396
glFrontFace(GL_CW);
2397
}
2398
_render_shadows(&render_data, screen_size);
2399
2400
_setup_lights(&render_data, true, render_data.directional_light_count, render_data.omni_light_count, render_data.spot_light_count, render_data.directional_shadow_count);
2401
_setup_environment(&render_data, is_reflection_probe, screen_size, flip_y, clear_color, false);
2402
2403
_fill_render_list(RENDER_LIST_OPAQUE, &render_data, PASS_MODE_COLOR);
2404
render_list[RENDER_LIST_OPAQUE].sort_by_key();
2405
render_list[RENDER_LIST_ALPHA].sort_by_reverse_depth_and_priority();
2406
2407
bool draw_sky = false;
2408
bool draw_sky_fog_only = false;
2409
bool keep_color = false;
2410
bool draw_canvas = false;
2411
bool draw_feed = false;
2412
float sky_energy_multiplier = 1.0;
2413
int camera_feed_id = -1;
2414
2415
if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW)) {
2416
clear_color = Color(0, 0, 0, 1); //in overdraw mode, BG should always be black
2417
} else if (render_data.environment.is_valid()) {
2418
RS::EnvironmentBG bg_mode = environment_get_background(render_data.environment);
2419
float bg_energy_multiplier = environment_get_bg_energy_multiplier(render_data.environment);
2420
bg_energy_multiplier *= environment_get_bg_intensity(render_data.environment);
2421
RS::EnvironmentReflectionSource reflection_source = environment_get_reflection_source(render_data.environment);
2422
RS::EnvironmentAmbientSource ambient_source = environment_get_ambient_source(render_data.environment);
2423
2424
if (render_data.camera_attributes.is_valid()) {
2425
bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(render_data.camera_attributes);
2426
}
2427
2428
switch (bg_mode) {
2429
case RS::ENV_BG_CLEAR_COLOR: {
2430
clear_color.r *= bg_energy_multiplier;
2431
clear_color.g *= bg_energy_multiplier;
2432
clear_color.b *= bg_energy_multiplier;
2433
if (!render_data.transparent_bg && environment_get_fog_enabled(render_data.environment)) {
2434
draw_sky_fog_only = true;
2435
GLES3::MaterialStorage::get_singleton()->material_set_param(sky_globals.fog_material, "clear_color", Variant(clear_color));
2436
}
2437
} break;
2438
case RS::ENV_BG_COLOR: {
2439
clear_color = environment_get_bg_color(render_data.environment);
2440
clear_color.r *= bg_energy_multiplier;
2441
clear_color.g *= bg_energy_multiplier;
2442
clear_color.b *= bg_energy_multiplier;
2443
if (!render_data.transparent_bg && environment_get_fog_enabled(render_data.environment)) {
2444
draw_sky_fog_only = true;
2445
GLES3::MaterialStorage::get_singleton()->material_set_param(sky_globals.fog_material, "clear_color", Variant(clear_color));
2446
}
2447
} break;
2448
case RS::ENV_BG_SKY: {
2449
draw_sky = !render_data.transparent_bg;
2450
} break;
2451
case RS::ENV_BG_CANVAS: {
2452
draw_canvas = true;
2453
} break;
2454
case RS::ENV_BG_KEEP: {
2455
keep_color = true;
2456
} break;
2457
case RS::ENV_BG_CAMERA_FEED: {
2458
camera_feed_id = environment_get_camera_feed_id(render_data.environment);
2459
draw_feed = true;
2460
keep_color = true;
2461
} break;
2462
default: {
2463
}
2464
}
2465
2466
bool sky_reflections = reflection_source == RS::ENV_REFLECTION_SOURCE_SKY;
2467
sky_reflections |= reflection_source == RS::ENV_REFLECTION_SOURCE_BG && bg_mode == RS::ENV_BG_SKY;
2468
bool sky_ambient = ambient_source == RS::ENV_AMBIENT_SOURCE_SKY;
2469
sky_ambient |= ambient_source == RS::ENV_AMBIENT_SOURCE_BG && bg_mode == RS::ENV_BG_SKY;
2470
2471
// setup sky if used for ambient, reflections, or background
2472
if (draw_sky || draw_sky_fog_only || sky_reflections || sky_ambient) {
2473
RENDER_TIMESTAMP("Setup Sky");
2474
Projection projection = render_data.cam_projection;
2475
if (is_reflection_probe) {
2476
Projection correction;
2477
correction.set_depth_correction(true, true, false);
2478
projection = correction * render_data.cam_projection;
2479
}
2480
2481
sky_energy_multiplier *= bg_energy_multiplier;
2482
2483
_setup_sky(&render_data, *render_data.lights, projection, render_data.cam_transform, screen_size);
2484
2485
if (environment_get_sky(render_data.environment).is_valid()) {
2486
if (sky_reflections || sky_ambient) {
2487
_update_sky_radiance(render_data.environment, projection, render_data.cam_transform, sky_energy_multiplier);
2488
}
2489
} else {
2490
// do not try to draw sky if invalid
2491
draw_sky = false;
2492
}
2493
}
2494
}
2495
2496
GLuint fbo = 0;
2497
if (is_reflection_probe && GLES3::LightStorage::get_singleton()->reflection_probe_has_atlas_index(render_data.reflection_probe)) {
2498
fbo = GLES3::LightStorage::get_singleton()->reflection_probe_instance_get_framebuffer(render_data.reflection_probe, render_data.reflection_probe_pass);
2499
} else {
2500
rb->set_apply_color_adjustments_in_post(apply_color_adjustments_in_post);
2501
fbo = rb->get_render_fbo();
2502
}
2503
2504
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2505
glViewport(0, 0, rb->internal_size.x, rb->internal_size.y);
2506
2507
scene_state.reset_gl_state();
2508
2509
// Do depth prepass if it's explicitly enabled
2510
bool use_depth_prepass = config->use_depth_prepass;
2511
2512
// Forcibly enable depth prepass if opaque stencil writes are used.
2513
use_depth_prepass = use_depth_prepass || scene_state.used_opaque_stencil;
2514
2515
// Don't do depth prepass we are rendering overdraw
2516
use_depth_prepass = use_depth_prepass && get_debug_draw_mode() != RS::VIEWPORT_DEBUG_DRAW_OVERDRAW;
2517
2518
if (use_depth_prepass) {
2519
RENDER_TIMESTAMP("Depth Prepass");
2520
//pre z pass
2521
2522
if (render_data.render_region != Rect2i()) {
2523
glViewport(render_data.render_region.position.x, render_data.render_region.position.y, render_data.render_region.size.width, render_data.render_region.size.height);
2524
}
2525
2526
scene_state.enable_gl_depth_test(true);
2527
scene_state.enable_gl_depth_draw(true);
2528
scene_state.enable_gl_blend(false);
2529
scene_state.set_gl_depth_func(GL_GEQUAL);
2530
scene_state.enable_gl_scissor_test(false);
2531
scene_state.enable_gl_stencil_test(false);
2532
2533
glColorMask(0, 0, 0, 0);
2534
RasterizerGLES3::clear_depth(0.0);
2535
RasterizerGLES3::clear_stencil(0);
2536
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2537
// Some desktop GL implementations fall apart when using Multiview with GL_NONE.
2538
GLuint db = p_camera_data->view_count > 1 ? GL_COLOR_ATTACHMENT0 : GL_NONE;
2539
glDrawBuffers(1, &db);
2540
2541
uint64_t spec_constant = SceneShaderGLES3::DISABLE_FOG | SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL |
2542
SceneShaderGLES3::DISABLE_LIGHTMAP | SceneShaderGLES3::DISABLE_LIGHT_OMNI |
2543
SceneShaderGLES3::DISABLE_LIGHT_SPOT;
2544
2545
RenderListParameters render_list_params(render_list[RENDER_LIST_OPAQUE].elements.ptr(), render_list[RENDER_LIST_OPAQUE].elements.size(), reverse_cull, spec_constant, use_wireframe);
2546
_render_list_template<PASS_MODE_DEPTH>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_OPAQUE].elements.size());
2547
2548
glColorMask(1, 1, 1, 1);
2549
2550
fb_cleared = true;
2551
scene_state.used_depth_prepass = true;
2552
} else {
2553
scene_state.used_depth_prepass = false;
2554
}
2555
2556
glBlendEquation(GL_FUNC_ADD);
2557
if (render_data.transparent_bg) {
2558
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2559
scene_state.enable_gl_blend(true);
2560
} else {
2561
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
2562
scene_state.enable_gl_blend(false);
2563
}
2564
scene_state.current_blend_mode = GLES3::SceneShaderData::BLEND_MODE_MIX;
2565
2566
scene_state.enable_gl_scissor_test(false);
2567
scene_state.enable_gl_depth_test(true);
2568
scene_state.enable_gl_depth_draw(true);
2569
scene_state.set_gl_depth_func(GL_GEQUAL);
2570
2571
{
2572
GLuint db = GL_COLOR_ATTACHMENT0;
2573
glDrawBuffers(1, &db);
2574
}
2575
2576
scene_state.enable_gl_stencil_test(false);
2577
2578
if (!fb_cleared) {
2579
RasterizerGLES3::clear_depth(0.0);
2580
RasterizerGLES3::clear_stencil(0);
2581
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2582
}
2583
2584
// Need to clear framebuffer unless:
2585
// a) We explicitly request not to (i.e. ENV_BG_KEEP).
2586
// b) We are rendering to a non-intermediate framebuffer with ENV_BG_CANVAS (shared between 2D and 3D).
2587
if (!keep_color && (!draw_canvas || fbo != rt->fbo)) {
2588
clear_color.a = render_data.transparent_bg ? 0.0f : 1.0f;
2589
glClearBufferfv(GL_COLOR, 0, clear_color.components);
2590
}
2591
if ((keep_color || draw_canvas) && fbo != rt->fbo) {
2592
// Need to copy our current contents to our intermediate/MSAA buffer
2593
GLES3::CopyEffects *copy_effects = GLES3::CopyEffects::get_singleton();
2594
2595
scene_state.enable_gl_depth_test(false);
2596
scene_state.enable_gl_depth_draw(false);
2597
2598
glActiveTexture(GL_TEXTURE0);
2599
glBindTexture(rt->view_count > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D, rt->color);
2600
2601
copy_effects->copy_screen(render_data.luminance_multiplier);
2602
2603
scene_state.enable_gl_depth_test(true);
2604
scene_state.enable_gl_depth_draw(true);
2605
}
2606
2607
RENDER_TIMESTAMP("Render Opaque Pass");
2608
uint64_t spec_constant_base_flags = 0;
2609
2610
if (render_data.render_region != Rect2i()) {
2611
glViewport(render_data.render_region.position.x, render_data.render_region.position.y, render_data.render_region.size.width, render_data.render_region.size.height);
2612
}
2613
2614
{
2615
// Specialization Constants that apply for entire rendering pass.
2616
if (render_data.directional_light_count == 0) {
2617
spec_constant_base_flags |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
2618
}
2619
2620
if (render_data.environment.is_null() || (render_data.environment.is_valid() && !environment_get_fog_enabled(render_data.environment))) {
2621
spec_constant_base_flags |= SceneShaderGLES3::DISABLE_FOG;
2622
}
2623
2624
if (render_data.environment.is_valid() && environment_get_fog_mode(render_data.environment) == RS::EnvironmentFogMode::ENV_FOG_MODE_DEPTH) {
2625
spec_constant_base_flags |= SceneShaderGLES3::USE_DEPTH_FOG;
2626
}
2627
2628
if (!apply_color_adjustments_in_post) {
2629
spec_constant_base_flags |= SceneShaderGLES3::APPLY_TONEMAPPING;
2630
}
2631
}
2632
2633
if (draw_feed && camera_feed_id > -1) {
2634
RENDER_TIMESTAMP("Render Camera feed");
2635
2636
scene_state.enable_gl_depth_draw(false);
2637
scene_state.enable_gl_depth_test(false);
2638
scene_state.enable_gl_blend(false);
2639
scene_state.set_gl_cull_mode(RS::CULL_MODE_BACK);
2640
2641
Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
2642
2643
if (feed.is_valid()) {
2644
RID camera_YCBCR = feed->get_texture(CameraServer::FEED_YCBCR_IMAGE);
2645
GLES3::TextureStorage::get_singleton()->texture_bind(camera_YCBCR, 0);
2646
2647
GLES3::FeedEffects *feed_effects = GLES3::FeedEffects::get_singleton();
2648
feed_effects->draw();
2649
}
2650
scene_state.enable_gl_depth_draw(true);
2651
scene_state.enable_gl_depth_test(true);
2652
scene_state.enable_gl_blend(true);
2653
}
2654
2655
// Render Opaque Objects.
2656
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);
2657
2658
_render_list_template<PASS_MODE_COLOR>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_OPAQUE].elements.size());
2659
2660
scene_state.enable_gl_depth_draw(false);
2661
scene_state.enable_gl_stencil_test(false);
2662
2663
if (draw_sky || draw_sky_fog_only) {
2664
RENDER_TIMESTAMP("Render Sky");
2665
2666
scene_state.enable_gl_depth_test(true);
2667
scene_state.set_gl_depth_func(GL_GEQUAL);
2668
scene_state.enable_gl_blend(false);
2669
scene_state.set_gl_cull_mode(RS::CULL_MODE_BACK);
2670
2671
Transform3D transform = render_data.cam_transform;
2672
Projection projection = render_data.cam_projection;
2673
if (is_reflection_probe) {
2674
Projection correction;
2675
correction.columns[1][1] = -1.0;
2676
projection = correction * render_data.cam_projection;
2677
} else if (render_data.cam_frustum) {
2678
// Sky is drawn upside down, the frustum offset doesn't know the image is upside down so needs a flip.
2679
projection[2].y = -projection[2].y;
2680
}
2681
2682
_draw_sky(render_data.environment, projection, transform, sky_energy_multiplier, render_data.luminance_multiplier, p_camera_data->view_count > 1, flip_y, apply_color_adjustments_in_post);
2683
}
2684
2685
if (rt && (scene_state.used_screen_texture || scene_state.used_depth_texture)) {
2686
Size2i size;
2687
GLuint backbuffer_fbo = 0;
2688
GLuint backbuffer = 0;
2689
GLuint backbuffer_depth = 0;
2690
2691
if (rb->get_scaling_3d_mode() == RS::VIEWPORT_SCALING_3D_MODE_OFF) {
2692
texture_storage->check_backbuffer(rt, scene_state.used_screen_texture, scene_state.used_depth_texture); // note, badly names, this just allocates!
2693
2694
size = rt->size;
2695
backbuffer_fbo = rt->backbuffer_fbo;
2696
backbuffer = rt->backbuffer;
2697
backbuffer_depth = rt->backbuffer_depth;
2698
} else {
2699
rb->check_backbuffer(scene_state.used_screen_texture, scene_state.used_depth_texture);
2700
size = rb->get_internal_size();
2701
backbuffer_fbo = rb->get_backbuffer_fbo();
2702
backbuffer = rb->get_backbuffer();
2703
backbuffer_depth = rb->get_backbuffer_depth();
2704
}
2705
2706
if (backbuffer_fbo != 0) {
2707
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
2708
glReadBuffer(GL_COLOR_ATTACHMENT0);
2709
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, backbuffer_fbo);
2710
if (scene_state.used_screen_texture) {
2711
glBlitFramebuffer(0, 0, size.x, size.y,
2712
0, 0, size.x, size.y,
2713
GL_COLOR_BUFFER_BIT, GL_NEAREST);
2714
glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 6);
2715
glBindTexture(GL_TEXTURE_2D, backbuffer);
2716
}
2717
if (scene_state.used_depth_texture) {
2718
glBlitFramebuffer(0, 0, size.x, size.y,
2719
0, 0, size.x, size.y,
2720
GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
2721
glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 7);
2722
glBindTexture(GL_TEXTURE_2D, backbuffer_depth);
2723
}
2724
}
2725
2726
// Bound framebuffer may have changed, so change it back
2727
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2728
}
2729
2730
RENDER_TIMESTAMP("Render 3D Transparent Pass");
2731
scene_state.enable_gl_blend(true);
2732
2733
//Render transparent pass
2734
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);
2735
2736
_render_list_template<PASS_MODE_COLOR_TRANSPARENT>(&render_list_params_alpha, &render_data, 0, render_list[RENDER_LIST_ALPHA].elements.size(), true);
2737
2738
scene_state.enable_gl_stencil_test(false);
2739
2740
if (!flip_y) {
2741
// Restore the default winding order.
2742
glFrontFace(GL_CCW);
2743
}
2744
2745
if (!is_reflection_probe && rb.is_valid()) {
2746
_render_buffers_debug_draw(rb, p_shadow_atlas, fbo);
2747
}
2748
2749
// Reset stuff that may trip up the next process.
2750
scene_state.reset_gl_state();
2751
glUseProgram(0);
2752
2753
if (!is_reflection_probe) {
2754
_render_post_processing(&render_data);
2755
2756
texture_storage->render_target_disable_clear_request(rb->render_target);
2757
}
2758
2759
glActiveTexture(GL_TEXTURE0);
2760
}
2761
2762
void RasterizerSceneGLES3::_render_post_processing(const RenderDataGLES3 *p_render_data) {
2763
GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
2764
GLES3::Glow *glow = GLES3::Glow::get_singleton();
2765
GLES3::PostEffects *post_effects = GLES3::PostEffects::get_singleton();
2766
2767
Ref<RenderSceneBuffersGLES3> rb = p_render_data->render_buffers;
2768
ERR_FAIL_COND(rb.is_null());
2769
2770
RID render_target = rb->get_render_target();
2771
Size2i internal_size = rb->get_internal_size();
2772
Size2i target_size = rb->get_target_size();
2773
uint32_t view_count = rb->get_view_count();
2774
2775
// bool msaa2d_needs_resolve = texture_storage->render_target_get_msaa(render_target) != RS::VIEWPORT_MSAA_DISABLED && !GLES3::Config::get_singleton()->rt_msaa_supported;
2776
bool msaa3d_needs_resolve = rb->get_msaa_needs_resolve();
2777
GLuint fbo_msaa_3d = rb->get_msaa3d_fbo();
2778
GLuint fbo_int = rb->get_internal_fbo();
2779
GLuint fbo_rt = texture_storage->render_target_get_fbo(render_target); // TODO if MSAA 2D is enabled and we're not using rt_msaa, get 2D render target here.
2780
2781
// Check if we have glow enabled and if so, check if our buffers were allocated
2782
bool glow_enabled = false;
2783
float glow_intensity = 1.0;
2784
float glow_bloom = 0.0;
2785
float glow_hdr_bleed_threshold = 1.0;
2786
float glow_hdr_bleed_scale = 2.0;
2787
float glow_hdr_luminance_cap = 12.0;
2788
if (p_render_data->environment.is_valid()) {
2789
glow_enabled = environment_get_glow_enabled(p_render_data->environment);
2790
glow_intensity = environment_get_glow_intensity(p_render_data->environment);
2791
glow_bloom = environment_get_glow_bloom(p_render_data->environment);
2792
glow_hdr_bleed_threshold = environment_get_glow_hdr_bleed_threshold(p_render_data->environment);
2793
glow_hdr_bleed_scale = environment_get_glow_hdr_bleed_scale(p_render_data->environment);
2794
glow_hdr_luminance_cap = environment_get_glow_hdr_luminance_cap(p_render_data->environment);
2795
}
2796
2797
if (glow_enabled) {
2798
rb->check_glow_buffers();
2799
}
2800
2801
uint64_t bcs_spec_constants = 0;
2802
if (p_render_data->environment.is_valid()) {
2803
bool use_bcs = environment_get_adjustments_enabled(p_render_data->environment);
2804
RID color_correction_texture = environment_get_color_correction(p_render_data->environment);
2805
if (use_bcs) {
2806
bcs_spec_constants |= PostShaderGLES3::USE_BCS;
2807
2808
if (color_correction_texture.is_valid()) {
2809
bcs_spec_constants |= PostShaderGLES3::USE_COLOR_CORRECTION;
2810
2811
bool use_1d_lut = environment_get_use_1d_color_correction(p_render_data->environment);
2812
GLenum texture_target = GL_TEXTURE_3D;
2813
if (use_1d_lut) {
2814
bcs_spec_constants |= PostShaderGLES3::USE_1D_LUT;
2815
texture_target = GL_TEXTURE_2D;
2816
}
2817
2818
glActiveTexture(GL_TEXTURE2);
2819
glBindTexture(texture_target, texture_storage->texture_get_texid(color_correction_texture));
2820
glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2821
glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2822
glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2823
glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2824
glTexParameteri(texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2825
}
2826
}
2827
}
2828
2829
if (view_count == 1) {
2830
// Resolve if needed.
2831
if (fbo_msaa_3d != 0 && msaa3d_needs_resolve) {
2832
// We can use blit to copy things over
2833
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo_msaa_3d);
2834
2835
if (fbo_int != 0) {
2836
// We can't combine resolve and scaling, so resolve into our internal buffer
2837
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_int);
2838
} else {
2839
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_rt);
2840
}
2841
glBlitFramebuffer(0, 0, internal_size.x, internal_size.y, 0, 0, internal_size.x, internal_size.y, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
2842
}
2843
2844
// Rendered to intermediate buffer, must copy to our render target
2845
if (fbo_int != 0) {
2846
// Apply glow/bloom if requested? then populate our glow buffers
2847
GLuint color = fbo_int != 0 ? rb->get_internal_color() : texture_storage->render_target_get_color(render_target);
2848
const GLES3::Glow::GLOWLEVEL *glow_buffers = nullptr;
2849
if (glow_enabled) {
2850
glow_buffers = rb->get_glow_buffers();
2851
2852
glow->set_luminance_multiplier(p_render_data->luminance_multiplier);
2853
2854
glow->set_intensity(glow_intensity);
2855
glow->set_glow_bloom(glow_bloom);
2856
glow->set_glow_hdr_bleed_threshold(glow_hdr_bleed_threshold);
2857
glow->set_glow_hdr_bleed_scale(glow_hdr_bleed_scale);
2858
glow->set_glow_hdr_luminance_cap(glow_hdr_luminance_cap);
2859
2860
glow->process_glow(color, internal_size, glow_buffers);
2861
}
2862
2863
// Copy color buffer
2864
post_effects->post_copy(fbo_rt, target_size, color, internal_size, p_render_data->luminance_multiplier, glow_buffers, glow_intensity, 0, false, bcs_spec_constants);
2865
2866
// Copy depth buffer
2867
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo_int);
2868
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_rt);
2869
glBlitFramebuffer(0, 0, internal_size.x, internal_size.y, 0, 0, target_size.x, target_size.y, GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
2870
}
2871
2872
glBindFramebuffer(GL_FRAMEBUFFER, fbo_rt);
2873
} else if ((fbo_msaa_3d != 0 && msaa3d_needs_resolve) || (fbo_int != 0)) {
2874
// TODO investigate if it's smarter to cache these FBOs
2875
GLuint fbos[3]; // read, write and post
2876
glGenFramebuffers(3, fbos);
2877
2878
// Resolve if needed.
2879
if (fbo_msaa_3d != 0 && msaa3d_needs_resolve) {
2880
GLuint read_color = rb->get_msaa3d_color();
2881
GLuint read_depth = rb->get_msaa3d_depth();
2882
GLuint write_color = 0;
2883
GLuint write_depth = 0;
2884
2885
if (fbo_int != 0) {
2886
write_color = rb->get_internal_color();
2887
write_depth = rb->get_internal_depth();
2888
} else {
2889
write_color = texture_storage->render_target_get_color(render_target);
2890
write_depth = texture_storage->render_target_get_depth(render_target);
2891
}
2892
2893
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
2894
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
2895
2896
for (uint32_t v = 0; v < view_count; v++) {
2897
glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, read_color, 0, v);
2898
glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, read_depth, 0, v);
2899
glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, write_color, 0, v);
2900
glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, write_depth, 0, v);
2901
glBlitFramebuffer(0, 0, internal_size.x, internal_size.y, 0, 0, internal_size.x, internal_size.y, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
2902
}
2903
}
2904
2905
// Rendered to intermediate buffer, must copy to our render target
2906
if (fbo_int != 0) {
2907
// Apply glow/bloom if requested? then populate our glow buffers
2908
const GLES3::Glow::GLOWLEVEL *glow_buffers = nullptr;
2909
GLuint source_color = fbo_int != 0 ? rb->get_internal_color() : texture_storage->render_target_get_color(render_target);
2910
2911
if (glow_enabled) {
2912
glow_buffers = rb->get_glow_buffers();
2913
2914
glow->set_luminance_multiplier(p_render_data->luminance_multiplier);
2915
2916
glow->set_intensity(glow_intensity);
2917
glow->set_glow_bloom(glow_bloom);
2918
glow->set_glow_hdr_bleed_threshold(glow_hdr_bleed_threshold);
2919
glow->set_glow_hdr_bleed_scale(glow_hdr_bleed_scale);
2920
glow->set_glow_hdr_luminance_cap(glow_hdr_luminance_cap);
2921
}
2922
2923
GLuint write_color = texture_storage->render_target_get_color(render_target);
2924
2925
for (uint32_t v = 0; v < view_count; v++) {
2926
if (glow_enabled) {
2927
glow->process_glow(source_color, internal_size, glow_buffers, v, true);
2928
}
2929
2930
glBindFramebuffer(GL_FRAMEBUFFER, fbos[2]);
2931
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, write_color, 0, v);
2932
post_effects->post_copy(fbos[2], target_size, source_color, internal_size, p_render_data->luminance_multiplier, glow_buffers, glow_intensity, v, true, bcs_spec_constants);
2933
}
2934
2935
// Copy depth
2936
GLuint read_depth = rb->get_internal_depth();
2937
GLuint write_depth = texture_storage->render_target_get_depth(render_target);
2938
2939
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
2940
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
2941
2942
for (uint32_t v = 0; v < view_count; v++) {
2943
glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, read_depth, 0, v);
2944
glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, write_depth, 0, v);
2945
2946
glBlitFramebuffer(0, 0, internal_size.x, internal_size.y, 0, 0, target_size.x, target_size.y, GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
2947
}
2948
}
2949
2950
glBindFramebuffer(GL_FRAMEBUFFER, fbo_rt);
2951
glDeleteFramebuffers(3, fbos);
2952
}
2953
2954
glActiveTexture(GL_TEXTURE2);
2955
glBindTexture(GL_TEXTURE_2D, 0);
2956
}
2957
2958
template <PassMode p_pass_mode>
2959
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) {
2960
GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
2961
GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
2962
GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
2963
2964
GLuint prev_vertex_array_gl = 0;
2965
GLuint prev_index_array_gl = 0;
2966
2967
GLES3::SceneMaterialData *prev_material_data = nullptr;
2968
GLES3::SceneShaderData *prev_shader = nullptr;
2969
GeometryInstanceGLES3 *prev_inst = nullptr;
2970
SceneShaderGLES3::ShaderVariant prev_variant = SceneShaderGLES3::ShaderVariant::MODE_COLOR;
2971
SceneShaderGLES3::ShaderVariant shader_variant = SceneShaderGLES3::MODE_COLOR; // Assigned to silence wrong -Wmaybe-initialized
2972
uint64_t prev_spec_constants = 0;
2973
2974
// Specializations constants used by all instances in the scene.
2975
uint64_t base_spec_constants = p_params->spec_constant_base_flags;
2976
2977
if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
2978
GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
2979
GLES3::Config *config = GLES3::Config::get_singleton();
2980
glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 2);
2981
GLuint texture_to_bind = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_CUBEMAP_BLACK))->tex_id;
2982
if (p_render_data->environment.is_valid()) {
2983
Sky *sky = sky_owner.get_or_null(environment_get_sky(p_render_data->environment));
2984
if (sky && sky->radiance != 0) {
2985
texture_to_bind = sky->radiance;
2986
base_spec_constants |= SceneShaderGLES3::USE_RADIANCE_MAP;
2987
}
2988
glBindTexture(GL_TEXTURE_CUBE_MAP, texture_to_bind);
2989
}
2990
2991
} else if constexpr (p_pass_mode == PASS_MODE_DEPTH || p_pass_mode == PASS_MODE_SHADOW) {
2992
shader_variant = SceneShaderGLES3::MODE_DEPTH;
2993
}
2994
2995
if (p_render_data->view_count > 1) {
2996
base_spec_constants |= SceneShaderGLES3::USE_MULTIVIEW;
2997
}
2998
2999
bool should_request_redraw = false;
3000
if constexpr (p_pass_mode != PASS_MODE_DEPTH) {
3001
// Don't count elements during depth pre-pass to match the RD renderers.
3002
if (p_render_data->render_info) {
3003
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;
3004
}
3005
}
3006
3007
for (uint32_t i = p_from_element; i < p_to_element; i++) {
3008
GeometryInstanceSurface *surf = p_params->elements[i];
3009
GeometryInstanceGLES3 *inst = surf->owner;
3010
3011
if (p_pass_mode == PASS_MODE_COLOR && !(surf->flags & GeometryInstanceSurface::FLAG_PASS_OPAQUE)) {
3012
continue; // Objects with "Depth-prepass" transparency are included in both render lists, but should only be rendered in the transparent pass
3013
}
3014
3015
if (inst->instance_count == 0) {
3016
continue;
3017
}
3018
3019
GLES3::SceneShaderData *shader;
3020
GLES3::SceneMaterialData *material_data;
3021
void *mesh_surface;
3022
3023
if constexpr (p_pass_mode == PASS_MODE_SHADOW) {
3024
shader = surf->shader_shadow;
3025
material_data = surf->material_shadow;
3026
mesh_surface = surf->surface_shadow;
3027
} else {
3028
if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW)) {
3029
material_data = overdraw_material_data_ptr;
3030
shader = material_data->shader_data;
3031
} else if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_LIGHTING)) {
3032
material_data = default_material_data_ptr;
3033
shader = material_data->shader_data;
3034
} else {
3035
shader = surf->shader;
3036
material_data = surf->material;
3037
}
3038
mesh_surface = surf->surface;
3039
}
3040
3041
if (!mesh_surface) {
3042
continue;
3043
}
3044
3045
//request a redraw if one of the shaders uses TIME
3046
if (shader->uses_time) {
3047
should_request_redraw = true;
3048
}
3049
3050
if constexpr (p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
3051
scene_state.enable_gl_depth_test(shader->depth_test != GLES3::SceneShaderData::DEPTH_TEST_DISABLED);
3052
}
3053
3054
if (shader->depth_test == GLES3::SceneShaderData::DEPTH_TEST_ENABLED_INVERTED) {
3055
scene_state.set_gl_depth_func(GL_LESS);
3056
} else {
3057
scene_state.set_gl_depth_func(GL_GEQUAL);
3058
}
3059
3060
if constexpr (p_pass_mode != PASS_MODE_SHADOW) {
3061
if (shader->depth_draw == GLES3::SceneShaderData::DEPTH_DRAW_OPAQUE) {
3062
scene_state.enable_gl_depth_draw((p_pass_mode == PASS_MODE_COLOR && !GLES3::Config::get_singleton()->use_depth_prepass) || p_pass_mode == PASS_MODE_DEPTH);
3063
} else {
3064
scene_state.enable_gl_depth_draw(shader->depth_draw == GLES3::SceneShaderData::DEPTH_DRAW_ALWAYS);
3065
}
3066
}
3067
3068
bool uses_additive_lighting = (inst->light_passes.size() + p_render_data->directional_shadow_count) > 0;
3069
uses_additive_lighting = uses_additive_lighting && !shader->unshaded;
3070
3071
// TODOS
3072
/*
3073
* Still a bug when atlas space is limited. Somehow need to evict light when it doesn't have a spot on the atlas, current check isn't enough
3074
* Disable depth draw
3075
*/
3076
3077
for (int32_t pass = 0; pass < MAX(1, int32_t(inst->light_passes.size() + p_render_data->directional_shadow_count)); pass++) {
3078
if constexpr (p_pass_mode == PASS_MODE_DEPTH || p_pass_mode == PASS_MODE_SHADOW) {
3079
if (pass > 0) {
3080
// Don't render shadow passes when doing depth or shadow pass.
3081
break;
3082
}
3083
}
3084
3085
// Stencil.
3086
if (p_pass_mode != PASS_MODE_DEPTH && shader->stencil_enabled) {
3087
static const GLenum stencil_compare_table[GLES3::SceneShaderData::STENCIL_COMPARE_MAX] = {
3088
GL_LESS,
3089
GL_EQUAL,
3090
GL_LEQUAL,
3091
GL_GREATER,
3092
GL_NOTEQUAL,
3093
GL_GEQUAL,
3094
GL_ALWAYS,
3095
};
3096
3097
GLenum stencil_compare = stencil_compare_table[shader->stencil_compare];
3098
GLuint stencil_compare_mask = 0;
3099
GLuint stencil_write_mask = 0;
3100
GLenum stencil_op_dpfail = GL_KEEP;
3101
GLenum stencil_op_dppass = GL_KEEP;
3102
3103
if (shader->stencil_flags & GLES3::SceneShaderData::STENCIL_FLAG_READ) {
3104
stencil_compare_mask = 255;
3105
}
3106
3107
if (shader->stencil_flags & GLES3::SceneShaderData::STENCIL_FLAG_WRITE) {
3108
stencil_op_dppass = GL_REPLACE;
3109
stencil_write_mask = 255;
3110
}
3111
3112
if (shader->stencil_flags & GLES3::SceneShaderData::STENCIL_FLAG_WRITE_DEPTH_FAIL) {
3113
stencil_op_dpfail = GL_REPLACE;
3114
stencil_write_mask = 255;
3115
}
3116
3117
scene_state.enable_gl_stencil_test(true);
3118
scene_state.set_gl_stencil_func(stencil_compare, shader->stencil_reference, stencil_compare_mask);
3119
scene_state.set_gl_stencil_write_mask(stencil_write_mask);
3120
scene_state.set_gl_stencil_op(GL_KEEP, stencil_op_dpfail, stencil_op_dppass);
3121
} else {
3122
scene_state.enable_gl_stencil_test(false);
3123
}
3124
3125
if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
3126
if (!uses_additive_lighting && pass == 1) {
3127
// Don't render additive passes if not using additive lighting.
3128
break;
3129
}
3130
if (uses_additive_lighting && pass == 1 && !p_render_data->transparent_bg) {
3131
// Enable blending if in opaque pass and not already enabled.
3132
scene_state.enable_gl_blend(true);
3133
}
3134
if (pass < int32_t(inst->light_passes.size())) {
3135
RID light_instance_rid = inst->light_passes[pass].light_instance_rid;
3136
if (!GLES3::LightStorage::get_singleton()->light_instance_has_shadow_atlas(light_instance_rid, p_render_data->shadow_atlas)) {
3137
// Shadow wasn't able to get a spot on the atlas. So skip it.
3138
continue;
3139
}
3140
} else if (pass > 0) {
3141
uint32_t shadow_id = MAX_DIRECTIONAL_LIGHTS - 1 - (pass - int32_t(inst->light_passes.size()));
3142
if (inst->lightmap_instance.is_valid() && scene_state.directional_lights[shadow_id].bake_mode == RenderingServer::LIGHT_BAKE_STATIC) {
3143
// Skip shadows for static lights on meshes with a lightmap.
3144
continue;
3145
}
3146
}
3147
}
3148
3149
if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
3150
GLES3::SceneShaderData::BlendMode desired_blend_mode;
3151
if (pass > 0) {
3152
desired_blend_mode = GLES3::SceneShaderData::BLEND_MODE_ADD;
3153
} else {
3154
desired_blend_mode = shader->blend_mode;
3155
}
3156
3157
if (desired_blend_mode != scene_state.current_blend_mode) {
3158
switch (desired_blend_mode) {
3159
case GLES3::SceneShaderData::BLEND_MODE_MIX: {
3160
glBlendEquation(GL_FUNC_ADD);
3161
if (p_render_data->transparent_bg) {
3162
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
3163
} else {
3164
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
3165
}
3166
3167
} break;
3168
case GLES3::SceneShaderData::BLEND_MODE_ADD: {
3169
glBlendEquation(GL_FUNC_ADD);
3170
glBlendFunc(p_pass_mode == PASS_MODE_COLOR_TRANSPARENT ? GL_SRC_ALPHA : GL_ONE, GL_ONE);
3171
3172
} break;
3173
case GLES3::SceneShaderData::BLEND_MODE_SUB: {
3174
glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
3175
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
3176
3177
} break;
3178
case GLES3::SceneShaderData::BLEND_MODE_MUL: {
3179
glBlendEquation(GL_FUNC_ADD);
3180
if (p_render_data->transparent_bg) {
3181
glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
3182
} else {
3183
glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
3184
}
3185
3186
} break;
3187
case GLES3::SceneShaderData::BLEND_MODE_PREMULT_ALPHA: {
3188
glBlendEquation(GL_FUNC_ADD);
3189
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
3190
3191
} break;
3192
case GLES3::SceneShaderData::BLEND_MODE_ALPHA_TO_COVERAGE: {
3193
// Do nothing for now.
3194
} break;
3195
}
3196
scene_state.current_blend_mode = desired_blend_mode;
3197
}
3198
}
3199
3200
// Find cull variant.
3201
RS::CullMode cull_mode = shader->cull_mode;
3202
3203
if (p_pass_mode == PASS_MODE_MATERIAL || (surf->flags & GeometryInstanceSurface::FLAG_USES_DOUBLE_SIDED_SHADOWS)) {
3204
cull_mode = RS::CULL_MODE_DISABLED;
3205
} else {
3206
bool mirror = inst->mirror;
3207
if (p_params->reverse_cull) {
3208
mirror = !mirror;
3209
}
3210
if (cull_mode == RS::CULL_MODE_FRONT && mirror) {
3211
cull_mode = RS::CULL_MODE_BACK;
3212
} else if (cull_mode == RS::CULL_MODE_BACK && mirror) {
3213
cull_mode = RS::CULL_MODE_FRONT;
3214
}
3215
}
3216
3217
scene_state.set_gl_cull_mode(cull_mode);
3218
3219
RS::PrimitiveType primitive = surf->primitive;
3220
if (shader->uses_point_size) {
3221
primitive = RS::PRIMITIVE_POINTS;
3222
}
3223
static const GLenum prim[5] = { GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP };
3224
GLenum primitive_gl = prim[int(primitive)];
3225
3226
GLuint vertex_array_gl = 0;
3227
GLuint index_array_gl = 0;
3228
uint64_t vertex_input_mask = shader->vertex_input_mask;
3229
if (inst->lightmap_instance.is_valid() || p_pass_mode == PASS_MODE_MATERIAL) {
3230
vertex_input_mask |= 1 << RS::ARRAY_TEX_UV2;
3231
}
3232
3233
// Skeleton and blend shapes.
3234
if (surf->owner->mesh_instance.is_valid()) {
3235
mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(surf->owner->mesh_instance, surf->surface_index, vertex_input_mask, vertex_array_gl);
3236
} else {
3237
mesh_storage->mesh_surface_get_vertex_arrays_and_format(mesh_surface, vertex_input_mask, vertex_array_gl);
3238
}
3239
3240
index_array_gl = mesh_storage->mesh_surface_get_index_buffer(mesh_surface, surf->lod_index);
3241
3242
if (prev_vertex_array_gl != vertex_array_gl) {
3243
if (vertex_array_gl != 0) {
3244
glBindVertexArray(vertex_array_gl);
3245
}
3246
prev_vertex_array_gl = vertex_array_gl;
3247
3248
// Invalidate the previous index array
3249
prev_index_array_gl = 0;
3250
}
3251
3252
bool use_wireframe = false;
3253
if (p_params->force_wireframe || shader->wireframe) {
3254
GLuint wireframe_index_array_gl = mesh_storage->mesh_surface_get_index_buffer_wireframe(mesh_surface);
3255
if (wireframe_index_array_gl) {
3256
index_array_gl = wireframe_index_array_gl;
3257
use_wireframe = true;
3258
}
3259
}
3260
3261
bool use_index_buffer = index_array_gl != 0;
3262
if (prev_index_array_gl != index_array_gl) {
3263
if (index_array_gl != 0) {
3264
// Bind index each time so we can use LODs
3265
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_array_gl);
3266
}
3267
prev_index_array_gl = index_array_gl;
3268
}
3269
3270
Transform3D world_transform;
3271
if (inst->store_transform_cache) {
3272
world_transform = inst->transform;
3273
}
3274
3275
if (prev_material_data != material_data) {
3276
material_data->bind_uniforms();
3277
prev_material_data = material_data;
3278
}
3279
3280
SceneShaderGLES3::ShaderVariant instance_variant = shader_variant;
3281
3282
if (inst->instance_count > 0) {
3283
// Will need to use instancing to draw (either MultiMesh or Particles).
3284
instance_variant = SceneShaderGLES3::ShaderVariant(1 + int(instance_variant));
3285
}
3286
3287
uint64_t spec_constants = base_spec_constants;
3288
3289
// Set up spec constants for lighting.
3290
if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
3291
// Only check during color passes as light shader code is compiled out during depth-only pass anyway.
3292
3293
if (pass == 0) {
3294
spec_constants |= SceneShaderGLES3::BASE_PASS;
3295
3296
if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
3297
spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_OMNI;
3298
spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_SPOT;
3299
spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
3300
spec_constants |= SceneShaderGLES3::DISABLE_LIGHTMAP;
3301
} else {
3302
if (inst->omni_light_gl_cache.is_empty()) {
3303
spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_OMNI;
3304
}
3305
3306
if (inst->spot_light_gl_cache.is_empty()) {
3307
spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_SPOT;
3308
}
3309
3310
if (p_render_data->directional_light_count == p_render_data->directional_shadow_count) {
3311
spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
3312
}
3313
3314
if (inst->reflection_probe_rid_cache.is_empty()) {
3315
// We don't have any probes.
3316
spec_constants |= SceneShaderGLES3::DISABLE_REFLECTION_PROBE;
3317
} else if (inst->reflection_probe_rid_cache.size() > 1) {
3318
// We have a second probe.
3319
spec_constants |= SceneShaderGLES3::SECOND_REFLECTION_PROBE;
3320
}
3321
3322
if (inst->lightmap_instance.is_valid()) {
3323
spec_constants |= SceneShaderGLES3::USE_LIGHTMAP;
3324
3325
GLES3::LightmapInstance *li = GLES3::LightStorage::get_singleton()->get_lightmap_instance(inst->lightmap_instance);
3326
GLES3::Lightmap *lm = GLES3::LightStorage::get_singleton()->get_lightmap(li->lightmap);
3327
3328
if (lm->uses_spherical_harmonics) {
3329
spec_constants |= SceneShaderGLES3::USE_SH_LIGHTMAP;
3330
}
3331
3332
if (lightmap_bicubic_upscale) {
3333
spec_constants |= SceneShaderGLES3::LIGHTMAP_BICUBIC_FILTER;
3334
}
3335
} else if (inst->lightmap_sh) {
3336
spec_constants |= SceneShaderGLES3::USE_LIGHTMAP_CAPTURE;
3337
} else {
3338
spec_constants |= SceneShaderGLES3::DISABLE_LIGHTMAP;
3339
}
3340
}
3341
} else {
3342
// Only base pass uses the radiance map.
3343
spec_constants &= ~SceneShaderGLES3::USE_RADIANCE_MAP;
3344
spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_OMNI;
3345
spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_SPOT;
3346
spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
3347
spec_constants |= SceneShaderGLES3::DISABLE_REFLECTION_PROBE;
3348
3349
bool disable_lightmaps = true;
3350
3351
// Additive directional passes may use shadowmasks, so enable lightmaps for them.
3352
if (pass >= int32_t(inst->light_passes.size()) && inst->lightmap_instance.is_valid()) {
3353
GLES3::LightmapInstance *li = GLES3::LightStorage::get_singleton()->get_lightmap_instance(inst->lightmap_instance);
3354
GLES3::Lightmap *lm = GLES3::LightStorage::get_singleton()->get_lightmap(li->lightmap);
3355
3356
if (lm->shadowmask_mode != RS::SHADOWMASK_MODE_NONE) {
3357
spec_constants |= SceneShaderGLES3::USE_LIGHTMAP;
3358
disable_lightmaps = false;
3359
3360
if (lightmap_bicubic_upscale) {
3361
spec_constants |= SceneShaderGLES3::LIGHTMAP_BICUBIC_FILTER;
3362
}
3363
}
3364
}
3365
3366
if (disable_lightmaps) {
3367
spec_constants |= SceneShaderGLES3::DISABLE_LIGHTMAP;
3368
}
3369
}
3370
3371
if (uses_additive_lighting) {
3372
spec_constants |= SceneShaderGLES3::USE_ADDITIVE_LIGHTING;
3373
3374
if (pass < int32_t(inst->light_passes.size())) {
3375
// Rendering positional lights.
3376
if (inst->light_passes[pass].is_omni) {
3377
spec_constants |= SceneShaderGLES3::ADDITIVE_OMNI;
3378
} else {
3379
spec_constants |= SceneShaderGLES3::ADDITIVE_SPOT;
3380
}
3381
3382
if (scene_state.positional_shadow_quality >= RS::SHADOW_QUALITY_SOFT_HIGH) {
3383
spec_constants |= SceneShaderGLES3::SHADOW_MODE_PCF_13;
3384
} else if (scene_state.positional_shadow_quality >= RS::SHADOW_QUALITY_SOFT_LOW) {
3385
spec_constants |= SceneShaderGLES3::SHADOW_MODE_PCF_5;
3386
}
3387
} else {
3388
// Render directional lights.
3389
3390
uint32_t shadow_id = MAX_DIRECTIONAL_LIGHTS - 1 - (pass - int32_t(inst->light_passes.size()));
3391
if (pass == 0 && inst->lightmap_instance.is_valid() && scene_state.directional_lights[shadow_id].bake_mode == RenderingServer::LIGHT_BAKE_STATIC) {
3392
// Disable additive lighting with a static light and a lightmap.
3393
spec_constants &= ~SceneShaderGLES3::USE_ADDITIVE_LIGHTING;
3394
}
3395
if (scene_state.directional_shadows[shadow_id].shadow_split_offsets[0] == scene_state.directional_shadows[shadow_id].shadow_split_offsets[1]) {
3396
// Orthogonal, do nothing.
3397
} else if (scene_state.directional_shadows[shadow_id].shadow_split_offsets[1] == scene_state.directional_shadows[shadow_id].shadow_split_offsets[2]) {
3398
spec_constants |= SceneShaderGLES3::LIGHT_USE_PSSM2;
3399
} else {
3400
spec_constants |= SceneShaderGLES3::LIGHT_USE_PSSM4;
3401
}
3402
3403
if (scene_state.directional_shadows[shadow_id].blend_splits) {
3404
spec_constants |= SceneShaderGLES3::LIGHT_USE_PSSM_BLEND;
3405
}
3406
3407
if (scene_state.directional_shadow_quality >= RS::SHADOW_QUALITY_SOFT_HIGH) {
3408
spec_constants |= SceneShaderGLES3::SHADOW_MODE_PCF_13;
3409
} else if (scene_state.directional_shadow_quality >= RS::SHADOW_QUALITY_SOFT_LOW) {
3410
spec_constants |= SceneShaderGLES3::SHADOW_MODE_PCF_5;
3411
}
3412
}
3413
}
3414
}
3415
3416
if (prev_shader != shader || prev_variant != instance_variant || spec_constants != prev_spec_constants) {
3417
bool success = material_storage->shaders.scene_shader.version_bind_shader(shader->version, instance_variant, spec_constants);
3418
if (!success) {
3419
break;
3420
}
3421
3422
float opaque_prepass_threshold = 0.0;
3423
if constexpr (p_pass_mode == PASS_MODE_DEPTH) {
3424
opaque_prepass_threshold = 0.99;
3425
} else if constexpr (p_pass_mode == PASS_MODE_SHADOW) {
3426
opaque_prepass_threshold = 0.1;
3427
}
3428
3429
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::OPAQUE_PREPASS_THRESHOLD, opaque_prepass_threshold, shader->version, instance_variant, spec_constants);
3430
}
3431
3432
// Pass in lighting uniforms.
3433
if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
3434
GLES3::Config *config = GLES3::Config::get_singleton();
3435
// Pass light and shadow index and bind shadow texture.
3436
if (uses_additive_lighting) {
3437
if (pass < int32_t(inst->light_passes.size())) {
3438
int32_t shadow_id = inst->light_passes[pass].shadow_id;
3439
if (shadow_id >= 0) {
3440
uint32_t light_id = inst->light_passes[pass].light_id;
3441
bool is_omni = inst->light_passes[pass].is_omni;
3442
SceneShaderGLES3::Uniforms uniform_name = is_omni ? SceneShaderGLES3::OMNI_LIGHT_INDEX : SceneShaderGLES3::SPOT_LIGHT_INDEX;
3443
material_storage->shaders.scene_shader.version_set_uniform(uniform_name, uint32_t(light_id), shader->version, instance_variant, spec_constants);
3444
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::POSITIONAL_SHADOW_INDEX, uint32_t(shadow_id), shader->version, instance_variant, spec_constants);
3445
3446
glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 3);
3447
RID light_instance_rid = inst->light_passes[pass].light_instance_rid;
3448
3449
GLuint tex = GLES3::LightStorage::get_singleton()->light_instance_get_shadow_texture(light_instance_rid, p_render_data->shadow_atlas);
3450
if (is_omni) {
3451
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3452
} else {
3453
glBindTexture(GL_TEXTURE_2D, tex);
3454
}
3455
}
3456
} else {
3457
uint32_t shadow_id = MAX_DIRECTIONAL_LIGHTS - 1 - (pass - int32_t(inst->light_passes.size()));
3458
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::DIRECTIONAL_SHADOW_INDEX, shadow_id, shader->version, instance_variant, spec_constants);
3459
3460
GLuint tex = GLES3::LightStorage::get_singleton()->directional_shadow_get_texture();
3461
glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 3);
3462
glBindTexture(GL_TEXTURE_2D, tex);
3463
3464
if (inst->lightmap_instance.is_valid()) {
3465
// Use shadowmasks for directional light passes.
3466
GLES3::LightmapInstance *li = GLES3::LightStorage::get_singleton()->get_lightmap_instance(inst->lightmap_instance);
3467
GLES3::Lightmap *lm = GLES3::LightStorage::get_singleton()->get_lightmap(li->lightmap);
3468
3469
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_SLICE, inst->lightmap_slice_index, shader->version, instance_variant, spec_constants);
3470
3471
Vector4 uv_scale(inst->lightmap_uv_scale.position.x, inst->lightmap_uv_scale.position.y, inst->lightmap_uv_scale.size.x, inst->lightmap_uv_scale.size.y);
3472
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_UV_SCALE, uv_scale, shader->version, instance_variant, spec_constants);
3473
3474
if (lightmap_bicubic_upscale) {
3475
Vector2 light_texture_size(lm->light_texture_size.x, lm->light_texture_size.y);
3476
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_TEXTURE_SIZE, light_texture_size, shader->version, instance_variant, spec_constants);
3477
}
3478
3479
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_SHADOWMASK_MODE, (uint32_t)lm->shadowmask_mode, shader->version, instance_variant, spec_constants);
3480
3481
if (lm->shadow_texture.is_valid()) {
3482
tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(lm->shadow_texture);
3483
} else {
3484
tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(GLES3::TextureStorage::get_singleton()->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE));
3485
}
3486
3487
glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 5);
3488
glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
3489
}
3490
}
3491
}
3492
3493
// Pass light count and array of light indices for base pass.
3494
if ((prev_inst != inst || prev_shader != shader || prev_variant != instance_variant || prev_spec_constants != spec_constants) && pass == 0) {
3495
// Rebind the light indices.
3496
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::OMNI_LIGHT_COUNT, inst->omni_light_gl_cache.size(), shader->version, instance_variant, spec_constants);
3497
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::SPOT_LIGHT_COUNT, inst->spot_light_gl_cache.size(), shader->version, instance_variant, spec_constants);
3498
3499
if (inst->omni_light_gl_cache.size()) {
3500
glUniform1uiv(material_storage->shaders.scene_shader.version_get_uniform(SceneShaderGLES3::OMNI_LIGHT_INDICES, shader->version, instance_variant, spec_constants), inst->omni_light_gl_cache.size(), inst->omni_light_gl_cache.ptr());
3501
}
3502
3503
if (inst->spot_light_gl_cache.size()) {
3504
glUniform1uiv(material_storage->shaders.scene_shader.version_get_uniform(SceneShaderGLES3::SPOT_LIGHT_INDICES, shader->version, instance_variant, spec_constants), inst->spot_light_gl_cache.size(), inst->spot_light_gl_cache.ptr());
3505
}
3506
3507
if (inst->lightmap_instance.is_valid()) {
3508
GLES3::LightmapInstance *li = GLES3::LightStorage::get_singleton()->get_lightmap_instance(inst->lightmap_instance);
3509
GLES3::Lightmap *lm = GLES3::LightStorage::get_singleton()->get_lightmap(li->lightmap);
3510
3511
GLuint tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(lm->light_texture);
3512
glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 4);
3513
glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
3514
3515
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_SLICE, inst->lightmap_slice_index, shader->version, instance_variant, spec_constants);
3516
3517
Vector4 uv_scale(inst->lightmap_uv_scale.position.x, inst->lightmap_uv_scale.position.y, inst->lightmap_uv_scale.size.x, inst->lightmap_uv_scale.size.y);
3518
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_UV_SCALE, uv_scale, shader->version, instance_variant, spec_constants);
3519
3520
if (lightmap_bicubic_upscale) {
3521
Vector2 light_texture_size(lm->light_texture_size.x, lm->light_texture_size.y);
3522
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_TEXTURE_SIZE, light_texture_size, shader->version, instance_variant, spec_constants);
3523
}
3524
3525
float exposure_normalization = 1.0;
3526
if (p_render_data->camera_attributes.is_valid()) {
3527
float enf = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
3528
exposure_normalization = enf / lm->baked_exposure;
3529
}
3530
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_EXPOSURE_NORMALIZATION, exposure_normalization, shader->version, instance_variant, spec_constants);
3531
3532
if (lm->uses_spherical_harmonics) {
3533
Basis to_lm = li->transform.basis.inverse() * p_render_data->cam_transform.basis;
3534
to_lm = to_lm.inverse().transposed();
3535
GLfloat matrix[9] = {
3536
(GLfloat)to_lm.rows[0][0],
3537
(GLfloat)to_lm.rows[1][0],
3538
(GLfloat)to_lm.rows[2][0],
3539
(GLfloat)to_lm.rows[0][1],
3540
(GLfloat)to_lm.rows[1][1],
3541
(GLfloat)to_lm.rows[2][1],
3542
(GLfloat)to_lm.rows[0][2],
3543
(GLfloat)to_lm.rows[1][2],
3544
(GLfloat)to_lm.rows[2][2],
3545
};
3546
glUniformMatrix3fv(material_storage->shaders.scene_shader.version_get_uniform(SceneShaderGLES3::LIGHTMAP_NORMAL_XFORM, shader->version, instance_variant, spec_constants), 1, GL_FALSE, matrix);
3547
}
3548
3549
} else if (inst->lightmap_sh) {
3550
glUniform4fv(material_storage->shaders.scene_shader.version_get_uniform(SceneShaderGLES3::LIGHTMAP_CAPTURES, shader->version, instance_variant, spec_constants), 9, reinterpret_cast<const GLfloat *>(inst->lightmap_sh->sh));
3551
}
3552
prev_inst = inst;
3553
}
3554
}
3555
3556
prev_shader = shader;
3557
prev_variant = instance_variant;
3558
prev_spec_constants = spec_constants;
3559
3560
// Pass in reflection probe data
3561
if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
3562
if (pass == 0 && inst->reflection_probe_rid_cache.size() > 0) {
3563
GLES3::Config *config = GLES3::Config::get_singleton();
3564
GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
3565
3566
// Setup first probe.
3567
{
3568
RID probe_rid = light_storage->reflection_probe_instance_get_probe(inst->reflection_probe_rid_cache[0]);
3569
GLES3::ReflectionProbe *probe = light_storage->get_reflection_probe(probe_rid);
3570
3571
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_USE_BOX_PROJECT, probe->box_projection, shader->version, instance_variant, spec_constants);
3572
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_BOX_EXTENTS, probe->size * 0.5, shader->version, instance_variant, spec_constants);
3573
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_BOX_OFFSET, probe->origin_offset, shader->version, instance_variant, spec_constants);
3574
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_EXTERIOR, !probe->interior, shader->version, instance_variant, spec_constants);
3575
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_INTENSITY, probe->intensity, shader->version, instance_variant, spec_constants);
3576
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_AMBIENT_MODE, int(probe->ambient_mode), shader->version, instance_variant, spec_constants);
3577
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_AMBIENT_COLOR, probe->ambient_color * probe->ambient_color_energy, shader->version, instance_variant, spec_constants);
3578
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_LOCAL_MATRIX, inst->reflection_probes_local_transform_cache[0], shader->version, instance_variant, spec_constants);
3579
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_BLEND_DISTANCE, probe->blend_distance, shader->version, instance_variant, spec_constants);
3580
3581
glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 8);
3582
glBindTexture(GL_TEXTURE_CUBE_MAP, light_storage->reflection_probe_instance_get_texture(inst->reflection_probe_rid_cache[0]));
3583
}
3584
3585
if (inst->reflection_probe_rid_cache.size() > 1) {
3586
// Setup second probe.
3587
RID probe_rid = light_storage->reflection_probe_instance_get_probe(inst->reflection_probe_rid_cache[1]);
3588
GLES3::ReflectionProbe *probe = light_storage->get_reflection_probe(probe_rid);
3589
3590
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_USE_BOX_PROJECT, probe->box_projection, shader->version, instance_variant, spec_constants);
3591
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_BOX_EXTENTS, probe->size * 0.5, shader->version, instance_variant, spec_constants);
3592
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_BOX_OFFSET, probe->origin_offset, shader->version, instance_variant, spec_constants);
3593
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_EXTERIOR, !probe->interior, shader->version, instance_variant, spec_constants);
3594
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_INTENSITY, probe->intensity, shader->version, instance_variant, spec_constants);
3595
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_AMBIENT_MODE, int(probe->ambient_mode), shader->version, instance_variant, spec_constants);
3596
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_AMBIENT_COLOR, probe->ambient_color * probe->ambient_color_energy, shader->version, instance_variant, spec_constants);
3597
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_LOCAL_MATRIX, inst->reflection_probes_local_transform_cache[1], shader->version, instance_variant, spec_constants);
3598
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_BLEND_DISTANCE, probe->blend_distance, shader->version, instance_variant, spec_constants);
3599
3600
glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 9);
3601
glBindTexture(GL_TEXTURE_CUBE_MAP, light_storage->reflection_probe_instance_get_texture(inst->reflection_probe_rid_cache[1]));
3602
3603
spec_constants |= SceneShaderGLES3::SECOND_REFLECTION_PROBE;
3604
}
3605
}
3606
}
3607
3608
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::WORLD_TRANSFORM, world_transform, shader->version, instance_variant, spec_constants);
3609
{
3610
GLES3::Mesh::Surface *s = reinterpret_cast<GLES3::Mesh::Surface *>(surf->surface);
3611
if (s->format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) {
3612
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::COMPRESSED_AABB_POSITION, s->aabb.position, shader->version, instance_variant, spec_constants);
3613
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::COMPRESSED_AABB_SIZE, s->aabb.size, shader->version, instance_variant, spec_constants);
3614
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::UV_SCALE, s->uv_scale, shader->version, instance_variant, spec_constants);
3615
} else {
3616
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::COMPRESSED_AABB_POSITION, Vector3(0.0, 0.0, 0.0), shader->version, instance_variant, spec_constants);
3617
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::COMPRESSED_AABB_SIZE, Vector3(1.0, 1.0, 1.0), shader->version, instance_variant, spec_constants);
3618
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::UV_SCALE, Vector4(0.0, 0.0, 0.0, 0.0), shader->version, instance_variant, spec_constants);
3619
}
3620
}
3621
3622
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::MODEL_FLAGS, inst->flags_cache, shader->version, instance_variant, spec_constants);
3623
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::INSTANCE_OFFSET, uint32_t(inst->shader_uniforms_offset), shader->version, instance_variant, spec_constants);
3624
3625
if (p_pass_mode == PASS_MODE_MATERIAL) {
3626
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::UV_OFFSET, p_params->uv_offset, shader->version, instance_variant, spec_constants);
3627
}
3628
3629
// Can be index count or vertex count
3630
uint32_t count = 0;
3631
if (surf->lod_index > 0) {
3632
count = surf->index_count;
3633
} else {
3634
count = mesh_storage->mesh_surface_get_vertices_drawn_count(mesh_surface);
3635
}
3636
3637
if (use_wireframe) {
3638
// In this case we are using index count, and we need double the indices for the wireframe mesh.
3639
count = count * 2;
3640
}
3641
3642
if constexpr (p_pass_mode != PASS_MODE_DEPTH) {
3643
// Don't count draw calls during depth pre-pass to match the RD renderers.
3644
if (p_render_data->render_info) {
3645
p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME]++;
3646
}
3647
}
3648
3649
if (inst->instance_count > 0) {
3650
// Using MultiMesh or Particles.
3651
// Bind instance buffers.
3652
3653
GLuint instance_buffer = 0;
3654
uint32_t stride = 0;
3655
if (inst->flags_cache & INSTANCE_DATA_FLAG_PARTICLES) {
3656
instance_buffer = particles_storage->particles_get_gl_buffer(inst->data->base);
3657
stride = 16; // 12 bytes for instance transform and 4 bytes for packed color and custom.
3658
} else {
3659
instance_buffer = mesh_storage->multimesh_get_gl_buffer(inst->data->base);
3660
stride = mesh_storage->multimesh_get_stride(inst->data->base);
3661
}
3662
3663
if (instance_buffer == 0) {
3664
// Instance buffer not initialized yet. Skip rendering for now.
3665
break;
3666
}
3667
3668
glBindBuffer(GL_ARRAY_BUFFER, instance_buffer);
3669
3670
glEnableVertexAttribArray(12);
3671
glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(0));
3672
glVertexAttribDivisor(12, 1);
3673
glEnableVertexAttribArray(13);
3674
glVertexAttribPointer(13, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4));
3675
glVertexAttribDivisor(13, 1);
3676
if (!(inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D)) {
3677
glEnableVertexAttribArray(14);
3678
glVertexAttribPointer(14, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(sizeof(float) * 8));
3679
glVertexAttribDivisor(14, 1);
3680
}
3681
3682
if ((inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR) || (inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA)) {
3683
uint32_t color_custom_offset = inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D ? 8 : 12;
3684
glEnableVertexAttribArray(15);
3685
glVertexAttribIPointer(15, 4, GL_UNSIGNED_INT, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(color_custom_offset * sizeof(float)));
3686
glVertexAttribDivisor(15, 1);
3687
} else {
3688
// Set all default instance color and custom data values to 1.0 or 0.0 using a compressed format.
3689
uint16_t zero = Math::make_half_float(0.0f);
3690
uint16_t one = Math::make_half_float(1.0f);
3691
GLuint default_color = (uint32_t(one) << 16) | one;
3692
GLuint default_custom = (uint32_t(zero) << 16) | zero;
3693
glVertexAttribI4ui(15, default_color, default_color, default_custom, default_custom);
3694
}
3695
3696
if (use_wireframe) {
3697
glDrawElementsInstanced(GL_LINES, count, GL_UNSIGNED_INT, nullptr, inst->instance_count);
3698
} else {
3699
if (use_index_buffer) {
3700
glDrawElementsInstanced(primitive_gl, count, mesh_storage->mesh_surface_get_index_type(mesh_surface), nullptr, inst->instance_count);
3701
} else {
3702
glDrawArraysInstanced(primitive_gl, 0, count, inst->instance_count);
3703
}
3704
}
3705
} else {
3706
// Using regular Mesh.
3707
if (use_wireframe) {
3708
glDrawElements(GL_LINES, count, GL_UNSIGNED_INT, nullptr);
3709
} else {
3710
if (use_index_buffer) {
3711
glDrawElements(primitive_gl, count, mesh_storage->mesh_surface_get_index_type(mesh_surface), nullptr);
3712
} else {
3713
glDrawArrays(primitive_gl, 0, count);
3714
}
3715
}
3716
}
3717
3718
if (inst->instance_count > 0) {
3719
glDisableVertexAttribArray(12);
3720
glDisableVertexAttribArray(13);
3721
glDisableVertexAttribArray(14);
3722
glDisableVertexAttribArray(15);
3723
}
3724
}
3725
if constexpr (p_pass_mode == PASS_MODE_COLOR) {
3726
if (uses_additive_lighting && !p_render_data->transparent_bg) {
3727
// Disable additive blending if enabled for additive lights.
3728
scene_state.enable_gl_blend(false);
3729
}
3730
}
3731
}
3732
3733
// Make the actual redraw request
3734
if (should_request_redraw) {
3735
RenderingServerDefault::redraw_request();
3736
}
3737
}
3738
3739
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) {
3740
}
3741
3742
void RasterizerSceneGLES3::render_particle_collider_heightfield(RID p_collider, const Transform3D &p_transform, const PagedArray<RenderGeometryInstance *> &p_instances) {
3743
GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
3744
3745
ERR_FAIL_COND(!particles_storage->particles_collision_is_heightfield(p_collider));
3746
Vector3 extents = particles_storage->particles_collision_get_extents(p_collider) * p_transform.basis.get_scale();
3747
Projection cm;
3748
cm.set_orthogonal(-extents.x, extents.x, -extents.z, extents.z, 0, extents.y * 2.0);
3749
3750
Vector3 cam_pos = p_transform.origin;
3751
cam_pos.y += extents.y;
3752
3753
Transform3D cam_xform;
3754
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());
3755
3756
GLuint fb = particles_storage->particles_collision_get_heightfield_framebuffer(p_collider);
3757
Size2i fb_size = particles_storage->particles_collision_get_heightfield_size(p_collider);
3758
3759
RENDER_TIMESTAMP("Setup GPUParticlesCollisionHeightField3D");
3760
3761
RenderDataGLES3 render_data;
3762
3763
render_data.cam_projection = cm;
3764
render_data.cam_transform = cam_xform;
3765
render_data.view_projection[0] = cm;
3766
render_data.inv_cam_transform = render_data.cam_transform.affine_inverse();
3767
render_data.cam_orthogonal = true;
3768
render_data.z_near = 0.0;
3769
render_data.z_far = cm.get_z_far();
3770
render_data.main_cam_transform = cam_xform;
3771
3772
render_data.instances = &p_instances;
3773
3774
_setup_environment(&render_data, true, Vector2(fb_size), true, Color(), false);
3775
3776
PassMode pass_mode = PASS_MODE_SHADOW;
3777
3778
_fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
3779
render_list[RENDER_LIST_SECONDARY].sort_by_key();
3780
3781
RENDER_TIMESTAMP("Render Collider Heightfield");
3782
3783
glBindFramebuffer(GL_FRAMEBUFFER, fb);
3784
glViewport(0, 0, fb_size.width, fb_size.height);
3785
3786
GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
3787
3788
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer);
3789
glBindBuffer(GL_UNIFORM_BUFFER, 0);
3790
3791
scene_state.reset_gl_state();
3792
scene_state.enable_gl_depth_test(true);
3793
scene_state.enable_gl_depth_draw(true);
3794
scene_state.set_gl_depth_func(GL_GREATER);
3795
3796
glDrawBuffers(0, nullptr);
3797
3798
glColorMask(0, 0, 0, 0);
3799
RasterizerGLES3::clear_depth(0.0);
3800
3801
glClear(GL_DEPTH_BUFFER_BIT);
3802
3803
RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), false, 31, false);
3804
3805
_render_list_template<PASS_MODE_SHADOW>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());
3806
3807
glColorMask(1, 1, 1, 1);
3808
glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
3809
}
3810
3811
void RasterizerSceneGLES3::_render_uv2(const PagedArray<RenderGeometryInstance *> &p_instances, GLuint p_framebuffer, const Rect2i &p_region) {
3812
RENDER_TIMESTAMP("Setup Rendering UV2");
3813
3814
RenderDataGLES3 render_data;
3815
render_data.instances = &p_instances;
3816
3817
scene_state.ubo.emissive_exposure_normalization = -1.0; // Use default exposure normalization.
3818
3819
_setup_environment(&render_data, true, Vector2(1, 1), true, Color(), false);
3820
3821
PassMode pass_mode = PASS_MODE_MATERIAL;
3822
3823
_fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
3824
render_list[RENDER_LIST_SECONDARY].sort_by_key();
3825
3826
RENDER_TIMESTAMP("Render 3D Material");
3827
3828
{
3829
glBindFramebuffer(GL_FRAMEBUFFER, p_framebuffer);
3830
glViewport(p_region.position.x, p_region.position.y, p_region.size.x, p_region.size.y);
3831
3832
GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
3833
3834
glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer);
3835
glBindBuffer(GL_UNIFORM_BUFFER, 0);
3836
3837
scene_state.reset_gl_state();
3838
scene_state.enable_gl_depth_test(true);
3839
scene_state.enable_gl_depth_draw(true);
3840
scene_state.set_gl_depth_func(GL_GREATER);
3841
3842
TightLocalVector<GLenum> draw_buffers;
3843
draw_buffers.push_back(GL_COLOR_ATTACHMENT0);
3844
draw_buffers.push_back(GL_COLOR_ATTACHMENT1);
3845
draw_buffers.push_back(GL_COLOR_ATTACHMENT2);
3846
draw_buffers.push_back(GL_COLOR_ATTACHMENT3);
3847
glDrawBuffers(draw_buffers.size(), draw_buffers.ptr());
3848
3849
glClearColor(0.0, 0.0, 0.0, 0.0);
3850
RasterizerGLES3::clear_depth(0.0);
3851
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
3852
3853
uint64_t base_spec_constant = 0;
3854
base_spec_constant |= SceneShaderGLES3::RENDER_MATERIAL;
3855
base_spec_constant |= SceneShaderGLES3::DISABLE_FOG;
3856
base_spec_constant |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
3857
base_spec_constant |= SceneShaderGLES3::DISABLE_LIGHT_OMNI;
3858
base_spec_constant |= SceneShaderGLES3::DISABLE_LIGHT_SPOT;
3859
base_spec_constant |= SceneShaderGLES3::DISABLE_LIGHTMAP;
3860
3861
RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), false, base_spec_constant, true, Vector2(0, 0));
3862
3863
const int uv_offset_count = 9;
3864
static const Vector2 uv_offsets[uv_offset_count] = {
3865
Vector2(-1, 1),
3866
Vector2(1, 1),
3867
Vector2(1, -1),
3868
Vector2(-1, -1),
3869
Vector2(-1, 0),
3870
Vector2(1, 0),
3871
Vector2(0, -1),
3872
Vector2(0, 1),
3873
Vector2(0, 0),
3874
};
3875
3876
for (int i = 0; i < uv_offset_count; i++) {
3877
Vector2 ofs = uv_offsets[i];
3878
ofs.x /= p_region.size.width;
3879
ofs.y /= p_region.size.height;
3880
render_list_params.uv_offset = ofs;
3881
_render_list_template<PASS_MODE_MATERIAL>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());
3882
}
3883
3884
render_list_params.uv_offset = Vector2(0, 0);
3885
render_list_params.force_wireframe = false;
3886
_render_list_template<PASS_MODE_MATERIAL>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());
3887
3888
GLuint db = GL_COLOR_ATTACHMENT0;
3889
glDrawBuffers(1, &db);
3890
glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
3891
}
3892
}
3893
3894
void RasterizerSceneGLES3::set_time(double p_time, double p_step) {
3895
time = p_time;
3896
time_step = p_step;
3897
}
3898
3899
void RasterizerSceneGLES3::set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) {
3900
debug_draw = p_debug_draw;
3901
}
3902
3903
Ref<RenderSceneBuffers> RasterizerSceneGLES3::render_buffers_create() {
3904
Ref<RenderSceneBuffersGLES3> rb;
3905
rb.instantiate();
3906
return rb;
3907
}
3908
3909
void RasterizerSceneGLES3::_render_buffers_debug_draw(Ref<RenderSceneBuffersGLES3> p_render_buffers, RID p_shadow_atlas, GLuint p_fbo) {
3910
GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
3911
GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
3912
GLES3::CopyEffects *copy_effects = GLES3::CopyEffects::get_singleton();
3913
3914
ERR_FAIL_COND(p_render_buffers.is_null());
3915
3916
RID render_target = p_render_buffers->render_target;
3917
GLES3::RenderTarget *rt = texture_storage->get_render_target(render_target);
3918
ERR_FAIL_NULL(rt);
3919
3920
if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SHADOW_ATLAS) {
3921
if (p_shadow_atlas.is_valid()) {
3922
// Get or create debug textures to display shadow maps as an atlas.
3923
GLuint shadow_atlas_texture = light_storage->shadow_atlas_get_debug_texture(p_shadow_atlas);
3924
GLuint shadow_atlas_fb = light_storage->shadow_atlas_get_debug_fb(p_shadow_atlas);
3925
3926
uint32_t shadow_atlas_size = light_storage->shadow_atlas_get_size(p_shadow_atlas);
3927
uint32_t quadrant_size = shadow_atlas_size >> 1;
3928
3929
glBindFramebuffer(GL_FRAMEBUFFER, shadow_atlas_fb);
3930
glViewport(0, 0, shadow_atlas_size, shadow_atlas_size);
3931
glActiveTexture(GL_TEXTURE0);
3932
scene_state.enable_gl_depth_draw(true);
3933
scene_state.set_gl_depth_func(GL_ALWAYS);
3934
scene_state.set_gl_cull_mode(RS::CULL_MODE_DISABLED);
3935
3936
// Loop through quadrants and copy shadows over.
3937
for (int quadrant = 0; quadrant < 4; quadrant++) {
3938
uint32_t subdivision = light_storage->shadow_atlas_get_quadrant_subdivision(p_shadow_atlas, quadrant);
3939
if (subdivision == 0) {
3940
continue;
3941
}
3942
3943
Rect2i atlas_rect;
3944
Rect2 atlas_uv_rect;
3945
3946
uint32_t shadow_size = (quadrant_size / subdivision);
3947
float size = float(shadow_size) / float(shadow_atlas_size);
3948
3949
uint32_t length = light_storage->shadow_atlas_get_quadrant_shadows_allocated(p_shadow_atlas, quadrant);
3950
for (uint32_t shadow_idx = 0; shadow_idx < length; shadow_idx++) {
3951
bool is_omni = light_storage->shadow_atlas_get_quadrant_shadow_is_omni(p_shadow_atlas, quadrant, shadow_idx);
3952
3953
// Calculate shadow's position in the debug atlas.
3954
atlas_rect.position.x = (quadrant & 1) * quadrant_size;
3955
atlas_rect.position.y = (quadrant >> 1) * quadrant_size;
3956
3957
atlas_rect.position.x += (shadow_idx % subdivision) * shadow_size;
3958
atlas_rect.position.y += (shadow_idx / subdivision) * shadow_size;
3959
3960
atlas_uv_rect.position = Vector2(atlas_rect.position) / float(shadow_atlas_size);
3961
3962
atlas_uv_rect.size = Vector2(size, size);
3963
3964
GLuint shadow_tex = light_storage->shadow_atlas_get_quadrant_shadow_texture(p_shadow_atlas, quadrant, shadow_idx);
3965
// Copy from shadowmap to debug atlas.
3966
if (is_omni) {
3967
glBindTexture(GL_TEXTURE_CUBE_MAP, shadow_tex);
3968
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_NONE);
3969
3970
copy_effects->copy_cube_to_rect(atlas_uv_rect);
3971
3972
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
3973
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
3974
} else {
3975
glBindTexture(GL_TEXTURE_2D, shadow_tex);
3976
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
3977
3978
copy_effects->copy_to_rect(atlas_uv_rect);
3979
3980
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
3981
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
3982
}
3983
}
3984
}
3985
3986
// Set back to FBO
3987
glBindFramebuffer(GL_FRAMEBUFFER, p_fbo);
3988
Size2i size = p_render_buffers->get_internal_size();
3989
glViewport(0, 0, size.width, size.height);
3990
glBindTexture(GL_TEXTURE_2D, shadow_atlas_texture);
3991
3992
copy_effects->copy_to_rect(Rect2(Vector2(), Vector2(0.5, 0.5)));
3993
glBindTexture(GL_TEXTURE_2D, 0);
3994
glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
3995
}
3996
}
3997
if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS) {
3998
if (light_storage->directional_shadow_get_texture() != 0) {
3999
GLuint shadow_atlas_texture = light_storage->directional_shadow_get_texture();
4000
glActiveTexture(GL_TEXTURE0);
4001
glBindTexture(GL_TEXTURE_2D, shadow_atlas_texture);
4002
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
4003
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
4004
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
4005
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
4006
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE);
4007
4008
scene_state.enable_gl_depth_test(false);
4009
scene_state.enable_gl_depth_draw(false);
4010
4011
copy_effects->copy_to_rect(Rect2(Vector2(), Vector2(0.5, 0.5)));
4012
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
4013
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
4014
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
4015
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
4016
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
4017
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
4018
glBindTexture(GL_TEXTURE_2D, 0);
4019
}
4020
}
4021
}
4022
4023
void RasterizerSceneGLES3::gi_set_use_half_resolution(bool p_enable) {
4024
}
4025
4026
void RasterizerSceneGLES3::screen_space_roughness_limiter_set_active(bool p_enable, float p_amount, float p_curve) {
4027
}
4028
4029
bool RasterizerSceneGLES3::screen_space_roughness_limiter_is_active() const {
4030
return false;
4031
}
4032
4033
void RasterizerSceneGLES3::sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality) {
4034
}
4035
4036
void RasterizerSceneGLES3::sub_surface_scattering_set_scale(float p_scale, float p_depth_scale) {
4037
}
4038
4039
TypedArray<Image> RasterizerSceneGLES3::bake_render_uv2(RID p_base, const TypedArray<RID> &p_material_overrides, const Size2i &p_image_size) {
4040
GLES3::Config *config = GLES3::Config::get_singleton();
4041
ERR_FAIL_COND_V_MSG(p_image_size.width <= 0, TypedArray<Image>(), "Image width must be greater than 0.");
4042
ERR_FAIL_COND_V_MSG(p_image_size.height <= 0, TypedArray<Image>(), "Image height must be greater than 0.");
4043
4044
GLuint albedo_alpha_tex = 0;
4045
GLuint normal_tex = 0;
4046
GLuint orm_tex = 0;
4047
GLuint emission_tex = 0;
4048
GLuint depth_tex = 0;
4049
glGenTextures(1, &albedo_alpha_tex);
4050
glGenTextures(1, &normal_tex);
4051
glGenTextures(1, &orm_tex);
4052
glGenTextures(1, &emission_tex);
4053
glGenTextures(1, &depth_tex);
4054
4055
glBindTexture(GL_TEXTURE_2D, albedo_alpha_tex);
4056
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_image_size.width, p_image_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
4057
GLES3::Utilities::get_singleton()->texture_allocated_data(albedo_alpha_tex, p_image_size.width * p_image_size.height * 4, "Lightmap albedo texture");
4058
4059
glBindTexture(GL_TEXTURE_2D, normal_tex);
4060
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_image_size.width, p_image_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
4061
GLES3::Utilities::get_singleton()->texture_allocated_data(normal_tex, p_image_size.width * p_image_size.height * 4, "Lightmap normal texture");
4062
4063
glBindTexture(GL_TEXTURE_2D, orm_tex);
4064
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_image_size.width, p_image_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
4065
GLES3::Utilities::get_singleton()->texture_allocated_data(orm_tex, p_image_size.width * p_image_size.height * 4, "Lightmap ORM texture");
4066
4067
// Consider rendering to RGBA8 encoded as RGBE, then manually convert to RGBAH on CPU.
4068
glBindTexture(GL_TEXTURE_2D, emission_tex);
4069
if (config->float_texture_supported) {
4070
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, p_image_size.width, p_image_size.height, 0, GL_RGBA, GL_FLOAT, nullptr);
4071
GLES3::Utilities::get_singleton()->texture_allocated_data(emission_tex, p_image_size.width * p_image_size.height * 16, "Lightmap emission texture");
4072
} else {
4073
// Fallback to RGBA8 on devices that don't support rendering to floating point textures. This will look bad, but we have no choice.
4074
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, p_image_size.width, p_image_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
4075
GLES3::Utilities::get_singleton()->texture_allocated_data(emission_tex, p_image_size.width * p_image_size.height * 4, "Lightmap emission texture");
4076
}
4077
4078
glBindTexture(GL_TEXTURE_2D, depth_tex);
4079
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, p_image_size.width, p_image_size.height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
4080
GLES3::Utilities::get_singleton()->texture_allocated_data(depth_tex, p_image_size.width * p_image_size.height * 3, "Lightmap depth texture");
4081
4082
GLuint fbo = 0;
4083
glGenFramebuffers(1, &fbo);
4084
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
4085
4086
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, albedo_alpha_tex, 0);
4087
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, normal_tex, 0);
4088
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, orm_tex, 0);
4089
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, emission_tex, 0);
4090
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_tex, 0);
4091
4092
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
4093
if (status != GL_FRAMEBUFFER_COMPLETE) {
4094
glDeleteFramebuffers(1, &fbo);
4095
GLES3::Utilities::get_singleton()->texture_free_data(albedo_alpha_tex);
4096
GLES3::Utilities::get_singleton()->texture_free_data(normal_tex);
4097
GLES3::Utilities::get_singleton()->texture_free_data(orm_tex);
4098
GLES3::Utilities::get_singleton()->texture_free_data(emission_tex);
4099
GLES3::Utilities::get_singleton()->texture_free_data(depth_tex);
4100
4101
WARN_PRINT("Could not create render target, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
4102
return TypedArray<Image>();
4103
}
4104
4105
RenderGeometryInstance *gi_inst = geometry_instance_create(p_base);
4106
ERR_FAIL_NULL_V(gi_inst, TypedArray<Image>());
4107
4108
uint32_t sc = RSG::mesh_storage->mesh_get_surface_count(p_base);
4109
Vector<RID> materials;
4110
materials.resize(sc);
4111
4112
for (uint32_t i = 0; i < sc; i++) {
4113
if (i < (uint32_t)p_material_overrides.size()) {
4114
materials.write[i] = p_material_overrides[i];
4115
}
4116
}
4117
4118
gi_inst->set_surface_materials(materials);
4119
4120
if (cull_argument.size() == 0) {
4121
cull_argument.push_back(nullptr);
4122
}
4123
cull_argument[0] = gi_inst;
4124
_render_uv2(cull_argument, fbo, Rect2i(0, 0, p_image_size.width, p_image_size.height));
4125
4126
geometry_instance_free(gi_inst);
4127
4128
TypedArray<Image> ret;
4129
4130
// Create a dummy texture so we can use texture_2d_get.
4131
RID tex_rid = GLES3::TextureStorage::get_singleton()->texture_allocate();
4132
GLES3::Texture texture;
4133
texture.width = p_image_size.width;
4134
texture.height = p_image_size.height;
4135
texture.alloc_width = p_image_size.width;
4136
texture.alloc_height = p_image_size.height;
4137
texture.format = Image::FORMAT_RGBA8;
4138
texture.real_format = Image::FORMAT_RGBA8;
4139
texture.gl_format_cache = GL_RGBA;
4140
texture.gl_type_cache = GL_UNSIGNED_BYTE;
4141
texture.type = GLES3::Texture::TYPE_2D;
4142
texture.target = GL_TEXTURE_2D;
4143
texture.active = true;
4144
texture.is_render_target = true; // Enable this so the texture isn't cached in the editor.
4145
4146
GLES3::TextureStorage::get_singleton()->texture_2d_initialize_from_texture(tex_rid, texture);
4147
GLES3::Texture *tex = GLES3::TextureStorage::get_singleton()->get_texture(tex_rid);
4148
4149
{
4150
tex->tex_id = albedo_alpha_tex;
4151
Ref<Image> img = GLES3::TextureStorage::get_singleton()->texture_2d_get(tex_rid);
4152
GLES3::Utilities::get_singleton()->texture_free_data(albedo_alpha_tex);
4153
ret.push_back(img);
4154
}
4155
4156
{
4157
tex->tex_id = normal_tex;
4158
Ref<Image> img = GLES3::TextureStorage::get_singleton()->texture_2d_get(tex_rid);
4159
GLES3::Utilities::get_singleton()->texture_free_data(normal_tex);
4160
ret.push_back(img);
4161
}
4162
4163
{
4164
tex->tex_id = orm_tex;
4165
Ref<Image> img = GLES3::TextureStorage::get_singleton()->texture_2d_get(tex_rid);
4166
GLES3::Utilities::get_singleton()->texture_free_data(orm_tex);
4167
ret.push_back(img);
4168
}
4169
4170
{
4171
tex->tex_id = emission_tex;
4172
if (config->float_texture_supported) {
4173
tex->format = Image::FORMAT_RGBAH;
4174
tex->real_format = Image::FORMAT_RGBAH;
4175
tex->gl_type_cache = GL_HALF_FLOAT;
4176
}
4177
Ref<Image> img = GLES3::TextureStorage::get_singleton()->texture_2d_get(tex_rid);
4178
GLES3::Utilities::get_singleton()->texture_free_data(emission_tex);
4179
ret.push_back(img);
4180
}
4181
4182
tex->is_render_target = false;
4183
tex->tex_id = 0;
4184
GLES3::TextureStorage::get_singleton()->texture_free(tex_rid);
4185
4186
GLES3::Utilities::get_singleton()->texture_free_data(depth_tex);
4187
glDeleteFramebuffers(1, &fbo);
4188
return ret;
4189
}
4190
4191
bool RasterizerSceneGLES3::free(RID p_rid) {
4192
if (is_environment(p_rid)) {
4193
environment_free(p_rid);
4194
} else if (sky_owner.owns(p_rid)) {
4195
Sky *sky = sky_owner.get_or_null(p_rid);
4196
ERR_FAIL_NULL_V(sky, false);
4197
_free_sky_data(sky);
4198
sky_owner.free(p_rid);
4199
} else if (GLES3::LightStorage::get_singleton()->owns_light_instance(p_rid)) {
4200
GLES3::LightStorage::get_singleton()->light_instance_free(p_rid);
4201
} else if (RSG::camera_attributes->owns_camera_attributes(p_rid)) {
4202
//not much to delete, just free it
4203
RSG::camera_attributes->camera_attributes_free(p_rid);
4204
} else if (is_compositor(p_rid)) {
4205
compositor_free(p_rid);
4206
} else if (is_compositor_effect(p_rid)) {
4207
compositor_effect_free(p_rid);
4208
} else {
4209
return false;
4210
}
4211
return true;
4212
}
4213
4214
void RasterizerSceneGLES3::update() {
4215
_update_dirty_skys();
4216
}
4217
4218
void RasterizerSceneGLES3::sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) {
4219
}
4220
4221
void RasterizerSceneGLES3::decals_set_filter(RS::DecalFilter p_filter) {
4222
}
4223
4224
void RasterizerSceneGLES3::light_projectors_set_filter(RS::LightProjectorFilter p_filter) {
4225
}
4226
4227
void RasterizerSceneGLES3::lightmaps_set_bicubic_filter(bool p_enable) {
4228
lightmap_bicubic_upscale = p_enable;
4229
}
4230
4231
RasterizerSceneGLES3::RasterizerSceneGLES3() {
4232
singleton = this;
4233
4234
GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
4235
GLES3::Config *config = GLES3::Config::get_singleton();
4236
4237
cull_argument.set_page_pool(&cull_argument_pool);
4238
4239
// Quality settings.
4240
use_physical_light_units = GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units");
4241
4242
positional_soft_shadow_filter_set_quality((RS::ShadowQuality)(int)GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/soft_shadow_filter_quality"));
4243
directional_soft_shadow_filter_set_quality((RS::ShadowQuality)(int)GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/soft_shadow_filter_quality"));
4244
lightmaps_set_bicubic_filter(GLOBAL_GET("rendering/lightmapping/lightmap_gi/use_bicubic_filter"));
4245
4246
{
4247
// Setup Lights
4248
4249
config->max_renderable_lights = MIN(config->max_renderable_lights, config->max_uniform_buffer_size / (int)sizeof(RasterizerSceneGLES3::LightData));
4250
config->max_lights_per_object = MIN(config->max_lights_per_object, config->max_renderable_lights);
4251
4252
uint32_t light_buffer_size = config->max_renderable_lights * sizeof(LightData);
4253
scene_state.omni_lights = memnew_arr(LightData, config->max_renderable_lights);
4254
scene_state.omni_light_sort = memnew_arr(InstanceSort<GLES3::LightInstance>, config->max_renderable_lights);
4255
glGenBuffers(1, &scene_state.omni_light_buffer);
4256
glBindBuffer(GL_UNIFORM_BUFFER, scene_state.omni_light_buffer);
4257
GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.omni_light_buffer, light_buffer_size, nullptr, GL_STREAM_DRAW, "OmniLight UBO");
4258
4259
scene_state.spot_lights = memnew_arr(LightData, config->max_renderable_lights);
4260
scene_state.spot_light_sort = memnew_arr(InstanceSort<GLES3::LightInstance>, config->max_renderable_lights);
4261
glGenBuffers(1, &scene_state.spot_light_buffer);
4262
glBindBuffer(GL_UNIFORM_BUFFER, scene_state.spot_light_buffer);
4263
GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.spot_light_buffer, light_buffer_size, nullptr, GL_STREAM_DRAW, "SpotLight UBO");
4264
4265
uint32_t directional_light_buffer_size = MAX_DIRECTIONAL_LIGHTS * sizeof(DirectionalLightData);
4266
scene_state.directional_lights = memnew_arr(DirectionalLightData, MAX_DIRECTIONAL_LIGHTS);
4267
glGenBuffers(1, &scene_state.directional_light_buffer);
4268
glBindBuffer(GL_UNIFORM_BUFFER, scene_state.directional_light_buffer);
4269
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");
4270
4271
uint32_t shadow_buffer_size = config->max_renderable_lights * sizeof(ShadowData) * 2;
4272
scene_state.positional_shadows = memnew_arr(ShadowData, config->max_renderable_lights * 2);
4273
glGenBuffers(1, &scene_state.positional_shadow_buffer);
4274
glBindBuffer(GL_UNIFORM_BUFFER, scene_state.positional_shadow_buffer);
4275
GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.positional_shadow_buffer, shadow_buffer_size, nullptr, GL_STREAM_DRAW, "Positional Shadow UBO");
4276
4277
uint32_t directional_shadow_buffer_size = MAX_DIRECTIONAL_LIGHTS * sizeof(DirectionalShadowData);
4278
scene_state.directional_shadows = memnew_arr(DirectionalShadowData, MAX_DIRECTIONAL_LIGHTS);
4279
glGenBuffers(1, &scene_state.directional_shadow_buffer);
4280
glBindBuffer(GL_UNIFORM_BUFFER, scene_state.directional_shadow_buffer);
4281
GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.directional_shadow_buffer, directional_shadow_buffer_size, nullptr, GL_STREAM_DRAW, "Directional Shadow UBO");
4282
4283
glBindBuffer(GL_UNIFORM_BUFFER, 0);
4284
}
4285
4286
{
4287
sky_globals.max_directional_lights = 4;
4288
uint32_t directional_light_buffer_size = sky_globals.max_directional_lights * sizeof(DirectionalLightData);
4289
sky_globals.directional_lights = memnew_arr(DirectionalLightData, sky_globals.max_directional_lights);
4290
sky_globals.last_frame_directional_lights = memnew_arr(DirectionalLightData, sky_globals.max_directional_lights);
4291
sky_globals.last_frame_directional_light_count = sky_globals.max_directional_lights + 1;
4292
glGenBuffers(1, &sky_globals.directional_light_buffer);
4293
glBindBuffer(GL_UNIFORM_BUFFER, sky_globals.directional_light_buffer);
4294
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");
4295
4296
glBindBuffer(GL_UNIFORM_BUFFER, 0);
4297
}
4298
4299
{
4300
String global_defines;
4301
global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now
4302
global_defines += "\n#define MAX_LIGHT_DATA_STRUCTS " + itos(config->max_renderable_lights) + "\n";
4303
global_defines += "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(MAX_DIRECTIONAL_LIGHTS) + "\n";
4304
global_defines += "\n#define MAX_FORWARD_LIGHTS " + itos(config->max_lights_per_object) + "u\n";
4305
global_defines += "\n#define MAX_ROUGHNESS_LOD " + itos(sky_globals.roughness_layers - 1) + ".0\n";
4306
if (config->force_vertex_shading) {
4307
global_defines += "\n#define USE_VERTEX_LIGHTING\n";
4308
}
4309
if (!config->specular_occlusion) {
4310
global_defines += "\n#define SPECULAR_OCCLUSION_DISABLED\n";
4311
}
4312
material_storage->shaders.scene_shader.initialize(global_defines);
4313
scene_globals.shader_default_version = material_storage->shaders.scene_shader.version_create();
4314
material_storage->shaders.scene_shader.version_bind_shader(scene_globals.shader_default_version, SceneShaderGLES3::MODE_COLOR);
4315
}
4316
4317
{
4318
//default material and shader
4319
scene_globals.default_shader = material_storage->shader_allocate();
4320
material_storage->shader_initialize(scene_globals.default_shader);
4321
material_storage->shader_set_code(scene_globals.default_shader, R"(
4322
// Default 3D material shader (Compatibility).
4323
4324
shader_type spatial;
4325
4326
void vertex() {
4327
ROUGHNESS = 0.8;
4328
}
4329
4330
void fragment() {
4331
ALBEDO = vec3(0.6);
4332
ROUGHNESS = 0.8;
4333
METALLIC = 0.2;
4334
}
4335
)");
4336
scene_globals.default_material = material_storage->material_allocate();
4337
material_storage->material_initialize(scene_globals.default_material);
4338
material_storage->material_set_shader(scene_globals.default_material, scene_globals.default_shader);
4339
default_material_data_ptr = static_cast<GLES3::SceneMaterialData *>(GLES3::MaterialStorage::get_singleton()->material_get_data(scene_globals.default_material, RS::SHADER_SPATIAL));
4340
}
4341
4342
{
4343
// Overdraw material and shader.
4344
scene_globals.overdraw_shader = material_storage->shader_allocate();
4345
material_storage->shader_initialize(scene_globals.overdraw_shader);
4346
material_storage->shader_set_code(scene_globals.overdraw_shader, R"(
4347
// 3D editor Overdraw debug draw mode shader (Compatibility).
4348
4349
shader_type spatial;
4350
4351
render_mode blend_add, unshaded, fog_disabled;
4352
4353
void fragment() {
4354
ALBEDO = vec3(0.4, 0.8, 0.8);
4355
ALPHA = 0.2;
4356
}
4357
)");
4358
scene_globals.overdraw_material = material_storage->material_allocate();
4359
material_storage->material_initialize(scene_globals.overdraw_material);
4360
material_storage->material_set_shader(scene_globals.overdraw_material, scene_globals.overdraw_shader);
4361
overdraw_material_data_ptr = static_cast<GLES3::SceneMaterialData *>(GLES3::MaterialStorage::get_singleton()->material_get_data(scene_globals.overdraw_material, RS::SHADER_SPATIAL));
4362
}
4363
4364
{
4365
// Initialize Sky stuff
4366
sky_globals.roughness_layers = GLOBAL_GET("rendering/reflections/sky_reflections/roughness_layers");
4367
4368
String global_defines;
4369
global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now
4370
global_defines += "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(sky_globals.max_directional_lights) + "\n";
4371
material_storage->shaders.sky_shader.initialize(global_defines);
4372
sky_globals.shader_default_version = material_storage->shaders.sky_shader.version_create();
4373
}
4374
4375
{
4376
sky_globals.default_shader = material_storage->shader_allocate();
4377
4378
material_storage->shader_initialize(sky_globals.default_shader);
4379
4380
material_storage->shader_set_code(sky_globals.default_shader, R"(
4381
// Default sky shader.
4382
4383
shader_type sky;
4384
4385
void sky() {
4386
COLOR = vec3(0.0);
4387
}
4388
)");
4389
sky_globals.default_material = material_storage->material_allocate();
4390
material_storage->material_initialize(sky_globals.default_material);
4391
4392
material_storage->material_set_shader(sky_globals.default_material, sky_globals.default_shader);
4393
}
4394
{
4395
sky_globals.fog_shader = material_storage->shader_allocate();
4396
material_storage->shader_initialize(sky_globals.fog_shader);
4397
4398
material_storage->shader_set_code(sky_globals.fog_shader, R"(
4399
// Default clear color sky shader.
4400
4401
shader_type sky;
4402
4403
uniform vec4 clear_color;
4404
4405
void sky() {
4406
COLOR = clear_color.rgb;
4407
}
4408
)");
4409
sky_globals.fog_material = material_storage->material_allocate();
4410
material_storage->material_initialize(sky_globals.fog_material);
4411
4412
material_storage->material_set_shader(sky_globals.fog_material, sky_globals.fog_shader);
4413
}
4414
4415
{
4416
glGenVertexArrays(1, &sky_globals.screen_triangle_array);
4417
glBindVertexArray(sky_globals.screen_triangle_array);
4418
glGenBuffers(1, &sky_globals.screen_triangle);
4419
glBindBuffer(GL_ARRAY_BUFFER, sky_globals.screen_triangle);
4420
4421
const float qv[6] = {
4422
-1.0f,
4423
-1.0f,
4424
3.0f,
4425
-1.0f,
4426
-1.0f,
4427
3.0f,
4428
};
4429
4430
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");
4431
4432
glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);
4433
glEnableVertexAttribArray(RS::ARRAY_VERTEX);
4434
glBindVertexArray(0);
4435
glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
4436
}
4437
4438
#ifdef GL_API_ENABLED
4439
if (RasterizerGLES3::is_gles_over_gl()) {
4440
glEnable(_EXT_TEXTURE_CUBE_MAP_SEAMLESS);
4441
}
4442
#endif // GL_API_ENABLED
4443
4444
// MultiMesh may read from color when color is disabled, so make sure that the color defaults to white instead of black;
4445
glVertexAttrib4f(RS::ARRAY_COLOR, 1.0, 1.0, 1.0, 1.0);
4446
}
4447
4448
RasterizerSceneGLES3::~RasterizerSceneGLES3() {
4449
GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.directional_light_buffer);
4450
GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.omni_light_buffer);
4451
GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.spot_light_buffer);
4452
GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.positional_shadow_buffer);
4453
GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.directional_shadow_buffer);
4454
memdelete_arr(scene_state.directional_lights);
4455
memdelete_arr(scene_state.omni_lights);
4456
memdelete_arr(scene_state.spot_lights);
4457
memdelete_arr(scene_state.omni_light_sort);
4458
memdelete_arr(scene_state.spot_light_sort);
4459
memdelete_arr(scene_state.positional_shadows);
4460
memdelete_arr(scene_state.directional_shadows);
4461
4462
// Scene Shader
4463
GLES3::MaterialStorage::get_singleton()->shaders.scene_shader.version_free(scene_globals.shader_default_version);
4464
RSG::material_storage->material_free(scene_globals.default_material);
4465
RSG::material_storage->shader_free(scene_globals.default_shader);
4466
4467
// Overdraw Shader
4468
RSG::material_storage->material_free(scene_globals.overdraw_material);
4469
RSG::material_storage->shader_free(scene_globals.overdraw_shader);
4470
4471
// Sky Shader
4472
GLES3::MaterialStorage::get_singleton()->shaders.sky_shader.version_free(sky_globals.shader_default_version);
4473
RSG::material_storage->material_free(sky_globals.default_material);
4474
RSG::material_storage->shader_free(sky_globals.default_shader);
4475
RSG::material_storage->material_free(sky_globals.fog_material);
4476
RSG::material_storage->shader_free(sky_globals.fog_shader);
4477
GLES3::Utilities::get_singleton()->buffer_free_data(sky_globals.screen_triangle);
4478
glDeleteVertexArrays(1, &sky_globals.screen_triangle_array);
4479
GLES3::Utilities::get_singleton()->buffer_free_data(sky_globals.directional_light_buffer);
4480
memdelete_arr(sky_globals.directional_lights);
4481
memdelete_arr(sky_globals.last_frame_directional_lights);
4482
4483
// UBOs
4484
if (scene_state.ubo_buffer != 0) {
4485
GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.ubo_buffer);
4486
}
4487
4488
if (scene_state.multiview_buffer != 0) {
4489
GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.multiview_buffer);
4490
}
4491
4492
if (scene_state.tonemap_buffer != 0) {
4493
GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.tonemap_buffer);
4494
}
4495
4496
singleton = nullptr;
4497
}
4498
4499
#endif // GLES3_ENABLED
4500
4501