VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
metalUtils.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Shared Metal utility functions and enum converters.
5// Extracted from MetalGraphicsDevice to enable reuse across Metal backend classes.
6//
7#pragma once
8
9#include <simd/simd.h>
10#include <Metal/Metal.hpp>
11
12#include "core/math/matrix4.h"
15#include "scene/mesh.h"
16
18{
20 inline simd::float4x4 toSimdMatrix(const Matrix4& matrix)
21 {
22 simd::float4x4 out{};
23 for (int col = 0; col < 4; ++col) {
24 for (int row = 0; row < 4; ++row) {
25 out.columns[col][row] = matrix.getElement(col, row);
26 }
27 }
28 return out;
29 }
30
32 inline MTL::PrimitiveType toMetalPrimitiveType(const PrimitiveType primitiveType)
33 {
34 switch (primitiveType) {
36 return MTL::PrimitiveTypePoint;
37 case PRIMITIVE_LINES:
38 return MTL::PrimitiveTypeLine;
41 return MTL::PrimitiveTypeLineStrip;
43 return MTL::PrimitiveTypeTriangleStrip;
46 default:
47 return MTL::PrimitiveTypeTriangle;
48 }
49 }
50
52 inline MTL::CullMode toMetalCullMode(const CullMode cullMode)
53 {
54 switch (cullMode) {
56 return MTL::CullModeBack;
58 return MTL::CullModeFront;
61 default:
62 return MTL::CullModeNone;
63 }
64 }
65
67 inline MTL::Texture* createDepthTexture(MTL::Device* device, const int width, const int height)
68 {
69 if (!device || width <= 0 || height <= 0) {
70 return nullptr;
71 }
72
73 auto* desc = MTL::TextureDescriptor::alloc()->init();
74 desc->setTextureType(MTL::TextureType2D);
75 desc->setPixelFormat(MTL::PixelFormatDepth32Float);
76 desc->setWidth(static_cast<NS::UInteger>(width));
77 desc->setHeight(static_cast<NS::UInteger>(height));
78 desc->setMipmapLevelCount(1);
79 desc->setSampleCount(1);
80 desc->setStorageMode(MTL::StorageModePrivate);
81 desc->setUsage(MTL::TextureUsageRenderTarget);
82
83 auto* texture = device->newTexture(desc);
84 desc->release();
85 return texture;
86 }
87
90 {
91 public:
93 : RenderTarget(options) {}
94
95 private:
96 void destroyFrameBuffers() override {}
97 void createFrameBuffers() override {}
98 };
99}
virtual void destroyFrameBuffers()=0
RenderTarget(const RenderTargetOptions &options={})
virtual void createFrameBuffers()=0
MetalBackBufferRenderTarget(const RenderTargetOptions &options)
Definition metalUtils.h:92
MTL::Texture * createDepthTexture(MTL::Device *device, const int width, const int height)
Create a Depth32Float texture for the back buffer depth attachment.
Definition metalUtils.h:67
MTL::PrimitiveType toMetalPrimitiveType(const PrimitiveType primitiveType)
Map engine PrimitiveType to Metal PrimitiveType.
Definition metalUtils.h:32
MTL::CullMode toMetalCullMode(const CullMode cullMode)
Map engine CullMode to Metal CullMode.
Definition metalUtils.h:52
simd::float4x4 toSimdMatrix(const Matrix4 &matrix)
Convert a column-major Matrix4 to a SIMD float4x4.
Definition metalUtils.h:20
@ PRIMITIVE_POINTS
Definition mesh.h:19
@ PRIMITIVE_LINES
Definition mesh.h:20
@ PRIMITIVE_LINESTRIP
Definition mesh.h:22
@ PRIMITIVE_LINELOOP
Definition mesh.h:21
@ PRIMITIVE_TRISTRIP
Definition mesh.h:24
@ PRIMITIVE_TRIFAN
Definition mesh.h:25
@ PRIMITIVE_TRIANGLES
Definition mesh.h:23
4x4 column-major transformation matrix with SIMD acceleration.
Definition matrix4.h:31
float getElement(const int col, int row) const
Definition matrix4.h:355