gl-cocoa.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. };
  22. struct gl_platform {
  23. NSOpenGLContext *context;
  24. };
  25. static NSOpenGLContext *gl_context_create(void)
  26. {
  27. unsigned attrib_count = 0;
  28. #define ADD_ATTR(x) \
  29. { attributes[attrib_count++] = (NSOpenGLPixelFormatAttribute)x; }
  30. #define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
  31. NSOpenGLPixelFormatAttribute attributes[40];
  32. ADD_ATTR(NSOpenGLPFADoubleBuffer);
  33. ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
  34. ADD_ATTR(0);
  35. #undef ADD_ATTR2
  36. #undef ADD_ATTR
  37. NSOpenGLPixelFormat *pf;
  38. pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  39. if(!pf) {
  40. blog(LOG_ERROR, "Failed to create pixel format");
  41. return NULL;
  42. }
  43. NSOpenGLContext *context;
  44. context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
  45. [pf release];
  46. if(!context) {
  47. blog(LOG_ERROR, "Failed to create context");
  48. return NULL;
  49. }
  50. [context clearDrawable];
  51. return context;
  52. }
  53. struct gl_platform *gl_platform_create(gs_device_t *device, uint32_t adapter)
  54. {
  55. struct gl_platform *plat = bzalloc(sizeof(struct gl_platform));
  56. GLint interval = 0;
  57. plat->context = gl_context_create();
  58. if (!plat->context)
  59. goto fail;
  60. [plat->context makeCurrentContext];
  61. [plat->context setValues:&interval forParameter:NSOpenGLCPSwapInterval];
  62. if (!gladLoadGL())
  63. goto fail;
  64. return plat;
  65. fail:
  66. blog(LOG_ERROR, "gl_platform_create failed");
  67. gl_platform_destroy(plat);
  68. UNUSED_PARAMETER(device);
  69. UNUSED_PARAMETER(adapter);
  70. return NULL;
  71. }
  72. void gl_platform_destroy(struct gl_platform *platform)
  73. {
  74. if(!platform)
  75. return;
  76. [platform->context release];
  77. platform->context = nil;
  78. bfree(platform);
  79. }
  80. bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  81. {
  82. UNUSED_PARAMETER(swap);
  83. return true;
  84. }
  85. void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  86. {
  87. UNUSED_PARAMETER(swap);
  88. }
  89. struct gl_windowinfo *gl_windowinfo_create(const struct gs_init_data *info)
  90. {
  91. if(!info)
  92. return NULL;
  93. if(!info->window.view)
  94. return NULL;
  95. struct gl_windowinfo *wi = bzalloc(sizeof(struct gl_windowinfo));
  96. wi->view = info->window.view;
  97. [info->window.view setWantsBestResolutionOpenGLSurface:YES];
  98. return wi;
  99. }
  100. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  101. {
  102. if(!wi)
  103. return;
  104. wi->view = nil;
  105. bfree(wi);
  106. }
  107. void gl_update(gs_device_t *device)
  108. {
  109. [device->plat->context update];
  110. }
  111. void device_enter_context(gs_device_t *device)
  112. {
  113. [device->plat->context makeCurrentContext];
  114. }
  115. void device_leave_context(gs_device_t *device)
  116. {
  117. UNUSED_PARAMETER(device);
  118. [NSOpenGLContext clearCurrentContext];
  119. }
  120. void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  121. {
  122. if(device->cur_swap == swap)
  123. return;
  124. device->cur_swap = swap;
  125. if (swap) {
  126. [device->plat->context setView:swap->wi->view];
  127. } else {
  128. [device->plat->context clearDrawable];
  129. }
  130. }
  131. void device_present(gs_device_t *device)
  132. {
  133. [device->plat->context flushBuffer];
  134. }
  135. void gl_getclientsize(const struct gs_swap_chain *swap, uint32_t *width,
  136. uint32_t *height)
  137. {
  138. if(width) *width = swap->info.cx;
  139. if(height) *height = swap->info.cy;
  140. }
  141. gs_texture_t *device_texture_create_from_iosurface(gs_device_t *device,
  142. void *iosurf)
  143. {
  144. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  145. struct gs_texture_2d *tex = bzalloc(sizeof(struct gs_texture_2d));
  146. OSType pf = IOSurfaceGetPixelFormat(ref);
  147. if (pf != 'BGRA')
  148. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  149. pf >> 24, pf >> 16, pf >> 8, pf);
  150. const enum gs_color_format color_format = GS_BGRA;
  151. tex->base.device = device;
  152. tex->base.type = GS_TEXTURE_2D;
  153. tex->base.format = GS_BGRA;
  154. tex->base.levels = 1;
  155. tex->base.gl_format = convert_gs_format(color_format);
  156. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  157. tex->base.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  158. tex->base.gl_target = GL_TEXTURE_RECTANGLE;
  159. tex->base.is_dynamic = false;
  160. tex->base.is_render_target = false;
  161. tex->base.gen_mipmaps = false;
  162. tex->width = IOSurfaceGetWidth(ref);
  163. tex->height = IOSurfaceGetHeight(ref);
  164. if (!gl_gen_textures(1, &tex->base.texture))
  165. goto fail;
  166. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  167. goto fail;
  168. CGLError err = CGLTexImageIOSurface2D(
  169. [[NSOpenGLContext currentContext] CGLContextObj],
  170. tex->base.gl_target,
  171. tex->base.gl_internal_format,
  172. tex->width, tex->height,
  173. tex->base.gl_format,
  174. tex->base.gl_type,
  175. ref, 0);
  176. if(err != kCGLNoError) {
  177. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  178. " (device_texture_create_from_iosurface)",
  179. err, CGLErrorString(err));
  180. gl_success("CGLTexImageIOSurface2D");
  181. goto fail;
  182. }
  183. if (!gl_tex_param_i(tex->base.gl_target,
  184. GL_TEXTURE_MAX_LEVEL, 0))
  185. goto fail;
  186. if (!gl_bind_texture(tex->base.gl_target, 0))
  187. goto fail;
  188. return (gs_texture_t*)tex;
  189. fail:
  190. gs_texture_destroy((gs_texture_t*)tex);
  191. blog(LOG_ERROR, "device_texture_create_from_iosurface (GL) failed");
  192. return NULL;
  193. }
  194. bool gs_texture_rebind_iosurface(gs_texture_t *texture, void *iosurf)
  195. {
  196. if (!texture)
  197. return false;
  198. if (!iosurf)
  199. return false;
  200. struct gs_texture_2d *tex = (struct gs_texture_2d*)texture;
  201. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  202. OSType pf = IOSurfaceGetPixelFormat(ref);
  203. if (pf != 'BGRA')
  204. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  205. pf >> 24, pf >> 16, pf >> 8, pf);
  206. if (tex->width != IOSurfaceGetWidth(ref) ||
  207. tex->height != IOSurfaceGetHeight(ref))
  208. return false;
  209. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  210. return false;
  211. CGLError err = CGLTexImageIOSurface2D(
  212. [[NSOpenGLContext currentContext] CGLContextObj],
  213. tex->base.gl_target,
  214. tex->base.gl_internal_format,
  215. tex->width, tex->height,
  216. tex->base.gl_format,
  217. tex->base.gl_type,
  218. ref, 0);
  219. if(err != kCGLNoError) {
  220. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  221. " (gs_texture_rebind_iosurface)",
  222. err, CGLErrorString(err));
  223. gl_success("CGLTexImageIOSurface2D");
  224. return false;
  225. }
  226. if (!gl_bind_texture(tex->base.gl_target, 0))
  227. return false;
  228. return true;
  229. }