- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 25
 
feat: Resolvable guarded compute pipelines #1874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            aleksanderkatan
  wants to merge
  7
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
feat/resolvable-guarded-compute-pipelines
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      2252a0f
              
                Add tests
              
              
                 14ae189
              
                Add getters to guardedPipeline
              
              
                 e4f5e4e
              
                Update docs
              
              
                 6114ec0
              
                Merge remote-tracking branch 'origin/main' into feat/resolvable-guard…
              
              
                 913abcf
              
                Update packages/typegpu/src/core/root/rootTypes.ts
              
              
                aleksanderkatan ca45c63
              
                Update packages/typegpu/src/core/root/rootTypes.ts
              
              
                aleksanderkatan 1702b33
              
                Merge branch 'main' into feat/resolvable-guarded-compute-pipelines
              
              
                aleksanderkatan File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import { describe, expect } from 'vitest'; | ||
| import * as d from '../src/data/index.ts'; | ||
| import tgpu from '../src/index.ts'; | ||
| import { it } from './utils/extendedIt.ts'; | ||
| 
     | 
||
| describe('resolve', () => { | ||
| const Boid = d.struct({ | ||
| position: d.vec2f, | ||
| color: d.vec4f, | ||
| }); | ||
| 
     | 
||
| const computeFn = tgpu['~unstable'].computeFn({ | ||
| workgroupSize: [1, 1, 1], | ||
| in: { gid: d.builtin.globalInvocationId }, | ||
| })(() => { | ||
| const myBoid = Boid({ | ||
| position: d.vec2f(0, 0), | ||
| color: d.vec4f(1, 0, 0, 1), | ||
| }); | ||
| }); | ||
| 
     | 
||
| const vertexFn = tgpu['~unstable'].vertexFn({ | ||
| out: { pos: d.builtin.position, color: d.vec4f }, | ||
| })(() => { | ||
| const myBoid = Boid(); | ||
| return { pos: d.vec4f(myBoid.position, 0, 1), color: myBoid.color }; | ||
| }); | ||
| 
     | 
||
| const fragmentFn = tgpu['~unstable'].fragmentFn({ | ||
| in: { color: d.vec4f }, | ||
| out: d.vec4f, | ||
| })((input) => { | ||
| return input.color; | ||
| }); | ||
| 
     | 
||
| it('can resolve a render pipeline', ({ root }) => { | ||
| const pipeline = root | ||
| .withVertex(vertexFn, {}) | ||
| .withFragment(fragmentFn, { format: 'rgba8unorm' }) | ||
| .createPipeline(); | ||
| 
     | 
||
| expect(tgpu.resolve({ externals: { pipeline } })).toMatchInlineSnapshot(` | ||
| "struct Boid_1 { | ||
| position: vec2f, | ||
| color: vec4f, | ||
| } | ||
| 
     | 
||
| struct vertexFn_Output_2 { | ||
| @builtin(position) pos: vec4f, | ||
| @location(0) color: vec4f, | ||
| } | ||
| 
     | 
||
| @vertex fn vertexFn_0() -> vertexFn_Output_2 { | ||
| var myBoid = Boid_1(); | ||
| return vertexFn_Output_2(vec4f(myBoid.position, 0, 1), myBoid.color); | ||
| } | ||
| 
     | 
||
| struct fragmentFn_Input_4 { | ||
| @location(0) color: vec4f, | ||
| } | ||
| 
     | 
||
| @fragment fn fragmentFn_3(input: fragmentFn_Input_4) -> @location(0) vec4f { | ||
| return input.color; | ||
| }" | ||
| `); | ||
| }); | ||
| 
     | 
||
| it('can resolve a compute pipeline', ({ root }) => { | ||
| const pipeline = root | ||
| .withCompute(computeFn) | ||
| .createPipeline(); | ||
| 
     | 
||
| expect(tgpu.resolve({ externals: { pipeline } })).toMatchInlineSnapshot(` | ||
| "struct Boid_1 { | ||
| position: vec2f, | ||
| color: vec4f, | ||
| } | ||
| 
     | 
||
| struct computeFn_Input_2 { | ||
| @builtin(global_invocation_id) gid: vec3u, | ||
| } | ||
| 
     | 
||
| @compute @workgroup_size(1, 1, 1) fn computeFn_0(_arg_0: computeFn_Input_2) { | ||
| var myBoid = Boid_1(vec2f(), vec4f(1, 0, 0, 1)); | ||
| }" | ||
| `); | ||
| }); | ||
| 
     | 
||
| it('can resolve a guarded compute pipeline', ({ root }) => { | ||
| const pipelineGuard = root.createGuardedComputePipeline((x, y, z) => { | ||
| 'use gpu'; | ||
                
      
                  aleksanderkatan marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| const myBoid = Boid({ | ||
                
      
                  aleksanderkatan marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| position: d.vec2f(0, 0), | ||
| color: d.vec4f(x, y, z, 1), | ||
| }); | ||
| }); | ||
| 
     | 
||
| expect(tgpu.resolve({ externals: { pipeline: pipelineGuard.pipeline } })) | ||
| .toMatchInlineSnapshot(` | ||
| "@group(0) @binding(0) var<uniform> sizeUniform_1: vec3u; | ||
| 
     | 
||
| struct Boid_3 { | ||
| position: vec2f, | ||
| color: vec4f, | ||
| } | ||
| 
     | 
||
| fn wrappedCallback_2(x: u32, y: u32, z: u32) { | ||
| var myBoid = Boid_3(vec2f(), vec4f(f32(x), f32(y), f32(z), 1)); | ||
| } | ||
| 
     | 
||
| struct mainCompute_Input_4 { | ||
| @builtin(global_invocation_id) id: vec3u, | ||
| } | ||
| 
     | 
||
| @compute @workgroup_size(8, 8, 4) fn mainCompute_0(in: mainCompute_Input_4) { | ||
| if (any(in.id >= sizeUniform_1)) { | ||
| return; | ||
| } | ||
| wrappedCallback_2(in.id.x, in.id.y, in.id.z); | ||
| }" | ||
| `); | ||
| }); | ||
| 
     | 
||
| it('throws when resolving multiple pipelines', ({ root }) => { | ||
| const renderPipeline = root | ||
| .withVertex(vertexFn, {}) | ||
| .withFragment(fragmentFn, { format: 'rgba8unorm' }) | ||
| .createPipeline(); | ||
| 
     | 
||
| const computePipeline = root | ||
| .withCompute(computeFn) | ||
| .createPipeline(); | ||
| 
     | 
||
| expect(() => | ||
| tgpu.resolve({ externals: { renderPipeline, computePipeline } }) | ||
| ).toThrowErrorMatchingInlineSnapshot( | ||
| `[Error: Found 2 pipelines but can only resolve one at a time.]`, | ||
| ); | ||
| }); | ||
| }); | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.