gl-subsystem.c 28 KB

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