gl-subsystem.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. #ifdef near
  19. #undef near
  20. #endif
  21. #ifdef far
  22. #undef far
  23. #endif
  24. /* #define SHOW_ALL_GL_MESSAGES */
  25. #ifdef _DEBUG
  26. static void APIENTRY gl_debug_proc(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
  27. const GLchar *message, const GLvoid *data)
  28. {
  29. UNUSED_PARAMETER(id);
  30. UNUSED_PARAMETER(data);
  31. char *source_str, *type_str, *severity_str;
  32. /* frames can get a bit too much spam with irrelevant/insignificant opengl
  33. * debug messages */
  34. #ifndef SHOW_ALL_GL_MESSAGES
  35. if (type > GL_DEBUG_TYPE_PORTABILITY && severity != GL_DEBUG_SEVERITY_HIGH) {
  36. return;
  37. }
  38. #endif
  39. switch (source) {
  40. case GL_DEBUG_SOURCE_API:
  41. source_str = "API";
  42. break;
  43. case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
  44. source_str = "Window System";
  45. break;
  46. case GL_DEBUG_SOURCE_SHADER_COMPILER:
  47. source_str = "Shader Compiler";
  48. break;
  49. case GL_DEBUG_SOURCE_THIRD_PARTY:
  50. source_str = "Third Party";
  51. break;
  52. case GL_DEBUG_SOURCE_APPLICATION:
  53. source_str = "Application";
  54. break;
  55. case GL_DEBUG_SOURCE_OTHER:
  56. source_str = "Other";
  57. break;
  58. default:
  59. source_str = "Unknown";
  60. }
  61. switch (type) {
  62. case GL_DEBUG_TYPE_ERROR:
  63. type_str = "Error";
  64. break;
  65. case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
  66. type_str = "Deprecated Behavior";
  67. break;
  68. case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
  69. type_str = "Undefined Behavior";
  70. break;
  71. case GL_DEBUG_TYPE_PORTABILITY:
  72. type_str = "Portability";
  73. break;
  74. case GL_DEBUG_TYPE_PERFORMANCE:
  75. type_str = "Performance";
  76. break;
  77. case GL_DEBUG_TYPE_OTHER:
  78. type_str = "Other";
  79. break;
  80. default:
  81. type_str = "Unknown";
  82. }
  83. switch (severity) {
  84. case GL_DEBUG_SEVERITY_HIGH:
  85. severity_str = "High";
  86. break;
  87. case GL_DEBUG_SEVERITY_MEDIUM:
  88. severity_str = "Medium";
  89. break;
  90. case GL_DEBUG_SEVERITY_LOW:
  91. severity_str = "Low";
  92. break;
  93. case GL_DEBUG_SEVERITY_NOTIFICATION:
  94. severity_str = "Notification";
  95. break;
  96. default:
  97. severity_str = "Unknown";
  98. }
  99. blog(LOG_DEBUG, "[%s][%s]{%s}: %.*s", source_str, type_str, severity_str, length, message);
  100. }
  101. static void gl_enable_debug()
  102. {
  103. if (GLAD_GL_VERSION_4_3) {
  104. glDebugMessageCallback(gl_debug_proc, NULL);
  105. gl_enable(GL_DEBUG_OUTPUT);
  106. } else if (GLAD_GL_ARB_debug_output) {
  107. glDebugMessageCallbackARB(gl_debug_proc, NULL);
  108. } else {
  109. blog(LOG_DEBUG, "Failed to set GL debug callback as it is "
  110. "not supported.");
  111. }
  112. }
  113. #else
  114. static void gl_enable_debug() {}
  115. #endif
  116. static bool gl_init_extensions(struct gs_device *device)
  117. {
  118. if (!GLAD_GL_VERSION_3_3) {
  119. blog(LOG_ERROR, "obs-studio requires OpenGL version 3.3 or higher.");
  120. return false;
  121. }
  122. gl_enable_debug();
  123. if (!GLAD_GL_EXT_texture_sRGB_decode) {
  124. blog(LOG_ERROR, "OpenGL extension EXT_texture_sRGB_decode "
  125. "is required.");
  126. return false;
  127. }
  128. gl_enable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  129. if (GLAD_GL_VERSION_4_3 || GLAD_GL_ARB_copy_image)
  130. device->copy_type = COPY_TYPE_ARB;
  131. else if (GLAD_GL_NV_copy_image)
  132. device->copy_type = COPY_TYPE_NV;
  133. else
  134. device->copy_type = COPY_TYPE_FBO_BLIT;
  135. return true;
  136. }
  137. static void clear_textures(struct gs_device *device)
  138. {
  139. GLenum i;
  140. for (i = 0; i < GS_MAX_TEXTURES; i++) {
  141. if (device->cur_textures[i]) {
  142. gl_active_texture(GL_TEXTURE0 + i);
  143. gl_bind_texture(device->cur_textures[i]->gl_target, 0);
  144. device->cur_textures[i] = NULL;
  145. }
  146. }
  147. }
  148. void convert_sampler_info(struct gs_sampler_state *sampler, const struct gs_sampler_info *info)
  149. {
  150. GLint max_anisotropy_max;
  151. convert_filter(info->filter, &sampler->min_filter, &sampler->mag_filter);
  152. sampler->address_u = convert_address_mode(info->address_u);
  153. sampler->address_v = convert_address_mode(info->address_v);
  154. sampler->address_w = convert_address_mode(info->address_w);
  155. sampler->max_anisotropy = info->max_anisotropy;
  156. max_anisotropy_max = 1;
  157. if (GLAD_GL_EXT_texture_filter_anisotropic) {
  158. glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy_max);
  159. gl_success("glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)");
  160. }
  161. if (1 <= sampler->max_anisotropy && sampler->max_anisotropy <= max_anisotropy_max)
  162. return;
  163. if (sampler->max_anisotropy < 1)
  164. sampler->max_anisotropy = 1;
  165. else if (sampler->max_anisotropy > max_anisotropy_max)
  166. sampler->max_anisotropy = max_anisotropy_max;
  167. vec4_from_rgba(&sampler->border_color, info->border_color);
  168. blog(LOG_DEBUG,
  169. "convert_sampler_info: 1 <= max_anisotropy <= "
  170. "%d violated, selected: %d, set: %d",
  171. max_anisotropy_max, info->max_anisotropy, sampler->max_anisotropy);
  172. }
  173. const char *device_get_name(void)
  174. {
  175. return "OpenGL";
  176. }
  177. int device_get_type(void)
  178. {
  179. return GS_DEVICE_OPENGL;
  180. }
  181. const char *device_preprocessor_name(void)
  182. {
  183. return "_OPENGL";
  184. }
  185. const char *gpu_get_driver_version(void)
  186. {
  187. return ((const char *)glGetString(GL_VERSION));
  188. }
  189. const char *gpu_get_renderer(void)
  190. {
  191. return ((const char *)glGetString(GL_RENDERER));
  192. }
  193. // Get the amount of dedicated GDDR memory, aka VRAM, in units of kilobytes.
  194. uint64_t gpu_get_dmem(void)
  195. {
  196. GLint dmem = 0;
  197. glGetIntegerv(GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &dmem);
  198. /* GLint is signed, however it makes little sense to have "negative" amounts of GPU memory. Check on this, clamp to
  199. * 0 if so, and cast to an unsigned value explicitly.
  200. */
  201. if (dmem < 0) {
  202. dmem = 0;
  203. }
  204. return (uint64_t)dmem;
  205. }
  206. // Get the amount of CPU memory shared by the GPU, in units of kilobytes.
  207. uint64_t gpu_get_smem(void)
  208. {
  209. GLint dmem, total_mem = 0;
  210. glGetIntegerv(GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &dmem);
  211. glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &total_mem);
  212. /* GLint is signed, however it makes little sense to have "negative" amounts of GPU memory. Check on this, clamp to
  213. * 0 if so, and cast to an unsigned value explicitly.
  214. */
  215. if (dmem < 0) {
  216. dmem = 0;
  217. }
  218. if (total_mem < 0) {
  219. total_mem = 0;
  220. }
  221. return (uint64_t)total_mem - dmem;
  222. }
  223. int device_create(gs_device_t **p_device, uint32_t adapter)
  224. {
  225. struct gs_device *device = bzalloc(sizeof(struct gs_device));
  226. int errorcode = GS_ERROR_FAIL;
  227. blog(LOG_INFO, "---------------------------------");
  228. blog(LOG_INFO, "Initializing OpenGL...");
  229. device->plat = gl_platform_create(device, adapter);
  230. if (!device->plat)
  231. goto fail;
  232. const char *glVendor = (const char *)glGetString(GL_VENDOR);
  233. const char *glRenderer = (const char *)glGetString(GL_RENDERER);
  234. blog(LOG_INFO, "Loading up OpenGL on adapter %s %s", glVendor, glRenderer);
  235. if (!gl_init_extensions(device)) {
  236. errorcode = GS_ERROR_NOT_SUPPORTED;
  237. goto fail;
  238. }
  239. const char *glVersion = (const char *)glGetString(GL_VERSION);
  240. const char *glShadingLanguage = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
  241. blog(LOG_INFO,
  242. "OpenGL loaded successfully, version %s, shading "
  243. "language %s",
  244. glVersion, glShadingLanguage);
  245. gl_enable(GL_CULL_FACE);
  246. gl_gen_vertex_arrays(1, &device->empty_vao);
  247. struct gs_sampler_info raw_load_info;
  248. raw_load_info.filter = GS_FILTER_POINT;
  249. raw_load_info.address_u = GS_ADDRESS_BORDER;
  250. raw_load_info.address_v = GS_ADDRESS_BORDER;
  251. raw_load_info.address_w = GS_ADDRESS_BORDER;
  252. raw_load_info.max_anisotropy = 1;
  253. raw_load_info.border_color = 0;
  254. device->raw_load_sampler = device_samplerstate_create(device, &raw_load_info);
  255. gl_clear_context(device);
  256. device->cur_swap = NULL;
  257. #ifdef _WIN32
  258. blog(LOG_INFO, "Warning: The OpenGL renderer is currently in use. "
  259. "On windows, the OpenGL renderer can decrease "
  260. "capture performance due to the lack of specific "
  261. "features used to maximize capture performance. "
  262. "The Direct3D 11 renderer is recommended instead.");
  263. #endif
  264. *p_device = device;
  265. return GS_SUCCESS;
  266. fail:
  267. blog(LOG_ERROR, "device_create (GL) failed");
  268. bfree(device);
  269. *p_device = NULL;
  270. return errorcode;
  271. }
  272. void device_destroy(gs_device_t *device)
  273. {
  274. if (device) {
  275. while (device->first_program)
  276. gs_program_destroy(device->first_program);
  277. samplerstate_release(device->raw_load_sampler);
  278. gl_delete_vertex_arrays(1, &device->empty_vao);
  279. da_free(device->proj_stack);
  280. gl_platform_destroy(device->plat);
  281. bfree(device);
  282. }
  283. }
  284. gs_swapchain_t *device_swapchain_create(gs_device_t *device, const struct gs_init_data *info)
  285. {
  286. struct gs_swap_chain *swap = bzalloc(sizeof(struct gs_swap_chain));
  287. swap->device = device;
  288. swap->info = *info;
  289. swap->wi = gl_windowinfo_create(info);
  290. if (!swap->wi) {
  291. blog(LOG_ERROR, "device_swapchain_create (GL) failed");
  292. gs_swapchain_destroy(swap);
  293. return NULL;
  294. }
  295. if (!gl_platform_init_swapchain(swap)) {
  296. blog(LOG_ERROR, "gl_platform_init_swapchain failed");
  297. gs_swapchain_destroy(swap);
  298. return NULL;
  299. }
  300. return swap;
  301. }
  302. void device_resize(gs_device_t *device, uint32_t cx, uint32_t cy)
  303. {
  304. /* GL automatically resizes the device, so it doesn't do much */
  305. if (device->cur_swap) {
  306. device->cur_swap->info.cx = cx;
  307. device->cur_swap->info.cy = cy;
  308. } else {
  309. blog(LOG_WARNING, "device_resize (GL): No active swap");
  310. }
  311. gl_update(device);
  312. }
  313. enum gs_color_space device_get_color_space(gs_device_t *device)
  314. {
  315. return device->cur_color_space;
  316. }
  317. void device_update_color_space(gs_device_t *device)
  318. {
  319. if (!device->cur_swap)
  320. blog(LOG_WARNING, "device_display_change (GL): No active swap");
  321. }
  322. void device_get_size(const gs_device_t *device, uint32_t *cx, uint32_t *cy)
  323. {
  324. if (device->cur_swap) {
  325. *cx = device->cur_swap->info.cx;
  326. *cy = device->cur_swap->info.cy;
  327. } else {
  328. blog(LOG_WARNING, "device_get_size (GL): No active swap");
  329. *cx = 0;
  330. *cy = 0;
  331. }
  332. }
  333. uint32_t device_get_width(const gs_device_t *device)
  334. {
  335. if (device->cur_swap) {
  336. return device->cur_swap->info.cx;
  337. } else {
  338. blog(LOG_WARNING, "device_get_width (GL): No active swap");
  339. return 0;
  340. }
  341. }
  342. uint32_t device_get_height(const gs_device_t *device)
  343. {
  344. if (device->cur_swap) {
  345. return device->cur_swap->info.cy;
  346. } else {
  347. blog(LOG_WARNING, "device_get_height (GL): No active swap");
  348. return 0;
  349. }
  350. }
  351. gs_samplerstate_t *device_samplerstate_create(gs_device_t *device, const struct gs_sampler_info *info)
  352. {
  353. struct gs_sampler_state *sampler;
  354. sampler = bzalloc(sizeof(struct gs_sampler_state));
  355. sampler->device = device;
  356. sampler->ref = 1;
  357. convert_sampler_info(sampler, info);
  358. return sampler;
  359. }
  360. gs_timer_t *device_timer_create(gs_device_t *device)
  361. {
  362. UNUSED_PARAMETER(device);
  363. struct gs_timer *timer;
  364. GLuint queries[2];
  365. glGenQueries(2, queries);
  366. if (!gl_success("glGenQueries"))
  367. return NULL;
  368. timer = bzalloc(sizeof(struct gs_timer));
  369. timer->queries[0] = queries[0];
  370. timer->queries[1] = queries[1];
  371. return timer;
  372. }
  373. gs_timer_range_t *device_timer_range_create(gs_device_t *device)
  374. {
  375. UNUSED_PARAMETER(device);
  376. return NULL;
  377. }
  378. enum gs_texture_type device_get_texture_type(const gs_texture_t *texture)
  379. {
  380. return texture->type;
  381. }
  382. static void strip_mipmap_filter(GLint *filter)
  383. {
  384. switch (*filter) {
  385. case GL_NEAREST:
  386. case GL_LINEAR:
  387. return;
  388. case GL_NEAREST_MIPMAP_NEAREST:
  389. case GL_NEAREST_MIPMAP_LINEAR:
  390. *filter = GL_NEAREST;
  391. return;
  392. case GL_LINEAR_MIPMAP_NEAREST:
  393. case GL_LINEAR_MIPMAP_LINEAR:
  394. *filter = GL_LINEAR;
  395. return;
  396. }
  397. *filter = GL_NEAREST;
  398. }
  399. static inline void apply_swizzle(struct gs_texture *tex)
  400. {
  401. if (tex->format == GS_A8) {
  402. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_R, GL_ONE);
  403. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_G, GL_ONE);
  404. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_B, GL_ONE);
  405. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_A, GL_RED);
  406. }
  407. }
  408. static bool load_texture_sampler(gs_texture_t *tex, gs_samplerstate_t *ss)
  409. {
  410. bool success = true;
  411. GLint min_filter;
  412. if (tex->cur_sampler == ss)
  413. return true;
  414. if (tex->cur_sampler)
  415. samplerstate_release(tex->cur_sampler);
  416. tex->cur_sampler = ss;
  417. if (!ss)
  418. return true;
  419. samplerstate_addref(ss);
  420. min_filter = ss->min_filter;
  421. if (gs_texture_is_rect(tex))
  422. strip_mipmap_filter(&min_filter);
  423. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MIN_FILTER, min_filter))
  424. success = false;
  425. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAG_FILTER, ss->mag_filter))
  426. success = false;
  427. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_S, ss->address_u))
  428. success = false;
  429. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_T, ss->address_v))
  430. success = false;
  431. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_R, ss->address_w))
  432. success = false;
  433. if (ss->address_u == GL_CLAMP_TO_BORDER || ss->address_v == GL_CLAMP_TO_BORDER ||
  434. ss->address_w == GL_CLAMP_TO_BORDER) {
  435. if (!gl_tex_param_fv(tex->gl_target, GL_TEXTURE_BORDER_COLOR, ss->border_color.ptr))
  436. success = false;
  437. }
  438. if (GLAD_GL_EXT_texture_filter_anisotropic) {
  439. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, ss->max_anisotropy))
  440. success = false;
  441. }
  442. apply_swizzle(tex);
  443. return success;
  444. }
  445. static inline struct gs_shader_param *get_texture_param(gs_device_t *device, int unit)
  446. {
  447. struct gs_shader *shader = device->cur_pixel_shader;
  448. size_t i;
  449. for (i = 0; i < shader->params.num; i++) {
  450. struct gs_shader_param *param = shader->params.array + i;
  451. if (param->type == GS_SHADER_PARAM_TEXTURE) {
  452. if (param->texture_id == unit)
  453. return param;
  454. }
  455. }
  456. return NULL;
  457. }
  458. static void device_load_texture_internal(gs_device_t *device, gs_texture_t *tex, int unit, GLint decode)
  459. {
  460. struct gs_shader_param *param;
  461. struct gs_sampler_state *sampler;
  462. struct gs_texture *cur_tex = device->cur_textures[unit];
  463. /* need a pixel shader to properly bind textures */
  464. if (!device->cur_pixel_shader)
  465. goto fail;
  466. if (cur_tex == tex)
  467. return;
  468. if (!gl_active_texture(GL_TEXTURE0 + unit))
  469. goto fail;
  470. /* the target for the previous text may not be the same as the
  471. * next texture, so unbind the previous texture first to be safe */
  472. if (cur_tex && (!tex || cur_tex->gl_target != tex->gl_target))
  473. gl_bind_texture(cur_tex->gl_target, 0);
  474. device->cur_textures[unit] = tex;
  475. param = get_texture_param(device, unit);
  476. if (!param)
  477. return;
  478. param->texture = tex;
  479. if (!tex)
  480. return;
  481. if (param->sampler_id != (size_t)-1)
  482. sampler = device->cur_samplers[param->sampler_id];
  483. else
  484. sampler = device->raw_load_sampler;
  485. if (!gl_bind_texture(tex->gl_target, tex->texture))
  486. goto fail;
  487. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_SRGB_DECODE_EXT, decode))
  488. goto fail;
  489. if (sampler && !load_texture_sampler(tex, sampler))
  490. goto fail;
  491. return;
  492. fail:
  493. blog(LOG_ERROR, "device_load_texture (GL) failed");
  494. }
  495. void device_load_texture(gs_device_t *device, gs_texture_t *tex, int unit)
  496. {
  497. device_load_texture_internal(device, tex, unit, GL_SKIP_DECODE_EXT);
  498. }
  499. void device_load_texture_srgb(gs_device_t *device, gs_texture_t *tex, int unit)
  500. {
  501. device_load_texture_internal(device, tex, unit, GL_DECODE_EXT);
  502. }
  503. static bool load_sampler_on_textures(gs_device_t *device, gs_samplerstate_t *ss, int sampler_unit)
  504. {
  505. struct gs_shader *shader = device->cur_pixel_shader;
  506. size_t i;
  507. for (i = 0; i < shader->params.num; i++) {
  508. struct gs_shader_param *param = shader->params.array + i;
  509. if (param->type == GS_SHADER_PARAM_TEXTURE && param->sampler_id == (uint32_t)sampler_unit &&
  510. param->texture) {
  511. if (!gl_active_texture(GL_TEXTURE0 + param->texture_id))
  512. return false;
  513. if (!load_texture_sampler(param->texture, ss))
  514. return false;
  515. }
  516. }
  517. return true;
  518. }
  519. void device_load_samplerstate(gs_device_t *device, gs_samplerstate_t *ss, int unit)
  520. {
  521. /* need a pixel shader to properly bind samplers */
  522. if (!device->cur_pixel_shader)
  523. ss = NULL;
  524. if (device->cur_samplers[unit] == ss)
  525. return;
  526. device->cur_samplers[unit] = ss;
  527. if (!ss)
  528. return;
  529. if (!load_sampler_on_textures(device, ss, unit))
  530. blog(LOG_ERROR, "device_load_samplerstate (GL) failed");
  531. return;
  532. }
  533. void device_load_vertexshader(gs_device_t *device, gs_shader_t *vertshader)
  534. {
  535. if (device->cur_vertex_shader == vertshader)
  536. return;
  537. if (vertshader && vertshader->type != GS_SHADER_VERTEX) {
  538. blog(LOG_ERROR, "Specified shader is not a vertex shader");
  539. blog(LOG_ERROR, "device_load_vertexshader (GL) failed");
  540. return;
  541. }
  542. device->cur_vertex_shader = vertshader;
  543. }
  544. static void load_default_pixelshader_samplers(struct gs_device *device, struct gs_shader *ps)
  545. {
  546. size_t i;
  547. if (!ps)
  548. return;
  549. for (i = 0; i < ps->samplers.num; i++) {
  550. struct gs_sampler_state *ss = ps->samplers.array[i];
  551. device->cur_samplers[i] = ss;
  552. }
  553. for (; i < GS_MAX_TEXTURES; i++)
  554. device->cur_samplers[i] = NULL;
  555. }
  556. void device_load_pixelshader(gs_device_t *device, gs_shader_t *pixelshader)
  557. {
  558. if (device->cur_pixel_shader == pixelshader)
  559. return;
  560. if (pixelshader && pixelshader->type != GS_SHADER_PIXEL) {
  561. blog(LOG_ERROR, "Specified shader is not a pixel shader");
  562. goto fail;
  563. }
  564. device->cur_pixel_shader = pixelshader;
  565. clear_textures(device);
  566. if (pixelshader)
  567. load_default_pixelshader_samplers(device, pixelshader);
  568. return;
  569. fail:
  570. blog(LOG_ERROR, "device_load_pixelshader (GL) failed");
  571. }
  572. void device_load_default_samplerstate(gs_device_t *device, bool b_3d, int unit)
  573. {
  574. /* TODO */
  575. UNUSED_PARAMETER(device);
  576. UNUSED_PARAMETER(b_3d);
  577. UNUSED_PARAMETER(unit);
  578. }
  579. gs_shader_t *device_get_vertex_shader(const gs_device_t *device)
  580. {
  581. return device->cur_vertex_shader;
  582. }
  583. gs_shader_t *device_get_pixel_shader(const gs_device_t *device)
  584. {
  585. return device->cur_pixel_shader;
  586. }
  587. gs_texture_t *device_get_render_target(const gs_device_t *device)
  588. {
  589. return device->cur_render_target;
  590. }
  591. gs_zstencil_t *device_get_zstencil_target(const gs_device_t *device)
  592. {
  593. return device->cur_zstencil_buffer;
  594. }
  595. static bool get_tex_dimensions(gs_texture_t *tex, uint32_t *width, uint32_t *height)
  596. {
  597. if (tex->type == GS_TEXTURE_2D) {
  598. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)tex;
  599. *width = tex2d->width;
  600. *height = tex2d->height;
  601. return true;
  602. } else if (tex->type == GS_TEXTURE_CUBE) {
  603. struct gs_texture_cube *cube = (struct gs_texture_cube *)tex;
  604. *width = cube->size;
  605. *height = cube->size;
  606. return true;
  607. }
  608. blog(LOG_ERROR, "Texture must be 2D or cubemap");
  609. return false;
  610. }
  611. /*
  612. * This automatically manages FBOs so that render targets are always given
  613. * an FBO that matches their width/height/format to maximize optimization
  614. */
  615. struct fbo_info *get_fbo(gs_texture_t *tex, uint32_t width, uint32_t height)
  616. {
  617. if (tex->fbo && tex->fbo->width == width && tex->fbo->height == height && tex->fbo->format == tex->format)
  618. return tex->fbo;
  619. GLuint fbo;
  620. glGenFramebuffers(1, &fbo);
  621. if (!gl_success("glGenFramebuffers"))
  622. return NULL;
  623. tex->fbo = bmalloc(sizeof(struct fbo_info));
  624. tex->fbo->fbo = fbo;
  625. tex->fbo->width = width;
  626. tex->fbo->height = height;
  627. tex->fbo->format = tex->format;
  628. tex->fbo->cur_render_target = NULL;
  629. tex->fbo->cur_render_side = 0;
  630. tex->fbo->cur_zstencil_buffer = NULL;
  631. return tex->fbo;
  632. }
  633. static inline struct fbo_info *get_fbo_by_tex(gs_texture_t *tex)
  634. {
  635. uint32_t width, height;
  636. if (!get_tex_dimensions(tex, &width, &height))
  637. return NULL;
  638. return get_fbo(tex, width, height);
  639. }
  640. static bool set_current_fbo(gs_device_t *device, struct fbo_info *fbo)
  641. {
  642. if (device->cur_fbo != fbo) {
  643. GLuint fbo_obj = fbo ? fbo->fbo : 0;
  644. if (!gl_bind_framebuffer(GL_DRAW_FRAMEBUFFER, fbo_obj))
  645. return false;
  646. if (device->cur_fbo) {
  647. device->cur_fbo->cur_render_target = NULL;
  648. device->cur_fbo->cur_zstencil_buffer = NULL;
  649. }
  650. }
  651. device->cur_fbo = fbo;
  652. return true;
  653. }
  654. static bool attach_rendertarget(struct fbo_info *fbo, gs_texture_t *tex, int side)
  655. {
  656. if (fbo->cur_render_target == tex)
  657. return true;
  658. fbo->cur_render_target = tex;
  659. if (tex->type == GS_TEXTURE_2D) {
  660. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex->texture, 0);
  661. } else if (tex->type == GS_TEXTURE_CUBE) {
  662. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + side,
  663. tex->texture, 0);
  664. } else {
  665. return false;
  666. }
  667. return gl_success("glFramebufferTexture2D");
  668. }
  669. static bool attach_zstencil(struct fbo_info *fbo, gs_zstencil_t *zs)
  670. {
  671. GLuint zsbuffer = 0;
  672. GLenum zs_attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  673. if (fbo->cur_zstencil_buffer == zs)
  674. return true;
  675. fbo->cur_zstencil_buffer = zs;
  676. if (zs) {
  677. zsbuffer = zs->buffer;
  678. zs_attachment = zs->attachment;
  679. }
  680. glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, zs_attachment, GL_RENDERBUFFER, zsbuffer);
  681. if (!gl_success("glFramebufferRenderbuffer"))
  682. return false;
  683. return true;
  684. }
  685. static bool set_target(gs_device_t *device, gs_texture_t *tex, int side, gs_zstencil_t *zs, enum gs_color_space space)
  686. {
  687. device->cur_color_space = space;
  688. if (device->cur_render_target == tex && device->cur_zstencil_buffer == zs && device->cur_render_side == side)
  689. return true;
  690. device->cur_render_target = tex;
  691. device->cur_render_side = side;
  692. device->cur_zstencil_buffer = zs;
  693. if (!tex)
  694. return set_current_fbo(device, NULL);
  695. struct fbo_info *const fbo = get_fbo_by_tex(tex);
  696. if (!fbo)
  697. return false;
  698. set_current_fbo(device, fbo);
  699. if (!attach_rendertarget(fbo, tex, side))
  700. return false;
  701. if (!attach_zstencil(fbo, zs))
  702. return false;
  703. return true;
  704. }
  705. void device_set_render_target(gs_device_t *device, gs_texture_t *tex, gs_zstencil_t *zstencil)
  706. {
  707. if (tex) {
  708. if (tex->type != GS_TEXTURE_2D) {
  709. blog(LOG_ERROR, "Texture is not a 2D texture");
  710. goto fail;
  711. }
  712. if (!tex->is_render_target) {
  713. blog(LOG_ERROR, "Texture is not a render target");
  714. goto fail;
  715. }
  716. }
  717. if (!set_target(device, tex, 0, zstencil, GS_CS_SRGB))
  718. goto fail;
  719. return;
  720. fail:
  721. blog(LOG_ERROR, "device_set_render_target (GL) failed");
  722. }
  723. void device_set_render_target_with_color_space(gs_device_t *device, gs_texture_t *tex, gs_zstencil_t *zstencil,
  724. enum gs_color_space space)
  725. {
  726. if (tex) {
  727. if (tex->type != GS_TEXTURE_2D) {
  728. blog(LOG_ERROR, "Texture is not a 2D texture");
  729. goto fail;
  730. }
  731. if (!tex->is_render_target) {
  732. blog(LOG_ERROR, "Texture is not a render target");
  733. goto fail;
  734. }
  735. }
  736. if (!set_target(device, tex, 0, zstencil, space))
  737. goto fail;
  738. return;
  739. fail:
  740. blog(LOG_ERROR, "device_set_render_target_with_color_space (GL) failed");
  741. }
  742. void device_set_cube_render_target(gs_device_t *device, gs_texture_t *cubetex, int side, gs_zstencil_t *zstencil)
  743. {
  744. if (cubetex) {
  745. if (cubetex->type != GS_TEXTURE_CUBE) {
  746. blog(LOG_ERROR, "Texture is not a cube texture");
  747. goto fail;
  748. }
  749. if (!cubetex->is_render_target) {
  750. blog(LOG_ERROR, "Texture is not a render target");
  751. goto fail;
  752. }
  753. }
  754. if (!set_target(device, cubetex, side, zstencil, GS_CS_SRGB))
  755. goto fail;
  756. return;
  757. fail:
  758. blog(LOG_ERROR, "device_set_cube_render_target (GL) failed");
  759. }
  760. void device_enable_framebuffer_srgb(gs_device_t *device, bool enable)
  761. {
  762. UNUSED_PARAMETER(device);
  763. if (enable)
  764. gl_enable(GL_FRAMEBUFFER_SRGB);
  765. else
  766. gl_disable(GL_FRAMEBUFFER_SRGB);
  767. }
  768. bool device_framebuffer_srgb_enabled(gs_device_t *device)
  769. {
  770. UNUSED_PARAMETER(device);
  771. const GLboolean enabled = glIsEnabled(GL_FRAMEBUFFER_SRGB);
  772. gl_success("glIsEnabled");
  773. return enabled == GL_TRUE;
  774. }
  775. void device_copy_texture_region(gs_device_t *device, gs_texture_t *dst, uint32_t dst_x, uint32_t dst_y,
  776. gs_texture_t *src, uint32_t src_x, uint32_t src_y, uint32_t src_w, uint32_t src_h)
  777. {
  778. struct gs_texture_2d *src2d = (struct gs_texture_2d *)src;
  779. struct gs_texture_2d *dst2d = (struct gs_texture_2d *)dst;
  780. if (!src) {
  781. blog(LOG_ERROR, "Source texture is NULL");
  782. goto fail;
  783. }
  784. if (!dst) {
  785. blog(LOG_ERROR, "Destination texture is NULL");
  786. goto fail;
  787. }
  788. if (dst->type != GS_TEXTURE_2D || src->type != GS_TEXTURE_2D) {
  789. blog(LOG_ERROR, "Source and destination textures must be 2D "
  790. "textures");
  791. goto fail;
  792. }
  793. if (gs_generalize_format(dst->format) != gs_generalize_format(src->format)) {
  794. blog(LOG_ERROR, "Source and destination formats do not match");
  795. goto fail;
  796. }
  797. uint32_t nw = (uint32_t)src_w ? (uint32_t)src_w : (src2d->width - src_x);
  798. uint32_t nh = (uint32_t)src_h ? (uint32_t)src_h : (src2d->height - src_y);
  799. if (dst2d->width - dst_x < nw || dst2d->height - dst_y < nh) {
  800. blog(LOG_ERROR, "Destination texture region is not big "
  801. "enough to hold the source region");
  802. goto fail;
  803. }
  804. if (!gl_copy_texture(device, dst, dst_x, dst_y, src, src_x, src_y, nw, nh))
  805. goto fail;
  806. return;
  807. fail:
  808. blog(LOG_ERROR, "device_copy_texture (GL) failed");
  809. }
  810. void device_copy_texture(gs_device_t *device, gs_texture_t *dst, gs_texture_t *src)
  811. {
  812. device_copy_texture_region(device, dst, 0, 0, src, 0, 0, 0, 0);
  813. }
  814. void device_begin_frame(gs_device_t *device)
  815. {
  816. /* does nothing */
  817. UNUSED_PARAMETER(device);
  818. }
  819. void device_begin_scene(gs_device_t *device)
  820. {
  821. clear_textures(device);
  822. }
  823. static inline bool can_render(const gs_device_t *device, uint32_t num_verts)
  824. {
  825. if (!device->cur_vertex_shader) {
  826. blog(LOG_ERROR, "No vertex shader specified");
  827. return false;
  828. }
  829. if (!device->cur_pixel_shader) {
  830. blog(LOG_ERROR, "No pixel shader specified");
  831. return false;
  832. }
  833. if (!device->cur_vertex_buffer && (num_verts == 0)) {
  834. blog(LOG_ERROR, "No vertex buffer specified");
  835. return false;
  836. }
  837. if (!device->cur_swap && !device->cur_render_target) {
  838. blog(LOG_ERROR, "No active swap chain or render target");
  839. return false;
  840. }
  841. return true;
  842. }
  843. static void update_viewproj_matrix(struct gs_device *device)
  844. {
  845. struct gs_shader *vs = device->cur_vertex_shader;
  846. struct matrix4 cur_proj;
  847. gs_matrix_get(&device->cur_view);
  848. matrix4_copy(&cur_proj, &device->cur_proj);
  849. if (device->cur_fbo) {
  850. cur_proj.x.y = -cur_proj.x.y;
  851. cur_proj.y.y = -cur_proj.y.y;
  852. cur_proj.z.y = -cur_proj.z.y;
  853. cur_proj.t.y = -cur_proj.t.y;
  854. glFrontFace(GL_CW);
  855. } else {
  856. glFrontFace(GL_CCW);
  857. }
  858. gl_success("glFrontFace");
  859. matrix4_mul(&device->cur_viewproj, &device->cur_view, &cur_proj);
  860. matrix4_transpose(&device->cur_viewproj, &device->cur_viewproj);
  861. if (vs->viewproj)
  862. gs_shader_set_matrix4(vs->viewproj, &device->cur_viewproj);
  863. }
  864. static inline struct gs_program *find_program(const struct gs_device *device)
  865. {
  866. struct gs_program *program = device->first_program;
  867. while (program) {
  868. if (program->vertex_shader == device->cur_vertex_shader &&
  869. program->pixel_shader == device->cur_pixel_shader)
  870. return program;
  871. program = program->next;
  872. }
  873. return NULL;
  874. }
  875. static inline struct gs_program *get_shader_program(struct gs_device *device)
  876. {
  877. struct gs_program *program = find_program(device);
  878. if (!program)
  879. program = gs_program_create(device);
  880. return program;
  881. }
  882. void device_draw(gs_device_t *device, enum gs_draw_mode draw_mode, uint32_t start_vert, uint32_t num_verts)
  883. {
  884. struct gs_vertex_buffer *vb = device->cur_vertex_buffer;
  885. struct gs_index_buffer *ib = device->cur_index_buffer;
  886. GLenum topology = convert_gs_topology(draw_mode);
  887. gs_effect_t *effect = gs_get_effect();
  888. struct gs_program *program;
  889. if (!can_render(device, num_verts))
  890. goto fail;
  891. if (effect)
  892. gs_effect_update_params(effect);
  893. program = get_shader_program(device);
  894. if (!program)
  895. goto fail;
  896. if (vb)
  897. load_vb_buffers(program, vb, ib);
  898. else
  899. gl_bind_vertex_array(device->empty_vao);
  900. if (program != device->cur_program && device->cur_program) {
  901. glUseProgram(0);
  902. gl_success("glUseProgram (zero)");
  903. }
  904. if (program != device->cur_program) {
  905. device->cur_program = program;
  906. glUseProgram(program->obj);
  907. if (!gl_success("glUseProgram"))
  908. goto fail;
  909. }
  910. update_viewproj_matrix(device);
  911. program_update_params(program);
  912. if (ib) {
  913. if (num_verts == 0)
  914. num_verts = (uint32_t)device->cur_index_buffer->num;
  915. glDrawElements(topology, num_verts, ib->gl_type, (const GLvoid *)(start_vert * ib->width));
  916. if (!gl_success("glDrawElements"))
  917. goto fail;
  918. } else {
  919. if (num_verts == 0)
  920. num_verts = (uint32_t)device->cur_vertex_buffer->num;
  921. glDrawArrays(topology, start_vert, num_verts);
  922. if (!gl_success("glDrawArrays"))
  923. goto fail;
  924. }
  925. return;
  926. fail:
  927. blog(LOG_ERROR, "device_draw (GL) failed");
  928. }
  929. void device_end_scene(gs_device_t *device)
  930. {
  931. /* does nothing */
  932. UNUSED_PARAMETER(device);
  933. }
  934. void device_clear(gs_device_t *device, uint32_t clear_flags, const struct vec4 *color, float depth, uint8_t stencil)
  935. {
  936. GLbitfield gl_flags = 0;
  937. if (clear_flags & GS_CLEAR_COLOR) {
  938. glClearColor(color->x, color->y, color->z, color->w);
  939. gl_flags |= GL_COLOR_BUFFER_BIT;
  940. }
  941. if (clear_flags & GS_CLEAR_DEPTH) {
  942. glClearDepth(depth);
  943. gl_flags |= GL_DEPTH_BUFFER_BIT;
  944. }
  945. if (clear_flags & GS_CLEAR_STENCIL) {
  946. glClearStencil(stencil);
  947. gl_flags |= GL_STENCIL_BUFFER_BIT;
  948. }
  949. glClear(gl_flags);
  950. if (!gl_success("glClear"))
  951. blog(LOG_ERROR, "device_clear (GL) failed");
  952. UNUSED_PARAMETER(device);
  953. }
  954. void device_flush(gs_device_t *device)
  955. {
  956. #ifdef __APPLE__
  957. if (!device->cur_swap)
  958. glFlush();
  959. #else
  960. glFlush();
  961. UNUSED_PARAMETER(device);
  962. #endif
  963. }
  964. void device_set_cull_mode(gs_device_t *device, enum gs_cull_mode mode)
  965. {
  966. if (device->cur_cull_mode == mode)
  967. return;
  968. if (device->cur_cull_mode == GS_NEITHER)
  969. gl_enable(GL_CULL_FACE);
  970. device->cur_cull_mode = mode;
  971. if (mode == GS_BACK)
  972. gl_cull_face(GL_BACK);
  973. else if (mode == GS_FRONT)
  974. gl_cull_face(GL_FRONT);
  975. else
  976. gl_disable(GL_CULL_FACE);
  977. }
  978. enum gs_cull_mode device_get_cull_mode(const gs_device_t *device)
  979. {
  980. return device->cur_cull_mode;
  981. }
  982. void device_enable_blending(gs_device_t *device, bool enable)
  983. {
  984. if (enable)
  985. gl_enable(GL_BLEND);
  986. else
  987. gl_disable(GL_BLEND);
  988. UNUSED_PARAMETER(device);
  989. }
  990. void device_enable_depth_test(gs_device_t *device, bool enable)
  991. {
  992. if (enable)
  993. gl_enable(GL_DEPTH_TEST);
  994. else
  995. gl_disable(GL_DEPTH_TEST);
  996. UNUSED_PARAMETER(device);
  997. }
  998. void device_enable_stencil_test(gs_device_t *device, bool enable)
  999. {
  1000. if (enable)
  1001. gl_enable(GL_STENCIL_TEST);
  1002. else
  1003. gl_disable(GL_STENCIL_TEST);
  1004. UNUSED_PARAMETER(device);
  1005. }
  1006. void device_enable_stencil_write(gs_device_t *device, bool enable)
  1007. {
  1008. if (enable)
  1009. glStencilMask(0xFFFFFFFF);
  1010. else
  1011. glStencilMask(0);
  1012. UNUSED_PARAMETER(device);
  1013. }
  1014. void device_enable_color(gs_device_t *device, bool red, bool green, bool blue, bool alpha)
  1015. {
  1016. glColorMask(red, green, blue, alpha);
  1017. UNUSED_PARAMETER(device);
  1018. }
  1019. void device_blend_function(gs_device_t *device, enum gs_blend_type src, enum gs_blend_type dest)
  1020. {
  1021. GLenum gl_src = convert_gs_blend_type(src);
  1022. GLenum gl_dst = convert_gs_blend_type(dest);
  1023. glBlendFunc(gl_src, gl_dst);
  1024. if (!gl_success("glBlendFunc"))
  1025. blog(LOG_ERROR, "device_blend_function (GL) failed");
  1026. UNUSED_PARAMETER(device);
  1027. }
  1028. void device_blend_function_separate(gs_device_t *device, enum gs_blend_type src_c, enum gs_blend_type dest_c,
  1029. enum gs_blend_type src_a, enum gs_blend_type dest_a)
  1030. {
  1031. GLenum gl_src_c = convert_gs_blend_type(src_c);
  1032. GLenum gl_dst_c = convert_gs_blend_type(dest_c);
  1033. GLenum gl_src_a = convert_gs_blend_type(src_a);
  1034. GLenum gl_dst_a = convert_gs_blend_type(dest_a);
  1035. glBlendFuncSeparate(gl_src_c, gl_dst_c, gl_src_a, gl_dst_a);
  1036. if (!gl_success("glBlendFuncSeparate"))
  1037. blog(LOG_ERROR, "device_blend_function_separate (GL) failed");
  1038. UNUSED_PARAMETER(device);
  1039. }
  1040. void device_blend_op(gs_device_t *device, enum gs_blend_op_type op)
  1041. {
  1042. GLenum gl_blend_op = convert_gs_blend_op_type(op);
  1043. glBlendEquation(gl_blend_op);
  1044. if (!gl_success("glBlendEquation"))
  1045. blog(LOG_ERROR, "device_blend_op (GL) failed");
  1046. UNUSED_PARAMETER(device);
  1047. }
  1048. void device_depth_function(gs_device_t *device, enum gs_depth_test test)
  1049. {
  1050. GLenum gl_test = convert_gs_depth_test(test);
  1051. glDepthFunc(gl_test);
  1052. if (!gl_success("glDepthFunc"))
  1053. blog(LOG_ERROR, "device_depth_function (GL) failed");
  1054. UNUSED_PARAMETER(device);
  1055. }
  1056. void device_stencil_function(gs_device_t *device, enum gs_stencil_side side, enum gs_depth_test test)
  1057. {
  1058. GLenum gl_side = convert_gs_stencil_side(side);
  1059. GLenum gl_test = convert_gs_depth_test(test);
  1060. glStencilFuncSeparate(gl_side, gl_test, 0, 0xFFFFFFFF);
  1061. if (!gl_success("glStencilFuncSeparate"))
  1062. blog(LOG_ERROR, "device_stencil_function (GL) failed");
  1063. UNUSED_PARAMETER(device);
  1064. }
  1065. void device_stencil_op(gs_device_t *device, enum gs_stencil_side side, enum gs_stencil_op_type fail,
  1066. enum gs_stencil_op_type zfail, enum gs_stencil_op_type zpass)
  1067. {
  1068. GLenum gl_side = convert_gs_stencil_side(side);
  1069. GLenum gl_fail = convert_gs_stencil_op(fail);
  1070. GLenum gl_zfail = convert_gs_stencil_op(zfail);
  1071. GLenum gl_zpass = convert_gs_stencil_op(zpass);
  1072. glStencilOpSeparate(gl_side, gl_fail, gl_zfail, gl_zpass);
  1073. if (!gl_success("glStencilOpSeparate"))
  1074. blog(LOG_ERROR, "device_stencil_op (GL) failed");
  1075. UNUSED_PARAMETER(device);
  1076. }
  1077. static inline uint32_t get_target_height(const struct gs_device *device)
  1078. {
  1079. if (!device->cur_render_target)
  1080. return device_get_height(device);
  1081. if (device->cur_render_target->type == GS_TEXTURE_2D)
  1082. return gs_texture_get_height(device->cur_render_target);
  1083. else /* cube map */
  1084. return gs_cubetexture_get_size(device->cur_render_target);
  1085. }
  1086. void device_set_viewport(gs_device_t *device, int x, int y, int width, int height)
  1087. {
  1088. uint32_t base_height = 0;
  1089. /* GL uses bottom-up coordinates for viewports. We want top-down */
  1090. if (device->cur_render_target) {
  1091. base_height = get_target_height(device);
  1092. } else if (device->cur_swap) {
  1093. uint32_t dw;
  1094. gl_getclientsize(device->cur_swap, &dw, &base_height);
  1095. }
  1096. GLint gl_y = y;
  1097. if (base_height && !device->cur_fbo)
  1098. gl_y = base_height - y - height;
  1099. glViewport(x, gl_y, width, height);
  1100. if (!gl_success("glViewport"))
  1101. blog(LOG_ERROR, "device_set_viewport (GL) failed");
  1102. device->cur_viewport.x = x;
  1103. device->cur_viewport.y = y;
  1104. device->cur_viewport.cx = width;
  1105. device->cur_viewport.cy = height;
  1106. }
  1107. void device_get_viewport(const gs_device_t *device, struct gs_rect *rect)
  1108. {
  1109. *rect = device->cur_viewport;
  1110. }
  1111. void device_set_scissor_rect(gs_device_t *device, const struct gs_rect *rect)
  1112. {
  1113. UNUSED_PARAMETER(device);
  1114. if (rect != NULL) {
  1115. glScissor(rect->x, rect->y, rect->cx, rect->cy);
  1116. if (gl_success("glScissor") && gl_enable(GL_SCISSOR_TEST))
  1117. return;
  1118. } else if (gl_disable(GL_SCISSOR_TEST)) {
  1119. return;
  1120. }
  1121. blog(LOG_ERROR, "device_set_scissor_rect (GL) failed");
  1122. }
  1123. void device_ortho(gs_device_t *device, float left, float right, float top, float bottom, float near, float far)
  1124. {
  1125. struct matrix4 *dst = &device->cur_proj;
  1126. float rml = right - left;
  1127. float bmt = bottom - top;
  1128. float fmn = far - near;
  1129. vec4_zero(&dst->x);
  1130. vec4_zero(&dst->y);
  1131. vec4_zero(&dst->z);
  1132. vec4_zero(&dst->t);
  1133. dst->x.x = 2.0f / rml;
  1134. dst->t.x = (left + right) / -rml;
  1135. dst->y.y = 2.0f / -bmt;
  1136. dst->t.y = (bottom + top) / bmt;
  1137. dst->z.z = -2.0f / fmn;
  1138. dst->t.z = (far + near) / -fmn;
  1139. dst->t.w = 1.0f;
  1140. }
  1141. void device_frustum(gs_device_t *device, float left, float right, float top, float bottom, float near, float far)
  1142. {
  1143. struct matrix4 *dst = &device->cur_proj;
  1144. float rml = right - left;
  1145. float tmb = top - bottom;
  1146. float nmf = near - far;
  1147. float nearx2 = 2.0f * near;
  1148. vec4_zero(&dst->x);
  1149. vec4_zero(&dst->y);
  1150. vec4_zero(&dst->z);
  1151. vec4_zero(&dst->t);
  1152. dst->x.x = nearx2 / rml;
  1153. dst->z.x = (left + right) / rml;
  1154. dst->y.y = nearx2 / tmb;
  1155. dst->z.y = (bottom + top) / tmb;
  1156. dst->z.z = (far + near) / nmf;
  1157. dst->t.z = 2.0f * (near * far) / nmf;
  1158. dst->z.w = -1.0f;
  1159. }
  1160. void device_projection_push(gs_device_t *device)
  1161. {
  1162. da_push_back(device->proj_stack, &device->cur_proj);
  1163. }
  1164. void device_projection_pop(gs_device_t *device)
  1165. {
  1166. struct matrix4 *end;
  1167. if (!device->proj_stack.num)
  1168. return;
  1169. end = da_end(device->proj_stack);
  1170. device->cur_proj = *end;
  1171. da_pop_back(device->proj_stack);
  1172. }
  1173. void device_debug_marker_begin(gs_device_t *device, const char *markername, const float color[4])
  1174. {
  1175. UNUSED_PARAMETER(device);
  1176. UNUSED_PARAMETER(color);
  1177. glPushDebugGroupKHR(GL_DEBUG_SOURCE_APPLICATION, 0, -1, markername);
  1178. }
  1179. void device_debug_marker_end(gs_device_t *device)
  1180. {
  1181. UNUSED_PARAMETER(device);
  1182. glPopDebugGroupKHR();
  1183. }
  1184. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1185. {
  1186. if (!swapchain)
  1187. return;
  1188. if (swapchain->device->cur_swap == swapchain)
  1189. device_load_swapchain(swapchain->device, NULL);
  1190. gl_platform_cleanup_swapchain(swapchain);
  1191. gl_windowinfo_destroy(swapchain->wi);
  1192. bfree(swapchain);
  1193. }
  1194. bool device_nv12_available(gs_device_t *device)
  1195. {
  1196. UNUSED_PARAMETER(device);
  1197. #ifdef _WIN32
  1198. return false;
  1199. #else
  1200. return true; // always a split R8,R8G8 texture.
  1201. #endif
  1202. }
  1203. bool device_p010_available(gs_device_t *device)
  1204. {
  1205. UNUSED_PARAMETER(device);
  1206. #ifdef _WIN32
  1207. return false;
  1208. #else
  1209. return true; // always a split R16,R16G16 texture.
  1210. #endif
  1211. }
  1212. uint32_t gs_voltexture_get_width(const gs_texture_t *voltex)
  1213. {
  1214. /* TODO */
  1215. UNUSED_PARAMETER(voltex);
  1216. return 0;
  1217. }
  1218. uint32_t gs_voltexture_get_height(const gs_texture_t *voltex)
  1219. {
  1220. /* TODO */
  1221. UNUSED_PARAMETER(voltex);
  1222. return 0;
  1223. }
  1224. uint32_t gs_voltexture_get_depth(const gs_texture_t *voltex)
  1225. {
  1226. /* TODO */
  1227. UNUSED_PARAMETER(voltex);
  1228. return 0;
  1229. }
  1230. enum gs_color_format gs_voltexture_get_color_format(const gs_texture_t *voltex)
  1231. {
  1232. /* TODO */
  1233. UNUSED_PARAMETER(voltex);
  1234. return GS_UNKNOWN;
  1235. }
  1236. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1237. {
  1238. if (!samplerstate)
  1239. return;
  1240. if (samplerstate->device)
  1241. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1242. if (samplerstate->device->cur_samplers[i] == samplerstate)
  1243. samplerstate->device->cur_samplers[i] = NULL;
  1244. samplerstate_release(samplerstate);
  1245. }
  1246. void gs_timer_destroy(gs_timer_t *timer)
  1247. {
  1248. if (!timer)
  1249. return;
  1250. glDeleteQueries(2, timer->queries);
  1251. gl_success("glDeleteQueries");
  1252. bfree(timer);
  1253. }
  1254. void gs_timer_begin(gs_timer_t *timer)
  1255. {
  1256. glQueryCounter(timer->queries[0], GL_TIMESTAMP);
  1257. gl_success("glQueryCounter");
  1258. }
  1259. void gs_timer_end(gs_timer_t *timer)
  1260. {
  1261. glQueryCounter(timer->queries[1], GL_TIMESTAMP);
  1262. gl_success("glQueryCounter");
  1263. }
  1264. bool gs_timer_get_data(gs_timer_t *timer, uint64_t *ticks)
  1265. {
  1266. GLint available = 0;
  1267. glGetQueryObjectiv(timer->queries[1], GL_QUERY_RESULT_AVAILABLE, &available);
  1268. GLuint64 begin, end;
  1269. glGetQueryObjectui64v(timer->queries[0], GL_QUERY_RESULT, &begin);
  1270. gl_success("glGetQueryObjectui64v");
  1271. glGetQueryObjectui64v(timer->queries[1], GL_QUERY_RESULT, &end);
  1272. gl_success("glGetQueryObjectui64v");
  1273. *ticks = end - begin;
  1274. return true;
  1275. }
  1276. void gs_timer_range_destroy(gs_timer_range_t *range)
  1277. {
  1278. UNUSED_PARAMETER(range);
  1279. }
  1280. void gs_timer_range_begin(gs_timer_range_t *range)
  1281. {
  1282. UNUSED_PARAMETER(range);
  1283. }
  1284. void gs_timer_range_end(gs_timer_range_t *range)
  1285. {
  1286. UNUSED_PARAMETER(range);
  1287. }
  1288. bool gs_timer_range_get_data(gs_timer_range_t *range, bool *disjoint, uint64_t *frequency)
  1289. {
  1290. UNUSED_PARAMETER(range);
  1291. *disjoint = false;
  1292. *frequency = 1000000000;
  1293. return true;
  1294. }