Skip to content

Commit

Permalink
Separate 2D lighting into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
s-macke committed Jul 12, 2024
1 parent f746d29 commit 4dcc40d
Show file tree
Hide file tree
Showing 16 changed files with 584 additions and 311 deletions.
26 changes: 23 additions & 3 deletions src/scripts/fluid/fluid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {Div} from "./div/div";
import {Project} from "./project/project";
import {Source} from "./source/source";
import {GPUAbstractRunner, RunnerType} from "../AbstractGPURunner";
import {LightScene} from "./scene/scene";
import {LightPropagation} from "../modules/light/propagation/light";
import {MonteCarloPathTracing} from "../modules/light/monte_carlo_path_tracing/light";

// Staggered Grid
/*
Expand Down Expand Up @@ -43,7 +46,9 @@ export class Fluid extends GPUAbstractRunner {
render: Render;
poisson: Poisson;
div: Div;
project: Project
project: Project;
scene: LightScene;
light: LightPropagation;

velocity: Texture;
density: Texture;
Expand Down Expand Up @@ -80,6 +85,15 @@ export class Fluid extends GPUAbstractRunner {
this.project = new Project(this.poisson.pressurea, this.velocity, this.flags);
await this.project.Init();

this.scene = new LightScene(this.density)
await this.scene.Init()
/*
this.light = new LightPropagation(this.scene.emitter)
*/
this.light = new MonteCarloPathTracing(this.scene.emitter, 20)
await this.light.Init()


this.render = new Render(this.density);
//this.render = new Render(this.div.div);
//this.render = new Render(this.velocity);
Expand Down Expand Up @@ -108,6 +122,8 @@ export class Fluid extends GPUAbstractRunner {
}

public async Destroy() {
await this.light.Destroy()
await this.scene.Destroy()
await this.project.Destroy()
await this.poisson.Destroy()
await this.div.Destroy()
Expand All @@ -121,28 +137,32 @@ export class Fluid extends GPUAbstractRunner {


async Run() {

this.light.Reset()
GPU.device.queue.submit([
this.source.GetCommandBuffer(),
this.transport.GetCommandBuffer(),
this.advect.GetCommandBuffer(),
this.div.GetCommandBuffer(),
this.poisson.GetCommandBuffer(),
this.project.GetCommandBuffer(),
this.scene.GetCommandBuffer(),
//this.light.GetCommandBuffer(),
])
//await GPU.Render(this.transport.texturea);
//await GPU.Render(this.transport.texturea);
//await GPU.Render(this.poisson.pressurea);
}

Render() {
//this.light.Render()

GPU.device.queue.submit([
this.render.GetCommandBuffer()
])
}

async InitVelocity() {
let vel = new Uint16Array(this.width * this.height * 4)
let vel = new Uint16Array(this.width * this.height * 4)
/*
for (let j = 0; j < this.height; j++)
for (let i = 0; i < this.width; i++) {
Expand Down
96 changes: 96 additions & 0 deletions src/scripts/fluid/scene/scene.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import {GPU} from "../../webgpu/gpu";
import {Texture} from "../../webgpu/texture";
import {Buffer} from "../../webgpu/buffer";
import {Raytrace} from "../../raytrace/raytrace";
import {ShowError} from "../../ui";
import {SDF} from "../../sdf/sdf";

export class LightScene {
width: number
height: number

emitter: Texture
density: Texture

bind_group_layout: GPUBindGroupLayout
bind_group: GPUBindGroup
pipeline_layout: GPUPipelineLayout
compute_pipeline: GPUComputePipeline
shader: GPUProgrammableStage

constructor(texture: Texture) {
this.density = texture
this.width = GPU.viewport.width
this.height = GPU.viewport.height
}

async Destroy() {
this.emitter.destroy()
}

async Init() {
let shader = await GPU.CreateShaderFromURL("scripts/fluid/scene/scene.wgsl")

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

this.bind_group_layout = GPU.device.createBindGroupLayout({
entries: [{
binding: 0,
storageTexture: {
access: "write-only",
format: this.emitter.format,
viewDimension: "2d-array"
},
visibility: GPUShaderStage.COMPUTE
}, {
binding: 1,
texture: {
sampleType: "unfilterable-float",
},
visibility: GPUShaderStage.COMPUTE
}, ]
});

this.bind_group = GPU.device.createBindGroup({
layout: this.bind_group_layout,
entries: [{
binding: 0,
resource: this.emitter.textureView
}, {
binding: 1,
resource: this.density.textureView
}]
});

this.pipeline_layout = GPU.device.createPipelineLayout({
bindGroupLayouts: [this.bind_group_layout]
});

this.compute_pipeline = GPU.device.createComputePipeline({
layout: this.pipeline_layout,
compute: shader
});
}

GetCommandBuffer() : GPUCommandBuffer {
let encoder: GPUCommandEncoder = GPU.CreateCommandEncoder();
{
let pass: GPUComputePassEncoder = encoder.beginComputePass();
pass.setBindGroup(0, this.bind_group);
pass.setPipeline(this.compute_pipeline);
pass.dispatchWorkgroups(this.width/8, this.height/8);
pass.end();
}
//encoder.copyTextureToTexture({texture: this.velocitydest.texture}, {texture: this.velocity.texture}, [this.width, this.height, 1]);

let command_buffer: GPUCommandBuffer = GPU.FinishCommandEncoder(encoder)
return command_buffer;
}

async Run() {
GPU.device.queue.submit([this.GetCommandBuffer()]);
await GPU.device.queue.onSubmittedWorkDone()
}
}
44 changes: 44 additions & 0 deletions src/scripts/fluid/scene/scene.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
struct StagingBuffer {
iMouse: vec2f,
wheel: f32,
iFrame: f32
};

@group(0) @binding(0) var scene : texture_storage_2d_array<rgba8unorm, write>;
@group(0) @binding(1) var density : texture_2d<f32>;

@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
//let iResolution = vec2f(textureDimensions(scene));
//resolution = iResolution.xy;
let p = vec2i(global_id.xy);
let d = textureLoad(density, p, 0)/256.0;
var ch = vec3f(0., 0., 0.);
var translucency = vec4f(0., 0., 0., 1.);

//ch = vec3f(d.g*1., d.g*1., d.g*1.);
//translucency = 1. - d.r*50. - d.g*10.;
//translucency = 1. - d.g*40.;

translucency.a = 1. - d.r*20.;
translucency.r = 0.9;
translucency.g = 0.0;
translucency.b = 0.0;

if (p.y > 508) && (p.x > 408) {
ch = vec3f(0.6, 0.6, 1.);
}
/*
if (p.y < 10) {
ch = vec3f(0.0, 0.3, 0.);
translucency.a = 0.1;
}
if (p.y < 11) {
translucency.a = 0.1;
}
*/

textureStore(scene, p, 0, vec4f(ch, 0.)); // emissive circular harmonics for rgb.
textureStore(scene, p, 1, translucency); // absorption and color of emitted light
}

2 changes: 1 addition & 1 deletion src/scripts/fluid/source/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class Source {
let pass: GPUComputePassEncoder = encoder.beginComputePass();
pass.setBindGroup(0, this.bind_group_a2b);
pass.setPipeline(this.compute_pipeline);
pass.dispatchWorkgroups((this.width)/2, (this.height)/2);
pass.dispatchWorkgroups((this.width)/8, (this.height)/8);
pass.end();
}
encoder.copyTextureToTexture({texture: this.velocitydest.texture}, {texture: this.velocitysrc.texture}, [this.width, this.height, 1]);
Expand Down
15 changes: 4 additions & 11 deletions src/scripts/fluid/source/source.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
var v = textureLoad(velocity_src, pixel_coords, 0);
var d = textureLoad(density_src, pixel_coords, 0);

if (((pixel_coords.x < 3)) || (pixel_coords.x > (dims.x - 2)) || ((pixel_coords.y < 2)) || (pixel_coords.y > (dims.y - 2))) {
if (pixel_coords.x < 3) {
v = vec4<f32>(1., 0., 0., 0.);
}

if (pixel_coords.x < 2) {
var dens = cos(f32(pixel_coords.y)*0.2);
if (dens > 0.6) {
d = vec4<f32>(1.);
if (dens > 0.0) {
d = vec4<f32>(0., dens, 0., 0.);
} else {
d = vec4<f32>(0.);
}
Expand All @@ -47,17 +47,10 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
//let dist = sphere(vec2<f32>(pixel_coords) - mouse_pos, (-mouse_wheel*0.005+0.05));
let dist = sphere(vec2<f32>(pixel_coords) - mouse_pos, -10.*mouse_wheel + 10.);
if (dist < 0) {
//v = vec4<f32>(0.0);
v *= 0.5;
d = vec4<f32>(1.0, 0., 0., 1.);
d.x += 0.01;
}

/*
if (((pixel_coords.x > 250)) && (pixel_coords.x < 260) && ((pixel_coords.y > 250)) && (pixel_coords.y < 260)) {
v = vec4<f32>(0.0);
d = vec4<f32>(1.0, 0., 0., 0.);
}
*/
textureStore(velocity_dest, pixel_coords, v);
textureStore(density_dest, pixel_coords, d);
}
Loading

0 comments on commit 4dcc40d

Please sign in to comment.