Правильное использование MipLevel
Создаю текстуру:
Texture::Texture( Graphics& gfx, uint32_t atlas_width, uint32_t atlas_height, uint32_t size, std::vector<std::byte>& data, UINT slot )
:
slot(slot)
{
INFOMAN(gfx)
D3D11_TEXTURE2D_DESC textureDesc = {};
textureDesc.Width = (UINT)atlas_width;
textureDesc.Height = (UINT)atlas_height;
textureDesc.MipLevels = CalculateNumberOfMipLevels(atlas_width, atlas_height);
textureDesc.ArraySize = size;
textureDesc.Format = DXGI_FORMAT_R8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
std::vector<D3D11_SUBRESOURCE_DATA> sSubData;
for (auto i = 0; i < size; i++)
{
for (uint32_t index_mip = 0; index_mip < CalculateNumberOfMipLevels(atlas_width, atlas_height); index_mip++)
{
uint32_t mip_width = atlas_width >> index_mip;
D3D11_SUBRESOURCE_DATA& subresource_data = sSubData.emplace_back(D3D11_SUBRESOURCE_DATA{});
subresource_data.pSysMem = std::data(data) + i * atlas_height * atlas_width;
subresource_data.SysMemPitch = textureDesc.Width;
subresource_data.SysMemSlicePitch = 0;
}
}
wrl::ComPtr<ID3D11Texture2D> pTexture;
GFX_THROW_INFO( GetDevice( gfx )->CreateTexture2D(
&textureDesc, sSubData.data(), &pTexture
) );
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {}; //
srvDesc.Format = textureDesc.Format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
srvDesc.Texture2DArray.MostDetailedMip = 0;
srvDesc.Texture2DArray.MipLevels = CalculateNumberOfMipLevels(atlas_width, atlas_height);
srvDesc.Texture2DArray.FirstArraySlice = 0;
srvDesc.Texture2DArray.ArraySize = textureDesc.ArraySize;
GFX_THROW_INFO(GetDevice(gfx)->CreateShaderResourceView(
pTexture.Get(), &srvDesc, &pTextureView
));
GetContext( gfx )->GenerateMips( pTextureView.Get() );
}
Но, если я включаю поддержку mip, тогда текстура становится странной. Как я думаю, неправильно высчитываю SysMemPitch, но если я прибавляю какое-то значение, например, textureDesc.Width * 2( для эксперемента ). Тогда крашится проект. Подскажите, кто знает.
