我在研究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: エラーを特定できません
我在網上搜索相同的問題或解決方案,但我找不到任何東西。
請給我任何建議。
謝謝,
我感謝您的幫助。 我嘗試使用Texture2DDescription修改我的代碼。 – yamamo2