-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBinding.ts
96 lines (86 loc) · 3.74 KB
/
Binding.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { toCamelCase } from '../../utils/utils'
import { MaterialShadersType } from '../../types/Materials'
import { TextureBinding } from './TextureBinding'
import { SamplerBinding } from './SamplerBinding'
/** Defines all kind of texture binding types */
export type TextureBindingType = 'texture' | 'externalTexture' | 'storage' | 'depth'
/** Defines all kind of binding types */
export type BindingType = 'uniform' | 'storage' | TextureBindingType | 'sampler'
// see https://www.w3.org/TR/WGSL/#memory-access-mode
/** Defines buffer binding memory access types (read only or read/write) */
export type BufferBindingMemoryAccessType = 'read' | 'read_write'
/** Defines texture binding memory access types (read only, write only or read/write) */
export type BindingMemoryAccessType = BufferBindingMemoryAccessType | 'write'
/**
* Defines all kind of {@link Binding} that are related to textures or samplers
*/
export type TextureSamplerBindings = TextureBinding | SamplerBinding
/**
* An object defining all possible {@link Binding} class instancing parameters
*/
export interface BindingParams {
/** {@link Binding} label */
label?: string
/** {@link Binding} name/key */
name?: string
/** {@link BindingType | binding type} to use with this {@link Binding} */
bindingType?: BindingType
/** {@link Binding} variables shaders visibility */
visibility?: MaterialShadersType | null
}
/**
* Used as a shell to build actual bindings upon, like {@link core/bindings/BufferBinding.BufferBinding | BufferBinding}, {@link core/bindings/WritableBufferBinding.WritableBufferBinding | WritableBufferBinding}, {@link TextureBinding} and {@link SamplerBinding}.
*
* Ultimately the goal of a {@link Binding} element is to provide correct resources for {@link GPUBindGroupLayoutEntry} and {@link GPUBindGroupEntry}
*
* ## WGSL
*
* Each {@link Binding} creates its own WGSL code snippet variable declaration, using structured types or not.
*/
export class Binding {
/** The label of the {@link Binding} */
label: string
/** The name/key of the {@link Binding} */
name: string
/** The binding type of the {@link Binding} */
bindingType: BindingType
/** The visibility of the {@link Binding} in the shaders */
visibility: GPUShaderStageFlags
/** Options used to create this {@link Binding} */
options: BindingParams
/** Flag indicating whether we should recreate the parentMesh {@link core/bindGroups/BindGroup.BindGroup#bindGroup | bind group}, usually when a resource has changed */
shouldResetBindGroup: boolean
/** Flag indicating whether we should recreate the parentMesh {@link core/bindGroups/BindGroup.BindGroup#bindGroupLayout | GPU bind group layout}, usually when a resource layout has changed */
shouldResetBindGroupLayout: boolean
/**
* Binding constructor
* @param parameters - {@link BindingParams | parameters} used to create our {@link Binding}
*/
constructor({ label = 'Uniform', name = 'uniform', bindingType = 'uniform', visibility }: BindingParams) {
this.label = label
this.name = toCamelCase(name)
this.bindingType = bindingType
this.visibility = visibility
? (() => {
switch (visibility) {
case 'vertex':
return GPUShaderStage.VERTEX
case 'fragment':
return GPUShaderStage.FRAGMENT
case 'compute':
return GPUShaderStage.COMPUTE
default:
return GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE
}
})()
: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT | GPUShaderStage.COMPUTE
this.options = {
label,
name,
bindingType,
visibility,
}
this.shouldResetBindGroup = false
this.shouldResetBindGroupLayout = false
}
}