VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
metalVertexBufferLayout.cpp
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 14.11.2025.
5//
7
8#include <sstream>
9
10namespace visutwin::canvas
11{
12 std::vector<void*> MetalVertexBufferLayout::get(const std::shared_ptr<VertexFormat>& vertexFormat0,
13 const std::shared_ptr<VertexFormat>& vertexFormat1)
14 {
15 std::string key = getKey(vertexFormat0, vertexFormat1);
16
17 auto it = _cache.find(key);
18 if (it != _cache.end()) {
19 return it->second;
20 }
21
22 auto layout = create(vertexFormat0, vertexFormat1);
23 _cache[key] = layout;
24
25 return layout;
26 }
27
28 std::string MetalVertexBufferLayout::getKey(const std::shared_ptr<VertexFormat>& vertexFormat0,
29 const std::shared_ptr<VertexFormat>& vertexFormat1) const
30 {
31 std::ostringstream oss;
32 if (vertexFormat0) {
33 oss << vertexFormat0->renderingHashString();
34 } else {
35 oss << "null";
36 }
37 oss << "-";
38
39 if (vertexFormat1) {
40 oss << vertexFormat1->renderingHashString();
41 } else {
42 oss << "null";
43 }
44
45 return oss.str();
46 }
47
48 std::vector<void*> MetalVertexBufferLayout::create(const std::shared_ptr<VertexFormat>& vertexFormat0,
49 const std::shared_ptr<VertexFormat>& vertexFormat1)
50 {
51 // Vector to hold vertex buffer layouts
52 std::vector<void*> layout;
53
54 // Helper lambda to process a single vertex format
55 auto addFormat = [&](std::shared_ptr<VertexFormat> format) {
56 if (!format)
57 {
58 return;
59 }
60
61 bool interleaved = format->isInterleaved();
62 const char* stepMode = format->isInstancing() ? "instance" : "vertex";
63
64 // Note: vertex buffer layout creation is stubbed — uses void* placeholders.
65 // Full implementation would create GPUVertexBufferLayout structures.
66
67 /*
68 std::vector<GPUVertexAttribute> attributes;
69 auto& elements = format->getElements();
70 int elementCount = elements.size();
71
72 for (int i = 0; i < elementCount; i++) {
73 auto& element = elements[i];
74 int location = semanticToLocation[element.getName()];
75
76 std::string formatStr = getGpuVertexFormat(
77 element.getDataType(),
78 element.getNumComponents(),
79 element.getNormalize()
80 );
81
82 GPUVertexAttribute attribute;
83 attribute.shaderLocation = location;
84 attribute.offset = interleaved ? element.getOffset() : 0;
85 attribute.format = formatStr;
86
87 attributes.push_back(attribute);
88
89 // If not interleaved, or this is the last element, create a buffer layout
90 if (!interleaved || i == elementCount - 1) {
91 GPUVertexBufferLayout bufferLayout;
92 bufferLayout.attributes = attributes;
93 bufferLayout.arrayStride = element.getStride();
94 bufferLayout.stepMode = stepMode;
95
96 layout.push_back(bufferLayout);
97 attributes.clear();
98 }
99 }
100 */
101
102 // Placeholder: add nullptr for now
103 layout.push_back(nullptr);
104 };
105
106 // Process both vertex formats
107 addFormat(vertexFormat0);
108 addFormat(vertexFormat1);
109
110 return layout;
111 }
112}
std::vector< void * > get(const std::shared_ptr< VertexFormat > &vertexFormat0, const std::shared_ptr< VertexFormat > &vertexFormat1=nullptr)