gl-subsystem.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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. device_leave_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_texture_t *device_voltexture_create(gs_device_t *device, uint32_t width,
  297. uint32_t height, uint32_t depth,
  298. enum gs_color_format color_format,
  299. uint32_t levels, const uint8_t **data,
  300. uint32_t flags)
  301. {
  302. /* TODO */
  303. UNUSED_PARAMETER(device);
  304. UNUSED_PARAMETER(width);
  305. UNUSED_PARAMETER(height);
  306. UNUSED_PARAMETER(depth);
  307. UNUSED_PARAMETER(color_format);
  308. UNUSED_PARAMETER(levels);
  309. UNUSED_PARAMETER(data);
  310. UNUSED_PARAMETER(flags);
  311. return NULL;
  312. }
  313. gs_samplerstate_t *
  314. device_samplerstate_create(gs_device_t *device,
  315. const struct gs_sampler_info *info)
  316. {
  317. struct gs_sampler_state *sampler;
  318. sampler = bzalloc(sizeof(struct gs_sampler_state));
  319. sampler->device = device;
  320. sampler->ref = 1;
  321. convert_sampler_info(sampler, info);
  322. return sampler;
  323. }
  324. gs_timer_t *device_timer_create(gs_device_t *device)
  325. {
  326. UNUSED_PARAMETER(device);
  327. struct gs_timer *timer;
  328. GLuint queries[2];
  329. glGenQueries(2, queries);
  330. if (!gl_success("glGenQueries"))
  331. return NULL;
  332. timer = bzalloc(sizeof(struct gs_timer));
  333. timer->queries[0] = queries[0];
  334. timer->queries[1] = queries[1];
  335. return timer;
  336. }
  337. gs_timer_range_t *device_timer_range_create(gs_device_t *device)
  338. {
  339. UNUSED_PARAMETER(device);
  340. return NULL;
  341. }
  342. enum gs_texture_type device_get_texture_type(const gs_texture_t *texture)
  343. {
  344. return texture->type;
  345. }
  346. static void strip_mipmap_filter(GLint *filter)
  347. {
  348. switch (*filter) {
  349. case GL_NEAREST:
  350. case GL_LINEAR:
  351. return;
  352. case GL_NEAREST_MIPMAP_NEAREST:
  353. case GL_NEAREST_MIPMAP_LINEAR:
  354. *filter = GL_NEAREST;
  355. return;
  356. case GL_LINEAR_MIPMAP_NEAREST:
  357. case GL_LINEAR_MIPMAP_LINEAR:
  358. *filter = GL_LINEAR;
  359. return;
  360. }
  361. *filter = GL_NEAREST;
  362. }
  363. static inline void apply_swizzle(struct gs_texture *tex)
  364. {
  365. if (tex->format == GS_A8) {
  366. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_R, GL_ONE);
  367. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_G, GL_ONE);
  368. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_B, GL_ONE);
  369. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_A, GL_RED);
  370. }
  371. }
  372. static bool load_texture_sampler(gs_texture_t *tex, gs_samplerstate_t *ss)
  373. {
  374. bool success = true;
  375. GLint min_filter;
  376. if (tex->cur_sampler == ss)
  377. return true;
  378. if (tex->cur_sampler)
  379. samplerstate_release(tex->cur_sampler);
  380. tex->cur_sampler = ss;
  381. if (!ss)
  382. return true;
  383. samplerstate_addref(ss);
  384. min_filter = ss->min_filter;
  385. if (gs_texture_is_rect(tex))
  386. strip_mipmap_filter(&min_filter);
  387. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MIN_FILTER, min_filter))
  388. success = false;
  389. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAG_FILTER,
  390. ss->mag_filter))
  391. success = false;
  392. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_S, ss->address_u))
  393. success = false;
  394. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_T, ss->address_v))
  395. success = false;
  396. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_R, ss->address_w))
  397. success = false;
  398. if (GLAD_GL_EXT_texture_filter_anisotropic) {
  399. if (!gl_tex_param_i(tex->gl_target,
  400. GL_TEXTURE_MAX_ANISOTROPY_EXT,
  401. ss->max_anisotropy))
  402. success = false;
  403. }
  404. apply_swizzle(tex);
  405. return success;
  406. }
  407. static inline struct gs_shader_param *get_texture_param(gs_device_t *device,
  408. int unit)
  409. {
  410. struct gs_shader *shader = device->cur_pixel_shader;
  411. size_t i;
  412. for (i = 0; i < shader->params.num; i++) {
  413. struct gs_shader_param *param = shader->params.array + i;
  414. if (param->type == GS_SHADER_PARAM_TEXTURE) {
  415. if (param->texture_id == unit)
  416. return param;
  417. }
  418. }
  419. return NULL;
  420. }
  421. void device_load_texture(gs_device_t *device, gs_texture_t *tex, int unit)
  422. {
  423. struct gs_shader_param *param;
  424. struct gs_sampler_state *sampler;
  425. struct gs_texture *cur_tex = device->cur_textures[unit];
  426. /* need a pixel shader to properly bind textures */
  427. if (!device->cur_pixel_shader)
  428. goto fail;
  429. if (cur_tex == tex)
  430. return;
  431. if (!gl_active_texture(GL_TEXTURE0 + unit))
  432. goto fail;
  433. /* the target for the previous text may not be the same as the
  434. * next texture, so unbind the previous texture first to be safe */
  435. if (cur_tex && (!tex || cur_tex->gl_target != tex->gl_target))
  436. gl_bind_texture(cur_tex->gl_target, 0);
  437. device->cur_textures[unit] = tex;
  438. param = get_texture_param(device, unit);
  439. if (!param)
  440. return;
  441. param->texture = tex;
  442. if (!tex)
  443. return;
  444. // texelFetch doesn't need a sampler
  445. if (param->sampler_id != (size_t)-1)
  446. sampler = device->cur_samplers[param->sampler_id];
  447. else
  448. sampler = NULL;
  449. if (!gl_bind_texture(tex->gl_target, tex->texture))
  450. goto fail;
  451. if (sampler && !load_texture_sampler(tex, sampler))
  452. goto fail;
  453. return;
  454. fail:
  455. blog(LOG_ERROR, "device_load_texture (GL) failed");
  456. }
  457. static bool load_sampler_on_textures(gs_device_t *device, gs_samplerstate_t *ss,
  458. int sampler_unit)
  459. {
  460. struct gs_shader *shader = device->cur_pixel_shader;
  461. size_t i;
  462. for (i = 0; i < shader->params.num; i++) {
  463. struct gs_shader_param *param = shader->params.array + i;
  464. if (param->type == GS_SHADER_PARAM_TEXTURE &&
  465. param->sampler_id == (uint32_t)sampler_unit &&
  466. param->texture) {
  467. if (!gl_active_texture(GL_TEXTURE0 + param->texture_id))
  468. return false;
  469. if (!load_texture_sampler(param->texture, ss))
  470. return false;
  471. }
  472. }
  473. return true;
  474. }
  475. void device_load_samplerstate(gs_device_t *device, gs_samplerstate_t *ss,
  476. int unit)
  477. {
  478. /* need a pixel shader to properly bind samplers */
  479. if (!device->cur_pixel_shader)
  480. ss = NULL;
  481. if (device->cur_samplers[unit] == ss)
  482. return;
  483. device->cur_samplers[unit] = ss;
  484. if (!ss)
  485. return;
  486. if (!load_sampler_on_textures(device, ss, unit))
  487. blog(LOG_ERROR, "device_load_samplerstate (GL) failed");
  488. return;
  489. }
  490. void device_load_vertexshader(gs_device_t *device, gs_shader_t *vertshader)
  491. {
  492. if (device->cur_vertex_shader == vertshader)
  493. return;
  494. if (vertshader && vertshader->type != GS_SHADER_VERTEX) {
  495. blog(LOG_ERROR, "Specified shader is not a vertex shader");
  496. blog(LOG_ERROR, "device_load_vertexshader (GL) failed");
  497. return;
  498. }
  499. device->cur_vertex_shader = vertshader;
  500. }
  501. static void load_default_pixelshader_samplers(struct gs_device *device,
  502. struct gs_shader *ps)
  503. {
  504. size_t i;
  505. if (!ps)
  506. return;
  507. for (i = 0; i < ps->samplers.num; i++) {
  508. struct gs_sampler_state *ss = ps->samplers.array[i];
  509. device->cur_samplers[i] = ss;
  510. }
  511. for (; i < GS_MAX_TEXTURES; i++)
  512. device->cur_samplers[i] = NULL;
  513. }
  514. void device_load_pixelshader(gs_device_t *device, gs_shader_t *pixelshader)
  515. {
  516. if (device->cur_pixel_shader == pixelshader)
  517. return;
  518. if (pixelshader && pixelshader->type != GS_SHADER_PIXEL) {
  519. blog(LOG_ERROR, "Specified shader is not a pixel shader");
  520. goto fail;
  521. }
  522. device->cur_pixel_shader = pixelshader;
  523. clear_textures(device);
  524. if (pixelshader)
  525. load_default_pixelshader_samplers(device, pixelshader);
  526. return;
  527. fail:
  528. blog(LOG_ERROR, "device_load_pixelshader (GL) failed");
  529. }
  530. void device_load_default_samplerstate(gs_device_t *device, bool b_3d, int unit)
  531. {
  532. /* TODO */
  533. UNUSED_PARAMETER(device);
  534. UNUSED_PARAMETER(b_3d);
  535. UNUSED_PARAMETER(unit);
  536. }
  537. gs_shader_t *device_get_vertex_shader(const gs_device_t *device)
  538. {
  539. return device->cur_vertex_shader;
  540. }
  541. gs_shader_t *device_get_pixel_shader(const gs_device_t *device)
  542. {
  543. return device->cur_pixel_shader;
  544. }
  545. gs_texture_t *device_get_render_target(const gs_device_t *device)
  546. {
  547. return device->cur_render_target;
  548. }
  549. gs_zstencil_t *device_get_zstencil_target(const gs_device_t *device)
  550. {
  551. return device->cur_zstencil_buffer;
  552. }
  553. static bool get_tex_dimensions(gs_texture_t *tex, uint32_t *width,
  554. uint32_t *height)
  555. {
  556. if (tex->type == GS_TEXTURE_2D) {
  557. struct gs_texture_2d *tex2d = (struct gs_texture_2d *)tex;
  558. *width = tex2d->width;
  559. *height = tex2d->height;
  560. return true;
  561. } else if (tex->type == GS_TEXTURE_CUBE) {
  562. struct gs_texture_cube *cube = (struct gs_texture_cube *)tex;
  563. *width = cube->size;
  564. *height = cube->size;
  565. return true;
  566. }
  567. blog(LOG_ERROR, "Texture must be 2D or cubemap");
  568. return false;
  569. }
  570. /*
  571. * This automatically manages FBOs so that render targets are always given
  572. * an FBO that matches their width/height/format to maximize optimization
  573. */
  574. struct fbo_info *get_fbo(gs_texture_t *tex, uint32_t width, uint32_t height)
  575. {
  576. if (tex->fbo && tex->fbo->width == width &&
  577. tex->fbo->height == height && tex->fbo->format == tex->format)
  578. return tex->fbo;
  579. GLuint fbo;
  580. glGenFramebuffers(1, &fbo);
  581. if (!gl_success("glGenFramebuffers"))
  582. return NULL;
  583. tex->fbo = bmalloc(sizeof(struct fbo_info));
  584. tex->fbo->fbo = fbo;
  585. tex->fbo->width = width;
  586. tex->fbo->height = height;
  587. tex->fbo->format = tex->format;
  588. tex->fbo->cur_render_target = NULL;
  589. tex->fbo->cur_render_side = 0;
  590. tex->fbo->cur_zstencil_buffer = NULL;
  591. return tex->fbo;
  592. }
  593. static inline struct fbo_info *get_fbo_by_tex(gs_texture_t *tex)
  594. {
  595. uint32_t width, height;
  596. if (!get_tex_dimensions(tex, &width, &height))
  597. return NULL;
  598. return get_fbo(tex, width, height);
  599. }
  600. static bool set_current_fbo(gs_device_t *device, struct fbo_info *fbo)
  601. {
  602. if (device->cur_fbo != fbo) {
  603. GLuint fbo_obj = fbo ? fbo->fbo : 0;
  604. if (!gl_bind_framebuffer(GL_DRAW_FRAMEBUFFER, fbo_obj))
  605. return false;
  606. if (device->cur_fbo) {
  607. device->cur_fbo->cur_render_target = NULL;
  608. device->cur_fbo->cur_zstencil_buffer = NULL;
  609. }
  610. }
  611. device->cur_fbo = fbo;
  612. return true;
  613. }
  614. static bool attach_rendertarget(struct fbo_info *fbo, gs_texture_t *tex,
  615. int side)
  616. {
  617. if (fbo->cur_render_target == tex)
  618. return true;
  619. fbo->cur_render_target = tex;
  620. if (tex->type == GS_TEXTURE_2D) {
  621. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
  622. GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  623. tex->texture, 0);
  624. } else if (tex->type == GS_TEXTURE_CUBE) {
  625. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
  626. GL_COLOR_ATTACHMENT0,
  627. GL_TEXTURE_CUBE_MAP_POSITIVE_X + side,
  628. tex->texture, 0);
  629. } else {
  630. return false;
  631. }
  632. return gl_success("glFramebufferTexture2D");
  633. }
  634. static bool attach_zstencil(struct fbo_info *fbo, gs_zstencil_t *zs)
  635. {
  636. GLuint zsbuffer = 0;
  637. GLenum zs_attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  638. if (fbo->cur_zstencil_buffer == zs)
  639. return true;
  640. fbo->cur_zstencil_buffer = zs;
  641. if (zs) {
  642. zsbuffer = zs->buffer;
  643. zs_attachment = zs->attachment;
  644. }
  645. glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, zs_attachment,
  646. GL_RENDERBUFFER, zsbuffer);
  647. if (!gl_success("glFramebufferRenderbuffer"))
  648. return false;
  649. return true;
  650. }
  651. static bool set_target(gs_device_t *device, gs_texture_t *tex, int side,
  652. gs_zstencil_t *zs)
  653. {
  654. struct fbo_info *fbo;
  655. if (device->cur_render_target == tex &&
  656. device->cur_zstencil_buffer == zs &&
  657. device->cur_render_side == side)
  658. return true;
  659. device->cur_render_target = tex;
  660. device->cur_render_side = side;
  661. device->cur_zstencil_buffer = zs;
  662. if (!tex)
  663. return set_current_fbo(device, NULL);
  664. fbo = get_fbo_by_tex(tex);
  665. if (!fbo)
  666. return false;
  667. set_current_fbo(device, fbo);
  668. if (!attach_rendertarget(fbo, tex, side))
  669. return false;
  670. if (!attach_zstencil(fbo, zs))
  671. return false;
  672. return true;
  673. }
  674. void device_set_render_target(gs_device_t *device, gs_texture_t *tex,
  675. gs_zstencil_t *zstencil)
  676. {
  677. if (tex) {
  678. if (tex->type != GS_TEXTURE_2D) {
  679. blog(LOG_ERROR, "Texture is not a 2D texture");
  680. goto fail;
  681. }
  682. if (!tex->is_render_target) {
  683. blog(LOG_ERROR, "Texture is not a render target");
  684. goto fail;
  685. }
  686. }
  687. if (!set_target(device, tex, 0, zstencil))
  688. goto fail;
  689. return;
  690. fail:
  691. blog(LOG_ERROR, "device_set_render_target (GL) failed");
  692. }
  693. void device_set_cube_render_target(gs_device_t *device, gs_texture_t *cubetex,
  694. int side, gs_zstencil_t *zstencil)
  695. {
  696. if (cubetex) {
  697. if (cubetex->type != GS_TEXTURE_CUBE) {
  698. blog(LOG_ERROR, "Texture is not a cube texture");
  699. goto fail;
  700. }
  701. if (!cubetex->is_render_target) {
  702. blog(LOG_ERROR, "Texture is not a render target");
  703. goto fail;
  704. }
  705. }
  706. if (!set_target(device, cubetex, side, zstencil))
  707. goto fail;
  708. return;
  709. fail:
  710. blog(LOG_ERROR, "device_set_cube_render_target (GL) failed");
  711. }
  712. void device_copy_texture_region(gs_device_t *device, gs_texture_t *dst,
  713. uint32_t dst_x, uint32_t dst_y,
  714. gs_texture_t *src, uint32_t src_x,
  715. uint32_t src_y, uint32_t src_w, uint32_t src_h)
  716. {
  717. struct gs_texture_2d *src2d = (struct gs_texture_2d *)src;
  718. struct gs_texture_2d *dst2d = (struct gs_texture_2d *)dst;
  719. if (!src) {
  720. blog(LOG_ERROR, "Source texture is NULL");
  721. goto fail;
  722. }
  723. if (!dst) {
  724. blog(LOG_ERROR, "Destination texture is NULL");
  725. goto fail;
  726. }
  727. if (dst->type != GS_TEXTURE_2D || src->type != GS_TEXTURE_2D) {
  728. blog(LOG_ERROR, "Source and destination textures must be 2D "
  729. "textures");
  730. goto fail;
  731. }
  732. if (dst->format != src->format) {
  733. blog(LOG_ERROR, "Source and destination formats do not match");
  734. goto fail;
  735. }
  736. uint32_t nw = (uint32_t)src_w ? (uint32_t)src_w
  737. : (src2d->width - src_x);
  738. uint32_t nh = (uint32_t)src_h ? (uint32_t)src_h
  739. : (src2d->height - src_y);
  740. if (dst2d->width - dst_x < nw || dst2d->height - dst_y < nh) {
  741. blog(LOG_ERROR, "Destination texture region is not big "
  742. "enough to hold the source region");
  743. goto fail;
  744. }
  745. if (!gl_copy_texture(device, dst, dst_x, dst_y, src, src_x, src_y, nw,
  746. nh))
  747. goto fail;
  748. return;
  749. fail:
  750. blog(LOG_ERROR, "device_copy_texture (GL) failed");
  751. }
  752. void device_copy_texture(gs_device_t *device, gs_texture_t *dst,
  753. gs_texture_t *src)
  754. {
  755. device_copy_texture_region(device, dst, 0, 0, src, 0, 0, 0, 0);
  756. }
  757. void device_begin_frame(gs_device_t *device)
  758. {
  759. /* does nothing */
  760. UNUSED_PARAMETER(device);
  761. }
  762. void device_begin_scene(gs_device_t *device)
  763. {
  764. clear_textures(device);
  765. }
  766. static inline bool can_render(const gs_device_t *device, uint32_t num_verts)
  767. {
  768. if (!device->cur_vertex_shader) {
  769. blog(LOG_ERROR, "No vertex shader specified");
  770. return false;
  771. }
  772. if (!device->cur_pixel_shader) {
  773. blog(LOG_ERROR, "No pixel shader specified");
  774. return false;
  775. }
  776. if (!device->cur_vertex_buffer && (num_verts == 0)) {
  777. blog(LOG_ERROR, "No vertex buffer specified");
  778. return false;
  779. }
  780. if (!device->cur_swap && !device->cur_render_target) {
  781. blog(LOG_ERROR, "No active swap chain or render target");
  782. return false;
  783. }
  784. return true;
  785. }
  786. static void update_viewproj_matrix(struct gs_device *device)
  787. {
  788. struct gs_shader *vs = device->cur_vertex_shader;
  789. struct matrix4 cur_proj;
  790. gs_matrix_get(&device->cur_view);
  791. matrix4_copy(&cur_proj, &device->cur_proj);
  792. if (device->cur_fbo) {
  793. cur_proj.x.y = -cur_proj.x.y;
  794. cur_proj.y.y = -cur_proj.y.y;
  795. cur_proj.z.y = -cur_proj.z.y;
  796. cur_proj.t.y = -cur_proj.t.y;
  797. glFrontFace(GL_CW);
  798. } else {
  799. glFrontFace(GL_CCW);
  800. }
  801. gl_success("glFrontFace");
  802. matrix4_mul(&device->cur_viewproj, &device->cur_view, &cur_proj);
  803. matrix4_transpose(&device->cur_viewproj, &device->cur_viewproj);
  804. if (vs->viewproj)
  805. gs_shader_set_matrix4(vs->viewproj, &device->cur_viewproj);
  806. }
  807. static inline struct gs_program *find_program(const struct gs_device *device)
  808. {
  809. struct gs_program *program = device->first_program;
  810. while (program) {
  811. if (program->vertex_shader == device->cur_vertex_shader &&
  812. program->pixel_shader == device->cur_pixel_shader)
  813. return program;
  814. program = program->next;
  815. }
  816. return NULL;
  817. }
  818. static inline struct gs_program *get_shader_program(struct gs_device *device)
  819. {
  820. struct gs_program *program = find_program(device);
  821. if (!program)
  822. program = gs_program_create(device);
  823. return program;
  824. }
  825. void device_draw(gs_device_t *device, enum gs_draw_mode draw_mode,
  826. uint32_t start_vert, uint32_t num_verts)
  827. {
  828. struct gs_vertex_buffer *vb = device->cur_vertex_buffer;
  829. struct gs_index_buffer *ib = device->cur_index_buffer;
  830. GLenum topology = convert_gs_topology(draw_mode);
  831. gs_effect_t *effect = gs_get_effect();
  832. struct gs_program *program;
  833. if (!can_render(device, num_verts))
  834. goto fail;
  835. if (effect)
  836. gs_effect_update_params(effect);
  837. program = get_shader_program(device);
  838. if (!program)
  839. goto fail;
  840. if (vb)
  841. load_vb_buffers(program, vb, ib);
  842. else
  843. gl_bind_vertex_array(device->empty_vao);
  844. if (program != device->cur_program && device->cur_program) {
  845. glUseProgram(0);
  846. gl_success("glUseProgram (zero)");
  847. }
  848. if (program != device->cur_program) {
  849. device->cur_program = program;
  850. glUseProgram(program->obj);
  851. if (!gl_success("glUseProgram"))
  852. goto fail;
  853. }
  854. update_viewproj_matrix(device);
  855. program_update_params(program);
  856. if (ib) {
  857. if (num_verts == 0)
  858. num_verts = (uint32_t)device->cur_index_buffer->num;
  859. glDrawElements(topology, num_verts, ib->gl_type,
  860. (const GLvoid *)(start_vert * ib->width));
  861. if (!gl_success("glDrawElements"))
  862. goto fail;
  863. } else {
  864. if (num_verts == 0)
  865. num_verts = (uint32_t)device->cur_vertex_buffer->num;
  866. glDrawArrays(topology, start_vert, num_verts);
  867. if (!gl_success("glDrawArrays"))
  868. goto fail;
  869. }
  870. return;
  871. fail:
  872. blog(LOG_ERROR, "device_draw (GL) failed");
  873. }
  874. void device_end_scene(gs_device_t *device)
  875. {
  876. /* does nothing */
  877. UNUSED_PARAMETER(device);
  878. }
  879. void device_clear(gs_device_t *device, uint32_t clear_flags,
  880. const struct vec4 *color, float depth, uint8_t stencil)
  881. {
  882. GLbitfield gl_flags = 0;
  883. if (clear_flags & GS_CLEAR_COLOR) {
  884. glClearColor(color->x, color->y, color->z, color->w);
  885. gl_flags |= GL_COLOR_BUFFER_BIT;
  886. }
  887. if (clear_flags & GS_CLEAR_DEPTH) {
  888. glClearDepth(depth);
  889. gl_flags |= GL_DEPTH_BUFFER_BIT;
  890. }
  891. if (clear_flags & GS_CLEAR_STENCIL) {
  892. glClearStencil(stencil);
  893. gl_flags |= GL_STENCIL_BUFFER_BIT;
  894. }
  895. glClear(gl_flags);
  896. if (!gl_success("glClear"))
  897. blog(LOG_ERROR, "device_clear (GL) failed");
  898. UNUSED_PARAMETER(device);
  899. }
  900. void device_flush(gs_device_t *device)
  901. {
  902. #ifdef __APPLE__
  903. if (!device->cur_swap)
  904. glFlush();
  905. #else
  906. glFlush();
  907. #endif
  908. UNUSED_PARAMETER(device);
  909. }
  910. void device_set_cull_mode(gs_device_t *device, enum gs_cull_mode mode)
  911. {
  912. if (device->cur_cull_mode == mode)
  913. return;
  914. if (device->cur_cull_mode == GS_NEITHER)
  915. gl_enable(GL_CULL_FACE);
  916. device->cur_cull_mode = mode;
  917. if (mode == GS_BACK)
  918. gl_cull_face(GL_BACK);
  919. else if (mode == GS_FRONT)
  920. gl_cull_face(GL_FRONT);
  921. else
  922. gl_disable(GL_CULL_FACE);
  923. }
  924. enum gs_cull_mode device_get_cull_mode(const gs_device_t *device)
  925. {
  926. return device->cur_cull_mode;
  927. }
  928. void device_enable_blending(gs_device_t *device, bool enable)
  929. {
  930. if (enable)
  931. gl_enable(GL_BLEND);
  932. else
  933. gl_disable(GL_BLEND);
  934. UNUSED_PARAMETER(device);
  935. }
  936. void device_enable_depth_test(gs_device_t *device, bool enable)
  937. {
  938. if (enable)
  939. gl_enable(GL_DEPTH_TEST);
  940. else
  941. gl_disable(GL_DEPTH_TEST);
  942. UNUSED_PARAMETER(device);
  943. }
  944. void device_enable_stencil_test(gs_device_t *device, bool enable)
  945. {
  946. if (enable)
  947. gl_enable(GL_STENCIL_TEST);
  948. else
  949. gl_disable(GL_STENCIL_TEST);
  950. UNUSED_PARAMETER(device);
  951. }
  952. void device_enable_stencil_write(gs_device_t *device, bool enable)
  953. {
  954. if (enable)
  955. glStencilMask(0xFFFFFFFF);
  956. else
  957. glStencilMask(0);
  958. UNUSED_PARAMETER(device);
  959. }
  960. void device_enable_color(gs_device_t *device, bool red, bool green, bool blue,
  961. bool alpha)
  962. {
  963. glColorMask(red, green, blue, alpha);
  964. UNUSED_PARAMETER(device);
  965. }
  966. void device_blend_function(gs_device_t *device, enum gs_blend_type src,
  967. enum gs_blend_type dest)
  968. {
  969. GLenum gl_src = convert_gs_blend_type(src);
  970. GLenum gl_dst = convert_gs_blend_type(dest);
  971. glBlendFunc(gl_src, gl_dst);
  972. if (!gl_success("glBlendFunc"))
  973. blog(LOG_ERROR, "device_blend_function (GL) failed");
  974. UNUSED_PARAMETER(device);
  975. }
  976. void device_blend_function_separate(gs_device_t *device,
  977. enum gs_blend_type src_c,
  978. enum gs_blend_type dest_c,
  979. enum gs_blend_type src_a,
  980. enum gs_blend_type dest_a)
  981. {
  982. GLenum gl_src_c = convert_gs_blend_type(src_c);
  983. GLenum gl_dst_c = convert_gs_blend_type(dest_c);
  984. GLenum gl_src_a = convert_gs_blend_type(src_a);
  985. GLenum gl_dst_a = convert_gs_blend_type(dest_a);
  986. glBlendFuncSeparate(gl_src_c, gl_dst_c, gl_src_a, gl_dst_a);
  987. if (!gl_success("glBlendFuncSeparate"))
  988. blog(LOG_ERROR, "device_blend_function_separate (GL) failed");
  989. UNUSED_PARAMETER(device);
  990. }
  991. void device_depth_function(gs_device_t *device, enum gs_depth_test test)
  992. {
  993. GLenum gl_test = convert_gs_depth_test(test);
  994. glDepthFunc(gl_test);
  995. if (!gl_success("glDepthFunc"))
  996. blog(LOG_ERROR, "device_depth_function (GL) failed");
  997. UNUSED_PARAMETER(device);
  998. }
  999. void device_stencil_function(gs_device_t *device, enum gs_stencil_side side,
  1000. enum gs_depth_test test)
  1001. {
  1002. GLenum gl_side = convert_gs_stencil_side(side);
  1003. GLenum gl_test = convert_gs_depth_test(test);
  1004. glStencilFuncSeparate(gl_side, gl_test, 0, 0xFFFFFFFF);
  1005. if (!gl_success("glStencilFuncSeparate"))
  1006. blog(LOG_ERROR, "device_stencil_function (GL) failed");
  1007. UNUSED_PARAMETER(device);
  1008. }
  1009. void device_stencil_op(gs_device_t *device, enum gs_stencil_side side,
  1010. enum gs_stencil_op_type fail,
  1011. enum gs_stencil_op_type zfail,
  1012. enum gs_stencil_op_type zpass)
  1013. {
  1014. GLenum gl_side = convert_gs_stencil_side(side);
  1015. GLenum gl_fail = convert_gs_stencil_op(fail);
  1016. GLenum gl_zfail = convert_gs_stencil_op(zfail);
  1017. GLenum gl_zpass = convert_gs_stencil_op(zpass);
  1018. glStencilOpSeparate(gl_side, gl_fail, gl_zfail, gl_zpass);
  1019. if (!gl_success("glStencilOpSeparate"))
  1020. blog(LOG_ERROR, "device_stencil_op (GL) failed");
  1021. UNUSED_PARAMETER(device);
  1022. }
  1023. static inline uint32_t get_target_height(const struct gs_device *device)
  1024. {
  1025. if (!device->cur_render_target)
  1026. return device_get_height(device);
  1027. if (device->cur_render_target->type == GS_TEXTURE_2D)
  1028. return gs_texture_get_height(device->cur_render_target);
  1029. else /* cube map */
  1030. return gs_cubetexture_get_size(device->cur_render_target);
  1031. }
  1032. void device_set_viewport(gs_device_t *device, int x, int y, int width,
  1033. int height)
  1034. {
  1035. uint32_t base_height = 0;
  1036. int gl_y = 0;
  1037. /* GL uses bottom-up coordinates for viewports. We want top-down */
  1038. if (device->cur_render_target) {
  1039. base_height = get_target_height(device);
  1040. } else if (device->cur_swap) {
  1041. uint32_t dw;
  1042. gl_getclientsize(device->cur_swap, &dw, &base_height);
  1043. }
  1044. if (base_height)
  1045. gl_y = base_height - y - height;
  1046. glViewport(x, gl_y, width, height);
  1047. if (!gl_success("glViewport"))
  1048. blog(LOG_ERROR, "device_set_viewport (GL) failed");
  1049. device->cur_viewport.x = x;
  1050. device->cur_viewport.y = y;
  1051. device->cur_viewport.cx = width;
  1052. device->cur_viewport.cy = height;
  1053. }
  1054. void device_get_viewport(const gs_device_t *device, struct gs_rect *rect)
  1055. {
  1056. *rect = device->cur_viewport;
  1057. }
  1058. void device_set_scissor_rect(gs_device_t *device, const struct gs_rect *rect)
  1059. {
  1060. UNUSED_PARAMETER(device);
  1061. if (rect != NULL) {
  1062. glScissor(rect->x, rect->y, rect->cx, rect->cy);
  1063. if (gl_success("glScissor") && gl_enable(GL_SCISSOR_TEST))
  1064. return;
  1065. } else if (gl_disable(GL_SCISSOR_TEST)) {
  1066. return;
  1067. }
  1068. blog(LOG_ERROR, "device_set_scissor_rect (GL) failed");
  1069. }
  1070. void device_ortho(gs_device_t *device, float left, float right, float top,
  1071. float bottom, float near, float far)
  1072. {
  1073. struct matrix4 *dst = &device->cur_proj;
  1074. float rml = right - left;
  1075. float bmt = bottom - top;
  1076. float fmn = far - near;
  1077. vec4_zero(&dst->x);
  1078. vec4_zero(&dst->y);
  1079. vec4_zero(&dst->z);
  1080. vec4_zero(&dst->t);
  1081. dst->x.x = 2.0f / rml;
  1082. dst->t.x = (left + right) / -rml;
  1083. dst->y.y = 2.0f / -bmt;
  1084. dst->t.y = (bottom + top) / bmt;
  1085. dst->z.z = -2.0f / fmn;
  1086. dst->t.z = (far + near) / -fmn;
  1087. dst->t.w = 1.0f;
  1088. }
  1089. void device_frustum(gs_device_t *device, float left, float right, float top,
  1090. float bottom, float near, float far)
  1091. {
  1092. struct matrix4 *dst = &device->cur_proj;
  1093. float rml = right - left;
  1094. float tmb = top - bottom;
  1095. float nmf = near - far;
  1096. float nearx2 = 2.0f * near;
  1097. vec4_zero(&dst->x);
  1098. vec4_zero(&dst->y);
  1099. vec4_zero(&dst->z);
  1100. vec4_zero(&dst->t);
  1101. dst->x.x = nearx2 / rml;
  1102. dst->z.x = (left + right) / rml;
  1103. dst->y.y = nearx2 / tmb;
  1104. dst->z.y = (bottom + top) / tmb;
  1105. dst->z.z = (far + near) / nmf;
  1106. dst->t.z = 2.0f * (near * far) / nmf;
  1107. dst->z.w = -1.0f;
  1108. }
  1109. void device_projection_push(gs_device_t *device)
  1110. {
  1111. da_push_back(device->proj_stack, &device->cur_proj);
  1112. }
  1113. void device_projection_pop(gs_device_t *device)
  1114. {
  1115. struct matrix4 *end;
  1116. if (!device->proj_stack.num)
  1117. return;
  1118. end = da_end(device->proj_stack);
  1119. device->cur_proj = *end;
  1120. da_pop_back(device->proj_stack);
  1121. }
  1122. void device_debug_marker_begin(gs_device_t *device, const char *markername,
  1123. const float color[4])
  1124. {
  1125. UNUSED_PARAMETER(device);
  1126. UNUSED_PARAMETER(color);
  1127. glPushDebugGroupKHR(GL_DEBUG_SOURCE_APPLICATION, 0, -1, markername);
  1128. }
  1129. void device_debug_marker_end(gs_device_t *device)
  1130. {
  1131. UNUSED_PARAMETER(device);
  1132. glPopDebugGroupKHR();
  1133. }
  1134. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  1135. {
  1136. if (!swapchain)
  1137. return;
  1138. if (swapchain->device->cur_swap == swapchain)
  1139. device_load_swapchain(swapchain->device, NULL);
  1140. gl_platform_cleanup_swapchain(swapchain);
  1141. gl_windowinfo_destroy(swapchain->wi);
  1142. bfree(swapchain);
  1143. }
  1144. void gs_voltexture_destroy(gs_texture_t *voltex)
  1145. {
  1146. /* TODO */
  1147. UNUSED_PARAMETER(voltex);
  1148. }
  1149. uint32_t gs_voltexture_get_width(const gs_texture_t *voltex)
  1150. {
  1151. /* TODO */
  1152. UNUSED_PARAMETER(voltex);
  1153. return 0;
  1154. }
  1155. uint32_t gs_voltexture_get_height(const gs_texture_t *voltex)
  1156. {
  1157. /* TODO */
  1158. UNUSED_PARAMETER(voltex);
  1159. return 0;
  1160. }
  1161. uint32_t gs_voltexture_get_depth(const gs_texture_t *voltex)
  1162. {
  1163. /* TODO */
  1164. UNUSED_PARAMETER(voltex);
  1165. return 0;
  1166. }
  1167. enum gs_color_format gs_voltexture_get_color_format(const gs_texture_t *voltex)
  1168. {
  1169. /* TODO */
  1170. UNUSED_PARAMETER(voltex);
  1171. return GS_UNKNOWN;
  1172. }
  1173. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1174. {
  1175. if (!samplerstate)
  1176. return;
  1177. if (samplerstate->device)
  1178. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1179. if (samplerstate->device->cur_samplers[i] ==
  1180. samplerstate)
  1181. samplerstate->device->cur_samplers[i] = NULL;
  1182. samplerstate_release(samplerstate);
  1183. }
  1184. void gs_timer_destroy(gs_timer_t *timer)
  1185. {
  1186. if (!timer)
  1187. return;
  1188. glDeleteQueries(2, timer->queries);
  1189. gl_success("glDeleteQueries");
  1190. bfree(timer);
  1191. }
  1192. void gs_timer_begin(gs_timer_t *timer)
  1193. {
  1194. glQueryCounter(timer->queries[0], GL_TIMESTAMP);
  1195. gl_success("glQueryCounter");
  1196. }
  1197. void gs_timer_end(gs_timer_t *timer)
  1198. {
  1199. glQueryCounter(timer->queries[1], GL_TIMESTAMP);
  1200. gl_success("glQueryCounter");
  1201. }
  1202. bool gs_timer_get_data(gs_timer_t *timer, uint64_t *ticks)
  1203. {
  1204. GLint available = 0;
  1205. glGetQueryObjectiv(timer->queries[1], GL_QUERY_RESULT_AVAILABLE,
  1206. &available);
  1207. GLuint64 begin, end;
  1208. glGetQueryObjectui64v(timer->queries[0], GL_QUERY_RESULT, &begin);
  1209. gl_success("glGetQueryObjectui64v");
  1210. glGetQueryObjectui64v(timer->queries[1], GL_QUERY_RESULT, &end);
  1211. gl_success("glGetQueryObjectui64v");
  1212. *ticks = end - begin;
  1213. return true;
  1214. }
  1215. void gs_timer_range_destroy(gs_timer_range_t *range)
  1216. {
  1217. UNUSED_PARAMETER(range);
  1218. }
  1219. void gs_timer_range_begin(gs_timer_range_t *range)
  1220. {
  1221. UNUSED_PARAMETER(range);
  1222. }
  1223. void gs_timer_range_end(gs_timer_range_t *range)
  1224. {
  1225. UNUSED_PARAMETER(range);
  1226. }
  1227. bool gs_timer_range_get_data(gs_timer_range_t *range, bool *disjoint,
  1228. uint64_t *frequency)
  1229. {
  1230. UNUSED_PARAMETER(range);
  1231. *disjoint = false;
  1232. *frequency = 1000000000;
  1233. return true;
  1234. }