WebGLTextureUtils.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 if ( texture.isData3DTexture === true ) {
  55. glTextureType = gl.TEXTURE_3D;
  56. } else {
  57. glTextureType = gl.TEXTURE_2D;
  58. }
  59. return glTextureType;
  60. }
  61. getInternalFormat( internalFormatName, glFormat, glType, colorSpace, forceLinearTransfer = false ) {
  62. const { gl, extensions } = this;
  63. if ( internalFormatName !== null ) {
  64. if ( gl[ internalFormatName ] !== undefined ) return gl[ internalFormatName ];
  65. console.warn( 'THREE.WebGLRenderer: Attempt to use non-existing WebGL internal format \'' + internalFormatName + '\'' );
  66. }
  67. let internalFormat = glFormat;
  68. if ( glFormat === gl.RED ) {
  69. if ( glType === gl.FLOAT ) internalFormat = gl.R32F;
  70. if ( glType === gl.HALF_FLOAT ) internalFormat = gl.R16F;
  71. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.R8;
  72. if ( glType === gl.UNSIGNED_SHORT ) internalFormat = gl.R16;
  73. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.R32UI;
  74. if ( glType === gl.BYTE ) internalFormat = gl.R8I;
  75. if ( glType === gl.SHORT ) internalFormat = gl.R16I;
  76. if ( glType === gl.INT ) internalFormat = gl.R32I;
  77. }
  78. if ( glFormat === gl.RED_INTEGER ) {
  79. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.R8UI;
  80. if ( glType === gl.UNSIGNED_SHORT ) internalFormat = gl.R16UI;
  81. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.R32UI;
  82. if ( glType === gl.BYTE ) internalFormat = gl.R8I;
  83. if ( glType === gl.SHORT ) internalFormat = gl.R16I;
  84. if ( glType === gl.INT ) internalFormat = gl.R32I;
  85. }
  86. if ( glFormat === gl.RG ) {
  87. if ( glType === gl.FLOAT ) internalFormat = gl.RG32F;
  88. if ( glType === gl.HALF_FLOAT ) internalFormat = gl.RG16F;
  89. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.RG8;
  90. if ( glType === gl.UNSIGNED_SHORT ) internalFormat = gl.RG16;
  91. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.RG32UI;
  92. if ( glType === gl.BYTE ) internalFormat = gl.RG8I;
  93. if ( glType === gl.SHORT ) internalFormat = gl.RG16I;
  94. if ( glType === gl.INT ) internalFormat = gl.RG32I;
  95. }
  96. if ( glFormat === gl.RG_INTEGER ) {
  97. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.RG8UI;
  98. if ( glType === gl.UNSIGNED_SHORT ) internalFormat = gl.RG16UI;
  99. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.RG32UI;
  100. if ( glType === gl.BYTE ) internalFormat = gl.RG8I;
  101. if ( glType === gl.SHORT ) internalFormat = gl.RG16I;
  102. if ( glType === gl.INT ) internalFormat = gl.RG32I;
  103. }
  104. if ( glFormat === gl.RGB ) {
  105. if ( glType === gl.FLOAT ) internalFormat = gl.RGB32F;
  106. if ( glType === gl.HALF_FLOAT ) internalFormat = gl.RGB16F;
  107. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.RGB8;
  108. if ( glType === gl.UNSIGNED_SHORT ) internalFormat = gl.RGB16;
  109. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.RGB32UI;
  110. if ( glType === gl.BYTE ) internalFormat = gl.RGB8I;
  111. if ( glType === gl.SHORT ) internalFormat = gl.RGB16I;
  112. if ( glType === gl.INT ) internalFormat = gl.RGB32I;
  113. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = ( colorSpace === SRGBColorSpace && forceLinearTransfer === false ) ? gl.SRGB8 : gl.RGB8;
  114. if ( glType === gl.UNSIGNED_SHORT_5_6_5 ) internalFormat = gl.RGB565;
  115. if ( glType === gl.UNSIGNED_SHORT_5_5_5_1 ) internalFormat = gl.RGB5_A1;
  116. if ( glType === gl.UNSIGNED_SHORT_4_4_4_4 ) internalFormat = gl.RGB4;
  117. if ( glType === gl.UNSIGNED_INT_5_9_9_9_REV ) internalFormat = gl.RGB9_E5;
  118. }
  119. if ( glFormat === gl.RGB_INTEGER ) {
  120. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.RGB8UI;
  121. if ( glType === gl.UNSIGNED_SHORT ) internalFormat = gl.RGB16UI;
  122. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.RGB32UI;
  123. if ( glType === gl.BYTE ) internalFormat = gl.RGB8I;
  124. if ( glType === gl.SHORT ) internalFormat = gl.RGB16I;
  125. if ( glType === gl.INT ) internalFormat = gl.RGB32I;
  126. }
  127. if ( glFormat === gl.RGBA ) {
  128. if ( glType === gl.FLOAT ) internalFormat = gl.RGBA32F;
  129. if ( glType === gl.HALF_FLOAT ) internalFormat = gl.RGBA16F;
  130. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.RGBA8;
  131. if ( glType === gl.UNSIGNED_SHORT ) internalFormat = gl.RGBA16;
  132. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.RGBA32UI;
  133. if ( glType === gl.BYTE ) internalFormat = gl.RGBA8I;
  134. if ( glType === gl.SHORT ) internalFormat = gl.RGBA16I;
  135. if ( glType === gl.INT ) internalFormat = gl.RGBA32I;
  136. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = ( colorSpace === SRGBColorSpace && forceLinearTransfer === false ) ? gl.SRGB8_ALPHA8 : gl.RGBA8;
  137. if ( glType === gl.UNSIGNED_SHORT_4_4_4_4 ) internalFormat = gl.RGBA4;
  138. if ( glType === gl.UNSIGNED_SHORT_5_5_5_1 ) internalFormat = gl.RGB5_A1;
  139. }
  140. if ( glFormat === gl.RGBA_INTEGER ) {
  141. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.RGBA8UI;
  142. if ( glType === gl.UNSIGNED_SHORT ) internalFormat = gl.RGBA16UI;
  143. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.RGBA32UI;
  144. if ( glType === gl.BYTE ) internalFormat = gl.RGBA8I;
  145. if ( glType === gl.SHORT ) internalFormat = gl.RGBA16I;
  146. if ( glType === gl.INT ) internalFormat = gl.RGBA32I;
  147. }
  148. if ( glFormat === gl.DEPTH_COMPONENT ) {
  149. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.DEPTH24_STENCIL8;
  150. if ( glType === gl.FLOAT ) internalFormat = gl.DEPTH_COMPONENT32F;
  151. }
  152. if ( glFormat === gl.DEPTH_STENCIL ) {
  153. if ( glType === gl.UNSIGNED_INT_24_8 ) internalFormat = gl.DEPTH24_STENCIL8;
  154. }
  155. if ( internalFormat === gl.R16F || internalFormat === gl.R32F ||
  156. internalFormat === gl.RG16F || internalFormat === gl.RG32F ||
  157. internalFormat === gl.RGBA16F || internalFormat === gl.RGBA32F ) {
  158. extensions.get( 'EXT_color_buffer_float' );
  159. }
  160. return internalFormat;
  161. }
  162. setTextureParameters( textureType, texture ) {
  163. const { gl, extensions, backend } = this;
  164. const { currentAnisotropy } = backend.get( texture );
  165. gl.texParameteri( textureType, gl.TEXTURE_WRAP_S, wrappingToGL[ texture.wrapS ] );
  166. gl.texParameteri( textureType, gl.TEXTURE_WRAP_T, wrappingToGL[ texture.wrapT ] );
  167. if ( textureType === gl.TEXTURE_3D || textureType === gl.TEXTURE_2D_ARRAY ) {
  168. gl.texParameteri( textureType, gl.TEXTURE_WRAP_R, wrappingToGL[ texture.wrapR ] );
  169. }
  170. gl.texParameteri( textureType, gl.TEXTURE_MAG_FILTER, filterToGL[ texture.magFilter ] );
  171. // follow WebGPU backend mapping for texture filtering
  172. const minFilter = ! texture.isVideoTexture && texture.minFilter === LinearFilter ? LinearMipmapLinearFilter : texture.minFilter;
  173. gl.texParameteri( textureType, gl.TEXTURE_MIN_FILTER, filterToGL[ minFilter ] );
  174. if ( texture.compareFunction ) {
  175. gl.texParameteri( textureType, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE );
  176. gl.texParameteri( textureType, gl.TEXTURE_COMPARE_FUNC, compareToGL[ texture.compareFunction ] );
  177. }
  178. if ( extensions.has( 'EXT_texture_filter_anisotropic' ) === true ) {
  179. if ( texture.magFilter === NearestFilter ) return;
  180. if ( texture.minFilter !== NearestMipmapLinearFilter && texture.minFilter !== LinearMipmapLinearFilter ) return;
  181. if ( texture.type === FloatType && extensions.has( 'OES_texture_float_linear' ) === false ) return; // verify extension for WebGL 1 and WebGL 2
  182. if ( texture.anisotropy > 1 || currentAnisotropy !== texture.anisotropy ) {
  183. const extension = extensions.get( 'EXT_texture_filter_anisotropic' );
  184. gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, backend.getMaxAnisotropy() ) );
  185. backend.get( texture ).currentAnisotropy = texture.anisotropy;
  186. }
  187. }
  188. }
  189. createDefaultTexture( texture ) {
  190. const { gl, backend, defaultTextures } = this;
  191. const glTextureType = this.getGLTextureType( texture );
  192. let textureGPU = defaultTextures[ glTextureType ];
  193. if ( textureGPU === undefined ) {
  194. textureGPU = gl.createTexture();
  195. backend.state.bindTexture( glTextureType, textureGPU );
  196. gl.texParameteri( glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  197. gl.texParameteri( glTextureType, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  198. // gl.texImage2D( glTextureType, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
  199. defaultTextures[ glTextureType ] = textureGPU;
  200. }
  201. backend.set( texture, {
  202. textureGPU,
  203. glTextureType,
  204. isDefault: true
  205. } );
  206. }
  207. createTexture( texture, options ) {
  208. const { gl, backend } = this;
  209. const { levels, width, height, depth } = options;
  210. const glFormat = backend.utils.convert( texture.format, texture.colorSpace );
  211. const glType = backend.utils.convert( texture.type );
  212. const glInternalFormat = this.getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace, texture.isVideoTexture );
  213. const textureGPU = gl.createTexture();
  214. const glTextureType = this.getGLTextureType( texture );
  215. backend.state.bindTexture( glTextureType, textureGPU );
  216. gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  217. gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  218. gl.pixelStorei( gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  219. gl.pixelStorei( gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE );
  220. this.setTextureParameters( glTextureType, texture );
  221. if ( texture.isDataArrayTexture ) {
  222. gl.texStorage3D( gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, width, height, depth );
  223. } else if ( texture.isData3DTexture ) {
  224. gl.texStorage3D( gl.TEXTURE_3D, levels, glInternalFormat, width, height, depth );
  225. } else if ( ! texture.isVideoTexture ) {
  226. gl.texStorage2D( glTextureType, levels, glInternalFormat, width, height );
  227. }
  228. backend.set( texture, {
  229. textureGPU,
  230. glTextureType,
  231. glFormat,
  232. glType,
  233. glInternalFormat
  234. } );
  235. }
  236. copyBufferToTexture( buffer, texture ) {
  237. const { gl, backend } = this;
  238. const { textureGPU, glTextureType, glFormat, glType } = backend.get( texture );
  239. const { width, height } = texture.source.data;
  240. gl.bindBuffer( gl.PIXEL_UNPACK_BUFFER, buffer );
  241. backend.state.bindTexture( glTextureType, textureGPU );
  242. gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, false );
  243. gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false );
  244. gl.texSubImage2D( glTextureType, 0, 0, 0, width, height, glFormat, glType, 0 );
  245. gl.bindBuffer( gl.PIXEL_UNPACK_BUFFER, null );
  246. backend.state.unbindTexture();
  247. // debug
  248. // const framebuffer = gl.createFramebuffer();
  249. // gl.bindFramebuffer( gl.FRAMEBUFFER, framebuffer );
  250. // gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, glTextureType, textureGPU, 0 );
  251. // const readout = new Float32Array( width * height * 4 );
  252. // const altFormat = gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_FORMAT );
  253. // const altType = gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_TYPE );
  254. // gl.readPixels( 0, 0, width, height, altFormat, altType, readout );
  255. // gl.bindFramebuffer( gl.FRAMEBUFFER, null );
  256. // console.log( readout );
  257. }
  258. updateTexture( texture, options ) {
  259. const { gl } = this;
  260. const { width, height } = options;
  261. const { textureGPU, glTextureType, glFormat, glType, glInternalFormat } = this.backend.get( texture );
  262. if ( texture.isRenderTargetTexture || ( textureGPU === undefined /* unsupported texture format */ ) )
  263. return;
  264. const getImage = ( source ) => {
  265. if ( source.isDataTexture ) {
  266. return source.image.data;
  267. } else if ( source instanceof ImageBitmap || source instanceof OffscreenCanvas || source instanceof HTMLImageElement || source instanceof HTMLCanvasElement ) {
  268. return source;
  269. }
  270. return source.data;
  271. };
  272. this.backend.state.bindTexture( glTextureType, textureGPU );
  273. if ( texture.isCompressedTexture ) {
  274. const mipmaps = texture.mipmaps;
  275. for ( let i = 0; i < mipmaps.length; i ++ ) {
  276. const mipmap = mipmaps[ i ];
  277. if ( texture.isCompressedArrayTexture ) {
  278. const image = options.image;
  279. if ( texture.format !== gl.RGBA ) {
  280. if ( glFormat !== null ) {
  281. gl.compressedTexSubImage3D( gl.TEXTURE_2D_ARRAY, i, 0, 0, 0, mipmap.width, mipmap.height, image.depth, glFormat, mipmap.data, 0, 0 );
  282. } else {
  283. console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' );
  284. }
  285. } else {
  286. gl.texSubImage3D( gl.TEXTURE_2D_ARRAY, i, 0, 0, 0, mipmap.width, mipmap.height, image.depth, glFormat, glType, mipmap.data );
  287. }
  288. } else {
  289. if ( glFormat !== null ) {
  290. gl.compressedTexSubImage2D( gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, mipmap.data );
  291. } else {
  292. console.warn( 'Unsupported compressed texture format' );
  293. }
  294. }
  295. }
  296. } else if ( texture.isCubeTexture ) {
  297. const images = options.images;
  298. for ( let i = 0; i < 6; i ++ ) {
  299. const image = getImage( images[ i ] );
  300. gl.texSubImage2D( gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, width, height, glFormat, glType, image );
  301. }
  302. } else if ( texture.isDataArrayTexture ) {
  303. const image = options.image;
  304. gl.texSubImage3D( gl.TEXTURE_2D_ARRAY, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data );
  305. } else if ( texture.isData3DTexture ) {
  306. const image = options.image;
  307. gl.texSubImage3D( gl.TEXTURE_3D, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data );
  308. } else if ( texture.isVideoTexture ) {
  309. texture.update();
  310. gl.texImage2D( glTextureType, 0, glInternalFormat, glFormat, glType, options.image );
  311. } else {
  312. const image = getImage( options.image );
  313. gl.texSubImage2D( glTextureType, 0, 0, 0, width, height, glFormat, glType, image );
  314. }
  315. }
  316. generateMipmaps( texture ) {
  317. const { gl, backend } = this;
  318. const { textureGPU, glTextureType } = backend.get( texture );
  319. backend.state.bindTexture( glTextureType, textureGPU );
  320. gl.generateMipmap( glTextureType );
  321. }
  322. deallocateRenderBuffers( renderTarget ) {
  323. const { gl, backend } = this;
  324. // remove framebuffer reference
  325. if ( renderTarget ) {
  326. const renderContextData = backend.get( renderTarget );
  327. renderContextData.renderBufferStorageSetup = undefined;
  328. if ( renderContextData.framebuffer ) {
  329. gl.deleteFramebuffer( renderContextData.framebuffer );
  330. renderContextData.framebuffer = undefined;
  331. }
  332. if ( renderContextData.depthRenderbuffer ) {
  333. gl.deleteRenderbuffer( renderContextData.depthRenderbuffer );
  334. renderContextData.depthRenderbuffer = undefined;
  335. }
  336. if ( renderContextData.stencilRenderbuffer ) {
  337. gl.deleteRenderbuffer( renderContextData.stencilRenderbuffer );
  338. renderContextData.stencilRenderbuffer = undefined;
  339. }
  340. if ( renderContextData.msaaFrameBuffer ) {
  341. gl.deleteFramebuffer( renderContextData.msaaFrameBuffer );
  342. renderContextData.msaaFrameBuffer = undefined;
  343. }
  344. if ( renderContextData.msaaRenderbuffers ) {
  345. for ( let i = 0; i < renderContextData.msaaRenderbuffers.length; i ++ ) {
  346. gl.deleteRenderbuffer( renderContextData.msaaRenderbuffers[ i ] );
  347. }
  348. renderContextData.msaaRenderbuffers = undefined;
  349. }
  350. }
  351. }
  352. destroyTexture( texture ) {
  353. const { gl, backend } = this;
  354. const { textureGPU, renderTarget } = backend.get( texture );
  355. this.deallocateRenderBuffers( renderTarget );
  356. gl.deleteTexture( textureGPU );
  357. backend.delete( texture );
  358. }
  359. copyTextureToTexture( srcTexture, dstTexture, srcRegion = null, dstPosition = null, level = 0 ) {
  360. const { gl, backend } = this;
  361. const { state } = this.backend;
  362. const { textureGPU: dstTextureGPU, glTextureType, glType, glFormat } = backend.get( dstTexture );
  363. let width, height, minX, minY;
  364. let dstX, dstY;
  365. if ( srcRegion !== null ) {
  366. width = srcRegion.max.x - srcRegion.min.x;
  367. height = srcRegion.max.y - srcRegion.min.y;
  368. minX = srcRegion.min.x;
  369. minY = srcRegion.min.y;
  370. } else {
  371. width = srcTexture.image.width;
  372. height = srcTexture.image.height;
  373. minX = 0;
  374. minY = 0;
  375. }
  376. if ( dstPosition !== null ) {
  377. dstX = dstPosition.x;
  378. dstY = dstPosition.y;
  379. } else {
  380. dstX = 0;
  381. dstY = 0;
  382. }
  383. state.bindTexture( glTextureType, dstTextureGPU );
  384. // As another texture upload may have changed pixelStorei
  385. // parameters, make sure they are correct for the dstTexture
  386. gl.pixelStorei( gl.UNPACK_ALIGNMENT, dstTexture.unpackAlignment );
  387. gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, dstTexture.flipY );
  388. gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, dstTexture.premultiplyAlpha );
  389. gl.pixelStorei( gl.UNPACK_ALIGNMENT, dstTexture.unpackAlignment );
  390. const currentUnpackRowLen = gl.getParameter( gl.UNPACK_ROW_LENGTH );
  391. const currentUnpackImageHeight = gl.getParameter( gl.UNPACK_IMAGE_HEIGHT );
  392. const currentUnpackSkipPixels = gl.getParameter( gl.UNPACK_SKIP_PIXELS );
  393. const currentUnpackSkipRows = gl.getParameter( gl.UNPACK_SKIP_ROWS );
  394. const currentUnpackSkipImages = gl.getParameter( gl.UNPACK_SKIP_IMAGES );
  395. const image = srcTexture.isCompressedTexture ? srcTexture.mipmaps[ level ] : srcTexture.image;
  396. gl.pixelStorei( gl.UNPACK_ROW_LENGTH, image.width );
  397. gl.pixelStorei( gl.UNPACK_IMAGE_HEIGHT, image.height );
  398. gl.pixelStorei( gl.UNPACK_SKIP_PIXELS, minX );
  399. gl.pixelStorei( gl.UNPACK_SKIP_ROWS, minY );
  400. if ( srcTexture.isDataTexture ) {
  401. gl.texSubImage2D( gl.TEXTURE_2D, level, dstX, dstY, width, height, glFormat, glType, image.data );
  402. } else {
  403. if ( srcTexture.isCompressedTexture ) {
  404. gl.compressedTexSubImage2D( gl.TEXTURE_2D, level, dstX, dstY, image.width, image.height, glFormat, image.data );
  405. } else {
  406. gl.texSubImage2D( gl.TEXTURE_2D, level, dstX, dstY, width, height, glFormat, glType, image );
  407. }
  408. }
  409. gl.pixelStorei( gl.UNPACK_ROW_LENGTH, currentUnpackRowLen );
  410. gl.pixelStorei( gl.UNPACK_IMAGE_HEIGHT, currentUnpackImageHeight );
  411. gl.pixelStorei( gl.UNPACK_SKIP_PIXELS, currentUnpackSkipPixels );
  412. gl.pixelStorei( gl.UNPACK_SKIP_ROWS, currentUnpackSkipRows );
  413. gl.pixelStorei( gl.UNPACK_SKIP_IMAGES, currentUnpackSkipImages );
  414. // Generate mipmaps only when copying level 0
  415. if ( level === 0 && dstTexture.generateMipmaps ) gl.generateMipmap( gl.TEXTURE_2D );
  416. state.unbindTexture();
  417. }
  418. copyFramebufferToTexture( texture, renderContext ) {
  419. const { gl } = this;
  420. const { state } = this.backend;
  421. const { textureGPU } = this.backend.get( texture );
  422. const width = texture.image.width;
  423. const height = texture.image.height;
  424. const requireDrawFrameBuffer = texture.isDepthTexture === true || ( renderContext.renderTarget && renderContext.renderTarget.samples > 0 );
  425. if ( requireDrawFrameBuffer ) {
  426. let mask;
  427. let attachment;
  428. if ( texture.isDepthTexture === true ) {
  429. mask = gl.DEPTH_BUFFER_BIT;
  430. attachment = gl.DEPTH_ATTACHMENT;
  431. if ( renderContext.stencil ) {
  432. mask |= gl.STENCIL_BUFFER_BIT;
  433. }
  434. } else {
  435. mask = gl.COLOR_BUFFER_BIT;
  436. attachment = gl.COLOR_ATTACHMENT0;
  437. }
  438. const fb = gl.createFramebuffer();
  439. state.bindFramebuffer( gl.DRAW_FRAMEBUFFER, fb );
  440. gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, attachment, gl.TEXTURE_2D, textureGPU, 0 );
  441. gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, gl.NEAREST );
  442. gl.deleteFramebuffer( fb );
  443. } else {
  444. state.bindTexture( gl.TEXTURE_2D, textureGPU );
  445. gl.copyTexSubImage2D( gl.TEXTURE_2D, 0, 0, 0, 0, 0, width, height );
  446. state.unbindTexture();
  447. }
  448. if ( texture.generateMipmaps ) this.generateMipmaps( texture );
  449. this.backend._setFramebuffer( renderContext );
  450. }
  451. // Setup storage for internal depth/stencil buffers and bind to correct framebuffer
  452. setupRenderBufferStorage( renderbuffer, renderContext ) {
  453. const { gl } = this;
  454. const renderTarget = renderContext.renderTarget;
  455. const { samples, depthTexture, depthBuffer, stencilBuffer, width, height } = renderTarget;
  456. gl.bindRenderbuffer( gl.RENDERBUFFER, renderbuffer );
  457. if ( depthBuffer && ! stencilBuffer ) {
  458. let glInternalFormat = gl.DEPTH_COMPONENT24;
  459. if ( samples > 0 ) {
  460. if ( depthTexture && depthTexture.isDepthTexture ) {
  461. if ( depthTexture.type === gl.FLOAT ) {
  462. glInternalFormat = gl.DEPTH_COMPONENT32F;
  463. }
  464. }
  465. gl.renderbufferStorageMultisample( gl.RENDERBUFFER, samples, glInternalFormat, width, height );
  466. } else {
  467. gl.renderbufferStorage( gl.RENDERBUFFER, glInternalFormat, width, height );
  468. }
  469. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer );
  470. } else if ( depthBuffer && stencilBuffer ) {
  471. if ( samples > 0 ) {
  472. gl.renderbufferStorageMultisample( gl.RENDERBUFFER, samples, gl.DEPTH24_STENCIL8, width, height );
  473. } else {
  474. gl.renderbufferStorage( gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height );
  475. }
  476. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, renderbuffer );
  477. }
  478. }
  479. async copyTextureToBuffer( texture, x, y, width, height ) {
  480. const { backend, gl } = this;
  481. const { textureGPU, glFormat, glType } = this.backend.get( texture );
  482. const fb = gl.createFramebuffer();
  483. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, fb );
  484. gl.framebufferTexture2D( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textureGPU, 0 );
  485. const typedArrayType = this._getTypedArrayType( glType );
  486. const bytesPerTexel = this._getBytesPerTexel( glFormat );
  487. const elementCount = width * height;
  488. const byteLength = elementCount * bytesPerTexel;
  489. const buffer = gl.createBuffer();
  490. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
  491. gl.bufferData( gl.PIXEL_PACK_BUFFER, byteLength, gl.STREAM_READ );
  492. gl.readPixels( x, y, width, height, glFormat, glType, 0 );
  493. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, null );
  494. await backend.utils._clientWaitAsync();
  495. const dstBuffer = new typedArrayType( byteLength / typedArrayType.BYTES_PER_ELEMENT );
  496. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
  497. gl.getBufferSubData( gl.PIXEL_PACK_BUFFER, 0, dstBuffer );
  498. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, null );
  499. gl.deleteFramebuffer( fb );
  500. return dstBuffer;
  501. }
  502. _getTypedArrayType( glType ) {
  503. const { gl } = this;
  504. if ( glType === gl.UNSIGNED_BYTE ) return Uint8Array;
  505. if ( glType === gl.UNSIGNED_SHORT_4_4_4_4 ) return Uint16Array;
  506. if ( glType === gl.UNSIGNED_SHORT_5_5_5_1 ) return Uint16Array;
  507. if ( glType === gl.UNSIGNED_SHORT_5_6_5 ) return Uint16Array;
  508. if ( glType === gl.UNSIGNED_SHORT ) return Uint16Array;
  509. if ( glType === gl.UNSIGNED_INT ) return Uint32Array;
  510. if ( glType === gl.FLOAT ) return Float32Array;
  511. throw new Error( `Unsupported WebGL type: ${glType}` );
  512. }
  513. _getBytesPerTexel( glFormat ) {
  514. const { gl } = this;
  515. if ( glFormat === gl.RGBA ) return 4;
  516. if ( glFormat === gl.RGB ) return 3;
  517. if ( glFormat === gl.ALPHA ) return 1;
  518. }
  519. }
  520. export default WebGLTextureUtils;