gl-subsystem.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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. #ifdef _DEBUG
  21. static void APIENTRY gl_debug_proc(
  22. GLenum source, GLenum type, GLuint id, GLenum severity,
  23. GLsizei length, const GLchar *message, const GLvoid *data )
  24. {
  25. UNUSED_PARAMETER(id);
  26. UNUSED_PARAMETER(data);
  27. char *source_str, *type_str, *severity_str;
  28. switch(source) {
  29. case GL_DEBUG_SOURCE_API:
  30. source_str = "API"; break;
  31. case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
  32. source_str = "Window System"; break;
  33. case GL_DEBUG_SOURCE_SHADER_COMPILER:
  34. source_str = "Shader Compiler"; break;
  35. case GL_DEBUG_SOURCE_THIRD_PARTY:
  36. source_str = "Third Party"; break;
  37. case GL_DEBUG_SOURCE_APPLICATION:
  38. source_str = "Application"; break;
  39. case GL_DEBUG_SOURCE_OTHER:
  40. source_str = "Other"; break;
  41. default:
  42. source_str = "Unknown";
  43. }
  44. switch(type) {
  45. case GL_DEBUG_TYPE_ERROR:
  46. type_str = "Error"; break;
  47. case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
  48. type_str = "Deprecated Behavior"; break;
  49. case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
  50. type_str = "Undefined Behavior"; break;
  51. case GL_DEBUG_TYPE_PORTABILITY:
  52. type_str = "Portability"; break;
  53. case GL_DEBUG_TYPE_PERFORMANCE:
  54. type_str = "Performance"; break;
  55. case GL_DEBUG_TYPE_OTHER:
  56. type_str = "Other"; break;
  57. default:
  58. type_str = "Unknown";
  59. }
  60. switch(severity) {
  61. case GL_DEBUG_SEVERITY_HIGH:
  62. severity_str = "High"; break;
  63. case GL_DEBUG_SEVERITY_MEDIUM:
  64. severity_str = "Medium"; break;
  65. case GL_DEBUG_SEVERITY_LOW:
  66. severity_str = "Low"; break;
  67. case GL_DEBUG_SEVERITY_NOTIFICATION:
  68. severity_str = "Notification"; break;
  69. default:
  70. severity_str = "Unknown";
  71. }
  72. blog(LOG_DEBUG,
  73. "[%s][%s]{%s}: %.*s",
  74. source_str, type_str, severity_str,
  75. length, message
  76. );
  77. }
  78. static void gl_enable_debug()
  79. {
  80. if (GLAD_GL_VERSION_4_3) {
  81. glDebugMessageCallback(gl_debug_proc, NULL);
  82. gl_enable(GL_DEBUG_OUTPUT);
  83. } else if (GLAD_GL_ARB_debug_output) {
  84. glDebugMessageCallbackARB(gl_debug_proc, NULL);
  85. } else {
  86. blog(LOG_DEBUG, "Failed to set GL debug callback as it is "
  87. "not supported.");
  88. }
  89. }
  90. #else
  91. static void gl_enable_debug() {}
  92. #endif
  93. static bool gl_init_extensions(struct gs_device* device)
  94. {
  95. if (!GLAD_GL_VERSION_2_1) {
  96. blog(LOG_ERROR, "obs-studio requires OpenGL version 2.1 or "
  97. "higher.");
  98. return false;
  99. }
  100. gl_enable_debug();
  101. if (!GLAD_GL_VERSION_3_0 && !GLAD_GL_ARB_framebuffer_object) {
  102. blog(LOG_ERROR, "OpenGL extension ARB_framebuffer_object "
  103. "is required.");
  104. return false;
  105. }
  106. if (GLAD_GL_VERSION_3_2 || GLAD_GL_ARB_seamless_cube_map) {
  107. gl_enable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  108. }
  109. if (GLAD_GL_VERSION_4_3 || GLAD_GL_ARB_copy_image)
  110. device->copy_type = COPY_TYPE_ARB;
  111. else if (GLAD_GL_NV_copy_image)
  112. device->copy_type = COPY_TYPE_NV;
  113. else
  114. device->copy_type = COPY_TYPE_FBO_BLIT;
  115. return true;
  116. }
  117. static void clear_textures(struct gs_device *device)
  118. {
  119. GLenum i;
  120. for (i = 0; i < GS_MAX_TEXTURES; i++) {
  121. if (device->cur_textures[i]) {
  122. gl_active_texture(GL_TEXTURE0 + i);
  123. gl_bind_texture(device->cur_textures[i]->gl_target, 0);
  124. device->cur_textures[i] = NULL;
  125. }
  126. }
  127. }
  128. void convert_sampler_info(struct gs_sampler_state *sampler,
  129. struct gs_sampler_info *info)
  130. {
  131. GLint max_anisotropy_max;
  132. convert_filter(info->filter, &sampler->min_filter,
  133. &sampler->mag_filter);
  134. sampler->address_u = convert_address_mode(info->address_u);
  135. sampler->address_v = convert_address_mode(info->address_v);
  136. sampler->address_w = convert_address_mode(info->address_w);
  137. sampler->max_anisotropy = info->max_anisotropy;
  138. max_anisotropy_max = 1;
  139. glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy_max);
  140. gl_success("glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX)");
  141. if (1 <= sampler->max_anisotropy &&
  142. sampler->max_anisotropy <= max_anisotropy_max)
  143. return;
  144. if (sampler->max_anisotropy < 1)
  145. sampler->max_anisotropy = 1;
  146. else if (sampler->max_anisotropy > max_anisotropy_max)
  147. sampler->max_anisotropy = max_anisotropy_max;
  148. blog(LOG_INFO, "convert_sampler_info: 1 <= max_anisotropy <= "
  149. "%d violated, selected: %d, set: %d",
  150. max_anisotropy_max,
  151. info->max_anisotropy, sampler->max_anisotropy);
  152. }
  153. const char *device_get_name(void)
  154. {
  155. return "OpenGL";
  156. }
  157. int device_get_type(void)
  158. {
  159. return GS_DEVICE_OPENGL;
  160. }
  161. const char *device_preprocessor_name(void)
  162. {
  163. return "_OPENGL";
  164. }
  165. int device_create(gs_device_t **p_device, struct gs_init_data *info)
  166. {
  167. struct gs_device *device = bzalloc(sizeof(struct gs_device));
  168. int errorcode = GS_ERROR_FAIL;
  169. device->plat = gl_platform_create(device, info);
  170. if (!device->plat)
  171. goto fail;
  172. if (!gl_init_extensions(device)) {
  173. errorcode = GS_ERROR_NOT_SUPPORTED;
  174. goto fail;
  175. }
  176. gl_enable(GL_CULL_FACE);
  177. device_leave_context(device);
  178. device->cur_swap = gl_platform_getswap(device->plat);
  179. *p_device = device;
  180. return GS_SUCCESS;
  181. fail:
  182. blog(LOG_ERROR, "device_create (GL) failed");
  183. bfree(device);
  184. *p_device = NULL;
  185. return errorcode;
  186. }
  187. void device_destroy(gs_device_t *device)
  188. {
  189. if (device) {
  190. size_t i;
  191. for (i = 0; i < device->fbos.num; i++)
  192. fbo_info_destroy(device->fbos.array[i]);
  193. while (device->first_program)
  194. gs_program_destroy(device->first_program);
  195. da_free(device->proj_stack);
  196. da_free(device->fbos);
  197. gl_platform_destroy(device->plat);
  198. bfree(device);
  199. }
  200. }
  201. gs_swapchain_t *device_swapchain_create(gs_device_t *device,
  202. struct gs_init_data *info)
  203. {
  204. struct gs_swap_chain *swap = bzalloc(sizeof(struct gs_swap_chain));
  205. swap->device = device;
  206. swap->info = *info;
  207. swap->wi = gl_windowinfo_create(info);
  208. if (!swap->wi) {
  209. blog(LOG_ERROR, "device_swapchain_create (GL) failed");
  210. gs_swapchain_destroy(swap);
  211. return NULL;
  212. }
  213. if (!gl_platform_init_swapchain(swap)) {
  214. blog(LOG_ERROR, "gl_platform_init_swapchain failed");
  215. gs_swapchain_destroy(swap);
  216. return NULL;
  217. }
  218. return swap;
  219. }
  220. void device_resize(gs_device_t *device, uint32_t cx, uint32_t cy)
  221. {
  222. /* GL automatically resizes the device, so it doesn't do much */
  223. device->cur_swap->info.cx = cx;
  224. device->cur_swap->info.cy = cy;
  225. gl_update(device);
  226. }
  227. void device_get_size(gs_device_t *device, uint32_t *cx, uint32_t *cy)
  228. {
  229. *cx = device->cur_swap->info.cx;
  230. *cy = device->cur_swap->info.cy;
  231. }
  232. uint32_t device_get_width(gs_device_t *device)
  233. {
  234. return device->cur_swap->info.cx;
  235. }
  236. uint32_t device_get_height(gs_device_t *device)
  237. {
  238. return device->cur_swap->info.cy;
  239. }
  240. gs_texture_t *device_voltexture_create(gs_device_t *device, uint32_t width,
  241. uint32_t height, uint32_t depth,
  242. enum gs_color_format color_format, uint32_t levels,
  243. const uint8_t **data, uint32_t flags)
  244. {
  245. /* TODO */
  246. UNUSED_PARAMETER(device);
  247. UNUSED_PARAMETER(width);
  248. UNUSED_PARAMETER(height);
  249. UNUSED_PARAMETER(depth);
  250. UNUSED_PARAMETER(color_format);
  251. UNUSED_PARAMETER(levels);
  252. UNUSED_PARAMETER(data);
  253. UNUSED_PARAMETER(flags);
  254. return NULL;
  255. }
  256. gs_samplerstate_t *device_samplerstate_create(gs_device_t *device,
  257. struct gs_sampler_info *info)
  258. {
  259. struct gs_sampler_state *sampler;
  260. sampler = bzalloc(sizeof(struct gs_sampler_state));
  261. sampler->device = device;
  262. sampler->ref = 1;
  263. convert_sampler_info(sampler, info);
  264. return sampler;
  265. }
  266. enum gs_texture_type device_get_texture_type(gs_texture_t *texture)
  267. {
  268. return texture->type;
  269. }
  270. static void strip_mipmap_filter(GLint *filter)
  271. {
  272. switch (*filter) {
  273. case GL_NEAREST:
  274. case GL_LINEAR:
  275. return;
  276. case GL_NEAREST_MIPMAP_NEAREST:
  277. case GL_NEAREST_MIPMAP_LINEAR:
  278. *filter = GL_NEAREST;
  279. return;
  280. case GL_LINEAR_MIPMAP_NEAREST:
  281. case GL_LINEAR_MIPMAP_LINEAR:
  282. *filter = GL_LINEAR;
  283. return;
  284. }
  285. *filter = GL_NEAREST;
  286. }
  287. static inline void apply_swizzle(struct gs_texture *tex)
  288. {
  289. if (tex->format == GS_A8) {
  290. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_R, GL_ONE);
  291. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_G, GL_ONE);
  292. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_B, GL_ONE);
  293. gl_tex_param_i(tex->gl_target, GL_TEXTURE_SWIZZLE_A, GL_RED);
  294. }
  295. }
  296. static bool load_texture_sampler(gs_texture_t *tex, gs_samplerstate_t *ss)
  297. {
  298. bool success = true;
  299. GLint min_filter;
  300. if (tex->cur_sampler == ss)
  301. return true;
  302. if (tex->cur_sampler)
  303. samplerstate_release(tex->cur_sampler);
  304. tex->cur_sampler = ss;
  305. if (!ss)
  306. return true;
  307. samplerstate_addref(ss);
  308. min_filter = ss->min_filter;
  309. if (gs_texture_is_rect(tex))
  310. strip_mipmap_filter(&min_filter);
  311. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MIN_FILTER,
  312. min_filter))
  313. success = false;
  314. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAG_FILTER,
  315. ss->mag_filter))
  316. success = false;
  317. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_S, ss->address_u))
  318. success = false;
  319. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_T, ss->address_v))
  320. success = false;
  321. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_WRAP_R, ss->address_w))
  322. success = false;
  323. if (!gl_tex_param_i(tex->gl_target, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  324. ss->max_anisotropy))
  325. success = false;
  326. apply_swizzle(tex);
  327. return success;
  328. }
  329. static inline struct gs_shader_param *get_texture_param(gs_device_t *device,
  330. int unit)
  331. {
  332. struct gs_shader *shader = device->cur_pixel_shader;
  333. size_t i;
  334. for (i = 0; i < shader->params.num; i++) {
  335. struct gs_shader_param *param = shader->params.array+i;
  336. if (param->type == GS_SHADER_PARAM_TEXTURE) {
  337. if (param->texture_id == unit)
  338. return param;
  339. }
  340. }
  341. return NULL;
  342. }
  343. void device_load_texture(gs_device_t *device, gs_texture_t *tex, int unit)
  344. {
  345. struct gs_shader_param *param;
  346. struct gs_sampler_state *sampler;
  347. struct gs_texture *cur_tex = device->cur_textures[unit];
  348. /* need a pixel shader to properly bind textures */
  349. if (!device->cur_pixel_shader)
  350. tex = NULL;
  351. if (cur_tex == tex)
  352. return;
  353. if (!gl_active_texture(GL_TEXTURE0 + unit))
  354. goto fail;
  355. /* the target for the previous text may not be the same as the
  356. * next texture, so unbind the previous texture first to be safe */
  357. if (cur_tex && (!tex || cur_tex->gl_target != tex->gl_target))
  358. gl_bind_texture(cur_tex->gl_target, 0);
  359. device->cur_textures[unit] = tex;
  360. param = get_texture_param(device, unit);
  361. if (!param)
  362. return;
  363. param->texture = tex;
  364. if (!tex)
  365. return;
  366. sampler = device->cur_samplers[param->sampler_id];
  367. if (!gl_bind_texture(tex->gl_target, tex->texture))
  368. goto fail;
  369. if (sampler && !load_texture_sampler(tex, sampler))
  370. goto fail;
  371. return;
  372. fail:
  373. blog(LOG_ERROR, "device_load_texture (GL) failed");
  374. }
  375. static bool load_sampler_on_textures(gs_device_t *device, gs_samplerstate_t *ss,
  376. int sampler_unit)
  377. {
  378. struct gs_shader *shader = device->cur_pixel_shader;
  379. size_t i;
  380. for (i = 0; i < shader->params.num; i++) {
  381. struct gs_shader_param *param = shader->params.array+i;
  382. if (param->type == GS_SHADER_PARAM_TEXTURE &&
  383. param->sampler_id == (uint32_t)sampler_unit &&
  384. param->texture) {
  385. if (!gl_active_texture(GL_TEXTURE0 + param->texture_id))
  386. return false;
  387. if (!load_texture_sampler(param->texture, ss))
  388. return false;
  389. }
  390. }
  391. return true;
  392. }
  393. void device_load_samplerstate(gs_device_t *device, gs_samplerstate_t *ss,
  394. int unit)
  395. {
  396. /* need a pixel shader to properly bind samplers */
  397. if (!device->cur_pixel_shader)
  398. ss = NULL;
  399. if (device->cur_samplers[unit] == ss)
  400. return;
  401. device->cur_samplers[unit] = ss;
  402. if (!ss)
  403. return;
  404. if (!load_sampler_on_textures(device, ss, unit))
  405. blog(LOG_ERROR, "device_load_samplerstate (GL) failed");
  406. return;
  407. }
  408. void device_load_vertexshader(gs_device_t *device, gs_shader_t *vertshader)
  409. {
  410. if (device->cur_vertex_shader == vertshader)
  411. return;
  412. if (vertshader && vertshader->type != GS_SHADER_VERTEX) {
  413. blog(LOG_ERROR, "Specified shader is not a vertex shader");
  414. blog(LOG_ERROR, "device_load_vertexshader (GL) failed");
  415. return;
  416. }
  417. device->cur_vertex_shader = vertshader;
  418. }
  419. static void load_default_pixelshader_samplers(struct gs_device *device,
  420. struct gs_shader *ps)
  421. {
  422. size_t i;
  423. if (!ps)
  424. return;
  425. for (i = 0; i < ps->samplers.num; i++) {
  426. struct gs_sampler_state *ss = ps->samplers.array[i];
  427. device->cur_samplers[i] = ss;
  428. }
  429. for (; i < GS_MAX_TEXTURES; i++)
  430. device->cur_samplers[i] = NULL;
  431. }
  432. void device_load_pixelshader(gs_device_t *device, gs_shader_t *pixelshader)
  433. {
  434. if (device->cur_pixel_shader == pixelshader)
  435. return;
  436. if (pixelshader && pixelshader->type != GS_SHADER_PIXEL) {
  437. blog(LOG_ERROR, "Specified shader is not a pixel shader");
  438. goto fail;
  439. }
  440. device->cur_pixel_shader = pixelshader;
  441. clear_textures(device);
  442. if (pixelshader)
  443. load_default_pixelshader_samplers(device, pixelshader);
  444. return;
  445. fail:
  446. blog(LOG_ERROR, "device_load_pixelshader (GL) failed");
  447. }
  448. void device_load_default_samplerstate(gs_device_t *device, bool b_3d, int unit)
  449. {
  450. /* TODO */
  451. UNUSED_PARAMETER(device);
  452. UNUSED_PARAMETER(b_3d);
  453. UNUSED_PARAMETER(unit);
  454. }
  455. gs_shader_t *device_get_vertex_shader(gs_device_t *device)
  456. {
  457. return device->cur_vertex_shader;
  458. }
  459. gs_shader_t *device_get_pixel_shader(gs_device_t *device)
  460. {
  461. return device->cur_pixel_shader;
  462. }
  463. gs_texture_t *device_get_render_target(gs_device_t *device)
  464. {
  465. return device->cur_render_target;
  466. }
  467. gs_zstencil_t *device_get_zstencil_target(gs_device_t *device)
  468. {
  469. return device->cur_zstencil_buffer;
  470. }
  471. static bool get_tex_dimensions(gs_texture_t *tex, uint32_t *width,
  472. uint32_t *height)
  473. {
  474. if (tex->type == GS_TEXTURE_2D) {
  475. struct gs_texture_2d *tex2d = (struct gs_texture_2d*)tex;
  476. *width = tex2d->width;
  477. *height = tex2d->height;
  478. return true;
  479. } else if (tex->type == GS_TEXTURE_CUBE) {
  480. struct gs_texture_cube *cube = (struct gs_texture_cube*)tex;
  481. *width = cube->size;
  482. *height = cube->size;
  483. return true;
  484. }
  485. blog(LOG_ERROR, "Texture must be 2D or cubemap");
  486. return false;
  487. }
  488. /*
  489. * This automatically manages FBOs so that render targets are always given
  490. * an FBO that matches their width/height/format to maximize optimization
  491. */
  492. struct fbo_info *get_fbo(struct gs_device *device,
  493. uint32_t width, uint32_t height, enum gs_color_format format)
  494. {
  495. size_t i;
  496. GLuint fbo;
  497. struct fbo_info *ptr;
  498. for (i = 0; i < device->fbos.num; i++) {
  499. ptr = device->fbos.array[i];
  500. if (ptr->width == width && ptr->height == height &&
  501. ptr->format == format)
  502. return ptr;
  503. }
  504. glGenFramebuffers(1, &fbo);
  505. if (!gl_success("glGenFramebuffers"))
  506. return NULL;
  507. ptr = bmalloc(sizeof(struct fbo_info));
  508. ptr->fbo = fbo;
  509. ptr->width = width;
  510. ptr->height = height;
  511. ptr->format = format;
  512. ptr->cur_render_target = NULL;
  513. ptr->cur_render_side = 0;
  514. ptr->cur_zstencil_buffer = NULL;
  515. da_push_back(device->fbos, &ptr);
  516. return ptr;
  517. }
  518. static inline struct fbo_info *get_fbo_by_tex(struct gs_device *device,
  519. gs_texture_t *tex)
  520. {
  521. uint32_t width, height;
  522. if (!get_tex_dimensions(tex, &width, &height))
  523. return NULL;
  524. return get_fbo(device, width, height, tex->format);
  525. }
  526. static bool set_current_fbo(gs_device_t *device, struct fbo_info *fbo)
  527. {
  528. if (device->cur_fbo != fbo) {
  529. GLuint fbo_obj = fbo ? fbo->fbo : 0;
  530. if (!gl_bind_framebuffer(GL_DRAW_FRAMEBUFFER, fbo_obj))
  531. return false;
  532. }
  533. device->cur_fbo = fbo;
  534. return true;
  535. }
  536. static bool attach_rendertarget(struct fbo_info *fbo, gs_texture_t *tex,
  537. int side)
  538. {
  539. if (fbo->cur_render_target == tex)
  540. return true;
  541. fbo->cur_render_target = tex;
  542. if (tex->type == GS_TEXTURE_2D) {
  543. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
  544. GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  545. tex->texture, 0);
  546. } else if (tex->type == GS_TEXTURE_CUBE) {
  547. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
  548. GL_COLOR_ATTACHMENT0,
  549. GL_TEXTURE_CUBE_MAP_POSITIVE_X + side,
  550. tex->texture, 0);
  551. } else {
  552. return false;
  553. }
  554. return gl_success("glFramebufferTexture2D");
  555. }
  556. static bool attach_zstencil(struct fbo_info *fbo, gs_zstencil_t *zs)
  557. {
  558. GLuint zsbuffer = 0;
  559. GLenum zs_attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  560. if (fbo->cur_zstencil_buffer == zs)
  561. return true;
  562. fbo->cur_zstencil_buffer = zs;
  563. if (zs) {
  564. zsbuffer = zs->buffer;
  565. zs_attachment = zs->attachment;
  566. }
  567. glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
  568. zs_attachment, GL_RENDERBUFFER, zsbuffer);
  569. if (!gl_success("glFramebufferRenderbuffer"))
  570. return false;
  571. return true;
  572. }
  573. static bool set_target(gs_device_t *device, gs_texture_t *tex, int side,
  574. gs_zstencil_t *zs)
  575. {
  576. struct fbo_info *fbo;
  577. if (device->cur_render_target == tex &&
  578. device->cur_zstencil_buffer == zs &&
  579. device->cur_render_side == side)
  580. return true;
  581. device->cur_render_target = tex;
  582. device->cur_render_side = side;
  583. device->cur_zstencil_buffer = zs;
  584. if (!tex)
  585. return set_current_fbo(device, NULL);
  586. fbo = get_fbo_by_tex(device, tex);
  587. if (!fbo)
  588. return false;
  589. set_current_fbo(device, fbo);
  590. if (!attach_rendertarget(fbo, tex, side))
  591. return false;
  592. if (!attach_zstencil(fbo, zs))
  593. return false;
  594. return true;
  595. }
  596. void device_set_render_target(gs_device_t *device, gs_texture_t *tex,
  597. gs_zstencil_t *zstencil)
  598. {
  599. if (tex) {
  600. if (tex->type != GS_TEXTURE_2D) {
  601. blog(LOG_ERROR, "Texture is not a 2D texture");
  602. goto fail;
  603. }
  604. if (!tex->is_render_target) {
  605. blog(LOG_ERROR, "Texture is not a render target");
  606. goto fail;
  607. }
  608. }
  609. if (!set_target(device, tex, 0, zstencil))
  610. goto fail;
  611. return;
  612. fail:
  613. blog(LOG_ERROR, "device_set_render_target (GL) failed");
  614. }
  615. void device_set_cube_render_target(gs_device_t *device, gs_texture_t *cubetex,
  616. int side, gs_zstencil_t *zstencil)
  617. {
  618. if (cubetex) {
  619. if (cubetex->type != GS_TEXTURE_CUBE) {
  620. blog(LOG_ERROR, "Texture is not a cube texture");
  621. goto fail;
  622. }
  623. if (!cubetex->is_render_target) {
  624. blog(LOG_ERROR, "Texture is not a render target");
  625. goto fail;
  626. }
  627. }
  628. if (!set_target(device, cubetex, side, zstencil))
  629. goto fail;
  630. return;
  631. fail:
  632. blog(LOG_ERROR, "device_set_cube_render_target (GL) failed");
  633. }
  634. void device_copy_texture_region(gs_device_t *device,
  635. gs_texture_t *dst, uint32_t dst_x, uint32_t dst_y,
  636. gs_texture_t *src, uint32_t src_x, uint32_t src_y,
  637. uint32_t src_w, uint32_t src_h)
  638. {
  639. struct gs_texture_2d *src2d = (struct gs_texture_2d*)src;
  640. struct gs_texture_2d *dst2d = (struct gs_texture_2d*)dst;
  641. if (!src) {
  642. blog(LOG_ERROR, "Source texture is NULL");
  643. goto fail;
  644. }
  645. if (!dst) {
  646. blog(LOG_ERROR, "Destination texture is NULL");
  647. goto fail;
  648. }
  649. if (dst->type != GS_TEXTURE_2D || src->type != GS_TEXTURE_2D) {
  650. blog(LOG_ERROR, "Source and destination textures must be 2D "
  651. "textures");
  652. goto fail;
  653. }
  654. if (dst->format != src->format) {
  655. blog(LOG_ERROR, "Source and destination formats do not match");
  656. goto fail;
  657. }
  658. uint32_t nw = (uint32_t)src_w ? (uint32_t)src_w : (src2d->width - src_x);
  659. uint32_t nh = (uint32_t)src_h ? (uint32_t)src_h : (src2d->height - src_y);
  660. if (dst2d->width - dst_x < nw || dst2d->height - dst_y < nh) {
  661. blog(LOG_ERROR, "Destination texture region is not big "
  662. "enough to hold the source region");
  663. goto fail;
  664. }
  665. if (!gl_copy_texture(device, dst->texture, dst->gl_target, dst_x, dst_y,
  666. src->texture, src->gl_target, src_x, src_y,
  667. nw, nh, src->format))
  668. goto fail;
  669. return;
  670. fail:
  671. blog(LOG_ERROR, "device_copy_texture (GL) failed");
  672. }
  673. void device_copy_texture(gs_device_t *device, gs_texture_t *dst,
  674. gs_texture_t *src)
  675. {
  676. device_copy_texture_region(device, dst, 0, 0, src, 0, 0, 0, 0);
  677. }
  678. void device_begin_scene(gs_device_t *device)
  679. {
  680. clear_textures(device);
  681. }
  682. static inline bool can_render(gs_device_t *device)
  683. {
  684. if (!device->cur_vertex_shader) {
  685. blog(LOG_ERROR, "No vertex shader specified");
  686. return false;
  687. }
  688. if (!device->cur_pixel_shader) {
  689. blog(LOG_ERROR, "No pixel shader specified");
  690. return false;
  691. }
  692. if (!device->cur_vertex_buffer) {
  693. blog(LOG_ERROR, "No vertex buffer specified");
  694. return false;
  695. }
  696. return true;
  697. }
  698. static void update_viewproj_matrix(struct gs_device *device)
  699. {
  700. struct gs_shader *vs = device->cur_vertex_shader;
  701. gs_matrix_get(&device->cur_view);
  702. matrix4_mul(&device->cur_viewproj, &device->cur_view,
  703. &device->cur_proj);
  704. matrix4_transpose(&device->cur_viewproj, &device->cur_viewproj);
  705. if (vs->viewproj)
  706. gs_shader_set_matrix4(vs->viewproj, &device->cur_viewproj);
  707. }
  708. static inline struct gs_program *find_program(struct gs_device *device)
  709. {
  710. struct gs_program *program = device->first_program;
  711. while (program) {
  712. if (program->vertex_shader == device->cur_vertex_shader &&
  713. program->pixel_shader == device->cur_pixel_shader)
  714. return program;
  715. program = program->next;
  716. }
  717. return NULL;
  718. }
  719. static inline struct gs_program *get_shader_program(struct gs_device *device)
  720. {
  721. struct gs_program *program = find_program(device);
  722. if (!program)
  723. program = gs_program_create(device);
  724. return program;
  725. }
  726. void device_draw(gs_device_t *device, enum gs_draw_mode draw_mode,
  727. uint32_t start_vert, uint32_t num_verts)
  728. {
  729. struct gs_index_buffer *ib = device->cur_index_buffer;
  730. GLenum topology = convert_gs_topology(draw_mode);
  731. gs_effect_t *effect = gs_get_effect();
  732. struct gs_program *program;
  733. if (!can_render(device))
  734. goto fail;
  735. if (effect)
  736. gs_effect_update_params(effect);
  737. program = get_shader_program(device);
  738. if (!program)
  739. goto fail;
  740. load_vb_buffers(program, device->cur_vertex_buffer);
  741. if (program != device->cur_program && device->cur_program) {
  742. glUseProgram(0);
  743. gl_success("glUseProgram (zero)");
  744. }
  745. if (program != device->cur_program) {
  746. device->cur_program = program;
  747. glUseProgram(program->obj);
  748. if (!gl_success("glUseProgram"))
  749. goto fail;
  750. }
  751. update_viewproj_matrix(device);
  752. program_update_params(program);
  753. if (ib) {
  754. if (num_verts == 0)
  755. num_verts = (uint32_t)device->cur_index_buffer->num;
  756. glDrawElements(topology, num_verts, ib->gl_type,
  757. (const GLvoid*)(start_vert * ib->width));
  758. if (!gl_success("glDrawElements"))
  759. goto fail;
  760. } else {
  761. if (num_verts == 0)
  762. num_verts = (uint32_t)device->cur_vertex_buffer->num;
  763. glDrawArrays(topology, start_vert, num_verts);
  764. if (!gl_success("glDrawArrays"))
  765. goto fail;
  766. }
  767. return;
  768. fail:
  769. blog(LOG_ERROR, "device_draw (GL) failed");
  770. }
  771. void device_end_scene(gs_device_t *device)
  772. {
  773. /* does nothing */
  774. UNUSED_PARAMETER(device);
  775. }
  776. void device_clear(gs_device_t *device, uint32_t clear_flags,
  777. struct vec4 *color, float depth, uint8_t stencil)
  778. {
  779. GLbitfield gl_flags = 0;
  780. if (clear_flags & GS_CLEAR_COLOR) {
  781. glClearColor(color->x, color->y, color->z, color->w);
  782. gl_flags |= GL_COLOR_BUFFER_BIT;
  783. }
  784. if (clear_flags & GS_CLEAR_DEPTH) {
  785. glClearDepth(depth);
  786. gl_flags |= GL_DEPTH_BUFFER_BIT;
  787. }
  788. if (clear_flags & GS_CLEAR_STENCIL) {
  789. glClearStencil(stencil);
  790. gl_flags |= GL_STENCIL_BUFFER_BIT;
  791. }
  792. glClear(gl_flags);
  793. if (!gl_success("glClear"))
  794. blog(LOG_ERROR, "device_clear (GL) failed");
  795. UNUSED_PARAMETER(device);
  796. }
  797. void device_flush(gs_device_t *device)
  798. {
  799. glFlush();
  800. UNUSED_PARAMETER(device);
  801. }
  802. void device_set_cull_mode(gs_device_t *device, enum gs_cull_mode mode)
  803. {
  804. if (device->cur_cull_mode == mode)
  805. return;
  806. if (device->cur_cull_mode == GS_NEITHER)
  807. gl_enable(GL_CULL_FACE);
  808. device->cur_cull_mode = mode;
  809. if (mode == GS_BACK)
  810. gl_cull_face(GL_BACK);
  811. else if (mode == GS_FRONT)
  812. gl_cull_face(GL_FRONT);
  813. else
  814. gl_disable(GL_CULL_FACE);
  815. }
  816. enum gs_cull_mode device_get_cull_mode(gs_device_t *device)
  817. {
  818. return device->cur_cull_mode;
  819. }
  820. void device_enable_blending(gs_device_t *device, bool enable)
  821. {
  822. if (enable)
  823. gl_enable(GL_BLEND);
  824. else
  825. gl_disable(GL_BLEND);
  826. UNUSED_PARAMETER(device);
  827. }
  828. void device_enable_depth_test(gs_device_t *device, bool enable)
  829. {
  830. if (enable)
  831. gl_enable(GL_DEPTH_TEST);
  832. else
  833. gl_disable(GL_DEPTH_TEST);
  834. UNUSED_PARAMETER(device);
  835. }
  836. void device_enable_stencil_test(gs_device_t *device, bool enable)
  837. {
  838. if (enable)
  839. gl_enable(GL_STENCIL_TEST);
  840. else
  841. gl_disable(GL_STENCIL_TEST);
  842. UNUSED_PARAMETER(device);
  843. }
  844. void device_enable_stencil_write(gs_device_t *device, bool enable)
  845. {
  846. if (enable)
  847. glStencilMask(0xFFFFFFFF);
  848. else
  849. glStencilMask(0);
  850. UNUSED_PARAMETER(device);
  851. }
  852. void device_enable_color(gs_device_t *device, bool red, bool green,
  853. bool blue, bool alpha)
  854. {
  855. glColorMask(red, green, blue, alpha);
  856. UNUSED_PARAMETER(device);
  857. }
  858. void device_blend_function(gs_device_t *device, enum gs_blend_type src,
  859. enum gs_blend_type dest)
  860. {
  861. GLenum gl_src = convert_gs_blend_type(src);
  862. GLenum gl_dst = convert_gs_blend_type(dest);
  863. glBlendFunc(gl_src, gl_dst);
  864. if (!gl_success("glBlendFunc"))
  865. blog(LOG_ERROR, "device_blend_function (GL) failed");
  866. UNUSED_PARAMETER(device);
  867. }
  868. void device_depth_function(gs_device_t *device, enum gs_depth_test test)
  869. {
  870. GLenum gl_test = convert_gs_depth_test(test);
  871. glDepthFunc(gl_test);
  872. if (!gl_success("glDepthFunc"))
  873. blog(LOG_ERROR, "device_depth_function (GL) failed");
  874. UNUSED_PARAMETER(device);
  875. }
  876. void device_stencil_function(gs_device_t *device, enum gs_stencil_side side,
  877. enum gs_depth_test test)
  878. {
  879. GLenum gl_side = convert_gs_stencil_side(side);
  880. GLenum gl_test = convert_gs_depth_test(test);
  881. glStencilFuncSeparate(gl_side, gl_test, 0, 0xFFFFFFFF);
  882. if (!gl_success("glStencilFuncSeparate"))
  883. blog(LOG_ERROR, "device_stencil_function (GL) failed");
  884. UNUSED_PARAMETER(device);
  885. }
  886. void device_stencil_op(gs_device_t *device, enum gs_stencil_side side,
  887. enum gs_stencil_op_type fail, enum gs_stencil_op_type zfail,
  888. enum gs_stencil_op_type zpass)
  889. {
  890. GLenum gl_side = convert_gs_stencil_side(side);
  891. GLenum gl_fail = convert_gs_stencil_op(fail);
  892. GLenum gl_zfail = convert_gs_stencil_op(zfail);
  893. GLenum gl_zpass = convert_gs_stencil_op(zpass);
  894. glStencilOpSeparate(gl_side, gl_fail, gl_zfail, gl_zpass);
  895. if (!gl_success("glStencilOpSeparate"))
  896. blog(LOG_ERROR, "device_stencil_op (GL) failed");
  897. UNUSED_PARAMETER(device);
  898. }
  899. static inline uint32_t get_target_height(struct gs_device *device)
  900. {
  901. if (!device->cur_render_target)
  902. return device_get_height(device);
  903. if (device->cur_render_target->type == GS_TEXTURE_2D)
  904. return gs_texture_get_height(device->cur_render_target);
  905. else /* cube map */
  906. return gs_cubetexture_get_size(device->cur_render_target);
  907. }
  908. void device_set_viewport(gs_device_t *device, int x, int y, int width,
  909. int height)
  910. {
  911. uint32_t base_height;
  912. /* GL uses bottom-up coordinates for viewports. We want top-down */
  913. if (device->cur_render_target) {
  914. base_height = get_target_height(device);
  915. } else {
  916. uint32_t dw;
  917. gl_getclientsize(device->cur_swap, &dw, &base_height);
  918. }
  919. glViewport(x, base_height - y - height, width, height);
  920. if (!gl_success("glViewport"))
  921. blog(LOG_ERROR, "device_set_viewport (GL) failed");
  922. device->cur_viewport.x = x;
  923. device->cur_viewport.y = y;
  924. device->cur_viewport.cx = width;
  925. device->cur_viewport.cy = height;
  926. }
  927. void device_get_viewport(gs_device_t *device, struct gs_rect *rect)
  928. {
  929. *rect = device->cur_viewport;
  930. }
  931. void device_set_scissor_rect(gs_device_t *device, struct gs_rect *rect)
  932. {
  933. UNUSED_PARAMETER(device);
  934. if (rect != NULL) {
  935. glScissor(rect->x, rect->y, rect->cx, rect->cy);
  936. if (gl_success("glScissor") && gl_enable(GL_SCISSOR_TEST))
  937. return;
  938. } else if (gl_disable(GL_SCISSOR_TEST)) {
  939. return;
  940. }
  941. blog(LOG_ERROR, "device_set_scissor_rect (GL) failed");
  942. }
  943. void device_ortho(gs_device_t *device, float left, float right,
  944. float top, float bottom, float near, float far)
  945. {
  946. struct matrix4 *dst = &device->cur_proj;
  947. float rml = right-left;
  948. float bmt = bottom-top;
  949. float fmn = far-near;
  950. vec4_zero(&dst->x);
  951. vec4_zero(&dst->y);
  952. vec4_zero(&dst->z);
  953. vec4_zero(&dst->t);
  954. dst->x.x = 2.0f / rml;
  955. dst->t.x = (left+right) / -rml;
  956. dst->y.y = 2.0f / -bmt;
  957. dst->t.y = (bottom+top) / bmt;
  958. dst->z.z = -2.0f / fmn;
  959. dst->t.z = (far+near) / -fmn;
  960. dst->t.w = 1.0f;
  961. }
  962. void device_frustum(gs_device_t *device, float left, float right,
  963. float top, float bottom, float near, float far)
  964. {
  965. struct matrix4 *dst = &device->cur_proj;
  966. float rml = right-left;
  967. float tmb = top-bottom;
  968. float nmf = near-far;
  969. float nearx2 = 2.0f*near;
  970. vec4_zero(&dst->x);
  971. vec4_zero(&dst->y);
  972. vec4_zero(&dst->z);
  973. vec4_zero(&dst->t);
  974. dst->x.x = nearx2 / rml;
  975. dst->z.x = (left+right) / rml;
  976. dst->y.y = nearx2 / tmb;
  977. dst->z.y = (bottom+top) / tmb;
  978. dst->z.z = (far+near) / nmf;
  979. dst->t.z = 2.0f * (near*far) / nmf;
  980. dst->z.w = -1.0f;
  981. }
  982. void device_projection_push(gs_device_t *device)
  983. {
  984. da_push_back(device->proj_stack, &device->cur_proj);
  985. }
  986. void device_projection_pop(gs_device_t *device)
  987. {
  988. struct matrix4 *end;
  989. if (!device->proj_stack.num)
  990. return;
  991. end = da_end(device->proj_stack);
  992. device->cur_proj = *end;
  993. da_pop_back(device->proj_stack);
  994. }
  995. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  996. {
  997. if (!swapchain)
  998. return;
  999. if (swapchain->device->cur_swap == swapchain)
  1000. device_load_swapchain(swapchain->device, NULL);
  1001. gl_platform_cleanup_swapchain(swapchain);
  1002. gl_windowinfo_destroy(swapchain->wi);
  1003. bfree(swapchain);
  1004. }
  1005. void gs_voltexture_destroy(gs_texture_t *voltex)
  1006. {
  1007. /* TODO */
  1008. UNUSED_PARAMETER(voltex);
  1009. }
  1010. uint32_t gs_voltexture_get_width(gs_texture_t *voltex)
  1011. {
  1012. /* TODO */
  1013. UNUSED_PARAMETER(voltex);
  1014. return 0;
  1015. }
  1016. uint32_t gs_voltexture_get_height(gs_texture_t *voltex)
  1017. {
  1018. /* TODO */
  1019. UNUSED_PARAMETER(voltex);
  1020. return 0;
  1021. }
  1022. uint32_t gs_voltexture_getdepth(gs_texture_t *voltex)
  1023. {
  1024. /* TODO */
  1025. UNUSED_PARAMETER(voltex);
  1026. return 0;
  1027. }
  1028. enum gs_color_format gs_voltexture_get_color_format(gs_texture_t *voltex)
  1029. {
  1030. /* TODO */
  1031. UNUSED_PARAMETER(voltex);
  1032. return GS_UNKNOWN;
  1033. }
  1034. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1035. {
  1036. if (!samplerstate)
  1037. return;
  1038. if (samplerstate->device)
  1039. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1040. if (samplerstate->device->cur_samplers[i] ==
  1041. samplerstate)
  1042. samplerstate->device->cur_samplers[i] = NULL;
  1043. samplerstate_release(samplerstate);
  1044. }