Sign in to follow this  

The problem with reading from texture

Recommended Posts

Hi, I'm trying to get work shadow mapping - exactly rendering light map with shadows and come across following problem: I have D3DFMT_R32F texture for rendering depths into it. I render depth into R32F texture as render target using shader and then set it as texture to read from it in next pass. But suddenly the value is not here and what I get from every read is 0. And it should be something around one or precisely one. My code is: The body from pixel shader writing value to texture:
	OUTPUT Output = (OUTPUT) 0;
	Output.Color = Input.Color.x; // writing to red channel of texture
	return Output;
The body from pixel shader reading value from texture:
sampler TexSamp2: register(s2);

	float4 Light: COLOR0; // Color
	float3 TexCoord: TEXCOORD0; // first two are tex coords and third is compare depth value

float4 LightMapPS(LIGHTMAP_PS_INPUT Input): COLOR0
	float4 TexCol;
	float Shadow;
	// Get shadow map depth to x
	TexCol = tex2D(TexSamp2, Input.TexCoord.xy);
	// compare
	Shadow = (TexCol.r >= Input.TexCoord.z);
	return Input.Light * Shadow;
My code is:
// Render shadow map texture
			ShadowMap->GetDynShadowTex()->GetSurfaceLevel(0, &MapSurface);

			LightMapView.X = 0;
			LightMapView.Y = 0;
			LightMapView.Width = ShadowMap->GetLightMapSize();
			LightMapView.Height = ShadowMap->GetLightMapSize();
			LightMapView.MinZ = 0;
			LightMapView.MaxZ = 1;

			// Set render target and clear it
			D3DDevice->SetRenderTarget(0, MapSurface);
			D3DDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(255,255, 255, 255), 1, 0);


			// apply bias to view space
			Shader->SetViewMatrix(LightViewMat * ShadowMap->GetBiasMat() );

			// FIRST PASS - render z-depth map
				// Render plate and light stand


			// Render shadow map texture
			LightMap->GetSurfaceLevel(0, &MapSurface);
			D3DDevice->SetRenderTarget(0, MapSurface);
			D3DDevice->Clear(0,0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0,0,0, 255), 1, 0);

			// Set depth texture
			D3DDevice->SetTexture(2, ShadowMap->GetDynShadowTex() );
			//D3DDevice->SetTransform(D3DTS_VIEW, &LightViewMat);
			//D3DDevice->SetTransform(D3DTS_PROJECTION, &LightProjMat);

			ShadowMat = LightProjMat * ShadowMap->GetCorrectMat();
			Shader->SetVSMatrix(ShadowMatHandle,  ShadowMat);

			// Set spot light
			// SECOND PASS - render light map with shadows
				// Render plate

I checked, that correct value is written to texture. I checked, that correct coords are given to texture read. Next strange thing is, when I render using REF device, I get completely black light map, but when I render with HAL I get circle of light in the center with some z-fight problem, which is not changing when I add or substract any value of bias as it is typical in shadow mapping. Does anybody know, what it can be caused by? I'm out of ideas. I'll provide any other info you want to know, just ask. I'm running it on ATI Radeon 9700 Pro. DirectX 9.0c - October 2004 update. Thanks everyone willing me to help. Pavel Celba [Edited by - Coder on November 14, 2004 10:30:56 PM]

Share this post

Link to post
Share on other sites
The problem actually was, that I rendered only to part of depth texture (shadow mapping first pass) and in second pass I forgot to set correction matrix to read texcoords only from that part.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this