ComputeNode.js 1012 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import Node from '../core/Node.js';
  2. import { NodeUpdateType } from '../core/constants.js';
  3. class ComputeNode extends Node {
  4. constructor( computeNode, count, workgroupSize = [ 64 ] ) {
  5. super( 'void' );
  6. this.isComputeNode = true;
  7. this.computeNode = computeNode;
  8. this.count = count;
  9. this.workgroupSize = workgroupSize;
  10. this.dispatchCount = 0;
  11. this.updateType = NodeUpdateType.Object;
  12. this.updateDispatchCount();
  13. }
  14. updateDispatchCount() {
  15. const { count, workgroupSize } = this;
  16. let size = workgroupSize[ 0 ];
  17. for ( let i = 1; i < workgroupSize.length; i ++ )
  18. size *= workgroupSize[ i ];
  19. this.dispatchCount = Math.ceil( count / size );
  20. }
  21. update( { renderer } ) {
  22. renderer.compute( this );
  23. }
  24. generate( builder ) {
  25. const { shaderStage } = builder;
  26. if ( shaderStage === 'compute' ) {
  27. const snippet = this.computeNode.build( builder, 'void' );
  28. if ( snippet !== '' ) {
  29. builder.addFlowCode( snippet );
  30. }
  31. }
  32. }
  33. }
  34. export default ComputeNode;