[DX11] Render scene to multiple windows

Started by
9 comments, last by EvilWeebl 12 years, 4 months ago
Hi all, I'm trying to create a level editor and haven't picked up directx since dx9. For simplicity's sake imagine that I have two windows that I need to render my scene to. It's the same scene but need to render it differently for each window i.e one with perspective camera, one with orthographic camera and in wireframe.

I've seen a lot of different explanations of how to do this and these include using multiple devices, multiple swapchains and multiple render targets.

Since this is all new to me could somebody shed some light as to the method I need to use and how to go about it?

I cant seem to find any resources that explain how it all fits together and what each component(device, factory, swapchain, immediate context, render targets) actually do. If there was any information on this I might have a better understanding of it all and a chance to work it out myself.

Any help would be much appreciated, thank you.
Advertisement
You want to use multiple swap chains. A swap chain is your interface for rendering to any particular window. When you create it you bind it to an HWND, and it provides you with a backbuffer which is a render target texture. So then each time you want to render to the window, you set the backbuffer render target as the current render target and then do all of your rendering. Then you call Present on that swap chain, and the contents of the backbuffer are copied to the window.
You might also be interested in taking a look at this thread, which also has some description of the same topic. I assume you are looking for D3D11 material, right???
Thanks Matt and Jason for your quick and helpful replies. It's a bit late in the day to be tinkering right now but I think you've set me along the right track. I'll have a fiddle and see what I can come up with tmw and post if I have any further questions.

Oh and also I would like to mention how funny it is that only last week did I ask for your book to be put on my xmas wishlist and have a feeling it might be wrapped up somewhere in the house haha. If only I could find it there might not have been a need for this post after all.

Many thanks.
For all of our sakes, I hope your not on the naughty list this year :)
Forgive me for this nooby question but could you explain the purpose of the immediate context to me. As I am at the very start of implementing DX into my winform editor I merely have this to change the window to red:

void Engine::Render()
{
float clearColor[] = {1.0f, 0, 0, 1.0f};
_ImmediateContext->ClearRenderTargetView(_RtvBackbuffer, clearColor);
_SwapChain->Present(0, 0);
}


Am I correct in thinking I only need the one immediate context and if I wanted to render to two windows and different colours it would be as such:

void Engine::Render()
{
float clearColor[] = {1.0f, 0, 0, 1.0f};
_ImmediateContext->ClearRenderTargetView(_RtvBackbuffer[0], clearColor);
_SwapChain[0]->Present(0, 0);

clearColor[] = {0, 1.0f, 0, 1.0f};
_ImmediateContext->ClearRenderTargetView(_RtvBackbuffer[1], clearColor);
_SwapChain[1]->Present(0, 0);
}


Finally, and I'm getting ahead of myself here, but when I come to rendering models/simple triangles with vertex buffers; what is it that I need to alter to render in wireframe mode? As I understand it, its a rasterizer state that can be set on the device...but surely that would make all render targets draw in wireframe when I only want a certain window. I take it I set wireframe setting in a shader and render that swapchain rendertargetview separately with that shader?

Oh and finally, finally, what is OMSetRenderTargets used for? Its used on all the samples I have, even the most basic directx11 tutorial where they only create the window and colour the window as I have with ClearRenderTargetView but mine seems to do the same job without it. My guess was that its sole purpose was to set the device to render to multiple targets at once yet all the dx samples use it and they only ever have the one window.

Many thanks to anyone who could shed some light on all this for me, I really wish there were some better resources about for learning all this stuff.
The basic layout of the API is that you have a ID3D11Device which is responsible for all D3D object creation, while there is an ID3D11DeviceContext which is responsible for actually setting state and drawing something. The device context is essentially your way of interacting with the rendering pipeline to get something done.

With D3D11 there is the concept of an immediate device context and a deferred device context. The deferred variant is only used for multithreaded rendering support, which you can safely ignore for now until you get the basics down. So in this case, you will use the immediate context for all of your interactions with the pipeline, and use the device itself to create resources.

As to your questions:
1. Your second code snippet is correct, as long as the two render target views were acquired for the swap chain of the window that you want to render into. You render into this RTV, and when you are done then you present the results to the window through the swap chain interface (that you created) for that window.

2. Fill mode is a part of the Rasterizer state, so you would set it there. This involves creating a state object with the desired settings, then binding that state to the pipeline through the immediate context (ID3D11DeviceContext::RSSetState()). If you want to draw different windows with different fill modes, you just need to create separate state objects and use them accordingly when rendering to each window.

3. The OM stands for Output Merger, which is the last stage in the pipeline. This is where you bind a render target view to the pipeline to receive the output of the pipeline. You may not need to use it right now since you are just clearing the resource color, but once you start doing some rendering then you will need to bind the render targets to the pipeline.

There are some good resources out there (and some not so good too...), but if you are a complete beginner to the topic then I would recommend getting a book or two to get you going. Some of these old journal posts might help you get started too:

Direct3D 11 Programming Tip #1

Direct3D 11 Programming Tip #2
Direct3D 11 Programming Tip #3
Direct3D 11 Programming Tip #4
Direct3D 11 Programming Tip #5
Direct3D 11 Programming Tip #6
Thanks again Jason, again you've been quick and concise and have my gratitude. You've cleared up a lot about the pipeline for me, I wish it was easier to find such clear info elsewhere. I will get onto reading those articles post haste.

You sir have earned yourself a 'like'.

Much appreciated.
Oh one more quick question and then I'll stop bugging you for a while I promise. This is just so I know for later when I start some proper rendering. If in my scenario I have:
  • 1 scene
  • 4 windows(1 is a perspective viewpoint and the other 3 are orthographic, wireframed and the cameras are set at different positions).
This means I have 4 swapchains and their accompanying rtv's.

Now let me know if I've got this wrong but wouldn't I have to set the rasterizer state on the device context to a solid state to render the first window and then have to change the rasterizer state to wireframe and render the next 3 windows? In which case doesnt that meant I couldn't use OMSetRenderTargets to set all 4 rtv's as they would all be drawn the same using only the rasterizer state that is currently set on the context? Would I have to individually set each rtv to the context using OMSetRenderTargets each time I rendered a different window? Ive seen that you can pass in multiple rtv's into OMSetRenderTargets at once so would it be possible for me to do that and yet still receive the desired different rendering for each window or would all rtv's be drawn the same?

Thanks again in advance for any help you can give.
You would need to set the render target for each swap chain one at a time, draw, present, and then switch to the render target for the next display. Multiple render targets doesn't work for your scenario of having multiple displays, since with multiple render targets every triangle you draw gets rasterized the same way to every render target that's bound. So the only thing that changes is the pixel color/value that you write out from your pixel shader. In your case you want to set each render target individually, because each you will render with a different viewpoint (as well as rasterizer settings for wireframe) for each window.

This topic is closed to new replies.

Advertisement