簡體   English   中英

SharpDX:從DataStream初始化Texture2D失敗(雖然Texture1D工作正常)

[英]SharpDX: Initializing a Texture2D from a DataStream fails (though Texture1D works fine)

我正在嘗試從內存數據創建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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM