SharpDX代码中Texture2D.FromMemory()的异常 [英] Exception of Texture2D.FromMemory() in SharpDX code

查看:179
本文介绍了SharpDX代码中Texture2D.FromMemory()的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究DirectX和SharpDX(使用2.6.2版)。

I'm studying the DirectX and SharpDX(use ver.2.6.2).

现在,我尝试使用Texture2D从字节数组创建纹理。 FromMemory()方法。
我的示例代码如下。

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

但是,我的代码出现了

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.

请给我任何建议。

谢谢

推荐答案

您不能从这样的内存块创建纹理。方法 Texture2D.FromMemory 期望 Texture2D.FromFile 支持的相同纹理。唯一的区别是

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.

从原始内存块创建纹理需要创建一个新的 Texture2D()通过 DataRectangle 结构来自纹理的 Texture2DDescription 和数据的存储区域。该描述包括宽度,高度,像素格式,mip数,数组数等。等效的本机方法是 ID3D11Device :: CreateTexture2D

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.

最后,像 Texture2D.FromFile / FromMemory 这样的D3DX函数都使用相同的 ID3D11Device :: CreateTexture2D 创建纹理。

In the end, the D3DX functions like Texture2D.FromFile/FromMemory are using the same ID3D11Device::CreateTexture2D to create a texture.

这篇关于SharpDX代码中Texture2D.FromMemory()的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆