WebGLTextureUtils.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 } = this;
  107. gl.texParameteri( textureType, gl.TEXTURE_WRAP_S, wrappingToGL[ texture.wrapS ] );
  108. gl.texParameteri( textureType, gl.TEXTURE_WRAP_T, wrappingToGL[ texture.wrapT ] );
  109. if ( textureType === gl.TEXTURE_3D || textureType === gl.TEXTURE_2D_ARRAY ) {
  110. gl.texParameteri( textureType, gl.TEXTURE_WRAP_R, wrappingToGL[ texture.wrapR ] );
  111. }
  112. gl.texParameteri( textureType, gl.TEXTURE_MAG_FILTER, filterToGL[ texture.magFilter ] );
  113. // follow WebGPU backend mapping for texture filtering
  114. const minFilter = texture.minFilter === LinearFilter ? LinearMipmapLinearFilter : texture.minFilter;
  115. gl.texParameteri( textureType, gl.TEXTURE_MIN_FILTER, filterToGL[ minFilter ] );
  116. if ( texture.compareFunction ) {
  117. gl.texParameteri( textureType, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE );
  118. gl.texParameteri( textureType, gl.TEXTURE_COMPARE_FUNC, compareToGL[ texture.compareFunction ] );
  119. }
  120. if ( extensions.has( 'EXT_texture_filter_anisotropic' ) === true ) {
  121. //extension = extensions.get( 'EXT_texture_filter_anisotropic' );
  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 /*|| properties.get( texture ).__currentAnisotropy*/ ) {
  126. //gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, capabilities.getMaxAnisotropy() ) );
  127. //properties.get( texture ).__currentAnisotropy = texture.anisotropy;
  128. }
  129. }
  130. }
  131. createDefaultTexture( texture ) {
  132. const { gl, backend, defaultTextures } = this;
  133. const glTextureType = this.getGLTextureType( texture );
  134. let textureGPU = defaultTextures[ glTextureType ];
  135. if ( textureGPU === undefined ) {
  136. textureGPU = gl.createTexture();
  137. backend.state.bindTexture( glTextureType, textureGPU );
  138. gl.texParameteri( glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  139. gl.texParameteri( glTextureType, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  140. // gl.texImage2D( glTextureType, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
  141. defaultTextures[ glTextureType ] = textureGPU;
  142. }
  143. backend.set( texture, {
  144. textureGPU,
  145. glTextureType,
  146. isDefault: true
  147. } );
  148. }
  149. createTexture( texture, options ) {
  150. const { gl, backend } = this;
  151. const { levels, width, height, depth } = options;
  152. const glFormat = backend.utils.convert( texture.format, texture.colorSpace );
  153. const glType = backend.utils.convert( texture.type );
  154. const glInternalFormat = this.getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace, texture.isVideoTexture );
  155. const textureGPU = gl.createTexture();
  156. const glTextureType = this.getGLTextureType( texture );
  157. backend.state.bindTexture( glTextureType, textureGPU );
  158. gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  159. gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  160. gl.pixelStorei( gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  161. gl.pixelStorei( gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE );
  162. this.setTextureParameters( glTextureType, texture );
  163. if ( texture.isDataArrayTexture ) {
  164. gl.texStorage3D( gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, width, height, depth );
  165. } else if ( ! texture.isVideoTexture ) {
  166. gl.texStorage2D( glTextureType, levels, glInternalFormat, width, height );
  167. }
  168. backend.set( texture, {
  169. textureGPU,
  170. glTextureType,
  171. glFormat,
  172. glType,
  173. glInternalFormat
  174. } );
  175. }
  176. updateTexture( texture, options ) {
  177. const { gl } = this;
  178. const { width, height } = options;
  179. const { textureGPU, glTextureType, glFormat, glType, glInternalFormat } = this.backend.get( texture );
  180. if ( texture.isRenderTargetTexture || ( textureGPU === undefined /* unsupported texture format */ ) )
  181. return;
  182. const getImage = ( source ) => {
  183. if ( source.isDataTexture ) {
  184. return source.image.data;
  185. } else if ( source instanceof ImageBitmap || source instanceof OffscreenCanvas || source instanceof HTMLImageElement || source instanceof HTMLCanvasElement ) {
  186. return source;
  187. }
  188. return source.data;
  189. };
  190. this.backend.state.bindTexture( glTextureType, textureGPU );
  191. if ( texture.isCompressedTexture ) {
  192. const mipmaps = texture.mipmaps;
  193. for ( let i = 0; i < mipmaps.length; i ++ ) {
  194. const mipmap = mipmaps[ i ];
  195. if ( texture.isCompressedArrayTexture ) {
  196. const image = options.image;
  197. if ( texture.format !== gl.RGBA ) {
  198. if ( glFormat !== null ) {
  199. gl.compressedTexSubImage3D( gl.TEXTURE_2D_ARRAY, i, 0, 0, 0, mipmap.width, mipmap.height, image.depth, glFormat, mipmap.data, 0, 0 );
  200. } else {
  201. console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' );
  202. }
  203. } else {
  204. gl.texSubImage3D( gl.TEXTURE_2D_ARRAY, i, 0, 0, 0, mipmap.width, mipmap.height, image.depth, glFormat, glType, mipmap.data );
  205. }
  206. } else {
  207. if ( glFormat !== null ) {
  208. gl.compressedTexSubImage2D( gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, mipmap.data );
  209. } else {
  210. console.warn( 'Unsupported compressed texture format' );
  211. }
  212. }
  213. }
  214. } else if ( texture.isCubeTexture ) {
  215. const images = options.images;
  216. for ( let i = 0; i < 6; i ++ ) {
  217. const image = getImage( images[ i ] );
  218. gl.texSubImage2D( gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, width, height, glFormat, glType, image );
  219. }
  220. } else if ( texture.isDataArrayTexture ) {
  221. const image = options.image;
  222. gl.texSubImage3D( gl.TEXTURE_2D_ARRAY, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data );
  223. } else if ( texture.isVideoTexture ) {
  224. texture.update();
  225. gl.texImage2D( glTextureType, 0, glInternalFormat, glFormat, glType, options.image );
  226. } else {
  227. const image = getImage( options.image );
  228. gl.texSubImage2D( glTextureType, 0, 0, 0, width, height, glFormat, glType, image );
  229. }
  230. }
  231. generateMipmaps( texture ) {
  232. const { gl, backend } = this;
  233. const { textureGPU, glTextureType } = backend.get( texture );
  234. backend.state.bindTexture( glTextureType, textureGPU );
  235. gl.generateMipmap( glTextureType );
  236. }
  237. deallocateRenderBuffers( renderTarget ) {
  238. const { gl, backend } = this;
  239. // remove framebuffer reference
  240. if ( renderTarget ) {
  241. const renderContextData = backend.get( renderTarget );
  242. renderContextData.renderBufferStorageSetup = undefined;
  243. if ( renderContextData.framebuffer ) {
  244. gl.deleteFramebuffer( renderContextData.framebuffer );
  245. renderContextData.framebuffer = undefined;
  246. }
  247. if ( renderContextData.depthRenderbuffer ) {
  248. gl.deleteRenderbuffer( renderContextData.depthRenderbuffer );
  249. renderContextData.depthRenderbuffer = undefined;
  250. }
  251. if ( renderContextData.stencilRenderbuffer ) {
  252. gl.deleteRenderbuffer( renderContextData.stencilRenderbuffer );
  253. renderContextData.stencilRenderbuffer = undefined;
  254. }
  255. if ( renderContextData.msaaFrameBuffer ) {
  256. gl.deleteFramebuffer( renderContextData.msaaFrameBuffer );
  257. renderContextData.msaaFrameBuffer = undefined;
  258. }
  259. if ( renderContextData.msaaRenderbuffers ) {
  260. for ( let i = 0; i < renderContextData.msaaRenderbuffers.length; i ++ ) {
  261. gl.deleteRenderbuffer( renderContextData.msaaRenderbuffers[ i ] );
  262. }
  263. renderContextData.msaaRenderbuffers = undefined;
  264. }
  265. }
  266. }
  267. destroyTexture( texture ) {
  268. const { gl, backend } = this;
  269. const { textureGPU, renderTarget } = backend.get( texture );
  270. this.deallocateRenderBuffers( renderTarget );
  271. gl.deleteTexture( textureGPU );
  272. backend.delete( texture );
  273. }
  274. copyFramebufferToTexture( texture, renderContext ) {
  275. const { gl } = this;
  276. const { state } = this.backend;
  277. const { textureGPU } = this.backend.get( texture );
  278. const width = texture.image.width;
  279. const height = texture.image.height;
  280. state.bindFramebuffer( gl.READ_FRAMEBUFFER, null );
  281. if ( texture.isDepthTexture ) {
  282. const fb = gl.createFramebuffer();
  283. gl.bindFramebuffer( gl.DRAW_FRAMEBUFFER, fb );
  284. gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, textureGPU, 0 );
  285. gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, gl.DEPTH_BUFFER_BIT, gl.NEAREST );
  286. gl.deleteFramebuffer( fb );
  287. } else {
  288. state.bindTexture( gl.TEXTURE_2D, textureGPU );
  289. gl.copyTexSubImage2D( gl.TEXTURE_2D, 0, 0, 0, 0, 0, width, height );
  290. state.unbindTexture();
  291. }
  292. if ( texture.generateMipmaps ) this.generateMipmaps( texture );
  293. this.backend._setFramebuffer( renderContext );
  294. }
  295. // Setup storage for internal depth/stencil buffers and bind to correct framebuffer
  296. setupRenderBufferStorage( renderbuffer, renderContext ) {
  297. const { gl } = this;
  298. const renderTarget = renderContext.renderTarget;
  299. const { samples, depthTexture, depthBuffer, stencilBuffer, width, height } = renderTarget;
  300. gl.bindRenderbuffer( gl.RENDERBUFFER, renderbuffer );
  301. if ( depthBuffer && ! stencilBuffer ) {
  302. let glInternalFormat = gl.DEPTH_COMPONENT24;
  303. if ( samples > 0 ) {
  304. if ( depthTexture && depthTexture.isDepthTexture ) {
  305. if ( depthTexture.type === gl.FLOAT ) {
  306. glInternalFormat = gl.DEPTH_COMPONENT32F;
  307. }
  308. }
  309. gl.renderbufferStorageMultisample( gl.RENDERBUFFER, samples, glInternalFormat, width, height );
  310. } else {
  311. gl.renderbufferStorage( gl.RENDERBUFFER, glInternalFormat, width, height );
  312. }
  313. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer );
  314. } else if ( depthBuffer && stencilBuffer ) {
  315. if ( samples > 0 ) {
  316. gl.renderbufferStorageMultisample( gl.RENDERBUFFER, samples, gl.DEPTH24_STENCIL8, width, height );
  317. } else {
  318. gl.renderbufferStorage( gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height );
  319. }
  320. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, renderbuffer );
  321. }
  322. }
  323. async copyTextureToBuffer( texture, x, y, width, height ) {
  324. const { backend, gl } = this;
  325. const { textureGPU, glFormat, glType } = this.backend.get( texture );
  326. const fb = gl.createFramebuffer();
  327. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, fb );
  328. gl.framebufferTexture2D( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textureGPU, 0 );
  329. const typedArrayType = this._getTypedArrayType( glType );
  330. const bytesPerTexel = this._getBytesPerTexel( glFormat );
  331. const elementCount = width * height;
  332. const byteLength = elementCount * bytesPerTexel;
  333. const buffer = gl.createBuffer();
  334. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
  335. gl.bufferData( gl.PIXEL_PACK_BUFFER, byteLength, gl.STREAM_READ );
  336. gl.readPixels( x, y, width, height, glFormat, glType, 0 );
  337. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, null );
  338. await backend.utils._clientWaitAsync();
  339. const dstBuffer = new typedArrayType( elementCount );
  340. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
  341. gl.getBufferSubData( gl.PIXEL_PACK_BUFFER, 0, dstBuffer );
  342. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, null );
  343. gl.deleteFramebuffer( fb );
  344. return dstBuffer;
  345. }
  346. _getTypedArrayType( glType ) {
  347. const { gl } = this;
  348. if ( glType === gl.UNSIGNED_BYTE ) return Uint8Array;
  349. if ( glType === gl.UNSIGNED_SHORT_4_4_4_4 ) return Uint16Array;
  350. if ( glType === gl.UNSIGNED_SHORT_5_5_5_1 ) return Uint16Array;
  351. if ( glType === gl.UNSIGNED_SHORT_5_6_5 ) return Uint16Array;
  352. if ( glType === gl.UNSIGNED_SHORT ) return Uint16Array;
  353. if ( glType === gl.UNSIGNED_INT ) return Uint32Array;
  354. if ( glType === gl.FLOAT ) return Float32Array;
  355. throw new Error( `Unsupported WebGL type: ${glType}` );
  356. }
  357. _getBytesPerTexel( glFormat ) {
  358. const { gl } = this;
  359. if ( glFormat === gl.RGBA ) return 4;
  360. if ( glFormat === gl.RGB ) return 3;
  361. if ( glFormat === gl.ALPHA ) return 1;
  362. }
  363. }
  364. export default WebGLTextureUtils;