graphics.c 45 KB

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