How to create a Texture2DArray from files in DX11?

Started by
12 comments, last by pnt1614 11 years, 3 months ago

I have list of files, now I want to create a Texture2DArray and bind to the shader but I do not know how to do it.

According to MJP in this topic: http://www.gamedev.net/topic/612963-how-do-i-load-id3d10shaderresourceview-into-texture2darray/. If we load all of textures using D3DX11CreateTextureFromFile, then all of textures need to be the same size, have the same format, and have the same number of mip levels. But all I have are a list of files (bmp, jpg....) and I think they have different sizes.

Anybody help me, please? If you have time, could you give me an example with source code?. Thank in advance.

Advertisement
Here is the code I'm using the load textures in texture array. It contains only the important parts, so you can't use it directly.

[Edit]

You'll need to create the texture array separately.

Cheers!

[source]

int D3DTexture::LoadTextureInToTextureArray(const char *pAddr,int Index)
{
// pAddr should point to file loaded in memory
// Index is a number between 0 and ArraySize


D3D11_TEXTURE2D_DESC ArrayDesc;

pTextureResource->GetDesc( &ArrayDesc );


D3D11_TEXTURE2D_DESC desc;


D3DX11_IMAGE_LOAD_INFO ImageloadInfo;
ZeroMemory( &ImageloadInfo, sizeof( D3DX11_IMAGE_LOAD_INFO ) );

ImageloadInfo.Width = ArrayDesc.Width;
ImageloadInfo.Height = ArrayDesc.Height;
ImageloadInfo.Depth = D3DX11_DEFAULT;
ImageloadInfo.FirstMipLevel = 0;
ImageloadInfo.MipLevels = ArrayDesc.MipLevels;
ImageloadInfo.Usage = D3D11_USAGE_STAGING;
ImageloadInfo.BindFlags = 0;
ImageloadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_READ;
ImageloadInfo.MiscFlags = 0;
ImageloadInfo.Format = ArrayDesc.Format;


ImageloadInfo.Filter = D3DX11_FILTER_SRGB_IN | D3DX11_FILTER_NONE; //<- SRGB flag should be used for gamma correct rendering
ImageloadInfo.MipFilter = D3DX11_FILTER_NONE;

ImageloadInfo.pSrcInfo = NULL;

ID3D11Resource *pRes = NULL;

HRESULT hr = D3DX11CreateTextureFromMemory(DXUTGetD3D11Device(),pAddr,FileSize,&ImageloadInfo,NULL,&pRes,NULL);

if(FAILED(hr))
{
//Texture creation failed
return 0;
}

if( pRes )
{
ID3D11Texture2D* pTemp;
pRes->QueryInterface( __uuidof( ID3D11Texture2D ), (LPVOID*)&pTemp );
pTemp->GetDesc( &desc );

D3D11_MAPPED_SUBRESOURCE mappedTex2D;

for(UINT i=0; i < desc.MipLevels; i++)
{
HRESULT hr = DXUTGetD3D11DeviceContext()->Map(pRes, i, D3D11_MAP_READ, 0, &mappedTex2D );

if(FAILED(hr))
{
return 0;
}

//pTextureResource should point to a valid and already created 2D Texture Array

if(mappedTex2D.pData)
{
DXUTGetD3D11DeviceContext()->UpdateSubresource( pTextureResource,
D3D11CalcSubresource( i, Index, ArrayDesc.MipLevels ),
NULL,
mappedTex2D.pData,
mappedTex2D.RowPitch,
0 );

DXUTGetD3D11DeviceContext()->Unmap(pRes,i);
}
}

pTemp->Release();
pRes->Release();
}

}

[/source]

Kauna, thank for replying. I have already done it :D

Although I have created a texture 2d array, but what about the resolution? For example, if we want to create a texture array which contains 3 textures with different resolutions. If the resolution is small, then it will be magnified sometimes.

Each slice in the texture array has the same resolution. In the loading phase you may resize/scale the textures you load. However, I strongly suggest you to use same resolution for the textures you intend to load inside an array. It will give you bad quality, and it makes the loading times longer (especially if you are resizing compressed textures).

Are you sure you'll need to use a texturearray?

Cheers!

Thank you, Kauna, I am sure that I must use Texture2D array because I have tried to use Texture2D array in shader: for instance, Texture2D pTextures[3], but it have not worked :(

What are you trying to implement? Can you show a bit of your shader code?

Cheers!

Sorry, kauna, I am doing a project, so it is very sensitive. By the way, how do you destroy temporary variables when you create a texture 2D array?. Because I always meet a memory problem:


        //*/
	//---------------------------------------------------------------------------------------
	//	Load all textures into Texture2D array with D3DX11CreateTextureFromFile
	//---------------------------------------------------------------------------------------
	UINT iNumOfMaterials = g_sFileNames.size();
	
	vector<ID3D11Texture2D*> pTexture2Ds;
	pTexture2Ds.resize(iNumOfMaterials);;
	
	for (int i=0; i<iNumOfMaterials; i++)
	{
		D3DX11_IMAGE_LOAD_INFO loadInfo;

		loadInfo.Width = resolution;		
		loadInfo.Height = resolution;
		loadInfo.Depth  = 0;
		loadInfo.FirstMipLevel = 0;
		loadInfo.MipLevels = 0;
		loadInfo.Usage = D3D11_USAGE_STAGING;
		loadInfo.BindFlags = 0;
		loadInfo.CpuAccessFlags = D3D10_CPU_ACCESS_WRITE | D3D10_CPU_ACCESS_READ;
		loadInfo.MiscFlags = 0;
		loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
		loadInfo.Filter = D3DX11_FILTER_NONE;
		loadInfo.MipFilter = D3DX11_FILTER_NONE;
		loadInfo.pSrcInfo  = 0;

		hr = D3DX11CreateTextureFromFile(pd3dDevice, g_sFileNames[i].c_str(), 		
			//&loadInfo, 0, (ID3D11Resource**)&pTexture2Ds[i], 0);
			&loadInfo, 0, &pTexture2Ds[i], 0);
	}

	//---------------------------------------------------------------------------------------
	//	Create the texture array.  Each element in the texture 
	//		array has the same format/dimensions.
	//---------------------------------------------------------------------------------------
	D3D11_TEXTURE2D_DESC texElementDesc;
	pTexture2Ds[0]->GetDesc(&texElementDesc);

	D3D11_TEXTURE2D_DESC texArrayDesc;
	texArrayDesc.Width              = texElementDesc.Width;
	texArrayDesc.Height             = texElementDesc.Height;
	texArrayDesc.MipLevels          = texElementDesc.MipLevels;
	texArrayDesc.ArraySize          = iNumOfMaterials;
	texArrayDesc.Format             = DXGI_FORMAT_R8G8B8A8_UNORM;
	texArrayDesc.SampleDesc.Count   = 1;
	texArrayDesc.SampleDesc.Quality = 0;
	texArrayDesc.Usage              = D3D11_USAGE_DEFAULT;
	texArrayDesc.BindFlags          = D3D11_BIND_SHADER_RESOURCE;
	texArrayDesc.CPUAccessFlags     = 0;
	texArrayDesc.MiscFlags          = 0;

	ID3D11Texture2D* texArray = 0;
	hr = pd3dDevice->CreateTexture2D( &texArrayDesc, 0, &texArray);
	//---------------------------------------------------------------------------------------


	//---------------------------------------------------------------------------------------
	//	Copy individual texture elements into texture array.
	//---------------------------------------------------------------------------------------
	// for each texture element...
	ID3D11DeviceContext* pd3dImmediateContext;
	pd3dDevice->GetImmediateContext(&pd3dImmediateContext);
	for(UINT i = 0; i < g_sFileNames.size(); ++i)
	{
		// for each mipmap level...
		for(UINT j = 0; j < texElementDesc.MipLevels; ++j)
		{			
			D3D11_MAPPED_SUBRESOURCE MappedResource;
			hr = pd3dImmediateContext->Map( pTexture2Ds[i], j, D3D11_MAP_READ, 0, &MappedResource ) ;			

			pd3dImmediateContext->UpdateSubresource(texArray, D3D11CalcSubresource(j, i, texElementDesc.MipLevels), 0, MappedResource.pData, MappedResource.RowPitch, 0);			

			pd3dImmediateContext->Unmap(pTexture2Ds[i], j);
		}
	}	
	//---------------------------------------------------------------------------------------


	//---------------------------------------------------------------------------------------
	//					Create a resource view to the texture array.
	//---------------------------------------------------------------------------------------
	D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;
	viewDesc.Format = texArrayDesc.Format;
	viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
	viewDesc.Texture2DArray.MostDetailedMip = 0;
	viewDesc.Texture2DArray.MipLevels = texArrayDesc.MipLevels;
	viewDesc.Texture2DArray.FirstArraySlice = 0;
	viewDesc.Texture2DArray.ArraySize = iNumOfMaterials;

	hr = pd3dDevice->CreateShaderResourceView(texArray, &viewDesc, &g_pTextureArray);
	//---------------------------------------------------------------------------------------

	SAFE_RELEASE(texArray);
	for (int i=0; i<pTexture2Ds.size(); i++)
		SAFE_RELEASE(pTexture2Ds[i]); 

I release all temporary variables: texArray and all pTexture2Ds except g_pTextureArray, which is used for binding to PS shader.

Why do you release your texArray? Isn't that the pointer to your texture array texture?

Which error you receive?

Cheers!

The error I got is: "The Direct3D device has a non-zero reference count, meaning some objects were not released". I released the texArray because we only need the shader resource view.

This topic is closed to new replies.

Advertisement