[英]C# texture2d how to convert to cpu image
How would you convert a Directx11 Texture2d into an image on the cpu that I can process?您如何将 Directx11 Texture2d 转换为我可以处理的 cpu 上的图像?
I have tried searching the issue and google comes up with unity answers that use exclusive API's or the answers reflect to System.Drawing.Texture2我曾尝试搜索问题,谷歌提出了使用独有 API 的统一答案或反映到 System.Drawing.Texture2 的答案
You need to create a staging texture, that you can then access through cpu.您需要创建一个暂存纹理,然后您可以通过 cpu 访问它。
To do so, I assume that you already have an existing SharpDX texture:为此,我假设您已经拥有一个现有的 SharpDX 纹理:
public static StagingTexture2d FromTexture(Texture2D texture)
{
if (texture == null)
throw new ArgumentNullException("texture");
//Get description, and swap a few flags around (make it readable, non bindable and staging usage)
Texture2DDescription description = texture.Description;
description.BindFlags = BindFlags.None;
description.CpuAccessFlags = CpuAccessFlags.Read;
description.Usage = ResourceUsage.Staging;
return new StagingTexture2d(texture.Device, description);
}
This new texture will allow read operations.这个新纹理将允许读取操作。
Next you need to copy your GPU texture into the staging Texture using the device context:接下来,您需要使用设备上下文将 GPU 纹理复制到暂存纹理中:
deviceContext.CopyResource(gpuTexture, stagingTexture);
Once this is done, you can map the staging texture to access it's content on the CPU:完成后,您可以 map 暂存纹理来访问它在 CPU 上的内容:
DataStream dataStream;
DataBox dataBox = deviceContext.MapSubresource(stagingTexture,0, MapMode.Read, MapFlags.None, out dataStream);
//Use either datastream to read data, or dataBox.DataPointer
//generally it's good to make a copy of that data immediately and unmap asap
//Very important, unmap once you done
deviceContext.UnmapSubresource(stagingTexture, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.