Blender V5.3
GHOST_ContextMTL.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "GHOST_Context.hh"
12#include "GHOST_Types.hh"
13
14#include <Cocoa/Cocoa.h>
15#include <Metal/Metal.h>
16#include <QuartzCore/QuartzCore.h>
17
18@class CAMetalLayer;
19@class MTLCommandQueue;
20@class MTLDevice;
21@class MTLRenderPipelineState;
22@class MTLTexture;
23@class NSView;
24
27
28 public:
29 /* Defines the number of simultaneous command buffers which can be in flight.
30 * The default limit of `64` is considered to be optimal for Blender. Too many command buffers
31 * will result in workload fragmentation and additional system-level overhead. This limit should
32 * also only be increased if the application is consistently exceeding the limit, and there are
33 * no command buffer leaks.
34 *
35 * If this limit is reached, starting a new command buffer will fail. The Metal back-end will
36 * therefore stall until completion and log a warning when this limit is reached in order to
37 * ensure correct function of the app.
38 *
39 * It is generally preferable to reduce the prevalence of GPU_flush or GPU Context switches
40 * (which will both break command submissions), rather than increasing this limit. */
41 static const int max_command_buffer_count = 64;
42
43 public:
47 GHOST_ContextMTL(const GHOST_ContextParams &context_params,
48 NSView *metalView,
49 CAMetalLayer *metalLayer);
50
54 ~GHOST_ContextMTL() override;
55
58 {
59 return GHOST_kSuccess;
60 }
61
67
73
79
80 unsigned int getDefaultFramebuffer() override;
81
87
94
100 GHOST_TSuccess setSwapInterval(int interval) override;
101
107 GHOST_TSuccess getSwapInterval(int &interval_out) override;
108
115
121 id<MTLTexture> metalOverlayTexture();
122
126 MTLCommandQueue *metalCommandQueue();
127
131 MTLDevice *metalDevice();
132
136 void metalRegisterPresentCallback(void (*callback)(
137 MTLRenderPassDescriptor *, id<MTLRenderPipelineState>, id<MTLTexture>, id<CAMetalDrawable>));
138 void metalRegisterXrBlitCallback(void (*callback)(id<MTLTexture>, int, int, int, int));
139
140 private:
142 NSView *metal_view_;
143 CAMetalLayer *metal_layer_;
144 MTLRenderPipelineState *metal_render_pipeline_;
145 bool owns_metal_device_;
146
153 static const int METAL_SWAPCHAIN_SIZE = 3;
154 struct MTLSwapchainTexture {
155 id<MTLTexture> texture;
156 unsigned int index;
157 };
158 MTLSwapchainTexture default_framebuffer_metal_texture_[METAL_SWAPCHAIN_SIZE];
159 unsigned int current_swapchain_index = 0;
160
161 /* Present callback.
162 * We use this such that presentation can be controlled from within the Metal
163 * Context. This is required for optimal performance and clean control flow.
164 * Also helps ensure flickering does not occur by present being dependent
165 * on existing submissions. */
166 void (*contextPresentCallback)(MTLRenderPassDescriptor *,
167 id<MTLRenderPipelineState>,
168 id<MTLTexture>,
169 id<CAMetalDrawable>);
170 /* XR texture blitting callback. */
171 void (*xrBlitCallback)(id<MTLTexture>, int, int, int, int);
172
173 int mtl_SwapInterval;
174
175 static int s_sharedCount;
176
177 /* Single device queue for multiple contexts. */
178 static MTLCommandQueue *s_sharedMetalCommandQueue;
179
180 /* Metal functions */
181 void metalInit();
182 void metalFree();
183 void metalInitFramebuffer();
184 void metalUpdateFramebuffer();
185 void metalSwapBuffers();
186 void initClear() {};
187};
GHOST_TSuccess
@ GHOST_kSuccess
GHOST_TSuccess releaseDrawingContext() override
static const int max_command_buffer_count
friend class GHOST_XrGraphicsBindingMetal
void metalRegisterXrBlitCallback(void(*callback)(id< MTLTexture >, int, int, int, int))
GHOST_TSuccess swapBufferAcquire() override
MTLCommandQueue * metalCommandQueue()
MTLDevice * metalDevice()
GHOST_TSuccess activateDrawingContext() override
void metalRegisterPresentCallback(void(*callback)(MTLRenderPassDescriptor *, id< MTLRenderPipelineState >, id< MTLTexture >, id< CAMetalDrawable >))
GHOST_TSuccess setSwapInterval(int interval) override
GHOST_ContextMTL(const GHOST_ContextParams &context_params, NSView *metalView, CAMetalLayer *metalLayer)
GHOST_TSuccess swapBufferRelease() override
GHOST_TSuccess updateDrawingContext() override
GHOST_TSuccess initializeDrawingContext() override
GHOST_TSuccess releaseNativeHandles() override
id< MTLTexture > metalOverlayTexture()
unsigned int getDefaultFramebuffer() override
GHOST_TSuccess getSwapInterval(int &interval_out) override
GHOST_Context(const GHOST_ContextParams &context_params)
#define index(i)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET