Blender V5.0
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
13#include <Cocoa/Cocoa.h>
14#include <Metal/Metal.h>
15#include <QuartzCore/QuartzCore.h>
16
17@class CAMetalLayer;
18@class MTLCommandQueue;
19@class MTLDevice;
20@class MTLRenderPipelineState;
21@class MTLTexture;
22@class NSView;
23
25 public:
26 /* Defines the number of simultaneous command buffers which can be in flight.
27 * The default limit of `64` is considered to be optimal for Blender. Too many command buffers
28 * will result in workload fragmentation and additional system-level overhead. This limit should
29 * also only be increased if the application is consistently exceeding the limit, and there are
30 * no command buffer leaks.
31 *
32 * If this limit is reached, starting a new command buffer will fail. The Metal back-end will
33 * therefore stall until completion and log a warning when this limit is reached in order to
34 * ensure correct function of the app.
35 *
36 * It is generally preferable to reduce the prevalence of GPU_flush or GPU Context switches
37 * (which will both break command submissions), rather than increasing this limit. */
38 static const int max_command_buffer_count = 64;
39
40 public:
44 GHOST_ContextMTL(const GHOST_ContextParams &context_params,
45 NSView *metalView,
46 CAMetalLayer *metalLayer);
47
51 ~GHOST_ContextMTL() override;
52
55 {
56 return GHOST_kSuccess;
57 }
58
64
70
76
77 unsigned int getDefaultFramebuffer() override;
78
84
91
97 GHOST_TSuccess setSwapInterval(int interval) override;
98
104 GHOST_TSuccess getSwapInterval(int &interval_out) override;
105
112
118 id<MTLTexture> metalOverlayTexture();
119
123 MTLCommandQueue *metalCommandQueue();
124
128 MTLDevice *metalDevice();
129
133 void metalRegisterPresentCallback(void (*callback)(
134 MTLRenderPassDescriptor *, id<MTLRenderPipelineState>, id<MTLTexture>, id<CAMetalDrawable>));
135
136 private:
138 NSView *metal_view_;
139 CAMetalLayer *metal_layer_;
140 MTLRenderPipelineState *metal_render_pipeline_;
141 bool owns_metal_device_;
142
149 static const int METAL_SWAPCHAIN_SIZE = 3;
150 struct MTLSwapchainTexture {
151 id<MTLTexture> texture;
152 unsigned int index;
153 };
154 MTLSwapchainTexture default_framebuffer_metal_texture_[METAL_SWAPCHAIN_SIZE];
155 unsigned int current_swapchain_index = 0;
156
157 /* Present callback.
158 * We use this such that presentation can be controlled from within the Metal
159 * Context. This is required for optimal performance and clean control flow.
160 * Also helps ensure flickering does not occur by present being dependent
161 * on existing submissions. */
162 void (*contextPresentCallback)(MTLRenderPassDescriptor *,
163 id<MTLRenderPipelineState>,
164 id<MTLTexture>,
165 id<CAMetalDrawable>);
166
167 int mtl_SwapInterval;
168
169 static int s_sharedCount;
170
171 /* Single device queue for multiple contexts. */
172 static MTLCommandQueue *s_sharedMetalCommandQueue;
173
174 /* Metal functions */
175 void metalInit();
176 void metalFree();
177 void metalInitFramebuffer();
178 void metalUpdateFramebuffer();
179 void metalSwapBuffers();
180 void initClear() {};
181};
GHOST_TSuccess
Definition GHOST_Types.h:57
@ GHOST_kSuccess
Definition GHOST_Types.h:57
GHOST_TSuccess releaseDrawingContext() override
static const int max_command_buffer_count
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()
~GHOST_ContextMTL() override
unsigned int getDefaultFramebuffer() override
GHOST_TSuccess getSwapInterval(int &interval_out) override
GHOST_Context(const GHOST_ContextParams &context_params)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET