Files
cocos4/cocos/core/gfx/descriptor-set.ts
T
Yun Hsiao Wu 32598c25cb switch to eslint (#7727)
* switch to eslint

* eslint: extends airbnb
2020-11-09 10:19:12 +08:00

147 lines
5.1 KiB
TypeScript

/**
* @packageDocumentation
* @module gfx
*/
import { Buffer } from './buffer';
import { DescriptorSetLayout } from './descriptor-set-layout';
import { Device } from './device';
import { Sampler } from './sampler';
import { Texture } from './texture';
import { DescriptorType, Obj, ObjectType } from './define';
export const DESCRIPTOR_BUFFER_TYPE =
DescriptorType.UNIFORM_BUFFER | DescriptorType.DYNAMIC_UNIFORM_BUFFER |
DescriptorType.STORAGE_BUFFER | DescriptorType.DYNAMIC_STORAGE_BUFFER;
export const DESCRIPTOR_SAMPLER_TYPE = DescriptorType.SAMPLER;
export class DescriptorSetInfo {
declare private _token: never; // to make sure all usages must be an instance of this exact class, not assembled from plain object
constructor (
public layout: DescriptorSetLayout,
) {}
}
/**
* @en GFX descriptor sets.
* @zh GFX 描述符集组。
*/
export abstract class DescriptorSet extends Obj {
get layout () {
return this._layout!;
}
protected _device: Device;
protected _layout: DescriptorSetLayout | null = null;
protected _buffers: Buffer[] = [];
protected _textures: Texture[] = [];
protected _samplers: Sampler[] = [];
protected _isDirty = false;
constructor (device: Device) {
super(ObjectType.DESCRIPTOR_SET);
this._device = device;
}
public abstract initialize (info: DescriptorSetInfo): 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: Buffer, index = 0) {
const bindingIndex = this._layout!.bindingIndices[binding];
const info = this._layout!.bindings[bindingIndex]; if (!info) { return; }
if (info.descriptorType & DESCRIPTOR_BUFFER_TYPE) {
const descriptorIndex = this._layout!.descriptorIndices[binding];
if (this._buffers[descriptorIndex + index] !== buffer) {
this._buffers[descriptorIndex + index] = buffer;
this._isDirty = true;
}
} else {
console.warn('Setting binding is not DescriptorType.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: Sampler, index = 0) {
const bindingIndex = this._layout!.bindingIndices[binding];
const info = this._layout!.bindings[bindingIndex]; if (!info) { return; }
if (info.descriptorType & DESCRIPTOR_SAMPLER_TYPE) {
const descriptorIndex = this._layout!.descriptorIndices[binding];
if (this._samplers[descriptorIndex + index] !== sampler) {
this._samplers[descriptorIndex + index] = sampler;
this._isDirty = true;
}
} else {
console.warn('Setting binding is not DescriptorType.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: Texture, index = 0) {
const bindingIndex = this._layout!.bindingIndices[binding];
const info = this._layout!.bindings[bindingIndex]; if (!info) { return; }
if (info.descriptorType & DESCRIPTOR_SAMPLER_TYPE) {
const descriptorIndex = this._layout!.descriptorIndices[binding];
if (this._textures[descriptorIndex + index] !== texture) {
this._textures[descriptorIndex + index] = texture;
this._isDirty = true;
}
} else {
console.warn('Setting binding is not DescriptorType.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];
}
}