WebGPUComputePipelines.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import WebGPUProgrammableStage from './WebGPUProgrammableStage.js';
  2. class WebGPUComputePipelines {
  3. constructor( device ) {
  4. this.device = device;
  5. this.pipelines = new WeakMap();
  6. this.stages = {
  7. compute: new WeakMap()
  8. };
  9. }
  10. get( param ) {
  11. let pipeline = this.pipelines.get( param );
  12. // @TODO: Reuse compute pipeline if possible, introduce WebGPUComputePipeline
  13. if ( pipeline === undefined ) {
  14. const device = this.device;
  15. const shader = {
  16. computeShader: param.shader
  17. };
  18. // programmable stage
  19. let stageCompute = this.stages.compute.get( shader );
  20. if ( stageCompute === undefined ) {
  21. stageCompute = new WebGPUProgrammableStage( device, shader.computeShader, 'compute' );
  22. this.stages.compute.set( shader, stageCompute );
  23. }
  24. pipeline = device.createComputePipeline( {
  25. compute: stageCompute.stage
  26. } );
  27. this.pipelines.set( param, pipeline );
  28. }
  29. return pipeline;
  30. }
  31. dispose() {
  32. this.pipelines = new WeakMap();
  33. this.stages = {
  34. compute: new WeakMap()
  35. };
  36. }
  37. }
  38. export default WebGPUComputePipelines;