/// <summary cref="DirectXBuffer.OnMap(DeviceContext)"/>
        protected override unsafe IntPtr OnMap(DeviceContext context)
        {
            Debug.Assert(cudaArray == IntPtr.Zero);

            CudaException.ThrowIfFailed(
                CudaNativeMethods.cuGraphicsSubResourceGetMappedArray(
                    out cudaArray,
                    cudaGraphicsResource,
                    0,
                    0));

            Debug.Assert(cudaArray != IntPtr.Zero);

            if (buffer == null)
            {
                CudaException.ThrowIfFailed(
                    CudaNativeMethods.cuArrayGetDescriptor(out desc, cudaArray));

                pixelByteSize = CudaNativeMethods.GetByteSize(desc.arrayFormat) * desc.numChannels;
                buffer        = Accelerator.Allocate <byte>(
                    desc.width.ToInt32() * desc.height.ToInt32() * pixelByteSize);
            }

            Debug.Assert(pixelByteSize > 0);

            if (ViewFlags != DirectXViewFlags.WriteDiscard)
            {
                // Copy texture data to buffer
                var args = new CudaMemcpy2DArgs()
                {
                    dstDevice     = buffer.NativePtr,
                    dstMemoryType = CudaMemoryType.Device,

                    srcArray      = cudaArray,
                    srcMemoryType = CudaMemoryType.Array,

                    WidthInBytes = new IntPtr(desc.width.ToInt32() * pixelByteSize),
                    Height       = desc.height,
                };

                CudaException.ThrowIfFailed(
                    CudaNativeMethods.cuMemcpy2D(ref args));
            }

            return(buffer.NativePtr);
        }
        /// <summary cref="DirectXBuffer.OnUnmap(DeviceContext)"/>
        protected override unsafe void OnUnmap(DeviceContext context)
        {
            Debug.Assert(pixelByteSize > 0);
            if (ViewFlags != DirectXViewFlags.ReadOnly)
            {
                // Copy buffer data to texture
                var args = new CudaMemcpy2DArgs()
                {
                    srcDevice     = buffer.NativePtr,
                    srcMemoryType = CudaMemoryType.Device,

                    dstArray      = cudaArray,
                    dstMemoryType = CudaMemoryType.Array,

                    WidthInBytes = new IntPtr(desc.width.ToInt32() * pixelByteSize),
                    Height       = desc.height,
                };

                CudaException.ThrowIfFailed(
                    CudaNativeMethods.cuMemcpy2D(ref args));
            }
            cudaArray = IntPtr.Zero;
        }
 public static extern CudaError cuMemcpy2D(
     ref CudaMemcpy2DArgs args);