Skip to content

Multiple Render Targets ​

One draw call, two output images: the fragment shader writes a color to one texture and a brightness mask to another, in the same pass.

ts
import {
  boxGeometry,
  Color,
  createDevice,
  CullState,
  DepthState,
  Device,
  FrameContext,
  PlatformId,
  SpriteBatch,
  Texture,
  TextureUsage,
} from '@gglib/graphics'
import {
  DEGREE_TO_RAD,
  mat4$initLookAt,
  mat4$initPerspectiveFieldOfView,
  mat4$initRotationY,
  mat4$invert,
  mat4Identity,
  vec3,
} from '@gglib/math'

export default async (canvas: HTMLCanvasElement, _: any, platform: PlatformId) => {
  const device: Device = await createDevice({ canvas, platform, autosize: true }).ready

  const sceneShader = device.createShaderModule({
    wgsl: { source: sceneWgsl },
    glsl: { vertex: sceneGlslVS, fragment: sceneGlslFS },
  })
  const spriteBatch = new SpriteBatch(device)
  const box = boxGeometry(device, { size: 2 })

  // Two color targets, written by the same draw calls in the same pass -
  // `outColor` in the shader below always lands in the first, `outBright`
  // always in the second.
  const colorTarget: Texture = device.createRenderTarget({
    width: device.output.width,
    height: device.output.height,
    format: 'rgba8unorm',
    usage: TextureUsage.TextureBinding,
  })
  const normalTarget: Texture = device.createRenderTarget({
    width: device.output.width,
    height: device.output.height,
    format: 'rgba8unorm',
    usage: TextureUsage.TextureBinding,
  })
  const depthTarget: 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.75, 4)

  const pass = device.renderPass
  function frame(ctx: FrameContext) {
    colorTarget.resizeToMatch(device.output)
    normalTarget.resizeToMatch(device.output)
    depthTarget.resizeToMatch(device.output)

    if (!sceneShader.isValid || !spriteBatch.shader.isValid) {
      return
    }

    mat4$initRotationY(world, ctx.time * 30 * DEGREE_TO_RAD)
    mat4$initLookAt(view, cameraPosition, vec3(0, 0, 0), vec3(0, 1, 0))
    mat4$invert(view)
    mat4$initPerspectiveFieldOfView(
      projection,
      45 * DEGREE_TO_RAD,
      device.output.aspectRatio / 2,
      0.1,
      100,
      device.ndcMinZ,
    )

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

    // Pass 1: one draw call, two output images
    pass.setRenderTarget(0, colorTarget)
    pass.setRenderTarget(1, normalTarget)
    pass.setDepthTarget(depthTarget)
    pass.setViewportState(0, 0, colorTarget.width, colorTarget.height)
    pass.setClearColor(0, Color.CornflowerBlue)
    pass.setClearColor(1, Color.Black)
    pass.setClearDepth(1)
    pass.clear()

    pass.setDepthState(DepthState.LessEqual)
    pass.setCullState(CullState.CullBack)
    pass.setProgram(sceneProgram)
    pass.render(box)

    // Pass 2: show both results side by side on the canvas
    // since we didn't flush() after first pass, we need to undo some settings
    pass.setProgram(null!) //                 undo program
    pass.setRenderTarget(0, device.output) //
    pass.setRenderTarget(1, null) //          undo rt2
    pass.setDepthTarget(null!) //             undo Depth
    pass.setViewportState(0, 0, device.output.width, device.output.height)
    pass.setClearColor(0, Color.Black)
    pass.clear()

    pass.setDepthState(DepthState.Disabled)
    pass.setCullState(CullState.Disabled)

    spriteBatch.begin()
    spriteBatch
      .next(colorTarget)
      .destination(0, 0, device.output.width / 2, device.output.height)
      .flipY(device.isWebGL2)
    spriteBatch
      .next(normalTarget)
      .destination(device.output.width / 2, 0, device.output.width / 2, device.output.height)
      .flipY(device.isWebGL2)

    spriteBatch.render(pass)

    pass.submit()
    pass.flush()
  }

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

const sceneGlslVS = /*glsl*/ `
  #version 300 es
  in vec3 position;
  in vec3 normal;
  uniform mat4 uWorld;
  uniform mat4 uView;
  uniform mat4 uProjection;
  out vec3 vertexNormal;
  out vec3 vertexColor;
  void main(void) {
    vertexNormal = normal;
    vertexColor = vec3(0.5) + position;
    gl_Position = uProjection * uView * uWorld * vec4(position, 1.0);
  }
`
const sceneGlslFS = /*glsl*/ `
  #version 300 es
  precision mediump float;
  in vec3 vertexNormal;
  in vec3 vertexColor;
  layout(location = 0) out vec4 outColor;
  layout(location = 1) out vec4 outNormal;
  void main(void) {
    outColor = vec4(vertexColor, 1.0);
    outNormal = vec4(vertexNormal * 0.5 + 0.5, 1.0);
  }
`
const sceneWgsl = /*wgsl*/ `
  @group(0) @binding(0) var<uniform> uWorld : mat4x4<f32>;
  @group(0) @binding(1) var<uniform> uView : mat4x4<f32>;
  @group(0) @binding(2) var<uniform> uProjection : mat4x4<f32>;

  struct VertexInput {
    @location(0) position : vec3<f32>,
    @location(1) normal : vec3<f32>,
  };
  struct VertexOutput {
    @builtin(position) Position : vec4<f32>,
    @location(0) vertexColor : vec3<f32>,
    @location(1) vertexNormal : vec3<f32>,
  };

  @vertex
  fn vs(input: VertexInput) -> VertexOutput {
    var output : VertexOutput;
    output.Position = uProjection * uView * uWorld * vec4<f32>(input.position, 1.0);
    output.vertexColor = input.position * 0.5 + vec3f(0.5);
    output.vertexNormal = input.normal;
    return output;
  }

  struct FragmentOutput {
    @location(0) outColor : vec4<f32>,
    @location(1) outNormal : vec4<f32>,
  };

  @fragment
  fn fs(input: VertexOutput) -> FragmentOutput {
    var output : FragmentOutput;
    output.outColor = vec4<f32>(input.vertexColor, 1.0);
    output.outNormal = vec4<f32>(vec3f(0.5) + input.vertexNormal * 0.5, 1.0);
    return output;
  }
`

Released under the MIT License.