gl-subsystem.c 37 KB

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