Files
cocos4/cocos/2d/renderer/mesh-buffer.ts
T
ChiaNing 24fe3ff784 UI GPU dirven add (#9378)
* 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
2021-09-29 22:32:33 +08:00

248 lines
8.1 KiB
TypeScript

/*
Copyright (c) 2019-2020 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @packageDocumentation
* @module ui
*/
import { BufferUsageBit, MemoryUsageBit, InputAssemblerInfo, Attribute, Buffer, BufferInfo, InputAssembler } from '../../core/gfx';
import { IBatcher } from './i-batcher';
import { getComponentPerVertex } from './vertex-format';
export class MeshBuffer {
public static OPACITY_OFFSET = 8;
get attributes () { return this._attributes; }
get vertexBuffers () { return this._vertexBuffers; }
get indexBuffer () { return this._indexBuffer; }
public vData: Float32Array | null = null;
public iData: Uint16Array | null = null;
public byteStart = 0;
public byteOffset = 0;
public indicesStart = 0;
public indicesOffset = 0;
public vertexStart = 0;
public vertexOffset = 0;
public lastByteOffset = 1;
private _attributes: Attribute[] = null!;
private _vertexBuffers: Buffer[] = [];
private _indexBuffer: Buffer = null!;
private _iaInfo: InputAssemblerInfo = null!;
// NOTE:
// actually 256 * 4 * (vertexFormat._bytes / 4)
// include pos, uv, color in ui attributes
private _batcher: IBatcher;
private _dirty = false;
private _vertexFormatBytes = 0;
private _initVDataCount = 0;
private _initIDataCount = 256 * 6;
private _outOfCallback: ((...args: number[]) => void) | null = null;
private _hInputAssemblers: InputAssembler[] = [];
private _nextFreeIAHandle = 0;
constructor (batcher: IBatcher) {
this._batcher = batcher;
}
get vertexFormatBytes (): number {
return this._vertexFormatBytes;
}
public initialize (attrs: Attribute[], outOfCallback: ((...args: number[]) => void) | null) {
this._outOfCallback = outOfCallback;
const formatBytes = getComponentPerVertex(attrs);
this._vertexFormatBytes = formatBytes * Float32Array.BYTES_PER_ELEMENT;
this._initVDataCount = 256 * this._vertexFormatBytes;
const vbStride = Float32Array.BYTES_PER_ELEMENT * formatBytes;
if (!this.vertexBuffers.length) {
this.vertexBuffers.push(this._batcher.device.createBuffer(new BufferInfo(
BufferUsageBit.VERTEX | BufferUsageBit.TRANSFER_DST,
MemoryUsageBit.HOST | MemoryUsageBit.DEVICE,
vbStride,
vbStride,
)));
}
const ibStride = Uint16Array.BYTES_PER_ELEMENT;
if (!this.indexBuffer) {
this._indexBuffer = this._batcher.device.createBuffer(new BufferInfo(
BufferUsageBit.INDEX | BufferUsageBit.TRANSFER_DST,
MemoryUsageBit.HOST | MemoryUsageBit.DEVICE,
ibStride,
ibStride,
));
}
this._attributes = attrs;
this._iaInfo = new InputAssemblerInfo(this.attributes, this.vertexBuffers, this.indexBuffer);
// for recycle pool using purpose --
if (!this.vData || !this.iData) {
this._reallocBuffer();
}
// ----------
}
public request (vertexCount = 4, indicesCount = 6) {
this.lastByteOffset = this.byteOffset;
const byteOffset = this.byteOffset + vertexCount * this._vertexFormatBytes;
const indicesOffset = this.indicesOffset + indicesCount;
if (vertexCount + this.vertexOffset > 65535) {
if (this._outOfCallback) {
this._outOfCallback.call(this._batcher, vertexCount, indicesCount);
}
return false;
}
let byteLength = this.vData!.byteLength;
let indicesLength = this.iData!.length;
if (byteOffset > byteLength || indicesOffset > indicesLength) {
while (byteLength < byteOffset || indicesLength < indicesOffset) {
this._initVDataCount *= 2;
this._initIDataCount *= 2;
byteLength = this._initVDataCount * 4;
indicesLength = this._initIDataCount;
}
this._reallocBuffer();
}
this.vertexOffset += vertexCount;
this.indicesOffset += indicesCount;
this.byteOffset = byteOffset;
this._dirty = true;
return true;
}
public reset () {
this.byteStart = 0;
this.byteOffset = 0;
this.indicesStart = 0;
this.indicesOffset = 0;
this.vertexStart = 0;
this.vertexOffset = 0;
this.lastByteOffset = 0;
this._nextFreeIAHandle = 0;
this._dirty = false;
}
public destroy () {
this._attributes = null!;
this.vertexBuffers[0].destroy();
this.vertexBuffers.length = 0;
this.indexBuffer.destroy();
this._indexBuffer = null!;
for (let i = 0; i < this._hInputAssemblers.length; i++) {
this._hInputAssemblers[i].destroy();
}
this._hInputAssemblers.length = 0;
}
public recordBatch (): InputAssembler | null {
const vCount = this.indicesOffset - this.indicesStart;
if (!vCount) {
return null;
}
if (this._hInputAssemblers.length <= this._nextFreeIAHandle) {
this._hInputAssemblers.push(this._batcher.device.createInputAssembler(this._iaInfo));
}
const ia = this._hInputAssemblers[this._nextFreeIAHandle++];
ia.firstIndex = this.indicesStart;
ia.indexCount = vCount;
return ia;
}
public uploadBuffers () {
if (this.byteOffset === 0 || !this._dirty) {
return;
}
const verticesData = new Float32Array(this.vData!.buffer, 0, this.byteOffset >> 2);
const indicesData = new Uint16Array(this.iData!.buffer, 0, this.indicesOffset);
if (this.byteOffset > this.vertexBuffers[0].size) {
this.vertexBuffers[0].resize(this.byteOffset);
}
this.vertexBuffers[0].update(verticesData);
if (this.indicesOffset * 2 > this.indexBuffer.size) {
this.indexBuffer.resize(this.indicesOffset * 2);
}
this.indexBuffer.update(indicesData);
this._dirty = false;
}
private _reallocBuffer () {
this._reallocVData(true);
this._reallocIData(true);
}
private _reallocVData (copyOldData: boolean) {
let oldVData;
if (this.vData) {
oldVData = new Uint8Array(this.vData.buffer);
}
this.vData = new Float32Array(this._initVDataCount);
if (oldVData && copyOldData) {
const newData = new Uint8Array(this.vData.buffer);
for (let i = 0, l = oldVData.length; i < l; i++) {
newData[i] = oldVData[i];
}
}
}
private _reallocIData (copyOldData: boolean) {
const oldIData = this.iData;
this.iData = new Uint16Array(this._initIDataCount);
if (oldIData && copyOldData) {
const iData = this.iData;
for (let i = 0, l = oldIData.length; i < l; i++) {
iData[i] = oldIData[i];
}
}
}
}