-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
584 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.