gl-subsystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. #ifdef GLEW_STATIC
  19. #include "glew/include/GL/glew.h"
  20. #else
  21. #include <GL/glew.h>
  22. #endif
  23. #include "gl-helpers.h"
  24. #include "gl-exports.h"
  25. struct gl_platform;
  26. struct gl_windowinfo;
  27. enum copy_type {
  28. COPY_TYPE_ARB,
  29. COPY_TYPE_NV,
  30. COPY_TYPE_FBO_BLIT
  31. };
  32. static inline GLint convert_gs_format(enum gs_color_format format)
  33. {
  34. switch (format) {
  35. case GS_A8: return GL_RGBA;
  36. case GS_R8: return GL_RED;
  37. case GS_RGBA: return GL_RGBA;
  38. case GS_BGRX: return GL_BGR;
  39. case GS_BGRA: return GL_BGRA;
  40. case GS_R10G10B10A2: return GL_RGBA;
  41. case GS_RGBA16: return GL_RGBA;
  42. case GS_R16: return GL_RED;
  43. case GS_RGBA16F: return GL_RGBA;
  44. case GS_RGBA32F: return GL_RGBA;
  45. case GS_RG16F: return GL_RG;
  46. case GS_RG32F: return GL_RG;
  47. case GS_R16F: return GL_RED;
  48. case GS_R32F: return GL_RED;
  49. case GS_DXT1: return GL_RGB;
  50. case GS_DXT3: return GL_RGBA;
  51. case GS_DXT5: return GL_RGBA;
  52. case GS_UNKNOWN: return 0;
  53. }
  54. return 0;
  55. }
  56. static inline GLint convert_gs_internal_format(enum gs_color_format format)
  57. {
  58. switch (format) {
  59. case GS_A8: return GL_R8; /* NOTE: use GL_TEXTURE_SWIZZLE_x */
  60. case GS_R8: return GL_R8;
  61. case GS_RGBA: return GL_RGBA;
  62. case GS_BGRX: return GL_RGBA;
  63. case GS_BGRA: return GL_RGBA;
  64. case GS_R10G10B10A2: return GL_RGB10_A2;
  65. case GS_RGBA16: return GL_RGBA16;
  66. case GS_R16: return GL_R16;
  67. case GS_RGBA16F: return GL_RGBA16F;
  68. case GS_RGBA32F: return GL_RGBA32F;
  69. case GS_RG16F: return GL_RG16F;
  70. case GS_RG32F: return GL_RG32F;
  71. case GS_R16F: return GL_R16F;
  72. case GS_R32F: return GL_R32F;
  73. case GS_DXT1: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  74. case GS_DXT3: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  75. case GS_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  76. case GS_UNKNOWN: return 0;
  77. }
  78. return 0;
  79. }
  80. static inline GLenum get_gl_format_type(enum gs_color_format format)
  81. {
  82. switch (format) {
  83. case GS_A8: return GL_UNSIGNED_BYTE;
  84. case GS_R8: return GL_UNSIGNED_BYTE;
  85. case GS_RGBA: return GL_UNSIGNED_BYTE;
  86. case GS_BGRX: return GL_UNSIGNED_BYTE;
  87. case GS_BGRA: return GL_UNSIGNED_BYTE;
  88. case GS_R10G10B10A2: return GL_UNSIGNED_INT_10_10_10_2;
  89. case GS_RGBA16: return GL_UNSIGNED_SHORT;
  90. case GS_R16: return GL_UNSIGNED_SHORT;
  91. case GS_RGBA16F: return GL_UNSIGNED_SHORT;
  92. case GS_RGBA32F: return GL_FLOAT;
  93. case GS_RG16F: return GL_UNSIGNED_SHORT;
  94. case GS_RG32F: return GL_FLOAT;
  95. case GS_R16F: return GL_UNSIGNED_SHORT;
  96. case GS_R32F: return GL_FLOAT;
  97. case GS_DXT1: return GL_UNSIGNED_BYTE;
  98. case GS_DXT3: return GL_UNSIGNED_BYTE;
  99. case GS_DXT5: return GL_UNSIGNED_BYTE;
  100. case GS_UNKNOWN: return 0;
  101. }
  102. return GL_UNSIGNED_BYTE;
  103. }
  104. static inline GLenum convert_zstencil_format(enum gs_zstencil_format format)
  105. {
  106. switch (format) {
  107. case GS_Z16: return GL_DEPTH_COMPONENT16;
  108. case GS_Z24_S8: return GL_DEPTH24_STENCIL8;
  109. case GS_Z32F: return GL_DEPTH_COMPONENT32F;
  110. case GS_Z32F_S8X24: return GL_DEPTH32F_STENCIL8;
  111. case GS_ZS_NONE: return 0;
  112. }
  113. return 0;
  114. }
  115. static inline GLenum convert_gs_depth_test(enum gs_depth_test test)
  116. {
  117. switch (test) {
  118. case GS_NEVER: return GL_NEVER;
  119. case GS_LESS: return GL_LESS;
  120. case GS_LEQUAL: return GL_LEQUAL;
  121. case GS_EQUAL: return GL_EQUAL;
  122. case GS_GEQUAL: return GL_GEQUAL;
  123. case GS_GREATER: return GL_GREATER;
  124. case GS_NOTEQUAL: return GL_NOTEQUAL;
  125. case GS_ALWAYS: return GL_ALWAYS;
  126. }
  127. return GL_NEVER;
  128. }
  129. static inline GLenum convert_gs_stencil_op(enum gs_stencil_op op)
  130. {
  131. switch (op) {
  132. case GS_KEEP: return GL_KEEP;
  133. case GS_ZERO: return GL_ZERO;
  134. case GS_REPLACE: return GL_REPLACE;
  135. case GS_INCR: return GL_INCR;
  136. case GS_DECR: return GL_DECR;
  137. case GS_INVERT: return GL_INVERT;
  138. }
  139. return GL_KEEP;
  140. }
  141. static inline GLenum convert_gs_stencil_side(enum gs_stencil_side side)
  142. {
  143. switch (side) {
  144. case GS_STENCIL_FRONT: return GL_FRONT;
  145. case GS_STENCIL_BACK: return GL_BACK;
  146. case GS_STENCIL_BOTH: return GL_FRONT_AND_BACK;
  147. }
  148. return GL_FRONT;
  149. }
  150. static inline GLenum convert_gs_blend_type(enum gs_blend_type type)
  151. {
  152. switch (type) {
  153. case GS_BLEND_ZERO: return GL_ZERO;
  154. case GS_BLEND_ONE: return GL_ONE;
  155. case GS_BLEND_SRCCOLOR: return GL_SRC_COLOR;
  156. case GS_BLEND_INVSRCCOLOR: return GL_ONE_MINUS_SRC_COLOR;
  157. case GS_BLEND_SRCALPHA: return GL_SRC_ALPHA;
  158. case GS_BLEND_INVSRCALPHA: return GL_ONE_MINUS_SRC_ALPHA;
  159. case GS_BLEND_DSTCOLOR: return GL_DST_COLOR;
  160. case GS_BLEND_INVDSTCOLOR: return GL_ONE_MINUS_DST_COLOR;
  161. case GS_BLEND_DSTALPHA: return GL_DST_ALPHA;
  162. case GS_BLEND_INVDSTALPHA: return GL_ONE_MINUS_DST_ALPHA;
  163. case GS_BLEND_SRCALPHASAT: return GL_SRC_ALPHA_SATURATE;
  164. }
  165. return GL_ONE;
  166. }
  167. static inline GLenum convert_shader_type(enum shader_type type)
  168. {
  169. switch (type) {
  170. case SHADER_VERTEX: return GL_VERTEX_SHADER;
  171. case SHADER_PIXEL: return GL_FRAGMENT_SHADER;
  172. }
  173. return GL_VERTEX_SHADER;
  174. }
  175. static inline void convert_filter(enum gs_sample_filter filter,
  176. GLint *min_filter, GLint *mag_filter)
  177. {
  178. switch (filter) {
  179. case GS_FILTER_POINT:
  180. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  181. *mag_filter = GL_NEAREST;
  182. return;
  183. case GS_FILTER_LINEAR:
  184. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  185. *mag_filter = GL_LINEAR;
  186. return;
  187. case GS_FILTER_MIN_MAG_POINT_MIP_LINEAR:
  188. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  189. *mag_filter = GL_NEAREST;
  190. return;
  191. case GS_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT:
  192. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  193. *mag_filter = GL_LINEAR;
  194. return;
  195. case GS_FILTER_MIN_POINT_MAG_MIP_LINEAR:
  196. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  197. *mag_filter = GL_LINEAR;
  198. return;
  199. case GS_FILTER_MIN_LINEAR_MAG_MIP_POINT:
  200. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  201. *mag_filter = GL_NEAREST;
  202. return;
  203. case GS_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
  204. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  205. *mag_filter = GL_NEAREST;
  206. return;
  207. case GS_FILTER_MIN_MAG_LINEAR_MIP_POINT:
  208. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  209. *mag_filter = GL_LINEAR;
  210. return;
  211. case GS_FILTER_ANISOTROPIC:
  212. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  213. *mag_filter = GL_LINEAR;
  214. return;
  215. }
  216. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  217. *mag_filter = GL_NEAREST;
  218. }
  219. static inline GLint convert_address_mode(enum gs_address_mode mode)
  220. {
  221. switch (mode) {
  222. case GS_ADDRESS_WRAP: return GL_REPEAT;
  223. case GS_ADDRESS_CLAMP: return GL_CLAMP_TO_EDGE;
  224. case GS_ADDRESS_MIRROR: return GL_MIRRORED_REPEAT;
  225. case GS_ADDRESS_BORDER: return GL_CLAMP_TO_BORDER;
  226. case GS_ADDRESS_MIRRORONCE: return GL_MIRROR_CLAMP_EXT;
  227. }
  228. return GL_REPEAT;
  229. }
  230. static inline GLenum convert_gs_topology(enum gs_draw_mode mode)
  231. {
  232. switch (mode) {
  233. case GS_POINTS: return GL_POINTS;
  234. case GS_LINES: return GL_LINES;
  235. case GS_LINESTRIP: return GL_LINE_STRIP;
  236. case GS_TRIS: return GL_TRIANGLES;
  237. case GS_TRISTRIP: return GL_TRIANGLE_STRIP;
  238. }
  239. return GL_POINTS;
  240. }
  241. extern void convert_sampler_info(struct gs_sampler_state *sampler,
  242. struct gs_sampler_info *info);
  243. struct gs_sampler_state {
  244. device_t device;
  245. volatile uint32_t ref;
  246. GLint min_filter;
  247. GLint mag_filter;
  248. GLint address_u;
  249. GLint address_v;
  250. GLint address_w;
  251. GLint max_anisotropy;
  252. };
  253. static inline void samplerstate_addref(samplerstate_t ss)
  254. {
  255. ss->ref++;
  256. }
  257. static inline void samplerstate_release(samplerstate_t ss)
  258. {
  259. if (--ss->ref == 0)
  260. bfree(ss);
  261. }
  262. struct shader_param {
  263. enum shader_param_type type;
  264. char *name;
  265. shader_t shader;
  266. GLint param;
  267. GLint texture_id;
  268. size_t sampler_id;
  269. int array_count;
  270. struct gs_texture *texture;
  271. DARRAY(uint8_t) cur_value;
  272. DARRAY(uint8_t) def_value;
  273. bool changed;
  274. };
  275. enum attrib_type {
  276. ATTRIB_POSITION,
  277. ATTRIB_NORMAL,
  278. ATTRIB_TANGENT,
  279. ATTRIB_COLOR,
  280. ATTRIB_TEXCOORD,
  281. ATTRIB_TARGET
  282. };
  283. struct shader_attrib {
  284. GLint attrib;
  285. size_t index;
  286. enum attrib_type type;
  287. };
  288. struct gs_shader {
  289. device_t device;
  290. enum shader_type type;
  291. GLuint program;
  292. struct shader_param *viewproj;
  293. struct shader_param *world;
  294. DARRAY(struct shader_attrib) attribs;
  295. DARRAY(struct shader_param) params;
  296. DARRAY(samplerstate_t) samplers;
  297. };
  298. extern void shader_update_textures(struct gs_shader *shader);
  299. struct gs_vertex_buffer {
  300. GLuint vao;
  301. GLuint vertex_buffer;
  302. GLuint normal_buffer;
  303. GLuint tangent_buffer;
  304. GLuint color_buffer;
  305. DARRAY(GLuint) uv_buffers;
  306. DARRAY(size_t) uv_sizes;
  307. device_t device;
  308. size_t num;
  309. bool dynamic;
  310. struct vb_data *data;
  311. };
  312. extern bool vertexbuffer_load(device_t device, vertbuffer_t vb);
  313. struct gs_index_buffer {
  314. GLuint buffer;
  315. enum gs_index_type type;
  316. GLuint gl_type;
  317. device_t device;
  318. void *data;
  319. size_t num;
  320. size_t width;
  321. size_t size;
  322. bool dynamic;
  323. };
  324. struct gs_texture {
  325. device_t device;
  326. enum gs_texture_type type;
  327. enum gs_color_format format;
  328. GLenum gl_format;
  329. GLenum gl_target;
  330. GLint gl_internal_format;
  331. GLenum gl_type;
  332. GLuint texture;
  333. uint32_t levels;
  334. bool is_dynamic;
  335. bool is_render_target;
  336. bool gen_mipmaps;
  337. samplerstate_t cur_sampler;
  338. };
  339. struct gs_texture_2d {
  340. struct gs_texture base;
  341. uint32_t width;
  342. uint32_t height;
  343. bool gen_mipmaps;
  344. GLuint unpack_buffer;
  345. };
  346. struct gs_texture_cube {
  347. struct gs_texture base;
  348. uint32_t size;
  349. };
  350. struct gs_stage_surface {
  351. enum gs_color_format format;
  352. uint32_t width;
  353. uint32_t height;
  354. uint32_t bytes_per_pixel;
  355. GLenum gl_format;
  356. GLint gl_internal_format;
  357. GLenum gl_type;
  358. GLuint texture;
  359. GLuint pack_buffer;
  360. };
  361. struct gs_zstencil_buffer {
  362. device_t device;
  363. GLuint buffer;
  364. GLuint attachment;
  365. GLenum format;
  366. };
  367. struct gs_swap_chain {
  368. device_t device;
  369. struct gl_windowinfo *wi;
  370. struct gs_init_data info;
  371. };
  372. struct fbo_info {
  373. GLuint fbo;
  374. uint32_t width;
  375. uint32_t height;
  376. enum gs_color_format format;
  377. texture_t cur_render_target;
  378. int cur_render_side;
  379. zstencil_t cur_zstencil_buffer;
  380. };
  381. static inline void fbo_info_destroy(struct fbo_info *fbo)
  382. {
  383. if (fbo) {
  384. glDeleteFramebuffers(1, &fbo->fbo);
  385. gl_success("glDeleteFramebuffers");
  386. bfree(fbo);
  387. }
  388. }
  389. struct gs_device {
  390. struct gl_platform *plat;
  391. GLuint pipeline;
  392. enum copy_type copy_type;
  393. texture_t cur_render_target;
  394. zstencil_t cur_zstencil_buffer;
  395. int cur_render_side;
  396. texture_t cur_textures[GS_MAX_TEXTURES];
  397. samplerstate_t cur_samplers[GS_MAX_TEXTURES];
  398. vertbuffer_t cur_vertex_buffer;
  399. indexbuffer_t cur_index_buffer;
  400. shader_t cur_vertex_shader;
  401. shader_t cur_pixel_shader;
  402. swapchain_t cur_swap;
  403. enum gs_cull_mode cur_cull_mode;
  404. struct gs_rect cur_viewport;
  405. struct matrix4 cur_proj;
  406. struct matrix4 cur_view;
  407. struct matrix4 cur_viewproj;
  408. DARRAY(struct matrix4) proj_stack;
  409. DARRAY(struct fbo_info*) fbos;
  410. struct fbo_info *cur_fbo;
  411. };
  412. extern struct gl_platform *gl_platform_create(device_t device,
  413. struct gs_init_data *info);
  414. extern struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform);
  415. extern void gl_platform_destroy(struct gl_platform *platform);
  416. extern struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info);
  417. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi);
  418. extern void gl_getclientsize(struct gs_swap_chain *swap,
  419. uint32_t *width,
  420. uint32_t *height);