Add resizing
This commit is contained in:
parent
9c7b91000f
commit
058d6014b7
18 changed files with 254 additions and 138 deletions
63
src/utils/graphics/resizable-texture.ts
Normal file
63
src/utils/graphics/resizable-texture.ts
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import { CopyPipeline } from '../../pipelines/copy/copy-pipeline';
|
||||
|
||||
import { vec2 } from 'gl-matrix';
|
||||
|
||||
export class ResizableTexture {
|
||||
private texture: GPUTexture;
|
||||
private textureView: GPUTextureView;
|
||||
private readonly copyPipeline: CopyPipeline;
|
||||
private size: vec2 | null = null;
|
||||
|
||||
public constructor(private readonly device: GPUDevice, size: vec2) {
|
||||
this.copyPipeline = new CopyPipeline(this.device);
|
||||
this.resize(size);
|
||||
}
|
||||
|
||||
public resize(size: vec2): void {
|
||||
if (this.size !== null && vec2.equals(this.size, size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newTexture = this.device.createTexture({
|
||||
format: 'rgba16float',
|
||||
dimension: '2d',
|
||||
mipLevelCount: 1,
|
||||
size: {
|
||||
width: size.x,
|
||||
height: size.y,
|
||||
depthOrArrayLayers: 1,
|
||||
},
|
||||
usage:
|
||||
GPUTextureUsage.STORAGE_BINDING |
|
||||
GPUTextureUsage.TEXTURE_BINDING |
|
||||
GPUTextureUsage.RENDER_ATTACHMENT,
|
||||
});
|
||||
|
||||
const newTextureView = newTexture.createView();
|
||||
|
||||
if (this.textureView) {
|
||||
const commandEncoder = this.device.createCommandEncoder();
|
||||
this.copyPipeline.execute(
|
||||
commandEncoder,
|
||||
this.textureView,
|
||||
newTextureView,
|
||||
vec2.div(vec2.create(), this.size, size)
|
||||
);
|
||||
this.device.queue.submit([commandEncoder.finish()]);
|
||||
this.texture.destroy();
|
||||
}
|
||||
|
||||
this.size = size;
|
||||
this.texture = newTexture;
|
||||
this.textureView = newTextureView;
|
||||
}
|
||||
|
||||
public getTextureView(): GPUTextureView {
|
||||
return this.textureView;
|
||||
}
|
||||
|
||||
public destroy(): void {
|
||||
this.texture.destroy();
|
||||
this.copyPipeline.destroy();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue