2

我正在尝试从内存数据创建一个 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 初始化吗?

4

1 回答 1

9

您需要将纹理 2D 的行跨度传递到 DataBox。就像是:

new DataBox(stream.DataPointer, 8 * 4)

或者以更通用的方式:

new DataBox(stream.DataPointer, description2D.Width
            * (int)FormatHelper.SizeOfInBytes(description2D.Format))
于 2012-11-07T01:24:24.967 回答