如何将Mipmap数据转换为位图?

时间:2019-03-05 04:35:20

标签: bitmap directx directx-12 dds-format

我有一个.DDS纹理文件,其格式为DXGI_FORMAT_BC3_UNORM,其中包含我在此处链接的10个mipmap:

https://1drv.ms/u/s!AiGFMy6hVmtN1Ba3UZsc8682VcEO

我想将此纹理的每个单独的mipmap显示到我的XAML应用程序的UI中,因此我需要将它们转换为位图。

我从DirectXTK12帮助程序库的DirectX::LoadDDSTextureFromMemory函数的第5个参数中获取每个mipmap的数据,该函数返回一个std::vector<D3D12_SUBRESOURCE_DATA>,其中填充了所有10个mipmap的数据。

我可以使用以下代码将质量最高的mipmap 0转换为位图:

std::vector<unsigned char> bytes = get_data_from_dds_file(L"WoodCrate01.dds");

std::vector<D3D12_SUBRESOURCE_DATA> subresources;
winrt::check_hresult(
    DirectX::LoadDDSTextureFromMemory(
        g_device,
        &bytes.front(),
        file_size,
        gpu_tex_resource.put(),
        subresources
    )
);

auto current_index = 0;

auto current_slice_pitch = subresources[current_index].SlicePitch;

std::vector<unsigned char> current_subresource_data;
current_subresource_data.resize(current_slice_pitch);
memcpy(&current_subresource_data[0], subresources[current_index].pData, current_slice_pitch);

Windows::Storage::Streams::InMemoryRandomAccessStream random_stream;
Windows::Storage::Streams::DataWriter writer(random_stream);

writer.WriteBytes(current_subresource_data);
co_await writer.StoreAsync();

auto decoder = co_await Windows::Graphics::Imaging::BitmapDecoder::CreateAsync(random_stream);
// Subresources at index higher than 0 crash at this point with the error: 0x88982F50 : 'The component cannot be found.'.

// The subresource at index 0 doesn't crash at the previous line, so I turn it into a SoftwareBitmap so that I can show it in XAML.
auto new_software_bitmap = co_await decoder.GetSoftwareBitmapAsync();

new_software_bitmap = Windows::Graphics::Imaging::SoftwareBitmap::Convert(
    new_software_bitmap,
    Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8,
    Windows::Graphics::Imaging::BitmapAlphaMode::Premultiplied);

Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource new_bitmap_source;
co_await new_bitmap_source.SetBitmapAsync(new_software_bitmap);
[...]

当我尝试将current_index更改为1或更高时,问题开始了。在尝试创建0x88982F50 : 'The component cannot be found.'的那一行出现错误BitmapDecoder

1 个答案:

答案 0 :(得分:0)