Skip to content

Commit

Permalink
feat(pipelinePool): add pipeline shader share
Browse files Browse the repository at this point in the history
add pipeline shader share
add occlusion by octree
fix shader state change bug
fix cluster number light count
fix webgpu version
fix shadowmapping bug
fix frag uv and uv2 uvTransform
fix cluster index error
  • Loading branch information
ZenderJK committed Sep 1, 2023
1 parent 6c99aa1 commit c88b687
Show file tree
Hide file tree
Showing 47 changed files with 563 additions and 357 deletions.
4 changes: 2 additions & 2 deletions samples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
menu.innerHTML = list
document.body.appendChild(menu)



// change sessionStorage.target on click, and reload iframe
menu.addEventListener('click', (e: Event) => {
const button = e.target as HTMLElement
Expand Down Expand Up @@ -71,6 +69,8 @@
}
}

// new Sample_PointLight().run();
// new Sample_LoadGLB4().run();

// import { Sample_Grass } from "./ext/Sample_Grass";
// console.log("a");
Expand Down
4 changes: 2 additions & 2 deletions samples/lights/PointLightsScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class PointLightsScript extends ComponentBase {
let poi = obj.addComponent(PointLight);
poi.name = UUID();
poi.transform.x = this._boundBox.center.x + this._boundBox.extents.x * Math.random();
poi.transform.y = this._boundBox.center.y + this._boundBox.extents.y * Math.random();
poi.transform.y = 10;//this._boundBox.center.y + this._boundBox.extents.y * Math.random();
poi.transform.z = this._boundBox.center.z + this._boundBox.extents.z * Math.random();
poi.range = 30
poi.r = Math.random() + 0.1;
Expand Down Expand Up @@ -121,7 +121,7 @@ export class PointLightsScript extends ComponentBase {
Vector3.HELP_0.scaleBy(Time.delta * 0.001 * pd.speed * 0.1);

po.transform.x += Vector3.HELP_0.x;
po.transform.y += Vector3.HELP_0.y;
// po.transform.y += Vector3.HELP_0.y;
po.transform.z += Vector3.HELP_0.z;
}
}
Expand Down
1 change: 1 addition & 0 deletions samples/lights/Sample_CSM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Sample_CSM {
GUIHelp.add(mainCamera, 'enableCSM');
GUIHelp.add(Engine3D.setting.shadow, 'csmScatteringExp', 0.5, 1.0, 0.01);
GUIHelp.add(Engine3D.setting.shadow, 'csmMargin', 0.01, 0.5, 0.01);
// Engine3D.setting.shadow.s
GUIHelp.open();
GUIHelp.endFolder();
Engine3D.startRenderView(view);
Expand Down
1 change: 1 addition & 0 deletions samples/lights/Sample_DirectLightShadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GUIUtil } from "@samples/utils/GUIUtil";
class Sample_DirectLightShadow {
scene: Scene3D;
async run() {
Engine3D.setting.shadow.enable = false;
Engine3D.setting.shadow.autoUpdate = true;
Engine3D.setting.shadow.shadowBound = 400;

Expand Down
5 changes: 4 additions & 1 deletion samples/lights/Sample_PointLight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GUIHelp } from "@orillusion/debug/GUIHelp";
import { AtmosphericComponent, BoxGeometry, CameraUtil, Engine3D, HoverCameraController, LitMaterial, MeshRenderer, Object3D, PointLight, Scene3D, SphereGeometry, View3D, } from "@orillusion/core";
import { PointLightsScript } from "./PointLightsScript";
import { Object3DUtil } from "../../src/util/Object3DUtil";
import { GUIUtil } from "@samples/utils/GUIUtil";

export class Sample_PointLight {
scene: Scene3D;
Expand All @@ -10,7 +11,7 @@ export class Sample_PointLight {
constructor() { }

async run() {

Engine3D.setting.render.debug = true;
await Engine3D.init({});

GUIHelp.init();
Expand All @@ -31,6 +32,8 @@ export class Sample_PointLight {
view.camera = mainCamera;

Engine3D.startRenderViews([view]);

GUIUtil.renderDebug();
}

initScene(scene: Scene3D) {
Expand Down
2 changes: 1 addition & 1 deletion samples/material/script/UVMoveComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class UVMoveComponent extends ComponentBase {
value.y += Time.delta * this._speed.y * 0.001;
value.z = this._speed.z;
value.w = this._speed.w;
this._material.defaultPass.setUniform(`transformUV2`, value);
this._material.defaultPass.setUniform(`transformUV1`, value);
}
}
}
43 changes: 43 additions & 0 deletions samples/utils/GUIUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { UVMoveComponent } from "@samples/material/script/UVMoveComponent";

export class GUIUtil {


public static renderShadowSetting(open: boolean = true) {
GUIHelp.addFolder('ShadowSetting');
let setting = Engine3D.setting.shadow;
Expand Down Expand Up @@ -358,4 +359,46 @@ export class GUIUtil {
GUIHelp.endFolder();
}


static renderDebug() {
// if (Engine3D.setting.render.debug) {
let debugTextures = Engine3D.getRenderJob(Engine3D.views[0]).postRenderer.debugTextures;

let debugTextureObj = { normalRender: -1 };
for (let i = 0; i < debugTextures.length; i++) {
const tex = debugTextures[i];
debugTextureObj[tex.name] = i;
}
GUIHelp.removeFolder(`RenderPerformance`);
//debug
let f = GUIHelp.addFolder('RenderPerformance');
f.open();
GUIHelp.add(Engine3D.setting.render, 'debugQuad', debugTextureObj);
let debugChanel = {
PositionView: 0,
ColorView: 1,
NormalView: 2,
IrradianceView: 3,
LightView: 4,
FinalView: 5,
EmissiveView: 6,
specularRadiance: 7,
AO: 8,
Roughness: 9,
Metallic: 10,
diffuse: 11,
ambient: 12,
meshID: 13,
debugCluster: 14,
debugClusterBox: 15,
debugClusterLightCount: 16,
}
GUIHelp.add(Engine3D.setting.render, 'renderState_left', debugChanel);
GUIHelp.add(Engine3D.setting.render, 'renderState_right', debugChanel);
GUIHelp.add(Engine3D.setting.render, 'renderState_split', 0.0, 2048, 0.001);
GUIHelp.add(Engine3D.setting.render, 'drawOpMin', 0.0, 10000, 1);
GUIHelp.add(Engine3D.setting.render, 'drawOpMax', 0.0, 10000, 1);
GUIHelp.endFolder();
}
// }
}
3 changes: 2 additions & 1 deletion src/Engine3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export class Engine3D {
document.body.appendChild(this.divB);

this.setting = { ...this.setting, ...descriptor.engineSetting }

await WasmMatrix.init(Matrix4.allocCount);

await webGPUContext.init(descriptor.canvasConfig);
Expand Down Expand Up @@ -488,6 +488,7 @@ export class Engine3D {
let i = 0;
for (i = 0; i < views.length; i++) {
const view = views[i];
view.camera.resetPerspective(webGPUContext.aspect);
// view.scene.transform.updateChildTransform()
// view.scene.transform.localChange = true;
}
Expand Down
42 changes: 20 additions & 22 deletions src/assets/shader/cluster/ClusterBoundsSource_cs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ClusterConfig } from "../../../gfx/renderJob/passRenderer/cluster/ClusterConfig";

export let ClusterBoundsSource_cs: string = /* wgsl */`
#include "GlobalUniform"
Expand Down Expand Up @@ -38,10 +40,7 @@ export let ClusterBoundsSource_cs: string = /* wgsl */`
fn ScreenToView(screen : vec4<f32>) -> vec4<f32> {
let texCoord = screen.xy / vec2<f32>(clustersUniform.screenWidth, clustersUniform.screenHeight);
let clip = vec4<f32>(vec2<f32>(texCoord.x , 1.0 - texCoord.y) * 2.0 - 1.0 , screen.z , screen.w);
// (tex.x * 2.0 - 1.0, 1.0 - 2.0 * tex.y, screenPos.z, screenPos.w)
// convertNDCToView(clip);
let clip = vec4<f32>(vec2<f32>(texCoord.x, 1.0 - texCoord.y) * 2.0 - vec2<f32>(1.0, 1.0), screen.z, screen.w);
return convertNDCToView(clip);
}
Expand All @@ -54,8 +53,9 @@ export let ClusterBoundsSource_cs: string = /* wgsl */`
return result;
}
@compute @workgroup_size(8,4,1)
@compute @workgroup_size(${ClusterConfig.clusterTileX},${ClusterConfig.clusterTileY},1)
fn CsMain( @builtin(workgroup_id) workgroup_id : vec3<u32> , @builtin(local_invocation_id) local_invocation_id : vec3<u32> ){
let i = local_invocation_id.x ;
let j = local_invocation_id.y ;
let k = workgroup_id.x ;
Expand All @@ -74,30 +74,28 @@ export let ClusterBoundsSource_cs: string = /* wgsl */`
let near = clustersUniform.near ;
let far = clustersUniform.far ;
let titleSize = vec2<f32>( clustersUniform.screenWidth / tx , clustersUniform.screenHeight / ty ) ;
let titleSize = vec2<f32>( globalUniform.windowWidth / tx , globalUniform.windowHeight / ty ) ;
var minPointSs = vec4<f32>(vec2<f32>(f32(i) , f32(j)) * titleSize,0.0, 1.0);
var maxPointSs = vec4<f32>(vec2<f32>(f32(i) + 1.0, f32(j) + 1.0) * titleSize, 0.0, 1.0);
var minPointSs = vec4<f32>(vec2<f32>(f32(i) , f32(j)) * titleSize, 0.0, 1.0);
var minPointVs = ScreenToView(minPointSs).xyz;
var maxPointVs = ScreenToView(maxPointSs).xyz;
let f = (far / near) ;
var minPointVs = ScreenToView(minPointSs).xyz;
var tileNear = near * pow(f, f32(k) / clustersUniform.clusterTileZ) ;
var tileFar = near * pow(f, f32(k + 1u) / clustersUniform.clusterTileZ) ;
var tileNear = clustersUniform.near * pow(clustersUniform.far / clustersUniform.near, f32(k) / clustersUniform.clusterTileZ);
var tileFar = clustersUniform.near * pow(clustersUniform.far / clustersUniform.near, (f32(k) + 1.0) / clustersUniform.clusterTileZ);
var minPointNear = LineIntersectionToZPlane(eyePos, minPointVs, tileNear);
var minPointFar = LineIntersectionToZPlane(eyePos, minPointVs, tileFar);
var maxPointNear = LineIntersectionToZPlane(eyePos, maxPointVs, tileNear);
var maxPointFar = LineIntersectionToZPlane(eyePos, maxPointVs, tileFar);
var minPointNear = LineIntersectionToZPlane(eyePos, minPointVs, tileNear );
var minPointFar = LineIntersectionToZPlane(eyePos, minPointVs, tileFar );
var maxPointNear = LineIntersectionToZPlane(eyePos, maxPointVs, tileNear );
var maxPointFar = LineIntersectionToZPlane(eyePos, maxPointVs, tileFar );
var minPointAABB = min(min(minPointNear, minPointFar),min(maxPointNear, maxPointFar));
var maxPointAABB = max(max(minPointNear, minPointFar),max(maxPointNear, maxPointFar));
var minPointAABB = min(min(minPointNear, minPointFar), min(maxPointNear, maxPointFar));
var maxPointAABB = max(max(minPointNear, minPointFar), max(maxPointNear, maxPointFar));
var clusterBox : ClusterBox ;
clusterBox.minPoint = vec4<f32>(minPointAABB.xyz,f32(tileIndex)) ;
clusterBox.maxPoint = vec4<f32>(maxPointAABB.xyz,f32(tileIndex)) ;
clusterBuffer[tileIndex] = clusterBox;
clusterBox.minPoint = vec4<f32>(minPointAABB,f32(tileIndex)) ;
clusterBox.maxPoint = vec4<f32>(maxPointAABB,f32(tileIndex)) ;
clusterBuffer[tileIndex] = clusterBox;
}
`
72 changes: 33 additions & 39 deletions src/assets/shader/cluster/ClusterLighting_cs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ClusterConfig } from "../../../gfx/renderJob/passRenderer/cluster/ClusterConfig";

export let ClusterLighting_cs: string = /*wgsl*/`
#include "GlobalUniform"
struct ClusterBox{
minPoint:vec4<f32>,
maxPoint:vec4<f32>
min:vec4<f32>,
max:vec4<f32>
}
struct Light {
Expand Down Expand Up @@ -51,94 +53,86 @@ struct ClustersUniform{
clusterPix:f32,
}
struct Uniforms {
matrix : array<mat4x4<f32>>
};
var<private> clusterTileX:f32 ;
var<private> clusterTileY:f32 ;
var<private> clusterTileZ:f32 ;
// @group(0) @binding(1) var<storage, read> models : Uniforms;
@group(0) @binding(2) var<uniform> clustersUniform : ClustersUniform;
@group(0) @binding(3) var<storage,read> clusterBuffer : array<ClusterBox>;
@group(0) @binding(4) var<storage,read> lightBuffer : array<Light>;
@group(0) @binding(5) var<storage,read_write> lightAssignBuffer : array<f32>;
@group(0) @binding(6) var<storage,read_write> assignTable : array<LightIndex>;
@group(0) @binding(1) var<uniform> clustersUniform : ClustersUniform;
@group(0) @binding(2) var<storage,read> clusterBuffer : array<ClusterBox>;
@group(0) @binding(3) var<storage,read> lightBuffer : array<Light>;
@group(0) @binding(4) var<storage,read_write> lightAssignBuffer : array<f32>;
@group(0) @binding(5) var<storage,read_write> assignTable : array<LightIndex>;
fn gridToIndex(i:vec3<u32>) -> u32{
return i.z * u32(clusterTileX) * u32(clusterTileY) + i.y * u32(clusterTileX) + i.x ;
}
fn GetSqdisPointAABB( pos:vec3<f32>, clusterIndex:u32 ) -> f32
fn GetSqdisPointAABB( pos:vec3<f32>, cluster:ClusterBox ) -> f32
{
var sqDistance = 0.0;
let cluster = clusterBuffer[clusterIndex];
for (var i = 0u; i < 3u; i+=1u)
{
var v = pos[i];
if (v < cluster.minPoint[i])
if (v < cluster.min[i])
{
let diff = cluster.minPoint[i] - v;
let diff = cluster.min[i] - v;
sqDistance += diff * diff;
}
if (v > cluster.maxPoint[i])
if (v > cluster.max[i])
{
let diff = v - cluster.maxPoint[i];
let diff = v - cluster.max[i];
sqDistance += diff * diff;
}
}
return sqDistance;
}
fn TestSphereAABB( lightIndex:i32 , clusterIndex : u32 ) -> bool
fn TestSphereAABB( box:ClusterBox ,light:Light ) -> bool
{
let light = lightBuffer[lightIndex];
let lightPos = light.position.xyz;
var radius = light.range ;
var spherePos = globalUniform.viewMat * vec4<f32>(lightPos.xyz, 1.0) ;
// spherePos = vec4<f32>(spherePos.xyz / spherePos.w , 1.0) ;
let sqDistance = GetSqdisPointAABB(spherePos.xyz , clusterIndex);
var radius = light.range * 2.0 ;
var spherePos = globalUniform.viewMat * vec4<f32>(lightPos.xyz, 1.0) ;
spherePos = spherePos / spherePos.w ;
let sqDistance = GetSqdisPointAABB(spherePos.xyz , box);
return sqDistance <= (radius*radius);
}
@compute @workgroup_size(8,4,1)
@compute @workgroup_size(${ClusterConfig.clusterTileX},${ClusterConfig.clusterTileY},1)
fn CsMain( @builtin(workgroup_id) workgroup_id : vec3<u32> , @builtin(local_invocation_id) local_invocation_id : vec3<u32> ){
clusterTileX = clustersUniform.clusterTileX;
clusterTileY = clustersUniform.clusterTileY;
clusterTileZ = clustersUniform.clusterTileZ;
// cluster ID
let i = local_invocation_id.x ;
let j = local_invocation_id.y ;
let k = workgroup_id.x ;
clusterTileX = clustersUniform.clusterTileX;
clusterTileY = clustersUniform.clusterTileY;
clusterTileZ = clustersUniform.clusterTileZ;
var clusterId_3D = vec3<u32>(i,j,k);
var clusterId_1D = gridToIndex(clusterId_3D);
var box:ClusterBox = clusterBuffer[clusterId_1D];
var startIndex = i32(clusterId_1D) * i32(clustersUniform.maxNumLightsPerCluster) ;
var endIndex = startIndex;
for(var lightID = 0 ; lightID < i32(clustersUniform.numLights) ; lightID+=1)
{
if(!TestSphereAABB(lightID, clusterId_1D)) {
continue;
};
let li:Light = lightBuffer[lightID];
if(!TestSphereAABB(box, li)) {
continue;
}
lightAssignBuffer[endIndex] = f32(lightID);
endIndex += 1 ;
endIndex++;
}
// workgroupBarrier();
var idx: LightIndex;
idx.count = f32(endIndex-startIndex);
idx.start = f32(startIndex);
// idx.empty0 = f32(clusterId_1D);
// idx.empty1 = f32(clustersUniform.maxNumLightsPerCluster);
idx.empty0 = f32(clusterId_1D);
idx.empty1 = f32(clustersUniform.maxNumLightsPerCluster);
assignTable[clusterId_1D] = idx;
}
`
1 change: 1 addition & 0 deletions src/assets/shader/core/base/Common_frag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export let Common_frag: string = /*wgsl*/ `
ORI_FragmentOutput.material = vec4<f32>(0.0,1.0,0.0,0.0);
#endif
frag();
#if USE_DEBUG
debugFragmentOut();
#endif
Expand Down

0 comments on commit c88b687

Please sign in to comment.