Jump to content
  • Advertisement
Sign in to follow this  
akira32

How to get the pixel color in surface (RenderTarget)

This topic is 3870 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 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();


Share this post


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

Share this post


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

Share this post


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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!