DirectX 11 maping ID3D11Texture2D

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

I suspect he is failing on the 2D Texture creation. Try setting the BindFlags to 0. I have just spent several hours trying to create a 2D Texture instance that the CPU can read from with no success until i set the BindFlags to 0.

Advertisement

I dont known why i cant move pointer of my structer. Any solution ?


	struct Color {
    float r, g, b, a;
		};
	Color* obj;
	obj=new Color[mapResource.RowPitch*Height];
	BYTE* mappedData = reinterpret_cast<BYTE*>(mapResource.pData);
	for(UINT i = 0; i < Height; ++i)
	{
	memcpy(obj, mappedData, mapResource.RowPitch);
	mappedData += mapResource.RowPitch;
	obj += mapResource.RowPitch;//without this line program work
	}

Pointer arithmetic. obj does not not increase by RowPitch but by RowPitch*sizeof(Color). Use a BYTE pointer for the loop.

Edit: Similarly, your allocated array is 16 times too big.

But since you're copying everything anyway you could as well use only one memcpy.

Now everything work.I found also thet all rows of pixel have extra 4 pixel data(I dont known what they mean) . If anybody later searching for solution I put code .


void Pick(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.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
	textureDesc.MiscFlags = 0;
	d3d11Device->CreateTexture2D(&textureDesc, NULL, &textureBuf);

	d3d11DevCon->CopyResource(textureBuf,texture);
	D3D11_MAPPED_SUBRESOURCE  mapResource;
	hr=d3d11DevCon->Map(textureBuf,0,D3D11_MAP_READ,NULL,&mapResource);
	
	struct Color {float r, g, b, a;};
	Color* obj;
	obj=new Color[(mapResource.RowPitch/sizeof(Color))*Height];
	memcpy(obj, mapResource.pData,mapResource.RowPitch*Height);

	if(mousePos.x>0&&mousePos.x<Width&&mousePos.y>0&&mousePos.y<Height)
	if(obj[(mousePos.y*Width)+(4*mousePos.y)+mousePos.x].r==1.0/*If object that we was pick mouse have 1.0 on red bit we draw little cloud*/)model.rysujOtoczk?(model.dolny,model.gorny);
	
	d3d11DevCon->Unmap(textureBuf,0);
	textureBuf->Release();
	delete [] obj;
}

You don't need to copy all memory just to access one element of mapped memory.


hr=d3d11DevCon->Map(textureBuf,0,D3D11_MAP_READ,NULL,&mapResource);
if (FAILED(hr)) abort();
    
struct Color {float r, g, b, a;};
Color* obj = (Color*)mapResource.pData;
 
if(mousePos.x>0&&mousePos.x<Width&&mousePos.y>0&&mousePos.y<Height)
  if(obj[(mousePos.y*(mapResource.RowPitch)/sizeof(Color))+(4*mousePos.y)+mousePos.x].r==1.0/*If object that we was pick mouse have 1.0 on red bit we draw little cloud*/)
    model.rysujOtoczk?(model.dolny,model.gorny);
    
d3d11DevCon->Unmap(textureBuf,0);
textureBuf->Release();

But in this case we must to cast data from D3D11_MAPPED_SUBRESOURCE to BYTE.

What?

Only thing you need to cast is pData member of D3D11_MAPPED_SUBRESOURCE. It has void* type, but you need Color*

Anyway "Color* obj = (Color*)mapResource.pData;" do the same that "memcpy(obj, mapResource.pData,mapResource.RowPitch*Height);"

This topic is closed to new replies.

Advertisement