gl-subsystem.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. Copyright (C) 2014 by Zachary Lund <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include <graphics/matrix3.h>
  16. #include "gl-subsystem.h"
  17. /* Goofy Windows.h macros need to be removed */
  18. #undef far
  19. #undef near
  20. /* #define SHOW_ALL_GL_MESSAGES */
  21. #ifdef _DEBUG
  22. static void APIENTRY gl_debug_proc(
  23. GLenum source, GLenum type, GLuint id, GLenum severity,
  24. GLsizei length, const GLchar *message, const GLvoid *data )
  25. {
  26. UNUSED_PARAMETER(id);
  27. UNUSED_PARAMETER(data);
  28. char *source_str, *type_str, *severity_str;
  29. /* frames can get a bit too much spam with irrelevant/insignificant opengl
  30. * debug messages */
  31. #ifndef SHOW_ALL_GL_MESSAGES
  32. if (type > GL_DEBUG_TYPE_PORTABILITY &&
  33. severity != GL_DEBUG_SEVERITY_HIGH) {
  34. return;
  35. }
  36. #endif
  37. switch(source) {
  38. case GL_DEBUG_SOURCE_API:
  39. source_str = "API"; break;
  40. case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
  41. source_str = "Window System"; break;
  42. case GL_DEBUG_SOURCE_SHADER_COMPILER:
  43. source_str = "Shader Compiler"; break;
  44. case GL_DEBUG_SOURCE_THIRD_PARTY:
  45. source_str = "Third Party"; break;
  46. case GL_DEBUG_SOURCE_APPLICATION:
  47. source_str = "Application"; break;
  48. case GL_DEBUG_SOURCE_OTHER:
  49. source_str = "Other"; break;
  50. default:
  51. source_str = "Unknown";
  52. }
  53. switch(type) {
  54. case GL_DEBUG_TYPE_ERROR:
  55. type_str = "Error"; break;
  56. case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
  57. type_str = "Deprecated Behavior"; break;
  58. case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
  59. type_str = "Undefined Behavior"; break;
  60. case GL_DEBUG_TYPE_PORTABILITY:
  61. type_str = "Portability"; break;
  62. case GL_DEBUG_TYPE_PERFORMANCE:
  63. type_str = "Performance"; break;
  64. case GL_DEBUG_TYPE_OTHER:
  65. type_str = "Other"; break;
  66. default:
  67. type_str = "Unknown";
  68. }
  69. switch(severity) {
  70. case GL_DEBUG_SEVERITY_HIGH:
  71. severity_str = "High"; break;
  72. case GL_DEBUG_SEVERITY_MEDIUM:
  73. severity_str = "Medium"; break;
  74. case GL_DEBUG_SEVERITY_LOW:
  75. severity_str = "Low"; break;
  76. case GL_DEBUG_SEVERITY_NOTIFICATION:
  77. severity_str = "Notification"; break;
  78. default:
  79. severity_str = "Unknown";
  80. }
  81. blog(LOG_DEBUG,
  82. "[%s][%s]{%s}: %.*s",
  83. source_str, type_str, severity_str,
  84. length, message
  85. );
  86. }
  87. static void gl_enable_debug()
  88. {
  89. if (GLAD_GL_VERSION_4_3) {
  90. glDebugMessageCallback(gl_debug_proc, NULL);
  91. gl_enable(GL_DEBUG_OUTPUT);
  92. } else if (GLAD_GL_ARB_debug_output) {
  93. glDebugMessageCallbackARB(gl_debug_proc, NULL);
  94. } else {
  95. blog(LOG_DEBUG, "Failed to set GL debug callback as it is "
  96. "not supported.");
  97. }
  98. }
  99. #else
  100. static void gl_enable_debug() {}
  101. #endif
  102. static bool gl_init_extensions(struct gs_device* device)
  103. {
  104. if (!GLAD_GL_VERSION_2_1) {
  105. blog(LOG_ERROR, "obs-studio requires OpenGL version 2.1 or "
  106. "higher.");
  107. return false;
  108. }
  109. gl_enable_debug();
  110. if (!GLAD_GL_VERSION_3_0 && !GLAD_GL_ARB_framebuffer_object) {
  111. blog(LOG_ERROR, "OpenGL extension ARB_framebuffer_object "
  112. "is required.");
  113. return false;
  114. }
  115. if (GLAD_GL_VERSION_3_2 || GLAD_GL_ARB_seamless_cube_map) {
  116. gl_enable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  117. }
  118. if (GLAD_GL_VERSION_4_3 || GLAD_GL_ARB_copy_image)
  119. device->copy_type = COPY_TYPE_ARB;
  120. else if (GLAD_GL_NV_copy_image)
  121. device->copy_type = COPY_TYPE_NV;
  122. else
  123. device->copy_type = COPY_TYPE_FBO_BLIT;
  124. return true;
  125. }
  126. static void clear_textures(struct gs_device *device)
  127. {
  128. GLenum i;
  129. for (i = 0; i < GS_MAX_TEXTURES; i++) {
  130. if (device->cur_textures[i]) {
  131. gl_active_texture(GL_TEXTURE0 + i);
  132. gl_bind_texture(device->cur_textures[i]->gl_target, 0);
  133. device->cur_textures[i] = NULL;
  134. }
  135. }
  136. }
  137. void convert_sampler_info(struct gs_sampler_state *sampler,
  138. const struct gs_sampler_info *info)
  139. {
  140. GLint max_anisotropy_max;
  141. convert_filter(info->filter, &sampler->min_filter,
  142. &sampler->mag_filter);
  143. sampler->address_u = convert_address_mode(info->address_u);
  144. sampler->address_v = convert_address_mode(info->address_v);
  145. sampler->address_w = convert_address_mode(info->address_w);
  146. sampler->max_anisotropy = info->max_anisotropy;
  147. max_anisotropy_max = 1;
  148. glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy_max);
  149. gl_success("glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX)");
  150. if (1 <= sampler->max_anisotropy &&
  151. sampler->max_anisotropy <= max_anisotropy_max)
  152. return;
  153. if (sampler->max_anisotropy < 1)
  154. sampler->max_anisotropy = 1;
  155. else if (sampler->max_anisotropy > max_anisotropy_max)
  156. sampler->max_anisotropy = max_anisotropy_max;
  157. blog(LOG_DEBUG, "convert_sampler_info: 1 <= max_anisotropy <= "
  158. "%d violated, selected: %d, set: %d",
  159. max_anisotropy_max,
  160. info->max_anisotropy, sampler->max_anisotropy);
  161. }
  162. const char *device_get_name(void)
  163. {
  164. return "OpenGL";
  165. }
  166. int device_get_type(void)
  167. {
  168. return GS_DEVICE_OPENGL;
  169. }
  170. const char *device_preprocessor_name(void)
  171. {
  172. return "_OPENGL";
  173. }
  174. int device_create(gs_device_t **p_device, uint32_t adapter)
  175. {
  176. struct gs_device *device = bzalloc(sizeof(struct gs_device));
  177. int errorcode = GS_ERROR_FAIL;
  178. device->plat = gl_platform_create(device, adapter);
  179. if (!device->plat)
  180. goto fail;
  181. if (!gl_init_extensions(device)) {
  182. errorcode = GS_ERROR_NOT_SUPPORTED;
  183. goto fail;
  184. }
  185. gl_enable(GL_CULL_FACE);
  186. device_leave_context(device);
  187. device->cur_swap = NULL;
  188. *p_device = device;
  189. return GS_SUCCESS;
  190. fail:
  191. blog(LOG_ERROR, "device_create (GL) failed");
  192. bfree(device);
  193. *p_device = NULL;
  194. return errorcode;
  195. }
  196. void device_destroy(gs_device_t *device)
  197. {
  198. if (device) {
  199. size_t i;
  200. for (i = 0; i < device->fbos.num; i++)
  201. fbo_info_destroy(device->fbos.array[i]);
  202. while (device->first_program)
  203. gs_program_destroy(device->first_program);
  204. da_free(device->proj_stack);
  205. da_free(device->fbos);
  206. gl_platform_destroy(device->plat);
  207. bfree(device);
  208. }
  209. }
  210. gs_swapchain_t *device_swapchain_create(gs_device_t *device,
  211. const struct gs_init_data *info)
  212. {
  213. struct gs_swap_chain *swap = bzalloc(sizeof(struct gs_swap_chain));
  214. swap->device = device;
  215. swap->info = *info;
  216. swap->wi = gl_windowinfo_create(info);
  217. if (!swap->wi) {
  218. blog(LOG_ERROR, "device_swapchain_create (GL) failed");
  219. gs_swapchain_destroy(swap);
  220. return NULL;
  221. }
  222. if (!gl_platform_init_swapchain(swap)) {
  223. blog(LOG_ERROR, "gl_platform_init_swapchain failed");
  224. gs_swapchain_destroy(swap);
  225. return NULL;
  226. }
  227. return swap;
  228. }
  229. void device_resize(gs_device_t *device, uint32_t cx, uint32_t cy)
  230. {
  231. /* GL automatically resizes the device, so it doesn't do much */
  232. if (device->cur_swap) {
  233. device->cur_swap->info.cx = cx;
  234. device->cur_swap->info.cy = cy;
  235. } else {
  236. blog(LOG_WARNING, "device_resize (GL): No active swap");
  237. }
  238. gl_update(device);
  239. }
  240. void device_get_size(const gs_device_t *device, uint32_t *cx, uint32_t *cy)
  241. {
  242. if (device->cur_swap) {
  243. *cx = device->cur_swap->info.cx;
  244. *cy = device->cur_swap->info.cy;
  245. } else {
  246. blog(LOG_WARNING, "device_get_size (GL): No active swap");
  247. *cx = 0;
  248. *cy = 0;
  249. }
  250. }
  251. uint32_t device_get_width(const gs_device_t *device)
  252. {
  253. if (device->cur_swap) {
  254. return device->cur_swap->info.cx;
  255. } else {
  256. blog(LOG_WARNING, "device_get_width (GL): No active swap");
  257. return 0;
  258. }
  259. }
  260. uint32_t device_get_height(const gs_device_t *device)
  261. {
  262. if (device->cur_swap) {
  263. return device->cur_swap->info.cy;
  264. } else {
  265. blog(LOG_WARNING, "device_get_height (GL): No active swap");
  266. return 0;
  267. }
  268. }
  269. gs_texture_t *device_voltexture_create(gs_device_t *device, uint32_t width,
  270. uint32_t height, uint32_t depth,
  271. enum gs_color_format color_format, uint32_t levels,
  272. const uint8_t **data, uint32_t flags)
  273. {
  274. /* TODO */
  275. UNUSED_PARAMETER(device);
  276. UNUSED_PARAMETER(width);
  277. UNUSED_PARAMETER(height);
  278. UNUSED_PARAMETER(depth);
  279. UNUSED_PARAMETER(color_format);
  280. UNUSED_PARAMETER(levels);
  281. UNUSED_PARAMETER(data);
  282. UNUSED_PARAMETER(flags);
  283. return NULL;
  284. }
  285. gs_samplerstate_t *device_samplerstate_create(gs_device_t *device,
  286. const struct gs_sampler_info *info)
  287. {
  288. struct gs_sampler_state *sampler;
  289. sampler = bzalloc(sizeof(struct gs_sampler_state));
  290. sampler->device = device;
  291. sampler->ref = 1;
  292. convert_sampler_info(sampler, info);
  293. return sampler;
  294. }
  295. enum gs_texture_type device_get_texture_type(const gs_texture_t *texture)
  296. {
  297. return texture->type;
  298. }
  299. static void strip_mipmap_filter(GLint *filter)
  300. {
  301. switch (*filter) {
  302. case GL_NEAREST:
  303. case GL_LINEAR:
  304. return;
  305. case GL_NEAREST_MIPMAP_NEAREST:
  306. case GL_NEAREST_MIPMAP_LINEAR:
  307. *filter = GL_NEAREST;
  308. return;
  309. case GL_LINEAR_MIPMAP_NEAREST:
  310. case GL_LINEAR_MIPMAP_LINEAR:
  311. *filter = GL_LINEAR;
  312. return;
  313. }
  314. *filter = GL_NEAREST;
  315. }
  316. static inline void apply_swizzle(struct gs_texture *tex)
  317. {
  318. if (tex->format == GS_A8) {
  319. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_R, GL_ONE);
  320. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_G, GL_ONE);
  321. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_B, GL_ONE);
  322. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_A, GL_RED);
  323. }
  324. }
  325. static bool load_texture_sampler(gs_texture_t *tex, gs_samplerstate_t *ss)
  326. {
  327. bool success = true;
  328. GLint min_filter;
  329. if (tex->cur_sampler == ss)
  330. return true;
  331. if (tex->cur_sampler)
  332. samplerstate_release(tex->cur_sampler);
  333. tex->cur_sampler = ss;
  334. if (!ss)
  335. return true;
  336. samplerstate_addref(ss);
  337. min_filter = ss->min_filter;
  338. if (gs_texture_is_rect(tex))
  339. strip_mipmap_filter(&min_filter);
  340. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MIN_FILTER,
  341. min_filter))
  342. success = false;
  343. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAG_FILTER,
  344. ss->mag_filter))
  345. success = false;
  346. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_S, ss->address_u))
  347. success = false;
  348. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_T, ss->address_v))
  349. success = false;
  350. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_R, ss->address_w))
  351. success = false;
  352. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  353. ss->max_anisotropy))
  354. success = false;
  355. apply_swizzle(tex);
  356. return success;
  357. }
  358. static inline struct gs_shader_param *get_texture_param(gs_device_t *device,
  359. int unit)
  360. {
  361. struct gs_shader *shader = device->cur_pixel_shader;
  362. size_t i;
  363. for (i = 0; i < shader->params.num; i++) {
  364. struct gs_shader_param *param = shader->params.array+i;
  365. if (param->type == GS_SHADER_PARAM_TEXTURE) {
  366. if (param->texture_id == unit)
  367. return param;
  368. }
  369. }
  370. return NULL;
  371. }
  372. void device_load_texture(gs_device_t *device, gs_texture_t *tex, int unit)
  373. {
  374. struct gs_shader_param *param;
  375. struct gs_sampler_state *sampler;
  376. struct gs_texture *cur_tex = device->cur_textures[unit];
  377. /* need a pixel shader to properly bind textures */
  378. if (!device->cur_pixel_shader)
  379. tex = NULL;
  380. if (cur_tex == tex)
  381. return;
  382. if (!gl_active_texture(GL_TEXTURE0 + unit))
  383. goto fail;
  384. /* the target for the previous text may not be the same as the
  385. * next texture, so unbind the previous texture first to be safe */
  386. if (cur_tex && (!tex || cur_tex->gl_target != tex->gl_target))
  387. gl_bind_texture(cur_tex->gl_target, 0);
  388. device->cur_textures[unit] = tex;
  389. param = get_texture_param(device, unit);
  390. if (!param)
  391. return;
  392. param->texture = tex;
  393. if (!tex)
  394. return;
  395. // texelFetch doesn't need a sampler
  396. if (param->sampler_id != (size_t)-1)
  397. sampler = device->cur_samplers[param->sampler_id];
  398. else
  399. sampler = NULL;
  400. if (!gl_bind_texture(tex->gl_target, tex->texture))
  401. goto fail;
  402. if (sampler && !load_texture_sampler(tex, sampler))
  403. goto fail;
  404. return;
  405. fail:
  406. blog(LOG_ERROR, "device_load_texture (GL) failed");
  407. }
  408. static bool load_sampler_on_textures(gs_device_t *device, gs_samplerstate_t *ss,
  409. int sampler_unit)
  410. {
  411. struct gs_shader *shader = device->cur_pixel_shader;
  412. size_t i;
  413. for (i = 0; i < shader->params.num; i++) {
  414. struct gs_shader_param *param = shader->params.array+i;
  415. if (param->type == GS_SHADER_PARAM_TEXTURE &&
  416. param->sampler_id == (uint32_t)sampler_unit &&
  417. param->texture) {
  418. if (!gl_active_texture(GL_TEXTURE0 + param->texture_id))
  419. return false;
  420. if (!load_texture_sampler(param->texture, ss))
  421. return false;
  422. }
  423. }
  424. return true;
  425. }
  426. void device_load_samplerstate(gs_device_t *device, gs_samplerstate_t *ss,
  427. int unit)
  428. {
  429. /* need a pixel shader to properly bind samplers */
  430. if (!device->cur_pixel_shader)
  431. ss = NULL;
  432. if (device->cur_samplers[unit] == ss)
  433. return;
  434. device->cur_samplers[unit] = ss;
  435. if (!ss)
  436. return;
  437. if (!load_sampler_on_textures(device, ss, unit))
  438. blog(LOG_ERROR, "device_load_samplerstate (GL) failed");
  439. return;
  440. }
  441. void device_load_vertexshader(gs_device_t *device, gs_shader_t *vertshader)
  442. {
  443. if (device->cur_vertex_shader == vertshader)
  444. return;
  445. if (vertshader && vertshader->type != GS_SHADER_VERTEX) {
  446. blog(LOG_ERROR, "Specified shader is not a vertex shader");
  447. blog(LOG_ERROR, "device_load_vertexshader (GL) failed");
  448. return;
  449. }
  450. device->cur_vertex_shader = vertshader;
  451. }
  452. static void load_default_pixelshader_samplers(struct gs_device *device,
  453. struct gs_shader *ps)
  454. {
  455. size_t i;
  456. if (!ps)
  457. return;
  458. for (i = 0; i < ps->samplers.num; i++) {
  459. struct gs_sampler_state *ss = ps->samplers.array[i];
  460. device->cur_samplers[i] = ss;
  461. }
  462. for (; i < GS_MAX_TEXTURES; i++)
  463. device->cur_samplers[i] = NULL;
  464. }
  465. void device_load_pixelshader(gs_device_t *device, gs_shader_t *pixelshader)
  466. {
  467. if (device->cur_pixel_shader == pixelshader)
  468. return;
  469. if (pixelshader && pixelshader->type != GS_SHADER_PIXEL) {
  470. blog(LOG_ERROR, "Specified shader is not a pixel shader");
  471. goto fail;
  472. }
  473. device->cur_pixel_shader = pixelshader;
  474. clear_textures(device);
  475. if (pixelshader)
  476. load_default_pixelshader_samplers(device, pixelshader);
  477. return;
  478. fail:
  479. blog(LOG_ERROR, "device_load_pixelshader (GL) failed");
  480. }
  481. void device_load_default_samplerstate(gs_device_t *device, bool b_3d, int unit)
  482. {
  483. /* TODO */
  484. UNUSED_PARAMETER(device);
  485. UNUSED_PARAMETER(b_3d);
  486. UNUSED_PARAMETER(unit);
  487. }
  488. gs_shader_t *device_get_vertex_shader(const gs_device_t *device)
  489. {
  490. return device->cur_vertex_shader;
  491. }
  492. gs_shader_t *device_get_pixel_shader(const gs_device_t *device)
  493. {
  494. return device->cur_pixel_shader;
  495. }
  496. gs_texture_t *device_get_render_target(const gs_device_t *device)
  497. {
  498. return device->cur_render_target;
  499. }
  500. gs_zstencil_t *device_get_zstencil_target(const gs_device_t *device)
  501. {
  502. return device->cur_zstencil_buffer;
  503. }
  504. static bool get_tex_dimensions(gs_texture_t *tex, uint32_t *width,
  505. uint32_t *height)
  506. {
  507. if (tex->type == GS_TEXTURE_2D) {
  508. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  509. *width = tex2d->width;
  510. *height = tex2d->height;
  511. return true;
  512. } else if (tex->type == GS_TEXTURE_CUBE) {
  513. struct gs_texture_cube *cube = (struct gs_texture_cube*)tex;
  514. *width = cube->size;
  515. *height = cube->size;
  516. return true;
  517. }
  518. blog(LOG_ERROR, "Texture must be 2D or cubemap");
  519. return false;
  520. }
  521. /*
  522. * This automatically manages FBOs so that render targets are always given
  523. * an FBO that matches their width/height/format to maximize optimization
  524. */
  525. struct fbo_info *get_fbo(struct gs_device *device,
  526. uint32_t width, uint32_t height, enum gs_color_format format)
  527. {
  528. size_t i;
  529. GLuint fbo;
  530. struct fbo_info *ptr;
  531. for (i = 0; i < device->fbos.num; i++) {
  532. ptr = device->fbos.array[i];
  533. if (ptr->width == width && ptr->height == height &&
  534. ptr->format == format)
  535. return ptr;
  536. }
  537. glGenFramebuffers(1, &fbo);
  538. if (!gl_success("glGenFramebuffers"))
  539. return NULL;
  540. ptr = bmalloc(sizeof(struct fbo_info));
  541. ptr->fbo = fbo;
  542. ptr->width = width;
  543. ptr->height = height;
  544. ptr->format = format;
  545. ptr->cur_render_target = NULL;
  546. ptr->cur_render_side = 0;
  547. ptr->cur_zstencil_buffer = NULL;
  548. da_push_back(device->fbos, &ptr);
  549. return ptr;
  550. }
  551. static inline struct fbo_info *get_fbo_by_tex(struct gs_device *device,
  552. gs_texture_t *tex)
  553. {
  554. uint32_t width, height;
  555. if (!get_tex_dimensions(tex, &width, &height))
  556. return NULL;
  557. return get_fbo(device, width, height, tex->format);
  558. }
  559. static bool set_current_fbo(gs_device_t *device, struct fbo_info *fbo)
  560. {
  561. if (device->cur_fbo != fbo) {
  562. GLuint fbo_obj = fbo ? fbo->fbo : 0;
  563. if (!gl_bind_framebuffer(GL_DRAW_FRAMEBUFFER, fbo_obj))
  564. return false;
  565. if (device->cur_fbo) {
  566. device->cur_fbo->cur_render_target = NULL;
  567. device->cur_fbo->cur_zstencil_buffer = NULL;
  568. }
  569. }
  570. device->cur_fbo = fbo;
  571. return true;
  572. }
  573. static bool attach_rendertarget(struct fbo_info *fbo, gs_texture_t *tex,
  574. int side)
  575. {
  576. if (fbo->cur_render_target == tex)
  577. return true;
  578. fbo->cur_render_target = tex;
  579. if (tex->type == GS_TEXTURE_2D) {
  580. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
  581. GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  582. tex->texture, 0);
  583. } else if (tex->type == GS_TEXTURE_CUBE) {
  584. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
  585. GL_COLOR_ATTACHMENT0,
  586. GL_TEXTURE_CUBE_MAP_POSITIVE_X + side,
  587. tex->texture, 0);
  588. } else {
  589. return false;
  590. }
  591. return gl_success("glFramebufferTexture2D");
  592. }
  593. static bool attach_zstencil(struct fbo_info *fbo, gs_zstencil_t *zs)
  594. {
  595. GLuint zsbuffer = 0;
  596. GLenum zs_attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  597. if (fbo->cur_zstencil_buffer == zs)
  598. return true;
  599. fbo->cur_zstencil_buffer = zs;
  600. if (zs) {
  601. zsbuffer = zs->buffer;
  602. zs_attachment = zs->attachment;
  603. }
  604. glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
  605. zs_attachment, GL_RENDERBUFFER, zsbuffer);
  606. if (!gl_success("glFramebufferRenderbuffer"))
  607. return false;
  608. return true;
  609. }
  610. static bool set_target(gs_device_t *device, gs_texture_t *tex, int side,
  611. gs_zstencil_t *zs)
  612. {
  613. struct fbo_info *fbo;
  614. if (device->cur_render_target == tex &&
  615. device->cur_zstencil_buffer == zs &&
  616. device->cur_render_side == side)
  617. return true;
  618. device->cur_render_target = tex;
  619. device->cur_render_side = side;
  620. device->cur_zstencil_buffer = zs;
  621. if (!tex)
  622. return set_current_fbo(device, NULL);
  623. fbo = get_fbo_by_tex(device, tex);
  624. if (!fbo)
  625. return false;
  626. set_current_fbo(device, fbo);
  627. if (!attach_rendertarget(fbo, tex, side))
  628. return false;
  629. if (!attach_zstencil(fbo, zs))
  630. return false;
  631. return true;
  632. }
  633. void device_set_render_target(gs_device_t *device, gs_texture_t *tex,
  634. gs_zstencil_t *zstencil)
  635. {
  636. if (tex) {
  637. if (tex->type != GS_TEXTURE_2D) {
  638. blog(LOG_ERROR, "Texture is not a 2D texture");
  639. goto fail;
  640. }
  641. if (!tex->is_render_target) {
  642. blog(LOG_ERROR, "Texture is not a render target");
  643. goto fail;
  644. }
  645. }
  646. if (!set_target(device, tex, 0, zstencil))
  647. goto fail;
  648. return;
  649. fail:
  650. blog(LOG_ERROR, "device_set_render_target (GL) failed");
  651. }
  652. void device_set_cube_render_target(gs_device_t *device, gs_texture_t *cubetex,
  653. int side, gs_zstencil_t *zstencil)
  654. {
  655. if (cubetex) {
  656. if (cubetex->type != GS_TEXTURE_CUBE) {
  657. blog(LOG_ERROR, "Texture is not a cube texture");
  658. goto fail;
  659. }
  660. if (!cubetex->is_render_target) {
  661. blog(LOG_ERROR, "Texture is not a render target");
  662. goto fail;
  663. }
  664. }
  665. if (!set_target(device, cubetex, side, zstencil))
  666. goto fail;
  667. return;
  668. fail:
  669. blog(LOG_ERROR, "device_set_cube_render_target (GL) failed");
  670. }
  671. void device_copy_texture_region(gs_device_t *device,
  672. gs_texture_t *dst, uint32_t dst_x, uint32_t dst_y,
  673. gs_texture_t *src, uint32_t src_x, uint32_t src_y,
  674. uint32_t src_w, uint32_t src_h)
  675. {
  676. struct gs_texture_2d *src2d = (struct gs_texture_2d*)src;
  677. struct gs_texture_2d *dst2d = (struct gs_texture_2d*)dst;
  678. if (!src) {
  679. blog(LOG_ERROR, "Source texture is NULL");
  680. goto fail;
  681. }
  682. if (!dst) {
  683. blog(LOG_ERROR, "Destination texture is NULL");
  684. goto fail;
  685. }
  686. if (dst->type != GS_TEXTURE_2D || src->type != GS_TEXTURE_2D) {
  687. blog(LOG_ERROR, "Source and destination textures must be 2D "
  688. "textures");
  689. goto fail;
  690. }
  691. if (dst->format != src->format) {
  692. blog(LOG_ERROR, "Source and destination formats do not match");
  693. goto fail;
  694. }
  695. uint32_t nw = (uint32_t)src_w ?
  696. (uint32_t)src_w : (src2d->width - src_x);
  697. uint32_t nh = (uint32_t)src_h ?
  698. (uint32_t)src_h : (src2d->height - src_y);
  699. if (dst2d->width - dst_x < nw || dst2d->height - dst_y < nh) {
  700. blog(LOG_ERROR, "Destination texture region is not big "
  701. "enough to hold the source region");
  702. goto fail;
  703. }
  704. if (!gl_copy_texture(device, dst->texture, dst->gl_target, dst_x, dst_y,
  705. src->texture, src->gl_target, src_x, src_y,
  706. nw, nh, src->format))
  707. goto fail;
  708. return;
  709. fail:
  710. blog(LOG_ERROR, "device_copy_texture (GL) failed");
  711. }
  712. void device_copy_texture(gs_device_t *device, gs_texture_t *dst,
  713. gs_texture_t *src)
  714. {
  715. device_copy_texture_region(device, dst, 0, 0, src, 0, 0, 0, 0);
  716. }
  717. void device_begin_scene(gs_device_t *device)
  718. {
  719. clear_textures(device);
  720. }
  721. static inline bool can_render(const gs_device_t *device)
  722. {
  723. if (!device->cur_vertex_shader) {
  724. blog(LOG_ERROR, "No vertex shader specified");
  725. return false;
  726. }
  727. if (!device->cur_pixel_shader) {
  728. blog(LOG_ERROR, "No pixel shader specified");
  729. return false;
  730. }
  731. if (!device->cur_vertex_buffer) {
  732. blog(LOG_ERROR, "No vertex buffer specified");
  733. return false;
  734. }
  735. if (!device->cur_swap && !device->cur_render_target) {
  736. blog(LOG_ERROR, "No active swap chain or render target");
  737. return false;
  738. }
  739. return true;
  740. }
  741. static void update_viewproj_matrix(struct gs_device *device)
  742. {
  743. struct gs_shader *vs = device->cur_vertex_shader;
  744. struct matrix4 cur_proj;
  745. gs_matrix_get(&device->cur_view);
  746. matrix4_copy(&cur_proj, &device->cur_proj);
  747. if (device->cur_fbo) {
  748. cur_proj.x.y = -cur_proj.x.y;
  749. cur_proj.y.y = -cur_proj.y.y;
  750. cur_proj.z.y = -cur_proj.z.y;
  751. cur_proj.t.y = -cur_proj.t.y;
  752. glFrontFace(GL_CW);
  753. } else {
  754. glFrontFace(GL_CCW);
  755. }
  756. gl_success("glFrontFace");
  757. matrix4_mul(&device->cur_viewproj, &device->cur_view, &cur_proj);
  758. matrix4_transpose(&device->cur_viewproj, &device->cur_viewproj);
  759. if (vs->viewproj)
  760. gs_shader_set_matrix4(vs->viewproj, &device->cur_viewproj);
  761. }
  762. static inline struct gs_program *find_program(const struct gs_device *device)
  763. {
  764. struct gs_program *program = device->first_program;
  765. while (program) {
  766. if (program->vertex_shader == device->cur_vertex_shader &&
  767. program->pixel_shader == device->cur_pixel_shader)
  768. return program;
  769. program = program->next;
  770. }
  771. return NULL;
  772. }
  773. static inline struct gs_program *get_shader_program(struct gs_device *device)
  774. {
  775. struct gs_program *program = find_program(device);
  776. if (!program)
  777. program = gs_program_create(device);
  778. return program;
  779. }
  780. void device_draw(gs_device_t *device, enum gs_draw_mode draw_mode,
  781. uint32_t start_vert, uint32_t num_verts)
  782. {
  783. struct gs_index_buffer *ib = device->cur_index_buffer;
  784. GLenum topology = convert_gs_topology(draw_mode);
  785. gs_effect_t *effect = gs_get_effect();
  786. struct gs_program *program;
  787. if (!can_render(device))
  788. goto fail;
  789. if (effect)
  790. gs_effect_update_params(effect);
  791. program = get_shader_program(device);
  792. if (!program)
  793. goto fail;
  794. load_vb_buffers(program, device->cur_vertex_buffer, ib);
  795. if (program != device->cur_program && device->cur_program) {
  796. glUseProgram(0);
  797. gl_success("glUseProgram (zero)");
  798. }
  799. if (program != device->cur_program) {
  800. device->cur_program = program;
  801. glUseProgram(program->obj);
  802. if (!gl_success("glUseProgram"))
  803. goto fail;
  804. }
  805. update_viewproj_matrix(device);
  806. program_update_params(program);
  807. if (ib) {
  808. if (num_verts == 0)
  809. num_verts = (uint32_t)device->cur_index_buffer->num;
  810. glDrawElements(topology, num_verts, ib->gl_type,
  811. (const GLvoid*)(start_vert * ib->width));
  812. if (!gl_success("glDrawElements"))
  813. goto fail;
  814. } else {
  815. if (num_verts == 0)
  816. num_verts = (uint32_t)device->cur_vertex_buffer->num;
  817. glDrawArrays(topology, start_vert, num_verts);
  818. if (!gl_success("glDrawArrays"))
  819. goto fail;
  820. }
  821. return;
  822. fail:
  823. blog(LOG_ERROR, "device_draw (GL) failed");
  824. }
  825. void device_end_scene(gs_device_t *device)
  826. {
  827. /* does nothing */
  828. UNUSED_PARAMETER(device);
  829. }
  830. void device_clear(gs_device_t *device, uint32_t clear_flags,
  831. const struct vec4 *color, float depth, uint8_t stencil)
  832. {
  833. GLbitfield gl_flags = 0;
  834. if (clear_flags & GS_CLEAR_COLOR) {
  835. glClearColor(color->x, color->y, color->z, color->w);
  836. gl_flags |= GL_COLOR_BUFFER_BIT;
  837. }
  838. if (clear_flags & GS_CLEAR_DEPTH) {
  839. glClearDepth(depth);
  840. gl_flags |= GL_DEPTH_BUFFER_BIT;
  841. }
  842. if (clear_flags & GS_CLEAR_STENCIL) {
  843. glClearStencil(stencil);
  844. gl_flags |= GL_STENCIL_BUFFER_BIT;
  845. }
  846. glClear(gl_flags);
  847. if (!gl_success("glClear"))
  848. blog(LOG_ERROR, "device_clear (GL) failed");
  849. UNUSED_PARAMETER(device);
  850. }
  851. void device_flush(gs_device_t *device)
  852. {
  853. #ifdef __APPLE__
  854. if (!device->cur_swap)
  855. glFlush();
  856. #else
  857. glFlush();
  858. #endif
  859. UNUSED_PARAMETER(device);
  860. }
  861. void device_set_cull_mode(gs_device_t *device, enum gs_cull_mode mode)
  862. {
  863. if (device->cur_cull_mode == mode)
  864. return;
  865. if (device->cur_cull_mode == GS_NEITHER)
  866. gl_enable(GL_CULL_FACE);
  867. device->cur_cull_mode = mode;
  868. if (mode == GS_BACK)
  869. gl_cull_face(GL_BACK);
  870. else if (mode == GS_FRONT)
  871. gl_cull_face(GL_FRONT);
  872. else
  873. gl_disable(GL_CULL_FACE);
  874. }
  875. enum gs_cull_mode device_get_cull_mode(const gs_device_t *device)
  876. {
  877. return device->cur_cull_mode;
  878. }
  879. void device_enable_blending(gs_device_t *device, bool enable)
  880. {
  881. if (enable)
  882. gl_enable(GL_BLEND);
  883. else
  884. gl_disable(GL_BLEND);
  885. UNUSED_PARAMETER(device);
  886. }
  887. void device_enable_depth_test(gs_device_t *device, bool enable)
  888. {
  889. if (enable)
  890. gl_enable(GL_DEPTH_TEST);
  891. else
  892. gl_disable(GL_DEPTH_TEST);
  893. UNUSED_PARAMETER(device);
  894. }
  895. void device_enable_stencil_test(gs_device_t *device, bool enable)
  896. {
  897. if (enable)
  898. gl_enable(GL_STENCIL_TEST);
  899. else
  900. gl_disable(GL_STENCIL_TEST);
  901. UNUSED_PARAMETER(device);
  902. }
  903. void device_enable_stencil_write(gs_device_t *device, bool enable)
  904. {
  905. if (enable)
  906. glStencilMask(0xFFFFFFFF);
  907. else
  908. glStencilMask(0);
  909. UNUSED_PARAMETER(device);
  910. }
  911. void device_enable_color(gs_device_t *device, bool red, bool green,
  912. bool blue, bool alpha)
  913. {
  914. glColorMask(red, green, blue, alpha);
  915. UNUSED_PARAMETER(device);
  916. }
  917. void device_blend_function(gs_device_t *device, enum gs_blend_type src,
  918. enum gs_blend_type dest)
  919. {
  920. GLenum gl_src = convert_gs_blend_type(src);
  921. GLenum gl_dst = convert_gs_blend_type(dest);
  922. glBlendFunc(gl_src, gl_dst);
  923. if (!gl_success("glBlendFunc"))
  924. blog(LOG_ERROR, "device_blend_function (GL) failed");
  925. UNUSED_PARAMETER(device);
  926. }
  927. void device_blend_function_separate(gs_device_t *device,
  928. enum gs_blend_type src_c, enum gs_blend_type dest_c,
  929. enum gs_blend_type src_a, enum gs_blend_type dest_a)
  930. {
  931. GLenum gl_src_c = convert_gs_blend_type(src_c);
  932. GLenum gl_dst_c = convert_gs_blend_type(dest_c);
  933. GLenum gl_src_a = convert_gs_blend_type(src_a);
  934. GLenum gl_dst_a = convert_gs_blend_type(dest_a);
  935. glBlendFuncSeparate(gl_src_c, gl_dst_c, gl_src_a, gl_dst_a);
  936. if (!gl_success("glBlendFuncSeparate"))
  937. blog(LOG_ERROR, "device_blend_function_separate (GL) failed");
  938. UNUSED_PARAMETER(device);
  939. }
  940. void device_depth_function(gs_device_t *device, enum gs_depth_test test)
  941. {
  942. GLenum gl_test = convert_gs_depth_test(test);
  943. glDepthFunc(gl_test);
  944. if (!gl_success("glDepthFunc"))
  945. blog(LOG_ERROR, "device_depth_function (GL) failed");
  946. UNUSED_PARAMETER(device);
  947. }
  948. void device_stencil_function(gs_device_t *device, enum gs_stencil_side side,
  949. enum gs_depth_test test)
  950. {
  951. GLenum gl_side = convert_gs_stencil_side(side);
  952. GLenum gl_test = convert_gs_depth_test(test);
  953. glStencilFuncSeparate(gl_side, gl_test, 0, 0xFFFFFFFF);
  954. if (!gl_success("glStencilFuncSeparate"))
  955. blog(LOG_ERROR, "device_stencil_function (GL) failed");
  956. UNUSED_PARAMETER(device);
  957. }
  958. void device_stencil_op(gs_device_t *device, enum gs_stencil_side side,
  959. enum gs_stencil_op_type fail, enum gs_stencil_op_type zfail,
  960. enum gs_stencil_op_type zpass)
  961. {
  962. GLenum gl_side = convert_gs_stencil_side(side);
  963. GLenum gl_fail = convert_gs_stencil_op(fail);
  964. GLenum gl_zfail = convert_gs_stencil_op(zfail);
  965. GLenum gl_zpass = convert_gs_stencil_op(zpass);
  966. glStencilOpSeparate(gl_side, gl_fail, gl_zfail, gl_zpass);
  967. if (!gl_success("glStencilOpSeparate"))
  968. blog(LOG_ERROR, "device_stencil_op (GL) failed");
  969. UNUSED_PARAMETER(device);
  970. }
  971. static inline uint32_t get_target_height(const struct gs_device *device)
  972. {
  973. if (!device->cur_render_target)
  974. return device_get_height(device);
  975. if (device->cur_render_target->type == GS_TEXTURE_2D)
  976. return gs_texture_get_height(device->cur_render_target);
  977. else /* cube map */
  978. return gs_cubetexture_get_size(device->cur_render_target);
  979. }
  980. void device_set_viewport(gs_device_t *device, int x, int y, int width,
  981. int height)
  982. {
  983. uint32_t base_height;
  984. /* GL uses bottom-up coordinates for viewports. We want top-down */
  985. if (device->cur_render_target) {
  986. base_height = get_target_height(device);
  987. } else {
  988. uint32_t dw;
  989. gl_getclientsize(device->cur_swap, &dw, &base_height);
  990. }
  991. glViewport(x, base_height - y - height, width, height);
  992. if (!gl_success("glViewport"))
  993. blog(LOG_ERROR, "device_set_viewport (GL) failed");
  994. device->cur_viewport.x = x;
  995. device->cur_viewport.y = y;
  996. device->cur_viewport.cx = width;
  997. device->cur_viewport.cy = height;
  998. }
  999. void device_get_viewport(const gs_device_t *device, struct gs_rect *rect)
  1000. {
  1001. *rect = device->cur_viewport;
  1002. }
  1003. void device_set_scissor_rect(gs_device_t *device, const struct gs_rect *rect)
  1004. {
  1005. UNUSED_PARAMETER(device);
  1006. if (rect != NULL) {
  1007. glScissor(rect->x, rect->y, rect->cx, rect->cy);
  1008. if (gl_success("glScissor") && gl_enable(GL_SCISSOR_TEST))
  1009. return;
  1010. } else if (gl_disable(GL_SCISSOR_TEST)) {
  1011. return;
  1012. }
  1013. blog(LOG_ERROR, "device_set_scissor_rect (GL) failed");
  1014. }
  1015. void device_ortho(gs_device_t *device, float left, float right,
  1016. float top, float bottom, float near, float far)
  1017. {
  1018. struct matrix4 *dst = &device->cur_proj;
  1019. float rml = right-left;
  1020. float bmt = bottom-top;
  1021. float fmn = far-near;
  1022. vec4_zero(&dst->x);
  1023. vec4_zero(&dst->y);
  1024. vec4_zero(&dst->z);
  1025. vec4_zero(&dst->t);
  1026. dst->x.x = 2.0f / rml;
  1027. dst->t.x = (left+right) / -rml;
  1028. dst->y.y = 2.0f / -bmt;
  1029. dst->t.y = (bottom+top) / bmt;
  1030. dst->z.z = -2.0f / fmn;
  1031. dst->t.z = (far+near) / -fmn;
  1032. dst->t.w = 1.0f;
  1033. }
  1034. void device_frustum(gs_device_t *device, float left, float right,
  1035. float top, float bottom, float near, float far)
  1036. {
  1037. struct matrix4 *dst = &device->cur_proj;
  1038. float rml = right-left;
  1039. float tmb = top-bottom;
  1040. float nmf = near-far;
  1041. float nearx2 = 2.0f*near;
  1042. vec4_zero(&dst->x);
  1043. vec4_zero(&dst->y);
  1044. vec4_zero(&dst->z);
  1045. vec4_zero(&dst->t);
  1046. dst->x.x = nearx2 / rml;
  1047. dst->z.x = (left+right) / rml;
  1048. dst->y.y = nearx2 / tmb;
  1049. dst->z.y = (bottom+top) / tmb;
  1050. dst->z.z = (far+near) / nmf;
  1051. dst->t.z = 2.0f * (near*far) / nmf;
  1052. dst->z.w = -1.0f;
  1053. }
  1054. void device_projection_push(gs_device_t *device)
  1055. {
  1056. da_push_back(device->proj_stack, &device->cur_proj);
  1057. }
  1058. void device_projection_pop(gs_device_t *device)
  1059. {
  1060. struct matrix4 *end;
  1061. if (!device->proj_stack.num)
  1062. return;
  1063. end = da_end(device->proj_stack);
  1064. device->cur_proj = *end;
  1065. da_pop_back(device->proj_stack);
  1066. }
  1067. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1068. {
  1069. if (!swapchain)
  1070. return;
  1071. if (swapchain->device->cur_swap == swapchain)
  1072. device_load_swapchain(swapchain->device, NULL);
  1073. gl_platform_cleanup_swapchain(swapchain);
  1074. gl_windowinfo_destroy(swapchain->wi);
  1075. bfree(swapchain);
  1076. }
  1077. void gs_voltexture_destroy(gs_texture_t *voltex)
  1078. {
  1079. /* TODO */
  1080. UNUSED_PARAMETER(voltex);
  1081. }
  1082. uint32_t gs_voltexture_get_width(const gs_texture_t *voltex)
  1083. {
  1084. /* TODO */
  1085. UNUSED_PARAMETER(voltex);
  1086. return 0;
  1087. }
  1088. uint32_t gs_voltexture_get_height(const gs_texture_t *voltex)
  1089. {
  1090. /* TODO */
  1091. UNUSED_PARAMETER(voltex);
  1092. return 0;
  1093. }
  1094. uint32_t gs_voltexture_get_depth(const gs_texture_t *voltex)
  1095. {
  1096. /* TODO */
  1097. UNUSED_PARAMETER(voltex);
  1098. return 0;
  1099. }
  1100. enum gs_color_format gs_voltexture_get_color_format(const gs_texture_t *voltex)
  1101. {
  1102. /* TODO */
  1103. UNUSED_PARAMETER(voltex);
  1104. return GS_UNKNOWN;
  1105. }
  1106. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1107. {
  1108. if (!samplerstate)
  1109. return;
  1110. if (samplerstate->device)
  1111. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1112. if (samplerstate->device->cur_samplers[i] ==
  1113. samplerstate)
  1114. samplerstate->device->cur_samplers[i] = NULL;
  1115. samplerstate_release(samplerstate);
  1116. }