public static CIContext FromMetalDevice(IMTLDevice device, CIContextOptions options) { if (options == null) return FromMetalDevice (device); return FromMetalDevice (device, options.Dictionary); }
SlimMPSCnnFullyConnected (IMTLDevice device, MPSCnnConvolutionDescriptor convolutionDescriptor, IntPtr kernelWeights, IntPtr biasTerms, MPSCnnConvolutionFlags flags) : base (NSObjectFlag.Empty) { var sel = "initWithDevice:convolutionDescriptor:kernelWeights:biasTerms:flags:"; var selector = Selector.GetHandle (sel); InitializeHandle (IntPtr_objc_msgSend_IntPtr_IntPtr_IntPtr_IntPtr_UInt64 (Handle, selector, device.Handle, convolutionDescriptor.Handle, kernelWeights, biasTerms, (ulong)flags), sel); }
public static SlimMPSCnnFullyConnected Create (uint kernelWidth, uint kernelHeight, uint inputFeatureChannels, uint outputFeatureChannels, MPSCnnNeuron neuronFilter, IMTLDevice device, string kernelParamsBinaryName, uint destinationFeatureChannelOffset = 0) { // get the url to this layer's weights and bias var wtPath = NSBundle.MainBundle.PathForResource ($"weights_{kernelParamsBinaryName}", "dat"); var bsPath = NSBundle.MainBundle.PathForResource ($"bias_{kernelParamsBinaryName}", "dat"); unsafe { using (var mmfW = MemoryMappedFile.CreateFromFile (wtPath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read)) using (var mmfB = MemoryMappedFile.CreateFromFile (bsPath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read)) using (var wView = mmfW.CreateViewAccessor (0, 0, MemoryMappedFileAccess.Read)) using (var bView = mmfB.CreateViewAccessor (0, 0, MemoryMappedFileAccess.Read)) { byte* w = null; wView.SafeMemoryMappedViewHandle.AcquirePointer (ref w); byte* b = null; bView.SafeMemoryMappedViewHandle.AcquirePointer (ref b); var convDesc = MPSCnnConvolutionDescriptor.GetConvolutionDescriptor ( kernelWidth, kernelHeight, inputFeatureChannels, outputFeatureChannels, neuronFilter); return new SlimMPSCnnFullyConnected (device, convDesc, (IntPtr)w, (IntPtr)b, MPSCnnConvolutionFlags.None) { DestinationFeatureChannelOffset = destinationFeatureChannelOffset }; } } }
public static MTKMesh[] FromAsset(MDLAsset asset, IMTLDevice device, out MDLMesh [] sourceMeshes, out NSError error) { NSArray aret; var ret = FromAsset (asset, device, out aret, out error); sourceMeshes = NSArray.FromArray<MDLMesh> (aret); return ret; }
public ImageView (IntPtr handle) : base (handle) { Opaque = true; BackgroundColor = UIColor.Clear; metalLayer = (CAMetalLayer)Layer; device = MTLDevice.SystemDefault; metalLayer.Device = device; metalLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm; metalLayer.FramebufferOnly = true; }
public static CVMetalTextureCache FromDevice(IMTLDevice metalDevice) { if (metalDevice == null) throw new ArgumentNullException ("metalDevice"); IntPtr handle; if (CVMetalTextureCacheCreate (IntPtr.Zero, IntPtr.Zero, /* change one day to support cache attributes */ metalDevice.Handle, IntPtr.Zero, /* change one day to support texture attribuets */ out handle) == 0) return new CVMetalTextureCache (handle); return null; }
public CVMetalTextureCache(IMTLDevice metalDevice) { if (metalDevice == null) throw new ArgumentNullException ("metalDevice"); if (CVMetalTextureCacheCreate (IntPtr.Zero, IntPtr.Zero, /* change one day to support cache attributes */ metalDevice.Handle, IntPtr.Zero, /* change one day to support texture attribuets */ out handle) == 0) return; throw new Exception ("Could not create the texture cache"); }
public MetalKitEssentialsMesh(MTKMesh mtkMesh, MDLMesh mdlMesh, IMTLDevice device) { mesh = mtkMesh; submeshes = new List<MetalKitEssentialsSubmesh> (); if ((nuint)mtkMesh.Submeshes.Length != mdlMesh.Submeshes.Count) throw new Exception ("Number od submeshes should be equal"); for (int i = 0; i < mtkMesh.Submeshes.Length; i++) { // Create our own app specifc submesh to hold the MetalKit submesh. var submesh = new MetalKitEssentialsSubmesh (mtkMesh.Submeshes[i], mdlMesh.Submeshes.GetItem <MDLSubmesh>((nuint)i), device); submeshes.Add (submesh); } }
public GameView (IntPtr handle) : base (handle) { Opaque = true; BackgroundColor = UIColor.Clear; metalLayer = (CAMetalLayer)Layer; ContentScaleFactor = UIScreen.MainScreen.Scale; metalLayer.PresentsWithTransaction = false; metalLayer.DrawsAsynchronously = true; device = MTLDevice.SystemDefault; metalLayer.Device = device; metalLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm; metalLayer.FramebufferOnly = true; drawableSize = Bounds.Size; }
public CVMetalTextureCache(IMTLDevice metalDevice) { if (metalDevice == null) { throw new ArgumentNullException("metalDevice"); } if (CVMetalTextureCacheCreate(IntPtr.Zero, IntPtr.Zero, /* change one day to support cache attributes */ metalDevice.Handle, IntPtr.Zero, /* change one day to support texture attribuets */ out handle) == 0) { return; } throw new Exception("Could not create the texture cache"); }
public static CVMetalTextureCache FromDevice(IMTLDevice metalDevice, CVMetalTextureAttributes textureAttributes, out CVReturn creationErr) { if (metalDevice == null) { throw new ArgumentNullException(nameof(metalDevice)); } IntPtr handle; creationErr = (CVReturn)CVMetalTextureCacheCreate(IntPtr.Zero, IntPtr.Zero, /* change one day to support cache attributes */ metalDevice.Handle, textureAttributes?.Dictionary.Handle ?? IntPtr.Zero, out handle); if (creationErr == CVReturn.Success) { return(new CVMetalTextureCache(handle)); } return(null); }
void GeneratePipelineStates(IMTLDevice device) { var states = new List <IMTLRenderPipelineState>(); foreach (var subpass in RenderPass.Subpasses) { var pipelineDescriptor = new MTLRenderPipelineDescriptor { Label = "Magnesium", VertexFunction = VertexFunction, FragmentFunction = FragmentFunction, VertexDescriptor = GetVertexDescriptor(), AlphaToCoverageEnabled = AlphaToCoverageEnabled, RasterizationEnabled = !RasterizationDiscardEnabled, AlphaToOneEnabled = AlphaToOneEnabled, }; subpass.InitializeFormat(pipelineDescriptor); for (var i = 0; i < Attachments.Length; ++i) { var attachment = Attachments[i]; pipelineDescriptor.ColorAttachments[i].BlendingEnabled = attachment.IsBlendingEnabled; pipelineDescriptor.ColorAttachments[i].RgbBlendOperation = attachment.RgbBlendOperation; pipelineDescriptor.ColorAttachments[i].AlphaBlendOperation = attachment.AlphaBlendOperation; pipelineDescriptor.ColorAttachments[i].SourceRgbBlendFactor = attachment.SourceRgbBlendFactor; pipelineDescriptor.ColorAttachments[i].SourceAlphaBlendFactor = attachment.SourceAlphaBlendFactor; pipelineDescriptor.ColorAttachments[i].DestinationRgbBlendFactor = attachment.DestinationRgbBlendFactor; pipelineDescriptor.ColorAttachments[i].DestinationAlphaBlendFactor = attachment.DestinationAlphaBlendFactor; pipelineDescriptor.ColorAttachments[i].WriteMask = attachment.ColorWriteMask; } Foundation.NSError err; var pipelineState = device.CreateRenderPipelineState(pipelineDescriptor, out err); if (pipelineState == null) { throw new InvalidOperationException("METAL : error " + err.ToString()); } states.Add(pipelineState); } PipelineStates = states.ToArray(); }
public AmtDeviceMemory(IMTLDevice device, MgMemoryAllocateInfo pAllocateInfo) { if (pAllocateInfo == null) { throw new ArgumentNullException(nameof(pAllocateInfo)); } if (pAllocateInfo.AllocationSize > nuint.MaxValue) { throw new ArgumentOutOfRangeException(nameof(pAllocateInfo.AllocationSize) + " must be <= nuint.MaxValue"); } AllocationSize = (nuint)pAllocateInfo.AllocationSize; var options = MTLResourceOptions.CpuCacheModeDefault; InternalBuffer = device.CreateBuffer(AllocationSize, options); }
/// <summary> /// Initializes a fully connected kernel. /// Returns: A valid SlimMPSCnnConvolution object or null, if failure. /// </summary> /// <param name="kernelWidth">Kernel Width</param> /// <param name="kernelHeight">Kernel Height</param> /// <param name="inputFeatureChannels">Number feature channels in input of this layer</param> /// <param name="outputFeatureChannels">Number feature channels from output of this layer</param> /// <param name="neuronFilter">A neuronFilter to add at the end as activation (could be null)</param> /// <param name="device">The IMTLDevice on which this SlimMPSCnnConvolution filter will be used</param> /// <param name="kernelParamsBinaryName">name of the layer to fetch kernelParameters by adding a prefix "weights_" or "bias_"</param> /// <param name="padding">Bool value whether to use padding or not</param> /// <param name="strideX">Stride of the filter</param> /// <param name="strideY">Stride of the filter</param> /// <param name="destinationFeatureChannelOffset">FeatureChannel no. in the destination MPSImage to start writing from, helps with concat operations</param> /// <param name="groupNum">if grouping is used, default value is 1 meaning no groups</param> public static SlimMPSCnnConvolution Create (uint kernelWidth, uint kernelHeight, uint inputFeatureChannels, uint outputFeatureChannels, MPSCnnNeuron neuronFilter, IMTLDevice device, string kernelParamsBinaryName, bool padding, uint strideX, uint strideY, uint destinationFeatureChannelOffset, uint groupNum) { // get the url to this layer's weights and bias var wtPath = NSBundle.MainBundle.PathForResource ($"weights_{kernelParamsBinaryName}", "dat"); var bsPath = NSBundle.MainBundle.PathForResource ($"bias_{kernelParamsBinaryName}", "dat"); // create appropriate convolution descriptor with appropriate stride var convDesc = MPSCnnConvolutionDescriptor.GetConvolutionDescriptor ( kernelWidth, kernelHeight, inputFeatureChannels, outputFeatureChannels, neuronFilter); convDesc.StrideInPixelsX = strideX; convDesc.StrideInPixelsY = strideY; if (groupNum <= 0) throw new ArgumentException ("Group size can't be less than 1"); convDesc.Groups = groupNum; unsafe { using (var mmfW = MemoryMappedFile.CreateFromFile (wtPath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read)) using (var mmfB = MemoryMappedFile.CreateFromFile (bsPath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read)) using (var wView = mmfW.CreateViewAccessor (0, 0, MemoryMappedFileAccess.Read)) using (var bView = mmfB.CreateViewAccessor (0, 0, MemoryMappedFileAccess.Read)) { byte* w = null; wView.SafeMemoryMappedViewHandle.AcquirePointer (ref w); byte* b = null; bView.SafeMemoryMappedViewHandle.AcquirePointer (ref b); return new SlimMPSCnnConvolution (device, convDesc, (IntPtr)w, (IntPtr)b, MPSCnnConvolutionFlags.None) { DestinationFeatureChannelOffset = destinationFeatureChannelOffset, padding = padding }; } } }
public void InitRenderer(MTKView view, IMTLDevice device) { #if BGFX var platformData = new PlatformData(); platformData.WindowHandle = view.Handle; platformData.Context = device.Handle; Bgfx.Bgfx.SetPlatformData(platformData); var settings = new InitSettings(); settings.Backend = RendererBackend.Metal; settings.Width = (int)view.Bounds.Width; settings.Height = (int)view.Bounds.Height; settings.ResetFlags = ResetFlags.Vsync; settings.PlatformData = platformData; //settings.limits.maxEncoders = 128; Bgfx.Bgfx.ManuallyRenderFrame(); Bgfx.Bgfx.Init(settings); var vertexLayout = new VertexLayout(); vertexLayout.Begin(RendererBackend.Metal); vertexLayout.Add(VertexAttributeUsage.Position, 3, VertexAttributeType.Float); vertexLayout.Add(VertexAttributeUsage.Color0, 4, VertexAttributeType.UInt8, true); vertexLayout.End(); var vertexBuffer = new VertexBuffer(MemoryBlock.FromArray(Data.cubeVertices), vertexLayout); var indexBuffer = new IndexBuffer(MemoryBlock.FromArray(Data.cubeTriList)); var vertexShader = LoadShader("vs_cubes.bin"); var fragmentShader = LoadShader("fs_cubes.bin"); var program = new Program(vertexShader, fragmentShader); _vertexBuffer = vertexBuffer; _indexBuffer = indexBuffer; _program = program; Bgfx.Bgfx.Touch(0); #endif view.Delegate = this; }
static IntPtr Create(IMTLDevice metalDevice, CVMetalTextureAttributes?textureAttributes) { if (metalDevice is null) { throw new ArgumentNullException(nameof(metalDevice)); } CVReturn err = (CVReturn)CVMetalTextureCacheCreate(IntPtr.Zero, IntPtr.Zero, /* change one day to support cache attributes */ metalDevice.Handle, textureAttributes.GetHandle(), out var handle); if (err == CVReturn.Success) { return(handle); } throw new Exception($"Could not create the texture cache, Reason: {err}."); }
public MnistFullLayerNeuralNetwork (IMTLCommandQueue commandQueueIn) { // CommandQueue to be kept around commandQueue = commandQueueIn; device = commandQueueIn.Device; // Initialize MPSImage from descriptors SrcImage = new MPSImage (device, SID); dstImage = new MPSImage (device, DID); // setup convolution layer (which is a fully-connected layer) // cliprect, offset is automatically set layer = SlimMPSCnnFullyConnected.Create (kernelWidth: 28, kernelHeight: 28, inputFeatureChannels: 1, outputFeatureChannels: 10, neuronFilter: null, device: device, kernelParamsBinaryName: "NN"); // prepare softmax layer to be applied at the end to get a clear label softmax = new MPSCnnSoftMax (device); }
public IMTLLibrary LoadLibrary(IMTLDevice device, MemoryStream ms) { using (var tr = new StreamReader(ms)) { string source = tr.ReadToEnd(); var options = new MTLCompileOptions { LanguageVersion = MTLLanguageVersion.v1_1, FastMathEnabled = false, }; NSError err; IMTLLibrary library = device.CreateLibrary(source, options, out err); if (library == null) { // TODO: better error handling throw new Exception(err.ToString()); } return(library); } }
public void Metal() { TestRuntime.AssertDevice(); TestRuntime.AssertXcodeVersion(10, 0); device = MTLDevice.SystemDefault; // some older hardware won't have a default if (device == null || !MPSKernel.Supports(device)) { Assert.Inconclusive("Metal is not supported"); } cache = NSArray <MPSImage> .FromNSObjects( new MPSImage (device, MPSImageDescriptor.GetImageDescriptor(MPSImageFeatureChannelFormat.Float32, 224, 224, 3)), new MPSImage (device, MPSImageDescriptor.GetImageDescriptor(MPSImageFeatureChannelFormat.Float32, 224, 224, 3)), new MPSImage (device, MPSImageDescriptor.GetImageDescriptor(MPSImageFeatureChannelFormat.Float32, 224, 224, 3)), new MPSImage (device, MPSImageDescriptor.GetImageDescriptor(MPSImageFeatureChannelFormat.Float32, 224, 224, 3)), new MPSImage (device, MPSImageDescriptor.GetImageDescriptor(MPSImageFeatureChannelFormat.Float32, 224, 224, 3)) ); }
public void Metal() { #if !MONOMAC TestRuntime.AssertXcodeVersion(7, 0); if (Runtime.Arch == Arch.SIMULATOR) { Assert.Inconclusive("Metal Performance Shaders is not supported in the simulator"); } #else TestRuntime.AssertXcodeVersion(9, 0); #endif device = MTLDevice.SystemDefault; // some older hardware won't have a default if (device == null || !MPSKernel.Supports(device)) { Assert.Inconclusive("Metal is not supported"); } }
public void Metal() { #if !MONOMAC TestRuntime.AssertXcodeVersion(7, 0); if (Runtime.Arch == Arch.SIMULATOR && Environment.OSVersion.Version.Major >= 15) { Assert.Inconclusive("Metal is not supported in the simulator on macOS 10.15"); } #else TestRuntime.AssertXcodeVersion(9, 0); #endif device = MTLDevice.SystemDefault; // some older hardware won't have a default if (device == null || !MPSKernel.Supports(device)) { Assert.Inconclusive("Metal is not supported"); } }
public MnistFullLayerNeuralNetwork(IMTLCommandQueue commandQueueIn) { // CommandQueue to be kept around commandQueue = commandQueueIn; device = commandQueueIn.Device; // Initialize MPSImage from descriptors SrcImage = new MPSImage(device, SID); dstImage = new MPSImage(device, DID); // setup convolution layer (which is a fully-connected layer) // cliprect, offset is automatically set layer = SlimMPSCnnFullyConnected.Create(kernelWidth: 28, kernelHeight: 28, inputFeatureChannels: 1, outputFeatureChannels: 10, neuronFilter: null, device: device, kernelParamsBinaryName: "NN"); // prepare softmax layer to be applied at the end to get a clear label softmax = new MPSCnnSoftMax(device); }
public CVMetalTextureCache(IMTLDevice metalDevice, CVMetalTextureAttributes textureAttributes) { if (metalDevice == null) { throw new ArgumentNullException(nameof(metalDevice)); } CVReturn err = (CVReturn)CVMetalTextureCacheCreate(IntPtr.Zero, IntPtr.Zero, /* change one day to support cache attributes */ metalDevice.Handle, textureAttributes?.Dictionary.Handle ?? IntPtr.Zero, out handle); if (err == CVReturn.Success) { return; } throw new Exception($"Could not create the texture cache, Reason: {err}."); }
// Designated initializer public Quad (IMTLDevice device) { if (device == null) throw new Exception ("ERROR: Invalid device!"); vertexBuffer = device.CreateBuffer<Vector4> (quadVertices, MTLResourceOptions.CpuCacheModeDefault); if (vertexBuffer == null) Console.WriteLine ("ERROR: Failed creating a vertex buffer for a quad!"); vertexBuffer.Label = "quad vertices"; texCoordBuffer = device.CreateBuffer<Vector2> (quadTexCoords, MTLResourceOptions.CpuCacheModeDefault); if (texCoordBuffer == null) Console.WriteLine ("ERROR: Failed creating a 2d texture coordinate buffer!"); texCoordBuffer.Label = "quad texcoords"; vertexIndex = 0; texCoordIndex = 1; Aspect = 1f; scale = Vector2.One; }
public void Metal() { TestRuntime.AssertDevice(); TestRuntime.AssertXcodeVersion(10, 0); device = MTLDevice.SystemDefault; // some older hardware won't have a default if (device == null || !MPSKernel.Supports(device)) { Assert.Inconclusive("Metal is not supported"); } cache = NSArray <MPSState> .FromNSObjects( new MPSState (device, MTLTextureDescriptor.CreateTexture2DDescriptor(MTLPixelFormat.RGBA32Float, 220, 220, false)), new MPSState (device, MTLTextureDescriptor.CreateTexture2DDescriptor(MTLPixelFormat.RGBA32Float, 221, 221, false)), new MPSState (device, MTLTextureDescriptor.CreateTexture2DDescriptor(MTLPixelFormat.RGBA32Float, 222, 222, false)), new MPSState (device, MTLTextureDescriptor.CreateTexture2DDescriptor(MTLPixelFormat.RGBA32Float, 223, 223, false)), new MPSState (device, MTLTextureDescriptor.CreateTexture2DDescriptor(MTLPixelFormat.RGBA32Float, 224, 224, false)), new MPSState (device, MTLTextureDescriptor.CreateTexture2DDescriptor(MTLPixelFormat.RGBA32Float, 225, 225, false)) ); }
void SetupMetal() { // Set the view to use the default device. device = MTLDevice.SystemDefault; if (device != null) { // mark that metal is supported by this device isMetalSupported = true; // Create a new command queue. commandQueue = device.CreateCommandQueue(); // Load all the shader files with a metal file extension in the project. defaultLibrary = device.CreateDefaultLibrary(); } else { // that mean that device doesn't support Metal isMetalSupported = false; } }
public MetalClothSimulator(IMTLDevice device) { this.device = device; this.commandQueue = this.device.CreateCommandQueue(); this.defaultLibrary = this.device.CreateDefaultLibrary(); this.functionClothSim = this.defaultLibrary.CreateFunction("updateVertex"); this.functionNormalUpdate = this.defaultLibrary.CreateFunction("updateNormal"); this.functionNormalSmooth = this.defaultLibrary.CreateFunction("smoothNormal"); this.pipelineStateClothSim = this.device.CreateComputePipelineState(this.functionClothSim, out NSError pipelineStateClothSimError); this.pipelineStateNormalUpdate = this.device.CreateComputePipelineState(this.functionNormalUpdate, out NSError pipelineStateNormalUpdateError); this.pipelineStateNormalSmooth = this.device.CreateComputePipelineState(this.functionNormalSmooth, out NSError pipelineStateNormalSmoothError); if (pipelineStateClothSimError != null || pipelineStateNormalUpdateError != null || pipelineStateNormalSmoothError != null) { throw new Exception("error"); } }
private void InitializeShaderFunctions(IAmtMetalLibraryLoader generator, IMTLDevice device, MgPipelineShaderStageCreateInfo[] stages) { IMTLFunction frag = null; IMTLFunction vert = null; foreach (var stage in stages) { IMTLFunction shaderFunc = null; var module = (AmtShaderModule)stage.Module; Debug.Assert(module != null); // LIBRARY : can't store function unless name matches if (module.Library != null) { shaderFunc = module.Library.CreateFunction(stage.Name); } else { using (var ms = new MemoryStream()) { module.Info.Code.CopyTo(ms, (int)module.Info.CodeSize.ToUInt32()); ms.Seek(0, SeekOrigin.Begin); module.Library = generator.LoadLibrary(device, ms); } } shaderFunc = module.Library.CreateFunction(stage.Name); Debug.Assert(shaderFunc != null); if (stage.Stage == MgShaderStageFlagBits.VERTEX_BIT) { vert = shaderFunc; } else if (stage.Stage == MgShaderStageFlagBits.FRAGMENT_BIT) { frag = shaderFunc; } } VertexFunction = vert; FragmentFunction = frag; }
public MetalKitEssentialsSubmesh(MTKSubmesh mtkSubmesh, MDLSubmesh mdlSubmesh, IMTLDevice device) { materialUniforms = device.CreateBuffer ((nuint)Marshal.SizeOf <MaterialUniforms> (), MTLResourceOptions.CpuCacheModeDefault); var uniforms = Marshal.PtrToStructure <MaterialUniforms> (materialUniforms.Contents); submesh = mtkSubmesh; for (nuint i = 0; i < mdlSubmesh.Material.Count; i++) { MDLMaterialProperty property = ObjectAtIndexedSubscript (mdlSubmesh.Material, i); if (property == null) continue; if (property.Name == "baseColorMap") { if (property.Type != MDLMaterialPropertyType.String) continue; var textureURL = new NSUrl (string.Format ("file://{0}", property.StringValue)); var textureLoader = new MTKTextureLoader (device); NSError error; diffuseTexture = textureLoader.FromUrl (textureURL, null, out error); if (diffuseTexture == null) throw new Exception (string.Format ("Diffuse texture load: {0}", error.LocalizedDescription)); } else if (property.Name == "BlinnSpecularColor") { if (property.Type == MDLMaterialPropertyType.Float4) uniforms.specularColor = property.Float4Value; else if (property.Type == MDLMaterialPropertyType.Float3) uniforms.specularColor = new Vector4 (property.Float3Value); } else if (property.Name == "emission") { if(property.Type == MDLMaterialPropertyType.Float4) uniforms.emissiveColor = property.Float4Value; else if (property.Type == MDLMaterialPropertyType.Float3) uniforms.emissiveColor = new Vector4 (property.Float3Value); } } Marshal.StructureToPtr (uniforms, materialUniforms.Contents, true); }
public Renderer () { // initialize properties sampleCount = 1; depthPixelFormat = MTLPixelFormat.Depth32Float; stencilPixelFormat = MTLPixelFormat.Invalid; // find a usable Device device = MTLDevice.SystemDefault; // create a new command queue commandQueue = device.CreateCommandQueue (); NSError error; shaderLibrary = device.CreateLibrary ("default.metallib", out error); // if the shader libary isnt loading, nothing good will happen if (shaderLibrary == null) throw new Exception ("ERROR: Couldnt create a default shader library"); inflightSemaphore = new Semaphore (maxInflightBuffers, maxInflightBuffers); }
public override void ViewDidLoad () { base.ViewDidLoad (); // Load default device. device = MTLDevice.SystemDefault; // Make sure the current device supports MetalPerformanceShaders. if (!MPSKernel.Supports (device)) { Console.WriteLine ("Metal Performance Shaders not Supported on current Device"); return; } // Create new command queue. commandQueue = device.CreateCommandQueue (); // initialize the networks we shall use to detect digits neuralNetwork = new MnistFullLayerNeuralNetwork (commandQueue); neuralNetworkDeep = new MnistDeepConvNeuralNetwork (commandQueue); runningNet = neuralNetwork; }
public void SetUp() { device = MTLDevice.SystemDefault; // some older hardware won't have a default if (device == null) { Assert.Inconclusive("Metal is not supported"); } commandQ = device.CreateCommandQueue(); if (commandQ == null) // this happens on a simulator { Assert.Inconclusive("Could not get the functions library for the device."); } commandBuffer = commandQ.CommandBuffer(); if (commandBuffer == null) // happens on sim { Assert.Inconclusive("Could not get the command buffer for the device."); } encoder = commandBuffer.ComputeCommandEncoder; }
public static void ConvertSparseTileRegions(this IMTLDevice This, MTLRegion [] tileRegions, MTLRegion [] pixelRegions, MTLSize tileSize, nuint numRegions) { if (tileRegions == null) { throw new ArgumentNullException(nameof(tileRegions)); } if (pixelRegions == null) { throw new ArgumentNullException(nameof(pixelRegions)); } var tileRegionsHandle = GCHandle.Alloc(tileRegions, GCHandleType.Pinned); var pixelRegionsHandle = GCHandle.Alloc(pixelRegions, GCHandleType.Pinned); try { IntPtr tilePtr = tileRegionsHandle.AddrOfPinnedObject(); IntPtr pixelPtr = pixelRegionsHandle.AddrOfPinnedObject(); This.ConvertSparseTileRegions(tilePtr, pixelPtr, tileSize, numRegions); } finally { tileRegionsHandle.Free(); pixelRegionsHandle.Free(); } }
public void SetUp() { device = MTLDevice.SystemDefault; // some older hardware won't have a default if (device == null) { Assert.Inconclusive("Metal is not supported"); } library = device.CreateDefaultLibrary(); if (library == null) // this happens on a simulator { Assert.Inconclusive("Could not get the functions library for the device."); } if (library.FunctionNames.Length == 0) { Assert.Inconclusive("Could not get functions for the pipeline."); } function = library.CreateFunction(library.FunctionNames [0]); encoder = function.CreateArgumentEncoder(0); }
public override void ViewDidLoad() { base.ViewDidLoad(); // Load default device. device = MTLDevice.SystemDefault; // Make sure the current device supports MetalPerformanceShaders. if (!MPSKernel.Supports(device)) { Console.WriteLine("Metal Performance Shaders not Supported on current Device"); return; } // Create new command queue. commandQueue = device.CreateCommandQueue(); // initialize the networks we shall use to detect digits neuralNetwork = new MnistFullLayerNeuralNetwork(commandQueue); neuralNetworkDeep = new MnistDeepConvNeuralNetwork(commandQueue); runningNet = neuralNetwork; }
public static void ConvertSparsePixelRegions(this IMTLDevice This, MTLRegion [] pixelRegions, MTLRegion [] tileRegions, MTLSize tileSize, MTLSparseTextureRegionAlignmentMode mode, nuint numRegions) { if (tileRegions == null) { ObjCRuntime.ThrowHelper.ThrowArgumentNullException(nameof(tileRegions)); } if (pixelRegions == null) { ObjCRuntime.ThrowHelper.ThrowArgumentNullException(nameof(pixelRegions)); } var tileRegionsHandle = GCHandle.Alloc(tileRegions, GCHandleType.Pinned); var pixelRegionsHandle = GCHandle.Alloc(pixelRegions, GCHandleType.Pinned); try { IntPtr tilePtr = tileRegionsHandle.AddrOfPinnedObject(); IntPtr pixelPtr = pixelRegionsHandle.AddrOfPinnedObject(); This.ConvertSparsePixelRegions(pixelPtr, tilePtr, tileSize, mode, numRegions); } finally { tileRegionsHandle.Free(); pixelRegionsHandle.Free(); } }
public AmtSampler(IMTLDevice mDevice, MgSamplerCreateInfo pCreateInfo) { if (pCreateInfo == null) { throw new ArgumentNullException(nameof(pCreateInfo)); } var descriptor = new MTLSamplerDescriptor { SAddressMode = TranslateAddressMode(pCreateInfo.AddressModeU), TAddressMode = TranslateAddressMode(pCreateInfo.AddressModeV), RAddressMode = TranslateAddressMode(pCreateInfo.AddressModeW), MinFilter = TranslateMinFilter(pCreateInfo.MinFilter), MagFilter = TranslateMagFilter(pCreateInfo.MagFilter), MipFilter = TranslateMipFilter(pCreateInfo.MipmapMode), LodMinClamp = pCreateInfo.MinLod, LodMaxClamp = pCreateInfo.MaxLod, MaxAnisotropy = (nuint)pCreateInfo.MaxAnisotropy, CompareFunction = TranslateCompareFunction(pCreateInfo.CompareOp), NormalizedCoordinates = !pCreateInfo.UnnormalizedCoordinates, }; Sampler = mDevice.CreateSamplerState(descriptor); }
public MPSImageThresholdBinary(IMTLDevice device, float thresholdValue, float maximumValue, /*[NullAllowed]*/ float[] transform) : this(device, thresholdValue, maximumValue, MPSKernel.GetPtr(transform, false)) { }
public MPSImageDilate(IMTLDevice device, nuint kernelWidth, nuint kernelHeight, float[] values) : this(device, kernelWidth, kernelHeight, MPSKernel.GetPtr(values, true)) { }
public MPSImageGaussianPyramid(IMTLDevice device, nuint kernelWidth, nuint kernelHeight, float[] kernelWeights) : this(device, kernelWidth, kernelHeight, MPSKernel.GetPtr(kernelWeights, true)) { }
public MPSImageConversion(IMTLDevice device, MPSAlphaType srcAlpha, MPSAlphaType destAlpha, nfloat[] backgroundColor, CGColorConversionInfo conversionInfo) : this(device, srcAlpha, destAlpha, MPSKernel.GetPtr(backgroundColor, false), conversionInfo) { }
public bool Finalize (IMTLDevice device) { if (MetalTexture != null) return true; UIImage image = UIImage.FromFile (path); if (image == null) return false; using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ()) { if (colorSpace == null) return false; Width = image.CGImage.Width; Height = image.CGImage.Height; nuint width = (nuint)Width; nuint height = (nuint)Height; nuint rowBytes = width * 4; var context = new CGBitmapContext (IntPtr.Zero, (int)width, (int)height, 8, (int)rowBytes, colorSpace, CGImageAlphaInfo.PremultipliedLast); if (context == null) return false; var bounds = new CGRect (0f, 0f, width, height); context.ClearRect (bounds); // Vertical Reflect if (flip) { context.TranslateCTM (width, height); context.ScaleCTM (-1f, -1f); } context.DrawImage (bounds, image.CGImage); MTLTextureDescriptor texDesc = MTLTextureDescriptor.CreateTexture2DDescriptor (MTLPixelFormat.RGBA8Unorm, width, height, false); if (texDesc == null) return false; MetalTexture = device.CreateTexture (texDesc); if (MetalTexture == null) { context.Dispose (); return false; } IntPtr pixels = context.Data; if (pixels != IntPtr.Zero) { var region = new MTLRegion (); region.Origin.X = 0; region.Origin.Y = 0; region.Size.Width = (nint)width; region.Size.Height = (nint)height; MetalTexture.ReplaceRegion (region, 0, pixels, rowBytes); } context.Dispose (); } return true; }
public MPSImageSobel(IMTLDevice device, float[] transform) : this(device, MPSKernel.GetPtr (transform, true)) { }
void SetupMetal () { // Set the view to use the default device. #if IOS device = MTLDevice.SystemDefault; #elif MAC // TODO: https://bugzilla.xamarin.com/show_bug.cgi?id=32680 var devicePointer = MTLCreateSystemDefaultDevice (); device = Runtime.GetINativeObject<IMTLDevice> (devicePointer, false); #endif // Create a new command queue. commandQueue = device.CreateCommandQueue (); // Load all the shader files with a metal file extension in the project. defaultLibrary = device.CreateDefaultLibrary (); }
public unsafe static MPSNNGraph Create(IMTLDevice device, MPSNNImageNode[] resultImages, bool[] resultsAreNeeded) { fixed(void *resultsAreNeededHandle = resultsAreNeeded) return(Create(device, resultImages, (IntPtr)resultsAreNeededHandle)); }
public static bool Supports(IMTLDevice device) { return(MPSSupportsMTLDevice(device == null ? IntPtr.Zero : device.Handle)); }
public Renderer () { sampleCount = 4; depthPixelFormat = MTLPixelFormat.Depth32Float; stencilPixelFormat = MTLPixelFormat.Invalid; // find a usable Device device = MTLDevice.SystemDefault; // create a new command queue commandQueue = device.CreateCommandQueue (); NSError error; defaultLibrary = device.CreateLibrary ("default.metallib", out error); // if the shader libary isnt loading, nothing good will happen if (defaultLibrary == null) throw new Exception ("ERROR: Couldnt create a default shader library"); constantDataBufferIndex = 0; inflightSemaphore = new Semaphore (max_inflight_buffers, max_inflight_buffers); constantBuffer = new Uniforms[2]; constantBuffer [0].ambientColor = box1AmbientColor; constantBuffer [0].diffuseColor = box1DiffuseColor; constantBuffer [1].ambientColor = box2AmbientColor; constantBuffer [1].diffuseColor = box2DiffuseColor; multiplier = 1; }
public MPSImageThresholdTruncate(IMTLDevice device, float thresholdValue, /*[NullAllowed]*/ float[] transform) : this(device, thresholdValue, MPSKernel.GetPtr (transform, false)) { }
public MPSImageThresholdBinaryInverse(IMTLDevice device, float thresholdValue, float maximumValue, /*[NullAllowed]*/ float[] transform) : this(device, thresholdValue, maximumValue, MPSKernel.GetPtr (transform, false)) { }
public MPSImageErode(IMTLDevice device, nuint kernelWidth, nuint kernelHeight, float[] values) : base(device, kernelWidth, kernelHeight, values) { }
void SetupMetal() { // Find a usable device device = MTLDevice.SystemDefault; // Create a new command queue commandQueue = device.CreateCommandQueue (); // Load all the shader files with a metal file extension in the project NSError error; defaultLibrary = device.CreateLibrary ("default.metallib", out error); // Setup metal layer and add as sub layer to view metalLayer = new CAMetalLayer (); metalLayer.Device = device; metalLayer.PixelFormat = MTLPixelFormat.BGRA8Unorm; // Change this to NO if the compute encoder is used as the last pass on the drawable texture metalLayer.FramebufferOnly = true; // Add metal layer to the views layer hierarchy metalLayer.Frame = View.Layer.Frame; View.Layer.AddSublayer (metalLayer); View.Opaque = true; View.BackgroundColor = null; View.ContentScaleFactor = UIScreen.MainScreen.Scale; }
public MPSImageDilate(IMTLDevice device, nuint kernelWidth, nuint kernelHeight, float[] values) : this(device, kernelWidth, kernelHeight, MPSKernel.GetPtr (values, true)) { }
public static bool Supports(IMTLDevice device) { return MPSSupportsMTLDevice (device == null ? IntPtr.Zero : device.Handle); }
public override void ViewDidLoad() { base.ViewDidLoad(); // Set the view to use the default device device = MTLDevice.SystemDefault; if (device == null) { Console.WriteLine("Metal is not supported on this device"); View = new NSView(View.Frame); } // Create a new command queue commandQueue = device.CreateCommandQueue(); // Load all the shader files with a metal file extension in the project defaultLibrary = device.CreateDefaultLibrary(); // Setup view view = (MTKView)View; view.Delegate = this; view.Device = device; view.SampleCount = 1; view.DepthStencilPixelFormat = MTLPixelFormat.Depth32Float_Stencil8; view.ColorPixelFormat = MTLPixelFormat.BGRA8Unorm; view.PreferredFramesPerSecond = 60; view.ClearColor = new MTLClearColor(0.5f, 0.5f, 0.5f, 1.0f); // Load the vertex program into the library IMTLFunction vertexProgram = defaultLibrary.CreateFunction("quad_vertex"); // Load the fragment program into the library IMTLFunction fragmentProgram = defaultLibrary.CreateFunction("quad_fragment"); // Create a vertex descriptor from the MTKMesh MTLVertexDescriptor vertexDescriptor = new MTLVertexDescriptor(); vertexDescriptor.Attributes[0].Format = MTLVertexFormat.Float4; vertexDescriptor.Attributes[0].BufferIndex = 0; vertexDescriptor.Attributes[0].Offset = 0; vertexDescriptor.Attributes[1].Format = MTLVertexFormat.Float2; vertexDescriptor.Attributes[1].BufferIndex = 0; vertexDescriptor.Attributes[1].Offset = 4 * sizeof(float); vertexDescriptor.Layouts[0].Stride = 6 * sizeof(float); vertexDescriptor.Layouts[0].StepRate = 1; vertexDescriptor.Layouts[0].StepFunction = MTLVertexStepFunction.PerVertex; this.vertexBuffer = device.CreateBuffer(vertexData, MTLResourceOptions.CpuCacheModeDefault);// (MTLResourceOptions)0); this.mipmapping = 0; this.constantBuffer = device.CreateBuffer(sizeof(float), MTLResourceOptions.CpuCacheModeDefault); SetConstantBuffer(this.mipmapping, constantBuffer); // Create a reusable pipeline state var pipelineStateDescriptor = new MTLRenderPipelineDescriptor { SampleCount = view.SampleCount, VertexFunction = vertexProgram, FragmentFunction = fragmentProgram, VertexDescriptor = vertexDescriptor, DepthAttachmentPixelFormat = view.DepthStencilPixelFormat, StencilAttachmentPixelFormat = view.DepthStencilPixelFormat }; pipelineStateDescriptor.ColorAttachments[0].PixelFormat = view.ColorPixelFormat; NSError error; pipelineState = device.CreateRenderPipelineState(pipelineStateDescriptor, out error); if (pipelineState == null) { Console.WriteLine("Failed to created pipeline state, error {0}", error); } var depthStateDesc = new MTLDepthStencilDescriptor { DepthCompareFunction = MTLCompareFunction.Less, DepthWriteEnabled = true }; depthState = device.CreateDepthStencilState(depthStateDesc); // Texture KTX NSUrl url = NSBundle.MainBundle.GetUrlForResource("crate", "ktx"); MTKTextureLoader mTKTextureLoader = new MTKTextureLoader(device); this.texture = mTKTextureLoader.FromUrl(url, new MTKTextureLoaderOptions(), out error); Console.WriteLine("Failed to created pipeline state, error {0}", error); MTLSamplerDescriptor samplerDescriptor = new MTLSamplerDescriptor() { MinFilter = MTLSamplerMinMagFilter.Linear, MagFilter = MTLSamplerMinMagFilter.Linear, MipFilter = MTLSamplerMipFilter.Linear, SAddressMode = MTLSamplerAddressMode.ClampToEdge, TAddressMode = MTLSamplerAddressMode.ClampToEdge, }; this.sampler = device.CreateSamplerState(samplerDescriptor); this.clock = new System.Diagnostics.Stopwatch(); this.clock.Start(); this.time = 0; }