gl-subsystem.h 13 KB

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