public virtual CVOpenGLESTexture ChromaTextureForPixelBuffer(CVPixelBuffer pixelBuffer)
        {
            CVOpenGLESTexture chromaTexture = null;
            CVReturn          err;

            if (VideoTextureCache == null)
            {
                Console.Error.WriteLine("No video texture cache");
                return(chromaTexture);
            }
            // Periodic texture cache flush every frame
            VideoTextureCache.Flush(0);

            // CVOpenGLTextureCacheCreateTextureFromImage will create GL texture optimally from CVPixelBufferRef.
            // UV
            var height = pixelBuffer.GetHeightOfPlane(1);
            var width  = pixelBuffer.GetWidthOfPlane(1);

            chromaTexture = VideoTextureCache.TextureFromImage(pixelBuffer, true, All.RgExt, width, height, All.RgExt, DataType.UnsignedByte, 1, out err);

            if (chromaTexture == null || err != CVReturn.Success)
            {
                Console.Error.WriteLine("Error at creating chroma texture using CVOpenGLESTextureCacheCreateTextureFromImage: " + err.ToString());
            }

            return(chromaTexture);
        }
        unsafe void UpdateBackground(ARFrame frame)
        {
            using (var img = frame.CapturedImage)
            {
                using (var ciImage = CIImage.FromImageBuffer(img))
                {
                    using (var rotatedImage = ciImage.CreateByApplyingOrientation(ImageIO.CGImagePropertyOrientation.Right))
                    {
                        var size = new CGSize(rotatedImage.Extent.Size);

                        using (var rotatedBuff = new CVPixelBuffer((nint)size.Width,
                                                                   (nint)size.Height, img.PixelFormatType))
                        {
                            var _coreImageContext = new CIContext(null);

                            rotatedBuff.Lock(CVPixelBufferLock.None);

                            _coreImageContext.Render(rotatedImage, rotatedBuff);

                            var yPtr  = rotatedBuff.BaseAddress;
                            var uvPtr = rotatedBuff.GetBaseAddress(1);

                            if (yPtr == IntPtr.Zero || uvPtr == IntPtr.Zero)
                            {
                                return;
                            }

                            int wY  = (int)rotatedBuff.Width;
                            int hY  = (int)rotatedBuff.Height;
                            int wUv = (int)rotatedBuff.GetWidthOfPlane(1);
                            int hUv = (int)rotatedBuff.GetHeightOfPlane(1);

                            cameraYtexture.SetData(0, 0, 0, wY, hY, (void *)yPtr);
                            cameraUVtexture.SetData(0, 0, 0, wUv, hUv, (void *)uvPtr);

                            rotatedBuff.Unlock(CVPixelBufferLock.None);

                            _coreImageContext.Dispose();
                        }
                    }
                }
            }
        }
        private void CreateTextures(CVPixelBuffer img)
        {
            var imageWidth  = (int)img.Width;
            var imageHeight = (int)img.Height;
            var widthUV     = (int)img.GetWidthOfPlane(1);
            var heightUV    = (int)img.GetHeightOfPlane(1);

            this.sizeY  = (int)img.GetBytesPerRowOfPlane(0) * imageHeight;
            this.sizeUV = (int)img.GetBytesPerRowOfPlane(1) * heightUV;

            this.cameraTextureY = new Texture2D()
            {
                Format    = PixelFormat.R8,
                Usage     = TextureUsage.Dynamic,
                CpuAccess = TextureCpuAccess.Write,
                Width     = imageWidth,
                Height    = imageHeight,
                Levels    = 1
            };

            this.cameraTextureUV = new Texture2D()
            {
                Format    = PixelFormat.R8G8,
                Usage     = TextureUsage.Dynamic,
                CpuAccess = TextureCpuAccess.Write,
                Width     = widthUV,
                Height    = heightUV,
                Levels    = 1,
            };

            this.graphicsDevice.Textures.UploadTexture(this.cameraTextureY);
            this.graphicsDevice.Textures.UploadTexture(this.cameraTextureUV);

            this.yuvMaterial.LuminanceTexture = this.cameraTextureY;
            this.yuvMaterial.ChromaTexture    = this.cameraTextureUV;
        }
public virtual CVOpenGLESTexture ChromaTextureForPixelBuffer (CVPixelBuffer pixelBuffer)
{
	CVOpenGLESTexture chromaTexture = null;
	CVReturn err;
	if (VideoTextureCache == null) {
		Console.Error.WriteLine ("No video texture cache");
		return chromaTexture;
	}
	// Periodic texture cache flush every frame
	VideoTextureCache.Flush (0);

	// CVOpenGLTextureCacheCreateTextureFromImage will create GL texture optimally from CVPixelBufferRef.
	// UV
	var height = pixelBuffer.GetHeightOfPlane (1);
	var width = pixelBuffer.GetWidthOfPlane (1);
	chromaTexture = VideoTextureCache.TextureFromImage (pixelBuffer, true, All.RgExt, width, height, All.RgExt, DataType.UnsignedByte, 1, out err);

	if (chromaTexture == null || err != CVReturn.Success)
		Console.Error.WriteLine ("Error at creating chroma texture using CVOpenGLESTextureCacheCreateTextureFromImage: " + err.ToString ());

	return chromaTexture;
}