Files
cocos4/cocos/2d/renderer/mesh-buffer.ts
T
pandamicro bd3c30fd4c BufferAccessor refactor for Batcher2D (#9802)
* VB cache add(only simple)

* sliced add

* after rebase

* Extract BufferAccessor from MeshBuffer

* Refactor for linear buffer accessor

* Support simple sprite

* Fix initialization issues

* Fix simple sprite rendering

* Rough adaptation

* tweaks

* Fix LinearBufferAccessor's appendBuffers

* Cleanup mesh buffer codes

* Reorganize ia allocation

* Middleware adaptation & Particle 2d adaptation (#31)

* Middleware adaptation

* particle-2d adaptation

* Initial implementation of static vb accessor

* Complete static vb accessor

* Improve static vb accessor

* Ensure MeshBuffer.byteOffset

* Fix record batch process in static vb accessor

* Fix vb reset

* simple & simple label add (#32)

* Meshbuffer static length (#33)

* set ib only first time

* Meshbuffer static length

* Some name refactor

* remove RenderData.vData

* Improve buffer update logics

* Use appendIndices in fillBuffers

* The quickest version(simple & label can render) (#34)

* Revert "Use appendIndices in fillBuffers"

This reverts commit 0600e26f7eee277c820c3097b1f48f79fcf60890.

* the quickest version

* pass renderScene

* batch

* change batcher update

Co-authored-by: pandamicro <lphuabin@gmail.com>

* Move ia record logic from accessor to batcher

* Make vertexFormat sourced from render data

* sliced & bar-filled (#35)

* radial-filled & tiled & label (#36)

* radial-filled & tiled

* bmfont & char label

* function change

* name change

* Remove uvDirty

fixes

Fix uv update for sprite

* Reduce mesh buffer setDirty

* uv fix

* Format label

* Refactor color and cascade opacity mechanism

* Fix empty render data or chunk absent issue

* fix some uv (#37)

* Fix non stop allocation issue in static vb accessor

* fix bmfont richText & remove uiComponent (#38)

* fix bmfont uv

* fix richText & remove ui Component

* Fix mesh render data batching

* Small fix

* Update for spine

* Formatting jsb spine and editor

* middle ware & particle 2d (#39)

* motion-streak

* dragon Bones add

* tiled map unsafe change

* particle-2d add

* function name change

* some function change (#40)

* Fix RenderData pool usage

* Fix merge issue

* fix misc bug (#41)

* Native adaptation for spine and dragonbones with MeshRenderData

* misc fix (#42)

* Improvements

* Fix ci and improvements

Co-authored-by: ChiaNing <sun.cloud.mo@gmail.com>
2022-01-07 15:43:04 +08:00

181 lines
6.3 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 { Device, BufferUsageBit, MemoryUsageBit, Attribute, Buffer, BufferInfo, InputAssembler, InputAssemblerInfo } from '../../core/gfx';
import { getComponentPerVertex } from './vertex-format';
import { warnID } from '../../core/platform/debug';
import { macro } from '../../core';
export class MeshBuffer {
public static IB_SCALE = 4; // ib size scale based on vertex count
get attributes () { return this._attributes; }
get vertexFormatBytes () { return this._vertexFormatBytes; }
get vertexBuffers (): Readonly<Buffer[]> { return this._vertexBuffers; }
get indexBuffer () { return this._indexBuffer; }
public byteOffset = 0;
public vertexOffset = 0;
public indexOffset = 0;
public vData: Float32Array = null!;
public iData: Uint16Array = null!;
private _dirty = false;
private _vertexFormatBytes = 0;
private _initVDataCount = 0;
private _initIDataCount = 0;
private _attributes: Attribute[] = null!;
private _vertexBuffers: Buffer[] = [];
private _indexBuffer: Buffer = null!;
// InputAssembler pools for each mesh buffer, array offset correspondent
private _iaPool: InputAssembler[] = [];
private _iaInfo: InputAssemblerInfo = null!;
private _nextFreeIAHandle = 0;
public initialize (device: Device, attrs: Attribute[]) {
const floatCount = getComponentPerVertex(attrs);
const vbStride = this._vertexFormatBytes = floatCount * Float32Array.BYTES_PER_ELEMENT;
const ibStride = Uint16Array.BYTES_PER_ELEMENT;
this._initVDataCount = macro.SIZE_PER_MESH_BUFFER * 1024 / Float32Array.BYTES_PER_ELEMENT;
this._initIDataCount = this._initVDataCount * MeshBuffer.IB_SCALE;
this._attributes = attrs;
this._vertexBuffers[0] = device.createBuffer(new BufferInfo(
BufferUsageBit.VERTEX | BufferUsageBit.TRANSFER_DST,
MemoryUsageBit.HOST | MemoryUsageBit.DEVICE,
vbStride,
vbStride,
));
this._indexBuffer = device.createBuffer(new BufferInfo(
BufferUsageBit.INDEX | BufferUsageBit.TRANSFER_DST,
MemoryUsageBit.HOST | MemoryUsageBit.DEVICE,
ibStride,
ibStride,
));
// for recycle pool using purpose --
if (!this.vData || !this.iData) {
this.vData = new Float32Array(this._initVDataCount);
this.iData = new Uint16Array(this._initIDataCount);
}
// ----------
this._iaInfo = new InputAssemblerInfo(this._attributes, this._vertexBuffers, this.indexBuffer);
}
public reset () {
this._nextFreeIAHandle = 0;
this._dirty = false;
}
public destroy () {
this.reset();
this._attributes = null!;
this.vData = null!;
this.iData = null!;
if (this._vertexBuffers[0]) {
this._vertexBuffers[0].destroy();
}
this._vertexBuffers.length = 0;
if (this._indexBuffer) {
this._indexBuffer.destroy();
}
this._indexBuffer = null!;
// Destroy InputAssemblers
for (let i = 0; i < this._iaPool.length; ++i) {
this._iaPool[i].destroy();
}
this._iaPool.length = 0;
}
public setDirty () {
this._dirty = true;
}
/**
* @deprecated since v3.4.0, please use BufferAccessor's request
* @see [[BufferAccessor.request]]
*/
public request (vertexCount: number, indexCount: number) {
warnID(9002);
return false;
}
public requireFreeIA (device: Device) {
if (this._iaPool.length <= this._nextFreeIAHandle) {
this._iaPool.push(device.createInputAssembler(this._iaInfo));
}
const ia = this._iaPool[this._nextFreeIAHandle++];
return ia;
}
public recycleIA (ia: InputAssembler) {
const pool = this._iaPool;
const id = pool.indexOf(ia);
if (id >= 0 && id < this._nextFreeIAHandle) {
// Swap to recycle the ia
pool[id] = pool[--this._nextFreeIAHandle];
pool[this._nextFreeIAHandle] = ia;
}
}
public checkCapacity (vertexCount: number, indexCount: number) {
const maxVertex = this.vertexOffset + vertexCount;
const maxIndex = this.indexOffset + indexCount;
if (maxVertex > this._initVDataCount || maxIndex > this._initIDataCount) {
return false;
}
return true;
}
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.indexOffset);
const vertexBuffer = this._vertexBuffers[0];
if (this.byteOffset > vertexBuffer.size) {
vertexBuffer.resize(this.byteOffset);
}
vertexBuffer.update(verticesData);
if (this.indexOffset * 2 > this.indexBuffer.size) {
this.indexBuffer.resize(this.indexOffset * 2);
}
this._indexBuffer.update(indicesData);
this._dirty = false;
}
}