gl-subsystem.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #ifndef GL_SUBSYSTEM_H
  15. #define GL_SUBSYSTEM_H
  16. #include "util/darray.h"
  17. #include "graphics/graphics.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. static inline GLint convert_gs_format(enum gs_color_format format)
  24. {
  25. switch (format) {
  26. case GS_A8: return GL_RGBA;
  27. case GS_R8: return GL_RED;
  28. case GS_RGBA: return GL_RGBA;
  29. case GS_BGRX: return GL_BGR;
  30. case GS_BGRA: return GL_BGRA;
  31. case GS_R10G10B10A2: return GL_RGBA;
  32. case GS_RGBA16: return GL_RGBA;
  33. case GS_R16: return GL_RED;
  34. case GS_RGBA16F: return GL_RGBA;
  35. case GS_RGBA32F: return GL_RGBA;
  36. case GS_RG16F: return GL_RG;
  37. case GS_RG32F: return GL_RG;
  38. case GS_R16F: return GL_RED;
  39. case GS_R32F: return GL_RED;
  40. case GS_DXT1: return GL_RGB;
  41. case GS_DXT3: return GL_RGBA;
  42. case GS_DXT5: return GL_RGBA;
  43. default: return 0;
  44. }
  45. }
  46. static inline GLint convert_gs_internal_format(enum gs_color_format format)
  47. {
  48. switch (format) {
  49. case GS_A8: return GL_R8; /* NOTE: use GL_TEXTURE_SWIZZLE_x */
  50. case GS_R8: return GL_R8;
  51. case GS_RGBA: return GL_RGBA;
  52. case GS_BGRX: return GL_RGBA;
  53. case GS_BGRA: return GL_RGBA;
  54. case GS_R10G10B10A2: return GL_RGB10_A2;
  55. case GS_RGBA16: return GL_RGBA16;
  56. case GS_R16: return GL_R16;
  57. case GS_RGBA16F: return GL_RGBA16F;
  58. case GS_RGBA32F: return GL_RGBA32F;
  59. case GS_RG16F: return GL_RG16F;
  60. case GS_RG32F: return GL_RG32F;
  61. case GS_R16F: return GL_R16F;
  62. case GS_R32F: return GL_R32F;
  63. case GS_DXT1: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  64. case GS_DXT3: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  65. case GS_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  66. default: return 0;
  67. }
  68. }
  69. static inline GLenum convert_zstencil_format(enum gs_zstencil_format format)
  70. {
  71. switch (format) {
  72. case GS_Z16: return GL_DEPTH_COMPONENT16;
  73. case GS_Z24_S8: return GL_DEPTH24_STENCIL8;
  74. case GS_Z32F: return GL_DEPTH_COMPONENT32F;
  75. case GS_Z32F_S8X24: return GL_DEPTH32F_STENCIL8;
  76. default: return 0;
  77. }
  78. }
  79. static inline GLenum convert_shader_type(enum shader_type type)
  80. {
  81. switch (type) {
  82. default:
  83. case SHADER_VERTEX: return GL_VERTEX_SHADER;
  84. case SHADER_PIXEL: return GL_FRAGMENT_SHADER;
  85. }
  86. }
  87. static inline void convert_filter(enum gs_sample_filter filter,
  88. GLint *min_filter, GLint *mag_filter)
  89. {
  90. switch (filter) {
  91. case GS_FILTER_ANISOTROPIC:
  92. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  93. *mag_filter = GL_LINEAR;
  94. break;
  95. default:
  96. case GS_FILTER_POINT:
  97. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  98. *mag_filter = GL_NEAREST;
  99. break;
  100. case GS_FILTER_LINEAR:
  101. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  102. *mag_filter = GL_LINEAR;
  103. break;
  104. case GS_FILTER_MIN_MAG_POINT_MIP_LINEAR:
  105. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  106. *mag_filter = GL_NEAREST;
  107. break;
  108. case GS_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT:
  109. *min_filter = GL_NEAREST_MIPMAP_NEAREST;
  110. *mag_filter = GL_LINEAR;
  111. break;
  112. case GS_FILTER_MIN_POINT_MAG_MIP_LINEAR:
  113. *min_filter = GL_NEAREST_MIPMAP_LINEAR;
  114. *mag_filter = GL_LINEAR;
  115. break;
  116. case GS_FILTER_MIN_LINEAR_MAG_MIP_POINT:
  117. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  118. *mag_filter = GL_NEAREST;
  119. break;
  120. case GS_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
  121. *min_filter = GL_LINEAR_MIPMAP_LINEAR;
  122. *mag_filter = GL_NEAREST;
  123. break;
  124. case GS_FILTER_MIN_MAG_LINEAR_MIP_POINT:
  125. *min_filter = GL_LINEAR_MIPMAP_NEAREST;
  126. *mag_filter = GL_LINEAR;
  127. break;
  128. }
  129. }
  130. static inline GLint convert_address_mode(enum gs_address_mode mode)
  131. {
  132. switch (mode) {
  133. default:
  134. case GS_ADDRESS_WRAP: return GL_REPEAT;
  135. case GS_ADDRESS_CLAMP: return GL_CLAMP;
  136. case GS_ADDRESS_MIRROR: return GL_MIRRORED_REPEAT;
  137. case GS_ADDRESS_BORDER: return GL_CLAMP_TO_BORDER;
  138. case GS_ADDRESS_MIRRORONCE: return GL_MIRROR_CLAMP_EXT;
  139. }
  140. }
  141. extern void convert_sampler_info(struct gs_sampler *sampler,
  142. struct gs_sampler_info *info);
  143. struct gs_sampler {
  144. GLint min_filter;
  145. GLint mag_filter;
  146. GLint address_u;
  147. GLint address_v;
  148. GLint address_w;
  149. GLint max_anisotropy;
  150. };
  151. struct shader_param {
  152. char *name;
  153. enum shader_param_type type;
  154. GLint param;
  155. GLint texture_id;
  156. size_t sampler_id;
  157. int array_count;
  158. struct gs_texture *texture;
  159. DARRAY(uint8_t) cur_value;
  160. DARRAY(uint8_t) def_value;
  161. bool changed;
  162. };
  163. struct gs_shader {
  164. device_t device;
  165. enum shader_type type;
  166. GLuint program;
  167. struct shader_param *viewproj;
  168. struct shader_param *world;
  169. DARRAY(struct gs_sampler) samplers;
  170. DARRAY(struct shader_param) params;
  171. };
  172. struct gs_texture {
  173. device_t device;
  174. enum gs_texture_type type;
  175. enum gs_color_format format;
  176. GLenum gl_format;
  177. GLint gl_internal_format;
  178. GLuint texture;
  179. uint32_t levels;
  180. bool is_dynamic;
  181. bool is_render_target;
  182. bool gen_mipmaps;
  183. };
  184. struct gs_texture_2d {
  185. struct gs_texture base;
  186. uint32_t width;
  187. uint32_t height;
  188. bool gen_mipmaps;
  189. GLuint unpack_buffer;
  190. };
  191. struct gs_texture_cube {
  192. struct gs_texture base;
  193. uint32_t size;
  194. };
  195. struct gs_zstencil_buffer {
  196. device_t device;
  197. GLuint buffer;
  198. GLenum format;
  199. };
  200. struct gs_swap_chain {
  201. device_t device;
  202. struct gl_windowinfo *wi;
  203. struct gs_init_data info;
  204. };
  205. struct gs_device {
  206. struct gl_platform *plat;
  207. struct gs_texture *cur_render_texture;
  208. int cur_render_side;
  209. struct gs_texture *cur_textures[GS_MAX_TEXTURES];
  210. struct gs_sampler *cur_samplers[GS_MAX_TEXTURES];
  211. struct gs_swap_chain *cur_swap;
  212. };
  213. extern struct gl_platform *gl_platform_create(device_t device,
  214. struct gs_init_data *info);
  215. extern struct gs_swap_chain *gl_platform_getswap(struct gl_platform *platform);
  216. extern void gl_platform_destroy(struct gl_platform *platform);
  217. extern struct gl_windowinfo *gl_windowinfo_create(struct gs_init_data *info);
  218. extern void gl_windowinfo_destroy(struct gl_windowinfo *wi);
  219. #endif