gl-subsystem.c 35 KB

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