-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlib.rs
More file actions
213 lines (183 loc) · 6.74 KB
/
lib.rs
File metadata and controls
213 lines (183 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use bevy::prelude::*;
use bevy::render::RenderApp;
use bevy::render::render_resource::{Texture, TextureFormat};
use bevy::render::renderer::RenderDevice;
use bevy_cuda::{CudaBuffer, CudaContext};
use processing_core::app_mut;
use processing_core::error::{ProcessingError, Result};
use processing_render::graphics::view_target;
use processing_render::image::{Image, gpu_image, pixel_size};
#[derive(Component)]
pub struct CudaImageBuffer {
pub buffer: CudaBuffer,
pub width: u32,
pub height: u32,
pub texture_format: TextureFormat,
}
pub struct CudaPlugin;
impl Plugin for CudaPlugin {
fn build(&self, _app: &mut App) {}
fn finish(&self, app: &mut App) {
let render_app = app.sub_app(RenderApp);
let render_device = render_app.world().resource::<RenderDevice>();
let wgpu_device = render_device.wgpu_device();
match CudaContext::new(wgpu_device, 0) {
Ok(ctx) => {
app.insert_resource(ctx);
}
Err(e) => {
warn!("CUDA not available, GPU interop disabled: {e}");
}
}
}
}
fn cuda_ctx(world: &World) -> Result<&CudaContext> {
world
.get_resource::<CudaContext>()
.ok_or(ProcessingError::CudaError("CUDA not available".into()))
}
fn resolve_texture(app: &mut App, entity: Entity) -> Result<(Texture, TextureFormat, u32, u32)> {
if app.world().get::<Image>(entity).is_some() {
let texture = gpu_image(app, entity)?.texture.clone();
let p_image = app.world().get::<Image>(entity).unwrap();
return Ok((
texture,
p_image.texture_format,
p_image.size.width,
p_image.size.height,
));
}
if let Ok(vt) = view_target(app, entity) {
let texture = vt.main_texture().clone();
let fmt = vt.main_texture_format();
let size = texture.size();
return Ok((texture, fmt, size.width, size.height));
}
Err(ProcessingError::ImageNotFound)
}
pub fn cuda_export(entity: Entity) -> Result<()> {
app_mut(|app| {
let (texture, texture_format, width, height) = resolve_texture(app, entity)?;
let px_size = pixel_size(texture_format)?;
let buffer_size = (width as u64) * (height as u64) * (px_size as u64);
let existing = app.world().get::<CudaImageBuffer>(entity);
let needs_alloc = existing.is_none_or(|buf| buf.buffer.size() != buffer_size);
if needs_alloc {
let cuda_ctx = cuda_ctx(app.world())?;
let buffer = cuda_ctx
.create_buffer(buffer_size)
.map_err(|e| ProcessingError::CudaError(format!("Buffer creation failed: {e}")))?;
app.world_mut().entity_mut(entity).insert(CudaImageBuffer {
buffer,
width,
height,
texture_format,
});
}
let world = app.world();
let cuda_buf = world.get::<CudaImageBuffer>(entity).unwrap();
let cuda_ctx = cuda_ctx(world)?;
cuda_ctx
.copy_texture_to_buffer(&texture, &cuda_buf.buffer, width, height, texture_format)
.map_err(|e| {
ProcessingError::CudaError(format!("Texture-to-buffer copy failed: {e}"))
})?;
Ok(())
})
}
pub fn cuda_import(entity: Entity, src_device_ptr: u64, byte_size: u64) -> Result<()> {
app_mut(|app| {
let (texture, texture_format, width, height) = resolve_texture(app, entity)?;
let existing = app.world().get::<CudaImageBuffer>(entity);
let needs_alloc = existing.is_none_or(|buf| buf.buffer.size() != byte_size);
if needs_alloc {
let cuda_ctx = cuda_ctx(app.world())?;
let buffer = cuda_ctx
.create_buffer(byte_size)
.map_err(|e| ProcessingError::CudaError(format!("Buffer creation failed: {e}")))?;
app.world_mut().entity_mut(entity).insert(CudaImageBuffer {
buffer,
width,
height,
texture_format,
});
}
let world = app.world();
let cuda_buf = world.get::<CudaImageBuffer>(entity).unwrap();
let cuda_ctx = cuda_ctx(world)?;
// wait for work (i.e. python) to be done with the buffer before we read from it
cuda_ctx
.synchronize()
.map_err(|e| ProcessingError::CudaError(format!("synchronize failed: {e}")))?;
cuda_buf
.buffer
.copy_from_device_ptr(src_device_ptr, byte_size)
.map_err(|e| ProcessingError::CudaError(format!("memcpy_dtod failed: {e}")))?;
cuda_ctx
.copy_buffer_to_texture(&cuda_buf.buffer, &texture, width, height, texture_format)
.map_err(|e| {
ProcessingError::CudaError(format!("Buffer-to-texture copy failed: {e}"))
})?;
Ok(())
})
}
pub fn cuda_write_back(entity: Entity) -> Result<()> {
app_mut(|app| {
let (texture, _, _, _) = resolve_texture(app, entity)?;
let cuda_buf = app
.world()
.get::<CudaImageBuffer>(entity)
.ok_or(ProcessingError::ImageNotFound)?;
let cuda_ctx = cuda_ctx(app.world())?;
cuda_ctx
.copy_buffer_to_texture(
&cuda_buf.buffer,
&texture,
cuda_buf.width,
cuda_buf.height,
cuda_buf.texture_format,
)
.map_err(|e| {
ProcessingError::CudaError(format!("Buffer-to-texture copy failed: {e}"))
})?;
Ok(())
})
}
pub struct CudaBufferInfo {
pub device_ptr: u64,
pub width: u32,
pub height: u32,
pub texture_format: TextureFormat,
}
pub fn cuda_buffer(entity: Entity) -> Result<CudaBufferInfo> {
app_mut(|app| {
let cuda_buf = app
.world()
.get::<CudaImageBuffer>(entity)
.ok_or(ProcessingError::ImageNotFound)?;
Ok(CudaBufferInfo {
device_ptr: cuda_buf.buffer.device_ptr(),
width: cuda_buf.width,
height: cuda_buf.height,
texture_format: cuda_buf.texture_format,
})
})
}
pub fn typestr_for_format(format: TextureFormat) -> Result<&'static str> {
match format {
TextureFormat::Rgba8Unorm | TextureFormat::Rgba8UnormSrgb => Ok("|u1"),
TextureFormat::Rgba16Float => Ok("<f2"),
TextureFormat::Rgba32Float => Ok("<f4"),
_ => Err(ProcessingError::UnsupportedTextureFormat),
}
}
pub fn elem_size_for_typestr(typestr: &str) -> Result<usize> {
match typestr {
"|u1" => Ok(1),
"<f2" => Ok(2),
"<f4" => Ok(4),
_ => Err(ProcessingError::CudaError(format!(
"unsupported typestr: {typestr}"
))),
}
}