gl-subsystem.c 28 KB

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