Direct3D Tutorial Win32 Sample

This is the DirectX SDK's Direct3D 11 tutorial series updated to use Visual Studio 2012 and the Windows SDK 8.0 without any dependencies on legacy DirectX SDK content.

C++ (357.1 KB)
52,801 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Tut 2 - Build and Runtime Errors
    3 Posts | Last post October 19, 2015
    • Hi Chuck,
      I'm running the tutorials on Windows 7 SP1, with Visual Studio Community 2013, and the DirectX SDK (June 2010) installed.
      Tutorial 1 builds and runs properly, but only after adding this pragma to Tutorial01.cpp:
      #pragma comment(lib,"d3d11.lib")
      Without the above pragma, I get these errors:
      Error	1	error LNK2019: unresolved external symbol _D3D11CreateDevice@40 referenced in function "long __cdecl InitDevice(void)" (?InitDevice@@YAJXZ)
      Error	2	error LNK1120: 1 unresolved externals
      Tutorial 2 builds, but only after adding these pragma to Tutorial02.cpp:
      #pragma comment(lib,"d3d11.lib")
      #pragma comment(lib,"D3DCompiler.lib")
      Without the above pragma, I get these errors:
      Error	1	error LNK2019: unresolved external symbol _D3D11CreateDevice@40 referenced in function "long __cdecl InitDevice(void)" (?InitDevice@@YAJXZ)
      Error	2	error LNK2019: unresolved external symbol _D3DCompileFromFile@36 referenced in function "long __cdecl CompileShaderFromFile(wchar_t *,char const *,char const *,struct ID3D10Blob * *)" (?CompileShaderFromFile@@YAJPA_WPBD1PAPAUID3D10Blob@@@Z)
      Error	3	error LNK1120: 2 unresolved externals
      Also, to build Tutorial 2 I must exclude all three of these files from the build, (Tutorial02.fx, Tutorial02_PS.hlsl, Tutorial02_VS.hlsl), or I get these errors:
      Error	1	error X3501: 'main': entrypoint not found
      With the above changes, Tutorial 2 builds without errors, but I get this run-time error:
      The program can't start because D3DCOMPILER_47.dll is missing from your computer. Try reinstalling the program to fix this problem.
      Any ideas?
      Thank you.
    • This all sounds like problems with your VC project files (vcxproj). They should upgrade in place from VS 2012 to 2013 "as is". You may want to try the 2013 files for the tutorials on <> to see if those work for you. If not, you should try the VS Direct3D Game template for VS 2013 <> and take a look at the DirectX Tool Kit tutorials.
    • Hi Chuck,
      I just added the DirectX SDK (June 2010) Include and Lib/x86 paths to the VC++ Directories property of Tutorial 1 on my VS 2013 project, but the build still fails unless I include:
      #pragma comment(lib,"d3d11.lib")
      But d3d11.lib is in the DirectX SDK (June 2010) Lib/x86 path, so I'm not sure why the build fails without the above pragma, given that the DirectX SDK Lib/x86 path is now added to the project.
      However, I did not take the VC project files (vcxproj) from the Tutorial distribution. I created an empty Win32 project and then added these files to the project, from the Tutorial distro:
      Does any of the above explain why the build fails without the pragma?
      I have another question about the vertex normals of 3D models imported from an external modeling program, into a DirectX project I'm building on VS 2013. This is probably not the place to ask you about that, so can you point me to a forum where you and I can have that discussion?
      Thank you.
  • weird overdraw with mdi applications
    2 Posts | Last post March 14, 2015
    • Hi Chuck,
      I placed this sample code within a mfc mdi application (wizard generated). I get weird results, there are some overdraws in the child frames and content while moving arround. This only occurs running with D3D_DRIVER_TYPE_HARDWARE, switching to D3D_DRIVER_TYPE_WARP is fine.
      Some screenshot:!212&authkey=!AA-UPWXbSmrkGPE&v=3&ithint=photo%2cpng
      Are there any special window flags needed for mdi applications?
      For the record, I checked this on different Windows 7 machines running different Intel graphics (mostly HD4000), with different driver versions.
      And, there are the same results using the code in a c# mdi application via a clr dll.
    • The tutorial doesn't handle resize and is simple just to get the basics out of the way. You need to handle resizing to get the right behavior for an MDI. See this VS 2013 template for a more complete example of handling resize <>.
  • D3D11CreateDeviceAndSwapChain
    3 Posts | Last post February 03, 2015
    • Hi,Chuck,
      I'm running this tutorial01,it shows up the window and shut down in less than 1 second,I followed the code running,and find out the D3D11CreateDeviceAndSwapChain method was not successed,I'm using vs 2012 and windows 8.1,don'know why this happen.
    • You are likely building the Debug configuration which uses D3D11_CREATE_DEVICE_DEBUG. This requires the SDK debug layer be present. Visual Studio 2012 with the Windows 8.0 SDK does not have the Windows 8.1 version of the SDK Debug Layers, so all attempts to use debugging fail. This should be addressed in a future update, but at the moment if you are using Windows 8.1 you should use VS 2013 (the RC was released today). Alternatively, you can remove the use of the debug layer.
    • See this blog post for more details about where to get the right debug layer for your OS: <>
  • Thanks for this, Chuck
    2 Posts | Last post February 03, 2015
    • The bit of errata about re-trying D3D11CreateDeviceAndSwapChain() calls with D3D_FEATURE_LEVEL_11_1 excluded got us up and running on stock Windows 7. I would have thought that feature levels that the OS doesn't know about would get skipped and the next FEATURE_LEVEL would be tried. The fact that it out-and-out fails if you pass in unknown feature levels in the array was really surprising.
    • Note that this is addressed in this blog post: <>
  • Can't use any file access in the sample
    2 Posts | Last post January 31, 2015
    • I am trying to write a buffer content to a file with the sample (any sample). First tried fopen or _fsopen, both can't open a valid file for writing, the errno is 13 (permission denied). Tried to use windows API CreateFile, but it would not even compile, says can't find the file definition, even I included the related header files.
      I am running this as a windows app (not even the phone version). I am wondering what is special about this sample here. Appreciate your quick response as I am stuck here.
    • This is the Win32 desktop version of the Direct3D tutorial. If you are targeting Windows Store or Windows Phone use that tutorial instead. Windows Store apps have limited file write access which is why you get error 13. CreateFile is unavailable to Windows Store apps, but you can use CreateFile2. See
  • Can not see file?
    5 Posts | Last post August 31, 2014
    • When I am browsing the Code for Tutorial #2 both the VS and PS shader files are including the fx file. I can not see this fx file. Without this I can not get the code to build.
    • Do you mean when you try to run it? Tutorial02.fx is in the directory with the sample source.
    • I have the *.fx file which I found in the 2010 SDK samples. However when I try to build in VS2012 I am getting this error.
      1>------ Build started: Project: Direct3DSamples, Configuration: Debug Win32 ------
      1>FXC : error X3501: 'main': entrypoint not found
      1>  compilation failed; no code produced
    • Redownload the sample package from MSDN Code Gallery. All configurations of it build fine in VS 2012. The Tutorial02.fx file is in the project but marked as "does to participate in build". The Tutorial02_VS.hlsl file includes "Tutorial02.fx" and is configured to use "VS" as the entry-point for a vs_4_0 profile. The Tutorial02_PS.hlsl file includes "Tutorial02.fx" and is configured to build "PS" as the entry-point for a ps_4_0 profile.
    • Thank You for the information. I had actually typed in all the code and created my own files. I didn't download and open the sample from the SDK browser. I was thinking of modifying this to use ps & vs 5_0 or 5_1. A while ago I was using Rastertek's website which was a great tutorial site for both DX10 and DX11 starting from basics and working through to advanced shaders and it even had its own section for 3D terrain rendering as well, but the site was recently taken down. It is very hard to find any truly good tutorial sites especially with good video tutorials.
  • Applications using DirectX TK stops with abort()
    5 Posts | Last post January 03, 2014
    • Hello,
      I got this message whenever I run an EXE that uses Directx TK from outside the Visual Studio:
      Microsoft Visual C++ Runtime Library
      Debug Error!
      Program: C:\DXTK_C++\C++\Debug\SimpleSample.exe
      - abort() has been called
      (Press Retry to debug the application)
      When I use the DirectX TK in my application, I get it when I run the compiled exe, so I tried to compile the Directx TK Simple Sample but it also behaves in the same way.
      I forgot to mention that I use the DirectX TK 13/11/2012 and the Visual Studio 2012 on the Windows 7. Please I hope I get a quick reply.
    • Without a debug stack, it's very difficult to determine the root cause. You should run under the debugger and determine where this is getting triggered. Make sure you are building DirectXTK with the same version of your compiler as the rest of your app.
      You may want to move this discussion to the DirectXTK CodePlex site <>
    • Hello,
      Thank you very much,I was not expecting such a quick reply, really thanks.
      Concerning the debugging, after I ran the application in debugging mode, the file SpriteFont.cpp stops with a break message on the line 82 on this part of code exactly:
      SpriteFont::Impl::Impl(_In_ ID3D11Device* device, _In_ BinaryReader* reader)
          // Validate the header.
          for (char const* magic = spriteFontMagic; *magic; magic++)
              if (reader->Read<uint8_t>() != *magic) // Here it stops exactly
                  throw std::exception("Not a MakeSpriteFont output binary");
      Sorry but the version I am using is not of 13/11/2012 but it is the one of 11/13/2013;
      I read in its associated documentation that it is however compatible with the Visual Studio 2012 and Windows 7.
      About the link you provided, I didn't manage to get there, since there is some kind of problem with it.
      I am a student and I has an assignment in which I preferred to benefit from the DirectX TK, so any help would be really appreciated.
      Thanks again.
    • You should enable C++ exception debugging and look at the callstack. You are probably providing the wrong kind of file to SpriteFont's constructor. See <> and <>
    • Note this question is not related to this particular sample. Please follow-up on the CodePlex <>
  • Project incompatible with current edition of visual studio
    4 Posts | Last post February 07, 2013
    • tried to load this sample (Direct3D Tutorial Win32 Sample) into VS 2012 Ultimate and each project fails to load with the text written under the project name - "This project is incompatible with the current edition of Visual Studio" - running Windows 7 and have installed the Windows 8 SDK. 
    • The most likely reason you'd see this is if you were actually opening it with VS 2012 Express for Windows 8. Make sure you are opening it with the correct version of VS and that you are using VS 2012 RTM, not an RC. Try opening the solution explicitly with VS 2012 rather than click-launching it.
      It works fine for me with VS 2012 RTM and VS 2012 Update 1 using Windows 7 and Windows 8.
    • The other possibility is that you are actually trying to open a Windows Store app project with VS 2012 on Windows 7 which doesn't work either. Make sure you aren't trying to open the other sample "Direct3D tutorial sample" which is a Windows Store app.
    • I had accidentally tried to open the store version - thanks. 
  • Getting error MSB3073: The command "copy... error and not sure how to fix
    7 Posts | Last post January 24, 2013
    • Mr. Walbourn,
      I am getting the following error for project 2 through 6. I understand that it is trying to copy the dll to the debug folder, but I don't understand why it is failing. I unpacked the data into the Projects folder, double clicked the .sln file and when I hit "Build Solution", the following error appears for project 2 through 6:
      コマンドの構文が誤っています。<-- I live in Japan, but this says there is a syntax error
      7>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppCommon.targets(204,5): error MSB3073: The command "copy “C:\Program Files (x86)\Windows Kits\8.0\redist\d3d\x86\D3DCompile*.DLL” “D:\Documents\Visual Studio 2012\Projects\Direct3D\C++\Debug\”
      7>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppCommon.targets(204,5): error MSB3073: :VCEnd" exited with code 1.
      Sorry, I am a newbie with Visual Studio Express 2012. Any help would be appreciated. Thank you.
    • I updated the sample recently to use "$(WindowsSdkDir)redist\d3d\x86\D3DCompile*.DLL" which works on the "RTM" version, rather than the older $(WindowsSdkDir)\redist\d3d\x86\D3DCompile*.DLL" which only worked with "RC" and other pre-release versions. It looks like the 'correct' thing is happening on your system with the source. Do you have the files "C:\Program Files (x86)\Windows Kits\8.0\redist\d3d\x86\D3DCompile*.DLL" on your system?
    • Mr. Walbourn,
      I finally figured out the reason it was failing:
      copy “$(WindowsSdkDir)redist\d3d\x86\D3DCompile*.DLL” “$(TargetDir)”
      copy "$(WindowsSdkDir)redist\d3d\x86\D3DCompile*.DLL" "$(TargetDir)"
      (Quotation marks used are different on first line and second line
      as long as MSDN website doesn't screw them up)
      The double quotes used where:  	LEFT DOUBLE QUOTATION MARK 0x201C(UTF-16)
      I changed them to: QUOTATION MARK 0x0022(UTF-16)
      If you have any ideas why this would occur, it would be appreciated.
      I'm using Visual Studio Express 2012 Version 11.0.51106.01 Update 1
      on Windows 7 SP1. I live in Japan, so I was wondering if that has something
      to do with it as the LEFT DOUBLE QUOTATION MARK is used when typing in Japanese.
      Thank you for your time.
    • The quote changes are likely not portable across all locales. Also, the copy can return a code if it produces a prompt to overwrite. The last problem is that the multi-threaded build can lock the source file for some configurations, which seems to be fixed by setting "Treat Output as Content" to "Yes".
    • >The quote changes are likely not portable across all locales. 
      This might be true, however, I have the language setting set to English in Visual Studio, and I see no reason why it should use the local locale here。
      I submitted a bug report:
    • Mr. Walbourn,
      I received an update to my bug report and here is the reply:
      "Thank you for creating a connect report to us. The sample has been fixed, but there is nothing we can do in the IDE for this case. The IDE does no quote manipulation or correction. If can think of something, feel free to share your thoughts and re-activiate this report."
      What do they mean by, "The sample has been fixed", was it a problem with the sample itself?
    • In this specific case, yes. The VCXPROJ file had the wrong kind of quotes in it.
  • Few questions about Tutorial01
    7 Posts | Last post January 13, 2013
    • Hi Chuck.
      1)Can we do not using global variable, by changing function input parameters for communication via local?
      2)In DX9 book at
      ShowWindow(g_hWnd, nCmdShow);
      return S_OK;
      UpdateWindow is present. Why in this sample it don't?
      3)DX9 book said that COM links must be set NULL after release. Why they don't in sample? And in VS2010/2012 it might now nullptr?
      In InitDevice(void) you can return hr because on begin you set it to S_OK.
    • (1) Many of the DirectX SDK samples use globals for simplicity. Better practice would be to use parameters or perhaps make use of C++ objects.
      (2) UpdateWindows forces a WM_PAINT. That's not necessary in the samples. It could well be a left-over from some workaround for an older version of Windows in that book.
      (3) It's always good practice to do 'pointer sanitation' of setting 'dangling' pointers to 0 explicitly. It's not required but it can be a safety measure. Many DirectX SDK samples make use of a SAFE_RELEASE macro to do this conviently, but that's no in the tutorial to keep the code a bit more simple to follow.
      #ifndef SAFE_RELEASE
      #define SAFE_RELEASE(p)      { if (p) { (p)->Release(); (p)=nullptr; } }
      Of course, an even better solution is to use a COM smart-pointer like ATL's CComPtr or WRL's ComPtr.
      'nullptr' is the C++11 type-safe version of the classic "NULL" #define. They mean the same thing, but 'nullptr' is the modern version.
    • 1), 2)Got it.
      3)During evolution of tutorial I see that macro way, but last time see templated class way. Like in your Win8UI sample "Media engine native C++ video playback sample".
      Smart-pointer sounds good, but to early to me, with Express Edition and without <atlbase.h> :(
      When in VS2008 EE I work with <MsHTML.h>, I have some internal COM smart-pointer 
      (for e.g. provided for this types)
      Can I use some near approach in this tutorial with Visual Studio Express 2012 for Windows Desktop?
      New short question. {0} and ZeroMemory using. Did you have some rules when you choose one of that?
      I read Raymond Chen explanation , so in my version of this sample I prefer to use ZeroMemory.
      I too in RECT and D3D11_VIEWPORT cases set only per members initialisation.
      But actually not in all cases per members initialization get a succesfull call of function.
      In my experience I take not succesfull (not run-time exception, app just do nothing) call of ChangeDisplaySettings (in DX9 app) when only simple set to all member of DEVMODE struct.
      {0} or ZeroMemory required before setting
      dm.dmSize = sizeof(DEVMODE);
      dm.dmPelsWidth = 640;
      dm.dmPelsHeight = 480;
      dm.dmBitsPerPel = 32;
      ChangeDisplaySettings(&dm, CDS_FULLSCREEN)
    • I train with feature levels and DirectX 10+ family, and founded that only in DirectX10_1 (maybe the reason only in this feature level) case I can see full screen in Virtual Machine (Virtual Box) of Windows 8 Enterprise Evaluation (Build 9200) by pressing Alt+Enter, in stand alone EXE. In VS (by F5, or Ctrl+F5) I do not enter in full screen mode, when close app I see next in output window DXGI ERROR: IDXGISwapChain::GetContainingOutput: The swapchain's adapter does not control the output on which the swapchain's window resides. [ MISCELLANEOUS ERROR #80: ]
      First-chance exception: Microsoft C++ exception: _com_error 
      On host machine I take full-screen in all variation:
    • With VS 2012, you can make use of the WRL template ComPtr.
      See DXGI Best Practices <> for recommendations about supporting full-screen.
    • During understanding the logic of DirectX in WinStore App I return to this topic, so I got it.
      Next what I have now (sample in Desktop App):
      #include <wrl.h>
      #include <d3d11_1.h>
      #pragma comment(lib, "d3d11.lib")
      namespace DX
      	inline void ThrowIfFailed(HRESULT hr)
      		if (FAILED(hr))
      			// Set a breakpoint on this line to catch DX API errors.
      			throw hr;
      class DxGraphics
      	Microsoft::WRL::ComPtr<ID3D11Device>            m_d3dDevice;
      	Microsoft::WRL::ComPtr<IDXGISwapChain>          m_swapChain;
      	Microsoft::WRL::ComPtr<ID3D11RenderTargetView>  m_d3dRenderTargetView;
      	Microsoft::WRL::ComPtr<ID3D11DeviceContext>     m_d3dContext;
      	D3D_FEATURE_LEVEL                               m_featureLevel;
      	void InitDevice4DesktopApp(HWND hOutputWindow);
      	void Render();
    • In DxGraphics.cpp at void InitDevice4DesktopApp(HWND hOutputWindow); I have next approach
      	D3D_DRIVER_TYPE driverType;
      	ComPtr<IDXGISwapChain> swapChain;
      	ComPtr<ID3D11Device> device;
      	ComPtr<ID3D11DeviceContext> context;
      	HRESULT hr = S_OK;
      	for(UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
      		driverType = driverTypes[driverTypeIndex];
      		hr = D3D11CreateDeviceAndSwapChain(nullptr, driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,
      			D3D11_SDK_VERSION, &sd, &swapChain, &device, &m_featureLevel, &context);
      	// Get the Direct3D swapChain, device and context interfaces.
      see more at DWriteHelloWorld sample (WinStore App).
1 - 10 of 13 Items