Skip to content

Commit

Permalink
Replace PipelineBuilder with trait
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Dec 21, 2023
1 parent a92a881 commit d529840
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 57 deletions.
17 changes: 10 additions & 7 deletions neothesia-core/src/render/background_animation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@ impl BgPipeline {

let target = wgpu_jumpstart::default_color_target_state(gpu.texture_format);

let render_pipeline =
RenderPipelineBuilder::new(render_pipeline_layout, "vs_main", &shader)
.fragment("fs_main", &shader, &[Some(target)])
.vertex_buffers(&[Shape::layout()])
.build(&gpu.device);
let render_pipeline = wgpu::RenderPipelineDescriptor::builder(
render_pipeline_layout,
wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[Shape::layout()],
},
)
.fragment("fs_main", &shader, &[Some(target)])
.create_render_pipeline(&gpu.device);

let fullscreen_quad = Shape::new_fullscreen_quad(&gpu.device);

Self {
render_pipeline,

fullscreen_quad,

time_uniform,
}
}
Expand Down
17 changes: 10 additions & 7 deletions neothesia-core/src/render/quad/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,23 @@ impl<'a> QuadPipeline {

let target = wgpu_jumpstart::default_color_target_state(gpu.texture_format);

let render_pipeline =
RenderPipelineBuilder::new(&render_pipeline_layout, "vs_main", &shader)
.fragment("fs_main", &shader, &[Some(target)])
.vertex_buffers(&[Shape::layout(), QuadInstance::layout(&ri_attrs)])
.build(&gpu.device);
let render_pipeline = wgpu::RenderPipelineDescriptor::builder(
&render_pipeline_layout,
wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[Shape::layout(), QuadInstance::layout(&ri_attrs)],
},
)
.fragment("fs_main", &shader, &[Some(target)])
.create_render_pipeline(&gpu.device);

let quad = Shape::new_quad(&gpu.device);
let instances = Instances::new(&gpu.device, 100_000);

Self {
render_pipeline,

quad,

instances,
}
}
Expand Down
18 changes: 10 additions & 8 deletions neothesia-core/src/render/waterfall/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,25 @@ impl<'a> WaterfallPipeline {

let target = wgpu_jumpstart::default_color_target_state(gpu.texture_format);

let render_pipeline =
RenderPipelineBuilder::new(render_pipeline_layout, "vs_main", &shader)
.fragment("fs_main", &shader, &[Some(target)])
.vertex_buffers(&[Shape::layout(), NoteInstance::layout(&ni_attrs)])
.build(&gpu.device);
let render_pipeline = wgpu::RenderPipelineDescriptor::builder(
render_pipeline_layout,
wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[Shape::layout(), NoteInstance::layout(&ni_attrs)],
},
)
.fragment("fs_main", &shader, &[Some(target)])
.create_render_pipeline(&gpu.device);

let quad = Shape::new_quad(&gpu.device);

let instances = Instances::new(&gpu.device, notes_count);

Self {
render_pipeline,

quad,

instances,

time_uniform,
}
}
Expand Down
62 changes: 27 additions & 35 deletions wgpu-jumpstart/src/render_pipeline_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,50 @@ pub fn default_color_target_state(texture_format: wgpu::TextureFormat) -> wgpu::
}
}

pub struct RenderPipelineBuilder<'a> {
render_pipeline_descriptor: wgpu::RenderPipelineDescriptor<'a>,
pub trait RenderPipelineBuilder<'a> {
fn builder(layout: &'a wgpu::PipelineLayout, vertex: wgpu::VertexState<'a>) -> Self;
fn fragment(
self,
entry_point: &'a str,
fragment_module: &'a wgpu::ShaderModule,
targets: &'a [Option<wgpu::ColorTargetState>],
) -> Self;
fn create_render_pipeline(&self, device: &wgpu::Device) -> wgpu::RenderPipeline;
}

impl<'a> RenderPipelineBuilder<'a> {
pub fn new(
layout: &'a wgpu::PipelineLayout,
entry_point: &'a str,
vertex_module: &'a wgpu::ShaderModule,
) -> Self {
Self {
render_pipeline_descriptor: wgpu::RenderPipelineDescriptor {
label: None,
layout: Some(layout),
vertex: wgpu::VertexState {
module: vertex_module,
entry_point,
buffers: &[],
},
fragment: None,
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
impl<'a> RenderPipelineBuilder<'a> for wgpu::RenderPipelineDescriptor<'a> {
fn builder(layout: &'a wgpu::PipelineLayout, vertex: wgpu::VertexState<'a>) -> Self {
wgpu::RenderPipelineDescriptor {
label: None,
layout: Some(layout),
vertex,
fragment: None,
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
}
}

pub fn fragment(
fn fragment(
mut self,
entry_point: &'a str,
fragment_module: &'a wgpu::ShaderModule,
targets: &'a [Option<wgpu::ColorTargetState>],
) -> Self {
self.render_pipeline_descriptor.fragment = Some(wgpu::FragmentState {
self.fragment = Some(wgpu::FragmentState {
module: fragment_module,
entry_point,
targets,
});
self
}

pub fn vertex_buffers(mut self, vertex_buffers: &'a [wgpu::VertexBufferLayout]) -> Self {
self.render_pipeline_descriptor.vertex.buffers = vertex_buffers;
self
}

pub fn build(self, device: &wgpu::Device) -> wgpu::RenderPipeline {
device.create_render_pipeline(&self.render_pipeline_descriptor)
fn create_render_pipeline(&self, device: &wgpu::Device) -> wgpu::RenderPipeline {
device.create_render_pipeline(self)
}
}

0 comments on commit d529840

Please sign in to comment.