123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- /**************************************************************************/
- /* rasterizer_gles3.cpp */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /**************************************************************************/
- #include "rasterizer_gles3.h"
- #include "storage/utilities.h"
- #ifdef GLES3_ENABLED
- #include "core/config/project_settings.h"
- #include "core/io/dir_access.h"
- #include "core/io/image.h"
- #include "core/os/os.h"
- #include "storage/texture_storage.h"
- #define _EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB 0x8242
- #define _EXT_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH_ARB 0x8243
- #define _EXT_DEBUG_CALLBACK_FUNCTION_ARB 0x8244
- #define _EXT_DEBUG_CALLBACK_USER_PARAM_ARB 0x8245
- #define _EXT_DEBUG_SOURCE_API_ARB 0x8246
- #define _EXT_DEBUG_SOURCE_WINDOW_SYSTEM_ARB 0x8247
- #define _EXT_DEBUG_SOURCE_SHADER_COMPILER_ARB 0x8248
- #define _EXT_DEBUG_SOURCE_THIRD_PARTY_ARB 0x8249
- #define _EXT_DEBUG_SOURCE_APPLICATION_ARB 0x824A
- #define _EXT_DEBUG_SOURCE_OTHER_ARB 0x824B
- #define _EXT_DEBUG_TYPE_ERROR_ARB 0x824C
- #define _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB 0x824D
- #define _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB 0x824E
- #define _EXT_DEBUG_TYPE_PORTABILITY_ARB 0x824F
- #define _EXT_DEBUG_TYPE_PERFORMANCE_ARB 0x8250
- #define _EXT_DEBUG_TYPE_OTHER_ARB 0x8251
- #define _EXT_DEBUG_TYPE_MARKER_ARB 0x8268
- #define _EXT_MAX_DEBUG_MESSAGE_LENGTH_ARB 0x9143
- #define _EXT_MAX_DEBUG_LOGGED_MESSAGES_ARB 0x9144
- #define _EXT_DEBUG_LOGGED_MESSAGES_ARB 0x9145
- #define _EXT_DEBUG_SEVERITY_HIGH_ARB 0x9146
- #define _EXT_DEBUG_SEVERITY_MEDIUM_ARB 0x9147
- #define _EXT_DEBUG_SEVERITY_LOW_ARB 0x9148
- #define _EXT_DEBUG_OUTPUT 0x92E0
- #ifndef GL_FRAMEBUFFER_SRGB
- #define GL_FRAMEBUFFER_SRGB 0x8DB9
- #endif
- #ifndef GLAPIENTRY
- #if defined(WINDOWS_ENABLED)
- #define GLAPIENTRY APIENTRY
- #else
- #define GLAPIENTRY
- #endif
- #endif
- #if !defined(IOS_ENABLED) && !defined(WEB_ENABLED)
- // We include EGL below to get debug callback on GLES2 platforms,
- // but EGL is not available on iOS or the web.
- #define CAN_DEBUG
- #endif
- #include "platform_gl.h"
- #if defined(MINGW_ENABLED) || defined(_MSC_VER)
- #define strcpy strcpy_s
- #endif
- #ifdef WINDOWS_ENABLED
- bool RasterizerGLES3::screen_flipped_y = false;
- #endif
- bool RasterizerGLES3::gles_over_gl = true;
- void RasterizerGLES3::begin_frame(double frame_step) {
- frame++;
- delta = frame_step;
- time_total += frame_step;
- double time_roll_over = GLOBAL_GET_CACHED(double, "rendering/limits/time/time_rollover_secs");
- time_total = Math::fmod(time_total, time_roll_over);
- canvas->set_time(time_total);
- scene->set_time(time_total, frame_step);
- GLES3::Utilities *utils = GLES3::Utilities::get_singleton();
- utils->_capture_timestamps_begin();
- //scene->iteration();
- }
- void RasterizerGLES3::end_frame(bool p_swap_buffers) {
- GLES3::Utilities *utils = GLES3::Utilities::get_singleton();
- utils->capture_timestamps_end();
- }
- void RasterizerGLES3::gl_end_frame(bool p_swap_buffers) {
- if (p_swap_buffers) {
- DisplayServer::get_singleton()->swap_buffers();
- } else {
- glFinish();
- }
- }
- void RasterizerGLES3::clear_depth(float p_depth) {
- #ifdef GL_API_ENABLED
- if (is_gles_over_gl()) {
- glClearDepth(p_depth);
- }
- #endif // GL_API_ENABLED
- #ifdef GLES_API_ENABLED
- if (!is_gles_over_gl()) {
- glClearDepthf(p_depth);
- }
- #endif // GLES_API_ENABLED
- }
- void RasterizerGLES3::clear_stencil(int32_t p_stencil) {
- glClearStencil(p_stencil);
- }
- #ifdef CAN_DEBUG
- static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const GLvoid *userParam) {
- // These are ultimately annoying, so removing for now.
- if (type == _EXT_DEBUG_TYPE_OTHER_ARB || type == _EXT_DEBUG_TYPE_PERFORMANCE_ARB || type == _EXT_DEBUG_TYPE_MARKER_ARB) {
- return;
- }
- char debSource[256], debType[256], debSev[256];
- if (source == _EXT_DEBUG_SOURCE_API_ARB) {
- strcpy(debSource, "OpenGL");
- } else if (source == _EXT_DEBUG_SOURCE_WINDOW_SYSTEM_ARB) {
- strcpy(debSource, "Windows");
- } else if (source == _EXT_DEBUG_SOURCE_SHADER_COMPILER_ARB) {
- strcpy(debSource, "Shader Compiler");
- } else if (source == _EXT_DEBUG_SOURCE_THIRD_PARTY_ARB) {
- strcpy(debSource, "Third Party");
- } else if (source == _EXT_DEBUG_SOURCE_APPLICATION_ARB) {
- strcpy(debSource, "Application");
- } else if (source == _EXT_DEBUG_SOURCE_OTHER_ARB) {
- strcpy(debSource, "Other");
- } else {
- ERR_FAIL_MSG(vformat("GL ERROR: Invalid or unhandled source '%d' in debug callback.", source));
- }
- if (type == _EXT_DEBUG_TYPE_ERROR_ARB) {
- strcpy(debType, "Error");
- } else if (type == _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB) {
- strcpy(debType, "Deprecated behavior");
- } else if (type == _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB) {
- strcpy(debType, "Undefined behavior");
- } else if (type == _EXT_DEBUG_TYPE_PORTABILITY_ARB) {
- strcpy(debType, "Portability");
- } else {
- ERR_FAIL_MSG(vformat("GL ERROR: Invalid or unhandled type '%d' in debug callback.", type));
- }
- if (severity == _EXT_DEBUG_SEVERITY_HIGH_ARB) {
- strcpy(debSev, "High");
- } else if (severity == _EXT_DEBUG_SEVERITY_MEDIUM_ARB) {
- strcpy(debSev, "Medium");
- } else if (severity == _EXT_DEBUG_SEVERITY_LOW_ARB) {
- strcpy(debSev, "Low");
- } else {
- ERR_FAIL_MSG(vformat("GL ERROR: Invalid or unhandled severity '%d' in debug callback.", severity));
- }
- String output = String() + "GL ERROR: Source: " + debSource + "\tType: " + debType + "\tID: " + itos(id) + "\tSeverity: " + debSev + "\tMessage: " + message;
- ERR_PRINT(output);
- }
- #endif
- typedef void(GLAPIENTRY *DEBUGPROCARB)(GLenum source,
- GLenum type,
- GLuint id,
- GLenum severity,
- GLsizei length,
- const char *message,
- const void *userParam);
- typedef void(GLAPIENTRY *DebugMessageCallbackARB)(DEBUGPROCARB callback, const void *userParam);
- void RasterizerGLES3::initialize() {
- Engine::get_singleton()->print_header(vformat("OpenGL API %s - Compatibility - Using Device: %s - %s", RS::get_singleton()->get_video_adapter_api_version(), RS::get_singleton()->get_video_adapter_vendor(), RS::get_singleton()->get_video_adapter_name()));
- // FLIP XY Bug: Are more devices affected?
- // Confirmed so far: all Adreno 3xx with old driver (until 2018)
- // ok on some tested Adreno devices: 4xx, 5xx and 6xx
- flip_xy_workaround = GLES3::Config::get_singleton()->flip_xy_workaround;
- }
- void RasterizerGLES3::finalize() {
- memdelete(scene);
- memdelete(canvas);
- memdelete(gi);
- memdelete(fog);
- memdelete(post_effects);
- memdelete(glow);
- memdelete(cubemap_filter);
- memdelete(copy_effects);
- memdelete(feed_effects);
- memdelete(light_storage);
- memdelete(particles_storage);
- memdelete(mesh_storage);
- memdelete(material_storage);
- memdelete(texture_storage);
- memdelete(utilities);
- memdelete(config);
- }
- RasterizerGLES3 *RasterizerGLES3::singleton = nullptr;
- #ifdef EGL_ENABLED
- void *_egl_load_function_wrapper(const char *p_name) {
- return (void *)eglGetProcAddress(p_name);
- }
- #endif
- RasterizerGLES3::RasterizerGLES3() {
- singleton = this;
- #ifdef GLAD_ENABLED
- bool glad_loaded = false;
- #ifdef EGL_ENABLED
- // There should be a more flexible system for getting the GL pointer, as
- // different DisplayServers can have different ways. We can just use the GLAD
- // version global to see if it loaded for now though, otherwise we fall back to
- // the generic loader below.
- #if defined(EGL_STATIC)
- bool has_egl = true;
- #else
- bool has_egl = (eglGetProcAddress != nullptr);
- #endif
- if (gles_over_gl) {
- if (has_egl && !glad_loaded && gladLoadGL((GLADloadfunc)&_egl_load_function_wrapper)) {
- glad_loaded = true;
- }
- } else {
- if (has_egl && !glad_loaded && gladLoadGLES2((GLADloadfunc)&_egl_load_function_wrapper)) {
- glad_loaded = true;
- }
- }
- #endif // EGL_ENABLED
- if (gles_over_gl) {
- if (!glad_loaded && gladLoaderLoadGL()) {
- glad_loaded = true;
- }
- } else {
- if (!glad_loaded && gladLoaderLoadGLES2()) {
- glad_loaded = true;
- }
- }
- // FIXME this is an early return from a constructor. Any other code using this instance will crash or the finalizer will crash, because none of
- // the members of this instance are initialized, so this just makes debugging harder. It should either crash here intentionally,
- // or we need to actually test for this situation before constructing this.
- ERR_FAIL_COND_MSG(!glad_loaded, "Error initializing GLAD.");
- if (gles_over_gl) {
- if (OS::get_singleton()->is_stdout_verbose()) {
- if (GLAD_GL_ARB_debug_output) {
- glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
- glDebugMessageCallbackARB((GLDEBUGPROCARB)_gl_debug_print, nullptr);
- glEnable(_EXT_DEBUG_OUTPUT);
- } else {
- print_line("OpenGL debugging not supported!");
- }
- }
- }
- #endif // GLAD_ENABLED
- // For debugging
- #ifdef CAN_DEBUG
- #ifdef GL_API_ENABLED
- if (gles_over_gl) {
- if (OS::get_singleton()->is_stdout_verbose() && GLAD_GL_ARB_debug_output) {
- glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_ERROR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
- glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
- glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
- glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_PORTABILITY_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
- glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_PERFORMANCE_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
- glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_OTHER_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
- }
- }
- #endif // GL_API_ENABLED
- #ifdef GLES_API_ENABLED
- if (!gles_over_gl) {
- if (OS::get_singleton()->is_stdout_verbose()) {
- DebugMessageCallbackARB callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallback");
- if (!callback) {
- callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallbackKHR");
- }
- if (callback) {
- print_line("godot: ENABLING GL DEBUG");
- glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
- callback((DEBUGPROCARB)_gl_debug_print, nullptr);
- glEnable(_EXT_DEBUG_OUTPUT);
- }
- }
- }
- #endif // GLES_API_ENABLED
- #endif // CAN_DEBUG
- {
- String shader_cache_dir = Engine::get_singleton()->get_shader_cache_path();
- if (shader_cache_dir.is_empty()) {
- shader_cache_dir = "user://";
- }
- Ref<DirAccess> da = DirAccess::open(shader_cache_dir);
- if (da.is_null()) {
- ERR_PRINT("Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir);
- } else {
- Error err = da->change_dir("shader_cache");
- if (err != OK) {
- err = da->make_dir("shader_cache");
- }
- if (err != OK) {
- ERR_PRINT("Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir);
- } else {
- shader_cache_dir = shader_cache_dir.path_join("shader_cache");
- bool shader_cache_enabled = GLOBAL_GET("rendering/shader_compiler/shader_cache/enabled");
- if (!Engine::get_singleton()->is_editor_hint() && !shader_cache_enabled) {
- shader_cache_dir = String(); //disable only if not editor
- }
- if (!shader_cache_dir.is_empty()) {
- ShaderGLES3::set_shader_cache_dir(shader_cache_dir);
- }
- }
- }
- }
- // OpenGL needs to be initialized before initializing the Rasterizers
- config = memnew(GLES3::Config);
- utilities = memnew(GLES3::Utilities);
- texture_storage = memnew(GLES3::TextureStorage);
- material_storage = memnew(GLES3::MaterialStorage);
- mesh_storage = memnew(GLES3::MeshStorage);
- particles_storage = memnew(GLES3::ParticlesStorage);
- light_storage = memnew(GLES3::LightStorage);
- copy_effects = memnew(GLES3::CopyEffects);
- cubemap_filter = memnew(GLES3::CubemapFilter);
- glow = memnew(GLES3::Glow);
- post_effects = memnew(GLES3::PostEffects);
- feed_effects = memnew(GLES3::FeedEffects);
- gi = memnew(GLES3::GI);
- fog = memnew(GLES3::Fog);
- canvas = memnew(RasterizerCanvasGLES3());
- scene = memnew(RasterizerSceneGLES3());
- // Disable OpenGL linear to sRGB conversion, because Godot will always do this conversion itself.
- if (config->srgb_framebuffer_supported) {
- glDisable(GL_FRAMEBUFFER_SRGB);
- }
- }
- RasterizerGLES3::~RasterizerGLES3() {
- }
- void RasterizerGLES3::_blit_render_target_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen &p_blit, bool p_first) {
- GLES3::RenderTarget *rt = GLES3::TextureStorage::get_singleton()->get_render_target(p_blit.render_target);
- ERR_FAIL_NULL(rt);
- // We normally render to the render target upside down, so flip Y when blitting to the screen.
- bool flip_y = true;
- bool linear_to_srgb = false;
- if (rt->overridden.color.is_valid()) {
- // If we've overridden the render target's color texture, that means we
- // didn't render upside down, so we don't need to flip it.
- // We're probably rendering directly to an XR device.
- flip_y = false;
- // It is 99% likely our texture uses the GL_SRGB8_ALPHA8 texture format in
- // which case we have a GPU sRGB to Linear conversion on texture read.
- // We need to counter this.
- // Unfortunately we do not have an API to check this as Godot does not
- // track this.
- linear_to_srgb = true;
- }
- #ifdef WINDOWS_ENABLED
- if (screen_flipped_y) {
- flip_y = !flip_y;
- }
- #endif
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
- if (p_first) {
- if (p_blit.dst_rect.position != Vector2() || p_blit.dst_rect.size != rt->size) {
- // Viewport doesn't cover entire window so clear window to black before blitting.
- // Querying the actual window size from the DisplayServer would deadlock in separate render thread mode,
- // so let's set the biggest viewport the implementation supports, to be sure the window is fully covered.
- Size2i max_vp = GLES3::Utilities::get_singleton()->get_maximum_viewport_size();
- glViewport(0, 0, max_vp[0], max_vp[1]);
- glClearColor(0.0, 0.0, 0.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- }
- }
- Vector2 screen_rect_end = p_blit.dst_rect.get_end();
- // Adreno (TM) 3xx devices have a bug that create wrong Landscape rotation of 180 degree
- // Reversing both the X and Y axis is equivalent to rotating 180 degrees
- bool flip_x = false;
- if (flip_xy_workaround && screen_rect_end.x > screen_rect_end.y) {
- flip_y = !flip_y;
- flip_x = !flip_x;
- }
- Vector2 p1 = Vector2(flip_x ? screen_rect_end.x : p_blit.dst_rect.position.x, flip_y ? screen_rect_end.y : p_blit.dst_rect.position.y);
- Vector2 p2 = Vector2(flip_x ? p_blit.dst_rect.position.x : screen_rect_end.x, flip_y ? p_blit.dst_rect.position.y : screen_rect_end.y);
- Vector2 size = p2 - p1;
- Rect2 screenrect = Rect2(Vector2(flip_x ? 1.0 : 0.0, flip_y ? 1.0 : 0.0), Vector2(flip_x ? -1.0 : 1.0, flip_y ? -1.0 : 1.0));
- glViewport(int(MIN(p1.x, p2.x)), int(MIN(p1.y, p2.y)), Math::abs(size.x), Math::abs(size.y));
- glActiveTexture(GL_TEXTURE0);
- GLenum target = rt->view_count > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
- glBindTexture(target, rt->color);
- glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glDisable(GL_CULL_FACE);
- glEnable(GL_BLEND);
- glBlendFunc(GL_ONE, GL_ZERO);
- if (p_blit.lens_distortion.apply && (p_blit.lens_distortion.k1 != 0.0 || p_blit.lens_distortion.k2)) {
- copy_effects->copy_with_lens_distortion(screenrect, p_blit.multi_view.use_layer ? p_blit.multi_view.layer : 0, p_blit.lens_distortion.eye_center, p_blit.lens_distortion.k1, p_blit.lens_distortion.k2, p_blit.lens_distortion.upscale, p_blit.lens_distortion.aspect_ratio, linear_to_srgb);
- } else if (rt->view_count > 1) {
- copy_effects->copy_to_rect_3d(screenrect, p_blit.multi_view.use_layer ? p_blit.multi_view.layer : 0, GLES3::Texture::TYPE_LAYERED, 0.0, linear_to_srgb);
- } else {
- copy_effects->copy_to_rect(screenrect, linear_to_srgb);
- }
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- // is this p_screen useless in a multi window environment?
- void RasterizerGLES3::blit_render_targets_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen *p_render_targets, int p_amount) {
- for (int i = 0; i < p_amount; i++) {
- _blit_render_target_to_screen(p_screen, p_render_targets[i], i == 0);
- }
- }
- void RasterizerGLES3::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
- if (p_image.is_null() || p_image->is_empty()) {
- return;
- }
- Size2i win_size = DisplayServer::get_singleton()->window_get_size();
- glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
- glViewport(0, 0, win_size.width, win_size.height);
- glEnable(GL_BLEND);
- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
- glDepthMask(GL_FALSE);
- glClearColor(p_color.r, p_color.g, p_color.b, OS::get_singleton()->is_layered_allowed() ? p_color.a : 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- RID texture = texture_storage->texture_allocate();
- texture_storage->texture_2d_initialize(texture, p_image);
- Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height());
- Rect2 screenrect;
- if (p_scale) {
- if (win_size.width > win_size.height) {
- //scale horizontally
- screenrect.size.y = win_size.height;
- screenrect.size.x = imgrect.size.x * win_size.height / imgrect.size.y;
- screenrect.position.x = (win_size.width - screenrect.size.x) / 2;
- } else {
- //scale vertically
- screenrect.size.x = win_size.width;
- screenrect.size.y = imgrect.size.y * win_size.width / imgrect.size.x;
- screenrect.position.y = (win_size.height - screenrect.size.y) / 2;
- }
- } else {
- screenrect = imgrect;
- screenrect.position += ((Size2(win_size.width, win_size.height) - screenrect.size) / 2.0).floor();
- }
- #ifdef WINDOWS_ENABLED
- if (!screen_flipped_y)
- #endif
- {
- // Flip Y.
- screenrect.position.y = win_size.y - screenrect.position.y;
- screenrect.size.y = -screenrect.size.y;
- }
- // Normalize texture coordinates to window size.
- screenrect.position /= win_size;
- screenrect.size /= win_size;
- GLES3::Texture *t = texture_storage->get_texture(texture);
- t->gl_set_filter(p_use_filter ? RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR : RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, t->tex_id);
- copy_effects->copy_to_rect(screenrect);
- glBindTexture(GL_TEXTURE_2D, 0);
- gl_end_frame(true);
- texture_storage->texture_free(texture);
- }
- #endif // GLES3_ENABLED
|