SharpDX:从DataStream初始化Texture2D失败(虽然Texture1D工作正常)

Jen*_*lte 2 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.
    }
}
Run Code Online (Sandbox Code Playgroud)

在不传递数据的情况下创建纹理工作正常.谁能告诉我如何修复Texture2D初始化?

xoo*_*ofx 8

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

new DataBox(stream.DataPointer, 8 * 4)
Run Code Online (Sandbox Code Playgroud)

或者以更通用的方式:

new DataBox(stream.DataPointer, description2D.Width
            * (int)FormatHelper.SizeOfInBytes(description2D.Format))
Run Code Online (Sandbox Code Playgroud)

  • 第三个参数用于Texture3D(它表示Z切片的字节大小),对于Texture2D应该设置为0.Texture1D并不关心这些参数,因为整个信息已经存储在Texture1DDescription中(它只是使用width*sizeof(pixelFormat)).MSDN文档中对此进行了解释:http://msdn.microsoft.com/en-us/library/windows/desktop/ff476182%28v=vs.85%29.aspx (2认同)