1
0

gl-subsystem.c 36 KB

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