mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-03 09:32:38 +08:00
fixed graphics data submission problem (#7870)
This commit is contained in:
@@ -139,6 +139,10 @@ export class MeshRenderData extends BaseRenderData {
|
||||
public indicesStart = 0;
|
||||
public byteStart = 0;
|
||||
public byteCount = 0;
|
||||
// only for graphics
|
||||
public lastFilledIndices = 0;
|
||||
public lastFilledVertex = 0;
|
||||
|
||||
private _formatByte:number;
|
||||
|
||||
constructor (vertexFloatCnt = 9) {
|
||||
@@ -221,6 +225,8 @@ export class MeshRenderData extends BaseRenderData {
|
||||
this.vertexStart = 0;
|
||||
this.indicesStart = 0;
|
||||
this.byteStart = 0;
|
||||
this.lastFilledIndices = 0;
|
||||
this.lastFilledVertex = 0;
|
||||
}
|
||||
|
||||
protected _reallocBuffer (vCount, iCount) {
|
||||
|
||||
@@ -107,6 +107,7 @@ export class UI {
|
||||
private _uiMaterials: Map<number, UIMaterial> = new Map<number, UIMaterial>();
|
||||
private _canvasMaterials: Map<number, Map<number, number>> = new Map<number, Map<number, number>>();
|
||||
private _batches: CachedArray<UIDrawBatch>;
|
||||
private _doUploadBuffersCall: Map<any, Function> = new Map();
|
||||
private _uiModelPool: Pool<UIBatchModel> | null = null;
|
||||
private _modelInUse: CachedArray<UIBatchModel>;
|
||||
// batcher
|
||||
@@ -176,7 +177,7 @@ export class UI {
|
||||
}
|
||||
|
||||
if (this._descriptorSetCacheMap) {
|
||||
this._destoryDescriptorSet();
|
||||
this._destroyDescriptorSet();
|
||||
}
|
||||
|
||||
this._meshBuffers.clear();
|
||||
@@ -316,6 +317,14 @@ export class UI {
|
||||
this._screens.sort(this._screenSort);
|
||||
}
|
||||
|
||||
public addUploadBuffersFunc(target: any, func: Function) {
|
||||
this._doUploadBuffersCall.set(target, func);
|
||||
}
|
||||
|
||||
public removeUploadBuffersFunc(target: any) {
|
||||
this._doUploadBuffersCall.delete(target);
|
||||
}
|
||||
|
||||
public update () {
|
||||
const screens = this._screens;
|
||||
for (let i = 0; i < screens.length; ++i) {
|
||||
@@ -390,6 +399,12 @@ export class UI {
|
||||
|
||||
public uploadBuffers () {
|
||||
if (this._batches.length > 0) {
|
||||
const calls = this._doUploadBuffersCall;
|
||||
for (const key of calls.keys()) {
|
||||
const list = calls.get(key);
|
||||
list!.call(key, this);
|
||||
}
|
||||
|
||||
const buffers = this._meshBuffers;
|
||||
for (const i of buffers.keys()) {
|
||||
const list = buffers.get(i);
|
||||
@@ -678,7 +693,7 @@ export class UI {
|
||||
const parentOpacity = this._parentOpacity;
|
||||
this._parentOpacity *= node._uiProps.opacity;
|
||||
|
||||
this._preprocess(node);
|
||||
this._preProcess(node);
|
||||
if (len > 0 && !node._static) {
|
||||
const children = node.children;
|
||||
for (let i = 0; i < children.length; ++i) {
|
||||
@@ -687,13 +702,13 @@ export class UI {
|
||||
}
|
||||
}
|
||||
|
||||
this._postprocess(node);
|
||||
this._postProcess(node);
|
||||
this._parentOpacity = parentOpacity;
|
||||
|
||||
level += 1;
|
||||
}
|
||||
|
||||
private _preprocess (node: Node) {
|
||||
private _preProcess (node: Node) {
|
||||
if (!node._uiProps.uiTransformComp) {
|
||||
return;
|
||||
}
|
||||
@@ -706,7 +721,7 @@ export class UI {
|
||||
}
|
||||
}
|
||||
|
||||
private _postprocess (node: Node) {
|
||||
private _postProcess (node: Node) {
|
||||
const render = node._uiProps.uiComp;
|
||||
if (render && render.enabledInHierarchy) {
|
||||
render.postUpdateAssembler(this);
|
||||
@@ -755,11 +770,11 @@ export class UI {
|
||||
const opacity = (this._parentOpacity *= color);
|
||||
const currMeshBuffer = this.currBufferBatch!;
|
||||
const byteOffset = currMeshBuffer.byteOffset >> 2;
|
||||
const vbuf = currMeshBuffer.vData!;
|
||||
const vBuf = currMeshBuffer.vData!;
|
||||
const lastByteOffset = currMeshBuffer.lastByteOffset >> 2;
|
||||
const stride = currMeshBuffer.vertexFormatBytes / 4;
|
||||
for (let i = lastByteOffset; i < byteOffset; i += stride) {
|
||||
vbuf[i + MeshBuffer.OPACITY_OFFSET] *= opacity;
|
||||
vBuf[i + MeshBuffer.OPACITY_OFFSET] *= opacity;
|
||||
}
|
||||
|
||||
currMeshBuffer.lastByteOffset = currMeshBuffer.byteOffset;
|
||||
@@ -782,7 +797,7 @@ export class UI {
|
||||
}
|
||||
}
|
||||
|
||||
private _destoryDescriptorSet () {
|
||||
private _destroyDescriptorSet () {
|
||||
this._descriptorSetCacheMap.forEach((value, key, map) => {
|
||||
value.forEach((hDescriptorSet) => {
|
||||
DSPool.free(hDescriptorSet);
|
||||
|
||||
@@ -28,12 +28,10 @@
|
||||
* @module ui-assembler
|
||||
*/
|
||||
|
||||
import { Attribute, Format } from '../../../../core/gfx';
|
||||
import { Color, Vec3 } from '../../../../core/math';
|
||||
import { IAssembler } from '../../../../core/renderer/ui/base';
|
||||
import { MeshRenderData } from '../../../../core/renderer/ui/render-data';
|
||||
import { UI } from '../../../../core/renderer/ui/ui';
|
||||
import { vfmtPosColor, getAttributeFormatBytes } from '../../../../core/renderer/ui/ui-vertex-format';
|
||||
import { Graphics } from '../../../components';
|
||||
import { LineCap, LineJoin, PointFlags } from '../types';
|
||||
import { earcut as Earcut } from './earcut';
|
||||
@@ -53,12 +51,6 @@ const atan2 = Math.atan2;
|
||||
|
||||
const attrBytes = 8;
|
||||
|
||||
const attributes = vfmtPosColor.concat([
|
||||
new Attribute('a_dist', Format.R32F),
|
||||
]);
|
||||
|
||||
const formatBytes = getAttributeFormatBytes(attributes);
|
||||
|
||||
let _renderData: MeshRenderData | null = null;
|
||||
let _impl: Impl | null = null;
|
||||
const _curColor = new Color();
|
||||
@@ -117,7 +109,8 @@ export const graphicsAssembler: IAssembler = {
|
||||
|
||||
let meshBuffer = renderData;
|
||||
|
||||
const maxVertexCount = meshBuffer ? meshBuffer.vertexCount + vertexCount : 0;
|
||||
|
||||
const maxVertexCount = meshBuffer ? meshBuffer.vertexStart + vertexCount : 0;
|
||||
if (maxVertexCount > MAX_VERTEX || maxVertexCount * 3 > MAX_INDICES) {
|
||||
++_impl.dataOffset;
|
||||
|
||||
@@ -167,36 +160,6 @@ export const graphicsAssembler: IAssembler = {
|
||||
},
|
||||
|
||||
end (graphics: Graphics) {
|
||||
const impl = graphics.impl;
|
||||
const renderDataList = impl && impl.getRenderData();
|
||||
if (!renderDataList || !graphics.model) {
|
||||
return;
|
||||
}
|
||||
|
||||
const subModelCount = graphics.model.subModels.length;
|
||||
const listLength = renderDataList.length;
|
||||
const delta = listLength - subModelCount;
|
||||
if (delta > 0) {
|
||||
for (let k = subModelCount; k < listLength; k++) {
|
||||
graphics.activeSubModel(k);
|
||||
}
|
||||
}
|
||||
|
||||
const subModelList = graphics.model.subModels;
|
||||
for (let i = 0; i < renderDataList.length; i++){
|
||||
const renderData = renderDataList[i];
|
||||
const ia = subModelList[i].inputAssembler;
|
||||
const vertexFormatBytes = Float32Array.BYTES_PER_ELEMENT * formatBytes;
|
||||
const byteOffset = renderData.vertexStart * vertexFormatBytes;
|
||||
const verticesData = new Float32Array(renderData.vData!.buffer, 0, byteOffset >> 2);
|
||||
ia.vertexCount = renderData.vertexStart;
|
||||
ia.vertexBuffers[0].update(verticesData);
|
||||
|
||||
const indicesData = new Uint16Array(renderData.iData!.buffer, 0, renderData.indicesStart);
|
||||
ia.indexCount = renderData.indicesStart;
|
||||
ia.indexBuffer!.update(indicesData);
|
||||
}
|
||||
|
||||
graphics.markForUpdateRenderData();
|
||||
},
|
||||
|
||||
|
||||
@@ -49,8 +49,7 @@ function applyAreaMask (mask: Mask, renderer: UI) {
|
||||
const mat = mask.graphics!.getMaterialInstance(0)!;
|
||||
renderer.forceMergeBatches(mat, mask.spriteFrame, mask.graphics!);
|
||||
} else {
|
||||
const mat = mask.graphics!.getMaterialInstance(0);
|
||||
renderer.commitModel(mask.graphics!, mask.graphics!.model, mat);
|
||||
mask.graphics!.updateAssembler(renderer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ import { LineCap, LineJoin } from '../assembler/graphics/types';
|
||||
import { Impl } from '../assembler/graphics/webgl/impl';
|
||||
import { RenderingSubMesh } from '../../core';
|
||||
import { Format, PrimitiveMode, Attribute, Device, BufferUsageBit, BufferInfo, MemoryUsageBit } from '../../core/gfx';
|
||||
import { vfmtPosColor, getAttributeStride } from '../../core/renderer/ui/ui-vertex-format';
|
||||
import { vfmtPosColor, getAttributeStride, getAttributeFormatBytes } from '../../core/renderer/ui/ui-vertex-format';
|
||||
import { legacyCC } from '../../core/global-exports';
|
||||
|
||||
const _matInsInfo: IMaterialInstanceInfo = {
|
||||
@@ -54,6 +54,8 @@ const attributes = vfmtPosColor.concat([
|
||||
new Attribute('a_dist', Format.R32F),
|
||||
]);
|
||||
|
||||
const formatBytes = getAttributeFormatBytes(attributes);
|
||||
|
||||
const stride = getAttributeStride(attributes);
|
||||
|
||||
/**
|
||||
@@ -246,6 +248,7 @@ export class Graphics extends UIRenderable {
|
||||
protected _miterLimit = 10;
|
||||
|
||||
protected _isDrawing = false;
|
||||
protected _isNeedUploadData = true;
|
||||
|
||||
constructor (){
|
||||
super();
|
||||
@@ -556,6 +559,7 @@ export class Graphics extends UIRenderable {
|
||||
}
|
||||
|
||||
this._isDrawing = true;
|
||||
this._isNeedUploadData = true;
|
||||
(this._assembler as IAssembler).stroke!(this);
|
||||
this._attachToScene();
|
||||
}
|
||||
@@ -573,6 +577,7 @@ export class Graphics extends UIRenderable {
|
||||
}
|
||||
|
||||
this._isDrawing = true;
|
||||
this._isNeedUploadData = true;
|
||||
(this._assembler as IAssembler).fill!(this);
|
||||
this._attachToScene();
|
||||
}
|
||||
@@ -608,8 +613,8 @@ export class Graphics extends UIRenderable {
|
||||
const indexBuffer = gfxDevice.createBuffer(new BufferInfo(
|
||||
BufferUsageBit.INDEX | BufferUsageBit.TRANSFER_DST,
|
||||
MemoryUsageBit.DEVICE,
|
||||
65535 * 2,
|
||||
2,
|
||||
65535 * Uint16Array.BYTES_PER_ELEMENT * 2,
|
||||
Uint16Array.BYTES_PER_ELEMENT,
|
||||
));
|
||||
|
||||
renderMesh = new RenderingSubMesh([vertexBuffer], attributes, PrimitiveMode.TRIANGLE_LIST, indexBuffer);
|
||||
@@ -619,7 +624,53 @@ export class Graphics extends UIRenderable {
|
||||
}
|
||||
}
|
||||
|
||||
protected _uploadData(render: UI) {
|
||||
const impl = this.impl;
|
||||
const renderDataList = impl && impl.getRenderData();
|
||||
if (!renderDataList || !this.model) {
|
||||
return;
|
||||
}
|
||||
|
||||
const subModelCount = this.model.subModels.length;
|
||||
const listLength = renderDataList.length;
|
||||
const delta = listLength - subModelCount;
|
||||
if (delta > 0) {
|
||||
for (let k = subModelCount; k < listLength; k++) {
|
||||
this.activeSubModel(k);
|
||||
}
|
||||
}
|
||||
|
||||
const subModelList = this.model.subModels;
|
||||
for (let i = 0; i < renderDataList.length; i++) {
|
||||
const renderData = renderDataList[i];
|
||||
const ia = subModelList[i].inputAssembler;
|
||||
const vertexFormatBytes = formatBytes * Float32Array.BYTES_PER_ELEMENT;
|
||||
const offset = renderData.lastFilledVertex * vertexFormatBytes;
|
||||
const byteOffset = renderData.vertexStart * vertexFormatBytes;
|
||||
if (offset === byteOffset) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const verticesData = new Float32Array(renderData.vData!.buffer, offset, (byteOffset - offset) >> 2);
|
||||
ia.vertexBuffers[0].update(verticesData, offset);
|
||||
ia.vertexCount = renderData.vertexStart;
|
||||
const indicesData = new Uint16Array(renderData.iData!.buffer, renderData.lastFilledIndices * Uint16Array.BYTES_PER_ELEMENT, renderData.indicesStart - renderData.lastFilledIndices);
|
||||
ia.indexBuffer!.update(indicesData, renderData.lastFilledIndices * Uint16Array.BYTES_PER_ELEMENT);
|
||||
ia.indexCount = renderData.indicesStart;
|
||||
|
||||
renderData.lastFilledVertex = renderData.vertexStart;
|
||||
renderData.lastFilledIndices = renderData.indicesStart;
|
||||
}
|
||||
|
||||
render.removeUploadBuffersFunc(this);
|
||||
this._isNeedUploadData = false;
|
||||
}
|
||||
|
||||
protected _render (render: UI) {
|
||||
if (this._isNeedUploadData) {
|
||||
render.addUploadBuffersFunc(this, this._uploadData);
|
||||
}
|
||||
|
||||
render.commitModel(this, this.model, this.getMaterialInstance(0));
|
||||
}
|
||||
|
||||
|
||||
@@ -566,8 +566,8 @@ export class Mask extends UIRenderable {
|
||||
const indexBuffer = gfxDevice.createBuffer(new BufferInfo(
|
||||
BufferUsageBit.INDEX | BufferUsageBit.TRANSFER_DST,
|
||||
MemoryUsageBit.DEVICE,
|
||||
6 * 2,
|
||||
2,
|
||||
6 * Uint16Array.BYTES_PER_ELEMENT,
|
||||
Uint16Array.BYTES_PER_ELEMENT,
|
||||
));
|
||||
|
||||
const ib = new Uint16Array([0, 1, 2, 2, 1, 3]);
|
||||
|
||||
Reference in New Issue
Block a user