Sign in to follow this  
jerrycao_1985

DX11 How to generate mipmap for a texture

Recommended Posts

hi all

 

Here is my case:

An empty 2d texture is created and then it is filled with some memory without mipmap information.

 

I want to generate mip mapping for the texture.

It's dx11. I see there is an interface called "GenerateMips" could do some sort of job.

But "GenerateMips" requires the "D3D11_RESOURCE_MISC_GENERATE_MIPS" , which is only available if the texture is both a render target and shader resource.

My texture is just used for texture mapping , it's not a render target of course.

 

So how can I generate mip map using DX11 interface.
( I know I can generate mipmap using cpu code , but I just want to know the professional way of doing it.)

 

Thanks

Share this post


Link to post
Share on other sites

So how can I generate mip map using DX11 interface.
( I know I can generate mipmap using cpu code , but I just want to know the professional way of doing it.)

 

if the dx11 API doesn't provide that functionality, you'll have to do it yourself. actually a great opportunity to implement mipmaps that don't blend the background into the edge of sprite textures. odds are either way (your code or DX11 code) the work will be done by the cpu, not gpu.

Share this post


Link to post
Share on other sites

- If you have photoshop you can generate the mipmap chain using the nvidia dds plugin:

  https://developer.nvidia.com/nvidia-texture-tools-adobe-photoshop

- For mipmap generation, you could integrate nvidia texture tools into your pipeline: http://code.google.com/p/nvidia-texture-tools/

- If you are looking for a general "howto" on generating and loading the chain, I would suggest checking out the replacement code for D3DX load texture:

  (This uses WIC (Windows Imaging Components) and I would not touch it with a 10 ft pole, however, it does show you the steps needed to load the mipmap chain etc...).

  http://msdn.microsoft.com/en-us/library/windows/apps/jj651550.aspx

- I think MJP's framework supports this, along with hieroglyph if you feel like diving through their code:

  (I think hieroglyph integrated the texture code from the Microsoft example when d3dx was phased out).

  http://mynameismjp.wordpress.com/

  http://hieroglyph3.codeplex.com/

Edited by sensate

Share this post


Link to post
Share on other sites

- I think MJP's framework supports this, along with hieroglyph if you feel like diving through their code:

(I think hieroglyph integrated the texture code from the Microsoft example when d3dx was phased out).

http://mynameismjp.wordpress.com/

http://hieroglyph3.codeplex.com/

Hieroglyph uses DirectXTK now that D3DX is deprecated, which is the same WIC method that you mentioned above.  Actually there is an alternative to WIC available in DirectXTK too, so you should take a look and see if it will work for you.

 

But the OP was specifically taking about generating the texture at runtime and then producing the mip-maps.  @jerrycao_1985: Have you tried creating your texture with the render target usage flag, even if you won't use it for a render target?  It would be worth a try to see if it allows you to generate your mips that way.  Otherwise, what the others have said is right - you will probably have to generate the mips on the CPU...

Share this post


Link to post
Share on other sites

Yes.

- If he is targeting Direct3d11 with non Direct3d11 conformant hardware he will have to generate the pyramid on the cpu. Which is why I suggested looking at the nvidia texture tools library.

- If he is targeting Direct3d11 with Direct3d11 conformant hardware he can use compute shaders (and apply compression as needed):

http://code.msdn.microsoft.com/windowsdesktop/BC6HBC7-DirectCompute-35e8884a

 

I have not tried applying rendertarget flags to a texture that I'm uploading data to from a staging resource before (never had the need).

I would be intrigued to hear if this works. If it does, this is of course the simplest line to follow, however you thenmiss out on writing a bunch of code that will be useful to you for as long as you continue to render (as long as you don't write it using DirectCompute).

Edited by sensate

Share this post


Link to post
Share on other sites

I would be intrigued to hear if this works. If it does, this is of course the simplest line to follow, however you thenmiss out on writing a bunch of code that will be useful to you for as long as you continue to render (as long as you don't write it using DirectCompute).

Maybe as a learning exercise, but I haven't ever had the need to generate my own mip-maps...  Can you think of other cases where this would be beneficial code to write?  Seems like a good thing to use a library for, but that is only my opinion.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      628282
    • Total Posts
      2981805
  • Similar Content

    • By noodleBowl
      I was wondering if anyone could explain the depth buffer and the depth stencil state comparison function to me as I'm a little confused
      So I have set up a depth stencil state where the DepthFunc is set to D3D11_COMPARISON_LESS, but what am I actually comparing here? What is actually written to the buffer, the pixel that should show up in the front?
      I have these 2 quad faces, a Red Face and a Blue Face. The Blue Face is further away from the Viewer with a Z index value of -100.0f. Where the Red Face is close to the Viewer with a Z index value of 0.0f.
      When DepthFunc is set to D3D11_COMPARISON_LESS the Red Face shows up in front of the Blue Face like it should based on the Z index values. BUT if I change the DepthFunc to D3D11_COMPARISON_LESS_EQUAL the Blue Face shows in front of the Red Face. Which does not make sense to me, I would think that when the function is set to D3D11_COMPARISON_LESS_EQUAL the Red Face would still show up in front of the Blue Face as the Z index for the Red Face is still closer to the viewer
      Am I thinking of this comparison function all wrong?
      Vertex data just in case
      //Vertex date that make up the 2 faces Vertex verts[] = { //Red face Vertex(Vector4(0.0f, 0.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(100.0f, 100.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(100.0f, 0.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(0.0f, 0.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(0.0f, 100.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(100.0f, 100.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), //Blue face Vertex(Vector4(0.0f, 0.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(100.0f, 100.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(100.0f, 0.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(0.0f, 0.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(0.0f, 100.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(100.0f, 100.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), };  
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By turanszkij
      If I do a buffer update with MAP_NO_OVERWRITE or MAP_DISCARD, can I just write to the buffer after I called Unmap() on the buffer? It seems to work fine for me (Nvidia driver), but is it actually legal to do so? I have a graphics device wrapper and I don't want to expose Map/Unmap, but just have a function like void* AllocateFromRingBuffer(GPUBuffer* buffer, uint size, uint& offset); This function would just call Map on the buffer, then Unmap immediately and then return the address of the buffer. It usually does a MAP_NO_OVERWRITE, but sometimes it is a WRITE_DISCARD (when the buffer wraps around). Previously I have been using it so that the function expected the data upfront and would copy to the buffer between Map/Unmap, but now I want to extend functionality of it so that it would just return an address to write to.
    • By mister345
      Trying to write a multitexturing shader in DirectX11 - 3 textures work fine, but adding 4th gets sampled as black!
      Could you please look at the textureClass.cpp line 79? - I'm guess its D3D11_TEXTURE2D_DESC settings are wrong, 
      but no idea how to set it up right. I tried changing ArraySize from 1 to 4, but does nothing. If thats not the issue, please look
      at the LightShader_ps - maybe doing something wrong there? Otherwise, no idea.
          // Setup the description of the texture.
          textureDesc.Height = height;
          textureDesc.Width = width;
          textureDesc.MipLevels = 0;
          textureDesc.ArraySize = 1;
          textureDesc.Format = DXGI_FORMAT_R8G8B8A8_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;
      Please help, thanks.
      https://github.com/mister51213/DirectX11Engine/blob/master/DirectX11Engine/Texture.cpp
       
    • By GameDevCoder
      I have to learn DirectX for a course I am studying. This book https://www.amazon.co.uk/Introduction-3D-Game-Programming-Directx/dp/1936420228 I felt would be great for me to learn from.
      The trouble is the examples which are all offered here http://www.d3dcoder.net/d3d11.htm . They do not work for me. This is a known issue as there is a link on the examples page saying how to fix it. I'm having difficulty with doing this though. This is the page with the solution http://www.d3dcoder.net/Data/Book4/d3d11Win10.htm.
      The reason why this problem is happening, the book was released before Windows 10 was released. Now when the examples are run they need slight fixes in order for them to even work. I just can't get these examples working at all.
      Would anyone be able to help me get the examples working please. I am running Windows 10 also just to make this clear, so this is why the examples are experiencing the not so desired behaviour. I just wish they would work straight away but there seems to be issues with the examples from this book mainly because of it trying to run from a Windows 10 OS.
      On top of this, if anyone has any suggestions with how I can learn DirectX 11 i would be most grateful. Thanks very much. I really would like to get them examples working to though from the book I mentioned.
      Look forward to reading any replies this thread receives.
       
      GameDevCoder.


      PS - If anyone has noticed. I asked this about 1 year ago also but this was when I was dabbling in it. Now I am actually needing to produce some stuff with DirectX so I have to get my head round this now. I felt at the time that I sort of understood what was being written to me in response to my thread back then. I had always been a little unsure though of being absolutely sure of what was happening with these troublesome examples. So I am really just trying to get to the bottom of this now. If anyone can help me work these examples out so I can see them working then hopefully I can learn DirectX 11 from them.
       
      *SOLUTION* - I was able to get the examples running thanks to the gamedev.net community. Great work guys. I'm so please now that I can learn from this book now I have the examples running.
      https://www.gamedev.net/forums/topic/693437-i-need-to-learn-directx-the-examples-for-introduction-to-3d-programming-with-directx-11-by-frank-d-luna-does-not-work-can-anyone-help-me/?do=findComment&comment=5363013
  • Popular Now