Skip to content

Commit

Permalink
Meshbuffer static length (cocos#33)
Browse files Browse the repository at this point in the history
* set ib only first time

* Meshbuffer static length
  • Loading branch information
LinYunMo committed Dec 24, 2021
1 parent 2780f5c commit 5f42e77
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 115 deletions.
20 changes: 16 additions & 4 deletions cocos/2d/assembler/sprite/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const vec3_temps: Vec3[] = [];
for (let i = 0; i < 4; i++) {
vec3_temps.push(new Vec3());
}
const QUAD_INDICES = Uint16Array.from([0, 1, 2, 2, 1, 3]);

/**
* simple 组装器
Expand Down Expand Up @@ -172,9 +171,22 @@ export const simple: IAssembler = {
this.vertexDirty = false;
}

// 更新 IB
VBChunk.setIndexBuffer(QUAD_INDICES);

// quick version
// const index0 = VBChunk.vertexOffset; const index1 = VBChunk.vertexOffset + 1;
// const index2 = VBChunk.vertexOffset + 2; const index3 = VBChunk.vertexOffset + 3;
// const meshBuffer = VBChunk.vertexAccessor.getMeshBuffer(VBChunk.bufferId);
// const IB = meshBuffer.iData!;
// let indexOffset = meshBuffer.indexOffset;
// IB[indexOffset++] = index0;
// IB[indexOffset++] = index1;
// IB[indexOffset++] = index2;
// IB[indexOffset++] = index2;
// IB[indexOffset++] = index1;
// IB[indexOffset++] = index3;
// meshBuffer.indexOffset += 6;
// meshBuffer.setDirty();

// slow version
renderer.switchBufferAccessor().appendIndices(VBChunk);
},

Expand Down
2 changes: 2 additions & 0 deletions cocos/2d/components/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,8 @@ export class Sprite extends Renderable2D {
if (this._assembler && this._assembler.createData) {
this._renderData = this._assembler.createData(this);
this.VBChunk = accessor.allocateChunk(this._renderData!.vertexCount, this._renderData!.indicesCount);
const QUAD_INDICES = Uint16Array.from([0, 1, 2, 2, 1, 3]);// todo,this type is only for simple
this.VBChunk!.setIndexBuffer(QUAD_INDICES);
this._renderData!.material = this.getRenderMaterial(0);
this.markForUpdateRenderData();
this._colorDirty = true;
Expand Down
90 changes: 13 additions & 77 deletions cocos/2d/renderer/mesh-buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
* @module ui
*/
import { Device, BufferUsageBit, MemoryUsageBit, Attribute, Buffer, BufferInfo, InputAssembler, InputAssemblerInfo } from '../../core/gfx';
import { ScalableContainer } from '../../core/memop/scalable-container';
import { getComponentPerVertex } from './vertex-format';
import { warnID } from '../../core/platform/debug';
import { macro } from '../../core';

export class MeshBuffer extends ScalableContainer {
public static OPACITY_OFFSET = 8;

export class MeshBuffer {
get attributes () { return this._attributes; }
get vertexFormatBytes () { return this._vertexFormatBytes; }
get vertexBuffers (): Readonly<Buffer[]> { return this._vertexBuffers; }
Expand All @@ -49,9 +47,7 @@ export class MeshBuffer extends ScalableContainer {
private _dirty = false;
private _vertexFormatBytes = 0;
private _initVDataCount = 0;
private _initIDataCount = 256 * 6;
private _lastUsedVDataSize = 0;
private _lastUsedIDataSize = 0;
private _initIDataCount = 0;
private _attributes: Attribute[] = null!;
private _vertexBuffers: Buffer[] = [];
private _indexBuffer: Buffer = null!;
Expand All @@ -61,11 +57,14 @@ export class MeshBuffer extends ScalableContainer {
private _iaInfo: InputAssemblerInfo = null!;
private _nextFreeIAHandle = 0;

private _preVertexUsed = 4; // ib 和 vb 的长度比 // 默认值存疑

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 = 256 * floatCount;
this._initVDataCount = macro.BYTE_LENGTH_PRE_MASHBUFFER * 1024 / Float32Array.BYTES_PER_ELEMENT;
this._initIDataCount = this._initVDataCount * this._preVertexUsed;
this._attributes = attrs;

this._vertexBuffers[0] = device.createBuffer(new BufferInfo(
Expand All @@ -83,7 +82,8 @@ export class MeshBuffer extends ScalableContainer {

// for recycle pool using purpose --
if (!this.vData || !this.iData) {
this._reallocBuffer();
this.vData = new Float32Array(this._initVDataCount);
this.iData = new Uint16Array(this._initIDataCount);
}
// ----------

Expand All @@ -109,13 +109,14 @@ export class MeshBuffer extends ScalableContainer {
}
this._indexBuffer = null!;

this.vData = null;
this.iData = null;

// Destroy InputAssemblers
for (let i = 0; i < this._iaPool.length; ++i) {
this._iaPool[i].destroy();
}
this._iaPool.length = 0;

super.destroy();
}

public setDirty () {
Expand Down Expand Up @@ -152,42 +153,12 @@ export class MeshBuffer extends ScalableContainer {
public ensureCapacity (vertexCount: number, indexCount: number) {
const maxVertex = this.vertexOffset + vertexCount;
const maxIndex = this.indexOffset + indexCount;
if (maxVertex > 65535) {
if (maxVertex > this._initVDataCount || maxIndex > this._initIDataCount) {
return false;
}
const maxByte = maxVertex * this.vertexFormatBytes;
let byteLength = this.vData!.byteLength;
let indexLength = this.iData!.length;
let realloc = false;
while (byteLength < maxByte) {
this._initVDataCount *= 2;
byteLength = this._initVDataCount * 4;
realloc = true;
}
while (indexLength < maxIndex) {
this._initIDataCount *= 2;
indexLength = this._initIDataCount;
realloc = true;
}
if (realloc) {
this._reallocBuffer();
}
return true;
}

public tryShrink () {
if (this._dirty || !this.vData || !this.iData) return;
if (this.vData.byteLength >> 2 > this._lastUsedVDataSize && this.iData.length >> 2 > this._lastUsedIDataSize) {
const vDataCount = Math.max(256 * this._vertexFormatBytes, this._initVDataCount >> 1);
const iDataCount = Math.max(256 * 6, this._initIDataCount >> 1);
if (vDataCount !== this._initVDataCount || iDataCount !== this._initIDataCount) {
this._initIDataCount = iDataCount;
this._initVDataCount = vDataCount;
this._reallocBuffer();
}
}
}

public uploadBuffers () {
if (this.byteOffset === 0 || !this._dirty) {
return;
Expand All @@ -206,41 +177,6 @@ export class MeshBuffer extends ScalableContainer {
this.indexBuffer.resize(this.indexOffset * 2);
}
this.indexBuffer.update(indicesData);
this._lastUsedVDataSize = this.byteOffset;
this._lastUsedIDataSize = this.indexOffset;
this._dirty = false;
}

private _reallocBuffer () {
this._reallocVData(true);
this._reallocIData(true);
}

private _reallocVData (copyOldData: boolean) {
let oldVData;
if (this.vData) {
const byteLength = Math.min(this._initVDataCount, this.vData.length) << 2;
oldVData = new Uint8Array(this.vData.buffer, 0, byteLength);
}

this.vData = new Float32Array(this._initVDataCount);

if (oldVData && copyOldData) {
const newData = new Uint8Array(this.vData.buffer);
newData.set(oldVData);
}
}

private _reallocIData (copyOldData: boolean) {
let oldIData;
if (this.iData && this._initVDataCount < this.iData.length) {
oldIData = new Uint16Array(this.iData.buffer, 0, this._initIDataCount << 1);
}

this.iData = new Uint16Array(this._initIDataCount);

if (oldIData && copyOldData) {
this.iData.set(oldIData);
}
}
}
45 changes: 11 additions & 34 deletions cocos/2d/renderer/static-vb-accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class StaticVBAccessor extends BufferAccessor {
if (!firstEntry || firstEntry.length < buffer.vData!.byteLength) {
buffer.uploadBuffers();
}
// Need destroy empty buffer
}
}

Expand All @@ -131,7 +132,7 @@ export class StaticVBAccessor extends BufferAccessor {
this._indexStart = buf.indexOffset;
}
// Append index buffer
buf.iData.set(vbChunk.ib, buf.indexOffset);
buf.iData!.set(vbChunk.ib, buf.indexOffset);
buf.indexOffset += vbChunk.ib.length;
buf.setDirty();
}
Expand All @@ -149,6 +150,7 @@ export class StaticVBAccessor extends BufferAccessor {
ia.firstIndex = this._indexStart;
ia.indexCount = buf.indexOffset - this._indexStart;
this._indexStart = buf.indexOffset;
this._currBID = -1;
return ia;
}

Expand All @@ -169,18 +171,10 @@ export class StaticVBAccessor extends BufferAccessor {
eid = e;
}
}
// Try to increase capacity
if (!entry) {
entry = this._increaseBufferCapacity(i, vertexCount, indexCount);
if (entry) {
bid = i;
eid = freeList.length - 1;
}
}
}
// Allocation fail
if (!entry) {
const bid = this._allocateBuffer();
bid = this._allocateBuffer();
buf = this._buffers[bid];
if (buf && buf.ensureCapacity(vertexCount, indexCount)) {
eid = 0;
Expand Down Expand Up @@ -275,30 +269,13 @@ export class StaticVBAccessor extends BufferAccessor {
return this._buffers[bid].vData!;
}

private _increaseBufferCapacity (bid: number, vertexCount: number, indexCount: number) {
const buf = this._buffers[bid];
const freeList = this._freeLists[bid];
assertIsTrue(buf && freeList);
const byteLength = buf.vData.byteLength;
buf.vertexOffset = byteLength / buf.vertexFormatBytes;
buf.indexOffset = buf.iData.length;
buf.byteOffset = byteLength;
if (buf.ensureCapacity(vertexCount, indexCount)) {
let entry = freeList[freeList.length - 1];
// Can be merge with last entry
if (entry && (entry.offset + entry.length === byteLength)) {
entry.length += buf.vData.byteLength - byteLength;
}
// Need a new entry
else {
entry = _entryPool.alloc();
entry.offset = byteLength;
entry.length = buf.vData.byteLength - byteLength;
freeList.push(entry);
}
return entry;
}
return null;
public getIndexBuffer (bid: number): Uint16Array {
return this._buffers[bid].iData!;
}

// hack
public getMeshBuffer (bid: number): MeshBuffer {
return this._buffers[bid];
}

private _allocateChunkFromEntry (bid: number, eid: number, entry: IFreeEntry, bytes: number) {
Expand Down
7 changes: 7 additions & 0 deletions cocos/core/platform/macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,13 @@ const macro = {
* @default 20
*/
MAX_LABEL_CANVAS_POOL_SIZE: 20,

/**
* @zh
* 每个 MeshBuffer 占用内存的大小(字节),默认值在默认格式下可容纳 4096 个顶点(4096*9*4/1024)
* @default 144
*/
BYTE_LENGTH_PRE_MASHBUFFER: 144,
};

legacyCC.macro = macro;
Expand Down

0 comments on commit 5f42e77

Please sign in to comment.