graphics.c 47 KB

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