graphics.c 46 KB

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