-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
descriptor-set.ts
138 lines (119 loc) · 4.74 KB
/
descriptor-set.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
/**
* @category gfx
*/
import { GFXBuffer } from './buffer';
import { GFXDescriptorType, GFXObject, GFXObjectType } from './define';
import { GFXDevice } from './device';
import { GFXSampler } from './sampler';
import { GFXTexture } from './texture';
import { GFXDescriptorSetLayout } from './descriptor-set-layout';
export const DESCRIPTOR_BUFFER_TYPE =
GFXDescriptorType.UNIFORM_BUFFER | GFXDescriptorType.DYNAMIC_UNIFORM_BUFFER |
GFXDescriptorType.STORAGE_BUFFER | GFXDescriptorType.DYNAMIC_STORAGE_BUFFER;
export const DESCRIPTOR_SAMPLER_TYPE = GFXDescriptorType.SAMPLER;
export interface IGFXDescriptorSetInfo {
layout: GFXDescriptorSetLayout;
}
/**
* @en GFX descriptor sets.
* @zh GFX 描述符集组。
*/
export abstract class GFXDescriptorSet extends GFXObject {
get layout () {
return this._layout!;
}
protected _device: GFXDevice;
protected _layout: GFXDescriptorSetLayout | null = null;
protected _buffers: GFXBuffer[] = [];
protected _textures: GFXTexture[] = [];
protected _samplers: GFXSampler[] = [];
protected _isDirty = false;
constructor (device: GFXDevice) {
super(GFXObjectType.DESCRIPTOR_SET);
this._device = device;
}
public abstract initialize (info: IGFXDescriptorSetInfo): boolean;
public abstract destroy (): void;
public abstract update (): void;
/**
* @en Bind buffer to the specified descriptor.
* @zh 在指定的描述符位置上绑定缓冲。
* @param binding The target binding.
* @param buffer The buffer to be bound.
*/
public bindBuffer (binding: number, buffer: GFXBuffer, index = 0) {
const info = this._layout!.bindings[binding];
const descriptorIndex = this._layout!.descriptorIndices[binding];
if (info && (info.descriptorType & DESCRIPTOR_BUFFER_TYPE)) {
if (this._buffers[descriptorIndex + index] !== buffer) {
this._buffers[descriptorIndex + index] = buffer;
this._isDirty = true;
}
} else {
console.error('Setting binding is not GFXDescriptorType.UNIFORM_BUFFER.');
}
}
/**
* @en Bind sampler to the specified descriptor.
* @zh 在指定的描述符位置上绑定采样器。
* @param binding The target binding.
* @param sampler The sampler to be bound.
*/
public bindSampler (binding: number, sampler: GFXSampler, index = 0) {
const info = this._layout!.bindings[binding];
const descriptorIndex = this._layout!.descriptorIndices[binding];
if (info && (info.descriptorType & DESCRIPTOR_SAMPLER_TYPE)) {
if (this._samplers[descriptorIndex + index] !== sampler) {
this._samplers[descriptorIndex + index] = sampler;
this._isDirty = true;
}
} else {
console.error('Setting binding is not GFXDescriptorType.SAMPLER.');
}
}
/**
* @en Bind texture to the specified descriptor.
* @zh 在指定的描述符位置上绑定纹理。
* @param binding The target binding.
* @param texture The texture to be bound.
*/
public bindTexture (binding: number, texture: GFXTexture, index = 0) {
const info = this._layout!.bindings[binding];
const descriptorIndex = this._layout!.descriptorIndices[binding];
if (info && (info.descriptorType & DESCRIPTOR_SAMPLER_TYPE)) {
if (this._textures[descriptorIndex + index] !== texture) {
this._textures[descriptorIndex + index] = texture;
this._isDirty = true;
}
} else {
console.error('Setting binding is not GFXDescriptorType.SAMPLER.');
}
}
/**
* @en Get buffer from the specified binding location.
* @zh 获取当前指定绑定位置上的缓冲。
* @param binding The target binding.
*/
public getBuffer (binding: number, index = 0) {
const descriptorIndex = this._layout!.descriptorIndices[binding];
return this._buffers[descriptorIndex + index];
}
/**
* @en Get sampler from the specified binding location.
* @zh 获取当前指定绑定位置上的采样器。
* @param binding The target binding.
*/
public getSampler (binding: number, index = 0) {
const descriptorIndex = this._layout!.descriptorIndices[binding];
return this._samplers[descriptorIndex + index];
}
/**
* @en Get texture from the specified binding location.
* @zh 获取当前指定绑定位置上的贴图。
* @param binding The target binding.
*/
public getTexture (binding: number, index = 0) {
const descriptorIndex = this._layout!.descriptorIndices[binding];
return this._textures[descriptorIndex + index];
}
}