How to get the pixel color in surface (RenderTarget)

Started by
2 comments, last by MJP 16 years, 1 month ago
I create a rendertarget texture and draw some object in its surface. Now I want to get each pixel color in the surface. But LockRect function always returns -2005530516 (D3DERR_INVALIDCALL). How do I get the pixel color in surface (rendertarget texture)?

	// setup shadow map objects
	V_RETURN(m_pd3dDevice->CreateTexture(
		SHADOW_RESULT_MAP_SIZE_WIDTH,
		SHADOW_RESULT_MAP_SIZE_HEIGHT,
		1, 
		D3DUSAGE_RENDERTARGET,//<====render target
		SHADOW_MAP_FORMAT, 
		D3DPOOL_DEFAULT,
		&m_pShadowMapTex,
		NULL
		));

	V_RETURN(m_pShadowMapTex->GetSurfaceLevel(0, &m_pShadowMapSurf));

	V_RETURN(m_pd3dDevice->CreateDepthStencilSurface(
		SHADOW_RESULT_MAP_SIZE_WIDTH,
		SHADOW_RESULT_MAP_SIZE_HEIGHT, 
		D3DFMT_D24S8,
		D3DMULTISAMPLE_NONE, 
		0,
		TRUE,
		&m_pShadowMapZ,
		NULL
		));

//lock surface and get color

	D3DLOCKED_RECT lrect;

	D3DSURFACE_DESC Desc;
	m_pShadowMapSurf->GetDesc(&Desc);
	hr=m_pShadowMapSurf->LockRect(&lrect,NULL,D3DLOCK_DONOTWAIT|D3DLOCK_READONLY );//<==hr=-2005530516 D3DERR_INVALIDCALL

	for (int y=0;y<Desc.Height;y++)
	{
		for (int x=0;Desc.Width;x++)
		{

			float dwColor = ((float*)lrect.pBits)[y*(lrect.Pitch/sizeof(float))+x];//Y*Width+X

			int r=(int)(dwColor*255.0f);

			if (dwColor<0.3f)
			{
				nPixel++;
			}
		}
	}
	m_pShadowMapSurf->UnlockRect();


akira32 編程之家 Yahoohttp://tw.myblog.yahoo.com/akira32-akira32
Advertisement
You can't lock textures created in D3DPOOL_DEFAULT, you have to copy the data to a texture in system memory. See the remarks section of the documentation for IDirect3DTexture9::LockRect for more details.
Quote:Original post by MJP
You can't lock textures created in D3DPOOL_DEFAULT, you have to copy the data to a texture in system memory. See the remarks section of the documentation for IDirect3DTexture9::LockRect for more details.


Thank you,MJP. Are you the MJP in XNA Create Club?

	int nPixel=0;	D3DLOCKED_RECT lrect;	D3DSURFACE_DESC Desc;	m_pShadowMapSurf->GetDesc(&Desc);	LPDIRECT3DSURFACE9 pDestSurface;	m_pd3dDevice->CreateOffscreenPlainSurface(Desc.Width,Desc.Height,Desc.Format,D3DPOOL_SYSTEMMEM,&pDestSurface,NULL);//Must be D3DPOOL_SYSTEMMEM for MSDN	m_pd3dDevice->GetRenderTargetData(m_pShadowMapSurf,pDestSurface);	hr=pDestSurface->LockRect(&lrect,NULL,D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_READONLY );	for (int y=0;y<Desc.Height;y++)	{		for (int x=0;x<Desc.Width;x++)		{			float dwColor = ((float*)lrect.pBits)[y*(lrect.Pitch/sizeof(float))+x];//Y*Width+X			int r=(int)(dwColor*255.0f);			if (dwColor<0.3f)			{				nPixel++;			}		}	}	pDestSurface->UnlockRect();	SAFE_RELEASE(pDestSurface);


[Edited by - akira32 on March 20, 2008 8:29:05 AM]
akira32 編程之家 Yahoohttp://tw.myblog.yahoo.com/akira32-akira32
Quote:Original post by akira32

Thank you,MVP. Are you the MVP in XNA Create Club?



No sir, I am not. I haven't actually done any work with XNA yet (my past few projects have used straight C++ and Direct3D9), but I've been learning C# in preparation for doing my next project in XNA through the Creators Club.

This topic is closed to new replies.

Advertisement