2014-07-13 43 views
0

我在研究DirectX和SharpDX(使用ver.2.6.2)。SharpDX代碼中Texture2D.FromMemory()的例外

現在,我嘗試使用Texture2D.FromMemory()方法從字節數組創建紋理。 我的示例代碼如下。

using System; 
using System.Diagnostics; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

using SharpDX; 
using SharpDX.D3DCompiler; 
using SharpDX.Direct3D; 
using SharpDX.Direct3D11; 
using SharpDX.DXGI; 
using SharpDX.Windows; 
using Buffer = SharpDX.Direct3D11.Buffer; 
using Device = SharpDX.Direct3D11.Device; 
using MapFlags = SharpDX.Direct3D11.MapFlags; 

namespace HLSLTest 
{ 
    static class Program 
    { 
     [STAThread] 
     static void Main() 
     { 
      Form1 form = new Form1(); 
      form.Text = "D3DRendering - Test"; 
      form.Width = 640; 
      form.Height = 480; 

      Device device; 
      SwapChain swapChain; 

      var desc = new SwapChainDescription() 
      { 
       BufferCount = 1, 
       ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), 
       IsWindowed = true, 
       OutputHandle = form.Handle, 
       SampleDescription = new SampleDescription(1, 0), 
       SwapEffect = SwapEffect.Discard, 
       Usage = Usage.RenderTargetOutput 
      }; 

      Device.CreateWithSwapChain(
       SharpDX.Direct3D.DriverType.Hardware, 
       DeviceCreationFlags.None, 
       new[] { 
        SharpDX.Direct3D.FeatureLevel.Level_11_0, 
        SharpDX.Direct3D.FeatureLevel.Level_10_1, 
        SharpDX.Direct3D.FeatureLevel.Level_10_0, 
       }, 
       desc, 
       out device, 
       out swapChain 
      ); 

      // It's Ok, no error 
      //var texture = Texture2D.FromFile<Texture2D>(device, "GeneticaMortarlessBlocks.jpg"); 

      // "An unhandled exception of type 'SharpDX.SharpDXException' occurred in SharpDX.dll" 
      // "Additional information: HRESULT: [0x80004005], Module: [General], ApiCode: [E_FAIL/Unspecified error]" 
      byte[] texArray = new byte[8]; 
      var texture = Texture2D.FromMemory(device, texArray); 

      var textureView = new ShaderResourceView(device, texture); 
     } 
    } 
} 

但是,我的代碼發生以下異常。

An unhandled exception of type 'SharpDX.SharpDXException' occurred in SharpDX.dll 
Additional information: HRESULT: [0x80004005], Module: [General], ApiCode:[E_FAIL/Unspecified error], Message: エラーを特定できません 

我在網上搜索相同的問題或解決方案,但我找不到任何東西。

請給我任何建議。

謝謝,

回答

1

你不能從這樣的內存塊創建一個紋理。方法Texture2D.FromMemory期待Texture2D.FromFile支持的相同種類的紋理,唯一的區別是不是從磁盤讀取,而是從內存中讀取。

從原始內存塊創建紋理需要從紋理的Texture2DDescription和通過DataRectangle結構的數據的內存區域創建新的Texture2D()。此說明包括寬度,高度,像素格式,mips數量,陣列數量等。等效的本地方法是ID3D11Device::CreateTexture2D

最後,D3DX功能如Texture2D.FromFile/FromMemory正在使用相同的ID3D11Device::CreateTexture2D來創建紋理。

+0

我感謝您的幫助。 我嘗試使用Texture2DDescription修改我的代碼。 – yamamo2