graphics.c 46 KB

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