|
| 1 | +// Copyright 2020 Brandon Jones |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | + |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | + |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +import { GPUIndexFormat, GPUFilterMode, GPUPrimitiveTopology } from './constants.js'; |
| 22 | + |
| 23 | +// ported from https://github.com/toji/web-texture-tool/blob/master/src/webgpu-mipmap-generator.js |
| 24 | + |
| 25 | +class WebGPUTextureUtils { |
| 26 | + |
| 27 | + constructor( device, glslang ) { |
| 28 | + |
| 29 | + this.device = device; |
| 30 | + |
| 31 | + const mipmapVertexSource = `#version 450 |
| 32 | + const vec2 pos[4] = vec2[4](vec2(-1.0f, 1.0f), vec2(1.0f, 1.0f), vec2(-1.0f, -1.0f), vec2(1.0f, -1.0f)); |
| 33 | + const vec2 tex[4] = vec2[4](vec2(0.0f, 0.0f), vec2(1.0f, 0.0f), vec2(0.0f, 1.0f), vec2(1.0f, 1.0f)); |
| 34 | + layout(location = 0) out vec2 vTex; |
| 35 | + void main() { |
| 36 | + vTex = tex[gl_VertexIndex]; |
| 37 | + gl_Position = vec4(pos[gl_VertexIndex], 0.0, 1.0); |
| 38 | + } |
| 39 | + `; |
| 40 | + |
| 41 | + const mipmapFragmentSource = `#version 450 |
| 42 | + layout(set = 0, binding = 0) uniform sampler imgSampler; |
| 43 | + layout(set = 0, binding = 1) uniform texture2D img; |
| 44 | + layout(location = 0) in vec2 vTex; |
| 45 | + layout(location = 0) out vec4 outColor; |
| 46 | + void main() { |
| 47 | + outColor = texture(sampler2D(img, imgSampler), vTex); |
| 48 | + }`; |
| 49 | + |
| 50 | + this.sampler = device.createSampler( { minFilter: GPUFilterMode.Linear } ); |
| 51 | + |
| 52 | + // We'll need a new pipeline for every texture format used. |
| 53 | + this.pipelines = {}; |
| 54 | + |
| 55 | + this.mipmapVertexShaderModule = device.createShaderModule( { |
| 56 | + code: glslang.compileGLSL( mipmapVertexSource, 'vertex' ), |
| 57 | + } ); |
| 58 | + this.mipmapFragmentShaderModule = device.createShaderModule( { |
| 59 | + code: glslang.compileGLSL( mipmapFragmentSource, 'fragment' ), |
| 60 | + } ); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + getMipmapPipeline( format ) { |
| 65 | + |
| 66 | + let pipeline = this.pipelines[ format ]; |
| 67 | + |
| 68 | + if ( pipeline === undefined ) { |
| 69 | + |
| 70 | + pipeline = this.device.createRenderPipeline( { |
| 71 | + vertexStage: { |
| 72 | + module: this.mipmapVertexShaderModule, |
| 73 | + entryPoint: 'main', |
| 74 | + }, |
| 75 | + fragmentStage: { |
| 76 | + module: this.mipmapFragmentShaderModule, |
| 77 | + entryPoint: 'main', |
| 78 | + }, |
| 79 | + primitiveTopology: GPUPrimitiveTopology.TriangleStrip, |
| 80 | + vertexState: { |
| 81 | + indexFormat: GPUIndexFormat.Uint32 |
| 82 | + }, |
| 83 | + colorStates: [ { format } ], |
| 84 | + } ); |
| 85 | + this.pipelines[ format ] = pipeline; |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + return pipeline; |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + generateMipmappedTexture( imageBitmap, textureGPU, textureGPUDescriptor ) { |
| 94 | + |
| 95 | + const pipeline = this.getMipmapPipeline( textureGPUDescriptor.format ); |
| 96 | + |
| 97 | + const commandEncoder = this.device.createCommandEncoder( {} ); |
| 98 | + const bindGroupLayout = pipeline.getBindGroupLayout( 0 ); // @TODO: Consider making this static. |
| 99 | + |
| 100 | + let srcView = textureGPU.createView( { |
| 101 | + baseMipLevel: 0, |
| 102 | + mipLevelCount: 1, |
| 103 | + } ); |
| 104 | + |
| 105 | + for ( let i = 1; i < textureGPUDescriptor.mipLevelCount; i ++ ) { |
| 106 | + |
| 107 | + const dstView = textureGPU.createView( { |
| 108 | + baseMipLevel: i, |
| 109 | + mipLevelCount: 1, |
| 110 | + } ); |
| 111 | + |
| 112 | + const passEncoder = commandEncoder.beginRenderPass( { |
| 113 | + colorAttachments: [ { |
| 114 | + attachment: dstView, |
| 115 | + loadValue: [ 0, 0, 0, 0 ], |
| 116 | + } ], |
| 117 | + } ); |
| 118 | + |
| 119 | + const bindGroup = this.device.createBindGroup( { |
| 120 | + layout: bindGroupLayout, |
| 121 | + entries: [ { |
| 122 | + binding: 0, |
| 123 | + resource: this.sampler, |
| 124 | + }, { |
| 125 | + binding: 1, |
| 126 | + resource: srcView, |
| 127 | + } ], |
| 128 | + } ); |
| 129 | + |
| 130 | + passEncoder.setPipeline( pipeline ); |
| 131 | + passEncoder.setBindGroup( 0, bindGroup ); |
| 132 | + passEncoder.draw( 4, 1, 0, 0 ); |
| 133 | + passEncoder.endPass(); |
| 134 | + |
| 135 | + srcView = dstView; |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + this.device.defaultQueue.submit( [ commandEncoder.finish() ] ); |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +export default WebGPUTextureUtils; |
0 commit comments