Split tracer and dataset
This commit is contained in:
parent
cb741c5360
commit
7fb503cf13
|
|
@ -1,6 +1,6 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use trace::{Sphere, Tracer, Vertex};
|
use trace::{Sphere, Tracer, TracerData, Vertex};
|
||||||
use winit::{
|
use winit::{
|
||||||
event::{Event, WindowEvent},
|
event::{Event, WindowEvent},
|
||||||
event_loop::EventLoop,
|
event_loop::EventLoop,
|
||||||
|
|
@ -43,8 +43,10 @@ fn main() {
|
||||||
sphere_count: 2,
|
sphere_count: 2,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
tracer.set_spheres(
|
|
||||||
|
let mut data = TracerData::new(
|
||||||
&device,
|
&device,
|
||||||
|
&tracer,
|
||||||
&[
|
&[
|
||||||
Sphere {
|
Sphere {
|
||||||
center: [0.0, 0.0, 0.0],
|
center: [0.0, 0.0, 0.0],
|
||||||
|
|
@ -110,7 +112,7 @@ fn main() {
|
||||||
occlusion_query_set: None,
|
occlusion_query_set: None,
|
||||||
timestamp_writes: None,
|
timestamp_writes: None,
|
||||||
});
|
});
|
||||||
tracer.render(&mut render_pass);
|
tracer.render(&mut render_pass, &data);
|
||||||
drop(render_pass);
|
drop(render_pass);
|
||||||
queue.submit(std::iter::once(encoder.finish()));
|
queue.submit(std::iter::once(encoder.finish()));
|
||||||
output.present();
|
output.present();
|
||||||
|
|
|
||||||
57
src/trace.rs
57
src/trace.rs
|
|
@ -33,11 +33,13 @@ pub struct Vertex {
|
||||||
pub struct Tracer {
|
pub struct Tracer {
|
||||||
view_buf: wgpu::Buffer,
|
view_buf: wgpu::Buffer,
|
||||||
params_buf: wgpu::Buffer,
|
params_buf: wgpu::Buffer,
|
||||||
spheres_buf: wgpu::Buffer,
|
|
||||||
bindings: wgpu::BindGroup,
|
|
||||||
pipeline: wgpu::RenderPipeline,
|
pipeline: wgpu::RenderPipeline,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct TracerData {
|
||||||
|
bindings: wgpu::BindGroup,
|
||||||
|
}
|
||||||
|
|
||||||
static SHADER: &str = include_str!("trace.wgsl");
|
static SHADER: &str = include_str!("trace.wgsl");
|
||||||
|
|
||||||
impl Tracer {
|
impl Tracer {
|
||||||
|
|
@ -54,12 +56,6 @@ impl Tracer {
|
||||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
mapped_at_creation: false,
|
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 {
|
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
source: wgpu::ShaderSource::Wgsl(SHADER.into()),
|
source: wgpu::ShaderSource::Wgsl(SHADER.into()),
|
||||||
|
|
@ -116,25 +112,9 @@ impl Tracer {
|
||||||
multiview: None,
|
multiview: None,
|
||||||
cache: 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 {
|
Self {
|
||||||
view_buf,
|
view_buf,
|
||||||
params_buf,
|
params_buf,
|
||||||
spheres_buf,
|
|
||||||
bindings,
|
|
||||||
pipeline,
|
pipeline,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -147,32 +127,35 @@ impl Tracer {
|
||||||
queue.write_buffer(&self.view_buf, 0, bytes_of(vertices));
|
queue.write_buffer(&self.view_buf, 0, bytes_of(vertices));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_spheres(&mut self, device: &wgpu::Device, spheres: &[Sphere]) {
|
pub fn render(&self, pass: &mut wgpu::RenderPass, data: &TracerData) {
|
||||||
self.spheres_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
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,
|
label: None,
|
||||||
contents: cast_slice(spheres),
|
contents: cast_slice(spheres),
|
||||||
usage: wgpu::BufferUsages::STORAGE,
|
usage: wgpu::BufferUsages::STORAGE,
|
||||||
});
|
});
|
||||||
self.bindings = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
let bindings = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
layout: &self.pipeline.get_bind_group_layout(0),
|
layout: &tracer.pipeline.get_bind_group_layout(0),
|
||||||
entries: &[
|
entries: &[
|
||||||
wgpu::BindGroupEntry {
|
wgpu::BindGroupEntry {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
resource: self.params_buf.as_entire_binding(),
|
resource: tracer.params_buf.as_entire_binding(),
|
||||||
},
|
},
|
||||||
wgpu::BindGroupEntry {
|
wgpu::BindGroupEntry {
|
||||||
binding: 1,
|
binding: 1,
|
||||||
resource: self.spheres_buf.as_entire_binding(),
|
resource: spheres_buf.as_entire_binding(),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
}
|
Self { bindings }
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user