gl-subsystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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_R16;
  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_SHORT;
  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_shader_param {
  367. enum gs_shader_param_type type;
  368. char *name;
  369. gs_shader_t *shader;
  370. gs_samplerstate_t *next_sampler;
  371. GLint texture_id;
  372. size_t sampler_id;
  373. int array_count;
  374. struct gs_texture *texture;
  375. DARRAY(uint8_t) cur_value;
  376. DARRAY(uint8_t) def_value;
  377. bool changed;
  378. };
  379. enum attrib_type {
  380. ATTRIB_POSITION,
  381. ATTRIB_NORMAL,
  382. ATTRIB_TANGENT,
  383. ATTRIB_COLOR,
  384. ATTRIB_TEXCOORD,
  385. ATTRIB_TARGET
  386. };
  387. struct shader_attrib {
  388. char *name;
  389. size_t index;
  390. enum attrib_type type;
  391. };
  392. struct gs_shader {
  393. gs_device_t *device;
  394. enum gs_shader_type type;
  395. GLuint obj;
  396. struct gs_shader_param *viewproj;
  397. struct gs_shader_param *world;
  398. DARRAY(struct shader_attrib) attribs;
  399. DARRAY(struct gs_shader_param) params;
  400. DARRAY(gs_samplerstate_t *) samplers;
  401. };
  402. struct program_param {
  403. GLint obj;
  404. struct gs_shader_param *param;
  405. };
  406. struct gs_program {
  407. gs_device_t *device;
  408. GLuint obj;
  409. struct gs_shader *vertex_shader;
  410. struct gs_shader *pixel_shader;
  411. DARRAY(struct program_param) params;
  412. DARRAY(GLint) attribs;
  413. struct gs_program **prev_next;
  414. struct gs_program *next;
  415. };
  416. extern struct gs_program *gs_program_create(struct gs_device *device);
  417. extern void gs_program_destroy(struct gs_program *program);
  418. extern void program_update_params(struct gs_program *shader);
  419. struct gs_vertex_buffer {
  420. GLuint vao;
  421. GLuint vertex_buffer;
  422. GLuint normal_buffer;
  423. GLuint tangent_buffer;
  424. GLuint color_buffer;
  425. DARRAY(GLuint) uv_buffers;
  426. DARRAY(size_t) uv_sizes;
  427. gs_device_t *device;
  428. size_t num;
  429. bool dynamic;
  430. struct gs_vb_data *data;
  431. };
  432. extern bool load_vb_buffers(struct gs_program *program,
  433. struct gs_vertex_buffer *vb,
  434. struct gs_index_buffer *ib);
  435. struct gs_index_buffer {
  436. GLuint buffer;
  437. enum gs_index_type type;
  438. GLuint gl_type;
  439. gs_device_t *device;
  440. void *data;
  441. size_t num;
  442. size_t width;
  443. size_t size;
  444. bool dynamic;
  445. };
  446. struct gs_texture {
  447. gs_device_t *device;
  448. enum gs_texture_type type;
  449. enum gs_color_format format;
  450. GLenum gl_format;
  451. GLenum gl_target;
  452. GLenum gl_internal_format;
  453. GLenum gl_type;
  454. GLuint texture;
  455. uint32_t levels;
  456. bool is_dynamic;
  457. bool is_render_target;
  458. bool is_dummy;
  459. bool gen_mipmaps;
  460. gs_samplerstate_t *cur_sampler;
  461. struct fbo_info *fbo;
  462. };
  463. struct gs_texture_2d {
  464. struct gs_texture base;
  465. uint32_t width;
  466. uint32_t height;
  467. bool gen_mipmaps;
  468. GLuint unpack_buffer;
  469. };
  470. struct gs_texture_cube {
  471. struct gs_texture base;
  472. uint32_t size;
  473. };
  474. struct gs_stage_surface {
  475. gs_device_t *device;
  476. enum gs_color_format format;
  477. uint32_t width;
  478. uint32_t height;
  479. uint32_t bytes_per_pixel;
  480. GLenum gl_format;
  481. GLint gl_internal_format;
  482. GLenum gl_type;
  483. GLuint pack_buffer;
  484. };
  485. struct gs_zstencil_buffer {
  486. gs_device_t *device;
  487. GLuint buffer;
  488. GLuint attachment;
  489. GLenum format;
  490. };
  491. struct gs_swap_chain {
  492. gs_device_t *device;
  493. struct gl_windowinfo *wi;
  494. struct gs_init_data info;
  495. };
  496. struct fbo_info {
  497. GLuint fbo;
  498. uint32_t width;
  499. uint32_t height;
  500. enum gs_color_format format;
  501. gs_texture_t *cur_render_target;
  502. int cur_render_side;
  503. gs_zstencil_t *cur_zstencil_buffer;
  504. };
  505. static inline void fbo_info_destroy(struct fbo_info *fbo)
  506. {
  507. if (fbo) {
  508. glDeleteFramebuffers(1, &fbo->fbo);
  509. gl_success("glDeleteFramebuffers");
  510. bfree(fbo);
  511. }
  512. }
  513. struct gs_device {
  514. struct gl_platform *plat;
  515. enum copy_type copy_type;
  516. GLuint empty_vao;
  517. gs_texture_t *cur_render_target;
  518. gs_zstencil_t *cur_zstencil_buffer;
  519. int cur_render_side;
  520. gs_texture_t *cur_textures[GS_MAX_TEXTURES];
  521. gs_samplerstate_t *cur_samplers[GS_MAX_TEXTURES];
  522. gs_vertbuffer_t *cur_vertex_buffer;
  523. gs_indexbuffer_t *cur_index_buffer;
  524. gs_shader_t *cur_vertex_shader;
  525. gs_shader_t *cur_pixel_shader;
  526. gs_swapchain_t *cur_swap;
  527. struct gs_program *cur_program;
  528. struct gs_program *first_program;
  529. enum gs_cull_mode cur_cull_mode;
  530. struct gs_rect cur_viewport;
  531. struct matrix4 cur_proj;
  532. struct matrix4 cur_view;
  533. struct matrix4 cur_viewproj;
  534. DARRAY(struct matrix4) proj_stack;
  535. struct fbo_info *cur_fbo;
  536. };
  537. extern struct fbo_info *get_fbo(gs_texture_t *tex, uint32_t width,
  538. uint32_t height);
  539. extern void gl_update(gs_device_t *device);
  540. extern struct gl_platform *gl_platform_create(gs_device_t *device,
  541. uint32_t adapter);
  542. extern void gl_platform_destroy(struct gl_platform *platform);
  543. extern bool gl_platform_init_swapchain(struct gs_swap_chain *swap);
  544. extern void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap);
  545. extern struct gl_windowinfo *
  546. gl_windowinfo_create(const struct gs_init_data *info);
  547. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi);
  548. extern void gl_getclientsize(const struct gs_swap_chain *swap, uint32_t *width,
  549. uint32_t *height);