gl-subsystem.c 34 KB

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