WebGLTextureUtils.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. import { LinearFilter, LinearMipmapLinearFilter, LinearMipmapNearestFilter, NearestFilter, NearestMipmapLinearFilter, NearestMipmapNearestFilter, FloatType, MirroredRepeatWrapping, ClampToEdgeWrapping, RepeatWrapping, SRGBColorSpace, NeverCompare, AlwaysCompare, LessCompare, LessEqualCompare, EqualCompare, GreaterEqualCompare, GreaterCompare, NotEqualCompare } from 'three';
  2. let initialized = false, wrappingToGL, filterToGL, compareToGL;
  3. class WebGLTextureUtils {
  4. constructor( backend ) {
  5. this.backend = backend;
  6. this.gl = backend.gl;
  7. this.extensions = backend.extensions;
  8. this.defaultTextures = {};
  9. if ( initialized === false ) {
  10. this._init( this.gl );
  11. initialized = true;
  12. }
  13. }
  14. _init( gl ) {
  15. // Store only WebGL constants here.
  16. wrappingToGL = {
  17. [ RepeatWrapping ]: gl.REPEAT,
  18. [ ClampToEdgeWrapping ]: gl.CLAMP_TO_EDGE,
  19. [ MirroredRepeatWrapping ]: gl.MIRRORED_REPEAT
  20. };
  21. filterToGL = {
  22. [ NearestFilter ]: gl.NEAREST,
  23. [ NearestMipmapNearestFilter ]: gl.NEAREST_MIPMAP_NEAREST,
  24. [ NearestMipmapLinearFilter ]: gl.NEAREST_MIPMAP_LINEAR,
  25. [ LinearFilter ]: gl.LINEAR,
  26. [ LinearMipmapNearestFilter ]: gl.LINEAR_MIPMAP_NEAREST,
  27. [ LinearMipmapLinearFilter ]: gl.LINEAR_MIPMAP_LINEAR
  28. };
  29. compareToGL = {
  30. [ NeverCompare ]: gl.NEVER,
  31. [ AlwaysCompare ]: gl.ALWAYS,
  32. [ LessCompare ]: gl.LESS,
  33. [ LessEqualCompare ]: gl.LEQUAL,
  34. [ EqualCompare ]: gl.EQUAL,
  35. [ GreaterEqualCompare ]: gl.GEQUAL,
  36. [ GreaterCompare ]: gl.GREATER,
  37. [ NotEqualCompare ]: gl.NOTEQUAL
  38. };
  39. }
  40. filterFallback( f ) {
  41. const { gl } = this;
  42. if ( f === NearestFilter || f === NearestMipmapNearestFilter || f === NearestMipmapLinearFilter ) {
  43. return gl.NEAREST;
  44. }
  45. return gl.LINEAR;
  46. }
  47. getGLTextureType( texture ) {
  48. const { gl } = this;
  49. let glTextureType;
  50. if ( texture.isCubeTexture === true ) {
  51. glTextureType = gl.TEXTURE_CUBE_MAP;
  52. } else if ( texture.isDataArrayTexture === true ) {
  53. glTextureType = gl.TEXTURE_2D_ARRAY;
  54. } else {
  55. glTextureType = gl.TEXTURE_2D;
  56. }
  57. return glTextureType;
  58. }
  59. getInternalFormat( internalFormatName, glFormat, glType, colorSpace, forceLinearTransfer = false ) {
  60. const { gl, extensions } = this;
  61. if ( internalFormatName !== null ) {
  62. if ( gl[ internalFormatName ] !== undefined ) return gl[ internalFormatName ];
  63. console.warn( 'THREE.WebGLRenderer: Attempt to use non-existing WebGL internal format \'' + internalFormatName + '\'' );
  64. }
  65. let internalFormat = glFormat;
  66. if ( glFormat === gl.RED ) {
  67. if ( glType === gl.FLOAT ) internalFormat = gl.R32F;
  68. if ( glType === gl.HALF_FLOAT ) internalFormat = gl.R16F;
  69. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.R8;
  70. }
  71. if ( glFormat === gl.RED_INTEGER ) {
  72. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.R8UI;
  73. if ( glType === gl.UNSIGNED_SHORT ) internalFormat = gl.R16UI;
  74. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.R32UI;
  75. if ( glType === gl.BYTE ) internalFormat = gl.R8I;
  76. if ( glType === gl.SHORT ) internalFormat = gl.R16I;
  77. if ( glType === gl.INT ) internalFormat = gl.R32I;
  78. }
  79. if ( glFormat === gl.RG ) {
  80. if ( glType === gl.FLOAT ) internalFormat = gl.RG32F;
  81. if ( glType === gl.HALF_FLOAT ) internalFormat = gl.RG16F;
  82. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.RG8;
  83. }
  84. if ( glFormat === gl.RGBA ) {
  85. if ( glType === gl.FLOAT ) internalFormat = gl.RGBA32F;
  86. if ( glType === gl.HALF_FLOAT ) internalFormat = gl.RGBA16F;
  87. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = ( colorSpace === SRGBColorSpace && forceLinearTransfer === false ) ? gl.SRGB8_ALPHA8 : gl.RGBA8;
  88. if ( glType === gl.UNSIGNED_SHORT_4_4_4_4 ) internalFormat = gl.RGBA4;
  89. if ( glType === gl.UNSIGNED_SHORT_5_5_5_1 ) internalFormat = gl.RGB5_A1;
  90. }
  91. if ( glFormat === gl.DEPTH_COMPONENT ) {
  92. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.DEPTH24_STENCIL8;
  93. if ( glType === gl.FLOAT ) internalFormat = gl.DEPTH_COMPONENT32F;
  94. }
  95. if ( glFormat === gl.DEPTH_STENCIL ) {
  96. if ( glType === gl.UNSIGNED_INT_24_8 ) internalFormat = gl.DEPTH24_STENCIL8;
  97. }
  98. if ( internalFormat === gl.R16F || internalFormat === gl.R32F ||
  99. internalFormat === gl.RG16F || internalFormat === gl.RG32F ||
  100. internalFormat === gl.RGBA16F || internalFormat === gl.RGBA32F ) {
  101. extensions.get( 'EXT_color_buffer_float' );
  102. }
  103. return internalFormat;
  104. }
  105. setTextureParameters( textureType, texture ) {
  106. const { gl, extensions, backend } = this;
  107. const { currentAnisotropy } = backend.get( texture );
  108. gl.texParameteri( textureType, gl.TEXTURE_WRAP_S, wrappingToGL[ texture.wrapS ] );
  109. gl.texParameteri( textureType, gl.TEXTURE_WRAP_T, wrappingToGL[ texture.wrapT ] );
  110. if ( textureType === gl.TEXTURE_3D || textureType === gl.TEXTURE_2D_ARRAY ) {
  111. gl.texParameteri( textureType, gl.TEXTURE_WRAP_R, wrappingToGL[ texture.wrapR ] );
  112. }
  113. gl.texParameteri( textureType, gl.TEXTURE_MAG_FILTER, filterToGL[ texture.magFilter ] );
  114. // follow WebGPU backend mapping for texture filtering
  115. const minFilter = ! texture.isVideoTexture && texture.minFilter === LinearFilter ? LinearMipmapLinearFilter : texture.minFilter;
  116. gl.texParameteri( textureType, gl.TEXTURE_MIN_FILTER, filterToGL[ minFilter ] );
  117. if ( texture.compareFunction ) {
  118. gl.texParameteri( textureType, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE );
  119. gl.texParameteri( textureType, gl.TEXTURE_COMPARE_FUNC, compareToGL[ texture.compareFunction ] );
  120. }
  121. if ( extensions.has( 'EXT_texture_filter_anisotropic' ) === true ) {
  122. if ( texture.magFilter === NearestFilter ) return;
  123. if ( texture.minFilter !== NearestMipmapLinearFilter && texture.minFilter !== LinearMipmapLinearFilter ) return;
  124. if ( texture.type === FloatType && extensions.has( 'OES_texture_float_linear' ) === false ) return; // verify extension for WebGL 1 and WebGL 2
  125. if ( texture.anisotropy > 1 || currentAnisotropy !== texture.anisotropy ) {
  126. const extension = extensions.get( 'EXT_texture_filter_anisotropic' );
  127. gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, backend.getMaxAnisotropy() ) );
  128. backend.get( texture ).currentAnisotropy = texture.anisotropy;
  129. }
  130. }
  131. }
  132. createDefaultTexture( texture ) {
  133. const { gl, backend, defaultTextures } = this;
  134. const glTextureType = this.getGLTextureType( texture );
  135. let textureGPU = defaultTextures[ glTextureType ];
  136. if ( textureGPU === undefined ) {
  137. textureGPU = gl.createTexture();
  138. backend.state.bindTexture( glTextureType, textureGPU );
  139. gl.texParameteri( glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  140. gl.texParameteri( glTextureType, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  141. // gl.texImage2D( glTextureType, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
  142. defaultTextures[ glTextureType ] = textureGPU;
  143. }
  144. backend.set( texture, {
  145. textureGPU,
  146. glTextureType,
  147. isDefault: true
  148. } );
  149. }
  150. createTexture( texture, options ) {
  151. const { gl, backend } = this;
  152. const { levels, width, height, depth } = options;
  153. const glFormat = backend.utils.convert( texture.format, texture.colorSpace );
  154. const glType = backend.utils.convert( texture.type );
  155. const glInternalFormat = this.getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace, texture.isVideoTexture );
  156. const textureGPU = gl.createTexture();
  157. const glTextureType = this.getGLTextureType( texture );
  158. backend.state.bindTexture( glTextureType, textureGPU );
  159. gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  160. gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  161. gl.pixelStorei( gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  162. gl.pixelStorei( gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE );
  163. this.setTextureParameters( glTextureType, texture );
  164. if ( texture.isDataArrayTexture ) {
  165. gl.texStorage3D( gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, width, height, depth );
  166. } else if ( ! texture.isVideoTexture ) {
  167. gl.texStorage2D( glTextureType, levels, glInternalFormat, width, height );
  168. }
  169. backend.set( texture, {
  170. textureGPU,
  171. glTextureType,
  172. glFormat,
  173. glType,
  174. glInternalFormat
  175. } );
  176. }
  177. updateTexture( texture, options ) {
  178. const { gl } = this;
  179. const { width, height } = options;
  180. const { textureGPU, glTextureType, glFormat, glType, glInternalFormat } = this.backend.get( texture );
  181. if ( texture.isRenderTargetTexture || ( textureGPU === undefined /* unsupported texture format */ ) )
  182. return;
  183. const getImage = ( source ) => {
  184. if ( source.isDataTexture ) {
  185. return source.image.data;
  186. } else if ( source instanceof ImageBitmap || source instanceof OffscreenCanvas || source instanceof HTMLImageElement || source instanceof HTMLCanvasElement ) {
  187. return source;
  188. }
  189. return source.data;
  190. };
  191. this.backend.state.bindTexture( glTextureType, textureGPU );
  192. if ( texture.isCompressedTexture ) {
  193. const mipmaps = texture.mipmaps;
  194. for ( let i = 0; i < mipmaps.length; i ++ ) {
  195. const mipmap = mipmaps[ i ];
  196. if ( texture.isCompressedArrayTexture ) {
  197. const image = options.image;
  198. if ( texture.format !== gl.RGBA ) {
  199. if ( glFormat !== null ) {
  200. gl.compressedTexSubImage3D( gl.TEXTURE_2D_ARRAY, i, 0, 0, 0, mipmap.width, mipmap.height, image.depth, glFormat, mipmap.data, 0, 0 );
  201. } else {
  202. console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' );
  203. }
  204. } else {
  205. gl.texSubImage3D( gl.TEXTURE_2D_ARRAY, i, 0, 0, 0, mipmap.width, mipmap.height, image.depth, glFormat, glType, mipmap.data );
  206. }
  207. } else {
  208. if ( glFormat !== null ) {
  209. gl.compressedTexSubImage2D( gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, mipmap.data );
  210. } else {
  211. console.warn( 'Unsupported compressed texture format' );
  212. }
  213. }
  214. }
  215. } else if ( texture.isCubeTexture ) {
  216. const images = options.images;
  217. for ( let i = 0; i < 6; i ++ ) {
  218. const image = getImage( images[ i ] );
  219. gl.texSubImage2D( gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, width, height, glFormat, glType, image );
  220. }
  221. } else if ( texture.isDataArrayTexture ) {
  222. const image = options.image;
  223. gl.texSubImage3D( gl.TEXTURE_2D_ARRAY, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data );
  224. } else if ( texture.isVideoTexture ) {
  225. texture.update();
  226. gl.texImage2D( glTextureType, 0, glInternalFormat, glFormat, glType, options.image );
  227. } else {
  228. const image = getImage( options.image );
  229. gl.texSubImage2D( glTextureType, 0, 0, 0, width, height, glFormat, glType, image );
  230. }
  231. }
  232. generateMipmaps( texture ) {
  233. const { gl, backend } = this;
  234. const { textureGPU, glTextureType } = backend.get( texture );
  235. backend.state.bindTexture( glTextureType, textureGPU );
  236. gl.generateMipmap( glTextureType );
  237. }
  238. deallocateRenderBuffers( renderTarget ) {
  239. const { gl, backend } = this;
  240. // remove framebuffer reference
  241. if ( renderTarget ) {
  242. const renderContextData = backend.get( renderTarget );
  243. renderContextData.renderBufferStorageSetup = undefined;
  244. if ( renderContextData.framebuffer ) {
  245. gl.deleteFramebuffer( renderContextData.framebuffer );
  246. renderContextData.framebuffer = undefined;
  247. }
  248. if ( renderContextData.depthRenderbuffer ) {
  249. gl.deleteRenderbuffer( renderContextData.depthRenderbuffer );
  250. renderContextData.depthRenderbuffer = undefined;
  251. }
  252. if ( renderContextData.stencilRenderbuffer ) {
  253. gl.deleteRenderbuffer( renderContextData.stencilRenderbuffer );
  254. renderContextData.stencilRenderbuffer = undefined;
  255. }
  256. if ( renderContextData.msaaFrameBuffer ) {
  257. gl.deleteFramebuffer( renderContextData.msaaFrameBuffer );
  258. renderContextData.msaaFrameBuffer = undefined;
  259. }
  260. if ( renderContextData.msaaRenderbuffers ) {
  261. for ( let i = 0; i < renderContextData.msaaRenderbuffers.length; i ++ ) {
  262. gl.deleteRenderbuffer( renderContextData.msaaRenderbuffers[ i ] );
  263. }
  264. renderContextData.msaaRenderbuffers = undefined;
  265. }
  266. }
  267. }
  268. destroyTexture( texture ) {
  269. const { gl, backend } = this;
  270. const { textureGPU, renderTarget } = backend.get( texture );
  271. this.deallocateRenderBuffers( renderTarget );
  272. gl.deleteTexture( textureGPU );
  273. backend.delete( texture );
  274. }
  275. copyFramebufferToTexture( texture, renderContext ) {
  276. const { gl } = this;
  277. const { state } = this.backend;
  278. const { textureGPU } = this.backend.get( texture );
  279. const width = texture.image.width;
  280. const height = texture.image.height;
  281. if ( texture.isDepthTexture ) {
  282. let mask = gl.DEPTH_BUFFER_BIT;
  283. if ( renderContext.stencil ) {
  284. mask |= gl.STENCIL_BUFFER_BIT;
  285. }
  286. const fb = gl.createFramebuffer();
  287. state.bindFramebuffer( gl.DRAW_FRAMEBUFFER, fb );
  288. gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, textureGPU, 0 );
  289. gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, gl.NEAREST );
  290. gl.deleteFramebuffer( fb );
  291. } else {
  292. state.bindTexture( gl.TEXTURE_2D, textureGPU );
  293. gl.copyTexSubImage2D( gl.TEXTURE_2D, 0, 0, 0, 0, 0, width, height );
  294. state.unbindTexture();
  295. }
  296. if ( texture.generateMipmaps ) this.generateMipmaps( texture );
  297. this.backend._setFramebuffer( renderContext );
  298. }
  299. // Setup storage for internal depth/stencil buffers and bind to correct framebuffer
  300. setupRenderBufferStorage( renderbuffer, renderContext ) {
  301. const { gl } = this;
  302. const renderTarget = renderContext.renderTarget;
  303. const { samples, depthTexture, depthBuffer, stencilBuffer, width, height } = renderTarget;
  304. gl.bindRenderbuffer( gl.RENDERBUFFER, renderbuffer );
  305. if ( depthBuffer && ! stencilBuffer ) {
  306. let glInternalFormat = gl.DEPTH_COMPONENT24;
  307. if ( samples > 0 ) {
  308. if ( depthTexture && depthTexture.isDepthTexture ) {
  309. if ( depthTexture.type === gl.FLOAT ) {
  310. glInternalFormat = gl.DEPTH_COMPONENT32F;
  311. }
  312. }
  313. gl.renderbufferStorageMultisample( gl.RENDERBUFFER, samples, glInternalFormat, width, height );
  314. } else {
  315. gl.renderbufferStorage( gl.RENDERBUFFER, glInternalFormat, width, height );
  316. }
  317. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer );
  318. } else if ( depthBuffer && stencilBuffer ) {
  319. if ( samples > 0 ) {
  320. gl.renderbufferStorageMultisample( gl.RENDERBUFFER, samples, gl.DEPTH24_STENCIL8, width, height );
  321. } else {
  322. gl.renderbufferStorage( gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height );
  323. }
  324. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, renderbuffer );
  325. }
  326. }
  327. async copyTextureToBuffer( texture, x, y, width, height ) {
  328. const { backend, gl } = this;
  329. const { textureGPU, glFormat, glType } = this.backend.get( texture );
  330. const fb = gl.createFramebuffer();
  331. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, fb );
  332. gl.framebufferTexture2D( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textureGPU, 0 );
  333. const typedArrayType = this._getTypedArrayType( glType );
  334. const bytesPerTexel = this._getBytesPerTexel( glFormat );
  335. const elementCount = width * height;
  336. const byteLength = elementCount * bytesPerTexel;
  337. const buffer = gl.createBuffer();
  338. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
  339. gl.bufferData( gl.PIXEL_PACK_BUFFER, byteLength, gl.STREAM_READ );
  340. gl.readPixels( x, y, width, height, glFormat, glType, 0 );
  341. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, null );
  342. await backend.utils._clientWaitAsync();
  343. const dstBuffer = new typedArrayType( elementCount );
  344. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
  345. gl.getBufferSubData( gl.PIXEL_PACK_BUFFER, 0, dstBuffer );
  346. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, null );
  347. gl.deleteFramebuffer( fb );
  348. return dstBuffer;
  349. }
  350. _getTypedArrayType( glType ) {
  351. const { gl } = this;
  352. if ( glType === gl.UNSIGNED_BYTE ) return Uint8Array;
  353. if ( glType === gl.UNSIGNED_SHORT_4_4_4_4 ) return Uint16Array;
  354. if ( glType === gl.UNSIGNED_SHORT_5_5_5_1 ) return Uint16Array;
  355. if ( glType === gl.UNSIGNED_SHORT_5_6_5 ) return Uint16Array;
  356. if ( glType === gl.UNSIGNED_SHORT ) return Uint16Array;
  357. if ( glType === gl.UNSIGNED_INT ) return Uint32Array;
  358. if ( glType === gl.FLOAT ) return Float32Array;
  359. throw new Error( `Unsupported WebGL type: ${glType}` );
  360. }
  361. _getBytesPerTexel( glFormat ) {
  362. const { gl } = this;
  363. if ( glFormat === gl.RGBA ) return 4;
  364. if ( glFormat === gl.RGB ) return 3;
  365. if ( glFormat === gl.ALPHA ) return 1;
  366. }
  367. }
  368. export default WebGLTextureUtils;