1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999 |
- import { LinearFilter, LinearMipmapLinearFilter, LinearMipmapNearestFilter, NearestFilter, NearestMipmapLinearFilter, NearestMipmapNearestFilter, RGBAFormat, DepthFormat, DepthStencilFormat, UnsignedShortType, UnsignedIntType, UnsignedInt248Type, FloatType, HalfFloatType, MirroredRepeatWrapping, ClampToEdgeWrapping, RepeatWrapping, sRGBEncoding, LinearEncoding, UnsignedByteType, _SRGBAFormat } from '../../constants.js';
- import * as MathUtils from '../../math/MathUtils.js';
- import { ImageUtils } from '../../extras/ImageUtils.js';
- import { createElementNS } from '../../utils.js';
- function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info ) {
- const isWebGL2 = capabilities.isWebGL2;
- const maxTextures = capabilities.maxTextures;
- const maxCubemapSize = capabilities.maxCubemapSize;
- const maxTextureSize = capabilities.maxTextureSize;
- const maxSamples = capabilities.maxSamples;
- const multisampledRTTExt = extensions.has( 'WEBGL_multisampled_render_to_texture' ) ? extensions.get( 'WEBGL_multisampled_render_to_texture' ) : null;
- const supportsInvalidateFramebuffer = typeof navigator === 'undefined' ? false : /OculusBrowser/g.test( navigator.userAgent );
- const _videoTextures = new WeakMap();
- let _canvas;
- const _sources = new WeakMap(); // maps WebglTexture objects to instances of Source
- // cordova iOS (as of 5.0) still uses UIWebView, which provides OffscreenCanvas,
- // also OffscreenCanvas.getContext("webgl"), but not OffscreenCanvas.getContext("2d")!
- // Some implementations may only implement OffscreenCanvas partially (e.g. lacking 2d).
- let useOffscreenCanvas = false;
- try {
- useOffscreenCanvas = typeof OffscreenCanvas !== 'undefined'
- // eslint-disable-next-line compat/compat
- && ( new OffscreenCanvas( 1, 1 ).getContext( '2d' ) ) !== null;
- } catch ( err ) {
- // Ignore any errors
- }
- function createCanvas( width, height ) {
- // Use OffscreenCanvas when available. Specially needed in web workers
- return useOffscreenCanvas ?
- // eslint-disable-next-line compat/compat
- new OffscreenCanvas( width, height ) : createElementNS( 'canvas' );
- }
- function resizeImage( image, needsPowerOfTwo, needsNewCanvas, maxSize ) {
- let scale = 1;
- // handle case if texture exceeds max size
- if ( image.width > maxSize || image.height > maxSize ) {
- scale = maxSize / Math.max( image.width, image.height );
- }
- // only perform resize if necessary
- if ( scale < 1 || needsPowerOfTwo === true ) {
- // only perform resize for certain image types
- if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
- ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
- ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
- const floor = needsPowerOfTwo ? MathUtils.floorPowerOfTwo : Math.floor;
- const width = floor( scale * image.width );
- const height = floor( scale * image.height );
- if ( _canvas === undefined ) _canvas = createCanvas( width, height );
- // cube textures can't reuse the same canvas
- const canvas = needsNewCanvas ? createCanvas( width, height ) : _canvas;
- canvas.width = width;
- canvas.height = height;
- const context = canvas.getContext( '2d' );
- context.drawImage( image, 0, 0, width, height );
- console.warn( 'THREE.WebGLRenderer: Texture has been resized from (' + image.width + 'x' + image.height + ') to (' + width + 'x' + height + ').' );
- return canvas;
- } else {
- if ( 'data' in image ) {
- console.warn( 'THREE.WebGLRenderer: Image in DataTexture is too big (' + image.width + 'x' + image.height + ').' );
- }
- return image;
- }
- }
- return image;
- }
- function isPowerOfTwo( image ) {
- return MathUtils.isPowerOfTwo( image.width ) && MathUtils.isPowerOfTwo( image.height );
- }
- function textureNeedsPowerOfTwo( texture ) {
- if ( isWebGL2 ) return false;
- return ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) ||
- ( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter );
- }
- function textureNeedsGenerateMipmaps( texture, supportsMips ) {
- return texture.generateMipmaps && supportsMips &&
- texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter;
- }
- function generateMipmap( target ) {
- _gl.generateMipmap( target );
- }
- function getInternalFormat( internalFormatName, glFormat, glType, encoding, forceLinearEncoding = false ) {
- if ( isWebGL2 === false ) return glFormat;
- if ( internalFormatName !== null ) {
- if ( _gl[ internalFormatName ] !== undefined ) return _gl[ internalFormatName ];
- console.warn( 'THREE.WebGLRenderer: Attempt to use non-existing WebGL internal format \'' + internalFormatName + '\'' );
- }
- let internalFormat = glFormat;
- if ( glFormat === _gl.RED ) {
- if ( glType === _gl.FLOAT ) internalFormat = _gl.R32F;
- if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.R16F;
- if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.R8;
- }
- if ( glFormat === _gl.RG ) {
- if ( glType === _gl.FLOAT ) internalFormat = _gl.RG32F;
- if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.RG16F;
- if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.RG8;
- }
- if ( glFormat === _gl.RGBA ) {
- if ( glType === _gl.FLOAT ) internalFormat = _gl.RGBA32F;
- if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.RGBA16F;
- if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = ( encoding === sRGBEncoding && forceLinearEncoding === false ) ? _gl.SRGB8_ALPHA8 : _gl.RGBA8;
- if ( glType === _gl.UNSIGNED_SHORT_4_4_4_4 ) internalFormat = _gl.RGBA4;
- if ( glType === _gl.UNSIGNED_SHORT_5_5_5_1 ) internalFormat = _gl.RGB5_A1;
- }
- if ( internalFormat === _gl.R16F || internalFormat === _gl.R32F ||
- internalFormat === _gl.RG16F || internalFormat === _gl.RG32F ||
- internalFormat === _gl.RGBA16F || internalFormat === _gl.RGBA32F ) {
- extensions.get( 'EXT_color_buffer_float' );
- }
- return internalFormat;
- }
- function getMipLevels( texture, image, supportsMips ) {
- if ( textureNeedsGenerateMipmaps( texture, supportsMips ) === true || ( texture.isFramebufferTexture && texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter ) ) {
- return Math.log2( Math.max( image.width, image.height ) ) + 1;
- } else if ( texture.mipmaps !== undefined && texture.mipmaps.length > 0 ) {
- // user-defined mipmaps
- return texture.mipmaps.length;
- } else if ( texture.isCompressedTexture && Array.isArray( texture.image ) ) {
- return image.mipmaps.length;
- } else {
- // texture without mipmaps (only base level)
- return 1;
- }
- }
- // Fallback filters for non-power-of-2 textures
- function filterFallback( f ) {
- if ( f === NearestFilter || f === NearestMipmapNearestFilter || f === NearestMipmapLinearFilter ) {
- return _gl.NEAREST;
- }
- return _gl.LINEAR;
- }
- //
- function onTextureDispose( event ) {
- const texture = event.target;
- texture.removeEventListener( 'dispose', onTextureDispose );
- deallocateTexture( texture );
- if ( texture.isVideoTexture ) {
- _videoTextures.delete( texture );
- }
- }
- function onRenderTargetDispose( event ) {
- const renderTarget = event.target;
- renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
- deallocateRenderTarget( renderTarget );
- }
- //
- function deallocateTexture( texture ) {
- const textureProperties = properties.get( texture );
- if ( textureProperties.__webglInit === undefined ) return;
- // check if it's necessary to remove the WebGLTexture object
- const source = texture.source;
- const webglTextures = _sources.get( source );
- if ( webglTextures ) {
- const webglTexture = webglTextures[ textureProperties.__cacheKey ];
- webglTexture.usedTimes --;
- // the WebGLTexture object is not used anymore, remove it
- if ( webglTexture.usedTimes === 0 ) {
- deleteTexture( texture );
- }
- // remove the weak map entry if no WebGLTexture uses the source anymore
- if ( Object.keys( webglTextures ).length === 0 ) {
- _sources.delete( source );
- }
- }
- properties.remove( texture );
- }
- function deleteTexture( texture ) {
- const textureProperties = properties.get( texture );
- _gl.deleteTexture( textureProperties.__webglTexture );
- const source = texture.source;
- const webglTextures = _sources.get( source );
- delete webglTextures[ textureProperties.__cacheKey ];
- info.memory.textures --;
- }
- function deallocateRenderTarget( renderTarget ) {
- const texture = renderTarget.texture;
- const renderTargetProperties = properties.get( renderTarget );
- const textureProperties = properties.get( texture );
- if ( textureProperties.__webglTexture !== undefined ) {
- _gl.deleteTexture( textureProperties.__webglTexture );
- info.memory.textures --;
- }
- if ( renderTarget.depthTexture ) {
- renderTarget.depthTexture.dispose();
- }
- if ( renderTarget.isWebGLCubeRenderTarget ) {
- for ( let i = 0; i < 6; i ++ ) {
- _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] );
- if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer[ i ] );
- }
- } else {
- _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer );
- if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer );
- if ( renderTargetProperties.__webglMultisampledFramebuffer ) _gl.deleteFramebuffer( renderTargetProperties.__webglMultisampledFramebuffer );
- if ( renderTargetProperties.__webglColorRenderbuffer ) {
- for ( let i = 0; i < renderTargetProperties.__webglColorRenderbuffer.length; i ++ ) {
- if ( renderTargetProperties.__webglColorRenderbuffer[ i ] ) _gl.deleteRenderbuffer( renderTargetProperties.__webglColorRenderbuffer[ i ] );
- }
- }
- if ( renderTargetProperties.__webglDepthRenderbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthRenderbuffer );
- }
- if ( renderTarget.isWebGLMultipleRenderTargets ) {
- for ( let i = 0, il = texture.length; i < il; i ++ ) {
- const attachmentProperties = properties.get( texture[ i ] );
- if ( attachmentProperties.__webglTexture ) {
- _gl.deleteTexture( attachmentProperties.__webglTexture );
- info.memory.textures --;
- }
- properties.remove( texture[ i ] );
- }
- }
- properties.remove( texture );
- properties.remove( renderTarget );
- }
- //
- let textureUnits = 0;
- function resetTextureUnits() {
- textureUnits = 0;
- }
- function allocateTextureUnit() {
- const textureUnit = textureUnits;
- if ( textureUnit >= maxTextures ) {
- console.warn( 'THREE.WebGLTextures: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + maxTextures );
- }
- textureUnits += 1;
- return textureUnit;
- }
- function getTextureCacheKey( texture ) {
- const array = [];
- array.push( texture.wrapS );
- array.push( texture.wrapT );
- array.push( texture.wrapR || 0 );
- array.push( texture.magFilter );
- array.push( texture.minFilter );
- array.push( texture.anisotropy );
- array.push( texture.internalFormat );
- array.push( texture.format );
- array.push( texture.type );
- array.push( texture.generateMipmaps );
- array.push( texture.premultiplyAlpha );
- array.push( texture.flipY );
- array.push( texture.unpackAlignment );
- array.push( texture.encoding );
- return array.join();
- }
- //
- function setTexture2D( texture, slot ) {
- const textureProperties = properties.get( texture );
- if ( texture.isVideoTexture ) updateVideoTexture( texture );
- if ( texture.isRenderTargetTexture === false && texture.version > 0 && textureProperties.__version !== texture.version ) {
- const image = texture.image;
- if ( image === null ) {
- console.warn( 'THREE.WebGLRenderer: Texture marked for update but no image data found.' );
- } else if ( image.complete === false ) {
- console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete' );
- } else {
- uploadTexture( textureProperties, texture, slot );
- return;
- }
- }
- state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture, _gl.TEXTURE0 + slot );
- }
- function setTexture2DArray( texture, slot ) {
- const textureProperties = properties.get( texture );
- if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
- uploadTexture( textureProperties, texture, slot );
- return;
- }
- state.bindTexture( _gl.TEXTURE_2D_ARRAY, textureProperties.__webglTexture, _gl.TEXTURE0 + slot );
- }
- function setTexture3D( texture, slot ) {
- const textureProperties = properties.get( texture );
- if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
- uploadTexture( textureProperties, texture, slot );
- return;
- }
- state.bindTexture( _gl.TEXTURE_3D, textureProperties.__webglTexture, _gl.TEXTURE0 + slot );
- }
- function setTextureCube( texture, slot ) {
- const textureProperties = properties.get( texture );
- if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
- uploadCubeTexture( textureProperties, texture, slot );
- return;
- }
- state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture, _gl.TEXTURE0 + slot );
- }
- const wrappingToGL = {
- [ RepeatWrapping ]: _gl.REPEAT,
- [ ClampToEdgeWrapping ]: _gl.CLAMP_TO_EDGE,
- [ MirroredRepeatWrapping ]: _gl.MIRRORED_REPEAT
- };
- const filterToGL = {
- [ NearestFilter ]: _gl.NEAREST,
- [ NearestMipmapNearestFilter ]: _gl.NEAREST_MIPMAP_NEAREST,
- [ NearestMipmapLinearFilter ]: _gl.NEAREST_MIPMAP_LINEAR,
- [ LinearFilter ]: _gl.LINEAR,
- [ LinearMipmapNearestFilter ]: _gl.LINEAR_MIPMAP_NEAREST,
- [ LinearMipmapLinearFilter ]: _gl.LINEAR_MIPMAP_LINEAR
- };
- function setTextureParameters( textureType, texture, supportsMips ) {
- if ( supportsMips ) {
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, wrappingToGL[ texture.wrapS ] );
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, wrappingToGL[ texture.wrapT ] );
- if ( textureType === _gl.TEXTURE_3D || textureType === _gl.TEXTURE_2D_ARRAY ) {
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_R, wrappingToGL[ texture.wrapR ] );
- }
- _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterToGL[ texture.magFilter ] );
- _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterToGL[ texture.minFilter ] );
- } else {
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
- if ( textureType === _gl.TEXTURE_3D || textureType === _gl.TEXTURE_2D_ARRAY ) {
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_R, _gl.CLAMP_TO_EDGE );
- }
- if ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) {
- console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT should be set to THREE.ClampToEdgeWrapping.' );
- }
- _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
- _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
- if ( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter ) {
- console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter.' );
- }
- }
- if ( extensions.has( 'EXT_texture_filter_anisotropic' ) === true ) {
- const extension = extensions.get( 'EXT_texture_filter_anisotropic' );
- if ( texture.magFilter === NearestFilter ) return;
- if ( texture.minFilter !== NearestMipmapLinearFilter && texture.minFilter !== LinearMipmapLinearFilter ) return;
- if ( texture.type === FloatType && extensions.has( 'OES_texture_float_linear' ) === false ) return; // verify extension for WebGL 1 and WebGL 2
- if ( isWebGL2 === false && ( texture.type === HalfFloatType && extensions.has( 'OES_texture_half_float_linear' ) === false ) ) return; // verify extension for WebGL 1 only
- if ( texture.anisotropy > 1 || properties.get( texture ).__currentAnisotropy ) {
- _gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, capabilities.getMaxAnisotropy() ) );
- properties.get( texture ).__currentAnisotropy = texture.anisotropy;
- }
- }
- }
- function initTexture( textureProperties, texture ) {
- let forceUpload = false;
- if ( textureProperties.__webglInit === undefined ) {
- textureProperties.__webglInit = true;
- texture.addEventListener( 'dispose', onTextureDispose );
- }
- // create Source <-> WebGLTextures mapping if necessary
- const source = texture.source;
- let webglTextures = _sources.get( source );
- if ( webglTextures === undefined ) {
- webglTextures = {};
- _sources.set( source, webglTextures );
- }
- // check if there is already a WebGLTexture object for the given texture parameters
- const textureCacheKey = getTextureCacheKey( texture );
- if ( textureCacheKey !== textureProperties.__cacheKey ) {
- // if not, create a new instance of WebGLTexture
- if ( webglTextures[ textureCacheKey ] === undefined ) {
- // create new entry
- webglTextures[ textureCacheKey ] = {
- texture: _gl.createTexture(),
- usedTimes: 0
- };
- info.memory.textures ++;
- // when a new instance of WebGLTexture was created, a texture upload is required
- // even if the image contents are identical
- forceUpload = true;
- }
- webglTextures[ textureCacheKey ].usedTimes ++;
- // every time the texture cache key changes, it's necessary to check if an instance of
- // WebGLTexture can be deleted in order to avoid a memory leak.
- const webglTexture = webglTextures[ textureProperties.__cacheKey ];
- if ( webglTexture !== undefined ) {
- webglTextures[ textureProperties.__cacheKey ].usedTimes --;
- if ( webglTexture.usedTimes === 0 ) {
- deleteTexture( texture );
- }
- }
- // store references to cache key and WebGLTexture object
- textureProperties.__cacheKey = textureCacheKey;
- textureProperties.__webglTexture = webglTextures[ textureCacheKey ].texture;
- }
- return forceUpload;
- }
- function uploadTexture( textureProperties, texture, slot ) {
- let textureType = _gl.TEXTURE_2D;
- if ( texture.isDataArrayTexture || texture.isCompressedArrayTexture ) textureType = _gl.TEXTURE_2D_ARRAY;
- if ( texture.isData3DTexture ) textureType = _gl.TEXTURE_3D;
- const forceUpload = initTexture( textureProperties, texture );
- const source = texture.source;
- state.bindTexture( textureType, textureProperties.__webglTexture, _gl.TEXTURE0 + slot );
- const sourceProperties = properties.get( source );
- if ( source.version !== sourceProperties.__version || forceUpload === true ) {
- state.activeTexture( _gl.TEXTURE0 + slot );
- _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
- _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
- _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
- _gl.pixelStorei( _gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, _gl.NONE );
- const needsPowerOfTwo = textureNeedsPowerOfTwo( texture ) && isPowerOfTwo( texture.image ) === false;
- let image = resizeImage( texture.image, needsPowerOfTwo, false, maxTextureSize );
- image = verifyColorSpace( texture, image );
- const supportsMips = isPowerOfTwo( image ) || isWebGL2,
- glFormat = utils.convert( texture.format, texture.encoding );
- let glType = utils.convert( texture.type ),
- glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding, texture.isVideoTexture );
- setTextureParameters( textureType, texture, supportsMips );
- let mipmap;
- const mipmaps = texture.mipmaps;
- const useTexStorage = ( isWebGL2 && texture.isVideoTexture !== true );
- const allocateMemory = ( sourceProperties.__version === undefined ) || ( forceUpload === true );
- const levels = getMipLevels( texture, image, supportsMips );
- if ( texture.isDepthTexture ) {
- // populate depth texture with dummy data
- glInternalFormat = _gl.DEPTH_COMPONENT;
- if ( isWebGL2 ) {
- if ( texture.type === FloatType ) {
- glInternalFormat = _gl.DEPTH_COMPONENT32F;
- } else if ( texture.type === UnsignedIntType ) {
- glInternalFormat = _gl.DEPTH_COMPONENT24;
- } else if ( texture.type === UnsignedInt248Type ) {
- glInternalFormat = _gl.DEPTH24_STENCIL8;
- } else {
- glInternalFormat = _gl.DEPTH_COMPONENT16; // WebGL2 requires sized internalformat for glTexImage2D
- }
- } else {
- if ( texture.type === FloatType ) {
- console.error( 'WebGLRenderer: Floating point depth texture requires WebGL2.' );
- }
- }
- // validation checks for WebGL 1
- if ( texture.format === DepthFormat && glInternalFormat === _gl.DEPTH_COMPONENT ) {
- // The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
- // DEPTH_COMPONENT and type is not UNSIGNED_SHORT or UNSIGNED_INT
- // (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
- if ( texture.type !== UnsignedShortType && texture.type !== UnsignedIntType ) {
- console.warn( 'THREE.WebGLRenderer: Use UnsignedShortType or UnsignedIntType for DepthFormat DepthTexture.' );
- texture.type = UnsignedIntType;
- glType = utils.convert( texture.type );
- }
- }
- if ( texture.format === DepthStencilFormat && glInternalFormat === _gl.DEPTH_COMPONENT ) {
- // Depth stencil textures need the DEPTH_STENCIL internal format
- // (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
- glInternalFormat = _gl.DEPTH_STENCIL;
- // The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
- // DEPTH_STENCIL and type is not UNSIGNED_INT_24_8_WEBGL.
- // (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
- if ( texture.type !== UnsignedInt248Type ) {
- console.warn( 'THREE.WebGLRenderer: Use UnsignedInt248Type for DepthStencilFormat DepthTexture.' );
- texture.type = UnsignedInt248Type;
- glType = utils.convert( texture.type );
- }
- }
- //
- if ( allocateMemory ) {
- if ( useTexStorage ) {
- state.texStorage2D( _gl.TEXTURE_2D, 1, glInternalFormat, image.width, image.height );
- } else {
- state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, null );
- }
- }
- } else if ( texture.isDataTexture ) {
- // use manually created mipmaps if available
- // if there are no manual mipmaps
- // set 0 level mipmap and then use GL to generate other mipmap levels
- if ( mipmaps.length > 0 && supportsMips ) {
- if ( useTexStorage && allocateMemory ) {
- state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, mipmaps[ 0 ].width, mipmaps[ 0 ].height );
- }
- for ( let i = 0, il = mipmaps.length; i < il; i ++ ) {
- mipmap = mipmaps[ i ];
- if ( useTexStorage ) {
- state.texSubImage2D( _gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, glType, mipmap.data );
- } else {
- state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
- }
- }
- texture.generateMipmaps = false;
- } else {
- if ( useTexStorage ) {
- if ( allocateMemory ) {
- state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, image.width, image.height );
- }
- state.texSubImage2D( _gl.TEXTURE_2D, 0, 0, 0, image.width, image.height, glFormat, glType, image.data );
- } else {
- state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, image.data );
- }
- }
- } else if ( texture.isCompressedTexture ) {
- if ( texture.isCompressedArrayTexture ) {
- if ( useTexStorage && allocateMemory ) {
- state.texStorage3D( _gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, mipmaps[ 0 ].width, mipmaps[ 0 ].height, image.depth );
- }
- for ( let i = 0, il = mipmaps.length; i < il; i ++ ) {
- mipmap = mipmaps[ i ];
- if ( texture.format !== RGBAFormat ) {
- if ( glFormat !== null ) {
- if ( useTexStorage ) {
- state.compressedTexSubImage3D( _gl.TEXTURE_2D_ARRAY, i, 0, 0, 0, mipmap.width, mipmap.height, image.depth, glFormat, mipmap.data, 0, 0 );
- } else {
- state.compressedTexImage3D( _gl.TEXTURE_2D_ARRAY, i, glInternalFormat, mipmap.width, mipmap.height, image.depth, 0, mipmap.data, 0, 0 );
- }
- } else {
- console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' );
- }
- } else {
- if ( useTexStorage ) {
- state.texSubImage3D( _gl.TEXTURE_2D_ARRAY, i, 0, 0, 0, mipmap.width, mipmap.height, image.depth, glFormat, glType, mipmap.data );
- } else {
- state.texImage3D( _gl.TEXTURE_2D_ARRAY, i, glInternalFormat, mipmap.width, mipmap.height, image.depth, 0, glFormat, glType, mipmap.data );
- }
- }
- }
- } else {
- if ( useTexStorage && allocateMemory ) {
- state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, mipmaps[ 0 ].width, mipmaps[ 0 ].height );
- }
- for ( let i = 0, il = mipmaps.length; i < il; i ++ ) {
- mipmap = mipmaps[ i ];
- if ( texture.format !== RGBAFormat ) {
- if ( glFormat !== null ) {
- if ( useTexStorage ) {
- state.compressedTexSubImage2D( _gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, mipmap.data );
- } else {
- state.compressedTexImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data );
- }
- } else {
- console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' );
- }
- } else {
- if ( useTexStorage ) {
- state.texSubImage2D( _gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, glType, mipmap.data );
- } else {
- state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
- }
- }
- }
- }
- } else if ( texture.isDataArrayTexture ) {
- if ( useTexStorage ) {
- if ( allocateMemory ) {
- state.texStorage3D( _gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, image.width, image.height, image.depth );
- }
- state.texSubImage3D( _gl.TEXTURE_2D_ARRAY, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data );
- } else {
- state.texImage3D( _gl.TEXTURE_2D_ARRAY, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data );
- }
- } else if ( texture.isData3DTexture ) {
- if ( useTexStorage ) {
- if ( allocateMemory ) {
- state.texStorage3D( _gl.TEXTURE_3D, levels, glInternalFormat, image.width, image.height, image.depth );
- }
- state.texSubImage3D( _gl.TEXTURE_3D, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data );
- } else {
- state.texImage3D( _gl.TEXTURE_3D, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data );
- }
- } else if ( texture.isFramebufferTexture ) {
- if ( allocateMemory ) {
- if ( useTexStorage ) {
- state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, image.width, image.height );
- } else {
- let width = image.width, height = image.height;
- for ( let i = 0; i < levels; i ++ ) {
- state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, width, height, 0, glFormat, glType, null );
- width >>= 1;
- height >>= 1;
- }
- }
- }
- } else {
- // regular Texture (image, video, canvas)
- // use manually created mipmaps if available
- // if there are no manual mipmaps
- // set 0 level mipmap and then use GL to generate other mipmap levels
- if ( mipmaps.length > 0 && supportsMips ) {
- if ( useTexStorage && allocateMemory ) {
- state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, mipmaps[ 0 ].width, mipmaps[ 0 ].height );
- }
- for ( let i = 0, il = mipmaps.length; i < il; i ++ ) {
- mipmap = mipmaps[ i ];
- if ( useTexStorage ) {
- state.texSubImage2D( _gl.TEXTURE_2D, i, 0, 0, glFormat, glType, mipmap );
- } else {
- state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, glFormat, glType, mipmap );
- }
- }
- texture.generateMipmaps = false;
- } else {
- if ( useTexStorage ) {
- if ( allocateMemory ) {
- state.texStorage2D( _gl.TEXTURE_2D, levels, glInternalFormat, image.width, image.height );
- }
- state.texSubImage2D( _gl.TEXTURE_2D, 0, 0, 0, glFormat, glType, image );
- } else {
- state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, glFormat, glType, image );
- }
- }
- }
- if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
- generateMipmap( textureType );
- }
- sourceProperties.__version = source.version;
- if ( texture.onUpdate ) texture.onUpdate( texture );
- }
- textureProperties.__version = texture.version;
- }
- function uploadCubeTexture( textureProperties, texture, slot ) {
- if ( texture.image.length !== 6 ) return;
- const forceUpload = initTexture( textureProperties, texture );
- const source = texture.source;
- state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture, _gl.TEXTURE0 + slot );
- const sourceProperties = properties.get( source );
- if ( source.version !== sourceProperties.__version || forceUpload === true ) {
- state.activeTexture( _gl.TEXTURE0 + slot );
- _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
- _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
- _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
- _gl.pixelStorei( _gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, _gl.NONE );
- const isCompressed = ( texture.isCompressedTexture || texture.image[ 0 ].isCompressedTexture );
- const isDataTexture = ( texture.image[ 0 ] && texture.image[ 0 ].isDataTexture );
- const cubeImage = [];
- for ( let i = 0; i < 6; i ++ ) {
- if ( ! isCompressed && ! isDataTexture ) {
- cubeImage[ i ] = resizeImage( texture.image[ i ], false, true, maxCubemapSize );
- } else {
- cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
- }
- cubeImage[ i ] = verifyColorSpace( texture, cubeImage[ i ] );
- }
- const image = cubeImage[ 0 ],
- supportsMips = isPowerOfTwo( image ) || isWebGL2,
- glFormat = utils.convert( texture.format, texture.encoding ),
- glType = utils.convert( texture.type ),
- glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding );
- const useTexStorage = ( isWebGL2 && texture.isVideoTexture !== true );
- const allocateMemory = ( sourceProperties.__version === undefined ) || ( forceUpload === true );
- let levels = getMipLevels( texture, image, supportsMips );
- setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, supportsMips );
- let mipmaps;
- if ( isCompressed ) {
- if ( useTexStorage && allocateMemory ) {
- state.texStorage2D( _gl.TEXTURE_CUBE_MAP, levels, glInternalFormat, image.width, image.height );
- }
- for ( let i = 0; i < 6; i ++ ) {
- mipmaps = cubeImage[ i ].mipmaps;
- for ( let j = 0; j < mipmaps.length; j ++ ) {
- const mipmap = mipmaps[ j ];
- if ( texture.format !== RGBAFormat ) {
- if ( glFormat !== null ) {
- if ( useTexStorage ) {
- state.compressedTexSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, 0, 0, mipmap.width, mipmap.height, glFormat, mipmap.data );
- } else {
- state.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data );
- }
- } else {
- console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setTextureCube()' );
- }
- } else {
- if ( useTexStorage ) {
- state.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, 0, 0, mipmap.width, mipmap.height, glFormat, glType, mipmap.data );
- } else {
- state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
- }
- }
- }
- }
- } else {
- mipmaps = texture.mipmaps;
- if ( useTexStorage && allocateMemory ) {
- // TODO: Uniformly handle mipmap definitions
- // Normal textures and compressed cube textures define base level + mips with their mipmap array
- // Uncompressed cube textures use their mipmap array only for mips (no base level)
- if ( mipmaps.length > 0 ) levels ++;
- state.texStorage2D( _gl.TEXTURE_CUBE_MAP, levels, glInternalFormat, cubeImage[ 0 ].width, cubeImage[ 0 ].height );
- }
- for ( let i = 0; i < 6; i ++ ) {
- if ( isDataTexture ) {
- if ( useTexStorage ) {
- state.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, cubeImage[ i ].width, cubeImage[ i ].height, glFormat, glType, cubeImage[ i ].data );
- } else {
- state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data );
- }
- for ( let j = 0; j < mipmaps.length; j ++ ) {
- const mipmap = mipmaps[ j ];
- const mipmapImage = mipmap.image[ i ].image;
- if ( useTexStorage ) {
- state.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, 0, 0, mipmapImage.width, mipmapImage.height, glFormat, glType, mipmapImage.data );
- } else {
- state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, glInternalFormat, mipmapImage.width, mipmapImage.height, 0, glFormat, glType, mipmapImage.data );
- }
- }
- } else {
- if ( useTexStorage ) {
- state.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, glFormat, glType, cubeImage[ i ] );
- } else {
- state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, glFormat, glType, cubeImage[ i ] );
- }
- for ( let j = 0; j < mipmaps.length; j ++ ) {
- const mipmap = mipmaps[ j ];
- if ( useTexStorage ) {
- state.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, 0, 0, glFormat, glType, mipmap.image[ i ] );
- } else {
- state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, glInternalFormat, glFormat, glType, mipmap.image[ i ] );
- }
- }
- }
- }
- }
- if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
- // We assume images for cube map have the same size.
- generateMipmap( _gl.TEXTURE_CUBE_MAP );
- }
- sourceProperties.__version = source.version;
- if ( texture.onUpdate ) texture.onUpdate( texture );
- }
- textureProperties.__version = texture.version;
- }
- // Render targets
- // Setup storage for target texture and bind it to correct framebuffer
- function setupFrameBufferTexture( framebuffer, renderTarget, texture, attachment, textureTarget ) {
- const glFormat = utils.convert( texture.format, texture.encoding );
- const glType = utils.convert( texture.type );
- const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding );
- const renderTargetProperties = properties.get( renderTarget );
- if ( ! renderTargetProperties.__hasExternalTextures ) {
- if ( textureTarget === _gl.TEXTURE_3D || textureTarget === _gl.TEXTURE_2D_ARRAY ) {
- state.texImage3D( textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, renderTarget.depth, 0, glFormat, glType, null );
- } else {
- state.texImage2D( textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
- }
- }
- state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
- if ( useMultisampledRTT( renderTarget ) ) {
- multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, 0, getRenderTargetSamples( renderTarget ) );
- } else if ( textureTarget === _gl.TEXTURE_2D || ( textureTarget >= _gl.TEXTURE_CUBE_MAP_POSITIVE_X && textureTarget <= _gl.TEXTURE_CUBE_MAP_NEGATIVE_Z ) ) { // see #24753
- _gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, 0 );
- }
- state.bindFramebuffer( _gl.FRAMEBUFFER, null );
- }
- // Setup storage for internal depth/stencil buffers and bind to correct framebuffer
- function setupRenderBufferStorage( renderbuffer, renderTarget, isMultisample ) {
- _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
- if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
- let glInternalFormat = _gl.DEPTH_COMPONENT16;
- if ( isMultisample || useMultisampledRTT( renderTarget ) ) {
- const depthTexture = renderTarget.depthTexture;
- if ( depthTexture && depthTexture.isDepthTexture ) {
- if ( depthTexture.type === FloatType ) {
- glInternalFormat = _gl.DEPTH_COMPONENT32F;
- } else if ( depthTexture.type === UnsignedIntType ) {
- glInternalFormat = _gl.DEPTH_COMPONENT24;
- }
- }
- const samples = getRenderTargetSamples( renderTarget );
- if ( useMultisampledRTT( renderTarget ) ) {
- multisampledRTTExt.renderbufferStorageMultisampleEXT( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
- } else {
- _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
- }
- } else {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, glInternalFormat, renderTarget.width, renderTarget.height );
- }
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
- } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
- const samples = getRenderTargetSamples( renderTarget );
- if ( isMultisample && useMultisampledRTT( renderTarget ) === false ) {
- _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, _gl.DEPTH24_STENCIL8, renderTarget.width, renderTarget.height );
- } else if ( useMultisampledRTT( renderTarget ) ) {
- multisampledRTTExt.renderbufferStorageMultisampleEXT( _gl.RENDERBUFFER, samples, _gl.DEPTH24_STENCIL8, renderTarget.width, renderTarget.height );
- } else {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
- }
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
- } else {
- const textures = renderTarget.isWebGLMultipleRenderTargets === true ? renderTarget.texture : [ renderTarget.texture ];
- for ( let i = 0; i < textures.length; i ++ ) {
- const texture = textures[ i ];
- const glFormat = utils.convert( texture.format, texture.encoding );
- const glType = utils.convert( texture.type );
- const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding );
- const samples = getRenderTargetSamples( renderTarget );
- if ( isMultisample && useMultisampledRTT( renderTarget ) === false ) {
- _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
- } else if ( useMultisampledRTT( renderTarget ) ) {
- multisampledRTTExt.renderbufferStorageMultisampleEXT( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
- } else {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, glInternalFormat, renderTarget.width, renderTarget.height );
- }
- }
- }
- _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
- }
- // Setup resources for a Depth Texture for a FBO (needs an extension)
- function setupDepthTexture( framebuffer, renderTarget ) {
- const isCube = ( renderTarget && renderTarget.isWebGLCubeRenderTarget );
- if ( isCube ) throw new Error( 'Depth Texture with cube render targets is not supported' );
- state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
- if ( ! ( renderTarget.depthTexture && renderTarget.depthTexture.isDepthTexture ) ) {
- throw new Error( 'renderTarget.depthTexture must be an instance of THREE.DepthTexture' );
- }
- // upload an empty depth texture with framebuffer size
- if ( ! properties.get( renderTarget.depthTexture ).__webglTexture ||
- renderTarget.depthTexture.image.width !== renderTarget.width ||
- renderTarget.depthTexture.image.height !== renderTarget.height ) {
- renderTarget.depthTexture.image.width = renderTarget.width;
- renderTarget.depthTexture.image.height = renderTarget.height;
- renderTarget.depthTexture.needsUpdate = true;
- }
- setTexture2D( renderTarget.depthTexture, 0 );
- const webglDepthTexture = properties.get( renderTarget.depthTexture ).__webglTexture;
- const samples = getRenderTargetSamples( renderTarget );
- if ( renderTarget.depthTexture.format === DepthFormat ) {
- if ( useMultisampledRTT( renderTarget ) ) {
- multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples );
- } else {
- _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 );
- }
- } else if ( renderTarget.depthTexture.format === DepthStencilFormat ) {
- if ( useMultisampledRTT( renderTarget ) ) {
- multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples );
- } else {
- _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 );
- }
- } else {
- throw new Error( 'Unknown depthTexture format' );
- }
- }
- // Setup GL resources for a non-texture depth buffer
- function setupDepthRenderbuffer( renderTarget ) {
- const renderTargetProperties = properties.get( renderTarget );
- const isCube = ( renderTarget.isWebGLCubeRenderTarget === true );
- if ( renderTarget.depthTexture && ! renderTargetProperties.__autoAllocateDepthBuffer ) {
- if ( isCube ) throw new Error( 'target.depthTexture not supported in Cube render targets' );
- setupDepthTexture( renderTargetProperties.__webglFramebuffer, renderTarget );
- } else {
- if ( isCube ) {
- renderTargetProperties.__webglDepthbuffer = [];
- for ( let i = 0; i < 6; i ++ ) {
- state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer[ i ] );
- renderTargetProperties.__webglDepthbuffer[ i ] = _gl.createRenderbuffer();
- setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer[ i ], renderTarget, false );
- }
- } else {
- state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
- renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer();
- setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer, renderTarget, false );
- }
- }
- state.bindFramebuffer( _gl.FRAMEBUFFER, null );
- }
- // rebind framebuffer with external textures
- function rebindTextures( renderTarget, colorTexture, depthTexture ) {
- const renderTargetProperties = properties.get( renderTarget );
- if ( colorTexture !== undefined ) {
- setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, renderTarget.texture, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D );
- }
- if ( depthTexture !== undefined ) {
- setupDepthRenderbuffer( renderTarget );
- }
- }
- // Set up GL resources for the render target
- function setupRenderTarget( renderTarget ) {
- const texture = renderTarget.texture;
- const renderTargetProperties = properties.get( renderTarget );
- const textureProperties = properties.get( texture );
- renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
- if ( renderTarget.isWebGLMultipleRenderTargets !== true ) {
- if ( textureProperties.__webglTexture === undefined ) {
- textureProperties.__webglTexture = _gl.createTexture();
- }
- textureProperties.__version = texture.version;
- info.memory.textures ++;
- }
- const isCube = ( renderTarget.isWebGLCubeRenderTarget === true );
- const isMultipleRenderTargets = ( renderTarget.isWebGLMultipleRenderTargets === true );
- const supportsMips = isPowerOfTwo( renderTarget ) || isWebGL2;
- // Setup framebuffer
- if ( isCube ) {
- renderTargetProperties.__webglFramebuffer = [];
- for ( let i = 0; i < 6; i ++ ) {
- renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer();
- }
- } else {
- renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();
- if ( isMultipleRenderTargets ) {
- if ( capabilities.drawBuffers ) {
- const textures = renderTarget.texture;
- for ( let i = 0, il = textures.length; i < il; i ++ ) {
- const attachmentProperties = properties.get( textures[ i ] );
- if ( attachmentProperties.__webglTexture === undefined ) {
- attachmentProperties.__webglTexture = _gl.createTexture();
- info.memory.textures ++;
- }
- }
- } else {
- console.warn( 'THREE.WebGLRenderer: WebGLMultipleRenderTargets can only be used with WebGL2 or WEBGL_draw_buffers extension.' );
- }
- }
- if ( ( isWebGL2 && renderTarget.samples > 0 ) && useMultisampledRTT( renderTarget ) === false ) {
- const textures = isMultipleRenderTargets ? texture : [ texture ];
- renderTargetProperties.__webglMultisampledFramebuffer = _gl.createFramebuffer();
- renderTargetProperties.__webglColorRenderbuffer = [];
- state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
- for ( let i = 0; i < textures.length; i ++ ) {
- const texture = textures[ i ];
- renderTargetProperties.__webglColorRenderbuffer[ i ] = _gl.createRenderbuffer();
- _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] );
- const glFormat = utils.convert( texture.format, texture.encoding );
- const glType = utils.convert( texture.type );
- const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding, renderTarget.isXRRenderTarget === true );
- const samples = getRenderTargetSamples( renderTarget );
- _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] );
- }
- _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
- if ( renderTarget.depthBuffer ) {
- renderTargetProperties.__webglDepthRenderbuffer = _gl.createRenderbuffer();
- setupRenderBufferStorage( renderTargetProperties.__webglDepthRenderbuffer, renderTarget, true );
- }
- state.bindFramebuffer( _gl.FRAMEBUFFER, null );
- }
- }
- // Setup color buffer
- if ( isCube ) {
- state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
- setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, supportsMips );
- for ( let i = 0; i < 6; i ++ ) {
- setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, texture, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
- }
- if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
- generateMipmap( _gl.TEXTURE_CUBE_MAP );
- }
- state.unbindTexture();
- } else if ( isMultipleRenderTargets ) {
- const textures = renderTarget.texture;
- for ( let i = 0, il = textures.length; i < il; i ++ ) {
- const attachment = textures[ i ];
- const attachmentProperties = properties.get( attachment );
- state.bindTexture( _gl.TEXTURE_2D, attachmentProperties.__webglTexture );
- setTextureParameters( _gl.TEXTURE_2D, attachment, supportsMips );
- setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, attachment, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D );
- if ( textureNeedsGenerateMipmaps( attachment, supportsMips ) ) {
- generateMipmap( _gl.TEXTURE_2D );
- }
- }
- state.unbindTexture();
- } else {
- let glTextureType = _gl.TEXTURE_2D;
- if ( renderTarget.isWebGL3DRenderTarget || renderTarget.isWebGLArrayRenderTarget ) {
- if ( isWebGL2 ) {
- glTextureType = renderTarget.isWebGL3DRenderTarget ? _gl.TEXTURE_3D : _gl.TEXTURE_2D_ARRAY;
- } else {
- console.error( 'THREE.WebGLTextures: THREE.Data3DTexture and THREE.DataArrayTexture only supported with WebGL2.' );
- }
- }
- state.bindTexture( glTextureType, textureProperties.__webglTexture );
- setTextureParameters( glTextureType, texture, supportsMips );
- setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, texture, _gl.COLOR_ATTACHMENT0, glTextureType );
- if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
- generateMipmap( glTextureType );
- }
- state.unbindTexture();
- }
- // Setup depth and stencil buffers
- if ( renderTarget.depthBuffer ) {
- setupDepthRenderbuffer( renderTarget );
- }
- }
- function updateRenderTargetMipmap( renderTarget ) {
- const supportsMips = isPowerOfTwo( renderTarget ) || isWebGL2;
- const textures = renderTarget.isWebGLMultipleRenderTargets === true ? renderTarget.texture : [ renderTarget.texture ];
- for ( let i = 0, il = textures.length; i < il; i ++ ) {
- const texture = textures[ i ];
- if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
- const target = renderTarget.isWebGLCubeRenderTarget ? _gl.TEXTURE_CUBE_MAP : _gl.TEXTURE_2D;
- const webglTexture = properties.get( texture ).__webglTexture;
- state.bindTexture( target, webglTexture );
- generateMipmap( target );
- state.unbindTexture();
- }
- }
- }
- function updateMultisampleRenderTarget( renderTarget ) {
- if ( ( isWebGL2 && renderTarget.samples > 0 ) && useMultisampledRTT( renderTarget ) === false ) {
- const textures = renderTarget.isWebGLMultipleRenderTargets ? renderTarget.texture : [ renderTarget.texture ];
- const width = renderTarget.width;
- const height = renderTarget.height;
- let mask = _gl.COLOR_BUFFER_BIT;
- const invalidationArray = [];
- const depthStyle = renderTarget.stencilBuffer ? _gl.DEPTH_STENCIL_ATTACHMENT : _gl.DEPTH_ATTACHMENT;
- const renderTargetProperties = properties.get( renderTarget );
- const isMultipleRenderTargets = ( renderTarget.isWebGLMultipleRenderTargets === true );
- // If MRT we need to remove FBO attachments
- if ( isMultipleRenderTargets ) {
- for ( let i = 0; i < textures.length; i ++ ) {
- state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.RENDERBUFFER, null );
- state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
- _gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D, null, 0 );
- }
- }
- state.bindFramebuffer( _gl.READ_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
- state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
- for ( let i = 0; i < textures.length; i ++ ) {
- invalidationArray.push( _gl.COLOR_ATTACHMENT0 + i );
- if ( renderTarget.depthBuffer ) {
- invalidationArray.push( depthStyle );
- }
- const ignoreDepthValues = ( renderTargetProperties.__ignoreDepthValues !== undefined ) ? renderTargetProperties.__ignoreDepthValues : false;
- if ( ignoreDepthValues === false ) {
- if ( renderTarget.depthBuffer ) mask |= _gl.DEPTH_BUFFER_BIT;
- if ( renderTarget.stencilBuffer ) mask |= _gl.STENCIL_BUFFER_BIT;
- }
- if ( isMultipleRenderTargets ) {
- _gl.framebufferRenderbuffer( _gl.READ_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] );
- }
- if ( ignoreDepthValues === true ) {
- _gl.invalidateFramebuffer( _gl.READ_FRAMEBUFFER, [ depthStyle ] );
- _gl.invalidateFramebuffer( _gl.DRAW_FRAMEBUFFER, [ depthStyle ] );
- }
- if ( isMultipleRenderTargets ) {
- const webglTexture = properties.get( textures[ i ] ).__webglTexture;
- _gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D, webglTexture, 0 );
- }
- _gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, _gl.NEAREST );
- if ( supportsInvalidateFramebuffer ) {
- _gl.invalidateFramebuffer( _gl.READ_FRAMEBUFFER, invalidationArray );
- }
- }
- state.bindFramebuffer( _gl.READ_FRAMEBUFFER, null );
- state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, null );
- // If MRT since pre-blit we removed the FBO we need to reconstruct the attachments
- if ( isMultipleRenderTargets ) {
- for ( let i = 0; i < textures.length; i ++ ) {
- state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] );
- const webglTexture = properties.get( textures[ i ] ).__webglTexture;
- state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
- _gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D, webglTexture, 0 );
- }
- }
- state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
- }
- }
- function getRenderTargetSamples( renderTarget ) {
- return Math.min( maxSamples, renderTarget.samples );
- }
- function useMultisampledRTT( renderTarget ) {
- const renderTargetProperties = properties.get( renderTarget );
- return isWebGL2 && renderTarget.samples > 0 && extensions.has( 'WEBGL_multisampled_render_to_texture' ) === true && renderTargetProperties.__useRenderToTexture !== false;
- }
- function updateVideoTexture( texture ) {
- const frame = info.render.frame;
- // Check the last frame we updated the VideoTexture
- if ( _videoTextures.get( texture ) !== frame ) {
- _videoTextures.set( texture, frame );
- texture.update();
- }
- }
- function verifyColorSpace( texture, image ) {
- const encoding = texture.encoding;
- const format = texture.format;
- const type = texture.type;
- if ( texture.isCompressedTexture === true || texture.isVideoTexture === true || texture.format === _SRGBAFormat ) return image;
- if ( encoding !== LinearEncoding ) {
- // sRGB
- if ( encoding === sRGBEncoding ) {
- if ( isWebGL2 === false ) {
- // in WebGL 1, try to use EXT_sRGB extension and unsized formats
- if ( extensions.has( 'EXT_sRGB' ) === true && format === RGBAFormat ) {
- texture.format = _SRGBAFormat;
- // it's not possible to generate mips in WebGL 1 with this extension
- texture.minFilter = LinearFilter;
- texture.generateMipmaps = false;
- } else {
- // slow fallback (CPU decode)
- image = ImageUtils.sRGBToLinear( image );
- }
- } else {
- // in WebGL 2 uncompressed textures can only be sRGB encoded if they have the RGBA8 format
- if ( format !== RGBAFormat || type !== UnsignedByteType ) {
- console.warn( 'THREE.WebGLTextures: sRGB encoded textures have to use RGBAFormat and UnsignedByteType.' );
- }
- }
- } else {
- console.error( 'THREE.WebGLTextures: Unsupported texture encoding:', encoding );
- }
- }
- return image;
- }
- //
- this.allocateTextureUnit = allocateTextureUnit;
- this.resetTextureUnits = resetTextureUnits;
- this.setTexture2D = setTexture2D;
- this.setTexture2DArray = setTexture2DArray;
- this.setTexture3D = setTexture3D;
- this.setTextureCube = setTextureCube;
- this.rebindTextures = rebindTextures;
- this.setupRenderTarget = setupRenderTarget;
- this.updateRenderTargetMipmap = updateRenderTargetMipmap;
- this.updateMultisampleRenderTarget = updateMultisampleRenderTarget;
- this.setupDepthRenderbuffer = setupDepthRenderbuffer;
- this.setupFrameBufferTexture = setupFrameBufferTexture;
- this.useMultisampledRTT = useMultisampledRTT;
- }
- export { WebGLTextures };
|