gl-subsystem.c 24 KB

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