How to compile hlsl shaders multiple times in visual studio?
我在我的Visual Studio项目中添加了HLSL着色器,并且在构建过程中将它们自动编译为cso文件。我面临的问题是,我需要使用不同的Shader模型多次编译它们,因为" / 4_0_level_9_3"在DirectX 11上有效,但在DirectX 9上无效,而" / 3_0"仅在DirectX 9上有效,而不适用于DirectX 9。在DirectX 11上
向Visual Studio项目中多次添加相同的文件是行不通的,我想避免复制HLSL源文件,因为它会增加维护工作量和潜在的错误原因。我也不能在Visual Studio中创建单独的目标/配置来实现此目的,因为我需要在可执行文件中同时支持两个DirectX版本(在运行时可切换)。
是否可以在Visual Studio中为单个HLSL文件指定多个编译(不同的着色器模型和不同的输出文件)?
将着色器代码放入
文件1(设置为从构建中排除)
1 2 3 4 5 6 7 8 9 10 11 | // MyShader.hlsli PS_INPUT VertexShader( VS_INPUT input ) { ... } float4 PixelShader( PS_INPUT input) { ... } |
文件2(设置为构建为Vertex Shader,Shader Model 9_3,入口点VertexShader)
1 2 3 | // MyShader_VS.hlsl #include"MyShader.hlsl" |
文件3(设置为构建为Pixel Shader,Shader Model 9_3,入口点PixelShader)
1 2 3 | // MyShader_PS.hlsl #include"MyShader.hlsl" |
文件4(设置为构建为Vertex Shader,Shader Model 4.0,入口点VertexShader)
1 2 3 | // MyShader_VS4.hlsl #include"MyShader.hlsl" |
文件5(设置为构建为Pixel Shader,Shader Model 4.0,入口点PixelShader)
1 2 3 | // MyShader_PS4.hlsl #include"MyShader.hlsl" |
请注意,通过使用文本编辑器手动编辑
1 2 3 4 5 6 7 8 9 10 | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <Link> ... </Link> <ClCompile> ... </ClCompile> <FXCompile> <ShaderModel>4.0_level_9_3</ShaderModel> </FXCompile> |
Unless you are specifically targeting Windows XP systems, there's not much value in using the legacy Direc3D 9 API at all. Just use DirectX 11 Feature Level 9.3+ to target Shader Model 2.0/3.0 era video cards. The main value of Shader Model 3.0 was vertex texture fetch which was never implemented by one of the major vendors anyhow. The
4_0_level_9_3 profile already builds the shader as both 2.0 and 4.0 in the same blob.