Skip to content

Commit

Permalink
[Added] new pipeline setter's light interface
Browse files Browse the repository at this point in the history
  • Loading branch information
GengineJS committed Sep 15, 2023
1 parent 7559379 commit 9fc62b4
Show file tree
Hide file tree
Showing 2 changed files with 274 additions and 30 deletions.
216 changes: 186 additions & 30 deletions cocos/rendering/custom/web-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ export class WebSetter implements Setter {
const block: string = this._currBlock;
const nodeId = this._lg.locateChild(0xFFFFFFFF, this._currStage);
const ppl = this._lg.getLayout(nodeId);
const layout = ppl.descriptorSets.get(UpdateFrequency.PER_PASS)!.descriptorSetLayoutData;
const layout = ppl.descriptorSets.get(this._currFrequency)!.descriptorSetLayoutData;
const nameID: number = this._lg.attributeIndex.get(block)!;
return layout.uniformBlocks.get(nameID);
}

protected _getCurrDescSetLayoutData (): DescriptorSetLayoutData {
const nodeId = this._lg.locateChild(0xFFFFFFFF, this._currStage);
const ppl = this._lg.getLayout(nodeId);
const layout = ppl.descriptorSets.get(UpdateFrequency.PER_PASS)!.descriptorSetLayoutData;
const layout = ppl.descriptorSets.get(this._currFrequency)!.descriptorSetLayoutData;
return layout;
}

Expand All @@ -171,9 +171,10 @@ export class WebSetter implements Setter {
return -1;
}

setCurrConstant (block: string, stage = 'default'): boolean {
setCurrConstant (block: string, stage = 'default', frequency: UpdateFrequency = UpdateFrequency.PER_PASS): boolean {
this._currBlock = block;
this._currStage = stage;
this._currFrequency = frequency;
const nameID: number = this._lg.attributeIndex.get(block)!;
this._currCount = 0;
const currBlock = this._getCurrUniformBlock();
Expand All @@ -189,9 +190,10 @@ export class WebSetter implements Setter {
return this._currConstant;
}

public addConstant (block: string, stage = 'default'): boolean {
public addConstant (block: string, stage = 'default', frequency: UpdateFrequency = UpdateFrequency.PER_PASS): boolean {
this._currBlock = block;
this._currStage = stage;
this._currFrequency = frequency;
const num = this._lg.attributeIndex.get(block)!;
this._currCount = 0;
const currBlock = this._getCurrUniformBlock();
Expand Down Expand Up @@ -269,32 +271,195 @@ export class WebSetter implements Setter {
const num = this._lg.attributeIndex.get(name)!;
this._data.samplers.set(num, sampler);
}

public getParentLayout (): string {
const director = cclegacy.director;
const root = director.root;
const pipeline = root.pipeline as WebPipeline;
const parId = pipeline.renderGraph!.getParent(this._vertID);
const layoutName = pipeline.renderGraph!.getLayout(parId);
return layoutName;
}

public getCurrentLayout (): string {
const director = cclegacy.director;
const root = director.root;
const pipeline = root.pipeline as WebPipeline;
const layoutName = pipeline.renderGraph!.getLayout(this._vertID);
return layoutName;
}

public setBuiltinCameraConstants (camera: Camera): void {
// TODO
const director = cclegacy.director;
const root = director.root;
const pipeline = root.pipeline as WebPipeline;
const layoutName = this.getParentLayout();
setCameraUBOValues(this, camera, pipeline.pipelineSceneData, camera.scene, layoutName);
}
public setBuiltinShadowMapConstants (light: Light, numLevels?: number): void {
// TODO
setShadowUBOView(this, null, this.getParentLayout());
}
public setBuiltinDirectionalLightFrustumConstants (camera: Camera, light: DirectionalLight, csmLevel = 0): void {
setShadowUBOLightView(this, camera, light, csmLevel);
}
public setBuiltinSpotLightFrustumConstants (light: SpotLight): void {
// TODO
setShadowUBOLightView(this, null, light, 0);
}
public setBuiltinDirectionalLightConstants (light: DirectionalLight, camera: Camera): void {
// TODO
}
public setBuiltinSphereLightConstants (light: SphereLight, camera: Camera): void {
// TODO
const director = cclegacy.director;
const pipeline = (director.root as Root).pipeline;
const sceneData = pipeline.pipelineSceneData;
if (!this.addConstant('CCForwardLight', this.getParentLayout(), UpdateFrequency.PER_BATCH)) return;
uniformOffset = this.getUniformOffset('cc_lightPos', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.SPHERE);
this.offsetVec4(_uboVec, uniformOffset);
}
uniformOffset = this.getUniformOffset('cc_lightSizeRangeAngle', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.size, light.range, 0.0, 0.0);
this.offsetVec4(_uboVec, uniformOffset);
}
const isHDR = sceneData.isHDR;
const lightMeterScale = 10000.0;
uniformOffset = this.getUniformOffset('cc_lightColor', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
if (light.useColorTemperature) {
const finalColor = light.finalColor;
_uboVec.x = finalColor.x;
_uboVec.y = finalColor.y;
_uboVec.z = finalColor.z;
}
if (isHDR) {
_uboVec.w = (light).luminance * camera.exposure * lightMeterScale;
} else {
_uboVec.w = (light).luminance;
}
this.offsetVec4(_uboVec, uniformOffset);
}
}
public setBuiltinSpotLightConstants (light: SpotLight, camera: Camera): void {
// TODO
const director = cclegacy.director;
const pipeline = (director.root as Root).pipeline;
const sceneData = pipeline.pipelineSceneData;

const shadowInfo = sceneData.shadows;
if (!this.addConstant('CCForwardLight', this.getParentLayout(), UpdateFrequency.PER_BATCH)) return;
uniformOffset = this.getUniformOffset('cc_lightPos', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.SPOT);
this.offsetVec4(_uboVec, uniformOffset);
}
uniformOffset = this.getUniformOffset('cc_lightSizeRangeAngle', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.size, light.range, light.spotAngle, (shadowInfo.enabled && light.shadowEnabled && shadowInfo.type === ShadowType.ShadowMap) ? 1 : 0);
this.offsetVec4(_uboVec, uniformOffset);
}
uniformOffset = this.getUniformOffset('cc_lightDir', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.direction.x, light.direction.y, light.direction.z, 0);
this.offsetVec4(_uboVec, uniformOffset);
}
const isHDR = sceneData.isHDR;
const lightMeterScale = 10000.0;
uniformOffset = this.getUniformOffset('cc_lightColor', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
if (light.useColorTemperature) {
const finalColor = light.finalColor;
_uboVec.x = finalColor.x;
_uboVec.y = finalColor.y;
_uboVec.z = finalColor.z;
}
if (isHDR) {
_uboVec.w = (light).luminance * camera.exposure * lightMeterScale;
} else {
_uboVec.w = (light).luminance;
}
this.offsetVec4(_uboVec, uniformOffset);
}
}
public setBuiltinPointLightConstants (light: PointLight, camera: Camera): void {
// TODO
const director = cclegacy.director;
const pipeline = (director.root as Root).pipeline;
const sceneData = pipeline.pipelineSceneData;
if (!this.addConstant('CCForwardLight', this.getParentLayout(), UpdateFrequency.PER_BATCH)) return;
uniformOffset = this.getUniformOffset('cc_lightPos', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.POINT);
this.offsetVec4(_uboVec, uniformOffset);
}
uniformOffset = this.getUniformOffset('cc_lightSizeRangeAngle', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(0.0, light.range, 0.0, 0.0);
this.offsetVec4(_uboVec, uniformOffset);
}
const isHDR = sceneData.isHDR;
const lightMeterScale = 10000.0;
uniformOffset = this.getUniformOffset('cc_lightColor', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
if (light.useColorTemperature) {
const finalColor = light.finalColor;
_uboVec.x = finalColor.x;
_uboVec.y = finalColor.y;
_uboVec.z = finalColor.z;
}
if (isHDR) {
_uboVec.w = (light).luminance * camera.exposure * lightMeterScale;
} else {
_uboVec.w = (light).luminance;
}
this.offsetVec4(_uboVec, uniformOffset);
}
}
public setBuiltinRangedDirectionalLightConstants (light: RangedDirectionalLight, camera: Camera): void {
// TODO
const director = cclegacy.director;
const pipeline = (director.root as Root).pipeline;
const sceneData = pipeline.pipelineSceneData;
if (!this.addConstant('CCForwardLight', this.getParentLayout(), UpdateFrequency.PER_BATCH)) return;
uniformOffset = this.getUniformOffset('cc_lightPos', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.position.x, light.position.y, light.position.z, LightType.RANGED_DIRECTIONAL);
this.offsetVec4(_uboVec, uniformOffset);
}
uniformOffset = this.getUniformOffset('cc_lightSizeRangeAngle', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.right.x, light.right.y, light.right.z, 0.0);
this.offsetVec4(_uboVec, uniformOffset);
}
uniformOffset = this.getUniformOffset('cc_lightDir', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.direction.x, light.direction.y, light.direction.z, 0);
this.offsetVec4(_uboVec, uniformOffset);
}
uniformOffset = this.getUniformOffset('cc_lightBoundingSizeVS', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
const scale = light.scale;
_uboVec.set(scale.x * 0.5, scale.y * 0.5, scale.z * 0.5, 0);
this.offsetVec4(_uboVec, uniformOffset);
}
const isHDR = sceneData.isHDR;
uniformOffset = this.getUniformOffset('cc_lightColor', Type.FLOAT4);
if (this.hasUniform(uniformOffset)) {
_uboVec.set(light.color.x, light.color.y, light.color.z, 0);
if (light.useColorTemperature) {
const finalColor = light.finalColor;
_uboVec.x = finalColor.x;
_uboVec.y = finalColor.y;
_uboVec.z = finalColor.z;
}
if (isHDR) {
_uboVec.w = light.illuminance * camera.exposure;
} else {
_uboVec.w = light.illuminance;
}
this.offsetVec4(_uboVec, uniformOffset);
}
}
public hasSampler (name: string): boolean {
const id = this._lg.attributeIndex.get(name);
Expand All @@ -317,15 +482,17 @@ export class WebSetter implements Setter {
// protected
protected _data: RenderData;
protected _lg: LayoutGraphData;
protected _vertID: number = -1;
protected _currBlock;
protected _currStage: string = '';
protected _currFrequency: UpdateFrequency = UpdateFrequency.PER_PASS;
protected _currCount;
protected _currConstant: number[] = [];
}

function setShadowUBOLightView (
setter: WebSetter,
camera: Camera,
camera: Camera | null,
light: Light,
csmLevel: number,
layout = 'default',
Expand All @@ -348,8 +515,8 @@ function setShadowUBOLightView (
if (shadowInfo.enabled) {
if (shadowInfo.type === ShadowType.ShadowMap) {
// update CSM layers
if (light && light.node) {
csmLayers.update(sceneData, camera);
if (light && light.node && light.type === LightType.DIRECTIONAL) {
csmLayers.update(sceneData, camera!);
}
}
}
Expand Down Expand Up @@ -905,7 +1072,7 @@ export class WebSceneBuilder extends WebSetter implements SceneBuilder {
const queueId = this._renderGraph.getParent(this._sceneId);
const passId = this._renderGraph.getParent(queueId);
const layoutName = this._renderGraph.getLayout(passId);
setShadowUBOLightView(this, this._scene.camera!, light, csmLevel, layoutName);
setShadowUBOLightView(this, this._scene.camera, light, csmLevel, layoutName);
}
private readonly _renderGraph: RenderGraph;
private readonly _scene: SceneData;
Expand All @@ -930,16 +1097,10 @@ export class WebRenderQueueBuilder extends WebSetter implements RenderQueueBuild
this._renderGraph.setName(this._vertID, name);
}

getLayoutName (): string {
const parId = this._renderGraph.getParent(this._vertID);
const layoutName = this._renderGraph.getLayout(parId);
return layoutName;
}

addSceneOfCamera (camera: Camera, light: LightInfo, sceneFlags = SceneFlags.NONE, name = 'Camera'): void {
const sceneData = new SceneData(camera.scene, camera, sceneFlags, light);
this._renderGraph.addVertex<RenderGraphValue.Scene>(RenderGraphValue.Scene, sceneData, name, '', new RenderData(), false, this._vertID);
const layoutName = this.getLayoutName();
const layoutName = this.getParentLayout();
const scene: Scene = cclegacy.director.getScene();
setCameraUBOValues(
this,
Expand All @@ -964,7 +1125,7 @@ export class WebRenderQueueBuilder extends WebSetter implements RenderQueueBuild
const renderData = new RenderData();
const sceneId = this._renderGraph.addVertex<RenderGraphValue.Scene>(RenderGraphValue.Scene, sceneData, 'Scene', '', renderData, false, this._vertID);
if (!(sceneFlags & SceneFlags.NON_BUILTIN)) {
const layoutName = this.getLayoutName();
const layoutName = this.getParentLayout();
setCameraUBOValues(
this,
camera,
Expand All @@ -988,7 +1149,7 @@ export class WebRenderQueueBuilder extends WebSetter implements RenderQueueBuild
false,
this._vertID,
);
const layoutName = this.getLayoutName();
const layoutName = this.getParentLayout();
const scene: Scene | null = cclegacy.director.getScene();
setCameraUBOValues(
this,
Expand All @@ -1015,7 +1176,7 @@ export class WebRenderQueueBuilder extends WebSetter implements RenderQueueBuild
false,
this._vertID,
);
const layoutName = this.getLayoutName();
const layoutName = this.getParentLayout();
const scene: Scene = cclegacy.director.getScene();
setCameraUBOValues(
this,
Expand Down Expand Up @@ -1050,7 +1211,6 @@ export class WebRenderQueueBuilder extends WebSetter implements RenderQueueBuild
throw new Error('Method not implemented.');
}
private _renderGraph: RenderGraph;
private _vertID: number;
private _queue: RenderQueue;
private _pipeline: PipelineSceneData;
}
Expand Down Expand Up @@ -1124,7 +1284,6 @@ export class WebRenderSubpassBuilder extends WebSetter implements RenderSubpassB
}

private readonly _renderGraph: RenderGraph;
private readonly _vertID: number;
private readonly _layoutID: number;
private readonly _subpass: RasterSubpass;
private readonly _pipeline: PipelineSceneData;
Expand Down Expand Up @@ -1311,7 +1470,6 @@ export class WebRenderPassBuilder extends WebSetter implements BasicMultisampleR
this._pass.showStatistics = enable;
}
private readonly _renderGraph: RenderGraph;
private readonly _vertID: number;
private readonly _layoutID: number;
private readonly _pass: RasterPass;
private readonly _pipeline: PipelineSceneData;
Expand Down Expand Up @@ -1359,7 +1517,6 @@ export class WebComputeQueueBuilder extends WebSetter implements ComputeQueueBui
);
}
private readonly _renderGraph: RenderGraph;
private readonly _vertID: number;
private readonly _queue: RenderQueue;
private readonly _pipeline: PipelineSceneData;
}
Expand Down Expand Up @@ -1433,7 +1590,6 @@ export class WebComputePassBuilder extends WebSetter implements ComputePassBuild
private readonly _renderGraph: RenderGraph;
private readonly _layoutGraph: LayoutGraphData;
private readonly _resourceGraph: ResourceGraph;
private readonly _vertID: number;
private readonly _layoutID: number;
private readonly _pass: ComputePass;
private readonly _pipeline: PipelineSceneData;
Expand Down
Loading

0 comments on commit 9fc62b4

Please sign in to comment.