I'm studying the DirectX and SharpDX(use ver.2.6.2).
Now, I try to create a texture from byte array by using Texture2D.FromMemory() method. My sample code is as follows.
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);
}
}
}
However, my code occurrs the following exception.
An unhandled exception of type 'SharpDX.SharpDXException' occurred in SharpDX.dll
Additional information: HRESULT: [0x80004005], Module: [General], ApiCode:[E_FAIL/Unspecified error], Message: エラーを特定できません
I search same problem or a solution on the web but I cannot find anything.
Please give me any advices.
Thanks,
You can't create a texture from a memory block like this. The method Texture2D.FromMemory
is expecting the same kind of texture supported by Texture2D.FromFile
, the only difference is that instead of reading from the disk, it can read from memory.
Creating a texture from a raw memory block requires to create a new Texture2D()
from a Texture2DDescription
of the texture and a memory region of the data through the DataRectangle
structure. This description includes the width, height, the pixel format, the number of mips, number of arrays...etc. The equivalent native method is ID3D11Device::CreateTexture2D.
In the end, the D3DX functions like Texture2D.FromFile/FromMemory
are using the same ID3D11Device::CreateTexture2D
to create a texture.