gl-cocoa.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /******************************************************************************
  2. Copyright (C) 2013 by Ruwen Hahn <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "gl-subsystem.h"
  15. #include <OpenGL/OpenGL.h>
  16. #import <Cocoa/Cocoa.h>
  17. #import <AppKit/AppKit.h>
  18. struct gl_windowinfo {
  19. NSView *view;
  20. NSOpenGLContext *context;
  21. gs_texture_t *texture;
  22. GLuint fbo;
  23. };
  24. struct gl_platform {
  25. NSOpenGLContext *context;
  26. };
  27. static NSOpenGLContext *gl_context_create(NSOpenGLContext *share)
  28. {
  29. NSOpenGLPixelFormatAttribute attributes[] = {NSOpenGLPFADoubleBuffer, NSOpenGLPFAOpenGLProfile,
  30. NSOpenGLProfileVersion3_2Core, 0};
  31. NSOpenGLPixelFormat *pf;
  32. pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  33. if (!pf) {
  34. blog(LOG_ERROR, "Failed to create pixel format");
  35. return NULL;
  36. }
  37. NSOpenGLContext *context;
  38. context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:share];
  39. [pf release];
  40. if (!context) {
  41. blog(LOG_ERROR, "Failed to create context");
  42. return NULL;
  43. }
  44. [context clearDrawable];
  45. return context;
  46. }
  47. struct gl_platform *gl_platform_create(gs_device_t *device __unused, uint32_t adapter __unused)
  48. {
  49. NSOpenGLContext *context = gl_context_create(nil);
  50. if (!context) {
  51. blog(LOG_ERROR, "gl_context_create failed");
  52. return NULL;
  53. }
  54. [context makeCurrentContext];
  55. GLint interval = 0;
  56. [context setValues:&interval forParameter:NSOpenGLContextParameterSwapInterval];
  57. const bool success = gladLoadGL() != 0;
  58. if (!success) {
  59. blog(LOG_ERROR, "gladLoadGL failed");
  60. [context release];
  61. return NULL;
  62. }
  63. struct gl_platform *plat = bzalloc(sizeof(struct gl_platform));
  64. plat->context = context;
  65. return plat;
  66. }
  67. void gl_platform_destroy(struct gl_platform *platform)
  68. {
  69. if (!platform)
  70. return;
  71. [platform->context release];
  72. platform->context = nil;
  73. bfree(platform);
  74. }
  75. bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  76. {
  77. NSOpenGLContext *parent = swap->device->plat->context;
  78. NSOpenGLContext *context = gl_context_create(parent);
  79. bool success = context != nil;
  80. if (success) {
  81. CGLContextObj parent_obj = [parent CGLContextObj];
  82. CGLLockContext(parent_obj);
  83. [parent makeCurrentContext];
  84. struct gs_init_data *init_data = &swap->info;
  85. swap->wi->texture = device_texture_create(swap->device, init_data->cx, init_data->cy, init_data->format, 1,
  86. NULL, GS_RENDER_TARGET);
  87. glFlush();
  88. [NSOpenGLContext clearCurrentContext];
  89. CGLContextObj context_obj = [context CGLContextObj];
  90. CGLLockContext(context_obj);
  91. [context makeCurrentContext];
  92. #pragma clang diagnostic push
  93. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  94. [context setView:swap->wi->view];
  95. #pragma clang diagnostic pop
  96. GLint interval = 0;
  97. [context setValues:&interval forParameter:NSOpenGLContextParameterSwapInterval];
  98. gl_gen_framebuffers(1, &swap->wi->fbo);
  99. gl_bind_framebuffer(GL_FRAMEBUFFER, swap->wi->fbo);
  100. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, swap->wi->texture->texture, 0);
  101. gl_success("glFrameBufferTexture2D");
  102. glFlush();
  103. [NSOpenGLContext clearCurrentContext];
  104. CGLUnlockContext(context_obj);
  105. CGLUnlockContext(parent_obj);
  106. swap->wi->context = context;
  107. }
  108. return success;
  109. }
  110. void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  111. {
  112. NSOpenGLContext *parent = swap->device->plat->context;
  113. CGLContextObj parent_obj = [parent CGLContextObj];
  114. CGLLockContext(parent_obj);
  115. NSOpenGLContext *context = swap->wi->context;
  116. CGLContextObj context_obj = [context CGLContextObj];
  117. CGLLockContext(context_obj);
  118. [context makeCurrentContext];
  119. gl_delete_framebuffers(1, &swap->wi->fbo);
  120. glFlush();
  121. [NSOpenGLContext clearCurrentContext];
  122. CGLUnlockContext(context_obj);
  123. [parent makeCurrentContext];
  124. gs_texture_destroy(swap->wi->texture);
  125. glFlush();
  126. [NSOpenGLContext clearCurrentContext];
  127. swap->wi->context = nil;
  128. CGLUnlockContext(parent_obj);
  129. }
  130. struct gl_windowinfo *gl_windowinfo_create(const struct gs_init_data *info)
  131. {
  132. if (!info)
  133. return NULL;
  134. if (!info->window.view)
  135. return NULL;
  136. struct gl_windowinfo *wi = bzalloc(sizeof(struct gl_windowinfo));
  137. wi->view = info->window.view;
  138. wi->view.window.colorSpace = NSColorSpace.sRGBColorSpace;
  139. #pragma clang diagnostic push
  140. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  141. wi->view.wantsBestResolutionOpenGLSurface = YES;
  142. #pragma clang diagnostic pop
  143. return wi;
  144. }
  145. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  146. {
  147. if (!wi)
  148. return;
  149. wi->view = nil;
  150. bfree(wi);
  151. }
  152. void gl_update(gs_device_t *device)
  153. {
  154. gs_swapchain_t *swap = device->cur_swap;
  155. NSOpenGLContext *parent = device->plat->context;
  156. NSOpenGLContext *context = swap->wi->context;
  157. dispatch_async(dispatch_get_main_queue(), ^() {
  158. if (!swap || !swap->wi) {
  159. return;
  160. }
  161. CGLContextObj parent_obj = [parent CGLContextObj];
  162. CGLLockContext(parent_obj);
  163. CGLContextObj context_obj = [context CGLContextObj];
  164. CGLLockContext(context_obj);
  165. [context makeCurrentContext];
  166. [context update];
  167. struct gs_init_data *info = &swap->info;
  168. gs_texture_t *previous = swap->wi->texture;
  169. swap->wi->texture = device_texture_create(device, info->cx, info->cy, info->format, 1, NULL, GS_RENDER_TARGET);
  170. gl_bind_framebuffer(GL_FRAMEBUFFER, swap->wi->fbo);
  171. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, swap->wi->texture->texture, 0);
  172. gl_success("glFrameBufferTexture2D");
  173. gs_texture_destroy(previous);
  174. glFlush();
  175. [NSOpenGLContext clearCurrentContext];
  176. CGLUnlockContext(context_obj);
  177. CGLUnlockContext(parent_obj);
  178. });
  179. }
  180. void gl_clear_context(gs_device_t *device)
  181. {
  182. UNUSED_PARAMETER(device);
  183. [NSOpenGLContext clearCurrentContext];
  184. }
  185. void device_enter_context(gs_device_t *device)
  186. {
  187. CGLLockContext([device->plat->context CGLContextObj]);
  188. [device->plat->context makeCurrentContext];
  189. }
  190. void device_leave_context(gs_device_t *device)
  191. {
  192. glFlush();
  193. [NSOpenGLContext clearCurrentContext];
  194. device->cur_vertex_buffer = NULL;
  195. device->cur_index_buffer = NULL;
  196. device->cur_render_target = NULL;
  197. device->cur_zstencil_buffer = NULL;
  198. device->cur_swap = NULL;
  199. device->cur_fbo = NULL;
  200. CGLUnlockContext([device->plat->context CGLContextObj]);
  201. }
  202. void *device_get_device_obj(gs_device_t *device)
  203. {
  204. return device->plat->context;
  205. }
  206. void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  207. {
  208. if (device->cur_swap == swap)
  209. return;
  210. device->cur_swap = swap;
  211. if (swap) {
  212. device_set_render_target(device, swap->wi->texture, NULL);
  213. }
  214. }
  215. bool device_is_present_ready(gs_device_t *device __unused)
  216. {
  217. return true;
  218. }
  219. void device_present(gs_device_t *device)
  220. {
  221. glFlush();
  222. [NSOpenGLContext clearCurrentContext];
  223. CGLLockContext([device->cur_swap->wi->context CGLContextObj]);
  224. [device->cur_swap->wi->context makeCurrentContext];
  225. gl_bind_framebuffer(GL_READ_FRAMEBUFFER, device->cur_swap->wi->fbo);
  226. gl_bind_framebuffer(GL_DRAW_FRAMEBUFFER, 0);
  227. const uint32_t width = device->cur_swap->info.cx;
  228. const uint32_t height = device->cur_swap->info.cy;
  229. glBlitFramebuffer(0, 0, width, height, 0, height, width, 0, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  230. [device->cur_swap->wi->context flushBuffer];
  231. glFlush();
  232. [NSOpenGLContext clearCurrentContext];
  233. CGLUnlockContext([device->cur_swap->wi->context CGLContextObj]);
  234. [device->plat->context makeCurrentContext];
  235. }
  236. bool device_is_monitor_hdr(gs_device_t *device __unused, void *monitor __unused)
  237. {
  238. return false;
  239. }
  240. void gl_getclientsize(const struct gs_swap_chain *swap, uint32_t *width, uint32_t *height)
  241. {
  242. if (width)
  243. *width = swap->info.cx;
  244. if (height)
  245. *height = swap->info.cy;
  246. }
  247. gs_texture_t *device_texture_create_from_iosurface(gs_device_t *device, void *iosurf)
  248. {
  249. IOSurfaceRef ref = (IOSurfaceRef) iosurf;
  250. struct gs_texture_2d *tex = bzalloc(sizeof(struct gs_texture_2d));
  251. OSType pf = IOSurfaceGetPixelFormat(ref);
  252. FourCharCode l10r_code = 0;
  253. l10r_code = ('l' << 24) | ('1' << 16) | ('0' << 8) | 'r';
  254. FourCharCode bgra_code = 0;
  255. bgra_code = ('B' << 24) | ('G' << 16) | ('R' << 8) | 'A';
  256. const bool l10r = pf == l10r_code;
  257. if (pf == 0)
  258. blog(LOG_ERROR, "Invalid IOSurface Buffer");
  259. else if ((pf != bgra_code) && !l10r)
  260. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf, pf >> 24, pf >> 16, pf >> 8, pf);
  261. const enum gs_color_format color_format = l10r ? GS_R10G10B10A2 : GS_BGRA;
  262. tex->base.device = device;
  263. tex->base.type = GS_TEXTURE_2D;
  264. tex->base.format = color_format;
  265. tex->base.levels = 1;
  266. tex->base.gl_format = l10r ? GL_BGRA : convert_gs_format(color_format);
  267. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  268. tex->base.gl_type = l10r ? GL_UNSIGNED_INT_2_10_10_10_REV : GL_UNSIGNED_INT_8_8_8_8_REV;
  269. tex->base.gl_target = GL_TEXTURE_RECTANGLE_ARB;
  270. tex->base.is_dynamic = false;
  271. tex->base.is_render_target = false;
  272. tex->base.gen_mipmaps = false;
  273. tex->width = (uint32_t) IOSurfaceGetWidth(ref);
  274. tex->height = (uint32_t) IOSurfaceGetHeight(ref);
  275. if (!gl_gen_textures(1, &tex->base.texture))
  276. goto fail;
  277. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  278. goto fail;
  279. CGLError err = CGLTexImageIOSurface2D([[NSOpenGLContext currentContext] CGLContextObj], tex->base.gl_target,
  280. tex->base.gl_internal_format, tex->width, tex->height, tex->base.gl_format,
  281. tex->base.gl_type, ref, 0);
  282. if (err != kCGLNoError) {
  283. blog(LOG_ERROR,
  284. "CGLTexImageIOSurface2D: %u, %s"
  285. " (device_texture_create_from_iosurface)",
  286. err, CGLErrorString(err));
  287. gl_success("CGLTexImageIOSurface2D");
  288. goto fail;
  289. }
  290. if (!gl_tex_param_i(tex->base.gl_target, GL_TEXTURE_MAX_LEVEL, 0))
  291. goto fail;
  292. if (!gl_bind_texture(tex->base.gl_target, 0))
  293. goto fail;
  294. return (gs_texture_t *) tex;
  295. fail:
  296. gs_texture_destroy((gs_texture_t *) tex);
  297. blog(LOG_ERROR, "device_texture_create_from_iosurface (GL) failed");
  298. return NULL;
  299. }
  300. gs_texture_t *device_texture_open_shared(gs_device_t *device, uint32_t handle)
  301. {
  302. gs_texture_t *texture = NULL;
  303. IOSurfaceRef ref = IOSurfaceLookupFromMachPort((mach_port_t) handle);
  304. texture = device_texture_create_from_iosurface(device, ref);
  305. CFRelease(ref);
  306. return texture;
  307. }
  308. bool device_shared_texture_available(void)
  309. {
  310. return true;
  311. }
  312. bool gs_texture_rebind_iosurface(gs_texture_t *texture, void *iosurf)
  313. {
  314. if (!texture)
  315. return false;
  316. if (!iosurf)
  317. return false;
  318. FourCharCode l10r_code = 0;
  319. l10r_code = ('l' << 24) | ('1' << 16) | ('0' << 8) | 'r';
  320. FourCharCode bgra_code = 0;
  321. bgra_code = ('B' << 24) | ('G' << 16) | ('R' << 8) | 'A';
  322. struct gs_texture_2d *tex = (struct gs_texture_2d *) texture;
  323. IOSurfaceRef ref = (IOSurfaceRef) iosurf;
  324. OSType pf = IOSurfaceGetPixelFormat(ref);
  325. if (pf == 0) {
  326. blog(LOG_ERROR, "Invalid IOSurface buffer");
  327. } else if ((pf != bgra_code) && (pf != l10r_code)) {
  328. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf, pf >> 24, pf >> 16, pf >> 8, pf);
  329. }
  330. tex->width = (uint32_t) IOSurfaceGetWidth(ref);
  331. tex->height = (uint32_t) IOSurfaceGetHeight(ref);
  332. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  333. return false;
  334. CGLError err = CGLTexImageIOSurface2D([[NSOpenGLContext currentContext] CGLContextObj], tex->base.gl_target,
  335. tex->base.gl_internal_format, tex->width, tex->height, tex->base.gl_format,
  336. tex->base.gl_type, ref, 0);
  337. if (err != kCGLNoError) {
  338. blog(LOG_ERROR,
  339. "CGLTexImageIOSurface2D: %u, %s"
  340. " (gs_texture_rebind_iosurface)",
  341. err, CGLErrorString(err));
  342. gl_success("CGLTexImageIOSurface2D");
  343. return false;
  344. }
  345. if (!gl_bind_texture(tex->base.gl_target, 0))
  346. return false;
  347. return true;
  348. }