gl-cocoa.m 7.1 KB

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