Jump to content
  • Advertisement
Sign in to follow this  
Matt_Aufderheide

DX11 Trouble reading data from texture

This topic is 820 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

HI, I am trying to read all the pixel data (just the red channel) from a texture into a 2d array. The code I am using compiles and runs without error but seems to always get zero values. Not sure I am understanding how this is supposed to really work.. fairly new to dx11...here is my complete code for this function:

void load_heightmap(ID3D11Device *d3d11Device, ID3D11DeviceContext *d3d11DevCon)
{
	UINT heightmapsize = 4096;

	D3DX11_IMAGE_LOAD_INFO loadInfo;
	loadInfo.Width = D3DX11_DEFAULT;
	loadInfo.Height = D3DX11_DEFAULT;
	loadInfo.Depth = D3DX11_DEFAULT;
	loadInfo.FirstMipLevel = D3DX11_DEFAULT;
	loadInfo.MipLevels = 0;
	loadInfo.Usage = D3D11_USAGE_STAGING;
	loadInfo.BindFlags = 0;
	loadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_READ;
	loadInfo.MiscFlags = 0;
	loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UINT;
	loadInfo.Filter = D3DX11_FILTER_NONE;
	loadInfo.MipFilter = D3DX11_DEFAULT;
	loadInfo.pSrcInfo = NULL;

	ID3D11Resource* res = nullptr;

	D3DX11CreateTextureFromFile(
		d3d11Device,
		L"world.dds",
		&loadInfo,
		NULL,
		&res,
		NULL
		);

	if (!res)
	{
		MessageBox(NULL, L"Error loading heightmap",
		L"Error", MB_OK | MB_ICONERROR);
		return;	
	}

	D3D11_MAPPED_SUBRESOURCE mappedResource;
	d3d11DevCon->Map(res, 0, D3D11_MAP_READ, NULL, &mappedResource);

	//Set the pixels
	UCHAR* pTexels = (UCHAR*)mappedResource.pData;
	for (UINT row = 0; row < heightmapsize; row++)
	{
		//Row number * height
		UINT rowStart = row * mappedResource.RowPitch;
		for (UINT col = 0; col < heightmapsize; col++)
		{
			//width * number of channels (r,g,b,a)
			UINT colStart = col * 4;
			heightmap[row][col]=(UINT)pTexels[rowStart + colStart + 0];//read red channel
		}
	}

	d3d11DevCon->Unmap(res, 0);
	res->Release();
}

Share this post


Link to post
Share on other sites
Advertisement

OK, I solved this by changing this line :

loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UINT

 

to this:

loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UNORM

 

Now it all works fine...I don't really understand why though

 

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!