gl-subsystem.h 13 KB

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