gl-subsystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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 { COPY_TYPE_ARB, COPY_TYPE_NV, COPY_TYPE_FBO_BLIT };
  25. static inline GLenum convert_gs_format(enum gs_color_format format)
  26. {
  27. switch (format) {
  28. case GS_A8:
  29. return GL_RED;
  30. case GS_R8:
  31. return GL_RED;
  32. case GS_RGBA:
  33. return GL_RGBA;
  34. case GS_BGRX:
  35. return GL_BGRA;
  36. case GS_BGRA:
  37. return GL_BGRA;
  38. case GS_R10G10B10A2:
  39. return GL_RGBA;
  40. case GS_RGBA16:
  41. return GL_RGBA;
  42. case GS_R16:
  43. return GL_RED;
  44. case GS_RGBA16F:
  45. return GL_RGBA;
  46. case GS_RGBA32F:
  47. return GL_RGBA;
  48. case GS_RG16F:
  49. return GL_RG;
  50. case GS_RG32F:
  51. return GL_RG;
  52. case GS_R8G8:
  53. return GL_RG;
  54. case GS_R16F:
  55. return GL_RED;
  56. case GS_R32F:
  57. return GL_RED;
  58. case GS_DXT1:
  59. return GL_RGB;
  60. case GS_DXT3:
  61. return GL_RGBA;
  62. case GS_DXT5:
  63. return GL_RGBA;
  64. case GS_UNKNOWN:
  65. return 0;
  66. }
  67. return 0;
  68. }
  69. static inline GLenum convert_gs_internal_format(enum gs_color_format format)
  70. {
  71. switch (format) {
  72. case GS_A8:
  73. return GL_R8; /* NOTE: use GL_TEXTURE_SWIZZLE_x */
  74. case GS_R8:
  75. return GL_R8;
  76. case GS_RGBA:
  77. return GL_RGBA;
  78. case GS_BGRX:
  79. return GL_RGB;
  80. case GS_BGRA:
  81. return GL_RGBA;
  82. case GS_R10G10B10A2:
  83. return GL_RGB10_A2;
  84. case GS_RGBA16:
  85. return GL_RGBA16;
  86. case GS_R16:
  87. return GL_R16;
  88. case GS_RGBA16F:
  89. return GL_RGBA16F;
  90. case GS_RGBA32F:
  91. return GL_RGBA32F;
  92. case GS_RG16F:
  93. return GL_RG16F;
  94. case GS_RG32F:
  95. return GL_RG32F;
  96. case GS_R8G8:
  97. return GL_RG8;
  98. case GS_R16F:
  99. return GL_R16F;
  100. case GS_R32F:
  101. return GL_R32F;
  102. case GS_DXT1:
  103. return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  104. case GS_DXT3:
  105. return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  106. case GS_DXT5:
  107. return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  108. case GS_UNKNOWN:
  109. return 0;
  110. }
  111. return 0;
  112. }
  113. static inline GLenum get_gl_format_type(enum gs_color_format format)
  114. {
  115. switch (format) {
  116. case GS_A8:
  117. return GL_UNSIGNED_BYTE;
  118. case GS_R8:
  119. return GL_UNSIGNED_BYTE;
  120. case GS_RGBA:
  121. return GL_UNSIGNED_BYTE;
  122. case GS_BGRX:
  123. return GL_UNSIGNED_BYTE;
  124. case GS_BGRA:
  125. return GL_UNSIGNED_BYTE;
  126. case GS_R10G10B10A2:
  127. return GL_UNSIGNED_INT_10_10_10_2;
  128. case GS_RGBA16:
  129. return GL_UNSIGNED_SHORT;
  130. case GS_R16:
  131. return GL_UNSIGNED_SHORT;
  132. case GS_RGBA16F:
  133. return GL_HALF_FLOAT;
  134. case GS_RGBA32F:
  135. return GL_FLOAT;
  136. case GS_RG16F:
  137. return GL_HALF_FLOAT;
  138. case GS_RG32F:
  139. return GL_FLOAT;
  140. case GS_R8G8:
  141. return GL_UNSIGNED_BYTE;
  142. case GS_R16F:
  143. return GL_HALF_FLOAT;
  144. case GS_R32F:
  145. return GL_FLOAT;
  146. case GS_DXT1:
  147. return GL_UNSIGNED_BYTE;
  148. case GS_DXT3:
  149. return GL_UNSIGNED_BYTE;
  150. case GS_DXT5:
  151. return GL_UNSIGNED_BYTE;
  152. case GS_UNKNOWN:
  153. return 0;
  154. }
  155. return GL_UNSIGNED_BYTE;
  156. }
  157. static inline GLenum convert_zstencil_format(enum gs_zstencil_format format)
  158. {
  159. switch (format) {
  160. case GS_Z16:
  161. return GL_DEPTH_COMPONENT16;
  162. case GS_Z24_S8:
  163. return GL_DEPTH24_STENCIL8;
  164. case GS_Z32F:
  165. return GL_DEPTH_COMPONENT32F;
  166. case GS_Z32F_S8X24:
  167. return GL_DEPTH32F_STENCIL8;
  168. case GS_ZS_NONE:
  169. return 0;
  170. }
  171. return 0;
  172. }
  173. static inline GLenum convert_gs_depth_test(enum gs_depth_test test)
  174. {
  175. switch (test) {
  176. case GS_NEVER:
  177. return GL_NEVER;
  178. case GS_LESS:
  179. return GL_LESS;
  180. case GS_LEQUAL:
  181. return GL_LEQUAL;
  182. case GS_EQUAL:
  183. return GL_EQUAL;
  184. case GS_GEQUAL:
  185. return GL_GEQUAL;
  186. case GS_GREATER:
  187. return GL_GREATER;
  188. case GS_NOTEQUAL:
  189. return GL_NOTEQUAL;
  190. case GS_ALWAYS:
  191. return GL_ALWAYS;
  192. }
  193. return GL_NEVER;
  194. }
  195. static inline GLenum convert_gs_stencil_op(enum gs_stencil_op_type op)
  196. {
  197. switch (op) {
  198. case GS_KEEP:
  199. return GL_KEEP;
  200. case GS_ZERO:
  201. return GL_ZERO;
  202. case GS_REPLACE:
  203. return GL_REPLACE;
  204. case GS_INCR:
  205. return GL_INCR;
  206. case GS_DECR:
  207. return GL_DECR;
  208. case GS_INVERT:
  209. return GL_INVERT;
  210. }
  211. return GL_KEEP;
  212. }
  213. static inline GLenum convert_gs_stencil_side(enum gs_stencil_side side)
  214. {
  215. switch (side) {
  216. case GS_STENCIL_FRONT:
  217. return GL_FRONT;
  218. case GS_STENCIL_BACK:
  219. return GL_BACK;
  220. case GS_STENCIL_BOTH:
  221. return GL_FRONT_AND_BACK;
  222. }
  223. return GL_FRONT;
  224. }
  225. static inline GLenum convert_gs_blend_type(enum gs_blend_type type)
  226. {
  227. switch (type) {
  228. case GS_BLEND_ZERO:
  229. return GL_ZERO;
  230. case GS_BLEND_ONE:
  231. return GL_ONE;
  232. case GS_BLEND_SRCCOLOR:
  233. return GL_SRC_COLOR;
  234. case GS_BLEND_INVSRCCOLOR:
  235. return GL_ONE_MINUS_SRC_COLOR;
  236. case GS_BLEND_SRCALPHA:
  237. return GL_SRC_ALPHA;
  238. case GS_BLEND_INVSRCALPHA:
  239. return GL_ONE_MINUS_SRC_ALPHA;
  240. case GS_BLEND_DSTCOLOR:
  241. return GL_DST_COLOR;
  242. case GS_BLEND_INVDSTCOLOR:
  243. return GL_ONE_MINUS_DST_COLOR;
  244. case GS_BLEND_DSTALPHA:
  245. return GL_DST_ALPHA;
  246. case GS_BLEND_INVDSTALPHA:
  247. return GL_ONE_MINUS_DST_ALPHA;
  248. case GS_BLEND_SRCALPHASAT:
  249. return GL_SRC_ALPHA_SATURATE;
  250. }
  251. return GL_ONE;
  252. }
  253. static inline GLenum convert_shader_type(enum gs_shader_type type)
  254. {
  255. switch (type) {
  256. case GS_SHADER_VERTEX:
  257. return GL_VERTEX_SHADER;
  258. case GS_SHADER_PIXEL:
  259. return GL_FRAGMENT_SHADER;
  260. }
  261. return GL_VERTEX_SHADER;
  262. }
  263. static inline void convert_filter(enum gs_sample_filter filter,
  264. GLint *min_filter, GLint *mag_filter)
  265. {
  266. switch (filter) {
  267. case GS_FILTER_POINT:
  268. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  269. *mag_filter = GL_NEAREST;
  270. return;
  271. case GS_FILTER_LINEAR:
  272. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  273. *mag_filter = GL_LINEAR;
  274. return;
  275. case GS_FILTER_MIN_MAG_POINT_MIP_LINEAR:
  276. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  277. *mag_filter = GL_NEAREST;
  278. return;
  279. case GS_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT:
  280. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  281. *mag_filter = GL_LINEAR;
  282. return;
  283. case GS_FILTER_MIN_POINT_MAG_MIP_LINEAR:
  284. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  285. *mag_filter = GL_LINEAR;
  286. return;
  287. case GS_FILTER_MIN_LINEAR_MAG_MIP_POINT:
  288. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  289. *mag_filter = GL_NEAREST;
  290. return;
  291. case GS_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
  292. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  293. *mag_filter = GL_NEAREST;
  294. return;
  295. case GS_FILTER_MIN_MAG_LINEAR_MIP_POINT:
  296. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  297. *mag_filter = GL_LINEAR;
  298. return;
  299. case GS_FILTER_ANISOTROPIC:
  300. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  301. *mag_filter = GL_LINEAR;
  302. return;
  303. }
  304. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  305. *mag_filter = GL_NEAREST;
  306. }
  307. static inline GLint convert_address_mode(enum gs_address_mode mode)
  308. {
  309. switch (mode) {
  310. case GS_ADDRESS_WRAP:
  311. return GL_REPEAT;
  312. case GS_ADDRESS_CLAMP:
  313. return GL_CLAMP_TO_EDGE;
  314. case GS_ADDRESS_MIRROR:
  315. return GL_MIRRORED_REPEAT;
  316. case GS_ADDRESS_BORDER:
  317. return GL_CLAMP_TO_BORDER;
  318. case GS_ADDRESS_MIRRORONCE:
  319. return GL_MIRROR_CLAMP_EXT;
  320. }
  321. return GL_REPEAT;
  322. }
  323. static inline GLenum convert_gs_topology(enum gs_draw_mode mode)
  324. {
  325. switch (mode) {
  326. case GS_POINTS:
  327. return GL_POINTS;
  328. case GS_LINES:
  329. return GL_LINES;
  330. case GS_LINESTRIP:
  331. return GL_LINE_STRIP;
  332. case GS_TRIS:
  333. return GL_TRIANGLES;
  334. case GS_TRISTRIP:
  335. return GL_TRIANGLE_STRIP;
  336. }
  337. return GL_POINTS;
  338. }
  339. extern void convert_sampler_info(struct gs_sampler_state *sampler,
  340. const struct gs_sampler_info *info);
  341. struct gs_sampler_state {
  342. gs_device_t *device;
  343. volatile long ref;
  344. GLint min_filter;
  345. GLint mag_filter;
  346. GLint address_u;
  347. GLint address_v;
  348. GLint address_w;
  349. GLint max_anisotropy;
  350. };
  351. static inline void samplerstate_addref(gs_samplerstate_t *ss)
  352. {
  353. os_atomic_inc_long(&ss->ref);
  354. }
  355. static inline void samplerstate_release(gs_samplerstate_t *ss)
  356. {
  357. if (os_atomic_dec_long(&ss->ref) == 0)
  358. bfree(ss);
  359. }
  360. struct gs_timer {
  361. GLuint queries[2];
  362. };
  363. struct gs_shader_param {
  364. enum gs_shader_param_type type;
  365. char *name;
  366. gs_shader_t *shader;
  367. gs_samplerstate_t *next_sampler;
  368. GLint texture_id;
  369. size_t sampler_id;
  370. int array_count;
  371. struct gs_texture *texture;
  372. DARRAY(uint8_t) cur_value;
  373. DARRAY(uint8_t) def_value;
  374. bool changed;
  375. };
  376. enum attrib_type {
  377. ATTRIB_POSITION,
  378. ATTRIB_NORMAL,
  379. ATTRIB_TANGENT,
  380. ATTRIB_COLOR,
  381. ATTRIB_TEXCOORD,
  382. ATTRIB_TARGET
  383. };
  384. struct shader_attrib {
  385. char *name;
  386. size_t index;
  387. enum attrib_type type;
  388. };
  389. struct gs_shader {
  390. gs_device_t *device;
  391. enum gs_shader_type type;
  392. GLuint obj;
  393. struct gs_shader_param *viewproj;
  394. struct gs_shader_param *world;
  395. DARRAY(struct shader_attrib) attribs;
  396. DARRAY(struct gs_shader_param) params;
  397. DARRAY(gs_samplerstate_t *) samplers;
  398. };
  399. struct program_param {
  400. GLint obj;
  401. struct gs_shader_param *param;
  402. };
  403. struct gs_program {
  404. gs_device_t *device;
  405. GLuint obj;
  406. struct gs_shader *vertex_shader;
  407. struct gs_shader *pixel_shader;
  408. DARRAY(struct program_param) params;
  409. DARRAY(GLint) attribs;
  410. struct gs_program **prev_next;
  411. struct gs_program *next;
  412. };
  413. extern struct gs_program *gs_program_create(struct gs_device *device);
  414. extern void gs_program_destroy(struct gs_program *program);
  415. extern void program_update_params(struct gs_program *shader);
  416. struct gs_vertex_buffer {
  417. GLuint vao;
  418. GLuint vertex_buffer;
  419. GLuint normal_buffer;
  420. GLuint tangent_buffer;
  421. GLuint color_buffer;
  422. DARRAY(GLuint) uv_buffers;
  423. DARRAY(size_t) uv_sizes;
  424. gs_device_t *device;
  425. size_t num;
  426. bool dynamic;
  427. struct gs_vb_data *data;
  428. };
  429. extern bool load_vb_buffers(struct gs_program *program,
  430. struct gs_vertex_buffer *vb,
  431. struct gs_index_buffer *ib);
  432. struct gs_index_buffer {
  433. GLuint buffer;
  434. enum gs_index_type type;
  435. GLuint gl_type;
  436. gs_device_t *device;
  437. void *data;
  438. size_t num;
  439. size_t width;
  440. size_t size;
  441. bool dynamic;
  442. };
  443. struct gs_texture {
  444. gs_device_t *device;
  445. enum gs_texture_type type;
  446. enum gs_color_format format;
  447. GLenum gl_format;
  448. GLenum gl_target;
  449. GLenum gl_internal_format;
  450. GLenum gl_type;
  451. GLuint texture;
  452. uint32_t levels;
  453. bool is_dynamic;
  454. bool is_render_target;
  455. bool is_dummy;
  456. bool gen_mipmaps;
  457. gs_samplerstate_t *cur_sampler;
  458. struct fbo_info *fbo;
  459. };
  460. struct gs_texture_2d {
  461. struct gs_texture base;
  462. uint32_t width;
  463. uint32_t height;
  464. bool gen_mipmaps;
  465. GLuint unpack_buffer;
  466. };
  467. struct gs_texture_3d {
  468. struct gs_texture base;
  469. uint32_t width;
  470. uint32_t height;
  471. uint32_t depth;
  472. bool gen_mipmaps;
  473. GLuint unpack_buffer;
  474. };
  475. struct gs_texture_cube {
  476. struct gs_texture base;
  477. uint32_t size;
  478. };
  479. struct gs_stage_surface {
  480. gs_device_t *device;
  481. enum gs_color_format format;
  482. uint32_t width;
  483. uint32_t height;
  484. uint32_t bytes_per_pixel;
  485. GLenum gl_format;
  486. GLint gl_internal_format;
  487. GLenum gl_type;
  488. GLuint pack_buffer;
  489. };
  490. struct gs_zstencil_buffer {
  491. gs_device_t *device;
  492. GLuint buffer;
  493. GLuint attachment;
  494. GLenum format;
  495. };
  496. struct gs_swap_chain {
  497. gs_device_t *device;
  498. struct gl_windowinfo *wi;
  499. struct gs_init_data info;
  500. };
  501. struct fbo_info {
  502. GLuint fbo;
  503. uint32_t width;
  504. uint32_t height;
  505. enum gs_color_format format;
  506. gs_texture_t *cur_render_target;
  507. int cur_render_side;
  508. gs_zstencil_t *cur_zstencil_buffer;
  509. };
  510. static inline void fbo_info_destroy(struct fbo_info *fbo)
  511. {
  512. if (fbo) {
  513. glDeleteFramebuffers(1, &fbo->fbo);
  514. gl_success("glDeleteFramebuffers");
  515. bfree(fbo);
  516. }
  517. }
  518. struct gs_device {
  519. struct gl_platform *plat;
  520. enum copy_type copy_type;
  521. GLuint empty_vao;
  522. gs_texture_t *cur_render_target;
  523. gs_zstencil_t *cur_zstencil_buffer;
  524. int cur_render_side;
  525. gs_texture_t *cur_textures[GS_MAX_TEXTURES];
  526. gs_samplerstate_t *cur_samplers[GS_MAX_TEXTURES];
  527. gs_vertbuffer_t *cur_vertex_buffer;
  528. gs_indexbuffer_t *cur_index_buffer;
  529. gs_shader_t *cur_vertex_shader;
  530. gs_shader_t *cur_pixel_shader;
  531. gs_swapchain_t *cur_swap;
  532. struct gs_program *cur_program;
  533. struct gs_program *first_program;
  534. enum gs_cull_mode cur_cull_mode;
  535. struct gs_rect cur_viewport;
  536. struct matrix4 cur_proj;
  537. struct matrix4 cur_view;
  538. struct matrix4 cur_viewproj;
  539. DARRAY(struct matrix4) proj_stack;
  540. struct fbo_info *cur_fbo;
  541. };
  542. extern struct fbo_info *get_fbo(gs_texture_t *tex, uint32_t width,
  543. uint32_t height);
  544. extern void gl_update(gs_device_t *device);
  545. extern void gl_clear_context(gs_device_t *device);
  546. extern struct gl_platform *gl_platform_create(gs_device_t *device,
  547. uint32_t adapter);
  548. extern void gl_platform_destroy(struct gl_platform *platform);
  549. extern bool gl_platform_init_swapchain(struct gs_swap_chain *swap);
  550. extern void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap);
  551. extern struct gl_windowinfo *
  552. gl_windowinfo_create(const struct gs_init_data *info);
  553. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi);
  554. extern void gl_getclientsize(const struct gs_swap_chain *swap, uint32_t *width,
  555. uint32_t *height);