graphics.c 46 KB

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