如何将自定义编译文件(在 Target/Exec 中)添加为 .csproj 中的资源?

How to add custom compiled file (in Target/Exec) as resource in .csproj's?

我在 wpf_c# 项目中做了一些着色效果,我不知道,也没有找到如何添加字节码像素着色器 (.ps) 作为资源被编译成target/exec。这是我的 csproj 代码片段:

<ItemGroup>
    <AvailableItemName Include="PixelShader"/>
</ItemGroup>
<ItemGroup>
    <PixelShader Include="Shaders\BlueToneShader.fx" />
    bla bla bla other shaders bla bla
</ItemGroup>
<Target Name="PixelShaderCompile" Condition="@(PixelShader)!=''" BeforeTargets="Build">
    <Exec Command="&quot;C:\Program Files (x86)\Windows Kits\bin.0.18362.0\x64\fxc.exe&quot; %(PixelShader.Identity) /T ps_3_0 /E main /Fo%(PixelShader.RelativeDir)%(PixelShader.Filename).ps" />
</Target>

一切顺利,.ps 文件已正确生成,例如:

PixelShaderCompile:   
"C:\Program Files (x86)\Windows Kits\bin.0.18362.0\x64\fxc.exe" Shaders\BlueToneShader.fx /T ps_3_0 /E main /FoShaders\BlueToneShader.ps
Microsoft (R) Direct3D Shader Compiler 10.1 Copyright (C) 2013 Microsoft. All rights reserved.

compilation object save succeeded; see "folder"...

但现在我不知道如何在编译期间将 .ps 文件添加为 'Resource'。任何人都知道如何?我没有找到任何明确的文档。

经过 3-4 小时的试错后,我找到了一种(我认为很肮脏的)方法:msbuild (.csproj) 看起来像:

<PropertyGroup>
    <BuildDependsOn>
       PixelShaderCompile
       $(BuildDependsOn)
    </BuildDependsOn>
</PropertyGroup>
<ItemGroup>
    <AvailableItemName Include="PixelShader">
       <Visible>true</Visible>
    </AvailableItemName>
</ItemGroup>
<ItemGroup>
    <PixelShader ... />
    ...
</ItemGroup>
<Target Name="PixelShaderCompile" Condition="@(PixelShader)!=''" BeforeTargets="BeforeBuild;BeforeRebuild">
    <MakeDir Directories="$(IntermediateOutputPath)%(PixelShader.RelativeDir)" Condition="!Exists('$(IntermediateOutputPath)%(PixelShader.RelativeDir)')" />  
    //You put your fxc.exe command here
    <Exec Command="&quot;C:\bla bla bla\fxc.exe&quot; %(PixelShader.Identity) /T ps_3_0 /E PSmain /O3 /Fo$(IntermediateOutputPath)%(PixelShader.RelativeDir)%(PixelShader.Filename).ps" Outputs="$(IntermediateOutputPath)%(PixelShader.RelativeDir)%(PixelShader.Filename).ps">
          <Output ItemName="CompiledPixelShader" TaskParameter="Outputs" />
    </Exec>
    <ItemGroup>
        <Resource Include="@(CompiledPixelShader)" />
    </ItemGroup>
</Target>
//If you want to clear the .ps generated file 
<Target Name="PixelShaderClean" Condition="@(PixelShader)!=''" AfterTargets="AfterBuild;AfterRebuild">
    <Delete Files="$(IntermediateOutputPath)%(PixelShader.RelativeDir)%(PixelShader.Filename).ps" />
</Target>

就是这样...很难,因为没有那么多 msbuild 文件的示例。