mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 08:42:43 +08:00
24fe3ff784
* uniform * uniform buffer add * add dummy IA * first working version * misc & data structure optimize * fix webGL1 bug * rebase on 3.3 * add maxQuadPerDrawcall by device 现在每个DC能绘制的数量由设备返回的 uniform 数量上限决定 * add index * index bug fix * sliced ia add(Not finish) * change mat4 to vec4 * change stride num * new vec4PreUI finish * misc fix * size & anchor dirty add * bug fix * add UICapacityDirty (cherry picked from commit 417e10c8d3422363d80828539b110b93ee4d3cb1) * static buffer cache version (cherry picked from commit 1ffe7e7943cdf9af085aeda5ca218c3442fa9119) * static buffer update version (cherry picked from commit 964d9cb5b3230963b2a535acac99a34cde7eb63c) * cache localbuffer (cherry picked from commit 641b956def64023e0d47be66bc0c0a37ef1e9791) * fix setPosition event & fix DS release * weak sliced & filp tiled mode add * fix tiled y * fix sliced bug * ugly but work filled type add * update effect * filled mode effect * shader fix * RenderData dirty for render data update * revert fillbuffer * misc fix * support label bmfont by meshBuffer * fix sliced bug & commit Model * fixed some render dirty flag bug * cut code by macro.UI_GPU_DRIVEN * cut code by UI_GPU_DRIVEN Macro on build * cut file by UI_GPU_DRIVEN * I-batcher interface add * remove render queue cache * file name change * file name change * after merge * fix for v3.4 * useless remove * fix texture rotation * fix sliced when atlas rotation * useless code remove * native bind * update effect * fix DynamicOffsets bind bug * PR review fix * effect encapsulation * change chunk * update effect * update shader * remove color in fs * move code * fix UBO buffer bug * fix ubo count to 10 * fix vk bind
247 lines
8.2 KiB
TypeScript
247 lines
8.2 KiB
TypeScript
import { Color, murmurhash2_32_gc, Vec2, Vec3, Vec4 } from '../../core';
|
|
import { Buffer } from '../../core/gfx';
|
|
import { BufferInfo, BufferUsageBit, BufferViewInfo, MemoryUsageBit } from '../../core/gfx/base/define';
|
|
import { Device } from '../../core/gfx/base/device';
|
|
|
|
export class UILocalBuffer {
|
|
private static UBO_COUNT = 10;
|
|
|
|
private _device: Device;
|
|
private _vec4PerUI: number;
|
|
private _UIPerUBO: number;
|
|
private _uniformBufferStride: number;
|
|
|
|
private _uniformBufferElementCount: number;
|
|
private _uniformBuffer: Buffer;
|
|
private _firstUniformBufferView: Buffer;
|
|
private _uniformBufferData: Float32Array;
|
|
|
|
// index = instanceID + uboIndex * _UIPerUBO
|
|
private _prevUBOIndex = 0;
|
|
private _prevInstanceID = -1;
|
|
|
|
public hash: number;
|
|
public poolIndex: number;
|
|
|
|
get prevUBOIndex () {
|
|
return this._prevUBOIndex;
|
|
}
|
|
|
|
get uniformBufferStride () {
|
|
return this._uniformBufferStride;
|
|
}
|
|
|
|
get prevInstanceID () {
|
|
return this._prevInstanceID;
|
|
}
|
|
|
|
constructor (device: Device, hash: number, UIPerUBO: number, vec4PerUI: number, poolIndex: number) {
|
|
this._vec4PerUI = vec4PerUI;
|
|
this._UIPerUBO = UIPerUBO;
|
|
this._device = device;
|
|
this.hash = hash;
|
|
this.poolIndex = poolIndex;
|
|
|
|
const alignment = this._device.capabilities.uboOffsetAlignment;
|
|
const unalignedStride = Float32Array.BYTES_PER_ELEMENT * 4 * vec4PerUI * UIPerUBO;
|
|
this._uniformBufferStride = Math.ceil(unalignedStride / alignment) * alignment; // memory alignment
|
|
this._uniformBufferElementCount = this._uniformBufferStride / Float32Array.BYTES_PER_ELEMENT;
|
|
|
|
this._uniformBuffer = this._device.createBuffer(new BufferInfo(
|
|
BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST,
|
|
MemoryUsageBit.DEVICE,
|
|
this._uniformBufferStride * UILocalBuffer.UBO_COUNT,
|
|
this._uniformBufferStride,
|
|
));
|
|
|
|
this._firstUniformBufferView = this._device.createBuffer(new BufferViewInfo(this._uniformBuffer, 0, unalignedStride));
|
|
|
|
this._uniformBufferData = new Float32Array(this._uniformBufferElementCount * UILocalBuffer.UBO_COUNT);
|
|
}
|
|
|
|
checkFull () {
|
|
return this._prevUBOIndex >= UILocalBuffer.UBO_COUNT - 1 && this._prevInstanceID + 1 >= this._UIPerUBO;
|
|
}
|
|
|
|
updateIndex () {
|
|
if (this._prevInstanceID + 1 >= this._UIPerUBO) {
|
|
++this._prevUBOIndex;
|
|
this._prevInstanceID = 0;
|
|
} else {
|
|
++this._prevInstanceID;
|
|
}
|
|
}
|
|
|
|
destroy () {
|
|
}
|
|
|
|
upload (t: Vec3, r: Quat, s: Vec3, to: number[], c: Color, mode: number, uvExtra: Vec4, fillType: number) {
|
|
this.updateIndex();
|
|
const data = this._uniformBufferData;
|
|
let offset = this._prevInstanceID * this._vec4PerUI * 4 + this._uniformBufferElementCount * this._prevUBOIndex;
|
|
data[offset + 0] = t.x;
|
|
data[offset + 1] = t.y;
|
|
data[offset + 2] = t.z;
|
|
data[offset + 3] = c.r + Math.min(c.y, 0.999);
|
|
// rotation
|
|
offset += 4;
|
|
data[offset + 0] = r.x;
|
|
data[offset + 1] = r.y;
|
|
data[offset + 2] = r.z;
|
|
data[offset + 3] = r.w;
|
|
// scale & BA & Mode.progress
|
|
offset += 4;
|
|
data[offset + 0] = s.x;
|
|
data[offset + 1] = s.y;
|
|
data[offset + 2] = c.b + Math.min(c.w, 0.999);
|
|
data[offset + 3] = mode + Math.min(fillType, 0.999);
|
|
// tilling offset
|
|
offset += 4;
|
|
data[offset + 0] = to[0];
|
|
data[offset + 1] = to[1];
|
|
data[offset + 2] = to[2];
|
|
data[offset + 3] = to[3];
|
|
offset += 4;
|
|
// uvExtra
|
|
if (mode > 0) {
|
|
data[offset + 0] = uvExtra.x;
|
|
data[offset + 1] = uvExtra.y;
|
|
data[offset + 2] = uvExtra.z;
|
|
data[offset + 3] = uvExtra.w;
|
|
}
|
|
}
|
|
|
|
getBufferView () {
|
|
return this._firstUniformBufferView;
|
|
}
|
|
|
|
updateBuffer () {
|
|
this._uniformBuffer.update(this._uniformBufferData);
|
|
}
|
|
|
|
reset () {
|
|
this._prevUBOIndex = 0;
|
|
this._prevInstanceID = -1;
|
|
}
|
|
|
|
updateDataTRSByDirty (instanceID: number, UBOIndex: number, t: Vec3, r?: Quat, s?: Vec3) {
|
|
let offset = instanceID * this._vec4PerUI * 4 + this._uniformBufferElementCount * UBOIndex;
|
|
const data = this._uniformBufferData;
|
|
data[offset + 0] = t.x;
|
|
data[offset + 1] = t.y;
|
|
data[offset + 2] = t.z;
|
|
if (r) {
|
|
offset += 4;
|
|
data[offset + 0] = r.x;
|
|
data[offset + 1] = r.y;
|
|
data[offset + 2] = r.z;
|
|
data[offset + 3] = r.w;
|
|
}
|
|
if (s) {
|
|
offset += 4;
|
|
data[offset + 0] = s.x;
|
|
data[offset + 1] = s.y;
|
|
}
|
|
}
|
|
|
|
updateDataByDirty (instanceID: number, UBOIndex: number, c: Color, mode: number, to: number[], uvExtra: Vec4, fillType: number) {
|
|
let offset = instanceID * this._vec4PerUI * 4 + this._uniformBufferElementCount * UBOIndex;
|
|
const data = this._uniformBufferData;
|
|
data[offset + 3] = c.r + Math.min(c.y, 0.999);
|
|
offset += 8;
|
|
data[offset + 2] = c.b + Math.min(c.w, 0.999);
|
|
data[offset + 3] = mode + Math.min(fillType, 0.999);
|
|
offset += 4;
|
|
data[offset + 0] = to[0];
|
|
data[offset + 1] = to[1];
|
|
data[offset + 2] = to[2];
|
|
data[offset + 3] = to[3];
|
|
offset += 4;
|
|
if (mode > 0) {
|
|
data[offset + 0] = uvExtra.x;
|
|
data[offset + 1] = uvExtra.y;
|
|
data[offset + 2] = uvExtra.z;
|
|
data[offset + 3] = uvExtra.w;
|
|
}
|
|
}
|
|
}
|
|
|
|
interface ILocalBufferPool {
|
|
pool: UILocalBuffer[];
|
|
currentIdx: number;
|
|
}
|
|
|
|
interface ILocalBuffers {
|
|
key: number;
|
|
value: ILocalBufferPool;
|
|
}
|
|
|
|
export class UILocalUBOManger {
|
|
public _localBuffers: ILocalBuffers[] = []; // UIPerUBO -> buffer[]
|
|
private _device: Device;
|
|
private _element: ILocalBuffers | null = null;
|
|
|
|
constructor (device: Device) {
|
|
this._device = device;
|
|
}
|
|
|
|
// each quad once
|
|
upload (t: Vec3, r: Quat, s: Vec3, to: number[], c: Color, mode: number, UIPerUBO: number, vec4PerUI: number, uvExtra: Vec4, fillType: number) {
|
|
// find/creat UILocalBuffer with UIPerUBO
|
|
for (let i = 0; i < this._localBuffers.length; i++) {
|
|
if (this._localBuffers[i].key === UIPerUBO) {
|
|
this._element = this._localBuffers[i];
|
|
}
|
|
}
|
|
if (this._element === null) {
|
|
this._localBuffers.push({
|
|
key: UIPerUBO,
|
|
value: {
|
|
pool: [this._createLocalBuffer(UIPerUBO, vec4PerUI, 0)],
|
|
currentIdx: 0,
|
|
},
|
|
});
|
|
this._element = this._localBuffers[this._localBuffers.length - 1];
|
|
}
|
|
const localBufferPool = this._element.value;
|
|
while (localBufferPool.pool[localBufferPool.currentIdx].checkFull()) {
|
|
if (++localBufferPool.currentIdx >= localBufferPool.pool.length) {
|
|
// create with hash
|
|
localBufferPool.pool.push(this._createLocalBuffer(UIPerUBO, vec4PerUI, localBufferPool.currentIdx));
|
|
}
|
|
}
|
|
const localBuffer = localBufferPool.pool[localBufferPool.currentIdx];
|
|
localBuffer.upload(t, r, s, to, c, mode, uvExtra, fillType);
|
|
return localBuffer;
|
|
}
|
|
|
|
public updateBuffer () {
|
|
const length = this._localBuffers.length;
|
|
let res;
|
|
for (let i = 0; i < length; ++i) {
|
|
res = this._localBuffers[i].value;
|
|
for (let j = 0; j <= res.currentIdx; ++j) {
|
|
res.pool[j].updateBuffer();
|
|
}
|
|
}
|
|
}
|
|
|
|
public reset () {
|
|
this._element = null;
|
|
const length = this._localBuffers.length;
|
|
let res;
|
|
for (let i = 0; i < length; ++i) {
|
|
res = this._localBuffers[i].value;
|
|
for (let j = 0; j <= res.currentIdx; ++j) {
|
|
res.pool[j].reset();
|
|
}
|
|
res.currentIdx = 0;
|
|
}
|
|
}
|
|
|
|
private _createLocalBuffer (capacity, vec4PerUI, idx) {
|
|
const hash = murmurhash2_32_gc(`UIUBO-${capacity}-${idx}`, 666);
|
|
return new UILocalBuffer(this._device, hash, capacity, vec4PerUI, idx);
|
|
}
|
|
}
|