Jump to content
  • Advertisement
Sign in to follow this  
bjogio

Render to texture [solved]

This topic is 4854 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'm experimenting render-to-texture under dx9 (2005 update). In my initialization routine (I work in windowed mode) I've written something like:
//D3DPresentParam.BackBufferFormat = D3DFMT_UNKNOWN;
Direct3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, CurrentWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &D3DPresentParam, &Direct3DDevice)))
//create vertex buffer, declaration, shader (1.1) etc. no error here
Direct3DDevice->CreateTexture(WindowClientRect.right - WindowClientRect.left, WindowClientRect.bottom - WindowClientRect.top, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &RenderTexture, NULL);
//no error, D3DFMT_X8R8G8B8 is like the back buffer
//I take the first layer
RenderTexture->GetSurfaceLevel(0, &RenderSurface);
//obviously
Direct3DDevice->GetRenderTarget(0, &BackBuffer);
//after this ->SetVertexDeclaration, ->SetVertexShader, ->SetStreamSource etc...


no error here so I continue and the render pass is:
//note this code is for testing
Direct3DDevice->SetRenderTarget(0, RenderSurface);
Direct3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0);
Direct3DDevice->BeginScene()))
Direct3DDevice->EndScene();

Direct3DDevice->SetRenderTarget(0, BackBuffer);
Direct3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);
Direct3DDevice->BeginScene();
Direct3DDevice->SetTexture(0, RenderTexture);
Direct3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2)))
Direct3DDevice->EndScene();


nothing wrong I think (I've omitted a lot of code in order to keep this post short. The shader used is quite simple: vs_1_1 dcl_position v0 dcl_texcoord v1 mov oPos, v0 mov oT0.xyzw, v1 but when I render in the central quad (that I render associated with the texture cleared with D3DCOLOR_XRGB(0, 255, 0) I got garbage: as you can see background is D3DCOLOR_XRGB(0,0,255) but the texture seems to not have been clear at all. That is very strange. If I'd use D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER (a commond mistake) this will be a normal error but now I'm a little confused [Edited by - bjogio on August 6, 2005 9:24:27 AM]

Share this post


Link to post
Share on other sites
Advertisement
Your Clear() calls look the wrong way round:

- the Clear() call for the render target texture (RenderSurface) has D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER

- the Clear() call for the back buffer (BackBuffer) has D3DCLEAR_TARGET


Since the render target texture doesn't have an attached Z buffer, the Clear() call will fail (its likely to fail silently if you're using the retail D3D runtime rather than the debug one).

Additionally, not clearing the Z buffer for the main back buffer will result in artifacts if there's anything already been rendered.


Other than that, there's nothing obvious - if the problem isn't related to your Clear() flags, could you post the part of your pixel shader (or equivilent FFP setup) where the texture is sampled when rendered onto the quad and the relevent binding between pixel shader input register and vertex shader output.

Share this post


Link to post
Share on other sites
Hi there bjogio,
How are you doing?

[The problem]
You get garbage when you render your texture that you rendered the scene to.

[Solutions]
Clear your Zbuffer
You aren't clearing your depth buffer for your color buffer (back buffer) but you are clearing the depth buffer for a non-attached ZBuffer.
[source lang = c++]
//this should be
Direct3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);
//this
Direct3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);




Hope this helps, Keep cool and take care.

PS: Just saw S1CA beat me to it :)

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!