1
0

gl-subsystem.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. const 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, const 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. const 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(const 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(const gs_device_t *device)
  233. {
  234. return device->cur_swap->info.cx;
  235. }
  236. uint32_t device_get_height(const 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. const 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(const 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(const gs_device_t *device)
  456. {
  457. return device->cur_vertex_shader;
  458. }
  459. gs_shader_t *device_get_pixel_shader(const gs_device_t *device)
  460. {
  461. return device->cur_pixel_shader;
  462. }
  463. gs_texture_t *device_get_render_target(const gs_device_t *device)
  464. {
  465. return device->cur_render_target;
  466. }
  467. gs_zstencil_t *device_get_zstencil_target(const 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 ?
  659. (uint32_t)src_w : (src2d->width - src_x);
  660. uint32_t nh = (uint32_t)src_h ?
  661. (uint32_t)src_h : (src2d->height - src_y);
  662. if (dst2d->width - dst_x < nw || dst2d->height - dst_y < nh) {
  663. blog(LOG_ERROR, "Destination texture region is not big "
  664. "enough to hold the source region");
  665. goto fail;
  666. }
  667. if (!gl_copy_texture(device, dst->texture, dst->gl_target, dst_x, dst_y,
  668. src->texture, src->gl_target, src_x, src_y,
  669. nw, nh, src->format))
  670. goto fail;
  671. return;
  672. fail:
  673. blog(LOG_ERROR, "device_copy_texture (GL) failed");
  674. }
  675. void device_copy_texture(gs_device_t *device, gs_texture_t *dst,
  676. gs_texture_t *src)
  677. {
  678. device_copy_texture_region(device, dst, 0, 0, src, 0, 0, 0, 0);
  679. }
  680. void device_begin_scene(gs_device_t *device)
  681. {
  682. clear_textures(device);
  683. }
  684. static inline bool can_render(const gs_device_t *device)
  685. {
  686. if (!device->cur_vertex_shader) {
  687. blog(LOG_ERROR, "No vertex shader specified");
  688. return false;
  689. }
  690. if (!device->cur_pixel_shader) {
  691. blog(LOG_ERROR, "No pixel shader specified");
  692. return false;
  693. }
  694. if (!device->cur_vertex_buffer) {
  695. blog(LOG_ERROR, "No vertex buffer specified");
  696. return false;
  697. }
  698. return true;
  699. }
  700. static void update_viewproj_matrix(struct gs_device *device)
  701. {
  702. struct gs_shader *vs = device->cur_vertex_shader;
  703. gs_matrix_get(&device->cur_view);
  704. matrix4_mul(&device->cur_viewproj, &device->cur_view,
  705. &device->cur_proj);
  706. matrix4_transpose(&device->cur_viewproj, &device->cur_viewproj);
  707. if (vs->viewproj)
  708. gs_shader_set_matrix4(vs->viewproj, &device->cur_viewproj);
  709. }
  710. static inline struct gs_program *find_program(const struct gs_device *device)
  711. {
  712. struct gs_program *program = device->first_program;
  713. while (program) {
  714. if (program->vertex_shader == device->cur_vertex_shader &&
  715. program->pixel_shader == device->cur_pixel_shader)
  716. return program;
  717. program = program->next;
  718. }
  719. return NULL;
  720. }
  721. static inline struct gs_program *get_shader_program(struct gs_device *device)
  722. {
  723. struct gs_program *program = find_program(device);
  724. if (!program)
  725. program = gs_program_create(device);
  726. return program;
  727. }
  728. void device_draw(gs_device_t *device, enum gs_draw_mode draw_mode,
  729. uint32_t start_vert, uint32_t num_verts)
  730. {
  731. struct gs_index_buffer *ib = device->cur_index_buffer;
  732. GLenum topology = convert_gs_topology(draw_mode);
  733. gs_effect_t *effect = gs_get_effect();
  734. struct gs_program *program;
  735. if (!can_render(device))
  736. goto fail;
  737. if (effect)
  738. gs_effect_update_params(effect);
  739. program = get_shader_program(device);
  740. if (!program)
  741. goto fail;
  742. load_vb_buffers(program, device->cur_vertex_buffer);
  743. if (program != device->cur_program && device->cur_program) {
  744. glUseProgram(0);
  745. gl_success("glUseProgram (zero)");
  746. }
  747. if (program != device->cur_program) {
  748. device->cur_program = program;
  749. glUseProgram(program->obj);
  750. if (!gl_success("glUseProgram"))
  751. goto fail;
  752. }
  753. update_viewproj_matrix(device);
  754. program_update_params(program);
  755. if (ib) {
  756. if (num_verts == 0)
  757. num_verts = (uint32_t)device->cur_index_buffer->num;
  758. glDrawElements(topology, num_verts, ib->gl_type,
  759. (const GLvoid*)(start_vert * ib->width));
  760. if (!gl_success("glDrawElements"))
  761. goto fail;
  762. } else {
  763. if (num_verts == 0)
  764. num_verts = (uint32_t)device->cur_vertex_buffer->num;
  765. glDrawArrays(topology, start_vert, num_verts);
  766. if (!gl_success("glDrawArrays"))
  767. goto fail;
  768. }
  769. return;
  770. fail:
  771. blog(LOG_ERROR, "device_draw (GL) failed");
  772. }
  773. void device_end_scene(gs_device_t *device)
  774. {
  775. /* does nothing */
  776. UNUSED_PARAMETER(device);
  777. }
  778. void device_clear(gs_device_t *device, uint32_t clear_flags,
  779. const struct vec4 *color, float depth, uint8_t stencil)
  780. {
  781. GLbitfield gl_flags = 0;
  782. if (clear_flags & GS_CLEAR_COLOR) {
  783. glClearColor(color->x, color->y, color->z, color->w);
  784. gl_flags |= GL_COLOR_BUFFER_BIT;
  785. }
  786. if (clear_flags & GS_CLEAR_DEPTH) {
  787. glClearDepth(depth);
  788. gl_flags |= GL_DEPTH_BUFFER_BIT;
  789. }
  790. if (clear_flags & GS_CLEAR_STENCIL) {
  791. glClearStencil(stencil);
  792. gl_flags |= GL_STENCIL_BUFFER_BIT;
  793. }
  794. glClear(gl_flags);
  795. if (!gl_success("glClear"))
  796. blog(LOG_ERROR, "device_clear (GL) failed");
  797. UNUSED_PARAMETER(device);
  798. }
  799. void device_flush(gs_device_t *device)
  800. {
  801. glFlush();
  802. UNUSED_PARAMETER(device);
  803. }
  804. void device_set_cull_mode(gs_device_t *device, enum gs_cull_mode mode)
  805. {
  806. if (device->cur_cull_mode == mode)
  807. return;
  808. if (device->cur_cull_mode == GS_NEITHER)
  809. gl_enable(GL_CULL_FACE);
  810. device->cur_cull_mode = mode;
  811. if (mode == GS_BACK)
  812. gl_cull_face(GL_BACK);
  813. else if (mode == GS_FRONT)
  814. gl_cull_face(GL_FRONT);
  815. else
  816. gl_disable(GL_CULL_FACE);
  817. }
  818. enum gs_cull_mode device_get_cull_mode(const gs_device_t *device)
  819. {
  820. return device->cur_cull_mode;
  821. }
  822. void device_enable_blending(gs_device_t *device, bool enable)
  823. {
  824. if (enable)
  825. gl_enable(GL_BLEND);
  826. else
  827. gl_disable(GL_BLEND);
  828. UNUSED_PARAMETER(device);
  829. }
  830. void device_enable_depth_test(gs_device_t *device, bool enable)
  831. {
  832. if (enable)
  833. gl_enable(GL_DEPTH_TEST);
  834. else
  835. gl_disable(GL_DEPTH_TEST);
  836. UNUSED_PARAMETER(device);
  837. }
  838. void device_enable_stencil_test(gs_device_t *device, bool enable)
  839. {
  840. if (enable)
  841. gl_enable(GL_STENCIL_TEST);
  842. else
  843. gl_disable(GL_STENCIL_TEST);
  844. UNUSED_PARAMETER(device);
  845. }
  846. void device_enable_stencil_write(gs_device_t *device, bool enable)
  847. {
  848. if (enable)
  849. glStencilMask(0xFFFFFFFF);
  850. else
  851. glStencilMask(0);
  852. UNUSED_PARAMETER(device);
  853. }
  854. void device_enable_color(gs_device_t *device, bool red, bool green,
  855. bool blue, bool alpha)
  856. {
  857. glColorMask(red, green, blue, alpha);
  858. UNUSED_PARAMETER(device);
  859. }
  860. void device_blend_function(gs_device_t *device, enum gs_blend_type src,
  861. enum gs_blend_type dest)
  862. {
  863. GLenum gl_src = convert_gs_blend_type(src);
  864. GLenum gl_dst = convert_gs_blend_type(dest);
  865. glBlendFunc(gl_src, gl_dst);
  866. if (!gl_success("glBlendFunc"))
  867. blog(LOG_ERROR, "device_blend_function (GL) failed");
  868. UNUSED_PARAMETER(device);
  869. }
  870. void device_depth_function(gs_device_t *device, enum gs_depth_test test)
  871. {
  872. GLenum gl_test = convert_gs_depth_test(test);
  873. glDepthFunc(gl_test);
  874. if (!gl_success("glDepthFunc"))
  875. blog(LOG_ERROR, "device_depth_function (GL) failed");
  876. UNUSED_PARAMETER(device);
  877. }
  878. void device_stencil_function(gs_device_t *device, enum gs_stencil_side side,
  879. enum gs_depth_test test)
  880. {
  881. GLenum gl_side = convert_gs_stencil_side(side);
  882. GLenum gl_test = convert_gs_depth_test(test);
  883. glStencilFuncSeparate(gl_side, gl_test, 0, 0xFFFFFFFF);
  884. if (!gl_success("glStencilFuncSeparate"))
  885. blog(LOG_ERROR, "device_stencil_function (GL) failed");
  886. UNUSED_PARAMETER(device);
  887. }
  888. void device_stencil_op(gs_device_t *device, enum gs_stencil_side side,
  889. enum gs_stencil_op_type fail, enum gs_stencil_op_type zfail,
  890. enum gs_stencil_op_type zpass)
  891. {
  892. GLenum gl_side = convert_gs_stencil_side(side);
  893. GLenum gl_fail = convert_gs_stencil_op(fail);
  894. GLenum gl_zfail = convert_gs_stencil_op(zfail);
  895. GLenum gl_zpass = convert_gs_stencil_op(zpass);
  896. glStencilOpSeparate(gl_side, gl_fail, gl_zfail, gl_zpass);
  897. if (!gl_success("glStencilOpSeparate"))
  898. blog(LOG_ERROR, "device_stencil_op (GL) failed");
  899. UNUSED_PARAMETER(device);
  900. }
  901. static inline uint32_t get_target_height(const struct gs_device *device)
  902. {
  903. if (!device->cur_render_target)
  904. return device_get_height(device);
  905. if (device->cur_render_target->type == GS_TEXTURE_2D)
  906. return gs_texture_get_height(device->cur_render_target);
  907. else /* cube map */
  908. return gs_cubetexture_get_size(device->cur_render_target);
  909. }
  910. void device_set_viewport(gs_device_t *device, int x, int y, int width,
  911. int height)
  912. {
  913. uint32_t base_height;
  914. /* GL uses bottom-up coordinates for viewports. We want top-down */
  915. if (device->cur_render_target) {
  916. base_height = get_target_height(device);
  917. } else {
  918. uint32_t dw;
  919. gl_getclientsize(device->cur_swap, &dw, &base_height);
  920. }
  921. glViewport(x, base_height - y - height, width, height);
  922. if (!gl_success("glViewport"))
  923. blog(LOG_ERROR, "device_set_viewport (GL) failed");
  924. device->cur_viewport.x = x;
  925. device->cur_viewport.y = y;
  926. device->cur_viewport.cx = width;
  927. device->cur_viewport.cy = height;
  928. }
  929. void device_get_viewport(const gs_device_t *device, struct gs_rect *rect)
  930. {
  931. *rect = device->cur_viewport;
  932. }
  933. void device_set_scissor_rect(gs_device_t *device, const struct gs_rect *rect)
  934. {
  935. UNUSED_PARAMETER(device);
  936. if (rect != NULL) {
  937. glScissor(rect->x, rect->y, rect->cx, rect->cy);
  938. if (gl_success("glScissor") && gl_enable(GL_SCISSOR_TEST))
  939. return;
  940. } else if (gl_disable(GL_SCISSOR_TEST)) {
  941. return;
  942. }
  943. blog(LOG_ERROR, "device_set_scissor_rect (GL) failed");
  944. }
  945. void device_ortho(gs_device_t *device, float left, float right,
  946. float top, float bottom, float near, float far)
  947. {
  948. struct matrix4 *dst = &device->cur_proj;
  949. float rml = right-left;
  950. float bmt = bottom-top;
  951. float fmn = far-near;
  952. vec4_zero(&dst->x);
  953. vec4_zero(&dst->y);
  954. vec4_zero(&dst->z);
  955. vec4_zero(&dst->t);
  956. dst->x.x = 2.0f / rml;
  957. dst->t.x = (left+right) / -rml;
  958. dst->y.y = 2.0f / -bmt;
  959. dst->t.y = (bottom+top) / bmt;
  960. dst->z.z = -2.0f / fmn;
  961. dst->t.z = (far+near) / -fmn;
  962. dst->t.w = 1.0f;
  963. }
  964. void device_frustum(gs_device_t *device, float left, float right,
  965. float top, float bottom, float near, float far)
  966. {
  967. struct matrix4 *dst = &device->cur_proj;
  968. float rml = right-left;
  969. float tmb = top-bottom;
  970. float nmf = near-far;
  971. float nearx2 = 2.0f*near;
  972. vec4_zero(&dst->x);
  973. vec4_zero(&dst->y);
  974. vec4_zero(&dst->z);
  975. vec4_zero(&dst->t);
  976. dst->x.x = nearx2 / rml;
  977. dst->z.x = (left+right) / rml;
  978. dst->y.y = nearx2 / tmb;
  979. dst->z.y = (bottom+top) / tmb;
  980. dst->z.z = (far+near) / nmf;
  981. dst->t.z = 2.0f * (near*far) / nmf;
  982. dst->z.w = -1.0f;
  983. }
  984. void device_projection_push(gs_device_t *device)
  985. {
  986. da_push_back(device->proj_stack, &device->cur_proj);
  987. }
  988. void device_projection_pop(gs_device_t *device)
  989. {
  990. struct matrix4 *end;
  991. if (!device->proj_stack.num)
  992. return;
  993. end = da_end(device->proj_stack);
  994. device->cur_proj = *end;
  995. da_pop_back(device->proj_stack);
  996. }
  997. void gs_swapchain_destroy(gs_swapchain_t *swapchain)
  998. {
  999. if (!swapchain)
  1000. return;
  1001. if (swapchain->device->cur_swap == swapchain)
  1002. device_load_swapchain(swapchain->device, NULL);
  1003. gl_platform_cleanup_swapchain(swapchain);
  1004. gl_windowinfo_destroy(swapchain->wi);
  1005. bfree(swapchain);
  1006. }
  1007. void gs_voltexture_destroy(gs_texture_t *voltex)
  1008. {
  1009. /* TODO */
  1010. UNUSED_PARAMETER(voltex);
  1011. }
  1012. uint32_t gs_voltexture_get_width(const gs_texture_t *voltex)
  1013. {
  1014. /* TODO */
  1015. UNUSED_PARAMETER(voltex);
  1016. return 0;
  1017. }
  1018. uint32_t gs_voltexture_get_height(const gs_texture_t *voltex)
  1019. {
  1020. /* TODO */
  1021. UNUSED_PARAMETER(voltex);
  1022. return 0;
  1023. }
  1024. uint32_t gs_voltexture_getdepth(const gs_texture_t *voltex)
  1025. {
  1026. /* TODO */
  1027. UNUSED_PARAMETER(voltex);
  1028. return 0;
  1029. }
  1030. enum gs_color_format gs_voltexture_get_color_format(const gs_texture_t *voltex)
  1031. {
  1032. /* TODO */
  1033. UNUSED_PARAMETER(voltex);
  1034. return GS_UNKNOWN;
  1035. }
  1036. void gs_samplerstate_destroy(gs_samplerstate_t *samplerstate)
  1037. {
  1038. if (!samplerstate)
  1039. return;
  1040. if (samplerstate->device)
  1041. for (int i = 0; i < GS_MAX_TEXTURES; i++)
  1042. if (samplerstate->device->cur_samplers[i] ==
  1043. samplerstate)
  1044. samplerstate->device->cur_samplers[i] = NULL;
  1045. samplerstate_release(samplerstate);
  1046. }