gl-subsystem.c 25 KB

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