gl-subsystem.c 29 KB

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