Debian Codegraph @

third_party/spirv-cross/shaders/comp/bake_gradient.comp

raw
1 #version 310 es
2
3 layout(local_size_x = 8, local_size_y = 8) in;
4
5 layout(binding = 0) uniform sampler2D uHeight;
6 layout(binding = 1) uniform sampler2D uDisplacement;
7 layout(rgba16f, binding = 2) uniform writeonly mediump image2D iHeightDisplacement;
8 layout(rgba16f, binding = 3) uniform writeonly mediump image2D iGradJacobian;
9
10 layout(binding = 4) uniform UBO
11 {
12 vec4 uInvSize;
13 vec4 uScale;
14 };
15
16 mediump float jacobian(mediump vec2 dDdx, mediump vec2 dDdy)
17 {
18 return (1.0 + dDdx.x) * (1.0 + dDdy.y) - dDdx.y * dDdy.x;
19 }
20 #define LAMBDA 1.2
21
22 void main()
23 {
24 vec4 uv = (vec2(gl_GlobalInvocationID.xy) * uInvSize.xy).xyxy + 0.5 * uInvSize;
25
26 float h = textureLod(uHeight, uv.xy, 0.0).x;
27
28 // Compute the heightmap gradient by simple differentiation.
29 float x0 = textureLodOffset(uHeight, uv.xy, 0.0, ivec2(-1, 0)).x;
30 float x1 = textureLodOffset(uHeight, uv.xy, 0.0, ivec2(+1, 0)).x;
31 float y0 = textureLodOffset(uHeight, uv.xy, 0.0, ivec2(0, -1)).x;
32 float y1 = textureLodOffset(uHeight, uv.xy, 0.0, ivec2(0, +1)).x;
33 vec2 grad = uScale.xy * 0.5 * vec2(x1 - x0, y1 - y0);
34
35 // Displacement map must be sampled with a different offset since it's a smaller texture.
36 vec2 displacement = LAMBDA * textureLod(uDisplacement, uv.zw, 0.0).xy;
37
38 // Compute jacobian.
39 vec2 dDdx = 0.5 * LAMBDA * (
40 textureLodOffset(uDisplacement, uv.zw, 0.0, ivec2(+1, 0)).xy -
41 textureLodOffset(uDisplacement, uv.zw, 0.0, ivec2(-1, 0)).xy);
42 vec2 dDdy = 0.5 * LAMBDA * (
43 textureLodOffset(uDisplacement, uv.zw, 0.0, ivec2(0, +1)).xy -
44 textureLodOffset(uDisplacement, uv.zw, 0.0, ivec2(0, -1)).xy);
45 float j = jacobian(dDdx * uScale.z, dDdy * uScale.z);
46
47 displacement = vec2(0.0);
48
49 // Read by vertex shader/tess shader.
50 imageStore(iHeightDisplacement, ivec2(gl_GlobalInvocationID.xy), vec4(h, displacement, 0.0));
51
52 // Read by fragment shader.
53 imageStore(iGradJacobian, ivec2(gl_GlobalInvocationID.xy), vec4(grad, j, 0.0));
54 }
55