WebGLTextureUtils.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. import { LinearFilter, LinearMipmapLinearFilter, LinearMipmapNearestFilter, NearestFilter, NearestMipmapLinearFilter, NearestMipmapNearestFilter, FloatType, MirroredRepeatWrapping, ClampToEdgeWrapping, RepeatWrapping, SRGBColorSpace, NeverCompare, AlwaysCompare, LessCompare, LessEqualCompare, EqualCompare, GreaterEqualCompare, GreaterCompare, NotEqualCompare } from '../../../constants.js';
  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 || texture.isCompressedArrayTexture === true ) {
  53. glTextureType = gl.TEXTURE_2D_ARRAY;
  54. } else if ( texture.isData3DTexture === true ) { // TODO: isCompressed3DTexture, wait for #26642
  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. gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  165. gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  166. gl.pixelStorei( gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  167. gl.pixelStorei( gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE );
  168. gl.texParameteri( textureType, gl.TEXTURE_WRAP_S, wrappingToGL[ texture.wrapS ] );
  169. gl.texParameteri( textureType, gl.TEXTURE_WRAP_T, wrappingToGL[ texture.wrapT ] );
  170. if ( textureType === gl.TEXTURE_3D || textureType === gl.TEXTURE_2D_ARRAY ) {
  171. gl.texParameteri( textureType, gl.TEXTURE_WRAP_R, wrappingToGL[ texture.wrapR ] );
  172. }
  173. gl.texParameteri( textureType, gl.TEXTURE_MAG_FILTER, filterToGL[ texture.magFilter ] );
  174. const hasMipmaps = texture.mipmaps !== undefined && texture.mipmaps.length > 0;
  175. // follow WebGPU backend mapping for texture filtering
  176. const minFilter = texture.minFilter === LinearFilter && hasMipmaps ? LinearMipmapLinearFilter : texture.minFilter;
  177. gl.texParameteri( textureType, gl.TEXTURE_MIN_FILTER, filterToGL[ minFilter ] );
  178. if ( texture.compareFunction ) {
  179. gl.texParameteri( textureType, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE );
  180. gl.texParameteri( textureType, gl.TEXTURE_COMPARE_FUNC, compareToGL[ texture.compareFunction ] );
  181. }
  182. if ( extensions.has( 'EXT_texture_filter_anisotropic' ) === true ) {
  183. if ( texture.magFilter === NearestFilter ) return;
  184. if ( texture.minFilter !== NearestMipmapLinearFilter && texture.minFilter !== LinearMipmapLinearFilter ) return;
  185. if ( texture.type === FloatType && extensions.has( 'OES_texture_float_linear' ) === false ) return; // verify extension for WebGL 1 and WebGL 2
  186. if ( texture.anisotropy > 1 ) {
  187. const extension = extensions.get( 'EXT_texture_filter_anisotropic' );
  188. gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, backend.getMaxAnisotropy() ) );
  189. }
  190. }
  191. }
  192. createDefaultTexture( texture ) {
  193. const { gl, backend, defaultTextures } = this;
  194. const glTextureType = this.getGLTextureType( texture );
  195. let textureGPU = defaultTextures[ glTextureType ];
  196. if ( textureGPU === undefined ) {
  197. textureGPU = gl.createTexture();
  198. backend.state.bindTexture( glTextureType, textureGPU );
  199. gl.texParameteri( glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  200. gl.texParameteri( glTextureType, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  201. // gl.texImage2D( glTextureType, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
  202. defaultTextures[ glTextureType ] = textureGPU;
  203. }
  204. backend.set( texture, {
  205. textureGPU,
  206. glTextureType,
  207. isDefault: true
  208. } );
  209. }
  210. createTexture( texture, options ) {
  211. const { gl, backend } = this;
  212. const { levels, width, height, depth } = options;
  213. const glFormat = backend.utils.convert( texture.format, texture.colorSpace );
  214. const glType = backend.utils.convert( texture.type );
  215. const glInternalFormat = this.getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace, texture.isVideoTexture );
  216. const textureGPU = gl.createTexture();
  217. const glTextureType = this.getGLTextureType( texture );
  218. backend.state.bindTexture( glTextureType, textureGPU );
  219. this.setTextureParameters( glTextureType, texture );
  220. if ( texture.isDataArrayTexture || texture.isCompressedArrayTexture ) {
  221. gl.texStorage3D( gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, width, height, depth );
  222. } else if ( texture.isData3DTexture ) {
  223. gl.texStorage3D( gl.TEXTURE_3D, levels, glInternalFormat, width, height, depth );
  224. } else if ( ! texture.isVideoTexture ) {
  225. gl.texStorage2D( glTextureType, levels, glInternalFormat, width, height );
  226. }
  227. backend.set( texture, {
  228. textureGPU,
  229. glTextureType,
  230. glFormat,
  231. glType,
  232. glInternalFormat
  233. } );
  234. }
  235. copyBufferToTexture( buffer, texture ) {
  236. const { gl, backend } = this;
  237. const { textureGPU, glTextureType, glFormat, glType } = backend.get( texture );
  238. const { width, height } = texture.source.data;
  239. gl.bindBuffer( gl.PIXEL_UNPACK_BUFFER, buffer );
  240. backend.state.bindTexture( glTextureType, textureGPU );
  241. gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, false );
  242. gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false );
  243. gl.texSubImage2D( glTextureType, 0, 0, 0, width, height, glFormat, glType, 0 );
  244. gl.bindBuffer( gl.PIXEL_UNPACK_BUFFER, null );
  245. backend.state.unbindTexture();
  246. // debug
  247. // const framebuffer = gl.createFramebuffer();
  248. // gl.bindFramebuffer( gl.FRAMEBUFFER, framebuffer );
  249. // gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, glTextureType, textureGPU, 0 );
  250. // const readout = new Float32Array( width * height * 4 );
  251. // const altFormat = gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_FORMAT );
  252. // const altType = gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_TYPE );
  253. // gl.readPixels( 0, 0, width, height, altFormat, altType, readout );
  254. // gl.bindFramebuffer( gl.FRAMEBUFFER, null );
  255. // console.log( readout );
  256. }
  257. updateTexture( texture, options ) {
  258. const { gl } = this;
  259. const { width, height } = options;
  260. const { textureGPU, glTextureType, glFormat, glType, glInternalFormat } = this.backend.get( texture );
  261. if ( texture.isRenderTargetTexture || ( textureGPU === undefined /* unsupported texture format */ ) )
  262. return;
  263. const getImage = ( source ) => {
  264. if ( source.isDataTexture ) {
  265. return source.image.data;
  266. } else if ( source instanceof ImageBitmap || source instanceof OffscreenCanvas || source instanceof HTMLImageElement || source instanceof HTMLCanvasElement ) {
  267. return source;
  268. }
  269. return source.data;
  270. };
  271. this.backend.state.bindTexture( glTextureType, textureGPU );
  272. this.setTextureParameters( glTextureType, texture );
  273. if ( texture.isCompressedTexture ) {
  274. const mipmaps = texture.mipmaps;
  275. const image = options.image;
  276. for ( let i = 0; i < mipmaps.length; i ++ ) {
  277. const mipmap = mipmaps[ i ];
  278. if ( texture.isCompressedArrayTexture ) {
  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.framebuffers ) {
  329. for ( const cacheKey in renderContextData.framebuffers ) {
  330. gl.deleteFramebuffer( renderContextData.framebuffers[ cacheKey ] );
  331. }
  332. delete renderContextData.framebuffers;
  333. }
  334. if ( renderContextData.depthRenderbuffer ) {
  335. gl.deleteRenderbuffer( renderContextData.depthRenderbuffer );
  336. delete renderContextData.depthRenderbuffer;
  337. }
  338. if ( renderContextData.stencilRenderbuffer ) {
  339. gl.deleteRenderbuffer( renderContextData.stencilRenderbuffer );
  340. delete renderContextData.stencilRenderbuffer;
  341. }
  342. if ( renderContextData.msaaFrameBuffer ) {
  343. gl.deleteFramebuffer( renderContextData.msaaFrameBuffer );
  344. delete renderContextData.msaaFrameBuffer;
  345. }
  346. if ( renderContextData.msaaRenderbuffers ) {
  347. for ( let i = 0; i < renderContextData.msaaRenderbuffers.length; i ++ ) {
  348. gl.deleteRenderbuffer( renderContextData.msaaRenderbuffers[ i ] );
  349. }
  350. delete renderContextData.msaaRenderbuffers;
  351. }
  352. }
  353. }
  354. destroyTexture( texture ) {
  355. const { gl, backend } = this;
  356. const { textureGPU, renderTarget } = backend.get( texture );
  357. this.deallocateRenderBuffers( renderTarget );
  358. gl.deleteTexture( textureGPU );
  359. backend.delete( texture );
  360. }
  361. copyTextureToTexture( srcTexture, dstTexture, srcRegion = null, dstPosition = null, level = 0 ) {
  362. const { gl, backend } = this;
  363. const { state } = this.backend;
  364. const { textureGPU: dstTextureGPU, glTextureType, glType, glFormat } = backend.get( dstTexture );
  365. let width, height, minX, minY;
  366. let dstX, dstY;
  367. if ( srcRegion !== null ) {
  368. width = srcRegion.max.x - srcRegion.min.x;
  369. height = srcRegion.max.y - srcRegion.min.y;
  370. minX = srcRegion.min.x;
  371. minY = srcRegion.min.y;
  372. } else {
  373. width = srcTexture.image.width;
  374. height = srcTexture.image.height;
  375. minX = 0;
  376. minY = 0;
  377. }
  378. if ( dstPosition !== null ) {
  379. dstX = dstPosition.x;
  380. dstY = dstPosition.y;
  381. } else {
  382. dstX = 0;
  383. dstY = 0;
  384. }
  385. state.bindTexture( glTextureType, dstTextureGPU );
  386. // As another texture upload may have changed pixelStorei
  387. // parameters, make sure they are correct for the dstTexture
  388. gl.pixelStorei( gl.UNPACK_ALIGNMENT, dstTexture.unpackAlignment );
  389. gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, dstTexture.flipY );
  390. gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, dstTexture.premultiplyAlpha );
  391. gl.pixelStorei( gl.UNPACK_ALIGNMENT, dstTexture.unpackAlignment );
  392. const currentUnpackRowLen = gl.getParameter( gl.UNPACK_ROW_LENGTH );
  393. const currentUnpackImageHeight = gl.getParameter( gl.UNPACK_IMAGE_HEIGHT );
  394. const currentUnpackSkipPixels = gl.getParameter( gl.UNPACK_SKIP_PIXELS );
  395. const currentUnpackSkipRows = gl.getParameter( gl.UNPACK_SKIP_ROWS );
  396. const currentUnpackSkipImages = gl.getParameter( gl.UNPACK_SKIP_IMAGES );
  397. const image = srcTexture.isCompressedTexture ? srcTexture.mipmaps[ level ] : srcTexture.image;
  398. gl.pixelStorei( gl.UNPACK_ROW_LENGTH, image.width );
  399. gl.pixelStorei( gl.UNPACK_IMAGE_HEIGHT, image.height );
  400. gl.pixelStorei( gl.UNPACK_SKIP_PIXELS, minX );
  401. gl.pixelStorei( gl.UNPACK_SKIP_ROWS, minY );
  402. if ( srcTexture.isDataTexture ) {
  403. gl.texSubImage2D( gl.TEXTURE_2D, level, dstX, dstY, width, height, glFormat, glType, image.data );
  404. } else {
  405. if ( srcTexture.isCompressedTexture ) {
  406. gl.compressedTexSubImage2D( gl.TEXTURE_2D, level, dstX, dstY, image.width, image.height, glFormat, image.data );
  407. } else {
  408. gl.texSubImage2D( gl.TEXTURE_2D, level, dstX, dstY, width, height, glFormat, glType, image );
  409. }
  410. }
  411. gl.pixelStorei( gl.UNPACK_ROW_LENGTH, currentUnpackRowLen );
  412. gl.pixelStorei( gl.UNPACK_IMAGE_HEIGHT, currentUnpackImageHeight );
  413. gl.pixelStorei( gl.UNPACK_SKIP_PIXELS, currentUnpackSkipPixels );
  414. gl.pixelStorei( gl.UNPACK_SKIP_ROWS, currentUnpackSkipRows );
  415. gl.pixelStorei( gl.UNPACK_SKIP_IMAGES, currentUnpackSkipImages );
  416. // Generate mipmaps only when copying level 0
  417. if ( level === 0 && dstTexture.generateMipmaps ) gl.generateMipmap( gl.TEXTURE_2D );
  418. state.unbindTexture();
  419. }
  420. copyFramebufferToTexture( texture, renderContext ) {
  421. const { gl } = this;
  422. const { state } = this.backend;
  423. const { textureGPU } = this.backend.get( texture );
  424. const width = texture.image.width;
  425. const height = texture.image.height;
  426. const requireDrawFrameBuffer = texture.isDepthTexture === true || ( renderContext.renderTarget && renderContext.renderTarget.samples > 0 );
  427. if ( requireDrawFrameBuffer ) {
  428. let mask;
  429. let attachment;
  430. if ( texture.isDepthTexture === true ) {
  431. mask = gl.DEPTH_BUFFER_BIT;
  432. attachment = gl.DEPTH_ATTACHMENT;
  433. if ( renderContext.stencil ) {
  434. mask |= gl.STENCIL_BUFFER_BIT;
  435. }
  436. } else {
  437. mask = gl.COLOR_BUFFER_BIT;
  438. attachment = gl.COLOR_ATTACHMENT0;
  439. }
  440. const fb = gl.createFramebuffer();
  441. state.bindFramebuffer( gl.DRAW_FRAMEBUFFER, fb );
  442. gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, attachment, gl.TEXTURE_2D, textureGPU, 0 );
  443. gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, gl.NEAREST );
  444. gl.deleteFramebuffer( fb );
  445. } else {
  446. state.bindTexture( gl.TEXTURE_2D, textureGPU );
  447. gl.copyTexSubImage2D( gl.TEXTURE_2D, 0, 0, 0, 0, 0, width, height );
  448. state.unbindTexture();
  449. }
  450. if ( texture.generateMipmaps ) this.generateMipmaps( texture );
  451. this.backend._setFramebuffer( renderContext );
  452. }
  453. // Setup storage for internal depth/stencil buffers and bind to correct framebuffer
  454. setupRenderBufferStorage( renderbuffer, renderContext ) {
  455. const { gl } = this;
  456. const renderTarget = renderContext.renderTarget;
  457. const { samples, depthTexture, depthBuffer, stencilBuffer, width, height } = renderTarget;
  458. gl.bindRenderbuffer( gl.RENDERBUFFER, renderbuffer );
  459. if ( depthBuffer && ! stencilBuffer ) {
  460. let glInternalFormat = gl.DEPTH_COMPONENT24;
  461. if ( samples > 0 ) {
  462. if ( depthTexture && depthTexture.isDepthTexture ) {
  463. if ( depthTexture.type === gl.FLOAT ) {
  464. glInternalFormat = gl.DEPTH_COMPONENT32F;
  465. }
  466. }
  467. gl.renderbufferStorageMultisample( gl.RENDERBUFFER, samples, glInternalFormat, width, height );
  468. } else {
  469. gl.renderbufferStorage( gl.RENDERBUFFER, glInternalFormat, width, height );
  470. }
  471. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer );
  472. } else if ( depthBuffer && stencilBuffer ) {
  473. if ( samples > 0 ) {
  474. gl.renderbufferStorageMultisample( gl.RENDERBUFFER, samples, gl.DEPTH24_STENCIL8, width, height );
  475. } else {
  476. gl.renderbufferStorage( gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height );
  477. }
  478. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, renderbuffer );
  479. }
  480. }
  481. async copyTextureToBuffer( texture, x, y, width, height ) {
  482. const { backend, gl } = this;
  483. const { textureGPU, glFormat, glType } = this.backend.get( texture );
  484. const fb = gl.createFramebuffer();
  485. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, fb );
  486. gl.framebufferTexture2D( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textureGPU, 0 );
  487. const typedArrayType = this._getTypedArrayType( glType );
  488. const bytesPerTexel = this._getBytesPerTexel( glType, glFormat );
  489. const elementCount = width * height;
  490. const byteLength = elementCount * bytesPerTexel;
  491. const buffer = gl.createBuffer();
  492. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
  493. gl.bufferData( gl.PIXEL_PACK_BUFFER, byteLength, gl.STREAM_READ );
  494. gl.readPixels( x, y, width, height, glFormat, glType, 0 );
  495. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, null );
  496. await backend.utils._clientWaitAsync();
  497. const dstBuffer = new typedArrayType( byteLength / typedArrayType.BYTES_PER_ELEMENT );
  498. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
  499. gl.getBufferSubData( gl.PIXEL_PACK_BUFFER, 0, dstBuffer );
  500. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, null );
  501. gl.deleteFramebuffer( fb );
  502. return dstBuffer;
  503. }
  504. _getTypedArrayType( glType ) {
  505. const { gl } = this;
  506. if ( glType === gl.UNSIGNED_BYTE ) return Uint8Array;
  507. if ( glType === gl.UNSIGNED_SHORT_4_4_4_4 ) return Uint16Array;
  508. if ( glType === gl.UNSIGNED_SHORT_5_5_5_1 ) return Uint16Array;
  509. if ( glType === gl.UNSIGNED_SHORT_5_6_5 ) return Uint16Array;
  510. if ( glType === gl.UNSIGNED_SHORT ) return Uint16Array;
  511. if ( glType === gl.UNSIGNED_INT ) return Uint32Array;
  512. if ( glType === gl.HALF_FLOAT ) return Uint16Array;
  513. if ( glType === gl.FLOAT ) return Float32Array;
  514. throw new Error( `Unsupported WebGL type: ${glType}` );
  515. }
  516. _getBytesPerTexel( glType, glFormat ) {
  517. const { gl } = this;
  518. let bytesPerComponent = 0;
  519. if ( glType === gl.UNSIGNED_BYTE ) bytesPerComponent = 1;
  520. if ( glType === gl.UNSIGNED_SHORT_4_4_4_4 ||
  521. glType === gl.UNSIGNED_SHORT_5_5_5_1 ||
  522. glType === gl.UNSIGNED_SHORT_5_6_5 ||
  523. glType === gl.UNSIGNED_SHORT ||
  524. glType === gl.HALF_FLOAT ) bytesPerComponent = 2;
  525. if ( glType === gl.UNSIGNED_INT ||
  526. glType === gl.FLOAT ) bytesPerComponent = 4;
  527. if ( glFormat === gl.RGBA ) return bytesPerComponent * 4;
  528. if ( glFormat === gl.RGB ) return bytesPerComponent * 3;
  529. if ( glFormat === gl.ALPHA ) return bytesPerComponent;
  530. }
  531. }
  532. export default WebGLTextureUtils;
粤ICP备19079148号