1
0

gl-cocoa.m 12 KB

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