WebGPUGeometries.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. class WebGPUGeometries {
  2. constructor( attributes, info ) {
  3. this.attributes = attributes;
  4. this.info = info;
  5. this.geometries = new WeakMap();
  6. }
  7. update( geometry ) {
  8. if ( this.geometries.has( geometry ) === false ) {
  9. const disposeCallback = onGeometryDispose.bind( this );
  10. this.geometries.set( geometry, disposeCallback );
  11. this.info.memory.geometries ++;
  12. geometry.addEventListener( 'dispose', disposeCallback );
  13. }
  14. const geometryAttributes = geometry.attributes;
  15. for ( const name in geometryAttributes ) {
  16. this.attributes.update( geometryAttributes[ name ] );
  17. }
  18. const index = geometry.index;
  19. if ( index !== null ) {
  20. this.attributes.update( index, true );
  21. }
  22. }
  23. }
  24. function onGeometryDispose( event ) {
  25. const geometry = event.target;
  26. const disposeCallback = this.geometries.get( geometry );
  27. this.geometries.delete( geometry );
  28. this.info.memory.geometries --;
  29. geometry.removeEventListener( 'dispose', disposeCallback );
  30. //
  31. const index = geometry.index;
  32. const geometryAttributes = geometry.attributes;
  33. if ( index !== null ) {
  34. this.attributes.remove( index );
  35. }
  36. for ( const name in geometryAttributes ) {
  37. this.attributes.remove( geometryAttributes[ name ] );
  38. }
  39. }
  40. export default WebGPUGeometries;