DX11 CreateTexture2D + automatic mips + initial data

Started by
1 comment, last by MickeyMouse 13 years ago
How shall one create 2D texture in DX11 with automatic mipmaps generation enabled and with initial data containing top mip level data?

Here's my code and it crashes inside CreateTexture2D call:

D3D11_TEXTURE2D_DESC desc;
[...]
desc.MipLevels = 0; // enable mipmaps generation during creation
[...]

D3D11_SUBRESOURCE_DATA initialData;
initialData.pSysMem = my_data;
initialData.SysMemPitch = my_pitch;
initialData.SysMemSlicePitch = 0;

ID3D11Texture2D* handle;
dx11device->CreateTexture2D(&desc, &initialData, &handle);

I guess the cause of the crash is due to CreateTexture2D expecting an array of D3D11_SUBRESOURCE_DATAs, one for each mip level. But this doesn't make any sense, does it? All I want is given top mip level create texture with full mip chain and autogenerate remaining mip levels at startup.

Thanks for any hints from DX11 gurus!
Maciej Sawitus
my blog | my games
Advertisement
You can't partially initialize a resource with data. You either set the data for all subresources, or for none. So you have these options:

1. Use ID3D11DeviceContext::UpdateSubresource to set the data for the first mip level, and then use GPU auto-generation
2. Use ID3D11DeviceContext::UpdateSubresource to set the data for the first mip level, and then use D3DX11FilterTexture to generate the mip maps
3. Create a texture with the same size/format with only 1 mip level that initialized with your data, then use CopyResource to copy it to the first mip level of the texture with the full mip chain
4. Create a texture with the same size/format with only 1 mip level that initialized with your data, then use D3DX11LoadTextureFromTexture to fill the mip levels of the texture with the full mip chain
Wow, that's really great answer! :)

Thanks a lot and I think I'm going for option 1 or 2.
Maciej Sawitus
my blog | my games

This topic is closed to new replies.

Advertisement