gl-cocoa.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 device_entercontext(device_t device)
  169. {
  170. [device->plat->context makeCurrentContext];
  171. }
  172. void device_leavecontext(device_t device)
  173. {
  174. [NSOpenGLContext clearCurrentContext];
  175. }
  176. void device_load_swapchain(device_t device, swapchain_t swap)
  177. {
  178. if(!swap)
  179. swap = &device->plat->swap;
  180. if(device->cur_swap == swap)
  181. return;
  182. device->cur_swap = swap;
  183. }
  184. void device_present(device_t device)
  185. {
  186. [device->plat->context flushBuffer];
  187. }
  188. void gl_getclientsize(struct gs_swap_chain *swap, uint32_t *width,
  189. uint32_t *height)
  190. {
  191. if(width) *width = swap->info.cx;
  192. if(height) *height = swap->info.cy;
  193. }
  194. texture_t texture_create_from_iosurface(device_t device, void *iosurf)
  195. {
  196. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  197. struct gs_texture_2d *tex = bmalloc(sizeof(struct gs_texture_2d));
  198. memset(tex, 0, sizeof(struct gs_texture_2d));
  199. const enum gs_color_format color_format = GS_BGRA;
  200. tex->base.device = device;
  201. tex->base.type = GS_TEXTURE_2D;
  202. tex->base.format = GS_BGRA;
  203. tex->base.levels = 1;
  204. tex->base.gl_format = convert_gs_format(color_format);
  205. tex->base.gl_internal_format = convert_gs_internal_format(color_format);
  206. tex->base.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  207. tex->base.gl_target = GL_TEXTURE_RECTANGLE;
  208. tex->base.is_dynamic = false;
  209. tex->base.is_render_target = false;
  210. tex->base.gen_mipmaps = false;
  211. tex->width = IOSurfaceGetWidth(ref);
  212. tex->height = IOSurfaceGetHeight(ref);
  213. if (!gl_gen_textures(1, &tex->base.texture))
  214. goto fail;
  215. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  216. goto fail;
  217. CGLError err = CGLTexImageIOSurface2D(
  218. [[NSOpenGLContext currentContext] CGLContextObj],
  219. tex->base.gl_target,
  220. tex->base.gl_internal_format,
  221. tex->width, tex->height,
  222. tex->base.gl_format,
  223. tex->base.gl_type,
  224. ref, 0);
  225. if(err != kCGLNoError) {
  226. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  227. " (texture_create_from_iosurface)",
  228. err, CGLErrorString(err));
  229. gl_success("CGLTexImageIOSurface2D");
  230. goto fail;
  231. }
  232. if (!gl_tex_param_i(tex->base.gl_target,
  233. GL_TEXTURE_MAX_LEVEL, 0))
  234. goto fail;
  235. if (!gl_bind_texture(tex->base.gl_target, 0))
  236. goto fail;
  237. return (texture_t)tex;
  238. fail:
  239. texture_destroy((texture_t)tex);
  240. blog(LOG_ERROR, "texture_create_from_iosurface (GL) failed");
  241. return NULL;
  242. }
  243. bool texture_rebind_iosurface(texture_t texture, void *iosurf)
  244. {
  245. if (!texture)
  246. return false;
  247. if (!iosurf)
  248. return false;
  249. struct gs_texture_2d *tex = (struct gs_texture_2d*)texture;
  250. IOSurfaceRef ref = (IOSurfaceRef)iosurf;
  251. if (tex->width != IOSurfaceGetWidth(ref) ||
  252. tex->height != IOSurfaceGetHeight(ref))
  253. return false;
  254. if (!gl_bind_texture(tex->base.gl_target, tex->base.texture))
  255. return false;
  256. CGLError err = CGLTexImageIOSurface2D(
  257. [[NSOpenGLContext currentContext] CGLContextObj],
  258. tex->base.gl_target,
  259. tex->base.gl_internal_format,
  260. tex->width, tex->height,
  261. tex->base.gl_format,
  262. tex->base.gl_type,
  263. ref, 0);
  264. if(err != kCGLNoError) {
  265. blog(LOG_ERROR, "CGLTexImageIOSurface2D: %u, %s"
  266. " (texture_rebind_iosurface)",
  267. err, CGLErrorString(err));
  268. gl_success("CGLTexImageIOSurface2D");
  269. return false;
  270. }
  271. if (!gl_bind_texture(tex->base.gl_target, 0))
  272. return false;
  273. return true;
  274. }