gl-subsystem.c 25 KB

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