gl-subsystem.h 14 KB

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