Skip to content

MSAA & Resolve ​

Toggle multisampling on and off and look closely at the cube's edges - MSAA renders each pixel at 4 sub-sample positions and averages them, softening the jagged "staircase" look of a single-sampled render.

ts
import {
  boxGeometry,
  Color,
  createDevice,
  CullState,
  DepthState,
  Device,
  FrameContext,
  PlatformId,
  Texture,
} from '@gglib/graphics'
import {
  DEGREE_TO_RAD,
  mat4$initIdentity,
  mat4$initLookAt,
  mat4$initPerspectiveFieldOfView,
  mat4$invert,
  mat4$rotateY,
  mat4Identity,
  vec3,
} from '@gglib/math'
import { mount } from 'tweak-ui'

export default async (canvas: HTMLCanvasElement, tools: HTMLElement, platform: PlatformId) => {
  const device: Device = await createDevice({ canvas, platform }).ready

  const shader = device.createShaderModule({
    wgsl: { source: wgsl },
    glsl: { vertex: glslVS, fragment: glslFS },
  })

  const cube = boxGeometry(device)

  const settings = { msaa: true }
  const panel = mount(tools, (ui) => {
    ui.bool(settings, 'msaa', { label: 'MSAA (4 samples)' })
  })

  // A multisampled render target - and a matching multisampled depth
  // target, sample counts have to agree - to render into instead of the
  // canvas directly. Both are resized to track the canvas every frame.
  const msaaColor: Texture = device.createRenderTarget({
    width: device.output.width,
    height: device.output.height,
    format: device.output.format,
    sampleCount: 4,
  })
  const msaaDepth: Texture = device.createDepthTarget({
    width: device.output.width,
    height: device.output.height,
    format: 'depth24plus',
    sampleCount: 4,
  })
  const plainDepth: Texture = device.createDepthTarget({
    width: device.output.width,
    height: device.output.height,
    format: 'depth24plus',
  })

  const world = mat4Identity()
  const view = mat4Identity()
  const projection = mat4Identity()
  const cameraPosition = vec3(0, 0.6, 3)

  const pass = device.renderPass
  function frame(ctx: FrameContext) {
    device.resize()
    msaaColor.resizeToMatch(device.output)
    msaaDepth.resizeToMatch(device.output)
    plainDepth.resizeToMatch(device.output)

    mat4$initIdentity(world)
    mat4$rotateY(world, ctx.time * 20 * DEGREE_TO_RAD)
    mat4$initLookAt(view, cameraPosition, vec3(0, 0, 0), vec3(0, 1, 0))
    mat4$invert(view)
    mat4$initPerspectiveFieldOfView(projection, 60 * DEGREE_TO_RAD, device.output.aspectRatio, 0.1, 100, device.ndcMinZ)

    if (settings.msaa) {
      // Render into the multisampled target, `device.output` given as the
      // resolve target - `pass.resolve()` below downsamples the 4 samples
      // per pixel into the single-sampled canvas image.
      pass.setRenderTarget(0, msaaColor, 0, 0, device.output)
      pass.setDepthTarget(msaaDepth)
    } else {
      pass.setRenderTarget(0, null)
      // pass.setDepthTarget(plainDepth)
    }
    pass.setViewportState(0, 0, device.output.width, device.output.height)
    pass.setClearColor(0, Color.CornflowerBlue)
    pass.setClearDepth(1)
    pass.clear()

    if (!shader.isValid) {
      return
    }

    const program = shader.program
    program.set('uWorld', world)
    program.set('uView', view)
    program.set('uProjection', projection)
    program.commit()

    pass.setDepthState(DepthState.LessEqual)
    pass.setCullState(CullState.CullBack)
    pass.setProgram(program)
    pass.render(cube)

    pass.submit()
    if (settings.msaa) {
      pass.resolve()
    }
    pass.flush()
  }

  device.schedule(frame)
  return () => {
    device.dispose()
    panel.dispose()
  }
}

const glslVS = /*glsl*/ `
  #version 300 es
  in vec3 position;
  out vec3 color;
  uniform mat4 uWorld;
  uniform mat4 uView;
  uniform mat4 uProjection;

  void main(void) {
    color = position + vec3(0.5);
    gl_Position = uProjection * uView * uWorld * vec4(position, 1.0);
  }
`
const glslFS = /*glsl*/ `
  #version 300 es
  precision highp float;
  in vec3 color;
  out vec4 fragColor;
  void main(void) {
    fragColor = vec4(color, 1.0);
  }
`

const wgsl = /*wgsl*/ `

  @group(0) @binding(0) var<uniform> uWorld : mat4x4f;
  @group(0) @binding(1) var<uniform> uView : mat4x4f;
  @group(0) @binding(2) var<uniform> uProjection : mat4x4f;

  struct VertexInput {
    @location(0) position : vec3f,
  };
  struct VertexOutput {
    @builtin(position) Position : vec4f,
    @location(0) color : vec3f,
  };

  @vertex
  fn vs(input: VertexInput) -> VertexOutput {
    var output : VertexOutput;
    output.color = input.position + vec3f(0.5);
    output.Position = uProjection * uView * uWorld * vec4f(input.position, 1.0);
    return output;
  }

  @fragment
  fn fs(input: VertexOutput) -> @location(0) vec4<f32> {
    return vec4f(input.color, 1.0);
  }
`

Released under the MIT License.