gl-subsystem.c 36 KB

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