# How to compile shader files in UWP

I wish to use this method

byte[] vertexShaderByteCode = ShaderBytecode.CompileFromFile(".......Transf_VS.hlsl", "VS", "vs_5_0");
device,
);


But it throws exception of either file not found or file cannot be open.

Is there any solution to this? I saw HelixToolkit using shader files that has already been compiled, how is it done then? https://github.com/helix-toolkit/helix-toolkit/blob/master/Source/HelixToolkit.UWP/Model/ExampleCube3D.cs

// Loads vertex shader bytecode


Thank you

• Have you checked that the filepath is valid? – Tyyppi_77 Nov 8 '16 at 15:21
• I have modified it several times, and sometimes it says cannot open instead of cannot find, so I know that's when it's valid – 王凯越 Kaiyue Wang Nov 9 '16 at 0:47
• gamedev.stackexchange.com/questions/132838/… Here is a possible solution to the problem, but there is still no display so I'm not sure whether the problem is finally solved. Please check this latter question and share your view – 王凯越 Kaiyue Wang Nov 10 '16 at 3:57

I was under the impression you had to have precompiled shader files under UWP. There is a tool, and info in the MSDN site for compiling files.

Apologies if this is not completely correct, more here say on my front.

https://msdn.microsoft.com/en-us/library/windows/desktop/bb509633(v=vs.85).aspx#compiling_at_build_time_to_object_files

• Your answer sounds a little bit confusing, and may have been better as a comment. Do you meant to say you heard this is true, but apart from that, have no real idea if it is? – Gnemlock Nov 9 '16 at 23:26
• gamedev.stackexchange.com/questions/132838/… Here is a possible solution to the problem, but there is still no display so I'm not sure whether the problem is finally solved. Please check this latter question and share your view – 王凯越 Kaiyue Wang Nov 10 '16 at 3:55

Here's a solution without explanation, it should apply for UWP apps:

I manually put the shader files in a directory like this

System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "ProjectName")


which in my case would be

F:\*****\SolutionFolder\StartupProjectFolder\bin\x64\Debug\AppX\FolderWithCurrentProjectName


Then the compiler can read the shader files, it works for either fx. files(hlsl source code) and fxo. files(compiled before the whole solution with another program the fxc.exe). The second method can use the guidance fromhttp://stackoverflow.com/questions/23293760/creating-fxo-shader-file-using-fxc-exe

There are other complications that one might want to notice

1. the visual studio create txt. files with UTF-8 coding, you may want to check out in ASCII coding to remove the chars at the beginning of a file, which may cause problem.
2. fxc.exe can be found for example in this directory

C:\Program Files (x86)\Windows Kits\10\bin\x64

3. You'd better copy the fxc.exe into another folder to run it, the directory of the exe. file and source file would better not contain blanks in any of its folder names in the path.

The idea comes from the example made by HelixToolkit. You see there are lot of bumps in route and since few are doing sharpdx + uwp right now we have to sweep them ourselves. I hope this can help others who face the same problem, and of course I hope there are pros who can explain why this works and otherwise it doesn't.

Thank you.

If you look at my answer to another question I show the steps of setting Visual Studio to compile shaders with a separate C++ project, you could use it the same way but just have the output option in the HLSL compiler tab set to your folder, that way you can build your shaders in VS and if you read my full answer I have a link to a nice tool as well.