public static IMTLBuffer CreateBufferNoCopy <T> (this IMTLDevice This, T [] data, MTLResourceOptions options, MTLDeallocator deallocator) where T : struct
        {
            var    handle = GCHandle.Alloc(data, GCHandleType.Pinned);           // This requires a pinned GCHandle, since it's not possible to use unsafe code to get the address of a generic object.
            IntPtr ptr    = handle.AddrOfPinnedObject();

            return(This.CreateBufferNoCopy(ptr, (nuint)(data.Length * Marshal.SizeOf(typeof(T))), options, (pointer, length) => {
                handle.Free();
                deallocator(pointer, length);
            }));
        }
Example #2
0
        public static IMTLBuffer CreateBufferNoCopy <T> (this IMTLDevice This, T [] data, MTLResourceOptions options, MTLDeallocator deallocator) where T : struct
        {
            var handle = GCHandle.Alloc(data, GCHandleType.Pinned);

            try {
                IntPtr ptr = handle.AddrOfPinnedObject();
                return(This.CreateBufferNoCopy(ptr, (nuint)(data.Length * Marshal.SizeOf(typeof(T))), options, deallocator));
            } finally {
                handle.Free();
            }
        }