Blender V4.3
GHOST_ContextCGL.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11/* Don't generate OpenGL deprecation warning. This is a known thing, and is not something easily
12 * solvable in a short term. */
13#ifdef __clang__
14# pragma clang diagnostic ignored "-Wdeprecated-declarations"
15#endif
16
17#include "GHOST_ContextCGL.hh"
18
19#import <Cocoa/Cocoa.h>
20#import <Metal/Metal.h>
21#import <QuartzCore/QuartzCore.h>
22
23#include <cassert>
24#include <vector>
25
26static const MTLPixelFormat METAL_FRAMEBUFFERPIXEL_FORMAT_EDR = MTLPixelFormatRGBA16Float;
27
28static void ghost_fatal_error_dialog(const char *msg)
29{
30 @autoreleasepool {
31 NSString *message = [NSString stringWithFormat:@"Error opening window:\n%s", msg];
32
33 NSAlert *alert = [[NSAlert alloc] init];
34
35 alert.messageText = @"Blender";
36 alert.informativeText = message;
37 alert.alertStyle = NSAlertStyleCritical;
38
39 [alert addButtonWithTitle:@"Quit"];
40 [alert runModal];
41 }
42
43 exit(1);
44}
45
46MTLCommandQueue *GHOST_ContextCGL::s_sharedMetalCommandQueue = nil;
47int GHOST_ContextCGL::s_sharedCount = 0;
48
50 NSView *metalView,
51 CAMetalLayer *metalLayer,
52 int debug)
53 : GHOST_Context(stereoVisual),
54 m_metalView(metalView),
55 m_metalLayer(metalLayer),
56 m_metalRenderPipeline(nil),
57 m_debug(debug)
58{
59 @autoreleasepool {
60 /* Initialize Metal Swap-chain. */
61 current_swapchain_index = 0;
62 for (int i = 0; i < METAL_SWAPCHAIN_SIZE; i++) {
63 m_defaultFramebufferMetalTexture[i].texture = nil;
64 m_defaultFramebufferMetalTexture[i].index = i;
65 }
66
67 if (m_metalView) {
68 m_ownsMetalDevice = false;
69 metalInit();
70 }
71 else {
72 /* Prepare offscreen GHOST Context Metal device. */
73 id<MTLDevice> metalDevice = MTLCreateSystemDefaultDevice();
74
75 if (m_debug) {
76 printf("Selected Metal Device: %s\n", [metalDevice.name UTF8String]);
77 }
78
79 m_ownsMetalDevice = true;
80 if (metalDevice) {
81 m_metalLayer = [[CAMetalLayer alloc] init];
82 m_metalLayer.edgeAntialiasingMask = 0;
83 m_metalLayer.masksToBounds = NO;
84 m_metalLayer.opaque = YES;
85 m_metalLayer.framebufferOnly = YES;
86 m_metalLayer.presentsWithTransaction = NO;
87 [m_metalLayer removeAllAnimations];
88 m_metalLayer.device = metalDevice;
89 m_metalLayer.allowsNextDrawableTimeout = NO;
90
91 /* Enable EDR support. This is done by:
92 * 1. Using a floating point render target, so that values outside 0..1 can be used
93 * 2. Informing the OS that we are EDR aware, and intend to use values outside 0..1
94 * 3. Setting the extended sRGB color space so that the OS knows how to interpret the
95 * values.
96 */
97 m_metalLayer.wantsExtendedDynamicRangeContent = YES;
98 m_metalLayer.pixelFormat = METAL_FRAMEBUFFERPIXEL_FORMAT_EDR;
99 const CFStringRef name = kCGColorSpaceExtendedSRGB;
100 CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(name);
101 m_metalLayer.colorspace = colorspace;
102 CGColorSpaceRelease(colorspace);
103
104 metalInit();
105 }
106 else {
108 "[ERROR] Failed to create Metal device for offscreen GHOST Context.\n");
109 }
110 }
111
112 /* Initialize swap-interval. */
113 mtl_SwapInterval = 60;
114 }
115}
116
118{
119 metalFree();
120
121 if (m_ownsMetalDevice) {
122 if (m_metalLayer) {
123 [m_metalLayer release];
124 m_metalLayer = nil;
125 }
126 }
127 assert(s_sharedCount);
128
129 s_sharedCount--;
130 [s_sharedMetalCommandQueue release];
131 if (s_sharedCount == 0) {
132 s_sharedMetalCommandQueue = nil;
133 }
134}
135
137{
138 if (m_metalView) {
139 metalSwapBuffers();
140 }
141 return GHOST_kSuccess;
142}
143
145{
146 mtl_SwapInterval = interval;
147 return GHOST_kSuccess;
148}
149
151{
152 intervalOut = mtl_SwapInterval;
153 return GHOST_kSuccess;
154}
155
160
165
167{
168 /* NOTE(Metal): This is not valid. */
169 return 0;
170}
171
173{
174 if (m_metalView) {
175 metalUpdateFramebuffer();
176 return GHOST_kSuccess;
177 }
178 return GHOST_kFailure;
179}
180
182{
183 /* Increment Swap-chain - Only needed if context is requesting a new texture */
184 current_swapchain_index = (current_swapchain_index + 1) % METAL_SWAPCHAIN_SIZE;
185
186 /* Ensure backing texture is ready for current swapchain index */
188
189 /* Return texture. */
190 return m_defaultFramebufferMetalTexture[current_swapchain_index].texture;
191}
192
194{
195 return s_sharedMetalCommandQueue;
196}
198{
199 id<MTLDevice> device = m_metalLayer.device;
200 return (MTLDevice *)device;
201}
202
204 MTLRenderPassDescriptor *, id<MTLRenderPipelineState>, id<MTLTexture>, id<CAMetalDrawable>))
205{
206 this->contextPresentCallback = callback;
207}
208
210{
211 @autoreleasepool {
212 if (m_metalView) {
213 metalInitFramebuffer();
214 }
215 }
216 return GHOST_kSuccess;
217}
218
220{
221 m_metalView = nil;
222
223 return GHOST_kSuccess;
224}
225
226void GHOST_ContextCGL::metalInit()
227{
228 @autoreleasepool {
229 id<MTLDevice> device = m_metalLayer.device;
230
231 /* Create a command queue for blit/present operation.
232 * NOTE: All context should share a single command queue
233 * to ensure correct ordering of work submitted from multiple contexts. */
234 if (s_sharedMetalCommandQueue == nil) {
235 s_sharedMetalCommandQueue = (MTLCommandQueue *)[device
236 newCommandQueueWithMaxCommandBufferCount:GHOST_ContextCGL::max_command_buffer_count];
237 }
238 /* Ensure active GHOSTContext retains a reference to the shared context. */
239 [s_sharedMetalCommandQueue retain];
240 s_sharedCount++;
241
242 /* Create shaders for blit operation. */
243 NSString *source = @R"msl(
244 using namespace metal;
245
246 struct Vertex {
247 float4 position [[position]];
248 float2 texCoord [[attribute(0)]];
249 };
250
251 vertex Vertex vertex_shader(uint v_id [[vertex_id]]) {
252 Vertex vtx;
253
254 vtx.position.x = float(v_id & 1) * 4.0 - 1.0;
255 vtx.position.y = float(v_id >> 1) * 4.0 - 1.0;
256 vtx.position.z = 0.0;
257 vtx.position.w = 1.0;
258
259 vtx.texCoord = vtx.position.xy * 0.5 + 0.5;
260
261 return vtx;
262 }
263
264 constexpr sampler s {};
265
266 fragment float4 fragment_shader(Vertex v [[stage_in]],
267 texture2d<float> t [[texture(0)]]) {
268
269 /* Final blit should ensure alpha is 1.0. This resolves
270 * rendering artifacts for blitting of final back-buffer. */
271 float4 out_tex = t.sample(s, v.texCoord);
272 out_tex.a = 1.0;
273 return out_tex;
274 }
275 )msl";
276
277 MTLCompileOptions *options = [[[MTLCompileOptions alloc] init] autorelease];
278 options.languageVersion = MTLLanguageVersion1_1;
279
280 NSError *error = nil;
281 id<MTLLibrary> library = [device newLibraryWithSource:source options:options error:&error];
282 if (error) {
284 "GHOST_ContextCGL::metalInit: newLibraryWithSource:options:error: failed!");
285 }
286
287 /* Create a render pipeline for blit operation. */
288 MTLRenderPipelineDescriptor *desc = [[[MTLRenderPipelineDescriptor alloc] init] autorelease];
289
290 desc.fragmentFunction = [library newFunctionWithName:@"fragment_shader"];
291 desc.vertexFunction = [library newFunctionWithName:@"vertex_shader"];
292 [desc.colorAttachments objectAtIndexedSubscript:0].pixelFormat =
294
295 /* Ensure library is released. */
296 [library autorelease];
297
298 m_metalRenderPipeline = (MTLRenderPipelineState *)[device
299 newRenderPipelineStateWithDescriptor:desc
300 error:&error];
301 if (error) {
303 "GHOST_ContextCGL::metalInit: newRenderPipelineStateWithDescriptor:error: failed!");
304 }
305
306 /* Create a render pipeline to composite things rendered with Metal on top
307 * of the frame-buffer contents. Uses the same vertex and fragment shader
308 * as the blit above, but with alpha blending enabled. */
309 desc.label = @"Metal Overlay";
310 desc.colorAttachments[0].blendingEnabled = YES;
311 desc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
312 desc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
313
314 if (error) {
316 "GHOST_ContextCGL::metalInit: newRenderPipelineStateWithDescriptor:error: failed (when "
317 "creating the Metal overlay pipeline)!");
318 }
319
320 [desc.fragmentFunction release];
321 [desc.vertexFunction release];
322 }
323}
324
325void GHOST_ContextCGL::metalFree()
326{
327 if (m_metalRenderPipeline) {
328 [m_metalRenderPipeline release];
329 m_metalRenderPipeline = nil;
330 }
331
332 for (int i = 0; i < METAL_SWAPCHAIN_SIZE; i++) {
333 if (m_defaultFramebufferMetalTexture[i].texture) {
334 [m_defaultFramebufferMetalTexture[i].texture release];
335 m_defaultFramebufferMetalTexture[i].texture = nil;
336 }
337 }
338}
339
340void GHOST_ContextCGL::metalInitFramebuffer()
341{
343}
344
345void GHOST_ContextCGL::metalUpdateFramebuffer()
346{
347 @autoreleasepool {
348 const NSRect bounds = [m_metalView bounds];
349 const NSSize backingSize = [m_metalView convertSizeToBacking:bounds.size];
350 const size_t width = size_t(backingSize.width);
351 const size_t height = size_t(backingSize.height);
352
353 if (m_defaultFramebufferMetalTexture[current_swapchain_index].texture &&
354 m_defaultFramebufferMetalTexture[current_swapchain_index].texture.width == width &&
355 m_defaultFramebufferMetalTexture[current_swapchain_index].texture.height == height)
356 {
357 return;
358 }
359
360 /* Free old texture */
361 [m_defaultFramebufferMetalTexture[current_swapchain_index].texture release];
362
363 id<MTLDevice> device = m_metalLayer.device;
364 MTLTextureDescriptor *overlayDesc = [MTLTextureDescriptor
365 texture2DDescriptorWithPixelFormat:METAL_FRAMEBUFFERPIXEL_FORMAT_EDR
366 width:width
367 height:height
368 mipmapped:NO];
369 overlayDesc.storageMode = MTLStorageModePrivate;
370 overlayDesc.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
371
372 id<MTLTexture> overlayTex = [device newTextureWithDescriptor:overlayDesc];
373 if (!overlayTex) {
375 "GHOST_ContextCGL::metalUpdateFramebuffer: failed to create Metal overlay texture!");
376 }
377 else {
378 overlayTex.label = [NSString
379 stringWithFormat:@"Metal Overlay for GHOST Context %p", this]; //@"";
380 }
381
382 m_defaultFramebufferMetalTexture[current_swapchain_index].texture = overlayTex;
383
384 /* Clear texture on create */
385 id<MTLCommandBuffer> cmdBuffer = [s_sharedMetalCommandQueue commandBuffer];
386 MTLRenderPassDescriptor *passDescriptor = [MTLRenderPassDescriptor renderPassDescriptor];
387 {
388 auto attachment = [passDescriptor.colorAttachments objectAtIndexedSubscript:0];
389 attachment.texture = m_defaultFramebufferMetalTexture[current_swapchain_index].texture;
390 attachment.loadAction = MTLLoadActionClear;
391 attachment.clearColor = MTLClearColorMake(0.294, 0.294, 0.294, 1.000);
392 attachment.storeAction = MTLStoreActionStore;
393 }
394 {
395 id<MTLRenderCommandEncoder> enc = [cmdBuffer
396 renderCommandEncoderWithDescriptor:passDescriptor];
397 [enc endEncoding];
398 }
399 [cmdBuffer commit];
400
401 m_metalLayer.drawableSize = CGSizeMake(CGFloat(width), CGFloat(height));
402 }
403}
404
405void GHOST_ContextCGL::metalSwapBuffers()
406{
407 @autoreleasepool {
409
410 id<CAMetalDrawable> drawable = [m_metalLayer nextDrawable];
411 if (!drawable) {
412 return;
413 }
414
415 MTLRenderPassDescriptor *passDescriptor = [MTLRenderPassDescriptor renderPassDescriptor];
416 {
417 auto attachment = [passDescriptor.colorAttachments objectAtIndexedSubscript:0];
418 attachment.texture = drawable.texture;
419 attachment.loadAction = MTLLoadActionClear;
420 attachment.clearColor = MTLClearColorMake(1.0, 0.294, 0.294, 1.000);
421 attachment.storeAction = MTLStoreActionStore;
422 }
423
424 assert(contextPresentCallback);
425 assert(m_defaultFramebufferMetalTexture[current_swapchain_index].texture != nil);
426 (*contextPresentCallback)(passDescriptor,
427 (id<MTLRenderPipelineState>)m_metalRenderPipeline,
428 m_defaultFramebufferMetalTexture[current_swapchain_index].texture,
429 drawable);
430 }
431}
static const MTLPixelFormat METAL_FRAMEBUFFERPIXEL_FORMAT_EDR
static void ghost_fatal_error_dialog(const char *msg)
GHOST_TSuccess
Definition GHOST_Types.h:87
@ GHOST_kFailure
Definition GHOST_Types.h:87
@ GHOST_kSuccess
Definition GHOST_Types.h:87
void init()
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
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
#define printf
CCL_NAMESPACE_BEGIN struct Options options
DEGForeachIDComponentCallback callback
static void error(const char *str)