2
0

WebGPUAttributeUtils.js 6.3 KB

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