graphics.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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. 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_rect(float *start, float *end, float size,
  516. bool flip)
  517. {
  518. if (!flip) {
  519. *start = 0.0f;
  520. *end = size;
  521. } else {
  522. *start = size;
  523. *end = 0.0f;
  524. }
  525. }
  526. static inline void assign_sprite_uv(float *start, float *end, bool flip)
  527. {
  528. if (!flip) {
  529. *start = 0.0f;
  530. *end = 1.0f;
  531. } else {
  532. *start = 1.0f;
  533. *end = 0.0f;
  534. }
  535. }
  536. static void build_sprite(struct vb_data *data, float fcx, float fcy,
  537. float start_u, float end_u, float start_v, float end_v,
  538. uint32_t flip)
  539. {
  540. struct vec2 *tvarray = data->tvarray[0].array;
  541. vec3_zero(data->points);
  542. vec3_set(data->points+1, fcx, 0.0f, 0.0f);
  543. vec3_set(data->points+2, 0.0f, fcy, 0.0f);
  544. vec3_set(data->points+3, fcx, fcy, 0.0f);
  545. vec2_set(tvarray, start_u, start_v);
  546. vec2_set(tvarray+1, end_u, start_v);
  547. vec2_set(tvarray+2, start_u, end_v);
  548. vec2_set(tvarray+3, end_u, end_v);
  549. }
  550. static inline void build_sprite_norm(struct vb_data *data, float fcx, float fcy,
  551. uint32_t flip)
  552. {
  553. float start_u, end_u;
  554. float start_v, end_v;
  555. assign_sprite_uv(&start_u, &end_u, (flip & GS_FLIP_U) != 0);
  556. assign_sprite_uv(&start_v, &end_v, (flip & GS_FLIP_V) != 0);
  557. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v, flip);
  558. }
  559. static inline void build_sprite_rect(struct vb_data *data, texture_t tex,
  560. float fcx, float fcy, uint32_t flip)
  561. {
  562. float start_u, end_u;
  563. float start_v, end_v;
  564. float width = (float)texture_getwidth(tex);
  565. float height = (float)texture_getheight(tex);
  566. assign_sprite_rect(&start_u, &end_u, width, (flip & GS_FLIP_U) != 0);
  567. assign_sprite_rect(&start_v, &end_v, height, (flip & GS_FLIP_V) != 0);
  568. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v, flip);
  569. }
  570. void gs_draw_sprite(texture_t tex, uint32_t flip, uint32_t width,
  571. uint32_t height)
  572. {
  573. graphics_t graphics = thread_graphics;
  574. float fcx, fcy;
  575. struct vb_data *data;
  576. assert(tex);
  577. if (gs_gettexturetype(tex) != GS_TEXTURE_2D) {
  578. blog(LOG_ERROR, "A sprite must be a 2D texture");
  579. return;
  580. }
  581. fcx = width ? (float)width : (float)texture_getwidth(tex);
  582. fcy = height ? (float)height : (float)texture_getheight(tex);
  583. data = vertexbuffer_getdata(graphics->sprite_buffer);
  584. if (texture_isrect(tex))
  585. build_sprite_rect(data, tex, fcx, fcy, flip);
  586. else
  587. build_sprite_norm(data, fcx, fcy, flip);
  588. vertexbuffer_flush(graphics->sprite_buffer, false);
  589. gs_load_vertexbuffer(graphics->sprite_buffer);
  590. gs_load_indexbuffer(NULL);
  591. gs_draw(GS_TRISTRIP, 0, 0);
  592. }
  593. void gs_draw_cube_backdrop(texture_t cubetex, const struct quat *rot,
  594. float left, float right, float top, float bottom, float znear)
  595. {
  596. /* TODO */
  597. }
  598. void gs_resetviewport(void)
  599. {
  600. uint32_t cx, cy;
  601. gs_getsize(&cx, &cy);
  602. gs_setviewport(0, 0, (int)cx, (int)cy);
  603. }
  604. void gs_set2dmode(void)
  605. {
  606. uint32_t cx, cy;
  607. gs_getsize(&cx, &cy);
  608. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -1.0, -1024.0f);
  609. }
  610. void gs_set3dmode(double fovy, double znear, double zvar)
  611. {
  612. /* TODO */
  613. }
  614. void gs_viewport_push(void)
  615. {
  616. struct gs_rect *rect = da_push_back_new(
  617. thread_graphics->viewport_stack);
  618. gs_getviewport(rect);
  619. }
  620. void gs_viewport_pop(void)
  621. {
  622. struct gs_rect *rect;
  623. if (!thread_graphics->viewport_stack.num)
  624. return;
  625. rect = da_end(thread_graphics->viewport_stack);
  626. gs_setviewport(rect->x, rect->y, rect->cx, rect->cy);
  627. da_pop_back(thread_graphics->viewport_stack);
  628. }
  629. void texture_setimage(texture_t tex, const void *data, uint32_t row_bytes,
  630. bool flip)
  631. {
  632. void *ptr;
  633. uint32_t row_bytes_out;
  634. uint32_t row_copy;
  635. int32_t height = (int32_t)texture_getheight(tex);
  636. int32_t y;
  637. if (!texture_map(tex, &ptr, &row_bytes_out))
  638. return;
  639. row_copy = (row_bytes < row_bytes_out) ? row_bytes : row_bytes_out;
  640. if (flip) {
  641. for (y = height-1; y >= 0; y--)
  642. memcpy((uint8_t*)ptr + (uint32_t)y * row_bytes_out,
  643. (uint8_t*)data + (uint32_t)y * row_bytes,
  644. row_copy);
  645. } else if (row_bytes == row_bytes_out) {
  646. memcpy(ptr, data, row_copy * height);
  647. } else {
  648. for (y = 0; y < height; y++)
  649. memcpy((uint8_t*)ptr + (uint32_t)y * row_bytes_out,
  650. (uint8_t*)data + (uint32_t)y * row_bytes,
  651. row_copy);
  652. }
  653. }
  654. void cubetexture_setimage(texture_t cubetex, uint32_t side, const void *data,
  655. uint32_t row_bytes, bool invert)
  656. {
  657. /* TODO */
  658. }
  659. void gs_perspective(float angle, float aspect, float near, float far)
  660. {
  661. graphics_t graphics = thread_graphics;
  662. float xmin, xmax, ymin, ymax;
  663. ymax = near * tanf(RAD(angle)*0.5f);
  664. ymin = -ymax;
  665. xmin = ymin * aspect;
  666. xmax = ymax * aspect;
  667. graphics->exports.device_frustum(graphics->device, xmin, xmax,
  668. ymin, ymax, near, far);
  669. }
  670. /* ------------------------------------------------------------------------- */
  671. swapchain_t gs_create_swapchain(struct gs_init_data *data)
  672. {
  673. graphics_t graphics = thread_graphics;
  674. return graphics->exports.device_create_swapchain(graphics->device,
  675. data);
  676. }
  677. void gs_resize(uint32_t x, uint32_t y)
  678. {
  679. graphics_t graphics = thread_graphics;
  680. graphics->exports.device_resize(graphics->device, x, y);
  681. }
  682. void gs_getsize(uint32_t *x, uint32_t *y)
  683. {
  684. graphics_t graphics = thread_graphics;
  685. graphics->exports.device_getsize(graphics->device, x, y);
  686. }
  687. uint32_t gs_getwidth(void)
  688. {
  689. graphics_t graphics = thread_graphics;
  690. return graphics->exports.device_getwidth(graphics->device);
  691. }
  692. uint32_t gs_getheight(void)
  693. {
  694. graphics_t graphics = thread_graphics;
  695. return graphics->exports.device_getheight(graphics->device);
  696. }
  697. static inline bool is_pow2(uint32_t size)
  698. {
  699. return size >= 2 && (size & (size-1)) == 0;
  700. }
  701. texture_t gs_create_texture(uint32_t width, uint32_t height,
  702. enum gs_color_format color_format, uint32_t levels,
  703. const void **data, uint32_t flags)
  704. {
  705. graphics_t graphics = thread_graphics;
  706. bool pow2tex = is_pow2(width) && is_pow2(height);
  707. bool uses_mipmaps = (flags & GS_BUILDMIPMAPS || levels != 1);
  708. if (uses_mipmaps && !pow2tex) {
  709. blog(LOG_WARNING, "Cannot use mipmaps with a "
  710. "non-power-of-two texture. Disabling "
  711. "mipmaps for this texture.");
  712. uses_mipmaps = false;
  713. flags &= ~GS_BUILDMIPMAPS;
  714. levels = 1;
  715. }
  716. if (uses_mipmaps && flags & GS_RENDERTARGET) {
  717. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  718. "Disabling mipmaps for this texture.");
  719. flags &= ~GS_BUILDMIPMAPS;
  720. levels = 1;
  721. }
  722. return graphics->exports.device_create_texture(graphics->device,
  723. width, height, color_format, levels, data, flags);
  724. }
  725. texture_t gs_create_cubetexture(uint32_t size,
  726. enum gs_color_format color_format, uint32_t levels,
  727. const void **data, uint32_t flags)
  728. {
  729. graphics_t graphics = thread_graphics;
  730. bool pow2tex = is_pow2(size);
  731. bool uses_mipmaps = (flags & GS_BUILDMIPMAPS || levels != 1);
  732. if (uses_mipmaps && !pow2tex) {
  733. blog(LOG_WARNING, "Cannot use mipmaps with a "
  734. "non-power-of-two texture. Disabling "
  735. "mipmaps for this texture.");
  736. uses_mipmaps = false;
  737. flags &= ~GS_BUILDMIPMAPS;
  738. levels = 1;
  739. }
  740. if (uses_mipmaps && flags & GS_RENDERTARGET) {
  741. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  742. "Disabling mipmaps for this texture.");
  743. flags &= ~GS_BUILDMIPMAPS;
  744. levels = 1;
  745. data = NULL;
  746. }
  747. return graphics->exports.device_create_cubetexture(graphics->device,
  748. size, color_format, levels, data, flags);
  749. }
  750. texture_t gs_create_volumetexture(uint32_t width, uint32_t height,
  751. uint32_t depth, enum gs_color_format color_format,
  752. uint32_t levels, const void **data, uint32_t flags)
  753. {
  754. graphics_t graphics = thread_graphics;
  755. return graphics->exports.device_create_volumetexture(graphics->device,
  756. width, height, depth, color_format, levels, data,
  757. flags);
  758. }
  759. zstencil_t gs_create_zstencil(uint32_t width, uint32_t height,
  760. enum gs_zstencil_format format)
  761. {
  762. graphics_t graphics = thread_graphics;
  763. return graphics->exports.device_create_zstencil(graphics->device,
  764. width, height, format);
  765. }
  766. stagesurf_t gs_create_stagesurface(uint32_t width, uint32_t height,
  767. enum gs_color_format color_format)
  768. {
  769. graphics_t graphics = thread_graphics;
  770. return graphics->exports.device_create_stagesurface(graphics->device,
  771. width, height, color_format);
  772. }
  773. samplerstate_t gs_create_samplerstate(struct gs_sampler_info *info)
  774. {
  775. graphics_t graphics = thread_graphics;
  776. return graphics->exports.device_create_samplerstate(graphics->device,
  777. info);
  778. }
  779. shader_t gs_create_vertexshader(const char *shader, const char *file,
  780. char **error_string)
  781. {
  782. graphics_t graphics = thread_graphics;
  783. return graphics->exports.device_create_vertexshader(graphics->device,
  784. shader, file, error_string);
  785. }
  786. shader_t gs_create_pixelshader(const char *shader,
  787. const char *file, char **error_string)
  788. {
  789. graphics_t graphics = thread_graphics;
  790. return graphics->exports.device_create_pixelshader(graphics->device,
  791. shader, file, error_string);
  792. }
  793. vertbuffer_t gs_create_vertexbuffer(struct vb_data *data,
  794. uint32_t flags)
  795. {
  796. graphics_t graphics = thread_graphics;
  797. return graphics->exports.device_create_vertexbuffer(graphics->device,
  798. data, flags);
  799. }
  800. indexbuffer_t gs_create_indexbuffer(enum gs_index_type type,
  801. void *indices, size_t num, uint32_t flags)
  802. {
  803. graphics_t graphics = thread_graphics;
  804. return graphics->exports.device_create_indexbuffer(graphics->device,
  805. type, indices, num, flags);
  806. }
  807. enum gs_texture_type gs_gettexturetype(texture_t texture)
  808. {
  809. graphics_t graphics = thread_graphics;
  810. return graphics->exports.device_gettexturetype(graphics->device,
  811. texture);
  812. }
  813. void gs_load_vertexbuffer(vertbuffer_t vertbuffer)
  814. {
  815. graphics_t graphics = thread_graphics;
  816. graphics->exports.device_load_vertexbuffer(graphics->device,
  817. vertbuffer);
  818. }
  819. void gs_load_indexbuffer(indexbuffer_t indexbuffer)
  820. {
  821. graphics_t graphics = thread_graphics;
  822. graphics->exports.device_load_indexbuffer(graphics->device,
  823. indexbuffer);
  824. }
  825. void gs_load_texture(texture_t tex, int unit)
  826. {
  827. graphics_t graphics = thread_graphics;
  828. graphics->exports.device_load_texture(graphics->device, tex, unit);
  829. }
  830. void gs_load_samplerstate(samplerstate_t samplerstate, int unit)
  831. {
  832. graphics_t graphics = thread_graphics;
  833. graphics->exports.device_load_samplerstate(graphics->device,
  834. samplerstate, unit);
  835. }
  836. void gs_load_vertexshader(shader_t vertshader)
  837. {
  838. graphics_t graphics = thread_graphics;
  839. graphics->exports.device_load_vertexshader(graphics->device,
  840. vertshader);
  841. }
  842. void gs_load_pixelshader(shader_t pixelshader)
  843. {
  844. graphics_t graphics = thread_graphics;
  845. graphics->exports.device_load_pixelshader(graphics->device,
  846. pixelshader);
  847. }
  848. void gs_load_defaultsamplerstate(bool b_3d, int unit)
  849. {
  850. graphics_t graphics = thread_graphics;
  851. graphics->exports.device_load_defaultsamplerstate(graphics->device,
  852. b_3d, unit);
  853. }
  854. shader_t gs_getvertexshader(void)
  855. {
  856. graphics_t graphics = thread_graphics;
  857. return graphics->exports.device_getvertexshader(graphics->device);
  858. }
  859. shader_t gs_getpixelshader(void)
  860. {
  861. graphics_t graphics = thread_graphics;
  862. return graphics->exports.device_getpixelshader(graphics->device);
  863. }
  864. texture_t gs_getrendertarget(void)
  865. {
  866. graphics_t graphics = thread_graphics;
  867. return graphics->exports.device_getrendertarget(graphics->device);
  868. }
  869. zstencil_t gs_getzstenciltarget(void)
  870. {
  871. graphics_t graphics = thread_graphics;
  872. return graphics->exports.device_getzstenciltarget(graphics->device);
  873. }
  874. void gs_setrendertarget(texture_t tex, zstencil_t zstencil)
  875. {
  876. graphics_t graphics = thread_graphics;
  877. graphics->exports.device_setrendertarget(graphics->device, tex,
  878. zstencil);
  879. }
  880. void gs_setcuberendertarget(texture_t cubetex, int side, zstencil_t zstencil)
  881. {
  882. graphics_t graphics = thread_graphics;
  883. graphics->exports.device_setcuberendertarget(graphics->device, cubetex,
  884. side, zstencil);
  885. }
  886. void gs_copy_texture(texture_t dst, texture_t src)
  887. {
  888. graphics_t graphics = thread_graphics;
  889. graphics->exports.device_copy_texture(graphics->device, dst, src);
  890. }
  891. void gs_stage_texture(stagesurf_t dst, texture_t src)
  892. {
  893. graphics_t graphics = thread_graphics;
  894. graphics->exports.device_stage_texture(graphics->device, dst, src);
  895. }
  896. void gs_beginscene(void)
  897. {
  898. graphics_t graphics = thread_graphics;
  899. graphics->exports.device_beginscene(graphics->device);
  900. }
  901. void gs_draw(enum gs_draw_mode draw_mode, uint32_t start_vert,
  902. uint32_t num_verts)
  903. {
  904. graphics_t graphics = thread_graphics;
  905. graphics->exports.device_draw(graphics->device, draw_mode,
  906. start_vert, num_verts);
  907. }
  908. void gs_endscene(void)
  909. {
  910. graphics_t graphics = thread_graphics;
  911. graphics->exports.device_endscene(graphics->device);
  912. }
  913. void gs_load_swapchain(swapchain_t swapchain)
  914. {
  915. graphics_t graphics = thread_graphics;
  916. graphics->exports.device_load_swapchain(graphics->device, swapchain);
  917. }
  918. void gs_clear(uint32_t clear_flags, struct vec4 *color, float depth,
  919. uint8_t stencil)
  920. {
  921. graphics_t graphics = thread_graphics;
  922. graphics->exports.device_clear(graphics->device, clear_flags, color,
  923. depth, stencil);
  924. }
  925. void gs_present(void)
  926. {
  927. graphics_t graphics = thread_graphics;
  928. graphics->exports.device_present(graphics->device);
  929. }
  930. void gs_setcullmode(enum gs_cull_mode mode)
  931. {
  932. graphics_t graphics = thread_graphics;
  933. graphics->exports.device_setcullmode(graphics->device, mode);
  934. }
  935. enum gs_cull_mode gs_getcullmode(void)
  936. {
  937. graphics_t graphics = thread_graphics;
  938. return graphics->exports.device_getcullmode(graphics->device);
  939. }
  940. void gs_enable_blending(bool enable)
  941. {
  942. graphics_t graphics = thread_graphics;
  943. graphics->exports.device_enable_blending(graphics->device, enable);
  944. }
  945. void gs_enable_depthtest(bool enable)
  946. {
  947. graphics_t graphics = thread_graphics;
  948. graphics->exports.device_enable_depthtest(graphics->device, enable);
  949. }
  950. void gs_enable_stenciltest(bool enable)
  951. {
  952. graphics_t graphics = thread_graphics;
  953. graphics->exports.device_enable_stenciltest(graphics->device, enable);
  954. }
  955. void gs_enable_stencilwrite(bool enable)
  956. {
  957. graphics_t graphics = thread_graphics;
  958. graphics->exports.device_enable_stencilwrite(graphics->device, enable);
  959. }
  960. void gs_enable_color(bool red, bool green, bool blue, bool alpha)
  961. {
  962. graphics_t graphics = thread_graphics;
  963. graphics->exports.device_enable_color(graphics->device, red, green,
  964. blue, alpha);
  965. }
  966. void gs_blendfunction(enum gs_blend_type src, enum gs_blend_type dest)
  967. {
  968. graphics_t graphics = thread_graphics;
  969. graphics->exports.device_blendfunction(graphics->device, src, dest);
  970. }
  971. void gs_depthfunction(enum gs_depth_test test)
  972. {
  973. graphics_t graphics = thread_graphics;
  974. graphics->exports.device_depthfunction(graphics->device, test);
  975. }
  976. void gs_stencilfunction(enum gs_stencil_side side, enum gs_depth_test test)
  977. {
  978. graphics_t graphics = thread_graphics;
  979. graphics->exports.device_stencilfunction(graphics->device, side, test);
  980. }
  981. void gs_stencilop(enum gs_stencil_side side, enum gs_stencil_op fail,
  982. enum gs_stencil_op zfail, enum gs_stencil_op zpass)
  983. {
  984. graphics_t graphics = thread_graphics;
  985. graphics->exports.device_stencilop(graphics->device, side, fail, zfail,
  986. zpass);
  987. }
  988. void gs_enable_fullscreen(bool enable)
  989. {
  990. graphics_t graphics = thread_graphics;
  991. graphics->exports.device_enable_fullscreen(graphics->device, enable);
  992. }
  993. int gs_fullscreen_enabled(void)
  994. {
  995. graphics_t graphics = thread_graphics;
  996. return graphics->exports.device_fullscreen_enabled(graphics->device);
  997. }
  998. void gs_setdisplaymode(const struct gs_display_mode *mode)
  999. {
  1000. graphics_t graphics = thread_graphics;
  1001. graphics->exports.device_setdisplaymode(graphics->device, mode);
  1002. }
  1003. void gs_getdisplaymode(struct gs_display_mode *mode)
  1004. {
  1005. graphics_t graphics = thread_graphics;
  1006. graphics->exports.device_getdisplaymode(graphics->device, mode);
  1007. }
  1008. void gs_setcolorramp(float gamma, float brightness, float contrast)
  1009. {
  1010. graphics_t graphics = thread_graphics;
  1011. graphics->exports.device_setcolorramp(graphics->device, gamma,
  1012. brightness, contrast);
  1013. }
  1014. void gs_setviewport(int x, int y, int width, int height)
  1015. {
  1016. graphics_t graphics = thread_graphics;
  1017. graphics->exports.device_setviewport(graphics->device, x, y, width,
  1018. height);
  1019. }
  1020. void gs_getviewport(struct gs_rect *rect)
  1021. {
  1022. graphics_t graphics = thread_graphics;
  1023. graphics->exports.device_getviewport(graphics->device, rect);
  1024. }
  1025. void gs_setscissorrect(struct gs_rect *rect)
  1026. {
  1027. graphics_t graphics = thread_graphics;
  1028. graphics->exports.device_setscissorrect(graphics->device, rect);
  1029. }
  1030. void gs_ortho(float left, float right, float top, float bottom, float znear,
  1031. float zfar)
  1032. {
  1033. graphics_t graphics = thread_graphics;
  1034. graphics->exports.device_ortho(graphics->device, left, right, top,
  1035. bottom, znear, zfar);
  1036. }
  1037. void gs_frustum(float left, float right, float top, float bottom, float znear,
  1038. float zfar)
  1039. {
  1040. graphics_t graphics = thread_graphics;
  1041. graphics->exports.device_frustum(graphics->device, left, right, top,
  1042. bottom, znear, zfar);
  1043. }
  1044. void gs_projection_push(void)
  1045. {
  1046. graphics_t graphics = thread_graphics;
  1047. graphics->exports.device_projection_push(graphics->device);
  1048. }
  1049. void gs_projection_pop(void)
  1050. {
  1051. graphics_t graphics = thread_graphics;
  1052. graphics->exports.device_projection_pop(graphics->device);
  1053. }
  1054. void swapchain_destroy(swapchain_t swapchain)
  1055. {
  1056. graphics_t graphics = thread_graphics;
  1057. graphics->exports.swapchain_destroy(swapchain);
  1058. }
  1059. void shader_destroy(shader_t shader)
  1060. {
  1061. graphics_t graphics = thread_graphics;
  1062. graphics->exports.shader_destroy(shader);
  1063. }
  1064. int shader_numparams(shader_t shader)
  1065. {
  1066. graphics_t graphics = thread_graphics;
  1067. return graphics->exports.shader_numparams(shader);
  1068. }
  1069. sparam_t shader_getparambyidx(shader_t shader, uint32_t param)
  1070. {
  1071. graphics_t graphics = thread_graphics;
  1072. return graphics->exports.shader_getparambyidx(shader, param);
  1073. }
  1074. sparam_t shader_getparambyname(shader_t shader, const char *name)
  1075. {
  1076. graphics_t graphics = thread_graphics;
  1077. return graphics->exports.shader_getparambyname(shader, name);
  1078. }
  1079. void shader_getparaminfo(shader_t shader, sparam_t param,
  1080. struct shader_param_info *info)
  1081. {
  1082. graphics_t graphics = thread_graphics;
  1083. graphics->exports.shader_getparaminfo(shader, param, info);
  1084. }
  1085. sparam_t shader_getviewprojmatrix(shader_t shader)
  1086. {
  1087. graphics_t graphics = thread_graphics;
  1088. return graphics->exports.shader_getviewprojmatrix(shader);
  1089. }
  1090. sparam_t shader_getworldmatrix(shader_t shader)
  1091. {
  1092. graphics_t graphics = thread_graphics;
  1093. return graphics->exports.shader_getworldmatrix(shader);
  1094. }
  1095. void shader_setbool(shader_t shader, sparam_t param, bool val)
  1096. {
  1097. graphics_t graphics = thread_graphics;
  1098. graphics->exports.shader_setbool(shader, param, val);
  1099. }
  1100. void shader_setfloat(shader_t shader, sparam_t param, float val)
  1101. {
  1102. graphics_t graphics = thread_graphics;
  1103. graphics->exports.shader_setfloat(shader, param, val);
  1104. }
  1105. void shader_setint(shader_t shader, sparam_t param, int val)
  1106. {
  1107. graphics_t graphics = thread_graphics;
  1108. graphics->exports.shader_setint(shader, param, val);
  1109. }
  1110. void shader_setmatrix3(shader_t shader, sparam_t param,
  1111. const struct matrix3 *val)
  1112. {
  1113. graphics_t graphics = thread_graphics;
  1114. graphics->exports.shader_setmatrix3(shader, param, val);
  1115. }
  1116. void shader_setmatrix4(shader_t shader, sparam_t param,
  1117. const struct matrix4 *val)
  1118. {
  1119. graphics_t graphics = thread_graphics;
  1120. graphics->exports.shader_setmatrix4(shader, param, val);
  1121. }
  1122. void shader_setvec2(shader_t shader, sparam_t param,
  1123. const struct vec2 *val)
  1124. {
  1125. graphics_t graphics = thread_graphics;
  1126. graphics->exports.shader_setvec2(shader, param, val);
  1127. }
  1128. void shader_setvec3(shader_t shader, sparam_t param,
  1129. const struct vec3 *val)
  1130. {
  1131. graphics_t graphics = thread_graphics;
  1132. graphics->exports.shader_setvec3(shader, param, val);
  1133. }
  1134. void shader_setvec4(shader_t shader, sparam_t param,
  1135. const struct vec4 *val)
  1136. {
  1137. graphics_t graphics = thread_graphics;
  1138. graphics->exports.shader_setvec4(shader, param, val);
  1139. }
  1140. void shader_settexture(shader_t shader, sparam_t param, texture_t val)
  1141. {
  1142. graphics_t graphics = thread_graphics;
  1143. graphics->exports.shader_settexture(shader, param, val);
  1144. }
  1145. void shader_setval(shader_t shader, sparam_t param, const void *val,
  1146. size_t size)
  1147. {
  1148. graphics_t graphics = thread_graphics;
  1149. graphics->exports.shader_setval(shader, param, val, size);
  1150. }
  1151. void shader_setdefault(shader_t shader, sparam_t param)
  1152. {
  1153. graphics_t graphics = thread_graphics;
  1154. graphics->exports.shader_setdefault(shader, param);
  1155. }
  1156. void texture_destroy(texture_t tex)
  1157. {
  1158. graphics_t graphics = thread_graphics;
  1159. graphics->exports.texture_destroy(tex);
  1160. }
  1161. uint32_t texture_getwidth(texture_t tex)
  1162. {
  1163. graphics_t graphics = thread_graphics;
  1164. return graphics->exports.texture_getwidth(tex);
  1165. }
  1166. uint32_t texture_getheight(texture_t tex)
  1167. {
  1168. graphics_t graphics = thread_graphics;
  1169. return graphics->exports.texture_getheight(tex);
  1170. }
  1171. enum gs_color_format texture_getcolorformat(texture_t tex)
  1172. {
  1173. graphics_t graphics = thread_graphics;
  1174. return graphics->exports.texture_getcolorformat(tex);
  1175. }
  1176. bool texture_map(texture_t tex, void **ptr, uint32_t *row_bytes)
  1177. {
  1178. graphics_t graphics = thread_graphics;
  1179. return graphics->exports.texture_map(tex, ptr, row_bytes);
  1180. }
  1181. void texture_unmap(texture_t tex)
  1182. {
  1183. graphics_t graphics = thread_graphics;
  1184. graphics->exports.texture_unmap(tex);
  1185. }
  1186. bool texture_isrect(texture_t tex)
  1187. {
  1188. graphics_t graphics = thread_graphics;
  1189. if (graphics->exports.texture_isrect)
  1190. return graphics->exports.texture_isrect(tex);
  1191. else
  1192. return false;
  1193. }
  1194. void cubetexture_destroy(texture_t cubetex)
  1195. {
  1196. graphics_t graphics = thread_graphics;
  1197. graphics->exports.cubetexture_destroy(cubetex);
  1198. }
  1199. uint32_t cubetexture_getsize(texture_t cubetex)
  1200. {
  1201. graphics_t graphics = thread_graphics;
  1202. return graphics->exports.cubetexture_getsize(cubetex);
  1203. }
  1204. enum gs_color_format cubetexture_getcolorformat(texture_t cubetex)
  1205. {
  1206. graphics_t graphics = thread_graphics;
  1207. return graphics->exports.cubetexture_getcolorformat(cubetex);
  1208. }
  1209. void volumetexture_destroy(texture_t voltex)
  1210. {
  1211. graphics_t graphics = thread_graphics;
  1212. graphics->exports.volumetexture_destroy(voltex);
  1213. }
  1214. uint32_t volumetexture_getwidth(texture_t voltex)
  1215. {
  1216. graphics_t graphics = thread_graphics;
  1217. return graphics->exports.volumetexture_getwidth(voltex);
  1218. }
  1219. uint32_t volumetexture_getheight(texture_t voltex)
  1220. {
  1221. graphics_t graphics = thread_graphics;
  1222. return graphics->exports.volumetexture_getheight(voltex);
  1223. }
  1224. uint32_t volumetexture_getdepth(texture_t voltex)
  1225. {
  1226. graphics_t graphics = thread_graphics;
  1227. return graphics->exports.volumetexture_getdepth(voltex);
  1228. }
  1229. enum gs_color_format volumetexture_getcolorformat(texture_t voltex)
  1230. {
  1231. graphics_t graphics = thread_graphics;
  1232. return graphics->exports.volumetexture_getcolorformat(voltex);
  1233. }
  1234. void stagesurface_destroy(stagesurf_t stagesurf)
  1235. {
  1236. graphics_t graphics = thread_graphics;
  1237. graphics->exports.stagesurface_destroy(stagesurf);
  1238. }
  1239. uint32_t stagesurface_getwidth(stagesurf_t stagesurf)
  1240. {
  1241. graphics_t graphics = thread_graphics;
  1242. return graphics->exports.stagesurface_getwidth(stagesurf);
  1243. }
  1244. uint32_t stagesurface_getheight(stagesurf_t stagesurf)
  1245. {
  1246. graphics_t graphics = thread_graphics;
  1247. return graphics->exports.stagesurface_getheight(stagesurf);
  1248. }
  1249. enum gs_color_format stagesurface_getcolorformat(stagesurf_t stagesurf)
  1250. {
  1251. graphics_t graphics = thread_graphics;
  1252. return graphics->exports.stagesurface_getcolorformat(stagesurf);
  1253. }
  1254. bool stagesurface_map(stagesurf_t stagesurf, const void **data,
  1255. uint32_t *row_bytes)
  1256. {
  1257. graphics_t graphics = thread_graphics;
  1258. return graphics->exports.stagesurface_map(stagesurf, data, row_bytes);
  1259. }
  1260. void stagesurface_unmap(stagesurf_t stagesurf)
  1261. {
  1262. graphics_t graphics = thread_graphics;
  1263. graphics->exports.stagesurface_unmap(stagesurf);
  1264. }
  1265. void zstencil_destroy(zstencil_t zstencil)
  1266. {
  1267. thread_graphics->exports.zstencil_destroy(zstencil);
  1268. }
  1269. void samplerstate_destroy(samplerstate_t samplerstate)
  1270. {
  1271. thread_graphics->exports.samplerstate_destroy(samplerstate);
  1272. }
  1273. void vertexbuffer_destroy(vertbuffer_t vertbuffer)
  1274. {
  1275. graphics_t graphics = thread_graphics;
  1276. graphics->exports.vertexbuffer_destroy(vertbuffer);
  1277. }
  1278. void vertexbuffer_flush(vertbuffer_t vertbuffer, bool rebuild)
  1279. {
  1280. thread_graphics->exports.vertexbuffer_flush(vertbuffer, rebuild);
  1281. }
  1282. struct vb_data *vertexbuffer_getdata(vertbuffer_t vertbuffer)
  1283. {
  1284. return thread_graphics->exports.vertexbuffer_getdata(vertbuffer);
  1285. }
  1286. void indexbuffer_destroy(indexbuffer_t indexbuffer)
  1287. {
  1288. graphics_t graphics = thread_graphics;
  1289. graphics->exports.indexbuffer_destroy(indexbuffer);
  1290. }
  1291. void indexbuffer_flush(indexbuffer_t indexbuffer)
  1292. {
  1293. thread_graphics->exports.indexbuffer_flush(indexbuffer);
  1294. }
  1295. void *indexbuffer_getdata(indexbuffer_t indexbuffer)
  1296. {
  1297. return thread_graphics->exports.indexbuffer_getdata(indexbuffer);
  1298. }
  1299. size_t indexbuffer_numindices(indexbuffer_t indexbuffer)
  1300. {
  1301. return thread_graphics->exports.indexbuffer_numindices(indexbuffer);
  1302. }
  1303. enum gs_index_type indexbuffer_gettype(indexbuffer_t indexbuffer)
  1304. {
  1305. return thread_graphics->exports.indexbuffer_gettype(indexbuffer);
  1306. }
  1307. /** Platform specific functions */
  1308. texture_t gs_create_texture_from_iosurface(void *iosurf)
  1309. {
  1310. graphics_t graphics = thread_graphics;
  1311. if (!graphics->exports.texture_create_from_iosurface)
  1312. return NULL;
  1313. return graphics->exports.texture_create_from_iosurface(
  1314. graphics->device, iosurf);
  1315. }
  1316. bool texture_rebind_iosurface(texture_t texture, void *iosurf)
  1317. {
  1318. graphics_t graphics = thread_graphics;
  1319. if (!graphics->exports.texture_rebind_iosurface)
  1320. return false;
  1321. return graphics->exports.texture_rebind_iosurface(texture, iosurf);
  1322. }