rasterizer_gles3.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*************************************************************************/
  2. /* rasterizer_gles3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 *utilities = GLES3::Utilities::get_singleton();
  90. utilities->info.render_final = utilities->info.render;
  91. utilities->info.render.reset();
  92. //scene->iteration();
  93. }
  94. void RasterizerGLES3::end_frame(bool p_swap_buffers) {
  95. // if (OS::get_singleton()->is_layered_allowed()) {
  96. // if (!OS::get_singleton()->get_window_per_pixel_transparency_enabled()) {
  97. //clear alpha
  98. // glColorMask(false, false, false, true);
  99. // glClearColor(0.5, 0, 0, 1);
  100. // glClear(GL_COLOR_BUFFER_BIT);
  101. // glColorMask(true, true, true, true);
  102. // }
  103. // }
  104. // glClearColor(1, 0, 0, 1);
  105. // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  106. if (p_swap_buffers) {
  107. DisplayServer::get_singleton()->swap_buffers();
  108. } else {
  109. glFinish();
  110. }
  111. }
  112. #ifdef CAN_DEBUG
  113. static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const GLvoid *userParam) {
  114. if (type == _EXT_DEBUG_TYPE_OTHER_ARB) {
  115. return;
  116. }
  117. if (type == _EXT_DEBUG_TYPE_PERFORMANCE_ARB) {
  118. return; //these are ultimately annoying, so removing for now
  119. }
  120. char debSource[256], debType[256], debSev[256];
  121. if (source == _EXT_DEBUG_SOURCE_API_ARB) {
  122. strcpy(debSource, "OpenGL");
  123. } else if (source == _EXT_DEBUG_SOURCE_WINDOW_SYSTEM_ARB) {
  124. strcpy(debSource, "Windows");
  125. } else if (source == _EXT_DEBUG_SOURCE_SHADER_COMPILER_ARB) {
  126. strcpy(debSource, "Shader Compiler");
  127. } else if (source == _EXT_DEBUG_SOURCE_THIRD_PARTY_ARB) {
  128. strcpy(debSource, "Third Party");
  129. } else if (source == _EXT_DEBUG_SOURCE_APPLICATION_ARB) {
  130. strcpy(debSource, "Application");
  131. } else if (source == _EXT_DEBUG_SOURCE_OTHER_ARB) {
  132. strcpy(debSource, "Other");
  133. }
  134. if (type == _EXT_DEBUG_TYPE_ERROR_ARB) {
  135. strcpy(debType, "Error");
  136. } else if (type == _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB) {
  137. strcpy(debType, "Deprecated behavior");
  138. } else if (type == _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB) {
  139. strcpy(debType, "Undefined behavior");
  140. } else if (type == _EXT_DEBUG_TYPE_PORTABILITY_ARB) {
  141. strcpy(debType, "Portability");
  142. } else if (type == _EXT_DEBUG_TYPE_PERFORMANCE_ARB) {
  143. strcpy(debType, "Performance");
  144. } else if (type == _EXT_DEBUG_TYPE_OTHER_ARB) {
  145. strcpy(debType, "Other");
  146. }
  147. if (severity == _EXT_DEBUG_SEVERITY_HIGH_ARB) {
  148. strcpy(debSev, "High");
  149. } else if (severity == _EXT_DEBUG_SEVERITY_MEDIUM_ARB) {
  150. strcpy(debSev, "Medium");
  151. } else if (severity == _EXT_DEBUG_SEVERITY_LOW_ARB) {
  152. strcpy(debSev, "Low");
  153. }
  154. String output = String() + "GL ERROR: Source: " + debSource + "\tType: " + debType + "\tID: " + itos(id) + "\tSeverity: " + debSev + "\tMessage: " + message;
  155. ERR_PRINT(output);
  156. }
  157. #endif
  158. typedef void (*DEBUGPROCARB)(GLenum source,
  159. GLenum type,
  160. GLuint id,
  161. GLenum severity,
  162. GLsizei length,
  163. const char *message,
  164. const void *userParam);
  165. typedef void (*DebugMessageCallbackARB)(DEBUGPROCARB callback, const void *userParam);
  166. void RasterizerGLES3::initialize() {
  167. print_line("OpenGL Renderer: " + RS::get_singleton()->get_video_adapter_name());
  168. }
  169. void RasterizerGLES3::finalize() {
  170. memdelete(scene);
  171. memdelete(canvas);
  172. memdelete(gi);
  173. memdelete(fog);
  174. memdelete(copy_effects);
  175. memdelete(light_storage);
  176. memdelete(particles_storage);
  177. memdelete(mesh_storage);
  178. memdelete(material_storage);
  179. memdelete(texture_storage);
  180. memdelete(utilities);
  181. memdelete(config);
  182. }
  183. RasterizerGLES3::RasterizerGLES3() {
  184. #ifdef GLAD_ENABLED
  185. if (!gladLoadGL()) {
  186. ERR_PRINT("Error initializing GLAD");
  187. // 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
  188. // the members of this instance are initialized, so this just makes debugging harder. It should either crash here intentionally,
  189. // or we need to actually test for this situation before constructing this.
  190. return;
  191. }
  192. #endif
  193. #ifdef GLAD_ENABLED
  194. if (OS::get_singleton()->is_stdout_verbose()) {
  195. if (GLAD_GL_ARB_debug_output) {
  196. glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
  197. glDebugMessageCallbackARB(_gl_debug_print, nullptr);
  198. glEnable(_EXT_DEBUG_OUTPUT);
  199. } else {
  200. print_line("OpenGL debugging not supported!");
  201. }
  202. }
  203. #endif // GLAD_ENABLED
  204. // For debugging
  205. #ifdef CAN_DEBUG
  206. #ifdef GLES_OVER_GL
  207. if (OS::get_singleton()->is_stdout_verbose() && GLAD_GL_ARB_debug_output) {
  208. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_ERROR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  209. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  210. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  211. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_PORTABILITY_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  212. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_PERFORMANCE_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  213. glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_OTHER_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, nullptr, GL_TRUE);
  214. // glDebugMessageInsertARB(
  215. // GL_DEBUG_SOURCE_API_ARB,
  216. // GL_DEBUG_TYPE_OTHER_ARB, 1,
  217. // GL_DEBUG_SEVERITY_HIGH_ARB, 5, "hello");
  218. }
  219. #else
  220. if (OS::get_singleton()->is_stdout_verbose()) {
  221. DebugMessageCallbackARB callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallback");
  222. if (!callback) {
  223. callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallbackKHR");
  224. }
  225. if (callback) {
  226. print_line("godot: ENABLING GL DEBUG");
  227. glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
  228. callback(_gl_debug_print, NULL);
  229. glEnable(_EXT_DEBUG_OUTPUT);
  230. }
  231. }
  232. #endif // GLES_OVER_GL
  233. #endif // CAN_DEBUG
  234. // OpenGL needs to be initialized before initializing the Rasterizers
  235. config = memnew(GLES3::Config);
  236. utilities = memnew(GLES3::Utilities);
  237. texture_storage = memnew(GLES3::TextureStorage);
  238. material_storage = memnew(GLES3::MaterialStorage);
  239. mesh_storage = memnew(GLES3::MeshStorage);
  240. particles_storage = memnew(GLES3::ParticlesStorage);
  241. light_storage = memnew(GLES3::LightStorage);
  242. copy_effects = memnew(GLES3::CopyEffects);
  243. gi = memnew(GLES3::GI);
  244. fog = memnew(GLES3::Fog);
  245. canvas = memnew(RasterizerCanvasGLES3());
  246. scene = memnew(RasterizerSceneGLES3());
  247. }
  248. RasterizerGLES3::~RasterizerGLES3() {
  249. }
  250. void RasterizerGLES3::prepare_for_blitting_render_targets() {
  251. }
  252. void RasterizerGLES3::_blit_render_target_to_screen(RID p_render_target, DisplayServer::WindowID p_screen, const Rect2 &p_screen_rect) {
  253. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  254. GLES3::RenderTarget *rt = texture_storage->get_render_target(p_render_target);
  255. ERR_FAIL_COND(!rt);
  256. // TODO: do we need a keep 3d linear option?
  257. // Make sure we are drawing to the right context.
  258. DisplayServer::get_singleton()->gl_window_make_current(p_screen);
  259. if (rt->external.fbo != 0) {
  260. glBindFramebuffer(GL_READ_FRAMEBUFFER, rt->external.fbo);
  261. } else {
  262. glBindFramebuffer(GL_READ_FRAMEBUFFER, rt->fbo);
  263. }
  264. glReadBuffer(GL_COLOR_ATTACHMENT0);
  265. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  266. // Flip content upside down to correct for coordinates.
  267. glBlitFramebuffer(0, 0, rt->size.x, rt->size.y, 0, p_screen_rect.size.y, p_screen_rect.size.x, 0, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  268. }
  269. // is this p_screen useless in a multi window environment?
  270. void RasterizerGLES3::blit_render_targets_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen *p_render_targets, int p_amount) {
  271. // All blits are going to the system framebuffer, so just bind once.
  272. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  273. for (int i = 0; i < p_amount; i++) {
  274. const BlitToScreen &blit = p_render_targets[i];
  275. RID rid_rt = blit.render_target;
  276. Rect2 dst_rect = blit.dst_rect;
  277. _blit_render_target_to_screen(rid_rt, p_screen, dst_rect);
  278. }
  279. }
  280. void RasterizerGLES3::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
  281. if (p_image.is_null() || p_image->is_empty()) {
  282. return;
  283. }
  284. Size2i win_size = DisplayServer::get_singleton()->screen_get_size();
  285. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  286. glViewport(0, 0, win_size.width, win_size.height);
  287. glDisable(GL_BLEND);
  288. glDepthMask(GL_FALSE);
  289. if (false) {
  290. // if (OS::get_singleton()->get_window_per_pixel_transparency_enabled()) {
  291. glClearColor(0.0, 0.0, 0.0, 0.0);
  292. } else {
  293. glClearColor(p_color.r, p_color.g, p_color.b, 1.0);
  294. }
  295. glClear(GL_COLOR_BUFFER_BIT);
  296. RID texture = texture_storage->texture_allocate();
  297. texture_storage->texture_2d_initialize(texture, p_image);
  298. Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height());
  299. Rect2 screenrect;
  300. if (p_scale) {
  301. if (win_size.width > win_size.height) {
  302. //scale horizontally
  303. screenrect.size.y = win_size.height;
  304. screenrect.size.x = imgrect.size.x * win_size.height / imgrect.size.y;
  305. screenrect.position.x = (win_size.width - screenrect.size.x) / 2;
  306. } else {
  307. //scale vertically
  308. screenrect.size.x = win_size.width;
  309. screenrect.size.y = imgrect.size.y * win_size.width / imgrect.size.x;
  310. screenrect.position.y = (win_size.height - screenrect.size.y) / 2;
  311. }
  312. } else {
  313. screenrect = imgrect;
  314. screenrect.position += ((Size2(win_size.width, win_size.height) - screenrect.size) / 2.0).floor();
  315. }
  316. GLES3::Texture *t = texture_storage->get_texture(texture);
  317. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 1);
  318. glBindTexture(GL_TEXTURE_2D, t->tex_id);
  319. glBindTexture(GL_TEXTURE_2D, 0);
  320. texture_storage->texture_free(texture);
  321. end_frame(true);
  322. }
  323. #endif // GLES3_ENABLED