我有一个 .DDS 纹理文件,其格式DXGI_FORMAT_BC3_UNORM
包含 10 个我已在此处链接的 mipmap:
https://1drv.ms/u/s!AiGFMy6hVmtN1Ba3UZsc8682VcEO
我想将此纹理的每个单独的 mipmap 显示到我的 XAML 应用程序的 UI,因此我需要将它们转换为位图。
我DirectX::LoadDDSTextureFromMemory
从 DirectXTK12 辅助库中的函数的第 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(¤t_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
.