Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the clip HLSL Function #99093

Closed
12 tasks
Tracked by #99235
farzonl opened this issue Jul 16, 2024 · 4 comments · Fixed by #114588
Closed
12 tasks
Tracked by #99235

Implement the clip HLSL Function #99093

farzonl opened this issue Jul 16, 2024 · 4 comments · Fixed by #114588
Assignees
Labels
backend:DirectX backend:SPIR-V bot:HLSL clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.

Comments

@farzonl
Copy link
Member

farzonl commented Jul 16, 2024

  • Implement clip clang builtin,
  • Link clip clang builtin with hlsl_intrinsics.h
  • Add sema checks for clip to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for clip to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/clip.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/clip-errors.hlsl
  • Create the int_dx_clip intrinsic in IntrinsicsDirectX.td
  • Create the DXILOpMapping of int_dx_clip to 82 in DXIL.td
  • Create the clip.ll and clip_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create the int_spv_clip intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the clip lowering and map it to int_spv_clip in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/clip.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
82 Discard 6.0 ('pixel',)

SPIR-V

OpDemoteToHelperInvocation:

Description:

(OpDemoteToHelperInvocationEXT)**

Demote this fragment shader invocation to a helper
invocation. Any stores to memory after this instruction are suppressed
and the fragment does not write outputs to the framebuffer.

Unlike the OpTerminateInvocation
instruction, this does not necessarily terminate the invocation which
might be needed for derivative calculations. It is not considered a flow
control instruction (flow control does not become non-uniform) and does
not terminate the block. The implementation may terminate helper
invocations before the end of the shader as an optimization, but doing
so must not affect derivative calculations and does not make control
flow non-uniform.

After an invocation executes this instruction, any subsequent load of
HelperInvocation within that invocation will load an undefined value
unless the HelperInvocation built-in variable is
decorated with Volatile or the load included
Volatile in its Memory Operands

This instruction is only valid in the Fragment Execution
Model
.

Capability:
DemoteToHelperInvocation

Missing before version 1.6.

Word Count Opcode Results Operands
1 5380

Test Case(s)

Example 1

//dxc clip_test.hlsl -T ps_6_8

RWBuffer<float4> Buf;

float4 main( ) : SV_Target {
  float4 p1 = Buf[0];
  clip(p1.a);
  return p1;
}

HLSL:

Discards the current pixel if the specified value is less than zero.

clip(x)

Parameters

Item Description
x
[in] The specified value.

Return Value

None.

Remarks

Use the clip HLSL intrinsic function to simulate clipping planes if each component of the x parameter represents the distance from a plane.

Also, use the clip function to test for alpha behavior, as shown in the following example:

clip( Input.Color.A < 0.1f ? -1:1 );

Type Description

Name Template Type Component Type Size
x scalar, vector, or matrix float any

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 4 yes (pixel shader only)
Shader Model 3 (DirectX HLSL) yes (pixel shader only)
Shader Model 2 (DirectX HLSL) yes (pixel shader only)
Shader Model 1 (DirectX HLSL) yes (pixel shader only)

See also

Intrinsic Functions (DirectX HLSL)

@llvm-beanz
Copy link
Collaborator

llvm-beanz commented Aug 6, 2024

Godbolt of DXC's behavior

Edit: Updated link with Vulkan 1.1 and 1.3 output.

@damyanp damyanp moved this to Ready in HLSL Support Oct 9, 2024
@joaosaffran
Copy link
Contributor

I can work on this

@joaosaffran joaosaffran self-assigned this Oct 28, 2024
@damyanp damyanp moved this from Ready to Active in HLSL Support Oct 31, 2024
@joaosaffran joaosaffran moved this from Active to Needs Review in HLSL Support Nov 4, 2024
@github-project-automation github-project-automation bot moved this from Needs Review to Closed in HLSL Support Nov 15, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:codegen IR generation bugs: mangling, exceptions, etc. labels Nov 15, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 15, 2024

@llvm/issue-subscribers-clang-codegen

Author: Farzon Lotfi (farzonl)

- [ ] Implement `clip` clang builtin, - [ ] Link `clip` clang builtin with `hlsl_intrinsics.h` - [ ] Add sema checks for `clip` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp` - [ ] Add codegen for `clip` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp` - [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/clip.hlsl` - [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/clip-errors.hlsl` - [ ] Create the `int_dx_clip` intrinsic in `IntrinsicsDirectX.td` - [ ] Create the `DXILOpMapping` of `int_dx_clip` to `82` in `DXIL.td` - [ ] Create the `clip.ll` and `clip_errors.ll` tests in `llvm/test/CodeGen/DirectX/` - [ ] Create the `int_spv_clip` intrinsic in `IntrinsicsSPIRV.td` - [ ] In SPIRVInstructionSelector.cpp create the `clip` lowering and map it to `int_spv_clip` in `SPIRVInstructionSelector::selectIntrinsic`. - [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/clip.ll`

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
82 Discard 6.0 ('pixel',)

SPIR-V

OpDemoteToHelperInvocation:

Description:

(OpDemoteToHelperInvocationEXT)**

Demote this fragment shader invocation to a helper
invocation. Any stores to memory after this instruction are suppressed
and the fragment does not write outputs to the framebuffer.

Unlike the OpTerminateInvocation
instruction, this does not necessarily terminate the invocation which
might be needed for derivative calculations. It is not considered a flow
control instruction (flow control does not become non-uniform) and does
not terminate the block. The implementation may terminate helper
invocations before the end of the shader as an optimization, but doing
so must not affect derivative calculations and does not make control
flow non-uniform.

After an invocation executes this instruction, any subsequent load of
HelperInvocation within that invocation will load an undefined value
unless the HelperInvocation built-in variable is
decorated with Volatile or the load included
Volatile in its Memory Operands

This instruction is only valid in the Fragment Execution
Model
.

Capability:
DemoteToHelperInvocation

Missing before version 1.6.

Word Count Opcode Results Operands
1 5380

Test Case(s)

Example 1

//dxc clip_test.hlsl -T ps_6_8

RWBuffer&lt;float4&gt; Buf;

float4 main( ) : SV_Target {
  float4 p1 = Buf[0];
  clip(p1.a);
  return p1;
}

HLSL:

Discards the current pixel if the specified value is less than zero.

clip(x)

Parameters

Item Description
<span id="x"></span><span id="X"></span>x<br/> [in] The specified value.<br/>

Return Value

None.

Remarks

Use the clip HLSL intrinsic function to simulate clipping planes if each component of the x parameter represents the distance from a plane.

Also, use the clip function to test for alpha behavior, as shown in the following example:

clip( Input.Color.A &lt; 0.1f ? -1:1 );

Type Description

Name Template Type Component Type Size
x scalar, vector, or matrix float any

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 4 yes (pixel shader only)
Shader Model 3 (DirectX HLSL) yes (pixel shader only)
Shader Model 2 (DirectX HLSL) yes (pixel shader only)
Shader Model 1 (DirectX HLSL) yes (pixel shader only)

See also

<dl> <dt>

Intrinsic Functions (DirectX HLSL)
</dt> </dl>

@llvmbot
Copy link
Member

llvmbot commented Nov 15, 2024

@llvm/issue-subscribers-clang-frontend

Author: Farzon Lotfi (farzonl)

- [ ] Implement `clip` clang builtin, - [ ] Link `clip` clang builtin with `hlsl_intrinsics.h` - [ ] Add sema checks for `clip` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp` - [ ] Add codegen for `clip` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp` - [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/clip.hlsl` - [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/clip-errors.hlsl` - [ ] Create the `int_dx_clip` intrinsic in `IntrinsicsDirectX.td` - [ ] Create the `DXILOpMapping` of `int_dx_clip` to `82` in `DXIL.td` - [ ] Create the `clip.ll` and `clip_errors.ll` tests in `llvm/test/CodeGen/DirectX/` - [ ] Create the `int_spv_clip` intrinsic in `IntrinsicsSPIRV.td` - [ ] In SPIRVInstructionSelector.cpp create the `clip` lowering and map it to `int_spv_clip` in `SPIRVInstructionSelector::selectIntrinsic`. - [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/clip.ll`

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
82 Discard 6.0 ('pixel',)

SPIR-V

OpDemoteToHelperInvocation:

Description:

(OpDemoteToHelperInvocationEXT)**

Demote this fragment shader invocation to a helper
invocation. Any stores to memory after this instruction are suppressed
and the fragment does not write outputs to the framebuffer.

Unlike the OpTerminateInvocation
instruction, this does not necessarily terminate the invocation which
might be needed for derivative calculations. It is not considered a flow
control instruction (flow control does not become non-uniform) and does
not terminate the block. The implementation may terminate helper
invocations before the end of the shader as an optimization, but doing
so must not affect derivative calculations and does not make control
flow non-uniform.

After an invocation executes this instruction, any subsequent load of
HelperInvocation within that invocation will load an undefined value
unless the HelperInvocation built-in variable is
decorated with Volatile or the load included
Volatile in its Memory Operands

This instruction is only valid in the Fragment Execution
Model
.

Capability:
DemoteToHelperInvocation

Missing before version 1.6.

Word Count Opcode Results Operands
1 5380

Test Case(s)

Example 1

//dxc clip_test.hlsl -T ps_6_8

RWBuffer&lt;float4&gt; Buf;

float4 main( ) : SV_Target {
  float4 p1 = Buf[0];
  clip(p1.a);
  return p1;
}

HLSL:

Discards the current pixel if the specified value is less than zero.

clip(x)

Parameters

Item Description
<span id="x"></span><span id="X"></span>x<br/> [in] The specified value.<br/>

Return Value

None.

Remarks

Use the clip HLSL intrinsic function to simulate clipping planes if each component of the x parameter represents the distance from a plane.

Also, use the clip function to test for alpha behavior, as shown in the following example:

clip( Input.Color.A &lt; 0.1f ? -1:1 );

Type Description

Name Template Type Component Type Size
x scalar, vector, or matrix float any

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 4 yes (pixel shader only)
Shader Model 3 (DirectX HLSL) yes (pixel shader only)
Shader Model 2 (DirectX HLSL) yes (pixel shader only)
Shader Model 1 (DirectX HLSL) yes (pixel shader only)

See also

<dl> <dt>

Intrinsic Functions (DirectX HLSL)
</dt> </dl>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:DirectX backend:SPIR-V bot:HLSL clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.
Projects
Status: Closed
Development

Successfully merging a pull request may close this issue.

5 participants