metal

Manually set a 1D Texture in Metal

佐手、 提交于 2019-12-04 19:17:52
I'm trying to fill a 1D texture with values manually and pass that texture to a compute shader (these are 2 pixels that I want to set via code, they don't represent any image). Due to the current small amount of Metal examples, all examples I could find deal with 2D textures that load the texture by converting a loaded UIImage to raw bytes data, but creating a dummy UIImage felt like a hack for me. This is the "naive" way I started with - ... var manualTextureData: [Float] = [ 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0 ]; let region: MTLRegion = MTLRegionMake1D(0, textureDescriptor.width);

Convert uintptr_t to id<MTLTexture>

对着背影说爱祢 提交于 2019-12-04 17:57:37
I wanted to create simple iOS plugin which can draw the texture to unity Texture2D. I've done it by CreateExternalTexture() and UpdateExternalTexture(), it's working fine, but I'm curious if I can actually fill the Unity texture straight from iOS side. Here's my code of iOS plugin: // // testTexturePlugin.m // Unity-iPhone // // Created by user on 18/01/16. // // #import <OpenGLES/ES2/gl.h> #import <OpenGLES/ES2/glext.h> #import <UIKit/UIKit.h> #include "UnityMetalSupport.h" #include <stdlib.h> #include <stdint.h> static UIImage* LoadImage() { NSString* imageName = @"logo"; //[NSString

Swift metal parallel sum calculation of array on iOS

偶尔善良 提交于 2019-12-04 15:31:19
Based on @Kametrixom answer , I have made some test application for parallel calculation of sum in an array. My test application looks like this: import UIKit import Metal class ViewController: UIViewController { // Data type, has to be the same as in the shader typealias DataType = CInt override func viewDidLoad() { super.viewDidLoad() let data = (0..<10000000).map{ _ in DataType(200) } // Our data, randomly generated var start, end : UInt64 var result:DataType = 0 start = mach_absolute_time() data.withUnsafeBufferPointer { buffer in for elem in buffer { result += elem } } end = mach_absolute

Making CIContext.render(CIImage, CVPixelBuffer) work with AVAssetWriter

五迷三道 提交于 2019-12-04 12:44:10
I want to use Core Image for processing a bunch of CGImage objects and turning them into a QuickTime movie on macOS . The following code demonstrates what's needed, but the output contains a lot of blank (black) frames : import AppKit import AVFoundation import CoreGraphics import Foundation import CoreVideo import Metal // Video output url. let url: URL = try! FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("av.mov") try? FileManager.default.removeItem(at: url) // Video frame size, total frame count, frame rate

Passing Metal texture2d_array to SceneKit shader modifier

别等时光非礼了梦想. 提交于 2019-12-04 12:11:58
I want to create shader modifier for SCNMaterial (with SCNShaderModifierEntryPointSurface ) and pass Metal texture2d_array as custom input. To achieve that I try to use key-value coding setting parameter of type SCNMaterialProperty . From SceneKit manual on SCNMaterialProperty property contents : You can set a value for this property using any of the following types: ... A texture (SKTexture, MDLTexture, MTLTexture , or GLKTextureInfo) ... So, I construct MTLTexture : Create MTLTextureDescriptor with type MTLTextureType2DArray Create MTLTexture with MTLDevice 's newTextureWithDescriptor: Fill

Using a Metal shader in SceneKit

Deadly 提交于 2019-12-04 09:59:54
I'd like to use a Metal shader to apply a toon/cell shading to materials used in the scene. The shader I'm trying to implement is Apple's own AAPLCelShader found in the MetalShaderShowcase . I'm a little new to implementing shaders in SceneKit so I could be doing everything completely wrong. From what I understand in the documentation a Metal or GL shader can be used to modify SceneKit rendering as of iOS 9 and Xcode 7. The documentation suggests that this is done in the same way as GL shaders. My method is to try and get the path for the .metal file and then load it into a string to be used

Memory usage keeps rising on older devices using Metal

徘徊边缘 提交于 2019-12-04 08:23:16
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 like iPhone 6 or iPhone 6s the memory usage keeps growing until eventually the system kills the app. I

Crop and scale MTLTexture

天大地大妈咪最大 提交于 2019-12-04 05:57:48
Can I create a new MTLTexture of dimensions w2/h2 of an existing MTLTexture region x1/y1/w1/h1 ? PS: I thought about using MTLTexture.buffer?.makeTexture but the offset needs to be 64 bytes. Why? Here's an example of how you might do this with MPSImageLanczosScale . Note that sourceRegion is expressed in the pixel coordinate system of the source texture, and destRegion should be equal to the full area of the destination texture (note that it specifically doesn't account for the origin of the destination region): let scaleX = Double(destRegion.size.width) / Double(sourceRegion.size.width) let

Display JPEG image using MTKView

寵の児 提交于 2019-12-04 04:29:49
问题 Is there a way to display JPEG image via MTKView and MTLBuffer(and within iPhone 6+). I've tried this in a following way(this was just test): - (id<MTLBuffer>)testBuffer { if (!_testBuffer) { // NSString *path = [[NSBundle mainBundle] pathForResource:@"testImage" ofType:@"jpg"]; NSData *imageData = [NSData dataWithContentsOfFile:path]; _testBuffer = [self.device newBufferWithBytes:imageData.bytes length:imageData.length options:MTLResourceCPUCacheModeWriteCombined]; } return _testBuffer; } -

iOS code to identify metal support in runtime?

 ̄綄美尐妖づ 提交于 2019-12-04 02:13:11
Usually, I use the below code to identify the iOS version of the device. if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) In a similar way, I'm trying to find Metal support for the device. Metal is supported for Apple devices with the A7 (or better) GPU and iOS 8.0. This is the way I expect my code to work: if (MetalSupported == true) { // metal programming } else { // opengles2 programming } How do I get the value for the Boolean variable MetalSupported ? rickster It's good that you're looking for something specific to Metal — generally, iOS version checks and hardware name