graphics.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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, 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. assert(thread_graphics != NULL);
  656. gs_getsize(&cx, &cy);
  657. gs_setviewport(0, 0, (int)cx, (int)cy);
  658. }
  659. void gs_set2dmode(void)
  660. {
  661. uint32_t cx, cy;
  662. assert(thread_graphics != NULL);
  663. gs_getsize(&cx, &cy);
  664. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -1.0, -1024.0f);
  665. }
  666. void gs_set3dmode(double fovy, double znear, double zvar)
  667. {
  668. /* TODO */
  669. UNUSED_PARAMETER(fovy);
  670. UNUSED_PARAMETER(znear);
  671. UNUSED_PARAMETER(zvar);
  672. }
  673. void gs_viewport_push(void)
  674. {
  675. if (!thread_graphics) return;
  676. struct gs_rect *rect = da_push_back_new(
  677. thread_graphics->viewport_stack);
  678. gs_getviewport(rect);
  679. }
  680. void gs_viewport_pop(void)
  681. {
  682. struct gs_rect *rect;
  683. if (!thread_graphics || !thread_graphics->viewport_stack.num)
  684. return;
  685. rect = da_end(thread_graphics->viewport_stack);
  686. gs_setviewport(rect->x, rect->y, rect->cx, rect->cy);
  687. da_pop_back(thread_graphics->viewport_stack);
  688. }
  689. void texture_setimage(texture_t tex, const void *data, uint32_t linesize,
  690. bool flip)
  691. {
  692. if (!thread_graphics || !tex)
  693. return;
  694. void *ptr;
  695. uint32_t linesize_out;
  696. uint32_t row_copy;
  697. int32_t height = (int32_t)texture_getheight(tex);
  698. int32_t y;
  699. if (!texture_map(tex, &ptr, &linesize_out))
  700. return;
  701. row_copy = (linesize < linesize_out) ? linesize : linesize_out;
  702. if (flip) {
  703. for (y = height-1; y >= 0; y--)
  704. memcpy((uint8_t*)ptr + (uint32_t)y * linesize_out,
  705. (uint8_t*)data + (uint32_t)y * linesize,
  706. row_copy);
  707. } else if (linesize == linesize_out) {
  708. memcpy(ptr, data, row_copy * height);
  709. } else {
  710. for (y = 0; y < height; y++)
  711. memcpy((uint8_t*)ptr + (uint32_t)y * linesize_out,
  712. (uint8_t*)data + (uint32_t)y * linesize,
  713. row_copy);
  714. }
  715. texture_unmap(tex);
  716. }
  717. void cubetexture_setimage(texture_t cubetex, uint32_t side, const void *data,
  718. uint32_t linesize, bool invert)
  719. {
  720. /* TODO */
  721. UNUSED_PARAMETER(cubetex);
  722. UNUSED_PARAMETER(side);
  723. UNUSED_PARAMETER(data);
  724. UNUSED_PARAMETER(linesize);
  725. UNUSED_PARAMETER(invert);
  726. }
  727. void gs_perspective(float angle, float aspect, float near, float far)
  728. {
  729. graphics_t graphics = thread_graphics;
  730. float xmin, xmax, ymin, ymax;
  731. if (!graphics) return;
  732. ymax = near * tanf(RAD(angle)*0.5f);
  733. ymin = -ymax;
  734. xmin = ymin * aspect;
  735. xmax = ymax * aspect;
  736. graphics->exports.device_frustum(graphics->device, xmin, xmax,
  737. ymin, ymax, near, far);
  738. }
  739. /* ------------------------------------------------------------------------- */
  740. const char *gs_preprocessor_name(void)
  741. {
  742. graphics_t graphics = thread_graphics;
  743. if (!graphics) return NULL;
  744. return graphics->exports.device_preprocessor_name();
  745. }
  746. swapchain_t gs_create_swapchain(struct gs_init_data *data)
  747. {
  748. graphics_t graphics = thread_graphics;
  749. if (!graphics) return NULL;
  750. return graphics->exports.device_create_swapchain(graphics->device,
  751. data);
  752. }
  753. void gs_resize(uint32_t x, uint32_t y)
  754. {
  755. graphics_t graphics = thread_graphics;
  756. if (!graphics) return;
  757. graphics->exports.device_resize(graphics->device, x, y);
  758. }
  759. void gs_getsize(uint32_t *x, uint32_t *y)
  760. {
  761. graphics_t graphics = thread_graphics;
  762. if (!graphics) return;
  763. graphics->exports.device_getsize(graphics->device, x, y);
  764. }
  765. uint32_t gs_getwidth(void)
  766. {
  767. graphics_t graphics = thread_graphics;
  768. if (!graphics) return 0;
  769. return graphics->exports.device_getwidth(graphics->device);
  770. }
  771. uint32_t gs_getheight(void)
  772. {
  773. graphics_t graphics = thread_graphics;
  774. if (!graphics) return 0;
  775. return graphics->exports.device_getheight(graphics->device);
  776. }
  777. static inline bool is_pow2(uint32_t size)
  778. {
  779. return size >= 2 && (size & (size-1)) == 0;
  780. }
  781. texture_t gs_create_texture(uint32_t width, uint32_t height,
  782. enum gs_color_format color_format, uint32_t levels,
  783. const void **data, uint32_t flags)
  784. {
  785. graphics_t graphics = thread_graphics;
  786. bool pow2tex = is_pow2(width) && is_pow2(height);
  787. bool uses_mipmaps = (flags & GS_BUILDMIPMAPS || levels != 1);
  788. if (!graphics)
  789. return NULL;
  790. if (uses_mipmaps && !pow2tex) {
  791. blog(LOG_WARNING, "Cannot use mipmaps with a "
  792. "non-power-of-two texture. Disabling "
  793. "mipmaps for this texture.");
  794. uses_mipmaps = false;
  795. flags &= ~GS_BUILDMIPMAPS;
  796. levels = 1;
  797. }
  798. if (uses_mipmaps && flags & GS_RENDERTARGET) {
  799. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  800. "Disabling mipmaps for this texture.");
  801. flags &= ~GS_BUILDMIPMAPS;
  802. levels = 1;
  803. }
  804. return graphics->exports.device_create_texture(graphics->device,
  805. width, height, color_format, levels, data, flags);
  806. }
  807. texture_t gs_create_cubetexture(uint32_t size,
  808. enum gs_color_format color_format, uint32_t levels,
  809. const void **data, uint32_t flags)
  810. {
  811. graphics_t graphics = thread_graphics;
  812. bool pow2tex = is_pow2(size);
  813. bool uses_mipmaps = (flags & GS_BUILDMIPMAPS || levels != 1);
  814. if (!graphics)
  815. return NULL;
  816. if (uses_mipmaps && !pow2tex) {
  817. blog(LOG_WARNING, "Cannot use mipmaps with a "
  818. "non-power-of-two texture. Disabling "
  819. "mipmaps for this texture.");
  820. uses_mipmaps = false;
  821. flags &= ~GS_BUILDMIPMAPS;
  822. levels = 1;
  823. }
  824. if (uses_mipmaps && flags & GS_RENDERTARGET) {
  825. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  826. "Disabling mipmaps for this texture.");
  827. flags &= ~GS_BUILDMIPMAPS;
  828. levels = 1;
  829. data = NULL;
  830. }
  831. return graphics->exports.device_create_cubetexture(graphics->device,
  832. size, color_format, levels, data, flags);
  833. }
  834. texture_t gs_create_volumetexture(uint32_t width, uint32_t height,
  835. uint32_t depth, enum gs_color_format color_format,
  836. uint32_t levels, const void **data, uint32_t flags)
  837. {
  838. graphics_t graphics = thread_graphics;
  839. if (!graphics) return NULL;
  840. return graphics->exports.device_create_volumetexture(graphics->device,
  841. width, height, depth, color_format, levels, data,
  842. flags);
  843. }
  844. zstencil_t gs_create_zstencil(uint32_t width, uint32_t height,
  845. enum gs_zstencil_format format)
  846. {
  847. graphics_t graphics = thread_graphics;
  848. if (!graphics) return NULL;
  849. return graphics->exports.device_create_zstencil(graphics->device,
  850. width, height, format);
  851. }
  852. stagesurf_t gs_create_stagesurface(uint32_t width, uint32_t height,
  853. enum gs_color_format color_format)
  854. {
  855. graphics_t graphics = thread_graphics;
  856. if (!graphics) return NULL;
  857. return graphics->exports.device_create_stagesurface(graphics->device,
  858. width, height, color_format);
  859. }
  860. samplerstate_t gs_create_samplerstate(struct gs_sampler_info *info)
  861. {
  862. graphics_t graphics = thread_graphics;
  863. if (!graphics) return NULL;
  864. return graphics->exports.device_create_samplerstate(graphics->device,
  865. info);
  866. }
  867. shader_t gs_create_vertexshader(const char *shader, const char *file,
  868. char **error_string)
  869. {
  870. graphics_t graphics = thread_graphics;
  871. if (!graphics) return NULL;
  872. return graphics->exports.device_create_vertexshader(graphics->device,
  873. shader, file, error_string);
  874. }
  875. shader_t gs_create_pixelshader(const char *shader,
  876. const char *file, char **error_string)
  877. {
  878. graphics_t graphics = thread_graphics;
  879. if (!graphics) return NULL;
  880. return graphics->exports.device_create_pixelshader(graphics->device,
  881. shader, file, error_string);
  882. }
  883. vertbuffer_t gs_create_vertexbuffer(struct vb_data *data,
  884. uint32_t flags)
  885. {
  886. graphics_t graphics = thread_graphics;
  887. if (!graphics) return NULL;
  888. return graphics->exports.device_create_vertexbuffer(graphics->device,
  889. data, flags);
  890. }
  891. indexbuffer_t gs_create_indexbuffer(enum gs_index_type type,
  892. void *indices, size_t num, uint32_t flags)
  893. {
  894. graphics_t graphics = thread_graphics;
  895. if (!graphics) return NULL;
  896. return graphics->exports.device_create_indexbuffer(graphics->device,
  897. type, indices, num, flags);
  898. }
  899. enum gs_texture_type gs_gettexturetype(texture_t texture)
  900. {
  901. graphics_t graphics = thread_graphics;
  902. if (!graphics) return GS_TEXTURE_2D;
  903. return graphics->exports.device_gettexturetype(texture);
  904. }
  905. void gs_load_vertexbuffer(vertbuffer_t vertbuffer)
  906. {
  907. graphics_t graphics = thread_graphics;
  908. if (!graphics) return;
  909. graphics->exports.device_load_vertexbuffer(graphics->device,
  910. vertbuffer);
  911. }
  912. void gs_load_indexbuffer(indexbuffer_t indexbuffer)
  913. {
  914. graphics_t graphics = thread_graphics;
  915. if (!graphics) return;
  916. graphics->exports.device_load_indexbuffer(graphics->device,
  917. indexbuffer);
  918. }
  919. void gs_load_texture(texture_t tex, int unit)
  920. {
  921. graphics_t graphics = thread_graphics;
  922. if (!graphics) return;
  923. graphics->exports.device_load_texture(graphics->device, tex, unit);
  924. }
  925. void gs_load_samplerstate(samplerstate_t samplerstate, int unit)
  926. {
  927. graphics_t graphics = thread_graphics;
  928. if (!graphics) return;
  929. graphics->exports.device_load_samplerstate(graphics->device,
  930. samplerstate, unit);
  931. }
  932. void gs_load_vertexshader(shader_t vertshader)
  933. {
  934. graphics_t graphics = thread_graphics;
  935. if (!graphics) return;
  936. graphics->exports.device_load_vertexshader(graphics->device,
  937. vertshader);
  938. }
  939. void gs_load_pixelshader(shader_t pixelshader)
  940. {
  941. graphics_t graphics = thread_graphics;
  942. if (!graphics) return;
  943. graphics->exports.device_load_pixelshader(graphics->device,
  944. pixelshader);
  945. }
  946. void gs_load_defaultsamplerstate(bool b_3d, int unit)
  947. {
  948. graphics_t graphics = thread_graphics;
  949. if (!graphics) return;
  950. graphics->exports.device_load_defaultsamplerstate(graphics->device,
  951. b_3d, unit);
  952. }
  953. shader_t gs_getvertexshader(void)
  954. {
  955. graphics_t graphics = thread_graphics;
  956. if (!graphics) return NULL;
  957. return graphics->exports.device_getvertexshader(graphics->device);
  958. }
  959. shader_t gs_getpixelshader(void)
  960. {
  961. graphics_t graphics = thread_graphics;
  962. if (!graphics) return NULL;
  963. return graphics->exports.device_getpixelshader(graphics->device);
  964. }
  965. texture_t gs_getrendertarget(void)
  966. {
  967. graphics_t graphics = thread_graphics;
  968. if (!graphics) return NULL;
  969. return graphics->exports.device_getrendertarget(graphics->device);
  970. }
  971. zstencil_t gs_getzstenciltarget(void)
  972. {
  973. graphics_t graphics = thread_graphics;
  974. if (!graphics) return NULL;
  975. return graphics->exports.device_getzstenciltarget(graphics->device);
  976. }
  977. void gs_setrendertarget(texture_t tex, zstencil_t zstencil)
  978. {
  979. graphics_t graphics = thread_graphics;
  980. if (!graphics) return;
  981. graphics->exports.device_setrendertarget(graphics->device, tex,
  982. zstencil);
  983. }
  984. void gs_setcuberendertarget(texture_t cubetex, int side, zstencil_t zstencil)
  985. {
  986. graphics_t graphics = thread_graphics;
  987. if (!graphics) return;
  988. graphics->exports.device_setcuberendertarget(graphics->device, cubetex,
  989. side, zstencil);
  990. }
  991. void gs_copy_texture(texture_t dst, texture_t src)
  992. {
  993. graphics_t graphics = thread_graphics;
  994. if (!graphics) return;
  995. graphics->exports.device_copy_texture(graphics->device, dst, src);
  996. }
  997. void gs_copy_texture_region(texture_t dst, uint32_t dst_x, uint32_t dst_y,
  998. texture_t src, uint32_t src_x, uint32_t src_y,
  999. uint32_t src_w, uint32_t src_h)
  1000. {
  1001. graphics_t graphics = thread_graphics;
  1002. if (!graphics) return;
  1003. graphics->exports.device_copy_texture_region(graphics->device,
  1004. dst, dst_x, dst_y,
  1005. src, src_x, src_y, src_w, src_h);
  1006. }
  1007. void gs_stage_texture(stagesurf_t dst, texture_t src)
  1008. {
  1009. graphics_t graphics = thread_graphics;
  1010. if (!graphics) return;
  1011. graphics->exports.device_stage_texture(graphics->device, dst, src);
  1012. }
  1013. void gs_beginscene(void)
  1014. {
  1015. graphics_t graphics = thread_graphics;
  1016. if (!graphics) return;
  1017. graphics->exports.device_beginscene(graphics->device);
  1018. }
  1019. void gs_draw(enum gs_draw_mode draw_mode, uint32_t start_vert,
  1020. uint32_t num_verts)
  1021. {
  1022. graphics_t graphics = thread_graphics;
  1023. if (!graphics) return;
  1024. graphics->exports.device_draw(graphics->device, draw_mode,
  1025. start_vert, num_verts);
  1026. }
  1027. void gs_endscene(void)
  1028. {
  1029. graphics_t graphics = thread_graphics;
  1030. if (!graphics) return;
  1031. graphics->exports.device_endscene(graphics->device);
  1032. }
  1033. void gs_load_swapchain(swapchain_t swapchain)
  1034. {
  1035. graphics_t graphics = thread_graphics;
  1036. if (!graphics) return;
  1037. graphics->exports.device_load_swapchain(graphics->device, swapchain);
  1038. }
  1039. void gs_clear(uint32_t clear_flags, struct vec4 *color, float depth,
  1040. uint8_t stencil)
  1041. {
  1042. graphics_t graphics = thread_graphics;
  1043. graphics->exports.device_clear(graphics->device, clear_flags, color,
  1044. depth, stencil);
  1045. }
  1046. void gs_present(void)
  1047. {
  1048. graphics_t graphics = thread_graphics;
  1049. if (!graphics) return;
  1050. graphics->exports.device_present(graphics->device);
  1051. }
  1052. void gs_setcullmode(enum gs_cull_mode mode)
  1053. {
  1054. graphics_t graphics = thread_graphics;
  1055. if (!graphics) return;
  1056. graphics->exports.device_setcullmode(graphics->device, mode);
  1057. }
  1058. enum gs_cull_mode gs_getcullmode(void)
  1059. {
  1060. graphics_t graphics = thread_graphics;
  1061. if (!graphics) return GS_NEITHER;
  1062. return graphics->exports.device_getcullmode(graphics->device);
  1063. }
  1064. void gs_enable_blending(bool enable)
  1065. {
  1066. graphics_t graphics = thread_graphics;
  1067. if (!graphics) return;
  1068. graphics->exports.device_enable_blending(graphics->device, enable);
  1069. }
  1070. void gs_enable_depthtest(bool enable)
  1071. {
  1072. graphics_t graphics = thread_graphics;
  1073. if (!graphics) return;
  1074. graphics->exports.device_enable_depthtest(graphics->device, enable);
  1075. }
  1076. void gs_enable_stenciltest(bool enable)
  1077. {
  1078. graphics_t graphics = thread_graphics;
  1079. if (!graphics) return;
  1080. graphics->exports.device_enable_stenciltest(graphics->device, enable);
  1081. }
  1082. void gs_enable_stencilwrite(bool enable)
  1083. {
  1084. graphics_t graphics = thread_graphics;
  1085. if (!graphics) return;
  1086. graphics->exports.device_enable_stencilwrite(graphics->device, enable);
  1087. }
  1088. void gs_enable_color(bool red, bool green, bool blue, bool alpha)
  1089. {
  1090. graphics_t graphics = thread_graphics;
  1091. if (!graphics) return;
  1092. graphics->exports.device_enable_color(graphics->device, red, green,
  1093. blue, alpha);
  1094. }
  1095. void gs_blendfunction(enum gs_blend_type src, enum gs_blend_type dest)
  1096. {
  1097. graphics_t graphics = thread_graphics;
  1098. if (!graphics) return;
  1099. graphics->exports.device_blendfunction(graphics->device, src, dest);
  1100. }
  1101. void gs_depthfunction(enum gs_depth_test test)
  1102. {
  1103. graphics_t graphics = thread_graphics;
  1104. if (!graphics) return;
  1105. graphics->exports.device_depthfunction(graphics->device, test);
  1106. }
  1107. void gs_stencilfunction(enum gs_stencil_side side, enum gs_depth_test test)
  1108. {
  1109. graphics_t graphics = thread_graphics;
  1110. if (!graphics) return;
  1111. graphics->exports.device_stencilfunction(graphics->device, side, test);
  1112. }
  1113. void gs_stencilop(enum gs_stencil_side side, enum gs_stencil_op fail,
  1114. enum gs_stencil_op zfail, enum gs_stencil_op zpass)
  1115. {
  1116. graphics_t graphics = thread_graphics;
  1117. if (!graphics) return;
  1118. graphics->exports.device_stencilop(graphics->device, side, fail, zfail,
  1119. zpass);
  1120. }
  1121. void gs_enable_fullscreen(bool enable)
  1122. {
  1123. graphics_t graphics = thread_graphics;
  1124. if (!graphics) return;
  1125. graphics->exports.device_enable_fullscreen(graphics->device, enable);
  1126. }
  1127. int gs_fullscreen_enabled(void)
  1128. {
  1129. graphics_t graphics = thread_graphics;
  1130. if (!graphics) return 0;
  1131. return graphics->exports.device_fullscreen_enabled(graphics->device);
  1132. }
  1133. void gs_setdisplaymode(const struct gs_display_mode *mode)
  1134. {
  1135. graphics_t graphics = thread_graphics;
  1136. if (!graphics) return;
  1137. graphics->exports.device_setdisplaymode(graphics->device, mode);
  1138. }
  1139. void gs_getdisplaymode(struct gs_display_mode *mode)
  1140. {
  1141. graphics_t graphics = thread_graphics;
  1142. if (!graphics) return;
  1143. graphics->exports.device_getdisplaymode(graphics->device, mode);
  1144. }
  1145. void gs_setcolorramp(float gamma, float brightness, float contrast)
  1146. {
  1147. graphics_t graphics = thread_graphics;
  1148. if (!graphics) return;
  1149. graphics->exports.device_setcolorramp(graphics->device, gamma,
  1150. brightness, contrast);
  1151. }
  1152. void gs_setviewport(int x, int y, int width, int height)
  1153. {
  1154. graphics_t graphics = thread_graphics;
  1155. if (!graphics) return;
  1156. graphics->exports.device_setviewport(graphics->device, x, y, width,
  1157. height);
  1158. }
  1159. void gs_getviewport(struct gs_rect *rect)
  1160. {
  1161. graphics_t graphics = thread_graphics;
  1162. if (!graphics) return;
  1163. graphics->exports.device_getviewport(graphics->device, rect);
  1164. }
  1165. void gs_setscissorrect(struct gs_rect *rect)
  1166. {
  1167. graphics_t graphics = thread_graphics;
  1168. if (!graphics) return;
  1169. graphics->exports.device_setscissorrect(graphics->device, rect);
  1170. }
  1171. void gs_ortho(float left, float right, float top, float bottom, float znear,
  1172. float zfar)
  1173. {
  1174. graphics_t graphics = thread_graphics;
  1175. if (!graphics) return;
  1176. graphics->exports.device_ortho(graphics->device, left, right, top,
  1177. bottom, znear, zfar);
  1178. }
  1179. void gs_frustum(float left, float right, float top, float bottom, float znear,
  1180. float zfar)
  1181. {
  1182. graphics_t graphics = thread_graphics;
  1183. if (!graphics) return;
  1184. graphics->exports.device_frustum(graphics->device, left, right, top,
  1185. bottom, znear, zfar);
  1186. }
  1187. void gs_projection_push(void)
  1188. {
  1189. graphics_t graphics = thread_graphics;
  1190. if (!graphics) return;
  1191. graphics->exports.device_projection_push(graphics->device);
  1192. }
  1193. void gs_projection_pop(void)
  1194. {
  1195. graphics_t graphics = thread_graphics;
  1196. if (!graphics) return;
  1197. graphics->exports.device_projection_pop(graphics->device);
  1198. }
  1199. void swapchain_destroy(swapchain_t swapchain)
  1200. {
  1201. graphics_t graphics = thread_graphics;
  1202. if (!graphics || !swapchain) return;
  1203. graphics->exports.swapchain_destroy(swapchain);
  1204. }
  1205. void shader_destroy(shader_t shader)
  1206. {
  1207. graphics_t graphics = thread_graphics;
  1208. if (!graphics || !shader) return;
  1209. graphics->exports.shader_destroy(shader);
  1210. }
  1211. int shader_numparams(shader_t shader)
  1212. {
  1213. graphics_t graphics = thread_graphics;
  1214. if (!graphics || !shader) return 0;
  1215. return graphics->exports.shader_numparams(shader);
  1216. }
  1217. sparam_t shader_getparambyidx(shader_t shader, uint32_t param)
  1218. {
  1219. graphics_t graphics = thread_graphics;
  1220. if (!graphics || !shader) return NULL;
  1221. return graphics->exports.shader_getparambyidx(shader, param);
  1222. }
  1223. sparam_t shader_getparambyname(shader_t shader, const char *name)
  1224. {
  1225. graphics_t graphics = thread_graphics;
  1226. if (!graphics || !shader) return NULL;
  1227. return graphics->exports.shader_getparambyname(shader, name);
  1228. }
  1229. void shader_getparaminfo(shader_t shader, sparam_t param,
  1230. struct shader_param_info *info)
  1231. {
  1232. graphics_t graphics = thread_graphics;
  1233. if (!graphics || !shader || !param) return;
  1234. graphics->exports.shader_getparaminfo(shader, param, info);
  1235. }
  1236. sparam_t shader_getviewprojmatrix(shader_t shader)
  1237. {
  1238. graphics_t graphics = thread_graphics;
  1239. if (!graphics || !shader) return NULL;
  1240. return graphics->exports.shader_getviewprojmatrix(shader);
  1241. }
  1242. sparam_t shader_getworldmatrix(shader_t shader)
  1243. {
  1244. graphics_t graphics = thread_graphics;
  1245. if (!graphics || !shader) return NULL;
  1246. return graphics->exports.shader_getworldmatrix(shader);
  1247. }
  1248. void shader_setbool(shader_t shader, sparam_t param, bool val)
  1249. {
  1250. graphics_t graphics = thread_graphics;
  1251. if (!graphics || !shader || !param) return;
  1252. graphics->exports.shader_setbool(shader, param, val);
  1253. }
  1254. void shader_setfloat(shader_t shader, sparam_t param, float val)
  1255. {
  1256. graphics_t graphics = thread_graphics;
  1257. if (!graphics || !shader || !param) return;
  1258. graphics->exports.shader_setfloat(shader, param, val);
  1259. }
  1260. void shader_setint(shader_t shader, sparam_t param, int val)
  1261. {
  1262. graphics_t graphics = thread_graphics;
  1263. if (!graphics || !shader || !param) return;
  1264. graphics->exports.shader_setint(shader, param, val);
  1265. }
  1266. void shader_setmatrix3(shader_t shader, sparam_t param,
  1267. const struct matrix3 *val)
  1268. {
  1269. graphics_t graphics = thread_graphics;
  1270. if (!graphics || !shader || !param) return;
  1271. graphics->exports.shader_setmatrix3(shader, param, val);
  1272. }
  1273. void shader_setmatrix4(shader_t shader, sparam_t param,
  1274. const struct matrix4 *val)
  1275. {
  1276. graphics_t graphics = thread_graphics;
  1277. if (!graphics || !shader || !param) return;
  1278. graphics->exports.shader_setmatrix4(shader, param, val);
  1279. }
  1280. void shader_setvec2(shader_t shader, sparam_t param,
  1281. const struct vec2 *val)
  1282. {
  1283. graphics_t graphics = thread_graphics;
  1284. if (!graphics || !shader || !param) return;
  1285. graphics->exports.shader_setvec2(shader, param, val);
  1286. }
  1287. void shader_setvec3(shader_t shader, sparam_t param,
  1288. const struct vec3 *val)
  1289. {
  1290. graphics_t graphics = thread_graphics;
  1291. if (!graphics || !shader || !param) return;
  1292. graphics->exports.shader_setvec3(shader, param, val);
  1293. }
  1294. void shader_setvec4(shader_t shader, sparam_t param,
  1295. const struct vec4 *val)
  1296. {
  1297. graphics_t graphics = thread_graphics;
  1298. if (!graphics || !shader || !param) return;
  1299. graphics->exports.shader_setvec4(shader, param, val);
  1300. }
  1301. void shader_settexture(shader_t shader, sparam_t param, texture_t val)
  1302. {
  1303. graphics_t graphics = thread_graphics;
  1304. if (!graphics || !shader || !param) return;
  1305. graphics->exports.shader_settexture(shader, param, val);
  1306. }
  1307. void shader_setval(shader_t shader, sparam_t param, const void *val,
  1308. size_t size)
  1309. {
  1310. graphics_t graphics = thread_graphics;
  1311. if (!graphics || !shader || !param) return;
  1312. graphics->exports.shader_setval(shader, param, val, size);
  1313. }
  1314. void shader_setdefault(shader_t shader, sparam_t param)
  1315. {
  1316. graphics_t graphics = thread_graphics;
  1317. if (!graphics || !shader || !param) return;
  1318. graphics->exports.shader_setdefault(shader, param);
  1319. }
  1320. void texture_destroy(texture_t tex)
  1321. {
  1322. graphics_t graphics = thread_graphics;
  1323. if (!graphics || !tex) return;
  1324. graphics->exports.texture_destroy(tex);
  1325. }
  1326. uint32_t texture_getwidth(texture_t tex)
  1327. {
  1328. graphics_t graphics = thread_graphics;
  1329. if (!graphics || !tex) return 0;
  1330. return graphics->exports.texture_getwidth(tex);
  1331. }
  1332. uint32_t texture_getheight(texture_t tex)
  1333. {
  1334. graphics_t graphics = thread_graphics;
  1335. if (!graphics || !tex) return 0;
  1336. return graphics->exports.texture_getheight(tex);
  1337. }
  1338. enum gs_color_format texture_getcolorformat(texture_t tex)
  1339. {
  1340. graphics_t graphics = thread_graphics;
  1341. if (!graphics || !tex) return GS_UNKNOWN;
  1342. return graphics->exports.texture_getcolorformat(tex);
  1343. }
  1344. bool texture_map(texture_t tex, void **ptr, uint32_t *linesize)
  1345. {
  1346. graphics_t graphics = thread_graphics;
  1347. if (!graphics || !tex) return false;
  1348. return graphics->exports.texture_map(tex, ptr, linesize);
  1349. }
  1350. void texture_unmap(texture_t tex)
  1351. {
  1352. graphics_t graphics = thread_graphics;
  1353. if (!graphics || !tex) return;
  1354. graphics->exports.texture_unmap(tex);
  1355. }
  1356. bool texture_isrect(texture_t tex)
  1357. {
  1358. graphics_t graphics = thread_graphics;
  1359. if (!graphics || !tex) return false;
  1360. if (graphics->exports.texture_isrect)
  1361. return graphics->exports.texture_isrect(tex);
  1362. else
  1363. return false;
  1364. }
  1365. void *texture_getobj(texture_t tex)
  1366. {
  1367. graphics_t graphics = thread_graphics;
  1368. if (!graphics || !tex) return NULL;
  1369. return graphics->exports.texture_getobj(tex);
  1370. }
  1371. void cubetexture_destroy(texture_t cubetex)
  1372. {
  1373. graphics_t graphics = thread_graphics;
  1374. if (!graphics || !cubetex) return;
  1375. graphics->exports.cubetexture_destroy(cubetex);
  1376. }
  1377. uint32_t cubetexture_getsize(texture_t cubetex)
  1378. {
  1379. graphics_t graphics = thread_graphics;
  1380. if (!graphics || !cubetex) return 0;
  1381. return graphics->exports.cubetexture_getsize(cubetex);
  1382. }
  1383. enum gs_color_format cubetexture_getcolorformat(texture_t cubetex)
  1384. {
  1385. graphics_t graphics = thread_graphics;
  1386. if (!graphics || !cubetex) return GS_UNKNOWN;
  1387. return graphics->exports.cubetexture_getcolorformat(cubetex);
  1388. }
  1389. void volumetexture_destroy(texture_t voltex)
  1390. {
  1391. graphics_t graphics = thread_graphics;
  1392. if (!graphics || !voltex) return;
  1393. graphics->exports.volumetexture_destroy(voltex);
  1394. }
  1395. uint32_t volumetexture_getwidth(texture_t voltex)
  1396. {
  1397. graphics_t graphics = thread_graphics;
  1398. if (!graphics || !voltex) return 0;
  1399. return graphics->exports.volumetexture_getwidth(voltex);
  1400. }
  1401. uint32_t volumetexture_getheight(texture_t voltex)
  1402. {
  1403. graphics_t graphics = thread_graphics;
  1404. if (!graphics || !voltex) return 0;
  1405. return graphics->exports.volumetexture_getheight(voltex);
  1406. }
  1407. uint32_t volumetexture_getdepth(texture_t voltex)
  1408. {
  1409. graphics_t graphics = thread_graphics;
  1410. if (!graphics || !voltex) return 0;
  1411. return graphics->exports.volumetexture_getdepth(voltex);
  1412. }
  1413. enum gs_color_format volumetexture_getcolorformat(texture_t voltex)
  1414. {
  1415. graphics_t graphics = thread_graphics;
  1416. if (!graphics || !voltex) return GS_UNKNOWN;
  1417. return graphics->exports.volumetexture_getcolorformat(voltex);
  1418. }
  1419. void stagesurface_destroy(stagesurf_t stagesurf)
  1420. {
  1421. graphics_t graphics = thread_graphics;
  1422. if (!graphics || !stagesurf) return;
  1423. graphics->exports.stagesurface_destroy(stagesurf);
  1424. }
  1425. uint32_t stagesurface_getwidth(stagesurf_t stagesurf)
  1426. {
  1427. graphics_t graphics = thread_graphics;
  1428. if (!graphics || !stagesurf) return 0;
  1429. return graphics->exports.stagesurface_getwidth(stagesurf);
  1430. }
  1431. uint32_t stagesurface_getheight(stagesurf_t stagesurf)
  1432. {
  1433. graphics_t graphics = thread_graphics;
  1434. if (!graphics || !stagesurf) return 0;
  1435. return graphics->exports.stagesurface_getheight(stagesurf);
  1436. }
  1437. enum gs_color_format stagesurface_getcolorformat(stagesurf_t stagesurf)
  1438. {
  1439. graphics_t graphics = thread_graphics;
  1440. if (!graphics || !stagesurf) return GS_UNKNOWN;
  1441. return graphics->exports.stagesurface_getcolorformat(stagesurf);
  1442. }
  1443. bool stagesurface_map(stagesurf_t stagesurf, uint8_t **data, uint32_t *linesize)
  1444. {
  1445. graphics_t graphics = thread_graphics;
  1446. if (!graphics || !stagesurf) return false;
  1447. return graphics->exports.stagesurface_map(stagesurf, data, linesize);
  1448. }
  1449. void stagesurface_unmap(stagesurf_t stagesurf)
  1450. {
  1451. graphics_t graphics = thread_graphics;
  1452. if (!graphics || !stagesurf) return;
  1453. graphics->exports.stagesurface_unmap(stagesurf);
  1454. }
  1455. void zstencil_destroy(zstencil_t zstencil)
  1456. {
  1457. if (!thread_graphics || !zstencil) return;
  1458. thread_graphics->exports.zstencil_destroy(zstencil);
  1459. }
  1460. void samplerstate_destroy(samplerstate_t samplerstate)
  1461. {
  1462. if (!thread_graphics || !samplerstate) return;
  1463. thread_graphics->exports.samplerstate_destroy(samplerstate);
  1464. }
  1465. void vertexbuffer_destroy(vertbuffer_t vertbuffer)
  1466. {
  1467. graphics_t graphics = thread_graphics;
  1468. if (!graphics || !vertbuffer) return;
  1469. graphics->exports.vertexbuffer_destroy(vertbuffer);
  1470. }
  1471. void vertexbuffer_flush(vertbuffer_t vertbuffer, bool rebuild)
  1472. {
  1473. if (!thread_graphics || !vertbuffer) return;
  1474. thread_graphics->exports.vertexbuffer_flush(vertbuffer, rebuild);
  1475. }
  1476. struct vb_data *vertexbuffer_getdata(vertbuffer_t vertbuffer)
  1477. {
  1478. if (!thread_graphics || !vertbuffer) return NULL;
  1479. return thread_graphics->exports.vertexbuffer_getdata(vertbuffer);
  1480. }
  1481. void indexbuffer_destroy(indexbuffer_t indexbuffer)
  1482. {
  1483. graphics_t graphics = thread_graphics;
  1484. if (!graphics || !indexbuffer) return;
  1485. graphics->exports.indexbuffer_destroy(indexbuffer);
  1486. }
  1487. void indexbuffer_flush(indexbuffer_t indexbuffer)
  1488. {
  1489. if (!thread_graphics || !indexbuffer) return;
  1490. thread_graphics->exports.indexbuffer_flush(indexbuffer);
  1491. }
  1492. void *indexbuffer_getdata(indexbuffer_t indexbuffer)
  1493. {
  1494. if (!thread_graphics || !indexbuffer) return NULL;
  1495. return thread_graphics->exports.indexbuffer_getdata(indexbuffer);
  1496. }
  1497. size_t indexbuffer_numindices(indexbuffer_t indexbuffer)
  1498. {
  1499. if (!thread_graphics || !indexbuffer) return 0;
  1500. return thread_graphics->exports.indexbuffer_numindices(indexbuffer);
  1501. }
  1502. enum gs_index_type indexbuffer_gettype(indexbuffer_t indexbuffer)
  1503. {
  1504. if (!thread_graphics || !indexbuffer) return (enum gs_index_type)0;
  1505. return thread_graphics->exports.indexbuffer_gettype(indexbuffer);
  1506. }
  1507. #ifdef __APPLE__
  1508. /** Platform specific functions */
  1509. texture_t gs_create_texture_from_iosurface(void *iosurf)
  1510. {
  1511. graphics_t graphics = thread_graphics;
  1512. if (!graphics || !iosurf ||
  1513. !graphics->exports.texture_create_from_iosurface)
  1514. return NULL;
  1515. return graphics->exports.texture_create_from_iosurface(
  1516. graphics->device, iosurf);
  1517. }
  1518. bool texture_rebind_iosurface(texture_t texture, void *iosurf)
  1519. {
  1520. graphics_t graphics = thread_graphics;
  1521. if (!graphics || !iosurf || !graphics->exports.texture_rebind_iosurface)
  1522. return false;
  1523. return graphics->exports.texture_rebind_iosurface(texture, iosurf);
  1524. }
  1525. #elif _WIN32
  1526. bool gs_gdi_texture_available(void)
  1527. {
  1528. if (!thread_graphics)
  1529. return false;
  1530. return thread_graphics->exports.gdi_texture_available();
  1531. }
  1532. /** creates a windows GDI-lockable texture */
  1533. texture_t gs_create_gdi_texture(uint32_t width, uint32_t height)
  1534. {
  1535. graphics_t graphics = thread_graphics;
  1536. if (!graphics) return NULL;
  1537. if (graphics->exports.device_create_gdi_texture)
  1538. return graphics->exports.device_create_gdi_texture(
  1539. graphics->device, width, height);
  1540. return NULL;
  1541. }
  1542. void *texture_get_dc(texture_t gdi_tex)
  1543. {
  1544. if (!thread_graphics || !gdi_tex)
  1545. return NULL;
  1546. if (thread_graphics->exports.texture_get_dc)
  1547. return thread_graphics->exports.texture_get_dc(gdi_tex);
  1548. return NULL;
  1549. }
  1550. void texture_release_dc(texture_t gdi_tex)
  1551. {
  1552. if (!thread_graphics || !gdi_tex)
  1553. return;
  1554. if (thread_graphics->exports.texture_release_dc)
  1555. thread_graphics->exports.texture_release_dc(gdi_tex);
  1556. }
  1557. #endif