WebGPUAttributeUtils.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { Float16BufferAttribute } from 'three';
  2. import { GPUInputStepMode } from './WebGPUConstants.js';
  3. const typedArraysToVertexFormatPrefix = new Map( [
  4. [ Int8Array, [ 'sint8', 'snorm8' ]],
  5. [ Uint8Array, [ 'uint8', 'unorm8' ]],
  6. [ Int16Array, [ 'sint16', 'snorm16' ]],
  7. [ Uint16Array, [ 'uint16', 'unorm16' ]],
  8. [ Int32Array, [ 'sint32', 'snorm32' ]],
  9. [ Uint32Array, [ 'uint32', 'unorm32' ]],
  10. [ Float32Array, [ 'float32', ]],
  11. ] );
  12. const typedAttributeToVertexFormatPrefix = new Map( [
  13. [ Float16BufferAttribute, [ 'float16', ]],
  14. ] );
  15. const typeArraysToVertexFormatPrefixForItemSize1 = new Map( [
  16. [ Int32Array, 'sint32' ],
  17. [ Uint32Array, 'uint32' ],
  18. [ Float32Array, 'float32' ]
  19. ] );
  20. class WebGPUAttributeUtils {
  21. constructor( backend ) {
  22. this.backend = backend;
  23. }
  24. createAttribute( attribute, usage ) {
  25. const bufferAttribute = this._getBufferAttribute( attribute );
  26. const backend = this.backend;
  27. const device = backend.device;
  28. const array = bufferAttribute.array;
  29. const size = array.byteLength + ( ( 4 - ( array.byteLength % 4 ) ) % 4 ); // ensure 4 byte alignment, see #20441
  30. const buffer = device.createBuffer( {
  31. label: bufferAttribute.name,
  32. size: size,
  33. usage: usage,
  34. mappedAtCreation: true
  35. } );
  36. new array.constructor( buffer.getMappedRange() ).set( array );
  37. buffer.unmap();
  38. backend.get( attribute ).buffer = buffer;
  39. }
  40. updateAttribute( attribute ) {
  41. const bufferAttribute = this._getBufferAttribute( attribute );
  42. const backend = this.backend;
  43. const device = backend.device;
  44. const buffer = backend.get( attribute ).buffer;
  45. const array = bufferAttribute.array;
  46. const updateRange = bufferAttribute.updateRange;
  47. if ( updateRange.count === - 1 ) {
  48. // Not using update ranges
  49. device.queue.writeBuffer(
  50. buffer,
  51. 0,
  52. array,
  53. 0
  54. );
  55. } else {
  56. device.queue.writeBuffer(
  57. buffer,
  58. 0,
  59. array,
  60. updateRange.offset * array.BYTES_PER_ELEMENT,
  61. updateRange.count * array.BYTES_PER_ELEMENT
  62. );
  63. updateRange.count = - 1; // reset range
  64. }
  65. }
  66. createShaderAttributes( renderObject ) {
  67. const attributes = renderObject.getAttributes();
  68. const shaderAttributes = [];
  69. for ( let slot = 0; slot < attributes.length; slot ++ ) {
  70. const geometryAttribute = attributes[ slot ];
  71. const bytesPerElement = geometryAttribute.array.BYTES_PER_ELEMENT;
  72. const format = this._getVertexFormat( geometryAttribute );
  73. let arrayStride = geometryAttribute.itemSize * bytesPerElement;
  74. let offset = 0;
  75. let stepMode = geometryAttribute.isInstancedBufferAttribute ? GPUInputStepMode.Instance : GPUInputStepMode.Vertex;
  76. if ( geometryAttribute.isInterleavedBufferAttribute === true ) {
  77. // @TODO: It can be optimized for "vertexBuffers" on RenderPipeline
  78. arrayStride = geometryAttribute.data.stride * bytesPerElement;
  79. offset = geometryAttribute.offset * bytesPerElement;
  80. if ( geometryAttribute.data.isInstancedInterleavedBuffer ) stepMode = GPUInputStepMode.Instance;
  81. }
  82. shaderAttributes.push( {
  83. geometryAttribute,
  84. arrayStride,
  85. stepMode,
  86. offset,
  87. format,
  88. slot
  89. } );
  90. }
  91. return shaderAttributes;
  92. }
  93. destroyAttribute( attribute ) {
  94. const backend = this.backend;
  95. const data = backend.get( attribute );
  96. data.buffer.destroy();
  97. backend.delete( attribute );
  98. }
  99. async getArrayBuffer( attribute ) {
  100. const backend = this.backend;
  101. const device = backend.device;
  102. const data = backend.get( attribute );
  103. //const bufferAttribute = this._getBufferAttribute( attribute );
  104. const bufferGPU = data.buffer;
  105. const size = bufferGPU.size;
  106. let readBufferGPU = data.readBuffer;
  107. let needsUnmap = true;
  108. if ( readBufferGPU === undefined ) {
  109. readBufferGPU = device.createBuffer( {
  110. label: attribute.name,
  111. size,
  112. usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
  113. } );
  114. needsUnmap = false;
  115. data.readBuffer = readBufferGPU;
  116. }
  117. const cmdEncoder = device.createCommandEncoder( {} );
  118. cmdEncoder.copyBufferToBuffer(
  119. bufferGPU,
  120. 0,
  121. readBufferGPU,
  122. 0,
  123. size
  124. );
  125. if ( needsUnmap ) readBufferGPU.unmap();
  126. const gpuCommands = cmdEncoder.finish();
  127. device.queue.submit( [ gpuCommands ] );
  128. await readBufferGPU.mapAsync( GPUMapMode.READ );
  129. const arrayBuffer = readBufferGPU.getMappedRange();
  130. return arrayBuffer;
  131. }
  132. _getVertexFormat( geometryAttribute ) {
  133. const { itemSize, normalized } = geometryAttribute;
  134. const ArrayType = geometryAttribute.array.constructor;
  135. const AttributeType = geometryAttribute.constructor;
  136. let format;
  137. if ( itemSize == 1 ) {
  138. format = typeArraysToVertexFormatPrefixForItemSize1.get( ArrayType );
  139. } else {
  140. const prefixOptions = typedAttributeToVertexFormatPrefix.get( AttributeType ) || typedArraysToVertexFormatPrefix.get( ArrayType );
  141. const prefix = prefixOptions[ normalized ? 1 : 0 ];
  142. if ( prefix ) {
  143. const bytesPerUnit = ArrayType.BYTES_PER_ELEMENT * itemSize;
  144. const paddedBytesPerUnit = Math.floor( ( bytesPerUnit + 3 ) / 4 ) * 4;
  145. const paddedItemSize = paddedBytesPerUnit / ArrayType.BYTES_PER_ELEMENT;
  146. if ( paddedItemSize % 1 ) {
  147. throw new Error( 'THREE.WebGPUAttributeUtils: Bad vertex format item size.' );
  148. }
  149. format = `${prefix}x${paddedItemSize}`;
  150. }
  151. }
  152. if ( ! format ) {
  153. console.error( 'THREE.WebGPUAttributeUtils: Vertex format not supported yet.' );
  154. }
  155. return format;
  156. }
  157. _getBufferAttribute( attribute ) {
  158. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  159. return attribute;
  160. }
  161. }
  162. export default WebGPUAttributeUtils;