X-Git-Url: https://git.sesse.net/?p=narabu;a=blobdiff_plain;f=encoder.shader;h=8d4bcc2e5b1e46e70cbe460daa93eaf1c3c07a9b;hp=7f0822acbf7779b2fbdee52ec7bd883837348c7f;hb=HEAD;hpb=873d453bb62b954ba959b70d98056da4ff150059 diff --git a/encoder.shader b/encoder.shader index 7f0822a..8d4bcc2 100644 --- a/encoder.shader +++ b/encoder.shader @@ -1,16 +1,44 @@ #version 440 #extension GL_ARB_shader_clock : enable -layout(local_size_x = 8) in; +// Do sixteen 8x8 blocks in a local group, because that matches up perfectly +// with needing 1024 coefficients for our four histograms (of 256 bins each). +#define NUM_Z 16 + +layout(local_size_x = 8, local_size_z = NUM_Z) in; layout(r16ui) uniform restrict writeonly uimage2D dc_ac7_tex; layout(r16ui) uniform restrict writeonly uimage2D ac1_ac6_tex; layout(r16ui) uniform restrict writeonly uimage2D ac2_ac5_tex; layout(r8i) uniform restrict writeonly iimage2D ac3_tex; layout(r8i) uniform restrict writeonly iimage2D ac4_tex; -layout(r8i) uniform restrict readonly iimage2D image_tex; +layout(r8ui) uniform restrict readonly uimage2D image_tex; + +shared uint temp[64 * NUM_Z]; + +layout(std430, binding = 9) buffer layoutName +{ + uint dist[4 * 256]; +}; -shared float temp[64]; +#define MAPPING(s0, s1, s2, s3, s4, s5, s6, s7) ((s0) | (s1 << 2) | (s2 << 4) | (s3 << 6) | (s4 << 8) | (s5 << 10) | (s6 << 12) | (s7 << 14)) + +const uint luma_mapping[8] = { + MAPPING(0, 0, 1, 1, 2, 2, 3, 3), + MAPPING(0, 0, 1, 2, 2, 2, 3, 3), + MAPPING(1, 1, 2, 2, 2, 3, 3, 3), + MAPPING(1, 1, 2, 2, 2, 3, 3, 3), + MAPPING(1, 2, 2, 2, 2, 3, 3, 3), + MAPPING(2, 2, 2, 2, 3, 3, 3, 3), + MAPPING(2, 2, 3, 3, 3, 3, 3, 3), + MAPPING(3, 3, 3, 3, 3, 3, 3, 3), +}; + +// Scale factors; 1.0 / (sqrt(2.0) * cos(k * M_PI / 16.0)), except for the first which is 1. +const float sf[8] = { + 1.0, 0.7209598220069479, 0.765366864730180, 0.8504300947672564, + 1.0, 1.2727585805728336, 1.847759065022573, 3.6245097854115502 +}; const float W[64] = { 8, 16, 19, 22, 26, 27, 29, 34, @@ -22,18 +50,19 @@ const float W[64] = { 26, 27, 29, 34, 38, 46, 56, 69, 27, 29, 35, 38, 46, 56, 69, 83 }; -const float S = 4.0; // whatever? +const float S = 4.0 * 0.5; // whatever? // NOTE: Contains factors to counteract the scaling in the DCT implementation. +#define QM(x, y) (sf[x] * sf[y] / (W[y*8 + x] * S)) const float quant_matrix[64] = { - 1.0 / 64.0, 1.0 / (W[ 1] * S), 1.0 / (W[ 2] * S), 1.0 / (W[ 3] * S), 1.0 / (W[ 4] * S), 1.0 / (W[ 5] * S), 1.0 / (W[ 6] * S), 1.0 / (W[ 7] * S), - 1.0 / (W[ 8] * S), 2.0 / (W[ 9] * S), 2.0 / (W[10] * S), 2.0 / (W[11] * S), 2.0 / (W[12] * S), 2.0 / (W[13] * S), 2.0 / (W[14] * S), 2.0 / (W[15] * S), - 1.0 / (W[16] * S), 2.0 / (W[17] * S), 2.0 / (W[18] * S), 2.0 / (W[19] * S), 2.0 / (W[20] * S), 2.0 / (W[21] * S), 2.0 / (W[22] * S), 2.0 / (W[23] * S), - 1.0 / (W[24] * S), 2.0 / (W[25] * S), 2.0 / (W[26] * S), 2.0 / (W[27] * S), 2.0 / (W[28] * S), 2.0 / (W[29] * S), 2.0 / (W[30] * S), 2.0 / (W[31] * S), - 1.0 / (W[32] * S), 2.0 / (W[33] * S), 2.0 / (W[34] * S), 2.0 / (W[35] * S), 2.0 / (W[36] * S), 2.0 / (W[37] * S), 2.0 / (W[38] * S), 2.0 / (W[39] * S), - 1.0 / (W[40] * S), 2.0 / (W[41] * S), 2.0 / (W[42] * S), 2.0 / (W[43] * S), 2.0 / (W[44] * S), 2.0 / (W[45] * S), 2.0 / (W[46] * S), 2.0 / (W[47] * S), - 1.0 / (W[48] * S), 2.0 / (W[49] * S), 2.0 / (W[50] * S), 2.0 / (W[51] * S), 2.0 / (W[52] * S), 2.0 / (W[53] * S), 2.0 / (W[54] * S), 2.0 / (W[55] * S), - 1.0 / (W[56] * S), 2.0 / (W[57] * S), 2.0 / (W[58] * S), 2.0 / (W[59] * S), 2.0 / (W[60] * S), 2.0 / (W[61] * S), 2.0 / (W[62] * S), 2.0 / (W[63] * S) + 1.0 / 64.0, QM(1, 0), QM(2, 0), QM(3, 0), QM(4, 0), QM(5, 0), QM(6, 0), QM(7, 0), + QM(0, 1), QM(1, 1), QM(2, 1), QM(3, 1), QM(4, 1), QM(5, 1), QM(6, 1), QM(7, 1), + QM(0, 2), QM(1, 2), QM(2, 2), QM(3, 2), QM(4, 2), QM(5, 2), QM(6, 2), QM(7, 2), + QM(0, 3), QM(1, 3), QM(2, 3), QM(3, 3), QM(4, 3), QM(5, 3), QM(6, 3), QM(7, 3), + QM(0, 4), QM(1, 4), QM(2, 4), QM(3, 4), QM(4, 4), QM(5, 4), QM(6, 4), QM(7, 4), + QM(0, 5), QM(1, 5), QM(2, 5), QM(3, 5), QM(4, 5), QM(5, 5), QM(6, 5), QM(7, 5), + QM(0, 6), QM(1, 6), QM(2, 6), QM(3, 6), QM(4, 6), QM(5, 6), QM(6, 6), QM(7, 6), + QM(0, 7), QM(1, 7), QM(2, 7), QM(3, 7), QM(4, 7), QM(5, 7), QM(6, 7), QM(7, 7) }; // Clamp and pack a 9-bit and a 7-bit signed value into a 16-bit word. @@ -42,7 +71,7 @@ uint pack_9_7(int v9, int v7) return (uint(clamp(v9, -256, 255)) & 0x1ffu) | ((uint(clamp(v7, -64, 63)) & 0x7fu) << 9); } -// Scaled 1D DCT. y0 output is scaled by 8, everything else is scaled by 16. +// Scaled 1D DCT (AA&N). y0 is correctly scaled, all other y_k are scaled by sqrt(2) cos(k * Pi / 16). void dct_1d(inout float y0, inout float y1, inout float y2, inout float y3, inout float y4, inout float y5, inout float y6, inout float y7) { const float a1 = 0.7071067811865474; // sqrt(2) @@ -97,11 +126,14 @@ void dct_1d(inout float y0, inout float y1, inout float y2, inout float y3, inou y7 = p5_5 - p4_6; y3 = p5_7 - p4_4; } + void main() { - uint x = 8 * gl_WorkGroupID.x; + uint sx = gl_WorkGroupID.x * NUM_Z + gl_LocalInvocationID.z; + uint x = 8 * sx; uint y = 8 * gl_WorkGroupID.y; uint n = gl_LocalInvocationID.x; + uint z = gl_LocalInvocationID.z; // Load column. float y0 = imageLoad(image_tex, ivec2(x + n, y + 0)).x; @@ -117,27 +149,28 @@ void main() dct_1d(y0, y1, y2, y3, y4, y5, y6, y7); // Communicate with the other shaders in the group. - temp[n + 0 * 8] = y0; - temp[n + 1 * 8] = y1; - temp[n + 2 * 8] = y2; - temp[n + 3 * 8] = y3; - temp[n + 4 * 8] = y4; - temp[n + 5 * 8] = y5; - temp[n + 6 * 8] = y6; - temp[n + 7 * 8] = y7; + uint base_idx = 64 * z; + temp[base_idx + 0 * 8 + n] = floatBitsToUint(y0); + temp[base_idx + 1 * 8 + n] = floatBitsToUint(y1); + temp[base_idx + 2 * 8 + n] = floatBitsToUint(y2); + temp[base_idx + 3 * 8 + n] = floatBitsToUint(y3); + temp[base_idx + 4 * 8 + n] = floatBitsToUint(y4); + temp[base_idx + 5 * 8 + n] = floatBitsToUint(y5); + temp[base_idx + 6 * 8 + n] = floatBitsToUint(y6); + temp[base_idx + 7 * 8 + n] = floatBitsToUint(y7); memoryBarrierShared(); barrier(); // Load row (so transpose, in a sense). - y0 = temp[n * 8 + 0]; - y1 = temp[n * 8 + 1]; - y2 = temp[n * 8 + 2]; - y3 = temp[n * 8 + 3]; - y4 = temp[n * 8 + 4]; - y5 = temp[n * 8 + 5]; - y6 = temp[n * 8 + 6]; - y7 = temp[n * 8 + 7]; + y0 = uintBitsToFloat(temp[base_idx + n * 8 + 0]); + y1 = uintBitsToFloat(temp[base_idx + n * 8 + 1]); + y2 = uintBitsToFloat(temp[base_idx + n * 8 + 2]); + y3 = uintBitsToFloat(temp[base_idx + n * 8 + 3]); + y4 = uintBitsToFloat(temp[base_idx + n * 8 + 4]); + y5 = uintBitsToFloat(temp[base_idx + n * 8 + 5]); + y6 = uintBitsToFloat(temp[base_idx + n * 8 + 6]); + y7 = uintBitsToFloat(temp[base_idx + n * 8 + 7]); // Horizontal DCT. dct_1d(y0, y1, y2, y3, y4, y5, y6, y7); @@ -153,11 +186,62 @@ void main() int c7 = int(round(y7 * quant_matrix[n * 8 + 7])); // Clamp, pack and store. - uint sx = gl_WorkGroupID.x; imageStore(dc_ac7_tex, ivec2(sx, y + n), uvec4(pack_9_7(c0, c7), 0, 0, 0)); imageStore(ac1_ac6_tex, ivec2(sx, y + n), uvec4(pack_9_7(c1, c6), 0, 0, 0)); imageStore(ac2_ac5_tex, ivec2(sx, y + n), uvec4(pack_9_7(c2, c5), 0, 0, 0)); imageStore(ac3_tex, ivec2(sx, y + n), ivec4(c3, 0, 0, 0)); imageStore(ac4_tex, ivec2(sx, y + n), ivec4(c4, 0, 0, 0)); -} + // Zero out the temporary area in preparation for counting up the histograms. + base_idx += 8 * n; + temp[base_idx + 0] = 0; + temp[base_idx + 1] = 0; + temp[base_idx + 2] = 0; + temp[base_idx + 3] = 0; + temp[base_idx + 4] = 0; + temp[base_idx + 5] = 0; + temp[base_idx + 6] = 0; + temp[base_idx + 7] = 0; + + memoryBarrierShared(); + barrier(); + + // Count frequencies into four histograms. We do this to local memory first, + // because this is _much_ faster; then we do global atomic adds for the nonzero + // members. + + // First take the absolute value (signs are encoded differently) and clamp, + // as any value over 255 is going to be encoded as an escape. + c0 = min(abs(c0), 255); + c1 = min(abs(c1), 255); + c2 = min(abs(c2), 255); + c3 = min(abs(c3), 255); + c4 = min(abs(c4), 255); + c5 = min(abs(c5), 255); + c6 = min(abs(c6), 255); + c7 = min(abs(c7), 255); + + // Add up in local memory. + uint m = luma_mapping[n]; + atomicAdd(temp[bitfieldExtract(m, 0, 2) * 256 + c0], 1); + atomicAdd(temp[bitfieldExtract(m, 2, 2) * 256 + c1], 1); + atomicAdd(temp[bitfieldExtract(m, 4, 2) * 256 + c2], 1); + atomicAdd(temp[bitfieldExtract(m, 6, 2) * 256 + c3], 1); + atomicAdd(temp[bitfieldExtract(m, 8, 2) * 256 + c4], 1); + atomicAdd(temp[bitfieldExtract(m, 10, 2) * 256 + c5], 1); + atomicAdd(temp[bitfieldExtract(m, 12, 2) * 256 + c6], 1); + atomicAdd(temp[bitfieldExtract(m, 14, 2) * 256 + c7], 1); + + memoryBarrierShared(); + barrier(); + + // Add from local memory to global memory. + if (temp[base_idx + 0] != 0) atomicAdd(dist[base_idx + 0], temp[base_idx + 0]); + if (temp[base_idx + 1] != 0) atomicAdd(dist[base_idx + 1], temp[base_idx + 1]); + if (temp[base_idx + 2] != 0) atomicAdd(dist[base_idx + 2], temp[base_idx + 2]); + if (temp[base_idx + 3] != 0) atomicAdd(dist[base_idx + 3], temp[base_idx + 3]); + if (temp[base_idx + 4] != 0) atomicAdd(dist[base_idx + 4], temp[base_idx + 4]); + if (temp[base_idx + 5] != 0) atomicAdd(dist[base_idx + 5], temp[base_idx + 5]); + if (temp[base_idx + 6] != 0) atomicAdd(dist[base_idx + 6], temp[base_idx + 6]); + if (temp[base_idx + 7] != 0) atomicAdd(dist[base_idx + 7], temp[base_idx + 7]); +}