Jump to content
  • Advertisement
Sign in to follow this  
pnt1614

DX11 Texture2DArray and mipmapping

This topic is 1196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to do mipmapping in the pixel shader with a Texture2DArray, but there is something wrong. I have also made a test that loading the same image, then used it to make  a Texture2DArray, and another Texture2D.

 

C++ code to create Texture2DArray, and the Texture2D is a shader resource view.

        D3DX11_IMAGE_LOAD_INFO loadInfo;
	loadInfo.Width = mMaxResolution.m_iWidth;
	loadInfo.Height = mMaxResolution.m_iHeight;
	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;


	UINT iNumOfMaterials = (UINT)m_sTextureNames.size();	
	vector<ID3D11Texture2D*> pTexture2Ds;
	pTexture2Ds.resize(iNumOfMaterials);

	for (UINT i = 0; i<iNumOfMaterials; i++)
	{		
		if (m_sTextureNames[i].compare("") != 0)
		{
			HR(D3DX11CreateTextureFromFileA(md3dDevice, m_sTextureNames[i].c_str(),
				&loadInfo, 0, (ID3D11Resource**)&pTexture2Ds[i], 0));
		}
	}

	hr = D3DX11CreateShaderResourceViewFromFileA(md3dDevice, m_sTextureNames[0].c_str(),
		NULL, NULL, &pTestSRV, &hr);

	//---------------------------------------------------------------------------------------
	//					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 = NULL;
	hr = md3dDevice->CreateTexture2D(&texArrayDesc, 0, &texArray);
	if (hr == E_FAIL) {
		for (int i = 0; i<pTexture2Ds.size(); i++)
			SAFE_RELEASE(pTexture2Ds[i]);

		printf("Error as creating a texture 2d array\n");
		return false;
	}

	SetDebugName(texArray, "Texture2D used for an Texture2D Array");
	//---------------------------------------------------------------------------------------


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

				UINT SubResource = D3D11CalcSubresource(j, i, MipLevel);
				pd3dImmediateContext->UpdateSubresource(texArray, SubResource, 0,
					MappedResource.pData, MappedResource.RowPitch, 0);
				pd3dImmediateContext->Unmap(pTexture2Ds[i], j);
			}
		}
	SAFE_RELEASE(pd3dImmediateContext);
	//---------------------------------------------------------------------------------------


	//---------------------------------------------------------------------------------------
	//					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 = (UINT)pTexture2Ds.size();

	hr = md3dDevice->CreateShaderResourceView(texArray, &viewDesc, &m_pTextureArray);
	SetDebugName(texArray, "SRV TextureArray");
	if (hr == E_FAIL)
	{
		for (int i = 0; i<pTexture2Ds.size(); i++)
			SAFE_RELEASE(pTexture2Ds[i]);

		SAFE_RELEASE(texArray);

		return false;
	}
	//---------------------------------------------------------------------------------------


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

	SAFE_RELEASE(texArray);

In the pixel shader, I have tested with LOD is 0.5

        Texture2D g_Texture;
        //Texture2DArray g_Texture;

        float2 dx = ddx(In.Tex);
	float2 dy = ddy(In.Tex);
	float d = max(dot(dx, dx), dot(dy, dy));
	return g_Texture.SampleLevel(samLinear, In.Tex, 0.5);

        return g_Texture.SampleLevel(samLinear, float3(In.Tex, TextureID), 0.5);

And this is the result of mipmapping using a Texture2D

texture2_D.jpg

 

And using a Texture2DArray

texture2_DArray.jpg

 

What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement

I fixed it, I just change the MipFilter into D3D11_FILTER_LINEAR when loading image information. But it seems that mipmapping has a problem as bleeding.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!