-Support DEPTH_TEXTURE in GLES2, fixes #25106

-Fix use of transparent framebuffers in GLES2
-Fix use of ambient color clearing in GLES2 when no environment exists.
This commit is contained in:
Juan Linietsky 2019-02-22 11:32:37 -03:00
parent 8b9ec8bc88
commit aab8f443f9
7 changed files with 48 additions and 33 deletions

View file

@ -115,12 +115,10 @@ void RasterizerCanvasGLES2::canvas_begin() {
state.using_transparent_rt = false; state.using_transparent_rt = false;
if (storage->frame.current_rt) { if (storage->frame.current_rt) {
glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
glColorMask(1, 1, 1, 1);
state.using_transparent_rt = storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]; state.using_transparent_rt = storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT];
} }
if (storage->frame.clear_request) { if (storage->frame.clear_request) {
glColorMask(true, true, true, true);
glClearColor(storage->frame.clear_request_color.r, glClearColor(storage->frame.clear_request_color.r,
storage->frame.clear_request_color.g, storage->frame.clear_request_color.g,
storage->frame.clear_request_color.b, storage->frame.clear_request_color.b,
@ -129,8 +127,6 @@ void RasterizerCanvasGLES2::canvas_begin() {
storage->frame.clear_request = false; storage->frame.clear_request = false;
} }
glColorMask(1, 1, 1, state.using_transparent_rt ? 1 : 0);
/* /*
if (storage->frame.current_rt) { if (storage->frame.current_rt) {
glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
@ -186,9 +182,6 @@ void RasterizerCanvasGLES2::canvas_end() {
state.using_texture_rect = false; state.using_texture_rect = false;
state.using_skeleton = false; state.using_skeleton = false;
state.using_ninepatch = false; state.using_ninepatch = false;
if (state.using_transparent_rt) {
glColorMask(1, 1, 1, 1);
}
state.using_transparent_rt = false; state.using_transparent_rt = false;
} }
@ -1182,10 +1175,6 @@ void RasterizerCanvasGLES2::_copy_screen(const Rect2 &p_rect) {
ERR_FAIL(); ERR_FAIL();
} }
if (state.using_transparent_rt) {
glColorMask(1, 1, 1, 1);
}
glDisable(GL_BLEND); glDisable(GL_BLEND);
Vector2 wh(storage->frame.current_rt->width, storage->frame.current_rt->height); Vector2 wh(storage->frame.current_rt->width, storage->frame.current_rt->height);
@ -1229,10 +1218,6 @@ void RasterizerCanvasGLES2::_copy_screen(const Rect2 &p_rect) {
storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, false); storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, false);
storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_NO_ALPHA, false); storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_NO_ALPHA, false);
if (state.using_transparent_rt) {
glColorMask(1, 1, 1, 0);
}
glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //back to front glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //back to front
glEnable(GL_BLEND); glEnable(GL_BLEND);
} }

View file

@ -1183,9 +1183,14 @@ bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m
glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color); glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color);
} }
if (p_material->shader->spatial.uses_depth_texture && storage->frame.current_rt) {
glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
}
bool shader_rebind = state.scene_shader.bind(); bool shader_rebind = state.scene_shader.bind();
if (p_material->shader->spatial.no_depth_test) { if (p_material->shader->spatial.no_depth_test || p_material->shader->spatial.uses_depth_texture) {
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
} else { } else {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
@ -1195,10 +1200,10 @@ bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m
case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS: case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS:
case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_OPAQUE: { case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_OPAQUE: {
glDepthMask(!p_alpha_pass); glDepthMask(!p_alpha_pass && !p_material->shader->spatial.uses_depth_texture);
} break; } break;
case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALWAYS: { case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALWAYS: {
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE && !p_material->shader->spatial.uses_depth_texture);
} break; } break;
case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_NEVER: { case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_NEVER: {
glDepthMask(GL_FALSE); glDepthMask(GL_FALSE);
@ -2359,7 +2364,7 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements,
} else { } else {
state.scene_shader.set_uniform(SceneShaderGLES2::BG_ENERGY, 1.0); state.scene_shader.set_uniform(SceneShaderGLES2::BG_ENERGY, 1.0);
state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_SKY_CONTRIBUTION, 1.0); state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_SKY_CONTRIBUTION, 1.0);
state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_COLOR, Color(1.0, 1.0, 1.0, 1.0)); state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_COLOR, state.default_ambient);
state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_ENERGY, 1.0); state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_ENERGY, 1.0);
} }
@ -2471,7 +2476,6 @@ void RasterizerSceneGLES2::_draw_sky(RasterizerStorageGLES2::Sky *p_sky, const C
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glDisable(GL_BLEND); glDisable(GL_BLEND);
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
glColorMask(1, 1, 1, 1);
// Camera // Camera
CameraMatrix camera; CameraMatrix camera;
@ -2673,7 +2677,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const
// clear color // clear color
Color clear_color(0, 0, 0, 0); Color clear_color(0, 0, 0, 1);
if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
clear_color = Color(0, 0, 0, 0); clear_color = Color(0, 0, 0, 0);
@ -2694,6 +2698,8 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const
glClearColor(clear_color.r, clear_color.g, clear_color.b, clear_color.a); glClearColor(clear_color.r, clear_color.g, clear_color.b, clear_color.a);
} }
state.default_ambient = Color(clear_color.r, clear_color.g, clear_color.b, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1); glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);

View file

@ -103,6 +103,7 @@ public:
GLuint sky_verts; GLuint sky_verts;
GLuint immediate_buffer; GLuint immediate_buffer;
Color default_ambient;
// ResolveShaderGLES3 resolve_shader; // ResolveShaderGLES3 resolve_shader;
// ScreenSpaceReflectionShaderGLES3 ssr_shader; // ScreenSpaceReflectionShaderGLES3 ssr_shader;

View file

@ -1230,6 +1230,10 @@ void RasterizerStorageGLES2::_update_shader(Shader *p_shader) const {
actions = &shaders.actions_scene; actions = &shaders.actions_scene;
actions->uniforms = &p_shader->uniforms; actions->uniforms = &p_shader->uniforms;
if (p_shader->spatial.uses_screen_texture && p_shader->spatial.uses_depth_texture) {
WARN_PRINT("Using both SCREEN_TEXTURE and DEPTH_TEXTURE is not supported in GLES2");
}
} break; } break;
default: { default: {
@ -4200,7 +4204,12 @@ void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) {
glGenTextures(1, &rt->color); glGenTextures(1, &rt->color);
glBindTexture(GL_TEXTURE_2D, rt->color); glBindTexture(GL_TEXTURE_2D, rt->color);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, rt->width, rt->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
if (rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, rt->width, rt->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, rt->width, rt->height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
}
if (texture->flags & VS::TEXTURE_FLAG_FILTER) { if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
@ -4218,20 +4227,26 @@ void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) {
// depth // depth
glGenRenderbuffers(1, &rt->depth); glGenTextures(1, &rt->depth);
glBindRenderbuffer(GL_RENDERBUFFER, rt->depth); glBindTexture(GL_TEXTURE_2D, rt->depth);
#ifdef JAVASCRIPT_ENABLED #ifdef JAVASCRIPT_ENABLED
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, rt->width, rt->height); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, rt->width, rt->height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
#else #else
glRenderbufferStorage(GL_RENDERBUFFER, _DEPTH_COMPONENT24_OES, rt->width, rt->height); glTexImage2D(GL_TEXTURE_2D, 0, _DEPTH_COMPONENT24_OES, rt->width, rt->height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
#endif #endif
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) { if (status != GL_FRAMEBUFFER_COMPLETE) {
glDeleteRenderbuffers(1, &rt->fbo); glDeleteFramebuffers(1, &rt->fbo);
glDeleteTextures(1, &rt->depth); glDeleteTextures(1, &rt->depth);
glDeleteTextures(1, &rt->color); glDeleteTextures(1, &rt->color);
rt->fbo = 0; rt->fbo = 0;
@ -4270,7 +4285,12 @@ void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) {
glGenTextures(1, &rt->copy_screen_effect.color); glGenTextures(1, &rt->copy_screen_effect.color);
glBindTexture(GL_TEXTURE_2D, rt->copy_screen_effect.color); glBindTexture(GL_TEXTURE_2D, rt->copy_screen_effect.color);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); if (rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, rt->width, rt->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, rt->width, rt->height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
@ -4302,7 +4322,7 @@ void RasterizerStorageGLES2::_render_target_clear(RenderTarget *rt) {
} }
if (rt->depth) { if (rt->depth) {
glDeleteRenderbuffers(1, &rt->depth); glDeleteTextures(1, &rt->depth);
rt->depth = 0; rt->depth = 0;
} }
@ -4313,7 +4333,6 @@ void RasterizerStorageGLES2::_render_target_clear(RenderTarget *rt) {
tex->height = 0; tex->height = 0;
tex->active = false; tex->active = false;
// TODO hardcoded texscreen copy effect
if (rt->copy_screen_effect.color) { if (rt->copy_screen_effect.color) {
glDeleteFramebuffers(1, &rt->copy_screen_effect.fbo); glDeleteFramebuffers(1, &rt->copy_screen_effect.fbo);
rt->copy_screen_effect.fbo = 0; rt->copy_screen_effect.fbo = 0;

View file

@ -859,7 +859,7 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
actions[VS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom"; actions[VS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom";
actions[VS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv"; actions[VS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv";
actions[VS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture"; actions[VS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture";
actions[VS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_buffer"; actions[VS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_texture";
// Defined in GLES3, but not available in GLES2 // Defined in GLES3, but not available in GLES2
//actions[VS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth"; //actions[VS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth";
actions[VS::SHADER_SPATIAL].renames["ALPHA_SCISSOR"] = "alpha_scissor"; actions[VS::SHADER_SPATIAL].renames["ALPHA_SCISSOR"] = "alpha_scissor";
@ -895,6 +895,7 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
actions[VS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n"; actions[VS::SHADER_SPATIAL].usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n";
actions[VS::SHADER_SPATIAL].usage_defines["TRANSMISSION"] = "#define TRANSMISSION_USED\n"; actions[VS::SHADER_SPATIAL].usage_defines["TRANSMISSION"] = "#define TRANSMISSION_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n"; actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["DEPTH_TEXTURE"] = "#define DEPTH_TEXTURE_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n"; actions[VS::SHADER_SPATIAL].usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
actions[VS::SHADER_SPATIAL].usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n"; actions[VS::SHADER_SPATIAL].usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";

View file

@ -724,6 +724,9 @@ uniform vec2 screen_pixel_size;
#if defined(SCREEN_TEXTURE_USED) #if defined(SCREEN_TEXTURE_USED)
uniform highp sampler2D screen_texture; //texunit:-4 uniform highp sampler2D screen_texture; //texunit:-4
#endif #endif
#if defined(DEPTH_TEXTURE_USED)
uniform highp sampler2D depth_texture; //texunit:-4
#endif
#ifdef USE_REFLECTION_PROBE1 #ifdef USE_REFLECTION_PROBE1

View file

@ -830,7 +830,7 @@ void SpatialMaterial::_update_shader() {
code += "\tALPHA = albedo.a * albedo_tex.a;\n"; code += "\tALPHA = albedo.a * albedo_tex.a;\n";
} }
if (!VisualServer::get_singleton()->is_low_end() && proximity_fade_enabled) { if (proximity_fade_enabled) {
code += "\tfloat depth_tex = textureLod(DEPTH_TEXTURE,SCREEN_UV,0.0).r;\n"; code += "\tfloat depth_tex = textureLod(DEPTH_TEXTURE,SCREEN_UV,0.0).r;\n";
code += "\tvec4 world_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV*2.0-1.0,depth_tex*2.0-1.0,1.0);\n"; code += "\tvec4 world_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV*2.0-1.0,depth_tex*2.0-1.0,1.0);\n";
code += "\tworld_pos.xyz/=world_pos.w;\n"; code += "\tworld_pos.xyz/=world_pos.w;\n";