diff options
Diffstat (limited to 'drivers/gles3/rasterizer_scene_gles3.cpp')
-rw-r--r-- | drivers/gles3/rasterizer_scene_gles3.cpp | 1196 |
1 files changed, 1148 insertions, 48 deletions
diff --git a/drivers/gles3/rasterizer_scene_gles3.cpp b/drivers/gles3/rasterizer_scene_gles3.cpp index 09a02b786c..68657b9152 100644 --- a/drivers/gles3/rasterizer_scene_gles3.cpp +++ b/drivers/gles3/rasterizer_scene_gles3.cpp @@ -31,6 +31,7 @@ #include "rasterizer_scene_gles3.h" #include "core/config/project_settings.h" #include "servers/rendering/rendering_server_default.h" +#include "storage/config.h" #ifdef GLES3_ENABLED @@ -42,66 +43,169 @@ RasterizerSceneGLES3 *RasterizerSceneGLES3::get_singleton() { return singleton; } -RasterizerSceneGLES3::GeometryInstance *RasterizerSceneGLES3::geometry_instance_create(RID p_base) { - return nullptr; +RendererSceneRender::GeometryInstance *RasterizerSceneGLES3::geometry_instance_create(RID p_base) { + RS::InstanceType type = storage->get_base_type(p_base); + ERR_FAIL_COND_V(!((1 << type) & RS::INSTANCE_GEOMETRY_MASK), nullptr); + + GeometryInstanceGLES3 *ginstance = geometry_instance_alloc.alloc(); + ginstance->data = memnew(GeometryInstanceGLES3::Data); + + ginstance->data->base = p_base; + ginstance->data->base_type = type; + + _geometry_instance_mark_dirty(ginstance); + + return ginstance; } void RasterizerSceneGLES3::geometry_instance_set_skeleton(GeometryInstance *p_geometry_instance, RID p_skeleton) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->data->skeleton = p_skeleton; + + _geometry_instance_mark_dirty(ginstance); + ginstance->data->dirty_dependencies = true; } void RasterizerSceneGLES3::geometry_instance_set_material_override(GeometryInstance *p_geometry_instance, RID p_override) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->data->material_override = p_override; + + _geometry_instance_mark_dirty(ginstance); + ginstance->data->dirty_dependencies = true; } void RasterizerSceneGLES3::geometry_instance_set_material_overlay(GeometryInstance *p_geometry_instance, RID p_overlay) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->data->material_overlay = p_overlay; + + _geometry_instance_mark_dirty(ginstance); + ginstance->data->dirty_dependencies = true; } -void RasterizerSceneGLES3::geometry_instance_set_surface_materials(GeometryInstance *p_geometry_instance, const Vector<RID> &p_material) { +void RasterizerSceneGLES3::geometry_instance_set_surface_materials(GeometryInstance *p_geometry_instance, const Vector<RID> &p_materials) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->data->surface_materials = p_materials; + + _geometry_instance_mark_dirty(ginstance); + ginstance->data->dirty_dependencies = true; } void RasterizerSceneGLES3::geometry_instance_set_mesh_instance(GeometryInstance *p_geometry_instance, RID p_mesh_instance) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ERR_FAIL_COND(!ginstance); + ginstance->mesh_instance = p_mesh_instance; + + _geometry_instance_mark_dirty(ginstance); } -void RasterizerSceneGLES3::geometry_instance_set_transform(GeometryInstance *p_geometry_instance, const Transform3D &p_transform, const AABB &p_aabb, const AABB &p_transformed_aabbb) { +void RasterizerSceneGLES3::geometry_instance_set_transform(GeometryInstance *p_geometry_instance, const Transform3D &p_transform, const AABB &p_aabb, const AABB &p_transformed_aabb) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->transform = p_transform; + ginstance->mirror = p_transform.basis.determinant() < 0; + ginstance->data->aabb = p_aabb; + ginstance->transformed_aabb = p_transformed_aabb; + + Vector3 model_scale_vec = p_transform.basis.get_scale_abs(); + // handle non uniform scale here + + float max_scale = MAX(model_scale_vec.x, MAX(model_scale_vec.y, model_scale_vec.z)); + float min_scale = MIN(model_scale_vec.x, MIN(model_scale_vec.y, model_scale_vec.z)); + ginstance->non_uniform_scale = max_scale >= 0.0 && (min_scale / max_scale) < 0.9; + + ginstance->lod_model_scale = max_scale; } void RasterizerSceneGLES3::geometry_instance_set_layer_mask(GeometryInstance *p_geometry_instance, uint32_t p_layer_mask) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->layer_mask = p_layer_mask; } void RasterizerSceneGLES3::geometry_instance_set_lod_bias(GeometryInstance *p_geometry_instance, float p_lod_bias) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->lod_bias = p_lod_bias; } void RasterizerSceneGLES3::geometry_instance_set_transparency(GeometryInstance *p_geometry_instance, float p_transparency) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->force_alpha = CLAMP(1.0 - p_transparency, 0, 1); } void RasterizerSceneGLES3::geometry_instance_set_fade_range(GeometryInstance *p_geometry_instance, bool p_enable_near, float p_near_begin, float p_near_end, bool p_enable_far, float p_far_begin, float p_far_end) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->fade_near = p_enable_near; + ginstance->fade_near_begin = p_near_begin; + ginstance->fade_near_end = p_near_end; + ginstance->fade_far = p_enable_far; + ginstance->fade_far_begin = p_far_begin; + ginstance->fade_far_end = p_far_end; } void RasterizerSceneGLES3::geometry_instance_set_parent_fade_alpha(GeometryInstance *p_geometry_instance, float p_alpha) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->parent_fade_alpha = p_alpha; } void RasterizerSceneGLES3::geometry_instance_set_use_baked_light(GeometryInstance *p_geometry_instance, bool p_enable) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->data->use_baked_light = p_enable; + + _geometry_instance_mark_dirty(ginstance); } void RasterizerSceneGLES3::geometry_instance_set_use_dynamic_gi(GeometryInstance *p_geometry_instance, bool p_enable) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->data->use_dynamic_gi = p_enable; + _geometry_instance_mark_dirty(ginstance); } void RasterizerSceneGLES3::geometry_instance_set_use_lightmap(GeometryInstance *p_geometry_instance, RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); } void RasterizerSceneGLES3::geometry_instance_set_lightmap_capture(GeometryInstance *p_geometry_instance, const Color *p_sh9) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); } void RasterizerSceneGLES3::geometry_instance_set_instance_shader_parameters_offset(GeometryInstance *p_geometry_instance, int32_t p_offset) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->shader_parameters_offset = p_offset; + _geometry_instance_mark_dirty(ginstance); } void RasterizerSceneGLES3::geometry_instance_set_cast_double_sided_shadows(GeometryInstance *p_geometry_instance, bool p_enable) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + ginstance->data->cast_double_sided_shadows = p_enable; + _geometry_instance_mark_dirty(ginstance); } uint32_t RasterizerSceneGLES3::geometry_instance_get_pair_mask() { - return 0; + return 0; //(1 << RS::INSTANCE_LIGHT); + // For now, nothing is paired } void RasterizerSceneGLES3::geometry_instance_pair_light_instances(GeometryInstance *p_geometry_instance, const RID *p_light_instances, uint32_t p_light_instance_count) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + + ginstance->omni_light_count = 0; + ginstance->spot_light_count = 0; } void RasterizerSceneGLES3::geometry_instance_pair_reflection_probe_instances(GeometryInstance *p_geometry_instance, const RID *p_reflection_probe_instances, uint32_t p_reflection_probe_instance_count) { @@ -114,9 +218,314 @@ void RasterizerSceneGLES3::geometry_instance_pair_voxel_gi_instances(GeometryIns } void RasterizerSceneGLES3::geometry_instance_set_softshadow_projector_pairing(GeometryInstance *p_geometry_instance, bool p_softshadow, bool p_projector) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); } void RasterizerSceneGLES3::geometry_instance_free(GeometryInstance *p_geometry_instance) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + ERR_FAIL_COND(!ginstance); + GeometryInstanceSurface *surf = ginstance->surface_caches; + while (surf) { + GeometryInstanceSurface *next = surf->next; + geometry_instance_surface_alloc.free(surf); + surf = next; + } + memdelete(ginstance->data); + geometry_instance_alloc.free(ginstance); +} + +void RasterizerSceneGLES3::_geometry_instance_mark_dirty(GeometryInstance *p_geometry_instance) { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + if (ginstance->dirty_list_element.in_list()) { + return; + } + + //clear surface caches + GeometryInstanceSurface *surf = ginstance->surface_caches; + + while (surf) { + GeometryInstanceSurface *next = surf->next; + geometry_instance_surface_alloc.free(surf); + surf = next; + } + + ginstance->surface_caches = nullptr; + + geometry_instance_dirty_list.add(&ginstance->dirty_list_element); +} + +void RasterizerSceneGLES3::_update_dirty_geometry_instances() { + while (geometry_instance_dirty_list.first()) { + _geometry_instance_update(geometry_instance_dirty_list.first()->self()); + } +} + +void RasterizerSceneGLES3::_geometry_instance_dependency_changed(RendererStorage::DependencyChangedNotification p_notification, RendererStorage::DependencyTracker *p_tracker) { + switch (p_notification) { + case RendererStorage::DEPENDENCY_CHANGED_MATERIAL: + case RendererStorage::DEPENDENCY_CHANGED_MESH: + case RendererStorage::DEPENDENCY_CHANGED_PARTICLES: + case RendererStorage::DEPENDENCY_CHANGED_MULTIMESH: + case RendererStorage::DEPENDENCY_CHANGED_SKELETON_DATA: { + static_cast<RasterizerSceneGLES3 *>(singleton)->_geometry_instance_mark_dirty(static_cast<GeometryInstance *>(p_tracker->userdata)); + } break; + case RendererStorage::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES: { + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_tracker->userdata); + if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) { + ginstance->instance_count = GLES3::MeshStorage::get_singleton()->multimesh_get_instances_to_draw(ginstance->data->base); + } + } break; + default: { + //rest of notifications of no interest + } break; + } +} + +void RasterizerSceneGLES3::_geometry_instance_dependency_deleted(const RID &p_dependency, RendererStorage::DependencyTracker *p_tracker) { + static_cast<RasterizerSceneGLES3 *>(singleton)->_geometry_instance_mark_dirty(static_cast<GeometryInstance *>(p_tracker->userdata)); +} + +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) { + GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton(); + + 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; + bool has_base_alpha = ((p_material->shader_data->uses_alpha && !p_material->shader_data->uses_alpha_clip) || has_read_screen_alpha); + bool has_blend_alpha = p_material->shader_data->uses_blend_alpha; + bool has_alpha = has_base_alpha || has_blend_alpha; + + uint32_t flags = 0; + + if (p_material->shader_data->uses_screen_texture) { + flags |= GeometryInstanceSurface::FLAG_USES_SCREEN_TEXTURE; + } + + if (p_material->shader_data->uses_depth_texture) { + flags |= GeometryInstanceSurface::FLAG_USES_DEPTH_TEXTURE; + } + + if (p_material->shader_data->uses_normal_texture) { + flags |= GeometryInstanceSurface::FLAG_USES_NORMAL_TEXTURE; + } + + if (ginstance->data->cast_double_sided_shadows) { + flags |= GeometryInstanceSurface::FLAG_USES_DOUBLE_SIDED_SHADOWS; + } + + if (has_alpha || has_read_screen_alpha || p_material->shader_data->depth_draw == GLES3::SceneShaderData::DEPTH_DRAW_DISABLED || p_material->shader_data->depth_test == GLES3::SceneShaderData::DEPTH_TEST_DISABLED) { + //material is only meant for alpha pass + flags |= GeometryInstanceSurface::FLAG_PASS_ALPHA; + if (p_material->shader_data->uses_depth_pre_pass && !(p_material->shader_data->depth_draw == GLES3::SceneShaderData::DEPTH_DRAW_DISABLED || p_material->shader_data->depth_test == GLES3::SceneShaderData::DEPTH_TEST_DISABLED)) { + flags |= GeometryInstanceSurface::FLAG_PASS_DEPTH; + flags |= GeometryInstanceSurface::FLAG_PASS_SHADOW; + } + } else { + flags |= GeometryInstanceSurface::FLAG_PASS_OPAQUE; + flags |= GeometryInstanceSurface::FLAG_PASS_DEPTH; + flags |= GeometryInstanceSurface::FLAG_PASS_SHADOW; + } + + GLES3::SceneMaterialData *material_shadow = nullptr; + void *surface_shadow = nullptr; + 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_pre_pass && !p_material->shader_data->uses_alpha_clip) { + flags |= GeometryInstanceSurface::FLAG_USES_SHARED_SHADOW_MATERIAL; + material_shadow = static_cast<GLES3::SceneMaterialData *>(GLES3::MaterialStorage::get_singleton()->material_get_data(scene_globals.default_material, RS::SHADER_SPATIAL)); + + RID shadow_mesh = mesh_storage->mesh_get_shadow_mesh(p_mesh); + + if (shadow_mesh.is_valid()) { + surface_shadow = mesh_storage->mesh_get_surface(shadow_mesh, p_surface); + } + + } else { + material_shadow = p_material; + } + + GeometryInstanceSurface *sdcache = geometry_instance_surface_alloc.alloc(); + + sdcache->flags = flags; + + sdcache->shader = p_material->shader_data; + sdcache->material = p_material; + sdcache->surface = mesh_storage->mesh_get_surface(p_mesh, p_surface); + sdcache->primitive = mesh_storage->mesh_surface_get_primitive(sdcache->surface); + sdcache->surface_index = p_surface; + + if (ginstance->data->dirty_dependencies) { + storage->base_update_dependency(p_mesh, &ginstance->data->dependency_tracker); + } + + //shadow + sdcache->shader_shadow = material_shadow->shader_data; + sdcache->material_shadow = material_shadow; + + sdcache->surface_shadow = surface_shadow ? surface_shadow : sdcache->surface; + + sdcache->owner = ginstance; + + sdcache->next = ginstance->surface_caches; + ginstance->surface_caches = sdcache; + + //sortkey + + sdcache->sort.sort_key1 = 0; + sdcache->sort.sort_key2 = 0; + + sdcache->sort.surface_index = p_surface; + sdcache->sort.material_id_low = p_material_id & 0x0000FFFF; + sdcache->sort.material_id_hi = p_material_id >> 16; + sdcache->sort.shader_id = p_shader_id; + sdcache->sort.geometry_id = p_mesh.get_local_index(); + sdcache->sort.priority = p_material->priority; +} + +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) { + GLES3::SceneMaterialData *material_data = p_material_data; + GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton(); + + _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); + + while (material_data->next_pass.is_valid()) { + RID next_pass = material_data->next_pass; + material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(next_pass, RS::SHADER_SPATIAL)); + if (!material_data || !material_data->shader_data->valid) { + break; + } + if (ginstance->data->dirty_dependencies) { + material_storage->material_update_dependency(next_pass, &ginstance->data->dependency_tracker); + } + _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); + } +} + +void RasterizerSceneGLES3::_geometry_instance_add_surface(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, RID p_material, RID p_mesh) { + GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton(); + RID m_src; + + m_src = ginstance->data->material_override.is_valid() ? ginstance->data->material_override : p_material; + + GLES3::SceneMaterialData *material_data = nullptr; + + if (m_src.is_valid()) { + material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(m_src, RS::SHADER_SPATIAL)); + if (!material_data || !material_data->shader_data->valid) { + material_data = nullptr; + } + } + + if (material_data) { + if (ginstance->data->dirty_dependencies) { + material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker); + } + } else { + material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(scene_globals.default_material, RS::SHADER_SPATIAL)); + m_src = scene_globals.default_material; + } + + ERR_FAIL_COND(!material_data); + + _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material_data, m_src, p_mesh); + + if (ginstance->data->material_overlay.is_valid()) { + m_src = ginstance->data->material_overlay; + + material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(m_src, RS::SHADER_SPATIAL)); + if (material_data && material_data->shader_data->valid) { + if (ginstance->data->dirty_dependencies) { + material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker); + } + + _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material_data, m_src, p_mesh); + } + } +} + +void RasterizerSceneGLES3::_geometry_instance_update(GeometryInstance *p_geometry_instance) { + GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton(); + GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance); + + if (ginstance->data->dirty_dependencies) { + ginstance->data->dependency_tracker.update_begin(); + } + + //add geometry for drawing + switch (ginstance->data->base_type) { + case RS::INSTANCE_MESH: { + const RID *materials = nullptr; + uint32_t surface_count; + RID mesh = ginstance->data->base; + + materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count); + if (materials) { + //if no materials, no surfaces. + const RID *inst_materials = ginstance->data->surface_materials.ptr(); + uint32_t surf_mat_count = ginstance->data->surface_materials.size(); + + for (uint32_t j = 0; j < surface_count; j++) { + RID material = (j < surf_mat_count && inst_materials[j].is_valid()) ? inst_materials[j] : materials[j]; + _geometry_instance_add_surface(ginstance, j, material, mesh); + } + } + + ginstance->instance_count = 1; + + } break; + + case RS::INSTANCE_MULTIMESH: { + RID mesh = mesh_storage->multimesh_get_mesh(ginstance->data->base); + if (mesh.is_valid()) { + const RID *materials = nullptr; + uint32_t surface_count; + + materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count); + if (materials) { + for (uint32_t j = 0; j < surface_count; j++) { + _geometry_instance_add_surface(ginstance, j, materials[j], mesh); + } + } + + ginstance->instance_count = mesh_storage->multimesh_get_instances_to_draw(ginstance->data->base); + } + + } break; + case RS::INSTANCE_PARTICLES: { + } break; + + default: { + } + } + + //Fill push constant + + bool store_transform = true; + ginstance->base_flags = 0; + + if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) { + ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH; + if (mesh_storage->multimesh_get_transform_format(ginstance->data->base) == RS::MULTIMESH_TRANSFORM_2D) { + ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D; + } + if (mesh_storage->multimesh_uses_colors(ginstance->data->base)) { + ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR; + } + if (mesh_storage->multimesh_uses_custom_data(ginstance->data->base)) { + ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA; + } + + //ginstance->transforms_uniform_set = mesh_storage->multimesh_get_3d_uniform_set(ginstance->data->base, scene_globals.default_shader_rd, TRANSFORMS_UNIFORM_SET); + + } else if (ginstance->data->base_type == RS::INSTANCE_PARTICLES) { + } else if (ginstance->data->base_type == RS::INSTANCE_MESH) { + } + + ginstance->store_transform_cache = store_transform; + + if (ginstance->data->dirty_dependencies) { + ginstance->data->dependency_tracker.update_end(); + ginstance->data->dirty_dependencies = false; + } + + ginstance->dirty_list_element.remove_from_list(); } /* SHADOW ATLAS API */ @@ -238,36 +647,70 @@ void RasterizerSceneGLES3::_update_dirty_skys() { dirty_sky_list = nullptr; } -void RasterizerSceneGLES3::_draw_sky(Sky *p_sky, const CameraMatrix &p_projection, const Transform3D &p_transform, float p_custom_fov, float p_energy, const Basis &p_sky_orientation) { - ERR_FAIL_COND(!p_sky); +void RasterizerSceneGLES3::_draw_sky(Environment *p_env, const CameraMatrix &p_projection, const Transform3D &p_transform) { + GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton(); + ERR_FAIL_COND(!p_env); - glDepthMask(GL_TRUE); - glEnable(GL_DEPTH_TEST); - glDisable(GL_CULL_FACE); - glDisable(GL_BLEND); - glDepthFunc(GL_LEQUAL); - glColorMask(1, 1, 1, 1); + Sky *sky = sky_owner.get_or_null(p_env->sky); + ERR_FAIL_COND(!sky); + + GLES3::SkyMaterialData *material_data = nullptr; + RID sky_material; + + RS::EnvironmentBG background = p_env->background; + + if (sky) { + ERR_FAIL_COND(!sky); + sky_material = sky->material; + + if (sky_material.is_valid()) { + material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY)); + if (!material_data || !material_data->shader_data->valid) { + material_data = nullptr; + } + } + + if (!material_data) { + sky_material = sky_globals.default_material; + material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY)); + } + } else if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) { + sky_material = sky_globals.fog_material; + material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY)); + } + + ERR_FAIL_COND(!material_data); + material_data->bind_uniforms(); + + GLES3::SkyShaderData *shader_data = material_data->shader_data; - //state.sky_shader.version_bind_shader(sky_globals.default_shader, SkyShaderGLES3::MODE_BACKGROUND); - //glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.canvas_instance_data_buffers[state.current_buffer]); // Canvas data updated here - //glBindBufferBase(GL_UNIFORM_BUFFER, 1, state.canvas_instance_data_buffers[state.current_buffer]); // Global data - //glBindBufferBase(GL_UNIFORM_BUFFER, 2, state.canvas_instance_data_buffers[state.current_buffer]); // Directional light data - //glBindBufferBase(GL_UNIFORM_BUFFER, 3, state.canvas_instance_data_buffers[state.current_buffer]); // Material uniforms + ERR_FAIL_COND(!shader_data); + + //glBindBufferBase(GL_UNIFORM_BUFFER, 2, p_sky.directional light data); // Directional light data // Camera CameraMatrix camera; - if (p_custom_fov) { + if (p_env->sky_custom_fov) { float near_plane = p_projection.get_z_near(); float far_plane = p_projection.get_z_far(); float aspect = p_projection.get_aspect(); - camera.set_perspective(p_custom_fov, aspect, near_plane, far_plane); - + camera.set_perspective(p_env->sky_custom_fov, aspect, near_plane, far_plane); } else { camera = p_projection; } - + Basis sky_transform = p_env->sky_orientation; + sky_transform.invert(); + sky_transform = p_transform.basis * sky_transform; + + GLES3::MaterialStorage::get_singleton()->shaders.sky_shader.version_bind_shader(shader_data->version, SkyShaderGLES3::MODE_BACKGROUND); + GLES3::MaterialStorage::get_singleton()->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::ORIENTATION, sky_transform, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND); + GLES3::MaterialStorage::get_singleton()->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::PROJECTION, camera.matrix[2][0], camera.matrix[0][0], camera.matrix[2][1], camera.matrix[1][1], shader_data->version, SkyShaderGLES3::MODE_BACKGROUND); + GLES3::MaterialStorage::get_singleton()->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::POSITION, p_transform.origin, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND); + GLES3::MaterialStorage::get_singleton()->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::TIME, time, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND); + // Bind a vertex array or else OpenGL complains. We won't actually use it + glBindVertexArray(sky_globals.quad_array); glDrawArrays(GL_TRIANGLES, 0, 3); } @@ -599,20 +1042,267 @@ void RasterizerSceneGLES3::voxel_gi_update(RID p_probe, bool p_update_light_inst void RasterizerSceneGLES3::voxel_gi_set_quality(RS::VoxelGIQuality) { } +void RasterizerSceneGLES3::_fill_render_list(RenderListType p_render_list, const RenderDataGLES3 *p_render_data, PassMode p_pass_mode, bool p_append) { + GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton(); + + if (p_render_list == RENDER_LIST_OPAQUE) { + scene_state.used_screen_texture = false; + scene_state.used_normal_texture = false; + scene_state.used_depth_texture = false; + } + + Plane near_plane; + if (p_render_data->cam_orthogonal) { + near_plane = Plane(-p_render_data->cam_transform.basis.get_column(Vector3::AXIS_Z), p_render_data->cam_transform.origin); + near_plane.d += p_render_data->cam_projection.get_z_near(); + } + float z_max = p_render_data->cam_projection.get_z_far() - p_render_data->cam_projection.get_z_near(); + + RenderList *rl = &render_list[p_render_list]; + + // Parse any updates on our geometry, updates surface caches and such + _update_dirty_geometry_instances(); + + if (!p_append) { + rl->clear(); + if (p_render_list == RENDER_LIST_OPAQUE) { + render_list[RENDER_LIST_ALPHA].clear(); //opaque fills alpha too + } + } + + //fill list + + for (int i = 0; i < (int)p_render_data->instances->size(); i++) { + GeometryInstanceGLES3 *inst = static_cast<GeometryInstanceGLES3 *>((*p_render_data->instances)[i]); + + if (p_render_data->cam_orthogonal) { + Vector3 support_min = inst->transformed_aabb.get_support(-near_plane.normal); + inst->depth = near_plane.distance_to(support_min); + } else { + Vector3 aabb_center = inst->transformed_aabb.position + (inst->transformed_aabb.size * 0.5); + inst->depth = p_render_data->cam_transform.origin.distance_to(aabb_center); + } + uint32_t depth_layer = CLAMP(int(inst->depth * 16 / z_max), 0, 15); + + uint32_t flags = inst->base_flags; //fill flags if appropriate + + if (inst->non_uniform_scale) { + flags |= INSTANCE_DATA_FLAGS_NON_UNIFORM_SCALE; + } + + //Process lights here, determine if they need extra passes + if (p_pass_mode == PASS_MODE_COLOR) { + } + + inst->flags_cache = flags; + + GeometryInstanceSurface *surf = inst->surface_caches; + + while (surf) { + // LOD + + if (p_render_data->screen_mesh_lod_threshold > 0.0 && mesh_storage->mesh_surface_has_lod(surf->surface)) { + //lod + Vector3 lod_support_min = inst->transformed_aabb.get_support(-p_render_data->lod_camera_plane.normal); + Vector3 lod_support_max = inst->transformed_aabb.get_support(p_render_data->lod_camera_plane.normal); + + float distance_min = p_render_data->lod_camera_plane.distance_to(lod_support_min); + float distance_max = p_render_data->lod_camera_plane.distance_to(lod_support_max); + + float distance = 0.0; + + if (distance_min * distance_max < 0.0) { + //crossing plane + distance = 0.0; + } else if (distance_min >= 0.0) { + distance = distance_min; + } else if (distance_max <= 0.0) { + distance = -distance_max; + } + + if (p_render_data->cam_orthogonal) { + distance = 1.0; + } + + uint32_t indices; + surf->lod_index = mesh_storage->mesh_surface_get_lod(surf->surface, inst->lod_model_scale * inst->lod_bias, distance * p_render_data->lod_distance_multiplier, p_render_data->screen_mesh_lod_threshold, &indices); + /* + if (p_render_data->render_info) { + indices = _indices_to_primitives(surf->primitive, indices); + if (p_render_list == RENDER_LIST_OPAQUE) { //opaque + p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices; + } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow + p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices; + } + } + */ + } else { + surf->lod_index = 0; + /* + if (p_render_data->render_info) { + uint32_t to_draw = mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface); + to_draw = _indices_to_primitives(surf->primitive, to_draw); + to_draw *= inst->instance_count; + if (p_render_list == RENDER_LIST_OPAQUE) { //opaque + p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface); + } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow + p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface); + } + } + */ + } + + // ADD Element + if (p_pass_mode == PASS_MODE_COLOR) { +#ifdef DEBUG_ENABLED + bool force_alpha = unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW); +#else + bool force_alpha = false; +#endif + if (!force_alpha && (surf->flags & GeometryInstanceSurface::FLAG_PASS_OPAQUE)) { + rl->add_element(surf); + } + if (force_alpha || (surf->flags & GeometryInstanceSurface::FLAG_PASS_ALPHA)) { + render_list[RENDER_LIST_ALPHA].add_element(surf); + } + + if (surf->flags & GeometryInstanceSurface::FLAG_USES_SCREEN_TEXTURE) { + scene_state.used_screen_texture = true; + } + if (surf->flags & GeometryInstanceSurface::FLAG_USES_NORMAL_TEXTURE) { + scene_state.used_normal_texture = true; + } + if (surf->flags & GeometryInstanceSurface::FLAG_USES_DEPTH_TEXTURE) { + scene_state.used_depth_texture = true; + } + + /* + Add elements here if there are shadows + */ + + } else if (p_pass_mode == PASS_MODE_SHADOW) { + if (surf->flags & GeometryInstanceSurface::FLAG_PASS_SHADOW) { + rl->add_element(surf); + } + } else { + if (surf->flags & (GeometryInstanceSurface::FLAG_PASS_DEPTH | GeometryInstanceSurface::FLAG_PASS_OPAQUE)) { + rl->add_element(surf); + } + } + + surf->sort.depth_layer = depth_layer; + + surf = surf->next; + } + } +} + +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) { + CameraMatrix correction; + correction.set_depth_correction(p_flip_y); + CameraMatrix projection = correction * p_render_data->cam_projection; + //store camera into ubo + RasterizerStorageGLES3::store_camera(projection, scene_state.ubo.projection_matrix); + RasterizerStorageGLES3::store_camera(projection.inverse(), scene_state.ubo.inv_projection_matrix); + RasterizerStorageGLES3::store_transform(p_render_data->cam_transform, scene_state.ubo.inv_view_matrix); + RasterizerStorageGLES3::store_transform(p_render_data->cam_transform.affine_inverse(), scene_state.ubo.view_matrix); + + scene_state.ubo.directional_light_count = 1; + + scene_state.ubo.z_far = p_render_data->z_far; + scene_state.ubo.z_near = p_render_data->z_near; + + scene_state.ubo.pancake_shadows = p_pancake_shadows; + + scene_state.ubo.viewport_size[0] = p_screen_size.x; + scene_state.ubo.viewport_size[1] = p_screen_size.y; + + Size2 screen_pixel_size = Vector2(1.0, 1.0) / Size2(p_screen_size); + scene_state.ubo.screen_pixel_size[0] = screen_pixel_size.x; + scene_state.ubo.screen_pixel_size[1] = screen_pixel_size.y; + + //time global variables + scene_state.ubo.time = time; + + if (is_environment(p_render_data->environment)) { + Environment *env = environment_owner.get_or_null(p_render_data->environment); + RS::EnvironmentBG env_bg = env->background; + RS::EnvironmentAmbientSource ambient_src = env->ambient_source; + + float bg_energy = env->bg_energy; + scene_state.ubo.ambient_light_color_energy[3] = bg_energy; + + scene_state.ubo.ambient_color_sky_mix = env->ambient_sky_contribution; + + //ambient + if (ambient_src == RS::ENV_AMBIENT_SOURCE_BG && (env_bg == RS::ENV_BG_CLEAR_COLOR || env_bg == RS::ENV_BG_COLOR)) { + Color color = env_bg == RS::ENV_BG_CLEAR_COLOR ? p_default_bg_color : env->bg_color; + color = color.srgb_to_linear(); + + scene_state.ubo.ambient_light_color_energy[0] = color.r * bg_energy; + scene_state.ubo.ambient_light_color_energy[1] = color.g * bg_energy; + scene_state.ubo.ambient_light_color_energy[2] = color.b * bg_energy; + } else { + float energy = env->ambient_light_energy; + Color color = env->ambient_light; + color = color.srgb_to_linear(); + scene_state.ubo.ambient_light_color_energy[0] = color.r * energy; + scene_state.ubo.ambient_light_color_energy[1] = color.g * energy; + scene_state.ubo.ambient_light_color_energy[2] = color.b * energy; + + Basis sky_transform = env->sky_orientation; + sky_transform = sky_transform.inverse() * p_render_data->cam_transform.basis; + RasterizerStorageGLES3::store_transform_3x3(sky_transform, scene_state.ubo.radiance_inverse_xform); + } + + scene_state.ubo.fog_enabled = env->fog_enabled; + scene_state.ubo.fog_density = env->fog_density; + scene_state.ubo.fog_height = env->fog_height; + scene_state.ubo.fog_height_density = env->fog_height_density; + scene_state.ubo.fog_aerial_perspective = env->fog_aerial_perspective; + + Color fog_color = env->fog_light_color.srgb_to_linear(); + float fog_energy = env->fog_light_energy; + + scene_state.ubo.fog_light_color[0] = fog_color.r * fog_energy; + scene_state.ubo.fog_light_color[1] = fog_color.g * fog_energy; + scene_state.ubo.fog_light_color[2] = fog_color.b * fog_energy; + + scene_state.ubo.fog_sun_scatter = env->fog_sun_scatter; + + } else { + } + + if (scene_state.ubo_buffer == 0) { + glGenBuffers(1, &scene_state.ubo_buffer); + } + glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DATA_UNIFORM_LOCATION, scene_state.ubo_buffer); + glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::UBO), &scene_state.ubo, GL_STREAM_DRAW); + glBindBuffer(GL_UNIFORM_BUFFER, 0); +} + void RasterizerSceneGLES3::render_scene(RID p_render_buffers, const CameraData *p_camera_data, const PagedArray<GeometryInstance *> &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_effects, 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, RendererScene::RenderInfo *r_render_info) { GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton(); + GLES3::Config *config = GLES3::Config::get_singleton(); RENDER_TIMESTAMP("Setup 3D Scene"); - // assign render data + + RenderBuffers *rb = nullptr; + if (p_render_buffers.is_valid()) { + rb = render_buffers_owner.get_or_null(p_render_buffers); + ERR_FAIL_COND(!rb); + } + + // Assign render data // Use the format from rendererRD RenderDataGLES3 render_data; { render_data.render_buffers = p_render_buffers; - + render_data.transparent_bg = rb->is_transparent; // Our first camera is used by default render_data.cam_transform = p_camera_data->main_transform; render_data.cam_projection = p_camera_data->main_projection; render_data.view_projection[0] = p_camera_data->main_projection; - render_data.cam_ortogonal = p_camera_data->is_orthogonal; + render_data.cam_orthogonal = p_camera_data->is_orthogonal; render_data.view_count = p_camera_data->view_count; for (uint32_t v = 0; v < p_camera_data->view_count; v++) { @@ -625,10 +1315,6 @@ void RasterizerSceneGLES3::render_scene(RID p_render_buffers, const CameraData * render_data.instances = &p_instances; render_data.lights = &p_lights; render_data.reflection_probes = &p_reflection_probes; - //render_data.voxel_gi_instances = &p_voxel_gi_instances; - //render_data.decals = &p_decals; - //render_data.lightmaps = &p_lightmaps; - //render_data.fog_volumes = &p_fog_volumes; render_data.environment = p_environment; render_data.camera_effects = p_camera_effects; render_data.shadow_atlas = p_shadow_atlas; @@ -638,7 +1324,7 @@ void RasterizerSceneGLES3::render_scene(RID p_render_buffers, const CameraData * // this should be the same for all cameras.. render_data.lod_distance_multiplier = p_camera_data->main_projection.get_lod_multiplier(); - render_data.lod_camera_plane = Plane(-p_camera_data->main_transform.basis.get_axis(Vector3::AXIS_Z), p_camera_data->main_transform.get_origin()); + render_data.lod_camera_plane = Plane(-p_camera_data->main_transform.basis.get_column(Vector3::AXIS_Z), p_camera_data->main_transform.get_origin()); if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) { render_data.screen_mesh_lod_threshold = 0.0; @@ -655,14 +1341,14 @@ void RasterizerSceneGLES3::render_scene(RID p_render_buffers, const CameraData * render_data.reflection_probes = ∅ } - RenderBuffers *rb = nullptr; - //RasterizerStorageGLES3::RenderTarget *rt = nullptr; - if (p_render_buffers.is_valid()) { - rb = render_buffers_owner.get_or_null(p_render_buffers); - ERR_FAIL_COND(!rb); - //rt = texture_storage->render_target_owner.get_or_null(rb->render_target); - //ERR_FAIL_COND(!rt); - } + bool reverse_cull = false; + + /////////// + // Fill Light lists here + ////////// + + GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_variables_get_uniform_buffer(); + glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer); Color clear_color; if (p_render_buffers.is_valid()) { @@ -675,11 +1361,84 @@ void RasterizerSceneGLES3::render_scene(RID p_render_buffers, const CameraData * bool fb_cleared = false; - glDepthFunc(GL_LEQUAL); + Size2i screen_size; + screen_size.x = rb->width; + screen_size.y = rb->height; - /* Depth Prepass */ + SceneState::TonemapUBO tonemap_ubo; + if (is_environment(p_environment)) { + tonemap_ubo.exposure = env->exposure; + tonemap_ubo.white = env->white; + tonemap_ubo.tonemapper = int32_t(env->tone_mapper); + } + + if (scene_state.tonemap_buffer == 0) { + // Only create if using 3D + glGenBuffers(1, &scene_state.tonemap_buffer); + } + glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_TONEMAP_UNIFORM_LOCATION, scene_state.tonemap_buffer); + glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::TonemapUBO), &tonemap_ubo, GL_STREAM_DRAW); + + _setup_environment(&render_data, render_data.reflection_probe.is_valid(), screen_size, !render_data.reflection_probe.is_valid(), clear_color, false); + + _fill_render_list(RENDER_LIST_OPAQUE, &render_data, PASS_MODE_COLOR); + render_list[RENDER_LIST_OPAQUE].sort_by_key(); + render_list[RENDER_LIST_ALPHA].sort_by_reverse_depth_and_priority(); glBindFramebuffer(GL_FRAMEBUFFER, rb->framebuffer); + glViewport(0, 0, rb->width, rb->height); + + // Do depth prepass if it's explicitly enabled + bool use_depth_prepass = config->use_depth_prepass; + + // Don't do depth prepass we are rendering overdraw + use_depth_prepass = use_depth_prepass && get_debug_draw_mode() != RS::VIEWPORT_DEBUG_DRAW_OVERDRAW; + + if (use_depth_prepass) { + //pre z pass + + glDisable(GL_BLEND); + glDepthMask(GL_TRUE); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glDisable(GL_SCISSOR_TEST); + glCullFace(GL_BACK); + glEnable(GL_CULL_FACE); + scene_state.cull_mode = GLES3::SceneShaderData::CULL_BACK; + + glColorMask(0, 0, 0, 0); + glClearDepth(1.0f); + glClear(GL_DEPTH_BUFFER_BIT); + + uint32_t spec_constant_base_flags = 0; + + RenderListParameters render_list_params(render_list[RENDER_LIST_OPAQUE].elements.ptr(), render_list[RENDER_LIST_OPAQUE].elements.size(), reverse_cull, spec_constant_base_flags, get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_WIREFRAME, render_data.lod_camera_plane, render_data.lod_distance_multiplier, render_data.screen_mesh_lod_threshold); + _render_list_template<PASS_MODE_DEPTH>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_OPAQUE].elements.size()); + + glColorMask(1, 1, 1, 1); + + fb_cleared = true; + scene_state.used_depth_prepass = true; + } else { + scene_state.used_depth_prepass = false; + } + + glBlendEquation(GL_FUNC_ADD); + + if (render_data.transparent_bg) { + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + } else { + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_BLEND); + } + scene_state.current_blend_mode = GLES3::SceneShaderData::BLEND_MODE_MIX; + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glDepthMask(GL_TRUE); + scene_state.current_depth_test = GLES3::SceneShaderData::DEPTH_TEST_ENABLED; + scene_state.current_depth_draw = GLES3::SceneShaderData::DEPTH_DRAW_OPAQUE; if (!fb_cleared) { glClearDepth(1.0f); @@ -690,9 +1449,6 @@ void RasterizerSceneGLES3::render_scene(RID p_render_buffers, const CameraData * bool keep_color = false; if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW) { - clear_color = Color(0, 0, 0, 1); - } - if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW) { clear_color = Color(0, 0, 0, 1); //in overdraw mode, BG should always be black } else if (is_environment(p_environment)) { RS::EnvironmentBG bg_mode = environment_get_background(p_environment); @@ -723,16 +1479,43 @@ void RasterizerSceneGLES3::render_scene(RID p_render_buffers, const CameraData * default: { } } + // Draw sky cubemap } if (!keep_color) { glClearBufferfv(GL_COLOR, 0, clear_color.components); } + uint32_t spec_constant_base_flags = 0; + //Render Opaque Objects + RenderListParameters render_list_params(render_list[RENDER_LIST_OPAQUE].elements.ptr(), render_list[RENDER_LIST_OPAQUE].elements.size(), reverse_cull, spec_constant_base_flags, get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_WIREFRAME, render_data.lod_camera_plane, render_data.lod_distance_multiplier, render_data.screen_mesh_lod_threshold); + + _render_list_template<PASS_MODE_COLOR>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_OPAQUE].elements.size()); + if (draw_sky) { - //_draw_sky(sky, render_data.cam_projection, render_data.cam_transform, env->sky_custom_fov, env->bg_energy, env->sky_orientation); + if (scene_state.current_depth_test != GLES3::SceneShaderData::DEPTH_TEST_ENABLED) { + glEnable(GL_DEPTH_TEST); + scene_state.current_depth_test = GLES3::SceneShaderData::DEPTH_TEST_ENABLED; + } + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_FALSE); + glDisable(GL_BLEND); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + scene_state.current_depth_test = GLES3::SceneShaderData::DEPTH_TEST_ENABLED; + scene_state.current_depth_draw = GLES3::SceneShaderData::DEPTH_DRAW_DISABLED; + scene_state.cull_mode = GLES3::SceneShaderData::CULL_BACK; + + _draw_sky(env, render_data.cam_projection, render_data.cam_transform); } + glEnable(GL_BLEND); + + //Render transparent pass + 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, get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_WIREFRAME, render_data.lod_camera_plane, render_data.lod_distance_multiplier, render_data.screen_mesh_lod_threshold); + + _render_list_template<PASS_MODE_COLOR_TRANSPARENT>(&render_list_params_alpha, &render_data, 0, render_list[RENDER_LIST_ALPHA].elements.size(), true); + if (p_render_buffers.is_valid()) { /* RENDER_TIMESTAMP("Tonemap"); @@ -741,9 +1524,225 @@ void RasterizerSceneGLES3::render_scene(RID p_render_buffers, const CameraData * _render_buffers_debug_draw(p_render_buffers, p_shadow_atlas, p_occluder_debug_tex); } + glDisable(GL_BLEND); texture_storage->render_target_disable_clear_request(rb->render_target); } +template <PassMode p_pass_mode> +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) { + GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton(); + + GLuint prev_vertex_array_gl = 0; + GLuint prev_index_array_gl = 0; + + GLES3::SceneMaterialData *prev_material_data = nullptr; + GLES3::SceneShaderData *prev_shader = nullptr; + + SceneShaderGLES3::ShaderVariant shader_variant = SceneShaderGLES3::MODE_COLOR; // Assigned to silence wrong -Wmaybe-initialized. + + switch (p_pass_mode) { + case PASS_MODE_COLOR: + case PASS_MODE_COLOR_TRANSPARENT: { + } break; + case PASS_MODE_COLOR_ADDITIVE: { + shader_variant = SceneShaderGLES3::MODE_ADDITIVE; + } break; + case PASS_MODE_SHADOW: + case PASS_MODE_DEPTH: { + shader_variant = SceneShaderGLES3::MODE_DEPTH; + } break; + } + + for (uint32_t i = p_from_element; i < p_to_element; i++) { + const GeometryInstanceSurface *surf = p_params->elements[i]; + const GeometryInstanceGLES3 *inst = surf->owner; + + if (p_pass_mode == PASS_MODE_COLOR && !(surf->flags & GeometryInstanceSurface::FLAG_PASS_OPAQUE)) { + continue; // Objects with "Depth-prepass" transparency are included in both render lists, but should only be rendered in the transparent pass + } + + if (inst->instance_count == 0) { + continue; + } + + //uint32_t base_spec_constants = p_params->spec_constant_base_flags; + + GLES3::SceneShaderData *shader; + GLES3::SceneMaterialData *material_data; + void *mesh_surface; + + if (p_pass_mode == PASS_MODE_SHADOW) { + shader = surf->shader_shadow; + material_data = surf->material_shadow; + mesh_surface = surf->surface_shadow; + } else { + shader = surf->shader; + material_data = surf->material; + mesh_surface = surf->surface; + } + + if (!mesh_surface) { + continue; + } + + if (p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) { + if (scene_state.current_depth_test != shader->depth_test) { + if (shader->depth_test == GLES3::SceneShaderData::DEPTH_TEST_DISABLED) { + glDisable(GL_DEPTH_TEST); + } else { + glEnable(GL_DEPTH_TEST); + } + scene_state.current_depth_test = shader->depth_test; + } + } + + if (scene_state.current_depth_draw != shader->depth_draw) { + switch (shader->depth_draw) { + case GLES3::SceneShaderData::DEPTH_DRAW_OPAQUE: { + glDepthMask(p_pass_mode == PASS_MODE_COLOR); + } break; + case GLES3::SceneShaderData::DEPTH_DRAW_ALWAYS: { + glDepthMask(GL_TRUE); + } break; + case GLES3::SceneShaderData::DEPTH_DRAW_DISABLED: { + glDepthMask(GL_FALSE); + } break; + } + + scene_state.current_depth_draw = shader->depth_draw; + } + + if (p_pass_mode == PASS_MODE_COLOR_TRANSPARENT || p_pass_mode == PASS_MODE_COLOR_ADDITIVE) { + GLES3::SceneShaderData::BlendMode desired_blend_mode; + if (p_pass_mode == PASS_MODE_COLOR_ADDITIVE) { + desired_blend_mode = GLES3::SceneShaderData::BLEND_MODE_ADD; + } else { + desired_blend_mode = shader->blend_mode; + } + + if (desired_blend_mode != scene_state.current_blend_mode) { + switch (desired_blend_mode) { + case GLES3::SceneShaderData::BLEND_MODE_MIX: { + glBlendEquation(GL_FUNC_ADD); + if (p_render_data->transparent_bg) { + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + } else { + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } + + } break; + case GLES3::SceneShaderData::BLEND_MODE_ADD: { + glBlendEquation(GL_FUNC_ADD); + glBlendFunc(p_pass_mode == PASS_MODE_COLOR_TRANSPARENT ? GL_SRC_ALPHA : GL_ONE, GL_ONE); + + } break; + case GLES3::SceneShaderData::BLEND_MODE_SUB: { + glBlendEquation(GL_FUNC_REVERSE_SUBTRACT); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + } break; + case GLES3::SceneShaderData::BLEND_MODE_MUL: { + glBlendEquation(GL_FUNC_ADD); + if (p_render_data->transparent_bg) { + glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO); + } else { + glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE); + } + + } break; + case GLES3::SceneShaderData::BLEND_MODE_ALPHA_TO_COVERAGE: { + // Do nothing for now. + } break; + } + scene_state.current_blend_mode = desired_blend_mode; + } + } + + //find cull variant + GLES3::SceneShaderData::Cull cull_mode = shader->cull_mode; + + if ((surf->flags & GeometryInstanceSurface::FLAG_USES_DOUBLE_SIDED_SHADOWS)) { + cull_mode = GLES3::SceneShaderData::CULL_DISABLED; + } else { + bool mirror = inst->mirror; + if (p_params->reverse_cull) { + mirror = !mirror; + } + if (cull_mode == GLES3::SceneShaderData::CULL_FRONT && mirror) { + cull_mode = GLES3::SceneShaderData::CULL_BACK; + } else if (cull_mode == GLES3::SceneShaderData::CULL_BACK && mirror) { + cull_mode = GLES3::SceneShaderData::CULL_FRONT; + } + } + + if (scene_state.cull_mode != cull_mode) { + if (cull_mode == GLES3::SceneShaderData::CULL_DISABLED) { + glDisable(GL_CULL_FACE); + } else { + if (scene_state.cull_mode == GLES3::SceneShaderData::CULL_DISABLED) { + // Last time was disabled, so enable and set proper face. + glEnable(GL_CULL_FACE); + } + glCullFace(cull_mode == GLES3::SceneShaderData::CULL_FRONT ? GL_FRONT : GL_BACK); + } + scene_state.cull_mode = cull_mode; + } + + RS::PrimitiveType primitive = surf->primitive; + static const GLenum prim[5] = { GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP }; + GLenum primitive_gl = prim[int(primitive)]; + + GLuint vertex_array_gl = 0; + GLuint index_array_gl = 0; + + //skeleton and blend shape + if (surf->owner->mesh_instance.is_valid()) { + mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(surf->owner->mesh_instance, surf->surface_index, shader->vertex_input_mask, vertex_array_gl); + } else { + mesh_storage->mesh_surface_get_vertex_arrays_and_format(mesh_surface, shader->vertex_input_mask, vertex_array_gl); + } + + index_array_gl = mesh_storage->mesh_surface_get_index_buffer(mesh_surface, surf->lod_index); + + if (prev_vertex_array_gl != vertex_array_gl) { + glBindVertexArray(vertex_array_gl); + prev_vertex_array_gl = vertex_array_gl; + } + + bool use_index_buffer = false; + if (prev_index_array_gl != index_array_gl) { + if (index_array_gl != 0) { + // Bind index each time so we can use LODs + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_array_gl); + use_index_buffer = true; + } + prev_index_array_gl = index_array_gl; + } + + // Update pipeline information here + + Transform3D world_transform; + if (inst->store_transform_cache) { + world_transform = inst->transform; + } + + if (prev_material_data != material_data) { + material_data->bind_uniforms(); + } + + if (prev_shader != shader) { + GLES3::MaterialStorage::get_singleton()->shaders.scene_shader.version_bind_shader(shader->version, shader_variant); + } + + GLES3::MaterialStorage::get_singleton()->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::WORLD_TRANSFORM, world_transform, shader->version, shader_variant); + + if (use_index_buffer) { + glDrawElements(primitive_gl, mesh_storage->mesh_surface_get_vertices_drawn_count(mesh_surface), mesh_storage->mesh_surface_get_index_type(mesh_surface), 0); + } else { + glDrawArrays(primitive_gl, 0, mesh_storage->mesh_surface_get_vertices_drawn_count(mesh_surface)); + } + } +} + void RasterizerSceneGLES3::render_material(const Transform3D &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_orthogonal, const PagedArray<GeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) { } @@ -879,6 +1878,8 @@ void RasterizerSceneGLES3::render_buffers_configure(RID p_render_buffers, RID p_ GLES3::RenderTarget *rt = texture_storage->get_render_target(p_render_target); + rb->is_transparent = rt->flags[RendererTextureStorage::RENDER_TARGET_TRANSPARENT]; + // framebuffer glGenFramebuffers(1, &rb->framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, rb->framebuffer); @@ -1007,6 +2008,38 @@ RasterizerSceneGLES3::RasterizerSceneGLES3(RasterizerStorageGLES3 *p_storage) { storage = p_storage; { + String global_defines; + global_defines += "#define MAX_GLOBAL_VARIABLES 256\n"; // TODO: this is arbitrary for now + material_storage->shaders.scene_shader.initialize(global_defines); + scene_globals.shader_default_version = material_storage->shaders.scene_shader.version_create(); + material_storage->shaders.scene_shader.version_bind_shader(scene_globals.shader_default_version, SceneShaderGLES3::MODE_COLOR); + } + + { + //default material and shader + scene_globals.default_shader = material_storage->shader_allocate(); + material_storage->shader_initialize(scene_globals.default_shader); + material_storage->shader_set_code(scene_globals.default_shader, R"( +// Default 3D material shader (clustered). + +shader_type spatial; + +void vertex() { + ROUGHNESS = 0.8; +} + +void fragment() { + ALBEDO = vec3(0.6); + ROUGHNESS = 0.8; + METALLIC = 0.2; +} +)"); + scene_globals.default_material = material_storage->material_allocate(); + material_storage->material_initialize(scene_globals.default_material); + material_storage->material_set_shader(scene_globals.default_material, scene_globals.default_shader); + } + + { // Initialize Sky stuff sky_globals.roughness_layers = GLOBAL_GET("rendering/reflections/sky_reflections/roughness_layers"); sky_globals.ggx_samples = GLOBAL_GET("rendering/reflections/sky_reflections/ggx_samples"); @@ -1014,9 +2047,9 @@ RasterizerSceneGLES3::RasterizerSceneGLES3(RasterizerStorageGLES3 *p_storage) { String global_defines; global_defines += "#define MAX_GLOBAL_VARIABLES 256\n"; // TODO: this is arbitrary for now global_defines += "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(sky_globals.max_directional_lights) + "\n"; - state.sky_shader.initialize(global_defines); - sky_globals.shader_default_version = state.sky_shader.version_create(); - state.sky_shader.version_bind_shader(sky_globals.shader_default_version, SkyShaderGLES3::MODE_BACKGROUND); + material_storage->shaders.sky_shader.initialize(global_defines); + sky_globals.shader_default_version = material_storage->shaders.sky_shader.version_create(); + material_storage->shaders.sky_shader.version_bind_shader(sky_globals.shader_default_version, SkyShaderGLES3::MODE_BACKGROUND); } { @@ -1038,12 +2071,79 @@ void sky() { material_storage->material_set_shader(sky_globals.default_material, sky_globals.default_shader); } + { + sky_globals.fog_shader = material_storage->shader_allocate(); + material_storage->shader_initialize(sky_globals.fog_shader); + + material_storage->shader_set_code(sky_globals.fog_shader, R"( +// Default clear color sky shader. + +shader_type sky; + +uniform vec4 clear_color; + +void sky() { + COLOR = clear_color.rgb; +} +)"); + sky_globals.fog_material = material_storage->material_allocate(); + material_storage->material_initialize(sky_globals.fog_material); + + material_storage->material_set_shader(sky_globals.fog_material, sky_globals.fog_shader); + } + { + { + //quad buffers + + glGenBuffers(1, &sky_globals.quad); + glBindBuffer(GL_ARRAY_BUFFER, sky_globals.quad); + { + const float qv[16] = { + -1, + -1, + 0, + 0, + -1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + -1, + 1, + 0, + }; + + glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW); + } + + glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind + + glGenVertexArrays(1, &sky_globals.quad_array); + glBindVertexArray(sky_globals.quad_array); + glBindBuffer(GL_ARRAY_BUFFER, sky_globals.quad); + glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, nullptr); + glEnableVertexAttribArray(RS::ARRAY_VERTEX); + glVertexAttribPointer(RS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, CAST_INT_TO_UCHAR_PTR(8)); + glEnableVertexAttribArray(RS::ARRAY_TEX_UV); + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind + } + } } RasterizerSceneGLES3::~RasterizerSceneGLES3() { - state.sky_shader.version_free(sky_globals.shader_default_version); + GLES3::MaterialStorage::get_singleton()->shaders.scene_shader.version_free(scene_globals.shader_default_version); + storage->free(scene_globals.default_material); + storage->free(scene_globals.default_shader); + GLES3::MaterialStorage::get_singleton()->shaders.sky_shader.version_free(sky_globals.shader_default_version); storage->free(sky_globals.default_material); storage->free(sky_globals.default_shader); + storage->free(sky_globals.fog_material); + storage->free(sky_globals.fog_shader); } #endif // GLES3_ENABLED |