c# - 来自字节数组 Sharpdx 的 Texture2d
问题描述
我是 C#、SharpDX 和 Directx 新手。请原谅我的无知。我正在跟进一篇旧帖子:SharpDX 代码中的 Texture2D.FromMemory() 异常。这很有帮助。
我的目标:
- 从软件位图构建一个 Texture2d。
- 使纹理可用于 HLSL。
我接近它的方式:
- 使用 IMemoryBufferByteAccess,我能够检索到字节的指针和 Frame 的总容量。从上一篇文章来看,我似乎需要使用 DataRectangle 来指向字节数组。
- 有 2 个具有不同描述符的纹理 - Texture1 (_staging_texture) - 无绑定标志、cpu 写入和读取权限、用法 - 暂存。我用指向字节数组的数据矩形创建了这个纹理。Texture2 (_final_texture) - 着色器绑定标志,无 cpu 访问权限,用法 - 默认。这个纹理最终将提供给着色器。目的是使用从 Texture1 到 Texture2 的 copyResource 函数。
下面,我复制我未抛光的代码以供参考:
bitmap = latestFrame.SoftwareBitmap;
Windows.Graphics.Imaging.BitmapBuffer bitmapBuffer= bitmap.LockBuffer(Windows.Graphics.Imaging.BitmapBufferAccessMode.Read);
Windows.Foundation.IMemoryBufferReference bufferReference = bitmapBuffer.CreateReference();
var staging_descriptor = new Texture2DDescription
{
Width = Width,
Height = Height,
MipLevels = 1,
ArraySize = 1,
Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
Usage = ResourceUsage.Staging,
BindFlags = BindFlags.None,
CpuAccessFlags = CpuAccessFlags.Read | CpuAccessFlags.Write,
OptionFlags = ResourceOptionFlags.None
};
var final_descriptor = new Texture2DDescription
{
Width = Width,
Height = Height,
MipLevels = 1,
ArraySize = 1,
Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
Usage = ResourceUsage.Default,
BindFlags = BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None
};
var dataRectangle = new SharpDX.DataRectangle();
unsafe
{
byte* dataInBytes;
uint capacityInBytes;
((InteropStatics.IMemoryBufferByteAccess)bufferReference).GetBuffer(out dataInBytes, out capacityInBytes);
dataRectangle.DataPointer = (IntPtr)dataInBytes;
dataRectangle.Pitch = 4;
}
Texture2D _stagingTexture = new Texture2D(device, staging_descriptor, dataRectangle);
Texture2D _finalTexture = new Texture2D(device, final_descriptor);
_stagingTexture.Device.ImmediateContext.CopyResource(_stagingTexture, _finalTexture);
我的问题有两个:
- DataRectangle 使用 IntPtr 类型,而从接口检索的指针是 Byte 数组。这不是问题吗?或者 DataRectangle 中的音高成员是否解决了这个问题?现在我将 byteArray 转换为 IntPtr。
- 这种方法行得通吗?或者有没有更好的方法来处理这个?
任何指示、建议或建设性批评将不胜感激!
解决方案
前段时间我一直在寻找同样的东西,我想出了这个功能,它总是适用于我的用例
public static Texture2D CreateTexture2DFrombytes(Device device, byte[] RawData, int width, int height)
{
Texture2DDescription desc;
desc.Width = width;
desc.Height = height;
desc.ArraySize = 1;
desc.BindFlags = BindFlags.ShaderResource;
desc.Usage = ResourceUsage.Immutable;
desc.CpuAccessFlags = CpuAccessFlags.None;
desc.Format = Format.B8G8R8A8_UNorm;
desc.MipLevels = 1;
desc.OptionFlags = ResourceOptionFlags.None;
desc.SampleDescription.Count = 1;
desc.SampleDescription.Quality = 0;
DataStream s = DataStream.Create(RawData, true, true);
DataRectangle rect = new DataRectangle(s.DataPointer, width * 4);
Texture2D t2D = new Texture2D(device, desc, rect);
return t2D;
}
推荐阅读
- mysql - 植物和营养素库存数据库设计
- r - 我可以使用 visNetwork 包创建家谱吗?(如家谱)
- cakephp - 如何在 cakephp3 中建立关系
- javascript - 过滤选择输入的最佳方法是什么?
- hsm - 如何从存储在 HSM 中的私钥中获取公钥
- json - 使用 Angular 从 API 获取 json 数据很困难
- batch-file - 批处理文件中的处理和拆分行
- java - Java中的Dgraph。如何运行原始字符串突变查询?
- ruby-on-rails - 载波 object.url VS object.image_url
- json - Struts 2 Action 不使用 json 数据