3

I'm trying to build pixel shader in a C# class library.

I have added fx file to the project. Since I can't find any suitable build action I tried to manually edit csproj:

<FxCompile Include="MyEffect.fx">
  <ShaderType>Pixel</ShaderType>
</FxCompile>

but when I try to locate the ps file, I get System.IO.IOException: Cannot locate resource 'MyEffect.ps'

here is the code that throws error:

 public class MyEffect : ShaderEffect
 {
   public MyEffect(){
     this.PixesShader = new Uri("pack://application:,,,/WpfControlLibrary1;component/MyEffect.ps");
   }
 }

How to setup my project so that I can locate the compiled shader?

All the tutorials I have found are for C++ projects or deals with old tools like custom build task, but I think that is not required since VS2012.

EDIT: Visual Studio does compile .fx files only in C++ projects. In C# projects it does not work. It is mentioneded here: http://blogs.msdn.com/b/chuckw/archive/2012/05/07/hlsl-fxc-and-d3dcompile.aspx

Liero
  • 19,054
  • 16
  • 100
  • 195
  • Any file added to a cs project will have a property called `Build Action`, it should be `Resource`. Looks like you edited it to use compile, which of course won't work. You should not edit the project file directly unless you understand thoroughly what you do. Your code looks fine, I think the problem is just because of `Build Action` set to `Compile`, which is wrong. – Hopeless Oct 08 '15 at 18:33
  • Well, I've set it to `FxCompile`, not `Compile`. I've noticed `FxCompile` in some cpp projects. Build Action `Resource` does not work, because the .fx file has to be compiled to .ps file first and that is added to resources. Notice that the resource name is `MyEffect.ps` not `MyEffect.fx` – Liero Oct 08 '15 at 19:16
  • 1
    What I told you to set its `Build Action` to `Resource` is the file `.ps`. BTW, I've even written a simple library containing just custom Effects and I know for sure that the `.ps` files should have `Build Action` set to `Resource`. What you have done to the csproject file should be rolled back (to be sure it does not affect what you do next). If possible try creating a new project again. – Hopeless Oct 08 '15 at 19:20
  • But I dont have the .ps file yet. So you are saying I have to compile the hlsl manually using fxc.exe? – Liero Oct 08 '15 at 19:32
  • yes, in fact I used `Shazzam` (supports HLSL editor and preview). So you want the VS to compile the HLSL source code? As far as I know VS 2012 cannot do that, not sure about newer versions. Or maybe it comes with some plugin helping do so. – Hopeless Oct 08 '15 at 19:35
  • [here](http://blogs.msdn.com/b/chuckw/archive/2012/05/07/hlsl-fxc-and-d3dcompile.aspx) the documentation says that fxc.exe is integrated to visual studio, but it works only with cpp projects, not C# – Liero Oct 08 '15 at 19:49
  • Setting it to Resource seems to help! That was not entirely obvious – jhaagsma May 17 '21 at 19:37

1 Answers1

4

You can set a Postbuild option to compile your fx files with fxc:

fxc /O0 /Fc /Zi /T  fx_5_0 /Fo myShader.fxo myShader.fx

But I compile all my shaders at runtime when developing. I'm not sure what C# library you are using but you can do this in both SharpDX and SlimDX:

ShaderBytecode.CompileFromFile(effectFile, "fx_5_0", ShaderFlags.None, EffectFlags.None);
Stefan Agartsson
  • 401
  • 2
  • 11