I'm having a problem with my .fx file in my D3D 11 application.

I can run the application just fine from Visual Studio, but when I run the .exe it fails when trying to load the .fx file.

I've checked the file name in the code and tried all sorts of paths but I can't figure this out, why is this happening?

  • \$\begingroup\$ Try just the file name with nothing prepended, and put the file in the same directory as the exe. \$\endgroup\$ – aaaaaaaaaaaa Jan 3 '13 at 21:47
  • \$\begingroup\$ A shot in the dark here, but if you are using VS2012 and your app has 'windows store support' selected, try right clicking your fx file, select properties, and verifying that 'Content' is set to yes. This gives VS the hint that it needs to be sent to the AppX folder during build along with the exe. \$\endgroup\$ – Steve H Mar 5 '13 at 13:13

This is likely due to a difference in the working directory for your program when run from Visual Studio versus launched "normally." Visual Studio sets the working directory for a process it launches. When you launch the executable normally, the working directory is the directory containing the executable itself.

Since relative paths begin at the working directory, this changes where you end up effectively looking for data files. Consider the following typical directory structure:

  Game.vcproj   <-- your project file
    Shader.fx   <-- your shader
      Game.exe  <-- your compiled executable

You probably refer to your shader file in code via the relative path "Assets/Shader.fx" (or similar, adjusted for your actual directory structure).

Visual Studio uses the project directory as the working directory by default for C++ projects when launching your process. In the case of the above structure, that is the path X/Projects/Game and consequently your relative path reference to the shader is X/Projects/Game/Assets/Shader.fx. This path exists, so everything runs normally when launched from the IDE.

But if you double-click the compiled Game.exe from explorer.exe your working directory is X/Projects/Game/bin/Debug and consequently your relative path reference becomes X/Projects/Game/bin/Debug/Assets/Shader.fx. That path doesn't exist, and until it does, your game won't run properly.

The solution is to ensure that the shader and other assets get copied to the output directory of the executable (maintaining their directory structure, if applicable). One way to do this is via custom build steps in your project, for example by launching a batch file to copy the relevant files or using a direct invocation of something like robocopy. Visual Studio has a fairly robust set of macros that can be used to make construction of this build step easier, so you could do something like:

robocopy $(ProjectDir)/Assets $(TargetDir) /S
| improve this answer | |
  • \$\begingroup\$ Thank you for all that but I'm still having the problem. I'm probably not setting up the custom build steps properly though. I'm having a hard time trying to understand how to do it. \$\endgroup\$ – Laegato Jan 3 '13 at 22:37
  • \$\begingroup\$ You can do a sanity check by first just manually copying the files into the same directory as your executable. You may also want to have your code resolve the absolute path to the files it is trying to look up and log them so you know what it is looking at. Once you have the files in the right spot, then set up your build actions. That way you solve one problem at a time. \$\endgroup\$ – user1430 Jan 3 '13 at 22:39
  • \$\begingroup\$ The .fx file is already in practically every directory on my computer, I've tried everywhere. I'll have a look at tying to log the directories, but I'm crap with c++ and windows programming \$\endgroup\$ – Laegato Jan 3 '13 at 22:43
  • \$\begingroup\$ Or just turn on "copy to output directory"... \$\endgroup\$ – 3Dave Mar 4 '13 at 22:38
  • \$\begingroup\$ "Copy to Output Directory" is great if you're using a project type / VS version combination that supports it, but unfortunately, not all do. :( \$\endgroup\$ – user1430 Mar 5 '13 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.