diff options
Diffstat (limited to 'drivers/gles2')
-rw-r--r-- | drivers/gles2/rasterizer_canvas_gles2.cpp | 86 | ||||
-rw-r--r-- | drivers/gles2/rasterizer_canvas_gles2.h | 2 | ||||
-rw-r--r-- | drivers/gles2/rasterizer_scene_gles2.cpp | 72 | ||||
-rw-r--r-- | drivers/gles2/rasterizer_scene_gles2.h | 1 | ||||
-rw-r--r-- | drivers/gles2/rasterizer_storage_gles2.cpp | 46 | ||||
-rw-r--r-- | drivers/gles2/rasterizer_storage_gles2.h | 2 | ||||
-rw-r--r-- | drivers/gles2/shader_compiler_gles2.cpp | 9 | ||||
-rw-r--r-- | drivers/gles2/shader_gles2.cpp | 2 | ||||
-rw-r--r-- | drivers/gles2/shaders/canvas.glsl | 31 | ||||
-rw-r--r-- | drivers/gles2/shaders/scene.glsl | 24 |
10 files changed, 189 insertions, 86 deletions
diff --git a/drivers/gles2/rasterizer_canvas_gles2.cpp b/drivers/gles2/rasterizer_canvas_gles2.cpp index 64e4dbdab4..9a49d5a7e4 100644 --- a/drivers/gles2/rasterizer_canvas_gles2.cpp +++ b/drivers/gles2/rasterizer_canvas_gles2.cpp @@ -112,25 +112,21 @@ void RasterizerCanvasGLES2::_set_uniforms() { void RasterizerCanvasGLES2::canvas_begin() { state.canvas_shader.bind(); - bool transparent = false; + state.using_transparent_rt = false; if (storage->frame.current_rt) { glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); - glColorMask(1, 1, 1, 1); - transparent = 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) { - glColorMask(true, true, true, true); glClearColor(storage->frame.clear_request_color.r, storage->frame.clear_request_color.g, storage->frame.clear_request_color.b, - transparent ? storage->frame.clear_request_color.a : 1.0); + state.using_transparent_rt ? storage->frame.clear_request_color.a : 1.0); glClear(GL_COLOR_BUFFER_BIT); storage->frame.clear_request = false; } - glColorMask(1, 1, 1, transparent ? 1 : 0); - /* if (storage->frame.current_rt) { glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); @@ -186,7 +182,7 @@ void RasterizerCanvasGLES2::canvas_end() { state.using_texture_rect = false; state.using_skeleton = false; state.using_ninepatch = false; - glColorMask(1, 1, 1, 1); + state.using_transparent_rt = false; } RasterizerStorageGLES2::Texture *RasterizerCanvasGLES2::_bind_canvas_texture(const RID &p_texture, const RID &p_normal_map) { @@ -576,8 +572,15 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } } else { + static const Vector2 uvs[4] = { + Vector2(0.0, 0.0), + Vector2(0.0, 1.0), + Vector2(1.0, 1.0), + Vector2(1.0, 0.0), + }; + state.canvas_shader.set_uniform(CanvasShaderGLES2::COLOR_TEXPIXEL_SIZE, Vector2()); - _draw_gui_primitive(4, points, NULL, NULL); + _draw_gui_primitive(4, points, NULL, uvs); } } else { @@ -844,11 +847,11 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur int indices[num_points * 3]; - for (int i = 0; i < num_points; i++) { - points[i] = circle->pos + Vector2(Math::sin(i * Math_PI * 2.0 / num_points), Math::cos(i * Math_PI * 2.0 / num_points)) * circle->radius; - indices[i * 3 + 0] = i; - indices[i * 3 + 1] = (i + 1) % num_points; - indices[i * 3 + 2] = num_points; + for (int j = 0; j < num_points; j++) { + points[j] = circle->pos + Vector2(Math::sin(j * Math_PI * 2.0 / num_points), Math::cos(j * Math_PI * 2.0 / num_points)) * circle->radius; + indices[j * 3 + 0] = j; + indices[j * 3 + 1] = (j + 1) % num_points; + indices[j * 3 + 2] = num_points; } _bind_canvas_texture(RID(), RID()); @@ -906,13 +909,13 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id); } - for (int i = 0; i < VS::ARRAY_MAX - 1; i++) { - if (s->attribs[i].enabled) { - glEnableVertexAttribArray(i); - glVertexAttribPointer(s->attribs[i].index, s->attribs[i].size, s->attribs[i].type, s->attribs[i].normalized, s->attribs[i].stride, (uint8_t *)0 + s->attribs[i].offset); + for (int k = 0; k < VS::ARRAY_MAX - 1; k++) { + if (s->attribs[k].enabled) { + glEnableVertexAttribArray(k); + glVertexAttribPointer(s->attribs[k].index, s->attribs[k].size, s->attribs[k].type, s->attribs[k].normalized, s->attribs[k].stride, (uint8_t *)0 + s->attribs[k].offset); } else { - glDisableVertexAttribArray(i); - switch (i) { + glDisableVertexAttribArray(k); + switch (k) { case VS::ARRAY_NORMAL: { glVertexAttrib4f(VS::ARRAY_NORMAL, 0.0, 0.0, 1, 1); } break; @@ -932,8 +935,8 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur } } - for (int i = 1; i < VS::ARRAY_MAX - 1; i++) { - glDisableVertexAttribArray(i); + for (int j = 1; j < VS::ARRAY_MAX - 1; j++) { + glDisableVertexAttribArray(j); } } @@ -995,13 +998,13 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id); } - for (int i = 0; i < VS::ARRAY_MAX - 1; i++) { - if (s->attribs[i].enabled) { - glEnableVertexAttribArray(i); - glVertexAttribPointer(s->attribs[i].index, s->attribs[i].size, s->attribs[i].type, s->attribs[i].normalized, s->attribs[i].stride, (uint8_t *)0 + s->attribs[i].offset); + for (int k = 0; k < VS::ARRAY_MAX - 1; k++) { + if (s->attribs[k].enabled) { + glEnableVertexAttribArray(k); + glVertexAttribPointer(s->attribs[k].index, s->attribs[k].size, s->attribs[k].type, s->attribs[k].normalized, s->attribs[k].stride, (uint8_t *)0 + s->attribs[k].offset); } else { - glDisableVertexAttribArray(i); - switch (i) { + glDisableVertexAttribArray(k); + switch (k) { case VS::ARRAY_NORMAL: { glVertexAttrib4f(VS::ARRAY_NORMAL, 0.0, 0.0, 1, 1); } break; @@ -1014,8 +1017,8 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur } } - for (int i = 0; i < amount; i++) { - const float *buffer = base_buffer + i * stride; + for (int k = 0; k < amount; k++) { + const float *buffer = base_buffer + k * stride; { @@ -1165,7 +1168,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur } } -void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) { +void RasterizerCanvasGLES2::_copy_screen(const Rect2 &p_rect) { if (storage->frame.current_rt->copy_screen_effect.color == 0) { ERR_EXPLAIN("Can't use screen texture copying in a render target configured without copy buffers"); @@ -1174,8 +1177,6 @@ void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) { glDisable(GL_BLEND); - state.canvas_texscreen_used = true; - Vector2 wh(storage->frame.current_rt->width, storage->frame.current_rt->height); Color copy_section(p_rect.position.x / wh.x, p_rect.position.y / wh.y, p_rect.size.x / wh.x, p_rect.size.y / wh.y); @@ -1184,13 +1185,12 @@ void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) { storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, true); } + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_NO_ALPHA, !state.using_transparent_rt); + glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->copy_screen_effect.fbo); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color); - glClearColor(1, 0, 1, 1); - glClear(GL_COLOR_BUFFER_BIT); - storage->shaders.copy.bind(); storage->shaders.copy.set_uniform(CopyShaderGLES2::COPY_SECTION, copy_section); @@ -1216,16 +1216,23 @@ void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) { _draw_polygon(indexpos, 6, 4, vertpos, uvpos, NULL, false); storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, false); + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_NO_ALPHA, false); glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //back to front + glEnable(GL_BLEND); +} + +void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) { + + state.canvas_texscreen_used = true; + + _copy_screen(p_rect); // back to canvas, force rebind state.using_texture_rect = false; state.canvas_shader.bind(); _bind_canvas_texture(state.current_tex, state.current_normal); _set_uniforms(); - - glEnable(GL_BLEND); } void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) { @@ -1878,6 +1885,8 @@ void RasterizerCanvasGLES2::draw_window_margins(int *black_margin, RID *black_im draw_generic_textured_rect(Rect2(0, window_h - black_margin[MARGIN_BOTTOM], window_w, black_margin[MARGIN_BOTTOM]), Rect2(0, 0, 1, 1)); } + + canvas_end(); } void RasterizerCanvasGLES2::initialize() { @@ -1998,6 +2007,7 @@ void RasterizerCanvasGLES2::initialize() { state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_PIXEL_SNAP, GLOBAL_DEF("rendering/quality/2d/use_pixel_snap", false)); state.using_light = NULL; + state.using_transparent_rt = false; } void RasterizerCanvasGLES2::finalize() { diff --git a/drivers/gles2/rasterizer_canvas_gles2.h b/drivers/gles2/rasterizer_canvas_gles2.h index 221427198a..af41e91e0c 100644 --- a/drivers/gles2/rasterizer_canvas_gles2.h +++ b/drivers/gles2/rasterizer_canvas_gles2.h @@ -93,6 +93,7 @@ public: Transform vp; Light *using_light; bool using_shadow; + bool using_transparent_rt; } state; @@ -118,6 +119,7 @@ public: _FORCE_INLINE_ void _draw_generic(GLuint p_primitive, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor); _FORCE_INLINE_ void _canvas_item_render_commands(Item *p_item, Item *current_clip, bool &reclip, RasterizerStorageGLES2::Material *p_material); + void _copy_screen(const Rect2 &p_rect); _FORCE_INLINE_ void _copy_texscreen(const Rect2 &p_rect); virtual void canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform); diff --git a/drivers/gles2/rasterizer_scene_gles2.cpp b/drivers/gles2/rasterizer_scene_gles2.cpp index 2c52895bda..a7becdde24 100644 --- a/drivers/gles2/rasterizer_scene_gles2.cpp +++ b/drivers/gles2/rasterizer_scene_gles2.cpp @@ -1120,10 +1120,10 @@ void RasterizerSceneGLES2::_fill_render_list(InstanceBase **p_cull_result, int p int num_surfaces = mesh->surfaces.size(); - for (int i = 0; i < num_surfaces; i++) { - int material_index = instance->materials[i].is_valid() ? i : -1; + for (int j = 0; j < num_surfaces; j++) { + int material_index = instance->materials[j].is_valid() ? j : -1; - RasterizerStorageGLES2::Surface *surface = mesh->surfaces[i]; + RasterizerStorageGLES2::Surface *surface = mesh->surfaces[j]; _add_geometry(surface, instance, NULL, material_index, p_depth_pass, p_shadow_pass); } @@ -1143,8 +1143,8 @@ void RasterizerSceneGLES2::_fill_render_list(InstanceBase **p_cull_result, int p int ssize = mesh->surfaces.size(); - for (int i = 0; i < ssize; i++) { - RasterizerStorageGLES2::Surface *s = mesh->surfaces[i]; + for (int j = 0; j < ssize; j++) { + RasterizerStorageGLES2::Surface *s = mesh->surfaces[j]; _add_geometry(s, instance, multi_mesh, -1, p_depth_pass, p_shadow_pass); } } break; @@ -1178,9 +1178,19 @@ bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m state.scene_shader.set_custom_shader(p_material->shader->custom_code_id); + if (p_material->shader->spatial.uses_screen_texture && storage->frame.current_rt) { + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4); + 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(); - 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); } else { glEnable(GL_DEPTH_TEST); @@ -1190,19 +1200,16 @@ bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS: case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_OPAQUE: { - glDepthMask(!p_alpha_pass); + glDepthMask(!p_alpha_pass && !p_material->shader->spatial.uses_depth_texture); } break; case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALWAYS: { - glDepthMask(GL_TRUE); + glDepthMask(GL_TRUE && !p_material->shader->spatial.uses_depth_texture); } break; case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_NEVER: { glDepthMask(GL_FALSE); } break; } - // TODO whyyyyy???? - p_reverse_cull = true; - switch (p_material->shader->spatial.cull_mode) { case RasterizerStorageGLES2::Shader::Spatial::CULL_MODE_DISABLED: { glDisable(GL_CULL_FACE); @@ -1564,6 +1571,8 @@ void RasterizerSceneGLES2::_render_geometry(RenderList::Element *p_element) { } else { glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 3, buffer + color_ofs); } + } else { + glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 3, 1.0, 1.0, 1.0, 1.0); } if (multi_mesh->custom_data_floats) { @@ -1786,6 +1795,9 @@ void RasterizerSceneGLES2::_setup_light(LightInstance *light, ShadowAtlas *shado Color color = light_ptr->color * sign * energy * Math_PI; state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_COLOR, color); + Color shadow_color = light_ptr->shadow_color.to_linear(); + state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_COLOR, shadow_color); + //specific parameters switch (light_ptr->type) { @@ -2337,7 +2349,7 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements, Transform sky_orientation(p_env->sky_orientation, Vector3(0.0, 0.0, 0.0)); state.scene_shader.set_uniform(SceneShaderGLES2::RADIANCE_INVERSE_XFORM, sky_orientation.affine_inverse() * p_view_transform); } else { - // would be a bit weird if we dont have this... + // would be a bit weird if we don't have this... state.scene_shader.set_uniform(SceneShaderGLES2::RADIANCE_INVERSE_XFORM, p_view_transform); } } @@ -2352,7 +2364,7 @@ void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements, } else { 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_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); } @@ -2464,7 +2476,6 @@ void RasterizerSceneGLES2::_draw_sky(RasterizerStorageGLES2::Sky *p_sky, const C glDisable(GL_CULL_FACE); glDisable(GL_BLEND); glDepthFunc(GL_LEQUAL); - glColorMask(1, 1, 1, 1); // Camera CameraMatrix camera; @@ -2555,11 +2566,19 @@ void RasterizerSceneGLES2::_draw_sky(RasterizerStorageGLES2::Sky *p_sky, const C void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID p_environment, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass) { + Transform cam_transform = p_cam_transform; + GLuint current_fb = 0; Environment *env = NULL; int viewport_width, viewport_height; bool probe_interior = false; + bool reverse_cull = false; + + if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]) { + cam_transform.basis.set_axis(1, -cam_transform.basis.get_axis(1)); + reverse_cull = true; + } if (p_reflection_probe.is_valid()) { ReflectionProbeInstance *probe = reflection_probe_instance_owner.getornull(p_reflection_probe); @@ -2586,6 +2605,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const viewport_height = storage->frame.current_rt->height; } + state.used_screen_texture = false; state.viewport_size.x = viewport_width; state.viewport_size.y = viewport_height; state.screen_pixel_size.x = 1.0 / viewport_width; @@ -2594,7 +2614,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const //push back the directional lights if (p_light_cull_count) { - //harcoded limit of 256 lights + //hardcoded limit of 256 lights render_light_instance_count = MIN(RenderList::MAX_LIGHTS, p_light_cull_count); render_light_instances = (LightInstance **)alloca(sizeof(LightInstance *) * render_light_instance_count); render_directional_lights = 0; @@ -2608,7 +2628,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const if (light->light_ptr->type == VS::LIGHT_DIRECTIONAL) { render_directional_lights++; - //as goin in reverse, directional lights are always first anyway + //as going in reverse, directional lights are always first anyway } light->light_index = index; @@ -2657,7 +2677,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const // 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]) { clear_color = Color(0, 0, 0, 0); @@ -2678,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); } + state.default_ambient = Color(clear_color.r, clear_color.g, clear_color.b, 1.0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1); @@ -2709,7 +2731,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const if (env && env->bg_mode == VS::ENV_BG_SKY && (!storage->frame.current_rt || !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT])) { if (sky && sky->panorama.is_valid()) { - _draw_sky(sky, p_cam_projection, p_cam_transform, false, env->sky_custom_fov, env->bg_energy, env->sky_orientation); + _draw_sky(sky, p_cam_projection, cam_transform, false, env->sky_custom_fov, env->bg_energy, env->sky_orientation); } } @@ -2719,8 +2741,12 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const // render opaque things first render_list.sort_by_key(false); - _render_render_list(render_list.elements, render_list.element_count, p_cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, false, false, false); + _render_render_list(render_list.elements, render_list.element_count, cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, reverse_cull, false, false); + if (storage->frame.current_rt && state.used_screen_texture) { + //copy screen texture + storage->canvas->_copy_screen(Rect2()); + } // alpha pass glBlendEquation(GL_FUNC_ADD); @@ -2728,7 +2754,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const render_list.sort_by_depth(true); - _render_render_list(&render_list.elements[render_list.max_elements - render_list.alpha_element_count], render_list.alpha_element_count, p_cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, false, true, false); + _render_render_list(&render_list.elements[render_list.max_elements - render_list.alpha_element_count], render_list.alpha_element_count, cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, reverse_cull, true, false); glDisable(GL_DEPTH_TEST); @@ -3041,7 +3067,9 @@ void RasterizerSceneGLES2::render_shadow(RID p_light, RID p_shadow_atlas, int p_ } } - glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height); + if (storage->frame.current_rt) { + glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height); + } glColorMask(1, 1, 1, 1); } @@ -3224,6 +3252,8 @@ void RasterizerSceneGLES2::initialize() { } shadow_filter_mode = SHADOW_FILTER_NEAREST; + + glFrontFace(GL_CW); } void RasterizerSceneGLES2::iteration() { diff --git a/drivers/gles2/rasterizer_scene_gles2.h b/drivers/gles2/rasterizer_scene_gles2.h index 0d917f4da2..aab9ae0fdc 100644 --- a/drivers/gles2/rasterizer_scene_gles2.h +++ b/drivers/gles2/rasterizer_scene_gles2.h @@ -103,6 +103,7 @@ public: GLuint sky_verts; GLuint immediate_buffer; + Color default_ambient; // ResolveShaderGLES3 resolve_shader; // ScreenSpaceReflectionShaderGLES3 ssr_shader; diff --git a/drivers/gles2/rasterizer_storage_gles2.cpp b/drivers/gles2/rasterizer_storage_gles2.cpp index d00d572ccf..9def9d27e7 100644 --- a/drivers/gles2/rasterizer_storage_gles2.cpp +++ b/drivers/gles2/rasterizer_storage_gles2.cpp @@ -1230,6 +1230,10 @@ void RasterizerStorageGLES2::_update_shader(Shader *p_shader) const { actions = &shaders.actions_scene; 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; default: { @@ -2231,7 +2235,7 @@ void RasterizerStorageGLES2::mesh_add_surface(RID p_mesh, uint32_t p_format, VS: } mesh->surfaces.push_back(surface); - mesh->instance_change_notify(true, false); + mesh->instance_change_notify(true, true); info.vertex_mem += surface->total_data_size; } @@ -4200,7 +4204,12 @@ void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) { glGenTextures(1, &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) { @@ -4218,20 +4227,26 @@ void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) { // depth - glGenRenderbuffers(1, &rt->depth); - glBindRenderbuffer(GL_RENDERBUFFER, rt->depth); + glGenTextures(1, &rt->depth); + glBindTexture(GL_TEXTURE_2D, rt->depth); + #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 - 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 - 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); if (status != GL_FRAMEBUFFER_COMPLETE) { - glDeleteRenderbuffers(1, &rt->fbo); + glDeleteFramebuffers(1, &rt->fbo); glDeleteTextures(1, &rt->depth); glDeleteTextures(1, &rt->color); rt->fbo = 0; @@ -4264,13 +4279,15 @@ void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) { // copy texscreen buffers if (!(rt->flags[RasterizerStorage::RENDER_TARGET_NO_SAMPLING])) { - int w = rt->width; - int h = rt->height; - glGenTextures(1, &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_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -4283,7 +4300,7 @@ void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) { glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); - GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (status != GL_FRAMEBUFFER_COMPLETE) { _render_target_clear(rt); ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE); @@ -4302,7 +4319,7 @@ void RasterizerStorageGLES2::_render_target_clear(RenderTarget *rt) { } if (rt->depth) { - glDeleteRenderbuffers(1, &rt->depth); + glDeleteTextures(1, &rt->depth); rt->depth = 0; } @@ -4313,7 +4330,6 @@ void RasterizerStorageGLES2::_render_target_clear(RenderTarget *rt) { tex->height = 0; tex->active = false; - // TODO hardcoded texscreen copy effect if (rt->copy_screen_effect.color) { glDeleteFramebuffers(1, &rt->copy_screen_effect.fbo); rt->copy_screen_effect.fbo = 0; diff --git a/drivers/gles2/rasterizer_storage_gles2.h b/drivers/gles2/rasterizer_storage_gles2.h index ba48ddd185..a0fa2aacc5 100644 --- a/drivers/gles2/rasterizer_storage_gles2.h +++ b/drivers/gles2/rasterizer_storage_gles2.h @@ -31,7 +31,7 @@ #ifndef RASTERIZERSTORAGEGLES2_H #define RASTERIZERSTORAGEGLES2_H -#include "core/dvector.h" +#include "core/pool_vector.h" #include "core/self_list.h" #include "servers/visual/rasterizer.h" #include "servers/visual/shader_language.h" diff --git a/drivers/gles2/shader_compiler_gles2.cpp b/drivers/gles2/shader_compiler_gles2.cpp index fa1242ceed..fdd905458b 100644 --- a/drivers/gles2/shader_compiler_gles2.cpp +++ b/drivers/gles2/shader_compiler_gles2.cpp @@ -81,7 +81,7 @@ static String _opstr(SL::Operator p_op) { static String _mkid(const String &p_id) { String id = "m_" + p_id; - return id.replace("__", "_dus_"); //doubleunderscore is reserverd in glsl + return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl } static String f2sp0(float p_float) { @@ -318,7 +318,7 @@ String ShaderCompilerGLES2::_dump_node_code(SL::Node *p_node, int p_level, Gener uniform_code += "uniform "; - uniform_code += _prestr(E->get().precission); + uniform_code += _prestr(E->get().precision); uniform_code += _typestr(E->get().type); uniform_code += " "; uniform_code += _mkid(E->key()); @@ -344,7 +344,7 @@ String ShaderCompilerGLES2::_dump_node_code(SL::Node *p_node, int p_level, Gener StringBuffer<> varying_code; varying_code += "varying "; - varying_code += _prestr(E->get().precission); + varying_code += _prestr(E->get().precision); varying_code += _typestr(E->get().type); varying_code += " "; varying_code += _mkid(E->key()); @@ -859,7 +859,7 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() { actions[VS::SHADER_SPATIAL].renames["INSTANCE_CUSTOM"] = "instance_custom"; actions[VS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv"; 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 //actions[VS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth"; 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["TRANSMISSION"] = "#define TRANSMISSION_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["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n"; diff --git a/drivers/gles2/shader_gles2.cpp b/drivers/gles2/shader_gles2.cpp index 5e259a01f0..012d1538b6 100644 --- a/drivers/gles2/shader_gles2.cpp +++ b/drivers/gles2/shader_gles2.cpp @@ -199,7 +199,7 @@ static void _display_error_with_code(const String &p_error, const Vector<const c static String _mkid(const String &p_id) { String id = "m_" + p_id; - return id.replace("__", "_dus_"); //doubleunderscore is reserverd in glsl + return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl } ShaderGLES2::Version *ShaderGLES2::get_current_version() { diff --git a/drivers/gles2/shaders/canvas.glsl b/drivers/gles2/shaders/canvas.glsl index c4a8c8b990..e74c7d7166 100644 --- a/drivers/gles2/shaders/canvas.glsl +++ b/drivers/gles2/shaders/canvas.glsl @@ -316,9 +316,35 @@ FRAGMENT_SHADER_GLOBALS /* clang-format on */ +void light_compute( + inout vec4 light, + inout vec2 light_vec, + inout float light_height, + inout vec4 light_color, + vec2 light_uv, + inout vec4 shadow_color, + vec3 normal, + vec2 uv, +#if defined(SCREEN_UV_USED) + vec2 screen_uv, +#endif + vec4 color) { + +#if defined(USE_LIGHT_SHADER_CODE) + + /* clang-format off */ + +LIGHT_SHADER_CODE + + /* clang-format on */ + +#endif +} + void main() { vec4 color = color_interp; + vec2 uv = uv_interp; #if !defined(COLOR_USED) //default behavior, texture by color @@ -414,7 +440,10 @@ FRAGMENT_SHADER_CODE color *= light; #ifdef USE_SHADOWS - light_vec = light_uv_interp.zw; //for shadows + // Reset light_vec to compute shadows, the shadow map is created from the light origin, so it only + // makes sense to compute shadows from there. + light_vec = light_uv_interp.zw; + float angle_to_light = -atan(light_vec.x, light_vec.y); float PI = 3.14159265358979323846264; /*int i = int(mod(floor((angle_to_light+7.0*PI/6.0)/(4.0*PI/6.0))+1.0, 3.0)); // +1 pq os indices estao em ordem 2,0,1 nos arrays diff --git a/drivers/gles2/shaders/scene.glsl b/drivers/gles2/shaders/scene.glsl index 6c3d3baccd..6f86152598 100644 --- a/drivers/gles2/shaders/scene.glsl +++ b/drivers/gles2/shaders/scene.glsl @@ -158,6 +158,7 @@ varying highp vec3 specular_interp; // general for all lights uniform highp vec4 light_color; +uniform highp vec4 shadow_color; uniform highp float light_specular; // directional @@ -723,6 +724,9 @@ uniform vec2 screen_pixel_size; #if defined(SCREEN_TEXTURE_USED) uniform highp sampler2D screen_texture; //texunit:-4 #endif +#if defined(DEPTH_TEXTURE_USED) +uniform highp sampler2D depth_texture; //texunit:-4 +#endif #ifdef USE_REFLECTION_PROBE1 @@ -912,6 +916,7 @@ uniform highp vec3 light_direction; //may be used by fog, so leave here //done in fragment // general for all lights uniform highp vec4 light_color; +uniform highp vec4 shadow_color; uniform highp float light_specular; // directional @@ -1680,7 +1685,7 @@ FRAGMENT_SHADER_CODE float shadow = sample_shadow(light_shadow_atlas, splane); - light_att *= shadow; + light_att *= mix(shadow_color.rgb, vec3(1.0), shadow); } #endif @@ -1757,7 +1762,7 @@ FRAGMENT_SHADER_CODE shadow_att = mix(shadow_att, shadow_att2, pssm_blend); } #endif - light_att *= shadow_att; + light_att *= mix(shadow_color.rgb, vec3(1.0), shadow_att); } #endif //LIGHT_USE_PSSM4 @@ -1798,7 +1803,7 @@ FRAGMENT_SHADER_CODE shadow_att = mix(shadow_att, shadow_att2, pssm_blend); } #endif - light_att *= shadow_att; + light_att *= mix(shadow_color.rgb, vec3(1.0), shadow_att); } #endif //LIGHT_USE_PSSM2 @@ -1905,7 +1910,7 @@ FRAGMENT_SHADER_CODE } #endif - light_att *= shadow; + light_att *= mix(shadow_color.rgb, vec3(1.0), shadow); } } #endif //use vertex lighting @@ -1953,7 +1958,7 @@ FRAGMENT_SHADER_CODE highp vec4 splane = shadow_coord; float shadow = sample_shadow(light_shadow_atlas, splane); - light_att *= shadow; + light_att *= mix(shadow_color.rgb, vec3(1.0), shadow); } #endif @@ -2049,7 +2054,12 @@ FRAGMENT_SHADER_CODE #if defined(USE_VERTEX_LIGHTING) +#if defined(BASE_PASS) gl_FragColor.rgb = mix(gl_FragColor.rgb, fog_interp.rgb, fog_interp.a); +#else + gl_FragColor.rgb *= (1.0 - fog_interp.a); +#endif // BASE_PASS + #else //pixel based fog float fog_amount = 0.0; @@ -2083,7 +2093,11 @@ FRAGMENT_SHADER_CODE } #endif +#if defined(BASE_PASS) gl_FragColor.rgb = mix(gl_FragColor.rgb, fog_color, fog_amount); +#else + gl_FragColor.rgb *= (1.0 - fog_amount); +#endif // BASE_PASS #endif //use vertex lit |