From 497123c830b637597cd82aed66ac63509955c32b Mon Sep 17 00:00:00 2001 From: numzero Date: Sun, 22 Dec 2024 00:19:50 +0300 Subject: [PATCH] Split tracer and dataset --- src/main.rs | 8 +++++--- src/trace.rs | 57 ++++++++++++++++++---------------------------------- 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/main.rs b/src/main.rs index 327d6a5..97b5347 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); diff --git a/src/trace.rs b/src/trace.rs index 67ced82..1e9b80d 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -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::() 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 } } }