Skip to content

Commit cf0e9f9

Browse files
authored
Adding copy_texture_to_buffer and copy_texture_to_texture (#1236)
* Adding copy_texture_to_buffer and copy_texture_to_texture * Adding CopyTextureToTexture and CopyTextureToBuffer in CommandQueue
1 parent 6a0116e commit cf0e9f9

File tree

4 files changed

+239
-0
lines changed

4 files changed

+239
-0
lines changed

crates/bevy_render/src/render_graph/command.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ pub enum Command {
2323
destination_mip_level: u32,
2424
size: Extent3d,
2525
},
26+
CopyTextureToTexture {
27+
source_texture: TextureId,
28+
source_origin: [u32; 3],
29+
source_mip_level: u32,
30+
destination_texture: TextureId,
31+
destination_origin: [u32; 3],
32+
destination_mip_level: u32,
33+
size: Extent3d,
34+
},
35+
CopyTextureToBuffer {
36+
source_texture: TextureId,
37+
source_origin: [u32; 3],
38+
source_mip_level: u32,
39+
destination_buffer: BufferId,
40+
destination_offset: u64,
41+
destination_bytes_per_row: u32,
42+
size: Extent3d,
43+
},
2644
// TODO: Frees probably don't need to be queued?
2745
FreeBuffer(BufferId),
2846
}
@@ -77,6 +95,50 @@ impl CommandQueue {
7795
});
7896
}
7997

98+
#[allow(clippy::too_many_arguments)]
99+
pub fn copy_texture_to_buffer(
100+
&mut self,
101+
source_texture: TextureId,
102+
source_origin: [u32; 3],
103+
source_mip_level: u32,
104+
destination_buffer: BufferId,
105+
destination_offset: u64,
106+
destination_bytes_per_row: u32,
107+
size: Extent3d,
108+
) {
109+
self.push(Command::CopyTextureToBuffer {
110+
source_texture,
111+
source_origin,
112+
source_mip_level,
113+
destination_buffer,
114+
destination_offset,
115+
destination_bytes_per_row,
116+
size,
117+
})
118+
}
119+
120+
#[allow(clippy::too_many_arguments)]
121+
pub fn copy_texture_to_texture(
122+
&mut self,
123+
source_texture: TextureId,
124+
source_origin: [u32; 3],
125+
source_mip_level: u32,
126+
destination_texture: TextureId,
127+
destination_origin: [u32; 3],
128+
destination_mip_level: u32,
129+
size: Extent3d,
130+
) {
131+
self.push(Command::CopyTextureToTexture {
132+
source_texture,
133+
source_origin,
134+
source_mip_level,
135+
destination_texture,
136+
destination_origin,
137+
destination_mip_level,
138+
size,
139+
})
140+
}
141+
80142
pub fn free_buffer(&mut self, buffer: BufferId) {
81143
self.push(Command::FreeBuffer(buffer));
82144
}
@@ -118,6 +180,40 @@ impl CommandQueue {
118180
destination_mip_level,
119181
size,
120182
),
183+
Command::CopyTextureToTexture {
184+
source_texture,
185+
source_origin,
186+
source_mip_level,
187+
destination_texture,
188+
destination_origin,
189+
destination_mip_level,
190+
size,
191+
} => render_context.copy_texture_to_texture(
192+
source_texture,
193+
source_origin,
194+
source_mip_level,
195+
destination_texture,
196+
destination_origin,
197+
destination_mip_level,
198+
size,
199+
),
200+
Command::CopyTextureToBuffer {
201+
source_texture,
202+
source_origin,
203+
source_mip_level,
204+
destination_buffer,
205+
destination_offset,
206+
destination_bytes_per_row,
207+
size,
208+
} => render_context.copy_texture_to_buffer(
209+
source_texture,
210+
source_origin,
211+
source_mip_level,
212+
destination_buffer,
213+
destination_offset,
214+
destination_bytes_per_row,
215+
size,
216+
),
121217
Command::FreeBuffer(buffer) => render_context.resources().remove_buffer(buffer),
122218
}
123219
}

crates/bevy_render/src/renderer/render_context.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@ pub trait RenderContext {
2727
destination_mip_level: u32,
2828
size: Extent3d,
2929
);
30+
#[allow(clippy::too_many_arguments)]
31+
fn copy_texture_to_buffer(
32+
&mut self,
33+
source_texture: TextureId,
34+
source_origin: [u32; 3],
35+
source_mip_level: u32,
36+
destination_buffer: BufferId,
37+
destination_offset: u64,
38+
destination_bytes_per_row: u32,
39+
size: Extent3d,
40+
);
41+
#[allow(clippy::too_many_arguments)]
42+
fn copy_texture_to_texture(
43+
&mut self,
44+
source_texture: TextureId,
45+
source_origin: [u32; 3],
46+
source_mip_level: u32,
47+
destination_texture: TextureId,
48+
destination_origin: [u32; 3],
49+
destination_mip_level: u32,
50+
size: Extent3d,
51+
);
3052
fn begin_pass(
3153
&mut self,
3254
pass_descriptor: &PassDescriptor,

crates/bevy_wgpu/src/renderer/wgpu_render_context.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,50 @@ impl RenderContext for WgpuRenderContext {
114114
)
115115
}
116116

117+
fn copy_texture_to_buffer(
118+
&mut self,
119+
source_texture: TextureId,
120+
source_origin: [u32; 3],
121+
source_mip_level: u32,
122+
destination_buffer: BufferId,
123+
destination_offset: u64,
124+
destination_bytes_per_row: u32,
125+
size: Extent3d,
126+
) {
127+
self.render_resource_context.copy_texture_to_buffer(
128+
self.command_encoder.get_or_create(&self.device),
129+
source_texture,
130+
source_origin,
131+
source_mip_level,
132+
destination_buffer,
133+
destination_offset,
134+
destination_bytes_per_row,
135+
size,
136+
)
137+
}
138+
139+
fn copy_texture_to_texture(
140+
&mut self,
141+
source_texture: TextureId,
142+
source_origin: [u32; 3],
143+
source_mip_level: u32,
144+
destination_texture: TextureId,
145+
destination_origin: [u32; 3],
146+
destination_mip_level: u32,
147+
size: Extent3d,
148+
) {
149+
self.render_resource_context.copy_texture_to_texture(
150+
self.command_encoder.get_or_create(&self.device),
151+
source_texture,
152+
source_origin,
153+
source_mip_level,
154+
destination_texture,
155+
destination_origin,
156+
destination_mip_level,
157+
size,
158+
)
159+
}
160+
117161
fn resources(&self) -> &dyn RenderResourceContext {
118162
&self.render_resource_context
119163
}

crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,83 @@ impl WgpuRenderResourceContext {
6565
);
6666
}
6767

68+
#[allow(clippy::too_many_arguments)]
69+
pub fn copy_texture_to_texture(
70+
&self,
71+
command_encoder: &mut wgpu::CommandEncoder,
72+
source_texture: TextureId,
73+
source_origin: [u32; 3], // TODO: replace with math type
74+
source_mip_level: u32,
75+
destination_texture: TextureId,
76+
destination_origin: [u32; 3], // TODO: replace with math type
77+
destination_mip_level: u32,
78+
size: Extent3d,
79+
) {
80+
let textures = self.resources.textures.read();
81+
let source = textures.get(&source_texture).unwrap();
82+
let destination = textures.get(&destination_texture).unwrap();
83+
command_encoder.copy_texture_to_texture(
84+
wgpu::TextureCopyView {
85+
texture: source,
86+
mip_level: source_mip_level,
87+
origin: wgpu::Origin3d {
88+
x: source_origin[0],
89+
y: source_origin[1],
90+
z: source_origin[2],
91+
},
92+
},
93+
wgpu::TextureCopyView {
94+
texture: destination,
95+
mip_level: destination_mip_level,
96+
origin: wgpu::Origin3d {
97+
x: destination_origin[0],
98+
y: destination_origin[1],
99+
z: destination_origin[2],
100+
},
101+
},
102+
size.wgpu_into(),
103+
)
104+
}
105+
106+
#[allow(clippy::too_many_arguments)]
107+
pub fn copy_texture_to_buffer(
108+
&self,
109+
command_encoder: &mut wgpu::CommandEncoder,
110+
source_texture: TextureId,
111+
source_origin: [u32; 3], // TODO: replace with math type
112+
source_mip_level: u32,
113+
destination_buffer: BufferId,
114+
destination_offset: u64,
115+
destination_bytes_per_row: u32,
116+
size: Extent3d,
117+
) {
118+
let buffers = self.resources.buffers.read();
119+
let textures = self.resources.textures.read();
120+
121+
let source = textures.get(&source_texture).unwrap();
122+
let destination = buffers.get(&destination_buffer).unwrap();
123+
command_encoder.copy_texture_to_buffer(
124+
wgpu::TextureCopyView {
125+
texture: source,
126+
mip_level: source_mip_level,
127+
origin: wgpu::Origin3d {
128+
x: source_origin[0],
129+
y: source_origin[1],
130+
z: source_origin[2],
131+
},
132+
},
133+
wgpu::BufferCopyView {
134+
buffer: destination,
135+
layout: wgpu::TextureDataLayout {
136+
offset: destination_offset,
137+
bytes_per_row: destination_bytes_per_row,
138+
rows_per_image: size.height,
139+
},
140+
},
141+
size.wgpu_into(),
142+
);
143+
}
144+
68145
#[allow(clippy::too_many_arguments)]
69146
pub fn copy_buffer_to_texture(
70147
&self,

0 commit comments

Comments
 (0)