1
0

graphics.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  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. #include <assert.h>
  15. #include "../util/base.h"
  16. #include "../util/bmem.h"
  17. #include "../util/platform.h"
  18. #include "graphics-internal.h"
  19. #include "vec2.h"
  20. #include "vec3.h"
  21. #include "quat.h"
  22. #include "axisang.h"
  23. #include "effect-parser.h"
  24. #include "effect.h"
  25. #ifdef _MSC_VER
  26. static __declspec(thread) graphics_t *thread_graphics = NULL;
  27. #else /* assume GCC or that other compiler we dare not mention */
  28. static __thread graphics_t *thread_graphics = NULL;
  29. #endif
  30. #define IMMEDIATE_COUNT 512
  31. extern void gs_init_image_deps(void);
  32. extern void gs_free_image_deps(void);
  33. bool load_graphics_imports(struct gs_exports *exports, void *module,
  34. const char *module_name);
  35. static bool graphics_init_immediate_vb(struct graphics_subsystem *graphics)
  36. {
  37. struct gs_vb_data *vbd;
  38. vbd = gs_vbdata_create();
  39. vbd->num = IMMEDIATE_COUNT;
  40. vbd->points = bmalloc(sizeof(struct vec3)*IMMEDIATE_COUNT);
  41. vbd->normals = bmalloc(sizeof(struct vec3)*IMMEDIATE_COUNT);
  42. vbd->colors = bmalloc(sizeof(uint32_t) *IMMEDIATE_COUNT);
  43. vbd->num_tex = 1;
  44. vbd->tvarray = bmalloc(sizeof(struct gs_tvertarray));
  45. vbd->tvarray[0].width = 2;
  46. vbd->tvarray[0].array =
  47. bmalloc(sizeof(struct vec2) * IMMEDIATE_COUNT);
  48. graphics->immediate_vertbuffer = graphics->exports.
  49. device_vertexbuffer_create(graphics->device, vbd, GS_DYNAMIC);
  50. if (!graphics->immediate_vertbuffer)
  51. return false;
  52. return true;
  53. }
  54. static bool graphics_init_sprite_vb(struct graphics_subsystem *graphics)
  55. {
  56. struct gs_vb_data *vbd;
  57. vbd = gs_vbdata_create();
  58. vbd->num = 4;
  59. vbd->points = bmalloc(sizeof(struct vec3) * 4);
  60. vbd->num_tex = 1;
  61. vbd->tvarray = bmalloc(sizeof(struct gs_tvertarray));
  62. vbd->tvarray[0].width = 2;
  63. vbd->tvarray[0].array = bmalloc(sizeof(struct vec2) * 4);
  64. memset(vbd->points, 0, sizeof(struct vec3) * 4);
  65. memset(vbd->tvarray[0].array, 0, sizeof(struct vec2) * 4);
  66. graphics->sprite_buffer = graphics->exports.
  67. device_vertexbuffer_create(graphics->device, vbd, GS_DYNAMIC);
  68. if (!graphics->sprite_buffer)
  69. return false;
  70. return true;
  71. }
  72. static bool graphics_init(struct graphics_subsystem *graphics)
  73. {
  74. struct matrix4 top_mat;
  75. matrix4_identity(&top_mat);
  76. da_push_back(graphics->matrix_stack, &top_mat);
  77. graphics->exports.device_enter_context(graphics->device);
  78. if (!graphics_init_immediate_vb(graphics))
  79. return false;
  80. if (!graphics_init_sprite_vb(graphics))
  81. return false;
  82. if (pthread_mutex_init(&graphics->mutex, NULL) != 0)
  83. return false;
  84. graphics->exports.device_blend_function(graphics->device,
  85. GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  86. graphics->cur_blend_state.enabled = true;
  87. graphics->cur_blend_state.src = GS_BLEND_SRCALPHA;
  88. graphics->cur_blend_state.dest = GS_BLEND_INVSRCALPHA;
  89. graphics->exports.device_leave_context(graphics->device);
  90. gs_init_image_deps();
  91. return true;
  92. }
  93. int gs_create(graphics_t **pgraphics, const char *module,
  94. const struct gs_init_data *data)
  95. {
  96. struct gs_init_data new_data = *data;
  97. int errcode = GS_ERROR_FAIL;
  98. graphics_t *graphics = bzalloc(sizeof(struct graphics_subsystem));
  99. pthread_mutex_init_value(&graphics->mutex);
  100. if (!new_data.num_backbuffers)
  101. new_data.num_backbuffers = 1;
  102. graphics->module = os_dlopen(module);
  103. if (!graphics->module) {
  104. errcode = GS_ERROR_MODULE_NOT_FOUND;
  105. goto error;
  106. }
  107. if (!load_graphics_imports(&graphics->exports, graphics->module,
  108. module))
  109. goto error;
  110. errcode = graphics->exports.device_create(&graphics->device, &new_data);
  111. if (errcode != GS_SUCCESS)
  112. goto error;
  113. if (!graphics_init(graphics)) {
  114. errcode = GS_ERROR_FAIL;
  115. goto error;
  116. }
  117. *pgraphics = graphics;
  118. return errcode;
  119. error:
  120. gs_destroy(graphics);
  121. return errcode;
  122. }
  123. void gs_destroy(graphics_t *graphics)
  124. {
  125. if (!graphics)
  126. return;
  127. while (thread_graphics)
  128. gs_leave_context();
  129. if (graphics->device) {
  130. graphics->exports.device_enter_context(graphics->device);
  131. graphics->exports.gs_vertexbuffer_destroy(
  132. graphics->sprite_buffer);
  133. graphics->exports.gs_vertexbuffer_destroy(
  134. graphics->immediate_vertbuffer);
  135. graphics->exports.device_destroy(graphics->device);
  136. }
  137. pthread_mutex_destroy(&graphics->mutex);
  138. da_free(graphics->matrix_stack);
  139. da_free(graphics->viewport_stack);
  140. if (graphics->module)
  141. os_dlclose(graphics->module);
  142. bfree(graphics);
  143. gs_free_image_deps();
  144. }
  145. void gs_enter_context(graphics_t *graphics)
  146. {
  147. if (!graphics) return;
  148. bool is_current = thread_graphics == graphics;
  149. if (thread_graphics && !is_current) {
  150. while (thread_graphics)
  151. gs_leave_context();
  152. }
  153. if (!is_current) {
  154. pthread_mutex_lock(&graphics->mutex);
  155. graphics->exports.device_enter_context(graphics->device);
  156. thread_graphics = graphics;
  157. }
  158. os_atomic_inc_long(&graphics->ref);
  159. }
  160. void gs_leave_context(void)
  161. {
  162. if (thread_graphics) {
  163. if (!os_atomic_dec_long(&thread_graphics->ref)) {
  164. graphics_t *graphics = thread_graphics;
  165. graphics->exports.device_leave_context(
  166. graphics->device);
  167. pthread_mutex_unlock(&graphics->mutex);
  168. thread_graphics = NULL;
  169. }
  170. }
  171. }
  172. graphics_t *gs_get_context(void)
  173. {
  174. return thread_graphics;
  175. }
  176. const char *gs_get_device_name(void)
  177. {
  178. return thread_graphics ?
  179. thread_graphics->exports.device_get_name() : NULL;
  180. }
  181. int gs_get_device_type(void)
  182. {
  183. return thread_graphics ?
  184. thread_graphics->exports.device_get_type() : -1;
  185. }
  186. static inline struct matrix4 *top_matrix(graphics_t *graphics)
  187. {
  188. return graphics ?
  189. (graphics->matrix_stack.array + graphics->cur_matrix) : NULL;
  190. }
  191. void gs_matrix_push(void)
  192. {
  193. graphics_t *graphics = thread_graphics;
  194. if (!graphics)
  195. return;
  196. struct matrix4 mat, *top_mat = top_matrix(graphics);
  197. memcpy(&mat, top_mat, sizeof(struct matrix4));
  198. da_push_back(graphics->matrix_stack, &mat);
  199. graphics->cur_matrix++;
  200. }
  201. void gs_matrix_pop(void)
  202. {
  203. graphics_t *graphics = thread_graphics;
  204. if (!graphics)
  205. return;
  206. if (graphics->cur_matrix == 0) {
  207. blog(LOG_ERROR, "Tried to pop last matrix on stack");
  208. return;
  209. }
  210. da_erase(graphics->matrix_stack, graphics->cur_matrix);
  211. graphics->cur_matrix--;
  212. }
  213. void gs_matrix_identity(void)
  214. {
  215. struct matrix4 *top_mat = top_matrix(thread_graphics);
  216. if (top_mat)
  217. matrix4_identity(top_mat);
  218. }
  219. void gs_matrix_transpose(void)
  220. {
  221. struct matrix4 *top_mat = top_matrix(thread_graphics);
  222. if (top_mat)
  223. matrix4_transpose(top_mat, top_mat);
  224. }
  225. void gs_matrix_set(const struct matrix4 *matrix)
  226. {
  227. struct matrix4 *top_mat = top_matrix(thread_graphics);
  228. if (top_mat)
  229. matrix4_copy(top_mat, matrix);
  230. }
  231. void gs_matrix_get(struct matrix4 *dst)
  232. {
  233. struct matrix4 *top_mat = top_matrix(thread_graphics);
  234. if (top_mat)
  235. matrix4_copy(dst, top_mat);
  236. }
  237. void gs_matrix_mul(const struct matrix4 *matrix)
  238. {
  239. struct matrix4 *top_mat = top_matrix(thread_graphics);
  240. if (top_mat)
  241. matrix4_mul(top_mat, matrix, top_mat);
  242. }
  243. void gs_matrix_rotquat(const struct quat *rot)
  244. {
  245. struct matrix4 *top_mat = top_matrix(thread_graphics);
  246. if (top_mat)
  247. matrix4_rotate_i(top_mat, rot, top_mat);
  248. }
  249. void gs_matrix_rotaa(const struct axisang *rot)
  250. {
  251. struct matrix4 *top_mat = top_matrix(thread_graphics);
  252. if (top_mat)
  253. matrix4_rotate_aa_i(top_mat, rot, top_mat);
  254. }
  255. void gs_matrix_translate(const struct vec3 *pos)
  256. {
  257. struct matrix4 *top_mat = top_matrix(thread_graphics);
  258. if (top_mat)
  259. matrix4_translate3v_i(top_mat, pos, top_mat);
  260. }
  261. void gs_matrix_scale(const struct vec3 *scale)
  262. {
  263. struct matrix4 *top_mat = top_matrix(thread_graphics);
  264. if (top_mat)
  265. matrix4_scale_i(top_mat, scale, top_mat);
  266. }
  267. void gs_matrix_rotaa4f(float x, float y, float z, float angle)
  268. {
  269. struct matrix4 *top_mat = top_matrix(thread_graphics);
  270. struct axisang aa;
  271. if (top_mat) {
  272. axisang_set(&aa, x, y, z, angle);
  273. matrix4_rotate_aa_i(top_mat, &aa, top_mat);
  274. }
  275. }
  276. void gs_matrix_translate3f(float x, float y, float z)
  277. {
  278. struct matrix4 *top_mat = top_matrix(thread_graphics);
  279. struct vec3 p;
  280. if (top_mat) {
  281. vec3_set(&p, x, y, z);
  282. matrix4_translate3v_i(top_mat, &p, top_mat);
  283. }
  284. }
  285. void gs_matrix_scale3f(float x, float y, float z)
  286. {
  287. struct matrix4 *top_mat = top_matrix(thread_graphics);
  288. struct vec3 p;
  289. if (top_mat) {
  290. vec3_set(&p, x, y, z);
  291. matrix4_scale_i(top_mat, &p, top_mat);
  292. }
  293. }
  294. static inline void reset_immediate_arrays(graphics_t *graphics)
  295. {
  296. da_init(graphics->verts);
  297. da_init(graphics->norms);
  298. da_init(graphics->colors);
  299. for (size_t i = 0; i < 16; i++)
  300. da_init(graphics->texverts[i]);
  301. }
  302. void gs_render_start(bool b_new)
  303. {
  304. graphics_t *graphics = thread_graphics;
  305. if (!graphics)
  306. return;
  307. graphics->using_immediate = !b_new;
  308. reset_immediate_arrays(graphics);
  309. if (b_new) {
  310. graphics->vbd = gs_vbdata_create();
  311. } else {
  312. graphics->vbd = gs_vertexbuffer_get_data(
  313. graphics->immediate_vertbuffer);
  314. memset(graphics->vbd->colors, 0xFF,
  315. sizeof(uint32_t) * IMMEDIATE_COUNT);
  316. graphics->verts.array = graphics->vbd->points;
  317. graphics->norms.array = graphics->vbd->normals;
  318. graphics->colors.array = graphics->vbd->colors;
  319. graphics->texverts[0].array = graphics->vbd->tvarray[0].array;
  320. graphics->verts.capacity = IMMEDIATE_COUNT;
  321. graphics->norms.capacity = IMMEDIATE_COUNT;
  322. graphics->colors.capacity = IMMEDIATE_COUNT;
  323. graphics->texverts[0].capacity = IMMEDIATE_COUNT;
  324. }
  325. }
  326. static inline size_t min_size(const size_t a, const size_t b)
  327. {
  328. return (a < b) ? a : b;
  329. }
  330. void gs_render_stop(enum gs_draw_mode mode)
  331. {
  332. graphics_t *graphics = thread_graphics;
  333. size_t i, num;
  334. if (!graphics)
  335. return;
  336. num = graphics->verts.num;
  337. if (!num) {
  338. if (!graphics->using_immediate) {
  339. da_free(graphics->verts);
  340. da_free(graphics->norms);
  341. da_free(graphics->colors);
  342. for (i = 0; i < 16; i++)
  343. da_free(graphics->texverts[i]);
  344. gs_vbdata_destroy(graphics->vbd);
  345. }
  346. return;
  347. }
  348. if (graphics->norms.num &&
  349. (graphics->norms.num != graphics->verts.num)) {
  350. blog(LOG_ERROR, "gs_render_stop: normal count does "
  351. "not match vertex count");
  352. num = min_size(num, graphics->norms.num);
  353. }
  354. if (graphics->colors.num &&
  355. (graphics->colors.num != graphics->verts.num)) {
  356. blog(LOG_ERROR, "gs_render_stop: color count does "
  357. "not match vertex count");
  358. num = min_size(num, graphics->colors.num);
  359. }
  360. if (graphics->texverts[0].num &&
  361. (graphics->texverts[0].num != graphics->verts.num)) {
  362. blog(LOG_ERROR, "gs_render_stop: texture vertex count does "
  363. "not match vertex count");
  364. num = min_size(num, graphics->texverts[0].num);
  365. }
  366. if (graphics->using_immediate) {
  367. gs_vertexbuffer_flush(graphics->immediate_vertbuffer);
  368. gs_load_vertexbuffer(graphics->immediate_vertbuffer);
  369. gs_load_indexbuffer(NULL);
  370. gs_draw(mode, 0, (uint32_t)num);
  371. reset_immediate_arrays(graphics);
  372. } else {
  373. gs_vertbuffer_t *vb = gs_render_save();
  374. gs_load_vertexbuffer(vb);
  375. gs_load_indexbuffer(NULL);
  376. gs_draw(mode, 0, 0);
  377. gs_vertexbuffer_destroy(vb);
  378. }
  379. graphics->vbd = NULL;
  380. }
  381. gs_vertbuffer_t *gs_render_save(void)
  382. {
  383. graphics_t *graphics = thread_graphics;
  384. size_t num_tex, i;
  385. if (!graphics)
  386. return NULL;
  387. if (graphics->using_immediate)
  388. return NULL;
  389. if (!graphics->verts.num) {
  390. gs_vbdata_destroy(graphics->vbd);
  391. return NULL;
  392. }
  393. for (num_tex = 0; num_tex < 16; num_tex++)
  394. if (!graphics->texverts[num_tex].num)
  395. break;
  396. graphics->vbd->points = graphics->verts.array;
  397. graphics->vbd->normals = graphics->norms.array;
  398. graphics->vbd->colors = graphics->colors.array;
  399. graphics->vbd->num = graphics->verts.num;
  400. graphics->vbd->num_tex = num_tex;
  401. if (graphics->vbd->num_tex) {
  402. graphics->vbd->tvarray =
  403. bmalloc(sizeof(struct gs_tvertarray) * num_tex);
  404. for (i = 0; i < num_tex; i++) {
  405. graphics->vbd->tvarray[i].width = 2;
  406. graphics->vbd->tvarray[i].array =
  407. graphics->texverts[i].array;
  408. }
  409. }
  410. reset_immediate_arrays(graphics);
  411. return gs_vertexbuffer_create(graphics->vbd, 0);
  412. }
  413. void gs_vertex2f(float x, float y)
  414. {
  415. struct vec3 v3;
  416. vec3_set(&v3, x, y, 0.0f);
  417. gs_vertex3v(&v3);
  418. }
  419. void gs_vertex3f(float x, float y, float z)
  420. {
  421. struct vec3 v3;
  422. vec3_set(&v3, x, y, z);
  423. gs_vertex3v(&v3);
  424. }
  425. void gs_normal3f(float x, float y, float z)
  426. {
  427. struct vec3 v3;
  428. vec3_set(&v3, x, y, z);
  429. gs_normal3v(&v3);
  430. }
  431. static inline bool validvertsize(graphics_t *graphics, size_t num,
  432. const char *name)
  433. {
  434. if (graphics->using_immediate && num == IMMEDIATE_COUNT) {
  435. blog(LOG_ERROR, "%s: tried to use over %u "
  436. "for immediate rendering",
  437. name, IMMEDIATE_COUNT);
  438. return false;
  439. }
  440. return true;
  441. }
  442. void gs_color(uint32_t color)
  443. {
  444. graphics_t *graphics = thread_graphics;
  445. if (!graphics)
  446. return;
  447. if (!validvertsize(graphics, graphics->colors.num, "gs_color"))
  448. return;
  449. da_push_back(graphics->colors, &color);
  450. }
  451. void gs_texcoord(float x, float y, int unit)
  452. {
  453. struct vec2 v2;
  454. vec2_set(&v2, x, y);
  455. gs_texcoord2v(&v2, unit);
  456. }
  457. void gs_vertex2v(const struct vec2 *v)
  458. {
  459. struct vec3 v3;
  460. vec3_set(&v3, v->x, v->y, 0.0f);
  461. gs_vertex3v(&v3);
  462. }
  463. void gs_vertex3v(const struct vec3 *v)
  464. {
  465. graphics_t *graphics = thread_graphics;
  466. if (!graphics)
  467. return;
  468. if (!validvertsize(graphics, graphics->verts.num, "gs_vertex"))
  469. return;
  470. da_push_back(graphics->verts, v);
  471. }
  472. void gs_normal3v(const struct vec3 *v)
  473. {
  474. graphics_t *graphics = thread_graphics;
  475. if (!graphics)
  476. return;
  477. if (!validvertsize(graphics, graphics->norms.num, "gs_normal"))
  478. return;
  479. da_push_back(graphics->norms, v);
  480. }
  481. void gs_color4v(const struct vec4 *v)
  482. {
  483. /* TODO */
  484. UNUSED_PARAMETER(v);
  485. }
  486. void gs_texcoord2v(const struct vec2 *v, int unit)
  487. {
  488. graphics_t *graphics = thread_graphics;
  489. if (!graphics)
  490. return;
  491. if (!validvertsize(graphics, graphics->texverts[unit].num,
  492. "gs_texcoord"))
  493. return;
  494. da_push_back(graphics->texverts[unit], v);
  495. }
  496. input_t *gs_get_input(void)
  497. {
  498. /* TODO */
  499. return NULL;
  500. }
  501. gs_effect_t *gs_get_effect(void)
  502. {
  503. return thread_graphics ? thread_graphics->cur_effect : NULL;
  504. }
  505. gs_effect_t *gs_effect_create_from_file(const char *file, char **error_string)
  506. {
  507. char *file_string;
  508. gs_effect_t *effect = NULL;
  509. if (!thread_graphics || !file)
  510. return NULL;
  511. file_string = os_quick_read_utf8_file(file);
  512. if (!file_string) {
  513. blog(LOG_ERROR, "Could not load effect file '%s'", file);
  514. return NULL;
  515. }
  516. effect = gs_effect_create(file_string, file, error_string);
  517. bfree(file_string);
  518. return effect;
  519. }
  520. gs_effect_t *gs_effect_create(const char *effect_string, const char *filename,
  521. char **error_string)
  522. {
  523. if (!thread_graphics || !effect_string)
  524. return NULL;
  525. struct gs_effect *effect = bzalloc(sizeof(struct gs_effect));
  526. struct effect_parser parser;
  527. bool success;
  528. effect->graphics = thread_graphics;
  529. ep_init(&parser);
  530. success = ep_parse(&parser, effect, effect_string, filename);
  531. if (!success) {
  532. if (error_string)
  533. *error_string = error_data_buildstring(
  534. &parser.cfp.error_list);
  535. gs_effect_destroy(effect);
  536. effect = NULL;
  537. }
  538. ep_free(&parser);
  539. return effect;
  540. }
  541. gs_shader_t *gs_vertexshader_create_from_file(const char *file,
  542. char **error_string)
  543. {
  544. if (!thread_graphics || !file)
  545. return NULL;
  546. char *file_string;
  547. gs_shader_t *shader = NULL;
  548. file_string = os_quick_read_utf8_file(file);
  549. if (!file_string) {
  550. blog(LOG_ERROR, "Could not load vertex shader file '%s'",
  551. file);
  552. return NULL;
  553. }
  554. shader = gs_vertexshader_create(file_string, file, error_string);
  555. bfree(file_string);
  556. return shader;
  557. }
  558. gs_shader_t *gs_pixelshader_create_from_file(const char *file,
  559. char **error_string)
  560. {
  561. char *file_string;
  562. gs_shader_t *shader = NULL;
  563. if (!thread_graphics || !file)
  564. return NULL;
  565. file_string = os_quick_read_utf8_file(file);
  566. if (!file_string) {
  567. blog(LOG_ERROR, "Could not load pixel shader file '%s'",
  568. file);
  569. return NULL;
  570. }
  571. shader = gs_pixelshader_create(file_string, file, error_string);
  572. bfree(file_string);
  573. return shader;
  574. }
  575. static inline void assign_sprite_rect(float *start, float *end, float size,
  576. bool flip)
  577. {
  578. if (!flip) {
  579. *start = 0.0f;
  580. *end = size;
  581. } else {
  582. *start = size;
  583. *end = 0.0f;
  584. }
  585. }
  586. static inline void assign_sprite_uv(float *start, float *end, bool flip)
  587. {
  588. if (!flip) {
  589. *start = 0.0f;
  590. *end = 1.0f;
  591. } else {
  592. *start = 1.0f;
  593. *end = 0.0f;
  594. }
  595. }
  596. static void build_sprite(struct gs_vb_data *data, float fcx, float fcy,
  597. float start_u, float end_u, float start_v, float end_v)
  598. {
  599. struct vec2 *tvarray = data->tvarray[0].array;
  600. vec3_zero(data->points);
  601. vec3_set(data->points+1, fcx, 0.0f, 0.0f);
  602. vec3_set(data->points+2, 0.0f, fcy, 0.0f);
  603. vec3_set(data->points+3, fcx, fcy, 0.0f);
  604. vec2_set(tvarray, start_u, start_v);
  605. vec2_set(tvarray+1, end_u, start_v);
  606. vec2_set(tvarray+2, start_u, end_v);
  607. vec2_set(tvarray+3, end_u, end_v);
  608. }
  609. static inline void build_sprite_norm(struct gs_vb_data *data, float fcx,
  610. float fcy, uint32_t flip)
  611. {
  612. float start_u, end_u;
  613. float start_v, end_v;
  614. assign_sprite_uv(&start_u, &end_u, (flip & GS_FLIP_U) != 0);
  615. assign_sprite_uv(&start_v, &end_v, (flip & GS_FLIP_V) != 0);
  616. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
  617. }
  618. static inline void build_sprite_rect(struct gs_vb_data *data, gs_texture_t *tex,
  619. float fcx, float fcy, uint32_t flip)
  620. {
  621. float start_u, end_u;
  622. float start_v, end_v;
  623. float width = (float)gs_texture_get_width(tex);
  624. float height = (float)gs_texture_get_height(tex);
  625. assign_sprite_rect(&start_u, &end_u, width, (flip & GS_FLIP_U) != 0);
  626. assign_sprite_rect(&start_v, &end_v, height, (flip & GS_FLIP_V) != 0);
  627. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
  628. }
  629. void gs_draw_sprite(gs_texture_t *tex, uint32_t flip, uint32_t width,
  630. uint32_t height)
  631. {
  632. graphics_t *graphics = thread_graphics;
  633. float fcx, fcy;
  634. struct gs_vb_data *data;
  635. assert(tex);
  636. if (!tex || !thread_graphics)
  637. return;
  638. if (gs_get_texture_type(tex) != GS_TEXTURE_2D) {
  639. blog(LOG_ERROR, "A sprite must be a 2D texture");
  640. return;
  641. }
  642. fcx = width ? (float)width : (float)gs_texture_get_width(tex);
  643. fcy = height ? (float)height : (float)gs_texture_get_height(tex);
  644. data = gs_vertexbuffer_get_data(graphics->sprite_buffer);
  645. if (gs_texture_is_rect(tex))
  646. build_sprite_rect(data, tex, fcx, fcy, flip);
  647. else
  648. build_sprite_norm(data, fcx, fcy, flip);
  649. gs_vertexbuffer_flush(graphics->sprite_buffer);
  650. gs_load_vertexbuffer(graphics->sprite_buffer);
  651. gs_load_indexbuffer(NULL);
  652. gs_draw(GS_TRISTRIP, 0, 0);
  653. }
  654. void gs_draw_cube_backdrop(gs_texture_t *cubetex, const struct quat *rot,
  655. float left, float right, float top, float bottom, float znear)
  656. {
  657. /* TODO */
  658. UNUSED_PARAMETER(cubetex);
  659. UNUSED_PARAMETER(rot);
  660. UNUSED_PARAMETER(left);
  661. UNUSED_PARAMETER(right);
  662. UNUSED_PARAMETER(top);
  663. UNUSED_PARAMETER(bottom);
  664. UNUSED_PARAMETER(znear);
  665. }
  666. void gs_reset_viewport(void)
  667. {
  668. uint32_t cx, cy;
  669. assert(thread_graphics != NULL);
  670. gs_get_size(&cx, &cy);
  671. gs_set_viewport(0, 0, (int)cx, (int)cy);
  672. }
  673. void gs_set_2d_mode(void)
  674. {
  675. uint32_t cx, cy;
  676. assert(thread_graphics != NULL);
  677. gs_get_size(&cx, &cy);
  678. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -1.0, -1024.0f);
  679. }
  680. void gs_set_3d_mode(double fovy, double znear, double zvar)
  681. {
  682. /* TODO */
  683. UNUSED_PARAMETER(fovy);
  684. UNUSED_PARAMETER(znear);
  685. UNUSED_PARAMETER(zvar);
  686. }
  687. void gs_viewport_push(void)
  688. {
  689. if (!thread_graphics) return;
  690. struct gs_rect *rect = da_push_back_new(
  691. thread_graphics->viewport_stack);
  692. gs_get_viewport(rect);
  693. }
  694. void gs_viewport_pop(void)
  695. {
  696. struct gs_rect *rect;
  697. if (!thread_graphics || !thread_graphics->viewport_stack.num)
  698. return;
  699. rect = da_end(thread_graphics->viewport_stack);
  700. gs_set_viewport(rect->x, rect->y, rect->cx, rect->cy);
  701. da_pop_back(thread_graphics->viewport_stack);
  702. }
  703. void gs_texture_set_image(gs_texture_t *tex, const uint8_t *data,
  704. uint32_t linesize, bool flip)
  705. {
  706. uint8_t *ptr;
  707. uint32_t linesize_out;
  708. uint32_t row_copy;
  709. int32_t height;
  710. int32_t y;
  711. if (!thread_graphics || !tex)
  712. return;
  713. height = (int32_t)gs_texture_get_height(tex);
  714. if (!gs_texture_map(tex, &ptr, &linesize_out))
  715. return;
  716. row_copy = (linesize < linesize_out) ? linesize : linesize_out;
  717. if (flip) {
  718. for (y = height-1; y >= 0; y--)
  719. memcpy(ptr + (uint32_t)y * linesize_out,
  720. data + (uint32_t)y * linesize,
  721. row_copy);
  722. } else if (linesize == linesize_out) {
  723. memcpy(ptr, data, row_copy * height);
  724. } else {
  725. for (y = 0; y < height; y++)
  726. memcpy(ptr + (uint32_t)y * linesize_out,
  727. data + (uint32_t)y * linesize,
  728. row_copy);
  729. }
  730. gs_texture_unmap(tex);
  731. }
  732. void gs_cubetexture_set_image(gs_texture_t *cubetex, uint32_t side,
  733. const void *data, uint32_t linesize, bool invert)
  734. {
  735. /* TODO */
  736. UNUSED_PARAMETER(cubetex);
  737. UNUSED_PARAMETER(side);
  738. UNUSED_PARAMETER(data);
  739. UNUSED_PARAMETER(linesize);
  740. UNUSED_PARAMETER(invert);
  741. }
  742. void gs_perspective(float angle, float aspect, float near, float far)
  743. {
  744. graphics_t *graphics = thread_graphics;
  745. float xmin, xmax, ymin, ymax;
  746. if (!graphics) return;
  747. ymax = near * tanf(RAD(angle)*0.5f);
  748. ymin = -ymax;
  749. xmin = ymin * aspect;
  750. xmax = ymax * aspect;
  751. graphics->exports.device_frustum(graphics->device, xmin, xmax,
  752. ymin, ymax, near, far);
  753. }
  754. void gs_reset_blend_state(void)
  755. {
  756. graphics_t *graphics = thread_graphics;
  757. if (!graphics) return;
  758. if (!graphics->cur_blend_state.enabled)
  759. gs_enable_blending(true);
  760. if (graphics->cur_blend_state.src != GS_BLEND_SRCALPHA ||
  761. graphics->cur_blend_state.dest != GS_BLEND_INVSRCALPHA)
  762. gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  763. }
  764. /* ------------------------------------------------------------------------- */
  765. const char *gs_preprocessor_name(void)
  766. {
  767. graphics_t *graphics = thread_graphics;
  768. if (!graphics) return NULL;
  769. return graphics->exports.device_preprocessor_name();
  770. }
  771. gs_swapchain_t *gs_swapchain_create(const struct gs_init_data *data)
  772. {
  773. struct gs_init_data new_data = *data;
  774. graphics_t *graphics = thread_graphics;
  775. if (!graphics) return NULL;
  776. if (new_data.num_backbuffers == 0)
  777. new_data.num_backbuffers = 1;
  778. return graphics->exports.device_swapchain_create(graphics->device,
  779. &new_data);
  780. }
  781. void gs_resize(uint32_t x, uint32_t y)
  782. {
  783. graphics_t *graphics = thread_graphics;
  784. if (!graphics) return;
  785. graphics->exports.device_resize(graphics->device, x, y);
  786. }
  787. void gs_get_size(uint32_t *x, uint32_t *y)
  788. {
  789. graphics_t *graphics = thread_graphics;
  790. if (!graphics) return;
  791. graphics->exports.device_get_size(graphics->device, x, y);
  792. }
  793. uint32_t gs_get_width(void)
  794. {
  795. graphics_t *graphics = thread_graphics;
  796. if (!graphics) return 0;
  797. return graphics->exports.device_get_width(graphics->device);
  798. }
  799. uint32_t gs_get_height(void)
  800. {
  801. graphics_t *graphics = thread_graphics;
  802. if (!graphics) return 0;
  803. return graphics->exports.device_get_height(graphics->device);
  804. }
  805. static inline bool is_pow2(uint32_t size)
  806. {
  807. return size >= 2 && (size & (size-1)) == 0;
  808. }
  809. gs_texture_t *gs_texture_create(uint32_t width, uint32_t height,
  810. enum gs_color_format color_format, uint32_t levels,
  811. const uint8_t **data, uint32_t flags)
  812. {
  813. graphics_t *graphics = thread_graphics;
  814. bool pow2tex = is_pow2(width) && is_pow2(height);
  815. bool uses_mipmaps = (flags & GS_BUILD_MIPMAPS || levels != 1);
  816. if (!graphics)
  817. return NULL;
  818. if (uses_mipmaps && !pow2tex) {
  819. blog(LOG_WARNING, "Cannot use mipmaps with a "
  820. "non-power-of-two texture. Disabling "
  821. "mipmaps for this texture.");
  822. uses_mipmaps = false;
  823. flags &= ~GS_BUILD_MIPMAPS;
  824. levels = 1;
  825. }
  826. if (uses_mipmaps && flags & GS_RENDER_TARGET) {
  827. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  828. "Disabling mipmaps for this texture.");
  829. flags &= ~GS_BUILD_MIPMAPS;
  830. levels = 1;
  831. }
  832. return graphics->exports.device_texture_create(graphics->device,
  833. width, height, color_format, levels, data, flags);
  834. }
  835. gs_texture_t *gs_cubetexture_create(uint32_t size,
  836. enum gs_color_format color_format, uint32_t levels,
  837. const uint8_t **data, uint32_t flags)
  838. {
  839. graphics_t *graphics = thread_graphics;
  840. bool pow2tex = is_pow2(size);
  841. bool uses_mipmaps = (flags & GS_BUILD_MIPMAPS || levels != 1);
  842. if (!graphics)
  843. return NULL;
  844. if (uses_mipmaps && !pow2tex) {
  845. blog(LOG_WARNING, "Cannot use mipmaps with a "
  846. "non-power-of-two texture. Disabling "
  847. "mipmaps for this texture.");
  848. uses_mipmaps = false;
  849. flags &= ~GS_BUILD_MIPMAPS;
  850. levels = 1;
  851. }
  852. if (uses_mipmaps && flags & GS_RENDER_TARGET) {
  853. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  854. "Disabling mipmaps for this texture.");
  855. flags &= ~GS_BUILD_MIPMAPS;
  856. levels = 1;
  857. data = NULL;
  858. }
  859. return graphics->exports.device_cubetexture_create(graphics->device,
  860. size, color_format, levels, data, flags);
  861. }
  862. gs_texture_t *gs_voltexture_create(uint32_t width, uint32_t height,
  863. uint32_t depth, enum gs_color_format color_format,
  864. uint32_t levels, const uint8_t **data, uint32_t flags)
  865. {
  866. graphics_t *graphics = thread_graphics;
  867. if (!graphics) return NULL;
  868. return graphics->exports.device_voltexture_create(graphics->device,
  869. width, height, depth, color_format, levels, data,
  870. flags);
  871. }
  872. gs_zstencil_t *gs_zstencil_create(uint32_t width, uint32_t height,
  873. enum gs_zstencil_format format)
  874. {
  875. graphics_t *graphics = thread_graphics;
  876. if (!graphics) return NULL;
  877. return graphics->exports.device_zstencil_create(graphics->device,
  878. width, height, format);
  879. }
  880. gs_stagesurf_t *gs_stagesurface_create(uint32_t width, uint32_t height,
  881. enum gs_color_format color_format)
  882. {
  883. graphics_t *graphics = thread_graphics;
  884. if (!graphics) return NULL;
  885. return graphics->exports.device_stagesurface_create(graphics->device,
  886. width, height, color_format);
  887. }
  888. gs_samplerstate_t *gs_samplerstate_create(const struct gs_sampler_info *info)
  889. {
  890. graphics_t *graphics = thread_graphics;
  891. if (!graphics) return NULL;
  892. return graphics->exports.device_samplerstate_create(graphics->device,
  893. info);
  894. }
  895. gs_shader_t *gs_vertexshader_create(const char *shader, const char *file,
  896. char **error_string)
  897. {
  898. graphics_t *graphics = thread_graphics;
  899. if (!graphics) return NULL;
  900. return graphics->exports.device_vertexshader_create(graphics->device,
  901. shader, file, error_string);
  902. }
  903. gs_shader_t *gs_pixelshader_create(const char *shader,
  904. const char *file, char **error_string)
  905. {
  906. graphics_t *graphics = thread_graphics;
  907. if (!graphics) return NULL;
  908. return graphics->exports.device_pixelshader_create(graphics->device,
  909. shader, file, error_string);
  910. }
  911. gs_vertbuffer_t *gs_vertexbuffer_create(struct gs_vb_data *data,
  912. uint32_t flags)
  913. {
  914. graphics_t *graphics = thread_graphics;
  915. if (!graphics) return NULL;
  916. return graphics->exports.device_vertexbuffer_create(graphics->device,
  917. data, flags);
  918. }
  919. gs_indexbuffer_t *gs_indexbuffer_create(enum gs_index_type type,
  920. void *indices, size_t num, uint32_t flags)
  921. {
  922. graphics_t *graphics = thread_graphics;
  923. if (!graphics) return NULL;
  924. return graphics->exports.device_indexbuffer_create(graphics->device,
  925. type, indices, num, flags);
  926. }
  927. enum gs_texture_type gs_get_texture_type(const gs_texture_t *texture)
  928. {
  929. graphics_t *graphics = thread_graphics;
  930. if (!graphics) return GS_TEXTURE_2D;
  931. return graphics->exports.device_get_texture_type(texture);
  932. }
  933. void gs_load_vertexbuffer(gs_vertbuffer_t *vertbuffer)
  934. {
  935. graphics_t *graphics = thread_graphics;
  936. if (!graphics) return;
  937. graphics->exports.device_load_vertexbuffer(graphics->device,
  938. vertbuffer);
  939. }
  940. void gs_load_indexbuffer(gs_indexbuffer_t *indexbuffer)
  941. {
  942. graphics_t *graphics = thread_graphics;
  943. if (!graphics) return;
  944. graphics->exports.device_load_indexbuffer(graphics->device,
  945. indexbuffer);
  946. }
  947. void gs_load_texture(gs_texture_t *tex, int unit)
  948. {
  949. graphics_t *graphics = thread_graphics;
  950. if (!graphics) return;
  951. graphics->exports.device_load_texture(graphics->device, tex, unit);
  952. }
  953. void gs_load_samplerstate(gs_samplerstate_t *samplerstate, int unit)
  954. {
  955. graphics_t *graphics = thread_graphics;
  956. if (!graphics) return;
  957. graphics->exports.device_load_samplerstate(graphics->device,
  958. samplerstate, unit);
  959. }
  960. void gs_load_vertexshader(gs_shader_t *vertshader)
  961. {
  962. graphics_t *graphics = thread_graphics;
  963. if (!graphics) return;
  964. graphics->exports.device_load_vertexshader(graphics->device,
  965. vertshader);
  966. }
  967. void gs_load_pixelshader(gs_shader_t *pixelshader)
  968. {
  969. graphics_t *graphics = thread_graphics;
  970. if (!graphics) return;
  971. graphics->exports.device_load_pixelshader(graphics->device,
  972. pixelshader);
  973. }
  974. void gs_load_default_samplerstate(bool b_3d, int unit)
  975. {
  976. graphics_t *graphics = thread_graphics;
  977. if (!graphics) return;
  978. graphics->exports.device_load_default_samplerstate(graphics->device,
  979. b_3d, unit);
  980. }
  981. gs_shader_t *gs_get_vertex_shader(void)
  982. {
  983. graphics_t *graphics = thread_graphics;
  984. if (!graphics) return NULL;
  985. return graphics->exports.device_get_vertex_shader(graphics->device);
  986. }
  987. gs_shader_t *gs_get_pixel_shader(void)
  988. {
  989. graphics_t *graphics = thread_graphics;
  990. if (!graphics) return NULL;
  991. return graphics->exports.device_get_pixel_shader(graphics->device);
  992. }
  993. gs_texture_t *gs_get_render_target(void)
  994. {
  995. graphics_t *graphics = thread_graphics;
  996. if (!graphics) return NULL;
  997. return graphics->exports.device_get_render_target(graphics->device);
  998. }
  999. gs_zstencil_t *gs_get_zstencil_target(void)
  1000. {
  1001. graphics_t *graphics = thread_graphics;
  1002. if (!graphics) return NULL;
  1003. return graphics->exports.device_get_zstencil_target(graphics->device);
  1004. }
  1005. void gs_set_render_target(gs_texture_t *tex, gs_zstencil_t *zstencil)
  1006. {
  1007. graphics_t *graphics = thread_graphics;
  1008. if (!graphics) return;
  1009. graphics->exports.device_set_render_target(graphics->device, tex,
  1010. zstencil);
  1011. }
  1012. void gs_set_cube_render_target(gs_texture_t *cubetex, int side,
  1013. gs_zstencil_t *zstencil)
  1014. {
  1015. graphics_t *graphics = thread_graphics;
  1016. if (!graphics) return;
  1017. graphics->exports.device_set_cube_render_target(graphics->device,
  1018. cubetex, side, zstencil);
  1019. }
  1020. void gs_copy_texture(gs_texture_t *dst, gs_texture_t *src)
  1021. {
  1022. graphics_t *graphics = thread_graphics;
  1023. if (!graphics) return;
  1024. graphics->exports.device_copy_texture(graphics->device, dst, src);
  1025. }
  1026. void gs_copy_texture_region(gs_texture_t *dst, uint32_t dst_x, uint32_t dst_y,
  1027. gs_texture_t *src, uint32_t src_x, uint32_t src_y,
  1028. uint32_t src_w, uint32_t src_h)
  1029. {
  1030. graphics_t *graphics = thread_graphics;
  1031. if (!graphics) return;
  1032. graphics->exports.device_copy_texture_region(graphics->device,
  1033. dst, dst_x, dst_y,
  1034. src, src_x, src_y, src_w, src_h);
  1035. }
  1036. void gs_stage_texture(gs_stagesurf_t *dst, gs_texture_t *src)
  1037. {
  1038. graphics_t *graphics = thread_graphics;
  1039. if (!graphics) return;
  1040. graphics->exports.device_stage_texture(graphics->device, dst, src);
  1041. }
  1042. void gs_begin_scene(void)
  1043. {
  1044. graphics_t *graphics = thread_graphics;
  1045. if (!graphics) return;
  1046. graphics->exports.device_begin_scene(graphics->device);
  1047. }
  1048. void gs_draw(enum gs_draw_mode draw_mode, uint32_t start_vert,
  1049. uint32_t num_verts)
  1050. {
  1051. graphics_t *graphics = thread_graphics;
  1052. if (!graphics) return;
  1053. graphics->exports.device_draw(graphics->device, draw_mode,
  1054. start_vert, num_verts);
  1055. }
  1056. void gs_end_scene(void)
  1057. {
  1058. graphics_t *graphics = thread_graphics;
  1059. if (!graphics) return;
  1060. graphics->exports.device_end_scene(graphics->device);
  1061. }
  1062. void gs_load_swapchain(gs_swapchain_t *swapchain)
  1063. {
  1064. graphics_t *graphics = thread_graphics;
  1065. if (!graphics) return;
  1066. graphics->exports.device_load_swapchain(graphics->device, swapchain);
  1067. }
  1068. void gs_clear(uint32_t clear_flags, const struct vec4 *color, float depth,
  1069. uint8_t stencil)
  1070. {
  1071. graphics_t *graphics = thread_graphics;
  1072. graphics->exports.device_clear(graphics->device, clear_flags, color,
  1073. depth, stencil);
  1074. }
  1075. void gs_present(void)
  1076. {
  1077. graphics_t *graphics = thread_graphics;
  1078. if (!graphics) return;
  1079. graphics->exports.device_present(graphics->device);
  1080. }
  1081. void gs_flush(void)
  1082. {
  1083. graphics_t *graphics = thread_graphics;
  1084. if (!graphics) return;
  1085. graphics->exports.device_flush(graphics->device);
  1086. }
  1087. void gs_set_cull_mode(enum gs_cull_mode mode)
  1088. {
  1089. graphics_t *graphics = thread_graphics;
  1090. if (!graphics) return;
  1091. graphics->exports.device_set_cull_mode(graphics->device, mode);
  1092. }
  1093. enum gs_cull_mode gs_get_cull_mode(void)
  1094. {
  1095. graphics_t *graphics = thread_graphics;
  1096. if (!graphics) return GS_NEITHER;
  1097. return graphics->exports.device_get_cull_mode(graphics->device);
  1098. }
  1099. void gs_enable_blending(bool enable)
  1100. {
  1101. graphics_t *graphics = thread_graphics;
  1102. if (!graphics) return;
  1103. graphics->cur_blend_state.enabled = enable;
  1104. graphics->exports.device_enable_blending(graphics->device, enable);
  1105. }
  1106. void gs_enable_depth_test(bool enable)
  1107. {
  1108. graphics_t *graphics = thread_graphics;
  1109. if (!graphics) return;
  1110. graphics->exports.device_enable_depth_test(graphics->device, enable);
  1111. }
  1112. void gs_enable_stencil_test(bool enable)
  1113. {
  1114. graphics_t *graphics = thread_graphics;
  1115. if (!graphics) return;
  1116. graphics->exports.device_enable_stencil_test(graphics->device, enable);
  1117. }
  1118. void gs_enable_stencil_write(bool enable)
  1119. {
  1120. graphics_t *graphics = thread_graphics;
  1121. if (!graphics) return;
  1122. graphics->exports.device_enable_stencil_write(graphics->device, enable);
  1123. }
  1124. void gs_enable_color(bool red, bool green, bool blue, bool alpha)
  1125. {
  1126. graphics_t *graphics = thread_graphics;
  1127. if (!graphics) return;
  1128. graphics->exports.device_enable_color(graphics->device, red, green,
  1129. blue, alpha);
  1130. }
  1131. void gs_blend_function(enum gs_blend_type src, enum gs_blend_type dest)
  1132. {
  1133. graphics_t *graphics = thread_graphics;
  1134. if (!graphics) return;
  1135. graphics->cur_blend_state.src = src;
  1136. graphics->cur_blend_state.dest = dest;
  1137. graphics->exports.device_blend_function(graphics->device, src, dest);
  1138. }
  1139. void gs_depth_function(enum gs_depth_test test)
  1140. {
  1141. graphics_t *graphics = thread_graphics;
  1142. if (!graphics) return;
  1143. graphics->exports.device_depth_function(graphics->device, test);
  1144. }
  1145. void gs_stencil_function(enum gs_stencil_side side, enum gs_depth_test test)
  1146. {
  1147. graphics_t *graphics = thread_graphics;
  1148. if (!graphics) return;
  1149. graphics->exports.device_stencil_function(graphics->device, side, test);
  1150. }
  1151. void gs_stencil_op(enum gs_stencil_side side, enum gs_stencil_op_type fail,
  1152. enum gs_stencil_op_type zfail, enum gs_stencil_op_type zpass)
  1153. {
  1154. graphics_t *graphics = thread_graphics;
  1155. if (!graphics) return;
  1156. graphics->exports.device_stencil_op(graphics->device, side, fail, zfail,
  1157. zpass);
  1158. }
  1159. void gs_set_viewport(int x, int y, int width, int height)
  1160. {
  1161. graphics_t *graphics = thread_graphics;
  1162. if (!graphics) return;
  1163. graphics->exports.device_set_viewport(graphics->device, x, y, width,
  1164. height);
  1165. }
  1166. void gs_get_viewport(struct gs_rect *rect)
  1167. {
  1168. graphics_t *graphics = thread_graphics;
  1169. if (!graphics) return;
  1170. graphics->exports.device_get_viewport(graphics->device, rect);
  1171. }
  1172. void gs_set_scissor_rect(const struct gs_rect *rect)
  1173. {
  1174. graphics_t *graphics = thread_graphics;
  1175. if (!graphics) return;
  1176. graphics->exports.device_set_scissor_rect(graphics->device, rect);
  1177. }
  1178. void gs_ortho(float left, float right, float top, float bottom, float znear,
  1179. float zfar)
  1180. {
  1181. graphics_t *graphics = thread_graphics;
  1182. if (!graphics) return;
  1183. graphics->exports.device_ortho(graphics->device, left, right, top,
  1184. bottom, znear, zfar);
  1185. }
  1186. void gs_frustum(float left, float right, float top, float bottom, float znear,
  1187. float zfar)
  1188. {
  1189. graphics_t *graphics = thread_graphics;
  1190. if (!graphics) return;
  1191. graphics->exports.device_frustum(graphics->device, left, right, top,
  1192. bottom, znear, zfar);
  1193. }
  1194. void gs_projection_push(void)
  1195. {
  1196. graphics_t *graphics = thread_graphics;
  1197. if (!graphics) return;
  1198. graphics->exports.device_projection_push(graphics->device);
  1199. }
  1200. void gs_projection_pop(void)
  1201. {
  1202. graphics_t *graphics = thread_graphics;
  1203. if (!graphics) return;
  1204. graphics->exports.device_projection_pop(graphics->device);
  1205. }
  1206. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1207. {
  1208. graphics_t *graphics = thread_graphics;
  1209. if (!graphics || !swapchain) return;
  1210. graphics->exports.gs_swapchain_destroy(swapchain);
  1211. }
  1212. void gs_shader_destroy(gs_shader_t *shader)
  1213. {
  1214. graphics_t *graphics = thread_graphics;
  1215. if (!graphics || !shader) return;
  1216. graphics->exports.gs_shader_destroy(shader);
  1217. }
  1218. int gs_shader_get_num_params(const gs_shader_t *shader)
  1219. {
  1220. graphics_t *graphics = thread_graphics;
  1221. if (!graphics || !shader) return 0;
  1222. return graphics->exports.gs_shader_get_num_params(shader);
  1223. }
  1224. gs_sparam_t *gs_shader_get_param_by_idx(gs_shader_t *shader, uint32_t param)
  1225. {
  1226. graphics_t *graphics = thread_graphics;
  1227. if (!graphics || !shader) return NULL;
  1228. return graphics->exports.gs_shader_get_param_by_idx(shader, param);
  1229. }
  1230. gs_sparam_t *gs_shader_get_param_by_name(gs_shader_t *shader, const char *name)
  1231. {
  1232. graphics_t *graphics = thread_graphics;
  1233. if (!graphics || !shader) return NULL;
  1234. return graphics->exports.gs_shader_get_param_by_name(shader, name);
  1235. }
  1236. gs_sparam_t *gs_shader_get_viewproj_matrix(const gs_shader_t *shader)
  1237. {
  1238. graphics_t *graphics = thread_graphics;
  1239. if (!graphics || !shader) return NULL;
  1240. return graphics->exports.gs_shader_get_viewproj_matrix(shader);
  1241. }
  1242. gs_sparam_t *gs_shader_get_world_matrix(const gs_shader_t *shader)
  1243. {
  1244. graphics_t *graphics = thread_graphics;
  1245. if (!graphics || !shader) return NULL;
  1246. return graphics->exports.gs_shader_get_world_matrix(shader);
  1247. }
  1248. void gs_shader_get_param_info(const gs_sparam_t *param,
  1249. struct gs_shader_param_info *info)
  1250. {
  1251. graphics_t *graphics = thread_graphics;
  1252. if (!graphics || !param) return;
  1253. graphics->exports.gs_shader_get_param_info(param, info);
  1254. }
  1255. void gs_shader_set_bool(gs_sparam_t *param, bool val)
  1256. {
  1257. graphics_t *graphics = thread_graphics;
  1258. if (!graphics || !param) return;
  1259. graphics->exports.gs_shader_set_bool(param, val);
  1260. }
  1261. void gs_shader_set_float(gs_sparam_t *param, float val)
  1262. {
  1263. graphics_t *graphics = thread_graphics;
  1264. if (!graphics || !param) return;
  1265. graphics->exports.gs_shader_set_float(param, val);
  1266. }
  1267. void gs_shader_set_int(gs_sparam_t *param, int val)
  1268. {
  1269. graphics_t *graphics = thread_graphics;
  1270. if (!graphics || !param) return;
  1271. graphics->exports.gs_shader_set_int(param, val);
  1272. }
  1273. void gs_shader_setmatrix3(gs_sparam_t *param, const struct matrix3 *val)
  1274. {
  1275. graphics_t *graphics = thread_graphics;
  1276. if (!graphics || !param) return;
  1277. graphics->exports.gs_shader_setmatrix3(param, val);
  1278. }
  1279. void gs_shader_set_matrix4(gs_sparam_t *param, const struct matrix4 *val)
  1280. {
  1281. graphics_t *graphics = thread_graphics;
  1282. if (!graphics || !param) return;
  1283. graphics->exports.gs_shader_set_matrix4(param, val);
  1284. }
  1285. void gs_shader_set_vec2(gs_sparam_t *param, const struct vec2 *val)
  1286. {
  1287. graphics_t *graphics = thread_graphics;
  1288. if (!graphics || !param) return;
  1289. graphics->exports.gs_shader_set_vec2(param, val);
  1290. }
  1291. void gs_shader_set_vec3(gs_sparam_t *param, const struct vec3 *val)
  1292. {
  1293. graphics_t *graphics = thread_graphics;
  1294. if (!graphics || !param) return;
  1295. graphics->exports.gs_shader_set_vec3(param, val);
  1296. }
  1297. void gs_shader_set_vec4(gs_sparam_t *param, const struct vec4 *val)
  1298. {
  1299. graphics_t *graphics = thread_graphics;
  1300. if (!graphics || !param) return;
  1301. graphics->exports.gs_shader_set_vec4(param, val);
  1302. }
  1303. void gs_shader_set_texture(gs_sparam_t *param, gs_texture_t *val)
  1304. {
  1305. graphics_t *graphics = thread_graphics;
  1306. if (!graphics || !param) return;
  1307. graphics->exports.gs_shader_set_texture(param, val);
  1308. }
  1309. void gs_shader_set_val(gs_sparam_t *param, const void *val, size_t size)
  1310. {
  1311. graphics_t *graphics = thread_graphics;
  1312. if (!graphics || !param) return;
  1313. graphics->exports.gs_shader_set_val(param, val, size);
  1314. }
  1315. void gs_shader_set_default(gs_sparam_t *param)
  1316. {
  1317. graphics_t *graphics = thread_graphics;
  1318. if (!graphics || !param) return;
  1319. graphics->exports.gs_shader_set_default(param);
  1320. }
  1321. void gs_texture_destroy(gs_texture_t *tex)
  1322. {
  1323. graphics_t *graphics = thread_graphics;
  1324. if (!graphics || !tex) return;
  1325. graphics->exports.gs_texture_destroy(tex);
  1326. }
  1327. uint32_t gs_texture_get_width(const gs_texture_t *tex)
  1328. {
  1329. graphics_t *graphics = thread_graphics;
  1330. if (!graphics || !tex) return 0;
  1331. return graphics->exports.gs_texture_get_width(tex);
  1332. }
  1333. uint32_t gs_texture_get_height(const gs_texture_t *tex)
  1334. {
  1335. graphics_t *graphics = thread_graphics;
  1336. if (!graphics || !tex) return 0;
  1337. return graphics->exports.gs_texture_get_height(tex);
  1338. }
  1339. enum gs_color_format gs_texture_get_color_format(const gs_texture_t *tex)
  1340. {
  1341. graphics_t *graphics = thread_graphics;
  1342. if (!graphics || !tex) return GS_UNKNOWN;
  1343. return graphics->exports.gs_texture_get_color_format(tex);
  1344. }
  1345. bool gs_texture_map(gs_texture_t *tex, uint8_t **ptr, uint32_t *linesize)
  1346. {
  1347. graphics_t *graphics = thread_graphics;
  1348. if (!graphics || !tex) return false;
  1349. return graphics->exports.gs_texture_map(tex, ptr, linesize);
  1350. }
  1351. void gs_texture_unmap(gs_texture_t *tex)
  1352. {
  1353. graphics_t *graphics = thread_graphics;
  1354. if (!graphics || !tex) return;
  1355. graphics->exports.gs_texture_unmap(tex);
  1356. }
  1357. bool gs_texture_is_rect(const gs_texture_t *tex)
  1358. {
  1359. graphics_t *graphics = thread_graphics;
  1360. if (!graphics || !tex) return false;
  1361. if (graphics->exports.gs_texture_is_rect)
  1362. return graphics->exports.gs_texture_is_rect(tex);
  1363. else
  1364. return false;
  1365. }
  1366. void *gs_texture_get_obj(gs_texture_t *tex)
  1367. {
  1368. graphics_t *graphics = thread_graphics;
  1369. if (!graphics || !tex) return NULL;
  1370. return graphics->exports.gs_texture_get_obj(tex);
  1371. }
  1372. void gs_cubetexture_destroy(gs_texture_t *cubetex)
  1373. {
  1374. graphics_t *graphics = thread_graphics;
  1375. if (!graphics || !cubetex) return;
  1376. graphics->exports.gs_cubetexture_destroy(cubetex);
  1377. }
  1378. uint32_t gs_cubetexture_get_size(const gs_texture_t *cubetex)
  1379. {
  1380. graphics_t *graphics = thread_graphics;
  1381. if (!graphics || !cubetex) return 0;
  1382. return graphics->exports.gs_cubetexture_get_size(cubetex);
  1383. }
  1384. enum gs_color_format gs_cubetexture_get_color_format(
  1385. const gs_texture_t *cubetex)
  1386. {
  1387. graphics_t *graphics = thread_graphics;
  1388. if (!graphics || !cubetex) return GS_UNKNOWN;
  1389. return graphics->exports.gs_cubetexture_get_color_format(cubetex);
  1390. }
  1391. void gs_voltexture_destroy(gs_texture_t *voltex)
  1392. {
  1393. graphics_t *graphics = thread_graphics;
  1394. if (!graphics || !voltex) return;
  1395. graphics->exports.gs_voltexture_destroy(voltex);
  1396. }
  1397. uint32_t gs_voltexture_get_width(const gs_texture_t *voltex)
  1398. {
  1399. graphics_t *graphics = thread_graphics;
  1400. if (!graphics || !voltex) return 0;
  1401. return graphics->exports.gs_voltexture_get_width(voltex);
  1402. }
  1403. uint32_t gs_voltexture_get_height(const gs_texture_t *voltex)
  1404. {
  1405. graphics_t *graphics = thread_graphics;
  1406. if (!graphics || !voltex) return 0;
  1407. return graphics->exports.gs_voltexture_get_height(voltex);
  1408. }
  1409. uint32_t gs_voltexture_getdepth(const gs_texture_t *voltex)
  1410. {
  1411. graphics_t *graphics = thread_graphics;
  1412. if (!graphics || !voltex) return 0;
  1413. return graphics->exports.gs_voltexture_getdepth(voltex);
  1414. }
  1415. enum gs_color_format gs_voltexture_get_color_format(const gs_texture_t *voltex)
  1416. {
  1417. graphics_t *graphics = thread_graphics;
  1418. if (!graphics || !voltex) return GS_UNKNOWN;
  1419. return graphics->exports.gs_voltexture_get_color_format(voltex);
  1420. }
  1421. void gs_stagesurface_destroy(gs_stagesurf_t *stagesurf)
  1422. {
  1423. graphics_t *graphics = thread_graphics;
  1424. if (!graphics || !stagesurf) return;
  1425. graphics->exports.gs_stagesurface_destroy(stagesurf);
  1426. }
  1427. uint32_t gs_stagesurface_get_width(const gs_stagesurf_t *stagesurf)
  1428. {
  1429. graphics_t *graphics = thread_graphics;
  1430. if (!graphics || !stagesurf) return 0;
  1431. return graphics->exports.gs_stagesurface_get_width(stagesurf);
  1432. }
  1433. uint32_t gs_stagesurface_get_height(const gs_stagesurf_t *stagesurf)
  1434. {
  1435. graphics_t *graphics = thread_graphics;
  1436. if (!graphics || !stagesurf) return 0;
  1437. return graphics->exports.gs_stagesurface_get_height(stagesurf);
  1438. }
  1439. enum gs_color_format gs_stagesurface_get_color_format(
  1440. const gs_stagesurf_t *stagesurf)
  1441. {
  1442. graphics_t *graphics = thread_graphics;
  1443. if (!graphics || !stagesurf) return GS_UNKNOWN;
  1444. return graphics->exports.gs_stagesurface_get_color_format(stagesurf);
  1445. }
  1446. bool gs_stagesurface_map(gs_stagesurf_t *stagesurf, uint8_t **data,
  1447. uint32_t *linesize)
  1448. {
  1449. graphics_t *graphics = thread_graphics;
  1450. if (!graphics || !stagesurf) return false;
  1451. return graphics->exports.gs_stagesurface_map(stagesurf, data, linesize);
  1452. }
  1453. void gs_stagesurface_unmap(gs_stagesurf_t *stagesurf)
  1454. {
  1455. graphics_t *graphics = thread_graphics;
  1456. if (!graphics || !stagesurf) return;
  1457. graphics->exports.gs_stagesurface_unmap(stagesurf);
  1458. }
  1459. void gs_zstencil_destroy(gs_zstencil_t *zstencil)
  1460. {
  1461. if (!thread_graphics || !zstencil) return;
  1462. thread_graphics->exports.gs_zstencil_destroy(zstencil);
  1463. }
  1464. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1465. {
  1466. if (!thread_graphics || !samplerstate) return;
  1467. thread_graphics->exports.gs_samplerstate_destroy(samplerstate);
  1468. }
  1469. void gs_vertexbuffer_destroy(gs_vertbuffer_t *vertbuffer)
  1470. {
  1471. graphics_t *graphics = thread_graphics;
  1472. if (!graphics || !vertbuffer) return;
  1473. graphics->exports.gs_vertexbuffer_destroy(vertbuffer);
  1474. }
  1475. void gs_vertexbuffer_flush(gs_vertbuffer_t *vertbuffer)
  1476. {
  1477. if (!thread_graphics || !vertbuffer) return;
  1478. thread_graphics->exports.gs_vertexbuffer_flush(vertbuffer);
  1479. }
  1480. struct gs_vb_data *gs_vertexbuffer_get_data(const gs_vertbuffer_t *vertbuffer)
  1481. {
  1482. if (!thread_graphics || !vertbuffer) return NULL;
  1483. return thread_graphics->exports.gs_vertexbuffer_get_data(vertbuffer);
  1484. }
  1485. void gs_indexbuffer_destroy(gs_indexbuffer_t *indexbuffer)
  1486. {
  1487. graphics_t *graphics = thread_graphics;
  1488. if (!graphics || !indexbuffer) return;
  1489. graphics->exports.gs_indexbuffer_destroy(indexbuffer);
  1490. }
  1491. void gs_indexbuffer_flush(gs_indexbuffer_t *indexbuffer)
  1492. {
  1493. if (!thread_graphics || !indexbuffer) return;
  1494. thread_graphics->exports.gs_indexbuffer_flush(indexbuffer);
  1495. }
  1496. void *gs_indexbuffer_get_data(const gs_indexbuffer_t *indexbuffer)
  1497. {
  1498. if (!thread_graphics || !indexbuffer) return NULL;
  1499. return thread_graphics->exports.gs_indexbuffer_get_data(indexbuffer);
  1500. }
  1501. size_t gs_indexbuffer_get_num_indices(const gs_indexbuffer_t *indexbuffer)
  1502. {
  1503. if (!thread_graphics || !indexbuffer) return 0;
  1504. return thread_graphics->exports.gs_indexbuffer_get_num_indices(
  1505. indexbuffer);
  1506. }
  1507. enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *indexbuffer)
  1508. {
  1509. if (!thread_graphics || !indexbuffer) return (enum gs_index_type)0;
  1510. return thread_graphics->exports.gs_indexbuffer_get_type(indexbuffer);
  1511. }
  1512. #ifdef __APPLE__
  1513. /** Platform specific functions */
  1514. gs_texture_t *gs_texture_create_from_iosurface(void *iosurf)
  1515. {
  1516. graphics_t *graphics = thread_graphics;
  1517. if (!graphics || !iosurf ||
  1518. !graphics->exports.device_texture_create_from_iosurface)
  1519. return NULL;
  1520. return graphics->exports.device_texture_create_from_iosurface(
  1521. graphics->device, iosurf);
  1522. }
  1523. bool gs_texture_rebind_iosurface(gs_texture_t *texture, void *iosurf)
  1524. {
  1525. graphics_t *graphics = thread_graphics;
  1526. if (!graphics || !iosurf ||
  1527. !graphics->exports.gs_texture_rebind_iosurface)
  1528. return false;
  1529. return graphics->exports.gs_texture_rebind_iosurface(texture, iosurf);
  1530. }
  1531. #elif _WIN32
  1532. bool gs_gdi_texture_available(void)
  1533. {
  1534. if (!thread_graphics)
  1535. return false;
  1536. return thread_graphics->exports.device_gdi_texture_available();
  1537. }
  1538. /** creates a windows GDI-lockable texture */
  1539. gs_texture_t *gs_texture_create_gdi(uint32_t width, uint32_t height)
  1540. {
  1541. graphics_t *graphics = thread_graphics;
  1542. if (!graphics) return NULL;
  1543. if (graphics->exports.device_texture_create_gdi)
  1544. return graphics->exports.device_texture_create_gdi(
  1545. graphics->device, width, height);
  1546. return NULL;
  1547. }
  1548. void *gs_texture_get_dc(gs_texture_t *gdi_tex)
  1549. {
  1550. if (!thread_graphics || !gdi_tex)
  1551. return NULL;
  1552. if (thread_graphics->exports.gs_texture_get_dc)
  1553. return thread_graphics->exports.gs_texture_get_dc(gdi_tex);
  1554. return NULL;
  1555. }
  1556. void gs_texture_release_dc(gs_texture_t *gdi_tex)
  1557. {
  1558. if (!thread_graphics || !gdi_tex)
  1559. return;
  1560. if (thread_graphics->exports.gs_texture_release_dc)
  1561. thread_graphics->exports.gs_texture_release_dc(gdi_tex);
  1562. }
  1563. #endif