Skip to content

Commit fef2f94

Browse files
Fix -fvk-invert-y (#7447)
#7446 This fixes some outdated documentation as well as a compile error when enabling fvk-invert-y on lib files and makes sure that it only gets enabled on SV_POSITION that is used in VS/GS/DS/MS (so PS doesn't get caught in the crossfire). Also tested the dx-position-w one and that one already has correct behavior here. --------- Co-authored-by: NielsbishereAlt <[email protected]>
1 parent 7054e52 commit fef2f94

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

docs/SPIR-V.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4227,7 +4227,7 @@ codegen for Vulkan:
42274227
- ``-fvk-use-dx-layout``: Uses DirectX layout rules for resources.
42284228
- ``-fvk-invert-y``: Negates (additively inverts) SV_Position.y before writing
42294229
to stage output. Used to accommodate the difference between Vulkan's
4230-
coordinate system and DirectX's. Only allowed in VS/DS/GS.
4230+
coordinate system and DirectX's. Only allowed in VS/DS/GS/MS/Lib.
42314231
- ``-fvk-use-dx-position-w``: Reciprocates (multiplicatively inverts)
42324232
SV_Position.w after reading from stage input. Used to accommodate the
42334233
difference between Vulkan DirectX: the w component of SV_Position in PS is

include/dxc/Support/HLSLOptions.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def fvk_bind_register : MultiArg<["-"], "fvk-bind-register", 4>, MetaVarName<"<t
368368
HelpText<"Specify Vulkan descriptor set and binding for a specific register">;
369369
def vkbr : MultiArg<["-"], "vkbr", 4>, Flags<[CoreOption, DriverOption]>, Alias<fvk_bind_register>;
370370
def fvk_invert_y: Flag<["-"], "fvk-invert-y">, Group<spirv_Group>, Flags<[CoreOption, DriverOption]>,
371-
HelpText<"Negate SV_Position.y before writing to stage output in VS/DS/GS to accommodate Vulkan's coordinate system">;
371+
HelpText<"Negate SV_Position.y before writing to stage output in VS/DS/GS/MS/Lib to accommodate Vulkan's coordinate system">;
372372
def fvk_use_dx_position_w: Flag<["-"], "fvk-use-dx-position-w">, Group<spirv_Group>, Flags<[CoreOption, DriverOption]>,
373373
HelpText<"Reciprocate SV_Position.w after reading from stage input in PS to accommodate the difference between Vulkan and DirectX">;
374374
def fvk_support_nonzero_base_instance: Flag<["-"], "fvk-support-nonzero-base-instance">, Group<spirv_Group>, Flags<[CoreOption, DriverOption]>,

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,8 @@ SpirvEmitter::SpirvEmitter(CompilerInstance &ci)
604604
emitError("unknown shader module: %0", {}) << shaderModel->GetName();
605605

606606
if (spirvOptions.invertY && !shaderModel->IsVS() && !shaderModel->IsDS() &&
607-
!shaderModel->IsGS() && !shaderModel->IsMS())
608-
emitError("-fvk-invert-y can only be used in VS/DS/GS/MS", {});
607+
!shaderModel->IsGS() && !shaderModel->IsMS() && !shaderModel->IsLib())
608+
emitError("-fvk-invert-y can only be used in VS/DS/GS/MS/Lib", {});
609609

610610
if (spirvOptions.useGlLayout && spirvOptions.useDxLayout)
611611
emitError("cannot specify both -fvk-use-dx-layout and -fvk-use-gl-layout",
@@ -14964,8 +14964,12 @@ SpirvEmitter::createSpirvIntrInstExt(llvm::ArrayRef<const Attr *> attrs,
1496414964
SpirvInstruction *SpirvEmitter::invertYIfRequested(SpirvInstruction *position,
1496514965
SourceLocation loc,
1496614966
SourceRange range) {
14967-
// Negate SV_Position.y if requested
14968-
if (spirvOptions.invertY) {
14967+
// Negate SV_Position.y if requested and supported
14968+
14969+
bool supportsInvertY = spvContext.isVS() || spvContext.isGS() ||
14970+
spvContext.isDS() || spvContext.isMS();
14971+
14972+
if (spirvOptions.invertY && supportsInvertY) {
1496914973
const auto oldY = spvBuilder.createCompositeExtract(
1497014974
astContext.FloatTy, position, {1}, loc, range);
1497114975
const auto newY = spvBuilder.createUnaryOp(
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %dxc -T lib_6_3 -fvk-invert-y -fcgl %s -spirv | FileCheck %s
2+
3+
[shader("vertex")]
4+
float4 main(float4 a : A) : SV_Position {
5+
return a;
6+
}
7+
8+
// CHECK: [[a:%[0-9]+]] = OpFunctionCall %v4float %src_main %param_var_a
9+
// CHECK-NEXT: [[oldY:%[0-9]+]] = OpCompositeExtract %float [[a]] 1
10+
// CHECK-NEXT: [[newY:%[0-9]+]] = OpFNegate %float [[oldY]]
11+
// CHECK-NEXT: [[pos:%[0-9]+]] = OpCompositeInsert %v4float [[newY]] [[a]] 1
12+
// CHECK-NEXT: OpStore %gl_Position [[pos]]

0 commit comments

Comments
 (0)