gl-cocoa.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/glew.h>
  15. #include "gl-subsystem.h"
  16. #include <OpenGL/OpenGL.h>
  17. #import <Cocoa/Cocoa.h>
  18. #import <AppKit/AppKit.h>
  19. //#include "util/darray.h"
  20. struct gl_windowinfo {
  21. NSView *view;
  22. };
  23. struct gl_platform {
  24. NSOpenGLContext *context;
  25. struct gs_swap_chain swap;
  26. };
  27. static NSOpenGLContext *gl_context_create(struct gs_init_data *info)
  28. {
  29. unsigned attrib_count = 0;
  30. #define ADD_ATTR(x) \
  31. { attributes[attrib_count++] = (NSOpenGLPixelFormatAttribute)x; }
  32. #define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
  33. NSOpenGLPixelFormatAttribute attributes[40];
  34. switch(info->num_backbuffers) {
  35. case 0:
  36. break;
  37. case 1:
  38. ADD_ATTR(NSOpenGLPFADoubleBuffer);
  39. break;
  40. case 2:
  41. ADD_ATTR(NSOpenGLPFATripleBuffer);
  42. break;
  43. default:
  44. blog(LOG_ERROR, "Requested backbuffers (%d) not "
  45. "supported", info->num_backbuffers);
  46. }
  47. ADD_ATTR(NSOpenGLPFAClosestPolicy);
  48. ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
  49. int color_bits = 0;//get_color_format_bits(info->format);
  50. if(color_bits == 0) color_bits = 24;
  51. else if(color_bits < 15) color_bits = 15;
  52. ADD_ATTR2(NSOpenGLPFAColorSize, color_bits);
  53. ADD_ATTR2(NSOpenGLPFAAlphaSize, 8);
  54. ADD_ATTR2(NSOpenGLPFADepthSize, 16);
  55. ADD_ATTR(0);
  56. #undef ADD_ATTR2
  57. #undef ADD_ATTR
  58. NSOpenGLPixelFormat *pf;
  59. pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  60. if(!pf) {
  61. blog(LOG_ERROR, "Failed to create pixel format");
  62. return NULL;
  63. }
  64. NSOpenGLContext *context;
  65. context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
  66. [pf release];
  67. if(!context) {
  68. blog(LOG_ERROR, "Failed to create context");
  69. return NULL;
  70. }
  71. [context setView:info->window.view];
  72. return context;
  73. }
  74. static inline void required_extension_error(const char *extension)
  75. {
  76. blog(LOG_ERROR, "OpenGL extension %s is required", extension);
  77. }
  78. static bool gl_init_extensions(device_t device)
  79. {
  80. glewExperimental=true;
  81. GLenum error = glewInit();
  82. if(error != GLEW_OK) {
  83. blog(LOG_ERROR, "glewInit failed, %u\n%s\n", error,
  84. glewGetErrorString(error));
  85. return false;
  86. }
  87. if(!GLEW_VERSION_2_1) {
  88. blog(LOG_ERROR, "OpenGL 2.1 minimum required by the graphics "
  89. "adapter");
  90. return false;
  91. }
  92. if(!GLEW_ARB_framebuffer_object) {
  93. required_extension_error("GL_ARB_framebuffer_object");
  94. return false;
  95. }
  96. if(!GLEW_ARB_separate_shader_objects) {
  97. required_extension_error("GL_ARB_separate_shader_objects");
  98. return false;
  99. }
  100. //something inside glew produces error code 1280 (invalid enum)
  101. glGetError();
  102. device->copy_type = COPY_TYPE_FBO_BLIT;
  103. return true;
  104. }
  105. static bool gl_init_default_swap(struct gl_platform *plat, device_t dev,
  106. struct gs_init_data *info)
  107. {
  108. if(!(plat->context = gl_context_create(info)))
  109. return false;
  110. plat->swap.device = dev;
  111. plat->swap.info = *info;
  112. plat->swap.wi = gl_windowinfo_create(info);
  113. return plat->swap.wi != NULL;
  114. }
  115. struct gl_platform *gl_platform_create(device_t device,
  116. struct gs_init_data *info)
  117. {
  118. struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
  119. memset(plat, 0, sizeof(struct gl_platform));
  120. if(!gl_init_default_swap(plat, device, info))
  121. goto fail;
  122. [plat->context makeCurrentContext];
  123. if(!gl_init_extensions(device))
  124. goto fail;
  125. if (GLEW_ARB_seamless_cube_map) {
  126. glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  127. gl_success("GL_TEXTURE_CUBE_MAP_SEAMLESS");
  128. }
  129. return plat;
  130. fail:
  131. blog(LOG_ERROR, "gl_platform_create failed");
  132. gl_platform_destroy(plat);
  133. return NULL;
  134. }
  135. struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform)
  136. {
  137. if(platform)
  138. return &platform->swap;
  139. return NULL;
  140. }
  141. void gl_platform_destroy(struct gl_platform *platform)
  142. {
  143. if(!platform)
  144. return;
  145. [platform->context release];
  146. platform->context = nil;
  147. gl_windowinfo_destroy(platform->swap.wi);
  148. bfree(platform);
  149. }
  150. struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info)
  151. {
  152. if(!info)
  153. return NULL;
  154. if(!info->window.view)
  155. return NULL;
  156. struct gl_windowinfo *wi = bmalloc(sizeof(struct gl_windowinfo));
  157. memset(wi, 0, sizeof(struct gl_windowinfo));
  158. wi->view = info->window.view;
  159. return wi;
  160. }
  161. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  162. {
  163. if(!wi)
  164. return;
  165. wi->view = nil;
  166. bfree(wi);
  167. }
  168. void gl_update(device_t device)
  169. {
  170. [device->plat->context update];
  171. }
  172. void device_entercontext(device_t device)
  173. {
  174. CGLLockContext([device->plat->context CGLContextObj]);
  175. [device->plat->context makeCurrentContext];
  176. }
  177. void device_leavecontext(device_t device)
  178. {
  179. [NSOpenGLContext clearCurrentContext];
  180. CGLUnlockContext([device->plat->context CGLContextObj]);
  181. }
  182. void device_load_swapchain(device_t device, swapchain_t swap)
  183. {
  184. if(!swap)
  185. swap = &device->plat->swap;
  186. if(device->cur_swap == swap)
  187. return;
  188. device->cur_swap = swap;
  189. }
  190. void device_present(device_t device)
  191. {
  192. [device->plat->context flushBuffer];
  193. }
  194. void gl_getclientsize(struct gs_swap_chain *swap, uint32_t *width,
  195. uint32_t *height)
  196. {
  197. if(width) *width = swap->info.cx;
  198. if(height) *height = swap->info.cy;
  199. }
  200. texture_t texture_create_from_iosurface(device_t device, void *iosurf)
  201. {
  202. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  203. struct gs_texture_2d *tex = bmalloc(sizeof(struct gs_texture_2d));
  204. memset(tex, 0, sizeof(struct gs_texture_2d));
  205. OSType pf = IOSurfaceGetPixelFormat(ref);
  206. if (pf != 'BGRA')
  207. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  208. pf >> 24, pf >> 16, pf >> 8, pf);
  209. const enum gs_color_format color_format = GS_BGRA;
  210. tex->base.device = device;
  211. tex->base.type = GS_TEXTURE_2D;
  212. tex->base.format = GS_BGRA;
  213. tex->base.levels = 1;
  214. tex->base.gl_format = convert_gs_format(color_format);
  215. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  216. tex->base.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  217. tex->base.gl_target = GL_TEXTURE_RECTANGLE;
  218. tex->base.is_dynamic = false;
  219. tex->base.is_render_target = false;
  220. tex->base.gen_mipmaps = false;
  221. tex->width = IOSurfaceGetWidth(ref);
  222. tex->height = IOSurfaceGetHeight(ref);
  223. if (!gl_gen_textures(1, &tex->base.texture))
  224. goto fail;
  225. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  226. goto fail;
  227. CGLError err = CGLTexImageIOSurface2D(
  228. [[NSOpenGLContext currentContext] CGLContextObj],
  229. tex->base.gl_target,
  230. tex->base.gl_internal_format,
  231. tex->width, tex->height,
  232. tex->base.gl_format,
  233. tex->base.gl_type,
  234. ref, 0);
  235. if(err != kCGLNoError) {
  236. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  237. " (texture_create_from_iosurface)",
  238. err, CGLErrorString(err));
  239. gl_success("CGLTexImageIOSurface2D");
  240. goto fail;
  241. }
  242. if (!gl_tex_param_i(tex->base.gl_target,
  243. GL_TEXTURE_MAX_LEVEL, 0))
  244. goto fail;
  245. if (!gl_bind_texture(tex->base.gl_target, 0))
  246. goto fail;
  247. return (texture_t)tex;
  248. fail:
  249. texture_destroy((texture_t)tex);
  250. blog(LOG_ERROR, "texture_create_from_iosurface (GL) failed");
  251. return NULL;
  252. }
  253. bool texture_rebind_iosurface(texture_t texture, void *iosurf)
  254. {
  255. if (!texture)
  256. return false;
  257. if (!iosurf)
  258. return false;
  259. struct gs_texture_2d *tex = (struct gs_texture_2d*)texture;
  260. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  261. OSType pf = IOSurfaceGetPixelFormat(ref);
  262. if (pf != 'BGRA')
  263. blog(LOG_ERROR, "Unexpected pixel format: %d (%c%c%c%c)", pf,
  264. pf >> 24, pf >> 16, pf >> 8, pf);
  265. if (tex->width != IOSurfaceGetWidth(ref) ||
  266. tex->height != IOSurfaceGetHeight(ref))
  267. return false;
  268. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  269. return false;
  270. CGLError err = CGLTexImageIOSurface2D(
  271. [[NSOpenGLContext currentContext] CGLContextObj],
  272. tex->base.gl_target,
  273. tex->base.gl_internal_format,
  274. tex->width, tex->height,
  275. tex->base.gl_format,
  276. tex->base.gl_type,
  277. ref, 0);
  278. if(err != kCGLNoError) {
  279. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  280. " (texture_rebind_iosurface)",
  281. err, CGLErrorString(err));
  282. gl_success("CGLTexImageIOSurface2D");
  283. return false;
  284. }
  285. if (!gl_bind_texture(tex->base.gl_target, 0))
  286. return false;
  287. return true;
  288. }