gl-subsystem.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[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. #ifndef GL_SUBSYSTEM_H
  15. #define GL_SUBSYSTEM_H
  16. #include "util/darray.h"
  17. #include "graphics/graphics.h"
  18. #include "glew/include/GL/glew.h"
  19. #include "gl-helpers.h"
  20. #include "gl-exports.h"
  21. struct gl_platform;
  22. struct gl_windowinfo;
  23. enum copy_type {
  24. COPY_TYPE_ARB,
  25. COPY_TYPE_NV,
  26. COPY_TYPE_FBO_BLIT
  27. };
  28. static inline GLint convert_gs_format(enum gs_color_format format)
  29. {
  30. switch (format) {
  31. case GS_A8: return GL_RGBA;
  32. case GS_R8: return GL_RED;
  33. case GS_RGBA: return GL_RGBA;
  34. case GS_BGRX: return GL_BGR;
  35. case GS_BGRA: return GL_BGRA;
  36. case GS_R10G10B10A2: return GL_RGBA;
  37. case GS_RGBA16: return GL_RGBA;
  38. case GS_R16: return GL_RED;
  39. case GS_RGBA16F: return GL_RGBA;
  40. case GS_RGBA32F: return GL_RGBA;
  41. case GS_RG16F: return GL_RG;
  42. case GS_RG32F: return GL_RG;
  43. case GS_R16F: return GL_RED;
  44. case GS_R32F: return GL_RED;
  45. case GS_DXT1: return GL_RGB;
  46. case GS_DXT3: return GL_RGBA;
  47. case GS_DXT5: return GL_RGBA;
  48. default: return 0;
  49. }
  50. }
  51. static inline GLint convert_gs_internal_format(enum gs_color_format format)
  52. {
  53. switch (format) {
  54. case GS_A8: return GL_R8; /* NOTE: use GL_TEXTURE_SWIZZLE_x */
  55. case GS_R8: return GL_R8;
  56. case GS_RGBA: return GL_RGBA;
  57. case GS_BGRX: return GL_RGBA;
  58. case GS_BGRA: return GL_RGBA;
  59. case GS_R10G10B10A2: return GL_RGB10_A2;
  60. case GS_RGBA16: return GL_RGBA16;
  61. case GS_R16: return GL_R16;
  62. case GS_RGBA16F: return GL_RGBA16F;
  63. case GS_RGBA32F: return GL_RGBA32F;
  64. case GS_RG16F: return GL_RG16F;
  65. case GS_RG32F: return GL_RG32F;
  66. case GS_R16F: return GL_R16F;
  67. case GS_R32F: return GL_R32F;
  68. case GS_DXT1: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  69. case GS_DXT3: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  70. case GS_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  71. default: return 0;
  72. }
  73. }
  74. static inline GLenum get_gl_format_type(enum color_format format)
  75. {
  76. switch (format) {
  77. case GS_A8: return GL_UNSIGNED_BYTE;
  78. case GS_R8: return GL_UNSIGNED_BYTE;
  79. case GS_RGBA: return GL_UNSIGNED_BYTE;
  80. case GS_BGRX: return GL_UNSIGNED_BYTE;
  81. case GS_BGRA: return GL_UNSIGNED_BYTE;
  82. case GS_R10G10B10A2: return GL_UNSIGNED_INT_10_10_10_2;
  83. case GS_RGBA16: return GL_UNSIGNED_SHORT;
  84. case GS_R16: return GL_UNSIGNED_SHORT;
  85. case GS_RGBA16F: return GL_UNSIGNED_SHORT;
  86. case GS_RGBA32F: return GL_FLOAT;
  87. case GS_RG16F: return GL_UNSIGNED_SHORT;
  88. case GS_RG32F: return GL_FLOAT;
  89. case GS_R16F: return GL_UNSIGNED_SHORT;
  90. case GS_R32F: return GL_FLOAT;
  91. case GS_DXT1: return GL_UNSIGNED_BYTE;
  92. case GS_DXT3: return GL_UNSIGNED_BYTE;
  93. case GS_DXT5: return GL_UNSIGNED_BYTE;
  94. default: return 0;
  95. }
  96. }
  97. static inline GLenum convert_zstencil_format(enum gs_zstencil_format format)
  98. {
  99. switch (format) {
  100. case GS_Z16: return GL_DEPTH_COMPONENT16;
  101. case GS_Z24_S8: return GL_DEPTH24_STENCIL8;
  102. case GS_Z32F: return GL_DEPTH_COMPONENT32F;
  103. case GS_Z32F_S8X24: return GL_DEPTH32F_STENCIL8;
  104. default: return 0;
  105. }
  106. }
  107. static inline GLenum convert_shader_type(enum shader_type type)
  108. {
  109. switch (type) {
  110. default:
  111. case SHADER_VERTEX: return GL_VERTEX_SHADER;
  112. case SHADER_PIXEL: return GL_FRAGMENT_SHADER;
  113. }
  114. }
  115. static inline void convert_filter(enum gs_sample_filter filter,
  116. GLint *min_filter, GLint *mag_filter)
  117. {
  118. switch (filter) {
  119. case GS_FILTER_ANISOTROPIC:
  120. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  121. *mag_filter = GL_LINEAR;
  122. break;
  123. default:
  124. case GS_FILTER_POINT:
  125. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  126. *mag_filter = GL_NEAREST;
  127. break;
  128. case GS_FILTER_LINEAR:
  129. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  130. *mag_filter = GL_LINEAR;
  131. break;
  132. case GS_FILTER_MIN_MAG_POINT_MIP_LINEAR:
  133. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  134. *mag_filter = GL_NEAREST;
  135. break;
  136. case GS_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT:
  137. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  138. *mag_filter = GL_LINEAR;
  139. break;
  140. case GS_FILTER_MIN_POINT_MAG_MIP_LINEAR:
  141. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  142. *mag_filter = GL_LINEAR;
  143. break;
  144. case GS_FILTER_MIN_LINEAR_MAG_MIP_POINT:
  145. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  146. *mag_filter = GL_NEAREST;
  147. break;
  148. case GS_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
  149. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  150. *mag_filter = GL_NEAREST;
  151. break;
  152. case GS_FILTER_MIN_MAG_LINEAR_MIP_POINT:
  153. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  154. *mag_filter = GL_LINEAR;
  155. break;
  156. }
  157. }
  158. static inline GLint convert_address_mode(enum gs_address_mode mode)
  159. {
  160. switch (mode) {
  161. default:
  162. case GS_ADDRESS_WRAP: return GL_REPEAT;
  163. case GS_ADDRESS_CLAMP: return GL_CLAMP;
  164. case GS_ADDRESS_MIRROR: return GL_MIRRORED_REPEAT;
  165. case GS_ADDRESS_BORDER: return GL_CLAMP_TO_BORDER;
  166. case GS_ADDRESS_MIRRORONCE: return GL_MIRROR_CLAMP_EXT;
  167. }
  168. }
  169. extern void convert_sampler_info(struct gs_sampler_state *sampler,
  170. struct gs_sampler_info *info);
  171. struct gs_sampler_state {
  172. GLint min_filter;
  173. GLint mag_filter;
  174. GLint address_u;
  175. GLint address_v;
  176. GLint address_w;
  177. GLint max_anisotropy;
  178. };
  179. struct shader_param {
  180. char *name;
  181. enum shader_param_type type;
  182. GLint param;
  183. GLint texture_id;
  184. size_t sampler_id;
  185. int array_count;
  186. struct gs_texture *texture;
  187. DARRAY(uint8_t) cur_value;
  188. DARRAY(uint8_t) def_value;
  189. bool changed;
  190. };
  191. struct gs_shader {
  192. device_t device;
  193. enum shader_type type;
  194. GLuint program;
  195. struct shader_param *viewproj;
  196. struct shader_param *world;
  197. DARRAY(struct gs_sampler_state) samplers;
  198. DARRAY(struct shader_param) params;
  199. };
  200. struct gs_vertex_buffer {
  201. GLuint vertex_buffer;
  202. GLuint normal_buffer;
  203. GLuint tangent_buffer;
  204. GLuint color_buffer;
  205. DARRAY(GLuint) uv_buffers;
  206. device_t device;
  207. bool dynamic;
  208. struct vb_data *data;
  209. };
  210. struct gs_index_buffer {
  211. GLuint buffer;
  212. enum gs_index_type type;
  213. GLuint gl_type;
  214. device_t device;
  215. void *data;
  216. size_t num;
  217. size_t size;
  218. bool dynamic;
  219. };
  220. struct gs_texture {
  221. device_t device;
  222. enum gs_texture_type type;
  223. enum gs_color_format format;
  224. GLenum gl_format;
  225. GLint gl_internal_format;
  226. GLenum gl_type;
  227. GLuint texture;
  228. uint32_t levels;
  229. bool is_dynamic;
  230. bool is_render_target;
  231. bool gen_mipmaps;
  232. };
  233. struct gs_texture_2d {
  234. struct gs_texture base;
  235. uint32_t width;
  236. uint32_t height;
  237. bool gen_mipmaps;
  238. GLuint unpack_buffer;
  239. };
  240. struct gs_texture_cube {
  241. struct gs_texture base;
  242. uint32_t size;
  243. };
  244. struct gs_stage_surface {
  245. enum gs_color_format format;
  246. uint32_t width;
  247. uint32_t height;
  248. uint32_t bytes_per_pixel;
  249. GLenum gl_format;
  250. GLint gl_internal_format;
  251. GLenum gl_type;
  252. GLuint texture;
  253. GLuint pack_buffer;
  254. };
  255. struct gs_zstencil_buffer {
  256. device_t device;
  257. GLuint buffer;
  258. GLenum format;
  259. };
  260. struct gs_swap_chain {
  261. device_t device;
  262. struct gl_windowinfo *wi;
  263. struct gs_init_data info;
  264. };
  265. struct gs_device {
  266. struct gl_platform *plat;
  267. enum copy_type copy_type;
  268. struct gs_texture *cur_render_texture;
  269. int cur_render_side;
  270. struct gs_texture *cur_textures[GS_MAX_TEXTURES];
  271. struct gs_sampler *cur_samplers[GS_MAX_TEXTURES];
  272. struct gs_swap_chain *cur_swap;
  273. };
  274. extern struct gl_platform *gl_platform_create(device_t device,
  275. struct gs_init_data *info);
  276. extern struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform);
  277. extern void gl_platform_destroy(struct gl_platform *platform);
  278. extern struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info);
  279. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi);
  280. #endif