Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to render to a texture, but nothing is being drawn. Everything before SetRenderTarget is rendered properly, everything after second SetRenderTarget is visible on the screen. Just the part that should have been rendered to a texture is ignored.

Below is a Delphi code showing what I'm doing. What am I missing?

//Random texture size, non-power-of-two textures supported by card
//Tried with 512x512, same results
FStaticWidth := 806;
FStaticHeight := 413;

//Error checking code left out for clarity
//But every operation is checked

//Create texture
dev.CreateTexture(FStaticWidth, FStaticHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, FStaticText, nil);
FStaticText.GetSurfaceLevel(0, surf);

//Save everything;
dev.GetViewport(oldVp);
dev.GetRenderTarget(0, oldTarget);

//Change render target
dev.EndScene;
dev.SetRenderTarget(0, surf);
dev.BeginScene;

//Reset viewport
vp.X := 0;
vp.Y := 0;
vp.Width := FStaticWidth;
vp.Height := FStaticHeight;
vp.MinZ := -1000;
vp.MaxZ := 1000;
dev.SetViewport(vp);

//Draw
dev.Clear(0, nil, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER, $FF112233, 1.0, 0);

//...whatever I do here, it's ignored.
//It doesn't appear neither on the surface, nor on the screen.
//Not even dev.Clear() above works.

//Restore render target
dev.EndScene;
dev.SetRenderTarget(0, OldTarget);
dev.BeginScene;

//Restore viewport
dev.SetViewport(oldVp);

//Dump the texture to a file
D3DXSaveSurfaceToFileW(PWideChar(AppFolder+'\static-surf.bmp'), D3DXIFF_BMP, surf, nil, nil);
//The file is a FStaticWidth x FStaticHeight bitmap,
//containing only black transparent ($00000000) pixels.

Here's how it looks like in PIX:

12570   <0x05AC9EA0> IDirect3DDevice9::GetViewport(0x0018FD40)
12571   <0x05AC9EA0> IDirect3DDevice9::GetRenderTarget(0x00000000, 0x0018FD80 --> 0x05ACA9B8)
12572   <0x05AC9EA0> IDirect3DDevice9::EndScene()
12573   <0x05AC9EA0> IDirect3DDevice9::SetRenderTarget(0x00000000, 0x05B53878)
12574   <0x05AC9EA0> IDirect3DDevice9::BeginScene()
12575   <0x05AC9EA0> IDirect3DDevice9::SetViewport(0x0018FD58)
12576   <0x05AC9EA0> IDirect3DDevice9::Clear(0x00000000, NULL, 0x00000003, D3DCOLOR_ARGB(0xff,0x11,0x22,0x33), 1.000f, 0x00000000)
12577   <0x05AC9EA0> IDirect3DDevice9::EndScene()
12578   <0x05AC9EA0> IDirect3DDevice9::SetRenderTarget(0x00000000, 0x05ACA9B8)
12579   <0x05AC9EA0> IDirect3DDevice9::BeginScene()
12580   <0x05AC9EA0> IDirect3DDevice9::SetViewport(0x0018FD40)

Render target 0x05B53878 remains empty (transparent black) after each event.

share|improve this question
2  
Not sure what you are doing wrong here, but you might investigate using PIX which is in the DirectX SDK. It will pretty-much give save you from having to do the ...SaveSurface... call and give you access to the textures without you having to worry about grabbing them correctly. –  Jonathan Dickinson Feb 14 '12 at 20:56
1  
Also you can turn on debugging in the DXSDK to get TTY output of errors straight from the horse's mouth. –  Patrick Hughes Feb 16 '12 at 15:45
add comment

1 Answer

up vote 0 down vote accepted

Solved. You have to set DepthStencil surface along with RenderTarget, and those have to be of the same dimensions and formats.

FStaticText.GetLevelDesc(0, desc);
dev.CreateDepthStencilSurface(FStaticWidth, FStaticHeight, D3DFMT_D16,
    desc.MultiSampleType, desc.MultiSampleQuality, true, FStaticStencil, nil);

//...
dev.GetDepthStencilSurface(oldStencil);
dev.SetDepthStencilSurface(FStaticStencil);

//...
dev.SetDepthStencilSurface(oldStencil);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.