Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer ImageBitmapLoader and dispose of textures after GPU upload #21

Merged
merged 8 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,8 @@ THREE.GLTFLoader = ( function () {

onLoad( result );

parser.cache.removeAll();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gltf parser also keeps a cache of things while its loading, don't see any reason we would want to keep it around after load, and it was retaining things.


} ).catch( onError );

};
Expand Down
16 changes: 11 additions & 5 deletions src/geometries/PlaneGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Float32BufferAttribute } from '../core/BufferAttribute.js';

// PlaneGeometry

function PlaneGeometry( width, height, widthSegments, heightSegments ) {
function PlaneGeometry( width, height, widthSegments, heightSegments, flipY ) {

Geometry.call( this );

Expand All @@ -19,7 +19,8 @@ function PlaneGeometry( width, height, widthSegments, heightSegments ) {
width: width,
height: height,
widthSegments: widthSegments,
heightSegments: heightSegments
heightSegments: heightSegments,
flipY
};

this.fromBufferGeometry( new PlaneBufferGeometry( width, height, widthSegments, heightSegments ) );
Expand All @@ -32,7 +33,7 @@ PlaneGeometry.prototype.constructor = PlaneGeometry;

// PlaneBufferGeometry

function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) {
function PlaneBufferGeometry( width, height, widthSegments, heightSegments, flipY = true ) {

BufferGeometry.call( this );

Expand All @@ -42,7 +43,8 @@ function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) {
width: width,
height: height,
widthSegments: widthSegments,
heightSegments: heightSegments
heightSegments: heightSegments,
flipY: flipY
};

width = width || 1;
Expand Down Expand Up @@ -84,7 +86,11 @@ function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) {
normals.push( 0, 0, 1 );

uvs.push( ix / gridX );
uvs.push( 1 - ( iy / gridY ) );
uvs.push(
flipY ?
1 - ( iy / gridY ) :
iy / gridY
);

}

Expand Down
26 changes: 25 additions & 1 deletion src/loaders/TextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

import { RGBAFormat, RGBFormat } from '../constants.js';
import { ImageLoader } from './ImageLoader.js';
import { ImageBitmapLoader } from './ImageBitmapLoader.js';
import { Texture } from '../textures/Texture.js';
import { DefaultLoadingManager } from './LoadingManager.js';

import { Cache } from './Cache.js';

function TextureLoader( manager ) {

Expand All @@ -22,12 +24,27 @@ Object.assign( TextureLoader.prototype, {

var texture = new Texture();

var loader = new ImageLoader( this.manager );
var loader;
if ( window.createImageBitmap !== undefined ) {

loader = new ImageBitmapLoader( this.manager );
texture.flipY = false;

} else {

loader = new ImageLoader( this.manager );

}

loader.setCrossOrigin( this.crossOrigin );
loader.setPath( this.path );

const cacheKey = this.manager.resolveURL( url );
loader.load( url, function ( image ) {

// Image was just added to cache before this function gets called, disable caching by immediatly removing it
Cache.remove( cacheKey );

texture.image = image;

// JPEGs can't have an alpha channel, so memory can be saved by storing them as RGB.
Expand All @@ -36,6 +53,13 @@ Object.assign( TextureLoader.prototype, {
texture.format = isJPEG ? RGBFormat : RGBAFormat;
texture.needsUpdate = true;

texture.onUpdate = function () {

texture.image.close && texture.image.close();
delete texture.image;

};

if ( onLoad !== undefined ) {

onLoad( texture );
Expand Down
10 changes: 10 additions & 0 deletions src/renderers/webgl/WebGLTextures.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,16 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,

}

// if ( window.ImageBitmap && texture.image instanceof ImageBitmap ) {

// console.info( "upload texture", "ImageBitmap", texture.id );

// } else if ( texture.image instanceof HTMLImageElement ) {

// console.info( "upload texture", "HTMLImageElement", texture.id, texture.image.src );

// }

}

if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
Expand Down