gl-cocoa.m 12 KB

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