gl-subsystem.c 30 KB

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