Skip to content

Commit

Permalink
BufferAccessor refactor for Batcher2D (#9802)
Browse files Browse the repository at this point in the history
* 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 0600e26.

* 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>
  • Loading branch information
pandamicro and LinYunMo committed Jan 7, 2022
1 parent 626f55f commit bd3c30f
Show file tree
Hide file tree
Showing 58 changed files with 2,486 additions and 2,280 deletions.
13 changes: 13 additions & 0 deletions EngineErrorMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ Invalid index in MultiplexLayer switchTo message
cc.Layer.addLayer(): layer should be non-null

### 2104

Layer collision. The name of layer (%s) is collided with the name or value of some layer

### 2200
Expand Down Expand Up @@ -3059,6 +3060,18 @@ Stencil manager does not support level bigger than %d in this device.

Stencil manager is already empty, cannot pop any mask

### 9002

Failed to request any buffer from a mesh buffer without accessor

### 9003

The internal state of LinearBufferAccessor have severe issue and irreversible, please check the reason

### 9004

Failed to allocate chunk in StaticVBAccessor, the requested buffer might be too large: %d bytes

### 9100

texture size exceeds current device limits %d/%d
Expand Down
8 changes: 4 additions & 4 deletions cocos/2d/assembler/graphics/webgl/graphics-assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,14 @@ export const graphicsAssembler: IAssembler = {
}

// stroke indices
let indicesOffset = meshBuffer.indicesStart;
let indicesOffset = meshBuffer.indexStart;
for (let begin = offset + 2, over = meshBuffer.vertexStart; begin < over; begin++) {
iData[indicesOffset++] = begin - 2;
iData[indicesOffset++] = begin - 1;
iData[indicesOffset++] = begin;
}

meshBuffer.indicesStart = indicesOffset;
meshBuffer.indexStart = indicesOffset;
}
_renderData = null;
_impl = null;
Expand Down Expand Up @@ -342,7 +342,7 @@ export const graphicsAssembler: IAssembler = {
this._vSet!(pts[j].x, pts[j].y);
}

let indicesOffset = renderData.indicesStart;
let indicesOffset = renderData.indexStart;

if (path.complex) {
const earcutData: number[] = [];
Expand Down Expand Up @@ -371,7 +371,7 @@ export const graphicsAssembler: IAssembler = {
}
}

meshBuffer.indicesStart = indicesOffset;
meshBuffer.indexStart = indicesOffset;
}

_renderData = null;
Expand Down
8 changes: 6 additions & 2 deletions cocos/2d/assembler/label/bmfont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const bmfont: IAssembler = {
const node = comp.node;
tempColor.set(comp.color);
tempColor.a = node._uiProps.opacity * 255;
// Fill All
fillMeshVertices3D(node, renderer, comp.renderData!, tempColor);
},

Expand All @@ -64,8 +65,7 @@ export const bmfont: IAssembler = {
const dataOffset = renderData.dataLength;

renderData.dataLength += 4;
renderData.vertexCount = renderData.dataLength;
renderData.indicesCount = renderData.dataLength / 2 * 3;
renderData.resize(renderData.dataLength, renderData.dataLength / 2 * 3);

const dataList = renderData.data;
const texW = spriteFrame.width;
Expand Down Expand Up @@ -117,6 +117,10 @@ export const bmfont: IAssembler = {
dataList[dataOffset + 3].x = x + rectWidth * scale;
dataList[dataOffset + 3].y = y;
},

updateColor (comp: Label) {

},
};

js.addon(bmfont, bmfontUtils);
21 changes: 19 additions & 2 deletions cocos/2d/assembler/label/bmfontUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ export const bmfontUtils = {

_comp.actualFontSize = _fontSize;
_uiTrans.setContentSize(_contentSize);
this.updateUVs(comp);

_comp.renderData!.vertDirty = _comp.renderData!.uvDirty = false;
_comp.renderData!.vertDirty = false;
// fix bmfont run updateRenderData twice bug
_comp.markForUpdateRenderData(false);

Expand All @@ -116,6 +117,20 @@ export const bmfontUtils = {
}
},

updateUVs (label: Label) {
const renderData = label.renderData!;
const vData = renderData.chunk.vb;
const vertexCount = renderData.vertexCount;
const dataList = renderData.data;
let vertexOffset = 3;
for (let i = 0; i < vertexCount; i++) {
const vert = dataList[i];
vData[vertexOffset] = vert.u;
vData[vertexOffset + 1] = vert.v;
vertexOffset += 9;
}
},

_updateFontScale () {
_bmfontScale = _fontSize / _originFontSize;
},
Expand All @@ -127,6 +142,7 @@ export const bmfontUtils = {
shareLabelInfo.fontAtlas = fontAsset.fontDefDictionary;

dynamicAtlasManager.packToDynamicAtlas(comp, _spriteFrame);
// TODO update material and uv
},

_updateLabelInfo (comp) {
Expand Down Expand Up @@ -550,7 +566,8 @@ export const bmfontUtils = {

const texture = _spriteFrame ? _spriteFrame.texture : shareLabelInfo.fontAtlas!.getTexture();
const renderData = _comp.renderData!;
renderData.dataLength = renderData.vertexCount = renderData.indicesCount = 0;
renderData.dataLength = 0;
renderData.resize(0, 0);
const anchorPoint = _uiTrans!.anchorPoint;
const contentSize = _contentSize;
const appX = anchorPoint.x * contentSize.width;
Expand Down
1 change: 0 additions & 1 deletion cocos/2d/assembler/label/letter-font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
* @packageDocumentation
* @hidden
*/
import { assetManager } from '../../../core/asset-manager';
import { mixin } from '../../../core/utils/js';
import { Label, LabelOutline } from '../../components';
import { bmfontUtils } from './bmfontUtils';
Expand Down
11 changes: 8 additions & 3 deletions cocos/2d/assembler/label/letter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { bmfont } from './bmfont';
import { letterFont } from './letter-font';
import { Color } from '../../../core/math/color';

const WHITE = new Color(255, 255, 255, 255);
const tempColor = new Color(255, 255, 255, 255);

/**
* letter 组装器
Expand All @@ -53,8 +53,13 @@ export const letter = {
}

const node = comp.node;
WHITE.a = node._uiProps.opacity * 255;
fillMeshVertices3D(node, renderer, comp.renderData, WHITE);
tempColor.a = node._uiProps.opacity * 255;
// Fill All
fillMeshVertices3D(node, renderer, comp.renderData, tempColor);
},

updateColor (comp: Label) {

},

appendQuad: bmfont.appendQuad,
Expand Down
78 changes: 33 additions & 45 deletions cocos/2d/assembler/label/ttf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/

import * as js from '../../../core/utils/js';
import { Color, Vec3 } from '../../../core/math';
import { Color } from '../../../core/math';
import { IBatcher } from '../../renderer/i-batcher';
import { Label } from '../../components/label';
import { IAssembler } from '../../renderer/base';
Expand All @@ -46,11 +46,10 @@ export const ttf: IAssembler = {
createData (comp: Label) {
const renderData = comp.requestRenderData()!;

renderData.dataLength = 4;
renderData.vertexCount = 4;
renderData.indicesCount = 6;
renderData.dataLength = 2;
renderData.resize(4, 6);

const vData = renderData.vData = new Float32Array(4 * 9);
const vData = renderData.chunk.vb;

vData[3] = vData[21] = vData[22] = vData[31] = 0;
vData[4] = vData[12] = vData[13] = vData[30] = 1;
Expand All @@ -63,28 +62,13 @@ export const ttf: IAssembler = {
},

fillBuffers (comp: Label, renderer: IBatcher) {
const renderData = comp.renderData!;
const dataList: IRenderData[] = renderData.data;
const chunk = comp.renderData!.chunk;
const dataList: IRenderData[] = comp.renderData!.data;
const node = comp.node;

let buffer = renderer.acquireBufferBatch()!;
let vertexOffset = buffer.byteOffset >> 2;
let indicesOffset = buffer.indicesOffset;
let vertexId = buffer.vertexOffset;
const isRecreate = buffer.request();
if (!isRecreate) {
buffer = renderer.currBufferBatch!;
indicesOffset = 0;
vertexId = 0;
vertexOffset = 0;
}

// buffer data may be reallocated, need get reference after request.
const vBuf = buffer.vData!;
const iBuf = buffer.iData!;
const vData = renderData.vData!;
const vData = chunk.vb;
const data0 = dataList[0];
const data3 = dataList[3];
const data3 = dataList[1];
/* */
node.updateWorldTransform();
// @ts-expect-error private property access
Expand Down Expand Up @@ -113,23 +97,29 @@ export const ttf: IAssembler = {
vData[27] = cx1 * bx + cx2 * by + x;
vData[28] = cy1 * by + cy2 * bx + y;

vBuf.set(vData, vertexOffset);

// fill index data
iBuf[indicesOffset++] = vertexId;
iBuf[indicesOffset++] = vertexId + 1;
iBuf[indicesOffset++] = vertexId + 2;
iBuf[indicesOffset++] = vertexId + 2;
iBuf[indicesOffset++] = vertexId + 1;
iBuf[indicesOffset++] = vertexId + 3;
// quick version
const bid = chunk.bufferId;
const vid = chunk.vertexOffset;
const meshBuffer = chunk.vertexAccessor.getMeshBuffer(chunk.bufferId);
const ib = chunk.vertexAccessor.getIndexBuffer(bid);
let indexOffset = meshBuffer.indexOffset;
ib[indexOffset++] = vid;
ib[indexOffset++] = vid + 1;
ib[indexOffset++] = vid + 2;
ib[indexOffset++] = vid + 2;
ib[indexOffset++] = vid + 1;
ib[indexOffset++] = vid + 3;
meshBuffer.indexOffset += 6;
// slow version
// const chunk = renderData.chunk;
// renderer.getBufferAccessor().appendIndices(chunk);
},

updateVertexData (comp: Label) {
const renderData = comp.renderData;
if (!renderData) {
return;
}

const uiTrans = comp.node._uiProps.uiTransformComp!;
const width = uiTrans.width;
const height = uiTrans.height;
Expand All @@ -139,21 +129,17 @@ export const ttf: IAssembler = {
const data = renderData.data;
data[0].x = -appX;
data[0].y = -appY;
data[3].x = width - appX;
data[3].y = height - appY;
data[1].x = width - appX;
data[1].y = height - appY;
},

updateUvs (comp: Label) {
updateUVs (comp: Label) {
const renderData = comp.renderData;
if (!renderData) {
if (!renderData || !comp.ttfSpriteFrame) {
return;
}
const vData = renderData.vData!;
if (!vData || !renderData.uvDirty) {
return;
}

const uv = comp.ttfSpriteFrame!.uv;
const vData = renderData.chunk.vb;
const uv = comp.ttfSpriteFrame.uv;
vData[3] = uv[0];
vData[4] = uv[1];
vData[12] = uv[2];
Expand All @@ -162,8 +148,10 @@ export const ttf: IAssembler = {
vData[22] = uv[5];
vData[30] = uv[6];
vData[31] = uv[7];
},

updateColor (comp: Label) {

renderData.uvDirty = false;
},
};

Expand Down
19 changes: 3 additions & 16 deletions cocos/2d/assembler/label/ttfUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,13 @@ export const ttfUtils = {
this._calculateLabelFont();
this._updateLabelDimensions();
this._updateTexture(comp);
this.updateOpacity(comp);
this._calDynamicAtlas(comp);

comp.actualFontSize = _fontSize;
trans.setContentSize(_canvasSize);

this.updateVertexData(comp);
this.updateUvs(comp);
this.updateUVs(comp);

comp.markForUpdateRenderData(false);

Expand All @@ -139,19 +138,7 @@ export const ttfUtils = {
updateVertexData (comp: Label) {
},

updateUvs (comp: Label) {
},

updateOpacity (comp: Label) {
const vData = comp.renderData!.vData;

let colorOffset = 5;
const colorA = comp.node._uiProps.opacity;
for (let i = 0; i < 4; i++) {
vData![colorOffset + 3] = colorA;

colorOffset += 9;
}
updateUVs (comp: Label) {
},

_updateFontFamily (comp: Label) {
Expand Down Expand Up @@ -366,7 +353,7 @@ export const ttfUtils = {
if (comp.cacheMode !== Label.CacheMode.BITMAP) return;
const frame = comp.ttfSpriteFrame!;
dynamicAtlasManager.packToDynamicAtlas(comp, frame);
comp.renderData!.uvDirty = true;
// TODO update material and uv
},

_setupOutline () {
Expand Down
8 changes: 2 additions & 6 deletions cocos/2d/assembler/mask/mask-assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
* @module ui-assembler
*/

import { IRenderData, RenderData } from '../../renderer/render-data';
import { IBatcher } from '../../renderer/i-batcher';
import { Mask, MaskType } from '../../components/mask';
import { IAssembler, IAssemblerManager } from '../../renderer/base';
Expand Down Expand Up @@ -56,11 +55,8 @@ function applyAreaMask (mask: Mask, renderer: IBatcher) {
export const maskAssembler: IAssembler = {
createData (mask: Mask) {
const renderData = mask.requestRenderData();
renderData.dataLength = 4;
renderData.vertexCount = 4;
renderData.indicesCount = 6;

renderData.vData = new Float32Array(4 * 9);
renderData.dataLength = 2;
renderData.resize(4, 6);
return renderData;
},

Expand Down
Loading

0 comments on commit bd3c30f

Please sign in to comment.