gl-subsystem.c 21 KB

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