Skip to content

Instantly share code, notes, and snippets.

@reinsteam
Created April 6, 2018 15:41
Show Gist options
  • Save reinsteam/750026a5948068efdf3e31163b59dcfd to your computer and use it in GitHub Desktop.
Save reinsteam/750026a5948068efdf3e31163b59dcfd to your computer and use it in GitHub Desktop.
Min/Max reduction example
groupshared float ldsMin[64];
groupshared float ldsMax[64];
void LdsMinMax(uint Idx, uint Ofs)
{
[branch] if (Idx < Ofs)
{
ldsMin[Idx] = min(ldsMin[Idx], ldsMin[Idx + Ofs]);
ldsMax[Idx] = max(ldsMax[Idx], ldsMax[Idx + Ofs]);
}
}
/* Avoid storing data to LDS if it will be read in a next step ? */
float2 LdsMinMax64_Reduce(float2 MinMax, uint LocalId)
{
ldsMin[LocalId] = MinMax.x;
ldsMax[LocalId] = MinMax.y;
LdsMinMax(LocalId, 32);
LdsMinMax(LocalId, 16);
LdsMinMax(LocalId, 8);
LdsMinMax(LocalId, 4);
LdsMinMax(LocalId, 2);
/*
LdsMinMax(LocalId, 1);
return float2(ldsMin[0], ldsMax[0]);
/*/
return float2(min(ldsMin[0], ldsMin[1]), max(ldsMax[0], ldsMax[1]));
//*/
}
groupshared uint ldsMinAtomic;
groupshared uint ldsMaxAtomic;
float2 LdsMinMax64_Atomic(float2 MinMax, uint LocalId)
{
[branch] if (LocalId == 0)
{
ldsMinAtomic = 0;
ldsMaxAtomic = 0;
}
InterlockedMax(ldsMinAtomic, asuint(MinMax.x));
InterlockedMax(ldsMaxAtomic, asuint(MinMax.y));
return asfloat(uint2(ldsMinAtomic, ldsMaxAtomic));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment