# D3D9 Generating Mip Maps of Rendertargets...

This topic is 3371 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am really hoping for some help - this one has me completely stumped... Here goes: I am trying to generate mip maps of my render target textures after rendering a scene into them. The documentation says to create the textures with the D3DUSAGE_AUTOGENMIPMAP flag, which I am doing. I am also using the D3DUSAGE_RENDERTARGET flag to allow me to render into the texture. The documentation also says to use check device format to check if the auto gen feature is supported for a particular format like so:
m_pD3D9->CheckDeviceFormat( 0, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DUSAGE_AUTOGENMIPMAP | D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A8R8G8B8 );

The return value is S_OK, meaning that it should work out just fine. The problem is, I can create the mip maps if I create the textures with a 0 levels argument like so:
HRESULT hr =
D3DXCreateTexture( m_pDevice, 512, 512,
0, D3DUSAGE_AUTOGENMIPMAP | D3DUSAGE_RENDERTARGET,
D3DFMT_A8R8G8B8, 0, (IDirect3DTexture9**)&pTexture);


I have checked with PIX to make sure the mip levels are being created, but even if I call the texture's generate mip level function GenerateMipSubLevels function, then all the sub levels show up black in PIX. I tried using the reference device, and had the same result - the mip levels are created but not ever updated. So I am pretty sure I must be doing something wrong (hopefully someone sees it!). I looked around and found some hints to use a 1 for the levels argument, but that doesn't even create the mip levels. Has anyone had this problem before? If so, how do I get around it? I could lock the texture and generate my own mip textures, but that is going to be slow as hell and I have a bunch of textures to update every frame. Does anyone have any suggestions??? Thanks in advance for any help!

##### Share on other sites
Looking at the SDK docs, I found out a couple of things that may help you (topic "Automatic Generation of Mipmaps"):

"D3DUSAGE_AUTOGENMIPMAP is only a hint, and specifying this during texture creation or when calling CheckDeviceFormat would not cause an error on any of the device driver interface (DDI) types."

So, you're not guaranteed to have support for automatic mipmap generation by just using CheckDeviceFormat.
Then, I found this:

"To check for support for automatic generation of mipmaps, check to see that D3DCAPS2_CANAUTOGENMIPMAP is set. If it is, call CheckDeviceFormat with D3DUSAGE_AUTOGENMIPMAP. If the return value is D3D_OK, the mipmaps are guaranteed to be autogenerated. If the return value is D3DOK_NOAUTOGEN, this means that the create call will succeed, but there won't be any mipmaps generated."

So you can check first for the D3DCAPS2_CANAUTOGENMIPMAP to know if your device supports automatic mipmap generation.

Hope this helps

##### Share on other sites
Thank you for the reply. I checked the caps bit that you mentioned using the caps viewer, and I do have support for the auto gen mip map. Check device format also returns S_OK. I'm not sure what to try next...

I think that since the reference device isn't creating the mip channels that something must be set up incorrectly. Is anyone successfully using this technique to update mip maps?

##### Share on other sites
I wanted to bump this topic, since I have tried an alternative method that still isn't seeming to solve the problem. Since I couldnt' get the mip maps to generate automatically, I decided to try to manually lock them and fill in the mips myself. However, since the textures are used as render targets I apparently can't lock the surfaces, making manual generation impossible.

So my questions is this: does anyone know of a functioning example or tutorial where it autogenerates the mip maps of a render target? Then I could try it out on my machine to see if the problem is on this laptop or if it is something that I am doing wrong. There are no D3D errors, but it doesn't seem to work...

Any help will be really appreciated!

##### Share on other sites
Using GenerateMipSubLevels ought to work, but I recall some problem with this once, I cant remember what. Try creating the texture using the device interface not the D3DX function...

Or you can do it manually using another method: create a second texture with a full mip map chain, also setting as a render target, with same format. Then use StretchRect to make the mip maps manually from the source texture.

##### Share on other sites
I've always had the device auto-generate mip levels when I use variance shadow mapping. It always seemed to work on my 4870 and on the 8800 I had before that. I don't know if I have anything that you could try on your PC thought...

##### Share on other sites
Thanks for the replies guys.

My main development machine is a laptop with an 8600M GT, so laptop drivers could potentially be suspect. I have an old desktop with a GeForce 6200 in it that I can update with the most recent drivers and try to run my program on that. If I still can't get it to work, I'll try posting the exe and see if someone can run it on a confirmed functional machine.

@MJP: Didn't you write an article on variance shadow maps for GPU Gems 3? If so, was there a demo that used the autgen mipmaps (in D3D9?). I can check when I get home - I don't recall if there was a demo included in the CD or not.

@Matt: That's an interesting concept, I hadn't thought of using stretch rect to make the copies. I'll experiment with this if I don't make any progress with the driver topics...

Thanks again for the replies!

##### Share on other sites
Quote:
 Original post by Jason Z@MJP: Didn't you write an article on variance shadow maps for GPU Gems 3? If so, was there a demo that used the autgen mipmaps (in D3D9?). I can check when I get home - I don't recall if there was a demo included in the CD or not.

No, that would be Andrew Lauritzen (AndyTX). I'm pretty sure he does auto-generate mip-maps, however that sample is written in D3D10.

##### Share on other sites
Well, using stretch rect has worked to a certain extent. I am able to copy from one surface to another and get the mip maps filled with the correct contents. It would still be better to get it automatically, but I'm too much of a pragmatist to worry about it much more.

I'll still check out my machine at home and see if it automatically generates them or not. I'll bump this post with my results over the next couple of days...

Thanks again for your help and suggestions!