Skip to content

Commit 77cb5cc

Browse files
committed
texture: Cache blit shader module
1 parent f2f8cb2 commit 77cb5cc

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

src/texture/capture.rs

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::cell::RefCell;
2+
13
/// Capture a texture to a raw RGBA buffer
24
pub fn capture_texture(
35
rs: &super::RenderState,
@@ -114,43 +116,51 @@ pub fn capture_texture(
114116
],
115117
});
116118

117-
let copy_shader = device.create_shader_module(include_wgsl!("../gui/shaders/copy.wgsl"));
118-
119-
let render_pipeline = device.create_render_pipeline(&RenderPipelineDescriptor {
120-
label: Some("Render Pipeline"),
121-
layout: Some(&pipeline_layout),
122-
multiview: None,
123-
vertex: VertexState {
124-
module: &copy_shader,
125-
entry_point: "vs_main",
126-
buffers: &[],
127-
compilation_options: Default::default(),
128-
},
129-
fragment: Some(FragmentState {
130-
module: &copy_shader,
131-
entry_point: "fs_main",
132-
targets: &[Some(ColorTargetState {
133-
format: TextureFormat::Rgba8UnormSrgb,
134-
blend: Some(BlendState::PREMULTIPLIED_ALPHA_BLENDING),
135-
write_mask: ColorWrites::all(),
136-
})],
137-
compilation_options: Default::default(),
138-
}),
139-
primitive: PrimitiveState {
140-
topology: PrimitiveTopology::TriangleList,
141-
strip_index_format: None,
142-
front_face: FrontFace::Cw,
143-
cull_mode: Some(Face::Back),
144-
polygon_mode: PolygonMode::Fill,
145-
conservative: false,
146-
unclipped_depth: false,
147-
},
148-
depth_stencil: None,
149-
multisample: MultisampleState {
150-
count: 1,
151-
mask: !0,
152-
alpha_to_coverage_enabled: false,
153-
},
119+
thread_local! {
120+
static COPY_SHADER: RefCell<Option<eframe::wgpu::ShaderModule>> = const { RefCell::new(None) };
121+
}
122+
// let copy_shader = device.create_shader_module(include_wgsl!("../gui/shaders/copy.wgsl"));
123+
let render_pipeline = COPY_SHADER.with_borrow_mut(|s| {
124+
let copy_shader = s.get_or_insert_with(|| {
125+
device.create_shader_module(include_wgsl!("../gui/shaders/copy.wgsl"))
126+
});
127+
128+
device.create_render_pipeline(&RenderPipelineDescriptor {
129+
label: Some("Render Pipeline"),
130+
layout: Some(&pipeline_layout),
131+
multiview: None,
132+
vertex: VertexState {
133+
module: copy_shader,
134+
entry_point: "vs_main",
135+
buffers: &[],
136+
compilation_options: Default::default(),
137+
},
138+
fragment: Some(FragmentState {
139+
module: copy_shader,
140+
entry_point: "fs_main",
141+
targets: &[Some(ColorTargetState {
142+
format: TextureFormat::Rgba8UnormSrgb,
143+
blend: Some(BlendState::PREMULTIPLIED_ALPHA_BLENDING),
144+
write_mask: ColorWrites::all(),
145+
})],
146+
compilation_options: Default::default(),
147+
}),
148+
primitive: PrimitiveState {
149+
topology: PrimitiveTopology::TriangleList,
150+
strip_index_format: None,
151+
front_face: FrontFace::Cw,
152+
cull_mode: Some(Face::Back),
153+
polygon_mode: PolygonMode::Fill,
154+
conservative: false,
155+
unclipped_depth: false,
156+
},
157+
depth_stencil: None,
158+
multisample: MultisampleState {
159+
count: 1,
160+
mask: !0,
161+
alpha_to_coverage_enabled: false,
162+
},
163+
})
154164
});
155165

156166
// Copy the original texture to the RGBA8 texture using the render pipeline

0 commit comments

Comments
 (0)