Skip to content

Instantly share code, notes, and snippets.

@sebbbi
Last active November 28, 2018 15:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebbbi/e0012076a73c233501cdf1ffdca35fc6 to your computer and use it in GitHub Desktop.
Save sebbbi/e0012076a73c233501cdf1ffdca35fc6 to your computer and use it in GitHub Desktop.
FramentShaderWaveCoherency test shader (Vulkan 1.1)
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_KHR_shader_subgroup_basic : enable
#extension GL_KHR_shader_subgroup_ballot : enable
#extension GL_KHR_shader_subgroup_vote : enable
#extension GL_KHR_shader_subgroup_arithmetic : enable
layout(location = 0) out vec4 outColor;
//#define VISUALIZE_WAVES
#define VISUALIZE_COHERENCY
vec3 hash32(vec2 p)
{
vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973));
p3 += dot(p3, p3.yxz+19.19);
return fract((p3.xxy+p3.yzz)*p3.zyx);
}
void main()
{
#ifdef VISUALIZE_WAVES
vec3 fragmentHash = hash32(gl_FragCoord.xy);
vec3 lane0Hash = subgroupBroadcast(fragmentHash, 0);
outColor = vec4(lane0Hash, 0.0);
#endif
#ifdef VISUALIZE_COHERENCY
bool allLanesActive = subgroupBallotBitCount(subgroupBallot(true)) == gl_SubgroupSize;
uvec2 tileCoord16 = uvec2(gl_FragCoord.xy) / 16;
int numTilesAffectedLane = 0;
bool laneProcessed = false;
while (!laneProcessed)
{
uvec2 firstActiveLaneTile = subgroupBroadcastFirst(tileCoord16);
bool inSameTileAsFirst = all(equal(tileCoord16, firstActiveLaneTile));
laneProcessed = laneProcessed || inSameTileAsFirst;
numTilesAffectedLane++;
}
int numTilesAffected = subgroupMax(numTilesAffectedLane);
outColor.r = allLanesActive ? 0.0 : 1.0;
outColor.g = float(5-numTilesAffected) * (1.0 / 4.0);
outColor.b = 0;
outColor.a = 1;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment