123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- import { GPUTextureFormat, GPUAddressMode, GPUFilterMode } from './constants.js';
- import { Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, RepeatWrapping, MirroredRepeatWrapping, FloatType, HalfFloatType } from '../../../../build/three.module.js';
- class WebGPUTextures {
- constructor( device, properties, info ) {
- this.device = device;
- this.properties = properties;
- this.info = info;
- this.defaultTexture = null;
- this.defaultSampler = null;
- this.samplerCache = new Map();
- }
- getDefaultSampler() {
- if ( this.defaultSampler === null ) {
- this.defaultSampler = this.device.createSampler( {} );
- }
- return this.defaultSampler;
- }
- getDefaultTexture() {
- if ( this.defaultTexture === null ) {
- this.defaultTexture = this._createTexture( new Texture() );
- }
- return this.defaultTexture;
- }
- getTextureGPU( texture ) {
- const textureProperties = this.properties.get( texture );
- return textureProperties.textureGPU;
- }
- getSampler( texture ) {
- const textureProperties = this.properties.get( texture );
- return textureProperties.samplerGPU;
- }
- updateTexture( texture ) {
- let forceUpdate = false;
- const textureProperties = this.properties.get( texture );
- if ( texture.version > 0 && textureProperties.version !== texture.version ) {
- const image = texture.image;
- if ( image === undefined ) {
- console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is undefined' );
- } else if ( image.complete === false ) {
- console.warn( 'THREE.WebGPURenderer: Texture marked for update but image is incomplete' );
- } else {
- // texture init
- if ( textureProperties.initialized === undefined ) {
- textureProperties.initialized = true;
- const disposeCallback = onTextureDispose.bind( this );
- textureProperties.disposeCallback = disposeCallback;
- texture.addEventListener( 'dispose', disposeCallback );
- this.info.memory.textures ++;
- }
- // texture creation
- if ( textureProperties.textureGPU !== undefined ) {
- // TODO: Avoid calling of destroy() in certain scenarios. When only the contents of a texture
- // are updated, a buffer upload should be sufficient. However, if the user changes
- // the dimensions of the texture, format or usage, a new instance of GPUTexture is required.
- textureProperties.textureGPU.destroy();
- }
- textureProperties.textureGPU = this._createTexture( texture );
- textureProperties.version = texture.version;
- forceUpdate = true;
- }
- }
- // if the texture is used for RTT, it's necessary to init it once so the binding
- // group's resource definition points to the respective GPUTexture
- if ( textureProperties.initializedRTT === false ) {
- textureProperties.initializedRTT = true;
- forceUpdate = true;
- }
- return forceUpdate;
- }
- updateSampler( texture ) {
- const array = [];
- array.push( texture.wrapS );
- array.push( texture.wrapT );
- array.push( texture.wrapR );
- array.push( texture.magFilter );
- array.push( texture.minFilter );
- array.push( texture.anisotropy );
- const key = array.join();
- let samplerGPU = this.samplerCache.get( key );
- if ( samplerGPU === undefined ) {
- samplerGPU = this.device.createSampler( {
- addressModeU: this._convertAddressMode( texture.wrapS ),
- addressModeV: this._convertAddressMode( texture.wrapT ),
- addressModeW: this._convertAddressMode( texture.wrapR ),
- magFilter: this._convertFilterMode( texture.magFilter ),
- minFilter: this._convertFilterMode( texture.minFilter ),
- mipmapFilter: this._convertFilterMode( texture.minFilter ),
- maxAnisotropy: texture.anisotropy
- } );
- this.samplerCache.set( key, samplerGPU );
- }
- const textureProperties = this.properties.get( texture );
- textureProperties.samplerGPU = samplerGPU;
- }
- initRenderTarget( renderTarget ) {
- const properties = this.properties;
- const renderTargetProperties = properties.get( renderTarget );
- if ( renderTargetProperties.initialized === undefined ) {
- const device = this.device;
- const width = renderTarget.width;
- const height = renderTarget.height;
- const colorTextureGPU = device.createTexture( {
- size: {
- width: width,
- height: height,
- depth: 1
- },
- format: GPUTextureFormat.BRGA8Unorm, // TODO: Make configurable
- usage: GPUTextureUsage.OUTPUT_ATTACHMENT | GPUTextureUsage.SAMPLED
- } );
- renderTargetProperties.colorTextureGPU = colorTextureGPU;
- // When the ".texture" or ".depthTexture" property of a render target is used as a map,
- // the renderer has to find the respective GPUTexture objects to setup the bind groups.
- // Since it's not possible to see just from a texture object whether it belongs to a render
- // target or not, we need the initializedRTT flag.
- const textureProperties = properties.get( renderTarget.texture );
- textureProperties.textureGPU = colorTextureGPU;
- textureProperties.initializedRTT = false;
- if ( renderTarget.depthBuffer === true ) {
- const depthTextureGPU = device.createTexture( {
- size: {
- width: width,
- height: height,
- depth: 1
- },
- format: GPUTextureFormat.Depth24PlusStencil8, // TODO: Make configurable
- usage: GPUTextureUsage.OUTPUT_ATTACHMENT
- } );
- renderTargetProperties.depthTextureGPU = depthTextureGPU;
- if ( renderTarget.depthTexture !== null ) {
- const depthTextureProperties = properties.get( renderTarget.depthTexture );
- depthTextureProperties.textureGPU = depthTextureGPU;
- depthTextureProperties.initializedRTT = false;
- }
- }
- renderTargetProperties.initialized = true;
- }
- }
- dispose() {
- this.samplerCache.clear();
- }
- _convertAddressMode( value ) {
- let addressMode = GPUAddressMode.ClampToEdge;
- if ( value === RepeatWrapping ) {
- addressMode = GPUAddressMode.Repeat;
- } else if ( value === MirroredRepeatWrapping ) {
- addressMode = GPUAddressMode.MirrorRepeat;
- }
- return addressMode;
- }
- _convertFilterMode( value ) {
- let filterMode = GPUFilterMode.Linear;
- if ( value === NearestFilter || value === NearestMipmapNearestFilter || value === NearestMipmapLinearFilter ) {
- filterMode = GPUFilterMode.Nearest;
- }
- return filterMode;
- }
- _convertFormat( type ) {
- let formatGPU = GPUTextureFormat.RGBA8Unorm;
- if ( type === FloatType ) {
- formatGPU = GPUTextureFormat.RGBA32Float;
- } else if ( type === HalfFloatType ) {
- formatGPU = GPUTextureFormat.RGBA16Float;
- }
- return formatGPU;
- }
- _createTexture( texture ) {
- const device = this.device;
- const image = texture.image;
- const width = ( image !== undefined ) ? image.width : 1;
- const height = ( image !== undefined ) ? image.height : 1;
- const format = this._convertFormat( texture.type );
- const textureGPU = device.createTexture( {
- size: {
- width: width,
- height: height,
- depth: 1,
- },
- format: format,
- usage: GPUTextureUsage.SAMPLED | GPUTextureUsage.COPY_DST,
- } );
- if ( texture.isDataTexture ) {
- this._copyBufferToTexture( image, format, textureGPU );
- } else {
- // convert HTML iamges and canvas elements to ImageBitmap before copy
- if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
- ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ) {
- const options = {};
- options.imageOrientation = ( texture.flipY === true ) ? 'flipY' : 'none';
- options.premultiplyAlpha = ( texture.premultiplyAlpha === true ) ? 'premultiply' : 'default';
- createImageBitmap( image, 0, 0, width, height, options ).then( imageBitmap => {
- this._copyImageBitmapToTexture( imageBitmap, textureGPU );
- } );
- } else {
- if ( image !== undefined ) {
- // assuming ImageBitmap. Directly start copy operation of the contents of ImageBitmap into the destination texture
- this._copyImageBitmapToTexture( image, textureGPU );
- }
- }
- }
- return textureGPU;
- }
- _copyBufferToTexture( image, format, textureGPU ) {
- // this code assumes data textures in RGBA format
- // TODO: Consider to support valid buffer layouts with other formats like RGB
- const device = this.device;
- const data = image.data;
- const bytesPerTexel = this._getBytesPerTexel( format );
- const bytesPerRow = Math.ceil( image.width * bytesPerTexel / 256 ) * 256;
- const textureDataBuffer = device.createBuffer( {
- size: data.byteLength,
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
- mappedAtCreation: true,
- } );
- new data.constructor( textureDataBuffer.getMappedRange() ).set( data );
- textureDataBuffer.unmap();
- const commandEncoder = device.createCommandEncoder( {} );
- commandEncoder.copyBufferToTexture(
- {
- buffer: textureDataBuffer,
- bytesPerRow: bytesPerRow
- }, {
- texture: textureGPU
- }, {
- width: image.width,
- height: image.height,
- depth: 1
- } );
- device.defaultQueue.submit( [ commandEncoder.finish() ] );
- textureDataBuffer.destroy();
- }
- _getBytesPerTexel( format ) {
- if ( format === GPUTextureFormat.RGBA8Unorm ) return 4;
- if ( format === GPUTextureFormat.RGBA16Float ) return 8;
- if ( format === GPUTextureFormat.RGBA32Float ) return 16;
- }
- _copyImageBitmapToTexture( imageBitmap, textureGPU ) {
- const device = this.device;
- device.defaultQueue.copyImageBitmapToTexture(
- {
- imageBitmap: imageBitmap
- }, {
- texture: textureGPU
- }, {
- width: imageBitmap.width,
- height: imageBitmap.height,
- depth: 1
- }
- );
- }
- }
- function onTextureDispose( event ) {
- const texture = event.target;
- const textureProperties = this.properties.get( texture );
- textureProperties.textureGPU.destroy();
- texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
- this.properties.remove( texture );
- this.info.memory.textures --;
- }
- export default WebGPUTextures;
|