gl-subsystem.c 29 KB

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