VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
metalRenderPipeline.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 22.07.2025.
5//
6#pragma once
7
8#include "Metal/Metal.hpp"
10#include "metalPipeline.h"
17#include "scene/mesh.h"
18
19namespace visutwin::canvas
20{
24 struct CacheEntry {
25 // Render pipeline
26 MTL::RenderPipelineState* pipeline = nullptr;
27
29 std::vector<uint32_t> hashes;
30 };
31
33 {
34
35 };
36
38 {
39 public:
40 explicit MetalRenderPipeline(const MetalGraphicsDevice* device);
41
43
44 [[nodiscard]] MTL::RenderPipelineState* raw() const { return _pipeline; }
45
46 // Get or create a render pipeline with the specified parameters
47 MTL::RenderPipelineState* get(const Primitive& primitive, const std::shared_ptr<VertexFormat>& vertexFormat0,
48 const std::shared_ptr<VertexFormat>& vertexFormat1, int ibFormat, const std::shared_ptr<Shader>& shader,
49 const std::shared_ptr<RenderTarget>& renderTarget,
50 const std::vector<std::shared_ptr<MetalBindGroupFormat>>& bindGroupFormats,
51 const std::shared_ptr<BlendState>& blendState, const std::shared_ptr<DepthState>& depthState,
52 CullMode cullMode, bool stencilEnabled,
53 const std::shared_ptr<StencilParameters>& stencilFront, const std::shared_ptr<StencilParameters>& stencilBack,
54 const std::shared_ptr<VertexFormat>& instancingFormat = nullptr);
55
56 private:
57 // Create a new render pipeline
58 MTL::RenderPipelineState* create(
59 const MTL::PrimitiveType primitiveTopology, int ibFormat, const std::shared_ptr<Shader>& shader,
60 const std::shared_ptr<RenderTarget>& renderTarget, metal::PipelineLayout* pipelineLayout,
61 std::shared_ptr<BlendState> blendState, std::shared_ptr<DepthState> depthState,
62 const std::vector<void*>& vertexBufferLayout, CullMode cullMode, bool stencilEnabled,
63 std::shared_ptr<StencilParameters> stencilFront, std::shared_ptr<StencilParameters> stencilBack,
64 int vertexStride = 56,
65 int instancingStride = 0
66 );
67
68 // Set the blend state configuration
69 void setBlend(MTL::RenderPipelineColorAttachmentDescriptor* colorAttachment, const std::shared_ptr<BlendState>& blendState);
70
71 MTL::RenderPipelineState* _pipeline;
72
73 // Temporary array for hash lookups
74 std::vector<uint32_t> _lookupHashes;
75
76 // The cache of render pipelines
77 std::unordered_map<uint32_t, std::vector<std::shared_ptr<CacheEntry>>> _cache;
78
79 // The cache of vertex buffer layouts
80 std::unique_ptr<MetalVertexBufferLayout> _vertexBufferLayout;
81
82 // Mapping tables
83 static const MTL::PrimitiveType primitiveTopology[5];
84 static const MTL::BlendOperation blendOperation[5];
85 static const MTL::BlendFactor blendFactor[13];
86 };
87}
MetalPipeline(const MetalGraphicsDevice *device)
MetalRenderPipeline(const MetalGraphicsDevice *device)
MTL::RenderPipelineState * raw() const
MTL::RenderPipelineState * get(const Primitive &primitive, const std::shared_ptr< VertexFormat > &vertexFormat0, const std::shared_ptr< VertexFormat > &vertexFormat1, int ibFormat, const std::shared_ptr< Shader > &shader, const std::shared_ptr< RenderTarget > &renderTarget, const std::vector< std::shared_ptr< MetalBindGroupFormat > > &bindGroupFormats, const std::shared_ptr< BlendState > &blendState, const std::shared_ptr< DepthState > &depthState, CullMode cullMode, bool stencilEnabled, const std::shared_ptr< StencilParameters > &stencilFront, const std::shared_ptr< StencilParameters > &stencilBack, const std::shared_ptr< VertexFormat > &instancingFormat=nullptr)
std::vector< uint32_t > hashes
MTL::RenderPipelineState * pipeline
Describes how vertex and index data should be interpreted for a draw call.
Definition mesh.h:33