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