Viewport & Scissor
The same rotating triangle, drawn four times into four independent viewports. An optional scissor rect then clips each one further, independently of the viewport transform.
ts
1import { Color, createDevice, Device, FrameContext, PlatformId, vertexLayout } from '@gglib/graphics'
2import { mat4$initRotationZ, mat4Identity } from '@gglib/math'
3import { mount } from 'tweak-ui'
4
5export default async (canvas: HTMLCanvasElement, tools: HTMLElement, platform: PlatformId) => {
6 const device: Device = await createDevice({ canvas, platform, autosize: true }).ready
7
8 const shader = device.createShaderModule({
9 wgsl: {
10 source: wgslShader,
11 },
12 glsl: {
13 vertex: glslVS,
14 fragment: glslFS,
15 },
16 })
17
18 const vertices = device.createVertexBuffer([
19 {
20 layout: vertexLayout({
21 vPosition: 'float32x3',
22 vColor: 'float32x3',
23 }),
24 // prettier-ignore
25 data: new Float32Array([
26 -0.7, -0.7, 0.0, 1, 0, 0,
27 0.7, -0.7, 0.0, 0, 1, 0,
28 0.0, 0.7, 0.0, 0, 0, 1,
29 ]),
30 },
31 ])
32
33 const settings = {
34 scissor: true,
35 }
36 const panel = mount(tools, (ui) => {
37 ui.bool(settings, 'scissor', { label: 'Scissor (inset clip rect)' })
38 })
39
40 const world = mat4Identity()
41 const pass = device.renderPass
42 function frame(ctx: FrameContext) {
43 // Clear the whole canvas once, before restricting the viewport to any
44 // of the four quadrants below.
45 pass.setViewportState(0, 0, device.output.width, device.output.height)
46 pass.setScissorState(null)
47 pass.setClearColor(0, Color.CornflowerBlue)
48 pass.clear()
49
50 if (!shader.isValid) {
51 return
52 }
53
54 mat4$initRotationZ(world, ctx.time)
55 const program = shader.program
56 program.set('uWorld', world)
57 program.commit()
58
59 pass.setProgram(program)
60 pass.setVertexBuffer(vertices)
61
62 // The viewport maps clip space (-1..1) to a region of the render
63 // target - drawing the same triangle into 4 different viewports
64 // renders 4 independent, identically-sized copies of it.
65 const w = device.output.width / 2
66 const h = device.output.height / 2
67 const quadrants: Array<[number, number]> = [
68 [0, 0],
69 [w, 0],
70 [0, h],
71 [w, h],
72 ]
73 for (const [x, y] of quadrants) {
74 pass.setViewportState(x, y, w, h)
75 if (settings.scissor) {
76 // The scissor rect clips pixels regardless of the viewport
77 // transform above - shrinking it below the viewport size cuts the
78 // triangle off at a hard edge, independent of its rotation.
79 const margin = 0.18
80 pass.setScissorState(x + w * margin, y + h * margin, w * (1 - 2 * margin), h * (1 - 2 * margin))
81 } else {
82 pass.setScissorState(null)
83 }
84 pass.draw(3)
85 }
86
87 pass.submit()
88 pass.flush()
89 }
90
91 device.schedule(frame)
92 return () => {
93 device.dispose()
94 panel.dispose()
95 }
96}
97
98const glslVS = /*glsl*/ `
99 #version 300 es
100 in vec3 vPosition;
101 in vec3 vColor;
102 uniform mat4 uWorld;
103 out vec3 vertexColor;
104 void main(void) {
105 vertexColor = vColor;
106 gl_Position = uWorld * vec4(vPosition, 1.0);
107 }
108`
109
110const glslFS = /*glsl*/ `
111 #version 300 es
112 precision mediump float;
113 in vec3 vertexColor;
114 out vec4 fragColor;
115 void main(void) {
116 fragColor = vec4(vertexColor, 1.0);
117 }
118`
119const wgslShader = /*wgsl*/ `
120 @group(0) @binding(0) var<uniform> uWorld : mat4x4<f32>;
121
122 struct VertexInput {
123 @location(0) vPosition : vec3<f32>,
124 @location(1) vColor : vec3<f32>,
125 };
126
127 struct VertexOutput {
128 @builtin(position) Position : vec4<f32>,
129 @location(0) vertexColor : vec3<f32>,
130 };
131
132 @vertex
133 fn vs(input: VertexInput) -> VertexOutput {
134 var output : VertexOutput;
135 output.Position = uWorld * vec4<f32>(input.vPosition, 1.0);
136 output.vertexColor = input.vColor;
137 return output;
138 }
139
140 @fragment
141 fn fs(input: VertexOutput) -> @location(0) vec4<f32> {
142 return vec4<f32>(input.vertexColor, 1.0);
143 }
144`