gl-subsystem.c 30 KB

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