gl-subsystem.c 25 KB

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