graphics.c 48 KB

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