public void WaitForCompletion()
    {
        if (Status != AsyncTextureReaderStatus.Reading)
        {
            return;
        }

        Update();

        if (Type == ReadType.Native)
        {
            if (!NativeReadRequest.done)
            {
                NativeReadRequest.WaitForCompletion();
            }
            Update();
        }
        else if (Type == ReadType.LinuxOpenGL)
        {
            if (LinuxId >= 0)
            {
                while (AsyncTextureReaderImports.AsyncTextureReaderGetStatus(LinuxId) != AsyncTextureReaderStatus.Finished)
                {
                    AsyncTextureReaderImports.AsyncTextureReaderWaitForCompletion(LinuxId);
                    GL.IssuePluginEvent(LinuxUpdate, LinuxId);
                }
                Status = AsyncTextureReaderStatus.Reading;
            }
            Update();
        }
    }
    public void Update()
    {
        if (Texture.IsCreated() == false)
        {
            // need to recreate native RenderTexture handle, because it is lost
            // this happens, for example, when you resize Unity Editor window on Linux
            if (Type == ReadType.Native)
            {
                NativeReadRequest.WaitForCompletion();
            }
            else if (Type == ReadType.LinuxOpenGL)
            {
                AsyncTextureReaderImports.AsyncTextureReaderDestroy(LinuxId);
                GL.IssuePluginEvent(LinuxUpdate, LinuxId);

                Texture.Create();
                LinuxId = AsyncTextureReaderImports.AsyncTextureReaderCreate(Texture.GetNativeTexturePtr(), Data.Length);
                GL.IssuePluginEvent(LinuxUpdate, LinuxId);
            }

            Status = AsyncTextureReaderStatus.Idle;
            return;
        }

        if (Status != AsyncTextureReaderStatus.Reading)
        {
            return;
        }

        if (Type == ReadType.Native)
        {
            if (NativeReadRequest.done)
            {
                if (NativeReadRequest.layerCount == 0)
                {
                    // start reading request was not issued yet
                    return;
                }
                // this will happen only if AsyncGPUReadback.Request was issued
                if (NativeReadRequest.hasError)
                {
                    return;
                }

                Data.CopyFrom(NativeReadRequest.GetData <T>());
                Status = AsyncTextureReaderStatus.Finished;
            }
        }
        else if (Type == ReadType.LinuxOpenGL)
        {
            if (LinuxId >= 0)
            {
                Status = AsyncTextureReaderImports.AsyncTextureReaderGetStatus(LinuxId);
                if (Status != AsyncTextureReaderStatus.Finished)
                {
                    GL.IssuePluginEvent(LinuxUpdate, LinuxId);
                    Status = AsyncTextureReaderImports.AsyncTextureReaderGetStatus(LinuxId);
                }
            }
            else
            {
                Status = AsyncTextureReaderStatus.Finished;
            }
        }
        else if (Type == ReadType.Sync)
        {
            var current = RenderTexture.active;
            RenderTexture.active = Texture;
            ReadTexture.ReadPixels(new Rect(0, 0, Texture.width, Texture.height), 0, 0);
            ReadTexture.Apply();
            RenderTexture.active = current;

            int    size  = ReadTexture.width * ReadTexture.height * BytesPerPixel;
            byte[] bytes = ReadTexture.GetRawTextureData();
            unsafe
            {
                fixed(void *ptr = bytes)
                {
                    Buffer.MemoryCopy(ptr, Data.GetUnsafePtr(), size, size);
                }
            }
            Status = AsyncTextureReaderStatus.Finished;
        }
    }
Example #3
0
    public void Update()
    {
        if (Texture.IsCreated() == false)
        {
            // need to recreate native RenderTexture handle, because it is lost
            // this happens, for example, when you resize Unity Editor window on Linux
            if (Type == ReadType.Native)
            {
                NativeReadRequest.WaitForCompletion();
            }
            else if (Type == ReadType.LinuxOpenGL)
            {
                AsyncTextureReaderImports.AsyncTextureReaderDestroy(LinuxId);
                GL.IssuePluginEvent(LinuxUpdate, LinuxId);

                Texture.Create();
                LinuxId = AsyncTextureReaderImports.AsyncTextureReaderCreate(Texture.GetNativeTexturePtr(), SizeInBytes);
                GL.IssuePluginEvent(LinuxUpdate, LinuxId);
            }

            Status = AsyncTextureReaderStatus.Idle;
            return;
        }

        if (Status != AsyncTextureReaderStatus.Reading)
        {
            return;
        }

        if (Type == ReadType.Native)
        {
            if (NativeReadRequest.done)
            {
                if (NativeReadRequest.layerCount == 0)
                {
                    // start reading request was not issued yet
                    return;
                }
                // this will happen only if AsyncGPUReadback.Request was issued
                if (NativeReadRequest.hasError)
                {
                    return;
                }

                NativeReadRequest.GetData <T>().CopyTo(Data);
                Status = AsyncTextureReaderStatus.Finished;
            }
        }
        else if (Type == ReadType.LinuxOpenGL)
        {
            if (LinuxId >= 0)
            {
                Status = AsyncTextureReaderImports.AsyncTextureReaderGetStatus(LinuxId);
                if (Status != AsyncTextureReaderStatus.Finished)
                {
                    GL.IssuePluginEvent(LinuxUpdate, LinuxId);
                    Status = AsyncTextureReaderImports.AsyncTextureReaderGetStatus(LinuxId);
                }
                if (Status == AsyncTextureReaderStatus.Finished)
                {
                    IntPtr src    = AsyncTextureReaderImports.AsyncTextureReaderGetBuffer(LinuxId);
                    var    handle = GCHandle.Alloc(Data, GCHandleType.Pinned);
                    try
                    {
                        IntPtr dst = handle.AddrOfPinnedObject();
                        unsafe
                        {
                            Buffer.MemoryCopy((void *)src, (void *)dst, SizeInBytes, SizeInBytes);
                        }
                    }
                    finally
                    {
                        handle.Free();
                    }
                }
            }
            else
            {
                Status = AsyncTextureReaderStatus.Finished;
            }
        }
        else if (Type == ReadType.Sync)
        {
            var current = RenderTexture.active;
            RenderTexture.active = Texture;
            ReadTexture.ReadPixels(new Rect(0, 0, Texture.width, Texture.height), 0, 0);
            ReadTexture.Apply();
            RenderTexture.active = current;

            ReadTexture.GetRawTextureData <T>().CopyTo(Data);
            Status = AsyncTextureReaderStatus.Finished;
        }
    }
Example #4
0
    public void Update()
    {
        if (Status != AsyncTextureReaderStatus.Reading)
        {
            return;
        }

        if (Type == ReadType.Native)
        {
            if (NativeReadRequest.done)
            {
                if (NativeReadRequest.layerCount == 0)
                {
                    // start reading request was not issued yet
                    return;
                }
                // this will happen only if AsyncGPUReadback.Request was issued
                if (NativeReadRequest.hasError)
                {
                    return;
                }

                Data   = NativeReadRequest.GetData <T>();
                Status = AsyncTextureReaderStatus.Finished;
            }
        }
        else if (Type == ReadType.LinuxOpenGL)
        {
            if (LinuxId >= 0)
            {
                Status = AsyncTextureReaderImports.AsyncTextureReaderGetStatus(LinuxId);
                if (Status != AsyncTextureReaderStatus.Finished)
                {
                    GL.IssuePluginEvent(LinuxUpdate, LinuxId);
                }
            }
            else
            {
                Status = AsyncTextureReaderStatus.Finished;
            }
        }
        else if (Type == ReadType.Sync)
        {
            var current = RenderTexture.active;
            RenderTexture.active = Texture;
            ReadTexture.ReadPixels(new Rect(0, 0, Texture.width, Texture.height), 0, 0);
            ReadTexture.Apply();
            RenderTexture.active = current;

            int    size  = ReadTexture.width * ReadTexture.height * BytesPerPixel;
            byte[] bytes = ReadTexture.GetRawTextureData();
            unsafe
            {
                fixed(void *ptr = bytes)
                {
                    Buffer.MemoryCopy(ptr, Data.GetUnsafePtr(), size, size);
                }
            }
            Status = AsyncTextureReaderStatus.Finished;
        }
    }