Skip to content

Commit

Permalink
Gpu driven 3rd (#16400)
Browse files Browse the repository at this point in the history
GPU Driven 3rd stage
  • Loading branch information
stanleyljl authored Oct 11, 2023
1 parent d8053c9 commit 4053bad
Show file tree
Hide file tree
Showing 63 changed files with 921 additions and 384 deletions.
41 changes: 35 additions & 6 deletions cocos/3d/framework/mesh-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ export class MeshRenderer extends ModelRenderer {
@serializable
protected _mesh: Mesh | null = null;

@serializable
protected _gpuDrivenEnabled: boolean = true;

@serializable
protected _shadowCastingMode = ModelShadowCastingMode.OFF;

Expand Down Expand Up @@ -456,7 +459,7 @@ export class MeshRenderer extends ModelRenderer {
* 注意,设置时,所有形变目标的权重都将归零。
*/
@type(Mesh)
@displayOrder(1)
@displayOrder(0)
get mesh (): Mesh | null {
return this._mesh;
}
Expand All @@ -479,6 +482,21 @@ export class MeshRenderer extends ModelRenderer {
this._updateReceiveDirLight();
}

/**
* @en Whether to enable GPU Driven.
* @zh 是否开启 GPU Driven 。
*/
@editable
@displayOrder(1)
get gpuDrivenEnabled (): boolean {
return this._gpuDrivenEnabled;
}

set gpuDrivenEnabled (val) {
this._gpuDrivenEnabled = val;
this._updateGPUDrivenEnabled();
}

/**
* @en Gets the model in [[RenderScene]].
* @zh 获取渲染场景 [[RenderScene]] 中对应的模型。
Expand Down Expand Up @@ -567,6 +585,7 @@ export class MeshRenderer extends ModelRenderer {
this._updateUseReflectionProbe();
this._updateReceiveDirLight();
this._updateStandardSkin();
this._updateGPUDrivenEnabled();
}

// Redo, Undo, Prefab restore, etc.
Expand All @@ -584,6 +603,7 @@ export class MeshRenderer extends ModelRenderer {
this._updateUseReflectionProbe();
this._updateReceiveDirLight();
this._updateStandardSkin();
this._updateGPUDrivenEnabled();
}

public onEnable (): void {
Expand All @@ -610,6 +630,7 @@ export class MeshRenderer extends ModelRenderer {
this._onUpdateReflectionProbeDataMap();
this._onUpdateLocalReflectionProbeData();
this._updateStandardSkin();
this._updateGPUDrivenEnabled();
this._attachToScene();
}

Expand Down Expand Up @@ -916,6 +937,7 @@ export class MeshRenderer extends ModelRenderer {
this._updateReceiveDirLight();
this._onUpdateReflectionProbeDataMap();
this._onUpdateLocalReflectionProbeData();
this._updateGPUDrivenEnabled();
}
}

Expand Down Expand Up @@ -974,9 +996,7 @@ export class MeshRenderer extends ModelRenderer {
this._detachFromScene();
}

if (this.supportGPUScene()) {
if (this.mesh) renderScene.addGPUMesh(this.mesh);

if (this.supportGPUDriven() && this.mesh!.isInGPUScene()) {
renderScene.addGPUModel(this._model);
} else {
renderScene.addModel(this._model);
Expand All @@ -988,7 +1008,7 @@ export class MeshRenderer extends ModelRenderer {
*/
public _detachFromScene (): void {
if (this._model && this._model.scene) {
if (this.supportGPUScene()) {
if (this.supportGPUDriven() && this.mesh!.isInGPUScene()) {
this._model.scene.removeGPUModel(this._model);
} else {
this._model.scene.removeModel(this._model);
Expand All @@ -999,12 +1019,16 @@ export class MeshRenderer extends ModelRenderer {
/**
* @engineInternal
*/
public supportGPUScene (): boolean {
public supportGPUDriven (): boolean {
const sceneData = cclegacy.director.root.pipeline.pipelineSceneData;
if (!sceneData || !sceneData.isGPUDrivenEnabled()) {
return false;
}

if (!this._gpuDrivenEnabled) {
return false;
}

if (!this._mesh || !this.node) {
return false;
}
Expand Down Expand Up @@ -1168,6 +1192,11 @@ export class MeshRenderer extends ModelRenderer {
}
}

protected _updateGPUDrivenEnabled (): void {
if (!this._model) { return; }
this._model.gpuDrivenEnabled = this._gpuDrivenEnabled;
}

protected onMobilityChanged (): void {
this._updateUseLightProbe();
this._updateReceiveDirLight();
Expand Down
2 changes: 1 addition & 1 deletion cocos/asset/assets/simple-texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ export class SimpleTexture extends TextureBase {
if (this._width === 0 || this._height === 0) { return; }
let flags = TextureFlagBit.NONE;
if (this._mipFilter !== Filter.NONE && canGenerateMipmap(device, this._width, this._height)) {
this._mipmapLevel = getMipLevel(this._width, this._height);
if (!this.isUsingOfflineMipmaps() && !this.isCompressed) {
flags = TextureFlagBit.GEN_MIPMAP;
this._mipmapLevel = getMipLevel(this._width, this._height);
}
}
const textureCreateInfo = this._getGfxTextureCreateInfo({
Expand Down
10 changes: 6 additions & 4 deletions cocos/game/director.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,11 @@ export class Director extends EventTarget {
onBeforeLoadScene();
}

if (scene.renderScene) {
scene.renderScene.activate();
if (scene) {
scene.renderScene?.activate();
scene.globals.activate(scene);
}

this.emit(Director.EVENT_BEFORE_SCENE_LAUNCH, scene);

// Run an Entity Scene
Expand Down Expand Up @@ -535,8 +537,8 @@ export class Director extends EventTarget {
for (let i = 0; i < renderers.length; i++) {
const renderer = renderers[i];
const mesh = renderer.mesh;
if (renderer.supportGPUScene()) {
meshes.push(mesh!);
if (mesh && mesh.supportGPUScene()) {
meshes.push(mesh);
}
}

Expand Down
12 changes: 3 additions & 9 deletions cocos/render-scene/core/render-scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,14 @@ export class RenderScene {
/**
* @engineInternal
*/
public activate () {
public activate (): void {
// Do nothing
}

/**
* @engineInternal
*/
public buildGPUScene (meshes: Mesh[]) {
public buildGPUScene (meshes: Mesh[]): void {
// Only support in native.
}

Expand Down Expand Up @@ -557,13 +558,6 @@ export class RenderScene {
this._models.length = 0;
}

/**
* Add a mesh to GPUScene.
* Only support in native.
* @internal
*/
public addGPUMesh (m: Mesh): void {}

/**
* @en Add a GPU Driven model, all models attached to the render scene will be submitted for rendering.
* @zh 增加一个 GPU Driven 模型,渲染场景上挂载的所有模型都会被提交渲染。
Expand Down
18 changes: 18 additions & 0 deletions cocos/render-scene/scene/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,18 @@ export class Model {
this._reflectionProbeBlendWeight = val;
}

/**
* @en Whether to enable GPU Driven.
* @zh 是否开启 GPU Driven 。
*/
get gpuDrivenEnabled (): boolean {
return this._gpuDrivenEnabled;
}

set gpuDrivenEnabled (val) {
this._gpuDrivenEnabled = val;
}

/**
* @en The type of the model
* @zh 模型类型
Expand Down Expand Up @@ -577,6 +589,12 @@ export class Model {
*/
protected _reflectionProbeType = ReflectionProbeType.NONE;

/**
* @en Whether to enable GPU Driven.
* @zh 是否开启 GPU Driven 。
*/
protected _gpuDrivenEnabled = true;

/**
* @internal
* @en native object
Expand Down
2 changes: 2 additions & 0 deletions cocos/rendering/custom/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1404,8 +1404,10 @@ export interface Pipeline extends BasicPipeline {
addBuiltinGpuCullingPass (
cullingID: number,
camera: Camera,
layoutPath?: string,
hzbName?: string,
light?: Light | null,
level?: number,
bMainPass?: boolean): void;
/**
* @en Add hierarchical z buffer generation pass
Expand Down
5 changes: 4 additions & 1 deletion cocos/rendering/custom/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* ========================= !DO NOT CHANGE THE FOLLOWING SECTION MANUALLY! =========================
*/
/* eslint-disable max-len */
import { ResolveMode, ShaderStageFlagBit, Type, UniformBlock } from '../../gfx';
import { ResolveMode, ShaderStageFlagBit, Type, UniformBlock, AccessFlags, AccessFlagBit } from '../../gfx';
import { Light } from '../../render-scene/scene';
import { OutputArchive, InputArchive } from './archive';
import { saveUniformBlock, loadUniformBlock } from './serialization';
Expand Down Expand Up @@ -475,6 +475,7 @@ export class MovePair {
targetMostDetailedMip = 0,
targetFirstSlice = 0,
targetPlaneSlice = 0,
possibleUsage = AccessFlagBit.NONE,
) {
this.source = source;
this.target = target;
Expand All @@ -483,6 +484,7 @@ export class MovePair {
this.targetMostDetailedMip = targetMostDetailedMip;
this.targetFirstSlice = targetFirstSlice;
this.targetPlaneSlice = targetPlaneSlice;
this.possibleUsage = possibleUsage;
}
source: string;
target: string;
Expand All @@ -491,6 +493,7 @@ export class MovePair {
targetMostDetailedMip: number;
targetFirstSlice: number;
targetPlaneSlice: number;
possibleUsage: AccessFlagBit;
}

export class PipelineStatistics {
Expand Down
6 changes: 0 additions & 6 deletions cocos/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,6 @@ export class Root {
//-----------------------------------------------
// pipeline initialization completed
//-----------------------------------------------
const scene = director.getScene();
if (scene) {
scene.globals.activate();
}

this.onGlobalPipelineStateChanged();
if (!this._batcher && internal.Batcher2D) {
this._batcher = new internal.Batcher2D(this);
if (!this._batcher!.initialize()) {
Expand Down
3 changes: 0 additions & 3 deletions cocos/scene-graph/scene.jsb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ sceneProto._activate = function (active: boolean) {
}
cclegacy.director._nodeActivator.activateNode(this, active);
// The test environment does not currently support the renderer
if (!TEST || EDITOR) {
this._globals.activate(this);
}
};

sceneProto._instantiate = function(): void {};
Expand Down
8 changes: 4 additions & 4 deletions cocos/scene-graph/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ export class Scene extends Node {
/**
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
public _onHierarchyChanged (): void { }

/**
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
public _onPostActivated (active: boolean): void {

}
Expand All @@ -155,10 +157,11 @@ export class Scene extends Node {
* @zh
* 参考 [[Node.updateWorldTransform]]
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
public updateWorldTransform (): void {}

// life-cycle call backs

// eslint-disable-next-line @typescript-eslint/no-empty-function
protected _instantiate (): void { }

/**
Expand Down Expand Up @@ -191,9 +194,6 @@ export class Scene extends Node {
}
cclegacy.director._nodeActivator.activateNode(this, active);
// The test environment does not currently support the renderer
if (!TEST) {
this._globals.activate(this);
}
}
}

Expand Down
10 changes: 2 additions & 8 deletions editor/assets/chunks/builtin/uniforms/cc-light-map.chunk
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
#if CC_USE_GPU_DRIVEN
#pragma rate cc_lightingMap pass
#pragma glBinding(2)
uniform sampler2D cc_lightingMap;
#else
#pragma builtin(local)
layout(set = 2, binding = 11) uniform sampler2D cc_lightingMap;
#endif
#pragma builtin(local)
layout(set = 2, binding = 11) uniform sampler2D cc_lightingMap;
Loading

0 comments on commit 4053bad

Please sign in to comment.