   Render to texture problems.
  I'm trying to render to texture using a screen quad and shaders but no matter what i try nothing works. I change one thing, i get D3DX helper errors but the application still runs although it spits out a ton of errors from Direct3D. I'm posting my code here to see if anyone knows what to do to get this to work.Cor_Direct3D9Renderer* Renderer = new Cor_Direct3D9Renderer();Screen_Quad* screen_quad;IDirect3DTexture9* g_pRenderTexture = 0;IDirect3DSurface9* g_pRenderTarget = 0;IDirect3DSurface9* g_pRenderTarget_main = 0;IDirect3DSurface9* back_buffer = 0;ID3DXEffect* FullScreen = 0;D3DXHANDLE tech;ID3DXBuffer* errors;IDirect3DDevice9* g_pDevice;ID3DXRenderToSurface* Surface;ID3DXMesh* g_pSphere;CXModel* Box = new CXModel();#define SCREEN_WIDTH 800#define SCREEN_HEIGHT 600int _tmain(int argc, _TCHAR* argv[]){HWND g_pWindow = Renderer->CreateWindowDX("Screen Quads",0,0,SCREEN_WIDTH,SCREEN_HEIGHT);if(g_pWindow){Renderer->CreateDirect3D(g_pWindow,SCREEN_WIDTH,SCREEN_HEIGHT,false,1,1,1);screen_quad = new Screen_Quad();g_pDevice = Renderer->GetDirect3DDevice9();}screen_quad->CreateQuad(g_pDevice,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,1.0);if(!Box->LoadXModel(g_pDevice,"Model//Box.x")){printf("Could not load model");}HRESULT HR = D3DXCreateEffectFromFileA(g_pDevice,"Shaders\\Screen_Effects.fx",0,0,0,0,&FullScreen,&errors);FullScreen->FindNextValidTechnique(0,&tech);if(FAILED(HR)){char shaderErrors[1024];sprintf(shaderErrors,"%d",errors->GetBufferPointer());MessageBoxA(NULL,(LPCSTR)shaderErrors,"Error",MB_OK);}g_pDevice->CreateRenderTarget(SCREEN_WIDTH,SCREEN_HEIGHT,D3DFMT_A32B32G32R32F,D3DMULTISAMPLE_NONE,1,false,&g_pRenderTarget,NULL);g_pDevice->CreateTexture(SCREEN_WIDTH,SCREEN_HEIGHT,10,D3DUSAGE_RENDERTARGET,D3DFMT_A32B32G32R32F,D3DPOOL_DEFAULT,&g_pRenderTexture,NULL);HR = D3DXCreateSphere(g_pDevice,50,20,20,&g_pSphere,NULL);HR = D3DXCreateRenderToSurface(g_pDevice,SCREEN_WIDTH,SCREEN_HEIGHT,D3DFMT_A32B32G32R32F,false,D3DFMT_D24S8,&Surface);if(FAILED(HR)){printf("Could not create Render To Surface");}if(FAILED(HR)){printf("Could not create Sphere");}while(Renderer->MessagePump(g_pWindow)){D3DVIEWPORT9 View;g_pDevice->GetViewport(&View);HR = Surface->BeginScene(g_pRenderTarget,&View);g_pDevice->SetRenderTarget(0,g_pRenderTarget);g_pDevice->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0,1);if(FAILED(HR)){printf("Could not begin rendering to surface");}D3DXMATRIX pView;D3DXMATRIX pProj;D3DXMATRIX pWorld;D3DXMATRIX Result;D3DXMatrixLookAtLH(&pView,&D3DXVECTOR3(40,10,20),&D3DXVECTOR3(10,50,-100),&D3DXVECTOR3(0,1,0));D3DXMatrixPerspectiveFovLH(&pProj,45.0,1.3333,1.0, 10000.0);D3DXMatrixTranslation(&pWorld,0,0,0);Result = pView * pProj * pWorld;HR = FullScreen->SetMatrix("matViewProjection",&Result);if(FAILED(HR)){printf("Could not set vertex Shader Matrix");}FullScreen->Begin(0,0);FullScreen->BeginPass(0);FullScreen->CommitChanges();if(!Box->DrawXModel(g_pDevice)){printf("Could not draw model");}FullScreen->EndPass();FullScreen->End();HR = Surface->EndScene(0);if(FAILED(HR)){printf("Could not end screen rendering");}g_pDevice->BeginScene();g_pDevice->SetRenderTarget(0,g_pRenderTarget_main);g_pDevice->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0,1);g_pRenderTexture->GetSurfaceLevel(1,&g_pRenderTarget);g_pDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&back_buffer);FullScreen->SetTexture("renderTexture_Tex",g_pRenderTexture);UINT Passes = 1;FullScreen->Begin(&Passes,0);FullScreen->BeginPass(1);FullScreen->CommitChanges();//g_pDevice->SetTexture(0,g_pRenderTexture);screen_quad->DrawQuad(g_pDevice);FullScreen->EndPass();FullScreen->End();g_pDevice->EndScene();g_pDevice->Present(NULL,NULL,NULL,NULL);}return 0;}and here is what Direct3D tells me when the application runs.D3D9 Helper: IDirect3DDevice9::SetRenderTarget failed: D3DERR_INVALIDCALLDirect3D9: (WARN) :Can not render to a render target that is also used as a texture. A render target was detected as bound, but couldn't detect if texture was actually used in rendering.Direct3D9: (ERROR) :Need to call BeginScene before rendering.First-chance exception at 0x75f3f328 in FullScreen.exe: Microsoft C++ exception: long at memory location 0x0072efdc..Direct3D9: (ERROR) :DrawIndexedPrimitive failed.D3D9 Helper: IDirect3DDevice9::DrawIndexedPrimitive failed: D3DERR_INVALIDCALLDirect3D9: (ERROR) :Illegal to set render target 0 to NULLD3D9 Helper: IDirect3DDevice9::SetRenderTarget failed: D3DERR_INVALIDCALLDirect3D9: (WARN) :Can not render to a render target that is also used as a texture. A render target was detected as bound, but couldn't detect if texture was actually used in rendering.Direct3D9: (ERROR) :Need to call BeginScene before rendering.and as you can see from my code i am calling BeginScene on the surface and on the back buffer. Hope someone knows how to get this to work as i want to be able to use shaders for post effects.   
  You need to call g_pDevice->BeginScene() before you start to render to you surface. The BeginScene on the surface just prepare the surface to be used as target but it doesn’t prepare the device.   ;  Ok, i changed the program around a little here it is:Cor_Direct3D9Renderer* Renderer = new Cor_Direct3D9Renderer();Screen_Quad* screen_quad;IDirect3DTexture9* g_pRenderTexture = 0;IDirect3DSurface9* g_pRenderTarget = 0;IDirect3DSurface9* g_pRenderTarget_main = 0;IDirect3DSurface9* back_buffer = 0;IDirect3DTexture9* g_pTexture = 0;ID3DXEffect* FullScreen = 0;D3DXHANDLE tech;ID3DXBuffer* errors;IDirect3DDevice9* g_pDevice;ID3DXRenderToSurface* Surface;ID3DXMesh* g_pSphere;CXModel* Box = new CXModel();#define SCREEN_WIDTH 800#define SCREEN_HEIGHT 600int _tmain(int argc, _TCHAR* argv[]){HWND g_pWindow = Renderer->CreateWindowDX("Screen Quads",0,0,SCREEN_WIDTH,SCREEN_HEIGHT);if(g_pWindow){Renderer->CreateDirect3D(g_pWindow,SCREEN_WIDTH,SCREEN_HEIGHT,false,1,1,1);screen_quad = new Screen_Quad();g_pDevice = Renderer->GetDirect3DDevice9();}screen_quad->CreateQuad(g_pDevice,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,1.0);if(!Box->LoadXModel(g_pDevice,"Model//Box.x")){printf("Could not load model");}HRESULT HR = D3DXCreateEffectFromFileA(g_pDevice,"Shaders\\Screen_Effects.fx",0,0,0,0,&FullScreen,&errors);FullScreen->FindNextValidTechnique(0,&tech);if(FAILED(HR)){char shaderErrors[1024];sprintf(shaderErrors,"%d",errors->GetBufferPointer());MessageBoxA(NULL,(LPCSTR)shaderErrors,"Error",MB_OK);}g_pDevice->CreateRenderTarget(SCREEN_WIDTH,SCREEN_HEIGHT,D3DFMT_A32B32G32R32F,D3DMULTISAMPLE_NONE,1,false,&g_pRenderTarget,NULL);g_pDevice->CreateRenderTarget(SCREEN_WIDTH,SCREEN_HEIGHT,D3DFMT_A32B32G32R32F,D3DMULTISAMPLE_NONE,1,false,&g_pRenderTarget_main,NULL);g_pDevice->CreateTexture(SCREEN_WIDTH,SCREEN_HEIGHT,10,D3DUSAGE_RENDERTARGET,D3DFMT_A32B32G32R32F,D3DPOOL_DEFAULT,&g_pRenderTexture,NULL);HR = D3DXCreateSphere(g_pDevice,50,20,20,&g_pSphere,NULL);HR = D3DXCreateRenderToSurface(g_pDevice,SCREEN_WIDTH,SCREEN_HEIGHT,D3DFMT_A32B32G32R32F,true,D3DFMT_D24S8,&Surface);if(FAILED(HR)){printf("Could not create Render To Surface");}if(FAILED(HR)){printf("Could not create Sphere");}while(Renderer->MessagePump(g_pWindow)){HR = g_pDevice->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,255,0),1.0,1);if(FAILED(HR)){printf("Could not clear render target");}HR = g_pDevice->SetRenderTarget(1,g_pRenderTarget);if(FAILED(HR)){printf("Could not set render target");} HR = g_pDevice->BeginScene();;if(FAILED(HR)){printf("Could not begin rendering to surface");}HR = g_pDevice->EndScene();if(FAILED(HR)){printf("Could not end screen rendering");}g_pDevice->BeginScene();D3DXMATRIX pView;D3DXMATRIX pProj;D3DXMATRIX pWorld;D3DXMATRIX Result;D3DXMatrixLookAtLH(&pView,&D3DXVECTOR3(40,10,20),&D3DXVECTOR3(0,0,-200),&D3DXVECTOR3(0,1,0));D3DXMatrixPerspectiveFovLH(&pProj,45.0,1.3333,1.0, 10000.0);D3DXMatrixTranslation(&pWorld,0,0,0);Result = pView * pProj * pWorld;HR = FullScreen->SetMatrix("matViewProjection",&Result);if(FAILED(HR)){printf("Could not set vertex Shader Matrix");}HR = FullScreen->SetTexture("Coin_Tex",g_pTexture);if(FAILED(HR)){printf("Could not set texture");}//FullScreen->SetTexture("renderTexture_Tex",g_pRenderTexture);UINT Pass = 1;FullScreen->Begin(&Pass,0);FullScreen->BeginPass(0);FullScreen->CommitChanges();if(!Box->DrawXModel(g_pDevice)){printf("Could not draw model");}FullScreen->EndPass();FullScreen->End();HR = g_pDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&back_buffer);if(FAILED(HR)){printf("Could not get back buffer");}HR = g_pRenderTexture->GetSurfaceLevel(0,&g_pRenderTarget);if(FAILED(HR)){printf("Could not get surface level 0");}HR = g_pDevice->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_STENCIL,D3DCOLOR_XRGB(255,0,0),1.0,1);if(FAILED(HR)){printf("Could not clear target");}HR = g_pDevice->SetRenderTarget(2,g_pRenderTarget_main);if(FAILED(HR)){printf("Could not set render target");}HR = FullScreen->SetTexture("renderTexture_Tex",g_pRenderTexture);if(FAILED(HR)){printf("Could not set render texture");}UINT Passes = 0;FullScreen->Begin(&Passes,0);FullScreen->BeginPass(1);FullScreen->CommitChanges();//g_pDevice->SetTexture(0,g_pRenderTexture);screen_quad->DrawQuad(g_pDevice);FullScreen->EndPass();FullScreen->End();g_pDevice->EndScene();g_pDevice->Present(NULL,NULL,NULL,NULL);}g_pDevice->Release();g_pRenderTarget->Release();g_pRenderTarget_main->Release();g_pRenderTexture->Release();g_pSphere->Release();g_pTexture->Release();return 0;}as you can see, i'm clearing the first render target to one color (which doesn't seem to be working as i get a screen full of random colors) and i don't see anything. what am i doing wrong now?   