WebGLAttributeUtils.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { IntType } from 'three';
  2. let _id = 0;
  3. class DualAttributeData {
  4. constructor( attributeData, dualBuffer ) {
  5. this.buffers = [ attributeData.bufferGPU, dualBuffer ];
  6. this.type = attributeData.type;
  7. this.bytesPerElement = attributeData.BYTES_PER_ELEMENT;
  8. this.version = attributeData.version;
  9. this.isInteger = attributeData.isInteger;
  10. this.activeBufferIndex = 0;
  11. this.baseId = attributeData.id;
  12. }
  13. get id() {
  14. return `${ this.baseId }|${ this.activeBufferIndex }`;
  15. }
  16. get bufferGPU() {
  17. return this.buffers[ this.activeBufferIndex ];
  18. }
  19. get transformBuffer() {
  20. return this.buffers[ this.activeBufferIndex ^ 1 ];
  21. }
  22. switchBuffers() {
  23. this.activeBufferIndex ^= 1;
  24. }
  25. }
  26. class WebGLAttributeUtils {
  27. constructor( backend ) {
  28. this.backend = backend;
  29. }
  30. createAttribute( attribute, bufferType ) {
  31. const backend = this.backend;
  32. const { gl } = backend;
  33. const array = attribute.array;
  34. const usage = attribute.usage || gl.STATIC_DRAW;
  35. const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
  36. const bufferData = backend.get( bufferAttribute );
  37. let bufferGPU = bufferData.bufferGPU;
  38. if ( bufferGPU === undefined ) {
  39. bufferGPU = this._createBuffer( gl, bufferType, array, usage );
  40. bufferData.bufferGPU = bufferGPU;
  41. bufferData.bufferType = bufferType;
  42. bufferData.version = bufferAttribute.version;
  43. }
  44. //attribute.onUploadCallback();
  45. let type;
  46. if ( array instanceof Float32Array ) {
  47. type = gl.FLOAT;
  48. } else if ( array instanceof Uint16Array ) {
  49. if ( attribute.isFloat16BufferAttribute ) {
  50. type = gl.HALF_FLOAT;
  51. } else {
  52. type = gl.UNSIGNED_SHORT;
  53. }
  54. } else if ( array instanceof Int16Array ) {
  55. type = gl.SHORT;
  56. } else if ( array instanceof Uint32Array ) {
  57. type = gl.UNSIGNED_INT;
  58. } else if ( array instanceof Int32Array ) {
  59. type = gl.INT;
  60. } else if ( array instanceof Int8Array ) {
  61. type = gl.BYTE;
  62. } else if ( array instanceof Uint8Array ) {
  63. type = gl.UNSIGNED_BYTE;
  64. } else if ( array instanceof Uint8ClampedArray ) {
  65. type = gl.UNSIGNED_BYTE;
  66. } else {
  67. throw new Error( 'THREE.WebGLBackend: Unsupported buffer data format: ' + array );
  68. }
  69. let attributeData = {
  70. bufferGPU,
  71. type,
  72. bytesPerElement: array.BYTES_PER_ELEMENT,
  73. version: attribute.version,
  74. isInteger: type === gl.INT || type === gl.UNSIGNED_INT || attribute.gpuType === IntType,
  75. id: _id ++
  76. };
  77. if ( attribute.isStorageBufferAttribute ) {
  78. // create buffer for tranform feedback use
  79. const bufferGPUDual = this._createBuffer( gl, bufferType, array, usage );
  80. attributeData = new DualAttributeData( attributeData, bufferGPUDual );
  81. }
  82. backend.set( attribute, attributeData );
  83. }
  84. updateAttribute( attribute ) {
  85. const backend = this.backend;
  86. const { gl } = backend;
  87. const array = attribute.array;
  88. const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
  89. const bufferData = backend.get( bufferAttribute );
  90. const bufferType = bufferData.bufferType;
  91. const updateRanges = attribute.isInterleavedBufferAttribute ? attribute.data.updateRanges : attribute.updateRanges;
  92. gl.bindBuffer( bufferType, bufferData.bufferGPU );
  93. if ( updateRanges.length === 0 ) {
  94. // Not using update ranges
  95. gl.bufferSubData( bufferType, 0, array );
  96. } else {
  97. for ( let i = 0, l = updateRanges.length; i < l; i ++ ) {
  98. const range = updateRanges[ i ];
  99. gl.bufferSubData( bufferType, range.start * array.BYTES_PER_ELEMENT,
  100. array, range.start, range.count );
  101. }
  102. bufferAttribute.clearUpdateRanges();
  103. }
  104. gl.bindBuffer( bufferType, null );
  105. bufferData.version = bufferAttribute.version;
  106. }
  107. destroyAttribute( attribute ) {
  108. const backend = this.backend;
  109. const { gl } = backend;
  110. if ( attribute.isInterleavedBufferAttribute ) {
  111. backend.delete( attribute.data );
  112. }
  113. const attributeData = backend.get( attribute );
  114. gl.deleteBuffer( attributeData.bufferGPU );
  115. backend.delete( attribute );
  116. }
  117. async getArrayBufferAsync( attribute ) {
  118. const backend = this.backend;
  119. const { gl } = backend;
  120. const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
  121. const { bufferGPU } = backend.get( bufferAttribute );
  122. const array = attribute.array;
  123. const byteLength = array.byteLength;
  124. gl.bindBuffer( gl.COPY_READ_BUFFER, bufferGPU );
  125. const writeBuffer = gl.createBuffer();
  126. gl.bindBuffer( gl.COPY_WRITE_BUFFER, writeBuffer );
  127. gl.bufferData( gl.COPY_WRITE_BUFFER, byteLength, gl.STREAM_READ );
  128. gl.copyBufferSubData( gl.COPY_READ_BUFFER, gl.COPY_WRITE_BUFFER, 0, 0, byteLength );
  129. await backend.utils._clientWaitAsync();
  130. const dstBuffer = new attribute.array.constructor( array.length );
  131. gl.getBufferSubData( gl.COPY_WRITE_BUFFER, 0, dstBuffer );
  132. gl.deleteBuffer( writeBuffer );
  133. return dstBuffer.buffer;
  134. }
  135. _createBuffer( gl, bufferType, array, usage ) {
  136. const bufferGPU = gl.createBuffer();
  137. gl.bindBuffer( bufferType, bufferGPU );
  138. gl.bufferData( bufferType, array, usage );
  139. gl.bindBuffer( bufferType, null );
  140. return bufferGPU;
  141. }
  142. }
  143. export default WebGLAttributeUtils;