1
0

gl-cocoa.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. typedef struct {
  28. gs_swapchain_t *swapchain;
  29. NSOpenGLContext *platformContext;
  30. NSOpenGLContext *swapchainContext;
  31. GLuint frameBufferObjectId;
  32. enum gs_color_format textureFormat;
  33. NSSize textureSize;
  34. } OBSFrameBufferUpdateData;
  35. static NSOpenGLContext *gl_context_create(NSOpenGLContext *share)
  36. {
  37. NSOpenGLPixelFormatAttribute attributes[] = {NSOpenGLPFADoubleBuffer, NSOpenGLPFAOpenGLProfile,
  38. NSOpenGLProfileVersion3_2Core, 0};
  39. NSOpenGLPixelFormat *pf;
  40. pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  41. if (!pf) {
  42. blog(LOG_ERROR, "Failed to create pixel format");
  43. return NULL;
  44. }
  45. NSOpenGLContext *context;
  46. context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:share];
  47. [pf release];
  48. if (!context) {
  49. blog(LOG_ERROR, "Failed to create context");
  50. return NULL;
  51. }
  52. [context clearDrawable];
  53. return context;
  54. }
  55. struct gl_platform *gl_platform_create(gs_device_t *device __unused, uint32_t adapter __unused)
  56. {
  57. NSOpenGLContext *context = gl_context_create(nil);
  58. if (!context) {
  59. blog(LOG_ERROR, "gl_context_create failed");
  60. return NULL;
  61. }
  62. [context makeCurrentContext];
  63. GLint interval = 0;
  64. [context setValues:&interval forParameter:NSOpenGLContextParameterSwapInterval];
  65. const bool success = gladLoadGL() != 0;
  66. if (!success) {
  67. blog(LOG_ERROR, "gladLoadGL failed");
  68. [context release];
  69. return NULL;
  70. }
  71. struct gl_platform *plat = bzalloc(sizeof(struct gl_platform));
  72. plat->context = context;
  73. return plat;
  74. }
  75. void gl_platform_destroy(struct gl_platform *platform)
  76. {
  77. if (!platform)
  78. return;
  79. [platform->context release];
  80. platform->context = nil;
  81. bfree(platform);
  82. }
  83. bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  84. {
  85. NSOpenGLContext *parent = swap->device->plat->context;
  86. NSOpenGLContext *context = gl_context_create(parent);
  87. bool success = context != nil;
  88. if (success) {
  89. CGLContextObj parent_obj = [parent CGLContextObj];
  90. CGLLockContext(parent_obj);
  91. [parent makeCurrentContext];
  92. struct gs_init_data *init_data = &swap->info;
  93. swap->wi->texture = device_texture_create(swap->device, init_data->cx, init_data->cy, init_data->format, 1,
  94. NULL, GS_RENDER_TARGET);
  95. glFlush();
  96. [NSOpenGLContext clearCurrentContext];
  97. CGLContextObj context_obj = [context CGLContextObj];
  98. CGLLockContext(context_obj);
  99. [context makeCurrentContext];
  100. #pragma clang diagnostic push
  101. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  102. [context setView:swap->wi->view];
  103. #pragma clang diagnostic pop
  104. GLint interval = 0;
  105. [context setValues:&interval forParameter:NSOpenGLContextParameterSwapInterval];
  106. gl_gen_framebuffers(1, &swap->wi->fbo);
  107. gl_bind_framebuffer(GL_FRAMEBUFFER, swap->wi->fbo);
  108. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 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. void updateSwapchainFramebuffer(gs_device_t *device, OBSFrameBufferUpdateData updateData)
  160. {
  161. if (!(device && updateData.swapchain)) {
  162. return;
  163. }
  164. NSOpenGLContext *platformContext = updateData.platformContext;
  165. NSOpenGLContext *swapContext = updateData.swapchainContext;
  166. CGLContextObj platformContextObj = platformContext.CGLContextObj;
  167. CGLLockContext(platformContextObj);
  168. CGLContextObj swapContextObj = swapContext.CGLContextObj;
  169. CGLLockContext(swapContextObj);
  170. [swapContext makeCurrentContext];
  171. [swapContext update];
  172. gs_texture_t *framebufferTexture = device_texture_create(device, updateData.textureSize.width,
  173. updateData.textureSize.height, updateData.textureFormat, 1,
  174. NULL, GS_RENDER_TARGET);
  175. if (!framebufferTexture) {
  176. blog(LOG_ERROR, "gl_update (deferred): Unable to generate backing texture for frame buffer.");
  177. goto updateSwapchainCleanup;
  178. }
  179. BOOL hasBoundFramebuffer = gl_bind_framebuffer(GL_FRAMEBUFFER, updateData.frameBufferObjectId);
  180. if (!hasBoundFramebuffer) {
  181. goto updateSwapchainCleanup;
  182. }
  183. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebufferTexture->texture, 0);
  184. BOOL hasBoundFramebufferTexture = gl_success("glFrameBufferTexture2D");
  185. if (!hasBoundFramebufferTexture) {
  186. GLenum framebufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  187. if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE) {
  188. switch (framebufferStatus) {
  189. case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  190. blog(LOG_ERROR, "gl_update (deferred): Framebuffer object attachment is not complete.");
  191. goto updateSwapchainCleanup;
  192. case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  193. blog(LOG_ERROR, "gl_update (deferred): Framebuffer object attachment is missing.");
  194. goto updateSwapchainCleanup;
  195. default:
  196. blog(LOG_ERROR, "gl_update (deferred): Framebuffer object is incomplete.");
  197. goto updateSwapchainCleanup;
  198. }
  199. }
  200. goto updateSwapchainCleanup;
  201. }
  202. gl_bind_framebuffer(GL_FRAMEBUFFER, 0);
  203. glFlush();
  204. gs_swapchain_t *currentSwapchain = updateData.swapchain;
  205. if (currentSwapchain && currentSwapchain->wi && currentSwapchain->wi->texture) {
  206. gs_texture_t *priorFramebufferTexture = currentSwapchain->wi->texture;
  207. currentSwapchain->wi->texture = framebufferTexture;
  208. gs_texture_destroy(priorFramebufferTexture);
  209. }
  210. updateSwapchainCleanup:
  211. [NSOpenGLContext clearCurrentContext];
  212. CGLUnlockContext(swapContextObj);
  213. CGLUnlockContext(platformContextObj);
  214. }
  215. void gl_update(gs_device_t *device)
  216. {
  217. if (!(device && device->cur_swap)) {
  218. return;
  219. }
  220. gs_swapchain_t *currentSwapchain = device->cur_swap;
  221. NSOpenGLContext *platformContext = device->plat->context;
  222. NSOpenGLContext *swapContext = currentSwapchain->wi->context;
  223. OBSFrameBufferUpdateData updateData = {.swapchain = currentSwapchain,
  224. .platformContext = platformContext,
  225. .swapchainContext = swapContext,
  226. .frameBufferObjectId = currentSwapchain->wi->fbo,
  227. .textureFormat = currentSwapchain->info.format,
  228. .textureSize =
  229. NSMakeSize(currentSwapchain->info.cx, currentSwapchain->info.cy)};
  230. if (platformContext && swapContext) {
  231. [platformContext retain];
  232. [swapContext retain];
  233. dispatch_async(dispatch_get_main_queue(), ^() {
  234. updateSwapchainFramebuffer(device, updateData);
  235. [swapContext release];
  236. [platformContext release];
  237. });
  238. }
  239. }
  240. void gl_clear_context(gs_device_t *device)
  241. {
  242. UNUSED_PARAMETER(device);
  243. [NSOpenGLContext clearCurrentContext];
  244. }
  245. void device_enter_context(gs_device_t *device)
  246. {
  247. CGLLockContext([device->plat->context CGLContextObj]);
  248. [device->plat->context makeCurrentContext];
  249. }
  250. void device_leave_context(gs_device_t *device)
  251. {
  252. glFlush();
  253. [NSOpenGLContext clearCurrentContext];
  254. device->cur_vertex_buffer = NULL;
  255. device->cur_index_buffer = NULL;
  256. device->cur_render_target = NULL;
  257. device->cur_zstencil_buffer = NULL;
  258. device->cur_swap = NULL;
  259. device->cur_fbo = NULL;
  260. CGLUnlockContext([device->plat->context CGLContextObj]);
  261. }
  262. void *device_get_device_obj(gs_device_t *device)
  263. {
  264. return device->plat->context;
  265. }
  266. void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  267. {
  268. if (device->cur_swap == swap)
  269. return;
  270. device->cur_swap = swap;
  271. if (swap) {
  272. device_set_render_target(device, swap->wi->texture, NULL);
  273. }
  274. }
  275. bool device_is_present_ready(gs_device_t *device __unused)
  276. {
  277. return true;
  278. }
  279. void device_present(gs_device_t *device)
  280. {
  281. glFlush();
  282. [NSOpenGLContext clearCurrentContext];
  283. CGLLockContext([device->cur_swap->wi->context CGLContextObj]);
  284. [device->cur_swap->wi->context makeCurrentContext];
  285. gl_bind_framebuffer(GL_READ_FRAMEBUFFER, device->cur_swap->wi->fbo);
  286. gl_bind_framebuffer(GL_DRAW_FRAMEBUFFER, 0);
  287. const uint32_t width = device->cur_swap->info.cx;
  288. const uint32_t height = device->cur_swap->info.cy;
  289. glBlitFramebuffer(0, 0, width, height, 0, height, width, 0, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  290. [device->cur_swap->wi->context flushBuffer];
  291. glFlush();
  292. [NSOpenGLContext clearCurrentContext];
  293. CGLUnlockContext([device->cur_swap->wi->context CGLContextObj]);
  294. [device->plat->context makeCurrentContext];
  295. }
  296. bool device_is_monitor_hdr(gs_device_t *device __unused, void *monitor __unused)
  297. {
  298. return false;
  299. }
  300. void gl_getclientsize(const struct gs_swap_chain *swap, uint32_t *width, uint32_t *height)
  301. {
  302. if (width)
  303. *width = swap->info.cx;
  304. if (height)
  305. *height = swap->info.cy;
  306. }
  307. gs_texture_t *device_texture_create_from_iosurface(gs_device_t *device, void *iosurf)
  308. {
  309. IOSurfaceRef ref = (IOSurfaceRef) iosurf;
  310. struct gs_texture_2d *tex = bzalloc(sizeof(struct gs_texture_2d));
  311. OSType pf = IOSurfaceGetPixelFormat(ref);
  312. FourCharCode l10r_code = 0;
  313. l10r_code = ('l' << 24) | ('1' << 16) | ('0' << 8) | 'r';
  314. FourCharCode bgra_code = 0;
  315. bgra_code = ('B' << 24) | ('G' << 16) | ('R' << 8) | 'A';
  316. const bool l10r = pf == l10r_code;
  317. if (pf == 0)
  318. blog(LOG_ERROR, "Invalid IOSurface Buffer");
  319. else if ((pf != bgra_code) && !l10r)
  320. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf, pf >> 24, pf >> 16, pf >> 8, pf);
  321. const enum gs_color_format color_format = l10r ? GS_R10G10B10A2 : GS_BGRA;
  322. tex->base.device = device;
  323. tex->base.type = GS_TEXTURE_2D;
  324. tex->base.format = color_format;
  325. tex->base.levels = 1;
  326. tex->base.gl_format = l10r ? GL_BGRA : convert_gs_format(color_format);
  327. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  328. tex->base.gl_type = l10r ? GL_UNSIGNED_INT_2_10_10_10_REV : GL_UNSIGNED_INT_8_8_8_8_REV;
  329. tex->base.gl_target = GL_TEXTURE_RECTANGLE_ARB;
  330. tex->base.is_dynamic = false;
  331. tex->base.is_render_target = false;
  332. tex->base.gen_mipmaps = false;
  333. tex->width = (uint32_t) IOSurfaceGetWidth(ref);
  334. tex->height = (uint32_t) IOSurfaceGetHeight(ref);
  335. if (!gl_gen_textures(1, &tex->base.texture))
  336. goto fail;
  337. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  338. goto fail;
  339. CGLError err = CGLTexImageIOSurface2D([[NSOpenGLContext currentContext] CGLContextObj], tex->base.gl_target,
  340. tex->base.gl_internal_format, tex->width, tex->height, tex->base.gl_format,
  341. tex->base.gl_type, ref, 0);
  342. if (err != kCGLNoError) {
  343. blog(LOG_ERROR,
  344. "CGLTexImageIOSurface2D: %u, %s"
  345. " (device_texture_create_from_iosurface)",
  346. err, CGLErrorString(err));
  347. gl_success("CGLTexImageIOSurface2D");
  348. goto fail;
  349. }
  350. if (!gl_tex_param_i(tex->base.gl_target, GL_TEXTURE_MAX_LEVEL, 0))
  351. goto fail;
  352. if (!gl_bind_texture(tex->base.gl_target, 0))
  353. goto fail;
  354. return (gs_texture_t *) tex;
  355. fail:
  356. gs_texture_destroy((gs_texture_t *) tex);
  357. blog(LOG_ERROR, "device_texture_create_from_iosurface (GL) failed");
  358. return NULL;
  359. }
  360. gs_texture_t *device_texture_open_shared(gs_device_t *device, uint32_t handle)
  361. {
  362. gs_texture_t *texture = NULL;
  363. IOSurfaceRef ref = IOSurfaceLookupFromMachPort((mach_port_t) handle);
  364. texture = device_texture_create_from_iosurface(device, ref);
  365. CFRelease(ref);
  366. return texture;
  367. }
  368. bool device_shared_texture_available(void)
  369. {
  370. return true;
  371. }
  372. bool gs_texture_rebind_iosurface(gs_texture_t *texture, void *iosurf)
  373. {
  374. if (!texture)
  375. return false;
  376. if (!iosurf)
  377. return false;
  378. FourCharCode l10r_code = 0;
  379. l10r_code = ('l' << 24) | ('1' << 16) | ('0' << 8) | 'r';
  380. FourCharCode bgra_code = 0;
  381. bgra_code = ('B' << 24) | ('G' << 16) | ('R' << 8) | 'A';
  382. struct gs_texture_2d *tex = (struct gs_texture_2d *) texture;
  383. IOSurfaceRef ref = (IOSurfaceRef) iosurf;
  384. OSType pf = IOSurfaceGetPixelFormat(ref);
  385. if (pf == 0) {
  386. blog(LOG_ERROR, "Invalid IOSurface buffer");
  387. } else if ((pf != bgra_code) && (pf != l10r_code)) {
  388. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf, pf >> 24, pf >> 16, pf >> 8, pf);
  389. }
  390. tex->width = (uint32_t) IOSurfaceGetWidth(ref);
  391. tex->height = (uint32_t) IOSurfaceGetHeight(ref);
  392. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  393. return false;
  394. CGLError err = CGLTexImageIOSurface2D([[NSOpenGLContext currentContext] CGLContextObj], tex->base.gl_target,
  395. tex->base.gl_internal_format, tex->width, tex->height, tex->base.gl_format,
  396. tex->base.gl_type, ref, 0);
  397. if (err != kCGLNoError) {
  398. blog(LOG_ERROR,
  399. "CGLTexImageIOSurface2D: %u, %s"
  400. " (gs_texture_rebind_iosurface)",
  401. err, CGLErrorString(err));
  402. gl_success("CGLTexImageIOSurface2D");
  403. return false;
  404. }
  405. if (!gl_bind_texture(tex->base.gl_target, 0))
  406. return false;
  407. return true;
  408. }