rasterizer_gles3.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /**************************************************************************/
  2. /* rasterizer_gles3.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "rasterizer_gles3.h"
  31. #include "storage/utilities.h"
  32. #ifdef GLES3_ENABLED
  33. #include "core/config/project_settings.h"
  34. #include "core/io/dir_access.h"
  35. #include "core/io/image.h"
  36. #include "core/os/os.h"
  37. #include "storage/texture_storage.h"
  38. #define _EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB 0x8242
  39. #define _EXT_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH_ARB 0x8243
  40. #define _EXT_DEBUG_CALLBACK_FUNCTION_ARB 0x8244
  41. #define _EXT_DEBUG_CALLBACK_USER_PARAM_ARB 0x8245
  42. #define _EXT_DEBUG_SOURCE_API_ARB 0x8246
  43. #define _EXT_DEBUG_SOURCE_WINDOW_SYSTEM_ARB 0x8247
  44. #define _EXT_DEBUG_SOURCE_SHADER_COMPILER_ARB 0x8248
  45. #define _EXT_DEBUG_SOURCE_THIRD_PARTY_ARB 0x8249
  46. #define _EXT_DEBUG_SOURCE_APPLICATION_ARB 0x824A
  47. #define _EXT_DEBUG_SOURCE_OTHER_ARB 0x824B
  48. #define _EXT_DEBUG_TYPE_ERROR_ARB 0x824C
  49. #define _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB 0x824D
  50. #define _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB 0x824E
  51. #define _EXT_DEBUG_TYPE_PORTABILITY_ARB 0x824F
  52. #define _EXT_DEBUG_TYPE_PERFORMANCE_ARB 0x8250
  53. #define _EXT_DEBUG_TYPE_OTHER_ARB 0x8251
  54. #define _EXT_DEBUG_TYPE_MARKER_ARB 0x8268
  55. #define _EXT_MAX_DEBUG_MESSAGE_LENGTH_ARB 0x9143
  56. #define _EXT_MAX_DEBUG_LOGGED_MESSAGES_ARB 0x9144
  57. #define _EXT_DEBUG_LOGGED_MESSAGES_ARB 0x9145
  58. #define _EXT_DEBUG_SEVERITY_HIGH_ARB 0x9146
  59. #define _EXT_DEBUG_SEVERITY_MEDIUM_ARB 0x9147
  60. #define _EXT_DEBUG_SEVERITY_LOW_ARB 0x9148
  61. #define _EXT_DEBUG_OUTPUT 0x92E0
  62. #ifndef GL_FRAMEBUFFER_SRGB
  63. #define GL_FRAMEBUFFER_SRGB 0x8DB9
  64. #endif
  65. #ifndef GLAPIENTRY
  66. #if defined(WINDOWS_ENABLED)
  67. #define GLAPIENTRY APIENTRY
  68. #else
  69. #define GLAPIENTRY
  70. #endif
  71. #endif
  72. #if !defined(IOS_ENABLED) && !defined(WEB_ENABLED)
  73. // We include EGL below to get debug callback on GLES2 platforms,
  74. // but EGL is not available on iOS or the web.
  75. #define CAN_DEBUG
  76. #endif
  77. #include "platform_gl.h"
  78. #if defined(MINGW_ENABLED) || defined(_MSC_VER)
  79. #define strcpy strcpy_s
  80. #endif
  81. #ifdef WINDOWS_ENABLED
  82. bool RasterizerGLES3::screen_flipped_y = false;
  83. #endif
  84. bool RasterizerGLES3::gles_over_gl = true;
  85. void RasterizerGLES3::begin_frame(double frame_step) {
  86. frame++;
  87. delta = frame_step;
  88. time_total += frame_step;
  89. double time_roll_over = GLOBAL_GET_CACHED(double, "rendering/limits/time/time_rollover_secs");
  90. time_total = Math::fmod(time_total, time_roll_over);
  91. canvas->set_time(time_total);
  92. scene->set_time(time_total, frame_step);
  93. GLES3::Utilities *utils = GLES3::Utilities::get_singleton();
  94. utils->_capture_timestamps_begin();
  95. //scene->iteration();
  96. }
  97. void RasterizerGLES3::end_frame(bool p_swap_buffers) {
  98. GLES3::Utilities *utils = GLES3::Utilities::get_singleton();
  99. utils->capture_timestamps_end();
  100. }
  101. void RasterizerGLES3::gl_end_frame(bool p_swap_buffers) {
  102. if (p_swap_buffers) {
  103. DisplayServer::get_singleton()->swap_buffers();
  104. } else {
  105. glFinish();
  106. }
  107. }
  108. void RasterizerGLES3::clear_depth(float p_depth) {
  109. #ifdef GL_API_ENABLED
  110. if (is_gles_over_gl()) {
  111. glClearDepth(p_depth);
  112. }
  113. #endif // GL_API_ENABLED
  114. #ifdef GLES_API_ENABLED
  115. if (!is_gles_over_gl()) {
  116. glClearDepthf(p_depth);
  117. }
  118. #endif // GLES_API_ENABLED
  119. }
  120. void RasterizerGLES3::clear_stencil(int32_t p_stencil) {
  121. glClearStencil(p_stencil);
  122. }
  123. #ifdef CAN_DEBUG
  124. static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const GLvoid *userParam) {
  125. // These are ultimately annoying, so removing for now.
  126. if (type == _EXT_DEBUG_TYPE_OTHER_ARB || type == _EXT_DEBUG_TYPE_PERFORMANCE_ARB || type == _EXT_DEBUG_TYPE_MARKER_ARB) {
  127. return;
  128. }
  129. char debSource[256], debType[256], debSev[256];
  130. if (source == _EXT_DEBUG_SOURCE_API_ARB) {
  131. strcpy(debSource, "OpenGL");
  132. } else if (source == _EXT_DEBUG_SOURCE_WINDOW_SYSTEM_ARB) {
  133. strcpy(debSource, "Windows");
  134. } else if (source == _EXT_DEBUG_SOURCE_SHADER_COMPILER_ARB) {
  135. strcpy(debSource, "Shader Compiler");
  136. } else if (source == _EXT_DEBUG_SOURCE_THIRD_PARTY_ARB) {
  137. strcpy(debSource, "Third Party");
  138. } else if (source == _EXT_DEBUG_SOURCE_APPLICATION_ARB) {
  139. strcpy(debSource, "Application");
  140. } else if (source == _EXT_DEBUG_SOURCE_OTHER_ARB) {
  141. strcpy(debSource, "Other");
  142. } else {
  143. ERR_FAIL_MSG(vformat("GL ERROR: Invalid or unhandled source '%d' in debug callback.", source));
  144. }
  145. if (type == _EXT_DEBUG_TYPE_ERROR_ARB) {
  146. strcpy(debType, "Error");
  147. } else if (type == _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB) {
  148. strcpy(debType, "Deprecated behavior");
  149. } else if (type == _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB) {
  150. strcpy(debType, "Undefined behavior");
  151. } else if (type == _EXT_DEBUG_TYPE_PORTABILITY_ARB) {
  152. strcpy(debType, "Portability");
  153. } else {
  154. ERR_FAIL_MSG(vformat("GL ERROR: Invalid or unhandled type '%d' in debug callback.", type));
  155. }
  156. if (severity == _EXT_DEBUG_SEVERITY_HIGH_ARB) {
  157. strcpy(debSev, "High");
  158. } else if (severity == _EXT_DEBUG_SEVERITY_MEDIUM_ARB) {
  159. strcpy(debSev, "Medium");
  160. } else if (severity == _EXT_DEBUG_SEVERITY_LOW_ARB) {
  161. strcpy(debSev, "Low");
  162. } else {
  163. ERR_FAIL_MSG(vformat("GL ERROR: Invalid or unhandled severity '%d' in debug callback.", severity));
  164. }
  165. String output = String() + "GL ERROR: Source: " + debSource + "\tType: " + debType + "\tID: " + itos(id) + "\tSeverity: " + debSev + "\tMessage: " + message;
  166. ERR_PRINT(output);
  167. }
  168. #endif
  169. typedef void(GLAPIENTRY *DEBUGPROCARB)(GLenum source,
  170. GLenum type,
  171. GLuint id,
  172. GLenum severity,
  173. GLsizei length,
  174. const char *message,
  175. const void *userParam);
  176. typedef void(GLAPIENTRY *DebugMessageCallbackARB)(DEBUGPROCARB callback, const void *userParam);
  177. void RasterizerGLES3::initialize() {
  178. 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()));
  179. // FLIP XY Bug: Are more devices affected?
  180. // Confirmed so far: all Adreno 3xx with old driver (until 2018)
  181. // ok on some tested Adreno devices: 4xx, 5xx and 6xx
  182. flip_xy_workaround = GLES3::Config::get_singleton()->flip_xy_workaround;
  183. }
  184. void RasterizerGLES3::finalize() {
  185. memdelete(scene);
  186. memdelete(canvas);
  187. memdelete(gi);
  188. memdelete(fog);
  189. memdelete(post_effects);
  190. memdelete(glow);
  191. memdelete(cubemap_filter);
  192. memdelete(copy_effects);
  193. memdelete(feed_effects);
  194. memdelete(light_storage);
  195. memdelete(particles_storage);
  196. memdelete(mesh_storage);
  197. memdelete(material_storage);
  198. memdelete(texture_storage);
  199. memdelete(utilities);
  200. memdelete(config);
  201. }
  202. RasterizerGLES3 *RasterizerGLES3::singleton = nullptr;
  203. #ifdef EGL_ENABLED
  204. void *_egl_load_function_wrapper(const char *p_name) {
  205. return (void *)eglGetProcAddress(p_name);
  206. }
  207. #endif
  208. RasterizerGLES3::RasterizerGLES3() {
  209. singleton = this;
  210. #ifdef GLAD_ENABLED
  211. bool glad_loaded = false;
  212. #ifdef EGL_ENABLED
  213. // There should be a more flexible system for getting the GL pointer, as
  214. // different DisplayServers can have different ways. We can just use the GLAD
  215. // version global to see if it loaded for now though, otherwise we fall back to
  216. // the generic loader below.
  217. #if defined(EGL_STATIC)
  218. bool has_egl = true;
  219. #else
  220. bool has_egl = (eglGetProcAddress != nullptr);
  221. #endif
  222. if (gles_over_gl) {
  223. if (has_egl && !glad_loaded && gladLoadGL((GLADloadfunc)&_egl_load_function_wrapper)) {
  224. glad_loaded = true;
  225. }
  226. } else {
  227. if (has_egl && !glad_loaded && gladLoadGLES2((GLADloadfunc)&_egl_load_function_wrapper)) {
  228. glad_loaded = true;
  229. }
  230. }
  231. #endif // EGL_ENABLED
  232. if (gles_over_gl) {
  233. if (!glad_loaded && gladLoaderLoadGL()) {
  234. glad_loaded = true;
  235. }
  236. } else {
  237. if (!glad_loaded && gladLoaderLoadGLES2()) {
  238. glad_loaded = true;
  239. }
  240. }
  241. // 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
  242. // the members of this instance are initialized, so this just makes debugging harder. It should either crash here intentionally,
  243. // or we need to actually test for this situation before constructing this.
  244. ERR_FAIL_COND_MSG(!glad_loaded, "Error initializing GLAD.");
  245. if (gles_over_gl) {
  246. if (OS::get_singleton()->is_stdout_verbose()) {
  247. if (GLAD_GL_ARB_debug_output) {
  248. glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
  249. glDebugMessageCallbackARB((GLDEBUGPROCARB)_gl_debug_print, nullptr);
  250. glEnable(_EXT_DEBUG_OUTPUT);
  251. } else {
  252. print_line("OpenGL debugging not supported!");
  253. }
  254. }
  255. }
  256. #endif // GLAD_ENABLED
  257. // For debugging
  258. #ifdef CAN_DEBUG
  259. #ifdef GL_API_ENABLED
  260. if (gles_over_gl) {
  261. if (OS::get_singleton()->is_stdout_verbose() && GLAD_GL_ARB_debug_output) {
  262. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_ERROR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  263. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  264. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  265. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_PORTABILITY_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  266. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_PERFORMANCE_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  267. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_OTHER_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  268. }
  269. }
  270. #endif // GL_API_ENABLED
  271. #ifdef GLES_API_ENABLED
  272. if (!gles_over_gl) {
  273. if (OS::get_singleton()->is_stdout_verbose()) {
  274. DebugMessageCallbackARB callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallback");
  275. if (!callback) {
  276. callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallbackKHR");
  277. }
  278. if (callback) {
  279. print_line("godot: ENABLING GL DEBUG");
  280. glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
  281. callback((DEBUGPROCARB)_gl_debug_print, nullptr);
  282. glEnable(_EXT_DEBUG_OUTPUT);
  283. }
  284. }
  285. }
  286. #endif // GLES_API_ENABLED
  287. #endif // CAN_DEBUG
  288. {
  289. String shader_cache_dir = Engine::get_singleton()->get_shader_cache_path();
  290. if (shader_cache_dir.is_empty()) {
  291. shader_cache_dir = "user://";
  292. }
  293. Ref<DirAccess> da = DirAccess::open(shader_cache_dir);
  294. if (da.is_null()) {
  295. ERR_PRINT("Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir);
  296. } else {
  297. Error err = da->change_dir("shader_cache");
  298. if (err != OK) {
  299. err = da->make_dir("shader_cache");
  300. }
  301. if (err != OK) {
  302. ERR_PRINT("Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir);
  303. } else {
  304. shader_cache_dir = shader_cache_dir.path_join("shader_cache");
  305. bool shader_cache_enabled = GLOBAL_GET("rendering/shader_compiler/shader_cache/enabled");
  306. if (!Engine::get_singleton()->is_editor_hint() && !shader_cache_enabled) {
  307. shader_cache_dir = String(); //disable only if not editor
  308. }
  309. if (!shader_cache_dir.is_empty()) {
  310. ShaderGLES3::set_shader_cache_dir(shader_cache_dir);
  311. }
  312. }
  313. }
  314. }
  315. // OpenGL needs to be initialized before initializing the Rasterizers
  316. config = memnew(GLES3::Config);
  317. utilities = memnew(GLES3::Utilities);
  318. texture_storage = memnew(GLES3::TextureStorage);
  319. material_storage = memnew(GLES3::MaterialStorage);
  320. mesh_storage = memnew(GLES3::MeshStorage);
  321. particles_storage = memnew(GLES3::ParticlesStorage);
  322. light_storage = memnew(GLES3::LightStorage);
  323. copy_effects = memnew(GLES3::CopyEffects);
  324. cubemap_filter = memnew(GLES3::CubemapFilter);
  325. glow = memnew(GLES3::Glow);
  326. post_effects = memnew(GLES3::PostEffects);
  327. feed_effects = memnew(GLES3::FeedEffects);
  328. gi = memnew(GLES3::GI);
  329. fog = memnew(GLES3::Fog);
  330. canvas = memnew(RasterizerCanvasGLES3());
  331. scene = memnew(RasterizerSceneGLES3());
  332. // Disable OpenGL linear to sRGB conversion, because Godot will always do this conversion itself.
  333. if (config->srgb_framebuffer_supported) {
  334. glDisable(GL_FRAMEBUFFER_SRGB);
  335. }
  336. }
  337. RasterizerGLES3::~RasterizerGLES3() {
  338. }
  339. void RasterizerGLES3::_blit_render_target_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen &p_blit, bool p_first) {
  340. GLES3::RenderTarget *rt = GLES3::TextureStorage::get_singleton()->get_render_target(p_blit.render_target);
  341. ERR_FAIL_NULL(rt);
  342. // We normally render to the render target upside down, so flip Y when blitting to the screen.
  343. bool flip_y = true;
  344. bool linear_to_srgb = false;
  345. if (rt->overridden.color.is_valid()) {
  346. // If we've overridden the render target's color texture, that means we
  347. // didn't render upside down, so we don't need to flip it.
  348. // We're probably rendering directly to an XR device.
  349. flip_y = false;
  350. // It is 99% likely our texture uses the GL_SRGB8_ALPHA8 texture format in
  351. // which case we have a GPU sRGB to Linear conversion on texture read.
  352. // We need to counter this.
  353. // Unfortunately we do not have an API to check this as Godot does not
  354. // track this.
  355. linear_to_srgb = true;
  356. }
  357. #ifdef WINDOWS_ENABLED
  358. if (screen_flipped_y) {
  359. flip_y = !flip_y;
  360. }
  361. #endif
  362. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  363. if (p_first) {
  364. if (p_blit.dst_rect.position != Vector2() || p_blit.dst_rect.size != rt->size) {
  365. // Viewport doesn't cover entire window so clear window to black before blitting.
  366. // Querying the actual window size from the DisplayServer would deadlock in separate render thread mode,
  367. // so let's set the biggest viewport the implementation supports, to be sure the window is fully covered.
  368. Size2i max_vp = GLES3::Utilities::get_singleton()->get_maximum_viewport_size();
  369. glViewport(0, 0, max_vp[0], max_vp[1]);
  370. glClearColor(0.0, 0.0, 0.0, 1.0);
  371. glClear(GL_COLOR_BUFFER_BIT);
  372. }
  373. }
  374. Vector2 screen_rect_end = p_blit.dst_rect.get_end();
  375. // Adreno (TM) 3xx devices have a bug that create wrong Landscape rotation of 180 degree
  376. // Reversing both the X and Y axis is equivalent to rotating 180 degrees
  377. bool flip_x = false;
  378. if (flip_xy_workaround && screen_rect_end.x > screen_rect_end.y) {
  379. flip_y = !flip_y;
  380. flip_x = !flip_x;
  381. }
  382. 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);
  383. 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);
  384. Vector2 size = p2 - p1;
  385. 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));
  386. glViewport(int(MIN(p1.x, p2.x)), int(MIN(p1.y, p2.y)), Math::abs(size.x), Math::abs(size.y));
  387. glActiveTexture(GL_TEXTURE0);
  388. GLenum target = rt->view_count > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
  389. glBindTexture(target, rt->color);
  390. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  391. glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  392. glDisable(GL_CULL_FACE);
  393. glEnable(GL_BLEND);
  394. glBlendFunc(GL_ONE, GL_ZERO);
  395. if (p_blit.lens_distortion.apply && (p_blit.lens_distortion.k1 != 0.0 || p_blit.lens_distortion.k2)) {
  396. 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);
  397. } else if (rt->view_count > 1) {
  398. 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);
  399. } else {
  400. copy_effects->copy_to_rect(screenrect, linear_to_srgb);
  401. }
  402. glBindTexture(GL_TEXTURE_2D, 0);
  403. }
  404. // is this p_screen useless in a multi window environment?
  405. void RasterizerGLES3::blit_render_targets_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen *p_render_targets, int p_amount) {
  406. for (int i = 0; i < p_amount; i++) {
  407. _blit_render_target_to_screen(p_screen, p_render_targets[i], i == 0);
  408. }
  409. }
  410. void RasterizerGLES3::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
  411. if (p_image.is_null() || p_image->is_empty()) {
  412. return;
  413. }
  414. Size2i win_size = DisplayServer::get_singleton()->window_get_size();
  415. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  416. glViewport(0, 0, win_size.width, win_size.height);
  417. glEnable(GL_BLEND);
  418. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
  419. glDepthMask(GL_FALSE);
  420. glClearColor(p_color.r, p_color.g, p_color.b, OS::get_singleton()->is_layered_allowed() ? p_color.a : 1.0);
  421. glClear(GL_COLOR_BUFFER_BIT);
  422. RID texture = texture_storage->texture_allocate();
  423. texture_storage->texture_2d_initialize(texture, p_image);
  424. Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height());
  425. Rect2 screenrect;
  426. if (p_scale) {
  427. if (win_size.width > win_size.height) {
  428. //scale horizontally
  429. screenrect.size.y = win_size.height;
  430. screenrect.size.x = imgrect.size.x * win_size.height / imgrect.size.y;
  431. screenrect.position.x = (win_size.width - screenrect.size.x) / 2;
  432. } else {
  433. //scale vertically
  434. screenrect.size.x = win_size.width;
  435. screenrect.size.y = imgrect.size.y * win_size.width / imgrect.size.x;
  436. screenrect.position.y = (win_size.height - screenrect.size.y) / 2;
  437. }
  438. } else {
  439. screenrect = imgrect;
  440. screenrect.position += ((Size2(win_size.width, win_size.height) - screenrect.size) / 2.0).floor();
  441. }
  442. #ifdef WINDOWS_ENABLED
  443. if (!screen_flipped_y)
  444. #endif
  445. {
  446. // Flip Y.
  447. screenrect.position.y = win_size.y - screenrect.position.y;
  448. screenrect.size.y = -screenrect.size.y;
  449. }
  450. // Normalize texture coordinates to window size.
  451. screenrect.position /= win_size;
  452. screenrect.size /= win_size;
  453. GLES3::Texture *t = texture_storage->get_texture(texture);
  454. t->gl_set_filter(p_use_filter ? RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR : RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  455. glActiveTexture(GL_TEXTURE0);
  456. glBindTexture(GL_TEXTURE_2D, t->tex_id);
  457. copy_effects->copy_to_rect(screenrect);
  458. glBindTexture(GL_TEXTURE_2D, 0);
  459. gl_end_frame(true);
  460. texture_storage->texture_free(texture);
  461. }
  462. #endif // GLES3_ENABLED