graphics-internal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/threading.h"
  16. #include "../util/darray.h"
  17. #include "graphics.h"
  18. #include "matrix3.h"
  19. #include "matrix4.h"
  20. struct gs_exports {
  21. const char *(*device_get_name)(void);
  22. int (*device_get_type)(void);
  23. const char *(*device_preprocessor_name)(void);
  24. int (*device_create)(gs_device_t **device,
  25. const struct gs_init_data *data);
  26. void (*device_destroy)(gs_device_t *device);
  27. void (*device_enter_context)(gs_device_t *device);
  28. void (*device_leave_context)(gs_device_t *device);
  29. gs_swapchain_t *(*device_swapchain_create)(gs_device_t *device,
  30. const struct gs_init_data *data);
  31. void (*device_resize)(gs_device_t *device, uint32_t x, uint32_t y);
  32. void (*device_get_size)(const gs_device_t *device,
  33. uint32_t *x, uint32_t *y);
  34. uint32_t (*device_get_width)(const gs_device_t *device);
  35. uint32_t (*device_get_height)(const gs_device_t *device);
  36. gs_texture_t *(*device_texture_create)(gs_device_t *device,
  37. uint32_t width, uint32_t height,
  38. enum gs_color_format color_format, uint32_t levels,
  39. const uint8_t **data, uint32_t flags);
  40. gs_texture_t *(*device_cubetexture_create)(gs_device_t *device,
  41. uint32_t size, enum gs_color_format color_format,
  42. uint32_t levels, const uint8_t **data, uint32_t flags);
  43. gs_texture_t *(*device_voltexture_create)(gs_device_t *device,
  44. uint32_t width, uint32_t height, uint32_t depth,
  45. enum gs_color_format color_format, uint32_t levels,
  46. const uint8_t **data, uint32_t flags);
  47. gs_zstencil_t *(*device_zstencil_create)(gs_device_t *device,
  48. uint32_t width, uint32_t height,
  49. enum gs_zstencil_format format);
  50. gs_stagesurf_t *(*device_stagesurface_create)(gs_device_t *device,
  51. uint32_t width, uint32_t height,
  52. enum gs_color_format color_format);
  53. gs_samplerstate_t *(*device_samplerstate_create)(gs_device_t *device,
  54. const struct gs_sampler_info *info);
  55. gs_shader_t *(*device_vertexshader_create)(gs_device_t *device,
  56. const char *shader, const char *file,
  57. char **error_string);
  58. gs_shader_t *(*device_pixelshader_create)(gs_device_t *device,
  59. const char *shader, const char *file,
  60. char **error_string);
  61. gs_vertbuffer_t *(*device_vertexbuffer_create)(gs_device_t *device,
  62. struct gs_vb_data *data, uint32_t flags);
  63. gs_indexbuffer_t *(*device_indexbuffer_create)(gs_device_t *device,
  64. enum gs_index_type type, void *indices, size_t num,
  65. uint32_t flags);
  66. enum gs_texture_type (*device_get_texture_type)(
  67. const gs_texture_t *texture);
  68. void (*device_load_vertexbuffer)(gs_device_t *device,
  69. gs_vertbuffer_t *vertbuffer);
  70. void (*device_load_indexbuffer)(gs_device_t *device,
  71. gs_indexbuffer_t *indexbuffer);
  72. void (*device_load_texture)(gs_device_t *device, gs_texture_t *tex,
  73. int unit);
  74. void (*device_load_samplerstate)(gs_device_t *device,
  75. gs_samplerstate_t *samplerstate, int unit);
  76. void (*device_load_vertexshader)(gs_device_t *device,
  77. gs_shader_t *vertshader);
  78. void (*device_load_pixelshader)(gs_device_t *device,
  79. gs_shader_t *pixelshader);
  80. void (*device_load_default_samplerstate)(gs_device_t *device,
  81. bool b_3d, int unit);
  82. gs_shader_t *(*device_get_vertex_shader)(const gs_device_t *device);
  83. gs_shader_t *(*device_get_pixel_shader)(const gs_device_t *device);
  84. gs_texture_t *(*device_get_render_target)(const gs_device_t *device);
  85. gs_zstencil_t *(*device_get_zstencil_target)(const gs_device_t *device);
  86. void (*device_set_render_target)(gs_device_t *device, gs_texture_t *tex,
  87. gs_zstencil_t *zstencil);
  88. void (*device_set_cube_render_target)(gs_device_t *device,
  89. gs_texture_t *cubetex, int side, gs_zstencil_t *zstencil);
  90. void (*device_copy_texture)(gs_device_t *device, gs_texture_t *dst,
  91. gs_texture_t *src);
  92. void (*device_copy_texture_region)(gs_device_t *device,
  93. gs_texture_t *dst, uint32_t dst_x, uint32_t dst_y,
  94. gs_texture_t *src, uint32_t src_x, uint32_t src_y,
  95. uint32_t src_w, uint32_t src_h);
  96. void (*device_stage_texture)(gs_device_t *device, gs_stagesurf_t *dst,
  97. gs_texture_t *src);
  98. void (*device_begin_scene)(gs_device_t *device);
  99. void (*device_draw)(gs_device_t *device, enum gs_draw_mode draw_mode,
  100. uint32_t start_vert, uint32_t num_verts);
  101. void (*device_end_scene)(gs_device_t *device);
  102. void (*device_load_swapchain)(gs_device_t *device,
  103. gs_swapchain_t *swaphchain);
  104. void (*device_clear)(gs_device_t *device, uint32_t clear_flags,
  105. const struct vec4 *color, float depth, uint8_t stencil);
  106. void (*device_present)(gs_device_t *device);
  107. void (*device_flush)(gs_device_t *device);
  108. void (*device_set_cull_mode)(gs_device_t *device,
  109. enum gs_cull_mode mode);
  110. enum gs_cull_mode (*device_get_cull_mode)(const gs_device_t *device);
  111. void (*device_enable_blending)(gs_device_t *device, bool enable);
  112. void (*device_enable_depth_test)(gs_device_t *device, bool enable);
  113. void (*device_enable_stencil_test)(gs_device_t *device, bool enable);
  114. void (*device_enable_stencil_write)(gs_device_t *device, bool enable);
  115. void (*device_enable_color)(gs_device_t *device, bool red, bool green,
  116. bool blue, bool alpha);
  117. void (*device_blend_function)(gs_device_t *device,
  118. enum gs_blend_type src, enum gs_blend_type dest);
  119. void (*device_depth_function)(gs_device_t *device,
  120. enum gs_depth_test test);
  121. void (*device_stencil_function)(gs_device_t *device,
  122. enum gs_stencil_side side, enum gs_depth_test test);
  123. void (*device_stencil_op)(gs_device_t *device,
  124. enum gs_stencil_side side,
  125. enum gs_stencil_op_type fail,
  126. enum gs_stencil_op_type zfail,
  127. enum gs_stencil_op_type zpass);
  128. void (*device_set_viewport)(gs_device_t *device, int x, int y,
  129. int width, int height);
  130. void (*device_get_viewport)(const gs_device_t *device,
  131. struct gs_rect *rect);
  132. void (*device_set_scissor_rect)(gs_device_t *device,
  133. const struct gs_rect *rect);
  134. void (*device_ortho)(gs_device_t *device, float left, float right,
  135. float top, float bottom, float znear, float zfar);
  136. void (*device_frustum)(gs_device_t *device, float left, float right,
  137. float top, float bottom, float znear, float zfar);
  138. void (*device_projection_push)(gs_device_t *device);
  139. void (*device_projection_pop)(gs_device_t *device);
  140. void (*gs_swapchain_destroy)(gs_swapchain_t *swapchain);
  141. void (*gs_texture_destroy)(gs_texture_t *tex);
  142. uint32_t (*gs_texture_get_width)(const gs_texture_t *tex);
  143. uint32_t (*gs_texture_get_height)(const gs_texture_t *tex);
  144. enum gs_color_format (*gs_texture_get_color_format)(
  145. const gs_texture_t *tex);
  146. bool (*gs_texture_map)(gs_texture_t *tex, uint8_t **ptr,
  147. uint32_t *linesize);
  148. void (*gs_texture_unmap)(gs_texture_t *tex);
  149. bool (*gs_texture_is_rect)(const gs_texture_t *tex);
  150. void *(*gs_texture_get_obj)(const gs_texture_t *tex);
  151. void (*gs_cubetexture_destroy)(gs_texture_t *cubetex);
  152. uint32_t (*gs_cubetexture_get_size)(const gs_texture_t *cubetex);
  153. enum gs_color_format (*gs_cubetexture_get_color_format)(
  154. const gs_texture_t *cubetex);
  155. void (*gs_voltexture_destroy)(gs_texture_t *voltex);
  156. uint32_t (*gs_voltexture_get_width)(const gs_texture_t *voltex);
  157. uint32_t (*gs_voltexture_get_height)(const gs_texture_t *voltex);
  158. uint32_t (*gs_voltexture_getdepth)(const gs_texture_t *voltex);
  159. enum gs_color_format (*gs_voltexture_get_color_format)(
  160. const gs_texture_t *voltex);
  161. void (*gs_stagesurface_destroy)(gs_stagesurf_t *stagesurf);
  162. uint32_t (*gs_stagesurface_get_width)(const gs_stagesurf_t *stagesurf);
  163. uint32_t (*gs_stagesurface_get_height)(const gs_stagesurf_t *stagesurf);
  164. enum gs_color_format (*gs_stagesurface_get_color_format)(
  165. const gs_stagesurf_t *stagesurf);
  166. bool (*gs_stagesurface_map)(gs_stagesurf_t *stagesurf,
  167. uint8_t **data, uint32_t *linesize);
  168. void (*gs_stagesurface_unmap)(gs_stagesurf_t *stagesurf);
  169. void (*gs_zstencil_destroy)(gs_zstencil_t *zstencil);
  170. void (*gs_samplerstate_destroy)(gs_samplerstate_t *samplerstate);
  171. void (*gs_vertexbuffer_destroy)(gs_vertbuffer_t *vertbuffer);
  172. void (*gs_vertexbuffer_flush)(gs_vertbuffer_t *vertbuffer);
  173. struct gs_vb_data *(*gs_vertexbuffer_get_data)(
  174. const gs_vertbuffer_t *vertbuffer);
  175. void (*gs_indexbuffer_destroy)(gs_indexbuffer_t *indexbuffer);
  176. void (*gs_indexbuffer_flush)(gs_indexbuffer_t *indexbuffer);
  177. void *(*gs_indexbuffer_get_data)(const gs_indexbuffer_t *indexbuffer);
  178. size_t (*gs_indexbuffer_get_num_indices)(
  179. const gs_indexbuffer_t *indexbuffer);
  180. enum gs_index_type (*gs_indexbuffer_get_type)(
  181. const gs_indexbuffer_t *indexbuffer);
  182. void (*gs_shader_destroy)(gs_shader_t *shader);
  183. int (*gs_shader_get_num_params)(const gs_shader_t *shader);
  184. gs_sparam_t *(*gs_shader_get_param_by_idx)(gs_shader_t *shader,
  185. uint32_t param);
  186. gs_sparam_t *(*gs_shader_get_param_by_name)(gs_shader_t *shader,
  187. const char *name);
  188. gs_sparam_t *(*gs_shader_get_viewproj_matrix)(
  189. const gs_shader_t *shader);
  190. gs_sparam_t *(*gs_shader_get_world_matrix)(const gs_shader_t *shader);
  191. void (*gs_shader_get_param_info)(const gs_sparam_t *param,
  192. struct gs_shader_param_info *info);
  193. void (*gs_shader_set_bool)(gs_sparam_t *param, bool val);
  194. void (*gs_shader_set_float)(gs_sparam_t *param, float val);
  195. void (*gs_shader_set_int)(gs_sparam_t *param, int val);
  196. void (*gs_shader_setmatrix3)(gs_sparam_t *param,
  197. const struct matrix3 *val);
  198. void (*gs_shader_set_matrix4)(gs_sparam_t *param,
  199. const struct matrix4 *val);
  200. void (*gs_shader_set_vec2)(gs_sparam_t *param, const struct vec2 *val);
  201. void (*gs_shader_set_vec3)(gs_sparam_t *param, const struct vec3 *val);
  202. void (*gs_shader_set_vec4)(gs_sparam_t *param, const struct vec4 *val);
  203. void (*gs_shader_set_texture)(gs_sparam_t *param, gs_texture_t *val);
  204. void (*gs_shader_set_val)(gs_sparam_t *param, const void *val,
  205. size_t size);
  206. void (*gs_shader_set_default)(gs_sparam_t *param);
  207. #ifdef __APPLE__
  208. /* OSX/Cocoa specific functions */
  209. gs_texture_t *(*device_texture_create_from_iosurface)(gs_device_t *dev,
  210. void *iosurf);
  211. bool (*gs_texture_rebind_iosurface)(gs_texture_t *texture,
  212. void *iosurf);
  213. #elif _WIN32
  214. bool (*device_gdi_texture_available)(void);
  215. bool (*device_shared_texture_available)(void);
  216. gs_texture_t *(*device_texture_create_gdi)(gs_device_t *device,
  217. uint32_t width, uint32_t height);
  218. void *(*gs_texture_get_dc)(gs_texture_t *gdi_tex);
  219. void (*gs_texture_release_dc)(gs_texture_t *gdi_tex);
  220. gs_texture_t *(*device_texture_open_shared)(gs_device_t *device,
  221. uint32_t handle);
  222. #endif
  223. };
  224. struct blend_state {
  225. bool enabled;
  226. enum gs_blend_type src;
  227. enum gs_blend_type dest;
  228. };
  229. struct graphics_subsystem {
  230. void *module;
  231. gs_device_t *device;
  232. struct gs_exports exports;
  233. DARRAY(struct gs_rect) viewport_stack;
  234. DARRAY(struct matrix4) matrix_stack;
  235. size_t cur_matrix;
  236. struct matrix4 projection;
  237. struct gs_effect *cur_effect;
  238. gs_vertbuffer_t *sprite_buffer;
  239. bool using_immediate;
  240. struct gs_vb_data *vbd;
  241. gs_vertbuffer_t *immediate_vertbuffer;
  242. DARRAY(struct vec3) verts;
  243. DARRAY(struct vec3) norms;
  244. DARRAY(uint32_t) colors;
  245. DARRAY(struct vec2) texverts[16];
  246. pthread_mutex_t mutex;
  247. volatile long ref;
  248. struct blend_state cur_blend_state;
  249. };