gl-subsystem.c 31 KB

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