summaryrefslogtreecommitdiff
path: root/src/gpu/uniform_helper.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpu/uniform_helper.h')
-rw-r--r--src/gpu/uniform_helper.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/gpu/uniform_helper.h b/src/gpu/uniform_helper.h
new file mode 100644
index 0000000..afc4a4b
--- /dev/null
+++ b/src/gpu/uniform_helper.h
@@ -0,0 +1,42 @@
+// 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_;
+};