Add ComputeParticlesTest
Renderer: Add a way to execute commands on the device
This commit is contained in:
committed by
Jérôme Leclercq
parent
9e7b98a017
commit
e34ba8c05d
30
assets/shaders/compute/compute_particle_texture.nzsl
Normal file
30
assets/shaders/compute/compute_particle_texture.nzsl
Normal file
@@ -0,0 +1,30 @@
|
||||
[nzsl_version("1.0")]
|
||||
module Compute.ParticleTexture;
|
||||
|
||||
external
|
||||
{
|
||||
[binding(0)] output_tex: texture2D[f32, writeonly, rgba8]
|
||||
}
|
||||
|
||||
struct Input
|
||||
{
|
||||
[builtin(global_invocation_indices)] global_invocation_id: vec3[u32]
|
||||
}
|
||||
|
||||
[entry(compute)]
|
||||
[workgroup(32, 32, 1)]
|
||||
fn main(input: Input)
|
||||
{
|
||||
let indices = vec2[i32](input.global_invocation_id.xy);
|
||||
|
||||
let uv = vec2[f32](indices) / vec2[f32](256.0, 256.0);
|
||||
uv -= vec2[f32](0.5, 0.5);
|
||||
|
||||
let outputColor = vec4[f32]
|
||||
(
|
||||
(pow(1.0 - length(uv), 20.0)).xxx,
|
||||
1.0
|
||||
);
|
||||
|
||||
output_tex.Write(indices, outputColor);
|
||||
}
|
||||
50
assets/shaders/compute/compute_particles.nzsl
Normal file
50
assets/shaders/compute/compute_particles.nzsl
Normal file
@@ -0,0 +1,50 @@
|
||||
[nzsl_version("1.0")]
|
||||
module Compute.Particles;
|
||||
|
||||
[layout(std140)]
|
||||
struct Particle
|
||||
{
|
||||
color: vec3[f32],
|
||||
position: vec2[f32],
|
||||
velocity: vec2[f32]
|
||||
}
|
||||
|
||||
[layout(std140)]
|
||||
struct ParticleData
|
||||
{
|
||||
particle_count: u32,
|
||||
particles: dyn_array[Particle]
|
||||
}
|
||||
|
||||
[layout(std140)]
|
||||
struct SceneData
|
||||
{
|
||||
deltaTime: f32,
|
||||
mousePos: vec2[f32]
|
||||
}
|
||||
|
||||
external
|
||||
{
|
||||
[binding(0)] data: storage[ParticleData],
|
||||
[binding(1)] sceneData: uniform[SceneData]
|
||||
}
|
||||
|
||||
struct Input
|
||||
{
|
||||
[builtin(global_invocation_indices)] indices: vec3[u32]
|
||||
}
|
||||
|
||||
[entry(compute)]
|
||||
[workgroup(64, 1, 1)]
|
||||
fn main(input: Input)
|
||||
{
|
||||
let index = input.indices.x;
|
||||
if (index >= data.particle_count)
|
||||
return;
|
||||
|
||||
let attract_pos = sceneData.mousePos;
|
||||
let dist = length(attract_pos - data.particles[index].position);
|
||||
data.particles[index].velocity += 10000.0 * (attract_pos - data.particles[index].position) * sceneData.deltaTime / (dist * dist);
|
||||
|
||||
data.particles[index].position += data.particles[index].velocity * sceneData.deltaTime;
|
||||
}
|
||||
Reference in New Issue
Block a user