MetalKit

RSS for tag

Render graphics in a standard Metal view, load textures from many sources, and work efficiently with models provided by Model I/O using MetalKit.

Posts under MetalKit tag

200 Posts

Post

Replies

Boosts

Views

Activity

Occasional pink frame in Metal on macOS Ventura (M2 Pro)
I am developing a macOS app which has a metal-backed view, based on CAMetalLayer. It worked fine on macOS Monterey, but after I switched to a M2 Pro machine and upgraded my macOS to Ventura (tried both 13.3 and 13.4), the view would randomly display a blank pink screen - as if the Metal is indicating an error or empty state. I don't get any log messages in the Console though. Particularly, the pink screen appears after I select the app's "Enter Full Screen" command. It appears only in ~1 of 10 attempts, and doesn't appear in 9 of 10 attempts. The pink screen goes away after the view renders its next frame (e.g. on mouse move). It's worth mentioning that the CAMetalLayer-backed view in question is hosted in the toolbar, so the issue is probably related to the fact that when an app switches to the full screen mode, macOS detaches the toolbar from the main window and attaches it to the separate toolbar window. I have tried to profile the app and catch the pink frame - particularly using -[MTLCaptureManager startCaptureWithDescriptor:error:], however when I analyze the trace, everything seems to be fine - all vertexes, textures and commands are being transmitted to the GPU correctly. However, on the screen, I still get the occasional pink screen. I wonder if anyone could give me hints as of why this might be happening and what other tools I can use to track down the problem? Or maybe it's a macOS Ventura bug? P.S. For brevity, I'm not attaching the code, which is too large and spread over multiple files to fit here. I'm interested in general advice.
1
0
1.6k
May ’23
Some feature requests for Metal
Hello guys. With the release of the M1 Pro and M1 Max in particular, the Mac has become a platform that could become very interesting for games in the future. However, since some features are still missing in Metal, it could be problematic for some developers to port their games to Metal. Especially with the Unreal Engine 5 you can already see a tendency in this direction, since e.g. Nanite and Lumen are unfortunately not available on the Mac. As a Vulkan developer I wanted to inquire about some features that are not yet available in Metal at the moment. These features are very interesting if you want to write a GPU driven renderer for modern game engines. Furthermore, these features could be used to emulate D3D12 on the Mac via MoltenVK, which would result in more games being available on the Mac. Buffer device address: This feature allows the application to query a 64-bit buffer device address value for a buffer. It is very useful for D3D12 emulation and for compatibility with Vulkan, e.g. to implement ray tracing on MoltenVK. DrawIndirectCount: This feature allows an application to source the number of draws for indirect drawing calls from a buffer. Also very useful in many gpu driven situations Only 500000 resources per argument buffer Metal has a limit of 500000 resources per argument buffer. To be equivalent to D3D12 Resource Binding Tear 2, you would need 1 million. This is also very important as so many DirectX12 game engines could be ported to Metal more easily. Mesh shader / Task shader: Two interesting new shader stages to optimize the rendering pipeline Are there any plans to implement this features in future? Is there a roadmap for metal? Is there a website where I can suggest features to the metal developers? I hope to see at least the first 3 features in metal in the future and I think that many developers feel the same way. Best regards, Marlon
5
3
5.0k
May ’23
Beginner help (obj-c, metal, reading inputs)
Hey all. I'm new to Apple's ecosystem and need some help with a few basics. Specifically, I've started with Xcode 14's metal game code and am struggling to read user inputs. I've tried variations of the below without luck: #import "GameViewController.h" #import "Renderer.h" @interface InputView : NSView @end @implementation InputView - (BOOL)acceptsFirstResponder{ return YES; } - (void)mouseDown:(NSEvent*)event { printf("mouse down"); } - (void)keyDown:(NSEvent *)event { printf("key down"); } @end @implementation GameViewController { MTKView *_view; InputView *_input_view; Renderer *_renderer; } - (void)viewDidLoad { [super viewDidLoad]; _view = (MTKView *)self.view; _view.device = MTLCreateSystemDefaultDevice(); if(!_view.device) { NSLog(@"Metal is not supported on this device"); self.view = [[NSView alloc] initWithFrame:self.view.frame]; return; } _renderer = [[Renderer alloc] initWithMetalKitView:_view]; [_renderer mtkView:_view drawableSizeWillChange:_view.bounds.size]; _view.delegate = _renderer; _input_view = [[InputView alloc] initWithFrame:_view.frame]; BOOL fr = [_input_view becomeFirstResponder]; printf("fr %d\n", fr); } @end But I also don't understand the basic structure of this boilerplate application. I see my main file is this: #import <Cocoa/Cocoa.h> int main(int argc, const char * argv[]) { @autoreleasepool { // Setup code that might create autoreleased objects goes here. } return NSApplicationMain(argc, argv); } It doesn't seem to touch any of the rest of the code (GameViewController, AppDelegate, etc.), and yet it obviously does, since it runs and renders things. I assume some build magic is happening, but where can I find information on that? What official resources can I read to understand how this app is being built? I've been having a lot of trouble navigating Apple's official documentation and could use some direction.
1
0
739
Apr ’23
Can MTKView display HDR CIImage/CVPixelBuffer?
I have tried everything but it looks to be impossible to get MTKView to display full range of colors of HDR CIImage made from CVPixelBuffer (in 10bit YUV format). Only builtin layers such as AVCaptureVideoPreviewLayer, AVPlayerLayer, AVSampleBufferDisplayLayer are able to fully display HDR images on iOS. Is MTKView incapable of displaying full BT2020_HLG color range? Why does MTKView clip colors no matter even if I set pixel Color format to bgra10_xr or bgra10_xr_srgb?  convenience init(frame: CGRect, contentScale:CGFloat) {         self.init(frame: frame)         contentScaleFactor = contentScale     }     convenience init(frame: CGRect) {         let device = MetalCamera.metalDevice         self.init(frame: frame, device: device)         colorPixelFormat = .bgra10_xr         self.preferredFramesPerSecond = 30     }     override init(frame frameRect: CGRect, device: MTLDevice?) {         guard let device = device else {             fatalError("Can't use Metal")         }         guard let cmdQueue = device.makeCommandQueue(maxCommandBufferCount: 5) else {             fatalError("Can't make Command Queue")         }         commandQueue = cmdQueue         context = CIContext(mtlDevice: device, options: [CIContextOption.cacheIntermediates: false])         super.init(frame: frameRect, device: device)         self.framebufferOnly = false         self.clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0)     } And then rendering code:  override func draw(_ rect: CGRect) {         guard let image = self.image else {             return         }         let dRect = self.bounds         let drawImage: CIImage         let targetSize = dRect.size         let imageSize = image.extent.size         let scalingFactor = min(targetSize.width/imageSize.width, targetSize.height/imageSize.height)         let scalingTransform = CGAffineTransform(scaleX: scalingFactor, y: scalingFactor)         let translation:CGPoint = CGPoint(x: (targetSize.width - imageSize.width * scalingFactor)/2 , y: (targetSize.height - imageSize.height * scalingFactor)/2)         let translationTransform = CGAffineTransform(translationX: translation.x, y: translation.y)         let scalingTranslationTransform = scalingTransform.concatenating(translationTransform)        drawImage = image.transformed(by: scalingTranslationTransform)         let commandBuffer = commandQueue.makeCommandBufferWithUnretainedReferences()         guard let texture = self.currentDrawable?.texture else {             return         }         var colorSpace:CGColorSpace                 if #available(iOS 14.0, *) {             colorSpace = CGColorSpace(name: CGColorSpace.itur_2100_HLG)!         } else {             // Fallback on earlier versions             colorSpace = drawImage.colorSpace ?? CGColorSpaceCreateDeviceRGB()         }         NSLog("Image \(colorSpace.name), \(image.colorSpace?.name)")         context.render(drawImage, to: texture, commandBuffer: commandBuffer, bounds: dRect, colorSpace: colorSpace)         commandBuffer?.present(self.currentDrawable!, afterMinimumDuration: 1.0/Double(self.preferredFramesPerSecond))         commandBuffer?.commit()     }
1
0
2.2k
Apr ’23
How to Show current GPU usage via METAL®
on macOS silicon where memory is unified its now important to see how much memory is being used by the GPU, but unlike OpenGL which has been depreciated for some time, metal doesnt seem to have any "Driver Monitor Parameters" to be able to display this information. Example of OpenGL Parameters: https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/OpenGLDriverMonitorUserGuide/Glossary/Glossary.html Im looking for an equivalent for METAL® but haven't been able to find any, unless im overlooking everything in how metal is laid out idk. any ideas or sense of direction would be ideal. thanks for anyone who can help.
0
0
916
Apr ’23
Draw something to CAMetalLayer cause it shows all red
I am writing something about video decoding. Now I have met some problems on darwin. First init the layer and bind it to a view CAMetalLayer *layer = [[CAMetalLayer alloc] init]; layer.device = MTLCreateSystemDefaultDevice(); layer.pixelFormat = MTLPixelFormatBGRA8Unorm; layer.drawableSize = self.view.frame.size; layer.contentsGravity = kCAGravityCenter; self.view.wantsLayer = YES; self.view.layer = layer; next when the video decoded frame coming, id <CAMetalDrawable> drawable = [metal_layer_ nextDrawable]; if (!drawable) { return; } id <MTLTexture> texture = [drawable texture]; IOSurfaceRef surface = [texture iosurface]; void *base_address = IOSurfaceGetBaseAddress(surface); int pitch[] = {(int) (IOSurfaceGetBytesPerRow(surface)), 0}; uint8_t *pixels[] = {static_cast<uint8_t *>(base_address), nullptr}; fn(pixels, pitch); // Which is ffmpeg swscale. [drawable present]; This does not work at all, the whole screen is red and would never change while new frame is coming. And I tried another way to present the video frame. It is still red. Is there any thing wrong or missed.
1
0
1.2k
Mar ’23
Tile Rendering in Metal
I developed a small drawing application for the iPad in Swift using Metal. It supports drawing Lines with the Apple Pencil, erase them, select them and so on. Everything is drawn onto an offscreen texture so I don't need to redraw all of them. The App also supports zooming and panning. If you zoom you have to redraw everything. And there begins the problem. At the moment if I pan my canvas I simply draw the offscreen texture at zoom level 1.0 and pan it around. So it looks very pixelated because I zoom into the offscreen texture without redrawing the offscreen texture itself. The problem is I can't redraw the whole offscreen texture every frame while panning because for performance reasons. My idea was to implement a tile based progressive rendering technique. I thought I could split up the screen into tiles with size of for example 256x256 pixels and if I pan I can move those tiles and only the new visible ones I need to render. But I don't really know where to start. I do not know how to render the lines onto those tiles. every line has a single VertexBuffer at the moment storing the triangles. I thought maybe you can use multiple color Attachments for The Render Encoder to draw the lines. So every colorAttachements[n].texture would be a tile. And maybe through the help of Viewport Culling? I don't have much experience in this area so I have no idea. I also found the Sparse Texture feature, which looked like it would solve my problem. But I need to support iPads that do not support this feature. Has someone else made something similar ? Or has any example code for this? Or has a complete different Idea that could help? Thank you very much for your help!
6
0
1.8k
Mar ’23
Setting up Metal cpp with GLFW and ImGUI
Dear experts, I'm working on adding UI for my cpp based path tracer renderer. I want to create metal cpp device and pass it to renderer, but also I want to use ImGui and GLFW (for window manager and input events handling). I've found solution how I can mix obj c code that requires by GLFW window setup and cpp code: https://github.com/ikryukov/MetalCppImGui // Here is key thing for integration GLFW with Metal Cpp // GLFW supports only obj c window handle NSWindow *nswin = glfwGetCocoaWindow(window); CA::MetalLayer* layer = CA::MetalLayer::layer(); layer->setDevice(device); layer->setPixelFormat(MTL::PixelFormatBGRA8Unorm); // bridge to obj c here because NSWindow expetcs objc CAMetalLayer* l = (__bridge CAMetalLayer*)layer; nswin.contentView.layer = l; nswin.contentView.wantsLayer = YES; Is there any official way to handle event in Metal cpp without objective c support? Maybe MetalKit will have such features in future?
1
0
3.9k
Feb ’23
Drawable size of MTKView
I have a MTKView as a document view of NSScrollView. Whenever i check the drawable size of this metal view , i am getting twice the size of the metal view. For a metal view of size 1000x1000, drawable size is 2000x2000. Why it is so? What is exactly the so called 'Drawable'.? Thanks in advance.
1
0
1.7k
Jan ’23
Crash when set CAMetalLayer.presentsWithTransaction=true in background thread
When set metalLayer.presentsWithTransaction = true, and we call drawable.present() in bg thread (our rendering thread is not main thread), the app sometimes will crash with: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.' Any solution?
1
0
1.4k
Jan ’23
How to load MTLTexture without premultiplied alpha?
Hello all, I need my texture to be not premultiplied alpha, because is use the alpha for some additional calculations in the fragment shader. At the moment I load my texture like this let textureLoader = MTKTextureLoader(device: sceneView.device!) myTexture = try textureLoader.newTexture(URL: URL(fileURLWithPath: path!), options: [MTKTextureLoader.Option.SRGB: false]) because I need the raw RGBA values. But of course it gets premultiplied. How can I turn off this behavior?
3
0
1.3k
Jan ’23
Ignoring maximumFramesPerSecond
I'm working on a game using Metal. I like to show it off to my friends every once in a while, who always make remarks about how the fps is capped at 60. I can increase preferredFramesPerSecond to 120 or something like that, but it seems as though my fps is still stuck at 60. The maximum that my monitor can do is 60, however, I know that more fps, even with a slower monitor, can help your performance in a game. How do I ignore the maximumFramesPerSecond variable and let my game run with an fps cap bigger than 60?
0
0
787
Jan ’23
NSScrollView of a large graphic jumps when scrolling on OSX Ventura (13.0.1)
This is an internal app that displays a large NSView (50K x 25K pixels) within a NSScrollView the size of my 27" iMac. The code worked fine, scrolling both ways was very smooth, under the latest release of OS-X 12. I upgraded to 13.0.1 and now the scrolling jerks every ~ 500 pixels in either direction. I am using XCode 14.1 and writing in Objective-C. I notice the NSLog now contains the message "Metal API Validation Enabled" which it never did before. Is this a bug or has something fundamental changed in Ventura which requires changing the viewing code. (Memo to self: NEVER install a ".0" release. Let somebody else be the guinea pig.) Thanks for whatever help you can provide. Ron
1
0
985
Dec ’22
Metal not rendering color with low saturation and alpha
I'm using MTKView to render some triangles. Everything works fine until I decrease the color's saturation and opacity value which makes the triangle completely transparent. Creating SwiftUI's Color with the same values shows correctly. This only happens for colors with "low" saturation, if the color has 100% saturation (like #FF0000), it still renders fine even with just 1% opacity. I noticed if I change the colorPixelFormat of the MTKView, the result will change. So not sure if I only need to change the colorPixelFormat to fix this, in that case, I don't know which one either as I have limited knowledge about graphics. Here is an example for color #FF8888: bgra8Unorm: minimum 55% opacity for it to render bgra8Unorm_srgb: minimum 77% opacity for it to render and the color is a lot lighter than what it should be. In Swift, I store the colors as [Float], in MSL, it will be converted to float4*. Nothing fancy with the vertex and fragment functions, just returning the input. This is not very likely where the issue lies as other colors work. Some code to show my setup: // MTKView's setup clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0) isOpaque = false layer.magnificationFilter = .nearest layer.minificationFilter = .nearest // State setup let pipelineDescriptor = MTLRenderPipelineDescriptor() pipelineDescriptor.vertexFunction = vertexFunction pipelineDescriptor.fragmentFunction = fragmentFunction pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm pipelineState = try? device.makeRenderPipelineState(descriptor: pipelineDescriptor) // draw method setup guard let vertexBuffer = vertexBuffer, let indexBuffer = indexBuffer, let indexCount = indexCount, let colorBuffer = colorBuffer, let pipelineState = pipelineState, let discriptor = view.currentRenderPassDescriptor, let commandBuffer = commandQueue.makeCommandBuffer(), let commandEncoder = commandBuffer.makeRenderCommandEncoder( descriptor: discriptor ), let drawable = view.currentDrawable else { return } commandEncoder.setRenderPipelineState(pipelineState) commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0) commandEncoder.setVertexBuffer(colorBuffer, offset: 0, index: 1) commandEncoder.drawIndexedPrimitives( type: .triangle, indexCount: indexCount, indexType: .uint32, indexBuffer: indexBuffer, indexBufferOffset: 0 ) commandEncoder.endEncoding() commandBuffer.present(drawable) commandBuffer.commit()
4
0
1.9k
Dec ’22
functions.data in Cache was deleted when launch after reboot
In my game project, there is a functions.data file in then /AppData/Library/Caches/[bundleID]/com.apple.metal/functions.data, when we reboot and launch the game, this file was rest to about 40KB, normaly this file's is about 30MB, this operation was done by the metal, Is there any way to avoid it?
Replies
1
Boosts
0
Views
2.3k
Activity
Jun ’23
Occasional pink frame in Metal on macOS Ventura (M2 Pro)
I am developing a macOS app which has a metal-backed view, based on CAMetalLayer. It worked fine on macOS Monterey, but after I switched to a M2 Pro machine and upgraded my macOS to Ventura (tried both 13.3 and 13.4), the view would randomly display a blank pink screen - as if the Metal is indicating an error or empty state. I don't get any log messages in the Console though. Particularly, the pink screen appears after I select the app's "Enter Full Screen" command. It appears only in ~1 of 10 attempts, and doesn't appear in 9 of 10 attempts. The pink screen goes away after the view renders its next frame (e.g. on mouse move). It's worth mentioning that the CAMetalLayer-backed view in question is hosted in the toolbar, so the issue is probably related to the fact that when an app switches to the full screen mode, macOS detaches the toolbar from the main window and attaches it to the separate toolbar window. I have tried to profile the app and catch the pink frame - particularly using -[MTLCaptureManager startCaptureWithDescriptor:error:], however when I analyze the trace, everything seems to be fine - all vertexes, textures and commands are being transmitted to the GPU correctly. However, on the screen, I still get the occasional pink screen. I wonder if anyone could give me hints as of why this might be happening and what other tools I can use to track down the problem? Or maybe it's a macOS Ventura bug? P.S. For brevity, I'm not attaching the code, which is too large and spread over multiple files to fit here. I'm interested in general advice.
Replies
1
Boosts
0
Views
1.6k
Activity
May ’23
Some feature requests for Metal
Hello guys. With the release of the M1 Pro and M1 Max in particular, the Mac has become a platform that could become very interesting for games in the future. However, since some features are still missing in Metal, it could be problematic for some developers to port their games to Metal. Especially with the Unreal Engine 5 you can already see a tendency in this direction, since e.g. Nanite and Lumen are unfortunately not available on the Mac. As a Vulkan developer I wanted to inquire about some features that are not yet available in Metal at the moment. These features are very interesting if you want to write a GPU driven renderer for modern game engines. Furthermore, these features could be used to emulate D3D12 on the Mac via MoltenVK, which would result in more games being available on the Mac. Buffer device address: This feature allows the application to query a 64-bit buffer device address value for a buffer. It is very useful for D3D12 emulation and for compatibility with Vulkan, e.g. to implement ray tracing on MoltenVK. DrawIndirectCount: This feature allows an application to source the number of draws for indirect drawing calls from a buffer. Also very useful in many gpu driven situations Only 500000 resources per argument buffer Metal has a limit of 500000 resources per argument buffer. To be equivalent to D3D12 Resource Binding Tear 2, you would need 1 million. This is also very important as so many DirectX12 game engines could be ported to Metal more easily. Mesh shader / Task shader: Two interesting new shader stages to optimize the rendering pipeline Are there any plans to implement this features in future? Is there a roadmap for metal? Is there a website where I can suggest features to the metal developers? I hope to see at least the first 3 features in metal in the future and I think that many developers feel the same way. Best regards, Marlon
Replies
5
Boosts
3
Views
5.0k
Activity
May ’23
Beginner help (obj-c, metal, reading inputs)
Hey all. I'm new to Apple's ecosystem and need some help with a few basics. Specifically, I've started with Xcode 14's metal game code and am struggling to read user inputs. I've tried variations of the below without luck: #import "GameViewController.h" #import "Renderer.h" @interface InputView : NSView @end @implementation InputView - (BOOL)acceptsFirstResponder{ return YES; } - (void)mouseDown:(NSEvent*)event { printf("mouse down"); } - (void)keyDown:(NSEvent *)event { printf("key down"); } @end @implementation GameViewController { MTKView *_view; InputView *_input_view; Renderer *_renderer; } - (void)viewDidLoad { [super viewDidLoad]; _view = (MTKView *)self.view; _view.device = MTLCreateSystemDefaultDevice(); if(!_view.device) { NSLog(@"Metal is not supported on this device"); self.view = [[NSView alloc] initWithFrame:self.view.frame]; return; } _renderer = [[Renderer alloc] initWithMetalKitView:_view]; [_renderer mtkView:_view drawableSizeWillChange:_view.bounds.size]; _view.delegate = _renderer; _input_view = [[InputView alloc] initWithFrame:_view.frame]; BOOL fr = [_input_view becomeFirstResponder]; printf("fr %d\n", fr); } @end But I also don't understand the basic structure of this boilerplate application. I see my main file is this: #import <Cocoa/Cocoa.h> int main(int argc, const char * argv[]) { @autoreleasepool { // Setup code that might create autoreleased objects goes here. } return NSApplicationMain(argc, argv); } It doesn't seem to touch any of the rest of the code (GameViewController, AppDelegate, etc.), and yet it obviously does, since it runs and renders things. I assume some build magic is happening, but where can I find information on that? What official resources can I read to understand how this app is being built? I've been having a lot of trouble navigating Apple's official documentation and could use some direction.
Replies
1
Boosts
0
Views
739
Activity
Apr ’23
Can MTKView display HDR CIImage/CVPixelBuffer?
I have tried everything but it looks to be impossible to get MTKView to display full range of colors of HDR CIImage made from CVPixelBuffer (in 10bit YUV format). Only builtin layers such as AVCaptureVideoPreviewLayer, AVPlayerLayer, AVSampleBufferDisplayLayer are able to fully display HDR images on iOS. Is MTKView incapable of displaying full BT2020_HLG color range? Why does MTKView clip colors no matter even if I set pixel Color format to bgra10_xr or bgra10_xr_srgb?  convenience init(frame: CGRect, contentScale:CGFloat) {         self.init(frame: frame)         contentScaleFactor = contentScale     }     convenience init(frame: CGRect) {         let device = MetalCamera.metalDevice         self.init(frame: frame, device: device)         colorPixelFormat = .bgra10_xr         self.preferredFramesPerSecond = 30     }     override init(frame frameRect: CGRect, device: MTLDevice?) {         guard let device = device else {             fatalError("Can't use Metal")         }         guard let cmdQueue = device.makeCommandQueue(maxCommandBufferCount: 5) else {             fatalError("Can't make Command Queue")         }         commandQueue = cmdQueue         context = CIContext(mtlDevice: device, options: [CIContextOption.cacheIntermediates: false])         super.init(frame: frameRect, device: device)         self.framebufferOnly = false         self.clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0)     } And then rendering code:  override func draw(_ rect: CGRect) {         guard let image = self.image else {             return         }         let dRect = self.bounds         let drawImage: CIImage         let targetSize = dRect.size         let imageSize = image.extent.size         let scalingFactor = min(targetSize.width/imageSize.width, targetSize.height/imageSize.height)         let scalingTransform = CGAffineTransform(scaleX: scalingFactor, y: scalingFactor)         let translation:CGPoint = CGPoint(x: (targetSize.width - imageSize.width * scalingFactor)/2 , y: (targetSize.height - imageSize.height * scalingFactor)/2)         let translationTransform = CGAffineTransform(translationX: translation.x, y: translation.y)         let scalingTranslationTransform = scalingTransform.concatenating(translationTransform)        drawImage = image.transformed(by: scalingTranslationTransform)         let commandBuffer = commandQueue.makeCommandBufferWithUnretainedReferences()         guard let texture = self.currentDrawable?.texture else {             return         }         var colorSpace:CGColorSpace                 if #available(iOS 14.0, *) {             colorSpace = CGColorSpace(name: CGColorSpace.itur_2100_HLG)!         } else {             // Fallback on earlier versions             colorSpace = drawImage.colorSpace ?? CGColorSpaceCreateDeviceRGB()         }         NSLog("Image \(colorSpace.name), \(image.colorSpace?.name)")         context.render(drawImage, to: texture, commandBuffer: commandBuffer, bounds: dRect, colorSpace: colorSpace)         commandBuffer?.present(self.currentDrawable!, afterMinimumDuration: 1.0/Double(self.preferredFramesPerSecond))         commandBuffer?.commit()     }
Replies
1
Boosts
0
Views
2.2k
Activity
Apr ’23
How to Show current GPU usage via METAL®
on macOS silicon where memory is unified its now important to see how much memory is being used by the GPU, but unlike OpenGL which has been depreciated for some time, metal doesnt seem to have any "Driver Monitor Parameters" to be able to display this information. Example of OpenGL Parameters: https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/OpenGLDriverMonitorUserGuide/Glossary/Glossary.html Im looking for an equivalent for METAL® but haven't been able to find any, unless im overlooking everything in how metal is laid out idk. any ideas or sense of direction would be ideal. thanks for anyone who can help.
Replies
0
Boosts
0
Views
916
Activity
Apr ’23
Draw something to CAMetalLayer cause it shows all red
I am writing something about video decoding. Now I have met some problems on darwin. First init the layer and bind it to a view CAMetalLayer *layer = [[CAMetalLayer alloc] init]; layer.device = MTLCreateSystemDefaultDevice(); layer.pixelFormat = MTLPixelFormatBGRA8Unorm; layer.drawableSize = self.view.frame.size; layer.contentsGravity = kCAGravityCenter; self.view.wantsLayer = YES; self.view.layer = layer; next when the video decoded frame coming, id <CAMetalDrawable> drawable = [metal_layer_ nextDrawable]; if (!drawable) { return; } id <MTLTexture> texture = [drawable texture]; IOSurfaceRef surface = [texture iosurface]; void *base_address = IOSurfaceGetBaseAddress(surface); int pitch[] = {(int) (IOSurfaceGetBytesPerRow(surface)), 0}; uint8_t *pixels[] = {static_cast<uint8_t *>(base_address), nullptr}; fn(pixels, pitch); // Which is ffmpeg swscale. [drawable present]; This does not work at all, the whole screen is red and would never change while new frame is coming. And I tried another way to present the video frame. It is still red. Is there any thing wrong or missed.
Replies
1
Boosts
0
Views
1.2k
Activity
Mar ’23
How to create a perspective transform using Metal
I have a quad made up of two triangles rendered using indexed primitives. How can I create a transform where only one corner of the quad is stretched, like the perspective tool in most drawing apps? For example:
Replies
2
Boosts
0
Views
1.2k
Activity
Mar ’23
Tile Rendering in Metal
I developed a small drawing application for the iPad in Swift using Metal. It supports drawing Lines with the Apple Pencil, erase them, select them and so on. Everything is drawn onto an offscreen texture so I don't need to redraw all of them. The App also supports zooming and panning. If you zoom you have to redraw everything. And there begins the problem. At the moment if I pan my canvas I simply draw the offscreen texture at zoom level 1.0 and pan it around. So it looks very pixelated because I zoom into the offscreen texture without redrawing the offscreen texture itself. The problem is I can't redraw the whole offscreen texture every frame while panning because for performance reasons. My idea was to implement a tile based progressive rendering technique. I thought I could split up the screen into tiles with size of for example 256x256 pixels and if I pan I can move those tiles and only the new visible ones I need to render. But I don't really know where to start. I do not know how to render the lines onto those tiles. every line has a single VertexBuffer at the moment storing the triangles. I thought maybe you can use multiple color Attachments for The Render Encoder to draw the lines. So every colorAttachements[n].texture would be a tile. And maybe through the help of Viewport Culling? I don't have much experience in this area so I have no idea. I also found the Sparse Texture feature, which looked like it would solve my problem. But I need to support iPads that do not support this feature. Has someone else made something similar ? Or has any example code for this? Or has a complete different Idea that could help? Thank you very much for your help!
Replies
6
Boosts
0
Views
1.8k
Activity
Mar ’23
ARKit + SceneKit. Scaled triangles appear after applying depth video with alpha channel on SCNPlane
I am using ARKit + SceneKit. After applying depth video with alpha channel on SCNPlane, scaled triangles appear. How to remove these triangles and vertices that remained on the SCNPlane with depth 0.0. Result The video for videotexture
Replies
2
Boosts
1
Views
950
Activity
Mar ’23
3d animation : lip syncing
I have a simple 3d avatar in the form of an FBX file. I can convert that to a scene file for SceneKit. I need to make it talk, at runtime. So I need a way to go from audio to movements of the mouse. How can I do that? Any pointer appreciated, thanks!
Replies
2
Boosts
0
Views
2.6k
Activity
Feb ’23
Setting up Metal cpp with GLFW and ImGUI
Dear experts, I'm working on adding UI for my cpp based path tracer renderer. I want to create metal cpp device and pass it to renderer, but also I want to use ImGui and GLFW (for window manager and input events handling). I've found solution how I can mix obj c code that requires by GLFW window setup and cpp code: https://github.com/ikryukov/MetalCppImGui // Here is key thing for integration GLFW with Metal Cpp // GLFW supports only obj c window handle NSWindow *nswin = glfwGetCocoaWindow(window); CA::MetalLayer* layer = CA::MetalLayer::layer(); layer->setDevice(device); layer->setPixelFormat(MTL::PixelFormatBGRA8Unorm); // bridge to obj c here because NSWindow expetcs objc CAMetalLayer* l = (__bridge CAMetalLayer*)layer; nswin.contentView.layer = l; nswin.contentView.wantsLayer = YES; Is there any official way to handle event in Metal cpp without objective c support? Maybe MetalKit will have such features in future?
Replies
1
Boosts
0
Views
3.9k
Activity
Feb ’23
Drawable size of MTKView
I have a MTKView as a document view of NSScrollView. Whenever i check the drawable size of this metal view , i am getting twice the size of the metal view. For a metal view of size 1000x1000, drawable size is 2000x2000. Why it is so? What is exactly the so called 'Drawable'.? Thanks in advance.
Replies
1
Boosts
0
Views
1.7k
Activity
Jan ’23
CAMetalLayer.nextDrawable() is very time-consuming, take 5ms-12ms
I think CAMetalLayer.nextDrawable() shouldn't be very time-consuming, it should be less than 1ms. But occasionally, it will take more than 5ms, just 7ms~13ms, which is a very long time. How can we optimize this? Since this will cause rendering junk.....
Replies
3
Boosts
0
Views
2.4k
Activity
Jan ’23
Crash when set CAMetalLayer.presentsWithTransaction=true in background thread
When set metalLayer.presentsWithTransaction = true, and we call drawable.present() in bg thread (our rendering thread is not main thread), the app sometimes will crash with: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.' Any solution?
Replies
1
Boosts
0
Views
1.4k
Activity
Jan ’23
How to load MTLTexture without premultiplied alpha?
Hello all, I need my texture to be not premultiplied alpha, because is use the alpha for some additional calculations in the fragment shader. At the moment I load my texture like this let textureLoader = MTKTextureLoader(device: sceneView.device!) myTexture = try textureLoader.newTexture(URL: URL(fileURLWithPath: path!), options: [MTKTextureLoader.Option.SRGB: false]) because I need the raw RGBA values. But of course it gets premultiplied. How can I turn off this behavior?
Replies
3
Boosts
0
Views
1.3k
Activity
Jan ’23
Ignoring maximumFramesPerSecond
I'm working on a game using Metal. I like to show it off to my friends every once in a while, who always make remarks about how the fps is capped at 60. I can increase preferredFramesPerSecond to 120 or something like that, but it seems as though my fps is still stuck at 60. The maximum that my monitor can do is 60, however, I know that more fps, even with a slower monitor, can help your performance in a game. How do I ignore the maximumFramesPerSecond variable and let my game run with an fps cap bigger than 60?
Replies
0
Boosts
0
Views
787
Activity
Jan ’23
NSScrollView of a large graphic jumps when scrolling on OSX Ventura (13.0.1)
This is an internal app that displays a large NSView (50K x 25K pixels) within a NSScrollView the size of my 27" iMac. The code worked fine, scrolling both ways was very smooth, under the latest release of OS-X 12. I upgraded to 13.0.1 and now the scrolling jerks every ~ 500 pixels in either direction. I am using XCode 14.1 and writing in Objective-C. I notice the NSLog now contains the message "Metal API Validation Enabled" which it never did before. Is this a bug or has something fundamental changed in Ventura which requires changing the viewing code. (Memo to self: NEVER install a ".0" release. Let somebody else be the guinea pig.) Thanks for whatever help you can provide. Ron
Replies
1
Boosts
0
Views
985
Activity
Dec ’22
Metal not rendering color with low saturation and alpha
I'm using MTKView to render some triangles. Everything works fine until I decrease the color's saturation and opacity value which makes the triangle completely transparent. Creating SwiftUI's Color with the same values shows correctly. This only happens for colors with "low" saturation, if the color has 100% saturation (like #FF0000), it still renders fine even with just 1% opacity. I noticed if I change the colorPixelFormat of the MTKView, the result will change. So not sure if I only need to change the colorPixelFormat to fix this, in that case, I don't know which one either as I have limited knowledge about graphics. Here is an example for color #FF8888: bgra8Unorm: minimum 55% opacity for it to render bgra8Unorm_srgb: minimum 77% opacity for it to render and the color is a lot lighter than what it should be. In Swift, I store the colors as [Float], in MSL, it will be converted to float4*. Nothing fancy with the vertex and fragment functions, just returning the input. This is not very likely where the issue lies as other colors work. Some code to show my setup: // MTKView's setup clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0) isOpaque = false layer.magnificationFilter = .nearest layer.minificationFilter = .nearest // State setup let pipelineDescriptor = MTLRenderPipelineDescriptor() pipelineDescriptor.vertexFunction = vertexFunction pipelineDescriptor.fragmentFunction = fragmentFunction pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm pipelineState = try? device.makeRenderPipelineState(descriptor: pipelineDescriptor) // draw method setup guard let vertexBuffer = vertexBuffer, let indexBuffer = indexBuffer, let indexCount = indexCount, let colorBuffer = colorBuffer, let pipelineState = pipelineState, let discriptor = view.currentRenderPassDescriptor, let commandBuffer = commandQueue.makeCommandBuffer(), let commandEncoder = commandBuffer.makeRenderCommandEncoder( descriptor: discriptor ), let drawable = view.currentDrawable else { return } commandEncoder.setRenderPipelineState(pipelineState) commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0) commandEncoder.setVertexBuffer(colorBuffer, offset: 0, index: 1) commandEncoder.drawIndexedPrimitives( type: .triangle, indexCount: indexCount, indexType: .uint32, indexBuffer: indexBuffer, indexBufferOffset: 0 ) commandEncoder.endEncoding() commandBuffer.present(drawable) commandBuffer.commit()
Replies
4
Boosts
0
Views
1.9k
Activity
Dec ’22
Iranian app
Hi there I need to use Iranian Application for many things but I can’t find in App Store plz give me Adhoc certificate for that Thanks &Regards
Replies
1
Boosts
0
Views
2.2k
Activity
Dec ’22