WebGLTextureUtils.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. if ( initialized === false ) {
  9. this._init( this.gl );
  10. initialized = true;
  11. }
  12. }
  13. _init( gl ) {
  14. // Store only WebGL constants here.
  15. wrappingToGL = {
  16. [ RepeatWrapping ]: gl.REPEAT,
  17. [ ClampToEdgeWrapping ]: gl.CLAMP_TO_EDGE,
  18. [ MirroredRepeatWrapping ]: gl.MIRRORED_REPEAT
  19. };
  20. filterToGL = {
  21. [ NearestFilter ]: gl.NEAREST,
  22. [ NearestMipmapNearestFilter ]: gl.NEAREST_MIPMAP_NEAREST,
  23. [ NearestMipmapLinearFilter ]: gl.NEAREST_MIPMAP_LINEAR,
  24. [ LinearFilter ]: gl.LINEAR,
  25. [ LinearMipmapNearestFilter ]: gl.LINEAR_MIPMAP_NEAREST,
  26. [ LinearMipmapLinearFilter ]: gl.LINEAR_MIPMAP_LINEAR
  27. };
  28. compareToGL = {
  29. [ NeverCompare ]: gl.NEVER,
  30. [ AlwaysCompare ]: gl.ALWAYS,
  31. [ LessCompare ]: gl.LESS,
  32. [ LessEqualCompare ]: gl.LEQUAL,
  33. [ EqualCompare ]: gl.EQUAL,
  34. [ GreaterEqualCompare ]: gl.GEQUAL,
  35. [ GreaterCompare ]: gl.GREATER,
  36. [ NotEqualCompare ]: gl.NOTEQUAL
  37. };
  38. }
  39. filterFallback( f ) {
  40. const { gl } = this;
  41. if ( f === NearestFilter || f === NearestMipmapNearestFilter || f === NearestMipmapLinearFilter ) {
  42. return gl.NEAREST;
  43. }
  44. return gl.LINEAR;
  45. }
  46. getGLTextureType( texture ) {
  47. const { gl } = this;
  48. let glTextureType;
  49. if ( texture.isCubeTexture === true ) {
  50. glTextureType = gl.TEXTURE_CUBE_MAP;
  51. } else if ( texture.isDataArrayTexture === true ) {
  52. glTextureType = gl.TEXTURE_2D_ARRAY;
  53. } else {
  54. glTextureType = gl.TEXTURE_2D;
  55. }
  56. return glTextureType;
  57. }
  58. getInternalFormat( internalFormatName, glFormat, glType, colorSpace, forceLinearTransfer = false ) {
  59. const { gl, extensions } = this;
  60. if ( internalFormatName !== null ) {
  61. if ( gl[ internalFormatName ] !== undefined ) return gl[ internalFormatName ];
  62. console.warn( 'THREE.WebGLRenderer: Attempt to use non-existing WebGL internal format \'' + internalFormatName + '\'' );
  63. }
  64. let internalFormat = glFormat;
  65. if ( glFormat === gl.RED ) {
  66. if ( glType === gl.FLOAT ) internalFormat = gl.R32F;
  67. if ( glType === gl.HALF_FLOAT ) internalFormat = gl.R16F;
  68. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.R8;
  69. }
  70. if ( glFormat === gl.RED_INTEGER ) {
  71. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.R8UI;
  72. if ( glType === gl.UNSIGNED_SHORT ) internalFormat = gl.R16UI;
  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.RG ) {
  79. if ( glType === gl.FLOAT ) internalFormat = gl.RG32F;
  80. if ( glType === gl.HALF_FLOAT ) internalFormat = gl.RG16F;
  81. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = gl.RG8;
  82. }
  83. if ( glFormat === gl.RGBA ) {
  84. if ( glType === gl.FLOAT ) internalFormat = gl.RGBA32F;
  85. if ( glType === gl.HALF_FLOAT ) internalFormat = gl.RGBA16F;
  86. if ( glType === gl.UNSIGNED_BYTE ) internalFormat = ( colorSpace === SRGBColorSpace && forceLinearTransfer === false ) ? gl.SRGB8_ALPHA8 : gl.RGBA8;
  87. if ( glType === gl.UNSIGNED_SHORT_4_4_4_4 ) internalFormat = gl.RGBA4;
  88. if ( glType === gl.UNSIGNED_SHORT_5_5_5_1 ) internalFormat = gl.RGB5_A1;
  89. }
  90. if ( glFormat === gl.DEPTH_COMPONENT ) {
  91. if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.DEPTH24_STENCIL8;
  92. if ( glType === gl.FLOAT ) internalFormat = gl.DEPTH_COMPONENT32F;
  93. }
  94. if ( glFormat === gl.DEPTH_STENCIL ) {
  95. if ( glType === gl.UNSIGNED_INT_24_8 ) internalFormat = gl.DEPTH24_STENCIL8;
  96. }
  97. if ( internalFormat === gl.R16F || internalFormat === gl.R32F ||
  98. internalFormat === gl.RG16F || internalFormat === gl.RG32F ||
  99. internalFormat === gl.RGBA16F || internalFormat === gl.RGBA32F ) {
  100. extensions.get( 'EXT_color_buffer_float' );
  101. }
  102. return internalFormat;
  103. }
  104. setTextureParameters( textureType, texture ) {
  105. const { gl, extensions } = this;
  106. gl.texParameteri( textureType, gl.TEXTURE_WRAP_S, wrappingToGL[ texture.wrapS ] );
  107. gl.texParameteri( textureType, gl.TEXTURE_WRAP_T, wrappingToGL[ texture.wrapT ] );
  108. if ( textureType === gl.TEXTURE_3D || textureType === gl.TEXTURE_2D_ARRAY ) {
  109. gl.texParameteri( textureType, gl.TEXTURE_WRAP_R, wrappingToGL[ texture.wrapR ] );
  110. }
  111. gl.texParameteri( textureType, gl.TEXTURE_MAG_FILTER, filterToGL[ texture.magFilter ] );
  112. gl.texParameteri( textureType, gl.TEXTURE_MIN_FILTER, filterToGL[ texture.minFilter ] );
  113. if ( texture.compareFunction ) {
  114. gl.texParameteri( textureType, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE );
  115. gl.texParameteri( textureType, gl.TEXTURE_COMPARE_FUNC, compareToGL[ texture.compareFunction ] );
  116. }
  117. if ( extensions.has( 'EXT_texture_filter_anisotropic' ) === true ) {
  118. //extension = extensions.get( 'EXT_texture_filter_anisotropic' );
  119. if ( texture.magFilter === NearestFilter ) return;
  120. if ( texture.minFilter !== NearestMipmapLinearFilter && texture.minFilter !== LinearMipmapLinearFilter ) return;
  121. if ( texture.type === FloatType && extensions.has( 'OES_texture_float_linear' ) === false ) return; // verify extension for WebGL 1 and WebGL 2
  122. if ( texture.anisotropy > 1 /*|| properties.get( texture ).__currentAnisotropy*/ ) {
  123. //gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, capabilities.getMaxAnisotropy() ) );
  124. //properties.get( texture ).__currentAnisotropy = texture.anisotropy;
  125. }
  126. }
  127. }
  128. async copyTextureToBuffer( texture, x, y, width, height ) {
  129. const { gl } = this;
  130. const { textureGPU, glFormat, glType } = this.backend.get( texture );
  131. const fb = gl.createFramebuffer();
  132. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, fb );
  133. gl.framebufferTexture2D( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textureGPU, 0 );
  134. const typedArrayType = this._getTypedArrayType( glType );
  135. const bytesPerTexel = this._getBytesPerTexel( glFormat );
  136. const elementCount = width * height;
  137. const byteLength = elementCount * bytesPerTexel;
  138. const buffer = gl.createBuffer();
  139. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
  140. gl.bufferData( gl.PIXEL_PACK_BUFFER, byteLength, gl.STREAM_READ );
  141. gl.readPixels( x, y, width, height, glFormat, glType, 0 );
  142. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, null );
  143. const sync = gl.fenceSync( gl.SYNC_GPU_COMMANDS_COMPLETE, 0 );
  144. gl.flush();
  145. await this._clientWaitAsync( sync );
  146. gl.deleteSync( sync );
  147. const dstBuffer = new typedArrayType( elementCount );
  148. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
  149. gl.getBufferSubData( gl.PIXEL_PACK_BUFFER, 0, dstBuffer );
  150. gl.bindBuffer( gl.PIXEL_PACK_BUFFER, null );
  151. return dstBuffer;
  152. }
  153. _getTypedArrayType( glType ) {
  154. const { gl } = this;
  155. if ( glType === gl.UNSIGNED_BYTE ) return Uint8Array;
  156. if ( glType === gl.UNSIGNED_SHORT_4_4_4_4 ) return Uint16Array;
  157. if ( glType === gl.UNSIGNED_SHORT_5_5_5_1 ) return Uint16Array;
  158. if ( glType === gl.UNSIGNED_SHORT_5_6_5 ) return Uint16Array;
  159. if ( glType === gl.UNSIGNED_SHORT ) return Uint16Array;
  160. if ( glType === gl.UNSIGNED_INT ) return Uint32Array;
  161. if ( glType === gl.UNSIGNED_FLOAT ) return Float32Array;
  162. }
  163. _getBytesPerTexel( glFormat ) {
  164. const { gl } = this;
  165. if ( glFormat === gl.RGBA ) return 4;
  166. if ( glFormat === gl.RGB ) return 3;
  167. if ( glFormat === gl.ALPHA ) return 1;
  168. }
  169. _clientWaitAsync( sync ) {
  170. const { gl } = this;
  171. return new Promise( ( resolve, reject ) => {
  172. function test() {
  173. const res = gl.clientWaitSync( sync, gl.SYNC_FLUSH_COMMANDS_BIT, 0 );
  174. if ( res === gl.WAIT_FAILED) {
  175. reject();
  176. return;
  177. }
  178. if ( res === gl.TIMEOUT_EXPIRED) {
  179. requestAnimationFrame( test );
  180. return;
  181. }
  182. resolve();
  183. }
  184. test();
  185. } );
  186. }
  187. }
  188. export default WebGLTextureUtils;