Posts
67
Comments
120
Trackbacks
0
Windows Store DirectX C++ Sample Base

I’ve been working on this on and off for over a year now (having essentially restarted entirely in the latter half of 2012). At last it is finished. I give you a Windows Store DirectX C++ Sample Base. It’s something you could build a game on top of. It’s something you could build a sample on top of. It passes WACK testing (or did until I reverted to the default icons anyway) and runs properly on my Surface RT as well as my other x64-based computers. It handles rotation. Lets you choose a fixed back buffer size. Includes an audio engine class, a Texture2D class, and a RenderTarget2D class. Makes use of DirectXTK. Lets you do fancy things like mixing XAML and DirectX while still using multisampling. Has a custom settings pane with premade volume controls, a privacy settings pane (you need to supply the appropriate privacy policy and any other controls you need, if any), and even an about settings pane. Shows you the basics of using XInput. Shows you the basics of doing async in C++ using the ConcRT PPL Tasks library. And it even includes Shawn Hargreaves’ cat and a Cornflower Blue background!

I’d love to write more about it, but I’ve been burning the candle at both ends to try to finish it up before January ended. I made it just in time! The code is all very commented. Key things you’ll want to look at are App.xaml.cpp, DirectXPage.xaml.cpp, DirectXPage.xaml, and Game.cpp (which derives from DirectXBase, which you may also want to look at). Note that the descriptions of what functions do are all in the header (.H) files whereas the comments on how they do it tend to all be in the CPP files.

Let me know what you think if you try it out. If you have any problems or questions, let me know that too. I’ve created a thread over in the Off Topic forum in the XNA/DirectX forums to facilitate discussion and centralize questions and answers - Windows Store DirectX C++ Sample Base.

You can grab the sample here: BaseWin8Direct3DApp.zip . You need to be running Windows 8 and Visual Studio Express 2012 for Windows 8 or Visual Studio 2012 Professional or above. I hope to start creating samples using this as a base to build on very soon. I’d be thrilled if others did the same. I’ll also try to include some posts about developing for the Windows Store in general (helpful links, good blogs, etc.) and about using C++. If you are new (or returning) to C++, I encourage you to check out the free (reg. req’d) eBook I wrote for SyncFusion last summer, C++ Succinctly - http://geekswithblogs.net/mikebmcl/archive/2012/09/07/c-succinctly-now-available.aspx . It has a 20 code samples that guide you through the important, tricky parts of C++.

Hopefully you will find it helpful!

posted on Thursday, January 31, 2013 11:14 AM Print
Comments
Gravatar
# re: Windows Store DirectX C++ Sample Base
Lloyd
2/4/2013 9:18 AM
Hi,
Speaking of Windows Store sample I'm learning WinRT DirectX and I have a project in progress wrapping DirectX for C# user, do you think you could have alook?
The URL: http://directwinrt.codeplex.com/
:)
Gravatar
# re: Windows Store DirectX C++ Sample Base
mikebmcl
2/5/2013 9:13 PM
You might want to check out the SharpDX project if you are interested in using DirectX with C#. Also, DirectXTK (which you include) is licensed under the Microsoft Public License yet you have your project licensed under the MIT license without any mention of DirectXTK's license. I strongly suggest you correct the licensing problem. If you have any questions or concerns, I recommend contact an attorney who is familiar with copyright law.
Gravatar
# re: Windows Store DirectX C++ Sample Base
Steve Hazen
2/20/2013 1:16 PM
Hi Mike,
Thanks for a wonderful starter base. While learning to work with c++ for the 1st time in my life and coming from c#/xna, I've found it and your guide (from a last year) indespensible.
I've found it challenging working with Concurrancy/PPL Asset loading. In your sample, although the assets are loaded asyncronounsly from the UI thread, the walking Man & cat are loaded lineraly (one after the other). Is it possible to load assets asyncronously? Say loading meshes or shaders while a different thread loads textures? I've tried this and what I see are errors with indications that two threads don't want to share the same D3D11 objects like device & deviceContext. I'm aware from the docs that the PPL has resource managent functionality but I'm not sure if it can handle the D3d11 objects. Do you have any input or insight on this?
Thank you.
Gravatar
# re: Windows Store DirectX C++ Sample Base
mikebmcl
2/20/2013 3:33 PM
Hey Steve,

Glad they've proved helpful. The Concurrency Runtime and PPLTasks are tricky. When you add in D3D threading it can get particularly messy. In general, you want to look at this link: http://msdn.microsoft.com/en-us/library/ff476880(v=vs.85).aspx#Threading_Considerations . The device itself is free threaded such that it should be possible to call as many member functions of ID3D11Device from as many threads as you want without problems. The device context(s) are single threaded; you can call an ID3D11DeviceContext's member functions from different thread but you would need to introduce synchronization code (CRITICAL_SECTION, mutex, etc.) to ensure that no two threads could be executing device context member functions concurrently for the same context on different threads. I serialized the two loads because I was initially passing in the actual immediate context when loading the PNG file (if the context is passed in, it'll generate mipmaps for you). I switched to passing in nullptr (no mipmaps created) for the context and so it should be safe to run both load concurrently.

In the pixel perfect collision sample, the two different functions that can be used to get the pixel data both require the immediate context (they both use ID3D11DeviceContext::Map which cannot be run on a deferred context). As such those have to run serially from the main thread. The reason why the main thread is that if we let it run on some other thread (without synchronization code) then the main thread could potentially be doing things like using the immediate context to display a loading animation or even just clearing the back buffer to cornflower blue while another thread is simultaneously trying to use the immediate context to map the intermediate staging texture that we use to read out data from the GPU to the CPU, causing nasty D3D error messages and (likely) a program crash. PPL can't do anything about it on its own, unfortunately, since the device contexts are only made to be single threaded. You could synchronize access to it, of course. But that may produce an unacceptable stall on the UI thread (i.e. the main thread). If you do decide to explore synchronization, I definitely recommend reading this article - http://msdn.microsoft.com/en-us/magazine/jj721588.aspx - from MSDN Magazine by Kenny Kerr as it's a great overview of the different types of locks and what not available on Windows using C++.

So the answer is that yes it should be possible to load reasources (meshes, shaders, textures, etc.) from different threads just so long as you never use a device context during the loading phase OR you synchronize access to any device context that could be accessed from multiple threads so that an operation on a device context (i.e. a member function call) completes before any other thread has a chance to try to use that same context.

If you have some spare time, it might prove very helpful to go through these presentations - http://blogs.msdn.com/b/chuckw/archive/2010/08/23/direct3d-11-multithreading.aspx - as they may help clear things up further. Let me know if you have any other questions!

-Mike
Comments have been closed on this topic.
Bob Taco Industries is an ISV focused on game and app development for Microsoft platforms headed up by Michael B. McLaughlin. Mike is a Microsoft Visual C++ MVP (previously an XNA/DirectX MVP from 2011-2013), a developer, a writer, a consultant, and a retired lawyer. If you're a developer who is just getting started, consider checking out the BTI website's section for developers for links to code samples and other helpful sites.
Tag Cloud