gl-subsystem.c 36 KB

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