DirectX 11 maping ID3D11Texture2D

Started by
16 comments, last by kolarz3 10 years, 9 months ago

I'd like to change ID3D11Texture2D to data. This is my funtion:


void getByte(ID3D11Texture2D* texture)
{
	ID3D11Texture2D* textureBuf;
	
	D3D11_TEXTURE2D_DESC textureDesc;

	ZeroMemory(&textureDesc, sizeof(textureDesc));
	textureDesc.Width = Width;
	textureDesc.Height = Height;
	textureDesc.MipLevels = 1;
	textureDesc.ArraySize = 1;
	textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
	textureDesc.SampleDesc.Count = 1;
	textureDesc.SampleDesc.Quality = 0;
	textureDesc.Usage = D3D11_USAGE_STAGING ;
	textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
	textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
	textureDesc.MiscFlags = 0;
	
	d3d11Device->CreateTexture2D(&textureDesc, NULL, &textureBuf);
	d3d11DevCon->CopyResource(textureBuf,texture);
	D3D11_MAPPED_SUBRESOURCE  mapResource;
	d3d11DevCon->Map(texture,0,D3D11_MAP_READ,NULL,&mapResource);
}

How can I now get pixel data ?

Advertisement

Your pixel data is in mapResource.pData. Typecast it into this structure (I hope I wrote correct order):


struct Color {
    float r, g, b, a;
};

Then access it like 1 dimensional array..

I do what you say but my bufor is empty (when I try save sample data to file visual throws null ptr).


	struct Color {
    float r, g, b, a;
		};
	Color* obj;
	obj=(Color *)mapResource.pData;
	std::fstream file( "color.txt", std::ios::out );
	std::string str;
	std::stringstream ss (std::stringstream::in | std::stringstream::out);
	ss << obj[0].r;
	str=ss.str();
	file.write( & str[ 0 ], str.length() );

Verify that calls to CreateTexture2D and Map functions are not giving you error. Basically assign them to HRESULT hr variable. and check that SUCCEEDED(hr) is true.

Or enable debug runtime - either pass D3D11_CREATE_DEVICE_DEBUG flag for D3D11CreateDevice function, or go to DirectX Control Panel. That will show you errors in debugger output.

Good idea, because those flags look incompatible (a staging resource can't be a render target or shader resource view).

Furthermore: The mapped memory you receive is not necessarily contiguous. You have to take the D3D11_MAPPED_SUBRESOURCE.RowPitch into account - which gives you the memory offset in byte from one row to the next.

Now I change desc but still i have problem. Maping is not succeeded.


        textureDesc.Width = Width;
	textureDesc.Height = Height;
	textureDesc.MipLevels = 1;
	textureDesc.ArraySize = 1;
	textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
	textureDesc.SampleDesc.Count = 1;
	textureDesc.SampleDesc.Quality = 0;
	textureDesc.Usage = D3D11_USAGE_STAGING;
	textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
	textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
	textureDesc.MiscFlags = 0;
	
	d3d11Device->CreateTexture2D(&textureDesc, NULL, &textureBuf);
	d3d11DevCon->CopyResource(textureBuf,texture);
	D3D11_MAPPED_SUBRESOURCE  mapResource;
	hr=d3d11DevCon->Map(texture,0,D3D11_MAP_READ,NULL,&mapResource);
  1. I don't think you can use D3D11_BIND_SHADER_RESOURCE flag either.
  2. You seem to be using DXGI_FORMAT_R32G32B32A32_FLOAT format, are you sure your source texture is same format?
  3. It seems you try to Map() wrong texture.

Now work(with copying with "obj=(Color *)mapResource.pData;"), but I don't known why I cant copy with memcpy. How can I copy this data with memcpy?


memcpy ( obj,mapResource.pData, sizeof(Color) );

sizeof(Color) is 16. You are copying only 16 bytes. You need to copy Height * RowPitch bytes.

I cant copy anything because debuger stop on memcpy, if I continue debuging visual jump into memcpy.asm file :


rep     movsd           ;N - move all of our dwords

memcpy(obj,mapResource.pData,mapResource.RowPitch);

Edit: I forget to allocate memory/

This topic is closed to new replies.

Advertisement