Skip to content

Commit 4dcc40d

Browse files
committed
Separate 2D lighting into modules
1 parent f746d29 commit 4dcc40d

16 files changed

Lines changed: 584 additions & 311 deletions

File tree

src/scripts/fluid/fluid.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {Div} from "./div/div";
99
import {Project} from "./project/project";
1010
import {Source} from "./source/source";
1111
import {GPUAbstractRunner, RunnerType} from "../AbstractGPURunner";
12+
import {LightScene} from "./scene/scene";
13+
import {LightPropagation} from "../modules/light/propagation/light";
14+
import {MonteCarloPathTracing} from "../modules/light/monte_carlo_path_tracing/light";
1215

1316
// Staggered Grid
1417
/*
@@ -43,7 +46,9 @@ export class Fluid extends GPUAbstractRunner {
4346
render: Render;
4447
poisson: Poisson;
4548
div: Div;
46-
project: Project
49+
project: Project;
50+
scene: LightScene;
51+
light: LightPropagation;
4752

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

88+
this.scene = new LightScene(this.density)
89+
await this.scene.Init()
90+
/*
91+
this.light = new LightPropagation(this.scene.emitter)
92+
*/
93+
this.light = new MonteCarloPathTracing(this.scene.emitter, 20)
94+
await this.light.Init()
95+
96+
8397
this.render = new Render(this.density);
8498
//this.render = new Render(this.div.div);
8599
//this.render = new Render(this.velocity);
@@ -108,6 +122,8 @@ export class Fluid extends GPUAbstractRunner {
108122
}
109123

110124
public async Destroy() {
125+
await this.light.Destroy()
126+
await this.scene.Destroy()
111127
await this.project.Destroy()
112128
await this.poisson.Destroy()
113129
await this.div.Destroy()
@@ -121,28 +137,32 @@ export class Fluid extends GPUAbstractRunner {
121137

122138

123139
async Run() {
124-
140+
this.light.Reset()
125141
GPU.device.queue.submit([
126142
this.source.GetCommandBuffer(),
127143
this.transport.GetCommandBuffer(),
128144
this.advect.GetCommandBuffer(),
129145
this.div.GetCommandBuffer(),
130146
this.poisson.GetCommandBuffer(),
131147
this.project.GetCommandBuffer(),
148+
this.scene.GetCommandBuffer(),
149+
//this.light.GetCommandBuffer(),
132150
])
133151
//await GPU.Render(this.transport.texturea);
134152
//await GPU.Render(this.transport.texturea);
135153
//await GPU.Render(this.poisson.pressurea);
136154
}
137155

138156
Render() {
157+
//this.light.Render()
158+
139159
GPU.device.queue.submit([
140160
this.render.GetCommandBuffer()
141161
])
142162
}
143163

144164
async InitVelocity() {
145-
let vel = new Uint16Array(this.width * this.height * 4)
165+
let vel = new Uint16Array(this.width * this.height * 4)
146166
/*
147167
for (let j = 0; j < this.height; j++)
148168
for (let i = 0; i < this.width; i++) {

src/scripts/fluid/scene/scene.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import {GPU} from "../../webgpu/gpu";
2+
import {Texture} from "../../webgpu/texture";
3+
import {Buffer} from "../../webgpu/buffer";
4+
import {Raytrace} from "../../raytrace/raytrace";
5+
import {ShowError} from "../../ui";
6+
import {SDF} from "../../sdf/sdf";
7+
8+
export class LightScene {
9+
width: number
10+
height: number
11+
12+
emitter: Texture
13+
density: Texture
14+
15+
bind_group_layout: GPUBindGroupLayout
16+
bind_group: GPUBindGroup
17+
pipeline_layout: GPUPipelineLayout
18+
compute_pipeline: GPUComputePipeline
19+
shader: GPUProgrammableStage
20+
21+
constructor(texture: Texture) {
22+
this.density = texture
23+
this.width = GPU.viewport.width
24+
this.height = GPU.viewport.height
25+
}
26+
27+
async Destroy() {
28+
this.emitter.destroy()
29+
}
30+
31+
async Init() {
32+
let shader = await GPU.CreateShaderFromURL("scripts/fluid/scene/scene.wgsl")
33+
34+
// 0: color emitter circular harmonics, z-component
35+
// 1: normal vector of the surface
36+
this.emitter = GPU.CreateStorageTextureArray(this.width, this.height, 2, "rgba8unorm")
37+
38+
this.bind_group_layout = GPU.device.createBindGroupLayout({
39+
entries: [{
40+
binding: 0,
41+
storageTexture: {
42+
access: "write-only",
43+
format: this.emitter.format,
44+
viewDimension: "2d-array"
45+
},
46+
visibility: GPUShaderStage.COMPUTE
47+
}, {
48+
binding: 1,
49+
texture: {
50+
sampleType: "unfilterable-float",
51+
},
52+
visibility: GPUShaderStage.COMPUTE
53+
}, ]
54+
});
55+
56+
this.bind_group = GPU.device.createBindGroup({
57+
layout: this.bind_group_layout,
58+
entries: [{
59+
binding: 0,
60+
resource: this.emitter.textureView
61+
}, {
62+
binding: 1,
63+
resource: this.density.textureView
64+
}]
65+
});
66+
67+
this.pipeline_layout = GPU.device.createPipelineLayout({
68+
bindGroupLayouts: [this.bind_group_layout]
69+
});
70+
71+
this.compute_pipeline = GPU.device.createComputePipeline({
72+
layout: this.pipeline_layout,
73+
compute: shader
74+
});
75+
}
76+
77+
GetCommandBuffer() : GPUCommandBuffer {
78+
let encoder: GPUCommandEncoder = GPU.CreateCommandEncoder();
79+
{
80+
let pass: GPUComputePassEncoder = encoder.beginComputePass();
81+
pass.setBindGroup(0, this.bind_group);
82+
pass.setPipeline(this.compute_pipeline);
83+
pass.dispatchWorkgroups(this.width/8, this.height/8);
84+
pass.end();
85+
}
86+
//encoder.copyTextureToTexture({texture: this.velocitydest.texture}, {texture: this.velocity.texture}, [this.width, this.height, 1]);
87+
88+
let command_buffer: GPUCommandBuffer = GPU.FinishCommandEncoder(encoder)
89+
return command_buffer;
90+
}
91+
92+
async Run() {
93+
GPU.device.queue.submit([this.GetCommandBuffer()]);
94+
await GPU.device.queue.onSubmittedWorkDone()
95+
}
96+
}

src/scripts/fluid/scene/scene.wgsl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
struct StagingBuffer {
2+
iMouse: vec2f,
3+
wheel: f32,
4+
iFrame: f32
5+
};
6+
7+
@group(0) @binding(0) var scene : texture_storage_2d_array<rgba8unorm, write>;
8+
@group(0) @binding(1) var density : texture_2d<f32>;
9+
10+
@compute @workgroup_size(8, 8)
11+
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
12+
//let iResolution = vec2f(textureDimensions(scene));
13+
//resolution = iResolution.xy;
14+
let p = vec2i(global_id.xy);
15+
let d = textureLoad(density, p, 0)/256.0;
16+
var ch = vec3f(0., 0., 0.);
17+
var translucency = vec4f(0., 0., 0., 1.);
18+
19+
//ch = vec3f(d.g*1., d.g*1., d.g*1.);
20+
//translucency = 1. - d.r*50. - d.g*10.;
21+
//translucency = 1. - d.g*40.;
22+
23+
translucency.a = 1. - d.r*20.;
24+
translucency.r = 0.9;
25+
translucency.g = 0.0;
26+
translucency.b = 0.0;
27+
28+
if (p.y > 508) && (p.x > 408) {
29+
ch = vec3f(0.6, 0.6, 1.);
30+
}
31+
/*
32+
if (p.y < 10) {
33+
ch = vec3f(0.0, 0.3, 0.);
34+
translucency.a = 0.1;
35+
}
36+
if (p.y < 11) {
37+
translucency.a = 0.1;
38+
}
39+
*/
40+
41+
textureStore(scene, p, 0, vec4f(ch, 0.)); // emissive circular harmonics for rgb.
42+
textureStore(scene, p, 1, translucency); // absorption and color of emitted light
43+
}
44+

src/scripts/fluid/source/source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class Source {
115115
let pass: GPUComputePassEncoder = encoder.beginComputePass();
116116
pass.setBindGroup(0, this.bind_group_a2b);
117117
pass.setPipeline(this.compute_pipeline);
118-
pass.dispatchWorkgroups((this.width)/2, (this.height)/2);
118+
pass.dispatchWorkgroups((this.width)/8, (this.height)/8);
119119
pass.end();
120120
}
121121
encoder.copyTextureToTexture({texture: this.velocitydest.texture}, {texture: this.velocitysrc.texture}, [this.width, this.height, 1]);

src/scripts/fluid/source/source.wgsl

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
2727
var v = textureLoad(velocity_src, pixel_coords, 0);
2828
var d = textureLoad(density_src, pixel_coords, 0);
2929

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

3434
if (pixel_coords.x < 2) {
3535
var dens = cos(f32(pixel_coords.y)*0.2);
36-
if (dens > 0.6) {
37-
d = vec4<f32>(1.);
36+
if (dens > 0.0) {
37+
d = vec4<f32>(0., dens, 0., 0.);
3838
} else {
3939
d = vec4<f32>(0.);
4040
}
@@ -47,17 +47,10 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
4747
//let dist = sphere(vec2<f32>(pixel_coords) - mouse_pos, (-mouse_wheel*0.005+0.05));
4848
let dist = sphere(vec2<f32>(pixel_coords) - mouse_pos, -10.*mouse_wheel + 10.);
4949
if (dist < 0) {
50-
//v = vec4<f32>(0.0);
5150
v *= 0.5;
52-
d = vec4<f32>(1.0, 0., 0., 1.);
51+
d.x += 0.01;
5352
}
5453

55-
/*
56-
if (((pixel_coords.x > 250)) && (pixel_coords.x < 260) && ((pixel_coords.y > 250)) && (pixel_coords.y < 260)) {
57-
v = vec4<f32>(0.0);
58-
d = vec4<f32>(1.0, 0., 0., 0.);
59-
}
60-
*/
6154
textureStore(velocity_dest, pixel_coords, v);
6255
textureStore(density_dest, pixel_coords, d);
6356
}

0 commit comments

Comments
 (0)