time now push constant to shader

This commit is contained in:
2024-10-03 15:50:53 -04:00
parent ce4ce5c4d0
commit 788764ab5d
5 changed files with 28 additions and 27 deletions

View File

@@ -23,6 +23,7 @@ pub struct Renderer<'a> {
voxel_pipeline: VoxelPipeline,
timer: GPUTimer,
camera: Camera,
start: std::time::Instant,
}
impl<'a> Renderer<'a> {
@@ -49,12 +50,14 @@ impl<'a> Renderer<'a> {
let (device, queue) = pollster::block_on(adapter.request_device(
&wgpu::DeviceDescriptor {
label: None,
required_features: wgpu::Features::TIMESTAMP_QUERY
required_features: wgpu::Features::PUSH_CONSTANTS
| wgpu::Features::TIMESTAMP_QUERY
| wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS
| wgpu::Features::TIMESTAMP_QUERY_INSIDE_PASSES,
required_limits: wgpu::Limits {
max_storage_buffer_binding_size: buf_size,
max_buffer_size: buf_size as u64,
max_push_constant_size: 4,
..Default::default()
},
memory_hints: wgpu::MemoryHints::default(),
@@ -106,6 +109,7 @@ impl<'a> Renderer<'a> {
device,
config,
queue,
start: std::time::Instant::now(),
}
}
@@ -124,6 +128,7 @@ impl<'a> Renderer<'a> {
}
pub fn draw(&mut self) {
let time = self.start.elapsed();
let mut encoder = std::mem::replace(&mut self.encoder, Self::create_encoder(&self.device));
let output = self.surface.get_current_texture().unwrap();
let view = output
@@ -135,7 +140,7 @@ impl<'a> Renderer<'a> {
timestamp_writes: None,
});
self.timer.start_compute(&mut compute_pass, 0);
self.voxel_pipeline.compute(&mut compute_pass);
self.voxel_pipeline.compute(&mut compute_pass, time);
self.timer.stop_compute(&mut compute_pass, 0);
drop(compute_pass);

View File

@@ -124,7 +124,10 @@ impl Layout {
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("voxel compute"),
bind_group_layouts: &[&compute_bind_layout],
push_constant_ranges: &[],
push_constant_ranges: &[wgpu::PushConstantRange {
range: 0..4,
stages: wgpu::ShaderStages::COMPUTE,
}],
});
Self {
view,

View File

@@ -14,10 +14,7 @@ use bevy_ecs::entity::Entity;
pub use color::*;
use layout::Layout;
use nalgebra::{Transform3, Translation3, Vector2};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use std::collections::HashMap;
use wgpu::include_wgsl;
use {chunk::Chunk, view::View};
@@ -171,9 +168,11 @@ impl VoxelPipeline {
pub const WORKGROUP_SIZE: u32 = 8;
pub fn compute(&self, pass: &mut wgpu::ComputePass) {
pub fn compute(&self, pass: &mut wgpu::ComputePass, time: std::time::Duration) {
let time = time.as_millis() as u32;
pass.set_pipeline(&self.compute_pipeline);
pass.set_bind_group(0, &self.compute_bind_group, &[]);
pass.set_push_constants(0, &time.to_le_bytes());
let buf = &self.layout.texture.texture;
let x = (buf.width() - 1) / Self::WORKGROUP_SIZE + 1;
let y = (buf.height() - 1) / Self::WORKGROUP_SIZE + 1;

View File

@@ -9,6 +9,8 @@ var<storage, read> global_lights: array<GlobalLight>;
@group(0) @binding(4)
var output: texture_storage_2d<rgba8unorm, write>;
var<push_constant> time: u32;
struct GlobalLight {
dir: vec3<f32>,
};
@@ -226,22 +228,6 @@ fn cast_ray(
return RayResult(hits, depth);
}
fn trace_chunk(
offset: u32,
inv_dir_bits: u32,
t: f32, t_mult: f32, inc_t: vec3<f32>,
min_adj: vec3<f32>
) {
}
fn dir_to_vec(bits: u32) -> vec3<u32> {
return vec3<u32>(bits >> 2, (bits & 2) >> 1, bits & 1);
}
fn vec_to_dir(vec: vec3<u32>) -> u32 {
return vec.x * 4 + vec.y * 2 + vec.z * 1;
}
fn min_alpha(id: u32) -> f32 {
switch id {
case 0u: {return 0.0;}
@@ -274,7 +260,8 @@ fn shade(id: u32, pos: vec3<f32>, normal: vec3<f32>, dir_view: vec3<f32>, dist:
case 3u: {
let fog = min(dist / 64.0, 1.0);
let a = 0.5;
let rgb = vec3<f32>(0.5, 0.5, 1.0) * (random2 * 0.2 + 0.8);
let rand = (sin((f32(time) / 2000.0 + random) * 6.28) + 1.0) / 2.0;
let rgb = vec3<f32>(0.5, 0.5, 1.0) * (rand * 0.2 + 0.8);
color = vec4<f32>(rgb * (1.0 - fog * a), a + fog * (1.0 - a));
}
default: {}
@@ -291,6 +278,14 @@ fn shade(id: u32, pos: vec3<f32>, normal: vec3<f32>, dir_view: vec3<f32>, dist:
return vec4<f32>(new_color * new_a, new_a);
}
fn dir_to_vec(bits: u32) -> vec3<u32> {
return vec3<u32>(bits >> 2, (bits & 2) >> 1, bits & 1);
}
fn vec_to_dir(vec: vec3<u32>) -> u32 {
return vec.x * 4 + vec.y * 2 + vec.z * 1;
}
fn random(pos: vec3<f32>) -> f32 {
return fract(sin(dot(pos, vec3<f32>(12.9898, 78.233, 25.1279))) * 43758.5453123);
}

View File

@@ -20,8 +20,7 @@ impl Timer {
self.start = Instant::now();
}
pub fn stop(&mut self) {
let duration = Instant::now() - self.start;
self.durs[self.pos] = Some(duration);
self.durs[self.pos] = Some(self.start.elapsed());
self.times[self.pos] = Some(self.start);
self.pos = (self.pos + 1) % self.times.len();
}