gl-cocoa.m 6.9 KB

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