memcpy NativeArray Index to NativeArray Index, of Length,怎么办? [英] memcpy NativeArray Index to NativeArray Index, of Length, how to do?

查看:34
本文介绍了memcpy NativeArray Index to NativeArray Index, of Length,怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unity 中的 NativeArrays 没有从一个到另一个的部分复制功能.

NativeArrays in Unity don't have a partial copy feature from one to another.

有多种方法可以解决这个问题(NativeSlices 和 GetSubArray),但最理想的是零检查、所有信任、memcpy.

There's various ways around this (NativeSlices and GetSubArray) but the ideal would be a zero checks, all trust, memcpy.

然而,我能想到的最接近的是这个,它什么也不做;

However, the closest I can conceive of is this, and it does nothing;

using Unity.Collections ;
using Unity.Collections.LowLevel.Unsafe ;

namespace Unity.Collections {
  public static class NativeArrayExtensions {
    public static unsafe void CopySuperFast <T> (
      this NativeArray<T> src, int srcIndex,
      NativeArray<T> dst, int dstIndex,
      int length ) where T : struct {
      
      GCHandle dstHandle = GCHandle.Alloc ( dst, GCHandleType.Pinned ) ;
      GCHandle srcHandle = GCHandle.Alloc ( src, GCHandleType.Pinned ) ;
      UnsafeUtility.MemCpy ( 
                            ( void* ) ( dstHandle.AddrOfPinnedObject (  )
                                      + dstIndex * UnsafeUtility.SizeOf<T> ( ) ), 
                             ( void* ) (  srcHandle.AddrOfPinnedObject ( ) 
                                       + srcIndex * UnsafeUtility.SizeOf<T> ( ) ), 
                            length ) ;
      dstHandle.Free (  );
      srcHandle.Free (  ) ;
    }
  }
}

推荐答案

我不认为这能回答您的问题,但我无法将其正确格式化为评论.

I do not believe this answers your question but I can not format this properly as a comment.

namespace Unity.Collections
{
    public static class NativeArrayExtensions
    {
        public static unsafe void CopySuperFast<T>(
          this NativeArray<T> src, int srcIndex,
               NativeArray<T> dst, int dstIndex,
          int length
          ) where T : unmanaged
        {
            T* srcPtr = (T*)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(src);
            {
                var d = dst.GetUnsafePtr();
                UnsafeUtility.MemCpy(d, srcPtr + dstIndex, sizeof(T) * length);
            }
        }
    }
}

以上代码段未经测试,我不确定它是否有效.更多的将它留在这里作为您可能弄清楚的起点.

The above snippet is untested and I am not sure if it will work. More leaving it here as a jumping-off point for you to potentially figure it out.

这篇关于memcpy NativeArray Index to NativeArray Index, of Length,怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆