VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
metalBuffer.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 19.07.2025.
5//
6#pragma once
7
8#include <Metal/Metal.hpp>
9
10#include "metalGraphicsDevice.h"
12
14{
15 enum BufferUsage : uint32_t {
16 MAP_READ = 0x0001,
17 MAP_WRITE = 0x0002,
18 COPY_SRC = 0x0004,
19 COPY_DST = 0x0008,
20 INDEX = 0x0010,
21 VERTEX = 0x0020,
22 UNIFORM = 0x0040,
23 STORAGE = 0x0080,
24 INDIRECT = 0x0100,
26 };
27
32 class MetalBuffer : public HardwareBuffer {
33 public:
34 explicit MetalBuffer(const BufferUsage usageFlags) : _usageFlags(usageFlags) {}
35 ~MetalBuffer() override;
36
37 void allocate(MTL::Device* device, size_t size);
38
46 void write(size_t bufferOffset, const void* data, size_t dataSize) const;
47
48 [[nodiscard]] size_t size() const {
49 return _buffer ? _buffer->length() : 0;
50 }
51
52 [[nodiscard]] MTL::Buffer* raw() const {
53 return _buffer;
54 }
55
56 // HardwareBuffer interface
57 void upload(GraphicsDevice* device, const void* data, size_t size) override;
58 void* nativeHandle() const override { return _buffer; }
59
60 // Unlock the buffer and upload data to GPU
61 void unlock(MetalGraphicsDevice* device, const std::vector<uint8_t>& storage);
62
63 // Allocate the buffer with the specified size
64 void allocate(MetalGraphicsDevice* device, size_t size);
65
69 void adoptBuffer(MTL::Buffer* buffer);
70
71 private:
72 BufferUsage _usageFlags;
73 MTL::Buffer* _buffer = nullptr;
74 };
75}
Abstract GPU interface for resource creation, state management, and draw submission.
void upload(GraphicsDevice *device, const void *data, size_t size) override
Upload data to the GPU buffer.
void * nativeHandle() const override
Returns the backend-specific native handle (MTL::Buffer*, VkBuffer, etc.).
Definition metalBuffer.h:58
MetalBuffer(const BufferUsage usageFlags)
Definition metalBuffer.h:34
void unlock(MetalGraphicsDevice *device, const std::vector< uint8_t > &storage)
void adoptBuffer(MTL::Buffer *buffer)
void allocate(MTL::Device *device, size_t size)
void write(size_t bufferOffset, const void *data, size_t dataSize) const