gl-cocoa.m 12 KB

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