1
1
import { Bindings } from "./compiler" ;
2
2
import { ThreadGroupSize } from "./slang-wasm" ;
3
3
4
- export class ComputePipeline
5
- {
4
+ export class ComputePipeline {
6
5
pipeline : GPUComputePipeline | undefined ;
7
6
pipelineLayout : GPUPipelineLayout | "auto" | undefined ;
8
7
@@ -16,28 +15,24 @@ export class ComputePipeline
16
15
bindGroup : GPUBindGroup | undefined ;
17
16
18
17
// thread group size (array of 3 integers)
19
- threadGroupSize : ThreadGroupSize | { x : number , y : number , z : number } | undefined ;
18
+ threadGroupSize : ThreadGroupSize | { x : number , y : number , z : number } | undefined ;
20
19
21
20
// resource name (string) -> binding descriptor
22
21
resourceBindings : Bindings | undefined ;
23
22
24
- constructor ( device : GPUDevice )
25
- {
23
+ constructor ( device : GPUDevice ) {
26
24
this . device = device ;
27
25
}
28
26
29
- setThreadGroupSize ( size : ThreadGroupSize | { x : number , y : number , z : number } )
30
- {
27
+ setThreadGroupSize ( size : ThreadGroupSize | { x : number , y : number , z : number } ) {
31
28
this . threadGroupSize = size ;
32
29
}
33
30
34
- createPipelineLayout ( resourceDescriptors : Bindings )
35
- {
31
+ createPipelineLayout ( resourceDescriptors : Bindings ) {
36
32
this . resourceBindings = resourceDescriptors ;
37
33
38
34
const entries : GPUBindGroupLayoutEntry [ ] = [ ] ;
39
- for ( const [ name , binding ] of this . resourceBindings )
40
- {
35
+ for ( const [ name , binding ] of this . resourceBindings ) {
41
36
entries . push ( binding ) ;
42
37
}
43
38
const bindGroupLayoutDescriptor : GPUBindGroupLayoutDescriptor = {
@@ -46,83 +41,73 @@ export class ComputePipeline
46
41
} ;
47
42
48
43
const bindGroupLayout = this . device . createBindGroupLayout ( bindGroupLayoutDescriptor ) ;
49
- const layout = this . device . createPipelineLayout ( { bindGroupLayouts : [ bindGroupLayout ] } ) ;
44
+ const layout = this . device . createPipelineLayout ( { bindGroupLayouts : [ bindGroupLayout ] } ) ;
50
45
51
46
this . pipelineLayout = layout ;
52
47
}
53
48
54
- createPipeline ( shaderModule : GPUShaderModule , resources : Map < string , GPUTexture | GPUBuffer > | null )
55
- {
56
- if ( this . pipelineLayout == undefined )
57
- throw new Error ( "Cannot create pipeline without layout" )
49
+ createPipeline ( shaderModule : GPUShaderModule , resources : Map < string , GPUTexture | GPUBuffer > | null ) {
50
+ if ( this . pipelineLayout == undefined )
51
+ throw new Error ( "Cannot create pipeline without layout" ) ;
58
52
const pipeline = this . device . createComputePipeline ( {
59
53
label : 'compute pipeline' ,
60
54
layout : this . pipelineLayout ,
61
- compute : { module : shaderModule } ,
62
- } ) ;
55
+ compute : { module : shaderModule } ,
56
+ } ) ;
63
57
64
58
this . pipeline = pipeline ;
65
-
59
+
66
60
// If resources are provided, create the bind group right away
67
61
if ( resources )
68
62
this . createBindGroup ( resources ) ;
69
63
}
70
64
71
- createBindGroup ( allocatedResources : Map < string , GPUObjectBase > )
72
- {
73
- if ( this . resourceBindings == undefined )
74
- throw new Error ( "No resource bindings" )
75
- if ( this . pipeline == undefined )
76
- throw new Error ( "No pipeline" )
65
+ createBindGroup ( allocatedResources : Map < string , GPUObjectBase > ) {
66
+ if ( this . resourceBindings == undefined )
67
+ throw new Error ( "No resource bindings" ) ;
68
+ if ( this . pipeline == undefined )
69
+ throw new Error ( "No pipeline" ) ;
77
70
78
71
const entries : GPUBindGroupEntry [ ] = [ ] ;
79
- for ( const [ name , resource ] of allocatedResources )
80
- {
72
+ for ( const [ name , resource ] of allocatedResources ) {
81
73
const bindInfo = this . resourceBindings . get ( name ) ;
82
-
83
- if ( bindInfo )
84
- {
85
- if ( bindInfo . buffer )
86
- {
87
- if ( ! ( resource instanceof GPUBuffer ) ) {
88
- throw new Error ( "Invalid state" )
74
+
75
+ if ( bindInfo ) {
76
+ if ( bindInfo . buffer ) {
77
+ if ( ! ( resource instanceof GPUBuffer ) ) {
78
+ throw new Error ( "Invalid state" ) ;
89
79
}
90
- entries . push ( { binding : bindInfo . binding , resource : { buffer : resource } } ) ;
80
+ entries . push ( { binding : bindInfo . binding , resource : { buffer : resource } } ) ;
91
81
}
92
- else if ( bindInfo . storageTexture )
93
- {
94
- if ( ! ( resource instanceof GPUTexture ) ) {
95
- throw new Error ( "Invalid state" )
82
+ else if ( bindInfo . storageTexture ) {
83
+ if ( ! ( resource instanceof GPUTexture ) ) {
84
+ throw new Error ( "Invalid state" ) ;
96
85
}
97
- entries . push ( { binding : bindInfo . binding , resource : resource . createView ( ) } ) ;
86
+ entries . push ( { binding : bindInfo . binding , resource : resource . createView ( ) } ) ;
98
87
}
99
- else if ( bindInfo . texture )
100
- {
101
- if ( ! ( resource instanceof GPUTexture ) ) {
102
- throw new Error ( "Invalid state" )
88
+ else if ( bindInfo . texture ) {
89
+ if ( ! ( resource instanceof GPUTexture ) ) {
90
+ throw new Error ( "Invalid state" ) ;
103
91
}
104
- entries . push ( { binding : bindInfo . binding , resource : resource . createView ( ) } ) ;
92
+ entries . push ( { binding : bindInfo . binding , resource : resource . createView ( ) } ) ;
105
93
}
106
94
}
107
95
}
108
96
109
97
// Check that all resources are bound
110
- if ( entries . length != this . resourceBindings . size )
111
- {
98
+ if ( entries . length != this . resourceBindings . size ) {
112
99
let missingEntries = [ ]
113
100
// print out the names of the resources that aren't bound
114
- for ( const [ name , resource ] of this . resourceBindings )
115
- {
101
+ for ( const [ name , resource ] of this . resourceBindings ) {
116
102
missingEntries = [ ]
117
- if ( ! entries . find ( entry => entry . binding == resource . binding ) )
118
- {
103
+ if ( ! entries . find ( entry => entry . binding == resource . binding ) ) {
119
104
missingEntries . push ( name ) ;
120
105
}
121
106
}
122
107
123
108
throw new Error ( "Cannot create bind-group. The following resources are not bound: " + missingEntries . join ( ", " ) ) ;
124
109
}
125
-
110
+
126
111
this . bindGroup = this . device . createBindGroup ( {
127
112
layout : this . pipeline . getBindGroupLayout ( 0 ) ,
128
113
entries : entries ,
0 commit comments