title: iOS: How to manually set a 2D Float data to MTLTexture or MPSImage?
body: <p>I'm trying to fill <code>MPSImage</code> or 2D Metal texture with values manually and pass that to do convolutional network operation. An input for CNN (Metal Performance Shaders) is usually an image (like this <a href="https://developer.apple.com/library/content/samplecode/MPSCNNHelloWorld/Introduction/Intro.html#//apple_ref/doc/uid/TP40017482-Intro-DontLinkElementID_2" rel="nofollow noreferrer">https://developer.apple.com/library/content/samplecode/MPSCNNHelloWorld/Introduction/Intro.html#//apple_ref/doc/uid/TP40017482-Intro-DontLinkElementID_2</a>), that's why I could pass <code>UnsafePointer</code> of <code>CGContext</code>, but this time I'd like to use Float array as an input. </p>
  
  <p>The following is what I tried. I converted an input array to <code>NSData</code>, but it didn't work. </p>
  
  <pre><code>var inputData = NSData(bytes: inputFloatArrayOfArray, length: inputFloatArrayOfArray.count * inputFloatArrayOfArray[0].count * MemoryLayout&lt;Float&gt;.size)
  // The type of inputFloatArrayOfArray is [[Float]] 
  network.srcImage.texture.replace(region: MTLRegion( origin: MTLOrigin(x: 0, y: 0, z: 0),
                                                      size: MTLSize(width: inputWidth, height: inputHeight, depth: 1)),
                                   mipmapLevel: 0,
                                   slice: 0,
                                   withBytes: &amp;inputData,
                                   bytesPerRow: inputWidth * MemoryLayout&lt;Float&gt;.size, 
                                   bytesPerImage: 0)
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/26694821/manually-set-a-1d-texture-in-metal">Manually set a 1D Texture in Metal</a> may relate to my question (FYI: it says "deal with 2D textures that load the texture by converting a loaded <code>UIImage</code> to raw bytes data, but creating a dummy <code>UIImage</code> felt like a hack for me." ), but it seems there is no enough answer. Now I have no ideas how to tackle this. Please let me know anything if you have any ideas. </p>
  
  <p>Thank you very much in advance. </p>