gl-subsystem.c 37 KB

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