Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gpu driven 3rd #16400

Merged
merged 16 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -125,7 +125,7 @@
* @param firstLevel @en First level to be updated. @zh 更新指定层的 mipmap。
* @param count @en Mipmap level count to be updated。 @zh 指定要更新层的数量。
*/
public updateMipmaps (firstLevel = 0, count?: number): void {

Check failure on line 128 in cocos/asset/assets/simple-texture.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Default parameters should be last

Check failure on line 128 in cocos/asset/assets/simple-texture.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected empty method 'updateMipmaps'

}

Expand Down Expand Up @@ -322,9 +322,9 @@
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 @@ -232,7 +232,7 @@
* @zh 计算从上一帧到现在的时间间隔,结果保存在私有属性中
* @deprecated since v3.3.0 no need to use it anymore
*/
public calculateDeltaTime (now): void {}

Check failure on line 235 in cocos/game/director.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected empty method 'calculateDeltaTime'

/**
* @en End the life of director in the next frame
Expand Down Expand Up @@ -293,7 +293,7 @@
this.purgeDirector();

for (const id in this._persistRootNodes) {
this.removePersistRootNode(this._persistRootNodes[id]);

Check failure on line 296 in cocos/game/director.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `Node`
}

// Clear scene
Expand Down Expand Up @@ -387,9 +387,11 @@
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 @@ -512,8 +514,8 @@
const bundle = assetManager.bundles.find((bundle): boolean => !!bundle.getSceneInfo(sceneName));
if (bundle) {
// NOTE: the similar function signatures but defined as deferent function types.
bundle.preloadScene(sceneName, null, onProgress as (finished: number, total: number, item: any) => void,

Check failure on line 517 in cocos/game/director.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected newline after '('

Check failure on line 517 in cocos/game/director.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected newline between arguments/params

Check failure on line 517 in cocos/game/director.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected newline between arguments/params

Check failure on line 517 in cocos/game/director.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

There should be no line break here
onLoaded as ((err?: Error | null) => void) | null);

Check failure on line 518 in cocos/game/director.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected newline before ')'
} else {
const err = `Can not preload the scene "${sceneName}" because it is not in the build settings.`;
if (onLoaded) {
Expand All @@ -535,8 +537,8 @@
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 Expand Up @@ -665,7 +667,7 @@
* @deprecated since 3.0.0
*/
public getAnimationManager (): any {
return this.getSystem(cclegacy.AnimationManager.ID);

Check failure on line 670 in cocos/game/director.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `string`
}

// Loop management
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
Loading