gl-subsystem.h 13 KB

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