Blender V4.3
GHOST_ContextCGL.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
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 NSOpenGLContext;
23@class NSOpenGLView;
24@class NSView;
25
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_ContextCGL(bool stereoVisual, NSView *metalView, CAMetalLayer *metalLayer, int debug);
48
52 ~GHOST_ContextCGL() override;
53
58 GHOST_TSuccess swapBuffers() override;
59
65
71
72 unsigned int getDefaultFramebuffer() override;
73
79
86
92 GHOST_TSuccess setSwapInterval(int interval) override;
93
99 GHOST_TSuccess getSwapInterval(int &intervalOut) override;
100
107
113 id<MTLTexture> metalOverlayTexture();
114
118 MTLCommandQueue *metalCommandQueue();
119
123 MTLDevice *metalDevice();
124
129 MTLRenderPassDescriptor *, id<MTLRenderPipelineState>, id<MTLTexture>, id<CAMetalDrawable>));
130
131 private:
133 NSView *m_metalView;
134 CAMetalLayer *m_metalLayer;
135 MTLRenderPipelineState *m_metalRenderPipeline;
136 bool m_ownsMetalDevice;
137
144 static const int METAL_SWAPCHAIN_SIZE = 3;
145 struct MTLSwapchainTexture {
146 id<MTLTexture> texture;
147 unsigned int index;
148 };
149 MTLSwapchainTexture m_defaultFramebufferMetalTexture[METAL_SWAPCHAIN_SIZE];
150 unsigned int current_swapchain_index = 0;
151
152 /* Present callback.
153 * We use this such that presentation can be controlled from within the Metal
154 * Context. This is required for optimal performance and clean control flow.
155 * Also helps ensure flickering does not occur by present being dependent
156 * on existing submissions. */
157 void (*contextPresentCallback)(MTLRenderPassDescriptor *,
158 id<MTLRenderPipelineState>,
159 id<MTLTexture>,
160 id<CAMetalDrawable>);
161
162 int mtl_SwapInterval;
163 const bool m_debug;
164
165 static int s_sharedCount;
166
167 /* Single device queue for multiple contexts. */
168 static MTLCommandQueue *s_sharedMetalCommandQueue;
169
170 /* Metal functions */
171 void metalInit();
172 void metalFree();
173 void metalInitFramebuffer();
174 void metalUpdateFramebuffer();
175 void metalSwapBuffers();
176 void initClear(){};
177};
GHOST_TSuccess
Definition GHOST_Types.h:87
void metalRegisterPresentCallback(void(*callback)(MTLRenderPassDescriptor *, id< MTLRenderPipelineState >, id< MTLTexture >, id< CAMetalDrawable >))
GHOST_TSuccess updateDrawingContext() override
id< MTLTexture > metalOverlayTexture()
GHOST_TSuccess swapBuffers() override
GHOST_TSuccess activateDrawingContext() override
GHOST_TSuccess getSwapInterval(int &intervalOut) override
MTLDevice * metalDevice()
GHOST_ContextCGL(bool stereoVisual, NSView *metalView, CAMetalLayer *metalLayer, int debug)
GHOST_TSuccess releaseDrawingContext() override
static const int max_command_buffer_count
GHOST_TSuccess initializeDrawingContext() override
unsigned int getDefaultFramebuffer() override
MTLCommandQueue * metalCommandQueue()
GHOST_TSuccess releaseNativeHandles() override
GHOST_TSuccess setSwapInterval(int interval) override
~GHOST_ContextCGL() override
DEGForeachIDComponentCallback callback