2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* rasterizer_gles3.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-03-05 14:47:28 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
#include "rasterizer_gles3.h"
|
2017-03-05 14:47:28 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/project_settings.h"
|
2018-08-24 06:47:34 +00:00
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
RasterizerStorage *RasterizerGLES3::get_storage() {
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
RasterizerCanvas *RasterizerGLES3::get_canvas() {
|
|
|
|
return canvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
RasterizerScene *RasterizerGLES3::get_scene() {
|
2016-10-19 14:14:41 +00:00
|
|
|
return scene;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 23:51:56 +00:00
|
|
|
#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_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
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2018-10-04 13:38:52 +00:00
|
|
|
#if defined(MINGW_ENABLED) || defined(_MSC_VER)
|
|
|
|
#define strcpy strcpy_s
|
|
|
|
#endif
|
|
|
|
|
2018-10-03 12:05:17 +00:00
|
|
|
#ifdef GLAD_ENABLED
|
|
|
|
// Restricting to GLAD as only used in initialize() with GLAD_GL_ARB_debug_output
|
2017-03-05 15:44:50 +00:00
|
|
|
static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const GLvoid *userParam) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (type == _EXT_DEBUG_TYPE_OTHER_ARB) {
|
2016-10-03 19:33:42 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (type == _EXT_DEBUG_TYPE_PERFORMANCE_ARB) {
|
2017-11-07 00:52:30 +00:00
|
|
|
return; //these are ultimately annoying, so removing for now
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-11-07 00:52:30 +00:00
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
char debSource[256], debType[256], debSev[256];
|
2021-05-05 10:44:11 +00:00
|
|
|
if (source == _EXT_DEBUG_SOURCE_API_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debSource, "OpenGL");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (source == _EXT_DEBUG_SOURCE_WINDOW_SYSTEM_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debSource, "Windows");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (source == _EXT_DEBUG_SOURCE_SHADER_COMPILER_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debSource, "Shader Compiler");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (source == _EXT_DEBUG_SOURCE_THIRD_PARTY_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debSource, "Third Party");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (source == _EXT_DEBUG_SOURCE_APPLICATION_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debSource, "Application");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (source == _EXT_DEBUG_SOURCE_OTHER_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debSource, "Other");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (type == _EXT_DEBUG_TYPE_ERROR_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debType, "Error");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (type == _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debType, "Deprecated behavior");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (type == _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debType, "Undefined behavior");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (type == _EXT_DEBUG_TYPE_PORTABILITY_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debType, "Portability");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (type == _EXT_DEBUG_TYPE_PERFORMANCE_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debType, "Performance");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (severity == _EXT_DEBUG_SEVERITY_HIGH_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debSev, "High");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (severity == _EXT_DEBUG_SEVERITY_MEDIUM_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debSev, "Medium");
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (severity == _EXT_DEBUG_SEVERITY_LOW_ARB) {
|
2017-03-05 15:44:50 +00:00
|
|
|
strcpy(debSev, "Low");
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
String output = String() + "GL ERROR: Source: " + debSource + "\tType: " + debType + "\tID: " + itos(id) + "\tSeverity: " + debSev + "\tMessage: " + message;
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2021-06-16 10:56:25 +00:00
|
|
|
ERR_PRINT(output);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
2018-10-03 12:05:17 +00:00
|
|
|
#endif // GLAD_ENABLED
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2016-11-22 23:51:56 +00:00
|
|
|
typedef void (*DEBUGPROCARB)(GLenum source,
|
2017-03-05 15:44:50 +00:00
|
|
|
GLenum type,
|
|
|
|
GLuint id,
|
|
|
|
GLenum severity,
|
|
|
|
GLsizei length,
|
|
|
|
const char *message,
|
|
|
|
const void *userParam);
|
2016-11-22 23:51:56 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
typedef void (*DebugMessageCallbackARB)(DEBUGPROCARB callback, const void *userParam);
|
2016-11-22 23:51:56 +00:00
|
|
|
|
2018-08-24 22:04:25 +00:00
|
|
|
Error RasterizerGLES3::is_viable() {
|
2017-11-19 19:47:53 +00:00
|
|
|
#ifdef GLAD_ENABLED
|
|
|
|
if (!gladLoadGL()) {
|
|
|
|
ERR_PRINT("Error initializing GLAD");
|
2018-08-24 22:04:25 +00:00
|
|
|
return ERR_UNAVAILABLE;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 19:47:53 +00:00
|
|
|
// GLVersion seems to be used for both GL and GL ES, so we need different version checks for them
|
|
|
|
#ifdef OPENGL_ENABLED // OpenGL 3.3 Core Profile required
|
2018-08-24 22:04:25 +00:00
|
|
|
if (GLVersion.major < 3 || (GLVersion.major == 3 && GLVersion.minor < 3)) {
|
2017-11-19 19:47:53 +00:00
|
|
|
#else // OpenGL ES 3.0
|
|
|
|
if (GLVersion.major < 3) {
|
|
|
|
#endif
|
2018-08-24 22:04:25 +00:00
|
|
|
return ERR_UNAVAILABLE;
|
2016-11-22 23:51:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 22:04:25 +00:00
|
|
|
#endif // GLAD_ENABLED
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RasterizerGLES3::initialize() {
|
|
|
|
print_verbose("Using GLES3 video driver");
|
|
|
|
|
|
|
|
#ifdef GLAD_ENABLED
|
2017-11-13 16:40:07 +00:00
|
|
|
if (OS::get_singleton()->is_stdout_verbose()) {
|
2018-03-05 18:30:04 +00:00
|
|
|
if (GLAD_GL_ARB_debug_output) {
|
|
|
|
glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
|
2021-05-04 14:00:45 +00:00
|
|
|
glDebugMessageCallbackARB(_gl_debug_print, nullptr);
|
2018-03-05 18:30:04 +00:00
|
|
|
glEnable(_EXT_DEBUG_OUTPUT);
|
|
|
|
} else {
|
|
|
|
print_line("OpenGL debugging not supported!");
|
|
|
|
}
|
2017-11-13 16:40:07 +00:00
|
|
|
}
|
2017-11-19 19:47:53 +00:00
|
|
|
#endif // GLAD_ENABLED
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2017-11-19 19:47:53 +00:00
|
|
|
/* // For debugging
|
2018-03-05 18:30:04 +00:00
|
|
|
if (GLAD_GL_ARB_debug_output) {
|
|
|
|
glDebugMessageControlARB(GL_DEBUG_SOURCE_API_ARB,GL_DEBUG_TYPE_ERROR_ARB,GL_DEBUG_SEVERITY_HIGH_ARB,0,NULL,GL_TRUE);
|
|
|
|
glDebugMessageControlARB(GL_DEBUG_SOURCE_API_ARB,GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB,GL_DEBUG_SEVERITY_HIGH_ARB,0,NULL,GL_TRUE);
|
|
|
|
glDebugMessageControlARB(GL_DEBUG_SOURCE_API_ARB,GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB,GL_DEBUG_SEVERITY_HIGH_ARB,0,NULL,GL_TRUE);
|
|
|
|
glDebugMessageControlARB(GL_DEBUG_SOURCE_API_ARB,GL_DEBUG_TYPE_PORTABILITY_ARB,GL_DEBUG_SEVERITY_HIGH_ARB,0,NULL,GL_TRUE);
|
|
|
|
glDebugMessageControlARB(GL_DEBUG_SOURCE_API_ARB,GL_DEBUG_TYPE_PERFORMANCE_ARB,GL_DEBUG_SEVERITY_HIGH_ARB,0,NULL,GL_TRUE);
|
|
|
|
glDebugMessageControlARB(GL_DEBUG_SOURCE_API_ARB,GL_DEBUG_TYPE_OTHER_ARB,GL_DEBUG_SEVERITY_HIGH_ARB,0,NULL,GL_TRUE);
|
|
|
|
glDebugMessageInsertARB(
|
|
|
|
GL_DEBUG_SOURCE_API_ARB,
|
|
|
|
GL_DEBUG_TYPE_OTHER_ARB, 1,
|
|
|
|
GL_DEBUG_SEVERITY_HIGH_ARB,5, "hello");
|
|
|
|
}
|
2017-11-19 19:47:53 +00:00
|
|
|
*/
|
2017-07-22 17:07:38 +00:00
|
|
|
|
2019-09-13 18:08:05 +00:00
|
|
|
print_line("OpenGL ES 3.0 Renderer: " + VisualServer::get_singleton()->get_video_adapter_name());
|
2016-10-03 19:33:42 +00:00
|
|
|
storage->initialize();
|
|
|
|
canvas->initialize();
|
2016-10-19 14:14:41 +00:00
|
|
|
scene->initialize();
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 06:11:43 +00:00
|
|
|
void RasterizerGLES3::begin_frame(double frame_step) {
|
2020-06-06 16:47:03 +00:00
|
|
|
time_total += frame_step * time_scale;
|
2017-01-02 01:16:52 +00:00
|
|
|
|
2018-05-09 06:11:43 +00:00
|
|
|
if (frame_step == 0) {
|
2017-08-08 11:31:34 +00:00
|
|
|
//to avoid hiccups
|
2018-05-09 06:11:43 +00:00
|
|
|
frame_step = 0.001;
|
2017-08-08 11:31:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 11:25:35 +00:00
|
|
|
double time_roll_over = GLOBAL_GET("rendering/limits/time/time_rollover_secs");
|
2020-04-29 20:33:03 +00:00
|
|
|
time_total = Math::fmod(time_total, time_roll_over);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
storage->frame.time[0] = time_total;
|
|
|
|
storage->frame.time[1] = Math::fmod(time_total, 3600);
|
|
|
|
storage->frame.time[2] = Math::fmod(time_total, 900);
|
|
|
|
storage->frame.time[3] = Math::fmod(time_total, 60);
|
2016-11-19 16:23:37 +00:00
|
|
|
storage->frame.count++;
|
2021-09-26 19:31:17 +00:00
|
|
|
storage->frame.shader_compiles_started = 0;
|
2018-05-09 06:11:43 +00:00
|
|
|
storage->frame.delta = frame_step;
|
2017-01-02 01:16:52 +00:00
|
|
|
|
2017-02-16 11:55:11 +00:00
|
|
|
storage->update_dirty_resources();
|
2016-10-10 21:31:01 +00:00
|
|
|
|
2017-06-11 21:13:04 +00:00
|
|
|
storage->info.render_final = storage->info.render;
|
|
|
|
storage->info.render.reset();
|
2016-12-21 17:20:35 +00:00
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
ShaderGLES3::current_frame = storage->frame.count;
|
|
|
|
|
2016-11-11 15:27:52 +00:00
|
|
|
scene->iteration();
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
2016-10-10 21:31:01 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RasterizerGLES3::set_current_render_target(RID p_render_target) {
|
2016-10-03 19:33:42 +00:00
|
|
|
if (!p_render_target.is_valid() && storage->frame.current_rt && storage->frame.clear_request) {
|
|
|
|
//handle pending clear request, if the framebuffer was not cleared
|
2017-03-05 15:44:50 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
|
2018-07-29 18:09:42 +00:00
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
glClearColor(
|
2017-03-05 15:44:50 +00:00
|
|
|
storage->frame.clear_request_color.r,
|
|
|
|
storage->frame.clear_request_color.g,
|
|
|
|
storage->frame.clear_request_color.b,
|
|
|
|
storage->frame.clear_request_color.a);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_render_target.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
RasterizerStorageGLES3::RenderTarget *rt = storage->render_target_owner.getornull(p_render_target);
|
|
|
|
storage->frame.current_rt = rt;
|
2017-08-25 15:14:33 +00:00
|
|
|
ERR_FAIL_COND(!rt);
|
2017-03-05 15:44:50 +00:00
|
|
|
storage->frame.clear_request = false;
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
glViewport(0, 0, rt->width, rt->height);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
} else {
|
2021-05-04 14:00:45 +00:00
|
|
|
storage->frame.current_rt = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
storage->frame.clear_request = false;
|
|
|
|
glViewport(0, 0, OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 13:03:49 +00:00
|
|
|
void RasterizerGLES3::restore_render_target(bool p_3d_was_drawn) {
|
2021-05-04 14:00:45 +00:00
|
|
|
ERR_FAIL_COND(storage->frame.current_rt == nullptr);
|
2017-03-05 15:44:50 +00:00
|
|
|
RasterizerStorageGLES3::RenderTarget *rt = storage->frame.current_rt;
|
2019-03-30 13:03:49 +00:00
|
|
|
if (p_3d_was_drawn && rt->external.fbo != 0) {
|
|
|
|
// our external render buffer is now leading, render 2d into that.
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, rt->external.fbo);
|
|
|
|
} else {
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
glViewport(0, 0, rt->width, rt->height);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RasterizerGLES3::clear_render_target(const Color &p_color) {
|
2016-10-03 19:33:42 +00:00
|
|
|
ERR_FAIL_COND(!storage->frame.current_rt);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
storage->frame.clear_request = true;
|
|
|
|
storage->frame.clear_request_color = p_color;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 20:37:19 +00:00
|
|
|
void RasterizerGLES3::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_image.is_null() || p_image->empty()) {
|
2017-04-09 23:02:04 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-04-09 23:02:04 +00:00
|
|
|
|
2018-05-09 06:11:43 +00:00
|
|
|
begin_frame(0.0);
|
2017-04-09 23:02:04 +00:00
|
|
|
|
|
|
|
int window_w = OS::get_singleton()->get_video_mode(0).width;
|
|
|
|
int window_h = OS::get_singleton()->get_video_mode(0).height;
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
|
|
|
|
glViewport(0, 0, window_w, window_h);
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glDepthMask(GL_FALSE);
|
2017-12-10 18:38:26 +00:00
|
|
|
if (OS::get_singleton()->get_window_per_pixel_transparency_enabled()) {
|
|
|
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
|
|
} else {
|
|
|
|
glClearColor(p_color.r, p_color.g, p_color.b, 1.0);
|
|
|
|
}
|
2017-04-09 23:02:04 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
canvas->canvas_begin();
|
|
|
|
|
|
|
|
RID texture = storage->texture_create();
|
2021-09-29 13:23:12 +00:00
|
|
|
storage->texture_allocate(texture, p_image->get_width(), p_image->get_height(), 0, p_image->get_format(), VS::TEXTURE_TYPE_2D, p_use_filter ? (uint32_t)VS::TEXTURE_FLAG_FILTER : 0);
|
2017-04-09 23:02:04 +00:00
|
|
|
storage->texture_set_data(texture, p_image);
|
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height());
|
2017-04-09 23:02:04 +00:00
|
|
|
Rect2 screenrect;
|
|
|
|
if (p_scale) {
|
|
|
|
if (window_w > window_h) {
|
|
|
|
//scale horizontally
|
|
|
|
screenrect.size.y = window_h;
|
|
|
|
screenrect.size.x = imgrect.size.x * window_h / imgrect.size.y;
|
2017-06-03 22:25:13 +00:00
|
|
|
screenrect.position.x = (window_w - screenrect.size.x) / 2;
|
2017-04-09 23:02:04 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
//scale vertically
|
|
|
|
screenrect.size.x = window_w;
|
|
|
|
screenrect.size.y = imgrect.size.y * window_w / imgrect.size.x;
|
2017-06-03 22:25:13 +00:00
|
|
|
screenrect.position.y = (window_h - screenrect.size.y) / 2;
|
2017-04-09 23:02:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
screenrect = imgrect;
|
2017-06-03 22:25:13 +00:00
|
|
|
screenrect.position += ((Size2(window_w, window_h) - screenrect.size) / 2.0).floor();
|
2017-04-09 23:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RasterizerStorageGLES3::Texture *t = storage->texture_owner.get(texture);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, t->tex_id);
|
|
|
|
canvas->draw_generic_textured_rect(screenrect, Rect2(0, 0, 1, 1));
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
canvas->canvas_end();
|
|
|
|
|
|
|
|
storage->free(texture); // free since it's only one frame that stays there
|
|
|
|
|
2018-07-25 14:34:25 +00:00
|
|
|
end_frame(true);
|
2017-04-09 23:02:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 16:47:03 +00:00
|
|
|
void RasterizerGLES3::set_shader_time_scale(float p_scale) {
|
|
|
|
time_scale = p_scale;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RasterizerGLES3::blit_render_target_to_screen(RID p_render_target, const Rect2 &p_screen_rect, int p_screen) {
|
|
|
|
ERR_FAIL_COND(storage->frame.current_rt);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
|
|
|
RasterizerStorageGLES3::RenderTarget *rt = storage->render_target_owner.getornull(p_render_target);
|
|
|
|
ERR_FAIL_COND(!rt);
|
|
|
|
|
2021-08-17 04:55:37 +00:00
|
|
|
if (rt->flags[RasterizerStorage::RENDER_TARGET_KEEP_3D_LINEAR]) {
|
|
|
|
// We need to add an sRGB conversion here as we kept our buffer linear (+ a little tone mapping).
|
|
|
|
|
|
|
|
canvas->_set_texture_rect_mode(true);
|
|
|
|
|
|
|
|
canvas->state.canvas_shader.set_custom_shader(0);
|
|
|
|
canvas->state.canvas_shader.set_conditional(CanvasShaderGLES3::LINEAR_TO_SRGB, true);
|
|
|
|
canvas->state.canvas_shader.bind();
|
|
|
|
|
|
|
|
canvas->canvas_begin();
|
|
|
|
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
|
|
|
// render to our framebuffer
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
|
|
|
|
|
|
|
|
// output our texture
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
if (rt->external.fbo != 0) {
|
|
|
|
glBindTexture(GL_TEXTURE_2D, rt->external.color);
|
|
|
|
} else {
|
|
|
|
glBindTexture(GL_TEXTURE_2D, rt->color);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->draw_generic_textured_rect(p_screen_rect, Rect2(0, 0, 1, -1));
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
canvas->canvas_end();
|
|
|
|
|
|
|
|
canvas->state.canvas_shader.set_conditional(CanvasShaderGLES3::LINEAR_TO_SRGB, false);
|
2019-03-30 13:03:49 +00:00
|
|
|
} else {
|
2021-08-17 04:55:37 +00:00
|
|
|
// No conversion needed, take the faster approach
|
|
|
|
|
|
|
|
Size2 win_size = OS::get_singleton()->get_window_size();
|
|
|
|
if (rt->external.fbo != 0) {
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, rt->external.fbo);
|
|
|
|
} else {
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, rt->fbo);
|
|
|
|
}
|
|
|
|
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
|
|
|
|
glBlitFramebuffer(0, 0, rt->width, rt->height, p_screen_rect.position.x, win_size.height - p_screen_rect.position.y - p_screen_rect.size.height, p_screen_rect.position.x + p_screen_rect.size.width, win_size.height - p_screen_rect.position.y, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
2019-03-30 13:03:49 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 20:51:50 +00:00
|
|
|
void RasterizerGLES3::output_lens_distorted_to_screen(RID p_render_target, const Rect2 &p_screen_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {
|
|
|
|
ERR_FAIL_COND(storage->frame.current_rt);
|
|
|
|
|
|
|
|
RasterizerStorageGLES3::RenderTarget *rt = storage->render_target_owner.getornull(p_render_target);
|
|
|
|
ERR_FAIL_COND(!rt);
|
|
|
|
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
|
|
|
// render to our framebuffer
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo);
|
|
|
|
|
|
|
|
// output our texture
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, rt->color);
|
|
|
|
|
|
|
|
canvas->draw_lens_distortion_rect(p_screen_rect, p_k1, p_k2, p_eye_center, p_oversample);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
}
|
|
|
|
|
2017-10-30 20:33:36 +00:00
|
|
|
void RasterizerGLES3::end_frame(bool p_swap_buffers) {
|
2017-12-10 18:38:26 +00:00
|
|
|
if (OS::get_singleton()->is_layered_allowed()) {
|
2020-05-10 12:40:19 +00:00
|
|
|
if (!OS::get_singleton()->get_window_per_pixel_transparency_enabled()) {
|
2017-12-10 18:38:26 +00:00
|
|
|
//clear alpha
|
|
|
|
glColorMask(false, false, false, true);
|
|
|
|
glClearColor(0, 0, 0, 1);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glColorMask(true, true, true, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:31:17 +00:00
|
|
|
ShaderGLES3::advance_async_shaders_compilation();
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_swap_buffers) {
|
2017-10-30 20:33:36 +00:00
|
|
|
OS::get_singleton()->swap_buffers();
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-10-30 20:33:36 +00:00
|
|
|
glFinish();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void RasterizerGLES3::finalize() {
|
2016-10-03 19:33:42 +00:00
|
|
|
storage->finalize();
|
|
|
|
canvas->finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Rasterizer *RasterizerGLES3::_create_current() {
|
2017-03-05 15:44:50 +00:00
|
|
|
return memnew(RasterizerGLES3);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RasterizerGLES3::make_current() {
|
2017-03-05 15:44:50 +00:00
|
|
|
_create_func = _create_current;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RasterizerGLES3::register_config() {
|
|
|
|
}
|
|
|
|
|
2021-06-21 09:11:33 +00:00
|
|
|
bool RasterizerGLES3::gl_check_errors() {
|
|
|
|
bool error_found = false;
|
|
|
|
GLenum error = glGetError();
|
|
|
|
while (error != GL_NO_ERROR) {
|
|
|
|
switch (error) {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
case GL_INVALID_ENUM: {
|
|
|
|
WARN_PRINT("GL_INVALID_ENUM: An unacceptable value is specified for an enumerated argument.");
|
|
|
|
} break;
|
|
|
|
case GL_INVALID_VALUE: {
|
|
|
|
WARN_PRINT("GL_INVALID_VALUE: A numeric argument is out of range.");
|
|
|
|
} break;
|
|
|
|
case GL_INVALID_OPERATION: {
|
|
|
|
WARN_PRINT("GL_INVALID_OPERATION: The specified operation is not allowed in the current state.");
|
|
|
|
} break;
|
|
|
|
case GL_INVALID_FRAMEBUFFER_OPERATION: {
|
|
|
|
WARN_PRINT("GL_INVALID_FRAMEBUFFER_OPERATION: The framebuffer object is not complete.");
|
|
|
|
} break;
|
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
case GL_OUT_OF_MEMORY: {
|
|
|
|
ERR_PRINT("GL_OUT_OF_MEMORY: There is not enough memory left to execute the command. The state of the GL is undefined.");
|
|
|
|
} break;
|
|
|
|
// GL_STACK_UNDERFLOW and GL_STACK_OVERFLOW are undefined in GLES2/gl2.h, which is used when not using GLAD.
|
|
|
|
//case GL_STACK_UNDERFLOW: {
|
|
|
|
// ERR_PRINT("GL_STACK_UNDERFLOW: An attempt has been made to perform an operation that would cause an internal stack to underflow.");
|
|
|
|
//} break;
|
|
|
|
//case GL_STACK_OVERFLOW: {
|
|
|
|
// ERR_PRINT("GL_STACK_OVERFLOW: An attempt has been made to perform an operation that would cause an internal stack to overflow.");
|
|
|
|
//} break;
|
|
|
|
default: {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
ERR_PRINT("Unrecognized GLError");
|
|
|
|
#endif // DEBUG_ENABLED
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
error_found = true;
|
|
|
|
error = glGetError();
|
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 06:49:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 09:11:33 +00:00
|
|
|
return error_found;
|
GLES3 fix normal map flipping with nvidia workaround
When not using TEXTURE_RECT path, flips have to sent via another method to the shader, to ensure that normal maps are correctly adjusted for direction. This PR adds an extra vertex attribute, LIGHT_ANGLE.
For nvidia workarounds, where the shader still has access to the final transform and extra matrix, the LIGHT_ANGLE can be 0 (no adjustment), 180 degrees for a horizontal flip, and negative indicates a vertical flip.
For batching path, the LIGHT_ANGLE can be used to directly specify the light angle for normal mapping, even when the final transform and extra matrix have been baked into vertex positions, so the same shader can be used for both.
2020-08-02 06:49:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
RasterizerGLES3::RasterizerGLES3() {
|
|
|
|
storage = memnew(RasterizerStorageGLES3);
|
|
|
|
canvas = memnew(RasterizerCanvasGLES3);
|
|
|
|
scene = memnew(RasterizerSceneGLES3);
|
|
|
|
canvas->storage = storage;
|
2017-06-27 01:58:03 +00:00
|
|
|
canvas->scene_render = scene;
|
2017-03-05 15:44:50 +00:00
|
|
|
storage->canvas = canvas;
|
|
|
|
scene->storage = storage;
|
|
|
|
storage->scene = scene;
|
2017-08-08 11:25:35 +00:00
|
|
|
|
|
|
|
time_total = 0;
|
2020-06-06 16:47:03 +00:00
|
|
|
time_scale = 1;
|
2021-09-26 19:31:17 +00:00
|
|
|
|
|
|
|
ShaderGLES3::compiles_started_this_frame = &storage->frame.shader_compiles_started;
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RasterizerGLES3::~RasterizerGLES3() {
|
|
|
|
memdelete(storage);
|
|
|
|
memdelete(canvas);
|
2018-03-13 01:40:47 +00:00
|
|
|
memdelete(scene);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|