Skip to content

Commit

Permalink
Light propagation 2: Increase speed a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
s-macke committed Oct 29, 2023
1 parent d52b712 commit e3ffa15
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
17 changes: 10 additions & 7 deletions src/scripts/light2/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ export class LightPropagation2 extends GPUAbstractRunner {
textureSrc: Texture

bind_group_layout: GPUBindGroupLayout
bind_groupa: GPUBindGroup
bind_groupb: GPUBindGroup
bind_group_atob: GPUBindGroup
bind_group_btoa: GPUBindGroup

scene_bind_group_layout: GPUBindGroupLayout
scene_bind_group: GPUBindGroup

pipeline_layout: GPUPipelineLayout
compute_pipeline: GPUComputePipeline
shader: GPUProgrammableStage

stagingBuffer: Buffer
stagingData: Float32Array

Expand Down Expand Up @@ -102,7 +105,7 @@ export class LightPropagation2 extends GPUAbstractRunner {
}]
})

this.bind_groupa = GPU.device.createBindGroup({
this.bind_group_atob = GPU.device.createBindGroup({
layout: this.bind_group_layout,
entries: [{
binding: 0,
Expand All @@ -116,7 +119,7 @@ export class LightPropagation2 extends GPUAbstractRunner {
}]
})

this.bind_groupb = GPU.device.createBindGroup({
this.bind_group_btoa = GPU.device.createBindGroup({
layout: this.bind_group_layout,
entries: [{
binding: 0,
Expand Down Expand Up @@ -168,16 +171,16 @@ export class LightPropagation2 extends GPUAbstractRunner {
GPU.device.queue.writeBuffer(this.stagingBuffer.buffer, 0, this.stagingData)

let encoder: GPUCommandEncoder = GPU.device.createCommandEncoder({});
for(let i = 0; i < 60; i++) {
for(let i = 0; i < 80; i++) {
let pass: GPUComputePassEncoder = encoder.beginComputePass();
pass.setBindGroup(0, this.bind_groupa);
pass.setBindGroup(0, this.bind_group_atob);
pass.setBindGroup(1, this.scene_bind_group);
pass.setPipeline(this.compute_pipeline);
pass.dispatchWorkgroups(this.width / 8, this.height / 8);
pass.end();

pass = encoder.beginComputePass();
pass.setBindGroup(0, this.bind_groupb);
pass.setBindGroup(0, this.bind_group_btoa);
pass.setBindGroup(1, this.scene_bind_group);
pass.setPipeline(this.compute_pipeline);
pass.dispatchWorkgroups(this.width / 8, this.height / 8);
Expand Down
18 changes: 11 additions & 7 deletions src/scripts/light2/propagate.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ fn retrieveConstants(pn: vec2i, F: ptr<function, ColorCH>, E: ptr<function, Col
*/
}

// note that sa2 = (90° - sa1) / 2
//const sa1: f32 = solid_angle(vec2f(1.5, -0.5), vec2<f32>(1.5, 0.5)); // ~53.13°, projecting to the right side of our pixel
//const sa2: f32 = solid_angle(vec2f(0.5, 0.5), vec2<f32>(1.5, 0.5)); // ~18.43°, projecting to the top/bottom side of our pixel
//const dn1: vec2f = normalize(vec2f(2., 1.0)); // normal to the center of the top side of our pixel

// TODO: the comments are wrong
const sa1= 0.6435011087932845; // ~53.13°, projecting to the right side of our pixel
const sa2= 0.4636476090008066; // ~18.43°, projecting to the top/bottom side of our pixel
const dn1 = vec2f(0.8944271909999159, 0.4472135954999579); // normal to the center of the top side of our pixel
const dn0 = vec2f(0.8944271909999159, -0.4472135954999579); // normal to the center of the bottom side of our pixel

fn lpv_kernel(p: vec2i, out: ptr<function, ColorCH>) {
let Em = textureLoad(scene, p, 0, 0).xyz; // emitted rgb light as circular harmonics
let translucency = textureLoad(scene, p, 1, 0).w;
Expand All @@ -75,11 +86,6 @@ fn lpv_kernel(p: vec2i, out: ptr<function, ColorCH>) {
let dn1: vec2f = normalize(vec2f(x - 0.5, 1.0)); // normal to the center of the top side of our pixel
let dn0: vec2f = normalize(vec2f(x - 0.5, -1.0)); // normal to the center of the bottom side of our pixel
*/
let sa1: f32 = solid_angle(vec2f(1.5, -0.5), vec2<f32>(1.5, 0.5)); // ~53.13°, projecting to the right side of our pixel
let sa2: f32 = solid_angle(vec2f(0.5, 0.5), vec2<f32>(1.5, 0.5)); // ~18.43°, projecting to the top/bottom side of our pixel
// note that sa2 = (90° - sa1) / 2
let dn1: vec2f = normalize(vec2f(2., 1.0)); // normal to the center of the top side of our pixel
let dn0: vec2f = normalize(vec2f(2., -1.0)); // normal to the center of the bottom side of our pixel

/*
float sa1 = solid_angle(vec2(1.5, -0.5),vec2(1.5,0.5)); // ~53.13°, projecting to the right side of our pixel
Expand All @@ -98,14 +104,12 @@ float sa1 = solid_angle(vec2(1.5, -0.5),vec2(1.5,0.5)); // ~53.13°, projecting
propagate(dn1, sa2, F, E, out);
propagate(dn0, sa2, F, E, out);


pn = p + vec2i(1, 0);
retrieveConstants(pn, &F, &E);
propagate(vec2f(-1., 0.), sa1, F, E, out);
propagate(-dn0, sa2, F, E, out);
propagate(-dn1, sa2, F, E, out);


pn = p + vec2i(0, -1);
retrieveConstants(pn, &F, &E);
propagate(vec2f(0., 1.), sa1, F, E, out);
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/light2/scene/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class LightScene {

// 0: color emitter circular harmonics, z-component
// 1: normal vector of the surface
this.emitter = GPU.CreateStorageTextureArray(this.width, this.height, 2, "rgba16float")
this.emitter = GPU.CreateStorageTextureArray(this.width, this.height, 2, "rgba8unorm")

this.raytrace = new Raytrace("fbm.wgsl")
try {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/light2/scene/scene.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn sphere(p: vec2<f32>, r: f32) -> f32 {
return length(p) - r;
}

@group(0) @binding(0) var scene : texture_storage_2d_array<rgba16float, write>;
@group(0) @binding(0) var scene : texture_storage_2d_array<rgba8unorm, write>;
@group(0) @binding(1) var<uniform> staging: StagingBuffer;
@group(0) @binding(2) var sdf : texture_2d<f32>;

Expand Down

0 comments on commit e3ffa15

Please sign in to comment.