gl-subsystem.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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_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,
  304. GLint *min_filter, GLint *mag_filter)
  305. {
  306. switch (filter) {
  307. case GS_FILTER_POINT:
  308. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  309. *mag_filter = GL_NEAREST;
  310. return;
  311. case GS_FILTER_LINEAR:
  312. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  313. *mag_filter = GL_LINEAR;
  314. return;
  315. case GS_FILTER_MIN_MAG_POINT_MIP_LINEAR:
  316. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  317. *mag_filter = GL_NEAREST;
  318. return;
  319. case GS_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT:
  320. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  321. *mag_filter = GL_LINEAR;
  322. return;
  323. case GS_FILTER_MIN_POINT_MAG_MIP_LINEAR:
  324. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  325. *mag_filter = GL_LINEAR;
  326. return;
  327. case GS_FILTER_MIN_LINEAR_MAG_MIP_POINT:
  328. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  329. *mag_filter = GL_NEAREST;
  330. return;
  331. case GS_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
  332. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  333. *mag_filter = GL_NEAREST;
  334. return;
  335. case GS_FILTER_MIN_MAG_LINEAR_MIP_POINT:
  336. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  337. *mag_filter = GL_LINEAR;
  338. return;
  339. case GS_FILTER_ANISOTROPIC:
  340. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  341. *mag_filter = GL_LINEAR;
  342. return;
  343. }
  344. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  345. *mag_filter = GL_NEAREST;
  346. }
  347. static inline GLint convert_address_mode(enum gs_address_mode mode)
  348. {
  349. switch (mode) {
  350. case GS_ADDRESS_WRAP:
  351. return GL_REPEAT;
  352. case GS_ADDRESS_CLAMP:
  353. return GL_CLAMP_TO_EDGE;
  354. case GS_ADDRESS_MIRROR:
  355. return GL_MIRRORED_REPEAT;
  356. case GS_ADDRESS_BORDER:
  357. return GL_CLAMP_TO_BORDER;
  358. case GS_ADDRESS_MIRRORONCE:
  359. return GL_MIRROR_CLAMP_EXT;
  360. }
  361. return GL_REPEAT;
  362. }
  363. static inline GLenum convert_gs_topology(enum gs_draw_mode mode)
  364. {
  365. switch (mode) {
  366. case GS_POINTS:
  367. return GL_POINTS;
  368. case GS_LINES:
  369. return GL_LINES;
  370. case GS_LINESTRIP:
  371. return GL_LINE_STRIP;
  372. case GS_TRIS:
  373. return GL_TRIANGLES;
  374. case GS_TRISTRIP:
  375. return GL_TRIANGLE_STRIP;
  376. }
  377. return GL_POINTS;
  378. }
  379. extern void convert_sampler_info(struct gs_sampler_state *sampler,
  380. const struct gs_sampler_info *info);
  381. struct gs_sampler_state {
  382. gs_device_t *device;
  383. volatile long ref;
  384. GLint min_filter;
  385. GLint mag_filter;
  386. GLint address_u;
  387. GLint address_v;
  388. GLint address_w;
  389. GLint max_anisotropy;
  390. struct vec4 border_color;
  391. };
  392. static inline void samplerstate_addref(gs_samplerstate_t *ss)
  393. {
  394. os_atomic_inc_long(&ss->ref);
  395. }
  396. static inline void samplerstate_release(gs_samplerstate_t *ss)
  397. {
  398. if (os_atomic_dec_long(&ss->ref) == 0)
  399. bfree(ss);
  400. }
  401. struct gs_timer {
  402. GLuint queries[2];
  403. };
  404. struct gs_shader_param {
  405. enum gs_shader_param_type type;
  406. char *name;
  407. gs_shader_t *shader;
  408. gs_samplerstate_t *next_sampler;
  409. GLint texture_id;
  410. size_t sampler_id;
  411. int array_count;
  412. struct gs_texture *texture;
  413. bool srgb;
  414. DARRAY(uint8_t) cur_value;
  415. DARRAY(uint8_t) def_value;
  416. bool changed;
  417. };
  418. enum attrib_type {
  419. ATTRIB_POSITION,
  420. ATTRIB_NORMAL,
  421. ATTRIB_TANGENT,
  422. ATTRIB_COLOR,
  423. ATTRIB_TEXCOORD,
  424. ATTRIB_TARGET
  425. };
  426. struct shader_attrib {
  427. char *name;
  428. size_t index;
  429. enum attrib_type type;
  430. };
  431. struct gs_shader {
  432. gs_device_t *device;
  433. enum gs_shader_type type;
  434. GLuint obj;
  435. struct gs_shader_param *viewproj;
  436. struct gs_shader_param *world;
  437. DARRAY(struct shader_attrib) attribs;
  438. DARRAY(struct gs_shader_param) params;
  439. DARRAY(gs_samplerstate_t *) samplers;
  440. };
  441. struct program_param {
  442. GLint obj;
  443. struct gs_shader_param *param;
  444. };
  445. struct gs_program {
  446. gs_device_t *device;
  447. GLuint obj;
  448. struct gs_shader *vertex_shader;
  449. struct gs_shader *pixel_shader;
  450. DARRAY(struct program_param) params;
  451. DARRAY(GLint) attribs;
  452. struct gs_program **prev_next;
  453. struct gs_program *next;
  454. };
  455. extern struct gs_program *gs_program_create(struct gs_device *device);
  456. extern void gs_program_destroy(struct gs_program *program);
  457. extern void program_update_params(struct gs_program *shader);
  458. struct gs_vertex_buffer {
  459. GLuint vao;
  460. GLuint vertex_buffer;
  461. GLuint normal_buffer;
  462. GLuint tangent_buffer;
  463. GLuint color_buffer;
  464. DARRAY(GLuint) uv_buffers;
  465. DARRAY(size_t) uv_sizes;
  466. gs_device_t *device;
  467. size_t num;
  468. bool dynamic;
  469. struct gs_vb_data *data;
  470. };
  471. extern bool load_vb_buffers(struct gs_program *program,
  472. struct gs_vertex_buffer *vb,
  473. struct gs_index_buffer *ib);
  474. struct gs_index_buffer {
  475. GLuint buffer;
  476. enum gs_index_type type;
  477. GLuint gl_type;
  478. gs_device_t *device;
  479. void *data;
  480. size_t num;
  481. size_t width;
  482. size_t size;
  483. bool dynamic;
  484. };
  485. struct gs_texture {
  486. gs_device_t *device;
  487. enum gs_texture_type type;
  488. enum gs_color_format format;
  489. GLenum gl_format;
  490. GLenum gl_target;
  491. GLenum gl_internal_format;
  492. GLenum gl_type;
  493. GLuint texture;
  494. uint32_t levels;
  495. bool is_dynamic;
  496. bool is_render_target;
  497. bool is_dummy;
  498. bool gen_mipmaps;
  499. gs_samplerstate_t *cur_sampler;
  500. struct fbo_info *fbo;
  501. };
  502. struct gs_texture_2d {
  503. struct gs_texture base;
  504. uint32_t width;
  505. uint32_t height;
  506. bool gen_mipmaps;
  507. GLuint unpack_buffer;
  508. };
  509. struct gs_texture_3d {
  510. struct gs_texture base;
  511. uint32_t width;
  512. uint32_t height;
  513. uint32_t depth;
  514. bool gen_mipmaps;
  515. GLuint unpack_buffer;
  516. };
  517. struct gs_texture_cube {
  518. struct gs_texture base;
  519. uint32_t size;
  520. };
  521. struct gs_stage_surface {
  522. gs_device_t *device;
  523. enum gs_color_format format;
  524. uint32_t width;
  525. uint32_t height;
  526. uint32_t bytes_per_pixel;
  527. GLenum gl_format;
  528. GLint gl_internal_format;
  529. GLenum gl_type;
  530. GLuint pack_buffer;
  531. };
  532. struct gs_zstencil_buffer {
  533. gs_device_t *device;
  534. GLuint buffer;
  535. GLuint attachment;
  536. GLenum format;
  537. };
  538. struct gs_swap_chain {
  539. gs_device_t *device;
  540. struct gl_windowinfo *wi;
  541. struct gs_init_data info;
  542. };
  543. struct fbo_info {
  544. GLuint fbo;
  545. uint32_t width;
  546. uint32_t height;
  547. enum gs_color_format format;
  548. gs_texture_t *cur_render_target;
  549. int cur_render_side;
  550. gs_zstencil_t *cur_zstencil_buffer;
  551. };
  552. static inline void fbo_info_destroy(struct fbo_info *fbo)
  553. {
  554. if (fbo) {
  555. glDeleteFramebuffers(1, &fbo->fbo);
  556. gl_success("glDeleteFramebuffers");
  557. bfree(fbo);
  558. }
  559. }
  560. struct gs_device {
  561. struct gl_platform *plat;
  562. enum copy_type copy_type;
  563. GLuint empty_vao;
  564. gs_samplerstate_t *raw_load_sampler;
  565. gs_texture_t *cur_render_target;
  566. gs_zstencil_t *cur_zstencil_buffer;
  567. int cur_render_side;
  568. gs_texture_t *cur_textures[GS_MAX_TEXTURES];
  569. gs_samplerstate_t *cur_samplers[GS_MAX_TEXTURES];
  570. gs_vertbuffer_t *cur_vertex_buffer;
  571. gs_indexbuffer_t *cur_index_buffer;
  572. gs_shader_t *cur_vertex_shader;
  573. gs_shader_t *cur_pixel_shader;
  574. gs_swapchain_t *cur_swap;
  575. struct gs_program *cur_program;
  576. enum gs_color_space cur_color_space;
  577. struct gs_program *first_program;
  578. enum gs_cull_mode cur_cull_mode;
  579. struct gs_rect cur_viewport;
  580. struct matrix4 cur_proj;
  581. struct matrix4 cur_view;
  582. struct matrix4 cur_viewproj;
  583. DARRAY(struct matrix4) proj_stack;
  584. struct fbo_info *cur_fbo;
  585. };
  586. extern struct fbo_info *get_fbo(gs_texture_t *tex, uint32_t width,
  587. uint32_t height);
  588. extern void gl_update(gs_device_t *device);
  589. extern void gl_clear_context(gs_device_t *device);
  590. extern struct gl_platform *gl_platform_create(gs_device_t *device,
  591. uint32_t adapter);
  592. extern void gl_platform_destroy(struct gl_platform *platform);
  593. extern bool gl_platform_init_swapchain(struct gs_swap_chain *swap);
  594. extern void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap);
  595. extern struct gl_windowinfo *
  596. gl_windowinfo_create(const struct gs_init_data *info);
  597. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi);
  598. extern void gl_getclientsize(const struct gs_swap_chain *swap, uint32_t *width,
  599. uint32_t *height);