diff --git a/drivers/gles2/rasterizer_scene_gles2.cpp b/drivers/gles2/rasterizer_scene_gles2.cpp index 83e1196979b..50cb39b13f0 100644 --- a/drivers/gles2/rasterizer_scene_gles2.cpp +++ b/drivers/gles2/rasterizer_scene_gles2.cpp @@ -1027,6 +1027,7 @@ void RasterizerSceneGLES2::_add_geometry_with_material(RasterizerStorageGLES2::G e->light_index = RenderList::MAX_LIGHTS; e->use_accum_ptr = &e->use_accum; e->instancing = (e->instance->base_type == VS::INSTANCE_MULTIMESH) ? 1 : 0; + e->front_facing = false; if (e->geometry->last_pass != render_pass) { e->geometry->last_pass = render_pass; @@ -1046,6 +1047,10 @@ void RasterizerSceneGLES2::_add_geometry_with_material(RasterizerStorageGLES2::G e->material_index = e->material->index; + if (mirror) { + e->front_facing = true; + } + e->refprobe_0_index = RenderList::MAX_REFLECTION_PROBES; //refprobe disabled by default e->refprobe_1_index = RenderList::MAX_REFLECTION_PROBES; //refprobe disabled by default @@ -1257,7 +1262,29 @@ static const GLenum gl_primitive[] = { GL_TRIANGLE_FAN }; -bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_material, bool p_reverse_cull, bool p_alpha_pass, Size2i p_skeleton_tex_size) { +void RasterizerSceneGLES2::_set_cull(bool p_front, bool p_disabled, bool p_reverse_cull) { + + bool front = p_front; + if (p_reverse_cull) + front = !front; + + if (p_disabled != state.cull_disabled) { + if (p_disabled) + glDisable(GL_CULL_FACE); + else + glEnable(GL_CULL_FACE); + + state.cull_disabled = p_disabled; + } + + if (front != state.cull_front) { + + glCullFace(front ? GL_FRONT : GL_BACK); + state.cull_front = front; + } +} + +bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_material, bool p_alpha_pass, Size2i p_skeleton_tex_size) { // material parameters @@ -1295,21 +1322,6 @@ bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m } break; } - switch (p_material->shader->spatial.cull_mode) { - case RasterizerStorageGLES2::Shader::Spatial::CULL_MODE_DISABLED: { - glDisable(GL_CULL_FACE); - } break; - - case RasterizerStorageGLES2::Shader::Spatial::CULL_MODE_BACK: { - glEnable(GL_CULL_FACE); - glCullFace(p_reverse_cull ? GL_FRONT : GL_BACK); - } break; - case RasterizerStorageGLES2::Shader::Spatial::CULL_MODE_FRONT: { - glEnable(GL_CULL_FACE); - glCullFace(p_reverse_cull ? GL_BACK : GL_FRONT); - } break; - } - int tc = p_material->textures.size(); const Pair *textures = p_material->textures.ptr(); @@ -2202,6 +2214,11 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements, int prev_blend_mode = -2; //will always catch the first go + state.cull_front = false; + state.cull_disabled = false; + glCullFace(GL_BACK); + glEnable(GL_CULL_FACE); + if (p_alpha_pass) { glEnable(GL_BLEND); } else { @@ -2441,12 +2458,14 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements, if (rebind || material != prev_material) { storage->info.render.material_switch_count++; - shader_rebind = _setup_material(material, p_reverse_cull, p_alpha_pass, Size2i(skeleton ? skeleton->size * 3 : 0, 0)); + shader_rebind = _setup_material(material, p_alpha_pass, Size2i(skeleton ? skeleton->size * 3 : 0, 0)); if (shader_rebind) { storage->info.render.shader_rebind_count++; } } + _set_cull(e->front_facing, material->shader->spatial.cull_mode == RasterizerStorageGLES2::Shader::Spatial::CULL_MODE_DISABLED, p_reverse_cull); + if (i == 0 || shader_rebind) { //first time must rebind if (p_shadow) { diff --git a/drivers/gles2/rasterizer_scene_gles2.h b/drivers/gles2/rasterizer_scene_gles2.h index bd1a61688cf..d5a691d0b96 100644 --- a/drivers/gles2/rasterizer_scene_gles2.h +++ b/drivers/gles2/rasterizer_scene_gles2.h @@ -198,14 +198,15 @@ public: int directional_light_count; int reflection_probe_count; - bool cull_front; - bool cull_disabled; bool used_sss; bool using_contact_shadows; VS::ViewportDebugDraw debug_draw; */ + bool cull_front; + bool cull_disabled; + bool used_screen_texture; bool shadow_is_dual_parabolloid; float dual_parbolloid_direction; @@ -503,8 +504,7 @@ public: enum { MAX_LIGHTS = 255, MAX_REFLECTION_PROBES = 255, - DEFAULT_MAX_ELEMENTS = 65536, - SORT_KEY_PRIORITY_SHIFT = 56 + DEFAULT_MAX_ELEMENTS = 65536 }; int max_elements; @@ -518,6 +518,7 @@ public: bool use_accum; //is this an add pass for multipass bool *use_accum_ptr; + bool front_facing; union { //TODO: should be endian swapped on big endian @@ -685,7 +686,8 @@ public: void _draw_sky(RasterizerStorageGLES2::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_custom_fov, float p_energy, const Basis &p_sky_orientation); - _FORCE_INLINE_ bool _setup_material(RasterizerStorageGLES2::Material *p_material, bool p_reverse_cull, bool p_alpha_pass, Size2i p_skeleton_tex_size = Size2i(0, 0)); + _FORCE_INLINE_ void _set_cull(bool p_front, bool p_disabled, bool p_reverse_cull); + _FORCE_INLINE_ bool _setup_material(RasterizerStorageGLES2::Material *p_material, bool p_alpha_pass, Size2i p_skeleton_tex_size = Size2i(0, 0)); _FORCE_INLINE_ void _setup_geometry(RenderList::Element *p_element, RasterizerStorageGLES2::Skeleton *p_skeleton); _FORCE_INLINE_ void _setup_light_type(LightInstance *p_light, ShadowAtlas *shadow_atlas); _FORCE_INLINE_ void _setup_light(LightInstance *p_light, ShadowAtlas *shadow_atlas, const Transform &p_view_transform);