Temporal dithering

This commit is contained in:
numzero 2024-11-17 20:09:19 +03:00
parent 30f02e4ff2
commit 92bd2ea9ad
3 changed files with 11 additions and 6 deletions

View File

@ -199,7 +199,7 @@ impl<'a> State<'a> {
required_features: wgpu::Features::PUSH_CONSTANTS required_features: wgpu::Features::PUSH_CONSTANTS
| wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES, | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES,
required_limits: wgpu::Limits { required_limits: wgpu::Limits {
max_push_constant_size: 16, max_push_constant_size: 20,
..wgpu::Limits::default() ..wgpu::Limits::default()
}, },
memory_hints: Default::default(), memory_hints: Default::default(),

View File

@ -7,6 +7,7 @@ var<uniform> camera: CameraUniform;
struct MeshUniform { struct MeshUniform {
color: vec4<f32>, color: vec4<f32>,
seed: u32,
} }
var<push_constant> mesh: MeshUniform; var<push_constant> mesh: MeshUniform;
@ -50,7 +51,7 @@ fn fs_main(in: VertexOutput) -> FragmentOutput {
var y = bitcast<u32>(in.clip_position.y); var y = bitcast<u32>(in.clip_position.y);
var z = bitcast<u32>(in.clip_position.z); var z = bitcast<u32>(in.clip_position.z);
var alpha = in.vertex_color.w; var alpha = in.vertex_color.w;
var seed = hash(hash(hash(x) ^ y) ^ z); var seed = hash(hash(hash(mesh.seed ^ x) ^ y) ^ z);
var mask = 0u; var mask = 0u;
for (var sample = 0u; sample < 8u; sample++) { for (var sample = 0u; sample < 8u; sample++) {
var threshold = f32(hash(seed ^ sample)) / 0x1p32; var threshold = f32(hash(seed ^ sample)) / 0x1p32;

View File

@ -15,6 +15,7 @@ struct Vertex {
#[derive(Copy, Clone, Pod, Zeroable)] #[derive(Copy, Clone, Pod, Zeroable)]
struct PushConsts { struct PushConsts {
pub color: [f32; 4], pub color: [f32; 4],
pub seed: u32,
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -23,15 +24,16 @@ pub struct Attrs {
} }
impl Attrs { impl Attrs {
fn consts(&self) -> PushConsts { fn consts(&self, seed: u32) -> PushConsts {
PushConsts { PushConsts {
color: self.color.to_array(), color: self.color.to_array(),
seed,
} }
} }
} }
pub struct Mesh { pub struct Mesh {
consts: PushConsts, attrs: Attrs,
npoints: u32, npoints: u32,
buf: wgpu::Buffer, buf: wgpu::Buffer,
} }
@ -64,7 +66,7 @@ impl Mesh {
usage: wgpu::BufferUsages::VERTEX, usage: wgpu::BufferUsages::VERTEX,
}); });
Mesh { Mesh {
consts: attrs.consts(), attrs,
npoints: data.len() as u32, npoints: data.len() as u32,
buf, buf,
} }
@ -157,10 +159,12 @@ impl Renderer {
cam_bind: &wgpu::BindGroup, cam_bind: &wgpu::BindGroup,
meshes: impl Iterator<Item = &'a Mesh>, meshes: impl Iterator<Item = &'a Mesh>,
) { ) {
let seed = rand::random();
pass.set_pipeline(&self.pipeline); pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, cam_bind, &[]); pass.set_bind_group(0, cam_bind, &[]);
for mesh in meshes { for mesh in meshes {
pass.set_push_constants(wgpu::ShaderStages::VERTEX, 0, bytes_of(&mesh.consts)); let consts = mesh.attrs.consts(seed);
pass.set_push_constants(wgpu::ShaderStages::VERTEX, 0, bytes_of(&consts));
pass.set_vertex_buffer(0, mesh.buf.slice(..)); pass.set_vertex_buffer(0, mesh.buf.slice(..));
pass.draw(0..mesh.npoints, 0..1); pass.draw(0..mesh.npoints, 0..1);
} }