Img2Num C++ (Internal Developer Docs) dev
API Documentation
Loading...
Searching...
No Matches
gpu::Buffer< T > Class Template Reference

RAII wrapper around a wgpu::Buffer with compile-time type safety. More...

#include <gpu_utils.h>

+ Collaboration diagram for gpu::Buffer< T >:

Public Member Functions

 Buffer (size_t count, wgpu::BufferUsage usage)
 Create a GPU buffer holding count elements of T.
 
void write (const T *data, size_t count, size_t offset_elements=0)
 Upload data from CPU to GPU.
 
void write (const std::vector< T > &data)
 Upload an entire vector to GPU.
 
std::vector< T > read ()
 Blocking readback: GPU → CPU.
 
const wgpu::Buffer & handle () const
 Returns the underlying wgpu::Buffer handle.
 
size_t byte_size () const
 Size in bytes.
 
size_t size () const
 Element count.
 
 Buffer (const Buffer &)=delete
 
Buffer & operator= (const Buffer &)=delete
 
 Buffer (Buffer &&other) noexcept
 Move constructor. Transfers ownership of the GPU buffer.
 
Buffer & operator= (Buffer &&other) noexcept
 Move assignment. Destroys the current buffer and takes ownership.
 
 ~Buffer ()
 Destructor. Releases the GPU buffer.
 

Private Attributes

wgpu::Buffer buffer_ {nullptr}
 
size_t count_ {0}
 
size_t byte_size_ {0}
 

Detailed Description

template<typename T>
class gpu::Buffer< T >

RAII wrapper around a wgpu::Buffer with compile-time type safety.

T must be a trivially copyable type. The buffer is destroyed when this object goes out of scope.

Template Parameters
TElement type (e.g. float, uint32_t, FilterParams).

Definition at line 131 of file gpu_utils.h.

Constructor & Destructor Documentation

◆ Buffer() [1/2]

template<typename T >
gpu::Buffer< T >::Buffer ( size_t  count,
wgpu::BufferUsage  usage 
)
inline

Create a GPU buffer holding count elements of T.

Parameters
countNumber of elements.
usagewgpu::BufferUsage flags.

Definition at line 138 of file gpu_utils.h.

139 : count_(count)
140 , byte_size_(count * sizeof(T)) {
141 wgpu::BufferDescriptor desc {};
142 desc.size = byte_size_;
143 desc.usage = usage;
144 buffer_ = GPU::getClassInstance().get_device().CreateBuffer(&desc);
145 }

◆ Buffer() [2/2]

template<typename T >
gpu::Buffer< T >::Buffer ( Buffer< T > &&  other)
inlinenoexcept

Move constructor. Transfers ownership of the GPU buffer.

Definition at line 211 of file gpu_utils.h.

212 : buffer_(std::move(other.buffer_))
213 , count_(other.count_)
214 , byte_size_(other.byte_size_) {
215 other.buffer_ = nullptr;
216 other.count_ = 0;
217 other.byte_size_ = 0;
218 }

◆ ~Buffer()

template<typename T >
gpu::Buffer< T >::~Buffer ( )
inline

Destructor. Releases the GPU buffer.

Definition at line 237 of file gpu_utils.h.

237 {
238 if (buffer_) {
239 buffer_.Destroy();
240 }
241 }

Member Function Documentation

◆ byte_size()

template<typename T >
size_t gpu::Buffer< T >::byte_size ( ) const
inline

Size in bytes.

Definition at line 197 of file gpu_utils.h.

197 {
198 return byte_size_;
199 }

Referenced by gpu::BindEntry::buffer_entry().

+ Here is the caller graph for this function:

◆ handle()

template<typename T >
const wgpu::Buffer & gpu::Buffer< T >::handle ( ) const
inline

Returns the underlying wgpu::Buffer handle.

Definition at line 192 of file gpu_utils.h.

192 {
193 return buffer_;
194 }

Referenced by gpu::BindEntry::buffer_entry().

+ Here is the caller graph for this function:

◆ operator=()

template<typename T >
Buffer & gpu::Buffer< T >::operator= ( Buffer< T > &&  other)
inlinenoexcept

Move assignment. Destroys the current buffer and takes ownership.

Definition at line 221 of file gpu_utils.h.

221 {
222 if (this != &other) {
223 if (buffer_) {
224 buffer_.Destroy();
225 }
226 buffer_ = std::move(other.buffer_);
227 count_ = other.count_;
228 byte_size_ = other.byte_size_;
229 other.buffer_ = nullptr;
230 other.count_ = 0;
231 other.byte_size_ = 0;
232 }
233 return *this;
234 }

◆ read()

template<typename T >
std::vector< T > gpu::Buffer< T >::read ( )
inline

Blocking readback: GPU → CPU.

Creates an internal staging buffer, copies, maps, waits, and returns the data. Works on both native Dawn and Emscripten.

Definition at line 166 of file gpu_utils.h.

166 {
167 // Create staging buffer
168 wgpu::BufferDescriptor staging_desc {};
169 staging_desc.size = byte_size_;
170 staging_desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
171 wgpu::Buffer staging = GPU::getClassInstance().get_device().CreateBuffer(&staging_desc);
172
173 // Copy GPU buffer → staging
174 wgpu::CommandEncoder encoder = GPU::getClassInstance().get_device().CreateCommandEncoder();
175 encoder.CopyBufferToBuffer(buffer_, 0, staging, 0, byte_size_);
176 wgpu::CommandBuffer commands = encoder.Finish();
177 GPU::getClassInstance().get_queue().Submit(1, &commands);
178
179 // Map and wait
180 const void* mapped = map_and_wait(staging, byte_size_);
181
182 // Copy to vector
183 std::vector<T> result(count_);
184 std::memcpy(result.data(), mapped, byte_size_);
185
186 staging.Unmap();
187 staging.Destroy();
188 return result;
189 }
const void * map_and_wait(wgpu::Buffer &buffer, size_t size)
Block until a MapAsync on buffer completes.
Definition gpu_utils.h:90

References gpu::map_and_wait().

+ Here is the call graph for this function:

◆ size()

template<typename T >
size_t gpu::Buffer< T >::size ( ) const
inline

Element count.

Definition at line 202 of file gpu_utils.h.

202 {
203 return count_;
204 }

◆ write() [1/2]

template<typename T >
void gpu::Buffer< T >::write ( const std::vector< T > &  data)
inline

Upload an entire vector to GPU.

Definition at line 158 of file gpu_utils.h.

158 {
159 write(data.data(), data.size());
160 }
void write(const T *data, size_t count, size_t offset_elements=0)
Upload data from CPU to GPU.
Definition gpu_utils.h:151

References gpu::Buffer< T >::write().

+ Here is the call graph for this function:

◆ write() [2/2]

template<typename T >
void gpu::Buffer< T >::write ( const T *  data,
size_t  count,
size_t  offset_elements = 0 
)
inline

Upload data from CPU to GPU.

Parameters
dataPointer to source data.
countNumber of elements to write.
offset_elementsElement offset into the buffer (default 0).

Definition at line 151 of file gpu_utils.h.

151 {
152 GPU::getClassInstance().get_queue().WriteBuffer(
153 buffer_, offset_elements * sizeof(T), data, count * sizeof(T)
154 );
155 }

Referenced by gpu::Buffer< T >::write().

+ Here is the caller graph for this function:

Member Data Documentation

◆ buffer_

template<typename T >
wgpu::Buffer gpu::Buffer< T >::buffer_ {nullptr}
private

Definition at line 244 of file gpu_utils.h.

244{nullptr};

◆ byte_size_

template<typename T >
size_t gpu::Buffer< T >::byte_size_ {0}
private

Definition at line 246 of file gpu_utils.h.

246{0};

◆ count_

template<typename T >
size_t gpu::Buffer< T >::count_ {0}
private

Definition at line 245 of file gpu_utils.h.

245{0};

The documentation for this class was generated from the following file: