|
| 1 | +import generateMipmapWGSL from './generateMipmap.wgsl'; |
| 2 | + |
| 3 | +export function numMipLevels(...sizes: number[]) { |
| 4 | + const maxSize = Math.max(...sizes); |
| 5 | + return (1 + Math.log2(maxSize)) | 0; |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * Get the default viewDimension |
| 10 | + * Note: It's only a guess. The user needs to tell us to be |
| 11 | + * correct in all cases because we can't distinguish between |
| 12 | + * a 2d texture and a 2d-array texture with 1 layer, nor can |
| 13 | + * we distinguish between a 2d-array texture with 6 layers and |
| 14 | + * a cubemap. |
| 15 | + */ |
| 16 | +export function getDefaultViewDimensionForTexture( |
| 17 | + dimension: GPUTextureDimension, |
| 18 | + depthOrArrayLayers: number |
| 19 | +) { |
| 20 | + switch (dimension) { |
| 21 | + case '1d': |
| 22 | + return '1d'; |
| 23 | + default: |
| 24 | + case '2d': |
| 25 | + return depthOrArrayLayers > 1 ? '2d-array' : '2d'; |
| 26 | + case '3d': |
| 27 | + return '3d'; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +type DeviceSpecificInfo = { |
| 32 | + sampler: GPUSampler; |
| 33 | + module: GPUShaderModule; |
| 34 | + pipelineByFormatAndView: Map<string, GPURenderPipeline>; |
| 35 | +}; |
| 36 | + |
| 37 | +function createDeviceSpecificInfo(device: GPUDevice): DeviceSpecificInfo { |
| 38 | + const module = device.createShaderModule({ |
| 39 | + label: 'textured quad shaders for mip level generation', |
| 40 | + code: generateMipmapWGSL, |
| 41 | + }); |
| 42 | + |
| 43 | + const sampler = device.createSampler({ |
| 44 | + minFilter: 'linear', |
| 45 | + magFilter: 'linear', |
| 46 | + }); |
| 47 | + |
| 48 | + return { |
| 49 | + module, |
| 50 | + sampler, |
| 51 | + pipelineByFormatAndView: new Map(), |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +const s_deviceToDeviceSpecificInfo = new WeakMap< |
| 56 | + GPUDevice, |
| 57 | + DeviceSpecificInfo |
| 58 | +>(); |
| 59 | + |
| 60 | +/** |
| 61 | + * Generates mip levels 1 to texture.mipLevelCount - 1 using |
| 62 | + * mip level 0 as the source. |
| 63 | + * @param device The device |
| 64 | + * @param texture The texture to generate mips for |
| 65 | + * @param textureBindingViewDimension The view dimension, needed for |
| 66 | + * compatibility mode if the texture is a cube map OR if the texture |
| 67 | + * is a 1 layer 2d-array. |
| 68 | + */ |
| 69 | +export function generateMips( |
| 70 | + device: GPUDevice, |
| 71 | + texture: GPUTexture, |
| 72 | + textureBindingViewDimension?: GPUTextureViewDimension |
| 73 | +) { |
| 74 | + textureBindingViewDimension = |
| 75 | + textureBindingViewDimension ?? |
| 76 | + getDefaultViewDimensionForTexture( |
| 77 | + texture.dimension, |
| 78 | + texture.depthOrArrayLayers |
| 79 | + ); |
| 80 | + const deviceSpecificInfo = |
| 81 | + s_deviceToDeviceSpecificInfo.get(device) ?? |
| 82 | + createDeviceSpecificInfo(device); |
| 83 | + s_deviceToDeviceSpecificInfo.set(device, deviceSpecificInfo); |
| 84 | + const { sampler, module, pipelineByFormatAndView } = deviceSpecificInfo; |
| 85 | + |
| 86 | + const id = `${texture.format}.${textureBindingViewDimension}`; |
| 87 | + |
| 88 | + if (!pipelineByFormatAndView.get(id)) { |
| 89 | + // Choose an fragment shader based on the viewDimension (removes the '-' from 2d-array and cube-array) |
| 90 | + const entryPoint = `fs${textureBindingViewDimension.replace('-', '')}`; |
| 91 | + pipelineByFormatAndView.set( |
| 92 | + id, |
| 93 | + device.createRenderPipeline({ |
| 94 | + label: `mip level generator pipeline for ${textureBindingViewDimension}, format: ${texture.format}`, |
| 95 | + layout: 'auto', |
| 96 | + vertex: { |
| 97 | + module, |
| 98 | + }, |
| 99 | + fragment: { |
| 100 | + module, |
| 101 | + entryPoint, |
| 102 | + targets: [{ format: texture.format }], |
| 103 | + }, |
| 104 | + }) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + const pipeline = pipelineByFormatAndView.get(id); |
| 109 | + const encoder = device.createCommandEncoder({ |
| 110 | + label: 'mip gen encoder', |
| 111 | + }); |
| 112 | + |
| 113 | + // For each mip level > 0, sample the previous mip level |
| 114 | + // while rendering into this one. |
| 115 | + for ( |
| 116 | + let baseMipLevel = 1; |
| 117 | + baseMipLevel < texture.mipLevelCount; |
| 118 | + ++baseMipLevel |
| 119 | + ) { |
| 120 | + for (let layer = 0; layer < texture.depthOrArrayLayers; ++layer) { |
| 121 | + const bindGroup = device.createBindGroup({ |
| 122 | + layout: pipeline.getBindGroupLayout(0), |
| 123 | + entries: [ |
| 124 | + { binding: 0, resource: sampler }, |
| 125 | + { |
| 126 | + binding: 1, |
| 127 | + resource: texture.createView({ |
| 128 | + dimension: textureBindingViewDimension, |
| 129 | + baseMipLevel: baseMipLevel - 1, |
| 130 | + mipLevelCount: 1, |
| 131 | + }), |
| 132 | + }, |
| 133 | + ], |
| 134 | + }); |
| 135 | + |
| 136 | + const renderPassDescriptor: GPURenderPassDescriptor = { |
| 137 | + label: 'our basic canvas renderPass', |
| 138 | + colorAttachments: [ |
| 139 | + { |
| 140 | + view: texture.createView({ |
| 141 | + dimension: '2d', |
| 142 | + baseMipLevel, |
| 143 | + mipLevelCount: 1, |
| 144 | + baseArrayLayer: layer, |
| 145 | + arrayLayerCount: 1, |
| 146 | + }), |
| 147 | + loadOp: 'clear', |
| 148 | + storeOp: 'store', |
| 149 | + }, |
| 150 | + ], |
| 151 | + }; |
| 152 | + |
| 153 | + const pass = encoder.beginRenderPass(renderPassDescriptor); |
| 154 | + pass.setPipeline(pipeline); |
| 155 | + pass.setBindGroup(0, bindGroup); |
| 156 | + // draw 3 vertices, 1 instance, first instance (instance_index) = layer |
| 157 | + pass.draw(3, 1, 0, layer); |
| 158 | + pass.end(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + const commandBuffer = encoder.finish(); |
| 163 | + device.queue.submit([commandBuffer]); |
| 164 | +} |
0 commit comments