c# - SharpDX:从 DataStream 初始化 Texture2D 失败(尽管 Texture1D 工作正常)

标签 c# directx textures directx-11 sharpdx

我正在尝试从内存数据创建 SharpDX.Direct3D11.Texture2D,但总是得到 SharpDXException(HRESULT:0x80070057,“参数不正确。”)。我为此目的使用了 Texture1D,之前可以毫无问题地创建它。

我已将代码缩减为仍然会产生异常的示例:

using (var device = new Device(DriverType.Hardware, DeviceCreationFlags.Debug)) {
    // empty stream sufficient for example
    var stream = new DataStream(16 * 4, true, true);

    var description1D = new Texture1DDescription() {
        Width = 16,
        ArraySize = 1,
        Format = Format.R8G8B8A8_UNorm,
        MipLevels = 1,
    };
    using (var texture1D = new Texture1D(device, description1D, new[] { new DataBox(stream.DataPointer) })) {
        // no exception on Texture1D
    }

    var description2D = new Texture2DDescription() {
        Width = 8,
        Height = 2,
        ArraySize = 1,
        MipLevels = 1,
        Format = Format.R8G8B8A8_UNorm,
        SampleDescription = new SampleDescription(1, 0),
    };
    using (var texture2D = new Texture2D(device, description2D, new[] { new DataBox(stream.DataPointer) })) {
        // HRESULT: [0x80070057], Module: [Unknown], ApiCode: [Unknown/Unknown], Message: The parameter is incorrect.
    }
}

在不传递数据的情况下创建纹理效果很好。谁能告诉我如何修复 Texture2D 初始化?

最佳答案

您需要将 2D 纹理的行步幅传递到 DataBox 中。像这样的东西:

new DataBox(stream.DataPointer, 8 * 4)

或者以更通用的方式:

new DataBox(stream.DataPointer, description2D.Width
            * (int)FormatHelper.SizeOfInBytes(description2D.Format))

关于c# - SharpDX:从 DataStream 初始化 Texture2D 失败(尽管 Texture1D 工作正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13261427/

相关文章:

c# - Akavache 的 GetObject<T> 在等待时挂起。知道这里出了什么问题吗?

C++低级文件加密函数

directx - SharpDX:从 D3D10.Texture2D 创建 D3D11.Texture2D?

c++ - 我怎样才能让这个函数返回一个 sf::Texture? SFML2.0

c# - finally block 中的异常

c# - SQL 异常 - 与网络相关或特定于实例。 SQL Express "It works on my machine"问题

ios - 在 iOS 中创建 RGB CVOpenGLESTexture

c# - XNA 4.0 模型纹理问题

c# - 对数据库存储的信息实现复杂的算法

c++ - 二维矩阵变换(有玩家和地面)