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/os/os.h"
39#include "storage/texture_storage.h"
40
41#define _EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB 0x8242
42#define _EXT_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH_ARB 0x8243
43#define _EXT_DEBUG_CALLBACK_FUNCTION_ARB 0x8244
44#define _EXT_DEBUG_CALLBACK_USER_PARAM_ARB 0x8245
45#define _EXT_DEBUG_SOURCE_API_ARB 0x8246
46#define _EXT_DEBUG_SOURCE_WINDOW_SYSTEM_ARB 0x8247
47#define _EXT_DEBUG_SOURCE_SHADER_COMPILER_ARB 0x8248
48#define _EXT_DEBUG_SOURCE_THIRD_PARTY_ARB 0x8249
49#define _EXT_DEBUG_SOURCE_APPLICATION_ARB 0x824A
50#define _EXT_DEBUG_SOURCE_OTHER_ARB 0x824B
51#define _EXT_DEBUG_TYPE_ERROR_ARB 0x824C
52#define _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB 0x824D
53#define _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB 0x824E
54#define _EXT_DEBUG_TYPE_PORTABILITY_ARB 0x824F
55#define _EXT_DEBUG_TYPE_PERFORMANCE_ARB 0x8250
56#define _EXT_DEBUG_TYPE_OTHER_ARB 0x8251
57#define _EXT_MAX_DEBUG_MESSAGE_LENGTH_ARB 0x9143
58#define _EXT_MAX_DEBUG_LOGGED_MESSAGES_ARB 0x9144
59#define _EXT_DEBUG_LOGGED_MESSAGES_ARB 0x9145
60#define _EXT_DEBUG_SEVERITY_HIGH_ARB 0x9146
61#define _EXT_DEBUG_SEVERITY_MEDIUM_ARB 0x9147
62#define _EXT_DEBUG_SEVERITY_LOW_ARB 0x9148
63#define _EXT_DEBUG_OUTPUT 0x92E0
64
65#ifndef GLAPIENTRY
66#if defined(WINDOWS_ENABLED)
67#define GLAPIENTRY APIENTRY
68#else
69#define GLAPIENTRY
70#endif
71#endif
72
73#if !defined(IOS_ENABLED) && !defined(WEB_ENABLED)
74// We include EGL below to get debug callback on GLES2 platforms,
75// but EGL is not available on iOS.
76#define CAN_DEBUG
77#endif
78
79#if !defined(GLES_OVER_GL) && defined(CAN_DEBUG)
80#include <GLES3/gl3.h>
81#include <GLES3/gl3ext.h>
82#include <GLES3/gl3platform.h>
83
84#include <EGL/egl.h>
85#include <EGL/eglext.h>
86#endif
87
88#if defined(MINGW_ENABLED) || defined(_MSC_VER)
89#define strcpy strcpy_s
90#endif
91
92void RasterizerGLES3::begin_frame(double frame_step) {
93 frame++;
94 delta = frame_step;
95
96 time_total += frame_step;
97
98 double time_roll_over = GLOBAL_GET("rendering/limits/time/time_rollover_secs");
99 time_total = Math::fmod(time_total, time_roll_over);
100
101 canvas->set_time(time_total);
102 scene->set_time(time_total, frame_step);
103
104 GLES3::Utilities *utils = GLES3::Utilities::get_singleton();
105 utils->_capture_timestamps_begin();
106
107 //scene->iteration();
108}
109
110void RasterizerGLES3::end_frame(bool p_swap_buffers) {
111 if (p_swap_buffers) {
112 DisplayServer::get_singleton()->swap_buffers();
113 } else {
114 glFinish();
115 }
116}
117
118#ifdef CAN_DEBUG
119static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const GLvoid *userParam) {
120 if (type == _EXT_DEBUG_TYPE_OTHER_ARB) {
121 return;
122 }
123
124 if (type == _EXT_DEBUG_TYPE_PERFORMANCE_ARB) {
125 return; //these are ultimately annoying, so removing for now
126 }
127
128 char debSource[256], debType[256], debSev[256];
129
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 }
143
144 if (type == _EXT_DEBUG_TYPE_ERROR_ARB) {
145 strcpy(debType, "Error");
146 } else if (type == _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB) {
147 strcpy(debType, "Deprecated behavior");
148 } else if (type == _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB) {
149 strcpy(debType, "Undefined behavior");
150 } else if (type == _EXT_DEBUG_TYPE_PORTABILITY_ARB) {
151 strcpy(debType, "Portability");
152 } else if (type == _EXT_DEBUG_TYPE_PERFORMANCE_ARB) {
153 strcpy(debType, "Performance");
154 } else if (type == _EXT_DEBUG_TYPE_OTHER_ARB) {
155 strcpy(debType, "Other");
156 }
157
158 if (severity == _EXT_DEBUG_SEVERITY_HIGH_ARB) {
159 strcpy(debSev, "High");
160 } else if (severity == _EXT_DEBUG_SEVERITY_MEDIUM_ARB) {
161 strcpy(debSev, "Medium");
162 } else if (severity == _EXT_DEBUG_SEVERITY_LOW_ARB) {
163 strcpy(debSev, "Low");
164 }
165
166 String output = String() + "GL ERROR: Source: " + debSource + "\tType: " + debType + "\tID: " + itos(id) + "\tSeverity: " + debSev + "\tMessage: " + message;
167
168 ERR_PRINT(output);
169}
170#endif
171
172typedef void (*DEBUGPROCARB)(GLenum source,
173 GLenum type,
174 GLuint id,
175 GLenum severity,
176 GLsizei length,
177 const char *message,
178 const void *userParam);
179
180typedef void (*DebugMessageCallbackARB)(DEBUGPROCARB callback, const void *userParam);
181
182void RasterizerGLES3::initialize() {
183 print_line(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()));
184}
185
186void RasterizerGLES3::finalize() {
187 memdelete(scene);
188 memdelete(canvas);
189 memdelete(gi);
190 memdelete(fog);
191 memdelete(copy_effects);
192 memdelete(light_storage);
193 memdelete(particles_storage);
194 memdelete(mesh_storage);
195 memdelete(material_storage);
196 memdelete(texture_storage);
197 memdelete(utilities);
198 memdelete(config);
199}
200
201RasterizerGLES3 *RasterizerGLES3::singleton = nullptr;
202
203RasterizerGLES3::RasterizerGLES3() {
204 singleton = this;
205
206#ifdef GLAD_ENABLED
207 if (!gladLoaderLoadGL()) {
208 ERR_PRINT("Error initializing GLAD");
209 // 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
210 // the members of this instance are initialized, so this just makes debugging harder. It should either crash here intentionally,
211 // or we need to actually test for this situation before constructing this.
212 return;
213 }
214#endif
215
216#ifdef GLAD_ENABLED
217 if (OS::get_singleton()->is_stdout_verbose()) {
218 if (GLAD_GL_ARB_debug_output) {
219 glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
220 glDebugMessageCallbackARB(_gl_debug_print, nullptr);
221 glEnable(_EXT_DEBUG_OUTPUT);
222 } else {
223 print_line("OpenGL debugging not supported!");
224 }
225 }
226#endif // GLAD_ENABLED
227
228 // For debugging
229#ifdef CAN_DEBUG
230#ifdef GLES_OVER_GL
231 if (OS::get_singleton()->is_stdout_verbose() && GLAD_GL_ARB_debug_output) {
232 glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_ERROR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
233 glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
234 glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
235 glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_PORTABILITY_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
236 glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_PERFORMANCE_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
237 glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_OTHER_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
238 // glDebugMessageInsertARB(
239 // GL_DEBUG_SOURCE_API_ARB,
240 // GL_DEBUG_TYPE_OTHER_ARB, 1,
241 // GL_DEBUG_SEVERITY_HIGH_ARB, 5, "hello");
242 }
243#else
244 if (OS::get_singleton()->is_stdout_verbose()) {
245 DebugMessageCallbackARB callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallback");
246 if (!callback) {
247 callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallbackKHR");
248 }
249
250 if (callback) {
251 print_line("godot: ENABLING GL DEBUG");
252 glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
253 callback(_gl_debug_print, NULL);
254 glEnable(_EXT_DEBUG_OUTPUT);
255 }
256 }
257#endif // GLES_OVER_GL
258#endif // CAN_DEBUG
259
260 {
261 String shader_cache_dir = Engine::get_singleton()->get_shader_cache_path();
262 if (shader_cache_dir.is_empty()) {
263 shader_cache_dir = "user://";
264 }
265 Ref<DirAccess> da = DirAccess::open(shader_cache_dir);
266 if (da.is_null()) {
267 ERR_PRINT("Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir);
268 } else {
269 Error err = da->change_dir("shader_cache");
270 if (err != OK) {
271 err = da->make_dir("shader_cache");
272 }
273 if (err != OK) {
274 ERR_PRINT("Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir);
275 } else {
276 shader_cache_dir = shader_cache_dir.path_join("shader_cache");
277
278 bool shader_cache_enabled = GLOBAL_GET("rendering/shader_compiler/shader_cache/enabled");
279 if (!Engine::get_singleton()->is_editor_hint() && !shader_cache_enabled) {
280 shader_cache_dir = String(); //disable only if not editor
281 }
282
283 if (!shader_cache_dir.is_empty()) {
284 ShaderGLES3::set_shader_cache_dir(shader_cache_dir);
285 }
286 }
287 }
288 }
289
290 // OpenGL needs to be initialized before initializing the Rasterizers
291 config = memnew(GLES3::Config);
292 utilities = memnew(GLES3::Utilities);
293 texture_storage = memnew(GLES3::TextureStorage);
294 material_storage = memnew(GLES3::MaterialStorage);
295 mesh_storage = memnew(GLES3::MeshStorage);
296 particles_storage = memnew(GLES3::ParticlesStorage);
297 light_storage = memnew(GLES3::LightStorage);
298 copy_effects = memnew(GLES3::CopyEffects);
299 gi = memnew(GLES3::GI);
300 fog = memnew(GLES3::Fog);
301 canvas = memnew(RasterizerCanvasGLES3());
302 scene = memnew(RasterizerSceneGLES3());
303}
304
305RasterizerGLES3::~RasterizerGLES3() {
306}
307
308void RasterizerGLES3::prepare_for_blitting_render_targets() {
309 // This is a hack, but this function is called one time after all viewports have been updated.
310 // So it marks the end of the frame for all viewports
311 // In the OpenGL renderer we have to call end_frame for each viewport so we can swap the
312 // buffers for each window before proceeding to the next.
313 // This allows us to only increment the frame after all viewports are done.
314 GLES3::Utilities *utils = GLES3::Utilities::get_singleton();
315 utils->capture_timestamps_end();
316}
317
318void RasterizerGLES3::_blit_render_target_to_screen(RID p_render_target, DisplayServer::WindowID p_screen, const Rect2 &p_screen_rect, uint32_t p_layer, bool p_first) {
319 GLES3::RenderTarget *rt = GLES3::TextureStorage::get_singleton()->get_render_target(p_render_target);
320
321 ERR_FAIL_NULL(rt);
322
323 // We normally render to the render target upside down, so flip Y when blitting to the screen.
324 bool flip_y = true;
325 if (rt->overridden.color.is_valid()) {
326 // If we've overridden the render target's color texture, that means we
327 // didn't render upside down, so we don't need to flip it.
328 // We're probably rendering directly to an XR device.
329 flip_y = false;
330 }
331
332 GLuint read_fbo = 0;
333 glGenFramebuffers(1, &read_fbo);
334 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_fbo);
335
336 if (rt->view_count > 1) {
337 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rt->color, 0, p_layer);
338 } else {
339 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color, 0);
340 }
341
342 glReadBuffer(GL_COLOR_ATTACHMENT0);
343 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
344
345 if (p_first) {
346 Size2i win_size = DisplayServer::get_singleton()->window_get_size();
347 if (p_screen_rect.position != Vector2() || p_screen_rect.size != rt->size) {
348 // Viewport doesn't cover entire window so clear window to black before blitting.
349 glViewport(0, 0, win_size.width, win_size.height);
350 glClearColor(0.0, 0.0, 0.0, 1.0);
351 glClear(GL_COLOR_BUFFER_BIT);
352 }
353 }
354
355 Vector2i screen_rect_end = p_screen_rect.get_end();
356 glBlitFramebuffer(0, 0, rt->size.x, rt->size.y,
357 p_screen_rect.position.x, flip_y ? screen_rect_end.y : p_screen_rect.position.y, screen_rect_end.x, flip_y ? p_screen_rect.position.y : screen_rect_end.y,
358 GL_COLOR_BUFFER_BIT, GL_NEAREST);
359
360 if (read_fbo != 0) {
361 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
362 glDeleteFramebuffers(1, &read_fbo);
363 }
364}
365
366// is this p_screen useless in a multi window environment?
367void RasterizerGLES3::blit_render_targets_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen *p_render_targets, int p_amount) {
368 for (int i = 0; i < p_amount; i++) {
369 const BlitToScreen &blit = p_render_targets[i];
370
371 RID rid_rt = blit.render_target;
372
373 Rect2 dst_rect = blit.dst_rect;
374 _blit_render_target_to_screen(rid_rt, p_screen, dst_rect, blit.multi_view.use_layer ? blit.multi_view.layer : 0, i == 0);
375 }
376}
377
378void RasterizerGLES3::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
379 if (p_image.is_null() || p_image->is_empty()) {
380 return;
381 }
382
383 Size2i win_size = DisplayServer::get_singleton()->window_get_size();
384
385 glBindFramebuffer(GL_FRAMEBUFFER, 0);
386 glViewport(0, 0, win_size.width, win_size.height);
387 glEnable(GL_BLEND);
388 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
389 glDepthMask(GL_FALSE);
390 glClearColor(p_color.r, p_color.g, p_color.b, 1.0);
391 glClear(GL_COLOR_BUFFER_BIT);
392
393 RID texture = texture_storage->texture_allocate();
394 texture_storage->texture_2d_initialize(texture, p_image);
395
396 Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height());
397 Rect2 screenrect;
398 if (p_scale) {
399 if (win_size.width > win_size.height) {
400 //scale horizontally
401 screenrect.size.y = win_size.height;
402 screenrect.size.x = imgrect.size.x * win_size.height / imgrect.size.y;
403 screenrect.position.x = (win_size.width - screenrect.size.x) / 2;
404
405 } else {
406 //scale vertically
407 screenrect.size.x = win_size.width;
408 screenrect.size.y = imgrect.size.y * win_size.width / imgrect.size.x;
409 screenrect.position.y = (win_size.height - screenrect.size.y) / 2;
410 }
411 } else {
412 screenrect = imgrect;
413 screenrect.position += ((Size2(win_size.width, win_size.height) - screenrect.size) / 2.0).floor();
414 }
415
416 // Flip Y.
417 screenrect.position.y = win_size.y - screenrect.position.y;
418 screenrect.size.y = -screenrect.size.y;
419
420 // Normalize texture coordinates to window size.
421 screenrect.position /= win_size;
422 screenrect.size /= win_size;
423
424 GLES3::Texture *t = texture_storage->get_texture(texture);
425 t->gl_set_filter(p_use_filter ? RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR : RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
426 glActiveTexture(GL_TEXTURE0);
427 glBindTexture(GL_TEXTURE_2D, t->tex_id);
428 copy_effects->copy_to_rect(screenrect);
429 glBindTexture(GL_TEXTURE_2D, 0);
430
431 end_frame(true);
432
433 texture_storage->texture_free(texture);
434}
435
436#endif // GLES3_ENABLED
437