Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/gles3/rasterizer_scene_gles3.h
22205 views
1
/**************************************************************************/
2
/* rasterizer_scene_gles3.h */
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
#pragma once
32
33
#include "platform_gl.h"
34
#ifdef GLES3_ENABLED
35
36
#include "core/math/projection.h"
37
#include "core/templates/paged_allocator.h"
38
#include "core/templates/rid_owner.h"
39
#include "core/templates/self_list.h"
40
#include "drivers/gles3/shaders/effects/cubemap_filter.glsl.gen.h"
41
#include "drivers/gles3/shaders/sky.glsl.gen.h"
42
#include "scene/resources/mesh.h"
43
#include "servers/rendering/renderer_compositor.h"
44
#include "servers/rendering/renderer_scene_render.h"
45
#include "servers/rendering/rendering_server.h"
46
#include "shader_gles3.h"
47
#include "storage/light_storage.h"
48
#include "storage/material_storage.h"
49
#include "storage/render_scene_buffers_gles3.h"
50
#include "storage/utilities.h"
51
52
enum RenderListType {
53
RENDER_LIST_OPAQUE, //used for opaque objects
54
RENDER_LIST_ALPHA, //used for transparent objects
55
RENDER_LIST_SECONDARY, //used for shadows and other objects
56
RENDER_LIST_MAX
57
};
58
59
enum PassMode {
60
PASS_MODE_COLOR,
61
PASS_MODE_COLOR_TRANSPARENT,
62
PASS_MODE_SHADOW,
63
PASS_MODE_DEPTH,
64
PASS_MODE_MATERIAL,
65
PASS_MODE_MOTION_VECTORS,
66
};
67
68
// These should share as much as possible with SkyUniform Location
69
enum SceneUniformLocation {
70
SCENE_TONEMAP_UNIFORM_LOCATION,
71
SCENE_GLOBALS_UNIFORM_LOCATION,
72
SCENE_DATA_UNIFORM_LOCATION,
73
SCENE_MATERIAL_UNIFORM_LOCATION,
74
SCENE_EMPTY1, // Unused, put here to avoid conflicts with SKY_DIRECTIONAL_LIGHT_UNIFORM_LOCATION.
75
SCENE_OMNILIGHT_UNIFORM_LOCATION,
76
SCENE_SPOTLIGHT_UNIFORM_LOCATION,
77
SCENE_DIRECTIONAL_LIGHT_UNIFORM_LOCATION,
78
SCENE_MULTIVIEW_UNIFORM_LOCATION,
79
SCENE_POSITIONAL_SHADOW_UNIFORM_LOCATION,
80
SCENE_DIRECTIONAL_SHADOW_UNIFORM_LOCATION,
81
SCENE_EMPTY2, // Unused, put here to avoid conflicts with SKY_MULTIVIEW_UNIFORM_LOCATION.
82
SCENE_PREV_DATA_UNIFORM_LOCATION,
83
SCENE_PREV_MULTIVIEW_UNIFORM_LOCATION,
84
};
85
86
enum SkyUniformLocation {
87
SKY_TONEMAP_UNIFORM_LOCATION,
88
SKY_GLOBALS_UNIFORM_LOCATION,
89
SKY_EMPTY1, // Unused, put here to avoid conflicts with SCENE_DATA_UNIFORM_LOCATION.
90
SKY_MATERIAL_UNIFORM_LOCATION,
91
SKY_DIRECTIONAL_LIGHT_UNIFORM_LOCATION,
92
SKY_EMPTY2, // Unused, put here to avoid conflicts with SCENE_OMNILIGHT_UNIFORM_LOCATION.
93
SKY_EMPTY3, // Unused, put here to avoid conflicts with SCENE_SPOTLIGHT_UNIFORM_LOCATION.
94
SKY_EMPTY4, // Unused, put here to avoid conflicts with SCENE_DIRECTIONAL_LIGHT_UNIFORM_LOCATION.
95
SKY_EMPTY5, // Unused, put here to avoid conflicts with SCENE_MULTIVIEW_UNIFORM_LOCATION.
96
SKY_EMPTY6, // Unused, put here to avoid conflicts with SCENE_POSITIONAL_SHADOW_UNIFORM_LOCATION.
97
SKY_EMPTY7, // Unused, put here to avoid conflicts with SCENE_DIRECTIONAL_SHADOW_UNIFORM_LOCATION.
98
SKY_MULTIVIEW_UNIFORM_LOCATION,
99
SKY_EMPTY8, // Unused, put here to avoid conflicts with SCENE_PREV_DATA_UNIFORM_LOCATION.
100
SKY_EMPTY9, // Unused, put here to avoid conflicts with SCENE_PREV_MULTIVIEW_UNIFORM_LOCATION.
101
};
102
103
struct RenderDataGLES3 {
104
Ref<RenderSceneBuffersGLES3> render_buffers;
105
bool transparent_bg = false;
106
Rect2i render_region;
107
108
Transform3D cam_transform;
109
Transform3D inv_cam_transform;
110
Projection cam_projection;
111
bool cam_orthogonal = false;
112
bool cam_frustum = false;
113
uint32_t camera_visible_layers = 0xFFFFFFFF;
114
115
// For billboards to cast correct shadows.
116
Transform3D main_cam_transform;
117
118
// For stereo rendering
119
uint32_t view_count = 1;
120
Vector3 view_eye_offset[RendererSceneRender::MAX_RENDER_VIEWS];
121
Projection view_projection[RendererSceneRender::MAX_RENDER_VIEWS];
122
123
float z_near = 0.0;
124
float z_far = 0.0;
125
126
const PagedArray<RenderGeometryInstance *> *instances = nullptr;
127
const PagedArray<RID> *lights = nullptr;
128
const PagedArray<RID> *reflection_probes = nullptr;
129
RID environment;
130
RID camera_attributes;
131
RID shadow_atlas;
132
RID reflection_probe;
133
int reflection_probe_pass = 0;
134
135
float lod_distance_multiplier = 0.0;
136
float screen_mesh_lod_threshold = 0.0;
137
138
uint32_t directional_light_count = 0;
139
uint32_t directional_shadow_count = 0;
140
141
uint32_t spot_light_count = 0;
142
uint32_t omni_light_count = 0;
143
144
float luminance_multiplier = 1.0;
145
146
RenderingMethod::RenderInfo *render_info = nullptr;
147
148
/* Shadow data */
149
const RendererSceneRender::RenderShadowData *render_shadows = nullptr;
150
int render_shadow_count = 0;
151
};
152
153
class RasterizerCanvasGLES3;
154
155
class RasterizerSceneGLES3 : public RendererSceneRender {
156
private:
157
static RasterizerSceneGLES3 *singleton;
158
RS::ViewportDebugDraw debug_draw = RS::VIEWPORT_DEBUG_DRAW_DISABLED;
159
uint64_t scene_pass = 0;
160
161
template <typename T>
162
struct InstanceSort {
163
float depth;
164
T *instance = nullptr;
165
bool operator<(const InstanceSort &p_sort) const {
166
return depth < p_sort.depth;
167
}
168
};
169
170
struct SceneGlobals {
171
RID shader_default_version;
172
RID default_material;
173
RID default_shader;
174
RID overdraw_material;
175
RID overdraw_shader;
176
} scene_globals;
177
178
GLES3::SceneMaterialData *default_material_data_ptr = nullptr;
179
GLES3::SceneMaterialData *overdraw_material_data_ptr = nullptr;
180
181
/* LIGHT INSTANCE */
182
183
struct LightData {
184
float position[3];
185
float inv_radius;
186
187
float direction[3]; // Only used by SpotLight
188
float size;
189
190
float color[3];
191
float attenuation;
192
193
float inv_spot_attenuation;
194
float cos_spot_angle;
195
float specular_amount;
196
float shadow_opacity;
197
198
float pad[3];
199
uint32_t bake_mode;
200
};
201
static_assert(sizeof(LightData) % 16 == 0, "LightData size must be a multiple of 16 bytes");
202
203
struct DirectionalLightData {
204
float direction[3];
205
float energy;
206
207
float color[3];
208
float size;
209
210
uint32_t enabled : 1; // For use by SkyShaders
211
uint32_t bake_mode : 2;
212
float shadow_opacity;
213
float specular;
214
uint32_t mask;
215
};
216
static_assert(sizeof(DirectionalLightData) % 16 == 0, "DirectionalLightData size must be a multiple of 16 bytes");
217
218
struct ShadowData {
219
float shadow_matrix[16];
220
221
float light_position[3];
222
float shadow_normal_bias;
223
224
float pad[3];
225
float shadow_atlas_pixel_size;
226
};
227
static_assert(sizeof(ShadowData) % 16 == 0, "ShadowData size must be a multiple of 16 bytes");
228
229
struct DirectionalShadowData {
230
float direction[3];
231
float shadow_atlas_pixel_size;
232
float shadow_normal_bias[4];
233
float shadow_split_offsets[4];
234
float shadow_matrices[4][16];
235
float fade_from;
236
float fade_to;
237
uint32_t blend_splits; // Not exposed to the shader.
238
uint32_t pad;
239
};
240
static_assert(sizeof(DirectionalShadowData) % 16 == 0, "DirectionalShadowData size must be a multiple of 16 bytes");
241
242
class GeometryInstanceGLES3;
243
244
// Cached data for drawing surfaces
245
struct GeometryInstanceSurface {
246
enum {
247
FLAG_PASS_DEPTH = 1,
248
FLAG_PASS_OPAQUE = 2,
249
FLAG_PASS_ALPHA = 4,
250
FLAG_PASS_SHADOW = 8,
251
FLAG_USES_SHARED_SHADOW_MATERIAL = 128,
252
FLAG_USES_SCREEN_TEXTURE = 2048,
253
FLAG_USES_DEPTH_TEXTURE = 4096,
254
FLAG_USES_NORMAL_TEXTURE = 8192,
255
FLAG_USES_DOUBLE_SIDED_SHADOWS = 16384,
256
FLAG_USES_STENCIL = 32768,
257
};
258
259
union {
260
struct {
261
uint64_t sort_key1;
262
uint64_t sort_key2;
263
};
264
struct {
265
uint64_t lod_index : 8;
266
uint64_t surface_index : 8;
267
uint64_t geometry_id : 32;
268
uint64_t material_id_low : 16;
269
270
uint64_t material_id_hi : 16;
271
uint64_t shader_id : 32;
272
uint64_t uses_softshadow : 1;
273
uint64_t uses_projector : 1;
274
uint64_t uses_forward_gi : 1;
275
uint64_t uses_lightmap : 1;
276
uint64_t depth_layer : 4;
277
uint64_t priority : 8;
278
};
279
} sort;
280
281
RS::PrimitiveType primitive = RS::PRIMITIVE_MAX;
282
uint32_t flags = 0;
283
uint32_t surface_index = 0;
284
uint32_t lod_index = 0;
285
uint32_t index_count = 0;
286
int32_t light_pass_index = -1;
287
bool finished_base_pass = false;
288
289
void *surface = nullptr;
290
GLES3::SceneShaderData *shader = nullptr;
291
GLES3::SceneMaterialData *material = nullptr;
292
293
void *surface_shadow = nullptr;
294
GLES3::SceneShaderData *shader_shadow = nullptr;
295
GLES3::SceneMaterialData *material_shadow = nullptr;
296
297
GeometryInstanceSurface *next = nullptr;
298
GeometryInstanceGLES3 *owner = nullptr;
299
};
300
301
struct GeometryInstanceLightmapSH {
302
Color sh[9];
303
};
304
305
class GeometryInstanceGLES3 : public RenderGeometryInstanceBase {
306
public:
307
//used during rendering
308
bool store_transform_cache = true;
309
310
// Used for generating motion vectors.
311
Transform3D prev_transform;
312
bool is_prev_transform_stored = false;
313
314
int32_t instance_count = 0;
315
316
bool can_sdfgi = false;
317
bool using_projectors = false;
318
bool using_softshadows = false;
319
320
struct LightPass {
321
int32_t light_id = -1; // Position in the light uniform buffer.
322
int32_t shadow_id = -1; // Position in the shadow uniform buffer.
323
RID light_instance_rid;
324
bool is_omni = false;
325
};
326
327
LocalVector<LightPass> light_passes;
328
329
uint32_t paired_omni_light_count = 0;
330
uint32_t paired_spot_light_count = 0;
331
LocalVector<RID> paired_omni_lights;
332
LocalVector<RID> paired_spot_lights;
333
LocalVector<uint32_t> omni_light_gl_cache;
334
LocalVector<uint32_t> spot_light_gl_cache;
335
336
LocalVector<RID> paired_reflection_probes;
337
LocalVector<RID> reflection_probe_rid_cache;
338
LocalVector<Transform3D> reflection_probes_local_transform_cache;
339
340
RID lightmap_instance;
341
Rect2 lightmap_uv_scale;
342
uint32_t lightmap_slice_index;
343
GeometryInstanceLightmapSH *lightmap_sh = nullptr;
344
345
// Used during setup.
346
GeometryInstanceSurface *surface_caches = nullptr;
347
SelfList<GeometryInstanceGLES3> dirty_list_element;
348
349
GeometryInstanceGLES3() :
350
dirty_list_element(this) {}
351
352
virtual void _mark_dirty() override;
353
virtual void set_use_lightmap(RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) override;
354
virtual void set_lightmap_capture(const Color *p_sh9) override;
355
356
virtual void clear_light_instances() override;
357
virtual void pair_light_instance(const RID p_light_instance, RS::LightType light_type, uint32_t placement_idx) override;
358
virtual void pair_reflection_probe_instances(const RID *p_reflection_probe_instances, uint32_t p_reflection_probe_instance_count) override;
359
virtual void pair_decal_instances(const RID *p_decal_instances, uint32_t p_decal_instance_count) override {}
360
virtual void pair_voxel_gi_instances(const RID *p_voxel_gi_instances, uint32_t p_voxel_gi_instance_count) override {}
361
362
virtual void set_softshadow_projector_pairing(bool p_softshadow, bool p_projector) override {}
363
};
364
365
virtual uint32_t get_max_lights_total() override;
366
virtual uint32_t get_max_lights_per_mesh() override;
367
368
enum {
369
INSTANCE_DATA_FLAGS_DYNAMIC = 1 << 3,
370
INSTANCE_DATA_FLAGS_NON_UNIFORM_SCALE = 1 << 4,
371
INSTANCE_DATA_FLAG_USE_GI_BUFFERS = 1 << 5,
372
INSTANCE_DATA_FLAG_USE_LIGHTMAP_CAPTURE = 1 << 7,
373
INSTANCE_DATA_FLAG_USE_LIGHTMAP = 1 << 8,
374
INSTANCE_DATA_FLAG_USE_SH_LIGHTMAP = 1 << 9,
375
INSTANCE_DATA_FLAG_USE_VOXEL_GI = 1 << 10,
376
INSTANCE_DATA_FLAG_PARTICLES = 1 << 11,
377
INSTANCE_DATA_FLAG_MULTIMESH = 1 << 12,
378
INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D = 1 << 13,
379
INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR = 1 << 14,
380
INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA = 1 << 15,
381
};
382
383
static void _geometry_instance_dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *p_tracker);
384
static void _geometry_instance_dependency_deleted(const RID &p_dependency, DependencyTracker *p_tracker);
385
386
SelfList<GeometryInstanceGLES3>::List geometry_instance_dirty_list;
387
388
// Use PagedAllocator instead of RID to maximize performance
389
PagedAllocator<GeometryInstanceGLES3> geometry_instance_alloc;
390
PagedAllocator<GeometryInstanceSurface> geometry_instance_surface_alloc;
391
392
void _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);
393
void _geometry_instance_add_surface_with_material_chain(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, GLES3::SceneMaterialData *p_material, RID p_mat_src, RID p_mesh);
394
void _geometry_instance_add_surface(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, RID p_material, RID p_mesh);
395
void _geometry_instance_update(RenderGeometryInstance *p_geometry_instance);
396
void _update_dirty_geometry_instances();
397
398
struct SceneState {
399
struct UBO {
400
float projection_matrix[16];
401
float inv_projection_matrix[16];
402
float inv_view_matrix[16];
403
float view_matrix[16];
404
405
float main_cam_inv_view_matrix[16];
406
407
float viewport_size[2];
408
float screen_pixel_size[2];
409
410
float ambient_light_color_energy[4];
411
412
float ambient_color_sky_mix;
413
uint32_t directional_shadow_count;
414
float emissive_exposure_normalization;
415
uint32_t use_ambient_light = 0;
416
417
uint32_t use_ambient_cubemap = 0;
418
uint32_t use_reflection_cubemap = 0;
419
float fog_aerial_perspective;
420
float time;
421
422
float radiance_inverse_xform[12];
423
424
uint32_t directional_light_count;
425
float z_far;
426
float z_near;
427
float IBL_exposure_normalization;
428
429
uint32_t fog_enabled;
430
uint32_t fog_mode;
431
float fog_density;
432
float fog_height;
433
434
float fog_height_density;
435
float fog_depth_curve;
436
float fog_sun_scatter;
437
float fog_depth_begin;
438
439
float fog_light_color[3];
440
float fog_depth_end;
441
442
float shadow_bias;
443
float luminance_multiplier;
444
uint32_t camera_visible_layers;
445
bool pancake_shadows;
446
};
447
static_assert(sizeof(UBO) % 16 == 0, "Scene UBO size must be a multiple of 16 bytes");
448
static_assert(sizeof(UBO) < 16384, "Scene UBO size must be 16384 bytes or smaller");
449
450
struct MultiviewUBO {
451
float projection_matrix_view[RendererSceneRender::MAX_RENDER_VIEWS][16];
452
float inv_projection_matrix_view[RendererSceneRender::MAX_RENDER_VIEWS][16];
453
float eye_offset[RendererSceneRender::MAX_RENDER_VIEWS][4];
454
};
455
static_assert(sizeof(MultiviewUBO) % 16 == 0, "Multiview UBO size must be a multiple of 16 bytes");
456
static_assert(sizeof(MultiviewUBO) < 16384, "MultiviewUBO size must be 16384 bytes or smaller");
457
458
struct TonemapUBO {
459
float exposure = 1.0;
460
int32_t tonemapper = 0;
461
int32_t pad = 0;
462
int32_t pad2 = 0;
463
float tonemapper_params[4] = { 0.0, 0.0, 0.0, 0.0 };
464
float brightness = 1.0;
465
float contrast = 1.0;
466
float saturation = 1.0;
467
int32_t pad3 = 0;
468
};
469
static_assert(sizeof(TonemapUBO) % 16 == 0, "Tonemap UBO size must be a multiple of 16 bytes");
470
471
UBO data;
472
UBO prev_data;
473
GLuint ubo_buffer = 0;
474
GLuint prev_ubo_buffer = 0;
475
MultiviewUBO multiview_data;
476
MultiviewUBO prev_multiview_data;
477
GLuint multiview_buffer = 0;
478
GLuint prev_multiview_buffer = 0;
479
GLuint tonemap_buffer = 0;
480
481
int prev_data_state = 0; // 0 = Motion vectors not used, 1 = use data (first frame only), 2 = use previous data
482
483
bool used_depth_prepass = false;
484
485
GLES3::SceneShaderData::BlendMode current_blend_mode = GLES3::SceneShaderData::BLEND_MODE_MIX;
486
RS::CullMode cull_mode = RS::CULL_MODE_BACK;
487
GLenum current_depth_function = GL_GEQUAL;
488
489
bool current_blend_enabled = false;
490
bool current_depth_draw_enabled = false;
491
bool current_depth_test_enabled = false;
492
bool current_scissor_test_enabled = false;
493
494
void reset_gl_state() {
495
glDisable(GL_BLEND);
496
current_blend_enabled = false;
497
498
glDisable(GL_SCISSOR_TEST);
499
current_scissor_test_enabled = false;
500
501
glCullFace(GL_BACK);
502
glEnable(GL_CULL_FACE);
503
cull_mode = RS::CULL_MODE_BACK;
504
505
glDepthMask(GL_FALSE);
506
current_depth_draw_enabled = false;
507
glDisable(GL_DEPTH_TEST);
508
current_depth_test_enabled = false;
509
510
glDepthFunc(GL_GEQUAL);
511
current_depth_function = GL_GEQUAL;
512
513
glDisable(GL_STENCIL_TEST);
514
current_stencil_test_enabled = false;
515
glStencilMask(255);
516
current_stencil_write_mask = 255;
517
glStencilFunc(GL_ALWAYS, 0, 255);
518
current_stencil_compare = GL_ALWAYS;
519
current_stencil_reference = 0;
520
current_stencil_compare_mask = 255;
521
}
522
523
void set_gl_cull_mode(RS::CullMode p_mode) {
524
if (cull_mode != p_mode) {
525
if (p_mode == RS::CULL_MODE_DISABLED) {
526
glDisable(GL_CULL_FACE);
527
} else {
528
if (cull_mode == RS::CULL_MODE_DISABLED) {
529
// Last time was disabled, so enable and set proper face.
530
glEnable(GL_CULL_FACE);
531
}
532
glCullFace(p_mode == RS::CULL_MODE_FRONT ? GL_FRONT : GL_BACK);
533
}
534
cull_mode = p_mode;
535
}
536
}
537
538
void enable_gl_blend(bool p_enabled) {
539
if (current_blend_enabled != p_enabled) {
540
if (p_enabled) {
541
glEnable(GL_BLEND);
542
} else {
543
glDisable(GL_BLEND);
544
}
545
current_blend_enabled = p_enabled;
546
}
547
}
548
549
void enable_gl_scissor_test(bool p_enabled) {
550
if (current_scissor_test_enabled != p_enabled) {
551
if (p_enabled) {
552
glEnable(GL_SCISSOR_TEST);
553
} else {
554
glDisable(GL_SCISSOR_TEST);
555
}
556
current_scissor_test_enabled = p_enabled;
557
}
558
}
559
560
void enable_gl_depth_draw(bool p_enabled) {
561
if (current_depth_draw_enabled != p_enabled) {
562
glDepthMask(p_enabled ? GL_TRUE : GL_FALSE);
563
current_depth_draw_enabled = p_enabled;
564
}
565
}
566
567
void enable_gl_depth_test(bool p_enabled) {
568
if (current_depth_test_enabled != p_enabled) {
569
if (p_enabled) {
570
glEnable(GL_DEPTH_TEST);
571
} else {
572
glDisable(GL_DEPTH_TEST);
573
}
574
current_depth_test_enabled = p_enabled;
575
}
576
}
577
578
void set_gl_depth_func(GLenum p_depth_func) {
579
if (current_depth_function != p_depth_func) {
580
glDepthFunc(p_depth_func);
581
current_depth_function = p_depth_func;
582
}
583
}
584
585
void enable_gl_stencil_test(bool p_enabled) {
586
if (current_stencil_test_enabled != p_enabled) {
587
if (p_enabled) {
588
glEnable(GL_STENCIL_TEST);
589
} else {
590
glDisable(GL_STENCIL_TEST);
591
}
592
current_stencil_test_enabled = p_enabled;
593
}
594
}
595
596
void set_gl_stencil_func(GLenum p_compare, GLint p_reference, GLenum p_compare_mask) {
597
if (current_stencil_compare != p_compare || current_stencil_reference != p_reference || current_stencil_compare_mask != p_compare_mask) {
598
glStencilFunc(p_compare, p_reference, p_compare_mask);
599
current_stencil_compare = p_compare;
600
current_stencil_reference = p_reference;
601
current_stencil_compare_mask = p_compare_mask;
602
}
603
}
604
605
void set_gl_stencil_write_mask(GLuint p_mask) {
606
if (current_stencil_write_mask != p_mask) {
607
glStencilMask(p_mask);
608
current_stencil_write_mask = p_mask;
609
}
610
}
611
612
void set_gl_stencil_op(GLenum p_op_fail, GLenum p_op_dpfail, GLenum p_op_dppass) {
613
if (current_stencil_op_fail != p_op_fail || current_stencil_op_dpfail != p_op_dpfail || current_stencil_op_dppass != p_op_dppass) {
614
glStencilOp(p_op_fail, p_op_dpfail, p_op_dppass);
615
current_stencil_op_fail = p_op_fail;
616
current_stencil_op_dpfail = p_op_dpfail;
617
current_stencil_op_dppass = p_op_dppass;
618
}
619
}
620
621
GLenum current_stencil_compare = GL_ALWAYS;
622
GLuint current_stencil_compare_mask = 255;
623
GLuint current_stencil_write_mask = 255;
624
GLint current_stencil_reference = 0;
625
GLenum current_stencil_op_fail = GL_KEEP;
626
GLenum current_stencil_op_dpfail = GL_KEEP;
627
GLenum current_stencil_op_dppass = GL_KEEP;
628
bool current_stencil_test_enabled = false;
629
630
bool texscreen_copied = false;
631
bool used_screen_texture = false;
632
bool used_normal_texture = false;
633
bool used_depth_texture = false;
634
bool used_opaque_stencil = false;
635
636
LightData *omni_lights = nullptr;
637
LightData *spot_lights = nullptr;
638
ShadowData *positional_shadows = nullptr;
639
640
InstanceSort<GLES3::LightInstance> *omni_light_sort;
641
InstanceSort<GLES3::LightInstance> *spot_light_sort;
642
GLuint omni_light_buffer = 0;
643
GLuint spot_light_buffer = 0;
644
GLuint positional_shadow_buffer = 0;
645
uint32_t omni_light_count = 0;
646
uint32_t spot_light_count = 0;
647
RS::ShadowQuality positional_shadow_quality = RS::ShadowQuality::SHADOW_QUALITY_SOFT_LOW;
648
649
DirectionalLightData *directional_lights = nullptr;
650
GLuint directional_light_buffer = 0;
651
DirectionalShadowData *directional_shadows = nullptr;
652
GLuint directional_shadow_buffer = 0;
653
RS::ShadowQuality directional_shadow_quality = RS::ShadowQuality::SHADOW_QUALITY_SOFT_LOW;
654
} scene_state;
655
656
struct RenderListParameters {
657
GeometryInstanceSurface **elements = nullptr;
658
int element_count = 0;
659
bool reverse_cull = false;
660
uint64_t spec_constant_base_flags = 0;
661
bool force_wireframe = false;
662
Vector2 uv_offset = Vector2(0, 0);
663
664
RenderListParameters(GeometryInstanceSurface **p_elements, int p_element_count, bool p_reverse_cull, uint64_t p_spec_constant_base_flags, bool p_force_wireframe = false, Vector2 p_uv_offset = Vector2()) {
665
elements = p_elements;
666
element_count = p_element_count;
667
reverse_cull = p_reverse_cull;
668
spec_constant_base_flags = p_spec_constant_base_flags;
669
force_wireframe = p_force_wireframe;
670
uv_offset = p_uv_offset;
671
}
672
};
673
674
struct RenderList {
675
LocalVector<GeometryInstanceSurface *> elements;
676
677
void clear() {
678
elements.clear();
679
}
680
681
//should eventually be replaced by radix
682
683
struct SortByKey {
684
_FORCE_INLINE_ bool operator()(const GeometryInstanceSurface *A, const GeometryInstanceSurface *B) const {
685
return (A->sort.sort_key2 == B->sort.sort_key2) ? (A->sort.sort_key1 < B->sort.sort_key1) : (A->sort.sort_key2 < B->sort.sort_key2);
686
}
687
};
688
689
void sort_by_key() {
690
SortArray<GeometryInstanceSurface *, SortByKey> sorter;
691
sorter.sort(elements.ptr(), elements.size());
692
}
693
694
void sort_by_key_range(uint32_t p_from, uint32_t p_size) {
695
SortArray<GeometryInstanceSurface *, SortByKey> sorter;
696
sorter.sort(elements.ptr() + p_from, p_size);
697
}
698
699
struct SortByDepth {
700
_FORCE_INLINE_ bool operator()(const GeometryInstanceSurface *A, const GeometryInstanceSurface *B) const {
701
return (A->owner->depth < B->owner->depth);
702
}
703
};
704
705
void sort_by_depth() { //used for shadows
706
707
SortArray<GeometryInstanceSurface *, SortByDepth> sorter;
708
sorter.sort(elements.ptr(), elements.size());
709
}
710
711
struct SortByReverseDepthAndPriority {
712
_FORCE_INLINE_ bool operator()(const GeometryInstanceSurface *A, const GeometryInstanceSurface *B) const {
713
return (A->sort.priority == B->sort.priority) ? (A->owner->depth > B->owner->depth) : (A->sort.priority < B->sort.priority);
714
}
715
};
716
717
void sort_by_reverse_depth_and_priority() { //used for alpha
718
719
SortArray<GeometryInstanceSurface *, SortByReverseDepthAndPriority> sorter;
720
sorter.sort(elements.ptr(), elements.size());
721
}
722
723
_FORCE_INLINE_ void add_element(GeometryInstanceSurface *p_element) {
724
elements.push_back(p_element);
725
}
726
};
727
728
RenderList render_list[RENDER_LIST_MAX];
729
730
void _update_scene_ubo(GLuint &p_ubo_buffer, GLuint p_index, uint32_t p_size, const void *p_source_data, String p_name = "");
731
732
void _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);
733
void _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 = 0.0);
734
void _fill_render_list(RenderListType p_render_list, const RenderDataGLES3 *p_render_data, PassMode p_pass_mode, bool p_append = false);
735
void _render_shadows(const RenderDataGLES3 *p_render_data, const Size2i &p_viewport_size = Size2i(1, 1));
736
void _render_shadow_pass(RID p_light, RID p_shadow_atlas, int p_pass, const PagedArray<RenderGeometryInstance *> &p_instances, float p_lod_distance_multiplier = 0, float p_screen_mesh_lod_threshold = 0.0, RenderingMethod::RenderInfo *p_render_info = nullptr, const Size2i &p_viewport_size = Size2i(1, 1), const Transform3D &p_main_cam_transform = Transform3D());
737
void _render_post_processing(const RenderDataGLES3 *p_render_data);
738
739
template <PassMode p_pass_mode>
740
_FORCE_INLINE_ void _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 = false);
741
742
protected:
743
double time;
744
double time_step = 0;
745
746
bool screen_space_roughness_limiter = false;
747
float screen_space_roughness_limiter_amount = 0.25;
748
float screen_space_roughness_limiter_limit = 0.18;
749
750
void _render_buffers_debug_draw(Ref<RenderSceneBuffersGLES3> p_render_buffers, RID p_shadow_atlas, GLuint p_fbo);
751
752
/* Camera Attributes */
753
754
struct CameraAttributes {
755
float exposure_multiplier = 1.0;
756
float exposure_normalization = 1.0;
757
};
758
759
bool use_physical_light_units = false;
760
mutable RID_Owner<CameraAttributes, true> camera_attributes_owner;
761
762
/* Environment */
763
764
RS::EnvironmentSSAOQuality ssao_quality = RS::ENV_SSAO_QUALITY_MEDIUM;
765
bool ssao_half_size = false;
766
float ssao_adaptive_target = 0.5;
767
int ssao_blur_passes = 2;
768
float ssao_fadeout_from = 50.0;
769
float ssao_fadeout_to = 300.0;
770
771
bool glow_bicubic_upscale = false;
772
773
bool lightmap_bicubic_upscale = false;
774
775
/* Sky */
776
777
struct SkyGlobals {
778
float fog_aerial_perspective = 0.0;
779
Color fog_light_color;
780
float fog_sun_scatter = 0.0;
781
bool fog_enabled = false;
782
float fog_density = 0.0;
783
float z_far = 0.0;
784
uint32_t directional_light_count = 0;
785
786
DirectionalLightData *directional_lights = nullptr;
787
DirectionalLightData *last_frame_directional_lights = nullptr;
788
uint32_t last_frame_directional_light_count = 0;
789
GLuint directional_light_buffer = 0;
790
791
RID shader_default_version;
792
RID default_material;
793
RID default_shader;
794
RID fog_material;
795
RID fog_shader;
796
GLuint screen_triangle = 0;
797
GLuint screen_triangle_array = 0;
798
uint32_t max_directional_lights = 4;
799
uint32_t roughness_layers = 8;
800
} sky_globals;
801
802
struct Sky {
803
// Screen Buffers
804
GLuint half_res_pass = 0;
805
GLuint half_res_framebuffer = 0;
806
GLuint quarter_res_pass = 0;
807
GLuint quarter_res_framebuffer = 0;
808
Size2i screen_size = Size2i(0, 0);
809
810
// Radiance Cubemap
811
GLuint radiance = 0;
812
GLuint radiance_framebuffer = 0;
813
GLuint raw_radiance = 0;
814
815
RID material;
816
GLuint uniform_buffer;
817
818
int radiance_size = 256;
819
int mipmap_count = 1;
820
821
RS::SkyMode mode = RS::SKY_MODE_AUTOMATIC;
822
823
//ReflectionData reflection;
824
bool reflection_dirty = false;
825
bool dirty = false;
826
int processing_layer = 0;
827
Sky *dirty_list = nullptr;
828
float baked_exposure = 1.0;
829
830
//State to track when radiance cubemap needs updating
831
GLES3::SkyMaterialData *prev_material = nullptr;
832
Vector3 prev_position = Vector3(0.0, 0.0, 0.0);
833
float prev_time = 0.0f;
834
};
835
836
Sky *dirty_sky_list = nullptr;
837
mutable RID_Owner<Sky, true> sky_owner;
838
839
void _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);
840
void _invalidate_sky(Sky *p_sky);
841
void _update_dirty_skys();
842
void _update_sky_radiance(RID p_env, const Projection &p_projection, const Transform3D &p_transform, float p_sky_energy_multiplier);
843
void _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);
844
void _free_sky_data(Sky *p_sky);
845
846
// Needed for a single argument calls (material and uv2).
847
PagedArrayPool<RenderGeometryInstance *> cull_argument_pool;
848
PagedArray<RenderGeometryInstance *> cull_argument;
849
850
public:
851
static RasterizerSceneGLES3 *get_singleton() { return singleton; }
852
853
RasterizerCanvasGLES3 *canvas = nullptr;
854
855
RenderGeometryInstance *geometry_instance_create(RID p_base) override;
856
void geometry_instance_free(RenderGeometryInstance *p_geometry_instance) override;
857
858
uint32_t geometry_instance_get_pair_mask() override;
859
860
/* PIPELINES */
861
862
virtual void mesh_generate_pipelines(RID p_mesh, bool p_background_compilation) override {}
863
virtual uint32_t get_pipeline_compilations(RS::PipelineSource p_source) override { return 0; }
864
865
/* SDFGI UPDATE */
866
867
void sdfgi_update(const Ref<RenderSceneBuffers> &p_render_buffers, RID p_environment, const Vector3 &p_world_position) override {}
868
int sdfgi_get_pending_region_count(const Ref<RenderSceneBuffers> &p_render_buffers) const override {
869
return 0;
870
}
871
AABB sdfgi_get_pending_region_bounds(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const override {
872
return AABB();
873
}
874
uint32_t sdfgi_get_pending_region_cascade(const Ref<RenderSceneBuffers> &p_render_buffers, int p_region) const override {
875
return 0;
876
}
877
878
/* SKY API */
879
880
RID sky_allocate() override;
881
void sky_initialize(RID p_rid) override;
882
void sky_set_radiance_size(RID p_sky, int p_radiance_size) override;
883
void sky_set_mode(RID p_sky, RS::SkyMode p_mode) override;
884
void sky_set_material(RID p_sky, RID p_material) override;
885
Ref<Image> sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) override;
886
float sky_get_baked_exposure(RID p_sky) const;
887
888
/* ENVIRONMENT API */
889
890
void environment_glow_set_use_bicubic_upscale(bool p_enable) override;
891
892
void environment_set_ssr_half_size(bool p_half_size) override;
893
void environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) override;
894
895
void 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) override;
896
897
void 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) override;
898
899
void environment_set_sdfgi_ray_count(RS::EnvironmentSDFGIRayCount p_ray_count) override;
900
void environment_set_sdfgi_frames_to_converge(RS::EnvironmentSDFGIFramesToConverge p_frames) override;
901
void environment_set_sdfgi_frames_to_update_light(RS::EnvironmentSDFGIFramesToUpdateLight p_update) override;
902
903
void environment_set_volumetric_fog_volume_size(int p_size, int p_depth) override;
904
void environment_set_volumetric_fog_filter_active(bool p_enable) override;
905
906
Ref<Image> environment_bake_panorama(RID p_env, bool p_bake_irradiance, const Size2i &p_size) override;
907
908
_FORCE_INLINE_ bool is_using_physical_light_units() {
909
return use_physical_light_units;
910
}
911
912
void positional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) override;
913
void directional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) override;
914
915
RID fog_volume_instance_create(RID p_fog_volume) override;
916
void fog_volume_instance_set_transform(RID p_fog_volume_instance, const Transform3D &p_transform) override;
917
void fog_volume_instance_set_active(RID p_fog_volume_instance, bool p_active) override;
918
RID fog_volume_instance_get_volume(RID p_fog_volume_instance) const override;
919
Vector3 fog_volume_instance_get_position(RID p_fog_volume_instance) const override;
920
921
RID voxel_gi_instance_create(RID p_voxel_gi) override;
922
void voxel_gi_instance_set_transform_to_data(RID p_probe, const Transform3D &p_xform) override;
923
bool voxel_gi_needs_update(RID p_probe) const override;
924
void voxel_gi_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RenderGeometryInstance *> &p_dynamic_objects) override;
925
926
void voxel_gi_set_quality(RS::VoxelGIQuality) override;
927
928
void 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, float p_window_output_max_value, const RenderSDFGIUpdateData *p_sdfgi_update_data = nullptr, RenderingMethod::RenderInfo *r_render_info = nullptr) override;
929
void 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) override;
930
void render_particle_collider_heightfield(RID p_collider, const Transform3D &p_transform, const PagedArray<RenderGeometryInstance *> &p_instances) override;
931
932
void set_scene_pass(uint64_t p_pass) override {
933
scene_pass = p_pass;
934
}
935
936
_FORCE_INLINE_ uint64_t get_scene_pass() {
937
return scene_pass;
938
}
939
940
void set_time(double p_time, double p_step) override;
941
void set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) override;
942
_FORCE_INLINE_ RS::ViewportDebugDraw get_debug_draw_mode() const {
943
return debug_draw;
944
}
945
946
Ref<RenderSceneBuffers> render_buffers_create() override;
947
void gi_set_use_half_resolution(bool p_enable) override;
948
949
void screen_space_roughness_limiter_set_active(bool p_enable, float p_amount, float p_curve) override;
950
bool screen_space_roughness_limiter_is_active() const override;
951
952
void sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality) override;
953
void sub_surface_scattering_set_scale(float p_scale, float p_depth_scale) override;
954
955
TypedArray<Image> bake_render_uv2(RID p_base, const TypedArray<RID> &p_material_overrides, const Size2i &p_image_size) override;
956
void _render_uv2(const PagedArray<RenderGeometryInstance *> &p_instances, GLuint p_framebuffer, const Rect2i &p_region);
957
958
bool free(RID p_rid) override;
959
void update() override;
960
void sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) override;
961
962
void decals_set_filter(RS::DecalFilter p_filter) override;
963
void light_projectors_set_filter(RS::LightProjectorFilter p_filter) override;
964
virtual void lightmaps_set_bicubic_filter(bool p_enable) override;
965
virtual void material_set_use_debanding(bool p_enable) override;
966
967
RasterizerSceneGLES3();
968
~RasterizerSceneGLES3();
969
};
970
971
#endif // GLES3_ENABLED
972
973