diff options
Diffstat (limited to 'src/gpu/uniform_helper.h')
| -rw-r--r-- | src/gpu/uniform_helper.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/gpu/uniform_helper.h b/src/gpu/uniform_helper.h new file mode 100644 index 0000000..151153f --- /dev/null +++ b/src/gpu/uniform_helper.h @@ -0,0 +1,41 @@ +// This file is part of the 64k demo project. +// It provides a generic uniform buffer helper to reduce boilerplate. +// Templated on uniform struct type for type safety and automatic sizing. + +#pragma once + +#include "gpu/gpu.h" +#include <cstring> + +// Generic uniform buffer helper +// Usage: +// UniformBuffer<MyUniforms> uniforms_; +// uniforms_.init(device); +// uniforms_.update(queue, my_data); +template <typename T> class UniformBuffer { + public: + UniformBuffer() = default; + + // Initialize the uniform buffer with the device + void init(WGPUDevice device) { + buffer_ = gpu_create_buffer( + device, sizeof(T), WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); + } + + // Update the uniform buffer with new data + void update(WGPUQueue queue, const T& data) { + wgpuQueueWriteBuffer(queue, buffer_.buffer, 0, &data, sizeof(T)); + } + + // Get the underlying GpuBuffer (for bind group creation) + GpuBuffer& get() { + return buffer_; + } + + const GpuBuffer& get() const { + return buffer_; + } + + private: + GpuBuffer buffer_; +}; |
