metal

How to use texture2d_array array in metal?

北城以北 提交于 2019-12-06 10:08:12
I have been trying to use texture2d_array for my application of live filters in metal. But I'm not getting the proper result. Im creating the texture array like this, Code: Class MetalTextureArray . class MetalTextureArray { private(set) var arrayTexture: MTLTexture private var width: Int private var height: Int init(_ width: Int, _ height: Int, _ arrayLength: Int, _ device: MTLDevice) { self.width = width self.height = height let textureDescriptor = MTLTextureDescriptor() textureDescriptor.textureType = .type2DArray textureDescriptor.pixelFormat = .bgra8Unorm textureDescriptor.width = width

Swift metal parallel sum calculation of array on iOS

对着背影说爱祢 提交于 2019-12-06 09:13:11
问题 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(

Passing Metal texture2d_array to SceneKit shader modifier

倾然丶 夕夏残阳落幕 提交于 2019-12-06 08:42:55
问题 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

Metal rendering really slow - how to speed it up

帅比萌擦擦* 提交于 2019-12-06 08:28:44
I have a working metal application that is extremely slow, and needs to run faster. I believe the problem is I am creating too many MTLCommandBuffer objects. The reason I am creating so many MTLCommandBuffer objects is I need to send different uniform values to the pixel shader. I've pasted a snippit of code to illustrate the problem below. for (int obj_i = 0 ; obj_i < n ; ++obj_i) { // I create one render command buffer per object I draw so I can use different uniforms id <MTLCommandBuffer> mtlCommandBuffer = [metal_info.g_commandQueue commandBuffer]; id <MTLRenderCommandEncoder>

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

為{幸葍}努か 提交于 2019-12-06 07:47:34
问题 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

Using a Metal shader in SceneKit

守給你的承諾、 提交于 2019-12-06 06:14:37
问题 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

Update contents of MTLBuffer in Metal

二次信任 提交于 2019-12-06 06:09:53
问题 I need help to replace the contents of a MTLBuffer without creating a new one. Content in both cases are Float Arrays. let vector:[Float] = [0,1,2,3,4,5,6,7,8,9] let byteLength = arr1.count*MemoryLayout<Float>.size let buffer = metalDevice.makeBuffer(bytes: &vector, length: byteLength, options: MTLResourceOptions()) let vector2:[Float] = [10,20,30,40,50,60,70,80,90] I understand buffer.contents() gives us a UnsafeMutableRawPointer . I would like to create a new UnsafeMutableRawPointer from

Replicate OpenGL Blending in Metal

﹥>﹥吖頭↗ 提交于 2019-12-06 05:38: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 code I can see controlling the GL Blending looks like: glShadeModel(GL_SMOOTH); glEnable(GL_ALPHA_TEST);

Metal Custom CIFilter different return value

折月煮酒 提交于 2019-12-06 05:11:54
I'm writing CIFilter, but result pixel colors are different than returned values from metal function. kernel.metal #include <CoreImage/CoreImage.h> extern "C" { namespace coreimage { float4 foo(sample_t rgb){ return float4(0.3f, 0.5f, 0.7f, 1.0f); } } MetalFilter.swift import CoreImage class MetalFilter: CIFilter { private let kernel: CIColorKernel var inputImage: CIImage? override init() { let url = Bundle.main.url(forResource: "default", withExtension: "metallib")! let data = try! Data(contentsOf: url) kernel = try! CIColorKernel(functionName: "foo", fromMetalLibraryData: data) super.init()

SCN shader modifier in metal - pass uniform to shader

*爱你&永不变心* 提交于 2019-12-06 02:58:23
I'm trying to use shaders modifiers with Metal. I cannot figure out to declare uniforms... So far my fragment modifier is: // color changes #pragma arguments float4x4 u_color_transformation; #pragma body _output.color.rgb = vec3(1.0) - (u_color_transformation * _output.color).rgb; This outputs a purple texture, with no log.. If I just have _output.color.rgb = (u_color_transformation * _output.color).rgb , things are ok. I think I'm following the doc but maybe not! The uniform is set with: material.shaderModifiers = [ SCNShaderModifierEntryPoint.fragment: theShaderAbove ] material.setValue