gl-subsystem.c 28 KB

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