gl-subsystem.c 25 KB

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