I got the impression that this is what you want, for D3D to automatically generate your mipmaps for you.
This is usually done when you actually render data to your texture render-target, and want the mip-maps auto-generated on the GPU after re-rendering the top level.
There is an easier way to do this when loading textures, by calculating the mips on the CPU, and then pass them in with the init-data. I believe D3DX11CreateTextureFromMemory does this automatically for you, if you can use D3DX11.
If you do not want to use that function, but do it yourself, there are two ways. You can manually resize your texture to half its size over and over to calculate all the mip-maps, and upload that data to the texture, using either init-data or UpdateSubresource. There is no real difference between init-data and UpdateSubresource if you have a default usage texture, except for that UpdateSubresource lets you update only one particular mip-level (be it the largest or any other).
The other option is to let D3D generate mipmaps with D3D11_RESOURCE_MISC_GENERATE_MIPS.
Using UpdateSubresource you can at any time change a D3D11_USAGE_DEFAULT texture without re-creating the texture.
Is your goal to do this during a game-frame, and automatically have mipmaps generated for your new texture?
Or do you just want to create a texture with mipmaps once that is static until the next time data/levels/other is reloaded?
If you do want the mip-maps auto-generated like that, then create your texture with default usage, mipmap-generation, and 0 as MipLevels, without any initialization data. Then call UpdateSubresource to update mip-level 0 and then call GenerateMips.
UpdateSubresource should be used something like this:
D3D11_BOX box; box.left = 0; box.right = width; box.top = 0; box.bottom = height; box.front = 0; box.back = 1; deviceContext->UpdateSubresource( texture2D, // the texture to update 0, // first mip-level &destBox, // position of the pixels to update in the texture pixels.Get(), // image data width * 4, // row pitch 0 // not used for 2D textures };
Also, I recommend that you visit the documentation-pages at MSDN or in the documentation that comes with the DirectX SDK, and carefully read through all the documentation for the functions you use. It is immensely helpful and will make you a better programmer. Everything we have said is described in detail on the documentation pages. You are not supposed to know everything by heart, but know where to look it up when you need it.
Edited by Erik Rufelt, 07 September 2012 - 03:37 AM.