Jump to content
  • Advertisement
Sign in to follow this  

Render to Texture and MRT

This topic is 4429 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've checked other threads related to this topic, yet none of them seemed to solve my problem. Basically I am trying to do a two pass rendering, the first rendering to textures, the second to the original surface. The first pass where I render to texture works fine; however, when I try to use the textures and render them to the original surface, all I get is red. My setup code is as follows (I snipped the parts that I know work):
HRESULT initSurfaces() {

	//create render surfaces
	if(FAILED(device->CreateTexture(screenWidth,screenHeight,1,D3DUSAGE_RENDERTARGET,
		D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&colorMap,NULL))) {
		::MessageBox(0, "CreateColorMapTexture() - FAILED", 0, 0);
		return E_FAIL;
	}
	if(FAILED(device->CreateTexture(screenWidth,screenHeight,1,D3DUSAGE_RENDERTARGET,
		D3DFMT_G16R16,D3DPOOL_DEFAULT,&depthBlurMap,NULL))) {
		::MessageBox(0, "CreateDepthBlurMapTexture() - FAILED", 0, 0);
		return E_FAIL;
	}
	
        //vertices for the screen aligned quad
	vertices[0] = Vertex(0.0,0.0,1.0,1.0,0.0,0.0); //x,y,z,w,u,v
	vertices[1] = Vertex(screenWidth,0.0,1.0,1.0,1.0,0.0);
	vertices[2] = Vertex(screenWidth,screenHeight,1.0,1.0,1.0,1.0);
	vertices[3] = Vertex(0.0,screenHeight,1.0,1.0,0.0,1.0);

	
	if(FAILED(device->GetRenderTarget(0,&originalSurf))) {
		::MessageBox(0, "GetRenderTarget() - FAILED", 0, 0);
		return E_FAIL;
	}
	
	if(FAILED(colorMap->GetSurfaceLevel(0,&colorMapSurf))) {
		::MessageBox(0, "GetColorMapSurface() - FAILED", 0, 0);
		return E_FAIL;
	}
	if(FAILED(depthBlurMap->GetSurfaceLevel(0,&depthBlurMapSurf))) {
		::MessageBox(0, "GetDepthBlurMapSurface() - FAILED", 0, 0);
		return E_FAIL;
	}

	return S_OK;
}



My render code is this:
void render() {
	ID3DXMesh* mesh = 0;
	D3DXMATRIX worldViewProj, worldView;
	
	D3DXCreateTeapot(device, &mesh, 0);

	device->SetRenderTarget(0,colorMapSurf);
	device->SetRenderTarget(1,depthBlurMapSurf);

	device->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0,0);

	if (SUCCEEDED(device->BeginScene())) {
		initMatrices();
		
		worldViewProj = matrices[0] * viewProj;
		sceneRenderVSConstTable->SetMatrix(device,"worldViewProj", &worldViewProj);
		
		worldView = matrices[0] * view;
		sceneRenderVSConstTable->SetMatrix(device,"worldView", &worldView);

		device->SetVertexShader(sceneRenderVS);
		device->SetPixelShader(sceneRenderPS);
		
		mesh->DrawSubset(0);
		
		device->EndScene();
		D3DXSaveTextureToFile("colormap.jpg",D3DXIFF_JPG,colorMap,NULL);
		D3DXSaveTextureToFile("depthblurmap.jpg",D3DXIFF_JPG, depthBlurMap,NULL);

		device->SetRenderTarget(0,originalSurf);
		device->SetRenderTarget(1,NULL);

		device->Clear(NULL,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(100,0,0),1,0);
		
		if(SUCCEEDED(device->BeginScene())) {
			device->SetTexture(0,colorMap);
			device->SetSamplerState(0,D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
			device->SetSamplerState(0,D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
			device->SetSamplerState(0,D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

			device->SetTexture(1,depthBlurMap);
			device->SetSamplerState(1,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
			device->SetSamplerState(1,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
			device->SetSamplerState(1,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);

			device->SetVertexShader(postProcessVS);
			device->SetPixelShader(NULL);
			
			device->SetFVF(VertexFVF);
			device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertices, sizeof(Vertex));

			device->EndScene();
		}
	}

	device->Present(NULL,NULL,NULL,NULL);

	SAFE_RELEASE(mesh);

}



I know the shaders do what they're supposed to be doing. The colormap and depthblurmap are both rendered correctly (I wrote them out to file to check). For now, the post process vertex shader simply returns white as the color, yet when I render the fullscreen quad I get red. Any ideas? [Edited by - ChopperDave on April 5, 2006 7:18:27 PM]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!