gl-subsystem.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 "GL/gl_obs.h"
  21. #include "gl-helpers.h"
  22. #define USE_FORMAT_SWIZZLE
  23. struct gl_platform;
  24. struct gl_windowinfo;
  25. enum copy_type {
  26. COPY_TYPE_ARB,
  27. COPY_TYPE_NV,
  28. COPY_TYPE_FBO_BLIT
  29. };
  30. static inline GLint convert_gs_format(enum gs_color_format format)
  31. {
  32. switch (format) {
  33. case GS_A8: return GL_RGBA;
  34. case GS_R8: return GL_RED;
  35. case GS_RGBA: return GL_RGBA;
  36. #ifdef USE_FORMAT_SWIZZLE
  37. case GS_BGRX: return GL_RGB;
  38. case GS_BGRA: return GL_RGBA;
  39. #else
  40. case GS_BGRX: return GL_BGR;
  41. case GS_BGRA: return GL_BGRA;
  42. #endif
  43. case GS_R10G10B10A2: return GL_RGBA;
  44. case GS_RGBA16: return GL_RGBA;
  45. case GS_R16: return GL_RED;
  46. case GS_RGBA16F: return GL_RGBA;
  47. case GS_RGBA32F: return GL_RGBA;
  48. case GS_RG16F: return GL_RG;
  49. case GS_RG32F: return GL_RG;
  50. case GS_R16F: return GL_RED;
  51. case GS_R32F: return GL_RED;
  52. case GS_DXT1: return GL_RGB;
  53. case GS_DXT3: return GL_RGBA;
  54. case GS_DXT5: return GL_RGBA;
  55. case GS_UNKNOWN: return 0;
  56. }
  57. return 0;
  58. }
  59. static inline GLint convert_gs_internal_format(enum gs_color_format format)
  60. {
  61. switch (format) {
  62. case GS_A8: return GL_R8; /* NOTE: use GL_TEXTURE_SWIZZLE_x */
  63. case GS_R8: return GL_R8;
  64. case GS_RGBA: return GL_RGBA;
  65. case GS_BGRX: return GL_RGBA;
  66. case GS_BGRA: return GL_RGBA;
  67. case GS_R10G10B10A2: return GL_RGB10_A2;
  68. case GS_RGBA16: return GL_RGBA16;
  69. case GS_R16: return GL_R16;
  70. case GS_RGBA16F: return GL_RGBA16F;
  71. case GS_RGBA32F: return GL_RGBA32F;
  72. case GS_RG16F: return GL_RG16F;
  73. case GS_RG32F: return GL_RG32F;
  74. case GS_R16F: return GL_R16F;
  75. case GS_R32F: return GL_R32F;
  76. case GS_DXT1: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  77. case GS_DXT3: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  78. case GS_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  79. case GS_UNKNOWN: return 0;
  80. }
  81. return 0;
  82. }
  83. static inline GLenum get_gl_format_type(enum gs_color_format format)
  84. {
  85. switch (format) {
  86. case GS_A8: return GL_UNSIGNED_BYTE;
  87. case GS_R8: return GL_UNSIGNED_BYTE;
  88. case GS_RGBA: return GL_UNSIGNED_BYTE;
  89. case GS_BGRX: return GL_UNSIGNED_BYTE;
  90. case GS_BGRA: return GL_UNSIGNED_BYTE;
  91. case GS_R10G10B10A2: return GL_UNSIGNED_INT_10_10_10_2;
  92. case GS_RGBA16: return GL_UNSIGNED_SHORT;
  93. case GS_R16: return GL_UNSIGNED_SHORT;
  94. case GS_RGBA16F: return GL_UNSIGNED_SHORT;
  95. case GS_RGBA32F: return GL_FLOAT;
  96. case GS_RG16F: return GL_UNSIGNED_SHORT;
  97. case GS_RG32F: return GL_FLOAT;
  98. case GS_R16F: return GL_UNSIGNED_SHORT;
  99. case GS_R32F: return GL_FLOAT;
  100. case GS_DXT1: return GL_UNSIGNED_BYTE;
  101. case GS_DXT3: return GL_UNSIGNED_BYTE;
  102. case GS_DXT5: return GL_UNSIGNED_BYTE;
  103. case GS_UNKNOWN: return 0;
  104. }
  105. return GL_UNSIGNED_BYTE;
  106. }
  107. static inline GLenum convert_zstencil_format(enum gs_zstencil_format format)
  108. {
  109. switch (format) {
  110. case GS_Z16: return GL_DEPTH_COMPONENT16;
  111. case GS_Z24_S8: return GL_DEPTH24_STENCIL8;
  112. case GS_Z32F: return GL_DEPTH_COMPONENT32F;
  113. case GS_Z32F_S8X24: return GL_DEPTH32F_STENCIL8;
  114. case GS_ZS_NONE: return 0;
  115. }
  116. return 0;
  117. }
  118. static inline GLenum convert_gs_depth_test(enum gs_depth_test test)
  119. {
  120. switch (test) {
  121. case GS_NEVER: return GL_NEVER;
  122. case GS_LESS: return GL_LESS;
  123. case GS_LEQUAL: return GL_LEQUAL;
  124. case GS_EQUAL: return GL_EQUAL;
  125. case GS_GEQUAL: return GL_GEQUAL;
  126. case GS_GREATER: return GL_GREATER;
  127. case GS_NOTEQUAL: return GL_NOTEQUAL;
  128. case GS_ALWAYS: return GL_ALWAYS;
  129. }
  130. return GL_NEVER;
  131. }
  132. static inline GLenum convert_gs_stencil_op(enum gs_stencil_op op)
  133. {
  134. switch (op) {
  135. case GS_KEEP: return GL_KEEP;
  136. case GS_ZERO: return GL_ZERO;
  137. case GS_REPLACE: return GL_REPLACE;
  138. case GS_INCR: return GL_INCR;
  139. case GS_DECR: return GL_DECR;
  140. case GS_INVERT: return GL_INVERT;
  141. }
  142. return GL_KEEP;
  143. }
  144. static inline GLenum convert_gs_stencil_side(enum gs_stencil_side side)
  145. {
  146. switch (side) {
  147. case GS_STENCIL_FRONT: return GL_FRONT;
  148. case GS_STENCIL_BACK: return GL_BACK;
  149. case GS_STENCIL_BOTH: return GL_FRONT_AND_BACK;
  150. }
  151. return GL_FRONT;
  152. }
  153. static inline GLenum convert_gs_blend_type(enum gs_blend_type type)
  154. {
  155. switch (type) {
  156. case GS_BLEND_ZERO: return GL_ZERO;
  157. case GS_BLEND_ONE: return GL_ONE;
  158. case GS_BLEND_SRCCOLOR: return GL_SRC_COLOR;
  159. case GS_BLEND_INVSRCCOLOR: return GL_ONE_MINUS_SRC_COLOR;
  160. case GS_BLEND_SRCALPHA: return GL_SRC_ALPHA;
  161. case GS_BLEND_INVSRCALPHA: return GL_ONE_MINUS_SRC_ALPHA;
  162. case GS_BLEND_DSTCOLOR: return GL_DST_COLOR;
  163. case GS_BLEND_INVDSTCOLOR: return GL_ONE_MINUS_DST_COLOR;
  164. case GS_BLEND_DSTALPHA: return GL_DST_ALPHA;
  165. case GS_BLEND_INVDSTALPHA: return GL_ONE_MINUS_DST_ALPHA;
  166. case GS_BLEND_SRCALPHASAT: return GL_SRC_ALPHA_SATURATE;
  167. }
  168. return GL_ONE;
  169. }
  170. static inline GLenum convert_shader_type(enum shader_type type)
  171. {
  172. switch (type) {
  173. case SHADER_VERTEX: return GL_VERTEX_SHADER;
  174. case SHADER_PIXEL: return GL_FRAGMENT_SHADER;
  175. }
  176. return GL_VERTEX_SHADER;
  177. }
  178. static inline void convert_filter(enum gs_sample_filter filter,
  179. GLint *min_filter, GLint *mag_filter)
  180. {
  181. switch (filter) {
  182. case GS_FILTER_POINT:
  183. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  184. *mag_filter = GL_NEAREST;
  185. return;
  186. case GS_FILTER_LINEAR:
  187. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  188. *mag_filter = GL_LINEAR;
  189. return;
  190. case GS_FILTER_MIN_MAG_POINT_MIP_LINEAR:
  191. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  192. *mag_filter = GL_NEAREST;
  193. return;
  194. case GS_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT:
  195. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  196. *mag_filter = GL_LINEAR;
  197. return;
  198. case GS_FILTER_MIN_POINT_MAG_MIP_LINEAR:
  199. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  200. *mag_filter = GL_LINEAR;
  201. return;
  202. case GS_FILTER_MIN_LINEAR_MAG_MIP_POINT:
  203. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  204. *mag_filter = GL_NEAREST;
  205. return;
  206. case GS_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
  207. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  208. *mag_filter = GL_NEAREST;
  209. return;
  210. case GS_FILTER_MIN_MAG_LINEAR_MIP_POINT:
  211. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  212. *mag_filter = GL_LINEAR;
  213. return;
  214. case GS_FILTER_ANISOTROPIC:
  215. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  216. *mag_filter = GL_LINEAR;
  217. return;
  218. }
  219. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  220. *mag_filter = GL_NEAREST;
  221. }
  222. static inline GLint convert_address_mode(enum gs_address_mode mode)
  223. {
  224. switch (mode) {
  225. case GS_ADDRESS_WRAP: return GL_REPEAT;
  226. case GS_ADDRESS_CLAMP: return GL_CLAMP_TO_EDGE;
  227. case GS_ADDRESS_MIRROR: return GL_MIRRORED_REPEAT;
  228. case GS_ADDRESS_BORDER: return GL_CLAMP_TO_BORDER;
  229. case GS_ADDRESS_MIRRORONCE: return GL_MIRROR_CLAMP_EXT;
  230. }
  231. return GL_REPEAT;
  232. }
  233. static inline GLenum convert_gs_topology(enum gs_draw_mode mode)
  234. {
  235. switch (mode) {
  236. case GS_POINTS: return GL_POINTS;
  237. case GS_LINES: return GL_LINES;
  238. case GS_LINESTRIP: return GL_LINE_STRIP;
  239. case GS_TRIS: return GL_TRIANGLES;
  240. case GS_TRISTRIP: return GL_TRIANGLE_STRIP;
  241. }
  242. return GL_POINTS;
  243. }
  244. extern void convert_sampler_info(struct gs_sampler_state *sampler,
  245. struct gs_sampler_info *info);
  246. struct gs_sampler_state {
  247. device_t device;
  248. volatile long ref;
  249. GLint min_filter;
  250. GLint mag_filter;
  251. GLint address_u;
  252. GLint address_v;
  253. GLint address_w;
  254. GLint max_anisotropy;
  255. };
  256. static inline void samplerstate_addref(samplerstate_t ss)
  257. {
  258. os_atomic_inc_long(&ss->ref);
  259. }
  260. static inline void samplerstate_release(samplerstate_t ss)
  261. {
  262. if (os_atomic_dec_long(&ss->ref) == 0)
  263. bfree(ss);
  264. }
  265. struct shader_param {
  266. enum shader_param_type type;
  267. char *name;
  268. shader_t shader;
  269. GLint param;
  270. GLint texture_id;
  271. size_t sampler_id;
  272. int array_count;
  273. struct gs_texture *texture;
  274. DARRAY(uint8_t) cur_value;
  275. DARRAY(uint8_t) def_value;
  276. bool changed;
  277. };
  278. enum attrib_type {
  279. ATTRIB_POSITION,
  280. ATTRIB_NORMAL,
  281. ATTRIB_TANGENT,
  282. ATTRIB_COLOR,
  283. ATTRIB_TEXCOORD,
  284. ATTRIB_TARGET
  285. };
  286. struct shader_attrib {
  287. GLint attrib;
  288. size_t index;
  289. enum attrib_type type;
  290. };
  291. struct gs_shader {
  292. device_t device;
  293. enum shader_type type;
  294. GLuint program;
  295. struct shader_param *viewproj;
  296. struct shader_param *world;
  297. DARRAY(struct shader_attrib) attribs;
  298. DARRAY(struct shader_param) params;
  299. DARRAY(samplerstate_t) samplers;
  300. };
  301. extern void shader_update_textures(struct gs_shader *shader);
  302. struct gs_vertex_buffer {
  303. GLuint vao;
  304. GLuint vertex_buffer;
  305. GLuint normal_buffer;
  306. GLuint tangent_buffer;
  307. GLuint color_buffer;
  308. DARRAY(GLuint) uv_buffers;
  309. DARRAY(size_t) uv_sizes;
  310. device_t device;
  311. size_t num;
  312. bool dynamic;
  313. struct vb_data *data;
  314. };
  315. extern bool vertexbuffer_load(device_t device, vertbuffer_t vb);
  316. struct gs_index_buffer {
  317. GLuint buffer;
  318. enum gs_index_type type;
  319. GLuint gl_type;
  320. device_t device;
  321. void *data;
  322. size_t num;
  323. size_t width;
  324. size_t size;
  325. bool dynamic;
  326. };
  327. struct gs_texture {
  328. device_t device;
  329. enum gs_texture_type type;
  330. enum gs_color_format format;
  331. GLenum gl_format;
  332. GLenum gl_target;
  333. GLint gl_internal_format;
  334. GLenum gl_type;
  335. GLuint texture;
  336. uint32_t levels;
  337. bool is_dynamic;
  338. bool is_render_target;
  339. bool is_dummy;
  340. bool gen_mipmaps;
  341. samplerstate_t cur_sampler;
  342. };
  343. struct gs_texture_2d {
  344. struct gs_texture base;
  345. uint32_t width;
  346. uint32_t height;
  347. bool gen_mipmaps;
  348. GLuint unpack_buffer;
  349. };
  350. struct gs_texture_cube {
  351. struct gs_texture base;
  352. uint32_t size;
  353. };
  354. struct gs_stage_surface {
  355. device_t device;
  356. enum gs_color_format format;
  357. uint32_t width;
  358. uint32_t height;
  359. uint32_t bytes_per_pixel;
  360. GLenum gl_format;
  361. GLint gl_internal_format;
  362. GLenum gl_type;
  363. GLuint pack_buffer;
  364. };
  365. struct gs_zstencil_buffer {
  366. device_t device;
  367. GLuint buffer;
  368. GLuint attachment;
  369. GLenum format;
  370. };
  371. struct gs_swap_chain {
  372. device_t device;
  373. struct gl_windowinfo *wi;
  374. struct gs_init_data info;
  375. };
  376. struct fbo_info {
  377. GLuint fbo;
  378. uint32_t width;
  379. uint32_t height;
  380. enum gs_color_format format;
  381. texture_t cur_render_target;
  382. int cur_render_side;
  383. zstencil_t cur_zstencil_buffer;
  384. };
  385. static inline void fbo_info_destroy(struct fbo_info *fbo)
  386. {
  387. if (fbo) {
  388. glDeleteFramebuffers(1, &fbo->fbo);
  389. gl_success("glDeleteFramebuffers");
  390. bfree(fbo);
  391. }
  392. }
  393. struct gs_device {
  394. struct gl_platform *plat;
  395. GLuint pipeline;
  396. enum copy_type copy_type;
  397. texture_t cur_render_target;
  398. zstencil_t cur_zstencil_buffer;
  399. int cur_render_side;
  400. texture_t cur_textures[GS_MAX_TEXTURES];
  401. samplerstate_t cur_samplers[GS_MAX_TEXTURES];
  402. vertbuffer_t cur_vertex_buffer;
  403. indexbuffer_t cur_index_buffer;
  404. shader_t cur_vertex_shader;
  405. shader_t cur_pixel_shader;
  406. swapchain_t cur_swap;
  407. enum gs_cull_mode cur_cull_mode;
  408. struct gs_rect cur_viewport;
  409. struct matrix4 cur_proj;
  410. struct matrix4 cur_view;
  411. struct matrix4 cur_viewproj;
  412. DARRAY(struct matrix4) proj_stack;
  413. DARRAY(struct fbo_info*) fbos;
  414. struct fbo_info *cur_fbo;
  415. };
  416. extern struct fbo_info *get_fbo(struct gs_device *device,
  417. uint32_t width, uint32_t height, enum gs_color_format format);
  418. extern void gl_update(device_t device);
  419. extern struct gl_platform *gl_platform_create(device_t device,
  420. struct gs_init_data *info);
  421. extern struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform);
  422. extern void gl_platform_destroy(struct gl_platform *platform);
  423. extern bool gl_platform_init_swapchain(struct gs_swap_chain *swap);
  424. extern void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap);
  425. extern struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info);
  426. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi);
  427. extern void gl_getclientsize(struct gs_swap_chain *swap,
  428. uint32_t *width,
  429. uint32_t *height);