gl-cocoa.m 12 KB

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