-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.ts
204 lines (176 loc) · 6.35 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { throwError } from '../../utils/utils'
import { GPURenderer } from './GPURenderer'
import { GPUCameraRenderer } from './GPUCameraRenderer'
import { GPUCurtainsRenderer } from '../../curtains/renderers/GPUCurtainsRenderer'
import { WritableBufferBinding } from '../bindings/WritableBufferBinding'
/**
* A Renderer could be either a {@link GPURenderer}, a {@link GPUCameraRenderer} or a {@link GPUCurtainsRenderer}
* @type {Renderer}
*/
export type Renderer = GPUCurtainsRenderer | GPUCameraRenderer | GPURenderer
/**
* A CameraRenderer could be either a {@link GPUCameraRenderer} or a {@link GPUCurtainsRenderer}
* @type {CameraRenderer}
*/
export type CameraRenderer = GPUCurtainsRenderer | GPUCameraRenderer
/**
* Format a renderer error based on given renderer, renderer type and object type
* @param renderer - renderer that failed the test
* @param rendererType - expected renderer type
* @param type - object type
*/
const formatRendererError = (renderer: Renderer, rendererType = 'GPURenderer', type: string | null): void => {
const error = type
? `Unable to create ${type} because the ${rendererType} is not defined: ${renderer}`
: `The ${rendererType} is not defined: ${renderer}`
throwError(error)
}
/**
* Check if the given renderer is a {@link Renderer}
* @param renderer - renderer to test
* @param type - object type used to format the error if needed
* @returns - whether the given renderer is a {@link Renderer}
*/
export const isRenderer = (renderer: Renderer | undefined, type: string | null): boolean => {
const isRenderer =
renderer &&
(renderer.type === 'GPURenderer' ||
renderer.type === 'GPUCameraRenderer' ||
renderer.type === 'GPUCurtainsRenderer')
if (!isRenderer) {
formatRendererError(renderer, 'GPURenderer', type)
}
return isRenderer
}
/**
* Check if the given renderer is a {@link CameraRenderer}
* @param renderer - renderer to test
* @param type - object type used to format the error if needed
* @returns - whether the given renderer is a {@link CameraRenderer}
*/
export const isCameraRenderer = (renderer: CameraRenderer | undefined, type: string | null): boolean => {
const isCameraRenderer =
renderer && (renderer.type === 'GPUCameraRenderer' || renderer.type === 'GPUCurtainsRenderer')
if (!isCameraRenderer) {
formatRendererError(renderer, 'GPUCameraRenderer', type)
}
return isCameraRenderer
}
/**
* Check if the given renderer is a {@link GPUCurtainsRenderer}
* @param renderer - renderer to test
* @param type - object type used to format the error if needed
* @returns - whether the given renderer is a {@link GPUCurtainsRenderer}
*/
export const isCurtainsRenderer = (renderer: GPUCurtainsRenderer | undefined, type: string | null): boolean => {
const isCurtainsRenderer = renderer && renderer.type === 'GPUCurtainsRenderer'
if (!isCurtainsRenderer) {
formatRendererError(renderer, 'GPUCurtainsRenderer', type)
}
return isCurtainsRenderer
}
/**
* Helper to generate mips on the GPU
* Taken from https://webgpufundamentals.org/webgpu/lessons/webgpu-importing-textures.html
*/
export const generateMips = (() => {
let sampler
let module
const pipelineByFormat = {}
return function generateMips(device: GPUDevice, texture: GPUTexture) {
if (!module) {
module = device.createShaderModule({
label: 'textured quad shaders for mip level generation',
code: `
struct VSOutput {
@builtin(position) position: vec4f,
@location(0) texcoord: vec2f,
};
@vertex fn vs(
@builtin(vertex_index) vertexIndex : u32
) -> VSOutput {
var pos = array<vec2f, 6>(
vec2f( 0.0, 0.0), // center
vec2f( 1.0, 0.0), // right, center
vec2f( 0.0, 1.0), // center, top
// 2st triangle
vec2f( 0.0, 1.0), // center, top
vec2f( 1.0, 0.0), // right, center
vec2f( 1.0, 1.0), // right, top
);
var vsOutput: VSOutput;
let xy = pos[vertexIndex];
vsOutput.position = vec4f(xy * 2.0 - 1.0, 0.0, 1.0);
vsOutput.texcoord = vec2f(xy.x, 1.0 - xy.y);
return vsOutput;
}
@group(0) @binding(0) var ourSampler: sampler;
@group(0) @binding(1) var ourTexture: texture_2d<f32>;
@fragment fn fs(fsInput: VSOutput) -> @location(0) vec4f {
return textureSample(ourTexture, ourSampler, fsInput.texcoord);
}
`,
})
sampler = device.createSampler({
minFilter: 'linear',
})
}
if (!pipelineByFormat[texture.format]) {
pipelineByFormat[texture.format] = device.createRenderPipeline({
label: 'mip level generator pipeline',
layout: 'auto',
vertex: {
module,
entryPoint: 'vs',
},
fragment: {
module,
entryPoint: 'fs',
targets: [{ format: texture.format }],
},
})
}
const pipeline = pipelineByFormat[texture.format]
const encoder = device.createCommandEncoder({
label: 'mip gen encoder',
})
let width = texture.width
let height = texture.height
let baseMipLevel = 0
while (width > 1 || height > 1) {
width = Math.max(1, (width / 2) | 0)
height = Math.max(1, (height / 2) | 0)
const bindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [
{ binding: 0, resource: sampler },
{
binding: 1,
resource: texture.createView({
baseMipLevel,
mipLevelCount: 1,
}),
},
],
})
++baseMipLevel
const renderPassDescriptor = {
label: 'our basic canvas renderPass',
colorAttachments: [
{
view: texture.createView({ baseMipLevel, mipLevelCount: 1 }),
loadOp: 'clear',
storeOp: 'store',
},
],
}
const pass = encoder.beginRenderPass(renderPassDescriptor as GPURenderPassDescriptor)
pass.setPipeline(pipeline)
pass.setBindGroup(0, bindGroup)
pass.draw(6) // call our vertex shader 6 times
pass.end()
}
const commandBuffer = encoder.finish()
device.queue.submit([commandBuffer])
}
})()