Compare commits

...

4 Commits

Author SHA1 Message Date
99b8d5eeeb Another kind of interacting layers 2025-01-11 16:19:44 +03:00
6bc2c1f452 2 interacting layers!! 2025-01-11 16:16:47 +03:00
79b63154d7 4 independent layers 2025-01-11 16:06:38 +03:00
c25573db87 Replace textureGathers with textureLoads 2025-01-11 16:00:37 +03:00
4 changed files with 46 additions and 36 deletions

View File

@ -12,11 +12,11 @@ fn on_vertex(@builtin(vertex_index) vi: u32) -> Varying {
} }
@fragment @fragment
fn on_fragment(in: Varying) -> @location(0) u32 { fn on_fragment(in: Varying) -> @location(0) vec4u {
let x = bitcast<u32>(in.screen.x); let x = bitcast<u32>(in.screen.x);
let y = bitcast<u32>(in.screen.y); let y = bitcast<u32>(in.screen.y);
let rand = hash(hash(hash(SEED) ^ hash(x)) ^ hash(y)); let rand = hash(hash(hash(SEED) ^ hash(x)) ^ hash(y));
return rand >> 31; return vec4(rand >> 31, rand >> 30, rand >> 29, rand >> 28) & vec4(1u);
} }
fn hash(key : u32) -> u32 { fn hash(key : u32) -> u32 {

View File

@ -47,7 +47,7 @@ fn main() {
entry_point: None, entry_point: None,
compilation_options: wgpu::PipelineCompilationOptions::default(), compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState { targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::R8Uint, format: wgpu::TextureFormat::Rgba8Uint,
blend: None, blend: None,
write_mask: wgpu::ColorWrites::ALL, write_mask: wgpu::ColorWrites::ALL,
})], })],
@ -80,7 +80,7 @@ fn main() {
entry_point: None, entry_point: None,
compilation_options: wgpu::PipelineCompilationOptions::default(), compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState { targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::R8Uint, format: wgpu::TextureFormat::Rgba8Uint,
blend: None, blend: None,
write_mask: wgpu::ColorWrites::ALL, write_mask: wgpu::ColorWrites::ALL,
})], })],
@ -122,17 +122,6 @@ fn main() {
cache: None, cache: None,
}); });
let state_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: None,
address_mode_u: wgpu::AddressMode::Repeat,
address_mode_v: wgpu::AddressMode::Repeat,
address_mode_w: wgpu::AddressMode::Repeat,
mag_filter: wgpu::FilterMode::Nearest,
min_filter: wgpu::FilterMode::Nearest,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
});
let mut state = None; let mut state = None;
#[allow(deprecated)] #[allow(deprecated)]
event_loop event_loop
@ -166,7 +155,7 @@ fn main() {
mip_level_count: 1, mip_level_count: 1,
sample_count: 1, sample_count: 1,
dimension: wgpu::TextureDimension::D2, dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R8Uint, format: wgpu::TextureFormat::Rgba8Uint,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[], view_formats: &[],
}) })
@ -253,16 +242,10 @@ fn main() {
&device.create_bind_group(&wgpu::BindGroupDescriptor { &device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None, label: None,
layout: &step_pipeline.get_bind_group_layout(0), layout: &step_pipeline.get_bind_group_layout(0),
entries: &[ entries: &[wgpu::BindGroupEntry {
wgpu::BindGroupEntry { binding: 0,
binding: 0, resource: wgpu::BindingResource::TextureView(&state.last),
resource: wgpu::BindingResource::TextureView(&state.last), }],
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&state_sampler),
},
],
}), }),
&[], &[],
); );

View File

@ -15,6 +15,6 @@ fn on_vertex(@builtin(vertex_index) vi: u32) -> Varying {
@fragment @fragment
fn on_fragment(in: Varying) -> @location(0) vec4f { fn on_fragment(in: Varying) -> @location(0) vec4f {
let pos = vec2u(in.screen.xy); let pos = vec2u(in.screen.xy);
let state = textureLoad(field, pos, 0).x; let state = textureLoad(field, pos, 0).xyz;
return vec4f(f32(state)); return vec4(vec3f(state), 1.);
} }

View File

@ -12,16 +12,43 @@ fn on_vertex(@builtin(vertex_index) vi: u32) -> Varying {
} }
@group(0) @binding(0) var field: texture_2d<u32>; @group(0) @binding(0) var field: texture_2d<u32>;
@group(0) @binding(1) var smp: sampler;
@fragment @fragment
fn on_fragment(in: Varying) -> @location(0) u32 { fn on_fragment(in: Varying) -> @location(0) vec4u {
let a = textureGather(0, field, smp, in.uv, vec2(0, 0)); let dim = vec2i(textureDimensions(field));
let b = textureGather(0, field, smp, in.uv, vec2(0, 1)); let center = vec2i(in.screen.xy);
let c = textureGather(0, field, smp, in.uv, vec2(1, 0)); var nb: array<array<vec4u, 3>, 3>;
let d = textureGather(0, field, smp, in.uv, vec2(1, 1)); for (var j = 0; j < 3; j++) {
let state = a.y; for (var i = 0; i < 3; i++) {
let n = a.x + a.z + a.w + b.x + b.y + c.y + c.z + d.y; let off = vec2i(i, j);
let coords = (center + off + dim - 1) % dim;
let px = textureLoad(field, coords, 0);
nb[j][i] = px;
}
}
let state = nb[1][1];
var n = vec4u();
for (var j = 0; j < 3; j++) {
for (var i = 0; i < 3; i++) {
n += nb[i][j];
}
}
n -= state;
let z = daynight(state.z, n.z);
let x = life(state.x, n.x + n.z);
return vec4(x, 0, z, 0u);
}
fn life(state: u32, n: u32) -> u32{
switch (n) {
case 2u: { return state; }
case 3u: { return 1u; }
default: { return 0u; }
}
}
fn daynight(state: u32, n: u32) -> u32{
switch (n) { switch (n) {
case 3u: { return 1u; } case 3u: { return 1u; }
case 4u: { return state; } case 4u: { return state; }