-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96e9967
commit d8dd196
Showing
15 changed files
with
811 additions
and
837 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,79 @@ | ||
mod instance_data; | ||
pub use instance_data::RectangleInstance; | ||
|
||
mod rectangle_pipeline; | ||
use crate::wgpu_jumpstart::{Gpu, Instances, RenderPipelineBuilder, Shape, Uniform}; | ||
use crate::TransformUniform; | ||
|
||
pub use instance_data::RectangleInstance; | ||
pub use rectangle_pipeline::RectanglePipeline; | ||
pub struct RectanglePipeline { | ||
render_pipeline: wgpu::RenderPipeline, | ||
|
||
quad: Shape, | ||
|
||
instances: Instances<RectangleInstance>, | ||
} | ||
|
||
impl<'a> RectanglePipeline { | ||
pub fn new(gpu: &Gpu, transform_uniform: &Uniform<TransformUniform>) -> Self { | ||
let vs_module = gpu | ||
.device | ||
.create_shader_module(wgpu::include_spirv!("shader/quad.vert.spv")); | ||
let fs_module = gpu | ||
.device | ||
.create_shader_module(wgpu::include_spirv!("shader/quad.frag.spv")); | ||
|
||
let render_pipeline_layout = | ||
gpu.device | ||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { | ||
label: None, | ||
bind_group_layouts: &[&transform_uniform.bind_group_layout], | ||
push_constant_ranges: &[], | ||
}); | ||
|
||
let ri_attrs = RectangleInstance::attributes(); | ||
|
||
let render_pipeline = RenderPipelineBuilder::new(&render_pipeline_layout, &vs_module) | ||
.fragment_stage(&fs_module) | ||
.vertex_buffers(&[ | ||
Shape::vertex_buffer_descriptor(), | ||
RectangleInstance::desc(&ri_attrs), | ||
]) | ||
.build(&gpu.device); | ||
|
||
let quad = Shape::new_quad(&gpu.device); | ||
let instances = Instances::new(&gpu.device, 100_000); | ||
|
||
Self { | ||
render_pipeline, | ||
|
||
quad, | ||
|
||
instances, | ||
} | ||
} | ||
pub fn render( | ||
&'a self, | ||
transform_uniform: &'a Uniform<TransformUniform>, | ||
render_pass: &mut wgpu::RenderPass<'a>, | ||
) { | ||
render_pass.set_pipeline(&self.render_pipeline); | ||
render_pass.set_bind_group(0, &transform_uniform.bind_group, &[]); | ||
|
||
render_pass.set_vertex_buffer(0, self.quad.vertex_buffer.slice(..)); | ||
render_pass.set_vertex_buffer(1, self.instances.buffer.slice(..)); | ||
|
||
render_pass.set_index_buffer(self.quad.index_buffer.slice(..)); | ||
|
||
render_pass.draw_indexed(0..self.quad.indices_len, 0, 0..self.instances.len()); | ||
} | ||
pub fn update_instance_buffer( | ||
&mut self, | ||
command_encoder: &mut wgpu::CommandEncoder, | ||
device: &wgpu::Device, | ||
instances: Vec<RectangleInstance>, | ||
) { | ||
if self.instances.data != instances { | ||
self.instances.data = instances; | ||
self.instances.update(command_encoder, device); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,75 @@ | ||
mod bg_pipeline; | ||
use crate::wgpu_jumpstart::{Gpu, RenderPipelineBuilder, Shape, Uniform}; | ||
use zerocopy::AsBytes; | ||
|
||
pub use bg_pipeline::BgPipeline; | ||
pub struct BgPipeline { | ||
render_pipeline: wgpu::RenderPipeline, | ||
|
||
fullscreen_quad: Shape, | ||
|
||
time_uniform: Uniform<TimeUniform>, | ||
} | ||
|
||
impl<'a> BgPipeline { | ||
pub fn new(gpu: &Gpu) -> Self { | ||
let vs_module = gpu | ||
.device | ||
.create_shader_module(wgpu::include_spirv!("shader/quad.vert.spv")); | ||
let fs_module = gpu | ||
.device | ||
.create_shader_module(wgpu::include_spirv!("shader/quad.frag.spv")); | ||
|
||
let time_uniform = Uniform::new( | ||
&gpu.device, | ||
TimeUniform::default(), | ||
wgpu::ShaderStage::FRAGMENT, | ||
); | ||
|
||
let render_pipeline_layout = | ||
&gpu.device | ||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { | ||
label: None, | ||
bind_group_layouts: &[&time_uniform.bind_group_layout], | ||
push_constant_ranges: &[], | ||
}); | ||
|
||
let render_pipeline = RenderPipelineBuilder::new(&render_pipeline_layout, &vs_module) | ||
.fragment_stage(&fs_module) | ||
.vertex_buffers(&[Shape::vertex_buffer_descriptor()]) | ||
.build(&gpu.device); | ||
|
||
let fullscreen_quad = Shape::new_fullscreen_quad(&gpu.device); | ||
|
||
Self { | ||
render_pipeline, | ||
|
||
fullscreen_quad, | ||
|
||
time_uniform, | ||
} | ||
} | ||
pub fn render(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { | ||
render_pass.set_pipeline(&self.render_pipeline); | ||
render_pass.set_bind_group(0, &self.time_uniform.bind_group, &[]); | ||
|
||
render_pass.set_vertex_buffer(0, self.fullscreen_quad.vertex_buffer.slice(..)); | ||
|
||
render_pass.set_index_buffer(self.fullscreen_quad.index_buffer.slice(..)); | ||
|
||
render_pass.draw_indexed(0..self.fullscreen_quad.indices_len, 0, 0..1); | ||
} | ||
pub fn update_time(&mut self, gpu: &mut Gpu, time: f32) { | ||
self.time_uniform.data.time = time; | ||
self.time_uniform.update(&mut gpu.encoder, &gpu.device); | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
#[derive(Clone, Copy, AsBytes)] | ||
struct TimeUniform { | ||
time: f32, | ||
} | ||
impl Default for TimeUniform { | ||
fn default() -> Self { | ||
Self { time: 0.0 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.