Skip to content

Commit

Permalink
rt(wgpu): reduce unnecessary arc clones
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Nov 20, 2024
1 parent 9910638 commit 8dce191
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 108 deletions.
3 changes: 1 addition & 2 deletions librashader-runtime-wgpu/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::ops::{Deref, DerefMut};
use std::sync::Arc;

pub struct WgpuStagedBuffer {
buffer: wgpu::Buffer,
Expand All @@ -8,7 +7,7 @@ pub struct WgpuStagedBuffer {

impl WgpuStagedBuffer {
pub fn new(
device: &Arc<wgpu::Device>,
device: &wgpu::Device,
usage: wgpu::BufferUsages,
size: wgpu::BufferAddress,
label: wgpu::Label<'static>,
Expand Down
24 changes: 12 additions & 12 deletions librashader-runtime-wgpu/src/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub(crate) struct FilterCommon {
pub samplers: SamplerSet,
pub config: RuntimeParameters,
pub(crate) draw_quad: DrawQuad,
device: Arc<Device>,
pub(crate) device: Arc<Device>,
pub(crate) queue: Arc<wgpu::Queue>,
}

Expand Down Expand Up @@ -184,15 +184,15 @@ impl FilterChainWgpu {

// initialize passes
let filters = Self::init_passes(
Arc::clone(&device),
&device,
passes,
&semantics,
options.and_then(|o| o.adapter_info.as_ref()),
disable_cache,
)?;

let samplers = SamplerSet::new(&device);
let mut mipmapper = MipmapGen::new(Arc::clone(&device));
let mut mipmapper = MipmapGen::new(&device);
let luts = FilterChainWgpu::load_luts(
&device,
&queue,
Expand All @@ -204,7 +204,7 @@ impl FilterChainWgpu {
//
let framebuffer_gen = || {
Ok::<_, error::FilterChainError>(OwnedImage::new(
Arc::clone(&device),
&device,
Size::new(1, 1),
1,
TextureFormat::Bgra8Unorm,
Expand Down Expand Up @@ -286,22 +286,22 @@ impl FilterChainWgpu {
let _old_back = std::mem::replace(
&mut back,
OwnedImage::new(
Arc::clone(&self.common.device),
&self.common.device,
input.size().into(),
1,
input.format().into(),
),
);
}

back.copy_from(cmd, input);
back.copy_from(&self.common.device, cmd, input);

self.history_framebuffers.push_front(back)
}
}

fn init_passes(
device: Arc<Device>,
device: &wgpu::Device,
passes: Vec<ShaderPassMeta>,
semantics: &ShaderSemantics,
adapter_info: Option<&wgpu::AdapterInfo>,
Expand Down Expand Up @@ -354,7 +354,7 @@ impl FilterChainWgpu {
};

let graphics_pipeline = WgpuGraphicsPipeline::new(
Arc::clone(&device),
&device,
&wgsl,
&reflection,
render_pass_format.unwrap_or(TextureFormat::Rgba8Unorm),
Expand All @@ -363,7 +363,6 @@ impl FilterChainWgpu {
);

Ok(FilterPass {
device: Arc::clone(&device),
reflection,
uniform_storage,
uniform_bindings,
Expand Down Expand Up @@ -458,7 +457,7 @@ impl FilterChainWgpu {
&mut self.output_framebuffers,
&mut self.feedback_framebuffers,
passes,
&(),
&self.common.device,
Some(&mut |index: usize,
pass: &FilterPass,
output: &OwnedImage,
Expand Down Expand Up @@ -506,7 +505,7 @@ impl FilterChainWgpu {
FilterMode::Nearest,
);

target.generate_mipmaps(cmd, &mut self.mipmapper, &sampler);
target.generate_mipmaps(&self.common.device, cmd, &mut self.mipmapper, &sampler);
}

source = self.common.output_textures[index].clone().unwrap();
Expand All @@ -519,7 +518,8 @@ impl FilterChainWgpu {
let index = passes_len - 1;
if !pass.graphics_pipeline.has_format(viewport.output.format) {
// need to recompile
pass.graphics_pipeline.recompile(viewport.output.format);
pass.graphics_pipeline
.recompile(&self.common.device, viewport.output.format);
}

source.filter_mode = pass.meta.filter;
Expand Down
11 changes: 5 additions & 6 deletions librashader-runtime-wgpu/src/filter_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ use std::sync::Arc;
use wgpu::{BindGroupDescriptor, BindGroupEntry, BindingResource, BufferBinding, ShaderStages};

pub struct FilterPass {
pub device: Arc<wgpu::Device>,
pub reflection: ShaderReflection,
pub(crate) uniform_storage: UniformStorage<
NoUniformBinder,
Option<()>,
WgpuStagedBuffer,
WgpuStagedBuffer,
Arc<wgpu::Device>,
wgpu::Device,
>,
pub uniform_bindings: FastHashMap<UniformBinding, MemberOffset>,
pub source: ShaderSource,
Expand All @@ -56,7 +55,7 @@ impl BindSemantics<NoUniformBinder, Option<()>, WgpuStagedBuffer, WgpuStagedBuff
&'a mut FastHashMap<u32, WgpuArcBinding<wgpu::TextureView>>,
&'a mut FastHashMap<u32, WgpuArcBinding<wgpu::Sampler>>,
);
type DeviceContext = Arc<wgpu::Device>;
type DeviceContext = wgpu::Device;
type UniformOffset = MemberOffset;

#[inline(always)]
Expand Down Expand Up @@ -162,13 +161,13 @@ impl FilterPass {
}
}

let main_bind_group = self.device.create_bind_group(&BindGroupDescriptor {
let main_bind_group = parent.device.create_bind_group(&BindGroupDescriptor {
label: Some("librashader main bind group"),
layout: &self.graphics_pipeline.layout.main_bind_group_layout,
entries: &main_heap_array,
});

let sampler_bind_group = self.device.create_bind_group(&BindGroupDescriptor {
let sampler_bind_group = parent.device.create_bind_group(&BindGroupDescriptor {
label: Some("librashader sampler bind group"),
layout: &self.graphics_pipeline.layout.sampler_bind_group_layout,
entries: &sampler_heap_array,
Expand Down Expand Up @@ -216,7 +215,7 @@ impl FilterPass {
sampler_heap: &'a mut FastHashMap<u32, WgpuArcBinding<wgpu::Sampler>>,
) {
Self::bind_semantics(
&self.device,
&parent.device,
&parent.samplers,
&mut self.uniform_storage,
&mut (main_heap, sampler_heap),
Expand Down
125 changes: 62 additions & 63 deletions librashader-runtime-wgpu/src/graphics_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use librashader_runtime::quad::VertexInput;
use librashader_runtime::render_target::RenderTarget;
use std::borrow::Cow;
use std::convert::Infallible;
use std::sync::Arc;
use wgpu::{
BindGroupLayout, BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingType,
BufferBindingType, BufferSize, CommandEncoder, Operations, PipelineLayout, PushConstantRange,
Expand All @@ -32,14 +31,13 @@ pub struct PipelineLayoutObjects {
vertex_entry_name: String,
vertex: ShaderModule,
fragment: ShaderModule,
device: Arc<wgpu::Device>,
}

impl PipelineLayoutObjects {
pub fn new(
reflection: &ShaderReflection,
shader_assembly: &ShaderCompilerOutput<String, NagaWgslContext>,
device: Arc<wgpu::Device>,
device: &wgpu::Device,
) -> Self {
let vertex = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("vertex"),
Expand Down Expand Up @@ -147,76 +145,75 @@ impl PipelineLayoutObjects {
vertex_entry_name: shader_assembly.context.vertex.entry_points[0].name.clone(),
vertex,
fragment,
device,
}
}

pub fn create_pipeline(
&self,
device: &wgpu::Device,
framebuffer_format: TextureFormat,
cache: Option<&wgpu::PipelineCache>,
) -> wgpu::RenderPipeline {
self.device
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render Pipeline"),
layout: Some(&self.layout),
vertex: wgpu::VertexState {
module: &self.vertex,
entry_point: Some(&self.vertex_entry_name),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[VertexBufferLayout {
array_stride: std::mem::size_of::<VertexInput>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x4,
offset: bytemuck::offset_of!(VertexInput, position)
as wgpu::BufferAddress,
shader_location: 0,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: bytemuck::offset_of!(VertexInput, texcoord)
as wgpu::BufferAddress,
shader_location: 1,
},
],
}],
},
fragment: Some(wgpu::FragmentState {
module: &self.fragment,
entry_point: Some(&self.fragment_entry_name),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format: framebuffer_format,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleStrip,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
cache,
})
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render Pipeline"),
layout: Some(&self.layout),
vertex: wgpu::VertexState {
module: &self.vertex,
entry_point: Some(&self.vertex_entry_name),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[VertexBufferLayout {
array_stride: std::mem::size_of::<VertexInput>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x4,
offset: bytemuck::offset_of!(VertexInput, position)
as wgpu::BufferAddress,
shader_location: 0,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: bytemuck::offset_of!(VertexInput, texcoord)
as wgpu::BufferAddress,
shader_location: 1,
},
],
}],
},
fragment: Some(wgpu::FragmentState {
module: &self.fragment,
entry_point: Some(&self.fragment_entry_name),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format: framebuffer_format,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleStrip,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
cache,
})
}
}

impl WgpuGraphicsPipeline {
pub fn new(
device: Arc<wgpu::Device>,
device: &wgpu::Device,
shader_assembly: &ShaderCompilerOutput<String, NagaWgslContext>,
reflection: &ShaderReflection,
render_pass_format: TextureFormat,
Expand Down Expand Up @@ -256,7 +253,7 @@ impl WgpuGraphicsPipeline {
let mut render_pipelines = FastHashMap::default();
render_pipelines.insert(
render_pass_format,
layout.create_pipeline(render_pass_format, cache.as_ref()),
layout.create_pipeline(device, render_pass_format, cache.as_ref()),
);
Self {
layout,
Expand All @@ -269,8 +266,10 @@ impl WgpuGraphicsPipeline {
self.render_pipelines.contains_key(&format)
}

pub fn recompile(&mut self, format: TextureFormat) {
let render_pipeline = self.layout.create_pipeline(format, self.cache.as_ref());
pub fn recompile(&mut self, device: &wgpu::Device, format: TextureFormat) {
let render_pipeline = self
.layout
.create_pipeline(device, format, self.cache.as_ref());
self.render_pipelines.insert(format, render_pipeline);
}

Expand Down
1 change: 1 addition & 0 deletions librashader-runtime-wgpu/src/luts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl LutTexture {

if config.mipmap {
mipmapper.generate_mipmaps(
device,
cmd,
&texture,
&*sampler_set.get(
Expand Down
Loading

0 comments on commit 8dce191

Please sign in to comment.