-
Notifications
You must be signed in to change notification settings - Fork 10
/
webgpu-mipmap-generator.js
193 lines (170 loc) · 6.45 KB
/
webgpu-mipmap-generator.js
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
export class WebGPUMipmapGenerator {
constructor(device) {
this.device = device;
this.sampler = device.createSampler({minFilter: 'linear'});
// We'll need a new pipeline for every texture format used.
this.pipelines = {};
}
getMipmapPipeline(format) {
let pipeline = this.pipelines[format];
if (!pipeline) {
// Shader modules is shared between all pipelines, so only create once.
if (!this.mipmapShaderModule) {
this.mipmapShaderModule = this.device.createShaderModule({
label: 'Mipmap Generator',
code: `
var<private> pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
vec2<f32>(-1.0, -1.0), vec2<f32>(-1.0, 3.0), vec2<f32>(3.0, -1.0));
struct VertexOutput {
@builtin(position) position : vec4<f32>,
@location(0) texCoord : vec2<f32>,
};
@vertex
fn vertexMain(@builtin(vertex_index) vertexIndex : u32) -> VertexOutput {
var output : VertexOutput;
output.texCoord = pos[vertexIndex] * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5);
output.position = vec4<f32>(pos[vertexIndex], 0.0, 1.0);
return output;
}
@group(0) @binding(0) var imgSampler : sampler;
@group(0) @binding(1) var img : texture_2d<f32>;
@fragment
fn fragmentMain(@location(0) texCoord : vec2<f32>) -> @location(0) vec4<f32> {
return textureSample(img, imgSampler, texCoord);
}
`,
});
this.bindGroupLayout = this.device.createBindGroupLayout({
label: 'Mipmap Generator',
entries: [{
binding: 0,
visibility: GPUShaderStage.FRAGMENT,
sampler: {},
}, {
binding: 1,
visibility: GPUShaderStage.FRAGMENT,
texture: {},
}]
});
this.pipelineLayout = this.device.createPipelineLayout({
label: 'Mipmap Generator',
bindGroupLayouts: [ this.bindGroupLayout ],
})
}
pipeline = this.device.createRenderPipeline({
layout: this.pipelineLayout,
vertex: {
module: this.mipmapShaderModule,
entryPoint: 'vertexMain',
},
fragment: {
module: this.mipmapShaderModule,
entryPoint: 'fragmentMain',
targets: [{format}],
}
});
this.pipelines[format] = pipeline;
}
return pipeline;
}
/**
* Generates mipmaps for the given GPUTexture from the data in level 0.
*
* @param {module:External.GPUTexture} texture - Texture to generate mipmaps for.
* @param {object} textureDescriptor - GPUTextureDescriptor the texture was created with.
* @returns {module:External.GPUTexture} - The originally passed texture
*/
generateMipmap(texture, textureDescriptor) {
// TODO: Does this need to handle sRGB formats differently?
const pipeline = this.getMipmapPipeline(textureDescriptor.format);
if (textureDescriptor.dimension == '3d' || textureDescriptor.dimension == '1d') {
throw new Error('Generating mipmaps for non-2d textures is currently unsupported!');
}
let mipTexture = texture;
const arrayLayerCount = textureDescriptor.size.depthOrArrayLayers || 1; // Only valid for 2D textures.
// If the texture was created with RENDER_ATTACHMENT usage we can render directly between mip levels.
const renderToSource = textureDescriptor.usage & GPUTextureUsage.RENDER_ATTACHMENT;
if (!renderToSource) {
// Otherwise we have to use a separate texture to render into. It can be one mip level smaller than the source
// texture, since we already have the top level.
const mipTextureDescriptor = {
size: {
width: Math.max(1, textureDescriptor.size.width >>> 1),
height: Math.max(1, textureDescriptor.size.height >>> 1),
depthOrArrayLayers: arrayLayerCount,
},
format: textureDescriptor.format,
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC | GPUTextureUsage.RENDER_ATTACHMENT,
mipLevelCount: textureDescriptor.mipLevelCount - 1,
};
mipTexture = this.device.createTexture(mipTextureDescriptor);
}
const commandEncoder = this.device.createCommandEncoder({});
for (let arrayLayer = 0; arrayLayer < arrayLayerCount; ++arrayLayer) {
let srcView = texture.createView({
baseMipLevel: 0,
mipLevelCount: 1,
dimension: '2d',
baseArrayLayer: arrayLayer,
arrayLayerCount: 1,
});
let dstMipLevel = renderToSource ? 1 : 0;
for (let i = 1; i < textureDescriptor.mipLevelCount; ++i) {
const dstView = mipTexture.createView({
baseMipLevel: dstMipLevel++,
mipLevelCount: 1,
dimension: '2d',
baseArrayLayer: arrayLayer,
arrayLayerCount: 1,
});
const passEncoder = commandEncoder.beginRenderPass({
colorAttachments: [{
view: dstView,
loadOp: 'clear',
storeOp: 'store'
}],
});
const bindGroup = this.device.createBindGroup({
layout: this.bindGroupLayout,
entries: [{
binding: 0,
resource: this.sampler,
}, {
binding: 1,
resource: srcView,
}],
});
passEncoder.setPipeline(pipeline);
passEncoder.setBindGroup(0, bindGroup);
passEncoder.draw(3, 1, 0, 0);
passEncoder.end();
srcView = dstView;
}
}
// If we didn't render to the source texture, finish by copying the mip results from the temporary mipmap texture
// to the source.
if (!renderToSource) {
const mipLevelSize = {
width: Math.max(1, textureDescriptor.size.width >>> 1),
height: Math.max(1, textureDescriptor.size.height >>> 1),
depthOrArrayLayers: arrayLayerCount,
};
for (let i = 1; i < textureDescriptor.mipLevelCount; ++i) {
commandEncoder.copyTextureToTexture({
texture: mipTexture,
mipLevel: i-1,
}, {
texture: texture,
mipLevel: i,
}, mipLevelSize);
mipLevelSize.width = Math.max(1, mipLevelSize.width >>> 1);
mipLevelSize.height = Math.max(1, mipLevelSize.height >>> 1);
}
}
this.device.queue.submit([commandEncoder.finish()]);
if (!renderToSource) {
mipTexture.destroy();
}
return texture;
}
}