簡體   English   中英

來自字節數組 Sharpdx 的 Texture2d

[英]Texture2d from Byte array Sharpdx

我是 C#、SharpDX 和 Directx 新手。 請原諒我的無知。 我正在跟進一篇舊帖子: SharpDX 代碼中的 Texture2D.FromMemory() 異常 這是非常有幫助的。

我的目標:

  1. 從軟件位圖構建 Texture2d。
  2. 使紋理可用於 HLSL。

我接近它的方式:

  1. 使用 IMemoryBufferByteAccess,我能夠檢索指向字節的指針和 Frame 的總容量。 從上一篇文章來看,我似乎需要使用 DataRectangle 來指向字節數組。
  2. 有 2 個具有不同描述符的紋理 - Texture1 (_staging_texture) - 無綁定標志,cpu 讀寫權限,用法 - 暫存。 我用指向字節數組的 datarectangle 創建了這個紋理。 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);

我的問題有兩個:

  1. DataRectangle 使用 IntPtr 類型,而從接口檢索的指針是 Byte 數組。這不是問題嗎? 或者 DataRectangle 中的音高成員是否解決了這個問題? 現在我將 byteArray 轉換為 IntPtr。
  2. 這種方法行得通嗎? 或者有沒有更好的方法來處理這個問題?

任何指示、建議或建設性的批評將不勝感激!

不久前,我一直在尋找相同的功能,並且我想出了這個功能對於我的用例來說總是很好用

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM