• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
pnt1614

DX11
How to create a Texture2DArray from files in DX11?

13 posts in this topic

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.

0

Share this post


Link to post
Share on other sites
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] Edited by kauna
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

 

Cheers!

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

 

Which error you receive?

 

Cheers!

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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?

0

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  
Followers 0

  • Similar Content

    • By YixunLiu
      Hi,
      I have a surface mesh and I want to use a cone to cut a hole on the surface mesh.
      Anybody know a fast method to calculate the intersected boundary of these two geometries?
       
      Thanks.
       
      YL
       
    • By hiya83
      Hi, I tried searching for this but either I failed or couldn't find anything. I know there's D11/D12 interop and there are extensions for GL/D11 (though not very efficient). I was wondering if there's any Vulkan/D11 or Vulkan/D12 interop?
      Thanks!
    • By lonewolff
      Hi Guys,
      I am just wondering if it is possible to acquire the address of the backbuffer if an API (based on DX11) only exposes the 'device' and 'context' pointers?
      Any advice would be greatly appreciated
    • By MarcusAseth
      bool InitDirect3D::Init() { if (!D3DApp::Init()) { return false; } //Additional Initialization //Disable Alt+Enter Fullscreen Toggle shortkey IDXGIFactory* factory; CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&factory)); factory->MakeWindowAssociation(mhWindow, DXGI_MWA_NO_WINDOW_CHANGES); factory->Release(); return true; }  
      As stated on the title and displayed on the code above, regardless of it Alt+Enter still takes effect...
      I recall something from the book during the swapChain creation, where in order to create it one has to use the same factory used to create the ID3D11Device, therefore I tested and indeed using that same factory indeed it work.
      How is that one particular factory related to my window and how come the MakeWindowAssociation won't take effect with a newly created factory?
      Also what's even the point of being able to create this Factories if they won't work,?(except from that one associated with the ID3D11Device) 
    • By ProfL
      Can anyone recommend a wrapper for Direct3D 11 that is similarly simple to use as SFML? I don't need all the image formats etc. BUT I want a simple way to open a window, allocate a texture, buffer, shader.
  • Popular Now