Error while converting MTLTexture to CVPixelBuffer

I am getting an error from the graphics driver while converting the EnvironmentTexture(from ARKIT.AREnvironmentProbeAnchor) to CVPixelBuffer. The EnvironmentTexture is an IMTLTexture. I am using Xamarin.iOS.


This is the code that I use to convert the IMTLTexture to CVPixelBuffer.
Code Block
buffers[i] = new CVPixelBuffer((nint)epAnchor.EnvironmentTexture.Width, (nint)epAnchor.EnvironmentTexture.Height, CVPixelFormatType.CV32RGBA);
GetEnvironmentTextureSlice(buffers[i], epAnchor.EnvironmentTexture, i);
public void GetEnvironmentTextureSlice(CVPixelBuffer pixelBuffer, Metal.IMTLTexture texture, int id)
{
Metal.MTLRegion mtlRegion = Metal.MTLRegion.Create2D((nuint)0, 0, 256, 256);
nuint bytesPerPixel = 4;
nuint bytesPerRow = bytesPerPixel * (nuint)mtlRegion.Size.Width; // (nuint)pixelBuffer.BytesPerRow;
nuint bytesPerImage = bytesPerRow * (nuint)mtlRegion.Size.Height;
pixelBuffer.Lock(CVPixelBufferLock.None);
texture.GetBytes(pixelBuffer.BaseAddress, (nuint)pixelBuffer.BytesPerRow, mtlRegion, 0);
pixelBuffer.Unlock(CVPixelBufferLock.None);
}


The error I am getting from the driver is AGX: Texture read/write assertion failed: bytes_per_row >= used_bytes_per_row

I tried with different values of pixelBuffer.BytesPerRow but still getting the error. Can someone help me?

For environment textures, I don't think it's possible to use that getBytes function. I use the one where you specify the slice: https://developer.apple.com/documentation/metal/mtltexture/1516318-getbytes

And I loop like this:

for i in 0..<6 {
        getBytes(ptr.baseAddress!.advanced(by: i * bytesPerImage),
                 bytesPerRow: bytesPerRow,
                 bytesPerImage: bytesPerImage,
                 from: region, mipmapLevel: 0, slice: i)
}

I hope that helps.

Error while converting MTLTexture to CVPixelBuffer
 
 
Q