gl-subsystem.c 28 KB

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