gl-subsystem.c 36 KB

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