gl-subsystem.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. Copyright (C) 2014 by Zachary Lund <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include <graphics/matrix3.h>
  16. #include "gl-subsystem.h"
  17. /* Goofy Windows.h macros need to be removed */
  18. #ifdef near
  19. #undef near
  20. #endif
  21. #ifdef far
  22. #undef far
  23. #endif
  24. /* #define SHOW_ALL_GL_MESSAGES */
  25. #ifdef _DEBUG
  26. static void APIENTRY gl_debug_proc(GLenum source, GLenum type, GLuint id,
  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. bool device_nv12_available(gs_device_t *device)
  1219. {
  1220. UNUSED_PARAMETER(device);
  1221. #ifdef _WIN32
  1222. return false;
  1223. #else
  1224. return true; // always a split R8,R8G8 texture.
  1225. #endif
  1226. }
  1227. bool device_p010_available(gs_device_t *device)
  1228. {
  1229. UNUSED_PARAMETER(device);
  1230. #ifdef _WIN32
  1231. return false;
  1232. #else
  1233. return true; // always a split R16,R16G16 texture.
  1234. #endif
  1235. }
  1236. uint32_t gs_voltexture_get_width(const gs_texture_t *voltex)
  1237. {
  1238. /* TODO */
  1239. UNUSED_PARAMETER(voltex);
  1240. return 0;
  1241. }
  1242. uint32_t gs_voltexture_get_height(const gs_texture_t *voltex)
  1243. {
  1244. /* TODO */
  1245. UNUSED_PARAMETER(voltex);
  1246. return 0;
  1247. }
  1248. uint32_t gs_voltexture_get_depth(const gs_texture_t *voltex)
  1249. {
  1250. /* TODO */
  1251. UNUSED_PARAMETER(voltex);
  1252. return 0;
  1253. }
  1254. enum gs_color_format gs_voltexture_get_color_format(const gs_texture_t *voltex)
  1255. {
  1256. /* TODO */
  1257. UNUSED_PARAMETER(voltex);
  1258. return GS_UNKNOWN;
  1259. }
  1260. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1261. {
  1262. if (!samplerstate)
  1263. return;
  1264. if (samplerstate->device)
  1265. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1266. if (samplerstate->device->cur_samplers[i] ==
  1267. samplerstate)
  1268. samplerstate->device->cur_samplers[i] = NULL;
  1269. samplerstate_release(samplerstate);
  1270. }
  1271. void gs_timer_destroy(gs_timer_t *timer)
  1272. {
  1273. if (!timer)
  1274. return;
  1275. glDeleteQueries(2, timer->queries);
  1276. gl_success("glDeleteQueries");
  1277. bfree(timer);
  1278. }
  1279. void gs_timer_begin(gs_timer_t *timer)
  1280. {
  1281. glQueryCounter(timer->queries[0], GL_TIMESTAMP);
  1282. gl_success("glQueryCounter");
  1283. }
  1284. void gs_timer_end(gs_timer_t *timer)
  1285. {
  1286. glQueryCounter(timer->queries[1], GL_TIMESTAMP);
  1287. gl_success("glQueryCounter");
  1288. }
  1289. bool gs_timer_get_data(gs_timer_t *timer, uint64_t *ticks)
  1290. {
  1291. GLint available = 0;
  1292. glGetQueryObjectiv(timer->queries[1], GL_QUERY_RESULT_AVAILABLE,
  1293. &available);
  1294. GLuint64 begin, end;
  1295. glGetQueryObjectui64v(timer->queries[0], GL_QUERY_RESULT, &begin);
  1296. gl_success("glGetQueryObjectui64v");
  1297. glGetQueryObjectui64v(timer->queries[1], GL_QUERY_RESULT, &end);
  1298. gl_success("glGetQueryObjectui64v");
  1299. *ticks = end - begin;
  1300. return true;
  1301. }
  1302. void gs_timer_range_destroy(gs_timer_range_t *range)
  1303. {
  1304. UNUSED_PARAMETER(range);
  1305. }
  1306. void gs_timer_range_begin(gs_timer_range_t *range)
  1307. {
  1308. UNUSED_PARAMETER(range);
  1309. }
  1310. void gs_timer_range_end(gs_timer_range_t *range)
  1311. {
  1312. UNUSED_PARAMETER(range);
  1313. }
  1314. bool gs_timer_range_get_data(gs_timer_range_t *range, bool *disjoint,
  1315. uint64_t *frequency)
  1316. {
  1317. UNUSED_PARAMETER(range);
  1318. *disjoint = false;
  1319. *frequency = 1000000000;
  1320. return true;
  1321. }