gl-subsystem.c 28 KB

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