我正在尝试学习 HLSL 以与 Vulkan 一起使用,但我似乎无法构建我的程序。我使用的是 Visual Studio 2022,错误列表显示“MSB6006“dxc.exe”已退出,代码为 1。”
检查项目的日志文件后,错误扩展为:
FXC : warning : no output provided for debug - embedding PDB in shader container. Use -Qembed_debug to silence this warning.
vertex_frag.hlsl(3,7): warning G8A84224D: 'location' attribute ignored [-Wignored-attributes]
[[vk::location(0)]] float4 Color : COLOR0;
^
vertex_frag.hlsl(4,7): warning G8A84224D: 'location' attribute ignored [-Wignored-attributes]
[[vk::location(1)]] float2 UVcoord : TEXCOORD0;
^
vertex_frag.hlsl(12,7): warning G8A84224D: 'location' attribute ignored [-Wignored-attributes]
[[vk::location(0)]] float4 Color : COLOR0;
^
FXC : warning : semantic 'COLOR0' on field overridden by function or enclosing type [-Winline-asm]
dxc failed : Target profile argument is missing
我的 HLSL 顶点着色器代码是:
struct VSInput {
[[vk::location(0)]] float4 Position : POSITION0;
[[vk::location(1)]] float4 Color : COLOR0;
[[vk::location(2)]] float2 UVcoord : TEXCOORD0;
};
struct Camera {
float4x4 view;
float4x4 proj;
float3 position;
};
struct UBO {
float dt;
float4x4 model;
Camera cam;
};
cbuffer ubo : register(b0, space0) { UBO ubo; }
struct VSOutput {
float4 Position : SV_POSITION;
[[vk::location(0)]] float4 Color : COLOR0;
[[vk::location(1)]] float2 UVcoord : TEXCOORD0;
};
VSOutput main(VSInput input, uint VertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput) 0;
float4x4 camMatrix = mul(ubo.cam.proj, ubo.cam.view);
output.Position = mul(camMatrix, mul(ubo.model, input.Position));
output.Color = input.Color;
output.UVcoord = input.UVcoord;
return output;
}
我的 HLSL prixel/fragment 着色器代码是:
struct PSInput {
float4 Position : SV_Position;
[[vk::location(0)]] float4 Color : COLOR0;
[[vk::location(1)]] float2 UVcoord : TEXCOORD0;
};
struct PSOutput {
[[vk::location(0)]] float4 Color : COLOR0;
};
PSOutput main(PSInput input) : SV_TARGET
{
PSOutput output = (PSOutput) 0;
output.Color = input.Color;
return output;
}
我读到,HLSL 中有一个隐式 Vulkan 命名空间,它添加了
[[vk::location(#)]]
属性等,但我没有在项目中的任何位置包含 Vulkan 命名空间。我希望命名空间的隐式性质只是超出了我的理解范围,但这并不能解释“缺少目标配置文件”错误、“忽略位置属性”错误或覆盖错误。
Khronos Group Vulkan-Guide Github 中有一个名为“使用库进行运行时编译”的部分,它提供了在运行时将 HLSL 编译为 SPIR-V 的代码。但是,目前我只是尝试构建项目,而不必编译 HLSL 文件。我是否遗漏了某些内容,或者我只需要包含此代码就可以构建项目?
在评论者的帮助下,我找到了错误的根源。由于我只是测试
.hlsl
文件的功能,因此我没有将任何资源绑定到着色器,因此在尝试查找绑定资源时构建失败。要解决此问题,请转到 Project Properties -> HLSL Compiler -> General,找到 All Resources Bound 字段,并将其设置为 No
。再次构建项目后,剩下的唯一错误是:
dxc failed : Target profile argument is missing
要修复此错误,请确保您的
.hlsl
文件属性已正确配置。右键单击 .hlsl
文件并选择 Properties -> HLSL Compiler -> General。这次,找到 Shader Type 字段并使用下拉菜单将其设置为适当的着色器类型。
完成所有这些步骤后,我的项目构建成功,并且没有遇到任何错误。