metal

Rendering small CIImage centered in MTKView

你。 提交于 2019-12-23 04:58:19
问题 I'm rendering a CIImage to MTKView and the image is smaller than the drawable. let centered = image.transformed(by: CGAffineTransform(translationX: (view.drawableSize.width - image.extent.width) / 2, y: (view.drawableSize.height - image.extent.height) / 2)) context.render(centered, to: drawable.texture, commandBuffer: buffer, bounds: centered.extent, colorSpace: CGColorSpaceCreateDeviceRGB()) I'd expect the code above to render the image in the center of the view, but the image is positioned

MTLBuffer allocation + CPU/GPU synchronisation

二次信任 提交于 2019-12-23 04:47:45
问题 I am using a metal performance shader( MPSImageHistogram ) to compute something in an MTLBuffer that I grab, perform computations, and then display via MTKView . The MTLBuffer output from the shader is small (~4K bytes). So I am allocating a new MTLBuffer object for every render pass, and there are atleast 30 renders per second for every video frame. calculation = MPSImageHistogram(device: device, histogramInfo: &histogramInfo) let bufferLength = calculation.histogramSize(forSourceFormat:

Replicate OpenGL Blending in Metal

瘦欲@ 提交于 2019-12-22 12:22:56
问题 I have been working on porting an open source game written using a fixed function pipeline to Metal. I have been able to redo all the projection transformation and have things being drawn where they should be drawn with the correct texture and vertex information but I'm having lots of trouble getting the fragment shader to match the OpenGL blending, the textures look correct but the blending and brightness are off. Here is what I'm trying to match: here is how it is currently rendering: The

Metal shader in SceneKit to outline an object

对着背影说爱祢 提交于 2019-12-22 10:55:30
问题 I'm playing around and trying to implement Metal shader in SceneKit that will outline an object. The idea is to draw an outline (or silhouette) similar to this image found in this blogpost (the blogpost doesn't contain any code): I'm new to SceneKit and Metal shaders so I'm able just to draw some geometry and write simple vertex or fragment shader. I'm curious how can I achieve this kind of effect? Is it done in multiple passes? Cheers! 回答1: The basic idea here is to clone the "selected" node

Can I get the size of a buffer from my Metal shader?

混江龙づ霸主 提交于 2019-12-22 08:36:45
问题 In my iOS app, written in Swift, I generate a Metal buffer with: vertexBuffer = device.newBufferWithBytes(vertices, length: vertices.count * sizeofValue(vertices[0]), options: nil) And bind it to my shader program with: renderCommandEncoder.setVertexBuffer(vertexBuffer, offset: 0, atIndex: 1) In my shader program, written in Metal shading language, can I access the size of the buffer? I would like to access the next vertex in my buffer to do some differential calculation. Something like:

Compiling and running the metal examples

我的未来我决定 提交于 2019-12-22 03:09:27
问题 It has been a while since I have used XCode, but since Apple revieled XCode 6 and the new Metal API I had to check it out. They have released examples, e.g. a converted version of their Basic3D example. I am having problems making them compile and run, though. I had to add paths the Quatz and Metal frameworks for compilation and linking to work - thought that should have worked out of the box with build-in libraries? Also the metal shader compilation step fails, it can not find the metal

Compiling and running the metal examples

倾然丶 夕夏残阳落幕 提交于 2019-12-22 03:09:09
问题 It has been a while since I have used XCode, but since Apple revieled XCode 6 and the new Metal API I had to check it out. They have released examples, e.g. a converted version of their Basic3D example. I am having problems making them compile and run, though. I had to add paths the Quatz and Metal frameworks for compilation and linking to work - thought that should have worked out of the box with build-in libraries? Also the metal shader compilation step fails, it can not find the metal

Memory usage keeps rising on older devices using Metal

▼魔方 西西 提交于 2019-12-21 15:18:13
问题 I use Metal and CADisplayLink to live filter a CIImage and render it into a MTKView . // Starting display link displayLink = CADisplayLink(target: self, selector: #selector(applyAnimatedFilter)) displayLink.preferredFramesPerSecond = 30 displayLink.add(to: .current, forMode: .default) @objc func applyAnimatedFilter() { ... metalView.image = filter.applyFilter(image: ciImage) } According to the memory monitor in Xcode, memory usage is stable on iPhone X and never goes above 100mb, on devices

How to use UnsafeMutableRawPointer to fill an array?

僤鯓⒐⒋嵵緔 提交于 2019-12-21 15:00:05
问题 I have a Metal texture, I want to access its data from Swift by making it a float4 array (so that I can access each pixel 4 color components). I discovered this method of MTLTexture : getBytes(UnsafeMutableRawPointer, bytesPerRow: Int, bytesPerImage: Int, from: MTLRegion, mipmapLevel: Int, slice: Int) I don't know at all how to use UnsafeMutableRawPointer, how it works, and how to get the data back into a simple Swift array. My first attempt was to create a pointer and allocate enough space

Efficiently copying Swift Array to memory buffer for iOS Metal

徘徊边缘 提交于 2019-12-20 15:45:12
问题 I am writing an iOS application using Apple's new Metal framework. I have an array of Matrix4 objects (see Ray Wenderlich's tutorial) that I need to pass in to a shader via the MTLDevice.newBufferWithLength() method. The Matrix4 object is leveraging Apple's GLKit (it contains a GLKMatrix4 object). I'm leveraging instancing with the GPU calls. I will later change this to a struct which includes more data per instance (beyond just the Matrix4 object. How can I efficiently copy the array of