Split tracer and dataset

This commit is contained in:
numzero 2024-12-22 00:19:50 +03:00
parent 1ff0608b65
commit 497123c830
2 changed files with 25 additions and 40 deletions

View File

@ -1,6 +1,6 @@
use std::error::Error;
use trace::{Sphere, Tracer, Vertex};
use trace::{Sphere, Tracer, TracerData, Vertex};
use winit::{
event::{Event, WindowEvent},
event_loop::EventLoop,
@ -43,8 +43,10 @@ fn main() {
sphere_count: 2,
},
);
tracer.set_spheres(
let mut data = TracerData::new(
&device,
&tracer,
&[
Sphere {
center: [0.0, 0.0, 0.0],
@ -110,7 +112,7 @@ fn main() {
occlusion_query_set: None,
timestamp_writes: None,
});
tracer.render(&mut render_pass);
tracer.render(&mut render_pass, &data);
drop(render_pass);
queue.submit(std::iter::once(encoder.finish()));
output.present();

View File

@ -33,11 +33,13 @@ pub struct Vertex {
pub struct Tracer {
view_buf: wgpu::Buffer,
params_buf: wgpu::Buffer,
spheres_buf: wgpu::Buffer,
bindings: wgpu::BindGroup,
pipeline: wgpu::RenderPipeline,
}
pub struct TracerData {
bindings: wgpu::BindGroup,
}
static SHADER: &str = include_str!("trace.wgsl");
impl Tracer {
@ -54,12 +56,6 @@ impl Tracer {
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let spheres_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: mem::size_of::<Sphere>() as u64,
usage: wgpu::BufferUsages::STORAGE,
mapped_at_creation: false,
});
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(SHADER.into()),
@ -116,25 +112,9 @@ impl Tracer {
multiview: None,
cache: None,
});
let bindings = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &pipeline.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: params_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: spheres_buf.as_entire_binding(),
},
],
});
Self {
view_buf,
params_buf,
spheres_buf,
bindings,
pipeline,
}
}
@ -147,32 +127,35 @@ impl Tracer {
queue.write_buffer(&self.view_buf, 0, bytes_of(vertices));
}
pub fn set_spheres(&mut self, device: &wgpu::Device, spheres: &[Sphere]) {
self.spheres_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
pub fn render(&self, pass: &mut wgpu::RenderPass, data: &TracerData) {
pass.set_pipeline(&self.pipeline);
pass.set_vertex_buffer(0, self.view_buf.slice(..));
pass.set_bind_group(0, &data.bindings, &[]);
pass.draw(0..4, 0..1);
}
}
impl TracerData {
pub fn new(device: &wgpu::Device, tracer: &Tracer, spheres: &[Sphere]) -> Self {
let spheres_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: None,
contents: cast_slice(spheres),
usage: wgpu::BufferUsages::STORAGE,
});
self.bindings = device.create_bind_group(&wgpu::BindGroupDescriptor {
let bindings = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &self.pipeline.get_bind_group_layout(0),
layout: &tracer.pipeline.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.params_buf.as_entire_binding(),
resource: tracer.params_buf.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.spheres_buf.as_entire_binding(),
resource: spheres_buf.as_entire_binding(),
},
],
});
}
pub fn render(&self, pass: &mut wgpu::RenderPass) {
pass.set_pipeline(&self.pipeline);
pass.set_vertex_buffer(0, self.view_buf.slice(..));
pass.set_bind_group(0, &self.bindings, &[]);
pass.draw(0..4, 0..1);
Self { bindings }
}
}