Jump to content
  • Advertisement
Sign in to follow this  
kolarz3

DirectX 11 maping ID3D11Texture2D

This topic is 2046 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'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 ?

Share this post


Link to post
Share on other sites
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..

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites

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);
Edited by kolarz3

Share this post


Link to post
Share on other sites
  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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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/

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!