graphics.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  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_ERROR, "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_ERROR, "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_ERROR, "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_ERROR, "%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. UNUSED_PARAMETER(v);
  452. }
  453. void gs_texcoord2v(const struct vec2 *v, int unit)
  454. {
  455. graphics_t graphics = thread_graphics;
  456. if (!validvertsize(graphics, graphics->texverts[unit].num,
  457. "gs_texcoord"))
  458. return;
  459. da_push_back(graphics->texverts[unit], v);
  460. }
  461. input_t gs_getinput(void)
  462. {
  463. /* TODO */
  464. return NULL;
  465. }
  466. effect_t gs_geteffect(void)
  467. {
  468. return thread_graphics ? thread_graphics->cur_effect : NULL;
  469. }
  470. effect_t gs_create_effect_from_file(const char *file, char **error_string)
  471. {
  472. char *file_string;
  473. effect_t effect = NULL;
  474. if (!thread_graphics || !file)
  475. return NULL;
  476. file_string = os_quick_read_utf8_file(file);
  477. if (!file_string) {
  478. blog(LOG_ERROR, "Could not load effect file '%s'", file);
  479. return NULL;
  480. }
  481. effect = gs_create_effect(file_string, file, error_string);
  482. bfree(file_string);
  483. return effect;
  484. }
  485. effect_t gs_create_effect(const char *effect_string, const char *filename,
  486. char **error_string)
  487. {
  488. if (!thread_graphics || !effect_string)
  489. return NULL;
  490. struct gs_effect *effect = bzalloc(sizeof(struct gs_effect));
  491. struct effect_parser parser;
  492. bool success;
  493. effect->graphics = thread_graphics;
  494. ep_init(&parser);
  495. success = ep_parse(&parser, effect, effect_string, filename);
  496. if (!success) {
  497. if (error_string)
  498. *error_string = error_data_buildstring(
  499. &parser.cfp.error_list);
  500. effect_destroy(effect);
  501. effect = NULL;
  502. }
  503. ep_free(&parser);
  504. return effect;
  505. }
  506. shader_t gs_create_vertexshader_from_file(const char *file, char **error_string)
  507. {
  508. if (!thread_graphics || !file)
  509. return NULL;
  510. char *file_string;
  511. shader_t shader = NULL;
  512. file_string = os_quick_read_utf8_file(file);
  513. if (!file_string) {
  514. blog(LOG_ERROR, "Could not load vertex shader file '%s'",
  515. file);
  516. return NULL;
  517. }
  518. shader = gs_create_vertexshader(file_string, file, error_string);
  519. bfree(file_string);
  520. return shader;
  521. }
  522. shader_t gs_create_pixelshader_from_file(const char *file, char **error_string)
  523. {
  524. char *file_string;
  525. shader_t shader = NULL;
  526. if (!thread_graphics || !file)
  527. return NULL;
  528. file_string = os_quick_read_utf8_file(file);
  529. if (!file_string) {
  530. blog(LOG_ERROR, "Could not load pixel shader file '%s'",
  531. file);
  532. return NULL;
  533. }
  534. shader = gs_create_pixelshader(file_string, file, error_string);
  535. bfree(file_string);
  536. return shader;
  537. }
  538. texture_t gs_create_texture_from_file(const char *file, uint32_t flags)
  539. {
  540. /* TODO */
  541. UNUSED_PARAMETER(file);
  542. UNUSED_PARAMETER(flags);
  543. return NULL;
  544. }
  545. texture_t gs_create_cubetexture_from_file(const char *file, uint32_t flags)
  546. {
  547. /* TODO */
  548. UNUSED_PARAMETER(file);
  549. UNUSED_PARAMETER(flags);
  550. return NULL;
  551. }
  552. texture_t gs_create_volumetexture_from_file(const char *file, uint32_t flags)
  553. {
  554. /* TODO */
  555. UNUSED_PARAMETER(file);
  556. UNUSED_PARAMETER(flags);
  557. return NULL;
  558. }
  559. static inline void assign_sprite_rect(float *start, float *end, float size,
  560. bool flip)
  561. {
  562. if (!flip) {
  563. *start = 0.0f;
  564. *end = size;
  565. } else {
  566. *start = size;
  567. *end = 0.0f;
  568. }
  569. }
  570. static inline void assign_sprite_uv(float *start, float *end, bool flip)
  571. {
  572. if (!flip) {
  573. *start = 0.0f;
  574. *end = 1.0f;
  575. } else {
  576. *start = 1.0f;
  577. *end = 0.0f;
  578. }
  579. }
  580. static void build_sprite(struct vb_data *data, float fcx, float fcy,
  581. float start_u, float end_u, float start_v, float end_v)
  582. {
  583. struct vec2 *tvarray = data->tvarray[0].array;
  584. vec3_zero(data->points);
  585. vec3_set(data->points+1, fcx, 0.0f, 0.0f);
  586. vec3_set(data->points+2, 0.0f, fcy, 0.0f);
  587. vec3_set(data->points+3, fcx, fcy, 0.0f);
  588. vec2_set(tvarray, start_u, start_v);
  589. vec2_set(tvarray+1, end_u, start_v);
  590. vec2_set(tvarray+2, start_u, end_v);
  591. vec2_set(tvarray+3, end_u, end_v);
  592. }
  593. static inline void build_sprite_norm(struct vb_data *data, float fcx, float fcy,
  594. uint32_t flip)
  595. {
  596. float start_u, end_u;
  597. float start_v, end_v;
  598. assign_sprite_uv(&start_u, &end_u, (flip & GS_FLIP_U) != 0);
  599. assign_sprite_uv(&start_v, &end_v, (flip & GS_FLIP_V) != 0);
  600. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
  601. }
  602. static inline void build_sprite_rect(struct vb_data *data, texture_t tex,
  603. float fcx, float fcy, uint32_t flip)
  604. {
  605. float start_u, end_u;
  606. float start_v, end_v;
  607. float width = (float)texture_getwidth(tex);
  608. float height = (float)texture_getheight(tex);
  609. assign_sprite_rect(&start_u, &end_u, width, (flip & GS_FLIP_U) != 0);
  610. assign_sprite_rect(&start_v, &end_v, height, (flip & GS_FLIP_V) != 0);
  611. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
  612. }
  613. void gs_draw_sprite(texture_t tex, uint32_t flip, uint32_t width,
  614. uint32_t height)
  615. {
  616. graphics_t graphics = thread_graphics;
  617. float fcx, fcy;
  618. struct vb_data *data;
  619. assert(tex);
  620. if (!tex || !thread_graphics)
  621. return;
  622. if (gs_gettexturetype(tex) != GS_TEXTURE_2D) {
  623. blog(LOG_ERROR, "A sprite must be a 2D texture");
  624. return;
  625. }
  626. fcx = width ? (float)width : (float)texture_getwidth(tex);
  627. fcy = height ? (float)height : (float)texture_getheight(tex);
  628. data = vertexbuffer_getdata(graphics->sprite_buffer);
  629. if (texture_isrect(tex))
  630. build_sprite_rect(data, tex, fcx, fcy, flip);
  631. else
  632. build_sprite_norm(data, fcx, fcy, flip);
  633. vertexbuffer_flush(graphics->sprite_buffer, false);
  634. gs_load_vertexbuffer(graphics->sprite_buffer);
  635. gs_load_indexbuffer(NULL);
  636. gs_draw(GS_TRISTRIP, 0, 0);
  637. }
  638. void gs_draw_cube_backdrop(texture_t cubetex, const struct quat *rot,
  639. float left, float right, float top, float bottom, float znear)
  640. {
  641. /* TODO */
  642. UNUSED_PARAMETER(cubetex);
  643. UNUSED_PARAMETER(rot);
  644. UNUSED_PARAMETER(left);
  645. UNUSED_PARAMETER(right);
  646. UNUSED_PARAMETER(top);
  647. UNUSED_PARAMETER(bottom);
  648. UNUSED_PARAMETER(znear);
  649. }
  650. void gs_resetviewport(void)
  651. {
  652. uint32_t cx, cy;
  653. gs_getsize(&cx, &cy);
  654. gs_setviewport(0, 0, (int)cx, (int)cy);
  655. }
  656. void gs_set2dmode(void)
  657. {
  658. uint32_t cx, cy;
  659. gs_getsize(&cx, &cy);
  660. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -1.0, -1024.0f);
  661. }
  662. void gs_set3dmode(double fovy, double znear, double zvar)
  663. {
  664. /* TODO */
  665. UNUSED_PARAMETER(fovy);
  666. UNUSED_PARAMETER(znear);
  667. UNUSED_PARAMETER(zvar);
  668. }
  669. void gs_viewport_push(void)
  670. {
  671. if (!thread_graphics) return;
  672. struct gs_rect *rect = da_push_back_new(
  673. thread_graphics->viewport_stack);
  674. gs_getviewport(rect);
  675. }
  676. void gs_viewport_pop(void)
  677. {
  678. struct gs_rect *rect;
  679. if (!thread_graphics || !thread_graphics->viewport_stack.num)
  680. return;
  681. rect = da_end(thread_graphics->viewport_stack);
  682. gs_setviewport(rect->x, rect->y, rect->cx, rect->cy);
  683. da_pop_back(thread_graphics->viewport_stack);
  684. }
  685. void texture_setimage(texture_t tex, const void *data, uint32_t linesize,
  686. bool flip)
  687. {
  688. if (!thread_graphics || !tex)
  689. return;
  690. void *ptr;
  691. uint32_t linesize_out;
  692. uint32_t row_copy;
  693. int32_t height = (int32_t)texture_getheight(tex);
  694. int32_t y;
  695. if (!texture_map(tex, &ptr, &linesize_out))
  696. return;
  697. row_copy = (linesize < linesize_out) ? linesize : linesize_out;
  698. if (flip) {
  699. for (y = height-1; y >= 0; y--)
  700. memcpy((uint8_t*)ptr + (uint32_t)y * linesize_out,
  701. (uint8_t*)data + (uint32_t)y * linesize,
  702. row_copy);
  703. } else if (linesize == linesize_out) {
  704. memcpy(ptr, data, row_copy * height);
  705. } else {
  706. for (y = 0; y < height; y++)
  707. memcpy((uint8_t*)ptr + (uint32_t)y * linesize_out,
  708. (uint8_t*)data + (uint32_t)y * linesize,
  709. row_copy);
  710. }
  711. texture_unmap(tex);
  712. }
  713. void cubetexture_setimage(texture_t cubetex, uint32_t side, const void *data,
  714. uint32_t linesize, bool invert)
  715. {
  716. /* TODO */
  717. UNUSED_PARAMETER(cubetex);
  718. UNUSED_PARAMETER(side);
  719. UNUSED_PARAMETER(data);
  720. UNUSED_PARAMETER(linesize);
  721. UNUSED_PARAMETER(invert);
  722. }
  723. void gs_perspective(float angle, float aspect, float near, float far)
  724. {
  725. graphics_t graphics = thread_graphics;
  726. float xmin, xmax, ymin, ymax;
  727. if (!graphics) return;
  728. ymax = near * tanf(RAD(angle)*0.5f);
  729. ymin = -ymax;
  730. xmin = ymin * aspect;
  731. xmax = ymax * aspect;
  732. graphics->exports.device_frustum(graphics->device, xmin, xmax,
  733. ymin, ymax, near, far);
  734. }
  735. /* ------------------------------------------------------------------------- */
  736. const char *gs_preprocessor_name(void)
  737. {
  738. graphics_t graphics = thread_graphics;
  739. if (!graphics) return NULL;
  740. return graphics->exports.device_preprocessor_name();
  741. }
  742. swapchain_t gs_create_swapchain(struct gs_init_data *data)
  743. {
  744. graphics_t graphics = thread_graphics;
  745. if (!graphics) return NULL;
  746. return graphics->exports.device_create_swapchain(graphics->device,
  747. data);
  748. }
  749. void gs_resize(uint32_t x, uint32_t y)
  750. {
  751. graphics_t graphics = thread_graphics;
  752. if (!graphics) return;
  753. graphics->exports.device_resize(graphics->device, x, y);
  754. }
  755. void gs_getsize(uint32_t *x, uint32_t *y)
  756. {
  757. graphics_t graphics = thread_graphics;
  758. if (!graphics) return;
  759. graphics->exports.device_getsize(graphics->device, x, y);
  760. }
  761. uint32_t gs_getwidth(void)
  762. {
  763. graphics_t graphics = thread_graphics;
  764. if (!graphics) return 0;
  765. return graphics->exports.device_getwidth(graphics->device);
  766. }
  767. uint32_t gs_getheight(void)
  768. {
  769. graphics_t graphics = thread_graphics;
  770. if (!graphics) return 0;
  771. return graphics->exports.device_getheight(graphics->device);
  772. }
  773. static inline bool is_pow2(uint32_t size)
  774. {
  775. return size >= 2 && (size & (size-1)) == 0;
  776. }
  777. texture_t gs_create_texture(uint32_t width, uint32_t height,
  778. enum gs_color_format color_format, uint32_t levels,
  779. const void **data, uint32_t flags)
  780. {
  781. graphics_t graphics = thread_graphics;
  782. bool pow2tex = is_pow2(width) && is_pow2(height);
  783. bool uses_mipmaps = (flags & GS_BUILDMIPMAPS || levels != 1);
  784. if (!graphics)
  785. return NULL;
  786. if (uses_mipmaps && !pow2tex) {
  787. blog(LOG_WARNING, "Cannot use mipmaps with a "
  788. "non-power-of-two texture. Disabling "
  789. "mipmaps for this texture.");
  790. uses_mipmaps = false;
  791. flags &= ~GS_BUILDMIPMAPS;
  792. levels = 1;
  793. }
  794. if (uses_mipmaps && flags & GS_RENDERTARGET) {
  795. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  796. "Disabling mipmaps for this texture.");
  797. flags &= ~GS_BUILDMIPMAPS;
  798. levels = 1;
  799. }
  800. return graphics->exports.device_create_texture(graphics->device,
  801. width, height, color_format, levels, data, flags);
  802. }
  803. texture_t gs_create_cubetexture(uint32_t size,
  804. enum gs_color_format color_format, uint32_t levels,
  805. const void **data, uint32_t flags)
  806. {
  807. graphics_t graphics = thread_graphics;
  808. bool pow2tex = is_pow2(size);
  809. bool uses_mipmaps = (flags & GS_BUILDMIPMAPS || levels != 1);
  810. if (!graphics)
  811. return NULL;
  812. if (uses_mipmaps && !pow2tex) {
  813. blog(LOG_WARNING, "Cannot use mipmaps with a "
  814. "non-power-of-two texture. Disabling "
  815. "mipmaps for this texture.");
  816. uses_mipmaps = false;
  817. flags &= ~GS_BUILDMIPMAPS;
  818. levels = 1;
  819. }
  820. if (uses_mipmaps && flags & GS_RENDERTARGET) {
  821. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  822. "Disabling mipmaps for this texture.");
  823. flags &= ~GS_BUILDMIPMAPS;
  824. levels = 1;
  825. data = NULL;
  826. }
  827. return graphics->exports.device_create_cubetexture(graphics->device,
  828. size, color_format, levels, data, flags);
  829. }
  830. texture_t gs_create_volumetexture(uint32_t width, uint32_t height,
  831. uint32_t depth, enum gs_color_format color_format,
  832. uint32_t levels, const void **data, uint32_t flags)
  833. {
  834. graphics_t graphics = thread_graphics;
  835. if (!graphics) return NULL;
  836. return graphics->exports.device_create_volumetexture(graphics->device,
  837. width, height, depth, color_format, levels, data,
  838. flags);
  839. }
  840. zstencil_t gs_create_zstencil(uint32_t width, uint32_t height,
  841. enum gs_zstencil_format format)
  842. {
  843. graphics_t graphics = thread_graphics;
  844. if (!graphics) return NULL;
  845. return graphics->exports.device_create_zstencil(graphics->device,
  846. width, height, format);
  847. }
  848. stagesurf_t gs_create_stagesurface(uint32_t width, uint32_t height,
  849. enum gs_color_format color_format)
  850. {
  851. graphics_t graphics = thread_graphics;
  852. if (!graphics) return NULL;
  853. return graphics->exports.device_create_stagesurface(graphics->device,
  854. width, height, color_format);
  855. }
  856. samplerstate_t gs_create_samplerstate(struct gs_sampler_info *info)
  857. {
  858. graphics_t graphics = thread_graphics;
  859. if (!graphics) return NULL;
  860. return graphics->exports.device_create_samplerstate(graphics->device,
  861. info);
  862. }
  863. shader_t gs_create_vertexshader(const char *shader, const char *file,
  864. char **error_string)
  865. {
  866. graphics_t graphics = thread_graphics;
  867. if (!graphics) return NULL;
  868. return graphics->exports.device_create_vertexshader(graphics->device,
  869. shader, file, error_string);
  870. }
  871. shader_t gs_create_pixelshader(const char *shader,
  872. const char *file, char **error_string)
  873. {
  874. graphics_t graphics = thread_graphics;
  875. if (!graphics) return NULL;
  876. return graphics->exports.device_create_pixelshader(graphics->device,
  877. shader, file, error_string);
  878. }
  879. vertbuffer_t gs_create_vertexbuffer(struct vb_data *data,
  880. uint32_t flags)
  881. {
  882. graphics_t graphics = thread_graphics;
  883. if (!graphics) return NULL;
  884. return graphics->exports.device_create_vertexbuffer(graphics->device,
  885. data, flags);
  886. }
  887. indexbuffer_t gs_create_indexbuffer(enum gs_index_type type,
  888. void *indices, size_t num, uint32_t flags)
  889. {
  890. graphics_t graphics = thread_graphics;
  891. if (!graphics) return NULL;
  892. return graphics->exports.device_create_indexbuffer(graphics->device,
  893. type, indices, num, flags);
  894. }
  895. enum gs_texture_type gs_gettexturetype(texture_t texture)
  896. {
  897. graphics_t graphics = thread_graphics;
  898. if (!graphics) return GS_TEXTURE_2D;
  899. return graphics->exports.device_gettexturetype(texture);
  900. }
  901. void gs_load_vertexbuffer(vertbuffer_t vertbuffer)
  902. {
  903. graphics_t graphics = thread_graphics;
  904. if (!graphics) return;
  905. graphics->exports.device_load_vertexbuffer(graphics->device,
  906. vertbuffer);
  907. }
  908. void gs_load_indexbuffer(indexbuffer_t indexbuffer)
  909. {
  910. graphics_t graphics = thread_graphics;
  911. if (!graphics) return;
  912. graphics->exports.device_load_indexbuffer(graphics->device,
  913. indexbuffer);
  914. }
  915. void gs_load_texture(texture_t tex, int unit)
  916. {
  917. graphics_t graphics = thread_graphics;
  918. if (!graphics) return;
  919. graphics->exports.device_load_texture(graphics->device, tex, unit);
  920. }
  921. void gs_load_samplerstate(samplerstate_t samplerstate, int unit)
  922. {
  923. graphics_t graphics = thread_graphics;
  924. if (!graphics) return;
  925. graphics->exports.device_load_samplerstate(graphics->device,
  926. samplerstate, unit);
  927. }
  928. void gs_load_vertexshader(shader_t vertshader)
  929. {
  930. graphics_t graphics = thread_graphics;
  931. if (!graphics) return;
  932. graphics->exports.device_load_vertexshader(graphics->device,
  933. vertshader);
  934. }
  935. void gs_load_pixelshader(shader_t pixelshader)
  936. {
  937. graphics_t graphics = thread_graphics;
  938. if (!graphics) return;
  939. graphics->exports.device_load_pixelshader(graphics->device,
  940. pixelshader);
  941. }
  942. void gs_load_defaultsamplerstate(bool b_3d, int unit)
  943. {
  944. graphics_t graphics = thread_graphics;
  945. if (!graphics) return;
  946. graphics->exports.device_load_defaultsamplerstate(graphics->device,
  947. b_3d, unit);
  948. }
  949. shader_t gs_getvertexshader(void)
  950. {
  951. graphics_t graphics = thread_graphics;
  952. if (!graphics) return NULL;
  953. return graphics->exports.device_getvertexshader(graphics->device);
  954. }
  955. shader_t gs_getpixelshader(void)
  956. {
  957. graphics_t graphics = thread_graphics;
  958. if (!graphics) return NULL;
  959. return graphics->exports.device_getpixelshader(graphics->device);
  960. }
  961. texture_t gs_getrendertarget(void)
  962. {
  963. graphics_t graphics = thread_graphics;
  964. if (!graphics) return NULL;
  965. return graphics->exports.device_getrendertarget(graphics->device);
  966. }
  967. zstencil_t gs_getzstenciltarget(void)
  968. {
  969. graphics_t graphics = thread_graphics;
  970. if (!graphics) return NULL;
  971. return graphics->exports.device_getzstenciltarget(graphics->device);
  972. }
  973. void gs_setrendertarget(texture_t tex, zstencil_t zstencil)
  974. {
  975. graphics_t graphics = thread_graphics;
  976. if (!graphics) return;
  977. graphics->exports.device_setrendertarget(graphics->device, tex,
  978. zstencil);
  979. }
  980. void gs_setcuberendertarget(texture_t cubetex, int side, zstencil_t zstencil)
  981. {
  982. graphics_t graphics = thread_graphics;
  983. if (!graphics) return;
  984. graphics->exports.device_setcuberendertarget(graphics->device, cubetex,
  985. side, zstencil);
  986. }
  987. void gs_copy_texture(texture_t dst, texture_t src)
  988. {
  989. graphics_t graphics = thread_graphics;
  990. if (!graphics) return;
  991. graphics->exports.device_copy_texture(graphics->device, dst, src);
  992. }
  993. void gs_stage_texture(stagesurf_t dst, texture_t src)
  994. {
  995. graphics_t graphics = thread_graphics;
  996. if (!graphics) return;
  997. graphics->exports.device_stage_texture(graphics->device, dst, src);
  998. }
  999. void gs_beginscene(void)
  1000. {
  1001. graphics_t graphics = thread_graphics;
  1002. if (!graphics) return;
  1003. graphics->exports.device_beginscene(graphics->device);
  1004. }
  1005. void gs_draw(enum gs_draw_mode draw_mode, uint32_t start_vert,
  1006. uint32_t num_verts)
  1007. {
  1008. graphics_t graphics = thread_graphics;
  1009. if (!graphics) return;
  1010. graphics->exports.device_draw(graphics->device, draw_mode,
  1011. start_vert, num_verts);
  1012. }
  1013. void gs_endscene(void)
  1014. {
  1015. graphics_t graphics = thread_graphics;
  1016. if (!graphics) return;
  1017. graphics->exports.device_endscene(graphics->device);
  1018. }
  1019. void gs_load_swapchain(swapchain_t swapchain)
  1020. {
  1021. graphics_t graphics = thread_graphics;
  1022. if (!graphics) return;
  1023. graphics->exports.device_load_swapchain(graphics->device, swapchain);
  1024. }
  1025. void gs_clear(uint32_t clear_flags, struct vec4 *color, float depth,
  1026. uint8_t stencil)
  1027. {
  1028. graphics_t graphics = thread_graphics;
  1029. graphics->exports.device_clear(graphics->device, clear_flags, color,
  1030. depth, stencil);
  1031. }
  1032. void gs_present(void)
  1033. {
  1034. graphics_t graphics = thread_graphics;
  1035. if (!graphics) return;
  1036. graphics->exports.device_present(graphics->device);
  1037. }
  1038. void gs_setcullmode(enum gs_cull_mode mode)
  1039. {
  1040. graphics_t graphics = thread_graphics;
  1041. if (!graphics) return;
  1042. graphics->exports.device_setcullmode(graphics->device, mode);
  1043. }
  1044. enum gs_cull_mode gs_getcullmode(void)
  1045. {
  1046. graphics_t graphics = thread_graphics;
  1047. if (!graphics) return GS_NEITHER;
  1048. return graphics->exports.device_getcullmode(graphics->device);
  1049. }
  1050. void gs_enable_blending(bool enable)
  1051. {
  1052. graphics_t graphics = thread_graphics;
  1053. if (!graphics) return;
  1054. graphics->exports.device_enable_blending(graphics->device, enable);
  1055. }
  1056. void gs_enable_depthtest(bool enable)
  1057. {
  1058. graphics_t graphics = thread_graphics;
  1059. if (!graphics) return;
  1060. graphics->exports.device_enable_depthtest(graphics->device, enable);
  1061. }
  1062. void gs_enable_stenciltest(bool enable)
  1063. {
  1064. graphics_t graphics = thread_graphics;
  1065. if (!graphics) return;
  1066. graphics->exports.device_enable_stenciltest(graphics->device, enable);
  1067. }
  1068. void gs_enable_stencilwrite(bool enable)
  1069. {
  1070. graphics_t graphics = thread_graphics;
  1071. if (!graphics) return;
  1072. graphics->exports.device_enable_stencilwrite(graphics->device, enable);
  1073. }
  1074. void gs_enable_color(bool red, bool green, bool blue, bool alpha)
  1075. {
  1076. graphics_t graphics = thread_graphics;
  1077. if (!graphics) return;
  1078. graphics->exports.device_enable_color(graphics->device, red, green,
  1079. blue, alpha);
  1080. }
  1081. void gs_blendfunction(enum gs_blend_type src, enum gs_blend_type dest)
  1082. {
  1083. graphics_t graphics = thread_graphics;
  1084. if (!graphics) return;
  1085. graphics->exports.device_blendfunction(graphics->device, src, dest);
  1086. }
  1087. void gs_depthfunction(enum gs_depth_test test)
  1088. {
  1089. graphics_t graphics = thread_graphics;
  1090. if (!graphics) return;
  1091. graphics->exports.device_depthfunction(graphics->device, test);
  1092. }
  1093. void gs_stencilfunction(enum gs_stencil_side side, enum gs_depth_test test)
  1094. {
  1095. graphics_t graphics = thread_graphics;
  1096. if (!graphics) return;
  1097. graphics->exports.device_stencilfunction(graphics->device, side, test);
  1098. }
  1099. void gs_stencilop(enum gs_stencil_side side, enum gs_stencil_op fail,
  1100. enum gs_stencil_op zfail, enum gs_stencil_op zpass)
  1101. {
  1102. graphics_t graphics = thread_graphics;
  1103. if (!graphics) return;
  1104. graphics->exports.device_stencilop(graphics->device, side, fail, zfail,
  1105. zpass);
  1106. }
  1107. void gs_enable_fullscreen(bool enable)
  1108. {
  1109. graphics_t graphics = thread_graphics;
  1110. if (!graphics) return;
  1111. graphics->exports.device_enable_fullscreen(graphics->device, enable);
  1112. }
  1113. int gs_fullscreen_enabled(void)
  1114. {
  1115. graphics_t graphics = thread_graphics;
  1116. if (!graphics) return 0;
  1117. return graphics->exports.device_fullscreen_enabled(graphics->device);
  1118. }
  1119. void gs_setdisplaymode(const struct gs_display_mode *mode)
  1120. {
  1121. graphics_t graphics = thread_graphics;
  1122. if (!graphics) return;
  1123. graphics->exports.device_setdisplaymode(graphics->device, mode);
  1124. }
  1125. void gs_getdisplaymode(struct gs_display_mode *mode)
  1126. {
  1127. graphics_t graphics = thread_graphics;
  1128. if (!graphics) return;
  1129. graphics->exports.device_getdisplaymode(graphics->device, mode);
  1130. }
  1131. void gs_setcolorramp(float gamma, float brightness, float contrast)
  1132. {
  1133. graphics_t graphics = thread_graphics;
  1134. if (!graphics) return;
  1135. graphics->exports.device_setcolorramp(graphics->device, gamma,
  1136. brightness, contrast);
  1137. }
  1138. void gs_setviewport(int x, int y, int width, int height)
  1139. {
  1140. graphics_t graphics = thread_graphics;
  1141. if (!graphics) return;
  1142. graphics->exports.device_setviewport(graphics->device, x, y, width,
  1143. height);
  1144. }
  1145. void gs_getviewport(struct gs_rect *rect)
  1146. {
  1147. graphics_t graphics = thread_graphics;
  1148. if (!graphics) return;
  1149. graphics->exports.device_getviewport(graphics->device, rect);
  1150. }
  1151. void gs_setscissorrect(struct gs_rect *rect)
  1152. {
  1153. graphics_t graphics = thread_graphics;
  1154. if (!graphics) return;
  1155. graphics->exports.device_setscissorrect(graphics->device, rect);
  1156. }
  1157. void gs_ortho(float left, float right, float top, float bottom, float znear,
  1158. float zfar)
  1159. {
  1160. graphics_t graphics = thread_graphics;
  1161. if (!graphics) return;
  1162. graphics->exports.device_ortho(graphics->device, left, right, top,
  1163. bottom, znear, zfar);
  1164. }
  1165. void gs_frustum(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_frustum(graphics->device, left, right, top,
  1171. bottom, znear, zfar);
  1172. }
  1173. void gs_projection_push(void)
  1174. {
  1175. graphics_t graphics = thread_graphics;
  1176. if (!graphics) return;
  1177. graphics->exports.device_projection_push(graphics->device);
  1178. }
  1179. void gs_projection_pop(void)
  1180. {
  1181. graphics_t graphics = thread_graphics;
  1182. if (!graphics) return;
  1183. graphics->exports.device_projection_pop(graphics->device);
  1184. }
  1185. void swapchain_destroy(swapchain_t swapchain)
  1186. {
  1187. graphics_t graphics = thread_graphics;
  1188. if (!graphics || !swapchain) return;
  1189. graphics->exports.swapchain_destroy(swapchain);
  1190. }
  1191. void shader_destroy(shader_t shader)
  1192. {
  1193. graphics_t graphics = thread_graphics;
  1194. if (!graphics || !shader) return;
  1195. graphics->exports.shader_destroy(shader);
  1196. }
  1197. int shader_numparams(shader_t shader)
  1198. {
  1199. graphics_t graphics = thread_graphics;
  1200. if (!graphics || !shader) return 0;
  1201. return graphics->exports.shader_numparams(shader);
  1202. }
  1203. sparam_t shader_getparambyidx(shader_t shader, uint32_t param)
  1204. {
  1205. graphics_t graphics = thread_graphics;
  1206. if (!graphics || !shader) return NULL;
  1207. return graphics->exports.shader_getparambyidx(shader, param);
  1208. }
  1209. sparam_t shader_getparambyname(shader_t shader, const char *name)
  1210. {
  1211. graphics_t graphics = thread_graphics;
  1212. if (!graphics || !shader) return NULL;
  1213. return graphics->exports.shader_getparambyname(shader, name);
  1214. }
  1215. void shader_getparaminfo(shader_t shader, sparam_t param,
  1216. struct shader_param_info *info)
  1217. {
  1218. graphics_t graphics = thread_graphics;
  1219. if (!graphics || !shader || !param) return;
  1220. graphics->exports.shader_getparaminfo(shader, param, info);
  1221. }
  1222. sparam_t shader_getviewprojmatrix(shader_t shader)
  1223. {
  1224. graphics_t graphics = thread_graphics;
  1225. if (!graphics || !shader) return NULL;
  1226. return graphics->exports.shader_getviewprojmatrix(shader);
  1227. }
  1228. sparam_t shader_getworldmatrix(shader_t shader)
  1229. {
  1230. graphics_t graphics = thread_graphics;
  1231. if (!graphics || !shader) return NULL;
  1232. return graphics->exports.shader_getworldmatrix(shader);
  1233. }
  1234. void shader_setbool(shader_t shader, sparam_t param, bool val)
  1235. {
  1236. graphics_t graphics = thread_graphics;
  1237. if (!graphics || !shader || !param) return;
  1238. graphics->exports.shader_setbool(shader, param, val);
  1239. }
  1240. void shader_setfloat(shader_t shader, sparam_t param, float val)
  1241. {
  1242. graphics_t graphics = thread_graphics;
  1243. if (!graphics || !shader || !param) return;
  1244. graphics->exports.shader_setfloat(shader, param, val);
  1245. }
  1246. void shader_setint(shader_t shader, sparam_t param, int val)
  1247. {
  1248. graphics_t graphics = thread_graphics;
  1249. if (!graphics || !shader || !param) return;
  1250. graphics->exports.shader_setint(shader, param, val);
  1251. }
  1252. void shader_setmatrix3(shader_t shader, sparam_t param,
  1253. const struct matrix3 *val)
  1254. {
  1255. graphics_t graphics = thread_graphics;
  1256. if (!graphics || !shader || !param) return;
  1257. graphics->exports.shader_setmatrix3(shader, param, val);
  1258. }
  1259. void shader_setmatrix4(shader_t shader, sparam_t param,
  1260. const struct matrix4 *val)
  1261. {
  1262. graphics_t graphics = thread_graphics;
  1263. if (!graphics || !shader || !param) return;
  1264. graphics->exports.shader_setmatrix4(shader, param, val);
  1265. }
  1266. void shader_setvec2(shader_t shader, sparam_t param,
  1267. const struct vec2 *val)
  1268. {
  1269. graphics_t graphics = thread_graphics;
  1270. if (!graphics || !shader || !param) return;
  1271. graphics->exports.shader_setvec2(shader, param, val);
  1272. }
  1273. void shader_setvec3(shader_t shader, sparam_t param,
  1274. const struct vec3 *val)
  1275. {
  1276. graphics_t graphics = thread_graphics;
  1277. if (!graphics || !shader || !param) return;
  1278. graphics->exports.shader_setvec3(shader, param, val);
  1279. }
  1280. void shader_setvec4(shader_t shader, sparam_t param,
  1281. const struct vec4 *val)
  1282. {
  1283. graphics_t graphics = thread_graphics;
  1284. if (!graphics || !shader || !param) return;
  1285. graphics->exports.shader_setvec4(shader, param, val);
  1286. }
  1287. void shader_settexture(shader_t shader, sparam_t param, texture_t val)
  1288. {
  1289. graphics_t graphics = thread_graphics;
  1290. if (!graphics || !shader || !param) return;
  1291. graphics->exports.shader_settexture(shader, param, val);
  1292. }
  1293. void shader_setval(shader_t shader, sparam_t param, const void *val,
  1294. size_t size)
  1295. {
  1296. graphics_t graphics = thread_graphics;
  1297. if (!graphics || !shader || !param) return;
  1298. graphics->exports.shader_setval(shader, param, val, size);
  1299. }
  1300. void shader_setdefault(shader_t shader, sparam_t param)
  1301. {
  1302. graphics_t graphics = thread_graphics;
  1303. if (!graphics || !shader || !param) return;
  1304. graphics->exports.shader_setdefault(shader, param);
  1305. }
  1306. void texture_destroy(texture_t tex)
  1307. {
  1308. graphics_t graphics = thread_graphics;
  1309. if (!graphics || !tex) return;
  1310. graphics->exports.texture_destroy(tex);
  1311. }
  1312. uint32_t texture_getwidth(texture_t tex)
  1313. {
  1314. graphics_t graphics = thread_graphics;
  1315. if (!graphics || !tex) return 0;
  1316. return graphics->exports.texture_getwidth(tex);
  1317. }
  1318. uint32_t texture_getheight(texture_t tex)
  1319. {
  1320. graphics_t graphics = thread_graphics;
  1321. if (!graphics || !tex) return 0;
  1322. return graphics->exports.texture_getheight(tex);
  1323. }
  1324. enum gs_color_format texture_getcolorformat(texture_t tex)
  1325. {
  1326. graphics_t graphics = thread_graphics;
  1327. if (!graphics || !tex) return GS_UNKNOWN;
  1328. return graphics->exports.texture_getcolorformat(tex);
  1329. }
  1330. bool texture_map(texture_t tex, void **ptr, uint32_t *linesize)
  1331. {
  1332. graphics_t graphics = thread_graphics;
  1333. if (!graphics || !tex) return false;
  1334. return graphics->exports.texture_map(tex, ptr, linesize);
  1335. }
  1336. void texture_unmap(texture_t tex)
  1337. {
  1338. graphics_t graphics = thread_graphics;
  1339. if (!graphics || !tex) return;
  1340. graphics->exports.texture_unmap(tex);
  1341. }
  1342. bool texture_isrect(texture_t tex)
  1343. {
  1344. graphics_t graphics = thread_graphics;
  1345. if (!graphics || !tex) return false;
  1346. if (graphics->exports.texture_isrect)
  1347. return graphics->exports.texture_isrect(tex);
  1348. else
  1349. return false;
  1350. }
  1351. void cubetexture_destroy(texture_t cubetex)
  1352. {
  1353. graphics_t graphics = thread_graphics;
  1354. if (!graphics || !cubetex) return;
  1355. graphics->exports.cubetexture_destroy(cubetex);
  1356. }
  1357. uint32_t cubetexture_getsize(texture_t cubetex)
  1358. {
  1359. graphics_t graphics = thread_graphics;
  1360. if (!graphics || !cubetex) return 0;
  1361. return graphics->exports.cubetexture_getsize(cubetex);
  1362. }
  1363. enum gs_color_format cubetexture_getcolorformat(texture_t cubetex)
  1364. {
  1365. graphics_t graphics = thread_graphics;
  1366. if (!graphics || !cubetex) return GS_UNKNOWN;
  1367. return graphics->exports.cubetexture_getcolorformat(cubetex);
  1368. }
  1369. void volumetexture_destroy(texture_t voltex)
  1370. {
  1371. graphics_t graphics = thread_graphics;
  1372. if (!graphics || !voltex) return;
  1373. graphics->exports.volumetexture_destroy(voltex);
  1374. }
  1375. uint32_t volumetexture_getwidth(texture_t voltex)
  1376. {
  1377. graphics_t graphics = thread_graphics;
  1378. if (!graphics || !voltex) return 0;
  1379. return graphics->exports.volumetexture_getwidth(voltex);
  1380. }
  1381. uint32_t volumetexture_getheight(texture_t voltex)
  1382. {
  1383. graphics_t graphics = thread_graphics;
  1384. if (!graphics || !voltex) return 0;
  1385. return graphics->exports.volumetexture_getheight(voltex);
  1386. }
  1387. uint32_t volumetexture_getdepth(texture_t voltex)
  1388. {
  1389. graphics_t graphics = thread_graphics;
  1390. if (!graphics || !voltex) return 0;
  1391. return graphics->exports.volumetexture_getdepth(voltex);
  1392. }
  1393. enum gs_color_format volumetexture_getcolorformat(texture_t voltex)
  1394. {
  1395. graphics_t graphics = thread_graphics;
  1396. if (!graphics || !voltex) return GS_UNKNOWN;
  1397. return graphics->exports.volumetexture_getcolorformat(voltex);
  1398. }
  1399. void stagesurface_destroy(stagesurf_t stagesurf)
  1400. {
  1401. graphics_t graphics = thread_graphics;
  1402. if (!graphics || !stagesurf) return;
  1403. graphics->exports.stagesurface_destroy(stagesurf);
  1404. }
  1405. uint32_t stagesurface_getwidth(stagesurf_t stagesurf)
  1406. {
  1407. graphics_t graphics = thread_graphics;
  1408. if (!graphics || !stagesurf) return 0;
  1409. return graphics->exports.stagesurface_getwidth(stagesurf);
  1410. }
  1411. uint32_t stagesurface_getheight(stagesurf_t stagesurf)
  1412. {
  1413. graphics_t graphics = thread_graphics;
  1414. if (!graphics || !stagesurf) return 0;
  1415. return graphics->exports.stagesurface_getheight(stagesurf);
  1416. }
  1417. enum gs_color_format stagesurface_getcolorformat(stagesurf_t stagesurf)
  1418. {
  1419. graphics_t graphics = thread_graphics;
  1420. if (!graphics || !stagesurf) return GS_UNKNOWN;
  1421. return graphics->exports.stagesurface_getcolorformat(stagesurf);
  1422. }
  1423. bool stagesurface_map(stagesurf_t stagesurf, uint8_t **data, uint32_t *linesize)
  1424. {
  1425. graphics_t graphics = thread_graphics;
  1426. if (!graphics || !stagesurf) return false;
  1427. return graphics->exports.stagesurface_map(stagesurf, data, linesize);
  1428. }
  1429. void stagesurface_unmap(stagesurf_t stagesurf)
  1430. {
  1431. graphics_t graphics = thread_graphics;
  1432. if (!graphics || !stagesurf) return;
  1433. graphics->exports.stagesurface_unmap(stagesurf);
  1434. }
  1435. void zstencil_destroy(zstencil_t zstencil)
  1436. {
  1437. if (!thread_graphics || !zstencil) return;
  1438. thread_graphics->exports.zstencil_destroy(zstencil);
  1439. }
  1440. void samplerstate_destroy(samplerstate_t samplerstate)
  1441. {
  1442. if (!thread_graphics || !samplerstate) return;
  1443. thread_graphics->exports.samplerstate_destroy(samplerstate);
  1444. }
  1445. void vertexbuffer_destroy(vertbuffer_t vertbuffer)
  1446. {
  1447. graphics_t graphics = thread_graphics;
  1448. if (!graphics || !vertbuffer) return;
  1449. graphics->exports.vertexbuffer_destroy(vertbuffer);
  1450. }
  1451. void vertexbuffer_flush(vertbuffer_t vertbuffer, bool rebuild)
  1452. {
  1453. if (!thread_graphics || !vertbuffer) return;
  1454. thread_graphics->exports.vertexbuffer_flush(vertbuffer, rebuild);
  1455. }
  1456. struct vb_data *vertexbuffer_getdata(vertbuffer_t vertbuffer)
  1457. {
  1458. if (!thread_graphics || !vertbuffer) return NULL;
  1459. return thread_graphics->exports.vertexbuffer_getdata(vertbuffer);
  1460. }
  1461. void indexbuffer_destroy(indexbuffer_t indexbuffer)
  1462. {
  1463. graphics_t graphics = thread_graphics;
  1464. if (!graphics || !indexbuffer) return;
  1465. graphics->exports.indexbuffer_destroy(indexbuffer);
  1466. }
  1467. void indexbuffer_flush(indexbuffer_t indexbuffer)
  1468. {
  1469. if (!thread_graphics || !indexbuffer) return;
  1470. thread_graphics->exports.indexbuffer_flush(indexbuffer);
  1471. }
  1472. void *indexbuffer_getdata(indexbuffer_t indexbuffer)
  1473. {
  1474. if (!thread_graphics || !indexbuffer) return NULL;
  1475. return thread_graphics->exports.indexbuffer_getdata(indexbuffer);
  1476. }
  1477. size_t indexbuffer_numindices(indexbuffer_t indexbuffer)
  1478. {
  1479. if (!thread_graphics || !indexbuffer) return 0;
  1480. return thread_graphics->exports.indexbuffer_numindices(indexbuffer);
  1481. }
  1482. enum gs_index_type indexbuffer_gettype(indexbuffer_t indexbuffer)
  1483. {
  1484. if (!thread_graphics || !indexbuffer) return (enum gs_index_type)0;
  1485. return thread_graphics->exports.indexbuffer_gettype(indexbuffer);
  1486. }
  1487. #ifdef __APPLE__
  1488. /** Platform specific functions */
  1489. texture_t gs_create_texture_from_iosurface(void *iosurf)
  1490. {
  1491. graphics_t graphics = thread_graphics;
  1492. if (!graphics || !iosurf ||
  1493. !graphics->exports.texture_create_from_iosurface)
  1494. return NULL;
  1495. return graphics->exports.texture_create_from_iosurface(
  1496. graphics->device, iosurf);
  1497. }
  1498. bool texture_rebind_iosurface(texture_t texture, void *iosurf)
  1499. {
  1500. graphics_t graphics = thread_graphics;
  1501. if (!graphics || !iosurf || !graphics->exports.texture_rebind_iosurface)
  1502. return false;
  1503. return graphics->exports.texture_rebind_iosurface(texture, iosurf);
  1504. }
  1505. #elif _WIN32
  1506. bool gs_gdi_texture_available(void)
  1507. {
  1508. if (!thread_graphics)
  1509. return false;
  1510. return thread_graphics->exports.gdi_texture_available();
  1511. }
  1512. /** creates a windows GDI-lockable texture */
  1513. texture_t gs_create_gdi_texture(uint32_t width, uint32_t height)
  1514. {
  1515. graphics_t graphics = thread_graphics;
  1516. if (!graphics) return NULL;
  1517. if (graphics->exports.device_create_gdi_texture)
  1518. return graphics->exports.device_create_gdi_texture(
  1519. graphics->device, width, height);
  1520. return NULL;
  1521. }
  1522. void *texture_get_dc(texture_t gdi_tex)
  1523. {
  1524. if (!thread_graphics || !gdi_tex)
  1525. return NULL;
  1526. if (thread_graphics->exports.texture_get_dc)
  1527. return thread_graphics->exports.texture_get_dc(gdi_tex);
  1528. return NULL;
  1529. }
  1530. void texture_release_dc(texture_t gdi_tex)
  1531. {
  1532. if (!thread_graphics || !gdi_tex)
  1533. return;
  1534. if (thread_graphics->exports.texture_release_dc)
  1535. thread_graphics->exports.texture_release_dc(gdi_tex);
  1536. }
  1537. #endif