Jump to content

  • Log In with Google      Sign In   
  • Create Account


Directx 11 Mipmaps, Subresources and MapSubresource


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 SGStino   Members   -  Reputation: 119

Like
0Likes
Like

Posted 02 January 2014 - 12:03 PM

hi,

I'm generating mipmaps from a bitmap texture i loaded, and i want to save those individual mipmaps.
But when i'm trying to access them I get weird results.

Lets say i have a Texture2D called texBegin, with one miplevel (so only the full size).

Then i create a new one with the same description but with the desired amount of mipmaps called texMipped and call:

context.CopySubresourceRegion(texBegin, 0, texMipped, 0,0,0,0);
context.GenerateMips(texMippedView); 

to copy the first mipmap and generate the rest of the mip chain.

the texMipped has Default BindFlags, so i can't access it on the cpu. To solve this, it's copied to a staging texture:

context.CopyResource(texMipped, texStaged);

now i use MapSubresources to acces each individual mip level.

DataBox data = context.MapSubresource(texStaged, mipLevel, MapMode.Read, MapFlags.None);

And this is where it becomes weird, lets say, i have a 128x128 texture with mips all the way down to 1x1 (that would be 8 mip levels, right?),
and when texture subresources are layed out like this:

IC520448.png 
(source: msdn)

then i would expect DataBoxes like this (for a texture format of 32bit per pixel, 4 bytes per pixel):

0: 128*128*4=65536 bytes of data, with a pitch of 512.
1: 64*64*4=16384 bytes of data, with a pitch of 256.
2: 32*32*4=4096 bytes of data, with a pitch of 128.
3: 16*16*4=1024 bytes of data, with a pitch of 64.
4: 8*8*4=256 bytes of data, with a pitch of 32.
5: 4*4*4=64 bytes of data, with a pitch of 16.
6: 2*2*4=16 bytes of data, with a pitch of 8.
7: 1*1*4=16 bytes of data, with a pitch of 4.

but instead i get:

0: 128*128*4=65536 bytes of data, with a pitch of 512.
1: 64*64*4=16384 bytes of data, with a pitch of 256.
2: 32*32*4=4096 bytes of data, with a pitch of 128.
3: 2048 bytes of data, with a pitch of 128.
4: 1024 bytes of data, with a pitch of 128.
5: 512 bytes of data, with a pitch of 128.
6: 256 bytes of data, with a pitch of 128.
7: 128 bytes of data, with a pitch of 128.

What am i doing wrong here?

The goal is, that i can save these individual mip levels, and load them into a full mip chain, or a partially filled mip chain as they are loading, or  load the individual mip levels into textures of their own. 

 

 

And the opposite of this: loading cpu-generated mipmaps doesn't seem to be working either.

DataRectangle[] data = new DataRectangle[mipLevels];
// data according to the first table earlier in this post ..
description.MipLevels = mipLevels;
Texture2D texture = new Texture2D(device, description, data);

This throws an exception when creating the texture.

So i'm not sure what i'm doing wrong here.

ps: I'd also like to know how to use Texture2D.LoadTextureFromTexture, as that one always throws exceptions when I try to use it to copy the texture and let it generate the miplevels.



Sponsor:

#2 MJP   Moderators   -  Reputation: 8773

Like
1Likes
Like

Posted 02 January 2014 - 05:04 PM

The pitch of 2D textures is typically padded to accommodate hardware restrictions. You'll need to account for that when reading the data that you get from Map.



#3 SGStino   Members   -  Reputation: 119

Like
0Likes
Like

Posted 03 January 2014 - 04:09 AM

Is there a way to know how much the padding will be on different platforms? because when creating textures from data generated data, how can i create the same padding again to get it in there?

I mean, if i save it with the padding like i get it from the hardware and try to load it on a different system, will it work with the same padding?


Edited by SGStino, 03 January 2014 - 07:11 AM.


#4 unbird   Crossbones+   -  Reputation: 4012

Like
1Likes
Like

Posted 03 January 2014 - 09:42 AM

No way, this is something the driver decides and can even change from call to call.
 
But this is for the Map call only and for initialized texture creation you're free to decide the pitch (almost, I think they need to be 4-byte aligned). Actually your sample works fine for me so there must be something else off. What does the D3D debug layer say ?
 
The underlying function of Texture2D.LoadTextureFromTexture is D3DX11LoadTextureFromTexture, a D3DX function. In case of failure you won't get debug messages here normally, unless you did a custom compilation of SlimDX using the debug library (d3dx11d.lib). Read here




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS