graphics.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  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. bool load_graphics_imports(struct gs_exports *exports, void *module,
  32. const char *module_name);
  33. static bool graphics_init_immediate_vb(struct graphics_subsystem *graphics)
  34. {
  35. struct vb_data *vbd;
  36. vbd = vbdata_create();
  37. vbd->num = IMMEDIATE_COUNT;
  38. vbd->points = bmalloc(sizeof(struct vec3)*IMMEDIATE_COUNT);
  39. vbd->normals = bmalloc(sizeof(struct vec3)*IMMEDIATE_COUNT);
  40. vbd->colors = bmalloc(sizeof(uint32_t) *IMMEDIATE_COUNT);
  41. vbd->num_tex = 1;
  42. vbd->tvarray = bmalloc(sizeof(struct tvertarray));
  43. vbd->tvarray[0].width = 2;
  44. vbd->tvarray[0].array =
  45. bmalloc(sizeof(struct vec2) * IMMEDIATE_COUNT);
  46. graphics->immediate_vertbuffer = graphics->exports.
  47. device_create_vertexbuffer(graphics->device, vbd, GS_DYNAMIC);
  48. if (!graphics->immediate_vertbuffer)
  49. return false;
  50. return true;
  51. }
  52. static bool graphics_init_sprite_vb(struct graphics_subsystem *graphics)
  53. {
  54. struct vb_data *vbd;
  55. vbd = vbdata_create();
  56. vbd->num = 4;
  57. vbd->points = bmalloc(sizeof(struct vec3) * 4);
  58. vbd->num_tex = 1;
  59. vbd->tvarray = bmalloc(sizeof(struct tvertarray));
  60. vbd->tvarray[0].width = 2;
  61. vbd->tvarray[0].array = bmalloc(sizeof(struct vec2) * 4);
  62. memset(vbd->points, 0, sizeof(struct vec3) * 4);
  63. memset(vbd->tvarray[0].array, 0, sizeof(struct vec2) * 4);
  64. graphics->sprite_buffer = graphics->exports.
  65. device_create_vertexbuffer(graphics->device, vbd, GS_DYNAMIC);
  66. if (!graphics->sprite_buffer)
  67. return false;
  68. return true;
  69. }
  70. static bool graphics_init(struct graphics_subsystem *graphics)
  71. {
  72. struct matrix3 top_mat;
  73. matrix3_identity(&top_mat);
  74. da_push_back(graphics->matrix_stack, &top_mat);
  75. graphics->exports.device_entercontext(graphics->device);
  76. if (!graphics_init_immediate_vb(graphics))
  77. return false;
  78. if (!graphics_init_sprite_vb(graphics))
  79. return false;
  80. if (pthread_mutex_init(&graphics->mutex, NULL) != 0)
  81. return false;
  82. graphics->exports.device_leavecontext(graphics->device);
  83. return true;
  84. }
  85. int gs_create(graphics_t *pgraphics, const char *module,
  86. struct gs_init_data *data)
  87. {
  88. int errcode = GS_ERROR_FAIL;
  89. graphics_t graphics = bzalloc(sizeof(struct graphics_subsystem));
  90. pthread_mutex_init_value(&graphics->mutex);
  91. graphics->module = os_dlopen(module);
  92. if (!graphics->module) {
  93. errcode = GS_ERROR_MODULENOTFOUND;
  94. goto error;
  95. }
  96. if (!load_graphics_imports(&graphics->exports, graphics->module,
  97. module))
  98. goto error;
  99. graphics->device = graphics->exports.device_create(data);
  100. if (!graphics->device)
  101. goto error;
  102. if (!graphics_init(graphics))
  103. goto error;
  104. *pgraphics = graphics;
  105. return GS_SUCCESS;
  106. error:
  107. gs_destroy(graphics);
  108. return errcode;
  109. }
  110. void gs_destroy(graphics_t graphics)
  111. {
  112. if (!graphics)
  113. return;
  114. while (thread_graphics)
  115. gs_leavecontext();
  116. if (graphics->device) {
  117. graphics->exports.device_entercontext(graphics->device);
  118. graphics->exports.vertexbuffer_destroy(graphics->sprite_buffer);
  119. graphics->exports.vertexbuffer_destroy(
  120. graphics->immediate_vertbuffer);
  121. graphics->exports.device_destroy(graphics->device);
  122. }
  123. pthread_mutex_destroy(&graphics->mutex);
  124. da_free(graphics->matrix_stack);
  125. da_free(graphics->viewport_stack);
  126. if (graphics->module)
  127. os_dlclose(graphics->module);
  128. bfree(graphics);
  129. }
  130. void gs_entercontext(graphics_t graphics)
  131. {
  132. if (!graphics) return;
  133. bool is_current = thread_graphics == graphics;
  134. if (thread_graphics && !is_current) {
  135. while (thread_graphics)
  136. gs_leavecontext();
  137. }
  138. if (!is_current) {
  139. pthread_mutex_lock(&graphics->mutex);
  140. graphics->exports.device_entercontext(graphics->device);
  141. thread_graphics = graphics;
  142. }
  143. graphics->ref++;
  144. }
  145. void gs_leavecontext(void)
  146. {
  147. if (thread_graphics) {
  148. if (!--thread_graphics->ref) {
  149. graphics_t graphics = thread_graphics;
  150. graphics->exports.device_leavecontext(graphics->device);
  151. pthread_mutex_unlock(&graphics->mutex);
  152. thread_graphics = NULL;
  153. }
  154. }
  155. }
  156. graphics_t gs_getcontext(void)
  157. {
  158. return thread_graphics;
  159. }
  160. static inline struct matrix3 *top_matrix(graphics_t graphics)
  161. {
  162. return graphics ?
  163. (graphics->matrix_stack.array + graphics->cur_matrix) : NULL;
  164. }
  165. void gs_matrix_push(void)
  166. {
  167. graphics_t graphics = thread_graphics;
  168. if (!graphics)
  169. return;
  170. struct matrix3 mat, *top_mat = top_matrix(graphics);
  171. memcpy(&mat, top_mat, sizeof(struct matrix3));
  172. da_push_back(graphics->matrix_stack, &mat);
  173. graphics->cur_matrix++;
  174. }
  175. void gs_matrix_pop(void)
  176. {
  177. graphics_t graphics = thread_graphics;
  178. if (!graphics)
  179. return;
  180. if (graphics->cur_matrix == 0) {
  181. blog(LOG_ERROR, "Tried to pop last matrix on stack");
  182. return;
  183. }
  184. da_erase(graphics->matrix_stack, graphics->cur_matrix);
  185. graphics->cur_matrix--;
  186. }
  187. void gs_matrix_identity(void)
  188. {
  189. struct matrix3 *top_mat = top_matrix(thread_graphics);
  190. if (top_mat)
  191. matrix3_identity(top_mat);
  192. }
  193. void gs_matrix_transpose(void)
  194. {
  195. struct matrix3 *top_mat = top_matrix(thread_graphics);
  196. if (top_mat)
  197. matrix3_transpose(top_mat, top_mat);
  198. }
  199. void gs_matrix_set(const struct matrix3 *matrix)
  200. {
  201. struct matrix3 *top_mat = top_matrix(thread_graphics);
  202. if (top_mat)
  203. matrix3_copy(top_mat, matrix);
  204. }
  205. void gs_matrix_get(struct matrix3 *dst)
  206. {
  207. struct matrix3 *top_mat = top_matrix(thread_graphics);
  208. if (top_mat)
  209. matrix3_copy(dst, top_mat);
  210. }
  211. void gs_matrix_mul(const struct matrix3 *matrix)
  212. {
  213. struct matrix3 *top_mat = top_matrix(thread_graphics);
  214. if (top_mat)
  215. matrix3_mul(top_mat, top_mat, matrix);
  216. }
  217. void gs_matrix_rotquat(const struct quat *rot)
  218. {
  219. struct matrix3 *top_mat = top_matrix(thread_graphics);
  220. if (top_mat)
  221. matrix3_rotate(top_mat, top_mat, rot);
  222. }
  223. void gs_matrix_rotaa(const struct axisang *rot)
  224. {
  225. struct matrix3 *top_mat = top_matrix(thread_graphics);
  226. if (top_mat)
  227. matrix3_rotate_aa(top_mat, top_mat, rot);
  228. }
  229. void gs_matrix_translate(const struct vec3 *pos)
  230. {
  231. struct matrix3 *top_mat = top_matrix(thread_graphics);
  232. if (top_mat)
  233. matrix3_translate(top_mat, top_mat, pos);
  234. }
  235. void gs_matrix_scale(const struct vec3 *scale)
  236. {
  237. struct matrix3 *top_mat = top_matrix(thread_graphics);
  238. if (top_mat)
  239. matrix3_scale(top_mat, top_mat, scale);
  240. }
  241. void gs_matrix_rotaa4f(float x, float y, float z, float angle)
  242. {
  243. struct matrix3 *top_mat = top_matrix(thread_graphics);
  244. struct axisang aa;
  245. if (!top_mat) {
  246. axisang_set(&aa, x, y, z, angle);
  247. matrix3_rotate_aa(top_mat, top_mat, &aa);
  248. }
  249. }
  250. void gs_matrix_translate3f(float x, float y, float z)
  251. {
  252. struct matrix3 *top_mat = top_matrix(thread_graphics);
  253. struct vec3 p;
  254. if (top_mat) {
  255. vec3_set(&p, x, y, z);
  256. matrix3_translate(top_mat, top_mat, &p);
  257. }
  258. }
  259. void gs_matrix_scale3f(float x, float y, float z)
  260. {
  261. struct matrix3 *top_mat = top_matrix(thread_graphics);
  262. struct vec3 p;
  263. if (top_mat) {
  264. vec3_set(&p, x, y, z);
  265. matrix3_scale(top_mat, top_mat, &p);
  266. }
  267. }
  268. static inline void reset_immediate_arrays(graphics_t graphics)
  269. {
  270. da_init(graphics->verts);
  271. da_init(graphics->norms);
  272. da_init(graphics->colors);
  273. for (size_t i = 0; i < 16; i++)
  274. da_init(graphics->texverts[i]);
  275. }
  276. void gs_renderstart(bool b_new)
  277. {
  278. graphics_t graphics = thread_graphics;
  279. if (!graphics)
  280. return;
  281. graphics->using_immediate = !b_new;
  282. reset_immediate_arrays(graphics);
  283. if (b_new) {
  284. graphics->vbd = vbdata_create();
  285. } else {
  286. graphics->vbd = vertexbuffer_getdata(
  287. graphics->immediate_vertbuffer);
  288. memset(graphics->vbd->colors, 0xFF,
  289. sizeof(uint32_t) * IMMEDIATE_COUNT);
  290. graphics->verts.array = graphics->vbd->points;
  291. graphics->norms.array = graphics->vbd->normals;
  292. graphics->colors.array = graphics->vbd->colors;
  293. graphics->texverts[0].array = graphics->vbd->tvarray[0].array;
  294. graphics->verts.capacity = IMMEDIATE_COUNT;
  295. graphics->norms.capacity = IMMEDIATE_COUNT;
  296. graphics->colors.capacity = IMMEDIATE_COUNT;
  297. graphics->texverts[0].capacity = IMMEDIATE_COUNT;
  298. }
  299. }
  300. static inline size_t min_size(const size_t a, const size_t b)
  301. {
  302. return (a < b) ? a : b;
  303. }
  304. void gs_renderstop(enum gs_draw_mode mode)
  305. {
  306. graphics_t graphics = thread_graphics;
  307. size_t i, num;
  308. if (!graphics)
  309. return;
  310. num = graphics->verts.num;
  311. if (!num) {
  312. if (!graphics->using_immediate) {
  313. da_free(graphics->verts);
  314. da_free(graphics->norms);
  315. da_free(graphics->colors);
  316. for (i = 0; i < 16; i++)
  317. da_free(graphics->texverts[i]);
  318. vbdata_destroy(graphics->vbd);
  319. }
  320. return;
  321. }
  322. if (graphics->norms.num &&
  323. (graphics->norms.num != graphics->verts.num)) {
  324. blog(LOG_WARNING, "gs_renderstop: normal count does "
  325. "not match vertex count");
  326. num = min_size(num, graphics->norms.num);
  327. }
  328. if (graphics->colors.num &&
  329. (graphics->colors.num != graphics->verts.num)) {
  330. blog(LOG_WARNING, "gs_renderstop: color count does "
  331. "not match vertex count");
  332. num = min_size(num, graphics->colors.num);
  333. }
  334. if (graphics->texverts[0].num &&
  335. (graphics->texverts[0].num != graphics->verts.num)) {
  336. blog(LOG_WARNING, "gs_renderstop: texture vertex count does "
  337. "not match vertex count");
  338. num = min_size(num, graphics->texverts[0].num);
  339. }
  340. if (graphics->using_immediate) {
  341. vertexbuffer_flush(graphics->immediate_vertbuffer, false);
  342. gs_load_vertexbuffer(graphics->immediate_vertbuffer);
  343. gs_load_indexbuffer(NULL);
  344. gs_draw(mode, 0, (uint32_t)num);
  345. reset_immediate_arrays(graphics);
  346. } else {
  347. vertbuffer_t vb = gs_rendersave();
  348. gs_load_vertexbuffer(vb);
  349. gs_load_indexbuffer(NULL);
  350. gs_draw(mode, 0, 0);
  351. vertexbuffer_destroy(vb);
  352. }
  353. graphics->vbd = NULL;
  354. }
  355. vertbuffer_t gs_rendersave(void)
  356. {
  357. graphics_t graphics = thread_graphics;
  358. size_t num_tex, i;
  359. if (!graphics)
  360. return NULL;
  361. if (graphics->using_immediate)
  362. return NULL;
  363. if (!graphics->vbd->num) {
  364. vbdata_destroy(graphics->vbd);
  365. return NULL;
  366. }
  367. for (num_tex = 0; num_tex < 16; num_tex++) {
  368. if (!graphics->texverts[num_tex].num)
  369. break;
  370. }
  371. graphics->vbd->points = graphics->verts.array;
  372. graphics->vbd->normals = graphics->norms.array;
  373. graphics->vbd->colors = graphics->colors.array;
  374. graphics->vbd->num = graphics->verts.num;
  375. graphics->vbd->num_tex = num_tex;
  376. graphics->vbd->tvarray = bmalloc(sizeof(struct tvertarray) * num_tex);
  377. for (i = 0; i < num_tex; i++) {
  378. graphics->vbd->tvarray[i].width = 2;
  379. graphics->vbd->tvarray[i].array = graphics->texverts[i].array;
  380. }
  381. reset_immediate_arrays(graphics);
  382. return gs_create_vertexbuffer(graphics->vbd, 0);
  383. }
  384. void gs_vertex2f(float x, float y)
  385. {
  386. struct vec3 v3;
  387. vec3_set(&v3, x, y, 0.0f);
  388. gs_vertex3v(&v3);
  389. }
  390. void gs_vertex3f(float x, float y, float z)
  391. {
  392. struct vec3 v3;
  393. vec3_set(&v3, x, y, z);
  394. gs_vertex3v(&v3);
  395. }
  396. void gs_normal3f(float x, float y, float z)
  397. {
  398. struct vec3 v3;
  399. vec3_set(&v3, x, y, z);
  400. gs_normal3v(&v3);
  401. }
  402. static inline bool validvertsize(graphics_t graphics, size_t num,
  403. const char *name)
  404. {
  405. if (!graphics)
  406. return false;
  407. if (graphics->using_immediate && num == IMMEDIATE_COUNT) {
  408. blog(LOG_WARNING, "%s: tried to use over %u "
  409. "for immediate rendering",
  410. name, IMMEDIATE_COUNT);
  411. return false;
  412. }
  413. return true;
  414. }
  415. void gs_color(uint32_t color)
  416. {
  417. graphics_t graphics = thread_graphics;
  418. if (!validvertsize(graphics, graphics->colors.num, "gs_color"))
  419. return;
  420. da_push_back(graphics->colors, &color);
  421. }
  422. void gs_texcoord(float x, float y, int unit)
  423. {
  424. struct vec2 v2;
  425. vec2_set(&v2, x, y);
  426. gs_texcoord2v(&v2, unit);
  427. }
  428. void gs_vertex2v(const struct vec2 *v)
  429. {
  430. struct vec3 v3;
  431. vec3_set(&v3, v->x, v->y, 0.0f);
  432. gs_vertex3v(&v3);
  433. }
  434. void gs_vertex3v(const struct vec3 *v)
  435. {
  436. graphics_t graphics = thread_graphics;
  437. if (!validvertsize(graphics, graphics->verts.num, "gs_vertex"))
  438. return;
  439. da_push_back(graphics->verts, v);
  440. }
  441. void gs_normal3v(const struct vec3 *v)
  442. {
  443. graphics_t graphics = thread_graphics;
  444. if (!validvertsize(graphics, graphics->norms.num, "gs_normal"))
  445. return;
  446. da_push_back(graphics->norms, v);
  447. }
  448. void gs_color4v(const struct vec4 *v)
  449. {
  450. /* TODO */
  451. #pragma message ("TODO: implement gs_color4v")
  452. UNUSED_PARAMETER(v);
  453. }
  454. void gs_texcoord2v(const struct vec2 *v, int unit)
  455. {
  456. graphics_t graphics = thread_graphics;
  457. if (!validvertsize(graphics, graphics->texverts[unit].num,
  458. "gs_texcoord"))
  459. return;
  460. da_push_back(graphics->texverts[unit], v);
  461. }
  462. input_t gs_getinput(void)
  463. {
  464. /* TODO */
  465. #pragma message ("TODO: implement gs_getinput (hmm, not sure about input yet)")
  466. return NULL;
  467. }
  468. effect_t gs_geteffect(void)
  469. {
  470. return thread_graphics ? thread_graphics->cur_effect : NULL;
  471. }
  472. effect_t gs_create_effect_from_file(const char *file, char **error_string)
  473. {
  474. char *file_string;
  475. effect_t effect = NULL;
  476. if (!thread_graphics || !file)
  477. return NULL;
  478. file_string = os_quick_read_utf8_file(file);
  479. if (!file_string) {
  480. blog(LOG_WARNING, "Could not load effect file '%s'", file);
  481. return NULL;
  482. }
  483. effect = gs_create_effect(file_string, file, error_string);
  484. bfree(file_string);
  485. return effect;
  486. }
  487. effect_t gs_create_effect(const char *effect_string, const char *filename,
  488. char **error_string)
  489. {
  490. if (!thread_graphics || !effect_string)
  491. return NULL;
  492. struct gs_effect *effect = bzalloc(sizeof(struct gs_effect));
  493. struct effect_parser parser;
  494. bool success;
  495. effect->graphics = thread_graphics;
  496. ep_init(&parser);
  497. success = ep_parse(&parser, effect, effect_string, filename);
  498. if (!success) {
  499. if (error_string)
  500. *error_string = error_data_buildstring(
  501. &parser.cfp.error_list);
  502. effect_destroy(effect);
  503. effect = NULL;
  504. }
  505. ep_free(&parser);
  506. return effect;
  507. }
  508. shader_t gs_create_vertexshader_from_file(const char *file, char **error_string)
  509. {
  510. if (!thread_graphics || !file)
  511. return NULL;
  512. char *file_string;
  513. shader_t shader = NULL;
  514. file_string = os_quick_read_utf8_file(file);
  515. if (!file_string) {
  516. blog(LOG_WARNING, "Could not load vertex shader file '%s'",
  517. file);
  518. return NULL;
  519. }
  520. shader = gs_create_vertexshader(file_string, file, error_string);
  521. bfree(file_string);
  522. return shader;
  523. }
  524. shader_t gs_create_pixelshader_from_file(const char *file, char **error_string)
  525. {
  526. char *file_string;
  527. shader_t shader = NULL;
  528. if (!thread_graphics || !file)
  529. return NULL;
  530. file_string = os_quick_read_utf8_file(file);
  531. if (!file_string) {
  532. blog(LOG_WARNING, "Could not load pixel shader file '%s'",
  533. file);
  534. return NULL;
  535. }
  536. shader = gs_create_pixelshader(file_string, file, error_string);
  537. bfree(file_string);
  538. return shader;
  539. }
  540. texture_t gs_create_texture_from_file(const char *file, uint32_t flags)
  541. {
  542. /* TODO */
  543. #pragma message ("TODO: implement gs_create_texture_from_file")
  544. UNUSED_PARAMETER(file);
  545. UNUSED_PARAMETER(flags);
  546. return NULL;
  547. }
  548. texture_t gs_create_cubetexture_from_file(const char *file, uint32_t flags)
  549. {
  550. /* TODO */
  551. #pragma message ("TODO: implement gs_create_cubetexture_from_file")
  552. UNUSED_PARAMETER(file);
  553. UNUSED_PARAMETER(flags);
  554. return NULL;
  555. }
  556. texture_t gs_create_volumetexture_from_file(const char *file, uint32_t flags)
  557. {
  558. /* TODO */
  559. #pragma message ("TODO: implement gs_create_volumetexture_from_file")
  560. UNUSED_PARAMETER(file);
  561. UNUSED_PARAMETER(flags);
  562. return NULL;
  563. }
  564. static inline void assign_sprite_rect(float *start, float *end, float size,
  565. bool flip)
  566. {
  567. if (!flip) {
  568. *start = 0.0f;
  569. *end = size;
  570. } else {
  571. *start = size;
  572. *end = 0.0f;
  573. }
  574. }
  575. static inline void assign_sprite_uv(float *start, float *end, bool flip)
  576. {
  577. if (!flip) {
  578. *start = 0.0f;
  579. *end = 1.0f;
  580. } else {
  581. *start = 1.0f;
  582. *end = 0.0f;
  583. }
  584. }
  585. static void build_sprite(struct vb_data *data, float fcx, float fcy,
  586. float start_u, float end_u, float start_v, float end_v)
  587. {
  588. struct vec2 *tvarray = data->tvarray[0].array;
  589. vec3_zero(data->points);
  590. vec3_set(data->points+1, fcx, 0.0f, 0.0f);
  591. vec3_set(data->points+2, 0.0f, fcy, 0.0f);
  592. vec3_set(data->points+3, fcx, fcy, 0.0f);
  593. vec2_set(tvarray, start_u, start_v);
  594. vec2_set(tvarray+1, end_u, start_v);
  595. vec2_set(tvarray+2, start_u, end_v);
  596. vec2_set(tvarray+3, end_u, end_v);
  597. }
  598. static inline void build_sprite_norm(struct vb_data *data, float fcx, float fcy,
  599. uint32_t flip)
  600. {
  601. float start_u, end_u;
  602. float start_v, end_v;
  603. assign_sprite_uv(&start_u, &end_u, (flip & GS_FLIP_U) != 0);
  604. assign_sprite_uv(&start_v, &end_v, (flip & GS_FLIP_V) != 0);
  605. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
  606. }
  607. static inline void build_sprite_rect(struct vb_data *data, texture_t tex,
  608. float fcx, float fcy, uint32_t flip)
  609. {
  610. float start_u, end_u;
  611. float start_v, end_v;
  612. float width = (float)texture_getwidth(tex);
  613. float height = (float)texture_getheight(tex);
  614. assign_sprite_rect(&start_u, &end_u, width, (flip & GS_FLIP_U) != 0);
  615. assign_sprite_rect(&start_v, &end_v, height, (flip & GS_FLIP_V) != 0);
  616. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
  617. }
  618. void gs_draw_sprite(texture_t tex, uint32_t flip, uint32_t width,
  619. uint32_t height)
  620. {
  621. graphics_t graphics = thread_graphics;
  622. float fcx, fcy;
  623. struct vb_data *data;
  624. assert(tex);
  625. if (!tex || !thread_graphics)
  626. return;
  627. if (gs_gettexturetype(tex) != GS_TEXTURE_2D) {
  628. blog(LOG_ERROR, "A sprite must be a 2D texture");
  629. return;
  630. }
  631. fcx = width ? (float)width : (float)texture_getwidth(tex);
  632. fcy = height ? (float)height : (float)texture_getheight(tex);
  633. data = vertexbuffer_getdata(graphics->sprite_buffer);
  634. if (texture_isrect(tex))
  635. build_sprite_rect(data, tex, fcx, fcy, flip);
  636. else
  637. build_sprite_norm(data, fcx, fcy, flip);
  638. vertexbuffer_flush(graphics->sprite_buffer, false);
  639. gs_load_vertexbuffer(graphics->sprite_buffer);
  640. gs_load_indexbuffer(NULL);
  641. gs_draw(GS_TRISTRIP, 0, 0);
  642. }
  643. void gs_draw_cube_backdrop(texture_t cubetex, const struct quat *rot,
  644. float left, float right, float top, float bottom, float znear)
  645. {
  646. /* TODO */
  647. #pragma message ("TODO: implement gs_draw_cube_backdrop")
  648. UNUSED_PARAMETER(cubetex);
  649. UNUSED_PARAMETER(rot);
  650. UNUSED_PARAMETER(left);
  651. UNUSED_PARAMETER(right);
  652. UNUSED_PARAMETER(top);
  653. UNUSED_PARAMETER(bottom);
  654. UNUSED_PARAMETER(znear);
  655. }
  656. void gs_resetviewport(void)
  657. {
  658. uint32_t cx, cy;
  659. gs_getsize(&cx, &cy);
  660. gs_setviewport(0, 0, (int)cx, (int)cy);
  661. }
  662. void gs_set2dmode(void)
  663. {
  664. uint32_t cx, cy;
  665. gs_getsize(&cx, &cy);
  666. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -1.0, -1024.0f);
  667. }
  668. void gs_set3dmode(double fovy, double znear, double zvar)
  669. {
  670. /* TODO */
  671. #pragma message ("TODO: implement gs_set3dmode")
  672. UNUSED_PARAMETER(fovy);
  673. UNUSED_PARAMETER(znear);
  674. UNUSED_PARAMETER(zvar);
  675. }
  676. void gs_viewport_push(void)
  677. {
  678. if (!thread_graphics) return;
  679. struct gs_rect *rect = da_push_back_new(
  680. thread_graphics->viewport_stack);
  681. gs_getviewport(rect);
  682. }
  683. void gs_viewport_pop(void)
  684. {
  685. struct gs_rect *rect;
  686. if (!thread_graphics || !thread_graphics->viewport_stack.num)
  687. return;
  688. rect = da_end(thread_graphics->viewport_stack);
  689. gs_setviewport(rect->x, rect->y, rect->cx, rect->cy);
  690. da_pop_back(thread_graphics->viewport_stack);
  691. }
  692. void texture_setimage(texture_t tex, const void *data, uint32_t linesize,
  693. bool flip)
  694. {
  695. if (!thread_graphics || !tex)
  696. return;
  697. void *ptr;
  698. uint32_t linesize_out;
  699. uint32_t row_copy;
  700. int32_t height = (int32_t)texture_getheight(tex);
  701. int32_t y;
  702. if (!texture_map(tex, &ptr, &linesize_out))
  703. return;
  704. row_copy = (linesize < linesize_out) ? linesize : linesize_out;
  705. if (flip) {
  706. for (y = height-1; y >= 0; y--)
  707. memcpy((uint8_t*)ptr + (uint32_t)y * linesize_out,
  708. (uint8_t*)data + (uint32_t)y * linesize,
  709. row_copy);
  710. } else if (linesize == linesize_out) {
  711. memcpy(ptr, data, row_copy * height);
  712. } else {
  713. for (y = 0; y < height; y++)
  714. memcpy((uint8_t*)ptr + (uint32_t)y * linesize_out,
  715. (uint8_t*)data + (uint32_t)y * linesize,
  716. row_copy);
  717. }
  718. texture_unmap(tex);
  719. }
  720. void cubetexture_setimage(texture_t cubetex, uint32_t side, const void *data,
  721. uint32_t linesize, bool invert)
  722. {
  723. /* TODO */
  724. #pragma message ("TODO: implement cubetexture_setimage")
  725. UNUSED_PARAMETER(cubetex);
  726. UNUSED_PARAMETER(side);
  727. UNUSED_PARAMETER(data);
  728. UNUSED_PARAMETER(linesize);
  729. UNUSED_PARAMETER(invert);
  730. }
  731. void gs_perspective(float angle, float aspect, float near, float far)
  732. {
  733. graphics_t graphics = thread_graphics;
  734. float xmin, xmax, ymin, ymax;
  735. if (!graphics) return;
  736. ymax = near * tanf(RAD(angle)*0.5f);
  737. ymin = -ymax;
  738. xmin = ymin * aspect;
  739. xmax = ymax * aspect;
  740. graphics->exports.device_frustum(graphics->device, xmin, xmax,
  741. ymin, ymax, near, far);
  742. }
  743. /* ------------------------------------------------------------------------- */
  744. const char *gs_preprocessor_name(void)
  745. {
  746. graphics_t graphics = thread_graphics;
  747. if (!graphics) return NULL;
  748. return graphics->exports.device_preprocessor_name();
  749. }
  750. swapchain_t gs_create_swapchain(struct gs_init_data *data)
  751. {
  752. graphics_t graphics = thread_graphics;
  753. if (!graphics) return NULL;
  754. return graphics->exports.device_create_swapchain(graphics->device,
  755. data);
  756. }
  757. void gs_resize(uint32_t x, uint32_t y)
  758. {
  759. graphics_t graphics = thread_graphics;
  760. if (!graphics) return;
  761. graphics->exports.device_resize(graphics->device, x, y);
  762. }
  763. void gs_getsize(uint32_t *x, uint32_t *y)
  764. {
  765. graphics_t graphics = thread_graphics;
  766. if (!graphics) return;
  767. graphics->exports.device_getsize(graphics->device, x, y);
  768. }
  769. uint32_t gs_getwidth(void)
  770. {
  771. graphics_t graphics = thread_graphics;
  772. if (!graphics) return 0;
  773. return graphics->exports.device_getwidth(graphics->device);
  774. }
  775. uint32_t gs_getheight(void)
  776. {
  777. graphics_t graphics = thread_graphics;
  778. if (!graphics) return 0;
  779. return graphics->exports.device_getheight(graphics->device);
  780. }
  781. static inline bool is_pow2(uint32_t size)
  782. {
  783. return size >= 2 && (size & (size-1)) == 0;
  784. }
  785. texture_t gs_create_texture(uint32_t width, uint32_t height,
  786. enum gs_color_format color_format, uint32_t levels,
  787. const void **data, uint32_t flags)
  788. {
  789. graphics_t graphics = thread_graphics;
  790. bool pow2tex = is_pow2(width) && is_pow2(height);
  791. bool uses_mipmaps = (flags & GS_BUILDMIPMAPS || levels != 1);
  792. if (!graphics)
  793. return NULL;
  794. if (uses_mipmaps && !pow2tex) {
  795. blog(LOG_WARNING, "Cannot use mipmaps with a "
  796. "non-power-of-two texture. Disabling "
  797. "mipmaps for this texture.");
  798. uses_mipmaps = false;
  799. flags &= ~GS_BUILDMIPMAPS;
  800. levels = 1;
  801. }
  802. if (uses_mipmaps && flags & GS_RENDERTARGET) {
  803. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  804. "Disabling mipmaps for this texture.");
  805. flags &= ~GS_BUILDMIPMAPS;
  806. levels = 1;
  807. }
  808. return graphics->exports.device_create_texture(graphics->device,
  809. width, height, color_format, levels, data, flags);
  810. }
  811. texture_t gs_create_cubetexture(uint32_t size,
  812. enum gs_color_format color_format, uint32_t levels,
  813. const void **data, uint32_t flags)
  814. {
  815. graphics_t graphics = thread_graphics;
  816. bool pow2tex = is_pow2(size);
  817. bool uses_mipmaps = (flags & GS_BUILDMIPMAPS || levels != 1);
  818. if (!graphics)
  819. return NULL;
  820. if (uses_mipmaps && !pow2tex) {
  821. blog(LOG_WARNING, "Cannot use mipmaps with a "
  822. "non-power-of-two texture. Disabling "
  823. "mipmaps for this texture.");
  824. uses_mipmaps = false;
  825. flags &= ~GS_BUILDMIPMAPS;
  826. levels = 1;
  827. }
  828. if (uses_mipmaps && flags & GS_RENDERTARGET) {
  829. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  830. "Disabling mipmaps for this texture.");
  831. flags &= ~GS_BUILDMIPMAPS;
  832. levels = 1;
  833. data = NULL;
  834. }
  835. return graphics->exports.device_create_cubetexture(graphics->device,
  836. size, color_format, levels, data, flags);
  837. }
  838. texture_t gs_create_volumetexture(uint32_t width, uint32_t height,
  839. uint32_t depth, enum gs_color_format color_format,
  840. uint32_t levels, const void **data, uint32_t flags)
  841. {
  842. graphics_t graphics = thread_graphics;
  843. if (!graphics) return NULL;
  844. return graphics->exports.device_create_volumetexture(graphics->device,
  845. width, height, depth, color_format, levels, data,
  846. flags);
  847. }
  848. zstencil_t gs_create_zstencil(uint32_t width, uint32_t height,
  849. enum gs_zstencil_format format)
  850. {
  851. graphics_t graphics = thread_graphics;
  852. if (!graphics) return NULL;
  853. return graphics->exports.device_create_zstencil(graphics->device,
  854. width, height, format);
  855. }
  856. stagesurf_t gs_create_stagesurface(uint32_t width, uint32_t height,
  857. enum gs_color_format color_format)
  858. {
  859. graphics_t graphics = thread_graphics;
  860. if (!graphics) return NULL;
  861. return graphics->exports.device_create_stagesurface(graphics->device,
  862. width, height, color_format);
  863. }
  864. samplerstate_t gs_create_samplerstate(struct gs_sampler_info *info)
  865. {
  866. graphics_t graphics = thread_graphics;
  867. if (!graphics) return NULL;
  868. return graphics->exports.device_create_samplerstate(graphics->device,
  869. info);
  870. }
  871. shader_t gs_create_vertexshader(const char *shader, const char *file,
  872. char **error_string)
  873. {
  874. graphics_t graphics = thread_graphics;
  875. if (!graphics) return NULL;
  876. return graphics->exports.device_create_vertexshader(graphics->device,
  877. shader, file, error_string);
  878. }
  879. shader_t gs_create_pixelshader(const char *shader,
  880. const char *file, char **error_string)
  881. {
  882. graphics_t graphics = thread_graphics;
  883. if (!graphics) return NULL;
  884. return graphics->exports.device_create_pixelshader(graphics->device,
  885. shader, file, error_string);
  886. }
  887. vertbuffer_t gs_create_vertexbuffer(struct vb_data *data,
  888. uint32_t flags)
  889. {
  890. graphics_t graphics = thread_graphics;
  891. if (!graphics) return NULL;
  892. return graphics->exports.device_create_vertexbuffer(graphics->device,
  893. data, flags);
  894. }
  895. indexbuffer_t gs_create_indexbuffer(enum gs_index_type type,
  896. void *indices, size_t num, uint32_t flags)
  897. {
  898. graphics_t graphics = thread_graphics;
  899. if (!graphics) return NULL;
  900. return graphics->exports.device_create_indexbuffer(graphics->device,
  901. type, indices, num, flags);
  902. }
  903. enum gs_texture_type gs_gettexturetype(texture_t texture)
  904. {
  905. graphics_t graphics = thread_graphics;
  906. if (!graphics) return GS_TEXTURE_2D;
  907. return graphics->exports.device_gettexturetype(texture);
  908. }
  909. void gs_load_vertexbuffer(vertbuffer_t vertbuffer)
  910. {
  911. graphics_t graphics = thread_graphics;
  912. if (!graphics) return;
  913. graphics->exports.device_load_vertexbuffer(graphics->device,
  914. vertbuffer);
  915. }
  916. void gs_load_indexbuffer(indexbuffer_t indexbuffer)
  917. {
  918. graphics_t graphics = thread_graphics;
  919. if (!graphics) return;
  920. graphics->exports.device_load_indexbuffer(graphics->device,
  921. indexbuffer);
  922. }
  923. void gs_load_texture(texture_t tex, int unit)
  924. {
  925. graphics_t graphics = thread_graphics;
  926. if (!graphics) return;
  927. graphics->exports.device_load_texture(graphics->device, tex, unit);
  928. }
  929. void gs_load_samplerstate(samplerstate_t samplerstate, int unit)
  930. {
  931. graphics_t graphics = thread_graphics;
  932. if (!graphics) return;
  933. graphics->exports.device_load_samplerstate(graphics->device,
  934. samplerstate, unit);
  935. }
  936. void gs_load_vertexshader(shader_t vertshader)
  937. {
  938. graphics_t graphics = thread_graphics;
  939. if (!graphics) return;
  940. graphics->exports.device_load_vertexshader(graphics->device,
  941. vertshader);
  942. }
  943. void gs_load_pixelshader(shader_t pixelshader)
  944. {
  945. graphics_t graphics = thread_graphics;
  946. if (!graphics) return;
  947. graphics->exports.device_load_pixelshader(graphics->device,
  948. pixelshader);
  949. }
  950. void gs_load_defaultsamplerstate(bool b_3d, int unit)
  951. {
  952. graphics_t graphics = thread_graphics;
  953. if (!graphics) return;
  954. graphics->exports.device_load_defaultsamplerstate(graphics->device,
  955. b_3d, unit);
  956. }
  957. shader_t gs_getvertexshader(void)
  958. {
  959. graphics_t graphics = thread_graphics;
  960. if (!graphics) return NULL;
  961. return graphics->exports.device_getvertexshader(graphics->device);
  962. }
  963. shader_t gs_getpixelshader(void)
  964. {
  965. graphics_t graphics = thread_graphics;
  966. if (!graphics) return NULL;
  967. return graphics->exports.device_getpixelshader(graphics->device);
  968. }
  969. texture_t gs_getrendertarget(void)
  970. {
  971. graphics_t graphics = thread_graphics;
  972. if (!graphics) return NULL;
  973. return graphics->exports.device_getrendertarget(graphics->device);
  974. }
  975. zstencil_t gs_getzstenciltarget(void)
  976. {
  977. graphics_t graphics = thread_graphics;
  978. if (!graphics) return NULL;
  979. return graphics->exports.device_getzstenciltarget(graphics->device);
  980. }
  981. void gs_setrendertarget(texture_t tex, zstencil_t zstencil)
  982. {
  983. graphics_t graphics = thread_graphics;
  984. if (!graphics) return;
  985. graphics->exports.device_setrendertarget(graphics->device, tex,
  986. zstencil);
  987. }
  988. void gs_setcuberendertarget(texture_t cubetex, int side, zstencil_t zstencil)
  989. {
  990. graphics_t graphics = thread_graphics;
  991. if (!graphics) return;
  992. graphics->exports.device_setcuberendertarget(graphics->device, cubetex,
  993. side, zstencil);
  994. }
  995. void gs_copy_texture(texture_t dst, texture_t src)
  996. {
  997. graphics_t graphics = thread_graphics;
  998. if (!graphics) return;
  999. graphics->exports.device_copy_texture(graphics->device, dst, src);
  1000. }
  1001. void gs_stage_texture(stagesurf_t dst, texture_t src)
  1002. {
  1003. graphics_t graphics = thread_graphics;
  1004. if (!graphics) return;
  1005. graphics->exports.device_stage_texture(graphics->device, dst, src);
  1006. }
  1007. void gs_beginscene(void)
  1008. {
  1009. graphics_t graphics = thread_graphics;
  1010. if (!graphics) return;
  1011. graphics->exports.device_beginscene(graphics->device);
  1012. }
  1013. void gs_draw(enum gs_draw_mode draw_mode, uint32_t start_vert,
  1014. uint32_t num_verts)
  1015. {
  1016. graphics_t graphics = thread_graphics;
  1017. if (!graphics) return;
  1018. graphics->exports.device_draw(graphics->device, draw_mode,
  1019. start_vert, num_verts);
  1020. }
  1021. void gs_endscene(void)
  1022. {
  1023. graphics_t graphics = thread_graphics;
  1024. if (!graphics) return;
  1025. graphics->exports.device_endscene(graphics->device);
  1026. }
  1027. void gs_load_swapchain(swapchain_t swapchain)
  1028. {
  1029. graphics_t graphics = thread_graphics;
  1030. if (!graphics) return;
  1031. graphics->exports.device_load_swapchain(graphics->device, swapchain);
  1032. }
  1033. void gs_clear(uint32_t clear_flags, struct vec4 *color, float depth,
  1034. uint8_t stencil)
  1035. {
  1036. graphics_t graphics = thread_graphics;
  1037. graphics->exports.device_clear(graphics->device, clear_flags, color,
  1038. depth, stencil);
  1039. }
  1040. void gs_present(void)
  1041. {
  1042. graphics_t graphics = thread_graphics;
  1043. if (!graphics) return;
  1044. graphics->exports.device_present(graphics->device);
  1045. }
  1046. void gs_setcullmode(enum gs_cull_mode mode)
  1047. {
  1048. graphics_t graphics = thread_graphics;
  1049. if (!graphics) return;
  1050. graphics->exports.device_setcullmode(graphics->device, mode);
  1051. }
  1052. enum gs_cull_mode gs_getcullmode(void)
  1053. {
  1054. graphics_t graphics = thread_graphics;
  1055. if (!graphics) return GS_NEITHER;
  1056. return graphics->exports.device_getcullmode(graphics->device);
  1057. }
  1058. void gs_enable_blending(bool enable)
  1059. {
  1060. graphics_t graphics = thread_graphics;
  1061. if (!graphics) return;
  1062. graphics->exports.device_enable_blending(graphics->device, enable);
  1063. }
  1064. void gs_enable_depthtest(bool enable)
  1065. {
  1066. graphics_t graphics = thread_graphics;
  1067. if (!graphics) return;
  1068. graphics->exports.device_enable_depthtest(graphics->device, enable);
  1069. }
  1070. void gs_enable_stenciltest(bool enable)
  1071. {
  1072. graphics_t graphics = thread_graphics;
  1073. if (!graphics) return;
  1074. graphics->exports.device_enable_stenciltest(graphics->device, enable);
  1075. }
  1076. void gs_enable_stencilwrite(bool enable)
  1077. {
  1078. graphics_t graphics = thread_graphics;
  1079. if (!graphics) return;
  1080. graphics->exports.device_enable_stencilwrite(graphics->device, enable);
  1081. }
  1082. void gs_enable_color(bool red, bool green, bool blue, bool alpha)
  1083. {
  1084. graphics_t graphics = thread_graphics;
  1085. if (!graphics) return;
  1086. graphics->exports.device_enable_color(graphics->device, red, green,
  1087. blue, alpha);
  1088. }
  1089. void gs_blendfunction(enum gs_blend_type src, enum gs_blend_type dest)
  1090. {
  1091. graphics_t graphics = thread_graphics;
  1092. if (!graphics) return;
  1093. graphics->exports.device_blendfunction(graphics->device, src, dest);
  1094. }
  1095. void gs_depthfunction(enum gs_depth_test test)
  1096. {
  1097. graphics_t graphics = thread_graphics;
  1098. if (!graphics) return;
  1099. graphics->exports.device_depthfunction(graphics->device, test);
  1100. }
  1101. void gs_stencilfunction(enum gs_stencil_side side, enum gs_depth_test test)
  1102. {
  1103. graphics_t graphics = thread_graphics;
  1104. if (!graphics) return;
  1105. graphics->exports.device_stencilfunction(graphics->device, side, test);
  1106. }
  1107. void gs_stencilop(enum gs_stencil_side side, enum gs_stencil_op fail,
  1108. enum gs_stencil_op zfail, enum gs_stencil_op zpass)
  1109. {
  1110. graphics_t graphics = thread_graphics;
  1111. if (!graphics) return;
  1112. graphics->exports.device_stencilop(graphics->device, side, fail, zfail,
  1113. zpass);
  1114. }
  1115. void gs_enable_fullscreen(bool enable)
  1116. {
  1117. graphics_t graphics = thread_graphics;
  1118. if (!graphics) return;
  1119. graphics->exports.device_enable_fullscreen(graphics->device, enable);
  1120. }
  1121. int gs_fullscreen_enabled(void)
  1122. {
  1123. graphics_t graphics = thread_graphics;
  1124. if (!graphics) return 0;
  1125. return graphics->exports.device_fullscreen_enabled(graphics->device);
  1126. }
  1127. void gs_setdisplaymode(const struct gs_display_mode *mode)
  1128. {
  1129. graphics_t graphics = thread_graphics;
  1130. if (!graphics) return;
  1131. graphics->exports.device_setdisplaymode(graphics->device, mode);
  1132. }
  1133. void gs_getdisplaymode(struct gs_display_mode *mode)
  1134. {
  1135. graphics_t graphics = thread_graphics;
  1136. if (!graphics) return;
  1137. graphics->exports.device_getdisplaymode(graphics->device, mode);
  1138. }
  1139. void gs_setcolorramp(float gamma, float brightness, float contrast)
  1140. {
  1141. graphics_t graphics = thread_graphics;
  1142. if (!graphics) return;
  1143. graphics->exports.device_setcolorramp(graphics->device, gamma,
  1144. brightness, contrast);
  1145. }
  1146. void gs_setviewport(int x, int y, int width, int height)
  1147. {
  1148. graphics_t graphics = thread_graphics;
  1149. if (!graphics) return;
  1150. graphics->exports.device_setviewport(graphics->device, x, y, width,
  1151. height);
  1152. }
  1153. void gs_getviewport(struct gs_rect *rect)
  1154. {
  1155. graphics_t graphics = thread_graphics;
  1156. if (!graphics) return;
  1157. graphics->exports.device_getviewport(graphics->device, rect);
  1158. }
  1159. void gs_setscissorrect(struct gs_rect *rect)
  1160. {
  1161. graphics_t graphics = thread_graphics;
  1162. if (!graphics) return;
  1163. graphics->exports.device_setscissorrect(graphics->device, rect);
  1164. }
  1165. void gs_ortho(float left, float right, float top, float bottom, float znear,
  1166. float zfar)
  1167. {
  1168. graphics_t graphics = thread_graphics;
  1169. if (!graphics) return;
  1170. graphics->exports.device_ortho(graphics->device, left, right, top,
  1171. bottom, znear, zfar);
  1172. }
  1173. void gs_frustum(float left, float right, float top, float bottom, float znear,
  1174. float zfar)
  1175. {
  1176. graphics_t graphics = thread_graphics;
  1177. if (!graphics) return;
  1178. graphics->exports.device_frustum(graphics->device, left, right, top,
  1179. bottom, znear, zfar);
  1180. }
  1181. void gs_projection_push(void)
  1182. {
  1183. graphics_t graphics = thread_graphics;
  1184. if (!graphics) return;
  1185. graphics->exports.device_projection_push(graphics->device);
  1186. }
  1187. void gs_projection_pop(void)
  1188. {
  1189. graphics_t graphics = thread_graphics;
  1190. if (!graphics) return;
  1191. graphics->exports.device_projection_pop(graphics->device);
  1192. }
  1193. void swapchain_destroy(swapchain_t swapchain)
  1194. {
  1195. graphics_t graphics = thread_graphics;
  1196. if (!graphics || !swapchain) return;
  1197. graphics->exports.swapchain_destroy(swapchain);
  1198. }
  1199. void shader_destroy(shader_t shader)
  1200. {
  1201. graphics_t graphics = thread_graphics;
  1202. if (!graphics || !shader) return;
  1203. graphics->exports.shader_destroy(shader);
  1204. }
  1205. int shader_numparams(shader_t shader)
  1206. {
  1207. graphics_t graphics = thread_graphics;
  1208. if (!graphics || !shader) return 0;
  1209. return graphics->exports.shader_numparams(shader);
  1210. }
  1211. sparam_t shader_getparambyidx(shader_t shader, uint32_t param)
  1212. {
  1213. graphics_t graphics = thread_graphics;
  1214. if (!graphics || !shader) return NULL;
  1215. return graphics->exports.shader_getparambyidx(shader, param);
  1216. }
  1217. sparam_t shader_getparambyname(shader_t shader, const char *name)
  1218. {
  1219. graphics_t graphics = thread_graphics;
  1220. if (!graphics || !shader) return NULL;
  1221. return graphics->exports.shader_getparambyname(shader, name);
  1222. }
  1223. void shader_getparaminfo(shader_t shader, sparam_t param,
  1224. struct shader_param_info *info)
  1225. {
  1226. graphics_t graphics = thread_graphics;
  1227. if (!graphics || !shader || !param) return;
  1228. graphics->exports.shader_getparaminfo(shader, param, info);
  1229. }
  1230. sparam_t shader_getviewprojmatrix(shader_t shader)
  1231. {
  1232. graphics_t graphics = thread_graphics;
  1233. if (!graphics || !shader) return NULL;
  1234. return graphics->exports.shader_getviewprojmatrix(shader);
  1235. }
  1236. sparam_t shader_getworldmatrix(shader_t shader)
  1237. {
  1238. graphics_t graphics = thread_graphics;
  1239. if (!graphics || !shader) return NULL;
  1240. return graphics->exports.shader_getworldmatrix(shader);
  1241. }
  1242. void shader_setbool(shader_t shader, sparam_t param, bool val)
  1243. {
  1244. graphics_t graphics = thread_graphics;
  1245. if (!graphics || !shader || !param) return;
  1246. graphics->exports.shader_setbool(shader, param, val);
  1247. }
  1248. void shader_setfloat(shader_t shader, sparam_t param, float val)
  1249. {
  1250. graphics_t graphics = thread_graphics;
  1251. if (!graphics || !shader || !param) return;
  1252. graphics->exports.shader_setfloat(shader, param, val);
  1253. }
  1254. void shader_setint(shader_t shader, sparam_t param, int val)
  1255. {
  1256. graphics_t graphics = thread_graphics;
  1257. if (!graphics || !shader || !param) return;
  1258. graphics->exports.shader_setint(shader, param, val);
  1259. }
  1260. void shader_setmatrix3(shader_t shader, sparam_t param,
  1261. const struct matrix3 *val)
  1262. {
  1263. graphics_t graphics = thread_graphics;
  1264. if (!graphics || !shader || !param) return;
  1265. graphics->exports.shader_setmatrix3(shader, param, val);
  1266. }
  1267. void shader_setmatrix4(shader_t shader, sparam_t param,
  1268. const struct matrix4 *val)
  1269. {
  1270. graphics_t graphics = thread_graphics;
  1271. if (!graphics || !shader || !param) return;
  1272. graphics->exports.shader_setmatrix4(shader, param, val);
  1273. }
  1274. void shader_setvec2(shader_t shader, sparam_t param,
  1275. const struct vec2 *val)
  1276. {
  1277. graphics_t graphics = thread_graphics;
  1278. if (!graphics || !shader || !param) return;
  1279. graphics->exports.shader_setvec2(shader, param, val);
  1280. }
  1281. void shader_setvec3(shader_t shader, sparam_t param,
  1282. const struct vec3 *val)
  1283. {
  1284. graphics_t graphics = thread_graphics;
  1285. if (!graphics || !shader || !param) return;
  1286. graphics->exports.shader_setvec3(shader, param, val);
  1287. }
  1288. void shader_setvec4(shader_t shader, sparam_t param,
  1289. const struct vec4 *val)
  1290. {
  1291. graphics_t graphics = thread_graphics;
  1292. if (!graphics || !shader || !param) return;
  1293. graphics->exports.shader_setvec4(shader, param, val);
  1294. }
  1295. void shader_settexture(shader_t shader, sparam_t param, texture_t val)
  1296. {
  1297. graphics_t graphics = thread_graphics;
  1298. if (!graphics || !shader || !param) return;
  1299. graphics->exports.shader_settexture(shader, param, val);
  1300. }
  1301. void shader_setval(shader_t shader, sparam_t param, const void *val,
  1302. size_t size)
  1303. {
  1304. graphics_t graphics = thread_graphics;
  1305. if (!graphics || !shader || !param) return;
  1306. graphics->exports.shader_setval(shader, param, val, size);
  1307. }
  1308. void shader_setdefault(shader_t shader, sparam_t param)
  1309. {
  1310. graphics_t graphics = thread_graphics;
  1311. if (!graphics || !shader || !param) return;
  1312. graphics->exports.shader_setdefault(shader, param);
  1313. }
  1314. void texture_destroy(texture_t tex)
  1315. {
  1316. graphics_t graphics = thread_graphics;
  1317. if (!graphics || !tex) return;
  1318. graphics->exports.texture_destroy(tex);
  1319. }
  1320. uint32_t texture_getwidth(texture_t tex)
  1321. {
  1322. graphics_t graphics = thread_graphics;
  1323. if (!graphics || !tex) return 0;
  1324. return graphics->exports.texture_getwidth(tex);
  1325. }
  1326. uint32_t texture_getheight(texture_t tex)
  1327. {
  1328. graphics_t graphics = thread_graphics;
  1329. if (!graphics || !tex) return 0;
  1330. return graphics->exports.texture_getheight(tex);
  1331. }
  1332. enum gs_color_format texture_getcolorformat(texture_t tex)
  1333. {
  1334. graphics_t graphics = thread_graphics;
  1335. if (!graphics || !tex) return GS_UNKNOWN;
  1336. return graphics->exports.texture_getcolorformat(tex);
  1337. }
  1338. bool texture_map(texture_t tex, void **ptr, uint32_t *linesize)
  1339. {
  1340. graphics_t graphics = thread_graphics;
  1341. if (!graphics || !tex) return false;
  1342. return graphics->exports.texture_map(tex, ptr, linesize);
  1343. }
  1344. void texture_unmap(texture_t tex)
  1345. {
  1346. graphics_t graphics = thread_graphics;
  1347. if (!graphics || !tex) return;
  1348. graphics->exports.texture_unmap(tex);
  1349. }
  1350. bool texture_isrect(texture_t tex)
  1351. {
  1352. graphics_t graphics = thread_graphics;
  1353. if (!graphics || !tex) return false;
  1354. if (graphics->exports.texture_isrect)
  1355. return graphics->exports.texture_isrect(tex);
  1356. else
  1357. return false;
  1358. }
  1359. void cubetexture_destroy(texture_t cubetex)
  1360. {
  1361. graphics_t graphics = thread_graphics;
  1362. if (!graphics || !cubetex) return;
  1363. graphics->exports.cubetexture_destroy(cubetex);
  1364. }
  1365. uint32_t cubetexture_getsize(texture_t cubetex)
  1366. {
  1367. graphics_t graphics = thread_graphics;
  1368. if (!graphics || !cubetex) return 0;
  1369. return graphics->exports.cubetexture_getsize(cubetex);
  1370. }
  1371. enum gs_color_format cubetexture_getcolorformat(texture_t cubetex)
  1372. {
  1373. graphics_t graphics = thread_graphics;
  1374. if (!graphics || !cubetex) return GS_UNKNOWN;
  1375. return graphics->exports.cubetexture_getcolorformat(cubetex);
  1376. }
  1377. void volumetexture_destroy(texture_t voltex)
  1378. {
  1379. graphics_t graphics = thread_graphics;
  1380. if (!graphics || !voltex) return;
  1381. graphics->exports.volumetexture_destroy(voltex);
  1382. }
  1383. uint32_t volumetexture_getwidth(texture_t voltex)
  1384. {
  1385. graphics_t graphics = thread_graphics;
  1386. if (!graphics || !voltex) return 0;
  1387. return graphics->exports.volumetexture_getwidth(voltex);
  1388. }
  1389. uint32_t volumetexture_getheight(texture_t voltex)
  1390. {
  1391. graphics_t graphics = thread_graphics;
  1392. if (!graphics || !voltex) return 0;
  1393. return graphics->exports.volumetexture_getheight(voltex);
  1394. }
  1395. uint32_t volumetexture_getdepth(texture_t voltex)
  1396. {
  1397. graphics_t graphics = thread_graphics;
  1398. if (!graphics || !voltex) return 0;
  1399. return graphics->exports.volumetexture_getdepth(voltex);
  1400. }
  1401. enum gs_color_format volumetexture_getcolorformat(texture_t voltex)
  1402. {
  1403. graphics_t graphics = thread_graphics;
  1404. if (!graphics || !voltex) return GS_UNKNOWN;
  1405. return graphics->exports.volumetexture_getcolorformat(voltex);
  1406. }
  1407. void stagesurface_destroy(stagesurf_t stagesurf)
  1408. {
  1409. graphics_t graphics = thread_graphics;
  1410. if (!graphics || !stagesurf) return;
  1411. graphics->exports.stagesurface_destroy(stagesurf);
  1412. }
  1413. uint32_t stagesurface_getwidth(stagesurf_t stagesurf)
  1414. {
  1415. graphics_t graphics = thread_graphics;
  1416. if (!graphics || !stagesurf) return 0;
  1417. return graphics->exports.stagesurface_getwidth(stagesurf);
  1418. }
  1419. uint32_t stagesurface_getheight(stagesurf_t stagesurf)
  1420. {
  1421. graphics_t graphics = thread_graphics;
  1422. if (!graphics || !stagesurf) return 0;
  1423. return graphics->exports.stagesurface_getheight(stagesurf);
  1424. }
  1425. enum gs_color_format stagesurface_getcolorformat(stagesurf_t stagesurf)
  1426. {
  1427. graphics_t graphics = thread_graphics;
  1428. if (!graphics || !stagesurf) return GS_UNKNOWN;
  1429. return graphics->exports.stagesurface_getcolorformat(stagesurf);
  1430. }
  1431. bool stagesurface_map(stagesurf_t stagesurf, const uint8_t **data,
  1432. uint32_t *linesize)
  1433. {
  1434. graphics_t graphics = thread_graphics;
  1435. if (!graphics || !stagesurf) return false;
  1436. return graphics->exports.stagesurface_map(stagesurf, data, linesize);
  1437. }
  1438. void stagesurface_unmap(stagesurf_t stagesurf)
  1439. {
  1440. graphics_t graphics = thread_graphics;
  1441. if (!graphics || !stagesurf) return;
  1442. graphics->exports.stagesurface_unmap(stagesurf);
  1443. }
  1444. void zstencil_destroy(zstencil_t zstencil)
  1445. {
  1446. if (!thread_graphics || !zstencil) return;
  1447. thread_graphics->exports.zstencil_destroy(zstencil);
  1448. }
  1449. void samplerstate_destroy(samplerstate_t samplerstate)
  1450. {
  1451. if (!thread_graphics || !samplerstate) return;
  1452. thread_graphics->exports.samplerstate_destroy(samplerstate);
  1453. }
  1454. void vertexbuffer_destroy(vertbuffer_t vertbuffer)
  1455. {
  1456. graphics_t graphics = thread_graphics;
  1457. if (!graphics || !vertbuffer) return;
  1458. graphics->exports.vertexbuffer_destroy(vertbuffer);
  1459. }
  1460. void vertexbuffer_flush(vertbuffer_t vertbuffer, bool rebuild)
  1461. {
  1462. if (!thread_graphics || !vertbuffer) return;
  1463. thread_graphics->exports.vertexbuffer_flush(vertbuffer, rebuild);
  1464. }
  1465. struct vb_data *vertexbuffer_getdata(vertbuffer_t vertbuffer)
  1466. {
  1467. if (!thread_graphics || !vertbuffer) return NULL;
  1468. return thread_graphics->exports.vertexbuffer_getdata(vertbuffer);
  1469. }
  1470. void indexbuffer_destroy(indexbuffer_t indexbuffer)
  1471. {
  1472. graphics_t graphics = thread_graphics;
  1473. if (!graphics || !indexbuffer) return;
  1474. graphics->exports.indexbuffer_destroy(indexbuffer);
  1475. }
  1476. void indexbuffer_flush(indexbuffer_t indexbuffer)
  1477. {
  1478. if (!thread_graphics || !indexbuffer) return;
  1479. thread_graphics->exports.indexbuffer_flush(indexbuffer);
  1480. }
  1481. void *indexbuffer_getdata(indexbuffer_t indexbuffer)
  1482. {
  1483. if (!thread_graphics || !indexbuffer) return NULL;
  1484. return thread_graphics->exports.indexbuffer_getdata(indexbuffer);
  1485. }
  1486. size_t indexbuffer_numindices(indexbuffer_t indexbuffer)
  1487. {
  1488. if (!thread_graphics || !indexbuffer) return 0;
  1489. return thread_graphics->exports.indexbuffer_numindices(indexbuffer);
  1490. }
  1491. enum gs_index_type indexbuffer_gettype(indexbuffer_t indexbuffer)
  1492. {
  1493. if (!thread_graphics || !indexbuffer) return (enum gs_index_type)0;
  1494. return thread_graphics->exports.indexbuffer_gettype(indexbuffer);
  1495. }
  1496. /** Platform specific functions */
  1497. texture_t gs_create_texture_from_iosurface(void *iosurf)
  1498. {
  1499. graphics_t graphics = thread_graphics;
  1500. if (!graphics || !iosurf ||
  1501. !graphics->exports.texture_create_from_iosurface)
  1502. return NULL;
  1503. return graphics->exports.texture_create_from_iosurface(
  1504. graphics->device, iosurf);
  1505. }
  1506. bool texture_rebind_iosurface(texture_t texture, void *iosurf)
  1507. {
  1508. graphics_t graphics = thread_graphics;
  1509. if (!graphics || !iosurf || !graphics->exports.texture_rebind_iosurface)
  1510. return false;
  1511. return graphics->exports.texture_rebind_iosurface(texture, iosurf);
  1512. }