gl-subsystem.c 30 KB

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