Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
Try just the file name with nothing prepended, and put the file in the same directory as the exe. –  eBusiness Jan 3 '13 at 21:47
    
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. –  Steve H Mar 5 '13 at 13:13
add comment

1 Answer

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:

X/Projects/Game
  Game.sln
  Game.vcproj   <-- your project file
  Source/
  Assets/
    Shader.fx   <-- your shader
  bin/
    Debug/
    Release/
      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
share|improve this answer
    
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. –  Laegato Jan 3 '13 at 22:37
    
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. –  Josh Petrie Jan 3 '13 at 22:39
    
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 –  Laegato Jan 3 '13 at 22:43
    
Or just turn on "copy to output directory"... –  David Lively Mar 4 '13 at 22:38
    
"Copy to Output Directory" is great if you're using a project type / VS version combination that supports it, but unfortunately, not all do. :( –  Josh Petrie Mar 5 '13 at 2:20
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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