renderer.spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { createGl } from './gl.shim';
  7. import { Camera } from '../../mol-canvas3d/camera';
  8. import { Vec3 } from '../../mol-math/linear-algebra';
  9. import { Renderer } from '../renderer';
  10. import { createContext } from '../webgl/context';
  11. import { Scene } from '../scene';
  12. import { createPoints } from './points.spec';
  13. export function createRenderer(gl: WebGLRenderingContext) {
  14. const ctx = createContext(gl);
  15. const camera = new Camera({
  16. position: Vec3.create(0, 0, 50)
  17. });
  18. const renderer = Renderer.create(ctx);
  19. return { ctx, camera, renderer };
  20. }
  21. describe('renderer', () => {
  22. it('basic', () => {
  23. const [width, height] = [32, 32];
  24. const gl = createGl(width, height, { preserveDrawingBuffer: true });
  25. const { ctx, renderer } = createRenderer(gl);
  26. expect(ctx.gl.drawingBufferWidth).toBe(32);
  27. expect(ctx.gl.drawingBufferHeight).toBe(32);
  28. expect(ctx.stats.resourceCounts.attribute).toBe(0);
  29. expect(ctx.stats.resourceCounts.texture).toBe(0);
  30. expect(ctx.stats.resourceCounts.vertexArray).toBe(0);
  31. expect(ctx.stats.resourceCounts.program).toBe(0);
  32. expect(ctx.stats.resourceCounts.shader).toBe(0);
  33. renderer.setViewport(0, 0, 64, 48);
  34. expect(ctx.gl.getParameter(ctx.gl.VIEWPORT)[2]).toBe(64);
  35. expect(ctx.gl.getParameter(ctx.gl.VIEWPORT)[3]).toBe(48);
  36. });
  37. it('points', async () => {
  38. const [width, height] = [32, 32];
  39. const gl = createGl(width, height, { preserveDrawingBuffer: true });
  40. const { ctx } = createRenderer(gl);
  41. const scene = Scene.create(ctx);
  42. const points = createPoints();
  43. scene.add(points);
  44. scene.commit();
  45. expect(ctx.stats.resourceCounts.attribute).toBe(ctx.isWebGL2 ? 4 : 5);
  46. expect(ctx.stats.resourceCounts.texture).toBe(7);
  47. expect(ctx.stats.resourceCounts.vertexArray).toBe(ctx.extensions.vertexArrayObject ? 8 : 0);
  48. expect(ctx.stats.resourceCounts.program).toBe(8);
  49. expect(ctx.stats.resourceCounts.shader).toBe(16);
  50. scene.remove(points);
  51. scene.commit();
  52. expect(ctx.stats.resourceCounts.attribute).toBe(0);
  53. expect(ctx.stats.resourceCounts.texture).toBe(0);
  54. expect(ctx.stats.resourceCounts.vertexArray).toBe(0);
  55. expect(ctx.stats.resourceCounts.program).toBe(8);
  56. expect(ctx.stats.resourceCounts.shader).toBe(16);
  57. ctx.resources.destroy();
  58. expect(ctx.stats.resourceCounts.program).toBe(0);
  59. expect(ctx.stats.resourceCounts.shader).toBe(0);
  60. });
  61. });