Unity异步把图片数据从显存下载到内存(GPU->CPU)

Unity异步把图片数据从显存下载到内存(GPU->CPU)

1.c#核心代码

using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEditor.PackageManager.Requests;
using UnityEngine;
using UnityEngine.Rendering;

namespace Tools
{
    /// <summary>
    /// RenderTexture转Bytes
    /// </summary>
    public class TextureOrBytes
    {
        private ComputeShader computeShader;
        private Texture srcRrenderTexture;
        private RenderTexture dstRenderTexture;
        private int kernelHandle;
        private bool requestPending;
        private NativeArray<byte> srcNativeArray;
        private byte[] dstRTData;
        private int width;
        private int height;
        private int memorySize;
        private int threadGroupsX;
        private int threadGroupsY;
        private object lockObject = new object();

        public TextureOrBytes(ComputeShader _computeShader, Texture _srcRrenderTexture)
        {
            computeShader = _computeShader;
            srcRrenderTexture = _srcRrenderTexture; 
            Init();
        }

        public void Update()
        {
            if (!requestPending)
            {
                computeShader.Dispatch(kernelHandle, threadGroupsX, threadGroupsY, 1);

                AsyncGPUReadback.Request(dstRenderTexture, 0, TextureFormat.RGBA32, OnCompleteReadback);
                requestPending = true;
            }
        }

        public void Clear()
        {
            if (dstRenderTexture != null)
            {
                dstRenderTexture.Release();
                dstRenderTexture = null;
            }
            if (srcNativeArray != null)
            {
                srcNativeArray.Dispose();
            }
            
            dstRTData = null;
            lockObject = null;
        }

        private void Init()
        {
            width = srcRrenderTexture.width;
            height = srcRrenderTexture.height;
            memorySize = width * height * 4;
            srcNativeArray = new NativeArray<byte>(memorySize, Allocator.Temp);
            dstRTData = new byte[memorySize];

            //创建临时RT
            dstRenderTexture = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32);
            dstRenderTexture.enableRandomWrite = true;
            dstRenderTexture.Create();

            kernelHandle = computeShader.FindKernel("CSMain");
            computeShader.SetTexture(kernelHandle, "sourceTex", srcRrenderTexture);
            computeShader.SetTexture(kernelHandle, "destTex", dstRenderTexture);
            threadGroupsX = Mathf.CeilToInt(width / 8.0f);
            threadGroupsY = Mathf.CeilToInt(height / 8.0f);
        }

        private void OnCompleteReadback(AsyncGPUReadbackRequest request)
        {
            if (srcNativeArray == null || dstRTData == null || lockObject == null)
            {
                requestPending = false;
                return;
            }

            if (!request.done)
            {
                Debug.Log("GPU readback hasnt done yet");
                return;
            }

            if (request.hasError)
            {
                Debug.LogError("GPU readback error detected.");
                requestPending = false;
                return;
            }

            srcNativeArray = request.GetData<byte>(); 

            lock (lockObject)
            {
                if (srcNativeArray.Length == dstRTData.Length)
                {
                    srcNativeArray.CopyTo(dstRTData);
                    
                }
            }
            requestPending = false;
        }

        public byte[] GetTexGPUBytesData()
        {
            lock (lockObject) 
            {
                return dstRTData;
            }
        }

        public Texture GetTex()
        {
            return srcRrenderTexture;
        }

        public RenderTexture GetRWTex()
        {
            return dstRenderTexture;
        }


        #region 递归方式..

        private void StartRequestReadbacks()
        {
            computeShader.Dispatch(kernelHandle, threadGroupsX, threadGroupsY, 1);

            AsyncGPUReadback.Request(dstRenderTexture, 0, TextureFormat.RGBA32, OnCompleteReadback_Recursive);
        }

        private void OnCompleteReadback_Recursive(AsyncGPUReadbackRequest request)
        {
            if (srcNativeArray == null || dstRTData == null || lockObject == null)
            {
                requestPending = false;
                return;
            }

            if (!request.done)
            {
                Debug.Log("GPU readback hasnt done yet");
                return;
            }

            if (request.hasError)
            {
                Debug.LogError("GPU readback error detected.");
                requestPending = false;
                return;
            }

            srcNativeArray = request.GetData<byte>();

            //lock (lockObject)
            {
                if (srcNativeArray.Length == dstRTData.Length)
                {
                    srcNativeArray.CopyTo(dstRTData);
                }
            }

            StartRequestReadbacks();
        }

        #endregion


    }

}

2.ComputeShader核心代码

// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain

// Texture to read from
Texture2D<float4> sourceTex;
// Texture to write to
RWTexture2D<float4> destTex;

[numthreads(8, 8, 1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
    float4 color = sourceTex[id.xy];
    destTex[id.xy] = color;
}

3.使用

  • 在外部实例化TextureOrBytes
  • 在update中调用 TextureOrBytes.Update();
  • 通过TextureOrBytes.GetTexGPUBytesData()获取CPU数据
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值