Jump to content

View more

Image of the Day

Adding some finishing touches...
Follow us for more
#screenshotsaturday #indiedev... by #MakeGoodGames https://t.co/Otbwywbm3a
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Mip map initialisation

4: Adsense

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 hupsilardee   Members   


Posted 13 September 2012 - 09:22 AM

So my adventures in D3D11 continue. I am struggling to understand how mipmapping works. In this project I am also trying not to depend on D3DX11 so I am creating textures programmatically. However I am struggling to understand how the mip map generation works. Here's how I create a texture (minus error checking etc)

DWORD* loaded_data; // loaded using FreeImage

uint width = FreeImage_GetWidth(image32);
uint height = FreeImage_GetHeight(image32);
uint mips = GetNumMipLevels(width, height);
Desc.Width = width;
Desc.Height = height;
Desc.MipLevels = mips; // auto-generates the whole chain
Desc.MiscFlags = 0;
Desc.ArraySize = 1;
Desc.CPUAccessFlags = 0;
Desc.SampleDesc.Count = 1;
Desc.SampleDesc.Quality = 0;

D3D11_SUBRESOURCE_DATA* initialData = new D3D11_SUBRESOURCE_DATA[mips];

for (uint i = 0; i < mips; i++)
  uint mip = (uint)powf(2.f, (float)i);
  initialData[i].pSysMem = loaded_data;
  initialData[i].SysMemPitch = width * 4;
  initialData[i].SysMemSlicePitch = width * height * 4;

pDevice->pDevice->CreateTexture2D(&Desc, initialData, &pTexture);
delete[] initialData;

ZeroMemory(&viewDesc, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
viewDesc.Format = Desc.Format;
viewDesc.Texture2D.MipLevels = -1;
viewDesc.Texture2D.MostDetailedMip = 0;
viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;

pDevice->pDevice->CreateShaderResourceView(pTexture, &viewDesc, &pView);

return S_OK;

However I don't think this is generating the mip maps correctly, or they aren't being used. Look at the left of the cube in this screenshot

Posted Image
Uploaded with ImageShack.us

I dont think the sampler is at fault, but just in case, it is set up like this
  desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  desc.AddressV = desc.AddressU;
  desc.AddressW = desc.AddressU;
  desc.BorderColor[0] = desc.BorderColor[1] = desc.BorderColor[2] = desc.BorderColor[3] = 1.0f;
  desc.ComparisonFunc = D3D11_COMPARISON_NEVER;
  desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  desc.MaxAnisotropy = 0;
  desc.MaxLOD = 0;
  desc.MinLOD = 0;
  desc.MipLODBias = 0.0f;

So can anyone see what I'm doing wrong?

#2 MJP   Moderators   


Posted 13 September 2012 - 11:51 AM

The way you're trying to do things would require that you have the mipmap data already generated when you created the function. There is no "automatic" mip generation for IMMUTABLE textures, since they require that you pass all of the required data to it. What you're actually doing in your code is you're passing the top-level mip data to all of the lower-resolution mip levels, which isn't right. If you do have all of the mips generated, then for each D3D11_SUBRESOURCE_DATA you need to have it point to the data for that mip. You would also need to set the proper pitch for that mip level, which get smaller as you go up in the levels.

If you don't actually have the mips generated and you want them to be generated by the hardware, you need to set it up differently. Here's an example of how to do it, taken from the WICTextureLoader sampler from DirectXTex:

// Create texture
desc.Width = twidth;
desc.Height = theight;
desc.MipLevels = (autogen) ? 0 : 1;
desc.ArraySize = 1;
desc.Format = format;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.CPUAccessFlags = 0;
desc.MiscFlags = (autogen) ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0;
initData.pSysMem = temp.get();
initData.SysMemPitch = static_cast<UINT>( rowPitch );
initData.SysMemSlicePitch = static_cast<UINT>( imageSize );
ID3D11Texture2D* tex = nullptr;
hr = d3dDevice->CreateTexture2D( &desc, (autogen) ? nullptr : &initData, &tex );
if ( SUCCEEDED(hr) && tex != 0 )
    if (textureView != 0)
	    memset( &SRVDesc, 0, sizeof( SRVDesc ) );
	    SRVDesc.Format = format;
	    SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	    SRVDesc.Texture2D.MipLevels = (autogen) ? -1 : 1;
	    hr = d3dDevice->CreateShaderResourceView( tex, &SRVDesc, textureView );
	    if ( FAILED(hr) )
		    return hr;
	    if ( autogen )
		    assert( d3dContext != 0 );
		    d3dContext->UpdateSubresource( tex, 0, nullptr, temp.get(), static_cast<UINT>(rowPitch), static_cast<UINT>(imageSize) );
		    d3dContext->GenerateMips( *textureView );

#3 hupsilardee   Members   


Posted 03 October 2012 - 06:44 PM

Sorry to bump such an old thread. I would like to say, thanks for the help MJP, and it has been solved as you said. FreeImage has image resize functions which made it very quick and easy, and they support a variety of filters as well, which is great.

I have another question about this method though. It appears that you are marking it for default usage and as a potential render target.
Surely there is a performance hit for this?

(I thought that if the texture is IMMUTABLE on some cards it goes in a different texture memory that the hardware can access faster. However if not then this is doubly excellent, not only is texture generation likely to be much faster due to GPU mip map generation, but all textures can be render targets, so I could have super fast bullet holes and decals!)

I am also bumping to point out that the sampler is in fact incorrectly set up. As the MinLOD and MaxLOD members are both set to 0, this forces the sampler to use only the most detailed mip, which of course is wrong. The correct sampler state is as above, with:

desc.MaxLOD = D3D11_FLOAT32_MAX; // or simply FLT_MAX
desc.MinLOD = 0;

#4 MJP   Moderators   


Posted 04 October 2012 - 11:28 PM

It's possible that using default usage and render target binding could cause different behavior for a texture, but that's completely dependent on the hardware and the driver. Your best bet for GPU performance is to use IMMUTABLE, since that guaranteed that the driver will put the texture in the best possible memory location with the best possible memory layout for that use case.

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.