gl-subsystem.h 13 KB

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