graphics.c 38 KB

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