Debian Codegraph @

modules/betsy/bc4.glsl

raw
1 #[versions]
2
3 unsigned = "";
4 signed = "#define SNORM";
5
6 #[compute]
7 #version 450
8
9 #VERSION_DEFINES
10
11 shared vec2 g_minMaxValues[4u * 4u * 4u];
12 shared uvec2 g_mask[4u * 4u];
13
14 layout(binding = 0) uniform sampler2D srcTex;
15 layout(binding = 1, rg32ui) uniform restrict writeonly uimage2D dstTexture;
16
17 layout(push_constant, std430) uniform Params {
18 uint p_channelIdx;
19 uint p_padding[3];
20 }
21 params;
22
23 layout(local_size_x = 4, //
24 local_size_y = 4, //
25 local_size_z = 4) in;
26
27 /// Each block is 16 pixels
28 /// Each thread works on 4 pixels
29 /// Therefore each block needs 4 threads, generating 8 masks
30 /// At the end these 8 masks get merged into 2 and results written to output
31 ///
32 /// **Q: Why 4 pixels per thread? Why not 1 pixel per thread? Why not 2? Why not 16?**
33 ///
34 /// A: It's a sweetspot.
35 /// - Very short threads cannot fill expensive GPUs with enough work (dispatch bound)
36 /// - Lots of threads means lots of synchronization (e.g. evaluating min/max, merging masks)
37 /// overhead, and also more LDS usage which reduces occupancy.
38 /// - Long threads (e.g. 1 thread per block) misses parallelism opportunities
39 void main() {
40 float minVal, maxVal;
41 vec4 srcPixel;
42
43 const uint blockThreadId = gl_LocalInvocationID.x;
44
45 const uvec2 pixelsToLoadBase = gl_GlobalInvocationID.yz << 2u;
46
47 for (uint i = 0u; i < 4u; ++i) {
48 const uvec2 pixelsToLoad = pixelsToLoadBase + uvec2(i, blockThreadId);
49
50 const vec4 value = texelFetch(srcTex, ivec2(pixelsToLoad), 0).xyzw;
51 srcPixel[i] = params.p_channelIdx == 0 ? value.x : (params.p_channelIdx == 1 ? value.y : value.w);
52 srcPixel[i] *= 255.0f;
53 }
54
55 minVal = min(srcPixel.x, min(srcPixel.y, srcPixel.z));
56 maxVal = max(srcPixel.x, max(srcPixel.y, srcPixel.z));
57 minVal = min(minVal, srcPixel.w);
58 maxVal = max(maxVal, srcPixel.w);
59
60 const uint minMaxIdxBase = (gl_LocalInvocationID.z << 4u) + (gl_LocalInvocationID.y << 2u);
61 const uint maskIdxBase = (gl_LocalInvocationID.z << 2u) + gl_LocalInvocationID.y;
62
63 g_minMaxValues[minMaxIdxBase + blockThreadId] = vec2(minVal, maxVal);
64 g_mask[maskIdxBase] = uvec2(0u, 0u);
65
66 memoryBarrierShared();
67 barrier();
68
69 // Have all 4 threads in the block grab the min/max value by comparing what all 4 threads uploaded
70 for (uint i = 0u; i < 4u; ++i) {
71 minVal = min(g_minMaxValues[minMaxIdxBase + i].x, minVal);
72 maxVal = max(g_minMaxValues[minMaxIdxBase + i].y, maxVal);
73 }
74
75 // determine bias and emit color indices
76 // given the choice of maxVal/minVal, these indices are optimal:
77 // http://fgiesen.wordpress.com/2009/12/15/dxt5-alpha-block-index-determination/
78 float dist = maxVal - minVal;
79 float dist4 = dist * 4.0f;
80 float dist2 = dist * 2.0f;
81 float bias = (dist < 8) ? (dist - 1) : (trunc(dist * 0.5f) + 2);
82 bias -= minVal * 7;
83
84 uint mask0 = 0u, mask1 = 0u;
85
86 for (uint i = 0u; i < 4u; ++i) {
87 float a = srcPixel[i] * 7.0f + bias;
88
89 int ind = 0;
90
91 // select index. this is a "linear scale" lerp factor between 0 (val=min) and 7 (val=max).
92 if (a >= dist4) {
93 ind = 4;
94 a -= dist4;
95 }
96
97 if (a >= dist2) {
98 ind += 2;
99 a -= dist2;
100 }
101
102 if (a >= dist) {
103 ind += 1;
104 }
105
106 // turn linear scale into DXT index (0/1 are extremal pts)
107 ind = -ind & 7;
108 ind ^= (2 > ind) ? 1 : 0;
109
110 // write index
111 const uint bits = 16u + ((blockThreadId << 2u) + i) * 3u;
112 if (bits < 32u) {
113 mask0 |= uint(ind) << bits;
114 if (bits + 3u > 32u) {
115 mask1 |= uint(ind) >> (32u - bits);
116 }
117 } else {
118 mask1 |= uint(ind) << (bits - 32u);
119 }
120 }
121
122 if (mask0 != 0u) {
123 atomicOr(g_mask[maskIdxBase].x, mask0);
124 }
125 if (mask1 != 0u) {
126 atomicOr(g_mask[maskIdxBase].y, mask1);
127 }
128
129 memoryBarrierShared();
130 barrier();
131
132 if (blockThreadId == 0u) {
133 // Save data
134 uvec2 outputBytes;
135
136 #ifdef SNORM
137 outputBytes.x =
138 packSnorm4x8(vec4(maxVal * (1.0f / 255.0f) * 2.0f - 1.0f,
139 minVal * (1.0f / 255.0f) * 2.0f - 1.0f, 0.0f, 0.0f));
140 #else
141 outputBytes.x = packUnorm4x8(
142 vec4(maxVal * (1.0f / 255.0f), minVal * (1.0f / 255.0f), 0.0f, 0.0f));
143 #endif
144
145 outputBytes.x |= g_mask[maskIdxBase].x;
146 outputBytes.y = g_mask[maskIdxBase].y;
147
148 uvec2 dstUV = gl_GlobalInvocationID.yz;
149 imageStore(dstTexture, ivec2(dstUV), uvec4(outputBytes.xy, 0u, 0u));
150 }
151 }