123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- /******************************************************************************
- Copyright (C) 2013 by Ruwen Hahn <[email protected]>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- ******************************************************************************/
- #include "gl-subsystem.h"
- #include <OpenGL/OpenGL.h>
- #import <Cocoa/Cocoa.h>
- #import <AppKit/AppKit.h>
- struct gl_windowinfo {
- NSView *view;
- NSOpenGLContext *context;
- gs_texture_t *texture;
- GLuint fbo;
- };
- struct gl_platform {
- NSOpenGLContext *context;
- };
- typedef struct {
- gs_swapchain_t *swapchain;
- NSOpenGLContext *platformContext;
- NSOpenGLContext *swapchainContext;
- GLuint frameBufferObjectId;
- enum gs_color_format textureFormat;
- NSSize textureSize;
- } OBSFrameBufferUpdateData;
- static NSOpenGLContext *gl_context_create(NSOpenGLContext *share)
- {
- NSOpenGLPixelFormatAttribute attributes[] = {NSOpenGLPFADoubleBuffer, NSOpenGLPFAOpenGLProfile,
- NSOpenGLProfileVersion3_2Core, 0};
- NSOpenGLPixelFormat *pixelFormat;
- pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
- if (!pixelFormat) {
- blog(LOG_ERROR, "Failed to create pixel format");
- return nil;
- }
- NSOpenGLContext *context;
- context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:share];
- [pixelFormat release];
- if (!context) {
- blog(LOG_ERROR, "Failed to create context");
- return nil;
- }
- [context clearDrawable];
- return context;
- }
- struct gl_platform *gl_platform_create(gs_device_t *device __unused, uint32_t adapter __unused)
- {
- NSOpenGLContext *context = gl_context_create(nil);
- if (!context) {
- blog(LOG_ERROR, "gl_context_create failed");
- return NULL;
- }
- [context makeCurrentContext];
- GLint swapInterval = 0;
- [context setValues:&swapInterval forParameter:NSOpenGLContextParameterSwapInterval];
- const bool isOpenGLLoaded = gladLoadGL() != 0;
- if (!isOpenGLLoaded) {
- blog(LOG_ERROR, "gladLoadGL failed");
- [context release];
- return NULL;
- }
- struct gl_platform *platform = bzalloc(sizeof(struct gl_platform));
- platform->context = context;
- return platform;
- }
- void gl_platform_destroy(struct gl_platform *platform)
- {
- if (!platform)
- return;
- [platform->context release];
- platform->context = nil;
- bfree(platform);
- }
- bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
- {
- NSOpenGLContext *platformContext = swap->device->plat->context;
- NSOpenGLContext *swapContext = gl_context_create(platformContext);
- BOOL hasFrameBuffer = NO;
- if (swapContext) {
- CGLContextObj platformContextObj = platformContext.CGLContextObj;
- CGLContextObj swapContextObj = nil;
- CGLLockContext(platformContextObj);
- [platformContext makeCurrentContext];
- struct gs_init_data *initData = &swap->info;
- gs_texture_t *framebufferTexture = device_texture_create(swap->device, initData->cx, initData->cy,
- initData->format, 1, NULL, GS_RENDER_TARGET);
- if (!framebufferTexture) {
- blog(LOG_ERROR, "gl_platform_init_swapchain: Unable to generate backing texture for frame buffer.");
- goto init_swapchain_cleanup;
- }
- glFlush();
- [NSOpenGLContext clearCurrentContext];
- swapContextObj = swapContext.CGLContextObj;
- CGLLockContext(swapContextObj);
- [swapContext makeCurrentContext];
- #pragma clang diagnostic push
- #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
- [swapContext setView:swap->wi->view];
- #pragma clang diagnostic pop
- GLint swapInterval = 0;
- [swapContext setValues:&swapInterval forParameter:NSOpenGLContextParameterSwapInterval];
- GLuint framebufferObjectId = 0;
- BOOL hasGeneratedFramebuffer = gl_gen_framebuffers(1, &framebufferObjectId);
- BOOL hasBoundFrameBuffer = gl_bind_framebuffer(GL_FRAMEBUFFER, framebufferObjectId);
- if (!(hasGeneratedFramebuffer && hasBoundFrameBuffer)) {
- goto init_swapchain_cleanup;
- }
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebufferTexture->texture, 0);
- BOOL hasBoundFramebufferTexture = gl_success("glFrameBufferTexture2D");
- if (!hasBoundFramebufferTexture) {
- goto init_swapchain_cleanup;
- }
- glFlush();
- gl_bind_framebuffer(GL_FRAMEBUFFER, 0);
- hasFrameBuffer = YES;
- swap->wi->context = swapContext;
- swap->wi->fbo = framebufferObjectId;
- swap->wi->texture = framebufferTexture;
- init_swapchain_cleanup:
- [NSOpenGLContext clearCurrentContext];
- if (swapContextObj) {
- CGLUnlockContext(swapContextObj);
- }
- CGLUnlockContext(platformContextObj);
- }
- return (swapContext && hasFrameBuffer);
- }
- void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
- {
- NSOpenGLContext *platformContext = swap->device->plat->context;
- NSOpenGLContext *swapContext = swap->wi->context;
- gs_texture_t *framebufferTexture = swap->wi->texture;
- GLuint framebufferObjectId = swap->wi->fbo;
- if (platformContext && swapContext) {
- CGLContextObj platformContextObj = platformContext.CGLContextObj;
- CGLLockContext(platformContextObj);
- CGLContextObj swapContextObj = swapContext.CGLContextObj;
- CGLLockContext(swapContextObj);
- [swapContext makeCurrentContext];
- gl_delete_framebuffers(1, &framebufferObjectId);
- glFlush();
- [NSOpenGLContext clearCurrentContext];
- CGLUnlockContext(swapContextObj);
- [platformContext makeCurrentContext];
- gs_texture_destroy(framebufferTexture);
- glFlush();
- [NSOpenGLContext clearCurrentContext];
- CGLUnlockContext(platformContextObj);
- swap->wi->fbo = 0;
- swap->wi->texture = NULL;
- swap->wi->context = nil;
- }
- }
- struct gl_windowinfo *gl_windowinfo_create(const struct gs_init_data *info)
- {
- if (!(info && info->window.view)) {
- return NULL;
- }
- NSView *projectorView = info->window.view;
- #pragma clang diagnostic push
- #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
- projectorView.wantsBestResolutionOpenGLSurface = YES;
- #pragma clang diagnostic pop
- if (projectorView.window) {
- projectorView.window.colorSpace = NSColorSpace.sRGBColorSpace;
- }
- struct gl_windowinfo *windowInfo = bzalloc(sizeof(struct gl_windowinfo));
- windowInfo->view = projectorView;
- return windowInfo;
- }
- void gl_windowinfo_destroy(struct gl_windowinfo *windowInfo)
- {
- if (!windowInfo)
- return;
- windowInfo->view = nil;
- bfree(windowInfo);
- }
- void updateSwapchainFramebuffer(gs_device_t *device, OBSFrameBufferUpdateData updateData)
- {
- if (!(device && updateData.swapchain)) {
- return;
- }
- NSOpenGLContext *platformContext = updateData.platformContext;
- NSOpenGLContext *swapContext = updateData.swapchainContext;
- CGLContextObj platformContextObj = platformContext.CGLContextObj;
- CGLLockContext(platformContextObj);
- CGLContextObj swapContextObj = swapContext.CGLContextObj;
- CGLLockContext(swapContextObj);
- [swapContext makeCurrentContext];
- [swapContext update];
- gs_texture_t *framebufferTexture = device_texture_create(device, updateData.textureSize.width,
- updateData.textureSize.height, updateData.textureFormat, 1,
- NULL, GS_RENDER_TARGET);
- if (!framebufferTexture) {
- blog(LOG_ERROR, "gl_update (deferred): Unable to generate backing texture for frame buffer.");
- goto updateSwapchainCleanup;
- }
- BOOL hasBoundFramebuffer = gl_bind_framebuffer(GL_FRAMEBUFFER, updateData.frameBufferObjectId);
- if (!hasBoundFramebuffer) {
- goto updateSwapchainCleanup;
- }
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebufferTexture->texture, 0);
- BOOL hasBoundFramebufferTexture = gl_success("glFrameBufferTexture2D");
- if (!hasBoundFramebufferTexture) {
- GLenum framebufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
- if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE) {
- switch (framebufferStatus) {
- case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
- blog(LOG_ERROR, "gl_update (deferred): Framebuffer object attachment is not complete.");
- goto updateSwapchainCleanup;
- case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
- blog(LOG_ERROR, "gl_update (deferred): Framebuffer object attachment is missing.");
- goto updateSwapchainCleanup;
- default:
- blog(LOG_ERROR, "gl_update (deferred): Framebuffer object is incomplete.");
- goto updateSwapchainCleanup;
- }
- }
- goto updateSwapchainCleanup;
- }
- gl_bind_framebuffer(GL_FRAMEBUFFER, 0);
- glFlush();
- gs_swapchain_t *currentSwapchain = updateData.swapchain;
- if (currentSwapchain && currentSwapchain->wi && currentSwapchain->wi->texture) {
- gs_texture_t *priorFramebufferTexture = currentSwapchain->wi->texture;
- currentSwapchain->wi->texture = framebufferTexture;
- gs_texture_destroy(priorFramebufferTexture);
- }
- updateSwapchainCleanup:
- [NSOpenGLContext clearCurrentContext];
- CGLUnlockContext(swapContextObj);
- CGLUnlockContext(platformContextObj);
- }
- void gl_update(gs_device_t *device)
- {
- if (!(device && device->cur_swap)) {
- return;
- }
- gs_swapchain_t *currentSwapchain = device->cur_swap;
- NSOpenGLContext *platformContext = device->plat->context;
- NSOpenGLContext *swapContext = currentSwapchain->wi->context;
- OBSFrameBufferUpdateData updateData = {.swapchain = currentSwapchain,
- .platformContext = platformContext,
- .swapchainContext = swapContext,
- .frameBufferObjectId = currentSwapchain->wi->fbo,
- .textureFormat = currentSwapchain->info.format,
- .textureSize =
- NSMakeSize(currentSwapchain->info.cx, currentSwapchain->info.cy)};
- if (platformContext && swapContext) {
- [platformContext retain];
- [swapContext retain];
- dispatch_async(dispatch_get_main_queue(), ^() {
- updateSwapchainFramebuffer(device, updateData);
- [swapContext release];
- [platformContext release];
- });
- }
- }
- void gl_clear_context(gs_device_t *device __unused)
- {
- [NSOpenGLContext clearCurrentContext];
- }
- void device_enter_context(gs_device_t *device)
- {
- NSOpenGLContext *platformContext = device->plat->context;
- CGLLockContext(platformContext.CGLContextObj);
- [platformContext makeCurrentContext];
- }
- void device_leave_context(gs_device_t *device)
- {
- glFlush();
- [NSOpenGLContext clearCurrentContext];
- device->cur_vertex_buffer = NULL;
- device->cur_index_buffer = NULL;
- device->cur_render_target = NULL;
- device->cur_zstencil_buffer = NULL;
- device->cur_swap = NULL;
- device->cur_fbo = NULL;
- NSOpenGLContext *platformContext = device->plat->context;
- CGLUnlockContext(platformContext.CGLContextObj);
- }
- void *device_get_device_obj(gs_device_t *device)
- {
- return device->plat->context;
- }
- void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
- {
- if (device->cur_swap == swap)
- return;
- device->cur_swap = swap;
- if (swap) {
- device_set_render_target(device, swap->wi->texture, NULL);
- }
- }
- bool device_is_present_ready(gs_device_t *device __unused)
- {
- return true;
- }
- void device_present(gs_device_t *device)
- {
- if (!device) {
- blog(LOG_ERROR, "device_present: Called without valid device reference");
- return;
- }
- if (!(device->cur_swap && device->cur_swap->wi && device->cur_swap->wi->context)) {
- blog(LOG_ERROR, "device_present: Called without valid window info or NSOpenGLContext");
- return;
- }
- gs_swapchain_t *currentSwapchain = device->cur_swap;
- struct gl_windowinfo *info = currentSwapchain->wi;
- glFlush();
- NSOpenGLContext *currentContext = NSOpenGLContext.currentContext;
- NSOpenGLContext *swapChainContext = info->context;
- CGLContextObj swapChainContextObj = [swapChainContext CGLContextObj];
- [NSOpenGLContext clearCurrentContext];
- CGLLockContext(swapChainContextObj);
- [swapChainContext makeCurrentContext];
- bool hasBoundReadFramebuffer = gl_bind_framebuffer(GL_READ_FRAMEBUFFER, info->fbo);
- bool hasBoundDrawFramebuffer = gl_bind_framebuffer(GL_DRAW_FRAMEBUFFER, 0);
- if (!(hasBoundReadFramebuffer && hasBoundDrawFramebuffer)) {
- goto device_present_cleanup;
- }
- const UInt32 width = currentSwapchain->info.cx;
- const UInt32 height = currentSwapchain->info.cy;
- glBlitFramebuffer(0, 0, width, height, 0, height, width, 0, GL_COLOR_BUFFER_BIT, GL_NEAREST);
- bool didBlitFramebuffer = gl_success("glBlitFramebuffer");
- if (!didBlitFramebuffer) {
- goto device_present_cleanup;
- }
- [swapChainContext flushBuffer];
- glFlush();
- device_present_cleanup:
- CGLUnlockContext(swapChainContextObj);
- [NSOpenGLContext clearCurrentContext];
- [currentContext makeCurrentContext];
- }
- bool device_is_monitor_hdr(gs_device_t *device __unused, void *monitor __unused)
- {
- return false;
- }
- void gl_getclientsize(const struct gs_swap_chain *swap, uint32_t *width, uint32_t *height)
- {
- if (width)
- *width = swap->info.cx;
- if (height)
- *height = swap->info.cy;
- }
- gs_texture_t *device_texture_create_from_iosurface(gs_device_t *device, void *iosurf)
- {
- IOSurfaceRef ref = (IOSurfaceRef) iosurf;
- struct gs_texture_2d *tex = bzalloc(sizeof(struct gs_texture_2d));
- OSType pixelFormat = IOSurfaceGetPixelFormat(ref);
- GLenum color_format = 0;
- GLenum internal_format = 0;
- GLenum texture_type = 0;
- switch (pixelFormat) {
- case kCVPixelFormatType_ARGB2101010LEPacked: {
- color_format = GL_BGRA;
- internal_format = convert_gs_internal_format(GS_R10G10B10A2);
- texture_type = GL_UNSIGNED_INT_2_10_10_10_REV;
- break;
- }
- case kCVPixelFormatType_32BGRA: {
- color_format = convert_gs_format(GS_BGRA);
- internal_format = convert_gs_internal_format(GS_BGRA);
- texture_type = GL_UNSIGNED_INT_8_8_8_8_REV;
- break;
- }
- case 0:
- blog(LOG_ERROR, "Invalid IOSurface Buffer");
- goto fail;
- default:
- blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pixelFormat, pixelFormat >> 24, pixelFormat >> 16,
- pixelFormat >> 8, pixelFormat);
- goto fail;
- }
- tex->base.device = device;
- tex->base.type = GS_TEXTURE_2D;
- tex->base.format = color_format;
- tex->base.levels = 1;
- tex->base.gl_format = color_format;
- tex->base.gl_internal_format = internal_format;
- tex->base.gl_type = texture_type;
- tex->base.gl_target = GL_TEXTURE_RECTANGLE_ARB;
- tex->base.is_dynamic = false;
- tex->base.is_render_target = false;
- tex->base.gen_mipmaps = false;
- tex->width = (uint32_t) IOSurfaceGetWidth(ref);
- tex->height = (uint32_t) IOSurfaceGetHeight(ref);
- if (!gl_gen_textures(1, &tex->base.texture))
- goto fail;
- if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
- goto fail;
- CGLError err = CGLTexImageIOSurface2D([[NSOpenGLContext currentContext] CGLContextObj], tex->base.gl_target,
- tex->base.gl_internal_format, tex->width, tex->height, tex->base.gl_format,
- tex->base.gl_type, ref, 0);
- if (err != kCGLNoError) {
- blog(LOG_ERROR,
- "CGLTexImageIOSurface2D: %u, %s"
- " (device_texture_create_from_iosurface)",
- err, CGLErrorString(err));
- gl_success("CGLTexImageIOSurface2D");
- goto fail;
- }
- if (!gl_tex_param_i(tex->base.gl_target, GL_TEXTURE_MAX_LEVEL, 0))
- goto fail;
- if (!gl_bind_texture(tex->base.gl_target, 0))
- goto fail;
- return (gs_texture_t *) tex;
- fail:
- gs_texture_destroy((gs_texture_t *) tex);
- blog(LOG_ERROR, "device_texture_create_from_iosurface (GL) failed");
- return NULL;
- }
- gs_texture_t *device_texture_open_shared(gs_device_t *device, uint32_t handle)
- {
- gs_texture_t *texture = NULL;
- IOSurfaceRef ref = IOSurfaceLookupFromMachPort((mach_port_t) handle);
- texture = device_texture_create_from_iosurface(device, ref);
- CFRelease(ref);
- return texture;
- }
- bool device_shared_texture_available(void)
- {
- return true;
- }
- bool gs_texture_rebind_iosurface(gs_texture_t *texture, void *iosurf)
- {
- if (!texture)
- return false;
- if (!iosurf)
- return false;
- struct gs_texture_2d *tex = (struct gs_texture_2d *) texture;
- IOSurfaceRef ref = (IOSurfaceRef) iosurf;
- OSType pixelFormat = IOSurfaceGetPixelFormat(ref);
- switch (pixelFormat) {
- case kCVPixelFormatType_ARGB2101010LEPacked:
- case kCVPixelFormatType_32BGRA:
- break;
- case 0:
- blog(LOG_ERROR, "Invalid IOSurface Buffer");
- return false;
- default:
- blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pixelFormat, pixelFormat >> 24, pixelFormat >> 16,
- pixelFormat >> 8, pixelFormat);
- return false;
- }
- tex->width = (uint32_t) IOSurfaceGetWidth(ref);
- tex->height = (uint32_t) IOSurfaceGetHeight(ref);
- if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
- return false;
- CGLError err = CGLTexImageIOSurface2D([[NSOpenGLContext currentContext] CGLContextObj], tex->base.gl_target,
- tex->base.gl_internal_format, tex->width, tex->height, tex->base.gl_format,
- tex->base.gl_type, ref, 0);
- if (err != kCGLNoError) {
- blog(LOG_ERROR,
- "CGLTexImageIOSurface2D: %u, %s"
- " (gs_texture_rebind_iosurface)",
- err, CGLErrorString(err));
- gl_success("CGLTexImageIOSurface2D");
- return false;
- }
- if (!gl_bind_texture(tex->base.gl_target, 0))
- return false;
- return true;
- }
|