From 71d8e7300d5752b1f45ff263d18424f47deaccb1 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Fri, 13 Dec 2024 06:52:34 +0300 Subject: [PATCH] a lot of useless optimizations --- prismarine-viewer/examples/Cube.comp.wgsl | 6 +-- prismarine-viewer/examples/Cube.vert.wgsl | 30 +----------- prismarine-viewer/examples/webgpuRenderer.ts | 47 ++++++++++++++++--- .../examples/webgpuRendererShared.ts | 4 +- 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/prismarine-viewer/examples/Cube.comp.wgsl b/prismarine-viewer/examples/Cube.comp.wgsl index d631f6e0b..25edd2245 100644 --- a/prismarine-viewer/examples/Cube.comp.wgsl +++ b/prismarine-viewer/examples/Cube.comp.wgsl @@ -70,8 +70,8 @@ fn main(@builtin(global_invocation_id) global_id: vec3) { clipX = clamp(clipX, -1, 1); } var pos : vec2u = vec2u(u32((clipX * 0.5 + 0.5) * f32(textureSize.x)),u32((clipY * 0.5 + 0.5) * f32(textureSize.y))); - - if (linearize_depth_ndc(clipDepth, 0.05, 10000) - 20 > linearize_depth_ndc(textureLoad(depthTexture, vec2u(pos.x, textureSize.y - pos.y), 0), 0.05, 10000) && !nearby) { + let k = linearize_depth_ndc(clipDepth, 0.05, 10000) ; + if (k- 20 > linearize_depth_ndc(textureLoad(depthTexture, vec2u(pos.x, textureSize.y - pos.y), 0), 0.05, 10000)) { return; } if (nearby) { @@ -84,7 +84,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3) { pos.y = index % textureSize.y; } } - let depth = u32(clipDepth * 10000); + let depth = u32(k + 1.5); var depthPrev = atomicMin(&depthAtomic.locks[pos.x][pos.y], depth); //depthPrev = atomicLoad(&depthAtomic.locks[pos.x][pos.y]); if (depth < depthPrev) { diff --git a/prismarine-viewer/examples/Cube.vert.wgsl b/prismarine-viewer/examples/Cube.vert.wgsl index f77ccb915..a002ac0ea 100644 --- a/prismarine-viewer/examples/Cube.vert.wgsl +++ b/prismarine-viewer/examples/Cube.vert.wgsl @@ -29,28 +29,7 @@ struct VertexOutput { @group(0) @binding(3) var models: array; @group(1) @binding(1) var visibleCubes: array; @group(1) @binding(2) var chunks : array; - -fn rotationX(angle: f32) -> mat4x4 { - let c = cos(angle); - let s = sin(angle); - return mat4x4( - vec4(1.0, 0.0, 0.0, 0.0), - vec4(0.0, c, -s, 0.0), - vec4(0.0, s, c, 0.0), - vec4(0.0, 0.0, 0.0, 1.0), - ); -} - -fn rotationY(angle: f32) -> mat4x4 { - let c = cos(angle); - let s = sin(angle); - return mat4x4( - vec4(c, 0.0, s, 0.0), - vec4(0.0, 1.0, 0.0, 0.0), - vec4(-s, 0.0, c, 0.0), - vec4(0.0, 0.0, 0.0, 1.0), - ); -} +@group(0) @binding(4) var rotatations: array, 6>; @vertex fn main( @@ -82,37 +61,32 @@ fn main( var normal : mat4x4; var Uv = vec2(uv.x, (1.0 - uv.y)); + normal = rotatations[normalIndex]; switch (normalIndex) { case 0: { Uv = vec2((1.0f-uv.x), (1.0 - uv.y)); - normal = rotationX(radians(-90f)); textureIndex = models[modelIndex].textureIndex123 & 1023; } case 1: { - normal = rotationX(radians(90f)); textureIndex = (models[modelIndex].textureIndex123 >> 10) & 1023; } case 2: { - normal = rotationX(radians(0f)); textureIndex = (models[modelIndex].textureIndex123 >> 20) & 1023; } case 3: { Uv = uv; - normal = rotationX(radians(180f)); textureIndex = models[modelIndex].textureIndex456 & 1023; } case 4: { - normal = rotationY(radians(90f)); textureIndex = (models[modelIndex].textureIndex456 >> 10) & 1023; } case 5, default: { - normal = rotationY(radians(-90f)); textureIndex = (models[modelIndex].textureIndex456 >> 20) & 1023; } } diff --git a/prismarine-viewer/examples/webgpuRenderer.ts b/prismarine-viewer/examples/webgpuRenderer.ts index 70e65f5d2..207fda12b 100644 --- a/prismarine-viewer/examples/webgpuRenderer.ts +++ b/prismarine-viewer/examples/webgpuRenderer.ts @@ -93,6 +93,7 @@ export class WebgpuRenderer { depthTextureAnother: GPUTexture volumetricRenderPassDescriptor: GPURenderPassDescriptor tempTexture: GPUTexture + rotationsUniform: GPUBuffer // eslint-disable-next-line max-params @@ -311,6 +312,26 @@ export class WebgpuRenderer { usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, }) + this.rotationsUniform = device.createBuffer({ + size: Mat4x4BufferSize * 6, + usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, + }) + + const matrixData = new Float32Array([ + ...new THREE.Matrix4().makeRotationX(THREE.MathUtils.degToRad(90)).toArray(), + ...new THREE.Matrix4().makeRotationX(THREE.MathUtils.degToRad(-90)).toArray(), + ...new THREE.Matrix4().makeRotationY(THREE.MathUtils.degToRad(0)).toArray(), + ...new THREE.Matrix4().makeRotationY(THREE.MathUtils.degToRad(180)).toArray(), + ...new THREE.Matrix4().makeRotationY(THREE.MathUtils.degToRad(-90)).toArray(), + ...new THREE.Matrix4().makeRotationY(THREE.MathUtils.degToRad(90)).toArray(), + ]) + + device.queue.writeBuffer( + this.rotationsUniform, + 0, + matrixData + ) + this.cameraComputeUniform = device.createBuffer({ size: Mat4x4BufferSize, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, @@ -588,6 +609,12 @@ export class WebgpuRenderer { resource: { buffer: this.modelsBuffer }, + }, + { + binding: 4, + resource: { + buffer: this.rotationsUniform + } } ], }) @@ -634,6 +661,12 @@ export class WebgpuRenderer { resource: { buffer: this.modelsBuffer }, + }, + { + binding: 4, + resource: { + buffer: this.rotationsUniform + } } ], }) @@ -962,7 +995,7 @@ export class WebgpuRenderer { // this.multisampleTexture = multisampleTexture // } - + // TODO! if (this.rendererParams.godRays) { @@ -987,9 +1020,9 @@ export class WebgpuRenderer { this.commandEncoder = device.createCommandEncoder() //this.commandEncoder.clearBuffer(this.occlusionTexture) - + //this.commandEncoder.clearBuffer(this.DepthTextureBuffer); - if (this.rendererParams.occlusion) { + if (this.rendererParams.occlusionActive) { this.commandEncoder.clearBuffer(this.occlusionTexture) this.commandEncoder.clearBuffer(this.visibleCubesBuffer) device.queue.writeBuffer(this.indirectDrawBuffer, 0, this.indirectDrawParams) @@ -999,7 +1032,7 @@ export class WebgpuRenderer { device.queue.writeBuffer(this.textureSizeBuffer, 0, textureSize) if (this.realNumberOfCubes) { - if (this.rendererParams.occlusion) { + if (this.rendererParams.occlusionActive) { { const computePass = this.commandEncoder.beginComputePass() computePass.label = 'Frustrum/Occluision Culling' @@ -1021,6 +1054,9 @@ export class WebgpuRenderer { computePass.setBindGroup(2, this.textureSizeBindGroup) computePass.dispatchWorkgroups(Math.ceil(this.canvas.width / 16), Math.ceil(this.canvas.height / 16)) computePass.end() + if (!this.indirectDrawBufferMapBeingUsed) { + this.commandEncoder.copyBufferToBuffer(this.indirectDrawBuffer, 0, this.indirectDrawBufferMap, 0, 16) + } device.queue.submit([this.commandEncoder.finish()]) } } @@ -1041,9 +1077,6 @@ export class WebgpuRenderer { } renderPass.end() - if (!this.indirectDrawBufferMapBeingUsed) { - this.commandEncoder.copyBufferToBuffer(this.indirectDrawBuffer, 0, this.indirectDrawBufferMap, 0, 16) - } device.queue.submit([this.commandEncoder.finish()]) } diff --git a/prismarine-viewer/examples/webgpuRendererShared.ts b/prismarine-viewer/examples/webgpuRendererShared.ts index 5147bd5e8..76144a67a 100644 --- a/prismarine-viewer/examples/webgpuRendererShared.ts +++ b/prismarine-viewer/examples/webgpuRendererShared.ts @@ -7,7 +7,7 @@ export const defaultWebgpuRendererParams = { cameraOffset: [0, 0, 0] as [number, number, number], webgpuWorker: workerParam ? workerParam === 'true' : !isSafari, godRays: false, - occlusion: true + occlusionActive: true, } export const rendererParamsGui = { @@ -17,7 +17,7 @@ export const rendererParamsGui = { qsReload: true }, godRays: true, - occlusion: true + occlusionActive: true, } export type RendererInitParams = GPURequestAdapterOptions & {}