gl-subsystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #pragma once
  15. #include "util/darray.h"
  16. #include "graphics/graphics.h"
  17. #include "graphics/matrix4.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_gs_depth_test(enum gs_depth_test test)
  108. {
  109. switch (test) {
  110. default:
  111. case GS_NEVER: return GL_NEVER;
  112. case GS_LESS: return GL_LESS;
  113. case GS_LEQUAL: return GL_LEQUAL;
  114. case GS_EQUAL: return GL_EQUAL;
  115. case GS_GEQUAL: return GL_GEQUAL;
  116. case GS_GREATER: return GL_GREATER;
  117. case GS_NOTEQUAL: return GL_NOTEQUAL;
  118. case GS_ALWAYS: return GL_ALWAYS;
  119. }
  120. }
  121. static inline GLenum convert_gs_stencil_op(enum gs_stencil_op op)
  122. {
  123. switch (op) {
  124. default:
  125. case GS_KEEP: return GL_KEEP;
  126. case GS_ZERO: return GL_ZERO;
  127. case GS_REPLACE: return GL_REPLACE;
  128. case GS_INCR: return GL_INCR;
  129. case GS_DECR: return GL_DECR;
  130. case GS_INVERT: return GL_INVERT;
  131. }
  132. }
  133. static inline GLenum convert_gs_stencil_side(enum gs_stencil_side side)
  134. {
  135. switch (side) {
  136. default:
  137. case GS_STENCIL_FRONT: return GL_FRONT;
  138. case GS_STENCIL_BACK: return GL_BACK;
  139. case GS_STENCIL_BOTH: return GL_FRONT_AND_BACK;
  140. }
  141. }
  142. static inline GLenum convert_gs_blend_type(enum gs_blend_type type)
  143. {
  144. switch (type) {
  145. default:
  146. case GS_BLEND_ZERO: return GL_ZERO;
  147. case GS_BLEND_ONE: return GL_ONE;
  148. case GS_BLEND_SRCCOLOR: return GL_SRC_COLOR;
  149. case GS_BLEND_INVSRCCOLOR: return GL_ONE_MINUS_SRC_COLOR;
  150. case GS_BLEND_SRCALPHA: return GL_SRC_ALPHA;
  151. case GS_BLEND_INVSRCALPHA: return GL_ONE_MINUS_SRC_ALPHA;
  152. case GS_BLEND_DSTCOLOR: return GL_DST_COLOR;
  153. case GS_BLEND_INVDSTCOLOR: return GL_ONE_MINUS_DST_COLOR;
  154. case GS_BLEND_DSTALPHA: return GL_DST_ALPHA;
  155. case GS_BLEND_INVDSTALPHA: return GL_ONE_MINUS_DST_ALPHA;
  156. case GS_BLEND_SRCALPHASAT: return GL_SRC_ALPHA_SATURATE;
  157. }
  158. }
  159. static inline GLenum convert_shader_type(enum shader_type type)
  160. {
  161. switch (type) {
  162. default:
  163. case SHADER_VERTEX: return GL_VERTEX_SHADER;
  164. case SHADER_PIXEL: return GL_FRAGMENT_SHADER;
  165. }
  166. }
  167. static inline void convert_filter(enum gs_sample_filter filter,
  168. GLint *min_filter, GLint *mag_filter)
  169. {
  170. switch (filter) {
  171. case GS_FILTER_ANISOTROPIC:
  172. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  173. *mag_filter = GL_LINEAR;
  174. break;
  175. default:
  176. case GS_FILTER_POINT:
  177. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  178. *mag_filter = GL_NEAREST;
  179. break;
  180. case GS_FILTER_LINEAR:
  181. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  182. *mag_filter = GL_LINEAR;
  183. break;
  184. case GS_FILTER_MIN_MAG_POINT_MIP_LINEAR:
  185. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  186. *mag_filter = GL_NEAREST;
  187. break;
  188. case GS_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT:
  189. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  190. *mag_filter = GL_LINEAR;
  191. break;
  192. case GS_FILTER_MIN_POINT_MAG_MIP_LINEAR:
  193. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  194. *mag_filter = GL_LINEAR;
  195. break;
  196. case GS_FILTER_MIN_LINEAR_MAG_MIP_POINT:
  197. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  198. *mag_filter = GL_NEAREST;
  199. break;
  200. case GS_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
  201. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  202. *mag_filter = GL_NEAREST;
  203. break;
  204. case GS_FILTER_MIN_MAG_LINEAR_MIP_POINT:
  205. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  206. *mag_filter = GL_LINEAR;
  207. break;
  208. }
  209. }
  210. static inline GLint convert_address_mode(enum gs_address_mode mode)
  211. {
  212. switch (mode) {
  213. default:
  214. case GS_ADDRESS_WRAP: return GL_REPEAT;
  215. case GS_ADDRESS_CLAMP: return GL_CLAMP;
  216. case GS_ADDRESS_MIRROR: return GL_MIRRORED_REPEAT;
  217. case GS_ADDRESS_BORDER: return GL_CLAMP_TO_BORDER;
  218. case GS_ADDRESS_MIRRORONCE: return GL_MIRROR_CLAMP_EXT;
  219. }
  220. }
  221. static inline GLenum convert_gs_topology(enum gs_draw_mode mode)
  222. {
  223. switch (mode) {
  224. default:
  225. case GS_POINTS: return GL_POINTS;
  226. case GS_LINES: return GL_LINES;
  227. case GS_LINESTRIP: return GL_LINE_STRIP;
  228. case GS_TRIS: return GL_TRIANGLES;
  229. case GS_TRISTRIP: return GL_TRIANGLE_STRIP;
  230. }
  231. }
  232. extern void convert_sampler_info(struct gs_sampler_state *sampler,
  233. struct gs_sampler_info *info);
  234. struct gs_sampler_state {
  235. device_t device;
  236. volatile uint32_t ref;
  237. GLint min_filter;
  238. GLint mag_filter;
  239. GLint address_u;
  240. GLint address_v;
  241. GLint address_w;
  242. GLint max_anisotropy;
  243. };
  244. static inline void samplerstate_addref(samplerstate_t ss)
  245. {
  246. ss->ref++;
  247. }
  248. static inline void samplerstate_release(samplerstate_t ss)
  249. {
  250. if (--ss->ref == 0)
  251. bfree(ss);
  252. }
  253. struct shader_param {
  254. enum shader_param_type type;
  255. char *name;
  256. GLint param;
  257. GLint texture_id;
  258. size_t sampler_id;
  259. int array_count;
  260. struct gs_texture *texture;
  261. DARRAY(uint8_t) cur_value;
  262. DARRAY(uint8_t) def_value;
  263. bool changed;
  264. };
  265. enum attrib_type {
  266. ATTRIB_POSITION,
  267. ATTRIB_NORMAL,
  268. ATTRIB_TANGENT,
  269. ATTRIB_COLOR,
  270. ATTRIB_TEXCOORD,
  271. ATTRIB_TARGET
  272. };
  273. struct shader_attrib {
  274. GLint attrib;
  275. size_t index;
  276. enum attrib_type type;
  277. };
  278. struct gs_shader {
  279. device_t device;
  280. enum shader_type type;
  281. GLuint program;
  282. struct shader_param *viewproj;
  283. struct shader_param *world;
  284. DARRAY(struct shader_attrib) attribs;
  285. DARRAY(struct shader_param) params;
  286. DARRAY(samplerstate_t) samplers;
  287. };
  288. struct gs_vertex_buffer {
  289. GLuint vertex_buffer;
  290. GLuint normal_buffer;
  291. GLuint tangent_buffer;
  292. GLuint color_buffer;
  293. DARRAY(GLuint) uv_buffers;
  294. DARRAY(size_t) uv_sizes;
  295. device_t device;
  296. bool dynamic;
  297. struct vb_data *data;
  298. };
  299. extern bool vertexbuffer_load(device_t device, vertbuffer_t vb);
  300. struct gs_index_buffer {
  301. GLuint buffer;
  302. enum gs_index_type type;
  303. GLuint gl_type;
  304. device_t device;
  305. void *data;
  306. size_t num;
  307. size_t width;
  308. size_t size;
  309. bool dynamic;
  310. };
  311. struct gs_texture {
  312. device_t device;
  313. enum gs_texture_type type;
  314. enum gs_color_format format;
  315. GLenum gl_format;
  316. GLenum gl_target;
  317. GLint gl_internal_format;
  318. GLenum gl_type;
  319. GLuint texture;
  320. uint32_t levels;
  321. bool is_dynamic;
  322. bool is_render_target;
  323. bool gen_mipmaps;
  324. samplerstate_t cur_sampler;
  325. };
  326. struct gs_texture_2d {
  327. struct gs_texture base;
  328. uint32_t width;
  329. uint32_t height;
  330. bool gen_mipmaps;
  331. GLuint unpack_buffer;
  332. };
  333. struct gs_texture_cube {
  334. struct gs_texture base;
  335. uint32_t size;
  336. };
  337. struct gs_stage_surface {
  338. enum gs_color_format format;
  339. uint32_t width;
  340. uint32_t height;
  341. uint32_t bytes_per_pixel;
  342. GLenum gl_format;
  343. GLint gl_internal_format;
  344. GLenum gl_type;
  345. GLuint texture;
  346. GLuint pack_buffer;
  347. };
  348. struct gs_zstencil_buffer {
  349. device_t device;
  350. GLuint buffer;
  351. GLuint attachment;
  352. GLenum format;
  353. };
  354. struct gs_swap_chain {
  355. device_t device;
  356. struct gl_windowinfo *wi;
  357. struct gs_init_data info;
  358. };
  359. struct fbo_info {
  360. GLuint fbo;
  361. uint32_t width;
  362. uint32_t height;
  363. enum gs_color_format format;
  364. texture_t cur_render_target;
  365. int cur_render_side;
  366. zstencil_t cur_zstencil_buffer;
  367. };
  368. static inline void fbo_info_destroy(struct fbo_info *fbo)
  369. {
  370. glDeleteFramebuffers(1, &fbo->fbo);
  371. gl_success("glDeleteFramebuffers");
  372. }
  373. struct gs_device {
  374. struct gl_platform *plat;
  375. GLuint pipeline;
  376. enum copy_type copy_type;
  377. texture_t cur_render_target;
  378. zstencil_t cur_zstencil_buffer;
  379. int cur_render_side;
  380. texture_t cur_textures[GS_MAX_TEXTURES];
  381. samplerstate_t cur_samplers[GS_MAX_TEXTURES];
  382. vertbuffer_t cur_vertex_buffer;
  383. indexbuffer_t cur_index_buffer;
  384. shader_t cur_vertex_shader;
  385. shader_t cur_pixel_shader;
  386. swapchain_t cur_swap;
  387. enum gs_cull_mode cur_cull_mode;
  388. struct gs_rect cur_viewport;
  389. struct matrix4 cur_proj;
  390. struct matrix4 cur_view;
  391. struct matrix4 cur_viewproj;
  392. DARRAY(struct matrix4) proj_stack;
  393. DARRAY(struct fbo_info*) fbos;
  394. struct fbo_info *cur_fbo;
  395. };
  396. extern struct gl_platform *gl_platform_create(device_t device,
  397. struct gs_init_data *info);
  398. extern struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform);
  399. extern void gl_platform_destroy(struct gl_platform *platform);
  400. extern struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info);
  401. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi);