WebGLTextureUtils.js 16 KB

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