graphics.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  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. void gs_enum_adapters(
  32. bool (*callback)(void *param, const char *name, uint32_t id),
  33. void *param)
  34. {
  35. graphics_t *graphics = thread_graphics;
  36. if (graphics && graphics->exports.device_enum_adapters) {
  37. if (graphics->exports.device_enum_adapters(callback, param)) {
  38. return;
  39. }
  40. }
  41. /* If the subsystem does not currently support device enumeration of
  42. * adapters or fails to enumerate adapters, just set it to one adapter
  43. * named "Default" */
  44. callback(param, "Default", 0);
  45. }
  46. extern void gs_init_image_deps(void);
  47. extern void gs_free_image_deps(void);
  48. bool load_graphics_imports(struct gs_exports *exports, void *module,
  49. const char *module_name);
  50. static bool graphics_init_immediate_vb(struct graphics_subsystem *graphics)
  51. {
  52. struct gs_vb_data *vbd;
  53. vbd = gs_vbdata_create();
  54. vbd->num = IMMEDIATE_COUNT;
  55. vbd->points = bmalloc(sizeof(struct vec3)*IMMEDIATE_COUNT);
  56. vbd->normals = bmalloc(sizeof(struct vec3)*IMMEDIATE_COUNT);
  57. vbd->colors = bmalloc(sizeof(uint32_t) *IMMEDIATE_COUNT);
  58. vbd->num_tex = 1;
  59. vbd->tvarray = bmalloc(sizeof(struct gs_tvertarray));
  60. vbd->tvarray[0].width = 2;
  61. vbd->tvarray[0].array =
  62. bmalloc(sizeof(struct vec2) * IMMEDIATE_COUNT);
  63. graphics->immediate_vertbuffer = graphics->exports.
  64. device_vertexbuffer_create(graphics->device, vbd, GS_DYNAMIC);
  65. if (!graphics->immediate_vertbuffer)
  66. return false;
  67. return true;
  68. }
  69. static bool graphics_init_sprite_vb(struct graphics_subsystem *graphics)
  70. {
  71. struct gs_vb_data *vbd;
  72. vbd = gs_vbdata_create();
  73. vbd->num = 4;
  74. vbd->points = bmalloc(sizeof(struct vec3) * 4);
  75. vbd->num_tex = 1;
  76. vbd->tvarray = bmalloc(sizeof(struct gs_tvertarray));
  77. vbd->tvarray[0].width = 2;
  78. vbd->tvarray[0].array = bmalloc(sizeof(struct vec2) * 4);
  79. memset(vbd->points, 0, sizeof(struct vec3) * 4);
  80. memset(vbd->tvarray[0].array, 0, sizeof(struct vec2) * 4);
  81. graphics->sprite_buffer = graphics->exports.
  82. device_vertexbuffer_create(graphics->device, vbd, GS_DYNAMIC);
  83. if (!graphics->sprite_buffer)
  84. return false;
  85. return true;
  86. }
  87. static bool graphics_init(struct graphics_subsystem *graphics)
  88. {
  89. struct matrix4 top_mat;
  90. matrix4_identity(&top_mat);
  91. da_push_back(graphics->matrix_stack, &top_mat);
  92. graphics->exports.device_enter_context(graphics->device);
  93. if (!graphics_init_immediate_vb(graphics))
  94. return false;
  95. if (!graphics_init_sprite_vb(graphics))
  96. return false;
  97. if (pthread_mutex_init(&graphics->mutex, NULL) != 0)
  98. return false;
  99. graphics->exports.device_blend_function(graphics->device,
  100. GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  101. graphics->cur_blend_state.enabled = true;
  102. graphics->cur_blend_state.src = GS_BLEND_SRCALPHA;
  103. graphics->cur_blend_state.dest = GS_BLEND_INVSRCALPHA;
  104. graphics->exports.device_leave_context(graphics->device);
  105. gs_init_image_deps();
  106. return true;
  107. }
  108. int gs_create(graphics_t **pgraphics, const char *module,
  109. const struct gs_init_data *data)
  110. {
  111. struct gs_init_data new_data = *data;
  112. int errcode = GS_ERROR_FAIL;
  113. graphics_t *graphics = bzalloc(sizeof(struct graphics_subsystem));
  114. pthread_mutex_init_value(&graphics->mutex);
  115. if (!new_data.num_backbuffers)
  116. new_data.num_backbuffers = 1;
  117. graphics->module = os_dlopen(module);
  118. if (!graphics->module) {
  119. errcode = GS_ERROR_MODULE_NOT_FOUND;
  120. goto error;
  121. }
  122. if (!load_graphics_imports(&graphics->exports, graphics->module,
  123. module))
  124. goto error;
  125. errcode = graphics->exports.device_create(&graphics->device, &new_data);
  126. if (errcode != GS_SUCCESS)
  127. goto error;
  128. if (!graphics_init(graphics)) {
  129. errcode = GS_ERROR_FAIL;
  130. goto error;
  131. }
  132. *pgraphics = graphics;
  133. return errcode;
  134. error:
  135. gs_destroy(graphics);
  136. return errcode;
  137. }
  138. void gs_destroy(graphics_t *graphics)
  139. {
  140. if (!graphics)
  141. return;
  142. while (thread_graphics)
  143. gs_leave_context();
  144. if (graphics->device) {
  145. graphics->exports.device_enter_context(graphics->device);
  146. graphics->exports.gs_vertexbuffer_destroy(
  147. graphics->sprite_buffer);
  148. graphics->exports.gs_vertexbuffer_destroy(
  149. graphics->immediate_vertbuffer);
  150. graphics->exports.device_destroy(graphics->device);
  151. }
  152. pthread_mutex_destroy(&graphics->mutex);
  153. da_free(graphics->matrix_stack);
  154. da_free(graphics->viewport_stack);
  155. if (graphics->module)
  156. os_dlclose(graphics->module);
  157. bfree(graphics);
  158. gs_free_image_deps();
  159. }
  160. void gs_enter_context(graphics_t *graphics)
  161. {
  162. if (!graphics) return;
  163. bool is_current = thread_graphics == graphics;
  164. if (thread_graphics && !is_current) {
  165. while (thread_graphics)
  166. gs_leave_context();
  167. }
  168. if (!is_current) {
  169. pthread_mutex_lock(&graphics->mutex);
  170. graphics->exports.device_enter_context(graphics->device);
  171. thread_graphics = graphics;
  172. }
  173. os_atomic_inc_long(&graphics->ref);
  174. }
  175. void gs_leave_context(void)
  176. {
  177. if (thread_graphics) {
  178. if (!os_atomic_dec_long(&thread_graphics->ref)) {
  179. graphics_t *graphics = thread_graphics;
  180. graphics->exports.device_leave_context(
  181. graphics->device);
  182. pthread_mutex_unlock(&graphics->mutex);
  183. thread_graphics = NULL;
  184. }
  185. }
  186. }
  187. graphics_t *gs_get_context(void)
  188. {
  189. return thread_graphics;
  190. }
  191. const char *gs_get_device_name(void)
  192. {
  193. return thread_graphics ?
  194. thread_graphics->exports.device_get_name() : NULL;
  195. }
  196. int gs_get_device_type(void)
  197. {
  198. return thread_graphics ?
  199. thread_graphics->exports.device_get_type() : -1;
  200. }
  201. static inline struct matrix4 *top_matrix(graphics_t *graphics)
  202. {
  203. return graphics ?
  204. (graphics->matrix_stack.array + graphics->cur_matrix) : NULL;
  205. }
  206. void gs_matrix_push(void)
  207. {
  208. graphics_t *graphics = thread_graphics;
  209. if (!graphics)
  210. return;
  211. struct matrix4 mat, *top_mat = top_matrix(graphics);
  212. memcpy(&mat, top_mat, sizeof(struct matrix4));
  213. da_push_back(graphics->matrix_stack, &mat);
  214. graphics->cur_matrix++;
  215. }
  216. void gs_matrix_pop(void)
  217. {
  218. graphics_t *graphics = thread_graphics;
  219. if (!graphics)
  220. return;
  221. if (graphics->cur_matrix == 0) {
  222. blog(LOG_ERROR, "Tried to pop last matrix on stack");
  223. return;
  224. }
  225. da_erase(graphics->matrix_stack, graphics->cur_matrix);
  226. graphics->cur_matrix--;
  227. }
  228. void gs_matrix_identity(void)
  229. {
  230. struct matrix4 *top_mat = top_matrix(thread_graphics);
  231. if (top_mat)
  232. matrix4_identity(top_mat);
  233. }
  234. void gs_matrix_transpose(void)
  235. {
  236. struct matrix4 *top_mat = top_matrix(thread_graphics);
  237. if (top_mat)
  238. matrix4_transpose(top_mat, top_mat);
  239. }
  240. void gs_matrix_set(const struct matrix4 *matrix)
  241. {
  242. struct matrix4 *top_mat = top_matrix(thread_graphics);
  243. if (top_mat)
  244. matrix4_copy(top_mat, matrix);
  245. }
  246. void gs_matrix_get(struct matrix4 *dst)
  247. {
  248. struct matrix4 *top_mat = top_matrix(thread_graphics);
  249. if (top_mat)
  250. matrix4_copy(dst, top_mat);
  251. }
  252. void gs_matrix_mul(const struct matrix4 *matrix)
  253. {
  254. struct matrix4 *top_mat = top_matrix(thread_graphics);
  255. if (top_mat)
  256. matrix4_mul(top_mat, matrix, top_mat);
  257. }
  258. void gs_matrix_rotquat(const struct quat *rot)
  259. {
  260. struct matrix4 *top_mat = top_matrix(thread_graphics);
  261. if (top_mat)
  262. matrix4_rotate_i(top_mat, rot, top_mat);
  263. }
  264. void gs_matrix_rotaa(const struct axisang *rot)
  265. {
  266. struct matrix4 *top_mat = top_matrix(thread_graphics);
  267. if (top_mat)
  268. matrix4_rotate_aa_i(top_mat, rot, top_mat);
  269. }
  270. void gs_matrix_translate(const struct vec3 *pos)
  271. {
  272. struct matrix4 *top_mat = top_matrix(thread_graphics);
  273. if (top_mat)
  274. matrix4_translate3v_i(top_mat, pos, top_mat);
  275. }
  276. void gs_matrix_scale(const struct vec3 *scale)
  277. {
  278. struct matrix4 *top_mat = top_matrix(thread_graphics);
  279. if (top_mat)
  280. matrix4_scale_i(top_mat, scale, top_mat);
  281. }
  282. void gs_matrix_rotaa4f(float x, float y, float z, float angle)
  283. {
  284. struct matrix4 *top_mat = top_matrix(thread_graphics);
  285. struct axisang aa;
  286. if (top_mat) {
  287. axisang_set(&aa, x, y, z, angle);
  288. matrix4_rotate_aa_i(top_mat, &aa, top_mat);
  289. }
  290. }
  291. void gs_matrix_translate3f(float x, float y, float z)
  292. {
  293. struct matrix4 *top_mat = top_matrix(thread_graphics);
  294. struct vec3 p;
  295. if (top_mat) {
  296. vec3_set(&p, x, y, z);
  297. matrix4_translate3v_i(top_mat, &p, top_mat);
  298. }
  299. }
  300. void gs_matrix_scale3f(float x, float y, float z)
  301. {
  302. struct matrix4 *top_mat = top_matrix(thread_graphics);
  303. struct vec3 p;
  304. if (top_mat) {
  305. vec3_set(&p, x, y, z);
  306. matrix4_scale_i(top_mat, &p, top_mat);
  307. }
  308. }
  309. static inline void reset_immediate_arrays(graphics_t *graphics)
  310. {
  311. da_init(graphics->verts);
  312. da_init(graphics->norms);
  313. da_init(graphics->colors);
  314. for (size_t i = 0; i < 16; i++)
  315. da_init(graphics->texverts[i]);
  316. }
  317. void gs_render_start(bool b_new)
  318. {
  319. graphics_t *graphics = thread_graphics;
  320. if (!graphics)
  321. return;
  322. graphics->using_immediate = !b_new;
  323. reset_immediate_arrays(graphics);
  324. if (b_new) {
  325. graphics->vbd = gs_vbdata_create();
  326. } else {
  327. graphics->vbd = gs_vertexbuffer_get_data(
  328. graphics->immediate_vertbuffer);
  329. memset(graphics->vbd->colors, 0xFF,
  330. sizeof(uint32_t) * IMMEDIATE_COUNT);
  331. graphics->verts.array = graphics->vbd->points;
  332. graphics->norms.array = graphics->vbd->normals;
  333. graphics->colors.array = graphics->vbd->colors;
  334. graphics->texverts[0].array = graphics->vbd->tvarray[0].array;
  335. graphics->verts.capacity = IMMEDIATE_COUNT;
  336. graphics->norms.capacity = IMMEDIATE_COUNT;
  337. graphics->colors.capacity = IMMEDIATE_COUNT;
  338. graphics->texverts[0].capacity = IMMEDIATE_COUNT;
  339. }
  340. }
  341. static inline size_t min_size(const size_t a, const size_t b)
  342. {
  343. return (a < b) ? a : b;
  344. }
  345. void gs_render_stop(enum gs_draw_mode mode)
  346. {
  347. graphics_t *graphics = thread_graphics;
  348. size_t i, num;
  349. if (!graphics)
  350. return;
  351. num = graphics->verts.num;
  352. if (!num) {
  353. if (!graphics->using_immediate) {
  354. da_free(graphics->verts);
  355. da_free(graphics->norms);
  356. da_free(graphics->colors);
  357. for (i = 0; i < 16; i++)
  358. da_free(graphics->texverts[i]);
  359. gs_vbdata_destroy(graphics->vbd);
  360. }
  361. return;
  362. }
  363. if (graphics->norms.num &&
  364. (graphics->norms.num != graphics->verts.num)) {
  365. blog(LOG_ERROR, "gs_render_stop: normal count does "
  366. "not match vertex count");
  367. num = min_size(num, graphics->norms.num);
  368. }
  369. if (graphics->colors.num &&
  370. (graphics->colors.num != graphics->verts.num)) {
  371. blog(LOG_ERROR, "gs_render_stop: color count does "
  372. "not match vertex count");
  373. num = min_size(num, graphics->colors.num);
  374. }
  375. if (graphics->texverts[0].num &&
  376. (graphics->texverts[0].num != graphics->verts.num)) {
  377. blog(LOG_ERROR, "gs_render_stop: texture vertex count does "
  378. "not match vertex count");
  379. num = min_size(num, graphics->texverts[0].num);
  380. }
  381. if (graphics->using_immediate) {
  382. gs_vertexbuffer_flush(graphics->immediate_vertbuffer);
  383. gs_load_vertexbuffer(graphics->immediate_vertbuffer);
  384. gs_load_indexbuffer(NULL);
  385. gs_draw(mode, 0, (uint32_t)num);
  386. reset_immediate_arrays(graphics);
  387. } else {
  388. gs_vertbuffer_t *vb = gs_render_save();
  389. gs_load_vertexbuffer(vb);
  390. gs_load_indexbuffer(NULL);
  391. gs_draw(mode, 0, 0);
  392. gs_vertexbuffer_destroy(vb);
  393. }
  394. graphics->vbd = NULL;
  395. }
  396. gs_vertbuffer_t *gs_render_save(void)
  397. {
  398. graphics_t *graphics = thread_graphics;
  399. size_t num_tex, i;
  400. if (!graphics)
  401. return NULL;
  402. if (graphics->using_immediate)
  403. return NULL;
  404. if (!graphics->verts.num) {
  405. gs_vbdata_destroy(graphics->vbd);
  406. return NULL;
  407. }
  408. for (num_tex = 0; num_tex < 16; num_tex++)
  409. if (!graphics->texverts[num_tex].num)
  410. break;
  411. graphics->vbd->points = graphics->verts.array;
  412. graphics->vbd->normals = graphics->norms.array;
  413. graphics->vbd->colors = graphics->colors.array;
  414. graphics->vbd->num = graphics->verts.num;
  415. graphics->vbd->num_tex = num_tex;
  416. if (graphics->vbd->num_tex) {
  417. graphics->vbd->tvarray =
  418. bmalloc(sizeof(struct gs_tvertarray) * num_tex);
  419. for (i = 0; i < num_tex; i++) {
  420. graphics->vbd->tvarray[i].width = 2;
  421. graphics->vbd->tvarray[i].array =
  422. graphics->texverts[i].array;
  423. }
  424. }
  425. reset_immediate_arrays(graphics);
  426. return gs_vertexbuffer_create(graphics->vbd, 0);
  427. }
  428. void gs_vertex2f(float x, float y)
  429. {
  430. struct vec3 v3;
  431. vec3_set(&v3, x, y, 0.0f);
  432. gs_vertex3v(&v3);
  433. }
  434. void gs_vertex3f(float x, float y, float z)
  435. {
  436. struct vec3 v3;
  437. vec3_set(&v3, x, y, z);
  438. gs_vertex3v(&v3);
  439. }
  440. void gs_normal3f(float x, float y, float z)
  441. {
  442. struct vec3 v3;
  443. vec3_set(&v3, x, y, z);
  444. gs_normal3v(&v3);
  445. }
  446. static inline bool validvertsize(graphics_t *graphics, size_t num,
  447. const char *name)
  448. {
  449. if (graphics->using_immediate && num == IMMEDIATE_COUNT) {
  450. blog(LOG_ERROR, "%s: tried to use over %u "
  451. "for immediate rendering",
  452. name, IMMEDIATE_COUNT);
  453. return false;
  454. }
  455. return true;
  456. }
  457. void gs_color(uint32_t color)
  458. {
  459. graphics_t *graphics = thread_graphics;
  460. if (!graphics)
  461. return;
  462. if (!validvertsize(graphics, graphics->colors.num, "gs_color"))
  463. return;
  464. da_push_back(graphics->colors, &color);
  465. }
  466. void gs_texcoord(float x, float y, int unit)
  467. {
  468. struct vec2 v2;
  469. vec2_set(&v2, x, y);
  470. gs_texcoord2v(&v2, unit);
  471. }
  472. void gs_vertex2v(const struct vec2 *v)
  473. {
  474. struct vec3 v3;
  475. vec3_set(&v3, v->x, v->y, 0.0f);
  476. gs_vertex3v(&v3);
  477. }
  478. void gs_vertex3v(const struct vec3 *v)
  479. {
  480. graphics_t *graphics = thread_graphics;
  481. if (!graphics)
  482. return;
  483. if (!validvertsize(graphics, graphics->verts.num, "gs_vertex"))
  484. return;
  485. da_push_back(graphics->verts, v);
  486. }
  487. void gs_normal3v(const struct vec3 *v)
  488. {
  489. graphics_t *graphics = thread_graphics;
  490. if (!graphics)
  491. return;
  492. if (!validvertsize(graphics, graphics->norms.num, "gs_normal"))
  493. return;
  494. da_push_back(graphics->norms, v);
  495. }
  496. void gs_color4v(const struct vec4 *v)
  497. {
  498. /* TODO */
  499. UNUSED_PARAMETER(v);
  500. }
  501. void gs_texcoord2v(const struct vec2 *v, int unit)
  502. {
  503. graphics_t *graphics = thread_graphics;
  504. if (!graphics)
  505. return;
  506. if (!validvertsize(graphics, graphics->texverts[unit].num,
  507. "gs_texcoord"))
  508. return;
  509. da_push_back(graphics->texverts[unit], v);
  510. }
  511. input_t *gs_get_input(void)
  512. {
  513. /* TODO */
  514. return NULL;
  515. }
  516. gs_effect_t *gs_get_effect(void)
  517. {
  518. return thread_graphics ? thread_graphics->cur_effect : NULL;
  519. }
  520. gs_effect_t *gs_effect_create_from_file(const char *file, char **error_string)
  521. {
  522. char *file_string;
  523. gs_effect_t *effect = NULL;
  524. if (!thread_graphics || !file)
  525. return NULL;
  526. file_string = os_quick_read_utf8_file(file);
  527. if (!file_string) {
  528. blog(LOG_ERROR, "Could not load effect file '%s'", file);
  529. return NULL;
  530. }
  531. effect = gs_effect_create(file_string, file, error_string);
  532. bfree(file_string);
  533. return effect;
  534. }
  535. gs_effect_t *gs_effect_create(const char *effect_string, const char *filename,
  536. char **error_string)
  537. {
  538. if (!thread_graphics || !effect_string)
  539. return NULL;
  540. struct gs_effect *effect = bzalloc(sizeof(struct gs_effect));
  541. struct effect_parser parser;
  542. bool success;
  543. effect->graphics = thread_graphics;
  544. ep_init(&parser);
  545. success = ep_parse(&parser, effect, effect_string, filename);
  546. if (!success) {
  547. if (error_string)
  548. *error_string = error_data_buildstring(
  549. &parser.cfp.error_list);
  550. gs_effect_destroy(effect);
  551. effect = NULL;
  552. }
  553. ep_free(&parser);
  554. return effect;
  555. }
  556. gs_shader_t *gs_vertexshader_create_from_file(const char *file,
  557. char **error_string)
  558. {
  559. if (!thread_graphics || !file)
  560. return NULL;
  561. char *file_string;
  562. gs_shader_t *shader = NULL;
  563. file_string = os_quick_read_utf8_file(file);
  564. if (!file_string) {
  565. blog(LOG_ERROR, "Could not load vertex shader file '%s'",
  566. file);
  567. return NULL;
  568. }
  569. shader = gs_vertexshader_create(file_string, file, error_string);
  570. bfree(file_string);
  571. return shader;
  572. }
  573. gs_shader_t *gs_pixelshader_create_from_file(const char *file,
  574. char **error_string)
  575. {
  576. char *file_string;
  577. gs_shader_t *shader = NULL;
  578. if (!thread_graphics || !file)
  579. return NULL;
  580. file_string = os_quick_read_utf8_file(file);
  581. if (!file_string) {
  582. blog(LOG_ERROR, "Could not load pixel shader file '%s'",
  583. file);
  584. return NULL;
  585. }
  586. shader = gs_pixelshader_create(file_string, file, error_string);
  587. bfree(file_string);
  588. return shader;
  589. }
  590. static inline void assign_sprite_rect(float *start, float *end, float size,
  591. bool flip)
  592. {
  593. if (!flip) {
  594. *start = 0.0f;
  595. *end = size;
  596. } else {
  597. *start = size;
  598. *end = 0.0f;
  599. }
  600. }
  601. static inline void assign_sprite_uv(float *start, float *end, bool flip)
  602. {
  603. if (!flip) {
  604. *start = 0.0f;
  605. *end = 1.0f;
  606. } else {
  607. *start = 1.0f;
  608. *end = 0.0f;
  609. }
  610. }
  611. static void build_sprite(struct gs_vb_data *data, float fcx, float fcy,
  612. float start_u, float end_u, float start_v, float end_v)
  613. {
  614. struct vec2 *tvarray = data->tvarray[0].array;
  615. vec3_zero(data->points);
  616. vec3_set(data->points+1, fcx, 0.0f, 0.0f);
  617. vec3_set(data->points+2, 0.0f, fcy, 0.0f);
  618. vec3_set(data->points+3, fcx, fcy, 0.0f);
  619. vec2_set(tvarray, start_u, start_v);
  620. vec2_set(tvarray+1, end_u, start_v);
  621. vec2_set(tvarray+2, start_u, end_v);
  622. vec2_set(tvarray+3, end_u, end_v);
  623. }
  624. static inline void build_sprite_norm(struct gs_vb_data *data, float fcx,
  625. float fcy, uint32_t flip)
  626. {
  627. float start_u, end_u;
  628. float start_v, end_v;
  629. assign_sprite_uv(&start_u, &end_u, (flip & GS_FLIP_U) != 0);
  630. assign_sprite_uv(&start_v, &end_v, (flip & GS_FLIP_V) != 0);
  631. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
  632. }
  633. static inline void build_sprite_rect(struct gs_vb_data *data, gs_texture_t *tex,
  634. float fcx, float fcy, uint32_t flip)
  635. {
  636. float start_u, end_u;
  637. float start_v, end_v;
  638. float width = (float)gs_texture_get_width(tex);
  639. float height = (float)gs_texture_get_height(tex);
  640. assign_sprite_rect(&start_u, &end_u, width, (flip & GS_FLIP_U) != 0);
  641. assign_sprite_rect(&start_v, &end_v, height, (flip & GS_FLIP_V) != 0);
  642. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
  643. }
  644. void gs_draw_sprite(gs_texture_t *tex, uint32_t flip, uint32_t width,
  645. uint32_t height)
  646. {
  647. graphics_t *graphics = thread_graphics;
  648. float fcx, fcy;
  649. struct gs_vb_data *data;
  650. assert(tex);
  651. if (!tex || !thread_graphics)
  652. return;
  653. if (gs_get_texture_type(tex) != GS_TEXTURE_2D) {
  654. blog(LOG_ERROR, "A sprite must be a 2D texture");
  655. return;
  656. }
  657. fcx = width ? (float)width : (float)gs_texture_get_width(tex);
  658. fcy = height ? (float)height : (float)gs_texture_get_height(tex);
  659. data = gs_vertexbuffer_get_data(graphics->sprite_buffer);
  660. if (gs_texture_is_rect(tex))
  661. build_sprite_rect(data, tex, fcx, fcy, flip);
  662. else
  663. build_sprite_norm(data, fcx, fcy, flip);
  664. gs_vertexbuffer_flush(graphics->sprite_buffer);
  665. gs_load_vertexbuffer(graphics->sprite_buffer);
  666. gs_load_indexbuffer(NULL);
  667. gs_draw(GS_TRISTRIP, 0, 0);
  668. }
  669. void gs_draw_cube_backdrop(gs_texture_t *cubetex, const struct quat *rot,
  670. float left, float right, float top, float bottom, float znear)
  671. {
  672. /* TODO */
  673. UNUSED_PARAMETER(cubetex);
  674. UNUSED_PARAMETER(rot);
  675. UNUSED_PARAMETER(left);
  676. UNUSED_PARAMETER(right);
  677. UNUSED_PARAMETER(top);
  678. UNUSED_PARAMETER(bottom);
  679. UNUSED_PARAMETER(znear);
  680. }
  681. void gs_reset_viewport(void)
  682. {
  683. uint32_t cx, cy;
  684. assert(thread_graphics != NULL);
  685. gs_get_size(&cx, &cy);
  686. gs_set_viewport(0, 0, (int)cx, (int)cy);
  687. }
  688. void gs_set_2d_mode(void)
  689. {
  690. uint32_t cx, cy;
  691. assert(thread_graphics != NULL);
  692. gs_get_size(&cx, &cy);
  693. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -1.0, -1024.0f);
  694. }
  695. void gs_set_3d_mode(double fovy, double znear, double zvar)
  696. {
  697. /* TODO */
  698. UNUSED_PARAMETER(fovy);
  699. UNUSED_PARAMETER(znear);
  700. UNUSED_PARAMETER(zvar);
  701. }
  702. void gs_viewport_push(void)
  703. {
  704. if (!thread_graphics) return;
  705. struct gs_rect *rect = da_push_back_new(
  706. thread_graphics->viewport_stack);
  707. gs_get_viewport(rect);
  708. }
  709. void gs_viewport_pop(void)
  710. {
  711. struct gs_rect *rect;
  712. if (!thread_graphics || !thread_graphics->viewport_stack.num)
  713. return;
  714. rect = da_end(thread_graphics->viewport_stack);
  715. gs_set_viewport(rect->x, rect->y, rect->cx, rect->cy);
  716. da_pop_back(thread_graphics->viewport_stack);
  717. }
  718. void gs_texture_set_image(gs_texture_t *tex, const uint8_t *data,
  719. uint32_t linesize, bool flip)
  720. {
  721. uint8_t *ptr;
  722. uint32_t linesize_out;
  723. uint32_t row_copy;
  724. int32_t height;
  725. int32_t y;
  726. if (!thread_graphics || !tex)
  727. return;
  728. height = (int32_t)gs_texture_get_height(tex);
  729. if (!gs_texture_map(tex, &ptr, &linesize_out))
  730. return;
  731. row_copy = (linesize < linesize_out) ? linesize : linesize_out;
  732. if (flip) {
  733. for (y = height-1; y >= 0; y--)
  734. memcpy(ptr + (uint32_t)y * linesize_out,
  735. data + (uint32_t)(height - y - 1) * linesize,
  736. row_copy);
  737. } else if (linesize == linesize_out) {
  738. memcpy(ptr, data, row_copy * height);
  739. } else {
  740. for (y = 0; y < height; y++)
  741. memcpy(ptr + (uint32_t)y * linesize_out,
  742. data + (uint32_t)y * linesize,
  743. row_copy);
  744. }
  745. gs_texture_unmap(tex);
  746. }
  747. void gs_cubetexture_set_image(gs_texture_t *cubetex, uint32_t side,
  748. const void *data, uint32_t linesize, bool invert)
  749. {
  750. /* TODO */
  751. UNUSED_PARAMETER(cubetex);
  752. UNUSED_PARAMETER(side);
  753. UNUSED_PARAMETER(data);
  754. UNUSED_PARAMETER(linesize);
  755. UNUSED_PARAMETER(invert);
  756. }
  757. void gs_perspective(float angle, float aspect, float near, float far)
  758. {
  759. graphics_t *graphics = thread_graphics;
  760. float xmin, xmax, ymin, ymax;
  761. if (!graphics) return;
  762. ymax = near * tanf(RAD(angle)*0.5f);
  763. ymin = -ymax;
  764. xmin = ymin * aspect;
  765. xmax = ymax * aspect;
  766. graphics->exports.device_frustum(graphics->device, xmin, xmax,
  767. ymin, ymax, near, far);
  768. }
  769. void gs_reset_blend_state(void)
  770. {
  771. graphics_t *graphics = thread_graphics;
  772. if (!graphics) return;
  773. if (!graphics->cur_blend_state.enabled)
  774. gs_enable_blending(true);
  775. if (graphics->cur_blend_state.src != GS_BLEND_SRCALPHA ||
  776. graphics->cur_blend_state.dest != GS_BLEND_INVSRCALPHA)
  777. gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  778. }
  779. /* ------------------------------------------------------------------------- */
  780. const char *gs_preprocessor_name(void)
  781. {
  782. graphics_t *graphics = thread_graphics;
  783. if (!graphics) return NULL;
  784. return graphics->exports.device_preprocessor_name();
  785. }
  786. gs_swapchain_t *gs_swapchain_create(const struct gs_init_data *data)
  787. {
  788. struct gs_init_data new_data = *data;
  789. graphics_t *graphics = thread_graphics;
  790. if (!graphics) return NULL;
  791. if (new_data.num_backbuffers == 0)
  792. new_data.num_backbuffers = 1;
  793. return graphics->exports.device_swapchain_create(graphics->device,
  794. &new_data);
  795. }
  796. void gs_resize(uint32_t x, uint32_t y)
  797. {
  798. graphics_t *graphics = thread_graphics;
  799. if (!graphics) return;
  800. graphics->exports.device_resize(graphics->device, x, y);
  801. }
  802. void gs_get_size(uint32_t *x, uint32_t *y)
  803. {
  804. graphics_t *graphics = thread_graphics;
  805. if (!graphics) return;
  806. graphics->exports.device_get_size(graphics->device, x, y);
  807. }
  808. uint32_t gs_get_width(void)
  809. {
  810. graphics_t *graphics = thread_graphics;
  811. if (!graphics) return 0;
  812. return graphics->exports.device_get_width(graphics->device);
  813. }
  814. uint32_t gs_get_height(void)
  815. {
  816. graphics_t *graphics = thread_graphics;
  817. if (!graphics) return 0;
  818. return graphics->exports.device_get_height(graphics->device);
  819. }
  820. static inline bool is_pow2(uint32_t size)
  821. {
  822. return size >= 2 && (size & (size-1)) == 0;
  823. }
  824. gs_texture_t *gs_texture_create(uint32_t width, uint32_t height,
  825. enum gs_color_format color_format, uint32_t levels,
  826. const uint8_t **data, uint32_t flags)
  827. {
  828. graphics_t *graphics = thread_graphics;
  829. bool pow2tex = is_pow2(width) && is_pow2(height);
  830. bool uses_mipmaps = (flags & GS_BUILD_MIPMAPS || levels != 1);
  831. if (!graphics)
  832. return NULL;
  833. if (uses_mipmaps && !pow2tex) {
  834. blog(LOG_WARNING, "Cannot use mipmaps with a "
  835. "non-power-of-two texture. Disabling "
  836. "mipmaps for this texture.");
  837. uses_mipmaps = false;
  838. flags &= ~GS_BUILD_MIPMAPS;
  839. levels = 1;
  840. }
  841. if (uses_mipmaps && flags & GS_RENDER_TARGET) {
  842. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  843. "Disabling mipmaps for this texture.");
  844. flags &= ~GS_BUILD_MIPMAPS;
  845. levels = 1;
  846. }
  847. return graphics->exports.device_texture_create(graphics->device,
  848. width, height, color_format, levels, data, flags);
  849. }
  850. gs_texture_t *gs_cubetexture_create(uint32_t size,
  851. enum gs_color_format color_format, uint32_t levels,
  852. const uint8_t **data, uint32_t flags)
  853. {
  854. graphics_t *graphics = thread_graphics;
  855. bool pow2tex = is_pow2(size);
  856. bool uses_mipmaps = (flags & GS_BUILD_MIPMAPS || levels != 1);
  857. if (!graphics)
  858. return NULL;
  859. if (uses_mipmaps && !pow2tex) {
  860. blog(LOG_WARNING, "Cannot use mipmaps with a "
  861. "non-power-of-two texture. Disabling "
  862. "mipmaps for this texture.");
  863. uses_mipmaps = false;
  864. flags &= ~GS_BUILD_MIPMAPS;
  865. levels = 1;
  866. }
  867. if (uses_mipmaps && flags & GS_RENDER_TARGET) {
  868. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  869. "Disabling mipmaps for this texture.");
  870. flags &= ~GS_BUILD_MIPMAPS;
  871. levels = 1;
  872. data = NULL;
  873. }
  874. return graphics->exports.device_cubetexture_create(graphics->device,
  875. size, color_format, levels, data, flags);
  876. }
  877. gs_texture_t *gs_voltexture_create(uint32_t width, uint32_t height,
  878. uint32_t depth, enum gs_color_format color_format,
  879. uint32_t levels, const uint8_t **data, uint32_t flags)
  880. {
  881. graphics_t *graphics = thread_graphics;
  882. if (!graphics) return NULL;
  883. return graphics->exports.device_voltexture_create(graphics->device,
  884. width, height, depth, color_format, levels, data,
  885. flags);
  886. }
  887. gs_zstencil_t *gs_zstencil_create(uint32_t width, uint32_t height,
  888. enum gs_zstencil_format format)
  889. {
  890. graphics_t *graphics = thread_graphics;
  891. if (!graphics) return NULL;
  892. return graphics->exports.device_zstencil_create(graphics->device,
  893. width, height, format);
  894. }
  895. gs_stagesurf_t *gs_stagesurface_create(uint32_t width, uint32_t height,
  896. enum gs_color_format color_format)
  897. {
  898. graphics_t *graphics = thread_graphics;
  899. if (!graphics) return NULL;
  900. return graphics->exports.device_stagesurface_create(graphics->device,
  901. width, height, color_format);
  902. }
  903. gs_samplerstate_t *gs_samplerstate_create(const struct gs_sampler_info *info)
  904. {
  905. graphics_t *graphics = thread_graphics;
  906. if (!graphics) return NULL;
  907. return graphics->exports.device_samplerstate_create(graphics->device,
  908. info);
  909. }
  910. gs_shader_t *gs_vertexshader_create(const char *shader, const char *file,
  911. char **error_string)
  912. {
  913. graphics_t *graphics = thread_graphics;
  914. if (!graphics) return NULL;
  915. return graphics->exports.device_vertexshader_create(graphics->device,
  916. shader, file, error_string);
  917. }
  918. gs_shader_t *gs_pixelshader_create(const char *shader,
  919. const char *file, char **error_string)
  920. {
  921. graphics_t *graphics = thread_graphics;
  922. if (!graphics) return NULL;
  923. return graphics->exports.device_pixelshader_create(graphics->device,
  924. shader, file, error_string);
  925. }
  926. gs_vertbuffer_t *gs_vertexbuffer_create(struct gs_vb_data *data,
  927. uint32_t flags)
  928. {
  929. graphics_t *graphics = thread_graphics;
  930. if (!graphics) return NULL;
  931. return graphics->exports.device_vertexbuffer_create(graphics->device,
  932. data, flags);
  933. }
  934. gs_indexbuffer_t *gs_indexbuffer_create(enum gs_index_type type,
  935. void *indices, size_t num, uint32_t flags)
  936. {
  937. graphics_t *graphics = thread_graphics;
  938. if (!graphics) return NULL;
  939. return graphics->exports.device_indexbuffer_create(graphics->device,
  940. type, indices, num, flags);
  941. }
  942. enum gs_texture_type gs_get_texture_type(const gs_texture_t *texture)
  943. {
  944. graphics_t *graphics = thread_graphics;
  945. if (!graphics) return GS_TEXTURE_2D;
  946. return graphics->exports.device_get_texture_type(texture);
  947. }
  948. void gs_load_vertexbuffer(gs_vertbuffer_t *vertbuffer)
  949. {
  950. graphics_t *graphics = thread_graphics;
  951. if (!graphics) return;
  952. graphics->exports.device_load_vertexbuffer(graphics->device,
  953. vertbuffer);
  954. }
  955. void gs_load_indexbuffer(gs_indexbuffer_t *indexbuffer)
  956. {
  957. graphics_t *graphics = thread_graphics;
  958. if (!graphics) return;
  959. graphics->exports.device_load_indexbuffer(graphics->device,
  960. indexbuffer);
  961. }
  962. void gs_load_texture(gs_texture_t *tex, int unit)
  963. {
  964. graphics_t *graphics = thread_graphics;
  965. if (!graphics) return;
  966. graphics->exports.device_load_texture(graphics->device, tex, unit);
  967. }
  968. void gs_load_samplerstate(gs_samplerstate_t *samplerstate, int unit)
  969. {
  970. graphics_t *graphics = thread_graphics;
  971. if (!graphics) return;
  972. graphics->exports.device_load_samplerstate(graphics->device,
  973. samplerstate, unit);
  974. }
  975. void gs_load_vertexshader(gs_shader_t *vertshader)
  976. {
  977. graphics_t *graphics = thread_graphics;
  978. if (!graphics) return;
  979. graphics->exports.device_load_vertexshader(graphics->device,
  980. vertshader);
  981. }
  982. void gs_load_pixelshader(gs_shader_t *pixelshader)
  983. {
  984. graphics_t *graphics = thread_graphics;
  985. if (!graphics) return;
  986. graphics->exports.device_load_pixelshader(graphics->device,
  987. pixelshader);
  988. }
  989. void gs_load_default_samplerstate(bool b_3d, int unit)
  990. {
  991. graphics_t *graphics = thread_graphics;
  992. if (!graphics) return;
  993. graphics->exports.device_load_default_samplerstate(graphics->device,
  994. b_3d, unit);
  995. }
  996. gs_shader_t *gs_get_vertex_shader(void)
  997. {
  998. graphics_t *graphics = thread_graphics;
  999. if (!graphics) return NULL;
  1000. return graphics->exports.device_get_vertex_shader(graphics->device);
  1001. }
  1002. gs_shader_t *gs_get_pixel_shader(void)
  1003. {
  1004. graphics_t *graphics = thread_graphics;
  1005. if (!graphics) return NULL;
  1006. return graphics->exports.device_get_pixel_shader(graphics->device);
  1007. }
  1008. gs_texture_t *gs_get_render_target(void)
  1009. {
  1010. graphics_t *graphics = thread_graphics;
  1011. if (!graphics) return NULL;
  1012. return graphics->exports.device_get_render_target(graphics->device);
  1013. }
  1014. gs_zstencil_t *gs_get_zstencil_target(void)
  1015. {
  1016. graphics_t *graphics = thread_graphics;
  1017. if (!graphics) return NULL;
  1018. return graphics->exports.device_get_zstencil_target(graphics->device);
  1019. }
  1020. void gs_set_render_target(gs_texture_t *tex, gs_zstencil_t *zstencil)
  1021. {
  1022. graphics_t *graphics = thread_graphics;
  1023. if (!graphics) return;
  1024. graphics->exports.device_set_render_target(graphics->device, tex,
  1025. zstencil);
  1026. }
  1027. void gs_set_cube_render_target(gs_texture_t *cubetex, int side,
  1028. gs_zstencil_t *zstencil)
  1029. {
  1030. graphics_t *graphics = thread_graphics;
  1031. if (!graphics) return;
  1032. graphics->exports.device_set_cube_render_target(graphics->device,
  1033. cubetex, side, zstencil);
  1034. }
  1035. void gs_copy_texture(gs_texture_t *dst, gs_texture_t *src)
  1036. {
  1037. graphics_t *graphics = thread_graphics;
  1038. if (!graphics) return;
  1039. graphics->exports.device_copy_texture(graphics->device, dst, src);
  1040. }
  1041. void gs_copy_texture_region(gs_texture_t *dst, uint32_t dst_x, uint32_t dst_y,
  1042. gs_texture_t *src, uint32_t src_x, uint32_t src_y,
  1043. uint32_t src_w, uint32_t src_h)
  1044. {
  1045. graphics_t *graphics = thread_graphics;
  1046. if (!graphics) return;
  1047. graphics->exports.device_copy_texture_region(graphics->device,
  1048. dst, dst_x, dst_y,
  1049. src, src_x, src_y, src_w, src_h);
  1050. }
  1051. void gs_stage_texture(gs_stagesurf_t *dst, gs_texture_t *src)
  1052. {
  1053. graphics_t *graphics = thread_graphics;
  1054. if (!graphics) return;
  1055. graphics->exports.device_stage_texture(graphics->device, dst, src);
  1056. }
  1057. void gs_begin_scene(void)
  1058. {
  1059. graphics_t *graphics = thread_graphics;
  1060. if (!graphics) return;
  1061. graphics->exports.device_begin_scene(graphics->device);
  1062. }
  1063. void gs_draw(enum gs_draw_mode draw_mode, uint32_t start_vert,
  1064. uint32_t num_verts)
  1065. {
  1066. graphics_t *graphics = thread_graphics;
  1067. if (!graphics) return;
  1068. graphics->exports.device_draw(graphics->device, draw_mode,
  1069. start_vert, num_verts);
  1070. }
  1071. void gs_end_scene(void)
  1072. {
  1073. graphics_t *graphics = thread_graphics;
  1074. if (!graphics) return;
  1075. graphics->exports.device_end_scene(graphics->device);
  1076. }
  1077. void gs_load_swapchain(gs_swapchain_t *swapchain)
  1078. {
  1079. graphics_t *graphics = thread_graphics;
  1080. if (!graphics) return;
  1081. graphics->exports.device_load_swapchain(graphics->device, swapchain);
  1082. }
  1083. void gs_clear(uint32_t clear_flags, const struct vec4 *color, float depth,
  1084. uint8_t stencil)
  1085. {
  1086. graphics_t *graphics = thread_graphics;
  1087. graphics->exports.device_clear(graphics->device, clear_flags, color,
  1088. depth, stencil);
  1089. }
  1090. void gs_present(void)
  1091. {
  1092. graphics_t *graphics = thread_graphics;
  1093. if (!graphics) return;
  1094. graphics->exports.device_present(graphics->device);
  1095. }
  1096. void gs_flush(void)
  1097. {
  1098. graphics_t *graphics = thread_graphics;
  1099. if (!graphics) return;
  1100. graphics->exports.device_flush(graphics->device);
  1101. }
  1102. void gs_set_cull_mode(enum gs_cull_mode mode)
  1103. {
  1104. graphics_t *graphics = thread_graphics;
  1105. if (!graphics) return;
  1106. graphics->exports.device_set_cull_mode(graphics->device, mode);
  1107. }
  1108. enum gs_cull_mode gs_get_cull_mode(void)
  1109. {
  1110. graphics_t *graphics = thread_graphics;
  1111. if (!graphics) return GS_NEITHER;
  1112. return graphics->exports.device_get_cull_mode(graphics->device);
  1113. }
  1114. void gs_enable_blending(bool enable)
  1115. {
  1116. graphics_t *graphics = thread_graphics;
  1117. if (!graphics) return;
  1118. graphics->cur_blend_state.enabled = enable;
  1119. graphics->exports.device_enable_blending(graphics->device, enable);
  1120. }
  1121. void gs_enable_depth_test(bool enable)
  1122. {
  1123. graphics_t *graphics = thread_graphics;
  1124. if (!graphics) return;
  1125. graphics->exports.device_enable_depth_test(graphics->device, enable);
  1126. }
  1127. void gs_enable_stencil_test(bool enable)
  1128. {
  1129. graphics_t *graphics = thread_graphics;
  1130. if (!graphics) return;
  1131. graphics->exports.device_enable_stencil_test(graphics->device, enable);
  1132. }
  1133. void gs_enable_stencil_write(bool enable)
  1134. {
  1135. graphics_t *graphics = thread_graphics;
  1136. if (!graphics) return;
  1137. graphics->exports.device_enable_stencil_write(graphics->device, enable);
  1138. }
  1139. void gs_enable_color(bool red, bool green, bool blue, bool alpha)
  1140. {
  1141. graphics_t *graphics = thread_graphics;
  1142. if (!graphics) return;
  1143. graphics->exports.device_enable_color(graphics->device, red, green,
  1144. blue, alpha);
  1145. }
  1146. void gs_blend_function(enum gs_blend_type src, enum gs_blend_type dest)
  1147. {
  1148. graphics_t *graphics = thread_graphics;
  1149. if (!graphics) return;
  1150. graphics->cur_blend_state.src = src;
  1151. graphics->cur_blend_state.dest = dest;
  1152. graphics->exports.device_blend_function(graphics->device, src, dest);
  1153. }
  1154. void gs_depth_function(enum gs_depth_test test)
  1155. {
  1156. graphics_t *graphics = thread_graphics;
  1157. if (!graphics) return;
  1158. graphics->exports.device_depth_function(graphics->device, test);
  1159. }
  1160. void gs_stencil_function(enum gs_stencil_side side, enum gs_depth_test test)
  1161. {
  1162. graphics_t *graphics = thread_graphics;
  1163. if (!graphics) return;
  1164. graphics->exports.device_stencil_function(graphics->device, side, test);
  1165. }
  1166. void gs_stencil_op(enum gs_stencil_side side, enum gs_stencil_op_type fail,
  1167. enum gs_stencil_op_type zfail, enum gs_stencil_op_type zpass)
  1168. {
  1169. graphics_t *graphics = thread_graphics;
  1170. if (!graphics) return;
  1171. graphics->exports.device_stencil_op(graphics->device, side, fail, zfail,
  1172. zpass);
  1173. }
  1174. void gs_set_viewport(int x, int y, int width, int height)
  1175. {
  1176. graphics_t *graphics = thread_graphics;
  1177. if (!graphics) return;
  1178. graphics->exports.device_set_viewport(graphics->device, x, y, width,
  1179. height);
  1180. }
  1181. void gs_get_viewport(struct gs_rect *rect)
  1182. {
  1183. graphics_t *graphics = thread_graphics;
  1184. if (!graphics) return;
  1185. graphics->exports.device_get_viewport(graphics->device, rect);
  1186. }
  1187. void gs_set_scissor_rect(const struct gs_rect *rect)
  1188. {
  1189. graphics_t *graphics = thread_graphics;
  1190. if (!graphics) return;
  1191. graphics->exports.device_set_scissor_rect(graphics->device, rect);
  1192. }
  1193. void gs_ortho(float left, float right, float top, float bottom, float znear,
  1194. float zfar)
  1195. {
  1196. graphics_t *graphics = thread_graphics;
  1197. if (!graphics) return;
  1198. graphics->exports.device_ortho(graphics->device, left, right, top,
  1199. bottom, znear, zfar);
  1200. }
  1201. void gs_frustum(float left, float right, float top, float bottom, float znear,
  1202. float zfar)
  1203. {
  1204. graphics_t *graphics = thread_graphics;
  1205. if (!graphics) return;
  1206. graphics->exports.device_frustum(graphics->device, left, right, top,
  1207. bottom, znear, zfar);
  1208. }
  1209. void gs_projection_push(void)
  1210. {
  1211. graphics_t *graphics = thread_graphics;
  1212. if (!graphics) return;
  1213. graphics->exports.device_projection_push(graphics->device);
  1214. }
  1215. void gs_projection_pop(void)
  1216. {
  1217. graphics_t *graphics = thread_graphics;
  1218. if (!graphics) return;
  1219. graphics->exports.device_projection_pop(graphics->device);
  1220. }
  1221. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1222. {
  1223. graphics_t *graphics = thread_graphics;
  1224. if (!graphics || !swapchain) return;
  1225. graphics->exports.gs_swapchain_destroy(swapchain);
  1226. }
  1227. void gs_shader_destroy(gs_shader_t *shader)
  1228. {
  1229. graphics_t *graphics = thread_graphics;
  1230. if (!graphics || !shader) return;
  1231. graphics->exports.gs_shader_destroy(shader);
  1232. }
  1233. int gs_shader_get_num_params(const gs_shader_t *shader)
  1234. {
  1235. graphics_t *graphics = thread_graphics;
  1236. if (!graphics || !shader) return 0;
  1237. return graphics->exports.gs_shader_get_num_params(shader);
  1238. }
  1239. gs_sparam_t *gs_shader_get_param_by_idx(gs_shader_t *shader, uint32_t param)
  1240. {
  1241. graphics_t *graphics = thread_graphics;
  1242. if (!graphics || !shader) return NULL;
  1243. return graphics->exports.gs_shader_get_param_by_idx(shader, param);
  1244. }
  1245. gs_sparam_t *gs_shader_get_param_by_name(gs_shader_t *shader, const char *name)
  1246. {
  1247. graphics_t *graphics = thread_graphics;
  1248. if (!graphics || !shader) return NULL;
  1249. return graphics->exports.gs_shader_get_param_by_name(shader, name);
  1250. }
  1251. gs_sparam_t *gs_shader_get_viewproj_matrix(const gs_shader_t *shader)
  1252. {
  1253. graphics_t *graphics = thread_graphics;
  1254. if (!graphics || !shader) return NULL;
  1255. return graphics->exports.gs_shader_get_viewproj_matrix(shader);
  1256. }
  1257. gs_sparam_t *gs_shader_get_world_matrix(const gs_shader_t *shader)
  1258. {
  1259. graphics_t *graphics = thread_graphics;
  1260. if (!graphics || !shader) return NULL;
  1261. return graphics->exports.gs_shader_get_world_matrix(shader);
  1262. }
  1263. void gs_shader_get_param_info(const gs_sparam_t *param,
  1264. struct gs_shader_param_info *info)
  1265. {
  1266. graphics_t *graphics = thread_graphics;
  1267. if (!graphics || !param) return;
  1268. graphics->exports.gs_shader_get_param_info(param, info);
  1269. }
  1270. void gs_shader_set_bool(gs_sparam_t *param, bool val)
  1271. {
  1272. graphics_t *graphics = thread_graphics;
  1273. if (!graphics || !param) return;
  1274. graphics->exports.gs_shader_set_bool(param, val);
  1275. }
  1276. void gs_shader_set_float(gs_sparam_t *param, float val)
  1277. {
  1278. graphics_t *graphics = thread_graphics;
  1279. if (!graphics || !param) return;
  1280. graphics->exports.gs_shader_set_float(param, val);
  1281. }
  1282. void gs_shader_set_int(gs_sparam_t *param, int val)
  1283. {
  1284. graphics_t *graphics = thread_graphics;
  1285. if (!graphics || !param) return;
  1286. graphics->exports.gs_shader_set_int(param, val);
  1287. }
  1288. void gs_shader_setmatrix3(gs_sparam_t *param, const struct matrix3 *val)
  1289. {
  1290. graphics_t *graphics = thread_graphics;
  1291. if (!graphics || !param) return;
  1292. graphics->exports.gs_shader_setmatrix3(param, val);
  1293. }
  1294. void gs_shader_set_matrix4(gs_sparam_t *param, const struct matrix4 *val)
  1295. {
  1296. graphics_t *graphics = thread_graphics;
  1297. if (!graphics || !param) return;
  1298. graphics->exports.gs_shader_set_matrix4(param, val);
  1299. }
  1300. void gs_shader_set_vec2(gs_sparam_t *param, const struct vec2 *val)
  1301. {
  1302. graphics_t *graphics = thread_graphics;
  1303. if (!graphics || !param) return;
  1304. graphics->exports.gs_shader_set_vec2(param, val);
  1305. }
  1306. void gs_shader_set_vec3(gs_sparam_t *param, const struct vec3 *val)
  1307. {
  1308. graphics_t *graphics = thread_graphics;
  1309. if (!graphics || !param) return;
  1310. graphics->exports.gs_shader_set_vec3(param, val);
  1311. }
  1312. void gs_shader_set_vec4(gs_sparam_t *param, const struct vec4 *val)
  1313. {
  1314. graphics_t *graphics = thread_graphics;
  1315. if (!graphics || !param) return;
  1316. graphics->exports.gs_shader_set_vec4(param, val);
  1317. }
  1318. void gs_shader_set_texture(gs_sparam_t *param, gs_texture_t *val)
  1319. {
  1320. graphics_t *graphics = thread_graphics;
  1321. if (!graphics || !param) return;
  1322. graphics->exports.gs_shader_set_texture(param, val);
  1323. }
  1324. void gs_shader_set_val(gs_sparam_t *param, const void *val, size_t size)
  1325. {
  1326. graphics_t *graphics = thread_graphics;
  1327. if (!graphics || !param) return;
  1328. graphics->exports.gs_shader_set_val(param, val, size);
  1329. }
  1330. void gs_shader_set_default(gs_sparam_t *param)
  1331. {
  1332. graphics_t *graphics = thread_graphics;
  1333. if (!graphics || !param) return;
  1334. graphics->exports.gs_shader_set_default(param);
  1335. }
  1336. void gs_texture_destroy(gs_texture_t *tex)
  1337. {
  1338. graphics_t *graphics = thread_graphics;
  1339. if (!graphics || !tex) return;
  1340. graphics->exports.gs_texture_destroy(tex);
  1341. }
  1342. uint32_t gs_texture_get_width(const gs_texture_t *tex)
  1343. {
  1344. graphics_t *graphics = thread_graphics;
  1345. if (!graphics || !tex) return 0;
  1346. return graphics->exports.gs_texture_get_width(tex);
  1347. }
  1348. uint32_t gs_texture_get_height(const gs_texture_t *tex)
  1349. {
  1350. graphics_t *graphics = thread_graphics;
  1351. if (!graphics || !tex) return 0;
  1352. return graphics->exports.gs_texture_get_height(tex);
  1353. }
  1354. enum gs_color_format gs_texture_get_color_format(const gs_texture_t *tex)
  1355. {
  1356. graphics_t *graphics = thread_graphics;
  1357. if (!graphics || !tex) return GS_UNKNOWN;
  1358. return graphics->exports.gs_texture_get_color_format(tex);
  1359. }
  1360. bool gs_texture_map(gs_texture_t *tex, uint8_t **ptr, uint32_t *linesize)
  1361. {
  1362. graphics_t *graphics = thread_graphics;
  1363. if (!graphics || !tex) return false;
  1364. return graphics->exports.gs_texture_map(tex, ptr, linesize);
  1365. }
  1366. void gs_texture_unmap(gs_texture_t *tex)
  1367. {
  1368. graphics_t *graphics = thread_graphics;
  1369. if (!graphics || !tex) return;
  1370. graphics->exports.gs_texture_unmap(tex);
  1371. }
  1372. bool gs_texture_is_rect(const gs_texture_t *tex)
  1373. {
  1374. graphics_t *graphics = thread_graphics;
  1375. if (!graphics || !tex) return false;
  1376. if (graphics->exports.gs_texture_is_rect)
  1377. return graphics->exports.gs_texture_is_rect(tex);
  1378. else
  1379. return false;
  1380. }
  1381. void *gs_texture_get_obj(gs_texture_t *tex)
  1382. {
  1383. graphics_t *graphics = thread_graphics;
  1384. if (!graphics || !tex) return NULL;
  1385. return graphics->exports.gs_texture_get_obj(tex);
  1386. }
  1387. void gs_cubetexture_destroy(gs_texture_t *cubetex)
  1388. {
  1389. graphics_t *graphics = thread_graphics;
  1390. if (!graphics || !cubetex) return;
  1391. graphics->exports.gs_cubetexture_destroy(cubetex);
  1392. }
  1393. uint32_t gs_cubetexture_get_size(const gs_texture_t *cubetex)
  1394. {
  1395. graphics_t *graphics = thread_graphics;
  1396. if (!graphics || !cubetex) return 0;
  1397. return graphics->exports.gs_cubetexture_get_size(cubetex);
  1398. }
  1399. enum gs_color_format gs_cubetexture_get_color_format(
  1400. const gs_texture_t *cubetex)
  1401. {
  1402. graphics_t *graphics = thread_graphics;
  1403. if (!graphics || !cubetex) return GS_UNKNOWN;
  1404. return graphics->exports.gs_cubetexture_get_color_format(cubetex);
  1405. }
  1406. void gs_voltexture_destroy(gs_texture_t *voltex)
  1407. {
  1408. graphics_t *graphics = thread_graphics;
  1409. if (!graphics || !voltex) return;
  1410. graphics->exports.gs_voltexture_destroy(voltex);
  1411. }
  1412. uint32_t gs_voltexture_get_width(const gs_texture_t *voltex)
  1413. {
  1414. graphics_t *graphics = thread_graphics;
  1415. if (!graphics || !voltex) return 0;
  1416. return graphics->exports.gs_voltexture_get_width(voltex);
  1417. }
  1418. uint32_t gs_voltexture_get_height(const gs_texture_t *voltex)
  1419. {
  1420. graphics_t *graphics = thread_graphics;
  1421. if (!graphics || !voltex) return 0;
  1422. return graphics->exports.gs_voltexture_get_height(voltex);
  1423. }
  1424. uint32_t gs_voltexture_getdepth(const gs_texture_t *voltex)
  1425. {
  1426. graphics_t *graphics = thread_graphics;
  1427. if (!graphics || !voltex) return 0;
  1428. return graphics->exports.gs_voltexture_getdepth(voltex);
  1429. }
  1430. enum gs_color_format gs_voltexture_get_color_format(const gs_texture_t *voltex)
  1431. {
  1432. graphics_t *graphics = thread_graphics;
  1433. if (!graphics || !voltex) return GS_UNKNOWN;
  1434. return graphics->exports.gs_voltexture_get_color_format(voltex);
  1435. }
  1436. void gs_stagesurface_destroy(gs_stagesurf_t *stagesurf)
  1437. {
  1438. graphics_t *graphics = thread_graphics;
  1439. if (!graphics || !stagesurf) return;
  1440. graphics->exports.gs_stagesurface_destroy(stagesurf);
  1441. }
  1442. uint32_t gs_stagesurface_get_width(const gs_stagesurf_t *stagesurf)
  1443. {
  1444. graphics_t *graphics = thread_graphics;
  1445. if (!graphics || !stagesurf) return 0;
  1446. return graphics->exports.gs_stagesurface_get_width(stagesurf);
  1447. }
  1448. uint32_t gs_stagesurface_get_height(const gs_stagesurf_t *stagesurf)
  1449. {
  1450. graphics_t *graphics = thread_graphics;
  1451. if (!graphics || !stagesurf) return 0;
  1452. return graphics->exports.gs_stagesurface_get_height(stagesurf);
  1453. }
  1454. enum gs_color_format gs_stagesurface_get_color_format(
  1455. const gs_stagesurf_t *stagesurf)
  1456. {
  1457. graphics_t *graphics = thread_graphics;
  1458. if (!graphics || !stagesurf) return GS_UNKNOWN;
  1459. return graphics->exports.gs_stagesurface_get_color_format(stagesurf);
  1460. }
  1461. bool gs_stagesurface_map(gs_stagesurf_t *stagesurf, uint8_t **data,
  1462. uint32_t *linesize)
  1463. {
  1464. graphics_t *graphics = thread_graphics;
  1465. if (!graphics || !stagesurf) return false;
  1466. return graphics->exports.gs_stagesurface_map(stagesurf, data, linesize);
  1467. }
  1468. void gs_stagesurface_unmap(gs_stagesurf_t *stagesurf)
  1469. {
  1470. graphics_t *graphics = thread_graphics;
  1471. if (!graphics || !stagesurf) return;
  1472. graphics->exports.gs_stagesurface_unmap(stagesurf);
  1473. }
  1474. void gs_zstencil_destroy(gs_zstencil_t *zstencil)
  1475. {
  1476. if (!thread_graphics || !zstencil) return;
  1477. thread_graphics->exports.gs_zstencil_destroy(zstencil);
  1478. }
  1479. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1480. {
  1481. if (!thread_graphics || !samplerstate) return;
  1482. thread_graphics->exports.gs_samplerstate_destroy(samplerstate);
  1483. }
  1484. void gs_vertexbuffer_destroy(gs_vertbuffer_t *vertbuffer)
  1485. {
  1486. graphics_t *graphics = thread_graphics;
  1487. if (!graphics || !vertbuffer) return;
  1488. graphics->exports.gs_vertexbuffer_destroy(vertbuffer);
  1489. }
  1490. void gs_vertexbuffer_flush(gs_vertbuffer_t *vertbuffer)
  1491. {
  1492. if (!thread_graphics || !vertbuffer) return;
  1493. thread_graphics->exports.gs_vertexbuffer_flush(vertbuffer);
  1494. }
  1495. struct gs_vb_data *gs_vertexbuffer_get_data(const gs_vertbuffer_t *vertbuffer)
  1496. {
  1497. if (!thread_graphics || !vertbuffer) return NULL;
  1498. return thread_graphics->exports.gs_vertexbuffer_get_data(vertbuffer);
  1499. }
  1500. void gs_indexbuffer_destroy(gs_indexbuffer_t *indexbuffer)
  1501. {
  1502. graphics_t *graphics = thread_graphics;
  1503. if (!graphics || !indexbuffer) return;
  1504. graphics->exports.gs_indexbuffer_destroy(indexbuffer);
  1505. }
  1506. void gs_indexbuffer_flush(gs_indexbuffer_t *indexbuffer)
  1507. {
  1508. if (!thread_graphics || !indexbuffer) return;
  1509. thread_graphics->exports.gs_indexbuffer_flush(indexbuffer);
  1510. }
  1511. void *gs_indexbuffer_get_data(const gs_indexbuffer_t *indexbuffer)
  1512. {
  1513. if (!thread_graphics || !indexbuffer) return NULL;
  1514. return thread_graphics->exports.gs_indexbuffer_get_data(indexbuffer);
  1515. }
  1516. size_t gs_indexbuffer_get_num_indices(const gs_indexbuffer_t *indexbuffer)
  1517. {
  1518. if (!thread_graphics || !indexbuffer) return 0;
  1519. return thread_graphics->exports.gs_indexbuffer_get_num_indices(
  1520. indexbuffer);
  1521. }
  1522. enum gs_index_type gs_indexbuffer_get_type(const gs_indexbuffer_t *indexbuffer)
  1523. {
  1524. if (!thread_graphics || !indexbuffer) return (enum gs_index_type)0;
  1525. return thread_graphics->exports.gs_indexbuffer_get_type(indexbuffer);
  1526. }
  1527. #ifdef __APPLE__
  1528. /** Platform specific functions */
  1529. gs_texture_t *gs_texture_create_from_iosurface(void *iosurf)
  1530. {
  1531. graphics_t *graphics = thread_graphics;
  1532. if (!graphics || !iosurf ||
  1533. !graphics->exports.device_texture_create_from_iosurface)
  1534. return NULL;
  1535. return graphics->exports.device_texture_create_from_iosurface(
  1536. graphics->device, iosurf);
  1537. }
  1538. bool gs_texture_rebind_iosurface(gs_texture_t *texture, void *iosurf)
  1539. {
  1540. graphics_t *graphics = thread_graphics;
  1541. if (!graphics || !iosurf ||
  1542. !graphics->exports.gs_texture_rebind_iosurface)
  1543. return false;
  1544. return graphics->exports.gs_texture_rebind_iosurface(texture, iosurf);
  1545. }
  1546. #elif _WIN32
  1547. bool gs_gdi_texture_available(void)
  1548. {
  1549. if (!thread_graphics)
  1550. return false;
  1551. return thread_graphics->exports.device_gdi_texture_available();
  1552. }
  1553. bool gs_shared_texture_available(void)
  1554. {
  1555. if (!thread_graphics)
  1556. return false;
  1557. return thread_graphics->exports.device_shared_texture_available();
  1558. }
  1559. bool gs_get_duplicator_monitor_info(int monitor_idx,
  1560. struct gs_monitor_info *monitor_info)
  1561. {
  1562. if (!thread_graphics)
  1563. return false;
  1564. if (!thread_graphics->exports.device_get_duplicator_monitor_info)
  1565. return false;
  1566. return thread_graphics->exports.device_get_duplicator_monitor_info(
  1567. thread_graphics->device, monitor_idx,
  1568. monitor_info);
  1569. }
  1570. gs_duplicator_t *gs_duplicator_create(int monitor_idx)
  1571. {
  1572. if (!thread_graphics)
  1573. return NULL;
  1574. if (!thread_graphics->exports.device_duplicator_create)
  1575. return NULL;
  1576. return thread_graphics->exports.device_duplicator_create(
  1577. thread_graphics->device, monitor_idx);
  1578. }
  1579. void gs_duplicator_destroy(gs_duplicator_t *duplicator)
  1580. {
  1581. if (!thread_graphics)
  1582. return;
  1583. if (!thread_graphics->exports.gs_duplicator_destroy)
  1584. return;
  1585. thread_graphics->exports.gs_duplicator_destroy(duplicator);
  1586. }
  1587. bool gs_duplicator_update_frame(gs_duplicator_t *duplicator)
  1588. {
  1589. if (!thread_graphics)
  1590. return true;
  1591. if (!thread_graphics->exports.gs_duplicator_get_texture)
  1592. return true;
  1593. return thread_graphics->exports.gs_duplicator_update_frame(duplicator);
  1594. }
  1595. gs_texture_t *gs_duplicator_get_texture(gs_duplicator_t *duplicator)
  1596. {
  1597. if (!thread_graphics)
  1598. return NULL;
  1599. if (!thread_graphics->exports.gs_duplicator_get_texture)
  1600. return NULL;
  1601. return thread_graphics->exports.gs_duplicator_get_texture(duplicator);
  1602. }
  1603. /** creates a windows GDI-lockable texture */
  1604. gs_texture_t *gs_texture_create_gdi(uint32_t width, uint32_t height)
  1605. {
  1606. graphics_t *graphics = thread_graphics;
  1607. if (!graphics) return NULL;
  1608. if (graphics->exports.device_texture_create_gdi)
  1609. return graphics->exports.device_texture_create_gdi(
  1610. graphics->device, width, height);
  1611. return NULL;
  1612. }
  1613. void *gs_texture_get_dc(gs_texture_t *gdi_tex)
  1614. {
  1615. if (!thread_graphics || !gdi_tex)
  1616. return NULL;
  1617. if (thread_graphics->exports.gs_texture_get_dc)
  1618. return thread_graphics->exports.gs_texture_get_dc(gdi_tex);
  1619. return NULL;
  1620. }
  1621. void gs_texture_release_dc(gs_texture_t *gdi_tex)
  1622. {
  1623. if (!thread_graphics || !gdi_tex)
  1624. return;
  1625. if (thread_graphics->exports.gs_texture_release_dc)
  1626. thread_graphics->exports.gs_texture_release_dc(gdi_tex);
  1627. }
  1628. gs_texture_t *gs_texture_open_shared(uint32_t handle)
  1629. {
  1630. graphics_t *graphics = thread_graphics;
  1631. if (!graphics)
  1632. return NULL;
  1633. if (graphics->exports.device_texture_open_shared)
  1634. return graphics->exports.device_texture_open_shared(
  1635. graphics->device, handle);
  1636. return NULL;
  1637. }
  1638. #endif