-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
binding-layout.ts
173 lines (157 loc) · 4.35 KB
/
binding-layout.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* @category gfx
*/
import { GFXBuffer } from './buffer';
import { GFXBindingType, GFXObject, GFXObjectType } from './define';
import { GFXDevice } from './device';
import { GFXSampler } from './sampler';
import { GFXTextureView } from './texture-view';
/**
* @zh
* GFX绑定。
*/
export interface IGFXBinding {
binding: number;
type: GFXBindingType;
name: string;
}
/**
* @zh
* GFX绑定布局描述信息。
*/
export interface IGFXBindingLayoutInfo {
bindings: IGFXBinding[];
}
/**
* @zh
* GFX绑定单元。
*/
export class GFXBindingUnit {
public binding: number = 0;
public type: GFXBindingType = GFXBindingType.UNKNOWN;
public name: string = '';
public buffer: GFXBuffer | null = null;
public texView: GFXTextureView | null = null;
public sampler: GFXSampler | null = null;
}
/**
* @zh
* GFX绑定布局。
*/
export abstract class GFXBindingLayout extends GFXObject {
/**
* @zh
* GFX设备。
*/
protected _device: GFXDevice;
/**
* @zh
* 绑定单元数组。
*/
protected _bindingUnits: GFXBindingUnit[] = [];
/**
* @zh
* 脏数据标识。
*/
protected _isDirty = false;
/**
* 构造函数。
* @param device GFX设备。
*/
constructor (device: GFXDevice) {
super(GFXObjectType.BINDING_LAYOUT);
this._device = device;
}
/**
* @zh
* 初始化函数。
* @param info GFX绑定布局描述信息。
*/
public abstract initialize (info: IGFXBindingLayoutInfo): boolean;
/**
* @zh
* 销毁函数。
*/
public abstract destroy ();
/**
* @zh
* 更新。
*/
public abstract update ();
/**
* @zh
* 在指定的binding位置上绑定缓冲。
* @param binding 绑定GFX组件的插槽。
* @param buffer GFX缓冲。
*/
public bindBuffer (binding: number, buffer: GFXBuffer) {
for (const bindingUnit of this._bindingUnits) {
if (bindingUnit.binding === binding) {
if (bindingUnit.type === GFXBindingType.UNIFORM_BUFFER) {
if (bindingUnit.buffer !== buffer) {
bindingUnit.buffer = buffer;
this._isDirty = true;
}
} else {
console.error('Setting binding is not GFXBindingType.UNIFORM_BUFFER.');
}
return;
}
}
}
/**
* @zh
* 在指定的binding位置上绑定采样器。
* @param binding 绑定GFX组件的插槽。
* @param sampler GFX采样器。
*/
public bindSampler (binding: number, sampler: GFXSampler) {
for (const bindingUnit of this._bindingUnits) {
if (bindingUnit.binding === binding) {
if (bindingUnit.type === GFXBindingType.SAMPLER) {
if (bindingUnit.sampler !== sampler) {
bindingUnit.sampler = sampler;
this._isDirty = true;
}
} else {
console.error('Setting binding is not GFXBindingType.SAMPLER.');
}
return;
}
}
}
/**
* @zh
* 在指定的binding位置上绑定纹理视图。
* @param binding 绑定GFX组件的插槽。
* @param texView GFX纹理视图。
*/
public bindTextureView (binding: number, texView: GFXTextureView) {
for (const bindingUnit of this._bindingUnits) {
if (bindingUnit.binding === binding) {
if (bindingUnit.type === GFXBindingType.SAMPLER) {
if (bindingUnit.texView !== texView) {
bindingUnit.texView = texView;
this._isDirty = true;
}
} else {
console.error('Setting binding is not GFXBindingType.SAMPLER.');
}
return;
}
}
}
/**
* @zh
* 得到指定的binding位置上的GFX绑定单元。
* @param binding 绑定GFX组件的插槽。
*/
public getBindingUnit (binding: number): GFXBindingUnit | null {
for (const unit of this._bindingUnits) {
if (unit.binding === binding) {
return unit;
}
}
return null;
}
}