Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/gles3/rasterizer_gles3.cpp
9973 views
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
31
#include "rasterizer_gles3.h"
32
#include "storage/utilities.h"
33
34
#ifdef GLES3_ENABLED
35
36
#include "core/config/project_settings.h"
37
#include "core/io/dir_access.h"
38
#include "core/io/image.h"
39
#include "core/os/os.h"
40
#include "storage/texture_storage.h"
41
42
#define _EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB 0x8242
43
#define _EXT_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH_ARB 0x8243
44
#define _EXT_DEBUG_CALLBACK_FUNCTION_ARB 0x8244
45
#define _EXT_DEBUG_CALLBACK_USER_PARAM_ARB 0x8245
46
#define _EXT_DEBUG_SOURCE_API_ARB 0x8246
47
#define _EXT_DEBUG_SOURCE_WINDOW_SYSTEM_ARB 0x8247
48
#define _EXT_DEBUG_SOURCE_SHADER_COMPILER_ARB 0x8248
49
#define _EXT_DEBUG_SOURCE_THIRD_PARTY_ARB 0x8249
50
#define _EXT_DEBUG_SOURCE_APPLICATION_ARB 0x824A
51
#define _EXT_DEBUG_SOURCE_OTHER_ARB 0x824B
52
#define _EXT_DEBUG_TYPE_ERROR_ARB 0x824C
53
#define _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB 0x824D
54
#define _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB 0x824E
55
#define _EXT_DEBUG_TYPE_PORTABILITY_ARB 0x824F
56
#define _EXT_DEBUG_TYPE_PERFORMANCE_ARB 0x8250
57
#define _EXT_DEBUG_TYPE_OTHER_ARB 0x8251
58
#define _EXT_DEBUG_TYPE_MARKER_ARB 0x8268
59
#define _EXT_MAX_DEBUG_MESSAGE_LENGTH_ARB 0x9143
60
#define _EXT_MAX_DEBUG_LOGGED_MESSAGES_ARB 0x9144
61
#define _EXT_DEBUG_LOGGED_MESSAGES_ARB 0x9145
62
#define _EXT_DEBUG_SEVERITY_HIGH_ARB 0x9146
63
#define _EXT_DEBUG_SEVERITY_MEDIUM_ARB 0x9147
64
#define _EXT_DEBUG_SEVERITY_LOW_ARB 0x9148
65
#define _EXT_DEBUG_OUTPUT 0x92E0
66
67
#ifndef GL_FRAMEBUFFER_SRGB
68
#define GL_FRAMEBUFFER_SRGB 0x8DB9
69
#endif
70
71
#ifndef GLAPIENTRY
72
#if defined(WINDOWS_ENABLED)
73
#define GLAPIENTRY APIENTRY
74
#else
75
#define GLAPIENTRY
76
#endif
77
#endif
78
79
#if !defined(IOS_ENABLED) && !defined(WEB_ENABLED)
80
// We include EGL below to get debug callback on GLES2 platforms,
81
// but EGL is not available on iOS or the web.
82
#define CAN_DEBUG
83
#endif
84
85
#include "platform_gl.h"
86
87
#if defined(MINGW_ENABLED) || defined(_MSC_VER)
88
#define strcpy strcpy_s
89
#endif
90
91
#ifdef WINDOWS_ENABLED
92
bool RasterizerGLES3::screen_flipped_y = false;
93
#endif
94
95
bool RasterizerGLES3::gles_over_gl = true;
96
97
void RasterizerGLES3::begin_frame(double frame_step) {
98
frame++;
99
delta = frame_step;
100
101
time_total += frame_step;
102
103
double time_roll_over = GLOBAL_GET_CACHED(double, "rendering/limits/time/time_rollover_secs");
104
time_total = Math::fmod(time_total, time_roll_over);
105
106
canvas->set_time(time_total);
107
scene->set_time(time_total, frame_step);
108
109
GLES3::Utilities *utils = GLES3::Utilities::get_singleton();
110
utils->_capture_timestamps_begin();
111
112
//scene->iteration();
113
}
114
115
void RasterizerGLES3::end_frame(bool p_swap_buffers) {
116
GLES3::Utilities *utils = GLES3::Utilities::get_singleton();
117
utils->capture_timestamps_end();
118
}
119
120
void RasterizerGLES3::gl_end_frame(bool p_swap_buffers) {
121
if (p_swap_buffers) {
122
DisplayServer::get_singleton()->swap_buffers();
123
} else {
124
glFinish();
125
}
126
}
127
128
void RasterizerGLES3::clear_depth(float p_depth) {
129
#ifdef GL_API_ENABLED
130
if (is_gles_over_gl()) {
131
glClearDepth(p_depth);
132
}
133
#endif // GL_API_ENABLED
134
#ifdef GLES_API_ENABLED
135
if (!is_gles_over_gl()) {
136
glClearDepthf(p_depth);
137
}
138
#endif // GLES_API_ENABLED
139
}
140
141
void RasterizerGLES3::clear_stencil(int32_t p_stencil) {
142
glClearStencil(p_stencil);
143
}
144
145
#ifdef CAN_DEBUG
146
static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const GLvoid *userParam) {
147
// These are ultimately annoying, so removing for now.
148
if (type == _EXT_DEBUG_TYPE_OTHER_ARB || type == _EXT_DEBUG_TYPE_PERFORMANCE_ARB || type == _EXT_DEBUG_TYPE_MARKER_ARB) {
149
return;
150
}
151
152
char debSource[256], debType[256], debSev[256];
153
154
if (source == _EXT_DEBUG_SOURCE_API_ARB) {
155
strcpy(debSource, "OpenGL");
156
} else if (source == _EXT_DEBUG_SOURCE_WINDOW_SYSTEM_ARB) {
157
strcpy(debSource, "Windows");
158
} else if (source == _EXT_DEBUG_SOURCE_SHADER_COMPILER_ARB) {
159
strcpy(debSource, "Shader Compiler");
160
} else if (source == _EXT_DEBUG_SOURCE_THIRD_PARTY_ARB) {
161
strcpy(debSource, "Third Party");
162
} else if (source == _EXT_DEBUG_SOURCE_APPLICATION_ARB) {
163
strcpy(debSource, "Application");
164
} else if (source == _EXT_DEBUG_SOURCE_OTHER_ARB) {
165
strcpy(debSource, "Other");
166
} else {
167
ERR_FAIL_MSG(vformat("GL ERROR: Invalid or unhandled source '%d' in debug callback.", source));
168
}
169
170
if (type == _EXT_DEBUG_TYPE_ERROR_ARB) {
171
strcpy(debType, "Error");
172
} else if (type == _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB) {
173
strcpy(debType, "Deprecated behavior");
174
} else if (type == _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB) {
175
strcpy(debType, "Undefined behavior");
176
} else if (type == _EXT_DEBUG_TYPE_PORTABILITY_ARB) {
177
strcpy(debType, "Portability");
178
} else {
179
ERR_FAIL_MSG(vformat("GL ERROR: Invalid or unhandled type '%d' in debug callback.", type));
180
}
181
182
if (severity == _EXT_DEBUG_SEVERITY_HIGH_ARB) {
183
strcpy(debSev, "High");
184
} else if (severity == _EXT_DEBUG_SEVERITY_MEDIUM_ARB) {
185
strcpy(debSev, "Medium");
186
} else if (severity == _EXT_DEBUG_SEVERITY_LOW_ARB) {
187
strcpy(debSev, "Low");
188
} else {
189
ERR_FAIL_MSG(vformat("GL ERROR: Invalid or unhandled severity '%d' in debug callback.", severity));
190
}
191
192
String output = String() + "GL ERROR: Source: " + debSource + "\tType: " + debType + "\tID: " + itos(id) + "\tSeverity: " + debSev + "\tMessage: " + message;
193
194
ERR_PRINT(output);
195
}
196
#endif
197
198
typedef void(GLAPIENTRY *DEBUGPROCARB)(GLenum source,
199
GLenum type,
200
GLuint id,
201
GLenum severity,
202
GLsizei length,
203
const char *message,
204
const void *userParam);
205
206
typedef void(GLAPIENTRY *DebugMessageCallbackARB)(DEBUGPROCARB callback, const void *userParam);
207
208
void RasterizerGLES3::initialize() {
209
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()));
210
}
211
212
void RasterizerGLES3::finalize() {
213
memdelete(scene);
214
memdelete(canvas);
215
memdelete(gi);
216
memdelete(fog);
217
memdelete(post_effects);
218
memdelete(glow);
219
memdelete(cubemap_filter);
220
memdelete(copy_effects);
221
memdelete(feed_effects);
222
memdelete(light_storage);
223
memdelete(particles_storage);
224
memdelete(mesh_storage);
225
memdelete(material_storage);
226
memdelete(texture_storage);
227
memdelete(utilities);
228
memdelete(config);
229
}
230
231
RasterizerGLES3 *RasterizerGLES3::singleton = nullptr;
232
233
#ifdef EGL_ENABLED
234
void *_egl_load_function_wrapper(const char *p_name) {
235
return (void *)eglGetProcAddress(p_name);
236
}
237
#endif
238
239
RasterizerGLES3::RasterizerGLES3() {
240
singleton = this;
241
242
#ifdef GLAD_ENABLED
243
bool glad_loaded = false;
244
245
#ifdef EGL_ENABLED
246
// There should be a more flexible system for getting the GL pointer, as
247
// different DisplayServers can have different ways. We can just use the GLAD
248
// version global to see if it loaded for now though, otherwise we fall back to
249
// the generic loader below.
250
#if defined(EGL_STATIC)
251
bool has_egl = true;
252
#else
253
bool has_egl = (eglGetProcAddress != nullptr);
254
#endif
255
256
if (gles_over_gl) {
257
if (has_egl && !glad_loaded && gladLoadGL((GLADloadfunc)&_egl_load_function_wrapper)) {
258
glad_loaded = true;
259
}
260
} else {
261
if (has_egl && !glad_loaded && gladLoadGLES2((GLADloadfunc)&_egl_load_function_wrapper)) {
262
glad_loaded = true;
263
}
264
}
265
#endif // EGL_ENABLED
266
267
if (gles_over_gl) {
268
if (!glad_loaded && gladLoaderLoadGL()) {
269
glad_loaded = true;
270
}
271
} else {
272
if (!glad_loaded && gladLoaderLoadGLES2()) {
273
glad_loaded = true;
274
}
275
}
276
277
// 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
278
// the members of this instance are initialized, so this just makes debugging harder. It should either crash here intentionally,
279
// or we need to actually test for this situation before constructing this.
280
ERR_FAIL_COND_MSG(!glad_loaded, "Error initializing GLAD.");
281
282
if (gles_over_gl) {
283
if (OS::get_singleton()->is_stdout_verbose()) {
284
if (GLAD_GL_ARB_debug_output) {
285
glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
286
glDebugMessageCallbackARB((GLDEBUGPROCARB)_gl_debug_print, nullptr);
287
glEnable(_EXT_DEBUG_OUTPUT);
288
} else {
289
print_line("OpenGL debugging not supported!");
290
}
291
}
292
}
293
#endif // GLAD_ENABLED
294
295
// For debugging
296
#ifdef CAN_DEBUG
297
#ifdef GL_API_ENABLED
298
if (gles_over_gl) {
299
if (OS::get_singleton()->is_stdout_verbose() && GLAD_GL_ARB_debug_output) {
300
glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_ERROR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
301
glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
302
glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
303
glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_PORTABILITY_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
304
glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_PERFORMANCE_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
305
glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_OTHER_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
306
}
307
}
308
#endif // GL_API_ENABLED
309
#ifdef GLES_API_ENABLED
310
if (!gles_over_gl) {
311
if (OS::get_singleton()->is_stdout_verbose()) {
312
DebugMessageCallbackARB callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallback");
313
if (!callback) {
314
callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallbackKHR");
315
}
316
317
if (callback) {
318
print_line("godot: ENABLING GL DEBUG");
319
glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
320
callback((DEBUGPROCARB)_gl_debug_print, nullptr);
321
glEnable(_EXT_DEBUG_OUTPUT);
322
}
323
}
324
}
325
#endif // GLES_API_ENABLED
326
#endif // CAN_DEBUG
327
328
{
329
String shader_cache_dir = Engine::get_singleton()->get_shader_cache_path();
330
if (shader_cache_dir.is_empty()) {
331
shader_cache_dir = "user://";
332
}
333
Ref<DirAccess> da = DirAccess::open(shader_cache_dir);
334
if (da.is_null()) {
335
ERR_PRINT("Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir);
336
} else {
337
Error err = da->change_dir("shader_cache");
338
if (err != OK) {
339
err = da->make_dir("shader_cache");
340
}
341
if (err != OK) {
342
ERR_PRINT("Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir);
343
} else {
344
shader_cache_dir = shader_cache_dir.path_join("shader_cache");
345
346
bool shader_cache_enabled = GLOBAL_GET("rendering/shader_compiler/shader_cache/enabled");
347
if (!Engine::get_singleton()->is_editor_hint() && !shader_cache_enabled) {
348
shader_cache_dir = String(); //disable only if not editor
349
}
350
351
if (!shader_cache_dir.is_empty()) {
352
ShaderGLES3::set_shader_cache_dir(shader_cache_dir);
353
}
354
}
355
}
356
}
357
358
// OpenGL needs to be initialized before initializing the Rasterizers
359
config = memnew(GLES3::Config);
360
utilities = memnew(GLES3::Utilities);
361
texture_storage = memnew(GLES3::TextureStorage);
362
material_storage = memnew(GLES3::MaterialStorage);
363
mesh_storage = memnew(GLES3::MeshStorage);
364
particles_storage = memnew(GLES3::ParticlesStorage);
365
light_storage = memnew(GLES3::LightStorage);
366
copy_effects = memnew(GLES3::CopyEffects);
367
cubemap_filter = memnew(GLES3::CubemapFilter);
368
glow = memnew(GLES3::Glow);
369
post_effects = memnew(GLES3::PostEffects);
370
feed_effects = memnew(GLES3::FeedEffects);
371
gi = memnew(GLES3::GI);
372
fog = memnew(GLES3::Fog);
373
canvas = memnew(RasterizerCanvasGLES3());
374
scene = memnew(RasterizerSceneGLES3());
375
376
// Disable OpenGL linear to sRGB conversion, because Godot will always do this conversion itself.
377
if (config->srgb_framebuffer_supported) {
378
glDisable(GL_FRAMEBUFFER_SRGB);
379
}
380
}
381
382
RasterizerGLES3::~RasterizerGLES3() {
383
}
384
385
void RasterizerGLES3::_blit_render_target_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen &p_blit, bool p_first) {
386
GLES3::RenderTarget *rt = GLES3::TextureStorage::get_singleton()->get_render_target(p_blit.render_target);
387
388
ERR_FAIL_NULL(rt);
389
390
// We normally render to the render target upside down, so flip Y when blitting to the screen.
391
bool flip_y = true;
392
bool linear_to_srgb = false;
393
if (rt->overridden.color.is_valid()) {
394
// If we've overridden the render target's color texture, that means we
395
// didn't render upside down, so we don't need to flip it.
396
// We're probably rendering directly to an XR device.
397
flip_y = false;
398
399
// It is 99% likely our texture uses the GL_SRGB8_ALPHA8 texture format in
400
// which case we have a GPU sRGB to Linear conversion on texture read.
401
// We need to counter this.
402
// Unfortunately we do not have an API to check this as Godot does not
403
// track this.
404
linear_to_srgb = true;
405
}
406
407
#ifdef WINDOWS_ENABLED
408
if (screen_flipped_y) {
409
flip_y = !flip_y;
410
}
411
#endif
412
413
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
414
415
if (p_first) {
416
if (p_blit.dst_rect.position != Vector2() || p_blit.dst_rect.size != rt->size) {
417
// Viewport doesn't cover entire window so clear window to black before blitting.
418
// Querying the actual window size from the DisplayServer would deadlock in separate render thread mode,
419
// so let's set the biggest viewport the implementation supports, to be sure the window is fully covered.
420
Size2i max_vp = GLES3::Utilities::get_singleton()->get_maximum_viewport_size();
421
glViewport(0, 0, max_vp[0], max_vp[1]);
422
glClearColor(0.0, 0.0, 0.0, 1.0);
423
glClear(GL_COLOR_BUFFER_BIT);
424
}
425
}
426
427
Vector2 screen_rect_end = p_blit.dst_rect.get_end();
428
429
Vector2 p1 = Vector2(p_blit.dst_rect.position.x, flip_y ? screen_rect_end.y : p_blit.dst_rect.position.y);
430
Vector2 p2 = Vector2(screen_rect_end.x, flip_y ? p_blit.dst_rect.position.y : screen_rect_end.y);
431
Vector2 size = p2 - p1;
432
433
Rect2 screenrect = Rect2(Vector2(0.0, flip_y ? 1.0 : 0.0), Vector2(1.0, flip_y ? -1.0 : 1.0));
434
435
glViewport(int(MIN(p1.x, p2.x)), int(MIN(p1.y, p2.y)), Math::abs(size.x), Math::abs(size.y));
436
437
glActiveTexture(GL_TEXTURE0);
438
GLenum target = rt->view_count > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
439
glBindTexture(target, rt->color);
440
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
441
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
442
443
glDisable(GL_CULL_FACE);
444
445
glEnable(GL_BLEND);
446
glBlendFunc(GL_ONE, GL_ZERO);
447
448
if (p_blit.lens_distortion.apply && (p_blit.lens_distortion.k1 != 0.0 || p_blit.lens_distortion.k2)) {
449
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);
450
} else if (rt->view_count > 1) {
451
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);
452
} else {
453
copy_effects->copy_to_rect(screenrect, linear_to_srgb);
454
}
455
456
glBindTexture(GL_TEXTURE_2D, 0);
457
}
458
459
// is this p_screen useless in a multi window environment?
460
void RasterizerGLES3::blit_render_targets_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen *p_render_targets, int p_amount) {
461
for (int i = 0; i < p_amount; i++) {
462
_blit_render_target_to_screen(p_screen, p_render_targets[i], i == 0);
463
}
464
}
465
466
void RasterizerGLES3::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
467
if (p_image.is_null() || p_image->is_empty()) {
468
return;
469
}
470
471
Size2i win_size = DisplayServer::get_singleton()->window_get_size();
472
473
glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
474
glViewport(0, 0, win_size.width, win_size.height);
475
glEnable(GL_BLEND);
476
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
477
glDepthMask(GL_FALSE);
478
glClearColor(p_color.r, p_color.g, p_color.b, OS::get_singleton()->is_layered_allowed() ? p_color.a : 1.0);
479
glClear(GL_COLOR_BUFFER_BIT);
480
481
RID texture = texture_storage->texture_allocate();
482
texture_storage->texture_2d_initialize(texture, p_image);
483
484
Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height());
485
Rect2 screenrect;
486
if (p_scale) {
487
if (win_size.width > win_size.height) {
488
//scale horizontally
489
screenrect.size.y = win_size.height;
490
screenrect.size.x = imgrect.size.x * win_size.height / imgrect.size.y;
491
screenrect.position.x = (win_size.width - screenrect.size.x) / 2;
492
493
} else {
494
//scale vertically
495
screenrect.size.x = win_size.width;
496
screenrect.size.y = imgrect.size.y * win_size.width / imgrect.size.x;
497
screenrect.position.y = (win_size.height - screenrect.size.y) / 2;
498
}
499
} else {
500
screenrect = imgrect;
501
screenrect.position += ((Size2(win_size.width, win_size.height) - screenrect.size) / 2.0).floor();
502
}
503
504
#ifdef WINDOWS_ENABLED
505
if (!screen_flipped_y)
506
#endif
507
{
508
// Flip Y.
509
screenrect.position.y = win_size.y - screenrect.position.y;
510
screenrect.size.y = -screenrect.size.y;
511
}
512
513
// Normalize texture coordinates to window size.
514
screenrect.position /= win_size;
515
screenrect.size /= win_size;
516
517
GLES3::Texture *t = texture_storage->get_texture(texture);
518
t->gl_set_filter(p_use_filter ? RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR : RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
519
glActiveTexture(GL_TEXTURE0);
520
glBindTexture(GL_TEXTURE_2D, t->tex_id);
521
copy_effects->copy_to_rect(screenrect);
522
glBindTexture(GL_TEXTURE_2D, 0);
523
524
gl_end_frame(true);
525
526
texture_storage->texture_free(texture);
527
}
528
529
#endif // GLES3_ENABLED
530
531