Skip to content

Commit

Permalink
a lot of useless optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Dec 13, 2024
1 parent 9000445 commit 71d8e73
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
6 changes: 3 additions & 3 deletions prismarine-viewer/examples/Cube.comp.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
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) {
Expand All @@ -84,7 +84,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
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) {
Expand Down
30 changes: 2 additions & 28 deletions prismarine-viewer/examples/Cube.vert.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,7 @@ struct VertexOutput {
@group(0) @binding(3) var<storage, read> models: array<CubeModel>;
@group(1) @binding(1) var<storage, read> visibleCubes: array<CubePointer>;
@group(1) @binding(2) var<storage, read> chunks : array<Chunk>;

fn rotationX(angle: f32) -> mat4x4<f32> {
let c = cos(angle);
let s = sin(angle);
return mat4x4<f32>(
vec4<f32>(1.0, 0.0, 0.0, 0.0),
vec4<f32>(0.0, c, -s, 0.0),
vec4<f32>(0.0, s, c, 0.0),
vec4<f32>(0.0, 0.0, 0.0, 1.0),
);
}

fn rotationY(angle: f32) -> mat4x4<f32> {
let c = cos(angle);
let s = sin(angle);
return mat4x4<f32>(
vec4<f32>(c, 0.0, s, 0.0),
vec4<f32>(0.0, 1.0, 0.0, 0.0),
vec4<f32>(-s, 0.0, c, 0.0),
vec4<f32>(0.0, 0.0, 0.0, 1.0),
);
}
@group(0) @binding(4) var<uniform> rotatations: array<mat4x4<f32>, 6>;

@vertex
fn main(
Expand Down Expand Up @@ -82,37 +61,32 @@ fn main(

var normal : mat4x4<f32>;
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;
}
}
Expand Down
47 changes: 40 additions & 7 deletions prismarine-viewer/examples/webgpuRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class WebgpuRenderer {
depthTextureAnother: GPUTexture
volumetricRenderPassDescriptor: GPURenderPassDescriptor
tempTexture: GPUTexture
rotationsUniform: GPUBuffer


// eslint-disable-next-line max-params
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -588,6 +609,12 @@ export class WebgpuRenderer {
resource: {
buffer: this.modelsBuffer
},
},
{
binding: 4,
resource: {
buffer: this.rotationsUniform
}
}
],
})
Expand Down Expand Up @@ -634,6 +661,12 @@ export class WebgpuRenderer {
resource: {
buffer: this.modelsBuffer
},
},
{
binding: 4,
resource: {
buffer: this.rotationsUniform
}
}
],
})
Expand Down Expand Up @@ -962,7 +995,7 @@ export class WebgpuRenderer {
// this.multisampleTexture = multisampleTexture
// }



// TODO!
if (this.rendererParams.godRays) {
Expand All @@ -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)
Expand All @@ -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'
Expand All @@ -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()])
}
}
Expand All @@ -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()])
}
Expand Down
4 changes: 2 additions & 2 deletions prismarine-viewer/examples/webgpuRendererShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -17,7 +17,7 @@ export const rendererParamsGui = {
qsReload: true
},
godRays: true,
occlusion: true
occlusionActive: true,
}

export type RendererInitParams = GPURequestAdapterOptions & {}
Expand Down

0 comments on commit 71d8e73

Please sign in to comment.