简体   繁体   中英

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

I'm doing a few shadereffects in a wpf_c# project and i don't know and i didn't find how to add the bytecode pixelshader (.ps) as Resource after be compiled by a target/exec. This is my csproj code fragment:

<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\10\bin\10.0.18362.0\x64\fxc.exe&quot; %(PixelShader.Identity) /T ps_3_0 /E main /Fo%(PixelShader.RelativeDir)%(PixelShader.Filename).ps" />
</Target>

Everything goes fine and the.ps files are correctly generated, as example:

PixelShaderCompile:   
"C:\Program Files (x86)\Windows Kits\10\bin\10.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"...

But now i dont know how to add that.ps file as 'Resource' during the compilation. Any one knows how? I didn't find any clear documentation.

After 3-4 hours of trial-error i found a (i think dirty) way to do it: the msbuild (.csproj) looks like:

<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>

That's it... So hard because there aren't so many made examples of msbuild files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM