Add hlsl helper for reading other lanes via derivative swizzling

This commit is contained in:
baldurk
2025-03-14 12:04:02 +00:00
parent 2322e165de
commit 58cace21fb
5 changed files with 121 additions and 0 deletions
+115
View File
@@ -0,0 +1,115 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2025 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
float4 quadSwizzleHelper(float4 c0, uint quadLaneIndex, uint readIndex)
{
bool quadX = (quadLaneIndex & 1u) != 0u;
bool quadY = (quadLaneIndex & 2u) != 0u;
bool readX = ((readIndex & 1u) != 0u);
bool readY = ((readIndex & 2u) != 0u);
float4 sign_x = 1.0f;
sign_x.x = quadX ? -1.0f : 1.0f;
sign_x.y = quadX ? -1.0f : 1.0f;
sign_x.z = quadX ? -1.0f : 1.0f;
sign_x.w = quadX ? -1.0f : 1.0f;
float4 sign_y = 1.0f;
sign_y.x = quadY ? -1.0f : 1.0f;
sign_y.y = quadY ? -1.0f : 1.0f;
sign_y.z = quadY ? -1.0f : 1.0f;
sign_y.w = quadY ? -1.0f : 1.0f;
float4 c1 = c0 + (sign_x * ddx_fine(c0));
float4 c2 = c0 + (sign_y * ddy_fine(c0));
float4 c3 = c2 + (sign_x * ddx_fine(c2));
if(quadLaneIndex == readIndex)
return c0;
else if(readY == quadY)
return c1;
else if(readX == quadX)
return c2;
else
return c3;
}
// helpers for smaller vector sizes
float3 quadSwizzleHelper(float3 c0, uint quadLaneIndex, uint readIndex)
{
return quadSwizzleHelper(float4(c0, 0.0f), quadLaneIndex, readIndex).xyz;
}
float2 quadSwizzleHelper(float2 c0, uint quadLaneIndex, uint readIndex)
{
return quadSwizzleHelper(float4(c0, 0.0f, 0.0f), quadLaneIndex, readIndex).xy;
}
float quadSwizzleHelper(float c0, uint quadLaneIndex, uint readIndex)
{
return quadSwizzleHelper(float4(c0, 0.0f, 0.0f, 0.0f), quadLaneIndex, readIndex).x;
}
// integer helpers. We expect to only use this for scalar uint and only for certain builtin inputs
// that have small values and so can be cast to/from float accurately
uint4 quadSwizzleHelper(uint4 c0, uint quadLaneIndex, uint readIndex)
{
return uint4(quadSwizzleHelper(float4(c0), quadLaneIndex, readIndex));
}
uint3 quadSwizzleHelper(uint3 c0, uint quadLaneIndex, uint readIndex)
{
return uint3(quadSwizzleHelper(float3(c0), quadLaneIndex, readIndex));
}
uint2 quadSwizzleHelper(uint2 c0, uint quadLaneIndex, uint readIndex)
{
return uint2(quadSwizzleHelper(float2(c0), quadLaneIndex, readIndex));
}
uint quadSwizzleHelper(uint c0, uint quadLaneIndex, uint readIndex)
{
return uint(quadSwizzleHelper(float(c0), quadLaneIndex, readIndex));
}
int4 quadSwizzleHelper(int4 c0, uint quadLaneIndex, uint readIndex)
{
return uint4(quadSwizzleHelper(float4(c0), quadLaneIndex, readIndex));
}
int3 quadSwizzleHelper(int3 c0, uint quadLaneIndex, uint readIndex)
{
return uint3(quadSwizzleHelper(float3(c0), quadLaneIndex, readIndex));
}
int2 quadSwizzleHelper(int2 c0, uint quadLaneIndex, uint readIndex)
{
return uint2(quadSwizzleHelper(float2(c0), quadLaneIndex, readIndex));
}
int quadSwizzleHelper(int c0, uint quadLaneIndex, uint readIndex)
{
return uint(quadSwizzleHelper(float(c0), quadLaneIndex, readIndex));
}
+1
View File
@@ -118,6 +118,7 @@ RESOURCE_shaderdebug_hlsl TYPE_EMBED "hlsl/shaderdebug.hlsl"
RESOURCE_d3d12_pixelhistory_hlsl TYPE_EMBED "hlsl/d3d12_pixelhistory.hlsl"
RESOURCE_depth_copy_hlsl TYPE_EMBED "hlsl/depth_copy.hlsl"
RESOURCE_raytracing_hlsl TYPE_EMBED "hlsl/raytracing.hlsl"
RESOURCE_quadswizzle_hlsl TYPE_EMBED "hlsl/quadswizzle.hlsl"
#ifdef RENDERDOC_BAKED_DXC_SHADERS
+1
View File
@@ -21,6 +21,7 @@
#define RESOURCE_d3d12_pixelhistory_hlsl 119
#define RESOURCE_depth_copy_hlsl 120
#define RESOURCE_raytracing_hlsl 121
#define RESOURCE_quadswizzle_hlsl 131
#define RESOURCE_fixedcol_0_dxbc 113
#define RESOURCE_fixedcol_1_dxbc 114
+1
View File
@@ -718,6 +718,7 @@
<None Include="data\hlsl\shaderdebug.hlsl" />
<None Include="data\hlsl\depth_copy.hlsl" />
<None Include="data\hlsl\raytracing.hlsl" />
<None Include="data\hlsl\quadswizzle.hlsl" />
</ItemGroup>
<!-- try to bake shaders needed in DXIL now, in case we won't be able to find a dxc at runtime -->
<UsingTask TaskName="GetDXCExecutable" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
+3
View File
@@ -1142,6 +1142,9 @@
<None Include="data\hlsl\raytracing.hlsl">
<Filter>Resources\hlsl</Filter>
</None>
<None Include="data\hlsl\quadswizzle.hlsl">
<Filter>Resources\hlsl</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="data\renderdoc.rc">