Trouble reading data from texture

Started by
2 comments, last by Matt_Aufderheide 8 years ago

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

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

If your texture data was all normalized floats (between 0 and 1), the converting them to UINT would result in 0s.

well it was just a 32 bit rgba texture saved as dds...maybe that is stored as normalized floats internally..?

This topic is closed to new replies.

Advertisement