Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render: Small improvements to render crates #19574

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions render/naga-agal/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,18 +923,13 @@ impl<'a> NagaBuilder<'a> {
(source.swizzle >> 4) & 0b11,
(source.swizzle >> 6) & 0b11,
];
let swizzle_components: [SwizzleComponent; 4] = swizzle_flags
.into_iter()
.map(|flag| match flag {
0b00 => SwizzleComponent::X,
0b01 => SwizzleComponent::Y,
0b10 => SwizzleComponent::Z,
0b11 => SwizzleComponent::W,
_ => unreachable!(),
})
.collect::<Vec<_>>()
.try_into()
.unwrap();
let swizzle_components = swizzle_flags.map(|flag| match flag {
0b00 => SwizzleComponent::X,
0b01 => SwizzleComponent::Y,
0b10 => SwizzleComponent::Z,
0b11 => SwizzleComponent::W,
_ => unreachable!(),
});

Ok(self.evaluate_expr(Expression::Swizzle {
size: output,
Expand Down
10 changes: 5 additions & 5 deletions render/wgpu/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ impl<T: RenderTarget + 'static> RenderBackend for WgpuRenderBackend<T> {
);

let handle = BitmapHandle(Arc::new(Texture {
texture: Arc::new(texture),
texture,
bind_linear: Default::default(),
bind_nearest: Default::default(),
copy_count: Cell::new(0),
Expand Down Expand Up @@ -912,7 +912,7 @@ impl<T: RenderTarget + 'static> RenderBackend for WgpuRenderBackend<T> {
| wgpu::TextureUsages::COPY_SRC,
});
BitmapHandle(Arc::new(Texture {
texture: Arc::new(texture),
texture,
bind_linear: Default::default(),
bind_nearest: Default::default(),
copy_count: Cell::new(0),
Expand Down Expand Up @@ -1072,7 +1072,7 @@ impl<T: RenderTarget + 'static> RenderBackend for WgpuRenderBackend<T> {
| wgpu::TextureUsages::COPY_SRC,
});
Ok(BitmapHandle(Arc::new(Texture {
texture: Arc::new(texture),
texture,
bind_linear: Default::default(),
bind_nearest: Default::default(),
copy_count: Cell::new(0),
Expand Down Expand Up @@ -1170,9 +1170,9 @@ pub enum RenderTargetMode {
// we will blend with the previous contents of the texture.
// This is used in `render_offscreen`, as we need to blend with the previous
// contents of our `BitmapData` texture
FreshWithTexture(Arc<wgpu::Texture>),
FreshWithTexture(wgpu::Texture),
// Use the provided texture as our frame buffer, and clear it with the given color.
ExistingWithColor(Arc<wgpu::Texture>, wgpu::Color),
ExistingWithColor(wgpu::Texture, wgpu::Color),
}

impl RenderTargetMode {
Expand Down
10 changes: 5 additions & 5 deletions render/wgpu/src/context3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl WgpuContext3D {
BitmapHandle(Arc::new(Texture {
bind_linear: Default::default(),
bind_nearest: Default::default(),
texture: Arc::new(dummy_texture),
texture: dummy_texture,
copy_count: Cell::new(0),
}))
};
Expand Down Expand Up @@ -669,13 +669,13 @@ impl Context3D for WgpuContext3D {
// which is what the Stage rendering code expects. In multisample mode,
// this is our resolve texture.
self.back_buffer_raw_texture_handle = BitmapHandle(Arc::new(Texture {
texture: Arc::new(back_buffer_resolve_texture.unwrap()),
texture: back_buffer_resolve_texture.unwrap(),
bind_linear: Default::default(),
bind_nearest: Default::default(),
copy_count: Cell::new(0),
}));
self.front_buffer_raw_texture_handle = BitmapHandle(Arc::new(Texture {
texture: Arc::new(front_buffer_resolve_texture.unwrap()),
texture: front_buffer_resolve_texture.unwrap(),
bind_linear: Default::default(),
bind_nearest: Default::default(),
copy_count: Cell::new(0),
Expand All @@ -685,13 +685,13 @@ impl Context3D for WgpuContext3D {
// so our main texture gets used as the raw texture handle.

self.back_buffer_raw_texture_handle = BitmapHandle(Arc::new(Texture {
texture: Arc::new(back_buffer_texture),
texture: back_buffer_texture,
bind_linear: Default::default(),
bind_nearest: Default::default(),
copy_count: Cell::new(0),
}));
self.front_buffer_raw_texture_handle = BitmapHandle(Arc::new(Texture {
texture: Arc::new(front_buffer_texture),
texture: front_buffer_texture,
bind_linear: Default::default(),
bind_nearest: Default::default(),
copy_count: Cell::new(0),
Expand Down
168 changes: 82 additions & 86 deletions render/wgpu/src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub struct Descriptors {
pub bitmap_samplers: BitmapSamplers,
pub bind_layouts: BindLayouts,
pub quad: Quad,
copy_pipeline: Mutex<FnvHashMap<(u32, wgpu::TextureFormat), Arc<wgpu::RenderPipeline>>>,
copy_srgb_pipeline: Mutex<FnvHashMap<(u32, wgpu::TextureFormat), Arc<wgpu::RenderPipeline>>>,
copy_pipeline: Mutex<FnvHashMap<(u32, wgpu::TextureFormat), wgpu::RenderPipeline>>,
copy_srgb_pipeline: Mutex<FnvHashMap<(u32, wgpu::TextureFormat), wgpu::RenderPipeline>>,
pub shaders: Shaders,
pipelines: Mutex<FnvHashMap<(u32, wgpu::TextureFormat), Arc<Pipelines>>>,
pub filters: Filters,
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Descriptors {
&self,
format: wgpu::TextureFormat,
msaa_sample_count: u32,
) -> Arc<wgpu::RenderPipeline> {
) -> wgpu::RenderPipeline {
let mut pipelines = self
.copy_srgb_pipeline
.lock()
Expand All @@ -91,48 +91,46 @@ impl Descriptors {
],
push_constant_ranges: &[],
});
Arc::new(
self.device
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: create_debug_label!("Copy sRGB pipeline").as_deref(),
layout: Some(copy_texture_pipeline_layout),
vertex: wgpu::VertexState {
module: &self.shaders.copy_srgb_shader,
entry_point: Some("main_vertex"),
buffers: &VERTEX_BUFFERS_DESCRIPTION_POS,
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &self.shaders.copy_srgb_shader,
entry_point: Some("main_fragment"),
targets: &[Some(wgpu::ColorTargetState {
format,
// All of our blending has been done by now, so we want
// to overwrite the target pixels without any blending
blend: Some(wgpu::BlendState::REPLACE),
write_mask: Default::default(),
})],
compilation_options: Default::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
polygon_mode: wgpu::PolygonMode::default(),
unclipped_depth: false,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: msaa_sample_count,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
cache: None,
self.device
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: create_debug_label!("Copy sRGB pipeline").as_deref(),
layout: Some(copy_texture_pipeline_layout),
vertex: wgpu::VertexState {
module: &self.shaders.copy_srgb_shader,
entry_point: Some("main_vertex"),
buffers: &VERTEX_BUFFERS_DESCRIPTION_POS,
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &self.shaders.copy_srgb_shader,
entry_point: Some("main_fragment"),
targets: &[Some(wgpu::ColorTargetState {
format,
// All of our blending has been done by now, so we want
// to overwrite the target pixels without any blending
blend: Some(wgpu::BlendState::REPLACE),
write_mask: Default::default(),
})],
compilation_options: Default::default(),
}),
)
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
polygon_mode: wgpu::PolygonMode::default(),
unclipped_depth: false,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: msaa_sample_count,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
cache: None,
})
})
.clone()
}
Expand All @@ -141,7 +139,7 @@ impl Descriptors {
&self,
format: wgpu::TextureFormat,
msaa_sample_count: u32,
) -> Arc<wgpu::RenderPipeline> {
) -> wgpu::RenderPipeline {
let mut pipelines = self
.copy_pipeline
.lock()
Expand All @@ -161,48 +159,46 @@ impl Descriptors {
],
push_constant_ranges: &[],
});
Arc::new(
self.device
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: create_debug_label!("Copy pipeline").as_deref(),
layout: Some(copy_texture_pipeline_layout),
vertex: wgpu::VertexState {
module: &self.shaders.copy_shader,
entry_point: Some("main_vertex"),
buffers: &VERTEX_BUFFERS_DESCRIPTION_POS,
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &self.shaders.copy_shader,
entry_point: Some("main_fragment"),
targets: &[Some(wgpu::ColorTargetState {
format,
// All of our blending has been done by now, so we want
// to overwrite the target pixels without any blending
blend: Some(wgpu::BlendState::REPLACE),
write_mask: Default::default(),
})],
compilation_options: Default::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
polygon_mode: wgpu::PolygonMode::default(),
unclipped_depth: false,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: msaa_sample_count,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
cache: None,
self.device
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: create_debug_label!("Copy pipeline").as_deref(),
layout: Some(copy_texture_pipeline_layout),
vertex: wgpu::VertexState {
module: &self.shaders.copy_shader,
entry_point: Some("main_vertex"),
buffers: &VERTEX_BUFFERS_DESCRIPTION_POS,
compilation_options: Default::default(),
},
fragment: Some(wgpu::FragmentState {
module: &self.shaders.copy_shader,
entry_point: Some("main_fragment"),
targets: &[Some(wgpu::ColorTargetState {
format,
// All of our blending has been done by now, so we want
// to overwrite the target pixels without any blending
blend: Some(wgpu::BlendState::REPLACE),
write_mask: Default::default(),
})],
compilation_options: Default::default(),
}),
)
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
polygon_mode: wgpu::PolygonMode::default(),
unclipped_depth: false,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: msaa_sample_count,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
cache: None,
})
})
.clone()
}
Expand Down
2 changes: 1 addition & 1 deletion render/wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl QueueSyncHandle {

#[derive(Debug)]
pub struct Texture {
pub(crate) texture: Arc<wgpu::Texture>,
pub(crate) texture: wgpu::Texture,
bind_linear: OnceCell<BitmapBinds>,
bind_nearest: OnceCell<BitmapBinds>,
copy_count: Cell<u8>,
Expand Down
18 changes: 6 additions & 12 deletions render/wgpu/src/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,11 @@ impl ShapePipeline {
stencilless: wgpu::RenderPipeline,
mut f: impl FnMut(MaskState) -> wgpu::RenderPipeline,
) -> Self {
let mask_array: [wgpu::RenderPipeline; MaskState::LENGTH] = (0..MaskState::LENGTH)
.map(|mask_enum| {
let mask_array: [wgpu::RenderPipeline; MaskState::LENGTH] =
std::array::from_fn(|mask_enum| {
let mask_state = MaskState::from_usize(mask_enum);
f(mask_state)
})
.collect::<Vec<_>>()
.try_into()
.unwrap();
});
ShapePipeline {
pipelines: EnumMap::from_array(mask_array),
stencilless,
Expand Down Expand Up @@ -162,8 +159,8 @@ impl Pipelines {
&bind_layouts.bitmap,
];

let bitmap_pipelines: [ShapePipeline; TrivialBlend::LENGTH] = (0..TrivialBlend::LENGTH)
.map(|blend| {
let bitmap_pipelines: [ShapePipeline; TrivialBlend::LENGTH] =
std::array::from_fn(|blend| {
let blend = TrivialBlend::from_usize(blend);
let name = format!("Bitmap ({blend:?})");
create_shape_pipeline(
Expand All @@ -178,10 +175,7 @@ impl Pipelines {
&[],
PrimitiveTopology::TriangleList,
)
})
.collect::<Vec<_>>()
.try_into()
.unwrap();
});

let bitmap_opaque_pipeline_layout_label =
create_debug_label!("Opaque bitmap pipeline layout");
Expand Down
Loading