gl-subsystem.c 29 KB

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