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 slightly confused about how Direct3D rendering works. Basically, as long as I render to one surface, everything is fine. But when I try rendering to multiple surfaces, it seems like everything is still rendered to one surface. I think there's something wrong with my calls.

For each update cycle this is what I do 1. device->BeginScene() 2. sprite->Begin(...)

... A bunch of GetRenderTarget to store the old render target, then SetRenderTarget to set a new surface, and then things like CreateVertexBuffer, SetTexture, etc to draw on the new render target. Then resetting to the old render target.

  1. sprite->Draw([the back buffer]) (the back buffer is actually another surface, not the actual back buffer. But here it is being drawn onto the actual back buffer, I think)
  2. sprite->End()
  3. device->EndScene()
  4. device->Present(...)

Also, it seems like if I mix sprite drawing and non-sprite drawing onto a surface, that first one set of render commands is executed and then the other set, rather than in order by when each command was called.

If anyone could shed light on any of this, it would be much appreciated.

EDIT: To clarify, my render targets are mainly surface 0 of the texture created by D3DXCreateTexture or D3DXCreateTextureFromFileEx.

share|improve this question
add comment

2 Answers

It sounds like the bulk of what you're doing when you say "... A bunch of GetRenderTarget to store the old render target, then SetRenderTarget to set a new surface, and then things like CreateVertexBuffer, SetTexture, etc to draw on the new render target. Then resetting to the old render target." is actually stuff that needs to be done outside of a BeginScene()/EndScene() block. Certainly you shouldn't be creating vertex buffers in there, in the general case (just for performance concerns).

Depending on what the render targets are, and how you're creating and setting them, you may need to doing one BeginScene/EndScene per target, and not shoving them all into a single scene.

I assume you're clearing the render targets before you start drawing, too.

Have you enabled the D3D debug runtimes (via the D3D "Control Panel" in the DX SDK's start menu folder, under the utilities or tools subfolder)? They will cause useful diagnostics to be printed to the output window in VS while you debug.

There are a few multiple-render-target samples in the SDK, too, and probably a few online (although they may deal with multiple swap chains, which isn't what you're done I don't think?)

share|improve this answer
add comment

The name "Draw" is somewhat misleading. It should better be called "AddToBatch". What it actually does is batch your Draw calls. Only when you call ID3DXSprite::Flush or Id3DXSprite::End it fills the sprites into one big vertex buffer and renders that onto the current render target.

So if you want to switch render targets in between, you will have to do something along these lines (pseudo-code):

Scene->Begin()

Device->SetRenderTarget( FirstTarget )
  Sprite->Begin()
  for each sprite to draw onto first target
    Sprite->Draw()
  Sprite->End()

Device->SetRenderTarget( SecondTarget )
  Sprite->Begin()
  for each sprite to draw onto second target
    Sprite->Draw()
  Sprite->End()

Scene->End()
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.