graphics.c 45 KB

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