当前位置: 首页 > news >正文

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];//创建临时RTdstRenderTexture = 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数据
http://www.lryc.cn/news/426899.html

相关文章:

  • 【MySQL】C/C++连接MySQL客户端,MySQL函数接口认知,图形化界面进行连接
  • Wireshark分析工具
  • linux网络配置脚本
  • IT管理:我与IT的故事4
  • 短链接系统设计方案
  • Cisco交换机SSH使用RSA公钥免密登录(IOS与Nexus,服务器以RHEL8为例)
  • QT判断操作系统类型和CPU架构
  • input[type=checkbox]勾选框自定义样式
  • 鼠害监测系统:科技守护农业安全
  • Ubuntu20.04如何安装配置JDK
  • Python3网络爬虫开发实战(9)代理的使用 (需补充代理池的构建)
  • 人际关系中的价值交换原理,在人类社会的复杂网络中,人际关系犹如一根根交织的丝线,将我们彼此紧密相连
  • 西安电子科技大学萌新智慧指南(校区篇)
  • JavaScript基础(33)_鼠标滚轮滚动事件、键盘事件
  • 怎样做网站推广
  • Unity引擎加密方案解析
  • 遇到的几个iOS问题
  • 掌握ChatGPT写作艺术:从入门到精通的四个层次
  • 虚幻UE5安装报错误代码:SU-PQR5
  • 谷歌开源Gemma-2 百亿参数大模型,性能超越Llama-3模型,免费使用
  • 人工智能与机器学习原理精解【12】
  • openEuler系统安装Visual Studio Code
  • Qt 系统相关 - 事件
  • Ubuntu最小化命令行系统 安装GUI 远程桌面
  • Web前端:CSS篇(二)背景,文本,链接
  • ubuntu 24.04 软件源配置,替换为国内源
  • 【Java 并发编程】(三) 从CPU缓存开始聊 volatile 底层原理
  • YOLOV8网络结构|搞懂Backbone-Conv
  • Elasticsearch Nested类型详解与实战
  • 网络编程,网络协议,UDP协议