Skip to content

Commit 017d5da

Browse files
committed
Feat: remove the need for &mut self in GpuBox and GpuBuffer
1 parent fae0692 commit 017d5da

File tree

5 files changed

+24
-44
lines changed

5 files changed

+24
-44
lines changed

crates/cust/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ Notable changes to this project will be documented in this file.
88
at some point, but now we reconsidered that it may be the wrong choice.
99
- Renamed `DBox` -> `DeviceBox`.
1010
- Fixed some doctests that were using old APIs.
11+
- Remove `GpuBox::as_device_ptr_mut` and `GpuBuffer::as_device_ptr_mut`.
12+
- Change `GpuBox::as_device_ptr` and `GpuBuffer::as_device_ptr` to take `&self` instead of `&mut self`.

crates/cust/src/memory/mod.rs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,13 @@ use core::num::*;
9494
/// A trait describing a generic buffer that can be accessed from the GPU. This could be either a [`UnifiedBuffer`]
9595
/// or a regular [`DeviceBuffer`].
9696
pub trait GpuBuffer<T: DeviceCopy>: private::Sealed {
97-
/// Obtains a device pointer to this buffer, not requiring `&mut self`. This means
98-
/// the buffer must be used only for immutable reads, and must not be written to.
99-
unsafe fn as_device_ptr(&self) -> DevicePointer<T>;
100-
fn as_device_ptr_mut(&mut self) -> DevicePointer<T>;
97+
fn as_device_ptr(&self) -> DevicePointer<T>;
10198
fn len(&self) -> usize;
10299
}
103100

104101
impl<T: DeviceCopy> GpuBuffer<T> for DeviceBuffer<T> {
105-
unsafe fn as_device_ptr(&self) -> DevicePointer<T> {
106-
DevicePointer::wrap((**self).as_ptr() as *mut _)
107-
}
108-
109-
fn as_device_ptr_mut(&mut self) -> DevicePointer<T> {
110-
(**self).as_device_ptr()
102+
fn as_device_ptr(&self) -> DevicePointer<T> {
103+
unsafe { DevicePointer::wrap((**self).as_ptr() as *mut _) }
111104
}
112105

113106
fn len(&self) -> usize {
@@ -116,12 +109,7 @@ impl<T: DeviceCopy> GpuBuffer<T> for DeviceBuffer<T> {
116109
}
117110

118111
impl<T: DeviceCopy> GpuBuffer<T> for UnifiedBuffer<T> {
119-
unsafe fn as_device_ptr(&self) -> DevicePointer<T> {
120-
DevicePointer::wrap(self.as_ptr() as *mut _)
121-
}
122-
123-
fn as_device_ptr_mut(&mut self) -> DevicePointer<T> {
124-
// SAFETY: unified pointers can be dereferenced from the gpu.
112+
fn as_device_ptr(&self) -> DevicePointer<T> {
125113
unsafe { DevicePointer::wrap(self.as_ptr() as *mut _) }
126114
}
127115

@@ -133,29 +121,17 @@ impl<T: DeviceCopy> GpuBuffer<T> for UnifiedBuffer<T> {
133121
/// A trait describing a generic pointer that can be accessed from the GPU. This could be either a [`UnifiedBox`]
134122
/// or a regular [`DeviceBox`].
135123
pub trait GpuBox<T: DeviceCopy>: private::Sealed {
136-
/// Obtains a device pointer to this value, not requiring `&mut self`. This means
137-
/// the value must be used only for immutable reads, and must not be written to.
138-
unsafe fn as_device_ptr(&self) -> DevicePointer<T>;
139-
fn as_device_ptr_mut(&mut self) -> DevicePointer<T>;
124+
fn as_device_ptr(&self) -> DevicePointer<T>;
140125
}
141126

142127
impl<T: DeviceCopy> GpuBox<T> for DeviceBox<T> {
143-
unsafe fn as_device_ptr(&self) -> DevicePointer<T> {
128+
fn as_device_ptr(&self) -> DevicePointer<T> {
144129
self.ptr
145130
}
146-
147-
fn as_device_ptr_mut(&mut self) -> DevicePointer<T> {
148-
DeviceBox::as_device_ptr(self)
149-
}
150131
}
151132

152133
impl<T: DeviceCopy> GpuBox<T> for UnifiedBox<T> {
153-
unsafe fn as_device_ptr(&self) -> DevicePointer<T> {
154-
DevicePointer::wrap(self.ptr.as_raw() as *mut _)
155-
}
156-
157-
fn as_device_ptr_mut(&mut self) -> DevicePointer<T> {
158-
// SAFETY: unified pointers can be dereferenced from the gpu.
134+
fn as_device_ptr(&self) -> DevicePointer<T> {
159135
unsafe { DevicePointer::wrap(self.ptr.as_raw() as *mut _) }
160136
}
161137
}

crates/optix/src/denoiser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ impl Denoiser {
374374
let raw_params = parameters.to_raw();
375375

376376
let mut out = input_image.to_raw();
377-
out.data = out_buffer.as_device_ptr_mut().as_raw_mut() as u64;
377+
out.data = out_buffer.as_device_ptr().as_raw_mut() as u64;
378378

379379
let layer = sys::OptixDenoiserLayer {
380380
input: input_image.to_raw(),
@@ -427,11 +427,11 @@ impl DenoiserParams<'_> {
427427
denoiseAlpha: self.denoise_alpha as u32,
428428
hdrIntensity: self
429429
.hdr_intensity
430-
.map(|x| unsafe { x.as_device_ptr().as_raw() as u64 })
430+
.map(|x| x.as_device_ptr().as_raw() as u64)
431431
.unwrap_or_default(),
432432
hdrAverageColor: self
433433
.hdr_average_color
434-
.map(|x| unsafe { x.as_device_ptr().as_raw() as u64 })
434+
.map(|x| x.as_device_ptr().as_raw() as u64)
435435
.unwrap_or_default(),
436436
blendFactor: self.blend_factor,
437437
}

examples/cuda/cpu/path_tracer/src/cuda/data.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::common::Camera;
22
use cust::{
33
error::CudaResult,
4-
memory::{DBuffer, DeviceCopy, UnifiedBuffer},
4+
memory::{DeviceBuffer, DeviceCopy, UnifiedBuffer},
55
util::SliceExt,
66
vek::{num_traits::Zero, Vec2, Vec3},
77
};
@@ -15,13 +15,13 @@ use super::SEED;
1515
/// You could put these in the CUDA renderer but we separate them out for code readability.
1616
pub struct CudaRendererBuffers {
1717
/// The buffer of accumulated colors, every sample/render call adds its color to this buffer.
18-
pub accumulated_buffer: DBuffer<Vec3<f32>>,
18+
pub accumulated_buffer: DeviceBuffer<Vec3<f32>>,
1919
/// The scaled buffer of colors, this is just the accumulated colors divided by sample count.
20-
pub scaled_buffer: DBuffer<Vec3<f32>>,
20+
pub scaled_buffer: DeviceBuffer<Vec3<f32>>,
2121
/// The final image buffer after denoising and postprocessing.
22-
pub out_buffer: DBuffer<Vec3<u8>>,
22+
pub out_buffer: DeviceBuffer<Vec3<u8>>,
2323
/// The scaled buffer but denoised. In the future we will use the same buffer for this.
24-
pub denoised_buffer: DBuffer<Vec3<f32>>,
24+
pub denoised_buffer: DeviceBuffer<Vec3<f32>>,
2525

2626
/// The viewport used by the render kernel to emit rays.
2727
pub viewport: Viewport,
@@ -75,7 +75,7 @@ impl CudaRendererBuffers {
7575
/// Reset the renderer's view, in the buffer's case this means clearing accumulated buffers from previous samples.
7676
/// As well as changing the viewport.
7777
pub fn update_camera(&mut self, new_camera: &Camera) -> CudaResult<()> {
78-
self.accumulated_buffer = unsafe { DBuffer::zeroed(self.accumulated_buffer.len())? };
78+
self.accumulated_buffer = unsafe { DeviceBuffer::zeroed(self.accumulated_buffer.len())? };
7979
new_camera.as_viewport(&mut self.viewport);
8080
Ok(())
8181
}
@@ -104,7 +104,9 @@ impl CudaRendererBuffers {
104104
}
105105

106106
// could also use the convenience method on optix::denoiser::Image for this
107-
fn image_buffer<T: DeviceCopy + Zero>(dimensions: Vec2<usize>) -> CudaResult<DBuffer<Vec3<T>>> {
108-
unsafe { DBuffer::zeroed(dimensions.product()) }
107+
fn image_buffer<T: DeviceCopy + Zero>(
108+
dimensions: Vec2<usize>,
109+
) -> CudaResult<DeviceBuffer<Vec3<T>>> {
110+
unsafe { DeviceBuffer::zeroed(dimensions.product()) }
109111
}
110112
}

examples/optix/denoiser/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cust::memory::DBuffer;
1+
use cust::memory::DeviceBuffer;
22
use cust::prelude::{Stream, StreamFlags};
33
use cust::util::SliceExt;
44
use cust::vek::{Clamp, Vec3};
@@ -63,7 +63,7 @@ fn main() -> Result<(), Box<dyn Error>> {
6363

6464
// Currently zeroed is unsafe, but in the future we will probably expose a safe way to do it
6565
// using bytemuck
66-
let mut out_buf = unsafe { DBuffer::<Vec3<f32>>::zeroed((width * height) as usize)? };
66+
let mut out_buf = unsafe { DeviceBuffer::<Vec3<f32>>::zeroed((width * height) as usize)? };
6767

6868
// make an image to tell OptiX about how our image buffer is represented
6969
let input_image = Image::new(&in_buf, ImageFormat::Float3, width, height);

0 commit comments

Comments
 (0)