Geeks With Blogs
Bob Taco Industries Blogging Division

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: . 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 - . 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 directx , C++ | Back to top

Comments on this post: Windows Store DirectX C++ Sample Base

# re: Windows Store DirectX C++ Sample Base
Requesting Gravatar...
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:
Left by Lloyd on Feb 04, 2013 9:18 AM

# re: Windows Store DirectX C++ Sample Base
Requesting Gravatar...
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.
Left by mikebmcl on Feb 05, 2013 9:13 PM

# re: Windows Store DirectX C++ Sample Base
Requesting Gravatar...
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.
Left by Steve Hazen on Feb 20, 2013 1:16 PM

# re: Windows Store DirectX C++ Sample Base
Requesting Gravatar...
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: . 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 - - 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 - - as they may help clear things up further. Let me know if you have any other questions!

Left by mikebmcl on Feb 20, 2013 3:33 PM

Comments have been closed on this topic.
Copyright © Michael B. McLaughlin | Powered by: