Sign in to follow this  
Brejlounek

Swap chains randomly switch rendering

Recommended Posts

Hi there,

I have two DirectX windows in my app and I use swap chains to render to them. This is code for directx initialization:


[source lang="cpp"]d3d=Direct3DCreate9( D3D_SDK_VERSION );


D3DDISPLAYMODE d3ddm;
d3d->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );

ZeroMemory( &d3dpp, sizeof(d3dpp) );

d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
hwnd_img, D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &Dev );

d3d->Release();

...

Dev->GetSwapChain( 0, &swap_view );

d3dpp.BackBufferWidth=256;
d3dpp.BackBufferHeight=256;

Dev->CreateAdditionalSwapChain( &d3dpp, &swap_pos );

swap_view->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &backbuf_view );
swap_pos->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &backbuf_pos );[/source]

This is my first window rendering:

[source lang="java"]Dev->SetRenderTarget( 0, backbuf_view );

Dev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(128,128,128), 1.0f, 0);
Dev->BeginScene();

shader_move->SetTechnique("move");
shader_move->Begin(0,0);
shader_move->BeginPass(0);
shader_move->SetTexture("tex",tex);

Dev->SetFVF(CUSTOMFVF);
Dev->SetStreamSource(0, vbuf, 0, sizeof(CUSTOMVERTEX));
Dev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);

shader_move->EndPass();
shader_move->End();

Dev->EndScene();
swap_view->Present(0,0,hwnd_img,0,0);[/source]

And second window rendering:

[source lang="java"]Dev->SetRenderTarget( 0, backbuf_pos );
Dev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(128,128,128), 1.0f, 0);
Dev->BeginScene();

shader_move->SetTechnique("move");
shader_move->Begin(0,0);
shader_move->BeginPass(0);
shader_move->SetTexture("tex",tex_pos);

Dev->SetFVF(CUSTOMFVF);
Dev->SetStreamSource(0, vbuf, 0, sizeof(CUSTOMVERTEX));
Dev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);

shader_move->EndPass();
shader_move->End();

Dev->EndScene();
swap_pos->Present(0,0,hwnd_pos,0,0);[/source]

Renderings of these windows are called irregulary (when resizing and redrawing). It should work, swap_pos->Present(0,0,hwnd_pos,0,0) should render to hwnd_pos only and swap_view->Present(0,0,hwnd_img,0,0); should render to hwnd_img only. However, it seems that sometimes what is rendered into one swapchains appears on the other and vice versa. They are mostly the first frame of rendering when the window is redrawed, but they render randomly when resizing.

Could someone point out, what could be wrong? Thanks.

EDIT: Problem still persists when I comment out the swap->Present code, so it's not really rendering to the bad window, it switches the texture for some reason...

EDIT2: Problem still persis even when tex or tex_pos temporarily point to the same texture. This is really strange.

Share this post


Link to post
Share on other sites
I have solved it already, I don't render to the second window at all, I just manipulate the buffer data directly (its just simple 2d graphics). Still, I may run into this some time in the future when I'll need to have 3d scene in my second window. So, the problem persists.

Could someone please take a look at the code or guess, where the problem could be?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this