为什么 COLOR 作为输出语义在 ps_4_0 目标时不受支持,但在 vs_4_0 中受支持?

Why COLOR as an output semantic is not supported when targeting ps_4_0 but supported in vs_4_0?

以下代码为例

struct vsout
{
  float4 pos : position;
  float4 clr : color;
};

cbuffer dmy : register(b0)
{
  float4x4 mat;
};

vsout vsmain(
  float4 pos : position,
  float2 tex : texcoord,
  float4 clr : color)
{
  vsout o;
  o.pos = mul(mat,pos);
  o.clr = clr;
  return o;
}

struct psout
{
  float4 clr : color;
};

psout psmain(
  float4 clr : color )
{
  psout o;
  o.clr = clr;
  return o;
}

fxc /Evsmain /Tvs_4_0 demo.hlsl,确定
fxc /Epsmain /Tps_4_0 demo.hlsl,失败
将 psout 更改为

struct psout
{
   float4 clr : sv_target;
};

fxc /Epsmain /Tps_4_0 demo.hlsl,确定
根据此文档 https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics COLOR 是 d3d9 及更高版本的有效语义。我使用的 fxc 是 D3DCOMPILER_47.dll 。这是怎么发生的?非常感谢!!!

您正在构建时没有 /Gec 标志,因此 HLSL 编译器不允许 'backward compatibility' 行为。

"COLOR" 在顶点着色器的情况下是一个顶点语义,在大多数情况下是 'user-defined' 语义。

"COLOR" 也是绑定顶点着色器输出和像素着色器输入的有效 'user-defined' 语义。

为了严格兼容,您需要使用 SV_Target 作为像素着色器的输出。

理想情况下,您还可以使用 SV_Position 而不是 Position 作为您的顶点着色器输入,但您必须匹配您在输入布局中提供的任何内容。 VS和PS之间用什么也必须保持一致。

If you add /Gec to the pixel shader command-line then it will build because it supports the older Direct3D 9 style: fxc /Epsmain /Gec /Tps_4_0 demo.hlsl

Microsoft Docs