gl-cocoa.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 3 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. GLuint vao;
  26. struct gs_swap_chain swap;
  27. };
  28. static NSOpenGLContext *gl_context_create(struct gs_init_data *info)
  29. {
  30. unsigned attrib_count = 0;
  31. #define ADD_ATTR(x) \
  32. { attributes[attrib_count++] = (NSOpenGLPixelFormatAttribute)x; }
  33. #define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
  34. NSOpenGLPixelFormatAttribute attributes[40];
  35. switch(info->num_backbuffers) {
  36. case 0:
  37. break;
  38. case 1:
  39. ADD_ATTR(NSOpenGLPFADoubleBuffer);
  40. break;
  41. case 2:
  42. ADD_ATTR(NSOpenGLPFATripleBuffer);
  43. break;
  44. default:
  45. blog(LOG_ERROR, "Requested backbuffers (%d) not "
  46. "supported", info->num_backbuffers);
  47. }
  48. ADD_ATTR(NSOpenGLPFAClosestPolicy);
  49. ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
  50. int color_bits = 0;//get_color_format_bits(info->format);
  51. if(color_bits == 0) color_bits = 24;
  52. else if(color_bits < 15) color_bits = 15;
  53. ADD_ATTR2(NSOpenGLPFAColorSize, color_bits);
  54. ADD_ATTR2(NSOpenGLPFAAlphaSize, 8);
  55. ADD_ATTR2(NSOpenGLPFADepthSize, 16);
  56. ADD_ATTR(0);
  57. #undef ADD_ATTR2
  58. #undef ADD_ATTR
  59. NSOpenGLPixelFormat *pf;
  60. pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  61. if(!pf) {
  62. blog(LOG_ERROR, "Failed to create pixel format");
  63. return NULL;
  64. }
  65. NSOpenGLContext *context;
  66. context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
  67. [pf release];
  68. if(!context) {
  69. blog(LOG_ERROR, "Failed to create context");
  70. return NULL;
  71. }
  72. [context setView:info->view];
  73. return context;
  74. }
  75. static inline void required_extension_error(const char *extension)
  76. {
  77. blog(LOG_ERROR, "OpenGL extension %s is required", extension);
  78. }
  79. static bool gl_init_extensions(device_t device)
  80. {
  81. glewExperimental=true;
  82. GLenum error = glewInit();
  83. if(error != GLEW_OK) {
  84. blog(LOG_ERROR, "glewInit failed, %u\n%s\n", error,
  85. glewGetErrorString(error));
  86. return false;
  87. }
  88. if(!GLEW_VERSION_2_1) {
  89. blog(LOG_ERROR, "OpenGL 2.1 minimum required by the graphics "
  90. "adapter");
  91. return false;
  92. }
  93. if(!GLEW_ARB_framebuffer_object) {
  94. required_extension_error("GL_ARB_framebuffer_object");
  95. return false;
  96. }
  97. if(!GLEW_ARB_separate_shader_objects) {
  98. required_extension_error("GL_ARB_separate_shader_objects");
  99. return false;
  100. }
  101. //something inside glew produces error code 1280 (invalid enum)
  102. glGetError();
  103. device->copy_type = COPY_TYPE_FBO_BLIT;
  104. return true;
  105. }
  106. static bool gl_init_default_swap(struct gl_platform *plat, device_t dev,
  107. struct gs_init_data *info)
  108. {
  109. if(!(plat->context = gl_context_create(info)))
  110. return false;
  111. plat->swap.device = dev;
  112. plat->swap.info = *info;
  113. plat->swap.wi = gl_windowinfo_create(info);
  114. return plat->swap.wi != NULL;
  115. }
  116. struct gl_platform *gl_platform_create(device_t device,
  117. struct gs_init_data *info)
  118. {
  119. struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
  120. memset(plat, 0, sizeof(struct gl_platform));
  121. if(!gl_init_default_swap(plat, device, info))
  122. goto fail;
  123. [plat->context makeCurrentContext];
  124. if(!gl_init_extensions(device))
  125. goto fail;
  126. gl_gen_vertex_arrays(1, &plat->vao);
  127. gl_bind_vertex_array(plat->vao);
  128. if (GLEW_ARB_seamless_cube_map) {
  129. glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  130. gl_success("GL_TEXTURE_CUBE_MAP_SEAMLESS");
  131. }
  132. return plat;
  133. fail:
  134. blog(LOG_ERROR, "gl_platform_create failed");
  135. gl_platform_destroy(plat);
  136. return NULL;
  137. }
  138. struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform)
  139. {
  140. if(platform)
  141. return &platform->swap;
  142. return NULL;
  143. }
  144. void gl_platform_destroy(struct gl_platform *platform)
  145. {
  146. if(!platform)
  147. return;
  148. gl_delete_vertex_arrays(1, &platform->vao);
  149. [platform->context release];
  150. platform->context = nil;
  151. gl_windowinfo_destroy(platform->swap.wi);
  152. bfree(platform);
  153. }
  154. struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info)
  155. {
  156. if(!info)
  157. return NULL;
  158. if(!info->view)
  159. return NULL;
  160. struct gl_windowinfo *wi = bmalloc(sizeof(struct gl_windowinfo));
  161. memset(wi, 0, sizeof(struct gl_windowinfo));
  162. wi->view = info->view;
  163. return wi;
  164. }
  165. void gl_windowinfo_destroy(struct gl_windowinfo *wi)
  166. {
  167. if(!wi)
  168. return;
  169. wi->view = nil;
  170. bfree(wi);
  171. }
  172. void device_entercontext(device_t device)
  173. {
  174. [device->plat->context makeCurrentContext];
  175. //gl_bind_vertex_array(device->plat->vao);
  176. }
  177. void device_leavecontext(device_t device)
  178. {
  179. [NSOpenGLContext clearCurrentContext];
  180. }
  181. void device_load_swapchain(device_t device, swapchain_t swap)
  182. {
  183. if(!swap)
  184. swap = &device->plat->swap;
  185. if(device->cur_swap == swap)
  186. return;
  187. device->cur_swap = swap;
  188. }
  189. void device_present(device_t device)
  190. {
  191. [device->plat->context flushBuffer];
  192. }
  193. void gl_getclientsize(struct gs_swap_chain *swap, uint32_t *width,
  194. uint32_t *height)
  195. {
  196. if(width) *width = swap->info.cx;
  197. if(height) *height = swap->info.cy;
  198. }