access particular set of pixels MTLTexture in melal


access particular set of pixels MTLTexture in melal



I created a MTLtexture using UIImage data as follows.


var texture = metalView.currentDrawable!.texture
let uiImg = createImageFromCurrentDrawable()

guard let device = metalView.device else {
fatalError("Device not created. Run on a physical device")
}
let textureLoader = MTKTextureLoader(device:device)
let imageData: NSData = UIImagePNGRepresentation(uiImg)! as NSData
texture = try! textureLoader.newTexture(data: imageData as Data, options: [MTKTextureLoader.Option.allocateMipmaps : (false as NSNumber)])



what I need to do is change pixels color in MTLTexture.Not all of them.So Is it possible to access particular set of pixels in MTLtexture and write into it in metal?




1 Answer
1



Yes, as a look at the MTLTexture documentation would have shown you. You can use one of the getBytes() methods to copy a region of texture data out to a buffer, and one of the replace() methods to replace a region of the texture's pixel with data from a buffer you supply.


MTLTexture


getBytes()


replace()





got it.It worked.thanks for the tip Ken.Btw when I replace portion of pixels in the texture, color values are changed from actual values of that particular set of pixels.Do you know any possible reason for this kind of behavior
– KP1239
Jul 4 at 12:20





Not sure. Depends on what you're doing exactly and the pixel format of the data you're supplying. Probably best to open a new question with those details, what you expected, and what you actually got.
– Ken Thomases
Jul 4 at 14:29





Thanks ken.I figured it out lately.Yes indeed it's because of pixel format. Instead of pixelFormat: .bgra8Unorm in MTLTextureDescriptor i have accidently added .argb8Unorm which causes to unexpected behaviour.
– KP1239
Jul 5 at 4:34






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy