gl-subsystem.c 28 KB

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