gl-subsystem.c 30 KB

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