Skip to content

Commit

Permalink
Add WGSL pack/unpack intrinsics
Browse files Browse the repository at this point in the history
This addresses issue shader-slang#5080.
  • Loading branch information
aleino-nv committed Sep 17, 2024
1 parent 2994aac commit bebfac8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
23 changes: 14 additions & 9 deletions source/slang/hlsl.meta.slang
Original file line number Diff line number Diff line change
Expand Up @@ -7275,12 +7275,13 @@ vector<T, 3> cross(vector<T, 3> left, vector<T, 3> right)

// Convert encoded color
[__readNone]
[require(cpp_cuda_glsl_hlsl_metal_spirv, sm_4_0_version)]
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, sm_4_0_version)]
int4 D3DCOLORtoUBYTE4(float4 color)
{
__target_switch
{
case hlsl: __intrinsic_asm "D3DCOLORtoUBYTE4";
case wgsl: __intrinsic_asm "bitcast<vec4i>(pack4x8unorm($0)).zyxw";
default:
let scaled = color.zyxw * 255.001999f;
return int4(scaled);
Expand Down Expand Up @@ -8030,7 +8031,7 @@ vector<T,N> exp10(vector<T,N> x)
__glsl_version(420)
__cuda_sm_version(6.0)
[__readNone]
[require(cpp_cuda_glsl_hlsl_metal_spirv, shader5_sm_5_0)]
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, shader5_sm_5_0)]
float f16tof32(uint value)
{
__target_switch
Expand All @@ -8048,12 +8049,13 @@ float f16tof32(uint value)
result:$$float = OpFConvert %half
};
}
case wgsl: __intrinsic_asm "unpack2x16float($0).x";
}
}

__generic<let N : int>
[__readNone]
[require(cpp_cuda_glsl_hlsl_metal_spirv, shader5_sm_5_0)]
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, shader5_sm_5_0)]
vector<float, N> f16tof32(vector<uint, N> value)
{
__target_switch
Expand All @@ -8078,7 +8080,7 @@ vector<float, N> f16tof32(vector<uint, N> value)
__glsl_version(420)
__cuda_sm_version(6.0)
[__readNone]
[require(cpp_cuda_glsl_hlsl_metal_spirv, shader5_sm_5_0)]
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, shader5_sm_5_0)]
uint f32tof16(float value)
{
__target_switch
Expand All @@ -8096,12 +8098,13 @@ uint f32tof16(float value)
result:$$uint = OpUConvert %lowBits
};
}
case wgsl: __intrinsic_asm "pack2x16float(vec2f($0,0.0))";
}
}

__generic<let N : int>
[__readNone]
[require(cpp_cuda_glsl_hlsl_metal_spirv, shader5_sm_5_0)]
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, shader5_sm_5_0)]
vector<uint, N> f32tof16(vector<float, N> value)
{
__target_switch
Expand All @@ -8127,7 +8130,7 @@ vector<uint, N> f32tof16(vector<float, N> value)

__glsl_version(420)
[__readNone]
[require(cpp_cuda_glsl_hlsl_metal_spirv, shader5_sm_5_0)]
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, shader5_sm_5_0)]
float f16tof32(float16_t value)
{
__target_switch
Expand All @@ -8143,12 +8146,13 @@ float f16tof32(float16_t value)
result:$$float = OpFConvert $value
};
}
case wgsl: __intrinsic_asm "f32($0)";
}
}

__generic<let N : int>
[__readNone]
[require(cpp_cuda_glsl_hlsl_metal_spirv, shader5_sm_5_0)]
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, shader5_sm_5_0)]
vector<float, N> f16tof32(vector<float16_t, N> value)
{
__target_switch
Expand All @@ -8167,7 +8171,7 @@ vector<float, N> f16tof32(vector<float16_t, N> value)
// Convert to float16_t
__glsl_version(420)
[__readNone]
[require(cuda_glsl_metal_spirv, shader5_sm_5_0)]
[require(cuda_glsl_metal_spirv_wgsl, shader5_sm_5_0)]
float16_t f32tof16_(float value)
{
__target_switch
Expand All @@ -8178,12 +8182,13 @@ float16_t f32tof16_(float value)
case spirv: return spirv_asm {
OpFConvert $$float16_t result $value
};
case wgsl: __intrinsic_asm "f16($0)";
}
}

__generic<let N : int>
[__readNone]
[require(cuda_glsl_metal_spirv, shader5_sm_5_0)]
[require(cuda_glsl_metal_spirv_wgsl, shader5_sm_5_0)]
vector<float16_t, N> f32tof16_(vector<float, N> value)
{
__target_switch
Expand Down
4 changes: 4 additions & 0 deletions source/slang/slang-capabilities.capdef
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ alias cuda_glsl_spirv = cuda | glsl | spirv;
/// [Compound]
alias cuda_glsl_metal_spirv = cuda | glsl | metal | spirv;

/// CUDA, GLSL, Metal, SPIRV and WGSL code-gen targets
/// [Compound]
alias cuda_glsl_metal_spirv_wgsl = cuda | glsl | metal | spirv | wgsl;

/// CUDA, and HLSL code-gen targets
/// [Compound]
alias cuda_hlsl = cuda | hlsl;
Expand Down
18 changes: 18 additions & 0 deletions source/slang/slang-emit-wgsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,4 +1002,22 @@ void WGSLSourceEmitter::emitFrontMatterImpl(TargetRequest* /* targetReq */)
}
}

void WGSLSourceEmitter::emitIntrinsicCallExprImpl(
IRCall* inst,
UnownedStringSlice intrinsicDefinition,
IRInst* intrinsicInst,
EmitOpInfo const& inOuterPrec
)
{
// The f16 constructor is generated for f32tof16
if (intrinsicDefinition.startsWith("f16"))
{
m_f16ExtensionEnabled = true;
}

CLikeSourceEmitter::emitIntrinsicCallExprImpl(
inst, intrinsicDefinition, intrinsicInst, inOuterPrec
);
}

} // namespace Slang
7 changes: 7 additions & 0 deletions source/slang/slang-emit-wgsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class WGSLSourceEmitter : public CLikeSourceEmitter
IRInst* inst, const EmitOpInfo& outerPrec
) SLANG_OVERRIDE;

virtual void emitIntrinsicCallExprImpl(
IRCall* inst,
UnownedStringSlice intrinsicDefinition,
IRInst* intrinsicInst,
EmitOpInfo const& inOuterPrec
) SLANG_OVERRIDE;

void emit(const AddressSpace addressSpace);

private:
Expand Down

0 comments on commit bebfac8

Please sign in to comment.