gl-subsystem.c 30 KB

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