gl-subsystem.c 30 KB

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