gl-subsystem.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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_2_1) {
  118. blog(LOG_ERROR, "obs-studio requires OpenGL version 2.1 or "
  119. "higher.");
  120. return false;
  121. }
  122. gl_enable_debug();
  123. if (!GLAD_GL_VERSION_3_0 && !GLAD_GL_ARB_framebuffer_object) {
  124. blog(LOG_ERROR, "OpenGL extension ARB_framebuffer_object "
  125. "is required.");
  126. return false;
  127. }
  128. if (GLAD_GL_VERSION_3_2 || GLAD_GL_ARB_seamless_cube_map) {
  129. gl_enable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  130. }
  131. if (GLAD_GL_VERSION_4_3 || GLAD_GL_ARB_copy_image)
  132. device->copy_type = COPY_TYPE_ARB;
  133. else if (GLAD_GL_NV_copy_image)
  134. device->copy_type = COPY_TYPE_NV;
  135. else
  136. device->copy_type = COPY_TYPE_FBO_BLIT;
  137. return true;
  138. }
  139. static void clear_textures(struct gs_device *device)
  140. {
  141. GLenum i;
  142. for (i = 0; i < GS_MAX_TEXTURES; i++) {
  143. if (device->cur_textures[i]) {
  144. gl_active_texture(GL_TEXTURE0 + i);
  145. gl_bind_texture(device->cur_textures[i]->gl_target, 0);
  146. device->cur_textures[i] = NULL;
  147. }
  148. }
  149. }
  150. void convert_sampler_info(struct gs_sampler_state *sampler,
  151. const struct gs_sampler_info *info)
  152. {
  153. GLint max_anisotropy_max;
  154. convert_filter(info->filter, &sampler->min_filter,
  155. &sampler->mag_filter);
  156. sampler->address_u = convert_address_mode(info->address_u);
  157. sampler->address_v = convert_address_mode(info->address_v);
  158. sampler->address_w = convert_address_mode(info->address_w);
  159. sampler->max_anisotropy = info->max_anisotropy;
  160. max_anisotropy_max = 1;
  161. glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy_max);
  162. gl_success("glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX)");
  163. if (1 <= sampler->max_anisotropy &&
  164. sampler->max_anisotropy <= max_anisotropy_max)
  165. return;
  166. if (sampler->max_anisotropy < 1)
  167. sampler->max_anisotropy = 1;
  168. else if (sampler->max_anisotropy > max_anisotropy_max)
  169. sampler->max_anisotropy = max_anisotropy_max;
  170. blog(LOG_DEBUG,
  171. "convert_sampler_info: 1 <= max_anisotropy <= "
  172. "%d violated, selected: %d, set: %d",
  173. max_anisotropy_max, info->max_anisotropy, sampler->max_anisotropy);
  174. }
  175. const char *device_get_name(void)
  176. {
  177. return "OpenGL";
  178. }
  179. int device_get_type(void)
  180. {
  181. return GS_DEVICE_OPENGL;
  182. }
  183. const char *device_preprocessor_name(void)
  184. {
  185. return "_OPENGL";
  186. }
  187. int device_create(gs_device_t **p_device, uint32_t adapter)
  188. {
  189. struct gs_device *device = bzalloc(sizeof(struct gs_device));
  190. int errorcode = GS_ERROR_FAIL;
  191. blog(LOG_INFO, "---------------------------------");
  192. blog(LOG_INFO, "Initializing OpenGL...");
  193. device->plat = gl_platform_create(device, adapter);
  194. if (!device->plat)
  195. goto fail;
  196. const char *glVendor = (const char *)glGetString(GL_VENDOR);
  197. const char *glRenderer = (const char *)glGetString(GL_RENDERER);
  198. blog(LOG_INFO, "Loading up OpenGL on adapter %s %s", glVendor,
  199. glRenderer);
  200. if (!gl_init_extensions(device)) {
  201. errorcode = GS_ERROR_NOT_SUPPORTED;
  202. goto fail;
  203. }
  204. const char *glVersion = (const char *)glGetString(GL_VERSION);
  205. const char *glShadingLanguage =
  206. (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
  207. blog(LOG_INFO,
  208. "OpenGL loaded successfully, version %s, shading "
  209. "language %s",
  210. glVersion, glShadingLanguage);
  211. gl_enable(GL_CULL_FACE);
  212. gl_gen_vertex_arrays(1, &device->empty_vao);
  213. device_leave_context(device);
  214. device->cur_swap = NULL;
  215. #ifdef _WIN32
  216. blog(LOG_INFO, "Warning: The OpenGL renderer is currently in use. "
  217. "On windows, the OpenGL renderer can decrease "
  218. "capture performance due to the lack of specific "
  219. "features used to maximize capture performance. "
  220. "The Direct3D 11 renderer is recommended instead.");
  221. #endif
  222. *p_device = device;
  223. return GS_SUCCESS;
  224. fail:
  225. blog(LOG_ERROR, "device_create (GL) failed");
  226. bfree(device);
  227. *p_device = NULL;
  228. return errorcode;
  229. }
  230. void device_destroy(gs_device_t *device)
  231. {
  232. if (device) {
  233. while (device->first_program)
  234. gs_program_destroy(device->first_program);
  235. gl_delete_vertex_arrays(1, &device->empty_vao);
  236. da_free(device->proj_stack);
  237. gl_platform_destroy(device->plat);
  238. bfree(device);
  239. }
  240. }
  241. gs_swapchain_t *device_swapchain_create(gs_device_t *device,
  242. const struct gs_init_data *info)
  243. {
  244. struct gs_swap_chain *swap = bzalloc(sizeof(struct gs_swap_chain));
  245. swap->device = device;
  246. swap->info = *info;
  247. swap->wi = gl_windowinfo_create(info);
  248. if (!swap->wi) {
  249. blog(LOG_ERROR, "device_swapchain_create (GL) failed");
  250. gs_swapchain_destroy(swap);
  251. return NULL;
  252. }
  253. if (!gl_platform_init_swapchain(swap)) {
  254. blog(LOG_ERROR, "gl_platform_init_swapchain failed");
  255. gs_swapchain_destroy(swap);
  256. return NULL;
  257. }
  258. return swap;
  259. }
  260. void device_resize(gs_device_t *device, uint32_t cx, uint32_t cy)
  261. {
  262. /* GL automatically resizes the device, so it doesn't do much */
  263. if (device->cur_swap) {
  264. device->cur_swap->info.cx = cx;
  265. device->cur_swap->info.cy = cy;
  266. } else {
  267. blog(LOG_WARNING, "device_resize (GL): No active swap");
  268. }
  269. gl_update(device);
  270. }
  271. void device_get_size(const gs_device_t *device, uint32_t *cx, uint32_t *cy)
  272. {
  273. if (device->cur_swap) {
  274. *cx = device->cur_swap->info.cx;
  275. *cy = device->cur_swap->info.cy;
  276. } else {
  277. blog(LOG_WARNING, "device_get_size (GL): No active swap");
  278. *cx = 0;
  279. *cy = 0;
  280. }
  281. }
  282. uint32_t device_get_width(const gs_device_t *device)
  283. {
  284. if (device->cur_swap) {
  285. return device->cur_swap->info.cx;
  286. } else {
  287. blog(LOG_WARNING, "device_get_width (GL): No active swap");
  288. return 0;
  289. }
  290. }
  291. uint32_t device_get_height(const gs_device_t *device)
  292. {
  293. if (device->cur_swap) {
  294. return device->cur_swap->info.cy;
  295. } else {
  296. blog(LOG_WARNING, "device_get_height (GL): No active swap");
  297. return 0;
  298. }
  299. }
  300. gs_texture_t *device_voltexture_create(gs_device_t *device, uint32_t width,
  301. uint32_t height, uint32_t depth,
  302. enum gs_color_format color_format,
  303. uint32_t levels, const uint8_t **data,
  304. uint32_t flags)
  305. {
  306. /* TODO */
  307. UNUSED_PARAMETER(device);
  308. UNUSED_PARAMETER(width);
  309. UNUSED_PARAMETER(height);
  310. UNUSED_PARAMETER(depth);
  311. UNUSED_PARAMETER(color_format);
  312. UNUSED_PARAMETER(levels);
  313. UNUSED_PARAMETER(data);
  314. UNUSED_PARAMETER(flags);
  315. return NULL;
  316. }
  317. gs_samplerstate_t *
  318. device_samplerstate_create(gs_device_t *device,
  319. const struct gs_sampler_info *info)
  320. {
  321. struct gs_sampler_state *sampler;
  322. sampler = bzalloc(sizeof(struct gs_sampler_state));
  323. sampler->device = device;
  324. sampler->ref = 1;
  325. convert_sampler_info(sampler, info);
  326. return sampler;
  327. }
  328. enum gs_texture_type device_get_texture_type(const gs_texture_t *texture)
  329. {
  330. return texture->type;
  331. }
  332. static void strip_mipmap_filter(GLint *filter)
  333. {
  334. switch (*filter) {
  335. case GL_NEAREST:
  336. case GL_LINEAR:
  337. return;
  338. case GL_NEAREST_MIPMAP_NEAREST:
  339. case GL_NEAREST_MIPMAP_LINEAR:
  340. *filter = GL_NEAREST;
  341. return;
  342. case GL_LINEAR_MIPMAP_NEAREST:
  343. case GL_LINEAR_MIPMAP_LINEAR:
  344. *filter = GL_LINEAR;
  345. return;
  346. }
  347. *filter = GL_NEAREST;
  348. }
  349. static inline void apply_swizzle(struct gs_texture *tex)
  350. {
  351. if (tex->format == GS_A8) {
  352. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_R, GL_ONE);
  353. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_G, GL_ONE);
  354. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_B, GL_ONE);
  355. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_A, GL_RED);
  356. }
  357. }
  358. static bool load_texture_sampler(gs_texture_t *tex, gs_samplerstate_t *ss)
  359. {
  360. bool success = true;
  361. GLint min_filter;
  362. if (tex->cur_sampler == ss)
  363. return true;
  364. if (tex->cur_sampler)
  365. samplerstate_release(tex->cur_sampler);
  366. tex->cur_sampler = ss;
  367. if (!ss)
  368. return true;
  369. samplerstate_addref(ss);
  370. min_filter = ss->min_filter;
  371. if (gs_texture_is_rect(tex))
  372. strip_mipmap_filter(&min_filter);
  373. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MIN_FILTER, min_filter))
  374. success = false;
  375. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAG_FILTER,
  376. ss->mag_filter))
  377. success = false;
  378. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_S, ss->address_u))
  379. success = false;
  380. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_T, ss->address_v))
  381. success = false;
  382. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_R, ss->address_w))
  383. success = false;
  384. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  385. ss->max_anisotropy))
  386. success = false;
  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_scene(gs_device_t *device)
  741. {
  742. clear_textures(device);
  743. }
  744. static inline bool can_render(const gs_device_t *device, uint32_t num_verts)
  745. {
  746. if (!device->cur_vertex_shader) {
  747. blog(LOG_ERROR, "No vertex shader specified");
  748. return false;
  749. }
  750. if (!device->cur_pixel_shader) {
  751. blog(LOG_ERROR, "No pixel shader specified");
  752. return false;
  753. }
  754. if (!device->cur_vertex_buffer && (num_verts == 0)) {
  755. blog(LOG_ERROR, "No vertex buffer specified");
  756. return false;
  757. }
  758. if (!device->cur_swap && !device->cur_render_target) {
  759. blog(LOG_ERROR, "No active swap chain or render target");
  760. return false;
  761. }
  762. return true;
  763. }
  764. static void update_viewproj_matrix(struct gs_device *device)
  765. {
  766. struct gs_shader *vs = device->cur_vertex_shader;
  767. struct matrix4 cur_proj;
  768. gs_matrix_get(&device->cur_view);
  769. matrix4_copy(&cur_proj, &device->cur_proj);
  770. if (device->cur_fbo) {
  771. cur_proj.x.y = -cur_proj.x.y;
  772. cur_proj.y.y = -cur_proj.y.y;
  773. cur_proj.z.y = -cur_proj.z.y;
  774. cur_proj.t.y = -cur_proj.t.y;
  775. glFrontFace(GL_CW);
  776. } else {
  777. glFrontFace(GL_CCW);
  778. }
  779. gl_success("glFrontFace");
  780. matrix4_mul(&device->cur_viewproj, &device->cur_view, &cur_proj);
  781. matrix4_transpose(&device->cur_viewproj, &device->cur_viewproj);
  782. if (vs->viewproj)
  783. gs_shader_set_matrix4(vs->viewproj, &device->cur_viewproj);
  784. }
  785. static inline struct gs_program *find_program(const struct gs_device *device)
  786. {
  787. struct gs_program *program = device->first_program;
  788. while (program) {
  789. if (program->vertex_shader == device->cur_vertex_shader &&
  790. program->pixel_shader == device->cur_pixel_shader)
  791. return program;
  792. program = program->next;
  793. }
  794. return NULL;
  795. }
  796. static inline struct gs_program *get_shader_program(struct gs_device *device)
  797. {
  798. struct gs_program *program = find_program(device);
  799. if (!program)
  800. program = gs_program_create(device);
  801. return program;
  802. }
  803. void device_draw(gs_device_t *device, enum gs_draw_mode draw_mode,
  804. uint32_t start_vert, uint32_t num_verts)
  805. {
  806. struct gs_vertex_buffer *vb = device->cur_vertex_buffer;
  807. struct gs_index_buffer *ib = device->cur_index_buffer;
  808. GLenum topology = convert_gs_topology(draw_mode);
  809. gs_effect_t *effect = gs_get_effect();
  810. struct gs_program *program;
  811. if (!can_render(device, num_verts))
  812. goto fail;
  813. if (effect)
  814. gs_effect_update_params(effect);
  815. program = get_shader_program(device);
  816. if (!program)
  817. goto fail;
  818. if (vb)
  819. load_vb_buffers(program, vb, ib);
  820. else
  821. gl_bind_vertex_array(device->empty_vao);
  822. if (program != device->cur_program && device->cur_program) {
  823. glUseProgram(0);
  824. gl_success("glUseProgram (zero)");
  825. }
  826. if (program != device->cur_program) {
  827. device->cur_program = program;
  828. glUseProgram(program->obj);
  829. if (!gl_success("glUseProgram"))
  830. goto fail;
  831. }
  832. update_viewproj_matrix(device);
  833. program_update_params(program);
  834. if (ib) {
  835. if (num_verts == 0)
  836. num_verts = (uint32_t)device->cur_index_buffer->num;
  837. glDrawElements(topology, num_verts, ib->gl_type,
  838. (const GLvoid *)(start_vert * ib->width));
  839. if (!gl_success("glDrawElements"))
  840. goto fail;
  841. } else {
  842. if (num_verts == 0)
  843. num_verts = (uint32_t)device->cur_vertex_buffer->num;
  844. glDrawArrays(topology, start_vert, num_verts);
  845. if (!gl_success("glDrawArrays"))
  846. goto fail;
  847. }
  848. return;
  849. fail:
  850. blog(LOG_ERROR, "device_draw (GL) failed");
  851. }
  852. void device_end_scene(gs_device_t *device)
  853. {
  854. /* does nothing */
  855. UNUSED_PARAMETER(device);
  856. }
  857. void device_clear(gs_device_t *device, uint32_t clear_flags,
  858. const struct vec4 *color, float depth, uint8_t stencil)
  859. {
  860. GLbitfield gl_flags = 0;
  861. if (clear_flags & GS_CLEAR_COLOR) {
  862. glClearColor(color->x, color->y, color->z, color->w);
  863. gl_flags |= GL_COLOR_BUFFER_BIT;
  864. }
  865. if (clear_flags & GS_CLEAR_DEPTH) {
  866. glClearDepth(depth);
  867. gl_flags |= GL_DEPTH_BUFFER_BIT;
  868. }
  869. if (clear_flags & GS_CLEAR_STENCIL) {
  870. glClearStencil(stencil);
  871. gl_flags |= GL_STENCIL_BUFFER_BIT;
  872. }
  873. glClear(gl_flags);
  874. if (!gl_success("glClear"))
  875. blog(LOG_ERROR, "device_clear (GL) failed");
  876. UNUSED_PARAMETER(device);
  877. }
  878. void device_flush(gs_device_t *device)
  879. {
  880. #ifdef __APPLE__
  881. if (!device->cur_swap)
  882. glFlush();
  883. #else
  884. glFlush();
  885. #endif
  886. UNUSED_PARAMETER(device);
  887. }
  888. void device_set_cull_mode(gs_device_t *device, enum gs_cull_mode mode)
  889. {
  890. if (device->cur_cull_mode == mode)
  891. return;
  892. if (device->cur_cull_mode == GS_NEITHER)
  893. gl_enable(GL_CULL_FACE);
  894. device->cur_cull_mode = mode;
  895. if (mode == GS_BACK)
  896. gl_cull_face(GL_BACK);
  897. else if (mode == GS_FRONT)
  898. gl_cull_face(GL_FRONT);
  899. else
  900. gl_disable(GL_CULL_FACE);
  901. }
  902. enum gs_cull_mode device_get_cull_mode(const gs_device_t *device)
  903. {
  904. return device->cur_cull_mode;
  905. }
  906. void device_enable_blending(gs_device_t *device, bool enable)
  907. {
  908. if (enable)
  909. gl_enable(GL_BLEND);
  910. else
  911. gl_disable(GL_BLEND);
  912. UNUSED_PARAMETER(device);
  913. }
  914. void device_enable_depth_test(gs_device_t *device, bool enable)
  915. {
  916. if (enable)
  917. gl_enable(GL_DEPTH_TEST);
  918. else
  919. gl_disable(GL_DEPTH_TEST);
  920. UNUSED_PARAMETER(device);
  921. }
  922. void device_enable_stencil_test(gs_device_t *device, bool enable)
  923. {
  924. if (enable)
  925. gl_enable(GL_STENCIL_TEST);
  926. else
  927. gl_disable(GL_STENCIL_TEST);
  928. UNUSED_PARAMETER(device);
  929. }
  930. void device_enable_stencil_write(gs_device_t *device, bool enable)
  931. {
  932. if (enable)
  933. glStencilMask(0xFFFFFFFF);
  934. else
  935. glStencilMask(0);
  936. UNUSED_PARAMETER(device);
  937. }
  938. void device_enable_color(gs_device_t *device, bool red, bool green, bool blue,
  939. bool alpha)
  940. {
  941. glColorMask(red, green, blue, alpha);
  942. UNUSED_PARAMETER(device);
  943. }
  944. void device_blend_function(gs_device_t *device, enum gs_blend_type src,
  945. enum gs_blend_type dest)
  946. {
  947. GLenum gl_src = convert_gs_blend_type(src);
  948. GLenum gl_dst = convert_gs_blend_type(dest);
  949. glBlendFunc(gl_src, gl_dst);
  950. if (!gl_success("glBlendFunc"))
  951. blog(LOG_ERROR, "device_blend_function (GL) failed");
  952. UNUSED_PARAMETER(device);
  953. }
  954. void device_blend_function_separate(gs_device_t *device,
  955. enum gs_blend_type src_c,
  956. enum gs_blend_type dest_c,
  957. enum gs_blend_type src_a,
  958. enum gs_blend_type dest_a)
  959. {
  960. GLenum gl_src_c = convert_gs_blend_type(src_c);
  961. GLenum gl_dst_c = convert_gs_blend_type(dest_c);
  962. GLenum gl_src_a = convert_gs_blend_type(src_a);
  963. GLenum gl_dst_a = convert_gs_blend_type(dest_a);
  964. glBlendFuncSeparate(gl_src_c, gl_dst_c, gl_src_a, gl_dst_a);
  965. if (!gl_success("glBlendFuncSeparate"))
  966. blog(LOG_ERROR, "device_blend_function_separate (GL) failed");
  967. UNUSED_PARAMETER(device);
  968. }
  969. void device_depth_function(gs_device_t *device, enum gs_depth_test test)
  970. {
  971. GLenum gl_test = convert_gs_depth_test(test);
  972. glDepthFunc(gl_test);
  973. if (!gl_success("glDepthFunc"))
  974. blog(LOG_ERROR, "device_depth_function (GL) failed");
  975. UNUSED_PARAMETER(device);
  976. }
  977. void device_stencil_function(gs_device_t *device, enum gs_stencil_side side,
  978. enum gs_depth_test test)
  979. {
  980. GLenum gl_side = convert_gs_stencil_side(side);
  981. GLenum gl_test = convert_gs_depth_test(test);
  982. glStencilFuncSeparate(gl_side, gl_test, 0, 0xFFFFFFFF);
  983. if (!gl_success("glStencilFuncSeparate"))
  984. blog(LOG_ERROR, "device_stencil_function (GL) failed");
  985. UNUSED_PARAMETER(device);
  986. }
  987. void device_stencil_op(gs_device_t *device, enum gs_stencil_side side,
  988. enum gs_stencil_op_type fail,
  989. enum gs_stencil_op_type zfail,
  990. enum gs_stencil_op_type zpass)
  991. {
  992. GLenum gl_side = convert_gs_stencil_side(side);
  993. GLenum gl_fail = convert_gs_stencil_op(fail);
  994. GLenum gl_zfail = convert_gs_stencil_op(zfail);
  995. GLenum gl_zpass = convert_gs_stencil_op(zpass);
  996. glStencilOpSeparate(gl_side, gl_fail, gl_zfail, gl_zpass);
  997. if (!gl_success("glStencilOpSeparate"))
  998. blog(LOG_ERROR, "device_stencil_op (GL) failed");
  999. UNUSED_PARAMETER(device);
  1000. }
  1001. static inline uint32_t get_target_height(const struct gs_device *device)
  1002. {
  1003. if (!device->cur_render_target)
  1004. return device_get_height(device);
  1005. if (device->cur_render_target->type == GS_TEXTURE_2D)
  1006. return gs_texture_get_height(device->cur_render_target);
  1007. else /* cube map */
  1008. return gs_cubetexture_get_size(device->cur_render_target);
  1009. }
  1010. void device_set_viewport(gs_device_t *device, int x, int y, int width,
  1011. int height)
  1012. {
  1013. uint32_t base_height = 0;
  1014. int gl_y = 0;
  1015. /* GL uses bottom-up coordinates for viewports. We want top-down */
  1016. if (device->cur_render_target) {
  1017. base_height = get_target_height(device);
  1018. } else if (device->cur_swap) {
  1019. uint32_t dw;
  1020. gl_getclientsize(device->cur_swap, &dw, &base_height);
  1021. }
  1022. if (base_height)
  1023. gl_y = base_height - y - height;
  1024. glViewport(x, gl_y, width, height);
  1025. if (!gl_success("glViewport"))
  1026. blog(LOG_ERROR, "device_set_viewport (GL) failed");
  1027. device->cur_viewport.x = x;
  1028. device->cur_viewport.y = y;
  1029. device->cur_viewport.cx = width;
  1030. device->cur_viewport.cy = height;
  1031. }
  1032. void device_get_viewport(const gs_device_t *device, struct gs_rect *rect)
  1033. {
  1034. *rect = device->cur_viewport;
  1035. }
  1036. void device_set_scissor_rect(gs_device_t *device, const struct gs_rect *rect)
  1037. {
  1038. UNUSED_PARAMETER(device);
  1039. if (rect != NULL) {
  1040. glScissor(rect->x, rect->y, rect->cx, rect->cy);
  1041. if (gl_success("glScissor") && gl_enable(GL_SCISSOR_TEST))
  1042. return;
  1043. } else if (gl_disable(GL_SCISSOR_TEST)) {
  1044. return;
  1045. }
  1046. blog(LOG_ERROR, "device_set_scissor_rect (GL) failed");
  1047. }
  1048. void device_ortho(gs_device_t *device, float left, float right, float top,
  1049. float bottom, float near, float far)
  1050. {
  1051. struct matrix4 *dst = &device->cur_proj;
  1052. float rml = right - left;
  1053. float bmt = bottom - top;
  1054. float fmn = far - near;
  1055. vec4_zero(&dst->x);
  1056. vec4_zero(&dst->y);
  1057. vec4_zero(&dst->z);
  1058. vec4_zero(&dst->t);
  1059. dst->x.x = 2.0f / rml;
  1060. dst->t.x = (left + right) / -rml;
  1061. dst->y.y = 2.0f / -bmt;
  1062. dst->t.y = (bottom + top) / bmt;
  1063. dst->z.z = -2.0f / fmn;
  1064. dst->t.z = (far + near) / -fmn;
  1065. dst->t.w = 1.0f;
  1066. }
  1067. void device_frustum(gs_device_t *device, float left, float right, float top,
  1068. float bottom, float near, float far)
  1069. {
  1070. struct matrix4 *dst = &device->cur_proj;
  1071. float rml = right - left;
  1072. float tmb = top - bottom;
  1073. float nmf = near - far;
  1074. float nearx2 = 2.0f * near;
  1075. vec4_zero(&dst->x);
  1076. vec4_zero(&dst->y);
  1077. vec4_zero(&dst->z);
  1078. vec4_zero(&dst->t);
  1079. dst->x.x = nearx2 / rml;
  1080. dst->z.x = (left + right) / rml;
  1081. dst->y.y = nearx2 / tmb;
  1082. dst->z.y = (bottom + top) / tmb;
  1083. dst->z.z = (far + near) / nmf;
  1084. dst->t.z = 2.0f * (near * far) / nmf;
  1085. dst->z.w = -1.0f;
  1086. }
  1087. void device_projection_push(gs_device_t *device)
  1088. {
  1089. da_push_back(device->proj_stack, &device->cur_proj);
  1090. }
  1091. void device_projection_pop(gs_device_t *device)
  1092. {
  1093. struct matrix4 *end;
  1094. if (!device->proj_stack.num)
  1095. return;
  1096. end = da_end(device->proj_stack);
  1097. device->cur_proj = *end;
  1098. da_pop_back(device->proj_stack);
  1099. }
  1100. void device_debug_marker_begin(gs_device_t *device, const char *markername,
  1101. const float color[4])
  1102. {
  1103. UNUSED_PARAMETER(device);
  1104. UNUSED_PARAMETER(color);
  1105. glPushDebugGroupKHR(GL_DEBUG_SOURCE_APPLICATION, 0, -1, markername);
  1106. }
  1107. void device_debug_marker_end(gs_device_t *device)
  1108. {
  1109. UNUSED_PARAMETER(device);
  1110. glPopDebugGroupKHR();
  1111. }
  1112. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1113. {
  1114. if (!swapchain)
  1115. return;
  1116. if (swapchain->device->cur_swap == swapchain)
  1117. device_load_swapchain(swapchain->device, NULL);
  1118. gl_platform_cleanup_swapchain(swapchain);
  1119. gl_windowinfo_destroy(swapchain->wi);
  1120. bfree(swapchain);
  1121. }
  1122. void gs_voltexture_destroy(gs_texture_t *voltex)
  1123. {
  1124. /* TODO */
  1125. UNUSED_PARAMETER(voltex);
  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. }