graphics.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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 gs_vb_data *vbd;
  38. vbd = gs_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 gs_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_vertexbuffer_create(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 gs_vb_data *vbd;
  57. vbd = gs_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 gs_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_vertexbuffer_create(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_enter_context(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_blend_function(graphics->device,
  85. GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  86. graphics->cur_blend_state.enabled = true;
  87. graphics->cur_blend_state.src = GS_BLEND_SRCALPHA;
  88. graphics->cur_blend_state.dest = GS_BLEND_INVSRCALPHA;
  89. graphics->exports.device_leave_context(graphics->device);
  90. gs_init_image_deps();
  91. return true;
  92. }
  93. int gs_create(graphics_t **pgraphics, const char *module,
  94. struct gs_init_data *data)
  95. {
  96. struct gs_init_data new_data = *data;
  97. int errcode = GS_ERROR_FAIL;
  98. graphics_t *graphics = bzalloc(sizeof(struct graphics_subsystem));
  99. pthread_mutex_init_value(&graphics->mutex);
  100. if (!new_data.num_backbuffers)
  101. new_data.num_backbuffers = 1;
  102. graphics->module = os_dlopen(module);
  103. if (!graphics->module) {
  104. errcode = GS_ERROR_MODULE_NOT_FOUND;
  105. goto error;
  106. }
  107. if (!load_graphics_imports(&graphics->exports, graphics->module,
  108. module))
  109. goto error;
  110. errcode = graphics->exports.device_create(&graphics->device, &new_data);
  111. if (errcode != GS_SUCCESS)
  112. goto error;
  113. if (!graphics_init(graphics)) {
  114. errcode = GS_ERROR_FAIL;
  115. goto error;
  116. }
  117. *pgraphics = graphics;
  118. return errcode;
  119. error:
  120. gs_destroy(graphics);
  121. return errcode;
  122. }
  123. void gs_destroy(graphics_t *graphics)
  124. {
  125. if (!graphics)
  126. return;
  127. while (thread_graphics)
  128. gs_leave_context();
  129. if (graphics->device) {
  130. graphics->exports.device_enter_context(graphics->device);
  131. graphics->exports.gs_vertexbuffer_destroy(
  132. graphics->sprite_buffer);
  133. graphics->exports.gs_vertexbuffer_destroy(
  134. graphics->immediate_vertbuffer);
  135. graphics->exports.device_destroy(graphics->device);
  136. }
  137. pthread_mutex_destroy(&graphics->mutex);
  138. da_free(graphics->matrix_stack);
  139. da_free(graphics->viewport_stack);
  140. if (graphics->module)
  141. os_dlclose(graphics->module);
  142. bfree(graphics);
  143. gs_free_image_deps();
  144. }
  145. void gs_enter_context(graphics_t *graphics)
  146. {
  147. if (!graphics) return;
  148. bool is_current = thread_graphics == graphics;
  149. if (thread_graphics && !is_current) {
  150. while (thread_graphics)
  151. gs_leave_context();
  152. }
  153. if (!is_current) {
  154. pthread_mutex_lock(&graphics->mutex);
  155. graphics->exports.device_enter_context(graphics->device);
  156. thread_graphics = graphics;
  157. }
  158. os_atomic_inc_long(&graphics->ref);
  159. }
  160. void gs_leave_context(void)
  161. {
  162. if (thread_graphics) {
  163. if (!os_atomic_dec_long(&thread_graphics->ref)) {
  164. graphics_t *graphics = thread_graphics;
  165. graphics->exports.device_leave_context(
  166. graphics->device);
  167. pthread_mutex_unlock(&graphics->mutex);
  168. thread_graphics = NULL;
  169. }
  170. }
  171. }
  172. graphics_t *gs_get_context(void)
  173. {
  174. return thread_graphics;
  175. }
  176. const char *gs_get_device_name(void)
  177. {
  178. return thread_graphics ?
  179. thread_graphics->exports.device_get_name() : NULL;
  180. }
  181. int gs_get_device_type(void)
  182. {
  183. return thread_graphics ?
  184. thread_graphics->exports.device_get_type() : -1;
  185. }
  186. static inline struct matrix4 *top_matrix(graphics_t *graphics)
  187. {
  188. return graphics ?
  189. (graphics->matrix_stack.array + graphics->cur_matrix) : NULL;
  190. }
  191. void gs_matrix_push(void)
  192. {
  193. graphics_t *graphics = thread_graphics;
  194. if (!graphics)
  195. return;
  196. struct matrix4 mat, *top_mat = top_matrix(graphics);
  197. memcpy(&mat, top_mat, sizeof(struct matrix4));
  198. da_push_back(graphics->matrix_stack, &mat);
  199. graphics->cur_matrix++;
  200. }
  201. void gs_matrix_pop(void)
  202. {
  203. graphics_t *graphics = thread_graphics;
  204. if (!graphics)
  205. return;
  206. if (graphics->cur_matrix == 0) {
  207. blog(LOG_ERROR, "Tried to pop last matrix on stack");
  208. return;
  209. }
  210. da_erase(graphics->matrix_stack, graphics->cur_matrix);
  211. graphics->cur_matrix--;
  212. }
  213. void gs_matrix_identity(void)
  214. {
  215. struct matrix4 *top_mat = top_matrix(thread_graphics);
  216. if (top_mat)
  217. matrix4_identity(top_mat);
  218. }
  219. void gs_matrix_transpose(void)
  220. {
  221. struct matrix4 *top_mat = top_matrix(thread_graphics);
  222. if (top_mat)
  223. matrix4_transpose(top_mat, top_mat);
  224. }
  225. void gs_matrix_set(const struct matrix4 *matrix)
  226. {
  227. struct matrix4 *top_mat = top_matrix(thread_graphics);
  228. if (top_mat)
  229. matrix4_copy(top_mat, matrix);
  230. }
  231. void gs_matrix_get(struct matrix4 *dst)
  232. {
  233. struct matrix4 *top_mat = top_matrix(thread_graphics);
  234. if (top_mat)
  235. matrix4_copy(dst, top_mat);
  236. }
  237. void gs_matrix_mul(const struct matrix4 *matrix)
  238. {
  239. struct matrix4 *top_mat = top_matrix(thread_graphics);
  240. if (top_mat)
  241. matrix4_mul(top_mat, matrix, top_mat);
  242. }
  243. void gs_matrix_rotquat(const struct quat *rot)
  244. {
  245. struct matrix4 *top_mat = top_matrix(thread_graphics);
  246. if (top_mat)
  247. matrix4_rotate_i(top_mat, rot, top_mat);
  248. }
  249. void gs_matrix_rotaa(const struct axisang *rot)
  250. {
  251. struct matrix4 *top_mat = top_matrix(thread_graphics);
  252. if (top_mat)
  253. matrix4_rotate_aa_i(top_mat, rot, top_mat);
  254. }
  255. void gs_matrix_translate(const struct vec3 *pos)
  256. {
  257. struct matrix4 *top_mat = top_matrix(thread_graphics);
  258. if (top_mat)
  259. matrix4_translate3v_i(top_mat, pos, top_mat);
  260. }
  261. void gs_matrix_scale(const struct vec3 *scale)
  262. {
  263. struct matrix4 *top_mat = top_matrix(thread_graphics);
  264. if (top_mat)
  265. matrix4_scale_i(top_mat, scale, top_mat);
  266. }
  267. void gs_matrix_rotaa4f(float x, float y, float z, float angle)
  268. {
  269. struct matrix4 *top_mat = top_matrix(thread_graphics);
  270. struct axisang aa;
  271. if (top_mat) {
  272. axisang_set(&aa, x, y, z, angle);
  273. matrix4_rotate_aa_i(top_mat, &aa, top_mat);
  274. }
  275. }
  276. void gs_matrix_translate3f(float x, float y, float z)
  277. {
  278. struct matrix4 *top_mat = top_matrix(thread_graphics);
  279. struct vec3 p;
  280. if (top_mat) {
  281. vec3_set(&p, x, y, z);
  282. matrix4_translate3v_i(top_mat, &p, top_mat);
  283. }
  284. }
  285. void gs_matrix_scale3f(float x, float y, float z)
  286. {
  287. struct matrix4 *top_mat = top_matrix(thread_graphics);
  288. struct vec3 p;
  289. if (top_mat) {
  290. vec3_set(&p, x, y, z);
  291. matrix4_scale_i(top_mat, &p, top_mat);
  292. }
  293. }
  294. static inline void reset_immediate_arrays(graphics_t *graphics)
  295. {
  296. da_init(graphics->verts);
  297. da_init(graphics->norms);
  298. da_init(graphics->colors);
  299. for (size_t i = 0; i < 16; i++)
  300. da_init(graphics->texverts[i]);
  301. }
  302. void gs_render_start(bool b_new)
  303. {
  304. graphics_t *graphics = thread_graphics;
  305. if (!graphics)
  306. return;
  307. graphics->using_immediate = !b_new;
  308. reset_immediate_arrays(graphics);
  309. if (b_new) {
  310. graphics->vbd = gs_vbdata_create();
  311. } else {
  312. graphics->vbd = gs_vertexbuffer_get_data(
  313. graphics->immediate_vertbuffer);
  314. memset(graphics->vbd->colors, 0xFF,
  315. sizeof(uint32_t) * IMMEDIATE_COUNT);
  316. graphics->verts.array = graphics->vbd->points;
  317. graphics->norms.array = graphics->vbd->normals;
  318. graphics->colors.array = graphics->vbd->colors;
  319. graphics->texverts[0].array = graphics->vbd->tvarray[0].array;
  320. graphics->verts.capacity = IMMEDIATE_COUNT;
  321. graphics->norms.capacity = IMMEDIATE_COUNT;
  322. graphics->colors.capacity = IMMEDIATE_COUNT;
  323. graphics->texverts[0].capacity = IMMEDIATE_COUNT;
  324. }
  325. }
  326. static inline size_t min_size(const size_t a, const size_t b)
  327. {
  328. return (a < b) ? a : b;
  329. }
  330. void gs_render_stop(enum gs_draw_mode mode)
  331. {
  332. graphics_t *graphics = thread_graphics;
  333. size_t i, num;
  334. if (!graphics)
  335. return;
  336. num = graphics->verts.num;
  337. if (!num) {
  338. if (!graphics->using_immediate) {
  339. da_free(graphics->verts);
  340. da_free(graphics->norms);
  341. da_free(graphics->colors);
  342. for (i = 0; i < 16; i++)
  343. da_free(graphics->texverts[i]);
  344. gs_vbdata_destroy(graphics->vbd);
  345. }
  346. return;
  347. }
  348. if (graphics->norms.num &&
  349. (graphics->norms.num != graphics->verts.num)) {
  350. blog(LOG_ERROR, "gs_render_stop: normal count does "
  351. "not match vertex count");
  352. num = min_size(num, graphics->norms.num);
  353. }
  354. if (graphics->colors.num &&
  355. (graphics->colors.num != graphics->verts.num)) {
  356. blog(LOG_ERROR, "gs_render_stop: color count does "
  357. "not match vertex count");
  358. num = min_size(num, graphics->colors.num);
  359. }
  360. if (graphics->texverts[0].num &&
  361. (graphics->texverts[0].num != graphics->verts.num)) {
  362. blog(LOG_ERROR, "gs_render_stop: texture vertex count does "
  363. "not match vertex count");
  364. num = min_size(num, graphics->texverts[0].num);
  365. }
  366. if (graphics->using_immediate) {
  367. gs_vertexbuffer_flush(graphics->immediate_vertbuffer);
  368. gs_load_vertexbuffer(graphics->immediate_vertbuffer);
  369. gs_load_indexbuffer(NULL);
  370. gs_draw(mode, 0, (uint32_t)num);
  371. reset_immediate_arrays(graphics);
  372. } else {
  373. gs_vertbuffer_t *vb = gs_render_save();
  374. gs_load_vertexbuffer(vb);
  375. gs_load_indexbuffer(NULL);
  376. gs_draw(mode, 0, 0);
  377. gs_vertexbuffer_destroy(vb);
  378. }
  379. graphics->vbd = NULL;
  380. }
  381. gs_vertbuffer_t *gs_render_save(void)
  382. {
  383. graphics_t *graphics = thread_graphics;
  384. size_t num_tex, i;
  385. if (!graphics)
  386. return NULL;
  387. if (graphics->using_immediate)
  388. return NULL;
  389. if (!graphics->verts.num) {
  390. gs_vbdata_destroy(graphics->vbd);
  391. return NULL;
  392. }
  393. for (num_tex = 0; num_tex < 16; num_tex++)
  394. if (!graphics->texverts[num_tex].num)
  395. break;
  396. graphics->vbd->points = graphics->verts.array;
  397. graphics->vbd->normals = graphics->norms.array;
  398. graphics->vbd->colors = graphics->colors.array;
  399. graphics->vbd->num = graphics->verts.num;
  400. graphics->vbd->num_tex = num_tex;
  401. if (graphics->vbd->num_tex) {
  402. graphics->vbd->tvarray =
  403. bmalloc(sizeof(struct gs_tvertarray) * num_tex);
  404. for (i = 0; i < num_tex; i++) {
  405. graphics->vbd->tvarray[i].width = 2;
  406. graphics->vbd->tvarray[i].array =
  407. graphics->texverts[i].array;
  408. }
  409. }
  410. reset_immediate_arrays(graphics);
  411. return gs_vertexbuffer_create(graphics->vbd, 0);
  412. }
  413. void gs_vertex2f(float x, float y)
  414. {
  415. struct vec3 v3;
  416. vec3_set(&v3, x, y, 0.0f);
  417. gs_vertex3v(&v3);
  418. }
  419. void gs_vertex3f(float x, float y, float z)
  420. {
  421. struct vec3 v3;
  422. vec3_set(&v3, x, y, z);
  423. gs_vertex3v(&v3);
  424. }
  425. void gs_normal3f(float x, float y, float z)
  426. {
  427. struct vec3 v3;
  428. vec3_set(&v3, x, y, z);
  429. gs_normal3v(&v3);
  430. }
  431. static inline bool validvertsize(graphics_t *graphics, size_t num,
  432. const char *name)
  433. {
  434. if (graphics->using_immediate && num == IMMEDIATE_COUNT) {
  435. blog(LOG_ERROR, "%s: tried to use over %u "
  436. "for immediate rendering",
  437. name, IMMEDIATE_COUNT);
  438. return false;
  439. }
  440. return true;
  441. }
  442. void gs_color(uint32_t color)
  443. {
  444. graphics_t *graphics = thread_graphics;
  445. if (!graphics)
  446. return;
  447. if (!validvertsize(graphics, graphics->colors.num, "gs_color"))
  448. return;
  449. da_push_back(graphics->colors, &color);
  450. }
  451. void gs_texcoord(float x, float y, int unit)
  452. {
  453. struct vec2 v2;
  454. vec2_set(&v2, x, y);
  455. gs_texcoord2v(&v2, unit);
  456. }
  457. void gs_vertex2v(const struct vec2 *v)
  458. {
  459. struct vec3 v3;
  460. vec3_set(&v3, v->x, v->y, 0.0f);
  461. gs_vertex3v(&v3);
  462. }
  463. void gs_vertex3v(const struct vec3 *v)
  464. {
  465. graphics_t *graphics = thread_graphics;
  466. if (!graphics)
  467. return;
  468. if (!validvertsize(graphics, graphics->verts.num, "gs_vertex"))
  469. return;
  470. da_push_back(graphics->verts, v);
  471. }
  472. void gs_normal3v(const struct vec3 *v)
  473. {
  474. graphics_t *graphics = thread_graphics;
  475. if (!graphics)
  476. return;
  477. if (!validvertsize(graphics, graphics->norms.num, "gs_normal"))
  478. return;
  479. da_push_back(graphics->norms, v);
  480. }
  481. void gs_color4v(const struct vec4 *v)
  482. {
  483. /* TODO */
  484. UNUSED_PARAMETER(v);
  485. }
  486. void gs_texcoord2v(const struct vec2 *v, int unit)
  487. {
  488. graphics_t *graphics = thread_graphics;
  489. if (!graphics)
  490. return;
  491. if (!validvertsize(graphics, graphics->texverts[unit].num,
  492. "gs_texcoord"))
  493. return;
  494. da_push_back(graphics->texverts[unit], v);
  495. }
  496. input_t *gs_get_input(void)
  497. {
  498. /* TODO */
  499. return NULL;
  500. }
  501. gs_effect_t *gs_get_effect(void)
  502. {
  503. return thread_graphics ? thread_graphics->cur_effect : NULL;
  504. }
  505. gs_effect_t *gs_effect_create_from_file(const char *file, char **error_string)
  506. {
  507. char *file_string;
  508. gs_effect_t *effect = NULL;
  509. if (!thread_graphics || !file)
  510. return NULL;
  511. file_string = os_quick_read_utf8_file(file);
  512. if (!file_string) {
  513. blog(LOG_ERROR, "Could not load effect file '%s'", file);
  514. return NULL;
  515. }
  516. effect = gs_effect_create(file_string, file, error_string);
  517. bfree(file_string);
  518. return effect;
  519. }
  520. gs_effect_t *gs_effect_create(const char *effect_string, const char *filename,
  521. char **error_string)
  522. {
  523. if (!thread_graphics || !effect_string)
  524. return NULL;
  525. struct gs_effect *effect = bzalloc(sizeof(struct gs_effect));
  526. struct effect_parser parser;
  527. bool success;
  528. effect->graphics = thread_graphics;
  529. ep_init(&parser);
  530. success = ep_parse(&parser, effect, effect_string, filename);
  531. if (!success) {
  532. if (error_string)
  533. *error_string = error_data_buildstring(
  534. &parser.cfp.error_list);
  535. gs_effect_destroy(effect);
  536. effect = NULL;
  537. }
  538. ep_free(&parser);
  539. return effect;
  540. }
  541. gs_shader_t *gs_vertexshader_create_from_file(const char *file,
  542. char **error_string)
  543. {
  544. if (!thread_graphics || !file)
  545. return NULL;
  546. char *file_string;
  547. gs_shader_t *shader = NULL;
  548. file_string = os_quick_read_utf8_file(file);
  549. if (!file_string) {
  550. blog(LOG_ERROR, "Could not load vertex shader file '%s'",
  551. file);
  552. return NULL;
  553. }
  554. shader = gs_vertexshader_create(file_string, file, error_string);
  555. bfree(file_string);
  556. return shader;
  557. }
  558. gs_shader_t *gs_pixelshader_create_from_file(const char *file,
  559. char **error_string)
  560. {
  561. char *file_string;
  562. gs_shader_t *shader = NULL;
  563. if (!thread_graphics || !file)
  564. return NULL;
  565. file_string = os_quick_read_utf8_file(file);
  566. if (!file_string) {
  567. blog(LOG_ERROR, "Could not load pixel shader file '%s'",
  568. file);
  569. return NULL;
  570. }
  571. shader = gs_pixelshader_create(file_string, file, error_string);
  572. bfree(file_string);
  573. return shader;
  574. }
  575. static inline void assign_sprite_rect(float *start, float *end, float size,
  576. bool flip)
  577. {
  578. if (!flip) {
  579. *start = 0.0f;
  580. *end = size;
  581. } else {
  582. *start = size;
  583. *end = 0.0f;
  584. }
  585. }
  586. static inline void assign_sprite_uv(float *start, float *end, bool flip)
  587. {
  588. if (!flip) {
  589. *start = 0.0f;
  590. *end = 1.0f;
  591. } else {
  592. *start = 1.0f;
  593. *end = 0.0f;
  594. }
  595. }
  596. static void build_sprite(struct gs_vb_data *data, float fcx, float fcy,
  597. float start_u, float end_u, float start_v, float end_v)
  598. {
  599. struct vec2 *tvarray = data->tvarray[0].array;
  600. vec3_zero(data->points);
  601. vec3_set(data->points+1, fcx, 0.0f, 0.0f);
  602. vec3_set(data->points+2, 0.0f, fcy, 0.0f);
  603. vec3_set(data->points+3, fcx, fcy, 0.0f);
  604. vec2_set(tvarray, start_u, start_v);
  605. vec2_set(tvarray+1, end_u, start_v);
  606. vec2_set(tvarray+2, start_u, end_v);
  607. vec2_set(tvarray+3, end_u, end_v);
  608. }
  609. static inline void build_sprite_norm(struct gs_vb_data *data, float fcx,
  610. float fcy, uint32_t flip)
  611. {
  612. float start_u, end_u;
  613. float start_v, end_v;
  614. assign_sprite_uv(&start_u, &end_u, (flip & GS_FLIP_U) != 0);
  615. assign_sprite_uv(&start_v, &end_v, (flip & GS_FLIP_V) != 0);
  616. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
  617. }
  618. static inline void build_sprite_rect(struct gs_vb_data *data, gs_texture_t *tex,
  619. float fcx, float fcy, uint32_t flip)
  620. {
  621. float start_u, end_u;
  622. float start_v, end_v;
  623. float width = (float)gs_texture_get_width(tex);
  624. float height = (float)gs_texture_get_height(tex);
  625. assign_sprite_rect(&start_u, &end_u, width, (flip & GS_FLIP_U) != 0);
  626. assign_sprite_rect(&start_v, &end_v, height, (flip & GS_FLIP_V) != 0);
  627. build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v);
  628. }
  629. void gs_draw_sprite(gs_texture_t *tex, uint32_t flip, uint32_t width,
  630. uint32_t height)
  631. {
  632. graphics_t *graphics = thread_graphics;
  633. float fcx, fcy;
  634. struct gs_vb_data *data;
  635. assert(tex);
  636. if (!tex || !thread_graphics)
  637. return;
  638. if (gs_get_texture_type(tex) != GS_TEXTURE_2D) {
  639. blog(LOG_ERROR, "A sprite must be a 2D texture");
  640. return;
  641. }
  642. fcx = width ? (float)width : (float)gs_texture_get_width(tex);
  643. fcy = height ? (float)height : (float)gs_texture_get_height(tex);
  644. data = gs_vertexbuffer_get_data(graphics->sprite_buffer);
  645. if (gs_texture_is_rect(tex))
  646. build_sprite_rect(data, tex, fcx, fcy, flip);
  647. else
  648. build_sprite_norm(data, fcx, fcy, flip);
  649. gs_vertexbuffer_flush(graphics->sprite_buffer);
  650. gs_load_vertexbuffer(graphics->sprite_buffer);
  651. gs_load_indexbuffer(NULL);
  652. gs_draw(GS_TRISTRIP, 0, 0);
  653. }
  654. void gs_draw_cube_backdrop(gs_texture_t *cubetex, const struct quat *rot,
  655. float left, float right, float top, float bottom, float znear)
  656. {
  657. /* TODO */
  658. UNUSED_PARAMETER(cubetex);
  659. UNUSED_PARAMETER(rot);
  660. UNUSED_PARAMETER(left);
  661. UNUSED_PARAMETER(right);
  662. UNUSED_PARAMETER(top);
  663. UNUSED_PARAMETER(bottom);
  664. UNUSED_PARAMETER(znear);
  665. }
  666. void gs_reset_viewport(void)
  667. {
  668. uint32_t cx, cy;
  669. assert(thread_graphics != NULL);
  670. gs_get_size(&cx, &cy);
  671. gs_set_viewport(0, 0, (int)cx, (int)cy);
  672. }
  673. void gs_set_2d_mode(void)
  674. {
  675. uint32_t cx, cy;
  676. assert(thread_graphics != NULL);
  677. gs_get_size(&cx, &cy);
  678. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -1.0, -1024.0f);
  679. }
  680. void gs_set_3d_mode(double fovy, double znear, double zvar)
  681. {
  682. /* TODO */
  683. UNUSED_PARAMETER(fovy);
  684. UNUSED_PARAMETER(znear);
  685. UNUSED_PARAMETER(zvar);
  686. }
  687. void gs_viewport_push(void)
  688. {
  689. if (!thread_graphics) return;
  690. struct gs_rect *rect = da_push_back_new(
  691. thread_graphics->viewport_stack);
  692. gs_get_viewport(rect);
  693. }
  694. void gs_viewport_pop(void)
  695. {
  696. struct gs_rect *rect;
  697. if (!thread_graphics || !thread_graphics->viewport_stack.num)
  698. return;
  699. rect = da_end(thread_graphics->viewport_stack);
  700. gs_set_viewport(rect->x, rect->y, rect->cx, rect->cy);
  701. da_pop_back(thread_graphics->viewport_stack);
  702. }
  703. void gs_texture_set_image(gs_texture_t *tex, const uint8_t *data,
  704. uint32_t linesize, bool flip)
  705. {
  706. uint8_t *ptr;
  707. uint32_t linesize_out;
  708. uint32_t row_copy;
  709. int32_t height;
  710. int32_t y;
  711. if (!thread_graphics || !tex)
  712. return;
  713. height = (int32_t)gs_texture_get_height(tex);
  714. if (!gs_texture_map(tex, &ptr, &linesize_out))
  715. return;
  716. row_copy = (linesize < linesize_out) ? linesize : linesize_out;
  717. if (flip) {
  718. for (y = height-1; y >= 0; y--)
  719. memcpy(ptr + (uint32_t)y * linesize_out,
  720. data + (uint32_t)y * linesize,
  721. row_copy);
  722. } else if (linesize == linesize_out) {
  723. memcpy(ptr, data, row_copy * height);
  724. } else {
  725. for (y = 0; y < height; y++)
  726. memcpy(ptr + (uint32_t)y * linesize_out,
  727. data + (uint32_t)y * linesize,
  728. row_copy);
  729. }
  730. gs_texture_unmap(tex);
  731. }
  732. void gs_cubetexture_set_image(gs_texture_t *cubetex, uint32_t side,
  733. const void *data, uint32_t linesize, bool invert)
  734. {
  735. /* TODO */
  736. UNUSED_PARAMETER(cubetex);
  737. UNUSED_PARAMETER(side);
  738. UNUSED_PARAMETER(data);
  739. UNUSED_PARAMETER(linesize);
  740. UNUSED_PARAMETER(invert);
  741. }
  742. void gs_perspective(float angle, float aspect, float near, float far)
  743. {
  744. graphics_t *graphics = thread_graphics;
  745. float xmin, xmax, ymin, ymax;
  746. if (!graphics) return;
  747. ymax = near * tanf(RAD(angle)*0.5f);
  748. ymin = -ymax;
  749. xmin = ymin * aspect;
  750. xmax = ymax * aspect;
  751. graphics->exports.device_frustum(graphics->device, xmin, xmax,
  752. ymin, ymax, near, far);
  753. }
  754. void gs_reset_blend_state(void)
  755. {
  756. graphics_t *graphics = thread_graphics;
  757. if (!graphics) return;
  758. if (!graphics->cur_blend_state.enabled)
  759. gs_enable_blending(true);
  760. if (graphics->cur_blend_state.src != GS_BLEND_SRCALPHA ||
  761. graphics->cur_blend_state.dest != GS_BLEND_INVSRCALPHA)
  762. gs_blend_function(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
  763. }
  764. /* ------------------------------------------------------------------------- */
  765. const char *gs_preprocessor_name(void)
  766. {
  767. graphics_t *graphics = thread_graphics;
  768. if (!graphics) return NULL;
  769. return graphics->exports.device_preprocessor_name();
  770. }
  771. gs_swapchain_t *gs_swapchain_create(struct gs_init_data *data)
  772. {
  773. struct gs_init_data new_data = *data;
  774. graphics_t *graphics = thread_graphics;
  775. if (!graphics) return NULL;
  776. if (new_data.num_backbuffers == 0)
  777. new_data.num_backbuffers = 1;
  778. return graphics->exports.device_swapchain_create(graphics->device,
  779. &new_data);
  780. }
  781. void gs_resize(uint32_t x, uint32_t y)
  782. {
  783. graphics_t *graphics = thread_graphics;
  784. if (!graphics) return;
  785. graphics->exports.device_resize(graphics->device, x, y);
  786. }
  787. void gs_get_size(uint32_t *x, uint32_t *y)
  788. {
  789. graphics_t *graphics = thread_graphics;
  790. if (!graphics) return;
  791. graphics->exports.device_get_size(graphics->device, x, y);
  792. }
  793. uint32_t gs_get_width(void)
  794. {
  795. graphics_t *graphics = thread_graphics;
  796. if (!graphics) return 0;
  797. return graphics->exports.device_get_width(graphics->device);
  798. }
  799. uint32_t gs_get_height(void)
  800. {
  801. graphics_t *graphics = thread_graphics;
  802. if (!graphics) return 0;
  803. return graphics->exports.device_get_height(graphics->device);
  804. }
  805. static inline bool is_pow2(uint32_t size)
  806. {
  807. return size >= 2 && (size & (size-1)) == 0;
  808. }
  809. gs_texture_t *gs_texture_create(uint32_t width, uint32_t height,
  810. enum gs_color_format color_format, uint32_t levels,
  811. const uint8_t **data, uint32_t flags)
  812. {
  813. graphics_t *graphics = thread_graphics;
  814. bool pow2tex = is_pow2(width) && is_pow2(height);
  815. bool uses_mipmaps = (flags & GS_BUILD_MIPMAPS || levels != 1);
  816. if (!graphics)
  817. return NULL;
  818. if (uses_mipmaps && !pow2tex) {
  819. blog(LOG_WARNING, "Cannot use mipmaps with a "
  820. "non-power-of-two texture. Disabling "
  821. "mipmaps for this texture.");
  822. uses_mipmaps = false;
  823. flags &= ~GS_BUILD_MIPMAPS;
  824. levels = 1;
  825. }
  826. if (uses_mipmaps && flags & GS_RENDER_TARGET) {
  827. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  828. "Disabling mipmaps for this texture.");
  829. flags &= ~GS_BUILD_MIPMAPS;
  830. levels = 1;
  831. }
  832. return graphics->exports.device_texture_create(graphics->device,
  833. width, height, color_format, levels, data, flags);
  834. }
  835. gs_texture_t *gs_cubetexture_create(uint32_t size,
  836. enum gs_color_format color_format, uint32_t levels,
  837. const uint8_t **data, uint32_t flags)
  838. {
  839. graphics_t *graphics = thread_graphics;
  840. bool pow2tex = is_pow2(size);
  841. bool uses_mipmaps = (flags & GS_BUILD_MIPMAPS || levels != 1);
  842. if (!graphics)
  843. return NULL;
  844. if (uses_mipmaps && !pow2tex) {
  845. blog(LOG_WARNING, "Cannot use mipmaps with a "
  846. "non-power-of-two texture. Disabling "
  847. "mipmaps for this texture.");
  848. uses_mipmaps = false;
  849. flags &= ~GS_BUILD_MIPMAPS;
  850. levels = 1;
  851. }
  852. if (uses_mipmaps && flags & GS_RENDER_TARGET) {
  853. blog(LOG_WARNING, "Cannot use mipmaps with render targets. "
  854. "Disabling mipmaps for this texture.");
  855. flags &= ~GS_BUILD_MIPMAPS;
  856. levels = 1;
  857. data = NULL;
  858. }
  859. return graphics->exports.device_cubetexture_create(graphics->device,
  860. size, color_format, levels, data, flags);
  861. }
  862. gs_texture_t *gs_voltexture_create(uint32_t width, uint32_t height,
  863. uint32_t depth, enum gs_color_format color_format,
  864. uint32_t levels, const uint8_t **data, uint32_t flags)
  865. {
  866. graphics_t *graphics = thread_graphics;
  867. if (!graphics) return NULL;
  868. return graphics->exports.device_voltexture_create(graphics->device,
  869. width, height, depth, color_format, levels, data,
  870. flags);
  871. }
  872. gs_zstencil_t *gs_zstencil_create(uint32_t width, uint32_t height,
  873. enum gs_zstencil_format format)
  874. {
  875. graphics_t *graphics = thread_graphics;
  876. if (!graphics) return NULL;
  877. return graphics->exports.device_zstencil_create(graphics->device,
  878. width, height, format);
  879. }
  880. gs_stagesurf_t *gs_stagesurface_create(uint32_t width, uint32_t height,
  881. enum gs_color_format color_format)
  882. {
  883. graphics_t *graphics = thread_graphics;
  884. if (!graphics) return NULL;
  885. return graphics->exports.device_stagesurface_create(graphics->device,
  886. width, height, color_format);
  887. }
  888. gs_samplerstate_t *gs_samplerstate_create(struct gs_sampler_info *info)
  889. {
  890. graphics_t *graphics = thread_graphics;
  891. if (!graphics) return NULL;
  892. return graphics->exports.device_samplerstate_create(graphics->device,
  893. info);
  894. }
  895. gs_shader_t *gs_vertexshader_create(const char *shader, const char *file,
  896. char **error_string)
  897. {
  898. graphics_t *graphics = thread_graphics;
  899. if (!graphics) return NULL;
  900. return graphics->exports.device_vertexshader_create(graphics->device,
  901. shader, file, error_string);
  902. }
  903. gs_shader_t *gs_pixelshader_create(const char *shader,
  904. const char *file, char **error_string)
  905. {
  906. graphics_t *graphics = thread_graphics;
  907. if (!graphics) return NULL;
  908. return graphics->exports.device_pixelshader_create(graphics->device,
  909. shader, file, error_string);
  910. }
  911. gs_vertbuffer_t *gs_vertexbuffer_create(struct gs_vb_data *data,
  912. uint32_t flags)
  913. {
  914. graphics_t *graphics = thread_graphics;
  915. if (!graphics) return NULL;
  916. return graphics->exports.device_vertexbuffer_create(graphics->device,
  917. data, flags);
  918. }
  919. gs_indexbuffer_t *gs_indexbuffer_create(enum gs_index_type type,
  920. void *indices, size_t num, uint32_t flags)
  921. {
  922. graphics_t *graphics = thread_graphics;
  923. if (!graphics) return NULL;
  924. return graphics->exports.device_indexbuffer_create(graphics->device,
  925. type, indices, num, flags);
  926. }
  927. enum gs_texture_type gs_get_texture_type(gs_texture_t *texture)
  928. {
  929. graphics_t *graphics = thread_graphics;
  930. if (!graphics) return GS_TEXTURE_2D;
  931. return graphics->exports.device_get_texture_type(texture);
  932. }
  933. void gs_load_vertexbuffer(gs_vertbuffer_t *vertbuffer)
  934. {
  935. graphics_t *graphics = thread_graphics;
  936. if (!graphics) return;
  937. graphics->exports.device_load_vertexbuffer(graphics->device,
  938. vertbuffer);
  939. }
  940. void gs_load_indexbuffer(gs_indexbuffer_t *indexbuffer)
  941. {
  942. graphics_t *graphics = thread_graphics;
  943. if (!graphics) return;
  944. graphics->exports.device_load_indexbuffer(graphics->device,
  945. indexbuffer);
  946. }
  947. void gs_load_texture(gs_texture_t *tex, int unit)
  948. {
  949. graphics_t *graphics = thread_graphics;
  950. if (!graphics) return;
  951. graphics->exports.device_load_texture(graphics->device, tex, unit);
  952. }
  953. void gs_load_samplerstate(gs_samplerstate_t *samplerstate, int unit)
  954. {
  955. graphics_t *graphics = thread_graphics;
  956. if (!graphics) return;
  957. graphics->exports.device_load_samplerstate(graphics->device,
  958. samplerstate, unit);
  959. }
  960. void gs_load_vertexshader(gs_shader_t *vertshader)
  961. {
  962. graphics_t *graphics = thread_graphics;
  963. if (!graphics) return;
  964. graphics->exports.device_load_vertexshader(graphics->device,
  965. vertshader);
  966. }
  967. void gs_load_pixelshader(gs_shader_t *pixelshader)
  968. {
  969. graphics_t *graphics = thread_graphics;
  970. if (!graphics) return;
  971. graphics->exports.device_load_pixelshader(graphics->device,
  972. pixelshader);
  973. }
  974. void gs_load_default_samplerstate(bool b_3d, int unit)
  975. {
  976. graphics_t *graphics = thread_graphics;
  977. if (!graphics) return;
  978. graphics->exports.device_load_default_samplerstate(graphics->device,
  979. b_3d, unit);
  980. }
  981. gs_shader_t *gs_get_vertex_shader(void)
  982. {
  983. graphics_t *graphics = thread_graphics;
  984. if (!graphics) return NULL;
  985. return graphics->exports.device_get_vertex_shader(graphics->device);
  986. }
  987. gs_shader_t *gs_get_pixel_shader(void)
  988. {
  989. graphics_t *graphics = thread_graphics;
  990. if (!graphics) return NULL;
  991. return graphics->exports.device_get_pixel_shader(graphics->device);
  992. }
  993. gs_texture_t *gs_get_render_target(void)
  994. {
  995. graphics_t *graphics = thread_graphics;
  996. if (!graphics) return NULL;
  997. return graphics->exports.device_get_render_target(graphics->device);
  998. }
  999. gs_zstencil_t *gs_get_zstencil_target(void)
  1000. {
  1001. graphics_t *graphics = thread_graphics;
  1002. if (!graphics) return NULL;
  1003. return graphics->exports.device_get_zstencil_target(graphics->device);
  1004. }
  1005. void gs_set_render_target(gs_texture_t *tex, gs_zstencil_t *zstencil)
  1006. {
  1007. graphics_t *graphics = thread_graphics;
  1008. if (!graphics) return;
  1009. graphics->exports.device_set_render_target(graphics->device, tex,
  1010. zstencil);
  1011. }
  1012. void gs_set_cube_render_target(gs_texture_t *cubetex, int side,
  1013. gs_zstencil_t *zstencil)
  1014. {
  1015. graphics_t *graphics = thread_graphics;
  1016. if (!graphics) return;
  1017. graphics->exports.device_set_cube_render_target(graphics->device,
  1018. cubetex, side, zstencil);
  1019. }
  1020. void gs_copy_texture(gs_texture_t *dst, gs_texture_t *src)
  1021. {
  1022. graphics_t *graphics = thread_graphics;
  1023. if (!graphics) return;
  1024. graphics->exports.device_copy_texture(graphics->device, dst, src);
  1025. }
  1026. void gs_copy_texture_region(gs_texture_t *dst, uint32_t dst_x, uint32_t dst_y,
  1027. gs_texture_t *src, uint32_t src_x, uint32_t src_y,
  1028. uint32_t src_w, uint32_t src_h)
  1029. {
  1030. graphics_t *graphics = thread_graphics;
  1031. if (!graphics) return;
  1032. graphics->exports.device_copy_texture_region(graphics->device,
  1033. dst, dst_x, dst_y,
  1034. src, src_x, src_y, src_w, src_h);
  1035. }
  1036. void gs_stage_texture(gs_stagesurf_t *dst, gs_texture_t *src)
  1037. {
  1038. graphics_t *graphics = thread_graphics;
  1039. if (!graphics) return;
  1040. graphics->exports.device_stage_texture(graphics->device, dst, src);
  1041. }
  1042. void gs_begin_scene(void)
  1043. {
  1044. graphics_t *graphics = thread_graphics;
  1045. if (!graphics) return;
  1046. graphics->exports.device_begin_scene(graphics->device);
  1047. }
  1048. void gs_draw(enum gs_draw_mode draw_mode, uint32_t start_vert,
  1049. uint32_t num_verts)
  1050. {
  1051. graphics_t *graphics = thread_graphics;
  1052. if (!graphics) return;
  1053. graphics->exports.device_draw(graphics->device, draw_mode,
  1054. start_vert, num_verts);
  1055. }
  1056. void gs_end_scene(void)
  1057. {
  1058. graphics_t *graphics = thread_graphics;
  1059. if (!graphics) return;
  1060. graphics->exports.device_end_scene(graphics->device);
  1061. }
  1062. void gs_load_swapchain(gs_swapchain_t *swapchain)
  1063. {
  1064. graphics_t *graphics = thread_graphics;
  1065. if (!graphics) return;
  1066. graphics->exports.device_load_swapchain(graphics->device, swapchain);
  1067. }
  1068. void gs_clear(uint32_t clear_flags, struct vec4 *color, float depth,
  1069. uint8_t stencil)
  1070. {
  1071. graphics_t *graphics = thread_graphics;
  1072. graphics->exports.device_clear(graphics->device, clear_flags, color,
  1073. depth, stencil);
  1074. }
  1075. void gs_present(void)
  1076. {
  1077. graphics_t *graphics = thread_graphics;
  1078. if (!graphics) return;
  1079. graphics->exports.device_present(graphics->device);
  1080. }
  1081. void gs_flush(void)
  1082. {
  1083. graphics_t *graphics = thread_graphics;
  1084. if (!graphics) return;
  1085. graphics->exports.device_flush(graphics->device);
  1086. }
  1087. void gs_set_cull_mode(enum gs_cull_mode mode)
  1088. {
  1089. graphics_t *graphics = thread_graphics;
  1090. if (!graphics) return;
  1091. graphics->exports.device_set_cull_mode(graphics->device, mode);
  1092. }
  1093. enum gs_cull_mode gs_get_cull_mode(void)
  1094. {
  1095. graphics_t *graphics = thread_graphics;
  1096. if (!graphics) return GS_NEITHER;
  1097. return graphics->exports.device_get_cull_mode(graphics->device);
  1098. }
  1099. void gs_enable_blending(bool enable)
  1100. {
  1101. graphics_t *graphics = thread_graphics;
  1102. if (!graphics) return;
  1103. graphics->cur_blend_state.enabled = enable;
  1104. graphics->exports.device_enable_blending(graphics->device, enable);
  1105. }
  1106. void gs_enable_depth_test(bool enable)
  1107. {
  1108. graphics_t *graphics = thread_graphics;
  1109. if (!graphics) return;
  1110. graphics->exports.device_enable_depth_test(graphics->device, enable);
  1111. }
  1112. void gs_enable_stencil_test(bool enable)
  1113. {
  1114. graphics_t *graphics = thread_graphics;
  1115. if (!graphics) return;
  1116. graphics->exports.device_enable_stencil_test(graphics->device, enable);
  1117. }
  1118. void gs_enable_stencil_write(bool enable)
  1119. {
  1120. graphics_t *graphics = thread_graphics;
  1121. if (!graphics) return;
  1122. graphics->exports.device_enable_stencil_write(graphics->device, enable);
  1123. }
  1124. void gs_enable_color(bool red, bool green, bool blue, bool alpha)
  1125. {
  1126. graphics_t *graphics = thread_graphics;
  1127. if (!graphics) return;
  1128. graphics->exports.device_enable_color(graphics->device, red, green,
  1129. blue, alpha);
  1130. }
  1131. void gs_blend_function(enum gs_blend_type src, enum gs_blend_type dest)
  1132. {
  1133. graphics_t *graphics = thread_graphics;
  1134. if (!graphics) return;
  1135. graphics->cur_blend_state.src = src;
  1136. graphics->cur_blend_state.dest = dest;
  1137. graphics->exports.device_blend_function(graphics->device, src, dest);
  1138. }
  1139. void gs_depth_function(enum gs_depth_test test)
  1140. {
  1141. graphics_t *graphics = thread_graphics;
  1142. if (!graphics) return;
  1143. graphics->exports.device_depth_function(graphics->device, test);
  1144. }
  1145. void gs_stencil_function(enum gs_stencil_side side, enum gs_depth_test test)
  1146. {
  1147. graphics_t *graphics = thread_graphics;
  1148. if (!graphics) return;
  1149. graphics->exports.device_stencil_function(graphics->device, side, test);
  1150. }
  1151. void gs_stencil_op(enum gs_stencil_side side, enum gs_stencil_op_type fail,
  1152. enum gs_stencil_op_type zfail, enum gs_stencil_op_type zpass)
  1153. {
  1154. graphics_t *graphics = thread_graphics;
  1155. if (!graphics) return;
  1156. graphics->exports.device_stencil_op(graphics->device, side, fail, zfail,
  1157. zpass);
  1158. }
  1159. void gs_set_viewport(int x, int y, int width, int height)
  1160. {
  1161. graphics_t *graphics = thread_graphics;
  1162. if (!graphics) return;
  1163. graphics->exports.device_set_viewport(graphics->device, x, y, width,
  1164. height);
  1165. }
  1166. void gs_get_viewport(struct gs_rect *rect)
  1167. {
  1168. graphics_t *graphics = thread_graphics;
  1169. if (!graphics) return;
  1170. graphics->exports.device_get_viewport(graphics->device, rect);
  1171. }
  1172. void gs_set_scissor_rect(struct gs_rect *rect)
  1173. {
  1174. graphics_t *graphics = thread_graphics;
  1175. if (!graphics) return;
  1176. graphics->exports.device_set_scissor_rect(graphics->device, rect);
  1177. }
  1178. void gs_ortho(float left, float right, float top, float bottom, float znear,
  1179. float zfar)
  1180. {
  1181. graphics_t *graphics = thread_graphics;
  1182. if (!graphics) return;
  1183. graphics->exports.device_ortho(graphics->device, left, right, top,
  1184. bottom, znear, zfar);
  1185. }
  1186. void gs_frustum(float left, float right, float top, float bottom, float znear,
  1187. float zfar)
  1188. {
  1189. graphics_t *graphics = thread_graphics;
  1190. if (!graphics) return;
  1191. graphics->exports.device_frustum(graphics->device, left, right, top,
  1192. bottom, znear, zfar);
  1193. }
  1194. void gs_projection_push(void)
  1195. {
  1196. graphics_t *graphics = thread_graphics;
  1197. if (!graphics) return;
  1198. graphics->exports.device_projection_push(graphics->device);
  1199. }
  1200. void gs_projection_pop(void)
  1201. {
  1202. graphics_t *graphics = thread_graphics;
  1203. if (!graphics) return;
  1204. graphics->exports.device_projection_pop(graphics->device);
  1205. }
  1206. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1207. {
  1208. graphics_t *graphics = thread_graphics;
  1209. if (!graphics || !swapchain) return;
  1210. graphics->exports.gs_swapchain_destroy(swapchain);
  1211. }
  1212. void gs_shader_destroy(gs_shader_t *shader)
  1213. {
  1214. graphics_t *graphics = thread_graphics;
  1215. if (!graphics || !shader) return;
  1216. graphics->exports.gs_shader_destroy(shader);
  1217. }
  1218. int gs_shader_get_num_params(gs_shader_t *shader)
  1219. {
  1220. graphics_t *graphics = thread_graphics;
  1221. if (!graphics || !shader) return 0;
  1222. return graphics->exports.gs_shader_get_num_params(shader);
  1223. }
  1224. gs_sparam_t *gs_shader_get_param_by_idx(gs_shader_t *shader, uint32_t param)
  1225. {
  1226. graphics_t *graphics = thread_graphics;
  1227. if (!graphics || !shader) return NULL;
  1228. return graphics->exports.gs_shader_get_param_by_idx(shader, param);
  1229. }
  1230. gs_sparam_t *gs_shader_get_param_by_name(gs_shader_t *shader, const char *name)
  1231. {
  1232. graphics_t *graphics = thread_graphics;
  1233. if (!graphics || !shader) return NULL;
  1234. return graphics->exports.gs_shader_get_param_by_name(shader, name);
  1235. }
  1236. gs_sparam_t *gs_shader_get_viewproj_matrix(gs_shader_t *shader)
  1237. {
  1238. graphics_t *graphics = thread_graphics;
  1239. if (!graphics || !shader) return NULL;
  1240. return graphics->exports.gs_shader_get_viewproj_matrix(shader);
  1241. }
  1242. gs_sparam_t *gs_shader_get_world_matrix(gs_shader_t *shader)
  1243. {
  1244. graphics_t *graphics = thread_graphics;
  1245. if (!graphics || !shader) return NULL;
  1246. return graphics->exports.gs_shader_get_world_matrix(shader);
  1247. }
  1248. void gs_shader_get_param_info(gs_sparam_t *param,
  1249. struct gs_shader_param_info *info)
  1250. {
  1251. graphics_t *graphics = thread_graphics;
  1252. if (!graphics || !param) return;
  1253. graphics->exports.gs_shader_get_param_info(param, info);
  1254. }
  1255. void gs_shader_set_bool(gs_sparam_t *param, bool val)
  1256. {
  1257. graphics_t *graphics = thread_graphics;
  1258. if (!graphics || !param) return;
  1259. graphics->exports.gs_shader_set_bool(param, val);
  1260. }
  1261. void gs_shader_set_float(gs_sparam_t *param, float val)
  1262. {
  1263. graphics_t *graphics = thread_graphics;
  1264. if (!graphics || !param) return;
  1265. graphics->exports.gs_shader_set_float(param, val);
  1266. }
  1267. void gs_shader_set_int(gs_sparam_t *param, int val)
  1268. {
  1269. graphics_t *graphics = thread_graphics;
  1270. if (!graphics || !param) return;
  1271. graphics->exports.gs_shader_set_int(param, val);
  1272. }
  1273. void gs_shader_setmatrix3(gs_sparam_t *param, const struct matrix3 *val)
  1274. {
  1275. graphics_t *graphics = thread_graphics;
  1276. if (!graphics || !param) return;
  1277. graphics->exports.gs_shader_setmatrix3(param, val);
  1278. }
  1279. void gs_shader_set_matrix4(gs_sparam_t *param, const struct matrix4 *val)
  1280. {
  1281. graphics_t *graphics = thread_graphics;
  1282. if (!graphics || !param) return;
  1283. graphics->exports.gs_shader_set_matrix4(param, val);
  1284. }
  1285. void gs_shader_set_vec2(gs_sparam_t *param, const struct vec2 *val)
  1286. {
  1287. graphics_t *graphics = thread_graphics;
  1288. if (!graphics || !param) return;
  1289. graphics->exports.gs_shader_set_vec2(param, val);
  1290. }
  1291. void gs_shader_set_vec3(gs_sparam_t *param, const struct vec3 *val)
  1292. {
  1293. graphics_t *graphics = thread_graphics;
  1294. if (!graphics || !param) return;
  1295. graphics->exports.gs_shader_set_vec3(param, val);
  1296. }
  1297. void gs_shader_set_vec4(gs_sparam_t *param, const struct vec4 *val)
  1298. {
  1299. graphics_t *graphics = thread_graphics;
  1300. if (!graphics || !param) return;
  1301. graphics->exports.gs_shader_set_vec4(param, val);
  1302. }
  1303. void gs_shader_set_texture(gs_sparam_t *param, gs_texture_t *val)
  1304. {
  1305. graphics_t *graphics = thread_graphics;
  1306. if (!graphics || !param) return;
  1307. graphics->exports.gs_shader_set_texture(param, val);
  1308. }
  1309. void gs_shader_set_val(gs_sparam_t *param, const void *val, size_t size)
  1310. {
  1311. graphics_t *graphics = thread_graphics;
  1312. if (!graphics || !param) return;
  1313. graphics->exports.gs_shader_set_val(param, val, size);
  1314. }
  1315. void gs_shader_set_default(gs_sparam_t *param)
  1316. {
  1317. graphics_t *graphics = thread_graphics;
  1318. if (!graphics || !param) return;
  1319. graphics->exports.gs_shader_set_default(param);
  1320. }
  1321. void gs_texture_destroy(gs_texture_t *tex)
  1322. {
  1323. graphics_t *graphics = thread_graphics;
  1324. if (!graphics || !tex) return;
  1325. graphics->exports.gs_texture_destroy(tex);
  1326. }
  1327. uint32_t gs_texture_get_width(gs_texture_t *tex)
  1328. {
  1329. graphics_t *graphics = thread_graphics;
  1330. if (!graphics || !tex) return 0;
  1331. return graphics->exports.gs_texture_get_width(tex);
  1332. }
  1333. uint32_t gs_texture_get_height(gs_texture_t *tex)
  1334. {
  1335. graphics_t *graphics = thread_graphics;
  1336. if (!graphics || !tex) return 0;
  1337. return graphics->exports.gs_texture_get_height(tex);
  1338. }
  1339. enum gs_color_format gs_texture_get_color_format(gs_texture_t *tex)
  1340. {
  1341. graphics_t *graphics = thread_graphics;
  1342. if (!graphics || !tex) return GS_UNKNOWN;
  1343. return graphics->exports.gs_texture_get_color_format(tex);
  1344. }
  1345. bool gs_texture_map(gs_texture_t *tex, uint8_t **ptr, uint32_t *linesize)
  1346. {
  1347. graphics_t *graphics = thread_graphics;
  1348. if (!graphics || !tex) return false;
  1349. return graphics->exports.gs_texture_map(tex, ptr, linesize);
  1350. }
  1351. void gs_texture_unmap(gs_texture_t *tex)
  1352. {
  1353. graphics_t *graphics = thread_graphics;
  1354. if (!graphics || !tex) return;
  1355. graphics->exports.gs_texture_unmap(tex);
  1356. }
  1357. bool gs_texture_is_rect(gs_texture_t *tex)
  1358. {
  1359. graphics_t *graphics = thread_graphics;
  1360. if (!graphics || !tex) return false;
  1361. if (graphics->exports.gs_texture_is_rect)
  1362. return graphics->exports.gs_texture_is_rect(tex);
  1363. else
  1364. return false;
  1365. }
  1366. void *gs_texture_get_obj(gs_texture_t *tex)
  1367. {
  1368. graphics_t *graphics = thread_graphics;
  1369. if (!graphics || !tex) return NULL;
  1370. return graphics->exports.gs_texture_get_obj(tex);
  1371. }
  1372. void gs_cubetexture_destroy(gs_texture_t *cubetex)
  1373. {
  1374. graphics_t *graphics = thread_graphics;
  1375. if (!graphics || !cubetex) return;
  1376. graphics->exports.gs_cubetexture_destroy(cubetex);
  1377. }
  1378. uint32_t gs_cubetexture_get_size(gs_texture_t *cubetex)
  1379. {
  1380. graphics_t *graphics = thread_graphics;
  1381. if (!graphics || !cubetex) return 0;
  1382. return graphics->exports.gs_cubetexture_get_size(cubetex);
  1383. }
  1384. enum gs_color_format gs_cubetexture_get_color_format(gs_texture_t *cubetex)
  1385. {
  1386. graphics_t *graphics = thread_graphics;
  1387. if (!graphics || !cubetex) return GS_UNKNOWN;
  1388. return graphics->exports.gs_cubetexture_get_color_format(cubetex);
  1389. }
  1390. void gs_voltexture_destroy(gs_texture_t *voltex)
  1391. {
  1392. graphics_t *graphics = thread_graphics;
  1393. if (!graphics || !voltex) return;
  1394. graphics->exports.gs_voltexture_destroy(voltex);
  1395. }
  1396. uint32_t gs_voltexture_get_width(gs_texture_t *voltex)
  1397. {
  1398. graphics_t *graphics = thread_graphics;
  1399. if (!graphics || !voltex) return 0;
  1400. return graphics->exports.gs_voltexture_get_width(voltex);
  1401. }
  1402. uint32_t gs_voltexture_get_height(gs_texture_t *voltex)
  1403. {
  1404. graphics_t *graphics = thread_graphics;
  1405. if (!graphics || !voltex) return 0;
  1406. return graphics->exports.gs_voltexture_get_height(voltex);
  1407. }
  1408. uint32_t gs_voltexture_getdepth(gs_texture_t *voltex)
  1409. {
  1410. graphics_t *graphics = thread_graphics;
  1411. if (!graphics || !voltex) return 0;
  1412. return graphics->exports.gs_voltexture_getdepth(voltex);
  1413. }
  1414. enum gs_color_format gs_voltexture_get_color_format(gs_texture_t *voltex)
  1415. {
  1416. graphics_t *graphics = thread_graphics;
  1417. if (!graphics || !voltex) return GS_UNKNOWN;
  1418. return graphics->exports.gs_voltexture_get_color_format(voltex);
  1419. }
  1420. void gs_stagesurface_destroy(gs_stagesurf_t *stagesurf)
  1421. {
  1422. graphics_t *graphics = thread_graphics;
  1423. if (!graphics || !stagesurf) return;
  1424. graphics->exports.gs_stagesurface_destroy(stagesurf);
  1425. }
  1426. uint32_t gs_stagesurface_get_width(gs_stagesurf_t *stagesurf)
  1427. {
  1428. graphics_t *graphics = thread_graphics;
  1429. if (!graphics || !stagesurf) return 0;
  1430. return graphics->exports.gs_stagesurface_get_width(stagesurf);
  1431. }
  1432. uint32_t gs_stagesurface_get_height(gs_stagesurf_t *stagesurf)
  1433. {
  1434. graphics_t *graphics = thread_graphics;
  1435. if (!graphics || !stagesurf) return 0;
  1436. return graphics->exports.gs_stagesurface_get_height(stagesurf);
  1437. }
  1438. enum gs_color_format gs_stagesurface_get_color_format(gs_stagesurf_t *stagesurf)
  1439. {
  1440. graphics_t *graphics = thread_graphics;
  1441. if (!graphics || !stagesurf) return GS_UNKNOWN;
  1442. return graphics->exports.gs_stagesurface_get_color_format(stagesurf);
  1443. }
  1444. bool gs_stagesurface_map(gs_stagesurf_t *stagesurf, uint8_t **data,
  1445. uint32_t *linesize)
  1446. {
  1447. graphics_t *graphics = thread_graphics;
  1448. if (!graphics || !stagesurf) return false;
  1449. return graphics->exports.gs_stagesurface_map(stagesurf, data, linesize);
  1450. }
  1451. void gs_stagesurface_unmap(gs_stagesurf_t *stagesurf)
  1452. {
  1453. graphics_t *graphics = thread_graphics;
  1454. if (!graphics || !stagesurf) return;
  1455. graphics->exports.gs_stagesurface_unmap(stagesurf);
  1456. }
  1457. void gs_zstencil_destroy(gs_zstencil_t *zstencil)
  1458. {
  1459. if (!thread_graphics || !zstencil) return;
  1460. thread_graphics->exports.gs_zstencil_destroy(zstencil);
  1461. }
  1462. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1463. {
  1464. if (!thread_graphics || !samplerstate) return;
  1465. thread_graphics->exports.gs_samplerstate_destroy(samplerstate);
  1466. }
  1467. void gs_vertexbuffer_destroy(gs_vertbuffer_t *vertbuffer)
  1468. {
  1469. graphics_t *graphics = thread_graphics;
  1470. if (!graphics || !vertbuffer) return;
  1471. graphics->exports.gs_vertexbuffer_destroy(vertbuffer);
  1472. }
  1473. void gs_vertexbuffer_flush(gs_vertbuffer_t *vertbuffer)
  1474. {
  1475. if (!thread_graphics || !vertbuffer) return;
  1476. thread_graphics->exports.gs_vertexbuffer_flush(vertbuffer);
  1477. }
  1478. struct gs_vb_data *gs_vertexbuffer_get_data(gs_vertbuffer_t *vertbuffer)
  1479. {
  1480. if (!thread_graphics || !vertbuffer) return NULL;
  1481. return thread_graphics->exports.gs_vertexbuffer_get_data(vertbuffer);
  1482. }
  1483. void gs_indexbuffer_destroy(gs_indexbuffer_t *indexbuffer)
  1484. {
  1485. graphics_t *graphics = thread_graphics;
  1486. if (!graphics || !indexbuffer) return;
  1487. graphics->exports.gs_indexbuffer_destroy(indexbuffer);
  1488. }
  1489. void gs_indexbuffer_flush(gs_indexbuffer_t *indexbuffer)
  1490. {
  1491. if (!thread_graphics || !indexbuffer) return;
  1492. thread_graphics->exports.gs_indexbuffer_flush(indexbuffer);
  1493. }
  1494. void *gs_indexbuffer_get_data(gs_indexbuffer_t *indexbuffer)
  1495. {
  1496. if (!thread_graphics || !indexbuffer) return NULL;
  1497. return thread_graphics->exports.gs_indexbuffer_get_data(indexbuffer);
  1498. }
  1499. size_t gs_indexbuffer_get_num_indices(gs_indexbuffer_t *indexbuffer)
  1500. {
  1501. if (!thread_graphics || !indexbuffer) return 0;
  1502. return thread_graphics->exports.gs_indexbuffer_get_num_indices(
  1503. indexbuffer);
  1504. }
  1505. enum gs_index_type gs_indexbuffer_get_type(gs_indexbuffer_t *indexbuffer)
  1506. {
  1507. if (!thread_graphics || !indexbuffer) return (enum gs_index_type)0;
  1508. return thread_graphics->exports.gs_indexbuffer_get_type(indexbuffer);
  1509. }
  1510. #ifdef __APPLE__
  1511. /** Platform specific functions */
  1512. gs_texture_t *gs_texture_create_from_iosurface(void *iosurf)
  1513. {
  1514. graphics_t *graphics = thread_graphics;
  1515. if (!graphics || !iosurf ||
  1516. !graphics->exports.device_texture_create_from_iosurface)
  1517. return NULL;
  1518. return graphics->exports.device_texture_create_from_iosurface(
  1519. graphics->device, iosurf);
  1520. }
  1521. bool gs_texture_rebind_iosurface(gs_texture_t *texture, void *iosurf)
  1522. {
  1523. graphics_t *graphics = thread_graphics;
  1524. if (!graphics || !iosurf ||
  1525. !graphics->exports.gs_texture_rebind_iosurface)
  1526. return false;
  1527. return graphics->exports.gs_texture_rebind_iosurface(texture, iosurf);
  1528. }
  1529. #elif _WIN32
  1530. bool gs_gdi_texture_available(void)
  1531. {
  1532. if (!thread_graphics)
  1533. return false;
  1534. return thread_graphics->exports.device_gdi_texture_available();
  1535. }
  1536. /** creates a windows GDI-lockable texture */
  1537. gs_texture_t *gs_texture_create_gdi(uint32_t width, uint32_t height)
  1538. {
  1539. graphics_t *graphics = thread_graphics;
  1540. if (!graphics) return NULL;
  1541. if (graphics->exports.device_texture_create_gdi)
  1542. return graphics->exports.device_texture_create_gdi(
  1543. graphics->device, width, height);
  1544. return NULL;
  1545. }
  1546. void *gs_texture_get_dc(gs_texture_t *gdi_tex)
  1547. {
  1548. if (!thread_graphics || !gdi_tex)
  1549. return NULL;
  1550. if (thread_graphics->exports.gs_texture_get_dc)
  1551. return thread_graphics->exports.gs_texture_get_dc(gdi_tex);
  1552. return NULL;
  1553. }
  1554. void gs_texture_release_dc(gs_texture_t *gdi_tex)
  1555. {
  1556. if (!thread_graphics || !gdi_tex)
  1557. return;
  1558. if (thread_graphics->exports.gs_texture_release_dc)
  1559. thread_graphics->exports.gs_texture_release_dc(gdi_tex);
  1560. }
  1561. #endif