rasterizer_gles3.cpp 15 KB

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