|
| 1 | +use std::cell::RefCell; |
| 2 | + |
1 | 3 | /// Capture a texture to a raw RGBA buffer |
2 | 4 | pub fn capture_texture( |
3 | 5 | rs: &super::RenderState, |
@@ -114,43 +116,51 @@ pub fn capture_texture( |
114 | 116 | ], |
115 | 117 | }); |
116 | 118 |
|
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: ©_shader, |
125 | | - entry_point: "vs_main", |
126 | | - buffers: &[], |
127 | | - compilation_options: Default::default(), |
128 | | - }, |
129 | | - fragment: Some(FragmentState { |
130 | | - module: ©_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 | + }) |
154 | 164 | }); |
155 | 165 |
|
156 | 166 | // Copy the original texture to the RGBA8 texture using the render pipeline |
|
0 commit comments