WebGPUAttributeUtils.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 bufferData = backend.get( bufferAttribute );
  28. let buffer = bufferData.buffer;
  29. if ( buffer === undefined ) {
  30. const device = backend.device;
  31. const array = bufferAttribute.array;
  32. const size = array.byteLength + ( ( 4 - ( array.byteLength % 4 ) ) % 4 ); // ensure 4 byte alignment, see #20441
  33. buffer = device.createBuffer( {
  34. label: bufferAttribute.name,
  35. size: size,
  36. usage: usage,
  37. mappedAtCreation: true
  38. } );
  39. new array.constructor( buffer.getMappedRange() ).set( array );
  40. buffer.unmap();
  41. bufferData.buffer = buffer;
  42. }
  43. }
  44. updateAttribute( attribute ) {
  45. const bufferAttribute = this._getBufferAttribute( attribute );
  46. const backend = this.backend;
  47. const device = backend.device;
  48. const buffer = backend.get( bufferAttribute ).buffer;
  49. const array = bufferAttribute.array;
  50. const updateRange = bufferAttribute.updateRange;
  51. if ( updateRange.count === - 1 ) {
  52. // Not using update ranges
  53. device.queue.writeBuffer(
  54. buffer,
  55. 0,
  56. array,
  57. 0
  58. );
  59. } else {
  60. device.queue.writeBuffer(
  61. buffer,
  62. 0,
  63. array,
  64. updateRange.offset * array.BYTES_PER_ELEMENT,
  65. updateRange.count * array.BYTES_PER_ELEMENT
  66. );
  67. updateRange.count = - 1; // reset range
  68. }
  69. }
  70. createShaderVertexBuffers( renderObject ) {
  71. const attributes = renderObject.getAttributes();
  72. const vertexBuffers = new Map();
  73. for ( let slot = 0; slot < attributes.length; slot ++ ) {
  74. const geometryAttribute = attributes[ slot ];
  75. const bytesPerElement = geometryAttribute.array.BYTES_PER_ELEMENT;
  76. const bufferAttribute = this._getBufferAttribute( geometryAttribute );
  77. let vertexBufferLayout = vertexBuffers.get( bufferAttribute );
  78. if ( vertexBufferLayout === undefined ) {
  79. let arrayStride, stepMode;
  80. if ( geometryAttribute.isInterleavedBufferAttribute === true ) {
  81. arrayStride = geometryAttribute.data.stride * bytesPerElement;
  82. stepMode = geometryAttribute.data.isInstancedInterleavedBuffer ? GPUInputStepMode.Instance : GPUInputStepMode.Vertex;
  83. } else {
  84. arrayStride = geometryAttribute.itemSize * bytesPerElement;
  85. stepMode = geometryAttribute.isInstancedBufferAttribute ? GPUInputStepMode.Instance : GPUInputStepMode.Vertex;
  86. }
  87. vertexBufferLayout = {
  88. arrayStride,
  89. attributes: [],
  90. stepMode
  91. };
  92. vertexBuffers.set( bufferAttribute, vertexBufferLayout );
  93. }
  94. const format = this._getVertexFormat( geometryAttribute );
  95. const offset = ( geometryAttribute.isInterleavedBufferAttribute === true ) ? geometryAttribute.offset * bytesPerElement : 0;
  96. vertexBufferLayout.attributes.push( {
  97. shaderLocation: slot,
  98. offset,
  99. format
  100. } );
  101. }
  102. return Array.from( vertexBuffers.values() );
  103. }
  104. destroyAttribute( attribute ) {
  105. const backend = this.backend;
  106. const data = backend.get( this._getBufferAttribute( attribute ) );
  107. data.buffer.destroy();
  108. backend.delete( attribute );
  109. }
  110. async getArrayBufferAsync( attribute ) {
  111. const backend = this.backend;
  112. const device = backend.device;
  113. const data = backend.get( this._getBufferAttribute( attribute ) );
  114. const bufferGPU = data.buffer;
  115. const size = bufferGPU.size;
  116. let readBufferGPU = data.readBuffer;
  117. let needsUnmap = true;
  118. if ( readBufferGPU === undefined ) {
  119. readBufferGPU = device.createBuffer( {
  120. label: attribute.name,
  121. size,
  122. usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
  123. } );
  124. needsUnmap = false;
  125. data.readBuffer = readBufferGPU;
  126. }
  127. const cmdEncoder = device.createCommandEncoder( {} );
  128. cmdEncoder.copyBufferToBuffer(
  129. bufferGPU,
  130. 0,
  131. readBufferGPU,
  132. 0,
  133. size
  134. );
  135. if ( needsUnmap ) readBufferGPU.unmap();
  136. const gpuCommands = cmdEncoder.finish();
  137. device.queue.submit( [ gpuCommands ] );
  138. await readBufferGPU.mapAsync( GPUMapMode.READ );
  139. const arrayBuffer = readBufferGPU.getMappedRange();
  140. return arrayBuffer;
  141. }
  142. _getVertexFormat( geometryAttribute ) {
  143. const { itemSize, normalized } = geometryAttribute;
  144. const ArrayType = geometryAttribute.array.constructor;
  145. const AttributeType = geometryAttribute.constructor;
  146. let format;
  147. if ( itemSize == 1 ) {
  148. format = typeArraysToVertexFormatPrefixForItemSize1.get( ArrayType );
  149. } else {
  150. const prefixOptions = typedAttributeToVertexFormatPrefix.get( AttributeType ) || typedArraysToVertexFormatPrefix.get( ArrayType );
  151. const prefix = prefixOptions[ normalized ? 1 : 0 ];
  152. if ( prefix ) {
  153. const bytesPerUnit = ArrayType.BYTES_PER_ELEMENT * itemSize;
  154. const paddedBytesPerUnit = Math.floor( ( bytesPerUnit + 3 ) / 4 ) * 4;
  155. const paddedItemSize = paddedBytesPerUnit / ArrayType.BYTES_PER_ELEMENT;
  156. if ( paddedItemSize % 1 ) {
  157. throw new Error( 'THREE.WebGPUAttributeUtils: Bad vertex format item size.' );
  158. }
  159. format = `${prefix}x${paddedItemSize}`;
  160. }
  161. }
  162. if ( ! format ) {
  163. console.error( 'THREE.WebGPUAttributeUtils: Vertex format not supported yet.' );
  164. }
  165. return format;
  166. }
  167. _getBufferAttribute( attribute ) {
  168. if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
  169. return attribute;
  170. }
  171. }
  172. export default WebGPUAttributeUtils;