gl-subsystem.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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. blog(LOG_INFO, "---------------------------------");
  179. blog(LOG_INFO, "Initializing OpenGL...");
  180. device->plat = gl_platform_create(device, adapter);
  181. if (!device->plat)
  182. goto fail;
  183. const char *glVendor = (const char *)glGetString(GL_VENDOR);
  184. const char *glRenderer = (const char *)glGetString(GL_RENDERER);
  185. blog(LOG_INFO, "Loading up OpenGL on adapter %s %s", glVendor,
  186. glRenderer);
  187. if (!gl_init_extensions(device)) {
  188. errorcode = GS_ERROR_NOT_SUPPORTED;
  189. goto fail;
  190. }
  191. const char *glVersion = (const char *)glGetString(GL_VERSION);
  192. const char *glShadingLanguage = (const char *)glGetString(
  193. GL_SHADING_LANGUAGE_VERSION);
  194. blog(LOG_INFO, "OpenGL loaded successfully, version %s, shading "
  195. "language %s", glVersion, glShadingLanguage);
  196. gl_enable(GL_CULL_FACE);
  197. device_leave_context(device);
  198. device->cur_swap = NULL;
  199. #ifdef _WIN32
  200. blog(LOG_INFO, "Warning: The OpenGL renderer is currently in use. "
  201. "On windows, the OpenGL renderer can decrease "
  202. "capture performance due to the lack of specific "
  203. "features used to maximize capture performance. "
  204. "The Direct3D 11 renderer is recommended instead.");
  205. #endif
  206. *p_device = device;
  207. return GS_SUCCESS;
  208. fail:
  209. blog(LOG_ERROR, "device_create (GL) failed");
  210. bfree(device);
  211. *p_device = NULL;
  212. return errorcode;
  213. }
  214. void device_destroy(gs_device_t *device)
  215. {
  216. if (device) {
  217. while (device->first_program)
  218. gs_program_destroy(device->first_program);
  219. da_free(device->proj_stack);
  220. gl_platform_destroy(device->plat);
  221. bfree(device);
  222. }
  223. }
  224. gs_swapchain_t *device_swapchain_create(gs_device_t *device,
  225. const struct gs_init_data *info)
  226. {
  227. struct gs_swap_chain *swap = bzalloc(sizeof(struct gs_swap_chain));
  228. swap->device = device;
  229. swap->info = *info;
  230. swap->wi = gl_windowinfo_create(info);
  231. if (!swap->wi) {
  232. blog(LOG_ERROR, "device_swapchain_create (GL) failed");
  233. gs_swapchain_destroy(swap);
  234. return NULL;
  235. }
  236. if (!gl_platform_init_swapchain(swap)) {
  237. blog(LOG_ERROR, "gl_platform_init_swapchain failed");
  238. gs_swapchain_destroy(swap);
  239. return NULL;
  240. }
  241. return swap;
  242. }
  243. void device_resize(gs_device_t *device, uint32_t cx, uint32_t cy)
  244. {
  245. /* GL automatically resizes the device, so it doesn't do much */
  246. if (device->cur_swap) {
  247. device->cur_swap->info.cx = cx;
  248. device->cur_swap->info.cy = cy;
  249. } else {
  250. blog(LOG_WARNING, "device_resize (GL): No active swap");
  251. }
  252. gl_update(device);
  253. }
  254. void device_get_size(const gs_device_t *device, uint32_t *cx, uint32_t *cy)
  255. {
  256. if (device->cur_swap) {
  257. *cx = device->cur_swap->info.cx;
  258. *cy = device->cur_swap->info.cy;
  259. } else {
  260. blog(LOG_WARNING, "device_get_size (GL): No active swap");
  261. *cx = 0;
  262. *cy = 0;
  263. }
  264. }
  265. uint32_t device_get_width(const gs_device_t *device)
  266. {
  267. if (device->cur_swap) {
  268. return device->cur_swap->info.cx;
  269. } else {
  270. blog(LOG_WARNING, "device_get_width (GL): No active swap");
  271. return 0;
  272. }
  273. }
  274. uint32_t device_get_height(const gs_device_t *device)
  275. {
  276. if (device->cur_swap) {
  277. return device->cur_swap->info.cy;
  278. } else {
  279. blog(LOG_WARNING, "device_get_height (GL): No active swap");
  280. return 0;
  281. }
  282. }
  283. gs_texture_t *device_voltexture_create(gs_device_t *device, uint32_t width,
  284. uint32_t height, uint32_t depth,
  285. enum gs_color_format color_format, uint32_t levels,
  286. const uint8_t **data, uint32_t flags)
  287. {
  288. /* TODO */
  289. UNUSED_PARAMETER(device);
  290. UNUSED_PARAMETER(width);
  291. UNUSED_PARAMETER(height);
  292. UNUSED_PARAMETER(depth);
  293. UNUSED_PARAMETER(color_format);
  294. UNUSED_PARAMETER(levels);
  295. UNUSED_PARAMETER(data);
  296. UNUSED_PARAMETER(flags);
  297. return NULL;
  298. }
  299. gs_samplerstate_t *device_samplerstate_create(gs_device_t *device,
  300. const struct gs_sampler_info *info)
  301. {
  302. struct gs_sampler_state *sampler;
  303. sampler = bzalloc(sizeof(struct gs_sampler_state));
  304. sampler->device = device;
  305. sampler->ref = 1;
  306. convert_sampler_info(sampler, info);
  307. return sampler;
  308. }
  309. enum gs_texture_type device_get_texture_type(const gs_texture_t *texture)
  310. {
  311. return texture->type;
  312. }
  313. static void strip_mipmap_filter(GLint *filter)
  314. {
  315. switch (*filter) {
  316. case GL_NEAREST:
  317. case GL_LINEAR:
  318. return;
  319. case GL_NEAREST_MIPMAP_NEAREST:
  320. case GL_NEAREST_MIPMAP_LINEAR:
  321. *filter = GL_NEAREST;
  322. return;
  323. case GL_LINEAR_MIPMAP_NEAREST:
  324. case GL_LINEAR_MIPMAP_LINEAR:
  325. *filter = GL_LINEAR;
  326. return;
  327. }
  328. *filter = GL_NEAREST;
  329. }
  330. static inline void apply_swizzle(struct gs_texture *tex)
  331. {
  332. if (tex->format == GS_A8) {
  333. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_R, GL_ONE);
  334. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_G, GL_ONE);
  335. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_B, GL_ONE);
  336. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_A, GL_RED);
  337. }
  338. }
  339. static bool load_texture_sampler(gs_texture_t *tex, gs_samplerstate_t *ss)
  340. {
  341. bool success = true;
  342. GLint min_filter;
  343. if (tex->cur_sampler == ss)
  344. return true;
  345. if (tex->cur_sampler)
  346. samplerstate_release(tex->cur_sampler);
  347. tex->cur_sampler = ss;
  348. if (!ss)
  349. return true;
  350. samplerstate_addref(ss);
  351. min_filter = ss->min_filter;
  352. if (gs_texture_is_rect(tex))
  353. strip_mipmap_filter(&min_filter);
  354. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MIN_FILTER,
  355. min_filter))
  356. success = false;
  357. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAG_FILTER,
  358. ss->mag_filter))
  359. success = false;
  360. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_S, ss->address_u))
  361. success = false;
  362. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_T, ss->address_v))
  363. success = false;
  364. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_R, ss->address_w))
  365. success = false;
  366. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  367. ss->max_anisotropy))
  368. success = false;
  369. apply_swizzle(tex);
  370. return success;
  371. }
  372. static inline struct gs_shader_param *get_texture_param(gs_device_t *device,
  373. int unit)
  374. {
  375. struct gs_shader *shader = device->cur_pixel_shader;
  376. size_t i;
  377. for (i = 0; i < shader->params.num; i++) {
  378. struct gs_shader_param *param = shader->params.array+i;
  379. if (param->type == GS_SHADER_PARAM_TEXTURE) {
  380. if (param->texture_id == unit)
  381. return param;
  382. }
  383. }
  384. return NULL;
  385. }
  386. void device_load_texture(gs_device_t *device, gs_texture_t *tex, int unit)
  387. {
  388. struct gs_shader_param *param;
  389. struct gs_sampler_state *sampler;
  390. struct gs_texture *cur_tex = device->cur_textures[unit];
  391. /* need a pixel shader to properly bind textures */
  392. if (!device->cur_pixel_shader)
  393. goto fail;
  394. if (cur_tex == tex)
  395. return;
  396. if (!gl_active_texture(GL_TEXTURE0 + unit))
  397. goto fail;
  398. /* the target for the previous text may not be the same as the
  399. * next texture, so unbind the previous texture first to be safe */
  400. if (cur_tex && (!tex || cur_tex->gl_target != tex->gl_target))
  401. gl_bind_texture(cur_tex->gl_target, 0);
  402. device->cur_textures[unit] = tex;
  403. param = get_texture_param(device, unit);
  404. if (!param)
  405. return;
  406. param->texture = tex;
  407. if (!tex)
  408. return;
  409. // texelFetch doesn't need a sampler
  410. if (param->sampler_id != (size_t)-1)
  411. sampler = device->cur_samplers[param->sampler_id];
  412. else
  413. sampler = NULL;
  414. if (!gl_bind_texture(tex->gl_target, tex->texture))
  415. goto fail;
  416. if (sampler && !load_texture_sampler(tex, sampler))
  417. goto fail;
  418. return;
  419. fail:
  420. blog(LOG_ERROR, "device_load_texture (GL) failed");
  421. }
  422. static bool load_sampler_on_textures(gs_device_t *device, gs_samplerstate_t *ss,
  423. int sampler_unit)
  424. {
  425. struct gs_shader *shader = device->cur_pixel_shader;
  426. size_t i;
  427. for (i = 0; i < shader->params.num; i++) {
  428. struct gs_shader_param *param = shader->params.array+i;
  429. if (param->type == GS_SHADER_PARAM_TEXTURE &&
  430. param->sampler_id == (uint32_t)sampler_unit &&
  431. param->texture) {
  432. if (!gl_active_texture(GL_TEXTURE0 + param->texture_id))
  433. return false;
  434. if (!load_texture_sampler(param->texture, ss))
  435. return false;
  436. }
  437. }
  438. return true;
  439. }
  440. void device_load_samplerstate(gs_device_t *device, gs_samplerstate_t *ss,
  441. int unit)
  442. {
  443. /* need a pixel shader to properly bind samplers */
  444. if (!device->cur_pixel_shader)
  445. ss = NULL;
  446. if (device->cur_samplers[unit] == ss)
  447. return;
  448. device->cur_samplers[unit] = ss;
  449. if (!ss)
  450. return;
  451. if (!load_sampler_on_textures(device, ss, unit))
  452. blog(LOG_ERROR, "device_load_samplerstate (GL) failed");
  453. return;
  454. }
  455. void device_load_vertexshader(gs_device_t *device, gs_shader_t *vertshader)
  456. {
  457. if (device->cur_vertex_shader == vertshader)
  458. return;
  459. if (vertshader && vertshader->type != GS_SHADER_VERTEX) {
  460. blog(LOG_ERROR, "Specified shader is not a vertex shader");
  461. blog(LOG_ERROR, "device_load_vertexshader (GL) failed");
  462. return;
  463. }
  464. device->cur_vertex_shader = vertshader;
  465. }
  466. static void load_default_pixelshader_samplers(struct gs_device *device,
  467. struct gs_shader *ps)
  468. {
  469. size_t i;
  470. if (!ps)
  471. return;
  472. for (i = 0; i < ps->samplers.num; i++) {
  473. struct gs_sampler_state *ss = ps->samplers.array[i];
  474. device->cur_samplers[i] = ss;
  475. }
  476. for (; i < GS_MAX_TEXTURES; i++)
  477. device->cur_samplers[i] = NULL;
  478. }
  479. void device_load_pixelshader(gs_device_t *device, gs_shader_t *pixelshader)
  480. {
  481. if (device->cur_pixel_shader == pixelshader)
  482. return;
  483. if (pixelshader && pixelshader->type != GS_SHADER_PIXEL) {
  484. blog(LOG_ERROR, "Specified shader is not a pixel shader");
  485. goto fail;
  486. }
  487. device->cur_pixel_shader = pixelshader;
  488. clear_textures(device);
  489. if (pixelshader)
  490. load_default_pixelshader_samplers(device, pixelshader);
  491. return;
  492. fail:
  493. blog(LOG_ERROR, "device_load_pixelshader (GL) failed");
  494. }
  495. void device_load_default_samplerstate(gs_device_t *device, bool b_3d, int unit)
  496. {
  497. /* TODO */
  498. UNUSED_PARAMETER(device);
  499. UNUSED_PARAMETER(b_3d);
  500. UNUSED_PARAMETER(unit);
  501. }
  502. gs_shader_t *device_get_vertex_shader(const gs_device_t *device)
  503. {
  504. return device->cur_vertex_shader;
  505. }
  506. gs_shader_t *device_get_pixel_shader(const gs_device_t *device)
  507. {
  508. return device->cur_pixel_shader;
  509. }
  510. gs_texture_t *device_get_render_target(const gs_device_t *device)
  511. {
  512. return device->cur_render_target;
  513. }
  514. gs_zstencil_t *device_get_zstencil_target(const gs_device_t *device)
  515. {
  516. return device->cur_zstencil_buffer;
  517. }
  518. static bool get_tex_dimensions(gs_texture_t *tex, uint32_t *width,
  519. uint32_t *height)
  520. {
  521. if (tex->type == GS_TEXTURE_2D) {
  522. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  523. *width = tex2d->width;
  524. *height = tex2d->height;
  525. return true;
  526. } else if (tex->type == GS_TEXTURE_CUBE) {
  527. struct gs_texture_cube *cube = (struct gs_texture_cube*)tex;
  528. *width = cube->size;
  529. *height = cube->size;
  530. return true;
  531. }
  532. blog(LOG_ERROR, "Texture must be 2D or cubemap");
  533. return false;
  534. }
  535. /*
  536. * This automatically manages FBOs so that render targets are always given
  537. * an FBO that matches their width/height/format to maximize optimization
  538. */
  539. struct fbo_info *get_fbo(gs_texture_t *tex, uint32_t width, uint32_t height)
  540. {
  541. if (tex->fbo && tex->fbo->width == width &&
  542. tex->fbo->height == height &&
  543. tex->fbo->format == tex->format)
  544. return tex->fbo;
  545. GLuint fbo;
  546. glGenFramebuffers(1, &fbo);
  547. if (!gl_success("glGenFramebuffers"))
  548. return NULL;
  549. tex->fbo = bmalloc(sizeof(struct fbo_info));
  550. tex->fbo->fbo = fbo;
  551. tex->fbo->width = width;
  552. tex->fbo->height = height;
  553. tex->fbo->format = tex->format;
  554. tex->fbo->cur_render_target = NULL;
  555. tex->fbo->cur_render_side = 0;
  556. tex->fbo->cur_zstencil_buffer = NULL;
  557. return tex->fbo;
  558. }
  559. static inline struct fbo_info *get_fbo_by_tex(gs_texture_t *tex)
  560. {
  561. uint32_t width, height;
  562. if (!get_tex_dimensions(tex, &width, &height))
  563. return NULL;
  564. return get_fbo(tex, width, height);
  565. }
  566. static bool set_current_fbo(gs_device_t *device, struct fbo_info *fbo)
  567. {
  568. if (device->cur_fbo != fbo) {
  569. GLuint fbo_obj = fbo ? fbo->fbo : 0;
  570. if (!gl_bind_framebuffer(GL_DRAW_FRAMEBUFFER, fbo_obj))
  571. return false;
  572. if (device->cur_fbo) {
  573. device->cur_fbo->cur_render_target = NULL;
  574. device->cur_fbo->cur_zstencil_buffer = NULL;
  575. }
  576. }
  577. device->cur_fbo = fbo;
  578. return true;
  579. }
  580. static bool attach_rendertarget(struct fbo_info *fbo, gs_texture_t *tex,
  581. int side)
  582. {
  583. if (fbo->cur_render_target == tex)
  584. return true;
  585. fbo->cur_render_target = tex;
  586. if (tex->type == GS_TEXTURE_2D) {
  587. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
  588. GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  589. tex->texture, 0);
  590. } else if (tex->type == GS_TEXTURE_CUBE) {
  591. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
  592. GL_COLOR_ATTACHMENT0,
  593. GL_TEXTURE_CUBE_MAP_POSITIVE_X + side,
  594. tex->texture, 0);
  595. } else {
  596. return false;
  597. }
  598. return gl_success("glFramebufferTexture2D");
  599. }
  600. static bool attach_zstencil(struct fbo_info *fbo, gs_zstencil_t *zs)
  601. {
  602. GLuint zsbuffer = 0;
  603. GLenum zs_attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  604. if (fbo->cur_zstencil_buffer == zs)
  605. return true;
  606. fbo->cur_zstencil_buffer = zs;
  607. if (zs) {
  608. zsbuffer = zs->buffer;
  609. zs_attachment = zs->attachment;
  610. }
  611. glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
  612. zs_attachment, GL_RENDERBUFFER, zsbuffer);
  613. if (!gl_success("glFramebufferRenderbuffer"))
  614. return false;
  615. return true;
  616. }
  617. static bool set_target(gs_device_t *device, gs_texture_t *tex, int side,
  618. gs_zstencil_t *zs)
  619. {
  620. struct fbo_info *fbo;
  621. if (device->cur_render_target == tex &&
  622. device->cur_zstencil_buffer == zs &&
  623. device->cur_render_side == side)
  624. return true;
  625. device->cur_render_target = tex;
  626. device->cur_render_side = side;
  627. device->cur_zstencil_buffer = zs;
  628. if (!tex)
  629. return set_current_fbo(device, NULL);
  630. fbo = get_fbo_by_tex(tex);
  631. if (!fbo)
  632. return false;
  633. set_current_fbo(device, fbo);
  634. if (!attach_rendertarget(fbo, tex, side))
  635. return false;
  636. if (!attach_zstencil(fbo, zs))
  637. return false;
  638. return true;
  639. }
  640. void device_set_render_target(gs_device_t *device, gs_texture_t *tex,
  641. gs_zstencil_t *zstencil)
  642. {
  643. if (tex) {
  644. if (tex->type != GS_TEXTURE_2D) {
  645. blog(LOG_ERROR, "Texture is not a 2D texture");
  646. goto fail;
  647. }
  648. if (!tex->is_render_target) {
  649. blog(LOG_ERROR, "Texture is not a render target");
  650. goto fail;
  651. }
  652. }
  653. if (!set_target(device, tex, 0, zstencil))
  654. goto fail;
  655. return;
  656. fail:
  657. blog(LOG_ERROR, "device_set_render_target (GL) failed");
  658. }
  659. void device_set_cube_render_target(gs_device_t *device, gs_texture_t *cubetex,
  660. int side, gs_zstencil_t *zstencil)
  661. {
  662. if (cubetex) {
  663. if (cubetex->type != GS_TEXTURE_CUBE) {
  664. blog(LOG_ERROR, "Texture is not a cube texture");
  665. goto fail;
  666. }
  667. if (!cubetex->is_render_target) {
  668. blog(LOG_ERROR, "Texture is not a render target");
  669. goto fail;
  670. }
  671. }
  672. if (!set_target(device, cubetex, side, zstencil))
  673. goto fail;
  674. return;
  675. fail:
  676. blog(LOG_ERROR, "device_set_cube_render_target (GL) failed");
  677. }
  678. void device_copy_texture_region(gs_device_t *device,
  679. gs_texture_t *dst, uint32_t dst_x, uint32_t dst_y,
  680. gs_texture_t *src, uint32_t src_x, uint32_t src_y,
  681. uint32_t src_w, uint32_t src_h)
  682. {
  683. struct gs_texture_2d *src2d = (struct gs_texture_2d*)src;
  684. struct gs_texture_2d *dst2d = (struct gs_texture_2d*)dst;
  685. if (!src) {
  686. blog(LOG_ERROR, "Source texture is NULL");
  687. goto fail;
  688. }
  689. if (!dst) {
  690. blog(LOG_ERROR, "Destination texture is NULL");
  691. goto fail;
  692. }
  693. if (dst->type != GS_TEXTURE_2D || src->type != GS_TEXTURE_2D) {
  694. blog(LOG_ERROR, "Source and destination textures must be 2D "
  695. "textures");
  696. goto fail;
  697. }
  698. if (dst->format != src->format) {
  699. blog(LOG_ERROR, "Source and destination formats do not match");
  700. goto fail;
  701. }
  702. uint32_t nw = (uint32_t)src_w ?
  703. (uint32_t)src_w : (src2d->width - src_x);
  704. uint32_t nh = (uint32_t)src_h ?
  705. (uint32_t)src_h : (src2d->height - src_y);
  706. if (dst2d->width - dst_x < nw || dst2d->height - dst_y < nh) {
  707. blog(LOG_ERROR, "Destination texture region is not big "
  708. "enough to hold the source region");
  709. goto fail;
  710. }
  711. if (!gl_copy_texture(device, dst, dst_x, dst_y, src, src_x, src_y, nw,
  712. nh))
  713. goto fail;
  714. return;
  715. fail:
  716. blog(LOG_ERROR, "device_copy_texture (GL) failed");
  717. }
  718. void device_copy_texture(gs_device_t *device, gs_texture_t *dst,
  719. gs_texture_t *src)
  720. {
  721. device_copy_texture_region(device, dst, 0, 0, src, 0, 0, 0, 0);
  722. }
  723. void device_begin_scene(gs_device_t *device)
  724. {
  725. clear_textures(device);
  726. }
  727. static inline bool can_render(const gs_device_t *device, uint32_t num_verts)
  728. {
  729. if (!device->cur_vertex_shader) {
  730. blog(LOG_ERROR, "No vertex shader specified");
  731. return false;
  732. }
  733. if (!device->cur_pixel_shader) {
  734. blog(LOG_ERROR, "No pixel shader specified");
  735. return false;
  736. }
  737. if (!device->cur_vertex_buffer && (num_verts == 0)) {
  738. blog(LOG_ERROR, "No vertex buffer specified");
  739. return false;
  740. }
  741. if (!device->cur_swap && !device->cur_render_target) {
  742. blog(LOG_ERROR, "No active swap chain or render target");
  743. return false;
  744. }
  745. return true;
  746. }
  747. static void update_viewproj_matrix(struct gs_device *device)
  748. {
  749. struct gs_shader *vs = device->cur_vertex_shader;
  750. struct matrix4 cur_proj;
  751. gs_matrix_get(&device->cur_view);
  752. matrix4_copy(&cur_proj, &device->cur_proj);
  753. if (device->cur_fbo) {
  754. cur_proj.x.y = -cur_proj.x.y;
  755. cur_proj.y.y = -cur_proj.y.y;
  756. cur_proj.z.y = -cur_proj.z.y;
  757. cur_proj.t.y = -cur_proj.t.y;
  758. glFrontFace(GL_CW);
  759. } else {
  760. glFrontFace(GL_CCW);
  761. }
  762. gl_success("glFrontFace");
  763. matrix4_mul(&device->cur_viewproj, &device->cur_view, &cur_proj);
  764. matrix4_transpose(&device->cur_viewproj, &device->cur_viewproj);
  765. if (vs->viewproj)
  766. gs_shader_set_matrix4(vs->viewproj, &device->cur_viewproj);
  767. }
  768. static inline struct gs_program *find_program(const struct gs_device *device)
  769. {
  770. struct gs_program *program = device->first_program;
  771. while (program) {
  772. if (program->vertex_shader == device->cur_vertex_shader &&
  773. program->pixel_shader == device->cur_pixel_shader)
  774. return program;
  775. program = program->next;
  776. }
  777. return NULL;
  778. }
  779. static inline struct gs_program *get_shader_program(struct gs_device *device)
  780. {
  781. struct gs_program *program = find_program(device);
  782. if (!program)
  783. program = gs_program_create(device);
  784. return program;
  785. }
  786. void device_draw(gs_device_t *device, enum gs_draw_mode draw_mode,
  787. uint32_t start_vert, uint32_t num_verts)
  788. {
  789. struct gs_index_buffer *ib = device->cur_index_buffer;
  790. GLenum topology = convert_gs_topology(draw_mode);
  791. gs_effect_t *effect = gs_get_effect();
  792. struct gs_program *program;
  793. if (!can_render(device, num_verts))
  794. goto fail;
  795. if (effect)
  796. gs_effect_update_params(effect);
  797. program = get_shader_program(device);
  798. if (!program)
  799. goto fail;
  800. load_vb_buffers(program, device->cur_vertex_buffer, ib);
  801. if (program != device->cur_program && device->cur_program) {
  802. glUseProgram(0);
  803. gl_success("glUseProgram (zero)");
  804. }
  805. if (program != device->cur_program) {
  806. device->cur_program = program;
  807. glUseProgram(program->obj);
  808. if (!gl_success("glUseProgram"))
  809. goto fail;
  810. }
  811. update_viewproj_matrix(device);
  812. program_update_params(program);
  813. if (ib) {
  814. if (num_verts == 0)
  815. num_verts = (uint32_t)device->cur_index_buffer->num;
  816. glDrawElements(topology, num_verts, ib->gl_type,
  817. (const GLvoid*)(start_vert * ib->width));
  818. if (!gl_success("glDrawElements"))
  819. goto fail;
  820. } else {
  821. if (num_verts == 0)
  822. num_verts = (uint32_t)device->cur_vertex_buffer->num;
  823. glDrawArrays(topology, start_vert, num_verts);
  824. if (!gl_success("glDrawArrays"))
  825. goto fail;
  826. }
  827. return;
  828. fail:
  829. blog(LOG_ERROR, "device_draw (GL) failed");
  830. }
  831. void device_end_scene(gs_device_t *device)
  832. {
  833. /* does nothing */
  834. UNUSED_PARAMETER(device);
  835. }
  836. void device_clear(gs_device_t *device, uint32_t clear_flags,
  837. const struct vec4 *color, float depth, uint8_t stencil)
  838. {
  839. GLbitfield gl_flags = 0;
  840. if (clear_flags & GS_CLEAR_COLOR) {
  841. glClearColor(color->x, color->y, color->z, color->w);
  842. gl_flags |= GL_COLOR_BUFFER_BIT;
  843. }
  844. if (clear_flags & GS_CLEAR_DEPTH) {
  845. glClearDepth(depth);
  846. gl_flags |= GL_DEPTH_BUFFER_BIT;
  847. }
  848. if (clear_flags & GS_CLEAR_STENCIL) {
  849. glClearStencil(stencil);
  850. gl_flags |= GL_STENCIL_BUFFER_BIT;
  851. }
  852. glClear(gl_flags);
  853. if (!gl_success("glClear"))
  854. blog(LOG_ERROR, "device_clear (GL) failed");
  855. UNUSED_PARAMETER(device);
  856. }
  857. void device_flush(gs_device_t *device)
  858. {
  859. #ifdef __APPLE__
  860. if (!device->cur_swap)
  861. glFlush();
  862. #else
  863. glFlush();
  864. #endif
  865. UNUSED_PARAMETER(device);
  866. }
  867. void device_set_cull_mode(gs_device_t *device, enum gs_cull_mode mode)
  868. {
  869. if (device->cur_cull_mode == mode)
  870. return;
  871. if (device->cur_cull_mode == GS_NEITHER)
  872. gl_enable(GL_CULL_FACE);
  873. device->cur_cull_mode = mode;
  874. if (mode == GS_BACK)
  875. gl_cull_face(GL_BACK);
  876. else if (mode == GS_FRONT)
  877. gl_cull_face(GL_FRONT);
  878. else
  879. gl_disable(GL_CULL_FACE);
  880. }
  881. enum gs_cull_mode device_get_cull_mode(const gs_device_t *device)
  882. {
  883. return device->cur_cull_mode;
  884. }
  885. void device_enable_blending(gs_device_t *device, bool enable)
  886. {
  887. if (enable)
  888. gl_enable(GL_BLEND);
  889. else
  890. gl_disable(GL_BLEND);
  891. UNUSED_PARAMETER(device);
  892. }
  893. void device_enable_depth_test(gs_device_t *device, bool enable)
  894. {
  895. if (enable)
  896. gl_enable(GL_DEPTH_TEST);
  897. else
  898. gl_disable(GL_DEPTH_TEST);
  899. UNUSED_PARAMETER(device);
  900. }
  901. void device_enable_stencil_test(gs_device_t *device, bool enable)
  902. {
  903. if (enable)
  904. gl_enable(GL_STENCIL_TEST);
  905. else
  906. gl_disable(GL_STENCIL_TEST);
  907. UNUSED_PARAMETER(device);
  908. }
  909. void device_enable_stencil_write(gs_device_t *device, bool enable)
  910. {
  911. if (enable)
  912. glStencilMask(0xFFFFFFFF);
  913. else
  914. glStencilMask(0);
  915. UNUSED_PARAMETER(device);
  916. }
  917. void device_enable_color(gs_device_t *device, bool red, bool green,
  918. bool blue, bool alpha)
  919. {
  920. glColorMask(red, green, blue, alpha);
  921. UNUSED_PARAMETER(device);
  922. }
  923. void device_blend_function(gs_device_t *device, enum gs_blend_type src,
  924. enum gs_blend_type dest)
  925. {
  926. GLenum gl_src = convert_gs_blend_type(src);
  927. GLenum gl_dst = convert_gs_blend_type(dest);
  928. glBlendFunc(gl_src, gl_dst);
  929. if (!gl_success("glBlendFunc"))
  930. blog(LOG_ERROR, "device_blend_function (GL) failed");
  931. UNUSED_PARAMETER(device);
  932. }
  933. void device_blend_function_separate(gs_device_t *device,
  934. enum gs_blend_type src_c, enum gs_blend_type dest_c,
  935. enum gs_blend_type src_a, enum gs_blend_type dest_a)
  936. {
  937. GLenum gl_src_c = convert_gs_blend_type(src_c);
  938. GLenum gl_dst_c = convert_gs_blend_type(dest_c);
  939. GLenum gl_src_a = convert_gs_blend_type(src_a);
  940. GLenum gl_dst_a = convert_gs_blend_type(dest_a);
  941. glBlendFuncSeparate(gl_src_c, gl_dst_c, gl_src_a, gl_dst_a);
  942. if (!gl_success("glBlendFuncSeparate"))
  943. blog(LOG_ERROR, "device_blend_function_separate (GL) failed");
  944. UNUSED_PARAMETER(device);
  945. }
  946. void device_depth_function(gs_device_t *device, enum gs_depth_test test)
  947. {
  948. GLenum gl_test = convert_gs_depth_test(test);
  949. glDepthFunc(gl_test);
  950. if (!gl_success("glDepthFunc"))
  951. blog(LOG_ERROR, "device_depth_function (GL) failed");
  952. UNUSED_PARAMETER(device);
  953. }
  954. void device_stencil_function(gs_device_t *device, enum gs_stencil_side side,
  955. enum gs_depth_test test)
  956. {
  957. GLenum gl_side = convert_gs_stencil_side(side);
  958. GLenum gl_test = convert_gs_depth_test(test);
  959. glStencilFuncSeparate(gl_side, gl_test, 0, 0xFFFFFFFF);
  960. if (!gl_success("glStencilFuncSeparate"))
  961. blog(LOG_ERROR, "device_stencil_function (GL) failed");
  962. UNUSED_PARAMETER(device);
  963. }
  964. void device_stencil_op(gs_device_t *device, enum gs_stencil_side side,
  965. enum gs_stencil_op_type fail, enum gs_stencil_op_type zfail,
  966. enum gs_stencil_op_type zpass)
  967. {
  968. GLenum gl_side = convert_gs_stencil_side(side);
  969. GLenum gl_fail = convert_gs_stencil_op(fail);
  970. GLenum gl_zfail = convert_gs_stencil_op(zfail);
  971. GLenum gl_zpass = convert_gs_stencil_op(zpass);
  972. glStencilOpSeparate(gl_side, gl_fail, gl_zfail, gl_zpass);
  973. if (!gl_success("glStencilOpSeparate"))
  974. blog(LOG_ERROR, "device_stencil_op (GL) failed");
  975. UNUSED_PARAMETER(device);
  976. }
  977. static inline uint32_t get_target_height(const struct gs_device *device)
  978. {
  979. if (!device->cur_render_target)
  980. return device_get_height(device);
  981. if (device->cur_render_target->type == GS_TEXTURE_2D)
  982. return gs_texture_get_height(device->cur_render_target);
  983. else /* cube map */
  984. return gs_cubetexture_get_size(device->cur_render_target);
  985. }
  986. void device_set_viewport(gs_device_t *device, int x, int y, int width,
  987. int height)
  988. {
  989. uint32_t base_height = 0;
  990. int gl_y = 0;
  991. /* GL uses bottom-up coordinates for viewports. We want top-down */
  992. if (device->cur_render_target) {
  993. base_height = get_target_height(device);
  994. } else if (device->cur_swap) {
  995. uint32_t dw;
  996. gl_getclientsize(device->cur_swap, &dw, &base_height);
  997. }
  998. if (base_height)
  999. gl_y = base_height - y - height;
  1000. glViewport(x, gl_y, width, height);
  1001. if (!gl_success("glViewport"))
  1002. blog(LOG_ERROR, "device_set_viewport (GL) failed");
  1003. device->cur_viewport.x = x;
  1004. device->cur_viewport.y = y;
  1005. device->cur_viewport.cx = width;
  1006. device->cur_viewport.cy = height;
  1007. }
  1008. void device_get_viewport(const gs_device_t *device, struct gs_rect *rect)
  1009. {
  1010. *rect = device->cur_viewport;
  1011. }
  1012. void device_set_scissor_rect(gs_device_t *device, const struct gs_rect *rect)
  1013. {
  1014. UNUSED_PARAMETER(device);
  1015. if (rect != NULL) {
  1016. glScissor(rect->x, rect->y, rect->cx, rect->cy);
  1017. if (gl_success("glScissor") && gl_enable(GL_SCISSOR_TEST))
  1018. return;
  1019. } else if (gl_disable(GL_SCISSOR_TEST)) {
  1020. return;
  1021. }
  1022. blog(LOG_ERROR, "device_set_scissor_rect (GL) failed");
  1023. }
  1024. void device_ortho(gs_device_t *device, float left, float right,
  1025. float top, float bottom, float near, float far)
  1026. {
  1027. struct matrix4 *dst = &device->cur_proj;
  1028. float rml = right-left;
  1029. float bmt = bottom-top;
  1030. float fmn = far-near;
  1031. vec4_zero(&dst->x);
  1032. vec4_zero(&dst->y);
  1033. vec4_zero(&dst->z);
  1034. vec4_zero(&dst->t);
  1035. dst->x.x = 2.0f / rml;
  1036. dst->t.x = (left+right) / -rml;
  1037. dst->y.y = 2.0f / -bmt;
  1038. dst->t.y = (bottom+top) / bmt;
  1039. dst->z.z = -2.0f / fmn;
  1040. dst->t.z = (far+near) / -fmn;
  1041. dst->t.w = 1.0f;
  1042. }
  1043. void device_frustum(gs_device_t *device, float left, float right,
  1044. float top, float bottom, float near, float far)
  1045. {
  1046. struct matrix4 *dst = &device->cur_proj;
  1047. float rml = right-left;
  1048. float tmb = top-bottom;
  1049. float nmf = near-far;
  1050. float nearx2 = 2.0f*near;
  1051. vec4_zero(&dst->x);
  1052. vec4_zero(&dst->y);
  1053. vec4_zero(&dst->z);
  1054. vec4_zero(&dst->t);
  1055. dst->x.x = nearx2 / rml;
  1056. dst->z.x = (left+right) / rml;
  1057. dst->y.y = nearx2 / tmb;
  1058. dst->z.y = (bottom+top) / tmb;
  1059. dst->z.z = (far+near) / nmf;
  1060. dst->t.z = 2.0f * (near*far) / nmf;
  1061. dst->z.w = -1.0f;
  1062. }
  1063. void device_projection_push(gs_device_t *device)
  1064. {
  1065. da_push_back(device->proj_stack, &device->cur_proj);
  1066. }
  1067. void device_projection_pop(gs_device_t *device)
  1068. {
  1069. struct matrix4 *end;
  1070. if (!device->proj_stack.num)
  1071. return;
  1072. end = da_end(device->proj_stack);
  1073. device->cur_proj = *end;
  1074. da_pop_back(device->proj_stack);
  1075. }
  1076. void device_debug_marker_begin(gs_device_t *device,
  1077. const char *markername, const float color[4])
  1078. {
  1079. UNUSED_PARAMETER(device);
  1080. UNUSED_PARAMETER(color);
  1081. glPushDebugGroupKHR(GL_DEBUG_SOURCE_APPLICATION, 0, -1, markername);
  1082. }
  1083. void device_debug_marker_end(gs_device_t *device)
  1084. {
  1085. UNUSED_PARAMETER(device);
  1086. glPopDebugGroupKHR();
  1087. }
  1088. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1089. {
  1090. if (!swapchain)
  1091. return;
  1092. if (swapchain->device->cur_swap == swapchain)
  1093. device_load_swapchain(swapchain->device, NULL);
  1094. gl_platform_cleanup_swapchain(swapchain);
  1095. gl_windowinfo_destroy(swapchain->wi);
  1096. bfree(swapchain);
  1097. }
  1098. void gs_voltexture_destroy(gs_texture_t *voltex)
  1099. {
  1100. /* TODO */
  1101. UNUSED_PARAMETER(voltex);
  1102. }
  1103. uint32_t gs_voltexture_get_width(const gs_texture_t *voltex)
  1104. {
  1105. /* TODO */
  1106. UNUSED_PARAMETER(voltex);
  1107. return 0;
  1108. }
  1109. uint32_t gs_voltexture_get_height(const gs_texture_t *voltex)
  1110. {
  1111. /* TODO */
  1112. UNUSED_PARAMETER(voltex);
  1113. return 0;
  1114. }
  1115. uint32_t gs_voltexture_get_depth(const gs_texture_t *voltex)
  1116. {
  1117. /* TODO */
  1118. UNUSED_PARAMETER(voltex);
  1119. return 0;
  1120. }
  1121. enum gs_color_format gs_voltexture_get_color_format(const gs_texture_t *voltex)
  1122. {
  1123. /* TODO */
  1124. UNUSED_PARAMETER(voltex);
  1125. return GS_UNKNOWN;
  1126. }
  1127. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1128. {
  1129. if (!samplerstate)
  1130. return;
  1131. if (samplerstate->device)
  1132. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1133. if (samplerstate->device->cur_samplers[i] ==
  1134. samplerstate)
  1135. samplerstate->device->cur_samplers[i] = NULL;
  1136. samplerstate_release(samplerstate);
  1137. }