forked from dexyfex/CodeWalker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPPReduceTo0DCS.hlsl
54 lines (43 loc) · 1.36 KB
/
PPReduceTo0DCS.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
StructuredBuffer<float> Input : register( t0 );
RWStructuredBuffer<float> Result : register( u0 );
cbuffer cbCS : register( b0 )
{
uint4 g_param; // g_param.x is the actual elements contained in Input
// g_param.y is the x dimension of the Dispatch call
};
#define groupthreads 128
groupshared float accum[groupthreads];
[numthreads(groupthreads,1,1)]
void main( uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uint GI : SV_GroupIndex )
{
if ( DTid.x < g_param.x )
accum[GI] = Input[DTid.x];
else
accum[GI] = 0;
// Parallel reduction algorithm follows
GroupMemoryBarrierWithGroupSync();
if ( GI < 64 )
accum[GI] += accum[64+GI];
GroupMemoryBarrierWithGroupSync();
if ( GI < 32 )
accum[GI] += accum[32+GI];
GroupMemoryBarrierWithGroupSync();
if ( GI < 16 )
accum[GI] += accum[16+GI];
GroupMemoryBarrierWithGroupSync();
if ( GI < 8 )
accum[GI] += accum[8+GI];
GroupMemoryBarrierWithGroupSync();
if ( GI < 4 )
accum[GI] += accum[4+GI];
GroupMemoryBarrierWithGroupSync();
if ( GI < 2 )
accum[GI] += accum[2+GI];
GroupMemoryBarrierWithGroupSync();
if ( GI < 1 )
accum[GI] += accum[1+GI];
if ( GI == 0 )
{
Result[Gid.x] = accum[0];
}
}