Skip to content

Commit 58cace2

Browse files
committed
Add hlsl helper for reading other lanes via derivative swizzling
1 parent 2322e16 commit 58cace2

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

renderdoc/data/hlsl/quadswizzle.hlsl

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/******************************************************************************
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2025 Baldur Karlsson
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
******************************************************************************/
24+
25+
float4 quadSwizzleHelper(float4 c0, uint quadLaneIndex, uint readIndex)
26+
{
27+
bool quadX = (quadLaneIndex & 1u) != 0u;
28+
bool quadY = (quadLaneIndex & 2u) != 0u;
29+
30+
bool readX = ((readIndex & 1u) != 0u);
31+
bool readY = ((readIndex & 2u) != 0u);
32+
33+
float4 sign_x = 1.0f;
34+
sign_x.x = quadX ? -1.0f : 1.0f;
35+
sign_x.y = quadX ? -1.0f : 1.0f;
36+
sign_x.z = quadX ? -1.0f : 1.0f;
37+
sign_x.w = quadX ? -1.0f : 1.0f;
38+
39+
float4 sign_y = 1.0f;
40+
sign_y.x = quadY ? -1.0f : 1.0f;
41+
sign_y.y = quadY ? -1.0f : 1.0f;
42+
sign_y.z = quadY ? -1.0f : 1.0f;
43+
sign_y.w = quadY ? -1.0f : 1.0f;
44+
45+
float4 c1 = c0 + (sign_x * ddx_fine(c0));
46+
float4 c2 = c0 + (sign_y * ddy_fine(c0));
47+
float4 c3 = c2 + (sign_x * ddx_fine(c2));
48+
49+
if(quadLaneIndex == readIndex)
50+
return c0;
51+
else if(readY == quadY)
52+
return c1;
53+
else if(readX == quadX)
54+
return c2;
55+
else
56+
return c3;
57+
}
58+
59+
// helpers for smaller vector sizes
60+
float3 quadSwizzleHelper(float3 c0, uint quadLaneIndex, uint readIndex)
61+
{
62+
return quadSwizzleHelper(float4(c0, 0.0f), quadLaneIndex, readIndex).xyz;
63+
}
64+
65+
float2 quadSwizzleHelper(float2 c0, uint quadLaneIndex, uint readIndex)
66+
{
67+
return quadSwizzleHelper(float4(c0, 0.0f, 0.0f), quadLaneIndex, readIndex).xy;
68+
}
69+
70+
float quadSwizzleHelper(float c0, uint quadLaneIndex, uint readIndex)
71+
{
72+
return quadSwizzleHelper(float4(c0, 0.0f, 0.0f, 0.0f), quadLaneIndex, readIndex).x;
73+
}
74+
75+
// integer helpers. We expect to only use this for scalar uint and only for certain builtin inputs
76+
// that have small values and so can be cast to/from float accurately
77+
uint4 quadSwizzleHelper(uint4 c0, uint quadLaneIndex, uint readIndex)
78+
{
79+
return uint4(quadSwizzleHelper(float4(c0), quadLaneIndex, readIndex));
80+
}
81+
82+
uint3 quadSwizzleHelper(uint3 c0, uint quadLaneIndex, uint readIndex)
83+
{
84+
return uint3(quadSwizzleHelper(float3(c0), quadLaneIndex, readIndex));
85+
}
86+
87+
uint2 quadSwizzleHelper(uint2 c0, uint quadLaneIndex, uint readIndex)
88+
{
89+
return uint2(quadSwizzleHelper(float2(c0), quadLaneIndex, readIndex));
90+
}
91+
92+
uint quadSwizzleHelper(uint c0, uint quadLaneIndex, uint readIndex)
93+
{
94+
return uint(quadSwizzleHelper(float(c0), quadLaneIndex, readIndex));
95+
}
96+
97+
int4 quadSwizzleHelper(int4 c0, uint quadLaneIndex, uint readIndex)
98+
{
99+
return uint4(quadSwizzleHelper(float4(c0), quadLaneIndex, readIndex));
100+
}
101+
102+
int3 quadSwizzleHelper(int3 c0, uint quadLaneIndex, uint readIndex)
103+
{
104+
return uint3(quadSwizzleHelper(float3(c0), quadLaneIndex, readIndex));
105+
}
106+
107+
int2 quadSwizzleHelper(int2 c0, uint quadLaneIndex, uint readIndex)
108+
{
109+
return uint2(quadSwizzleHelper(float2(c0), quadLaneIndex, readIndex));
110+
}
111+
112+
int quadSwizzleHelper(int c0, uint quadLaneIndex, uint readIndex)
113+
{
114+
return uint(quadSwizzleHelper(float(c0), quadLaneIndex, readIndex));
115+
}

renderdoc/data/renderdoc.rc

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ RESOURCE_shaderdebug_hlsl TYPE_EMBED "hlsl/shaderdebug.hlsl"
118118
RESOURCE_d3d12_pixelhistory_hlsl TYPE_EMBED "hlsl/d3d12_pixelhistory.hlsl"
119119
RESOURCE_depth_copy_hlsl TYPE_EMBED "hlsl/depth_copy.hlsl"
120120
RESOURCE_raytracing_hlsl TYPE_EMBED "hlsl/raytracing.hlsl"
121+
RESOURCE_quadswizzle_hlsl TYPE_EMBED "hlsl/quadswizzle.hlsl"
121122

122123
#ifdef RENDERDOC_BAKED_DXC_SHADERS
123124

renderdoc/data/resource.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define RESOURCE_d3d12_pixelhistory_hlsl 119
2222
#define RESOURCE_depth_copy_hlsl 120
2323
#define RESOURCE_raytracing_hlsl 121
24+
#define RESOURCE_quadswizzle_hlsl 131
2425

2526
#define RESOURCE_fixedcol_0_dxbc 113
2627
#define RESOURCE_fixedcol_1_dxbc 114

renderdoc/renderdoc.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@
718718
<None Include="data\hlsl\shaderdebug.hlsl" />
719719
<None Include="data\hlsl\depth_copy.hlsl" />
720720
<None Include="data\hlsl\raytracing.hlsl" />
721+
<None Include="data\hlsl\quadswizzle.hlsl" />
721722
</ItemGroup>
722723
<!-- try to bake shaders needed in DXIL now, in case we won't be able to find a dxc at runtime -->
723724
<UsingTask TaskName="GetDXCExecutable" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">

renderdoc/renderdoc.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,9 @@
11421142
<None Include="data\hlsl\raytracing.hlsl">
11431143
<Filter>Resources\hlsl</Filter>
11441144
</None>
1145+
<None Include="data\hlsl\quadswizzle.hlsl">
1146+
<Filter>Resources\hlsl</Filter>
1147+
</None>
11451148
</ItemGroup>
11461149
<ItemGroup>
11471150
<ResourceCompile Include="data\renderdoc.rc">

0 commit comments

Comments
 (0)