graphics-internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. bool (*device_enum_adapters)(bool (*callback)(void *, const char *,
  24. uint32_t),
  25. void *);
  26. const char *(*device_preprocessor_name)(void);
  27. int (*device_create)(gs_device_t **device, uint32_t adapter);
  28. void (*device_destroy)(gs_device_t *device);
  29. void (*device_enter_context)(gs_device_t *device);
  30. void (*device_leave_context)(gs_device_t *device);
  31. void *(*device_get_device_obj)(gs_device_t *device);
  32. gs_swapchain_t *(*device_swapchain_create)(
  33. gs_device_t *device, const struct gs_init_data *data);
  34. void (*device_resize)(gs_device_t *device, uint32_t x, uint32_t y);
  35. void (*device_get_size)(const gs_device_t *device, uint32_t *x,
  36. uint32_t *y);
  37. uint32_t (*device_get_width)(const gs_device_t *device);
  38. uint32_t (*device_get_height)(const gs_device_t *device);
  39. gs_texture_t *(*device_texture_create)(
  40. gs_device_t *device, uint32_t width, uint32_t height,
  41. enum gs_color_format color_format, uint32_t levels,
  42. const uint8_t **data, uint32_t flags);
  43. gs_texture_t *(*device_cubetexture_create)(
  44. gs_device_t *device, uint32_t size,
  45. enum gs_color_format color_format, uint32_t levels,
  46. const uint8_t **data, uint32_t flags);
  47. gs_texture_t *(*device_voltexture_create)(
  48. gs_device_t *device, uint32_t width, uint32_t height,
  49. uint32_t depth, enum gs_color_format color_format,
  50. uint32_t levels, const uint8_t **data, uint32_t flags);
  51. gs_zstencil_t *(*device_zstencil_create)(
  52. gs_device_t *device, uint32_t width, uint32_t height,
  53. enum gs_zstencil_format format);
  54. gs_stagesurf_t *(*device_stagesurface_create)(
  55. gs_device_t *device, uint32_t width, uint32_t height,
  56. enum gs_color_format color_format);
  57. gs_samplerstate_t *(*device_samplerstate_create)(
  58. gs_device_t *device, const struct gs_sampler_info *info);
  59. gs_shader_t *(*device_vertexshader_create)(gs_device_t *device,
  60. const char *shader,
  61. const char *file,
  62. char **error_string);
  63. gs_shader_t *(*device_pixelshader_create)(gs_device_t *device,
  64. const char *shader,
  65. const char *file,
  66. char **error_string);
  67. gs_vertbuffer_t *(*device_vertexbuffer_create)(gs_device_t *device,
  68. struct gs_vb_data *data,
  69. uint32_t flags);
  70. gs_indexbuffer_t *(*device_indexbuffer_create)(gs_device_t *device,
  71. enum gs_index_type type,
  72. void *indices,
  73. size_t num,
  74. uint32_t flags);
  75. gs_timer_t *(*device_timer_create)(gs_device_t *device);
  76. gs_timer_range_t *(*device_timer_range_create)(gs_device_t *device);
  77. enum gs_texture_type (*device_get_texture_type)(
  78. const gs_texture_t *texture);
  79. void (*device_load_vertexbuffer)(gs_device_t *device,
  80. gs_vertbuffer_t *vertbuffer);
  81. void (*device_load_indexbuffer)(gs_device_t *device,
  82. gs_indexbuffer_t *indexbuffer);
  83. void (*device_load_texture)(gs_device_t *device, gs_texture_t *tex,
  84. int unit);
  85. void (*device_load_samplerstate)(gs_device_t *device,
  86. gs_samplerstate_t *samplerstate,
  87. int unit);
  88. void (*device_load_vertexshader)(gs_device_t *device,
  89. gs_shader_t *vertshader);
  90. void (*device_load_pixelshader)(gs_device_t *device,
  91. gs_shader_t *pixelshader);
  92. void (*device_load_default_samplerstate)(gs_device_t *device, bool b_3d,
  93. int unit);
  94. gs_shader_t *(*device_get_vertex_shader)(const gs_device_t *device);
  95. gs_shader_t *(*device_get_pixel_shader)(const gs_device_t *device);
  96. gs_texture_t *(*device_get_render_target)(const gs_device_t *device);
  97. gs_zstencil_t *(*device_get_zstencil_target)(const gs_device_t *device);
  98. void (*device_set_render_target)(gs_device_t *device, gs_texture_t *tex,
  99. gs_zstencil_t *zstencil);
  100. void (*device_set_cube_render_target)(gs_device_t *device,
  101. gs_texture_t *cubetex, int side,
  102. gs_zstencil_t *zstencil);
  103. void (*device_copy_texture)(gs_device_t *device, gs_texture_t *dst,
  104. gs_texture_t *src);
  105. void (*device_copy_texture_region)(gs_device_t *device,
  106. gs_texture_t *dst, uint32_t dst_x,
  107. uint32_t dst_y, gs_texture_t *src,
  108. uint32_t src_x, uint32_t src_y,
  109. uint32_t src_w, uint32_t src_h);
  110. void (*device_stage_texture)(gs_device_t *device, gs_stagesurf_t *dst,
  111. gs_texture_t *src);
  112. void (*device_begin_scene)(gs_device_t *device);
  113. void (*device_draw)(gs_device_t *device, enum gs_draw_mode draw_mode,
  114. uint32_t start_vert, uint32_t num_verts);
  115. void (*device_end_scene)(gs_device_t *device);
  116. void (*device_load_swapchain)(gs_device_t *device,
  117. gs_swapchain_t *swaphchain);
  118. void (*device_clear)(gs_device_t *device, uint32_t clear_flags,
  119. const struct vec4 *color, float depth,
  120. uint8_t stencil);
  121. void (*device_present)(gs_device_t *device);
  122. void (*device_flush)(gs_device_t *device);
  123. void (*device_set_cull_mode)(gs_device_t *device,
  124. enum gs_cull_mode mode);
  125. enum gs_cull_mode (*device_get_cull_mode)(const gs_device_t *device);
  126. void (*device_enable_blending)(gs_device_t *device, bool enable);
  127. void (*device_enable_depth_test)(gs_device_t *device, bool enable);
  128. void (*device_enable_stencil_test)(gs_device_t *device, bool enable);
  129. void (*device_enable_stencil_write)(gs_device_t *device, bool enable);
  130. void (*device_enable_color)(gs_device_t *device, bool red, bool green,
  131. bool blue, bool alpha);
  132. void (*device_blend_function)(gs_device_t *device,
  133. enum gs_blend_type src,
  134. enum gs_blend_type dest);
  135. void (*device_blend_function_separate)(gs_device_t *device,
  136. enum gs_blend_type src_c,
  137. enum gs_blend_type dest_c,
  138. enum gs_blend_type src_a,
  139. enum gs_blend_type dest_a);
  140. void (*device_depth_function)(gs_device_t *device,
  141. enum gs_depth_test test);
  142. void (*device_stencil_function)(gs_device_t *device,
  143. enum gs_stencil_side side,
  144. enum gs_depth_test test);
  145. void (*device_stencil_op)(gs_device_t *device,
  146. enum gs_stencil_side side,
  147. enum gs_stencil_op_type fail,
  148. enum gs_stencil_op_type zfail,
  149. enum gs_stencil_op_type zpass);
  150. void (*device_set_viewport)(gs_device_t *device, int x, int y,
  151. int width, int height);
  152. void (*device_get_viewport)(const gs_device_t *device,
  153. struct gs_rect *rect);
  154. void (*device_set_scissor_rect)(gs_device_t *device,
  155. const struct gs_rect *rect);
  156. void (*device_ortho)(gs_device_t *device, float left, float right,
  157. float top, float bottom, float znear, float zfar);
  158. void (*device_frustum)(gs_device_t *device, float left, float right,
  159. float top, float bottom, float znear,
  160. float zfar);
  161. void (*device_projection_push)(gs_device_t *device);
  162. void (*device_projection_pop)(gs_device_t *device);
  163. void (*gs_swapchain_destroy)(gs_swapchain_t *swapchain);
  164. void (*gs_texture_destroy)(gs_texture_t *tex);
  165. uint32_t (*gs_texture_get_width)(const gs_texture_t *tex);
  166. uint32_t (*gs_texture_get_height)(const gs_texture_t *tex);
  167. enum gs_color_format (*gs_texture_get_color_format)(
  168. const gs_texture_t *tex);
  169. bool (*gs_texture_map)(gs_texture_t *tex, uint8_t **ptr,
  170. uint32_t *linesize);
  171. void (*gs_texture_unmap)(gs_texture_t *tex);
  172. bool (*gs_texture_is_rect)(const gs_texture_t *tex);
  173. void *(*gs_texture_get_obj)(const gs_texture_t *tex);
  174. void (*gs_cubetexture_destroy)(gs_texture_t *cubetex);
  175. uint32_t (*gs_cubetexture_get_size)(const gs_texture_t *cubetex);
  176. enum gs_color_format (*gs_cubetexture_get_color_format)(
  177. const gs_texture_t *cubetex);
  178. void (*gs_voltexture_destroy)(gs_texture_t *voltex);
  179. uint32_t (*gs_voltexture_get_width)(const gs_texture_t *voltex);
  180. uint32_t (*gs_voltexture_get_height)(const gs_texture_t *voltex);
  181. uint32_t (*gs_voltexture_get_depth)(const gs_texture_t *voltex);
  182. enum gs_color_format (*gs_voltexture_get_color_format)(
  183. const gs_texture_t *voltex);
  184. void (*gs_stagesurface_destroy)(gs_stagesurf_t *stagesurf);
  185. uint32_t (*gs_stagesurface_get_width)(const gs_stagesurf_t *stagesurf);
  186. uint32_t (*gs_stagesurface_get_height)(const gs_stagesurf_t *stagesurf);
  187. enum gs_color_format (*gs_stagesurface_get_color_format)(
  188. const gs_stagesurf_t *stagesurf);
  189. bool (*gs_stagesurface_map)(gs_stagesurf_t *stagesurf, uint8_t **data,
  190. uint32_t *linesize);
  191. void (*gs_stagesurface_unmap)(gs_stagesurf_t *stagesurf);
  192. void (*gs_zstencil_destroy)(gs_zstencil_t *zstencil);
  193. void (*gs_samplerstate_destroy)(gs_samplerstate_t *samplerstate);
  194. void (*gs_vertexbuffer_destroy)(gs_vertbuffer_t *vertbuffer);
  195. void (*gs_vertexbuffer_flush)(gs_vertbuffer_t *vertbuffer);
  196. void (*gs_vertexbuffer_flush_direct)(gs_vertbuffer_t *vertbuffer,
  197. const struct gs_vb_data *data);
  198. struct gs_vb_data *(*gs_vertexbuffer_get_data)(
  199. const gs_vertbuffer_t *vertbuffer);
  200. void (*gs_indexbuffer_destroy)(gs_indexbuffer_t *indexbuffer);
  201. void (*gs_indexbuffer_flush)(gs_indexbuffer_t *indexbuffer);
  202. void (*gs_indexbuffer_flush_direct)(gs_indexbuffer_t *indexbuffer,
  203. const void *data);
  204. void *(*gs_indexbuffer_get_data)(const gs_indexbuffer_t *indexbuffer);
  205. size_t (*gs_indexbuffer_get_num_indices)(
  206. const gs_indexbuffer_t *indexbuffer);
  207. enum gs_index_type (*gs_indexbuffer_get_type)(
  208. const gs_indexbuffer_t *indexbuffer);
  209. void (*gs_timer_destroy)(gs_timer_t *timer);
  210. void (*gs_timer_begin)(gs_timer_t *timer);
  211. void (*gs_timer_end)(gs_timer_t *timer);
  212. bool (*gs_timer_get_data)(gs_timer_t *timer, uint64_t *ticks);
  213. void (*gs_timer_range_destroy)(gs_timer_range_t *range);
  214. bool (*gs_timer_range_begin)(gs_timer_range_t *range);
  215. bool (*gs_timer_range_end)(gs_timer_range_t *range);
  216. bool (*gs_timer_range_get_data)(gs_timer_range_t *range, bool *disjoint,
  217. uint64_t *frequency);
  218. void (*gs_shader_destroy)(gs_shader_t *shader);
  219. int (*gs_shader_get_num_params)(const gs_shader_t *shader);
  220. gs_sparam_t *(*gs_shader_get_param_by_idx)(gs_shader_t *shader,
  221. uint32_t param);
  222. gs_sparam_t *(*gs_shader_get_param_by_name)(gs_shader_t *shader,
  223. const char *name);
  224. gs_sparam_t *(*gs_shader_get_viewproj_matrix)(const gs_shader_t *shader);
  225. gs_sparam_t *(*gs_shader_get_world_matrix)(const gs_shader_t *shader);
  226. void (*gs_shader_get_param_info)(const gs_sparam_t *param,
  227. struct gs_shader_param_info *info);
  228. void (*gs_shader_set_bool)(gs_sparam_t *param, bool val);
  229. void (*gs_shader_set_float)(gs_sparam_t *param, float val);
  230. void (*gs_shader_set_int)(gs_sparam_t *param, int val);
  231. void (*gs_shader_set_matrix3)(gs_sparam_t *param,
  232. const struct matrix3 *val);
  233. void (*gs_shader_set_matrix4)(gs_sparam_t *param,
  234. const struct matrix4 *val);
  235. void (*gs_shader_set_vec2)(gs_sparam_t *param, const struct vec2 *val);
  236. void (*gs_shader_set_vec3)(gs_sparam_t *param, const struct vec3 *val);
  237. void (*gs_shader_set_vec4)(gs_sparam_t *param, const struct vec4 *val);
  238. void (*gs_shader_set_texture)(gs_sparam_t *param, gs_texture_t *val);
  239. void (*gs_shader_set_val)(gs_sparam_t *param, const void *val,
  240. size_t size);
  241. void (*gs_shader_set_default)(gs_sparam_t *param);
  242. void (*gs_shader_set_next_sampler)(gs_sparam_t *param,
  243. gs_samplerstate_t *sampler);
  244. bool (*device_nv12_available)(gs_device_t *device);
  245. void (*device_debug_marker_begin)(gs_device_t *device,
  246. const char *markername,
  247. const float color[4]);
  248. void (*device_debug_marker_end)(gs_device_t *device);
  249. #ifdef __APPLE__
  250. /* OSX/Cocoa specific functions */
  251. gs_texture_t *(*device_texture_create_from_iosurface)(gs_device_t *dev,
  252. void *iosurf);
  253. bool (*gs_texture_rebind_iosurface)(gs_texture_t *texture,
  254. void *iosurf);
  255. #elif _WIN32
  256. bool (*device_gdi_texture_available)(void);
  257. bool (*device_shared_texture_available)(void);
  258. bool (*device_get_duplicator_monitor_info)(
  259. gs_device_t *device, int monitor_idx,
  260. struct gs_monitor_info *monitor_info);
  261. gs_duplicator_t *(*device_duplicator_create)(gs_device_t *device,
  262. int monitor_idx);
  263. void (*gs_duplicator_destroy)(gs_duplicator_t *duplicator);
  264. bool (*gs_duplicator_update_frame)(gs_duplicator_t *duplicator);
  265. gs_texture_t *(*gs_duplicator_get_texture)(gs_duplicator_t *duplicator);
  266. gs_texture_t *(*device_texture_create_gdi)(gs_device_t *device,
  267. uint32_t width,
  268. uint32_t height);
  269. void *(*gs_texture_get_dc)(gs_texture_t *gdi_tex);
  270. void (*gs_texture_release_dc)(gs_texture_t *gdi_tex);
  271. gs_texture_t *(*device_texture_open_shared)(gs_device_t *device,
  272. uint32_t handle);
  273. uint32_t (*device_texture_get_shared_handle)(gs_texture_t *tex);
  274. int (*device_texture_acquire_sync)(gs_texture_t *tex, uint64_t key,
  275. uint32_t ms);
  276. int (*device_texture_release_sync)(gs_texture_t *tex, uint64_t key);
  277. bool (*device_texture_create_nv12)(gs_device_t *device,
  278. gs_texture_t **tex_y,
  279. gs_texture_t **tex_uv,
  280. uint32_t width, uint32_t height,
  281. uint32_t flags);
  282. gs_stagesurf_t *(*device_stagesurface_create_nv12)(gs_device_t *device,
  283. uint32_t width,
  284. uint32_t height);
  285. #endif
  286. };
  287. struct blend_state {
  288. bool enabled;
  289. enum gs_blend_type src_c;
  290. enum gs_blend_type dest_c;
  291. enum gs_blend_type src_a;
  292. enum gs_blend_type dest_a;
  293. };
  294. struct graphics_subsystem {
  295. void *module;
  296. gs_device_t *device;
  297. struct gs_exports exports;
  298. DARRAY(struct gs_rect) viewport_stack;
  299. DARRAY(struct matrix4) matrix_stack;
  300. size_t cur_matrix;
  301. struct matrix4 projection;
  302. struct gs_effect *cur_effect;
  303. gs_vertbuffer_t *sprite_buffer;
  304. bool using_immediate;
  305. struct gs_vb_data *vbd;
  306. gs_vertbuffer_t *immediate_vertbuffer;
  307. DARRAY(struct vec3) verts;
  308. DARRAY(struct vec3) norms;
  309. DARRAY(uint32_t) colors;
  310. DARRAY(struct vec2) texverts[16];
  311. pthread_mutex_t effect_mutex;
  312. struct gs_effect *first_effect;
  313. pthread_mutex_t mutex;
  314. volatile long ref;
  315. struct blend_state cur_blend_state;
  316. DARRAY(struct blend_state) blend_state_stack;
  317. };