Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to create a Texture2DArray from files in DX11?


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
13 replies to this topic

#1 steven166   Members   -  Reputation: 234

Like
0Likes
Like

Posted 06 January 2013 - 10:39 PM

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.



Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2167

Like
1Likes
Like

Posted 07 January 2013 - 02:36 AM

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!



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();

}


}



Edited by kauna, 07 January 2013 - 04:24 AM.


#3 steven166   Members   -  Reputation: 234

Like
0Likes
Like

Posted 07 January 2013 - 06:16 AM

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



#4 steven166   Members   -  Reputation: 234

Like
0Likes
Like

Posted 10 January 2013 - 05:00 AM

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.



#5 kauna   Crossbones+   -  Reputation: 2167

Like
0Likes
Like

Posted 10 January 2013 - 06:30 AM

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!



#6 steven166   Members   -  Reputation: 234

Like
0Likes
Like

Posted 10 January 2013 - 07:46 PM

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 :(


Edited by steven166, 10 January 2013 - 07:57 PM.


#7 kauna   Crossbones+   -  Reputation: 2167

Like
0Likes
Like

Posted 11 January 2013 - 02:29 AM

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

 

Cheers!



#8 steven166   Members   -  Reputation: 234

Like
0Likes
Like

Posted 11 January 2013 - 02:43 AM

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.



#9 kauna   Crossbones+   -  Reputation: 2167

Like
0Likes
Like

Posted 13 January 2013 - 08:40 AM

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

 

Which error you receive?

 

Cheers!



#10 steven166   Members   -  Reputation: 234

Like
0Likes
Like

Posted 14 January 2013 - 01:53 AM

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.



#11 kauna   Crossbones+   -  Reputation: 2167

Like
0Likes
Like

Posted 14 January 2013 - 04:09 AM

I'm a bit suspicious about "I released the texArray because we only need the shader resource view.". As far as I understand, you'll need the texture object, even if you don't use it for other things than just creating SRVs. 

 

SRV is just an object for the shader to interpret the texture contents. You may even have several SRVs per texture and they both point to the same data.

 

Actually, it could be that the texture isn't released because creating a SRV may increase the texture's reference count and for that reason the texture isn't released before you release the SRV. Perhaps someone can confirm this. 

 

Do you release your SRV when the program finishes?

 

Cheers!



#12 steven166   Members   -  Reputation: 234

Like
0Likes
Like

Posted 14 January 2013 - 07:59 PM

thank kauna, let me check it.



#13 MJP   Moderators   -  Reputation: 10272

Like
0Likes
Like

Posted 14 January 2013 - 08:41 PM

It's safe to release a texture if you only need the SRV. The SRV will increment the internal refcount of the texture resource, so it will stay alive until you release the SRV. Same goes for RTV's and UAV's.



#14 steven166   Members   -  Reputation: 234

Like
0Likes
Like

Posted 15 January 2013 - 12:30 AM

Thank for helping me, guys. But the error is still there, although I release the SRV at the end of program. Is there any way to check which one increases the internal refcount?






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