gl-subsystem.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 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. #pragma once
  15. #include <util/darray.h>
  16. #include <util/threading.h>
  17. #include <graphics/graphics.h>
  18. #include <graphics/device-exports.h>
  19. #include <graphics/matrix4.h>
  20. #include <glad/glad.h>
  21. #include "gl-helpers.h"
  22. struct gl_platform;
  23. struct gl_windowinfo;
  24. enum copy_type {
  25. COPY_TYPE_ARB,
  26. COPY_TYPE_NV,
  27. COPY_TYPE_FBO_BLIT
  28. };
  29. static inline GLenum convert_gs_format(enum gs_color_format format)
  30. {
  31. switch (format) {
  32. case GS_A8: return GL_RED;
  33. case GS_R8: return GL_RED;
  34. case GS_RGBA: return GL_RGBA;
  35. case GS_BGRX: return GL_BGRA;
  36. case GS_BGRA: return GL_BGRA;
  37. case GS_R10G10B10A2: return GL_RGBA;
  38. case GS_RGBA16: return GL_RGBA;
  39. case GS_R16: return GL_RED;
  40. case GS_RGBA16F: return GL_RGBA;
  41. case GS_RGBA32F: return GL_RGBA;
  42. case GS_RG16F: return GL_RG;
  43. case GS_RG32F: return GL_RG;
  44. case GS_R8G8: return GL_RG;
  45. case GS_R16F: return GL_RED;
  46. case GS_R32F: return GL_RED;
  47. case GS_DXT1: return GL_RGB;
  48. case GS_DXT3: return GL_RGBA;
  49. case GS_DXT5: return GL_RGBA;
  50. case GS_UNKNOWN: return 0;
  51. }
  52. return 0;
  53. }
  54. static inline GLenum convert_gs_internal_format(enum gs_color_format format)
  55. {
  56. switch (format) {
  57. case GS_A8: return GL_R8; /* NOTE: use GL_TEXTURE_SWIZZLE_x */
  58. case GS_R8: return GL_R8;
  59. case GS_RGBA: return GL_RGBA;
  60. case GS_BGRX: return GL_RGB;
  61. case GS_BGRA: return GL_RGBA;
  62. case GS_R10G10B10A2: return GL_RGB10_A2;
  63. case GS_RGBA16: return GL_RGBA16;
  64. case GS_R16: return GL_R16;
  65. case GS_RGBA16F: return GL_RGBA16F;
  66. case GS_RGBA32F: return GL_RGBA32F;
  67. case GS_RG16F: return GL_RG16F;
  68. case GS_RG32F: return GL_RG32F;
  69. case GS_R8G8: return GL_R16;
  70. case GS_R16F: return GL_R16F;
  71. case GS_R32F: return GL_R32F;
  72. case GS_DXT1: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  73. case GS_DXT3: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  74. case GS_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  75. case GS_UNKNOWN: return 0;
  76. }
  77. return 0;
  78. }
  79. static inline GLenum get_gl_format_type(enum gs_color_format format)
  80. {
  81. switch (format) {
  82. case GS_A8: return GL_UNSIGNED_BYTE;
  83. case GS_R8: return GL_UNSIGNED_BYTE;
  84. case GS_RGBA: return GL_UNSIGNED_BYTE;
  85. case GS_BGRX: return GL_UNSIGNED_BYTE;
  86. case GS_BGRA: return GL_UNSIGNED_BYTE;
  87. case GS_R10G10B10A2: return GL_UNSIGNED_INT_10_10_10_2;
  88. case GS_RGBA16: return GL_UNSIGNED_SHORT;
  89. case GS_R16: return GL_UNSIGNED_SHORT;
  90. case GS_RGBA16F: return GL_UNSIGNED_SHORT;
  91. case GS_RGBA32F: return GL_FLOAT;
  92. case GS_RG16F: return GL_UNSIGNED_SHORT;
  93. case GS_RG32F: return GL_FLOAT;
  94. case GS_R8G8: return GL_UNSIGNED_SHORT;
  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_type 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 gs_shader_type type)
  168. {
  169. switch (type) {
  170. case GS_SHADER_VERTEX: return GL_VERTEX_SHADER;
  171. case GS_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. const struct gs_sampler_info *info);
  243. struct gs_sampler_state {
  244. gs_device_t *device;
  245. volatile long 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(gs_samplerstate_t *ss)
  254. {
  255. os_atomic_inc_long(&ss->ref);
  256. }
  257. static inline void samplerstate_release(gs_samplerstate_t *ss)
  258. {
  259. if (os_atomic_dec_long(&ss->ref) == 0)
  260. bfree(ss);
  261. }
  262. struct gs_shader_param {
  263. enum gs_shader_param_type type;
  264. char *name;
  265. gs_shader_t *shader;
  266. gs_samplerstate_t *next_sampler;
  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. char *name;
  285. size_t index;
  286. enum attrib_type type;
  287. };
  288. struct gs_shader {
  289. gs_device_t *device;
  290. enum gs_shader_type type;
  291. GLuint obj;
  292. struct gs_shader_param *viewproj;
  293. struct gs_shader_param *world;
  294. DARRAY(struct shader_attrib) attribs;
  295. DARRAY(struct gs_shader_param) params;
  296. DARRAY(gs_samplerstate_t*) samplers;
  297. };
  298. struct program_param {
  299. GLint obj;
  300. struct gs_shader_param *param;
  301. };
  302. struct gs_program {
  303. gs_device_t *device;
  304. GLuint obj;
  305. struct gs_shader *vertex_shader;
  306. struct gs_shader *pixel_shader;
  307. DARRAY(struct program_param) params;
  308. DARRAY(GLint) attribs;
  309. struct gs_program **prev_next;
  310. struct gs_program *next;
  311. };
  312. extern struct gs_program *gs_program_create(struct gs_device *device);
  313. extern void gs_program_destroy(struct gs_program *program);
  314. extern void program_update_params(struct gs_program *shader);
  315. struct gs_vertex_buffer {
  316. GLuint vao;
  317. GLuint vertex_buffer;
  318. GLuint normal_buffer;
  319. GLuint tangent_buffer;
  320. GLuint color_buffer;
  321. DARRAY(GLuint) uv_buffers;
  322. DARRAY(size_t) uv_sizes;
  323. gs_device_t *device;
  324. size_t num;
  325. bool dynamic;
  326. struct gs_vb_data *data;
  327. };
  328. extern bool load_vb_buffers(struct gs_program *program,
  329. struct gs_vertex_buffer *vb, struct gs_index_buffer *ib);
  330. struct gs_index_buffer {
  331. GLuint buffer;
  332. enum gs_index_type type;
  333. GLuint gl_type;
  334. gs_device_t *device;
  335. void *data;
  336. size_t num;
  337. size_t width;
  338. size_t size;
  339. bool dynamic;
  340. };
  341. struct gs_texture {
  342. gs_device_t *device;
  343. enum gs_texture_type type;
  344. enum gs_color_format format;
  345. GLenum gl_format;
  346. GLenum gl_target;
  347. GLenum gl_internal_format;
  348. GLenum gl_type;
  349. GLuint texture;
  350. uint32_t levels;
  351. bool is_dynamic;
  352. bool is_render_target;
  353. bool is_dummy;
  354. bool gen_mipmaps;
  355. gs_samplerstate_t *cur_sampler;
  356. struct fbo_info *fbo;
  357. };
  358. struct gs_texture_2d {
  359. struct gs_texture base;
  360. uint32_t width;
  361. uint32_t height;
  362. bool gen_mipmaps;
  363. GLuint unpack_buffer;
  364. };
  365. struct gs_texture_cube {
  366. struct gs_texture base;
  367. uint32_t size;
  368. };
  369. struct gs_stage_surface {
  370. gs_device_t *device;
  371. enum gs_color_format format;
  372. uint32_t width;
  373. uint32_t height;
  374. uint32_t bytes_per_pixel;
  375. GLenum gl_format;
  376. GLint gl_internal_format;
  377. GLenum gl_type;
  378. GLuint pack_buffer;
  379. };
  380. struct gs_zstencil_buffer {
  381. gs_device_t *device;
  382. GLuint buffer;
  383. GLuint attachment;
  384. GLenum format;
  385. };
  386. struct gs_swap_chain {
  387. gs_device_t *device;
  388. struct gl_windowinfo *wi;
  389. struct gs_init_data info;
  390. };
  391. struct fbo_info {
  392. GLuint fbo;
  393. uint32_t width;
  394. uint32_t height;
  395. enum gs_color_format format;
  396. gs_texture_t *cur_render_target;
  397. int cur_render_side;
  398. gs_zstencil_t *cur_zstencil_buffer;
  399. };
  400. static inline void fbo_info_destroy(struct fbo_info *fbo)
  401. {
  402. if (fbo) {
  403. glDeleteFramebuffers(1, &fbo->fbo);
  404. gl_success("glDeleteFramebuffers");
  405. bfree(fbo);
  406. }
  407. }
  408. struct gs_device {
  409. struct gl_platform *plat;
  410. enum copy_type copy_type;
  411. gs_texture_t *cur_render_target;
  412. gs_zstencil_t *cur_zstencil_buffer;
  413. int cur_render_side;
  414. gs_texture_t *cur_textures[GS_MAX_TEXTURES];
  415. gs_samplerstate_t *cur_samplers[GS_MAX_TEXTURES];
  416. gs_vertbuffer_t *cur_vertex_buffer;
  417. gs_indexbuffer_t *cur_index_buffer;
  418. gs_shader_t *cur_vertex_shader;
  419. gs_shader_t *cur_pixel_shader;
  420. gs_swapchain_t *cur_swap;
  421. struct gs_program *cur_program;
  422. struct gs_program *first_program;
  423. enum gs_cull_mode cur_cull_mode;
  424. struct gs_rect cur_viewport;
  425. struct matrix4 cur_proj;
  426. struct matrix4 cur_view;
  427. struct matrix4 cur_viewproj;
  428. DARRAY(struct matrix4) proj_stack;
  429. struct fbo_info *cur_fbo;
  430. };
  431. extern struct fbo_info *get_fbo(gs_texture_t *tex, uint32_t width,
  432. uint32_t height);
  433. extern void gl_update(gs_device_t *device);
  434. extern struct gl_platform *gl_platform_create(gs_device_t *device,
  435. uint32_t adapter);
  436. extern void gl_platform_destroy(struct gl_platform *platform);
  437. extern bool gl_platform_init_swapchain(struct gs_swap_chain *swap);
  438. extern void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap);
  439. extern struct gl_windowinfo *gl_windowinfo_create(
  440. const struct gs_init_data *info);
  441. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi);
  442. extern void gl_getclientsize(const struct gs_swap_chain *swap,
  443. uint32_t *width,
  444. uint32_t *height);