1
0

graphics.c 40 KB

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