Skip to content

Commit

Permalink
fix(MatrixDO): MatrixDO Buffer (#108)
Browse files Browse the repository at this point in the history
* fix(MatrixDO): MatrixDO Buffer
fix matrixDO Buffer Descript bytesLength count wrong
  • Loading branch information
ZenderJK committed May 7, 2023
1 parent f455f42 commit 5e6fcdb
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 90 deletions.
2 changes: 1 addition & 1 deletion samples/base/Sample_Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Sample_Transform {

// add a basic camera controller
let hoverCameraController = mainCamera.object3D.addComponent(HoverCameraController);
hoverCameraController.setCamera(15, -15, 10);
hoverCameraController.setCamera(15, -15, 300);

// create a basic cube
let cubeObj = new Object3D();
Expand Down
4 changes: 2 additions & 2 deletions samples/material/Sample_PBRMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Sample_PBRMaterial {
GUIHelp.init(999);

this.scene = new Scene3D();
this.scene.hideSky();
let camera = CameraUtil.createCamera3DObject(this.scene);
camera.perspective(60, webGPUContext.aspect, 0.01, 5000.0);

Expand All @@ -43,7 +42,8 @@ class Sample_PBRMaterial {
async initScene() {
/******** sky *******/
{
this.scene.addComponent(AtmosphericComponent);
let sky = this.scene.addComponent(AtmosphericComponent);
sky.enable = false;
}
/******** light *******/
{
Expand Down
4 changes: 2 additions & 2 deletions src/Engine3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export class Engine3D {
this.renderJobs.set(view, renderJob);
renderJob.addPost(new FXAAPost());
renderJob.start();
this.render(0);
this.resume();
return renderJob;
}

Expand All @@ -314,7 +314,7 @@ export class Engine3D {
renderJob.addPost(new FXAAPost());
renderJob.start();
}
this.render(0);
this.resume();
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/components/Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class Transform extends ComponentBase {
private _left: Vector3 = new Vector3();
private _up: Vector3 = new Vector3();
private _down: Vector3 = new Vector3();
public _worldMatrix: Matrix4;
public readonly _worldMatrix: Matrix4;
private _localChange: boolean = true;

private _targetPos: Vector3;
Expand Down Expand Up @@ -160,7 +160,7 @@ export class Transform extends ComponentBase {

constructor() {
super();
this.worldMatrix = new Matrix4(false);
this._worldMatrix = new Matrix4(true);
this._localPos = new Vector3();
this._localRot = new Vector3();
this._localRotQuat = new Quaternion();
Expand Down Expand Up @@ -352,13 +352,6 @@ export class Transform extends ComponentBase {
return this._worldMatrix;
}

/**
* @internal
*/
public set worldMatrix(value: Matrix4) {
this._worldMatrix = value;
}

/**
*
* Update the matrix4 in world space
Expand Down Expand Up @@ -429,7 +422,6 @@ export class Transform extends ComponentBase {
this.localRotQuat = null;
this.localRotation = null;
this.localScale = null;
this._worldMatrix = null;
}
super.destroy();
}
Expand Down
75 changes: 34 additions & 41 deletions src/components/renderer/RenderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ export class RenderNode extends ComponentBase {
this._castReflection = value;
}



public renderPass(view: View3D, passType: RendererType, renderContext: RenderContext) {
let renderNode = this;
for (let i = 0; i < renderNode.materials.length; i++) {
Expand All @@ -251,7 +249,7 @@ export class RenderNode extends ComponentBase {
if (!passes || passes.length == 0) continue;

GPUContext.bindGeometryBuffer(renderContext.encoder, renderNode._geometry);
let worldMatrix = renderNode.object3D.transform._worldMatrix;
let worldMatrix = renderNode.transform._worldMatrix;
for (let j = 0; j < passes.length; j++) {
if (!passes || passes.length == 0) continue;
let matPass = passes[j];
Expand All @@ -260,7 +258,6 @@ export class RenderNode extends ComponentBase {
// for (let j = passes.length > 1 ? 1 : 0 ; j < passes.length; j++) {
const renderShader = matPass.renderShader;
if (renderShader.shaderState.splitTexture) {

renderContext.endRenderPass();
RTResourceMap.WriteSplitColorTexture(renderNode.instanceID);
renderContext.beginRenderPass();
Expand All @@ -270,17 +267,17 @@ export class RenderNode extends ComponentBase {
}
GPUContext.bindPipeline(renderContext.encoder, renderShader);
let subGeometries = renderNode._geometry.subGeometries;
for (let k = 0; k < subGeometries.length; k++) {
const subGeometry = subGeometries[k];
let lodInfos = subGeometry.lodLevels;
let lodInfo = lodInfos[renderNode.lodLevel];
// for (let k = 0; k < subGeometries.length; k++) {
const subGeometry = subGeometries[i];
let lodInfos = subGeometry.lodLevels;
let lodInfo = lodInfos[renderNode.lodLevel];

if (renderNode.instanceCount > 0) {
GPUContext.drawIndexed(renderContext.encoder, lodInfo.indexCount, renderNode.instanceCount, lodInfo.indexStart, 0, 0);
} else {
GPUContext.drawIndexed(renderContext.encoder, lodInfo.indexCount, 1, lodInfo.indexStart, 0, worldMatrix.index);
}
if (renderNode.instanceCount > 0) {
GPUContext.drawIndexed(renderContext.encoder, lodInfo.indexCount, renderNode.instanceCount, lodInfo.indexStart, 0, 0);
} else {
GPUContext.drawIndexed(renderContext.encoder, lodInfo.indexCount, 1, lodInfo.indexStart, 0, worldMatrix.index);
}
// }
}
}
}
Expand All @@ -305,24 +302,21 @@ export class RenderNode extends ComponentBase {

let worldMatrix = node.object3D.transform._worldMatrix;
if (this.drawType == 2) {
for (let i = 0; i < passes.length; i++) {
let renderShader = passes[i].renderShader;
for (let j = 0; j < passes.length; j++) {
let renderShader = passes[j].renderShader;
GPUContext.bindPipeline(encoder, renderShader);
GPUContext.draw(encoder, 6, 1, 0, worldMatrix.index);
}
} else {
GPUContext.bindGeometryBuffer(encoder, node._geometry);
for (let i = 0; i < passes.length; i++) {
let renderShader = passes[i].renderShader;

for (let j = 0; j < passes.length; j++) {
let renderShader = passes[j].renderShader;
GPUContext.bindPipeline(encoder, renderShader);
let subGeometries = node._geometry.subGeometries;
for (let k = 0; k < subGeometries.length; k++) {
const subGeometry = subGeometries[k];
let lodInfos = subGeometry.lodLevels;
let lodInfo = lodInfos[node.lodLevel];
GPUContext.drawIndexed(encoder, lodInfo.indexCount, 1, lodInfo.indexStart, 0, worldMatrix.index);
}
const subGeometry = subGeometries[i];
let lodInfos = subGeometry.lodLevels;
let lodInfo = lodInfos[node.lodLevel];
GPUContext.drawIndexed(encoder, lodInfo.indexCount, 1, lodInfo.indexStart, 0, worldMatrix.index);
}
}

Expand All @@ -343,17 +337,14 @@ export class RenderNode extends ComponentBase {
if (!passes || passes.length == 0) return;

let worldMatrix = node.object3D.transform._worldMatrix;
for (let i = 0; i < passes.length; i++) {
const renderShader = passes[i].renderShader;

for (let j = 0; j < passes.length; j++) {
const renderShader = passes[j].renderShader;
GPUContext.bindPipeline(encoder, renderShader);
let subGeometries = node._geometry.subGeometries;
for (let k = 0; k < subGeometries.length; k++) {
const subGeometry = subGeometries[k];
let lodInfos = subGeometry.lodLevels;
let lodInfo = lodInfos[node.lodLevel];
GPUContext.drawIndexed(encoder, lodInfo.indexCount, 1, lodInfo.indexStart, 0, worldMatrix.index);
}
const subGeometry = subGeometries[i];
let lodInfos = subGeometry.lodLevels;
let lodInfo = lodInfos[node.lodLevel];
GPUContext.drawIndexed(encoder, lodInfo.indexCount, 1, lodInfo.indexStart, 0, worldMatrix.index);
}
}

Expand All @@ -373,16 +364,15 @@ export class RenderNode extends ComponentBase {
let passes = material.renderPasses.get(passType);
if (passes) {
for (let i = 0; i < passes.length; i++) {
const pass = passes[i];// RenderShader.getShader(passes[i].shaderID);
const renderShader = pass.renderShader;// RenderShader.getShader(passes[i].shaderID);

const pass = passes[i];
// RenderShader.getShader(passes[i].shaderID);
const renderShader = pass.renderShader;
// RenderShader.getShader(passes[i].shaderID);
if (renderShader.shaderState.splitTexture) {
let splitTexture = RTResourceMap.CreateSplitTexture(this.instanceID);
renderShader.setTexture("splitTexture_Map", splitTexture);
}

// renderShader.setUniformVector3("center", this.transform.worldPosition);

// if(scene3D.envMapChange){
if (!this._ignoreEnvMap) {
renderShader.setTexture(`envMap`, view.scene.envMap);
Expand All @@ -400,13 +390,16 @@ export class RenderNode extends ComponentBase {
let bdrflutTex = Engine3D.res.getTexture(`BRDFLUT`);
renderShader.setTexture(`brdflutMap`, bdrflutTex);

if (Engine3D.getRenderJob(view).shadowMapPassRenderer.depth2DTextureArray) {
let shadowRenderer = Engine3D.getRenderJob(view).shadowMapPassRenderer;
if (shadowRenderer && shadowRenderer.depth2DTextureArray) {
renderShader.setTexture(`shadowMap`, Engine3D.getRenderJob(view).shadowMapPassRenderer.depth2DTextureArray);
}

// let shadowLight = ShadowLights.list;
// if (shadowLight.length) {
renderShader.setTexture(`pointShadowMap`, Engine3D.getRenderJob(view).pointLightShadowRenderer.cubeTextureArray);
let pointShadowRenderer = Engine3D.getRenderJob(view).pointLightShadowRenderer;
if (pointShadowRenderer && pointShadowRenderer.cubeTextureArray) {
renderShader.setTexture(`pointShadowMap`, pointShadowRenderer.cubeTextureArray);
}
// }

let iesTexture = IESProfiles.iesTexture;
Expand Down
10 changes: 0 additions & 10 deletions src/core/Scene3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ export class Scene3D extends Object3D {
EntityCollect.instance.sky.map = value;
}

public showSky() {
// if (EntityCollect.instance.sky)
// EntityCollect.instance.sky.enable = true;
}

public hideSky() {
// if (EntityCollect.instance.sky)
// EntityCollect.instance.sky.enable = false;
}

/**
* Exposure of Sky Box. A larger value produces a sky box with stronger exposure and a brighter appearance.
* A smaller value produces a sky box with weaker exposure and a darker appearance.
Expand Down
15 changes: 1 addition & 14 deletions src/gfx/graphics/webGpu/core/bindGroups/GlobalUniformGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class GlobalUniformGroup {

createBindGroup() {
this.uniformByteLength = this.uniformGPUBuffer.memory.shareDataBuffer.byteLength;
this.matrixesByteLength = Matrix4.blockBytes * Matrix4.totalCount;
this.matrixesByteLength = Matrix4.blockBytes * Matrix4.maxCount;

this.globalBindGroup = webGPUContext.device.createBindGroup({
label: `global_bindGroupLayout`,
Expand Down Expand Up @@ -130,18 +130,5 @@ export class GlobalUniformGroup {

public addUniformNode() { }

writeBuffer() {
const matBytes = Matrix4.matrixBytes;
let totalBytes = matBytes.byteLength;
let offsetBytes = 0;

// webGPUContext.device.queue.writeBuffer(this.matrixBufferDst, offsetBytes, matBytes.buffer, matBytes.byteOffset, matBytes.byteLength);

const space = 5000 * 64; // 5000 * 16 * 4
while (offsetBytes < totalBytes) {
let len = Math.min(space, totalBytes - offsetBytes);
webGPUContext.device.queue.writeBuffer(this.matrixBindGroup.matrixBufferDst.buffer, offsetBytes, matBytes.buffer, matBytes.byteOffset + offsetBytes, len);
offsetBytes += len;
}
}
}
1 change: 1 addition & 0 deletions src/gfx/graphics/webGpu/core/bindGroups/MatrixBindGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class MatrixBindGroup {
this.cacheWorldMatrix();
}


private cacheWorldMatrix() {
this.groupBufferSize = Matrix4.maxCount * Matrix4.blockBytes;
this.matrixBufferDst = new StorageGPUBuffer(this.groupBufferSize / 4);
Expand Down
23 changes: 13 additions & 10 deletions src/math/Matrix4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class Matrix4 {
/**
* matrix do total count
*/
public static totalCount: number = 0;
public static allocCount: number = 0;

/**
* matrix has max limit count
*/
public static maxCount: number = 300000;
public static maxCount: number = 200000;

/**
* current matrix use count
Expand Down Expand Up @@ -119,10 +119,10 @@ export class Matrix4 {
* @param count every alloc matrix count
* @version Orillusion3D 0.5.1
*/
public static allocMatrix(totalCount: number) {
this.totalCount = totalCount;
public static allocMatrix(allocCount: number) {
this.allocCount = allocCount;

Matrix4.matrixBytes = new Float32Array(totalCount * 16);
Matrix4.matrixBytes = new Float32Array(allocCount * 16);
Matrix4.buffer = Matrix4.matrixBytes.buffer;
Matrix4.wasmMatrixPtr = 0;

Expand Down Expand Up @@ -317,19 +317,22 @@ export class Matrix4 {
*
* @param local --
*/
constructor(local: boolean = true) {
// if (!local) {
if (Matrix4.useCount >= Matrix4.totalCount) {
Matrix4.allocMatrix(Matrix4.totalCount + 1000);
constructor(doMatrix: boolean = false) {
// if (doMatrix) {
if (Matrix4.useCount >= Matrix4.allocCount) {
Matrix4.allocMatrix(Matrix4.allocCount + 1000);
}

this.index = Matrix4.useCount;
this.offset = Matrix4.useCount * Matrix4.blockBytes + Matrix4.wasmMatrixPtr;

Matrix4.globalMatrixRef[this.index] = this;
Matrix4.useCount++;

// console.log(this.index);
this.rawData = new Float32Array(Matrix4.matrixBytes.buffer, this.offset, 16);
// } else {
// this.rawData = new Float32Array(16);
// }

this._position = new Vector3();

Expand Down

0 comments on commit 5e6fcdb

Please sign in to comment.