Skip to content

Conversation

@Nielsbishere
Copy link
Contributor

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

NielsbishereAlt and others added 3 commits May 12, 2025 14:45
…t; avoids pixel shader from inverting SV_POSITION.y in a lib file
@Nielsbishere
Copy link
Contributor Author

Extra context:

//dxc -T lib_6_3 test.hlsl -spirv -fvk-invert-y

[shader("vertex")]
float4 test(float4 a : A) : SV_POSITION {
    return a;
}

Before fix (can't enable -fvk-invert-y without an error, so would end up like this without the flag):

; SPIR-V
; Version: 1.0
; Generator: Google spiregg; 0
; Bound: 12
; Schema: 0
               OpCapability Shader
               OpMemoryModel Logical GLSL450
               OpEntryPoint Vertex %test "test" %in_var_A %gl_Position
               OpSource HLSL 630
               OpName %in_var_A "in.var.A"
               OpName %test "test"
               OpDecorate %gl_Position BuiltIn Position
               OpDecorate %in_var_A Location 0
      %float = OpTypeFloat 32
    %v4float = OpTypeVector %float 4
%_ptr_Input_v4float = OpTypePointer Input %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
       %void = OpTypeVoid
          %9 = OpTypeFunction %void
   %in_var_A = OpVariable %_ptr_Input_v4float Input
%gl_Position = OpVariable %_ptr_Output_v4float Output
       %test = OpFunction %void None %9
         %10 = OpLabel
         %11 = OpLoad %v4float %in_var_A
               OpStore %gl_Position %11
               OpReturn
               OpFunctionEnd

After fix:

; SPIR-V
; Version: 1.0
; Generator: Google spiregg; 0
; Bound: 15
; Schema: 0
               OpCapability Shader
               OpMemoryModel Logical GLSL450
               OpEntryPoint Vertex %test "test" %in_var_A %gl_Position
               OpSource HLSL 630
               OpName %in_var_A "in.var.A"
               OpName %test "test"
               OpDecorate %gl_Position BuiltIn Position
               OpDecorate %in_var_A Location 0
      %float = OpTypeFloat 32
    %v4float = OpTypeVector %float 4
%_ptr_Input_v4float = OpTypePointer Input %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
       %void = OpTypeVoid
          %9 = OpTypeFunction %void
   %in_var_A = OpVariable %_ptr_Input_v4float Input
%gl_Position = OpVariable %_ptr_Output_v4float Output
       %test = OpFunction %void None %9
         %10 = OpLabel
         %11 = OpLoad %v4float %in_var_A
         %12 = OpCompositeExtract %float %11 1
         %13 = OpFNegate %float %12
         %14 = OpCompositeInsert %v4float %13 %11 1
               OpStore %gl_Position %14
               OpReturn
               OpFunctionEnd

Copy link
Collaborator

@llvm-beanz llvm-beanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need a test case, but the code change looks reasonable to me.

@sperron can you or someone from your team take a look at this?

@s-perron
Copy link
Collaborator

I need to look at this carefully. I have some corner cases in mind, and I need to double check if they work.

Copy link
Collaborator

@s-perron s-perron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing this is a reasonable implementation. As mentioned above add a test. Include multiple shaders in the test. Try to show one shader inverting y while another does not.

I have one concern about usability, but that is outside of the scope of this PR. You could run into trouble if you have a VS->GS->PS pipeline. If the VS and GS are both compiled with invert-y, then they will both invert it and cancel each other out, right? Just something to watch out for.

@Nielsbishere
Copy link
Contributor Author

Nielsbishere commented May 13, 2025

Thanks for contributing this is a reasonable implementation. As mentioned above add a test. Include multiple shaders in the test. Try to show one shader inverting y while another does not.

I have one concern about usability, but that is outside of the scope of this PR. You could run into trouble if you have a VS->GS->PS pipeline. If the VS and GS are both compiled with invert-y, then they will both invert it and cancel each other out, right? Just something to watch out for.

Yes that's actually a big issue, I guess that is an issue already in some pipelines using the old way (like mine) where each shader is compiled with this flag and it actually doesn't make sense in case GS/DS is used. I'm not sure what the right solution would be though; if you only flip VS/MS then you might miss other cases. The only thing I can think of is an annotation on the entrypoint that tells it that you don't want to flip it.
EDIT: Now that I think about it, this can only be solved by the one linking the final version, as VS -> ... -> PS you would want the last stage to flip it and from the compiler you can't know this. But that wouldn't work if lib files are arbitrarily linked, unless the user manually flips these at the right moment.

…nly supported on GS/VS/DS/MS already. Added test case for fvk-invert-y for lib files
@github-actions
Copy link
Contributor

github-actions bot commented May 14, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@Nielsbishere
Copy link
Contributor Author

After another look, it seems like DS/GS/MS/VS aren't the only ones capable of writing to SV_POSITION. I'm not entirely sure if this means HS is excluded on purpose or by accident. But I made it behave as before.

@s-perron s-perron merged commit fef2f94 into microsoft:main May 16, 2025
12 checks passed
@github-project-automation github-project-automation bot moved this from New to Done in HLSL Roadmap May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants