• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Milla.M

DirectX drawing in other thread

4 posts in this topic

Hi
I have some directx code that I want to run in second thread.
I read the following [url="http://www.codermind.com/answers/How-do-I-run-with-a-multithreaded-device-in-Direct3D9.html"]http://www.codermind...-Direct3D9.html[/url] which says that is ok to use directx device from other thread as long as you are not using concurrently from many threads, and I am not doing that. And that you have to call CreateDevice and CreateWindow from the same thread , and some others functions like TestCooperativeLevel, Reset etc

And I am doing the following :

1) create window and directx device in main thread

2) start second thread and do the rendering and updating there , basically :

update
BeginScene
Draw
EndScene
Present


3) Also I care to call TestCooperativeLevel, Reset, pDevice->Release and pDirect3D->Release() from the main thread.

Problem :
The code is working when I test it in debug mode. But it doesn't in relase mode i.e it seems that the rendering threads draws all fine, but my main thread seems blocked and doesn't process messages, or maybe sometimes process them very slow. If just comment the call to Present , everything is fine ( of course it doesnot draw on screen ) i.e the main thread is responsive and second thread is running too. Sooo, is there some problem when calling Present from other thread, if the window and device is created in main thread ? As I read the following [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb147224%28v=vs.85%29.aspx"]http://msdn.microsof...4(v=vs.85).aspx[/url] such restriction has only on CreateDevice,TestCooperativeLevel and Release.
0

Share this post


Link to post
Share on other sites
Could you post some code on how you do this?

I have had similar errors, and had to solve it with a sleep(1);

And if i remember correct, you should not render on a secondthread, you should render on your main thread, but do all the updating and setup all data to
just be scheduald for drawing, so that the main thread do as litle as possible when the drawing´s begins.

but if you switch to dx11, you get a even more efficent way for multithreading and dx support. (Worth a tought)
0

Share this post


Link to post
Share on other sites
Hi
I can send minimum ( doesn't compile code). I should mention that the code uses wild magic 5 library for rendering, and I am using Qt , to start the second thread. The second thread starts its own message loop , so it can receive the update message . I don't know if you familiar wit qt signal/slots mechanism but this should be irrelevant here.
[CODE]
void Initialize( HWND parentHWND)
{
// DirectX uses a projection matrix for depth in [0,1].
Wm5::Camera::SetDefaultDepthType(Wm5::Camera::PM_DEPTH_ZERO_TO_ONE);

// === Create the window for rendering. ===
// Register the window class.
static wchar_t sWindowClass[] = L"My Window";
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = MyEventHandler;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = 0;
wc.hIcon = 0;
wc.hCursor =0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = sWindowClass;
wc.lpszMenuName = 0;
RegisterClass(&wc);

DWORD dwStyle;
dwStyle = WS_CHILDWINDOW;

// resize it, to fit its parent
RECT rect;
::GetWindowRect(parentHWND, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;


static wchar_t sWindowName[] = L"MyWindow";
// Create the application window.
HWND handle = CreateWindow(sWindowClass,
sWindowName,
dwStyle,
0,//xPos
0,//yPos
width,
height,
parentHWND,
0,
0,
0);

// Create the device for rendering.
mRenderInput.mWindowHandle = handle;
mRenderInput.mDriver = Direct3DCreate9(D3D_SDK_VERSION);
assertion(mRenderInput.mDriver != 0, "Failed to create Direct3D9\n");
mRenderer = new0 Wm5::Renderer(mRenderInput, width, height,
mColorFormat, mDepthStencilFormat, mNumMultisamples);

// Display the window.
ShowWindow(handle, SW_SHOW);
UpdateWindow(handle);

}
class MyRunner: public QObject
{

public slots :
void update()
{

mSceneGraph->Update();
mCuller.ComputeVisibleSet(mSceneGraph->GetRoot());

if (mRenderer->PreDraw()) // PreDraw just calls BeginScene
{
mRenderer->ClearBuffers();
mRenderer->Draw(mCuller.GetVisibleSet());
mRenderer->PostDraw(); // PostDraw just calls EndScene
mRenderer->DisplayColorBuffer(); // DisplayColorBuffer just calls Present
}

emit(SignalUpdate()); // this post message to the second thread message loop so it can basically start the update cycle anew

}
};
class MyThread : public QThread
{
void run()
{
this->exec(); // this starts message loop
}
};
HWND mainAppHwnd = GetMainHwnd();
Initialize(mainAppHwnd);
MyThread* thread = new MyThread;
MyRunner* runner = new MyRunner;
runner->moveToThread(thread);
QObject::connect(thread,SIGNAL(started()),runner,SLOT(update()));
thread->start();
[/CODE]


"And if i remember correct, you should not render on a secondthread, you should render on your main thread, but do all the updating and setup all data to
just be scheduald for drawing, so that the main thread do as litle as possible when the drawing´s begins."

I don't uderstand this.
Does this mean that in my case that I should call the updating of the scenegraph, and drawing functions on second thread, but only call Present on main thread ?

By the way what problem did you solve with Sleep(1) ?

Also I can't switch to DirectX11 Edited by Milla.M
0

Share this post


Link to post
Share on other sites
[quote name='Milla.M' timestamp='1341238074' post='4954910']
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]"And if i remember correct, you should not render on a secondthread, you should render on your main thread, but do all the updating and setup all data to[/background][/left][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]just be scheduald for drawing, so that the main thread do as litle as possible when the drawing´s begins."[/background][/left][/size][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]I don't uderstand this.[/background][/left][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]Does this mean that in my case that I should call the updating of the scenegraph, and drawing functions on second thread, but only call Present on main thread ?[/background][/left][/size][/font][/color]
[/quote]

by this i mean, have your scenegraph updated at the secondary thread(s).
but use your draw & simlar commands on the main thread.

i belive it has something to do with dx9 isént built around multithreading.


"[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]By the way what problem did you solve with Sleep(1) ?"[/background][/left][/size][/font][/color]
[left][font="helvetica, arial, verdana, tahoma, sans-serif"][size="2"][color="#282828"]the freeze, at the end of my multithread job i added a Sleep(1);[/color][/size][/font][/left]
0

Share this post


Link to post
Share on other sites
[quote name='Tordin' timestamp='1341241198' post='4954923']
by this i mean, have your scenegraph updated at the secondary thread(s).
but use your draw & simlar commands on the main thread.
[/quote]
but then I need to have synchronization for each frame between my main thread and rendering thread, so the main thread doesn't update when the rendering thread draws.I think this would be no good.

So anyone else have other idea why the rendering in second thread doesn't work
0

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  
Followers 0