Files
cocos4/cocos/2d/renderer/draw-batch.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

279 lines
8.6 KiB
TypeScript

/*
Copyright (c) 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
* @hidden
*/
import { JSB } from 'internal:constants';
import { MeshBuffer } from './mesh-buffer';
import { Material } from '../../core/assets/material';
import { Texture, Sampler, DrawInfo, Buffer, InputAssembler, DescriptorSet, Shader } from '../../core/gfx';
import { Node } from '../../core/scene-graph';
import { Camera } from '../../core/renderer/scene/camera';
import { RenderScene } from '../../core/renderer/scene/render-scene';
import { Model } from '../../core/renderer/scene/model';
import { Layers } from '../../core/scene-graph/layers';
import { legacyCC } from '../../core/global-exports';
import { Pass } from '../../core/renderer/core/pass';
import { RecyclePool } from '../../core/memop/recycle-pool';
import { NativeDrawBatch2D, NativeDrawCall, NativePass } from '../../core/renderer/scene';
import { IBatcher } from './i-batcher';
const UI_VIS_FLAG = Layers.Enum.NONE | Layers.Enum.UI_3D;
export class DrawCall {
// UBO info
public bufferHash = 0;
public bufferUboIndex = 0;
public _bufferView: Buffer | null = null;
// actual draw call info
private _descriptorSet: DescriptorSet | null = null;
private _dynamicOffsets = [0, 0];// uboindex * _uniformBufferStride
private _drawInfo: DrawInfo| null = null;
private declare _nativeObj: NativeDrawCall | null;
public get native (): NativeDrawCall {
return this._nativeObj!;
}
public get bufferView () {
return this._bufferView;
}
public set bufferView (buffer: Buffer | null) {
this._bufferView = buffer;
if (JSB) {
this._nativeObj!.bufferView = buffer;
}
}
public get descriptorSet () {
return this._descriptorSet;
}
public set descriptorSet (ds: DescriptorSet | null) {
this._descriptorSet = ds;
if (JSB) {
this._nativeObj!.descriptorSet = ds;
}
}
public get drawInfo () {
return this._drawInfo;
}
public get dynamicOffsets () {
return this._dynamicOffsets;
}
public set dynamicOffsets (offsets: number[]) {
this._dynamicOffsets = offsets;
if (JSB) {
this._nativeObj!.dynamicOffsets = this._dynamicOffsets;
}
}
public setDynamicOffsets (value: number) {
this._dynamicOffsets[1] = value;
if (JSB) {
this._nativeObj!.setDynamicOffsets(value);
}
}
constructor () {
if (JSB) {
this._drawInfo = new gfx.DrawInfo();
this._nativeObj = new NativeDrawCall();
this._nativeObj.drawInfo = this.drawInfo;
} else {
this._drawInfo = new DrawInfo();
}
}
}
export class DrawBatch2D {
static drawcallPool = new RecyclePool(() => new DrawCall(), 100);
public get native (): NativeDrawBatch2D {
return this._nativeObj!;
}
public get inputAssembler () {
return this._inputAssember;
}
public set inputAssembler (ia: InputAssembler | null) {
this._inputAssember = ia;
if (JSB) {
this._nativeObj!.inputAssembler = ia;
}
}
public get descriptorSet () {
return this._descriptorSet;
}
public set descriptorSet (ds: DescriptorSet | null) {
this._descriptorSet = ds;
if (JSB) {
this._nativeObj!.descriptorSet = ds;
}
}
public get visFlags () {
return this._visFlags;
}
public set visFlags (vis) {
this._visFlags = vis;
if (JSB) {
this._nativeObj!.visFlags = vis;
}
}
get passes () {
return this._passes;
}
public get shaders () {
return this._shaders;
}
public bufferBatch: MeshBuffer | null = null;
public camera: Camera | null = null;
public renderScene: RenderScene | null = null;
public model: Model | null = null;
public texture: Texture | null = null;
public sampler: Sampler | null = null;
public useLocalData: Node | null = null;
public isStatic = false;
public textureHash = 0;
public samplerHash = 0;
private _passes: Pass[] = [];
private _shaders: Shader[] = [];
private _visFlags: number = UI_VIS_FLAG;
private _inputAssember: InputAssembler | null = null;
private _descriptorSet: DescriptorSet | null = null;
private declare _nativeObj: NativeDrawBatch2D | null;
protected _drawCalls: DrawCall[] = [];
get drawCalls () { return this._drawCalls; }
constructor () {
if (JSB) {
this._nativeObj = new NativeDrawBatch2D();
this._nativeObj.visFlags = this._visFlags;
}
}
public destroy (ui: IBatcher) {
this._passes = [];
if (JSB) {
this._nativeObj = null;
}
}
public clear () {
this.bufferBatch = null;
this.inputAssembler = null;
this.descriptorSet = null;
this.camera = null;
this.texture = null;
this.sampler = null;
this.model = null;
this.isStatic = false;
this.useLocalData = null;
this.visFlags = UI_VIS_FLAG;
this.renderScene = null;
this._drawCalls.length = 0;
if (JSB) { this._nativeObj!.clearDrawCalls(); }
}
protected _pushDrawCall (dc: DrawCall) {
this._drawCalls.push(dc);
if (JSB) { this._nativeObj!.pushDrawCall(dc.native); }
}
// object version
public fillPasses (mat: Material | null, dss, dssHash, bs, bsHash, patches, batcher: IBatcher) {
if (mat) {
const passes = mat.passes;
if (!passes) { return; }
let hashFactor = 0;
let dirty = false;
this._shaders.length = passes.length;
for (let i = 0; i < passes.length; i++) {
if (!this._passes[i]) {
this._passes[i] = new Pass(legacyCC.director.root);
}
const mtlPass = passes[i];
const passInUse = this._passes[i];
mtlPass.update();
// Hack: Cause pass.hash can not check all pass value
if (!dss) { dss = mtlPass.depthStencilState; dssHash = 0; }
if (!bs) { bs = mtlPass.blendState; bsHash = 0; }
if (bsHash === -1) { bsHash = 0; }
hashFactor = (dssHash << 16) | bsHash;
// @ts-expect-error hack for UI use pass object
passInUse._initPassFromTarget(mtlPass, dss, bs, hashFactor);
this._shaders[i] = passInUse.getShaderVariant(patches)!;
dirty = true;
}
if (JSB) {
if (dirty) {
const nativePasses: NativePass[] = [];
const passes = this._passes;
for (let i = 0; i < passes.length; i++) {
nativePasses.push(passes[i].native);
}
this._nativeObj!.passes = nativePasses;
this._nativeObj!.shaders = this._shaders;
}
}
}
}
public fillDrawCallAssembler () {
let dc = this._drawCalls[0];
const ia = this.inputAssembler;
if (!dc) {
dc = DrawBatch2D.drawcallPool.add();
this._pushDrawCall(dc);
}
if (ia) {
dc.drawInfo!.firstIndex = ia.drawInfo.firstIndex;
dc.drawInfo!.indexCount = ia.drawInfo.indexCount;
}
}
}