NativeArray <byte> GetImageBytesToConvert(
            NativeSlice <byte> imageBytes, Vector2Int sizeInPixels, ref TextureFormat format, ref JobHandle inputDeps)
        {
            // RGBA32 is not supported by CVPixelBuffer, but ARGB32 is, so
            // we offer a conversion for this common case.
            if (format == TextureFormat.RGBA32)
            {
                int numPixels        = sizeInPixels.x * sizeInPixels.y;
                var argb32ImageBytes = new NativeArray <byte>(
                    numPixels * 4,
                    Allocator.Persistent,
                    NativeArrayOptions.UninitializedMemory);

                inputDeps = new ConvertRGBA32ToARGB32Job
                {
                    rgbaImage = imageBytes.SliceConvert <uint>(),
                    argbImage = argb32ImageBytes.Slice().SliceConvert <uint>()
                }.Schedule(numPixels, 64, inputDeps);

                // Format is now ARGB32
                format = TextureFormat.ARGB32;

                return(argb32ImageBytes);
            }

            // No conversion necessary; echo back inputs
            return(default);
Exemplo n.º 2
0
        protected override unsafe JobHandle ScheduleAddImageJobImpl(
            NativeSlice <byte> imageBytes,
            Vector2Int sizeInPixels,
            TextureFormat format,
            XRReferenceImage referenceImage,
            JobHandle inputDeps)
        {
            if (!referenceImage.specifySize)
            {
                throw new InvalidOperationException("ARKit requires physical dimensions for all reference images.");
            }

            // Add a reference to keep the native object alive
            // even if we get finalized while a job is running
            UnityARKit_CFRetain(nativePtr);

            // RGBA32 is not supported by CVPixelBuffer, but ARGB32 is, so
            // we offer a conversion for this common case.
            var convertedImage = new NativeArray <byte>();

            if (format == TextureFormat.RGBA32)
            {
                int numPixels = sizeInPixels.x * sizeInPixels.y;
                convertedImage = new NativeArray <byte>(
                    numPixels * 4,
                    Allocator.Persistent,
                    NativeArrayOptions.UninitializedMemory);

                inputDeps = new ConvertRGBA32ToARGB32Job
                {
                    rgbaImage = imageBytes.SliceConvert <uint>(),
                    argbImage = convertedImage.Slice().SliceConvert <uint>()
                }.Schedule(numPixels, 64, inputDeps);

                // Format is now ARGB32
                format = TextureFormat.ARGB32;
            }

            // Schedule the actual addition of the image to the database
            inputDeps = new AddImageJob
            {
                image                 = convertedImage.IsCreated ? new NativeSlice <byte>(convertedImage) : imageBytes,
                database              = nativePtr,
                width                 = sizeInPixels.x,
                height                = sizeInPixels.y,
                physicalWidth         = referenceImage.size.x,
                format                = format,
                managedReferenceImage = new ManagedReferenceImage(referenceImage)
            }.Schedule(inputDeps);

            // If we had to perform a conversion, then release that memory
            if (convertedImage.IsCreated)
            {
                inputDeps = new DeallocateNativeArrayJob <byte> {
                    array = convertedImage
                }.Schedule(inputDeps);
            }

            return(inputDeps);
        }
Exemplo n.º 3
0
        public unsafe void Update(NativeSlice <NodeState> state, NativeSlice <byte> data)
        {
            var arr = data.SliceConvert <T>();

            for (int i = 0; i < arr.Length; ++i)
            {
                var v = arr[i];
                Update(ref v);
                arr[i] = v;
            }
        }
Exemplo n.º 4
0
    private static void InitializeWeights(NativeSlice <sbyte> weights, ref Rng rng)
    {
        // Since we want to assign random bits, the integer type or wordsize doesn't matter
        // So we can reinterpret as generic uints, randomize, and save some compute that way
        var weightInts = weights.SliceConvert <uint>();

        for (int i = 0; i < weightInts.Length; i++)
        {
            weightInts[i] = rng.NextUInt();
        }
    }
Exemplo n.º 5
0
        private static void ReadVoxelFile(int sizeVox, byte[] rawBytes, ref Voxel[] voxelArray)
        {
            if (voxelArray == null)
            {
                voxelArray = new Voxel[sizeVox * sizeVox * sizeVox];
            }

            var voxelBytes = new NativeArray <byte>(rawBytes, Allocator.Temp);
            var bytes      = new NativeSlice <byte>(voxelBytes);
            var voxelSlice = bytes.SliceConvert <Voxel>();

            DirectNativeCollectionsAccess.CopyTo(voxelSlice, voxelArray);
            voxelBytes.Dispose();
        }
Exemplo n.º 6
0
        public unsafe void Update(float dt, int nodeIndex, TreeDef treeDef, NativeSlice <NodeState> state, NativeSlice <byte> data)
        {
            Profiler.BeginSample("TNode::Update");
            var arr = data.SliceConvert <T>();

            for (int i = 0; i < arr.Length; ++i)
            {
                var h = new NodeStateHandle(nodeIndex, state.Slice(i * treeDef.Nodes.Length, treeDef.Nodes.Length), treeDef);
                var v = arr[i];

                if (h.State == NodeState.Activating)
                {
                    h.State = Activate(h, ref v);
                }

                if (h.State == NodeState.Running)
                {
                    h.State = Update(dt, h, ref v);
                    arr[i]  = v;
                }
            }
            Profiler.EndSample();
        }
Exemplo n.º 7
0
        public static JobHandle Schedule(
            NativeSlice <byte> inputImage,
            Vector2Int sizeInPixels,
            TextureFormat format,
            NativeArray <byte> grayscaleImage,
            JobHandle inputDeps)
        {
            int width  = sizeInPixels.x;
            int height = sizeInPixels.y;

            switch (format)
            {
            case TextureFormat.R8:
            case TextureFormat.Alpha8:
                return(new FlipVerticalJob
                {
                    width = width,
                    height = height,
                    grayscaleIn = inputImage,
                    grayscaleOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));

            case TextureFormat.RGB24:
                return(new ConvertStridedToGrayscaleJob
                {
                    stride = 3,
                    width = width,
                    height = height,
                    colorImageIn = inputImage,
                    grayscaleImageOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));

            case TextureFormat.RGBA32:
                return(new ConvertStridedToGrayscaleJob
                {
                    stride = 4,
                    width = width,
                    height = height,
                    colorImageIn = inputImage,
                    grayscaleImageOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));

            case TextureFormat.ARGB32:
                return(new ConvertARGB32ToGrayscaleJob
                {
                    width = width,
                    height = height,
                    colorImageIn = inputImage,
                    grayscaleImageOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));

            case TextureFormat.BGRA32:
                return(new ConvertBGRA32ToGrayscaleJob
                {
                    width = width,
                    height = height,
                    colorImageIn = inputImage,
                    grayscaleImageOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));

            case TextureFormat.RFloat:
                return(new ConvertRFloatToGrayscaleJob
                {
                    width = width,
                    height = height,
                    rfloatIn = inputImage.SliceConvert <float>(),
                    grayscaleImageOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));

            default:
                throw new InvalidOperationException($"Texture format {format} is not supported.");
            }
        }
Exemplo n.º 8
0
        public static JobHandle Schedule(
            NativeSlice <byte> inputImage,
            Vector2Int sizeInPixels,
            TextureFormat format,
            NativeArray <byte> grayscaleImage,
            JobHandle inputDeps)
        {
            int width  = sizeInPixels.x;
            int height = sizeInPixels.y;

            if ((format == TextureFormat.R8) ||
                (format == TextureFormat.Alpha8))
            {
                return(new FlipVerticalJob
                {
                    width = width,
                    height = height,
                    grayscaleIn = inputImage,
                    grayscaleOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));
            }

            // We'll have to convert it. Create an output buffer.
            if (format == TextureFormat.RGB24)
            {
                return(new ConvertStridedToGrayscaleJob
                {
                    stride = 3,
                    width = width,
                    height = height,
                    colorImageIn = inputImage,
                    grayscaleImageOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));
            }
            else if (format == TextureFormat.RGBA32)
            {
                return(new ConvertStridedToGrayscaleJob
                {
                    stride = 4,
                    width = width,
                    height = height,
                    colorImageIn = inputImage,
                    grayscaleImageOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));
            }
            else if (format == TextureFormat.ARGB32)
            {
                return(new ConvertARGB32ToGrayscaleJob
                {
                    width = width,
                    height = height,
                    colorImageIn = inputImage,
                    grayscaleImageOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));
            }
            else if (format == TextureFormat.BGRA32)
            {
                return(new ConvertBGRA32ToGrayscaleJob
                {
                    width = width,
                    height = height,
                    colorImageIn = inputImage,
                    grayscaleImageOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));
            }
            else if (format == TextureFormat.RFloat)
            {
                return(new ConvertRFloatToGrayscaleJob
                {
                    width = width,
                    height = height,
                    rfloatIn = inputImage.SliceConvert <float>(),
                    grayscaleImageOut = grayscaleImage
                }.Schedule(height, 1, inputDeps));
            }
            else
            {
                throw new InvalidOperationException($"Texture format {format} is not supported.");
            }
        }