gl-subsystem.c 30 KB

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