Skip to content

Commit

Permalink
Simplifications, remove sampler, rename blit to texture_copy
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Dec 12, 2024
1 parent 15cbf21 commit 69a5c0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 54 deletions.
50 changes: 13 additions & 37 deletions crates/egui-wgpu/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ use wgpu::{BindGroupLayout, MultisampleState, Sampler, StoreOp};
/// The texture is required since [`wgpu::TextureUsages::COPY_SRC`] is not an allowed
/// flag for the surface texture on all platforms. This means that anytime we want to
/// capture the frame, we first render it to this texture, and then we can copy it to
/// both the surface texture (via blit) and the buffer, from where we can pull it back
/// both the surface texture (via a render pass) and the buffer (via a texture to buffer copy),
/// from where we can pull it back
/// to the cpu.
pub struct CaptureState {
padding: BufferPadding,
pub texture: wgpu::Texture,
sampler: wgpu::Sampler,
pipeline: wgpu::RenderPipeline,
bind_group: wgpu::BindGroup,
buffer: Option<wgpu::Buffer>,
}

impl CaptureState {
pub fn new(device: &wgpu::Device, surface_texture: &wgpu::Texture) -> Self {
let shader = device.create_shader_module(wgpu::include_wgsl!("blit.wgsl"));
let shader = device.create_shader_module(wgpu::include_wgsl!("texture_copy.wgsl"));

let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("blit"),
label: Some("texture_copy"),
layout: None,
vertex: wgpu::VertexState {
module: &shader,
Expand All @@ -50,24 +50,12 @@ impl CaptureState {

let bind_group_layout = pipeline.get_bind_group_layout(0);

let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("mip"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
});

let (texture, padding, bind_group) =
Self::create_texture(device, surface_texture, &sampler, &bind_group_layout);
Self::create_texture(device, surface_texture, &bind_group_layout);

Self {
padding,
texture,
sampler,
pipeline,
bind_group,
buffer: None,
Expand All @@ -77,7 +65,6 @@ impl CaptureState {
fn create_texture(
device: &wgpu::Device,
surface_texture: &wgpu::Texture,
sampler: &Sampler,
layout: &BindGroupLayout,
) -> (wgpu::Texture, BufferPadding, wgpu::BindGroup) {
let texture = device.create_texture(&wgpu::TextureDescriptor {
Expand All @@ -99,16 +86,10 @@ impl CaptureState {

let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(sampler),
},
],
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&view),
}],
label: None,
});

Expand All @@ -118,12 +99,8 @@ impl CaptureState {
/// Updates the [`CaptureState`] if the size of the surface texture has changed
pub fn update(&mut self, device: &wgpu::Device, texture: &wgpu::Texture) {
if self.texture.size() != texture.size() {
let (new_texture, padding, bind_group) = Self::create_texture(
device,
texture,
&self.sampler,
&self.pipeline.get_bind_group_layout(0),
);
let (new_texture, padding, bind_group) =
Self::create_texture(device, texture, &self.pipeline.get_bind_group_layout(0));
self.texture = new_texture;
self.padding = padding;
self.bind_group = bind_group;
Expand Down Expand Up @@ -180,7 +157,7 @@ impl CaptureState {

{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("blit"),
label: Some("texture_copy"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &output_frame.texture.create_view(&Default::default()),
resolve_target: None,
Expand Down Expand Up @@ -244,7 +221,6 @@ impl CaptureState {
)).ok();
ctx.request_repaint();
});
device.poll(wgpu::Maintain::WaitForSubmissionIndex(id));
}

/// Handles copying from the [`CaptureState`] texture to the surface texture and the cpu
Expand Down Expand Up @@ -290,7 +266,7 @@ impl CaptureState {

{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("blit"),
label: Some("texture_copy"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &output_frame.texture.create_view(&Default::default()),
resolve_target: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
};

var<private> positions: array<vec2f, 3> = array<vec2f, 3>(
vec2f(-1.0, -3.0),
vec2f(-1.0, 1.0),
vec2f(3.0, 1.0)
);

// meant to be called with 3 vertex indices: 0, 1, 2
// draws one large triangle over the clip space like this:
// (the asterisks represent the clip space bounds)
Expand All @@ -24,29 +29,15 @@ struct VertexOutput {
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var result: VertexOutput;
let x = i32(vertex_index) / 2;
let y = i32(vertex_index) & 1;
let tc = vec2<f32>(
f32(x) * 2.0,
f32(y) * 2.0
);
result.position = vec4<f32>(
tc.x * 2.0 - 1.0,
1.0 - tc.y * 2.0,
0.0, 1.0
);
result.tex_coords = tc;
result.position = vec4f(positions[vertex_index], 0.0, 1.0);
return result;
}

@group(0)
@binding(0)
var r_color: texture_2d<f32>;
@group(0)
@binding(1)
var r_sampler: sampler;

@fragment
fn fs_main(vertex: VertexOutput) -> @location(0) vec4<f32> {
return textureSample(r_color, r_sampler, vertex.tex_coords);
return textureLoad(r_color, vec2i(vertex.position.xy), 0);
}

0 comments on commit 69a5c0d

Please sign in to comment.