summaryrefslogtreecommitdiff
path: root/src/gpu/uniform_helper.h
blob: afc4a4bddaa03dd989d5de322cdb190c479e2a57 (plain)
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
// 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_;
};