【问题标题】:How to add custom compiled file (in Target/Exec) as resource in .csproj's?如何在 .csproj 中添加自定义编译文件(在 Target/Exec 中)作为资源?
【发布时间】:2019-10-29 01:51:05
【问题描述】:

我在 wpf_c# 项目中做一些着色器效果,我不知道,也没有找到如何在由目标/执行程序编译后将字节码像素着色器 (.ps) 添加为资源。这是我的 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\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>

一切正常,正确生成 .ps 文件,例如:

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

但现在我不知道如何在编译期间将该 .ps 文件添加为“资源”。有谁知道怎么做?我没有找到任何明确的文档。

【问题讨论】:

    标签: c# msbuild csproj


    【解决方案1】:

    经过 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 文件示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多