gl-subsystem.c 27 KB

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