metal

Allocating memory for data used by MTLBuffer in iOS Metal

萝らか妹 提交于 2019-12-01 03:28:14
问题 As a follow-up question to this answer. I am trying to replace a for-loop running on CPU with a kernel function in Metal to parallelize computation and speed up performance. My function is basically a convolution. Since I repeatedly receive new data for my input array values (the data stems from a AVCaptureSession ) it seems that using newBufferWithBytesNoCopy:length:options:deallocator: is the sensible option for creating the MTLBuffer objects. Here is the relevant code: id <MTLBuffer>

Texture Brush (Drawing Application ) Using Metal

跟風遠走 提交于 2019-12-01 01:54:34
I am trying to implement a metal-backed drawing application where brushstrokes are drawn on an MTKView by textured square repeatedly along a finger position. I am drawing this with alpha 0.2. When the squares are overlapped the color is added. How can I draw with alpha 0.2. I think you need to draw the brush squares to a separate texture, initially cleared to transparent, without blending. Then draw that whole texture to your view with blending. If you draw the brush squares directly to the view, then they will accumulate. After you draw square 1, it's part of the image. Metal can no longer

Compile each .metal file into a separate .metallib

浪子不回头ぞ 提交于 2019-11-30 17:46:23
问题 I would like to override the default Xcode Metal compiler behaviour of compiling all project .metal files into a single default.metallib and instead compile each .metal file into a separate .metallib file. However I cannot see how to approach this; can anyone provide a lead? Xcode version: 10.1 回答1: I achieved this in two steps: 1) Add a custom build rule for .metal files that compiles them individually into .metallib s: 2) Add a custom build step for copying those .metallib s into your

How to Speed Up Metal Code for iOS/Mac OS

Deadly 提交于 2019-11-30 14:09:42
问题 I'm trying to implement code in Metal that performs a 1D convolution between two vectors with lengths. I've implemented the following which works correctly kernel void convolve(const device float *dataVector [[ buffer(0) ]], const device int& dataSize [[ buffer(1) ]], const device float *filterVector [[ buffer(2) ]], const device int& filterSize [[ buffer(3) ]], device float *outVector [[ buffer(4) ]], uint id [[ thread_position_in_grid ]]) { int outputSize = dataSize - filterSize + 1; for

Holding onto a MTLTexture from a CVImageBuffer causes stuttering

拟墨画扇 提交于 2019-11-30 08:37:53
I'm creating a MTLTexture from CVImageBuffer s (from camera and players) using CVMetalTextureCacheCreateTextureFromImage to get a CVMetalTexture and then CVMetalTextureGetTexture to get the MTLTexture . The problem I'm seeing is that when I later render the texture using Metal, I occasionally see video frames rendered out of order (visually it stutters back and forth in time), presumably because CoreVideo is modifying the underlying CVImageBuffer storage and the MTLTexture is just pointing there. Is there any way to make CoreVideo not touch that buffer and use another one from its pool until I

Metal Shader with SceneKit SCNProgram

时光怂恿深爱的人放手 提交于 2019-11-30 05:36:38
I'm looking for just a working Metal shader that works in SceneKit with SCNProgram. Can someone show me the correct method declarations/how to hook this up? let program = SCNProgram() program.vertexFunctionName = "myVertex" program.fragmentFunctionName = "myFragment" material.program = program and then the shader //MyShader.metal vertex something myVertex(something) { return something; } fragment float4 myFragment(something) { return something } I'm just looking for the most basic example please. I clipped out all the 'unnecessary' stuff, this is about as basic as it gets and pretty much what

Off Screen Rendering Metal

只愿长相守 提交于 2019-11-29 15:48:34
问题 func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) { print("current drawable size:\(view.drawableSize)") } func draw(in view: MTKView) { guard let drawable = view.currentDrawable else { return } let textureDescriptor = MTLTextureDescriptor() textureDescriptor.textureType = MTLTextureType.type2D textureDescriptor.width = drawable.texture.width textureDescriptor.height = drawable.texture.height textureDescriptor.pixelFormat = .bgra8Unorm textureDescriptor.storageMode = .shared

Holding onto a MTLTexture from a CVImageBuffer causes stuttering

假装没事ソ 提交于 2019-11-29 11:25:32
问题 I'm creating a MTLTexture from CVImageBuffer s (from camera and players) using CVMetalTextureCacheCreateTextureFromImage to get a CVMetalTexture and then CVMetalTextureGetTexture to get the MTLTexture . The problem I'm seeing is that when I later render the texture using Metal, I occasionally see video frames rendered out of order (visually it stutters back and forth in time), presumably because CoreVideo is modifying the underlying CVImageBuffer storage and the MTLTexture is just pointing

Metal MTLTexture replaces semi-transparent areas with black when alpha values that aren't 1 or 0

前提是你 提交于 2019-11-29 09:34:55
While using Apple's texture importer , or my own, a white soft-edged circle drawn in software (with a transparent bg) or in Photoshop (saved as a PNG) when rendered will have its semi-transparent colors replaced with black when brought into Metal. Below is a screen grab from Xcode's Metal debugger, you can see the texture before being sent to shaders. Image located here (I'm not high ranked enough to embed) In Xcode, finder, and when put into an UIImageView, the source texture does not have the ring. But somewhere along the UIImage -> CGContex -> MTLTexture process (I'm thinking specifically

Take a snapshot of current screen with Metal in swift

隐身守侯 提交于 2019-11-29 08:06:11
I tried: let scale = UIScreen.mainScreen().scale UIGraphicsBeginImageContextWithOptions(metalLayer.bounds.size,false,scale) // metalLayer.renderInContext(UIGraphicsGetCurrentContext()!) // self.view.layer ... metalLayer.presentationLayer()!.renderInContext(UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) But the result is a empty screenshot. Any help would be nice! Please keep in mind that i want to take a snapshot of a CAMetalLayer To make a screenshot, you need to get