From 7ef879b29684510298a13b02942bf4fe960dea77 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 30 Jul 2023 12:09:02 -0400 Subject: [PATCH 01/33] restrict --- src/video_core/host_shaders/astc_decoder.comp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index bf2693559..a622d89a9 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -48,11 +48,11 @@ struct TexelWeightParams { bool void_extent_hdr; }; -layout(binding = BINDING_INPUT_BUFFER, std430) readonly buffer InputBufferU32 { +layout(binding = BINDING_INPUT_BUFFER, std430) readonly restrict buffer InputBufferU32 { uvec4 astc_data[]; }; -layout(binding = BINDING_OUTPUT_IMAGE, rgba8) uniform writeonly image2DArray dest_image; +layout(binding = BINDING_OUTPUT_IMAGE, rgba8) uniform writeonly restrict image2DArray dest_image; const uint GOB_SIZE_X_SHIFT = 6; const uint GOB_SIZE_Y_SHIFT = 3; From e0c59c7b0b1f082a884414acc5006b243df29e57 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 30 Jul 2023 12:11:55 -0400 Subject: [PATCH 02/33] skip bits --- src/video_core/host_shaders/astc_decoder.comp | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index a622d89a9..55b23201a 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -360,6 +360,11 @@ uint StreamBits(uint num_bits) { return ret; } +void SkipBits(uint num_bits) { + const int int_bits = int(num_bits); + total_bitsread += int_bits; +} + uint StreamColorBits(uint num_bits) { uint ret = 0; int int_bits = int(num_bits); @@ -1071,15 +1076,15 @@ void FillError(ivec3 coord) { } void FillVoidExtentLDR(ivec3 coord) { - StreamBits(52); - uint r_u = StreamBits(16); - uint g_u = StreamBits(16); - uint b_u = StreamBits(16); - uint a_u = StreamBits(16); - float a = float(a_u) / 65535.0f; - float r = float(r_u) / 65535.0f; - float g = float(g_u) / 65535.0f; - float b = float(b_u) / 65535.0f; + SkipBits(52); + const uint r_u = StreamBits(16); + const uint g_u = StreamBits(16); + const uint b_u = StreamBits(16); + const uint a_u = StreamBits(16); + const float a = float(a_u) / 65535.0f; + const float r = float(r_u) / 65535.0f; + const float g = float(g_u) / 65535.0f; + const float b = float(b_u) / 65535.0f; for (uint j = 0; j < block_dims.y; j++) { for (uint i = 0; i < block_dims.x; i++) { imageStore(dest_image, coord + ivec3(i, j, 0), vec4(r, g, b, a)); From 6ff65abd62c4a4f7cc83a79e1ba30d0d99f26ff3 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 30 Jul 2023 12:13:03 -0400 Subject: [PATCH 03/33] params.max_weight --- src/video_core/host_shaders/astc_decoder.comp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 55b23201a..a814ef483 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -1058,12 +1058,9 @@ TexelWeightParams DecodeBlockInfo() { } weight_index -= 2; if ((mode_layout != 9) && ((mode & 0x200) != 0)) { - const int max_weights[6] = int[6](7, 8, 9, 10, 11, 12); - params.max_weight = max_weights[weight_index]; - } else { - const int max_weights[6] = int[6](1, 2, 3, 4, 5, 6); - params.max_weight = max_weights[weight_index]; + weight_index += 6; } + params.max_weight = weight_index + 1; return params; } From ac09cc3504fd9c2e256377f75e7ecb187c4bb6f7 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 30 Jul 2023 12:26:48 -0400 Subject: [PATCH 04/33] weights refactor --- src/video_core/host_shaders/astc_decoder.comp | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index a814ef483..b84ddd67d 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -116,8 +116,6 @@ const uint REPLICATE_7_BIT_TO_8_TABLE[128] = 237, 239, 241, 243, 245, 247, 249, 251, 253, 255); // Input ASTC texture globals -uint current_index = 0; -int bitsread = 0; int total_bitsread = 0; uvec4 local_buff; @@ -144,13 +142,6 @@ int texel_vector_index = 0; uint unquantized_texel_weights[2][144]; -uint SwizzleOffset(uvec2 pos) { - uint x = pos.x; - uint y = pos.y; - return ((x % 64) / 32) * 256 + ((y % 8) / 2) * 64 + ((x % 32) / 16) * 32 + - (y % 2) * 16 + (x % 16); -} - // Replicates low num_bits such that [(to_bit - 1):(to_bit - 1 - from_bit)] // is the same as [(num_bits - 1):0] and repeats all the way down. uint Replicate(uint val, uint num_bits, uint to_bit) { @@ -1224,33 +1215,40 @@ void DecompressBlock(ivec3 coord) { uint local_partition = 0; if (num_partitions > 1) { local_partition = Select2DPartition(partition_index, i, j, num_partitions, - (block_dims.y * block_dims.x) < 32); + (block_dims.y * block_dims.x) < 32); } - vec4 p; - uvec4 C0 = ReplicateByteTo16(endpoints[local_partition][0]); - uvec4 C1 = ReplicateByteTo16(endpoints[local_partition][1]); - uvec4 plane_vec = uvec4(0); - uvec4 weight_vec = uvec4(0); - for (uint c = 0; c < 4; c++) { - if (params.dual_plane && (((plane_index + 1) & 3) == c)) { - plane_vec[c] = 1; + const uvec4 C0 = ReplicateByteTo16(endpoints[local_partition][0]); + const uvec4 C1 = ReplicateByteTo16(endpoints[local_partition][1]); + const uint weight_offset = (j * block_dims.x + i); + const uint primary_weight = unquantized_texel_weights[weight_offset][0]; + uvec4 weight_vec = uvec4(primary_weight); + if (params.dual_plane) { + const uint secondary_weight = unquantized_texel_weights[weight_offset][1]; + for (uint c = 0; c < 4; c++) { + const bool is_secondary = ((plane_index + 1u) & 3u) == c; + weight_vec[c] = is_secondary ? secondary_weight : primary_weight; } - weight_vec[c] = unquantized_texel_weights[plane_vec[c]][j * block_dims.x + i]; } - vec4 Cf = vec4((C0 * (uvec4(64) - weight_vec) + C1 * weight_vec + uvec4(32)) / 64); - p = (Cf / 65535.0); + const vec4 Cf = + vec4((C0 * (uvec4(64) - weight_vec) + C1 * weight_vec + uvec4(32)) / 64); + const vec4 p = (Cf / 65535.0); imageStore(dest_image, coord + ivec3(i, j, 0), p.gbar); } } } + +uint SwizzleOffset(uvec2 pos) { + uint x = pos.x; + uint y = pos.y; + return ((x % 64) / 32) * 256 + ((y % 8) / 2) * 64 + ((x % 32) / 16) * 32 + + (y % 2) * 16 + (x % 16); +} + void main() { uvec3 pos = gl_GlobalInvocationID; pos.x <<= BYTES_PER_BLOCK_LOG2; - - // Read as soon as possible due to its latency const uint swizzle = SwizzleOffset(pos.xy); - const uint block_y = pos.y >> GOB_SIZE_Y_SHIFT; uint offset = 0; @@ -1264,8 +1262,6 @@ void main() { if (any(greaterThanEqual(coord, imageSize(dest_image)))) { return; } - current_index = 0; - bitsread = 0; local_buff = astc_data[offset / 16]; DecompressBlock(coord); } From 27c8bb9615039eefc837ade5d91d5733c738f4a0 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 30 Jul 2023 12:41:52 -0400 Subject: [PATCH 05/33] flattening --- src/video_core/host_shaders/astc_decoder.comp | 87 ++++++++++--------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index b84ddd67d..f720df6d2 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -140,8 +140,6 @@ int result_index = 0; EncodingData texel_vector[144]; int texel_vector_index = 0; -uint unquantized_texel_weights[2][144]; - // Replicates low num_bits such that [(to_bit - 1):(to_bit - 1 - from_bit)] // is the same as [(num_bits - 1):0] and repeats all the way down. uint Replicate(uint val, uint num_bits, uint to_bit) { @@ -879,58 +877,60 @@ uint UnquantizeTexelWeight(EncodingData val) { return result; } -void UnquantizeTexelWeights(bool dual_plane, uvec2 size) { - uint weight_idx = 0; - uint unquantized[2][144]; - uint area = size.x * size.y; - for (uint itr = 0; itr < texel_vector_index; itr++) { - unquantized[0][weight_idx] = UnquantizeTexelWeight(texel_vector[itr]); - if (dual_plane) { - ++itr; - unquantized[1][weight_idx] = UnquantizeTexelWeight(texel_vector[itr]); - if (itr == texel_vector_index) { - break; - } - } - if (++weight_idx >= (area)) - break; - } - +void UnquantizeTexelWeights(bool is_dual_plane, uvec2 size, out uint unquantized_texel_weights[2 * 144]) { const uint Ds = uint((block_dims.x * 0.5f + 1024) / (block_dims.x - 1)); const uint Dt = uint((block_dims.y * 0.5f + 1024) / (block_dims.y - 1)); - const uint k_plane_scale = dual_plane ? 2 : 1; - for (uint plane = 0; plane < k_plane_scale; plane++) { + const uint num_planes = is_dual_plane ? 2 : 1; + const uint area = size.x * size.y; + const uint loop_count = min(result_index, area * num_planes); + uint unquantized[2 * 144]; + for (uint itr = 0; itr < loop_count; ++itr) { + unquantized[itr] = UnquantizeTexelWeight(texel_vector[itr]); + } + for (uint plane = 0; plane < num_planes; ++plane) { for (uint t = 0; t < block_dims.y; t++) { for (uint s = 0; s < block_dims.x; s++) { - uint cs = Ds * s; - uint ct = Dt * t; - uint gs = (cs * (size.x - 1) + 32) >> 6; - uint gt = (ct * (size.y - 1) + 32) >> 6; - uint js = gs >> 4; - uint fs = gs & 0xF; - uint jt = gt >> 4; - uint ft = gt & 0x0F; - uint w11 = (fs * ft + 8) >> 4; - uint w10 = ft - w11; - uint w01 = fs - w11; - uint w00 = 16 - fs - ft + w11; - uvec4 w = uvec4(w00, w01, w10, w11); - uint v0 = jt * size.x + js; + const uint cs = Ds * s; + const uint ct = Dt * t; + const uint gs = (cs * (size.x - 1) + 32) >> 6; + const uint gt = (ct * (size.y - 1) + 32) >> 6; + const uint js = gs >> 4; + const uint fs = gs & 0xF; + const uint jt = gt >> 4; + const uint ft = gt & 0x0F; + const uint w11 = (fs * ft + 8) >> 4; + const uint w10 = ft - w11; + const uint w01 = fs - w11; + const uint w00 = 16 - fs - ft + w11; + const uvec4 w = uvec4(w00, w01, w10, w11); + const uint v0 = jt * size.x + js; uvec4 p = uvec4(0); + +#define VectorIndicesFromBase(offset_base) \ + const uint offset = is_dual_plane ? 2 * offset_base + plane : offset_base; \ + if (v0 < area) { - p.x = unquantized[plane][v0]; + const uint offset_base = v0; + VectorIndicesFromBase(offset_base); + p.x = unquantized[offset]; } if ((v0 + 1) < (area)) { - p.y = unquantized[plane][v0 + 1]; + const uint offset_base = v0 + 1; + VectorIndicesFromBase(offset_base); + p.y = unquantized[offset]; } if ((v0 + size.x) < (area)) { - p.z = unquantized[plane][(v0 + size.x)]; + const uint offset_base = v0 + size.x; + VectorIndicesFromBase(offset_base); + p.z = unquantized[offset]; } if ((v0 + size.x + 1) < (area)) { - p.w = unquantized[plane][(v0 + size.x + 1)]; + const uint offset_base = v0 + size.x + 1; + VectorIndicesFromBase(offset_base); + p.w = unquantized[offset]; } - unquantized_texel_weights[plane][t * block_dims.x + s] = (uint(dot(p, w)) + 8) >> 4; + unquantized_texel_weights[plane * 144 + t * block_dims.x + s] = (uint(dot(p, w)) + 8) >> 4; } } } @@ -1208,7 +1208,8 @@ void DecompressBlock(ivec3 coord) { texel_flag = true; // use texel "vector" and bit stream in integer decoding DecodeIntegerSequence(params.max_weight, GetNumWeightValues(params.size, params.dual_plane)); - UnquantizeTexelWeights(params.dual_plane, params.size); + uint unquantized_texel_weights[2 * 144]; + UnquantizeTexelWeights(params.dual_plane, params.size, unquantized_texel_weights); for (uint j = 0; j < block_dims.y; j++) { for (uint i = 0; i < block_dims.x; i++) { @@ -1220,10 +1221,10 @@ void DecompressBlock(ivec3 coord) { const uvec4 C0 = ReplicateByteTo16(endpoints[local_partition][0]); const uvec4 C1 = ReplicateByteTo16(endpoints[local_partition][1]); const uint weight_offset = (j * block_dims.x + i); - const uint primary_weight = unquantized_texel_weights[weight_offset][0]; + const uint primary_weight = unquantized_texel_weights[weight_offset]; uvec4 weight_vec = uvec4(primary_weight); if (params.dual_plane) { - const uint secondary_weight = unquantized_texel_weights[weight_offset][1]; + const uint secondary_weight = unquantized_texel_weights[weight_offset + 144]; for (uint c = 0; c < 4; c++) { const bool is_secondary = ((plane_index + 1u) & 3u) == c; weight_vec[c] = is_secondary ? secondary_weight : primary_weight; From b8ca47e094738689e4e8470b0f1f00d60ee26f4c Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 30 Jul 2023 12:56:32 -0400 Subject: [PATCH 06/33] EncodingData pack --- src/video_core/host_shaders/astc_decoder.comp | 113 +++++++++++------- 1 file changed, 69 insertions(+), 44 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index f720df6d2..37b502324 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -33,10 +33,7 @@ UNIFORM(6) uint block_height_mask; END_PUSH_CONSTANTS struct EncodingData { - uint encoding; - uint num_bits; - uint bit_value; - uint quint_trit_value; + uint data; }; struct TexelWeightParams { @@ -66,17 +63,47 @@ const int TRIT = 2; // ASTC Encodings data, sorted in ascending order based on their BitLength value // (see GetBitLength() function) -EncodingData encoding_values[22] = EncodingData[]( - EncodingData(JUST_BITS, 0, 0, 0), EncodingData(JUST_BITS, 1, 0, 0), EncodingData(TRIT, 0, 0, 0), - EncodingData(JUST_BITS, 2, 0, 0), EncodingData(QUINT, 0, 0, 0), EncodingData(TRIT, 1, 0, 0), - EncodingData(JUST_BITS, 3, 0, 0), EncodingData(QUINT, 1, 0, 0), EncodingData(TRIT, 2, 0, 0), - EncodingData(JUST_BITS, 4, 0, 0), EncodingData(QUINT, 2, 0, 0), EncodingData(TRIT, 3, 0, 0), - EncodingData(JUST_BITS, 5, 0, 0), EncodingData(QUINT, 3, 0, 0), EncodingData(TRIT, 4, 0, 0), - EncodingData(JUST_BITS, 6, 0, 0), EncodingData(QUINT, 4, 0, 0), EncodingData(TRIT, 5, 0, 0), - EncodingData(JUST_BITS, 7, 0, 0), EncodingData(QUINT, 5, 0, 0), EncodingData(TRIT, 6, 0, 0), - EncodingData(JUST_BITS, 8, 0, 0) +const EncodingData encoding_values[22] = EncodingData[]( + EncodingData(JUST_BITS), EncodingData(JUST_BITS | (1u << 8u)), EncodingData(TRIT), EncodingData(JUST_BITS | (2u << 8u)), + EncodingData(QUINT), EncodingData(TRIT | (1u << 8u)), EncodingData(JUST_BITS | (3u << 8u)), EncodingData(QUINT | (1u << 8u)), + EncodingData(TRIT | (2u << 8u)), EncodingData(JUST_BITS | (4u << 8u)), EncodingData(QUINT | (2u << 8u)), EncodingData(TRIT | (3u << 8u)), + EncodingData(JUST_BITS | (5u << 8u)), EncodingData(QUINT | (3u << 8u)), EncodingData(TRIT | (4u << 8u)), EncodingData(JUST_BITS | (6u << 8u)), + EncodingData(QUINT | (4u << 8u)), EncodingData(TRIT | (5u << 8u)), EncodingData(JUST_BITS | (7u << 8u)), EncodingData(QUINT | (5u << 8u)), + EncodingData(TRIT | (6u << 8u)), EncodingData(JUST_BITS | (8u << 8u)) ); +// EncodingData helpers +uint Encoding(EncodingData val) { + return bitfieldExtract(val.data, 0, 8); +} +uint NumBits(EncodingData val) { + return bitfieldExtract(val.data, 8, 8); +} +uint BitValue(EncodingData val) { + return bitfieldExtract(val.data, 16, 8); +} +uint QuintTritValue(EncodingData val) { + return bitfieldExtract(val.data, 24, 8); +} + +void Encoding(inout EncodingData val, uint v) { + val.data = bitfieldInsert(val.data, v, 0, 8); +} +void NumBits(inout EncodingData val, uint v) { + val.data = bitfieldInsert(val.data, v, 8, 8); +} +void BitValue(inout EncodingData val, uint v) { + val.data = bitfieldInsert(val.data, v, 16, 8); +} +void QuintTritValue(inout EncodingData val, uint v) { + val.data = bitfieldInsert(val.data, v, 24, 8); +} + +EncodingData CreateEncodingData(uint encoding, uint num_bits, uint bit_val, uint quint_trit_val) { + return EncodingData(((encoding) << 0u) | ((num_bits) << 8u) | + ((bit_val) << 16u) | ((quint_trit_val) << 24u)); +} + // The following constants are expanded variants of the Replicate() // function calls corresponding to the following arguments: // value: index into the generated table @@ -379,10 +406,12 @@ void ResultEmplaceBack(EncodingData val) { // Returns the number of bits required to encode n_vals values. uint GetBitLength(uint n_vals, uint encoding_index) { - uint total_bits = encoding_values[encoding_index].num_bits * n_vals; - if (encoding_values[encoding_index].encoding == TRIT) { + const EncodingData encoding_value = encoding_values[encoding_index]; + const uint encoding = Encoding(encoding_value); + uint total_bits = NumBits(encoding_value) * n_vals; + if (encoding == TRIT) { total_bits += Div5Ceil(n_vals * 8); - } else if (encoding_values[encoding_index].encoding == QUINT) { + } else if (encoding == QUINT) { total_bits += Div3Ceil(n_vals * 7); } return total_bits; @@ -451,11 +480,7 @@ void DecodeQuintBlock(uint num_bits) { } } for (uint i = 0; i < 3; i++) { - EncodingData val; - val.encoding = QUINT; - val.num_bits = num_bits; - val.bit_value = m[i]; - val.quint_trit_value = q[i]; + const EncodingData val = CreateEncodingData(QUINT, num_bits, m[i], q[i]); ResultEmplaceBack(val); } } @@ -503,30 +528,28 @@ void DecodeTritBlock(uint num_bits) { t[0] = (BitsBracket(C, 1) << 1) | (BitsBracket(C, 0) & ~BitsBracket(C, 1)); } for (uint i = 0; i < 5; i++) { - EncodingData val; - val.encoding = TRIT; - val.num_bits = num_bits; - val.bit_value = m[i]; - val.quint_trit_value = t[i]; + const EncodingData val = CreateEncodingData(TRIT, num_bits, m[i], t[i]); ResultEmplaceBack(val); } } void DecodeIntegerSequence(uint max_range, uint num_values) { EncodingData val = encoding_values[max_range]; + const uint encoding = Encoding(val); + const uint num_bits = NumBits(val); uint vals_decoded = 0; while (vals_decoded < num_values) { - switch (val.encoding) { + switch (encoding) { case QUINT: - DecodeQuintBlock(val.num_bits); + DecodeQuintBlock(num_bits); vals_decoded += 3; break; case TRIT: - DecodeTritBlock(val.num_bits); + DecodeTritBlock(num_bits); vals_decoded += 5; break; case JUST_BITS: - val.bit_value = StreamColorBits(val.num_bits); + BitValue(val, StreamColorBits(num_bits)); ResultEmplaceBack(val); vals_decoded++; break; @@ -554,17 +577,18 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { if (out_index >= num_values) { break; } - EncodingData val = result_vector[itr]; - uint bitlen = val.num_bits; - uint bitval = val.bit_value; + const EncodingData val = result_vector[itr]; + const uint encoding = Encoding(val); + const uint bitlen = NumBits(val); + const uint bitval = BitValue(val); uint A = 0, B = 0, C = 0, D = 0; A = ReplicateBitTo9((bitval & 1)); - switch (val.encoding) { + switch (encoding) { case JUST_BITS: color_values[out_index++] = FastReplicateTo8(bitval, bitlen); break; case TRIT: { - D = val.quint_trit_value; + D = QuintTritValue(val); switch (bitlen) { case 1: C = 204; @@ -603,7 +627,7 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { break; } case QUINT: { - D = val.quint_trit_value; + D = QuintTritValue(val); switch (bitlen) { case 1: C = 113; @@ -636,7 +660,7 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { break; } } - if (val.encoding != JUST_BITS) { + if (encoding != JUST_BITS) { uint T = (D * C) + B; T ^= A; T = (A & 0x80) | (T >> 2); @@ -806,17 +830,18 @@ void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode) { } uint UnquantizeTexelWeight(EncodingData val) { - uint bitval = val.bit_value; - uint bitlen = val.num_bits; - uint A = ReplicateBitTo7((bitval & 1)); + const uint encoding = Encoding(val); + const uint bitlen = NumBits(val); + const uint bitval = BitValue(val); + const uint A = ReplicateBitTo7((bitval & 1)); uint B = 0, C = 0, D = 0; uint result = 0; - switch (val.encoding) { + switch (encoding) { case JUST_BITS: result = FastReplicateTo6(bitval, bitlen); break; case TRIT: { - D = val.quint_trit_value; + D = QuintTritValue(val); switch (bitlen) { case 0: { uint results[3] = {0, 32, 63}; @@ -845,7 +870,7 @@ uint UnquantizeTexelWeight(EncodingData val) { break; } case QUINT: { - D = val.quint_trit_value; + D = QuintTritValue(val); switch (bitlen) { case 0: { uint results[5] = {0, 16, 32, 47, 63}; @@ -866,7 +891,7 @@ uint UnquantizeTexelWeight(EncodingData val) { break; } } - if (val.encoding != JUST_BITS && bitlen > 0) { + if (encoding != JUST_BITS && bitlen > 0) { result = D * C + B; result ^= A; result = (A & 0x20) | (result >> 2); From 0078e5a33822d0e15cc7fab2809e5bc4883cff26 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 30 Jul 2023 13:04:41 -0400 Subject: [PATCH 07/33] reuse vectors memory --- src/video_core/host_shaders/astc_decoder.comp | 50 +++++++------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 37b502324..4277b0756 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -154,19 +154,10 @@ int color_bitsread = 0; uint color_values[32]; int colvals_index = 0; -// Weight data globals -uvec4 texel_weight_data; -int texel_bitsread = 0; - -bool texel_flag = false; - // Global "vectors" to be pushed into when decoding EncodingData result_vector[144]; int result_index = 0; -EncodingData texel_vector[144]; -int texel_vector_index = 0; - // Replicates low num_bits such that [(to_bit - 1):(to_bit - 1 - from_bit)] // is the same as [(num_bits - 1):0] and repeats all the way down. uint Replicate(uint val, uint num_bits, uint to_bit) { @@ -382,26 +373,15 @@ void SkipBits(uint num_bits) { } uint StreamColorBits(uint num_bits) { - uint ret = 0; - int int_bits = int(num_bits); - if (texel_flag) { - ret = ExtractBits(texel_weight_data, texel_bitsread, int_bits); - texel_bitsread += int_bits; - } else { - ret = ExtractBits(color_endpoint_data, color_bitsread, int_bits); - color_bitsread += int_bits; - } + const int int_bits = int(num_bits); + const uint ret = ExtractBits(color_endpoint_data, color_bitsread, int_bits); + color_bitsread += int_bits; return ret; } void ResultEmplaceBack(EncodingData val) { - if (texel_flag) { - texel_vector[texel_vector_index] = val; - ++texel_vector_index; - } else { - result_vector[result_index] = val; - ++result_index; - } + result_vector[result_index] = val; + ++result_index; } // Returns the number of bits required to encode n_vals values. @@ -910,7 +890,7 @@ void UnquantizeTexelWeights(bool is_dual_plane, uvec2 size, out uint unquantized const uint loop_count = min(result_index, area * num_planes); uint unquantized[2 * 144]; for (uint itr = 0; itr < loop_count; ++itr) { - unquantized[itr] = UnquantizeTexelWeight(texel_vector[itr]); + unquantized[itr] = UnquantizeTexelWeight(result_vector[itr]); } for (uint plane = 0; plane < num_planes; ++plane) { for (uint t = 0; t < block_dims.y; t++) { @@ -1215,22 +1195,26 @@ void DecompressBlock(ivec3 coord) { ComputeEndpoints(endpoints[i][0], endpoints[i][1], color_endpoint_mode[i]); } - texel_weight_data = local_buff; - texel_weight_data = bitfieldReverse(texel_weight_data).wzyx; + color_endpoint_data = local_buff; + color_endpoint_data = bitfieldReverse(color_endpoint_data).wzyx; uint clear_byte_start = (GetPackedBitSize(params.size, params.dual_plane, params.max_weight) >> 3) + 1; - uint byte_insert = ExtractBits(texel_weight_data, int(clear_byte_start - 1) * 8, 8) & + uint byte_insert = ExtractBits(color_endpoint_data, int(clear_byte_start - 1) * 8, 8) & uint( ((1 << (GetPackedBitSize(params.size, params.dual_plane, params.max_weight) % 8)) - 1)); uint vec_index = (clear_byte_start - 1) >> 2; - texel_weight_data[vec_index] = - bitfieldInsert(texel_weight_data[vec_index], byte_insert, int((clear_byte_start - 1) % 4) * 8, 8); + color_endpoint_data[vec_index] = + bitfieldInsert(color_endpoint_data[vec_index], byte_insert, int((clear_byte_start - 1) % 4) * 8, 8); for (uint i = clear_byte_start; i < 16; ++i) { uint idx = i >> 2; - texel_weight_data[idx] = bitfieldInsert(texel_weight_data[idx], 0, int(i % 4) * 8, 8); + color_endpoint_data[idx] = bitfieldInsert(color_endpoint_data[idx], 0, int(i % 4) * 8, 8); } - texel_flag = true; // use texel "vector" and bit stream in integer decoding + + // Re-init vector variables for next decode phase + result_index = 0; + color_bitsread = 0; + DecodeIntegerSequence(params.max_weight, GetNumWeightValues(params.size, params.dual_plane)); uint unquantized_texel_weights[2 * 144]; From d17a51bc59462b136635515e40019c8ae4e05b5e Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 30 Jul 2023 13:05:45 -0400 Subject: [PATCH 08/33] extractbits robustness --- src/video_core/host_shaders/astc_decoder.comp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 4277b0756..f65e1d1b9 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -349,14 +349,17 @@ uint ExtractBits(uvec4 payload, int offset, int bits) { if (bits <= 0) { return 0; } - int last_offset = offset + bits - 1; - int shifted_offset = offset >> 5; + if (bits > 32) { + return 0; + } + const int last_offset = offset + bits - 1; + const int shifted_offset = offset >> 5; if ((last_offset >> 5) == shifted_offset) { return bitfieldExtract(payload[shifted_offset], offset & 31, bits); } - int first_bits = 32 - (offset & 31); - int result_first = int(bitfieldExtract(payload[shifted_offset], offset & 31, first_bits)); - int result_second = int(bitfieldExtract(payload[shifted_offset + 1], 0, bits - first_bits)); + const int first_bits = 32 - (offset & 31); + const int result_first = int(bitfieldExtract(payload[shifted_offset], offset & 31, first_bits)); + const int result_second = int(bitfieldExtract(payload[shifted_offset + 1], 0, bits - first_bits)); return result_first | (result_second << first_bits); } From 998246efc27950e3fbebc00a8ca3442dfabb9904 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 30 Jul 2023 13:12:12 -0400 Subject: [PATCH 09/33] minor redundancy cleanup --- src/video_core/host_shaders/astc_decoder.comp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index f65e1d1b9..90b40c55f 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -418,14 +418,6 @@ uint BitsBracket(uint bits, uint pos) { } uint BitsOp(uint bits, uint start, uint end) { - if (start == end) { - return BitsBracket(bits, start); - } else if (start > end) { - uint t = start; - start = end; - end = t; - } - uint mask = (1 << (end - start + 1)) - 1; return ((bits >> start) & mask); } @@ -665,10 +657,8 @@ ivec2 BitTransferSigned(int a, int b) { } uvec4 ClampByte(ivec4 color) { - for (uint i = 0; i < 4; ++i) { - color[i] = (color[i] < 0) ? 0 : ((color[i] > 255) ? 255 : color[i]); - } - return uvec4(color); + const uvec4 clamped = uvec4(clamp(color, 0, 255)); + return clamped; } ivec4 BlueContract(int a, int r, int g, int b) { From 5248fa926dd53948b0df4f93c50107dc30ae2305 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 17:22:03 -0400 Subject: [PATCH 10/33] const, pack result_vector and replicate tables, undo amd opts --- src/video_core/host_shaders/astc_decoder.comp | 491 ++++++++++-------- 1 file changed, 262 insertions(+), 229 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 90b40c55f..e8801b0ff 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -57,20 +57,40 @@ const uint GOB_SIZE_SHIFT = GOB_SIZE_X_SHIFT + GOB_SIZE_Y_SHIFT; const uint BYTES_PER_BLOCK_LOG2 = 4; -const int JUST_BITS = 0; -const int QUINT = 1; -const int TRIT = 2; +const uint JUST_BITS = 0u; +const uint QUINT = 1u; +const uint TRIT = 2u; // ASTC Encodings data, sorted in ascending order based on their BitLength value // (see GetBitLength() function) -const EncodingData encoding_values[22] = EncodingData[]( - EncodingData(JUST_BITS), EncodingData(JUST_BITS | (1u << 8u)), EncodingData(TRIT), EncodingData(JUST_BITS | (2u << 8u)), - EncodingData(QUINT), EncodingData(TRIT | (1u << 8u)), EncodingData(JUST_BITS | (3u << 8u)), EncodingData(QUINT | (1u << 8u)), - EncodingData(TRIT | (2u << 8u)), EncodingData(JUST_BITS | (4u << 8u)), EncodingData(QUINT | (2u << 8u)), EncodingData(TRIT | (3u << 8u)), - EncodingData(JUST_BITS | (5u << 8u)), EncodingData(QUINT | (3u << 8u)), EncodingData(TRIT | (4u << 8u)), EncodingData(JUST_BITS | (6u << 8u)), - EncodingData(QUINT | (4u << 8u)), EncodingData(TRIT | (5u << 8u)), EncodingData(JUST_BITS | (7u << 8u)), EncodingData(QUINT | (5u << 8u)), - EncodingData(TRIT | (6u << 8u)), EncodingData(JUST_BITS | (8u << 8u)) -); +const uvec4 encoding_values[6] = uvec4[]( + uvec4((JUST_BITS), (JUST_BITS | (1u << 8u)), (TRIT), (JUST_BITS | (2u << 8u))), + uvec4((QUINT), (TRIT | (1u << 8u)), (JUST_BITS | (3u << 8u)), (QUINT | (1u << 8u))), + uvec4((TRIT | (2u << 8u)), (JUST_BITS | (4u << 8u)), (QUINT | (2u << 8u)), (TRIT | (3u << 8u))), + uvec4((JUST_BITS | (5u << 8u)), (QUINT | (3u << 8u)), (TRIT | (4u << 8u)), (JUST_BITS | (6u << 8u))), + uvec4((QUINT | (4u << 8u)), (TRIT | (5u << 8u)), (JUST_BITS | (7u << 8u)), (QUINT | (5u << 8u))), + uvec4((TRIT | (6u << 8u)), (JUST_BITS | (8u << 8u)), 0u, 0u)); + +// Input ASTC texture globals +int total_bitsread = 0; +uvec4 local_buff; + +// Color data globals +uvec4 color_endpoint_data; +int color_bitsread = 0; + +// Global "vector" to be pushed into when decoding +// At most will require BLOCK_WIDTH x BLOCK_HEIGHT in single plane mode +// At most will require BLOCK_WIDTH x BLOCK_HEIGHT x 2 in dual plane mode +// So the maximum would be 144 (12 x 12) elements, x 2 for two planes +#define DIVCEIL(number, divisor) (number + divisor - 1) / divisor +#define ARRAY_NUM_ELEMENTS 144 +#define VECTOR_ARRAY_SIZE DIVCEIL(ARRAY_NUM_ELEMENTS * 2, 4) +uvec4 result_vector[VECTOR_ARRAY_SIZE]; + +int result_index = 0; +uint result_vector_max_index; +bool result_limit_reached = false; // EncodingData helpers uint Encoding(EncodingData val) { @@ -104,78 +124,17 @@ EncodingData CreateEncodingData(uint encoding, uint num_bits, uint bit_val, uint ((bit_val) << 16u) | ((quint_trit_val) << 24u)); } -// The following constants are expanded variants of the Replicate() -// function calls corresponding to the following arguments: -// value: index into the generated table -// num_bits: the after "REPLICATE" in the table name. i.e. 4 is num_bits in REPLICATE_4. -// to_bit: the integer after "TO_" -const uint REPLICATE_BIT_TO_7_TABLE[2] = uint[](0, 127); -const uint REPLICATE_1_BIT_TO_9_TABLE[2] = uint[](0, 511); -const uint REPLICATE_1_BIT_TO_8_TABLE[2] = uint[](0, 255); -const uint REPLICATE_2_BIT_TO_8_TABLE[4] = uint[](0, 85, 170, 255); -const uint REPLICATE_3_BIT_TO_8_TABLE[8] = uint[](0, 36, 73, 109, 146, 182, 219, 255); -const uint REPLICATE_4_BIT_TO_8_TABLE[16] = - uint[](0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255); -const uint REPLICATE_5_BIT_TO_8_TABLE[32] = - uint[](0, 8, 16, 24, 33, 41, 49, 57, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, 148, 156, 165, - 173, 181, 189, 198, 206, 214, 222, 231, 239, 247, 255); -const uint REPLICATE_1_BIT_TO_6_TABLE[2] = uint[](0, 63); -const uint REPLICATE_2_BIT_TO_6_TABLE[4] = uint[](0, 21, 42, 63); -const uint REPLICATE_3_BIT_TO_6_TABLE[8] = uint[](0, 9, 18, 27, 36, 45, 54, 63); -const uint REPLICATE_4_BIT_TO_6_TABLE[16] = - uint[](0, 4, 8, 12, 17, 21, 25, 29, 34, 38, 42, 46, 51, 55, 59, 63); -const uint REPLICATE_5_BIT_TO_6_TABLE[32] = - uint[](0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 33, 35, 37, 39, 41, 43, 45, - 47, 49, 51, 53, 55, 57, 59, 61, 63); -const uint REPLICATE_6_BIT_TO_8_TABLE[64] = - uint[](0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 65, 69, 73, 77, 81, 85, 89, - 93, 97, 101, 105, 109, 113, 117, 121, 125, 130, 134, 138, 142, 146, 150, 154, 158, 162, - 166, 170, 174, 178, 182, 186, 190, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, - 239, 243, 247, 251, 255); -const uint REPLICATE_7_BIT_TO_8_TABLE[128] = - uint[](0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, - 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, - 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, - 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, - 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, - 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, - 237, 239, 241, 243, 245, 247, 249, 251, 253, 255); - -// Input ASTC texture globals -int total_bitsread = 0; -uvec4 local_buff; - -// Color data globals -uvec4 color_endpoint_data; -int color_bitsread = 0; - -// Four values, two endpoints, four maximum partitions -uint color_values[32]; -int colvals_index = 0; - -// Global "vectors" to be pushed into when decoding -EncodingData result_vector[144]; -int result_index = 0; - -// Replicates low num_bits such that [(to_bit - 1):(to_bit - 1 - from_bit)] -// is the same as [(num_bits - 1):0] and repeats all the way down. -uint Replicate(uint val, uint num_bits, uint to_bit) { - const uint v = val & uint((1 << num_bits) - 1); - uint res = v; - uint reslen = num_bits; - while (reslen < to_bit) { - uint comp = 0; - if (num_bits > to_bit - reslen) { - uint newshift = to_bit - reslen; - comp = num_bits - newshift; - num_bits = newshift; - } - res = uint(res << num_bits); - res = uint(res | (v >> comp)); - reslen += num_bits; +void ResultEmplaceBack(EncodingData val) { + if (result_index >= result_vector_max_index) { + // Alert callers to avoid decoding more than needed by this phase + result_limit_reached = true; + return; } - return res; + const uint array_index = result_index / 4; + const uint vector_index = result_index % 4; + result_vector[array_index][vector_index] = val.data; + ++result_index; } uvec4 ReplicateByteTo16(uvec4 value) { @@ -183,64 +142,105 @@ uvec4 ReplicateByteTo16(uvec4 value) { } uint ReplicateBitTo7(uint value) { - return REPLICATE_BIT_TO_7_TABLE[value]; + return value * 127; } uint ReplicateBitTo9(uint value) { - return REPLICATE_1_BIT_TO_9_TABLE[value]; -} - -uint FastReplicate(uint value, uint num_bits, uint to_bit) { - if (num_bits == 0) { - return 0; - } - if (num_bits == to_bit) { - return value; - } - if (to_bit == 6) { - switch (num_bits) { - case 1: - return REPLICATE_1_BIT_TO_6_TABLE[value]; - case 2: - return REPLICATE_2_BIT_TO_6_TABLE[value]; - case 3: - return REPLICATE_3_BIT_TO_6_TABLE[value]; - case 4: - return REPLICATE_4_BIT_TO_6_TABLE[value]; - case 5: - return REPLICATE_5_BIT_TO_6_TABLE[value]; - default: - break; - } - } else { /* if (to_bit == 8) */ - switch (num_bits) { - case 1: - return REPLICATE_1_BIT_TO_8_TABLE[value]; - case 2: - return REPLICATE_2_BIT_TO_8_TABLE[value]; - case 3: - return REPLICATE_3_BIT_TO_8_TABLE[value]; - case 4: - return REPLICATE_4_BIT_TO_8_TABLE[value]; - case 5: - return REPLICATE_5_BIT_TO_8_TABLE[value]; - case 6: - return REPLICATE_6_BIT_TO_8_TABLE[value]; - case 7: - return REPLICATE_7_BIT_TO_8_TABLE[value]; - default: - break; - } - } - return Replicate(value, num_bits, to_bit); + return value * 511; } uint FastReplicateTo8(uint value, uint num_bits) { - return FastReplicate(value, num_bits, 8); + if (value == 0) { + return 0; + } + const uint array_index = value / 4; + const uint vector_index = value % 4; + switch (num_bits) { + case 1: + return 255; + case 2: { + const uvec4 REPLICATE_2_BIT_TO_8_TABLE = (uvec4(0, 85, 170, 255)); + return REPLICATE_2_BIT_TO_8_TABLE[vector_index]; + } + case 3: { + const uvec4 REPLICATE_3_BIT_TO_8_TABLE[2] = + uvec4[](uvec4(0, 36, 73, 109), uvec4(146, 182, 219, 255)); + return REPLICATE_3_BIT_TO_8_TABLE[array_index][vector_index]; + } + case 4: { + const uvec4 REPLICATE_4_BIT_TO_8_TABLE[4] = + uvec4[](uvec4(0, 17, 34, 51), uvec4(68, 85, 102, 119), uvec4(136, 153, 170, 187), + uvec4(204, 221, 238, 255)); + return REPLICATE_4_BIT_TO_8_TABLE[array_index][vector_index]; + } + case 5: { + const uvec4 REPLICATE_5_BIT_TO_8_TABLE[8] = + uvec4[](uvec4(0, 8, 16, 24), uvec4(33, 41, 49, 57), uvec4(66, 74, 82, 90), + uvec4(99, 107, 115, 123), uvec4(132, 140, 148, 156), uvec4(165, 173, 181, 189), + uvec4(198, 206, 214, 222), uvec4(231, 239, 247, 255)); + return REPLICATE_5_BIT_TO_8_TABLE[array_index][vector_index]; + } + case 6: { + const uvec4 REPLICATE_6_BIT_TO_8_TABLE[16] = uvec4[]( + uvec4(0, 4, 8, 12), uvec4(16, 20, 24, 28), uvec4(32, 36, 40, 44), uvec4(48, 52, 56, 60), + uvec4(65, 69, 73, 77), uvec4(81, 85, 89, 93), uvec4(97, 101, 105, 109), + uvec4(113, 117, 121, 125), uvec4(130, 134, 138, 142), uvec4(146, 150, 154, 158), + uvec4(162, 166, 170, 174), uvec4(178, 182, 186, 190), uvec4(195, 199, 203, 207), + uvec4(211, 215, 219, 223), uvec4(227, 231, 235, 239), uvec4(243, 247, 251, 255)); + return REPLICATE_6_BIT_TO_8_TABLE[array_index][vector_index]; + } + case 7: { + const uvec4 REPLICATE_7_BIT_TO_8_TABLE[32] = + uvec4[](uvec4(0, 2, 4, 6), uvec4(8, 10, 12, 14), uvec4(16, 18, 20, 22), + uvec4(24, 26, 28, 30), uvec4(32, 34, 36, 38), uvec4(40, 42, 44, 46), + uvec4(48, 50, 52, 54), uvec4(56, 58, 60, 62), uvec4(64, 66, 68, 70), + uvec4(72, 74, 76, 78), uvec4(80, 82, 84, 86), uvec4(88, 90, 92, 94), + uvec4(96, 98, 100, 102), uvec4(104, 106, 108, 110), uvec4(112, 114, 116, 118), + uvec4(120, 122, 124, 126), uvec4(129, 131, 133, 135), uvec4(137, 139, 141, 143), + uvec4(145, 147, 149, 151), uvec4(153, 155, 157, 159), uvec4(161, 163, 165, 167), + uvec4(169, 171, 173, 175), uvec4(177, 179, 181, 183), uvec4(185, 187, 189, 191), + uvec4(193, 195, 197, 199), uvec4(201, 203, 205, 207), uvec4(209, 211, 213, 215), + uvec4(217, 219, 221, 223), uvec4(225, 227, 229, 231), uvec4(233, 235, 237, 239), + uvec4(241, 243, 245, 247), uvec4(249, 251, 253, 255)); + return REPLICATE_7_BIT_TO_8_TABLE[array_index][vector_index]; + } + } + return value; } uint FastReplicateTo6(uint value, uint num_bits) { - return FastReplicate(value, num_bits, 6); + if (value == 0) { + return 0; + } + const uint array_index = value / 4; + const uint vector_index = value % 4; + switch (num_bits) { + case 1: + return 63; + case 2: { + const uvec4 REPLICATE_2_BIT_TO_6_TABLE = uvec4(0, 21, 42, 63); + return REPLICATE_2_BIT_TO_6_TABLE[vector_index]; + } + case 3: { + const uvec4 REPLICATE_3_BIT_TO_6_TABLE[2] = + uvec4[](uvec4(0, 9, 18, 27), uvec4(36, 45, 54, 63)); + return REPLICATE_3_BIT_TO_6_TABLE[array_index][vector_index]; + } + case 4: { + const uvec4 REPLICATE_4_BIT_TO_6_TABLE[4] = + uvec4[](uvec4(0, 4, 8, 12), uvec4(17, 21, 25, 29), uvec4(34, 38, 42, 46), + uvec4(51, 55, 59, 63)); + return REPLICATE_4_BIT_TO_6_TABLE[array_index][vector_index]; + } + case 5: { + const uvec4 REPLICATE_5_BIT_TO_6_TABLE[8] = + uvec4[](uvec4(0, 2, 4, 6), uvec4(8, 10, 12, 14), uvec4(16, 18, 20, 22), + uvec4(24, 26, 28, 30), uvec4(33, 35, 37, 39), uvec4(41, 43, 45, 47), + uvec4(49, 51, 53, 55), uvec4(57, 59, 61, 63)); + return REPLICATE_5_BIT_TO_6_TABLE[array_index][vector_index]; + } + } + return value; } uint Div3Floor(uint v) { @@ -281,7 +281,7 @@ uint Select2DPartition(uint seed, uint x, uint y, uint partition_count, bool sma seed += (partition_count - 1) * 1024; - uint rnum = Hash52(uint(seed)); + const uint rnum = Hash52(uint(seed)); uint seed1 = uint(rnum & 0xF); uint seed2 = uint((rnum >> 4) & 0xF); uint seed3 = uint((rnum >> 8) & 0xF); @@ -364,8 +364,8 @@ uint ExtractBits(uvec4 payload, int offset, int bits) { } uint StreamBits(uint num_bits) { - int int_bits = int(num_bits); - uint ret = ExtractBits(local_buff, total_bitsread, int_bits); + const int int_bits = int(num_bits); + const uint ret = ExtractBits(local_buff, total_bitsread, int_bits); total_bitsread += int_bits; return ret; } @@ -382,14 +382,18 @@ uint StreamColorBits(uint num_bits) { return ret; } -void ResultEmplaceBack(EncodingData val) { - result_vector[result_index] = val; - ++result_index; +EncodingData GetEncodingFromVector(uint index) { + const uint array_index = index / 4; + const uint vector_index = index % 4; + + const uint data = result_vector[array_index][vector_index]; + return EncodingData(data); } // Returns the number of bits required to encode n_vals values. uint GetBitLength(uint n_vals, uint encoding_index) { - const EncodingData encoding_value = encoding_values[encoding_index]; + const EncodingData encoding_value = + EncodingData(encoding_values[encoding_index / 4][encoding_index % 4]); const uint encoding = Encoding(encoding_value); uint total_bits = NumBits(encoding_value) * n_vals; if (encoding == TRIT) { @@ -409,7 +413,7 @@ uint GetNumWeightValues(uvec2 size, bool dual_plane) { } uint GetPackedBitSize(uvec2 size, bool dual_plane, uint max_weight) { - uint n_vals = GetNumWeightValues(size, dual_plane); + const uint n_vals = GetNumWeightValues(size, dual_plane); return GetBitLength(n_vals, max_weight); } @@ -418,13 +422,13 @@ uint BitsBracket(uint bits, uint pos) { } uint BitsOp(uint bits, uint start, uint end) { - uint mask = (1 << (end - start + 1)) - 1; + const uint mask = (1 << (end - start + 1)) - 1; return ((bits >> start) & mask); } void DecodeQuintBlock(uint num_bits) { - uint m[3]; - uint q[3]; + uvec3 m; + uvec3 q; uint Q; m[0] = StreamColorBits(num_bits); Q = StreamColorBits(3); @@ -433,25 +437,25 @@ void DecodeQuintBlock(uint num_bits) { m[2] = StreamColorBits(num_bits); Q |= StreamColorBits(2) << 5; if (BitsOp(Q, 1, 2) == 3 && BitsOp(Q, 5, 6) == 0) { - q[0] = 4; - q[1] = 4; - q[2] = (BitsBracket(Q, 0) << 2) | ((BitsBracket(Q, 4) & ~BitsBracket(Q, 0)) << 1) | - (BitsBracket(Q, 3) & ~BitsBracket(Q, 0)); + q.x = 4; + q.y = 4; + q.z = (BitsBracket(Q, 0) << 2) | ((BitsBracket(Q, 4) & ~BitsBracket(Q, 0)) << 1) | + (BitsBracket(Q, 3) & ~BitsBracket(Q, 0)); } else { uint C = 0; if (BitsOp(Q, 1, 2) == 3) { - q[2] = 4; + q.z = 4; C = (BitsOp(Q, 3, 4) << 3) | ((~BitsOp(Q, 5, 6) & 3) << 1) | BitsBracket(Q, 0); } else { - q[2] = BitsOp(Q, 5, 6); + q.z = BitsOp(Q, 5, 6); C = BitsOp(Q, 0, 4); } if (BitsOp(C, 0, 2) == 5) { - q[1] = 4; - q[0] = BitsOp(C, 3, 4); + q.y = 4; + q.x = BitsOp(C, 3, 4); } else { - q[1] = BitsOp(C, 3, 4); - q[0] = BitsOp(C, 0, 2); + q.y = BitsOp(C, 3, 4); + q.x = BitsOp(C, 0, 2); } } for (uint i = 0; i < 3; i++) { @@ -509,11 +513,11 @@ void DecodeTritBlock(uint num_bits) { } void DecodeIntegerSequence(uint max_range, uint num_values) { - EncodingData val = encoding_values[max_range]; + EncodingData val = EncodingData(encoding_values[max_range / 4][max_range % 4]); const uint encoding = Encoding(val); const uint num_bits = NumBits(val); uint vals_decoded = 0; - while (vals_decoded < num_values) { + while (vals_decoded < num_values && !result_limit_reached) { switch (encoding) { case QUINT: DecodeQuintBlock(num_bits); @@ -532,7 +536,8 @@ void DecodeIntegerSequence(uint max_range, uint num_values) { } } -void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { +void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits, + out uvec4 color_values[8]) { uint num_values = 0; for (uint i = 0; i < num_partitions; i++) { num_values += ((modes[i] >> 2) + 1) << 1; @@ -540,8 +545,8 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { // Find the largest encoding that's within color_data_bits // TODO(ameerj): profile with binary search int range = 0; - while (++range < encoding_values.length()) { - uint bit_length = GetBitLength(num_values, range); + while (++range < ((encoding_values.length() * 4) - 2)) { + const uint bit_length = GetBitLength(num_values, range); if (bit_length > color_data_bits) { break; } @@ -552,7 +557,7 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { if (out_index >= num_values) { break; } - const EncodingData val = result_vector[itr]; + const EncodingData val = GetEncodingFromVector(itr); const uint encoding = Encoding(val); const uint bitlen = NumBits(val); const uint bitval = BitValue(val); @@ -560,7 +565,8 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { A = ReplicateBitTo9((bitval & 1)); switch (encoding) { case JUST_BITS: - color_values[out_index++] = FastReplicateTo8(bitval, bitlen); + color_values[out_index / 4][out_index % 4] = FastReplicateTo8(bitval, bitlen); + ++out_index; break; case TRIT: { D = QuintTritValue(val); @@ -570,31 +576,31 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { break; case 2: { C = 93; - uint b = (bitval >> 1) & 1; + const uint b = (bitval >> 1) & 1; B = (b << 8) | (b << 4) | (b << 2) | (b << 1); break; } case 3: { C = 44; - uint cb = (bitval >> 1) & 3; + const uint cb = (bitval >> 1) & 3; B = (cb << 7) | (cb << 2) | cb; break; } case 4: { C = 22; - uint dcb = (bitval >> 1) & 7; + const uint dcb = (bitval >> 1) & 7; B = (dcb << 6) | dcb; break; } case 5: { C = 11; - uint edcb = (bitval >> 1) & 0xF; + const uint edcb = (bitval >> 1) & 0xF; B = (edcb << 5) | (edcb >> 2); break; } case 6: { C = 5; - uint fedcb = (bitval >> 1) & 0x1F; + const uint fedcb = (bitval >> 1) & 0x1F; B = (fedcb << 4) | (fedcb >> 4); break; } @@ -609,25 +615,25 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { break; case 2: { C = 54; - uint b = (bitval >> 1) & 1; + const uint b = (bitval >> 1) & 1; B = (b << 8) | (b << 3) | (b << 2); break; } case 3: { C = 26; - uint cb = (bitval >> 1) & 3; + const uint cb = (bitval >> 1) & 3; B = (cb << 7) | (cb << 1) | (cb >> 1); break; } case 4: { C = 13; - uint dcb = (bitval >> 1) & 7; + const uint dcb = (bitval >> 1) & 7; B = (dcb << 6) | (dcb >> 1); break; } case 5: { C = 6; - uint edcb = (bitval >> 1) & 0xF; + const uint edcb = (bitval >> 1) & 0xF; B = (edcb << 5) | (edcb >> 3); break; } @@ -639,7 +645,8 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { uint T = (D * C) + B; T ^= A; T = (A & 0x80) | (T >> 2); - color_values[out_index++] = T; + color_values[out_index / 4][out_index % 4] = T; + ++out_index; } } } @@ -657,25 +664,30 @@ ivec2 BitTransferSigned(int a, int b) { } uvec4 ClampByte(ivec4 color) { - const uvec4 clamped = uvec4(clamp(color, 0, 255)); - return clamped; + for (uint i = 0; i < 4; ++i) { + color[i] = clamp(color[i], 0, 255); + } + return uvec4(color); } ivec4 BlueContract(int a, int r, int g, int b) { return ivec4(a, (r + b) >> 1, (g + b) >> 1, b); } -void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode) { +void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, + in uvec4 color_values[8], inout uint colvals_index) { #define READ_UINT_VALUES(N) \ uint v[N]; \ for (uint i = 0; i < N; i++) { \ - v[i] = color_values[colvals_index++]; \ + v[i] = color_values[colvals_index / 4][colvals_index % 4]; \ + ++colvals_index; \ } #define READ_INT_VALUES(N) \ int v[N]; \ for (uint i = 0; i < N; i++) { \ - v[i] = int(color_values[colvals_index++]); \ + v[i] = int(color_values[colvals_index / 4][colvals_index % 4]); \ + ++colvals_index; \ } switch (color_endpoint_mode) { @@ -687,8 +699,8 @@ void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode) { } case 1: { READ_UINT_VALUES(2) - uint L0 = (v[0] >> 2) | (v[1] & 0xC0); - uint L1 = min(L0 + (v[1] & 0x3F), 0xFFU); + const uint L0 = (v[0] >> 2) | (v[1] & 0xC0); + const uint L1 = min(L0 + (v[1] & 0x3F), 0xFFU); ep1 = uvec4(0xFF, L0, L0, L0); ep2 = uvec4(0xFF, L1, L1, L1); break; @@ -817,7 +829,7 @@ uint UnquantizeTexelWeight(EncodingData val) { D = QuintTritValue(val); switch (bitlen) { case 0: { - uint results[3] = {0, 32, 63}; + const uint results[3] = {0, 32, 63}; result = results[D]; break; } @@ -827,13 +839,13 @@ uint UnquantizeTexelWeight(EncodingData val) { } case 2: { C = 23; - uint b = (bitval >> 1) & 1; + const uint b = (bitval >> 1) & 1; B = (b << 6) | (b << 2) | b; break; } case 3: { C = 11; - uint cb = (bitval >> 1) & 3; + const uint cb = (bitval >> 1) & 3; B = (cb << 5) | cb; break; } @@ -846,7 +858,7 @@ uint UnquantizeTexelWeight(EncodingData val) { D = QuintTritValue(val); switch (bitlen) { case 0: { - uint results[5] = {0, 16, 32, 47, 63}; + const uint results[5] = {0, 16, 32, 47, 63}; result = results[D]; break; } @@ -856,7 +868,7 @@ uint UnquantizeTexelWeight(EncodingData val) { } case 2: { C = 13; - uint b = (bitval >> 1) & 1; + const uint b = (bitval >> 1) & 1; B = (b << 6) | (b << 1); break; } @@ -875,15 +887,18 @@ uint UnquantizeTexelWeight(EncodingData val) { return result; } -void UnquantizeTexelWeights(bool is_dual_plane, uvec2 size, out uint unquantized_texel_weights[2 * 144]) { +void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane, + out uvec4 unquantized_texel_weights[VECTOR_ARRAY_SIZE]) { const uint Ds = uint((block_dims.x * 0.5f + 1024) / (block_dims.x - 1)); const uint Dt = uint((block_dims.y * 0.5f + 1024) / (block_dims.y - 1)); const uint num_planes = is_dual_plane ? 2 : 1; const uint area = size.x * size.y; const uint loop_count = min(result_index, area * num_planes); - uint unquantized[2 * 144]; for (uint itr = 0; itr < loop_count; ++itr) { - unquantized[itr] = UnquantizeTexelWeight(result_vector[itr]); + const uint array_index = itr / 4; + const uint vector_index = itr % 4; + result_vector[array_index][vector_index] = + UnquantizeTexelWeight(GetEncodingFromVector(itr)); } for (uint plane = 0; plane < num_planes; ++plane) { for (uint t = 0; t < block_dims.y; t++) { @@ -907,28 +922,33 @@ void UnquantizeTexelWeights(bool is_dual_plane, uvec2 size, out uint unquantized #define VectorIndicesFromBase(offset_base) \ const uint offset = is_dual_plane ? 2 * offset_base + plane : offset_base; \ + const uint array_index = offset / 4; \ + const uint vector_index = offset % 4; if (v0 < area) { const uint offset_base = v0; VectorIndicesFromBase(offset_base); - p.x = unquantized[offset]; + p.x = result_vector[array_index][vector_index]; } if ((v0 + 1) < (area)) { const uint offset_base = v0 + 1; VectorIndicesFromBase(offset_base); - p.y = unquantized[offset]; + p.y = result_vector[array_index][vector_index]; } if ((v0 + size.x) < (area)) { const uint offset_base = v0 + size.x; VectorIndicesFromBase(offset_base); - p.z = unquantized[offset]; + p.z = result_vector[array_index][vector_index]; } if ((v0 + size.x + 1) < (area)) { const uint offset_base = v0 + size.x + 1; VectorIndicesFromBase(offset_base); - p.w = unquantized[offset]; + p.w = result_vector[array_index][vector_index]; } - unquantized_texel_weights[plane * 144 + t * block_dims.x + s] = (uint(dot(p, w)) + 8) >> 4; + const uint offset = (t * block_dims.x + s) + ARRAY_NUM_ELEMENTS * plane; + const uint array_index = offset / 4; + const uint vector_index = offset % 4; + unquantized_texel_weights[array_index][vector_index] = (uint(dot(p, w)) + 8) >> 4; } } } @@ -1050,6 +1070,7 @@ TexelWeightParams DecodeBlockInfo() { weight_index += 6; } params.max_weight = weight_index + 1; + return params; } @@ -1079,7 +1100,7 @@ void FillVoidExtentLDR(ivec3 coord) { } void DecompressBlock(ivec3 coord) { - TexelWeightParams params = DecodeBlockInfo(); + const TexelWeightParams params = DecodeBlockInfo(); if (params.error_state) { FillError(coord); return; @@ -1096,12 +1117,11 @@ void DecompressBlock(ivec3 coord) { FillError(coord); return; } - uint num_partitions = StreamBits(2) + 1; + const uint num_partitions = StreamBits(2) + 1; if (num_partitions > 4 || (num_partitions == 4 && params.dual_plane)) { FillError(coord); return; } - int plane_index = -1; uint partition_index = 1; uvec4 color_endpoint_mode = uvec4(0); uint ced_pointer = 0; @@ -1113,8 +1133,8 @@ void DecompressBlock(ivec3 coord) { partition_index = StreamBits(10); base_cem = StreamBits(6); } - uint base_mode = base_cem & 3; - uint weight_bits = GetPackedBitSize(params.size, params.dual_plane, params.max_weight); + const uint base_mode = base_cem & 3; + const uint weight_bits = GetPackedBitSize(params.size, params.dual_plane, params.max_weight); uint remaining_bits = 128 - weight_bits - total_bitsread; uint extra_cem_bits = 0; if (base_mode > 0) { @@ -1133,10 +1153,7 @@ void DecompressBlock(ivec3 coord) { } } remaining_bits -= extra_cem_bits; - uint plane_selector_bits = 0; - if (params.dual_plane) { - plane_selector_bits = 2; - } + const uint plane_selector_bits = params.dual_plane ? 2 : 0; remaining_bits -= plane_selector_bits; if (remaining_bits > 128) { // Bad data, more remaining bits than 4 bytes @@ -1144,17 +1161,17 @@ void DecompressBlock(ivec3 coord) { return; } // Read color data... - uint color_data_bits = remaining_bits; + const uint color_data_bits = remaining_bits; while (remaining_bits > 0) { - int nb = int(min(remaining_bits, 32U)); - uint b = StreamBits(nb); + const int nb = int(min(remaining_bits, 32U)); + const uint b = StreamBits(nb); color_endpoint_data[ced_pointer] = uint(bitfieldExtract(b, 0, nb)); ++ced_pointer; remaining_bits -= nb; } - plane_index = int(StreamBits(plane_selector_bits)); + const uint plane_index = uint(StreamBits(plane_selector_bits)); if (base_mode > 0) { - uint extra_cem = StreamBits(extra_cem_bits); + const uint extra_cem = StreamBits(extra_cem_bits); uint cem = (extra_cem << 6) | base_cem; cem >>= 2; uvec4 C = uvec4(0); @@ -1176,43 +1193,54 @@ void DecompressBlock(ivec3 coord) { color_endpoint_mode[i] |= M[i]; } } else if (num_partitions > 1) { - uint cem = base_cem >> 2; + const uint cem = base_cem >> 2; for (uint i = 0; i < num_partitions; i++) { color_endpoint_mode[i] = cem; } } - DecodeColorValues(color_endpoint_mode, num_partitions, color_data_bits); - uvec4 endpoints[4][2]; - for (uint i = 0; i < num_partitions; i++) { - ComputeEndpoints(endpoints[i][0], endpoints[i][1], color_endpoint_mode[i]); + uvec4 endpoints0[4]; + uvec4 endpoints1[4]; + { + // This decode phase should at most push 32 elements into the vector + result_vector_max_index = 32; + + uvec4 color_values[8]; + uint colvals_index = 0; + DecodeColorValues(color_endpoint_mode, num_partitions, color_data_bits, color_values); + for (uint i = 0; i < num_partitions; i++) { + ComputeEndpoints(endpoints0[i], endpoints1[i], color_endpoint_mode[i], color_values, + colvals_index); + } } - color_endpoint_data = local_buff; color_endpoint_data = bitfieldReverse(color_endpoint_data).wzyx; - uint clear_byte_start = - (GetPackedBitSize(params.size, params.dual_plane, params.max_weight) >> 3) + 1; + const uint clear_byte_start = (weight_bits >> 3) + 1; - uint byte_insert = ExtractBits(color_endpoint_data, int(clear_byte_start - 1) * 8, 8) & - uint( - ((1 << (GetPackedBitSize(params.size, params.dual_plane, params.max_weight) % 8)) - 1)); - uint vec_index = (clear_byte_start - 1) >> 2; - color_endpoint_data[vec_index] = - bitfieldInsert(color_endpoint_data[vec_index], byte_insert, int((clear_byte_start - 1) % 4) * 8, 8); + const uint byte_insert = ExtractBits(color_endpoint_data, int(clear_byte_start - 1) * 8, 8) & + uint(((1 << (weight_bits % 8)) - 1)); + const uint vec_index = (clear_byte_start - 1) >> 2; + color_endpoint_data[vec_index] = bitfieldInsert(color_endpoint_data[vec_index], byte_insert, + int((clear_byte_start - 1) % 4) * 8, 8); for (uint i = clear_byte_start; i < 16; ++i) { - uint idx = i >> 2; + const uint idx = i >> 2; color_endpoint_data[idx] = bitfieldInsert(color_endpoint_data[idx], 0, int(i % 4) * 8, 8); } // Re-init vector variables for next decode phase result_index = 0; color_bitsread = 0; + result_limit_reached = false; + // The limit for the Unquantize phase, avoids decoding more data than needed. + result_vector_max_index = params.size.x * params.size.y; + if (params.dual_plane) { + result_vector_max_index *= 2; + } DecodeIntegerSequence(params.max_weight, GetNumWeightValues(params.size, params.dual_plane)); - uint unquantized_texel_weights[2 * 144]; - UnquantizeTexelWeights(params.dual_plane, params.size, unquantized_texel_weights); - + uvec4 unquantized_texel_weights[VECTOR_ARRAY_SIZE]; + UnquantizeTexelWeights(params.size, params.dual_plane, unquantized_texel_weights); for (uint j = 0; j < block_dims.y; j++) { for (uint i = 0; i < block_dims.x; i++) { uint local_partition = 0; @@ -1220,13 +1248,19 @@ void DecompressBlock(ivec3 coord) { local_partition = Select2DPartition(partition_index, i, j, num_partitions, (block_dims.y * block_dims.x) < 32); } - const uvec4 C0 = ReplicateByteTo16(endpoints[local_partition][0]); - const uvec4 C1 = ReplicateByteTo16(endpoints[local_partition][1]); + const uvec4 C0 = ReplicateByteTo16(endpoints0[local_partition]); + const uvec4 C1 = ReplicateByteTo16(endpoints1[local_partition]); const uint weight_offset = (j * block_dims.x + i); - const uint primary_weight = unquantized_texel_weights[weight_offset]; + const uint array_index = weight_offset / 4; + const uint vector_index = weight_offset % 4; + const uint primary_weight = unquantized_texel_weights[array_index][vector_index]; uvec4 weight_vec = uvec4(primary_weight); if (params.dual_plane) { - const uint secondary_weight = unquantized_texel_weights[weight_offset + 144]; + const uint secondary_weight_offset = (j * block_dims.x + i) + ARRAY_NUM_ELEMENTS; + const uint secondary_array_index = secondary_weight_offset / 4; + const uint secondary_vector_index = secondary_weight_offset % 4; + const uint secondary_weight = + unquantized_texel_weights[secondary_array_index][secondary_vector_index]; for (uint c = 0; c < 4; c++) { const bool is_secondary = ((plane_index + 1u) & 3u) == c; weight_vec[c] = is_secondary ? secondary_weight : primary_weight; @@ -1240,12 +1274,11 @@ void DecompressBlock(ivec3 coord) { } } - uint SwizzleOffset(uvec2 pos) { - uint x = pos.x; - uint y = pos.y; - return ((x % 64) / 32) * 256 + ((y % 8) / 2) * 64 + ((x % 32) / 16) * 32 + - (y % 2) * 16 + (x % 16); + const uint x = pos.x; + const uint y = pos.y; + return ((x % 64) / 32) * 256 + ((y % 8) / 2) * 64 + + ((x % 32) / 16) * 32 + (y % 2) * 16 + (x % 16); } void main() { From 3494fce86467878cd62a2faf54da2958b7495a17 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 19:43:22 -0400 Subject: [PATCH 11/33] gl --- src/video_core/renderer_opengl/util_shaders.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/video_core/renderer_opengl/util_shaders.cpp b/src/video_core/renderer_opengl/util_shaders.cpp index 544982d18..c437013e6 100644 --- a/src/video_core/renderer_opengl/util_shaders.cpp +++ b/src/video_core/renderer_opengl/util_shaders.cpp @@ -68,6 +68,7 @@ void UtilShaders::ASTCDecode(Image& image, const StagingBufferMap& map, std::span swizzles) { static constexpr GLuint BINDING_INPUT_BUFFER = 0; static constexpr GLuint BINDING_OUTPUT_IMAGE = 0; + program_manager.LocalMemoryWarmup(); const Extent2D tile_size{ .width = VideoCore::Surface::DefaultBlockWidth(image.info.format), From 05ee37a1f0641a2e1a15e9052371a5ce0cd20058 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 19:48:19 -0400 Subject: [PATCH 12/33] amd opts --- src/video_core/host_shaders/astc_decoder.comp | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index e8801b0ff..ca93dc2a2 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -154,7 +154,7 @@ uint FastReplicateTo8(uint value, uint num_bits) { return 0; } const uint array_index = value / 4; - const uint vector_index = value % 4; + const uint vector_index = bitfieldExtract(value, 0, 2); switch (num_bits) { case 1: return 255; @@ -213,7 +213,7 @@ uint FastReplicateTo6(uint value, uint num_bits) { return 0; } const uint array_index = value / 4; - const uint vector_index = value % 4; + const uint vector_index = bitfieldExtract(value, 0, 2); switch (num_bits) { case 1: return 63; @@ -536,8 +536,8 @@ void DecodeIntegerSequence(uint max_range, uint num_values) { } } -void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits, - out uvec4 color_values[8]) { +uvec4 color_values[8]; +void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { uint num_values = 0; for (uint i = 0; i < num_partitions; i++) { num_values += ((modes[i] >> 2) + 1) << 1; @@ -664,10 +664,7 @@ ivec2 BitTransferSigned(int a, int b) { } uvec4 ClampByte(ivec4 color) { - for (uint i = 0; i < 4; ++i) { - color[i] = clamp(color[i], 0, 255); - } - return uvec4(color); + return uvec4(clamp(color, 0, 255)); } ivec4 BlueContract(int a, int r, int g, int b) { @@ -675,7 +672,7 @@ ivec4 BlueContract(int a, int r, int g, int b) { } void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, - in uvec4 color_values[8], inout uint colvals_index) { + inout uint colvals_index) { #define READ_UINT_VALUES(N) \ uint v[N]; \ for (uint i = 0; i < N; i++) { \ @@ -887,8 +884,9 @@ uint UnquantizeTexelWeight(EncodingData val) { return result; } -void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane, - out uvec4 unquantized_texel_weights[VECTOR_ARRAY_SIZE]) { +uvec4 unquantized_texel_weights[VECTOR_ARRAY_SIZE]; + +void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { const uint Ds = uint((block_dims.x * 0.5f + 1024) / (block_dims.x - 1)); const uint Dt = uint((block_dims.y * 0.5f + 1024) / (block_dims.y - 1)); const uint num_planes = is_dual_plane ? 2 : 1; @@ -1205,11 +1203,11 @@ void DecompressBlock(ivec3 coord) { // This decode phase should at most push 32 elements into the vector result_vector_max_index = 32; - uvec4 color_values[8]; + // uvec4 color_values[8]; uint colvals_index = 0; - DecodeColorValues(color_endpoint_mode, num_partitions, color_data_bits, color_values); + DecodeColorValues(color_endpoint_mode, num_partitions, color_data_bits); for (uint i = 0; i < num_partitions; i++) { - ComputeEndpoints(endpoints0[i], endpoints1[i], color_endpoint_mode[i], color_values, + ComputeEndpoints(endpoints0[i], endpoints1[i], color_endpoint_mode[i], colvals_index); } } @@ -1239,8 +1237,7 @@ void DecompressBlock(ivec3 coord) { } DecodeIntegerSequence(params.max_weight, GetNumWeightValues(params.size, params.dual_plane)); - uvec4 unquantized_texel_weights[VECTOR_ARRAY_SIZE]; - UnquantizeTexelWeights(params.size, params.dual_plane, unquantized_texel_weights); + UnquantizeTexelWeights(params.size, params.dual_plane); for (uint j = 0; j < block_dims.y; j++) { for (uint i = 0; i < block_dims.x; i++) { uint local_partition = 0; From 6b0b584eba111dfd716990b62499c307c930f555 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 20:11:01 -0400 Subject: [PATCH 13/33] uint result index --- src/video_core/host_shaders/astc_decoder.comp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index ca93dc2a2..fd38dcfe5 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -88,7 +88,7 @@ int color_bitsread = 0; #define VECTOR_ARRAY_SIZE DIVCEIL(ARRAY_NUM_ELEMENTS * 2, 4) uvec4 result_vector[VECTOR_ARRAY_SIZE]; -int result_index = 0; +uint result_index = 0; uint result_vector_max_index; bool result_limit_reached = false; From 5c1655969478ca0b47c9f52d333f798b614b71ea Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 20:10:46 -0400 Subject: [PATCH 14/33] bfe instead of mod --- src/video_core/host_shaders/astc_decoder.comp | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index fd38dcfe5..5346cba0c 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -21,6 +21,8 @@ #endif +#define bfe bitfieldExtract + layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; BEGIN_PUSH_CONSTANTS @@ -132,7 +134,7 @@ void ResultEmplaceBack(EncodingData val) { return; } const uint array_index = result_index / 4; - const uint vector_index = result_index % 4; + const uint vector_index = bfe(result_index, 0, 2); result_vector[array_index][vector_index] = val.data; ++result_index; } @@ -384,7 +386,7 @@ uint StreamColorBits(uint num_bits) { EncodingData GetEncodingFromVector(uint index) { const uint array_index = index / 4; - const uint vector_index = index % 4; + const uint vector_index = bfe(index, 0, 2); const uint data = result_vector[array_index][vector_index]; return EncodingData(data); @@ -393,7 +395,7 @@ EncodingData GetEncodingFromVector(uint index) { // Returns the number of bits required to encode n_vals values. uint GetBitLength(uint n_vals, uint encoding_index) { const EncodingData encoding_value = - EncodingData(encoding_values[encoding_index / 4][encoding_index % 4]); + EncodingData(encoding_values[encoding_index / 4][bfe(encoding_index, 0, 2)]); const uint encoding = Encoding(encoding_value); uint total_bits = NumBits(encoding_value) * n_vals; if (encoding == TRIT) { @@ -513,7 +515,7 @@ void DecodeTritBlock(uint num_bits) { } void DecodeIntegerSequence(uint max_range, uint num_values) { - EncodingData val = EncodingData(encoding_values[max_range / 4][max_range % 4]); + EncodingData val = EncodingData(encoding_values[max_range / 4][bfe(max_range, 0, 2)]); const uint encoding = Encoding(val); const uint num_bits = NumBits(val); uint vals_decoded = 0; @@ -565,7 +567,7 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { A = ReplicateBitTo9((bitval & 1)); switch (encoding) { case JUST_BITS: - color_values[out_index / 4][out_index % 4] = FastReplicateTo8(bitval, bitlen); + color_values[out_index / 4][bfe(out_index, 0, 2)] = FastReplicateTo8(bitval, bitlen); ++out_index; break; case TRIT: { @@ -645,7 +647,7 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { uint T = (D * C) + B; T ^= A; T = (A & 0x80) | (T >> 2); - color_values[out_index / 4][out_index % 4] = T; + color_values[out_index / 4][bfe(out_index, 0, 2)] = T; ++out_index; } } @@ -676,14 +678,14 @@ void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, #define READ_UINT_VALUES(N) \ uint v[N]; \ for (uint i = 0; i < N; i++) { \ - v[i] = color_values[colvals_index / 4][colvals_index % 4]; \ + v[i] = color_values[colvals_index / 4][bfe(colvals_index, 0, 2)]; \ ++colvals_index; \ } #define READ_INT_VALUES(N) \ int v[N]; \ for (uint i = 0; i < N; i++) { \ - v[i] = int(color_values[colvals_index / 4][colvals_index % 4]); \ + v[i] = int(color_values[colvals_index / 4][bfe(colvals_index, 0, 2)]); \ ++colvals_index; \ } @@ -894,7 +896,7 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { const uint loop_count = min(result_index, area * num_planes); for (uint itr = 0; itr < loop_count; ++itr) { const uint array_index = itr / 4; - const uint vector_index = itr % 4; + const uint vector_index = bfe(itr, 0, 2); result_vector[array_index][vector_index] = UnquantizeTexelWeight(GetEncodingFromVector(itr)); } @@ -921,7 +923,7 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { #define VectorIndicesFromBase(offset_base) \ const uint offset = is_dual_plane ? 2 * offset_base + plane : offset_base; \ const uint array_index = offset / 4; \ - const uint vector_index = offset % 4; + const uint vector_index = bfe(offset, 0, 2); if (v0 < area) { const uint offset_base = v0; @@ -945,7 +947,7 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { } const uint offset = (t * block_dims.x + s) + ARRAY_NUM_ELEMENTS * plane; const uint array_index = offset / 4; - const uint vector_index = offset % 4; + const uint vector_index = bfe(offset, 0, 2); unquantized_texel_weights[array_index][vector_index] = (uint(dot(p, w)) + 8) >> 4; } } @@ -1249,13 +1251,13 @@ void DecompressBlock(ivec3 coord) { const uvec4 C1 = ReplicateByteTo16(endpoints1[local_partition]); const uint weight_offset = (j * block_dims.x + i); const uint array_index = weight_offset / 4; - const uint vector_index = weight_offset % 4; + const uint vector_index = bfe(weight_offset, 0, 2); const uint primary_weight = unquantized_texel_weights[array_index][vector_index]; uvec4 weight_vec = uvec4(primary_weight); if (params.dual_plane) { const uint secondary_weight_offset = (j * block_dims.x + i) + ARRAY_NUM_ELEMENTS; const uint secondary_array_index = secondary_weight_offset / 4; - const uint secondary_vector_index = secondary_weight_offset % 4; + const uint secondary_vector_index = bfe(secondary_weight_offset, 0, 2); const uint secondary_weight = unquantized_texel_weights[secondary_array_index][secondary_vector_index]; for (uint c = 0; c < 4; c++) { From c077e467c4fb10ef92c1369e9b9116ed653af1de Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 20:19:45 -0400 Subject: [PATCH 15/33] global endpoints --- src/video_core/host_shaders/astc_decoder.comp | 76 +++++++++---------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 5346cba0c..077bec576 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -94,6 +94,8 @@ uint result_index = 0; uint result_vector_max_index; bool result_limit_reached = false; +uvec4 endpoints[2][4]; + // EncodingData helpers uint Encoding(EncodingData val) { return bitfieldExtract(val.data, 0, 8); @@ -673,7 +675,7 @@ ivec4 BlueContract(int a, int r, int g, int b) { return ivec4(a, (r + b) >> 1, (g + b) >> 1, b); } -void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, +void ComputeEndpoints(uint ep_index, uint color_endpoint_mode, inout uint colvals_index) { #define READ_UINT_VALUES(N) \ uint v[N]; \ @@ -692,22 +694,22 @@ void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, switch (color_endpoint_mode) { case 0: { READ_UINT_VALUES(2) - ep1 = uvec4(0xFF, v[0], v[0], v[0]); - ep2 = uvec4(0xFF, v[1], v[1], v[1]); + endpoints[0][ep_index] = uvec4(0xFF, v[0], v[0], v[0]); + endpoints[1][ep_index] = uvec4(0xFF, v[1], v[1], v[1]); break; } case 1: { READ_UINT_VALUES(2) const uint L0 = (v[0] >> 2) | (v[1] & 0xC0); const uint L1 = min(L0 + (v[1] & 0x3F), 0xFFU); - ep1 = uvec4(0xFF, L0, L0, L0); - ep2 = uvec4(0xFF, L1, L1, L1); + endpoints[0][ep_index] = uvec4(0xFF, L0, L0, L0); + endpoints[1][ep_index] = uvec4(0xFF, L1, L1, L1); break; } case 4: { READ_UINT_VALUES(4) - ep1 = uvec4(v[2], v[0], v[0], v[0]); - ep2 = uvec4(v[3], v[1], v[1], v[1]); + endpoints[0][ep_index] = uvec4(v[2], v[0], v[0], v[0]); + endpoints[1][ep_index] = uvec4(v[3], v[1], v[1], v[1]); break; } case 5: { @@ -718,24 +720,24 @@ void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, transferred = BitTransferSigned(v[3], v[2]); v[3] = transferred.x; v[2] = transferred.y; - ep1 = ClampByte(ivec4(v[2], v[0], v[0], v[0])); - ep2 = ClampByte(ivec4(v[2] + v[3], v[0] + v[1], v[0] + v[1], v[0] + v[1])); + endpoints[0][ep_index] = ClampByte(ivec4(v[2], v[0], v[0], v[0])); + endpoints[1][ep_index] = ClampByte(ivec4(v[2] + v[3], v[0] + v[1], v[0] + v[1], v[0] + v[1])); break; } case 6: { READ_UINT_VALUES(4) - ep1 = uvec4(0xFF, (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8); - ep2 = uvec4(0xFF, v[0], v[1], v[2]); + endpoints[0][ep_index] = uvec4(0xFF, (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8); + endpoints[1][ep_index] = uvec4(0xFF, v[0], v[1], v[2]); break; } case 8: { READ_UINT_VALUES(6) if ((v[1] + v[3] + v[5]) >= (v[0] + v[2] + v[4])) { - ep1 = uvec4(0xFF, v[0], v[2], v[4]); - ep2 = uvec4(0xFF, v[1], v[3], v[5]); + endpoints[0][ep_index] = uvec4(0xFF, v[0], v[2], v[4]); + endpoints[1][ep_index] = uvec4(0xFF, v[1], v[3], v[5]); } else { - ep1 = uvec4(BlueContract(0xFF, int(v[1]), int(v[3]), int(v[5]))); - ep2 = uvec4(BlueContract(0xFF, int(v[0]), int(v[2]), int(v[4]))); + endpoints[0][ep_index] = uvec4(BlueContract(0xFF, int(v[1]), int(v[3]), int(v[5]))); + endpoints[1][ep_index] = uvec4(BlueContract(0xFF, int(v[0]), int(v[2]), int(v[4]))); } break; } @@ -751,28 +753,28 @@ void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, v[5] = transferred.x; v[4] = transferred.y; if ((v[1] + v[3] + v[5]) >= 0) { - ep1 = ClampByte(ivec4(0xFF, v[0], v[2], v[4])); - ep2 = ClampByte(ivec4(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5])); + endpoints[0][ep_index] = ClampByte(ivec4(0xFF, v[0], v[2], v[4])); + endpoints[1][ep_index] = ClampByte(ivec4(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5])); } else { - ep1 = ClampByte(BlueContract(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5])); - ep2 = ClampByte(BlueContract(0xFF, v[0], v[2], v[4])); + endpoints[0][ep_index] = ClampByte(BlueContract(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5])); + endpoints[1][ep_index] = ClampByte(BlueContract(0xFF, v[0], v[2], v[4])); } break; } case 10: { READ_UINT_VALUES(6) - ep1 = uvec4(v[4], (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8); - ep2 = uvec4(v[5], v[0], v[1], v[2]); + endpoints[0][ep_index] = uvec4(v[4], (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8); + endpoints[1][ep_index] = uvec4(v[5], v[0], v[1], v[2]); break; } case 12: { READ_UINT_VALUES(8) if ((v[1] + v[3] + v[5]) >= (v[0] + v[2] + v[4])) { - ep1 = uvec4(v[6], v[0], v[2], v[4]); - ep2 = uvec4(v[7], v[1], v[3], v[5]); + endpoints[0][ep_index] = uvec4(v[6], v[0], v[2], v[4]); + endpoints[1][ep_index] = uvec4(v[7], v[1], v[3], v[5]); } else { - ep1 = uvec4(BlueContract(int(v[7]), int(v[1]), int(v[3]), int(v[5]))); - ep2 = uvec4(BlueContract(int(v[6]), int(v[0]), int(v[2]), int(v[4]))); + endpoints[0][ep_index] = uvec4(BlueContract(int(v[7]), int(v[1]), int(v[3]), int(v[5]))); + endpoints[1][ep_index] = uvec4(BlueContract(int(v[6]), int(v[0]), int(v[2]), int(v[4]))); } break; } @@ -794,18 +796,18 @@ void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, v[6] = transferred.y; if ((v[1] + v[3] + v[5]) >= 0) { - ep1 = ClampByte(ivec4(v[6], v[0], v[2], v[4])); - ep2 = ClampByte(ivec4(v[7] + v[6], v[0] + v[1], v[2] + v[3], v[4] + v[5])); + endpoints[0][ep_index] = ClampByte(ivec4(v[6], v[0], v[2], v[4])); + endpoints[1][ep_index] = ClampByte(ivec4(v[7] + v[6], v[0] + v[1], v[2] + v[3], v[4] + v[5])); } else { - ep1 = ClampByte(BlueContract(v[6] + v[7], v[0] + v[1], v[2] + v[3], v[4] + v[5])); - ep2 = ClampByte(BlueContract(v[6], v[0], v[2], v[4])); + endpoints[0][ep_index] = ClampByte(BlueContract(v[6] + v[7], v[0] + v[1], v[2] + v[3], v[4] + v[5])); + endpoints[1][ep_index] = ClampByte(BlueContract(v[6], v[0], v[2], v[4])); } break; } default: { // HDR mode, or more likely a bug computing the color_endpoint_mode - ep1 = uvec4(0xFF, 0xFF, 0, 0); - ep2 = uvec4(0xFF, 0xFF, 0, 0); + endpoints[0][ep_index] = uvec4(0xFF, 0xFF, 0, 0); + endpoints[1][ep_index] = uvec4(0xFF, 0xFF, 0, 0); break; } } @@ -1198,10 +1200,6 @@ void DecompressBlock(ivec3 coord) { color_endpoint_mode[i] = cem; } } - - uvec4 endpoints0[4]; - uvec4 endpoints1[4]; - { // This decode phase should at most push 32 elements into the vector result_vector_max_index = 32; @@ -1209,10 +1207,8 @@ void DecompressBlock(ivec3 coord) { uint colvals_index = 0; DecodeColorValues(color_endpoint_mode, num_partitions, color_data_bits); for (uint i = 0; i < num_partitions; i++) { - ComputeEndpoints(endpoints0[i], endpoints1[i], color_endpoint_mode[i], - colvals_index); + ComputeEndpoints(i, color_endpoint_mode[i], colvals_index); } - } color_endpoint_data = local_buff; color_endpoint_data = bitfieldReverse(color_endpoint_data).wzyx; const uint clear_byte_start = (weight_bits >> 3) + 1; @@ -1247,8 +1243,8 @@ void DecompressBlock(ivec3 coord) { local_partition = Select2DPartition(partition_index, i, j, num_partitions, (block_dims.y * block_dims.x) < 32); } - const uvec4 C0 = ReplicateByteTo16(endpoints0[local_partition]); - const uvec4 C1 = ReplicateByteTo16(endpoints1[local_partition]); + const uvec4 C0 = ReplicateByteTo16(endpoints[0][local_partition]); + const uvec4 C1 = ReplicateByteTo16(endpoints[1][local_partition]); const uint weight_offset = (j * block_dims.x + i); const uint array_index = weight_offset / 4; const uint vector_index = bfe(weight_offset, 0, 2); From 553dd3e1202638fa8bad9fed56110ee447208ecf Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 20:53:25 -0400 Subject: [PATCH 16/33] Revert "global endpoints" This reverts commit d8f5bfd1df2b7469ef6abcee182aa110602d1751. --- src/video_core/host_shaders/astc_decoder.comp | 76 ++++++++++--------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 077bec576..5346cba0c 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -94,8 +94,6 @@ uint result_index = 0; uint result_vector_max_index; bool result_limit_reached = false; -uvec4 endpoints[2][4]; - // EncodingData helpers uint Encoding(EncodingData val) { return bitfieldExtract(val.data, 0, 8); @@ -675,7 +673,7 @@ ivec4 BlueContract(int a, int r, int g, int b) { return ivec4(a, (r + b) >> 1, (g + b) >> 1, b); } -void ComputeEndpoints(uint ep_index, uint color_endpoint_mode, +void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, inout uint colvals_index) { #define READ_UINT_VALUES(N) \ uint v[N]; \ @@ -694,22 +692,22 @@ void ComputeEndpoints(uint ep_index, uint color_endpoint_mode, switch (color_endpoint_mode) { case 0: { READ_UINT_VALUES(2) - endpoints[0][ep_index] = uvec4(0xFF, v[0], v[0], v[0]); - endpoints[1][ep_index] = uvec4(0xFF, v[1], v[1], v[1]); + ep1 = uvec4(0xFF, v[0], v[0], v[0]); + ep2 = uvec4(0xFF, v[1], v[1], v[1]); break; } case 1: { READ_UINT_VALUES(2) const uint L0 = (v[0] >> 2) | (v[1] & 0xC0); const uint L1 = min(L0 + (v[1] & 0x3F), 0xFFU); - endpoints[0][ep_index] = uvec4(0xFF, L0, L0, L0); - endpoints[1][ep_index] = uvec4(0xFF, L1, L1, L1); + ep1 = uvec4(0xFF, L0, L0, L0); + ep2 = uvec4(0xFF, L1, L1, L1); break; } case 4: { READ_UINT_VALUES(4) - endpoints[0][ep_index] = uvec4(v[2], v[0], v[0], v[0]); - endpoints[1][ep_index] = uvec4(v[3], v[1], v[1], v[1]); + ep1 = uvec4(v[2], v[0], v[0], v[0]); + ep2 = uvec4(v[3], v[1], v[1], v[1]); break; } case 5: { @@ -720,24 +718,24 @@ void ComputeEndpoints(uint ep_index, uint color_endpoint_mode, transferred = BitTransferSigned(v[3], v[2]); v[3] = transferred.x; v[2] = transferred.y; - endpoints[0][ep_index] = ClampByte(ivec4(v[2], v[0], v[0], v[0])); - endpoints[1][ep_index] = ClampByte(ivec4(v[2] + v[3], v[0] + v[1], v[0] + v[1], v[0] + v[1])); + ep1 = ClampByte(ivec4(v[2], v[0], v[0], v[0])); + ep2 = ClampByte(ivec4(v[2] + v[3], v[0] + v[1], v[0] + v[1], v[0] + v[1])); break; } case 6: { READ_UINT_VALUES(4) - endpoints[0][ep_index] = uvec4(0xFF, (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8); - endpoints[1][ep_index] = uvec4(0xFF, v[0], v[1], v[2]); + ep1 = uvec4(0xFF, (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8); + ep2 = uvec4(0xFF, v[0], v[1], v[2]); break; } case 8: { READ_UINT_VALUES(6) if ((v[1] + v[3] + v[5]) >= (v[0] + v[2] + v[4])) { - endpoints[0][ep_index] = uvec4(0xFF, v[0], v[2], v[4]); - endpoints[1][ep_index] = uvec4(0xFF, v[1], v[3], v[5]); + ep1 = uvec4(0xFF, v[0], v[2], v[4]); + ep2 = uvec4(0xFF, v[1], v[3], v[5]); } else { - endpoints[0][ep_index] = uvec4(BlueContract(0xFF, int(v[1]), int(v[3]), int(v[5]))); - endpoints[1][ep_index] = uvec4(BlueContract(0xFF, int(v[0]), int(v[2]), int(v[4]))); + ep1 = uvec4(BlueContract(0xFF, int(v[1]), int(v[3]), int(v[5]))); + ep2 = uvec4(BlueContract(0xFF, int(v[0]), int(v[2]), int(v[4]))); } break; } @@ -753,28 +751,28 @@ void ComputeEndpoints(uint ep_index, uint color_endpoint_mode, v[5] = transferred.x; v[4] = transferred.y; if ((v[1] + v[3] + v[5]) >= 0) { - endpoints[0][ep_index] = ClampByte(ivec4(0xFF, v[0], v[2], v[4])); - endpoints[1][ep_index] = ClampByte(ivec4(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5])); + ep1 = ClampByte(ivec4(0xFF, v[0], v[2], v[4])); + ep2 = ClampByte(ivec4(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5])); } else { - endpoints[0][ep_index] = ClampByte(BlueContract(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5])); - endpoints[1][ep_index] = ClampByte(BlueContract(0xFF, v[0], v[2], v[4])); + ep1 = ClampByte(BlueContract(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5])); + ep2 = ClampByte(BlueContract(0xFF, v[0], v[2], v[4])); } break; } case 10: { READ_UINT_VALUES(6) - endpoints[0][ep_index] = uvec4(v[4], (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8); - endpoints[1][ep_index] = uvec4(v[5], v[0], v[1], v[2]); + ep1 = uvec4(v[4], (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8); + ep2 = uvec4(v[5], v[0], v[1], v[2]); break; } case 12: { READ_UINT_VALUES(8) if ((v[1] + v[3] + v[5]) >= (v[0] + v[2] + v[4])) { - endpoints[0][ep_index] = uvec4(v[6], v[0], v[2], v[4]); - endpoints[1][ep_index] = uvec4(v[7], v[1], v[3], v[5]); + ep1 = uvec4(v[6], v[0], v[2], v[4]); + ep2 = uvec4(v[7], v[1], v[3], v[5]); } else { - endpoints[0][ep_index] = uvec4(BlueContract(int(v[7]), int(v[1]), int(v[3]), int(v[5]))); - endpoints[1][ep_index] = uvec4(BlueContract(int(v[6]), int(v[0]), int(v[2]), int(v[4]))); + ep1 = uvec4(BlueContract(int(v[7]), int(v[1]), int(v[3]), int(v[5]))); + ep2 = uvec4(BlueContract(int(v[6]), int(v[0]), int(v[2]), int(v[4]))); } break; } @@ -796,18 +794,18 @@ void ComputeEndpoints(uint ep_index, uint color_endpoint_mode, v[6] = transferred.y; if ((v[1] + v[3] + v[5]) >= 0) { - endpoints[0][ep_index] = ClampByte(ivec4(v[6], v[0], v[2], v[4])); - endpoints[1][ep_index] = ClampByte(ivec4(v[7] + v[6], v[0] + v[1], v[2] + v[3], v[4] + v[5])); + ep1 = ClampByte(ivec4(v[6], v[0], v[2], v[4])); + ep2 = ClampByte(ivec4(v[7] + v[6], v[0] + v[1], v[2] + v[3], v[4] + v[5])); } else { - endpoints[0][ep_index] = ClampByte(BlueContract(v[6] + v[7], v[0] + v[1], v[2] + v[3], v[4] + v[5])); - endpoints[1][ep_index] = ClampByte(BlueContract(v[6], v[0], v[2], v[4])); + ep1 = ClampByte(BlueContract(v[6] + v[7], v[0] + v[1], v[2] + v[3], v[4] + v[5])); + ep2 = ClampByte(BlueContract(v[6], v[0], v[2], v[4])); } break; } default: { // HDR mode, or more likely a bug computing the color_endpoint_mode - endpoints[0][ep_index] = uvec4(0xFF, 0xFF, 0, 0); - endpoints[1][ep_index] = uvec4(0xFF, 0xFF, 0, 0); + ep1 = uvec4(0xFF, 0xFF, 0, 0); + ep2 = uvec4(0xFF, 0xFF, 0, 0); break; } } @@ -1200,6 +1198,10 @@ void DecompressBlock(ivec3 coord) { color_endpoint_mode[i] = cem; } } + + uvec4 endpoints0[4]; + uvec4 endpoints1[4]; + { // This decode phase should at most push 32 elements into the vector result_vector_max_index = 32; @@ -1207,8 +1209,10 @@ void DecompressBlock(ivec3 coord) { uint colvals_index = 0; DecodeColorValues(color_endpoint_mode, num_partitions, color_data_bits); for (uint i = 0; i < num_partitions; i++) { - ComputeEndpoints(i, color_endpoint_mode[i], colvals_index); + ComputeEndpoints(endpoints0[i], endpoints1[i], color_endpoint_mode[i], + colvals_index); } + } color_endpoint_data = local_buff; color_endpoint_data = bitfieldReverse(color_endpoint_data).wzyx; const uint clear_byte_start = (weight_bits >> 3) + 1; @@ -1243,8 +1247,8 @@ void DecompressBlock(ivec3 coord) { local_partition = Select2DPartition(partition_index, i, j, num_partitions, (block_dims.y * block_dims.x) < 32); } - const uvec4 C0 = ReplicateByteTo16(endpoints[0][local_partition]); - const uvec4 C1 = ReplicateByteTo16(endpoints[1][local_partition]); + const uvec4 C0 = ReplicateByteTo16(endpoints0[local_partition]); + const uvec4 C1 = ReplicateByteTo16(endpoints1[local_partition]); const uint weight_offset = (j * block_dims.x + i); const uint array_index = weight_offset / 4; const uint vector_index = bfe(weight_offset, 0, 2); From f41fb3ec0b12761c723f26e5f44d1c7f15468153 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 20:52:14 -0400 Subject: [PATCH 17/33] Revert "bfe instead of mod" This reverts commit 86006a3b09e8a8c17d2ade61be76736a79e3f58a. --- src/video_core/host_shaders/astc_decoder.comp | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 5346cba0c..fd38dcfe5 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -21,8 +21,6 @@ #endif -#define bfe bitfieldExtract - layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; BEGIN_PUSH_CONSTANTS @@ -134,7 +132,7 @@ void ResultEmplaceBack(EncodingData val) { return; } const uint array_index = result_index / 4; - const uint vector_index = bfe(result_index, 0, 2); + const uint vector_index = result_index % 4; result_vector[array_index][vector_index] = val.data; ++result_index; } @@ -386,7 +384,7 @@ uint StreamColorBits(uint num_bits) { EncodingData GetEncodingFromVector(uint index) { const uint array_index = index / 4; - const uint vector_index = bfe(index, 0, 2); + const uint vector_index = index % 4; const uint data = result_vector[array_index][vector_index]; return EncodingData(data); @@ -395,7 +393,7 @@ EncodingData GetEncodingFromVector(uint index) { // Returns the number of bits required to encode n_vals values. uint GetBitLength(uint n_vals, uint encoding_index) { const EncodingData encoding_value = - EncodingData(encoding_values[encoding_index / 4][bfe(encoding_index, 0, 2)]); + EncodingData(encoding_values[encoding_index / 4][encoding_index % 4]); const uint encoding = Encoding(encoding_value); uint total_bits = NumBits(encoding_value) * n_vals; if (encoding == TRIT) { @@ -515,7 +513,7 @@ void DecodeTritBlock(uint num_bits) { } void DecodeIntegerSequence(uint max_range, uint num_values) { - EncodingData val = EncodingData(encoding_values[max_range / 4][bfe(max_range, 0, 2)]); + EncodingData val = EncodingData(encoding_values[max_range / 4][max_range % 4]); const uint encoding = Encoding(val); const uint num_bits = NumBits(val); uint vals_decoded = 0; @@ -567,7 +565,7 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { A = ReplicateBitTo9((bitval & 1)); switch (encoding) { case JUST_BITS: - color_values[out_index / 4][bfe(out_index, 0, 2)] = FastReplicateTo8(bitval, bitlen); + color_values[out_index / 4][out_index % 4] = FastReplicateTo8(bitval, bitlen); ++out_index; break; case TRIT: { @@ -647,7 +645,7 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { uint T = (D * C) + B; T ^= A; T = (A & 0x80) | (T >> 2); - color_values[out_index / 4][bfe(out_index, 0, 2)] = T; + color_values[out_index / 4][out_index % 4] = T; ++out_index; } } @@ -678,14 +676,14 @@ void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, #define READ_UINT_VALUES(N) \ uint v[N]; \ for (uint i = 0; i < N; i++) { \ - v[i] = color_values[colvals_index / 4][bfe(colvals_index, 0, 2)]; \ + v[i] = color_values[colvals_index / 4][colvals_index % 4]; \ ++colvals_index; \ } #define READ_INT_VALUES(N) \ int v[N]; \ for (uint i = 0; i < N; i++) { \ - v[i] = int(color_values[colvals_index / 4][bfe(colvals_index, 0, 2)]); \ + v[i] = int(color_values[colvals_index / 4][colvals_index % 4]); \ ++colvals_index; \ } @@ -896,7 +894,7 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { const uint loop_count = min(result_index, area * num_planes); for (uint itr = 0; itr < loop_count; ++itr) { const uint array_index = itr / 4; - const uint vector_index = bfe(itr, 0, 2); + const uint vector_index = itr % 4; result_vector[array_index][vector_index] = UnquantizeTexelWeight(GetEncodingFromVector(itr)); } @@ -923,7 +921,7 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { #define VectorIndicesFromBase(offset_base) \ const uint offset = is_dual_plane ? 2 * offset_base + plane : offset_base; \ const uint array_index = offset / 4; \ - const uint vector_index = bfe(offset, 0, 2); + const uint vector_index = offset % 4; if (v0 < area) { const uint offset_base = v0; @@ -947,7 +945,7 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { } const uint offset = (t * block_dims.x + s) + ARRAY_NUM_ELEMENTS * plane; const uint array_index = offset / 4; - const uint vector_index = bfe(offset, 0, 2); + const uint vector_index = offset % 4; unquantized_texel_weights[array_index][vector_index] = (uint(dot(p, w)) + 8) >> 4; } } @@ -1251,13 +1249,13 @@ void DecompressBlock(ivec3 coord) { const uvec4 C1 = ReplicateByteTo16(endpoints1[local_partition]); const uint weight_offset = (j * block_dims.x + i); const uint array_index = weight_offset / 4; - const uint vector_index = bfe(weight_offset, 0, 2); + const uint vector_index = weight_offset % 4; const uint primary_weight = unquantized_texel_weights[array_index][vector_index]; uvec4 weight_vec = uvec4(primary_weight); if (params.dual_plane) { const uint secondary_weight_offset = (j * block_dims.x + i) + ARRAY_NUM_ELEMENTS; const uint secondary_array_index = secondary_weight_offset / 4; - const uint secondary_vector_index = bfe(secondary_weight_offset, 0, 2); + const uint secondary_vector_index = secondary_weight_offset % 4; const uint secondary_weight = unquantized_texel_weights[secondary_array_index][secondary_vector_index]; for (uint c = 0; c < 4; c++) { From f2cf81e0b69652bec91857fd6d447e0e4ea137b7 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 20:52:17 -0400 Subject: [PATCH 18/33] Revert "uint result index" This reverts commit 0e978786b5a8e7382005d8b1e16cfa12f3eeb775. --- src/video_core/host_shaders/astc_decoder.comp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index fd38dcfe5..ca93dc2a2 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -88,7 +88,7 @@ int color_bitsread = 0; #define VECTOR_ARRAY_SIZE DIVCEIL(ARRAY_NUM_ELEMENTS * 2, 4) uvec4 result_vector[VECTOR_ARRAY_SIZE]; -uint result_index = 0; +int result_index = 0; uint result_vector_max_index; bool result_limit_reached = false; From 441b8471070dfbf8724d6ec195c0083ad4d9bd2b Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:22:21 -0400 Subject: [PATCH 19/33] more packing --- src/video_core/host_shaders/astc_decoder.comp | 212 +++++++++--------- 1 file changed, 106 insertions(+), 106 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index ca93dc2a2..0e611ede5 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -88,7 +88,7 @@ int color_bitsread = 0; #define VECTOR_ARRAY_SIZE DIVCEIL(ARRAY_NUM_ELEMENTS * 2, 4) uvec4 result_vector[VECTOR_ARRAY_SIZE]; -int result_index = 0; +uint result_index = 0; uint result_vector_max_index; bool result_limit_reached = false; @@ -131,8 +131,8 @@ void ResultEmplaceBack(EncodingData val) { result_limit_reached = true; return; } - const uint array_index = result_index / 4; - const uint vector_index = result_index % 4; + const uint array_index = result_index / 4u; + const uint vector_index = result_index % 4u; result_vector[array_index][vector_index] = val.data; ++result_index; } @@ -428,69 +428,68 @@ uint BitsOp(uint bits, uint start, uint end) { void DecodeQuintBlock(uint num_bits) { uvec3 m; - uvec3 q; - uint Q; + uvec4 qQ; m[0] = StreamColorBits(num_bits); - Q = StreamColorBits(3); + qQ.w = StreamColorBits(3); m[1] = StreamColorBits(num_bits); - Q |= StreamColorBits(2) << 3; + qQ.w |= StreamColorBits(2) << 3; m[2] = StreamColorBits(num_bits); - Q |= StreamColorBits(2) << 5; - if (BitsOp(Q, 1, 2) == 3 && BitsOp(Q, 5, 6) == 0) { - q.x = 4; - q.y = 4; - q.z = (BitsBracket(Q, 0) << 2) | ((BitsBracket(Q, 4) & ~BitsBracket(Q, 0)) << 1) | - (BitsBracket(Q, 3) & ~BitsBracket(Q, 0)); + qQ.w |= StreamColorBits(2) << 5; + if (BitsOp(qQ.w, 1, 2) == 3 && BitsOp(qQ.w, 5, 6) == 0) { + qQ.x = 4; + qQ.y = 4; + qQ.z = (BitsBracket(qQ.w, 0) << 2) | ((BitsBracket(qQ.w, 4) & ~BitsBracket(qQ.w, 0)) << 1) | + (BitsBracket(qQ.w, 3) & ~BitsBracket(qQ.w, 0)); } else { uint C = 0; - if (BitsOp(Q, 1, 2) == 3) { - q.z = 4; - C = (BitsOp(Q, 3, 4) << 3) | ((~BitsOp(Q, 5, 6) & 3) << 1) | BitsBracket(Q, 0); + if (BitsOp(qQ.w, 1, 2) == 3) { + qQ.z = 4; + C = (BitsOp(qQ.w, 3, 4) << 3) | ((~BitsOp(qQ.w, 5, 6) & 3) << 1) | BitsBracket(qQ.w, 0); } else { - q.z = BitsOp(Q, 5, 6); - C = BitsOp(Q, 0, 4); + qQ.z = BitsOp(qQ.w, 5, 6); + C = BitsOp(qQ.w, 0, 4); } if (BitsOp(C, 0, 2) == 5) { - q.y = 4; - q.x = BitsOp(C, 3, 4); + qQ.y = 4; + qQ.x = BitsOp(C, 3, 4); } else { - q.y = BitsOp(C, 3, 4); - q.x = BitsOp(C, 0, 2); + qQ.y = BitsOp(C, 3, 4); + qQ.x = BitsOp(C, 0, 2); } } for (uint i = 0; i < 3; i++) { - const EncodingData val = CreateEncodingData(QUINT, num_bits, m[i], q[i]); + const EncodingData val = CreateEncodingData(QUINT, num_bits, m[i], qQ[i]); ResultEmplaceBack(val); } } void DecodeTritBlock(uint num_bits) { - uint m[5]; - uint t[5]; - uint T; + uvec4 m; + uvec4 t; + uvec3 Tm5t5; m[0] = StreamColorBits(num_bits); - T = StreamColorBits(2); + Tm5t5.x = StreamColorBits(2); m[1] = StreamColorBits(num_bits); - T |= StreamColorBits(2) << 2; + Tm5t5.x |= StreamColorBits(2) << 2; m[2] = StreamColorBits(num_bits); - T |= StreamColorBits(1) << 4; + Tm5t5.x |= StreamColorBits(1) << 4; m[3] = StreamColorBits(num_bits); - T |= StreamColorBits(2) << 5; - m[4] = StreamColorBits(num_bits); - T |= StreamColorBits(1) << 7; + Tm5t5.x |= StreamColorBits(2) << 5; + Tm5t5.y = StreamColorBits(num_bits); + Tm5t5.x |= StreamColorBits(1) << 7; uint C = 0; - if (BitsOp(T, 2, 4) == 7) { - C = (BitsOp(T, 5, 7) << 2) | BitsOp(T, 0, 1); - t[4] = 2; + if (BitsOp(Tm5t5.x, 2, 4) == 7) { + C = (BitsOp(Tm5t5.x, 5, 7) << 2) | BitsOp(Tm5t5.x, 0, 1); + Tm5t5.z = 2; t[3] = 2; } else { - C = BitsOp(T, 0, 4); - if (BitsOp(T, 5, 6) == 3) { - t[4] = 2; - t[3] = BitsBracket(T, 7); + C = BitsOp(Tm5t5.x, 0, 4); + if (BitsOp(Tm5t5.x, 5, 6) == 3) { + Tm5t5.z = 2; + t[3] = BitsBracket(Tm5t5.x, 7); } else { - t[4] = BitsBracket(T, 7); - t[3] = BitsOp(T, 5, 6); + Tm5t5.z = BitsBracket(Tm5t5.x, 7); + t[3] = BitsOp(Tm5t5.x, 5, 6); } } if (BitsOp(C, 0, 1) == 3) { @@ -506,10 +505,12 @@ void DecodeTritBlock(uint num_bits) { t[1] = BitsOp(C, 2, 3); t[0] = (BitsBracket(C, 1) << 1) | (BitsBracket(C, 0) & ~BitsBracket(C, 1)); } - for (uint i = 0; i < 5; i++) { + for (uint i = 0; i < 4; i++) { const EncodingData val = CreateEncodingData(TRIT, num_bits, m[i], t[i]); ResultEmplaceBack(val); } + const EncodingData val = CreateEncodingData(TRIT, num_bits, Tm5t5.y, Tm5t5.z); + ResultEmplaceBack(val); } void DecodeIntegerSequence(uint max_range, uint num_values) { @@ -674,129 +675,128 @@ ivec4 BlueContract(int a, int r, int g, int b) { void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, inout uint colvals_index) { #define READ_UINT_VALUES(N) \ - uint v[N]; \ + uvec4 V[2]; \ for (uint i = 0; i < N; i++) { \ - v[i] = color_values[colvals_index / 4][colvals_index % 4]; \ + V[i / 4][i % 4] = color_values[colvals_index / 4][colvals_index % 4]; \ ++colvals_index; \ } - #define READ_INT_VALUES(N) \ - int v[N]; \ + ivec4 V[2]; \ for (uint i = 0; i < N; i++) { \ - v[i] = int(color_values[colvals_index / 4][colvals_index % 4]); \ + V[i / 4][i % 4] = int(color_values[colvals_index / 4][colvals_index % 4]); \ ++colvals_index; \ } switch (color_endpoint_mode) { case 0: { READ_UINT_VALUES(2) - ep1 = uvec4(0xFF, v[0], v[0], v[0]); - ep2 = uvec4(0xFF, v[1], v[1], v[1]); + ep1 = uvec4(0xFF, V[0].x, V[0].x, V[0].x); + ep2 = uvec4(0xFF, V[0].y, V[0].y, V[0].y); break; } case 1: { READ_UINT_VALUES(2) - const uint L0 = (v[0] >> 2) | (v[1] & 0xC0); - const uint L1 = min(L0 + (v[1] & 0x3F), 0xFFU); + const uint L0 = (V[0].x >> 2) | (V[0].y & 0xC0); + const uint L1 = min(L0 + (V[0].y & 0x3F), 0xFFU); ep1 = uvec4(0xFF, L0, L0, L0); ep2 = uvec4(0xFF, L1, L1, L1); break; } case 4: { READ_UINT_VALUES(4) - ep1 = uvec4(v[2], v[0], v[0], v[0]); - ep2 = uvec4(v[3], v[1], v[1], v[1]); + ep1 = uvec4(V[0].z, V[0].x, V[0].x, V[0].x); + ep2 = uvec4(V[0].w, V[0].y, V[0].y, V[0].y); break; } case 5: { READ_INT_VALUES(4) - ivec2 transferred = BitTransferSigned(v[1], v[0]); - v[1] = transferred.x; - v[0] = transferred.y; - transferred = BitTransferSigned(v[3], v[2]); - v[3] = transferred.x; - v[2] = transferred.y; - ep1 = ClampByte(ivec4(v[2], v[0], v[0], v[0])); - ep2 = ClampByte(ivec4(v[2] + v[3], v[0] + v[1], v[0] + v[1], v[0] + v[1])); + ivec2 transferred = BitTransferSigned(V[0].y, V[0].x); + V[0].y = transferred.x; + V[0].x = transferred.y; + transferred = BitTransferSigned(V[0].w, V[0].z); + V[0].w = transferred.x; + V[0].z = transferred.y; + ep1 = ClampByte(ivec4(V[0].z, V[0].x, V[0].x, V[0].x)); + ep2 = ClampByte(ivec4(V[0].z + V[0].w, V[0].x + V[0].y, V[0].x + V[0].y, V[0].x + V[0].y)); break; } case 6: { READ_UINT_VALUES(4) - ep1 = uvec4(0xFF, (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8); - ep2 = uvec4(0xFF, v[0], v[1], v[2]); + ep1 = uvec4(0xFF, (V[0].x * V[0].w) >> 8, (V[0].y * V[0].w) >> 8, (V[0].z * V[0].w) >> 8); + ep2 = uvec4(0xFF, V[0].x, V[0].y, V[0].z); break; } case 8: { READ_UINT_VALUES(6) - if ((v[1] + v[3] + v[5]) >= (v[0] + v[2] + v[4])) { - ep1 = uvec4(0xFF, v[0], v[2], v[4]); - ep2 = uvec4(0xFF, v[1], v[3], v[5]); + if ((V[0].y + V[0].w + V[1].y) >= (V[0].x + V[0].z + V[1].x)) { + ep1 = uvec4(0xFF, V[0].x, V[0].z, V[1].x); + ep2 = uvec4(0xFF, V[0].y, V[0].w, V[1].y); } else { - ep1 = uvec4(BlueContract(0xFF, int(v[1]), int(v[3]), int(v[5]))); - ep2 = uvec4(BlueContract(0xFF, int(v[0]), int(v[2]), int(v[4]))); + ep1 = uvec4(BlueContract(0xFF, int(V[0].y), int(V[0].w), int(V[1].y))); + ep2 = uvec4(BlueContract(0xFF, int(V[0].x), int(V[0].z), int(V[1].x))); } break; } case 9: { READ_INT_VALUES(6) - ivec2 transferred = BitTransferSigned(v[1], v[0]); - v[1] = transferred.x; - v[0] = transferred.y; - transferred = BitTransferSigned(v[3], v[2]); - v[3] = transferred.x; - v[2] = transferred.y; - transferred = BitTransferSigned(v[5], v[4]); - v[5] = transferred.x; - v[4] = transferred.y; - if ((v[1] + v[3] + v[5]) >= 0) { - ep1 = ClampByte(ivec4(0xFF, v[0], v[2], v[4])); - ep2 = ClampByte(ivec4(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5])); + ivec2 transferred = BitTransferSigned(V[0].y, V[0].x); + V[0].y = transferred.x; + V[0].x = transferred.y; + transferred = BitTransferSigned(V[0].w, V[0].z); + V[0].w = transferred.x; + V[0].z = transferred.y; + transferred = BitTransferSigned(V[1].y, V[1].x); + V[1].y = transferred.x; + V[1].x = transferred.y; + if ((V[0].y + V[0].w + V[1].y) >= 0) { + ep1 = ClampByte(ivec4(0xFF, V[0].x, V[0].z, V[1].x)); + ep2 = ClampByte(ivec4(0xFF, V[0].x + V[0].y, V[0].z + V[0].w, V[1].x + V[1].y)); } else { - ep1 = ClampByte(BlueContract(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5])); - ep2 = ClampByte(BlueContract(0xFF, v[0], v[2], v[4])); + ep1 = ClampByte(BlueContract(0xFF, V[0].x + V[0].y, V[0].z + V[0].w, V[1].x + V[1].y)); + ep2 = ClampByte(BlueContract(0xFF, V[0].x, V[0].z, V[1].x)); } break; } case 10: { READ_UINT_VALUES(6) - ep1 = uvec4(v[4], (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8); - ep2 = uvec4(v[5], v[0], v[1], v[2]); + ep1 = uvec4(V[1].x, (V[0].x * V[0].w) >> 8, (V[0].y * V[0].w) >> 8, (V[0].z * V[0].w) >> 8); + ep2 = uvec4(V[1].y, V[0].x, V[0].y, V[0].z); break; } case 12: { READ_UINT_VALUES(8) - if ((v[1] + v[3] + v[5]) >= (v[0] + v[2] + v[4])) { - ep1 = uvec4(v[6], v[0], v[2], v[4]); - ep2 = uvec4(v[7], v[1], v[3], v[5]); + if ((V[0].y + V[0].w + V[1].y) >= (V[0].x + V[0].z + V[1].x)) { + ep1 = uvec4(V[1].z, V[0].x, V[0].z, V[1].x); + ep2 = uvec4(V[1].w, V[0].y, V[0].w, V[1].y); } else { - ep1 = uvec4(BlueContract(int(v[7]), int(v[1]), int(v[3]), int(v[5]))); - ep2 = uvec4(BlueContract(int(v[6]), int(v[0]), int(v[2]), int(v[4]))); + ep1 = uvec4(BlueContract(int(V[1].w), int(V[0].y), int(V[0].w), int(V[1].y))); + ep2 = uvec4(BlueContract(int(V[1].z), int(V[0].x), int(V[0].z), int(V[1].x))); } break; } case 13: { READ_INT_VALUES(8) - ivec2 transferred = BitTransferSigned(v[1], v[0]); - v[1] = transferred.x; - v[0] = transferred.y; - transferred = BitTransferSigned(v[3], v[2]); - v[3] = transferred.x; - v[2] = transferred.y; + ivec2 transferred = BitTransferSigned(V[0].y, V[0].x); + V[0].y = transferred.x; + V[0].x = transferred.y; + transferred = BitTransferSigned(V[0].w, V[0].z); + V[0].w = transferred.x; + V[0].z = transferred.y; - transferred = BitTransferSigned(v[5], v[4]); - v[5] = transferred.x; - v[4] = transferred.y; + transferred = BitTransferSigned(V[1].y, V[1].x); + V[1].y = transferred.x; + V[1].x = transferred.y; - transferred = BitTransferSigned(v[7], v[6]); - v[7] = transferred.x; - v[6] = transferred.y; + transferred = BitTransferSigned(V[1].w, V[1].z); + V[1].w = transferred.x; + V[1].z = transferred.y; - if ((v[1] + v[3] + v[5]) >= 0) { - ep1 = ClampByte(ivec4(v[6], v[0], v[2], v[4])); - ep2 = ClampByte(ivec4(v[7] + v[6], v[0] + v[1], v[2] + v[3], v[4] + v[5])); + if ((V[0].y + V[0].w + V[1].y) >= 0) { + ep1 = ClampByte(ivec4(V[1].z, V[0].x, V[0].z, V[1].x)); + ep2 = ClampByte(ivec4(V[1].w + V[1].z, V[0].x + V[0].y, V[0].z + V[0].w, V[1].x + V[1].y)); } else { - ep1 = ClampByte(BlueContract(v[6] + v[7], v[0] + v[1], v[2] + v[3], v[4] + v[5])); - ep2 = ClampByte(BlueContract(v[6], v[0], v[2], v[4])); + ep1 = ClampByte(BlueContract(V[1].z + V[1].w, V[0].x + V[0].y, V[0].z + V[0].w, V[1].x + V[1].y)); + ep2 = ClampByte(BlueContract(V[1].z, V[0].x, V[0].z, V[1].x)); } break; } From 75ac7845ce2f9fe46602eeac03207040659e6bc9 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:33:47 -0400 Subject: [PATCH 20/33] error/void extent funcs --- src/video_core/host_shaders/astc_decoder.comp | 91 +++++++++---------- 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 0e611ede5..63b3d9f44 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -40,9 +40,6 @@ struct TexelWeightParams { uvec2 size; uint max_weight; bool dual_plane; - bool error_state; - bool void_extent_ldr; - bool void_extent_hdr; }; layout(binding = BINDING_INPUT_BUFFER, std430) readonly restrict buffer InputBufferU32 { @@ -983,28 +980,54 @@ int FindLayout(uint mode) { return 5; } -TexelWeightParams DecodeBlockInfo() { - TexelWeightParams params = TexelWeightParams(uvec2(0), 0, false, false, false, false); - uint mode = StreamBits(11); + +void FillError(ivec3 coord) { + for (uint j = 0; j < block_dims.y; j++) { + for (uint i = 0; i < block_dims.x; i++) { + imageStore(dest_image, coord + ivec3(i, j, 0), vec4(0.0, 0.0, 0.0, 0.0)); + } + } +} + +void FillVoidExtentLDR(ivec3 coord) { + SkipBits(52); + const uint r_u = StreamBits(16); + const uint g_u = StreamBits(16); + const uint b_u = StreamBits(16); + const uint a_u = StreamBits(16); + const float a = float(a_u) / 65535.0f; + const float r = float(r_u) / 65535.0f; + const float g = float(g_u) / 65535.0f; + const float b = float(b_u) / 65535.0f; + for (uint j = 0; j < block_dims.y; j++) { + for (uint i = 0; i < block_dims.x; i++) { + imageStore(dest_image, coord + ivec3(i, j, 0), vec4(r, g, b, a)); + } + } +} + +bool IsError(uint mode) { if ((mode & 0x1ff) == 0x1fc) { if ((mode & 0x200) != 0) { - params.void_extent_hdr = true; - } else { - params.void_extent_ldr = true; + // params.void_extent_hdr = true; + return true; } if ((mode & 0x400) == 0 || StreamBits(1) == 0) { - params.error_state = true; + return true; } - return params; + return false; } if ((mode & 0xf) == 0) { - params.error_state = true; - return params; + return true; } if ((mode & 3) == 0 && (mode & 0x1c0) == 0x1c0) { - params.error_state = true; - return params; + return true; } + return false; +} + +TexelWeightParams DecodeBlockInfo(uint mode) { + TexelWeightParams params = TexelWeightParams(uvec2(0), 0, false); uint A, B; uint mode_layout = FindLayout(mode); switch (mode_layout) { @@ -1053,7 +1076,6 @@ TexelWeightParams DecodeBlockInfo() { params.size = uvec2(A + 6, B + 6); break; default: - params.error_state = true; break; } params.dual_plane = (mode_layout != 9) && ((mode & 0x400) != 0); @@ -1072,42 +1094,15 @@ TexelWeightParams DecodeBlockInfo() { return params; } -void FillError(ivec3 coord) { - for (uint j = 0; j < block_dims.y; j++) { - for (uint i = 0; i < block_dims.x; i++) { - imageStore(dest_image, coord + ivec3(i, j, 0), vec4(0.0, 0.0, 0.0, 0.0)); - } - } -} - -void FillVoidExtentLDR(ivec3 coord) { - SkipBits(52); - const uint r_u = StreamBits(16); - const uint g_u = StreamBits(16); - const uint b_u = StreamBits(16); - const uint a_u = StreamBits(16); - const float a = float(a_u) / 65535.0f; - const float r = float(r_u) / 65535.0f; - const float g = float(g_u) / 65535.0f; - const float b = float(b_u) / 65535.0f; - for (uint j = 0; j < block_dims.y; j++) { - for (uint i = 0; i < block_dims.x; i++) { - imageStore(dest_image, coord + ivec3(i, j, 0), vec4(r, g, b, a)); - } - } -} - void DecompressBlock(ivec3 coord) { - const TexelWeightParams params = DecodeBlockInfo(); - if (params.error_state) { + uint mode = StreamBits(11); + const TexelWeightParams params = DecodeBlockInfo(mode); + if (IsError(mode)) { FillError(coord); return; } - if (params.void_extent_hdr) { - FillError(coord); - return; - } - if (params.void_extent_ldr) { + if ((mode & 0x1ff) == 0x1fc) { + // params.void_extent_ldr = true; FillVoidExtentLDR(coord); return; } From fd2051b401bf0469bf199ce972289abad8087620 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:41:46 -0400 Subject: [PATCH 21/33] remove TexelWeightParams --- src/video_core/host_shaders/astc_decoder.comp | 77 ++++++++----------- 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 63b3d9f44..f497c4d17 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -36,12 +36,6 @@ struct EncodingData { uint data; }; -struct TexelWeightParams { - uvec2 size; - uint max_weight; - bool dual_plane; -}; - layout(binding = BINDING_INPUT_BUFFER, std430) readonly restrict buffer InputBufferU32 { uvec4 astc_data[]; }; @@ -1026,59 +1020,50 @@ bool IsError(uint mode) { return false; } -TexelWeightParams DecodeBlockInfo(uint mode) { - TexelWeightParams params = TexelWeightParams(uvec2(0), 0, false); +uvec2 DecodeBlockSize(uint mode) { uint A, B; - uint mode_layout = FindLayout(mode); - switch (mode_layout) { + switch (FindLayout(mode)) { case 0: A = (mode >> 5) & 0x3; B = (mode >> 7) & 0x3; - params.size = uvec2(B + 4, A + 2); - break; + return uvec2(B + 4, A + 2); case 1: A = (mode >> 5) & 0x3; B = (mode >> 7) & 0x3; - params.size = uvec2(B + 8, A + 2); - break; + return uvec2(B + 8, A + 2); case 2: A = (mode >> 5) & 0x3; B = (mode >> 7) & 0x3; - params.size = uvec2(A + 2, B + 8); - break; + return uvec2(A + 2, B + 8); case 3: A = (mode >> 5) & 0x3; B = (mode >> 7) & 0x1; - params.size = uvec2(A + 2, B + 6); - break; + return uvec2(A + 2, B + 6); case 4: A = (mode >> 5) & 0x3; B = (mode >> 7) & 0x1; - params.size = uvec2(B + 2, A + 2); - break; + return uvec2(B + 2, A + 2); case 5: A = (mode >> 5) & 0x3; - params.size = uvec2(12, A + 2); - break; + return uvec2(12, A + 2); case 6: A = (mode >> 5) & 0x3; - params.size = uvec2(A + 2, 12); - break; + return uvec2(A + 2, 12); case 7: - params.size = uvec2(6, 10); - break; + return uvec2(6, 10); case 8: - params.size = uvec2(10, 6); - break; + return uvec2(10, 6); case 9: A = (mode >> 5) & 0x3; B = (mode >> 9) & 0x3; - params.size = uvec2(A + 6, B + 6); - break; + return uvec2(A + 6, B + 6); default: - break; + return uvec2(0); } - params.dual_plane = (mode_layout != 9) && ((mode & 0x400) != 0); +} + +uint DecodeMaxWeight(uint mode) { + const uint mode_layout = FindLayout(mode); uint weight_index = (mode & 0x10) != 0 ? 1 : 0; if (mode_layout < 5) { weight_index |= (mode & 0x3) << 1; @@ -1089,14 +1074,11 @@ TexelWeightParams DecodeBlockInfo(uint mode) { if ((mode_layout != 9) && ((mode & 0x200) != 0)) { weight_index += 6; } - params.max_weight = weight_index + 1; - - return params; + return weight_index + 1; } void DecompressBlock(ivec3 coord) { uint mode = StreamBits(11); - const TexelWeightParams params = DecodeBlockInfo(mode); if (IsError(mode)) { FillError(coord); return; @@ -1106,12 +1088,15 @@ void DecompressBlock(ivec3 coord) { FillVoidExtentLDR(coord); return; } - if ((params.size.x > block_dims.x) || (params.size.y > block_dims.y)) { + const uvec2 size_params = DecodeBlockSize(mode); + if ((size_params.x > block_dims.x) || (size_params.y > block_dims.y)) { FillError(coord); return; } const uint num_partitions = StreamBits(2) + 1; - if (num_partitions > 4 || (num_partitions == 4 && params.dual_plane)) { + const uint mode_layout = FindLayout(mode); + const bool dual_plane = (mode_layout != 9) && ((mode & 0x400) != 0); + if (num_partitions > 4 || (num_partitions == 4 && dual_plane)) { FillError(coord); return; } @@ -1127,7 +1112,8 @@ void DecompressBlock(ivec3 coord) { base_cem = StreamBits(6); } const uint base_mode = base_cem & 3; - const uint weight_bits = GetPackedBitSize(params.size, params.dual_plane, params.max_weight); + const uint max_weight = DecodeMaxWeight(mode); + const uint weight_bits = GetPackedBitSize(size_params, dual_plane, max_weight); uint remaining_bits = 128 - weight_bits - total_bitsread; uint extra_cem_bits = 0; if (base_mode > 0) { @@ -1146,7 +1132,7 @@ void DecompressBlock(ivec3 coord) { } } remaining_bits -= extra_cem_bits; - const uint plane_selector_bits = params.dual_plane ? 2 : 0; + const uint plane_selector_bits = dual_plane ? 2 : 0; remaining_bits -= plane_selector_bits; if (remaining_bits > 128) { // Bad data, more remaining bits than 4 bytes @@ -1198,7 +1184,6 @@ void DecompressBlock(ivec3 coord) { // This decode phase should at most push 32 elements into the vector result_vector_max_index = 32; - // uvec4 color_values[8]; uint colvals_index = 0; DecodeColorValues(color_endpoint_mode, num_partitions, color_data_bits); for (uint i = 0; i < num_partitions; i++) { @@ -1226,13 +1211,13 @@ void DecompressBlock(ivec3 coord) { result_limit_reached = false; // The limit for the Unquantize phase, avoids decoding more data than needed. - result_vector_max_index = params.size.x * params.size.y; - if (params.dual_plane) { + result_vector_max_index = size_params.x * size_params.y; + if (dual_plane) { result_vector_max_index *= 2; } - DecodeIntegerSequence(params.max_weight, GetNumWeightValues(params.size, params.dual_plane)); + DecodeIntegerSequence(max_weight, GetNumWeightValues(size_params, dual_plane)); - UnquantizeTexelWeights(params.size, params.dual_plane); + UnquantizeTexelWeights(size_params, dual_plane); for (uint j = 0; j < block_dims.y; j++) { for (uint i = 0; i < block_dims.x; i++) { uint local_partition = 0; @@ -1247,7 +1232,7 @@ void DecompressBlock(ivec3 coord) { const uint vector_index = weight_offset % 4; const uint primary_weight = unquantized_texel_weights[array_index][vector_index]; uvec4 weight_vec = uvec4(primary_weight); - if (params.dual_plane) { + if (dual_plane) { const uint secondary_weight_offset = (j * block_dims.x + i) + ARRAY_NUM_ELEMENTS; const uint secondary_array_index = secondary_weight_offset / 4; const uint secondary_vector_index = secondary_weight_offset % 4; From 7a0d7e7668b13ecf67760843f5a72cf681d8ea34 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:47:10 -0400 Subject: [PATCH 22/33] small_block opt --- src/video_core/host_shaders/astc_decoder.comp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index f497c4d17..a33c916ac 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -264,8 +264,8 @@ uint Hash52(uint p) { return p; } -uint Select2DPartition(uint seed, uint x, uint y, uint partition_count, bool small_block) { - if (small_block) { +uint Select2DPartition(uint seed, uint x, uint y, uint partition_count) { + if ((block_dims.y * block_dims.x) < 32) { x <<= 1; y <<= 1; } @@ -1222,8 +1222,7 @@ void DecompressBlock(ivec3 coord) { for (uint i = 0; i < block_dims.x; i++) { uint local_partition = 0; if (num_partitions > 1) { - local_partition = Select2DPartition(partition_index, i, j, num_partitions, - (block_dims.y * block_dims.x) < 32); + local_partition = Select2DPartition(partition_index, i, j, num_partitions); } const uvec4 C0 = ReplicateByteTo16(endpoints0[local_partition]); const uvec4 C1 = ReplicateByteTo16(endpoints1[local_partition]); From 5a78b35b1abf071bd62b1ff8d0cb939bd30a549f Mon Sep 17 00:00:00 2001 From: ameerj <52414509+ameerj@users.noreply.github.com> Date: Wed, 28 Jun 2023 01:24:52 -0400 Subject: [PATCH 23/33] vulkan dims specialization --- src/video_core/host_shaders/CMakeLists.txt | 57 ++++++++++ src/video_core/host_shaders/astc_decoder.comp | 42 ++++--- .../host_shaders/astc_decoder_spv_includes.h | 20 ++++ .../renderer_vulkan/vk_compute_pass.cpp | 104 ++++++++++++++++-- .../renderer_vulkan/vk_compute_pass.h | 2 + 5 files changed, 198 insertions(+), 27 deletions(-) create mode 100644 src/video_core/host_shaders/astc_decoder_spv_includes.h diff --git a/src/video_core/host_shaders/CMakeLists.txt b/src/video_core/host_shaders/CMakeLists.txt index e61d9af80..20e8388ee 100644 --- a/src/video_core/host_shaders/CMakeLists.txt +++ b/src/video_core/host_shaders/CMakeLists.txt @@ -13,6 +13,11 @@ set(GLSL_INCLUDES ${FIDELITYFX_FILES} ) +set(ASTC_INCLUDES + # astc_decoder_glsl_includes.h + astc_decoder_spv_includes.h +) + set(SHADER_FILES astc_decoder.comp blit_color_float.frag @@ -95,9 +100,60 @@ if (NOT GLSLANG_ERROR STREQUAL "") set(QUIET_FLAG "") endif() +macro(ASTC_GEN) + # paired list of valid astc block dimensions + set(ASTC_WIDTHS 4 5 5 6 6 6 8 8 8 10 10 10 10 12 12) + set(ASTC_HEIGHTS 4 4 5 4 5 6 5 6 8 5 6 8 10 10 12) + list(LENGTH ASTC_WIDTHS NUM_ASTC_FORMATS) + math(EXPR NUM_ASTC_FORMATS "${NUM_ASTC_FORMATS}-1") + foreach(i RANGE ${NUM_ASTC_FORMATS}) + list(GET ASTC_WIDTHS ${i} ASTC_WIDTH) + list(GET ASTC_HEIGHTS ${i} ASTC_HEIGHT) + + # Vulkan SPIR-V Specialization + + string(TOUPPER ${SHADER_NAME}_${ASTC_WIDTH}x${ASTC_HEIGHT}_SPV SPIRV_VARIABLE_NAME) + set(SPIRV_HEADER_FILE ${SHADER_DIR}/${SHADER_NAME}_${ASTC_WIDTH}x${ASTC_HEIGHT}_spv.h) + add_custom_command( + OUTPUT + ${SPIRV_HEADER_FILE} + COMMAND + ${GLSLANGVALIDATOR} -V -DBLOCK_WIDTH=${ASTC_WIDTH} -DBLOCK_HEIGHT=${ASTC_HEIGHT} ${QUIET_FLAG} ${GLSL_FLAGS} --variable-name ${SPIRV_VARIABLE_NAME} -o ${SPIRV_HEADER_FILE} ${SOURCE_FILE} + MAIN_DEPENDENCY + ${SOURCE_FILE} + ) + set(SHADER_HEADERS ${SHADER_HEADERS} ${SPIRV_HEADER_FILE}) + + # GLSL Specialization + # Disabled as there was no noticeable performance uplift specializing the shaders for OGL + + # set(SOURCE_HEADER_FILE ${SHADER_DIR}/${SHADER_NAME}_${ASTC_WIDTH}x${ASTC_HEIGHT}.h) + # set(SHADER_DEFINES "#define BLOCK_WIDTH ${ASTC_WIDTH}" "#define BLOCK_HEIGHT ${ASTC_HEIGHT}") + # set(DEFINES_LINE_NUMBER 14) + # string(TOUPPER ${SHADER_NAME}_${ASTC_WIDTH}x${ASTC_HEIGHT} GLSL_VARIABLE_NAME) + # add_custom_command( + # OUTPUT + # ${SOURCE_HEADER_FILE} + # COMMAND + # ${CMAKE_COMMAND} -P ${HEADER_GENERATOR} ${SOURCE_FILE} ${SOURCE_HEADER_FILE} ${INPUT_FILE} "${SHADER_DEFINES}" ${DEFINES_LINE_NUMBER} ${GLSL_VARIABLE_NAME} + # MAIN_DEPENDENCY + # ${SOURCE_FILE} + # DEPENDS + # ${INPUT_FILE} + # ${SOURCE_FILE} + # ) + # set(SHADER_HEADERS ${SHADER_HEADERS} ${SOURCE_HEADER_FILE}) + endforeach() +endmacro() + foreach(FILENAME IN ITEMS ${SHADER_FILES}) string(REPLACE "." "_" SHADER_NAME ${FILENAME}) set(SOURCE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}) + + if (${FILENAME} MATCHES "astc_decoder.comp") + ASTC_GEN() + endif() + # Skip generating source headers on Vulkan exclusive files if (NOT ${FILENAME} MATCHES "vulkan.*") set(SOURCE_HEADER_FILE ${SHADER_DIR}/${SHADER_NAME}.h) @@ -151,6 +207,7 @@ endforeach() set(SHADER_SOURCES ${SHADER_FILES}) list(APPEND SHADER_SOURCES ${GLSL_INCLUDES}) +list(APPEND SHADER_SOURCES ${ASTC_INCLUDES}) add_custom_target(host_shaders DEPENDS diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index a33c916ac..b4bb8299f 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -24,7 +24,9 @@ layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; BEGIN_PUSH_CONSTANTS +#ifndef BLOCK_WIDTH UNIFORM(1) uvec2 block_dims; +#endif UNIFORM(2) uint layer_stride; UNIFORM(3) uint block_size; UNIFORM(4) uint x_shift; @@ -75,7 +77,15 @@ int color_bitsread = 0; // At most will require BLOCK_WIDTH x BLOCK_HEIGHT x 2 in dual plane mode // So the maximum would be 144 (12 x 12) elements, x 2 for two planes #define DIVCEIL(number, divisor) (number + divisor - 1) / divisor + +#ifndef BLOCK_WIDTH +#define BLOCK_WIDTH block_dims.x +#define BLOCK_HEIGHT block_dims.y #define ARRAY_NUM_ELEMENTS 144 +#else +#define ARRAY_NUM_ELEMENTS BLOCK_WIDTH * BLOCK_HEIGHT +#endif + #define VECTOR_ARRAY_SIZE DIVCEIL(ARRAY_NUM_ELEMENTS * 2, 4) uvec4 result_vector[VECTOR_ARRAY_SIZE]; @@ -265,7 +275,7 @@ uint Hash52(uint p) { } uint Select2DPartition(uint seed, uint x, uint y, uint partition_count) { - if ((block_dims.y * block_dims.x) < 32) { + if ((BLOCK_WIDTH * BLOCK_HEIGHT) < 32) { x <<= 1; y <<= 1; } @@ -878,8 +888,8 @@ uint UnquantizeTexelWeight(EncodingData val) { uvec4 unquantized_texel_weights[VECTOR_ARRAY_SIZE]; void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { - const uint Ds = uint((block_dims.x * 0.5f + 1024) / (block_dims.x - 1)); - const uint Dt = uint((block_dims.y * 0.5f + 1024) / (block_dims.y - 1)); + const uint Ds = uint((BLOCK_WIDTH * 0.5f + 1024) / (BLOCK_WIDTH - 1)); + const uint Dt = uint((BLOCK_HEIGHT * 0.5f + 1024) / (BLOCK_HEIGHT - 1)); const uint num_planes = is_dual_plane ? 2 : 1; const uint area = size.x * size.y; const uint loop_count = min(result_index, area * num_planes); @@ -890,8 +900,8 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { UnquantizeTexelWeight(GetEncodingFromVector(itr)); } for (uint plane = 0; plane < num_planes; ++plane) { - for (uint t = 0; t < block_dims.y; t++) { - for (uint s = 0; s < block_dims.x; s++) { + for (uint t = 0; t < BLOCK_HEIGHT; t++) { + for (uint s = 0; s < BLOCK_WIDTH; s++) { const uint cs = Ds * s; const uint ct = Dt * t; const uint gs = (cs * (size.x - 1) + 32) >> 6; @@ -934,7 +944,7 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { VectorIndicesFromBase(offset_base); p.w = result_vector[array_index][vector_index]; } - const uint offset = (t * block_dims.x + s) + ARRAY_NUM_ELEMENTS * plane; + const uint offset = (t * BLOCK_WIDTH + s) + ARRAY_NUM_ELEMENTS * plane; const uint array_index = offset / 4; const uint vector_index = offset % 4; unquantized_texel_weights[array_index][vector_index] = (uint(dot(p, w)) + 8) >> 4; @@ -976,8 +986,8 @@ int FindLayout(uint mode) { void FillError(ivec3 coord) { - for (uint j = 0; j < block_dims.y; j++) { - for (uint i = 0; i < block_dims.x; i++) { + for (uint j = 0; j < BLOCK_HEIGHT; j++) { + for (uint i = 0; i < BLOCK_WIDTH; i++) { imageStore(dest_image, coord + ivec3(i, j, 0), vec4(0.0, 0.0, 0.0, 0.0)); } } @@ -993,8 +1003,8 @@ void FillVoidExtentLDR(ivec3 coord) { const float r = float(r_u) / 65535.0f; const float g = float(g_u) / 65535.0f; const float b = float(b_u) / 65535.0f; - for (uint j = 0; j < block_dims.y; j++) { - for (uint i = 0; i < block_dims.x; i++) { + for (uint j = 0; j < BLOCK_HEIGHT; j++) { + for (uint i = 0; i < BLOCK_WIDTH; i++) { imageStore(dest_image, coord + ivec3(i, j, 0), vec4(r, g, b, a)); } } @@ -1089,7 +1099,7 @@ void DecompressBlock(ivec3 coord) { return; } const uvec2 size_params = DecodeBlockSize(mode); - if ((size_params.x > block_dims.x) || (size_params.y > block_dims.y)) { + if ((size_params.x > BLOCK_WIDTH) || (size_params.y > BLOCK_HEIGHT)) { FillError(coord); return; } @@ -1218,21 +1228,21 @@ void DecompressBlock(ivec3 coord) { DecodeIntegerSequence(max_weight, GetNumWeightValues(size_params, dual_plane)); UnquantizeTexelWeights(size_params, dual_plane); - for (uint j = 0; j < block_dims.y; j++) { - for (uint i = 0; i < block_dims.x; i++) { + for (uint j = 0; j < BLOCK_HEIGHT; j++) { + for (uint i = 0; i < BLOCK_WIDTH; i++) { uint local_partition = 0; if (num_partitions > 1) { local_partition = Select2DPartition(partition_index, i, j, num_partitions); } const uvec4 C0 = ReplicateByteTo16(endpoints0[local_partition]); const uvec4 C1 = ReplicateByteTo16(endpoints1[local_partition]); - const uint weight_offset = (j * block_dims.x + i); + const uint weight_offset = (j * BLOCK_WIDTH + i); const uint array_index = weight_offset / 4; const uint vector_index = weight_offset % 4; const uint primary_weight = unquantized_texel_weights[array_index][vector_index]; uvec4 weight_vec = uvec4(primary_weight); if (dual_plane) { - const uint secondary_weight_offset = (j * block_dims.x + i) + ARRAY_NUM_ELEMENTS; + const uint secondary_weight_offset = (j * BLOCK_WIDTH + i) + ARRAY_NUM_ELEMENTS; const uint secondary_array_index = secondary_weight_offset / 4; const uint secondary_vector_index = secondary_weight_offset % 4; const uint secondary_weight = @@ -1270,7 +1280,7 @@ void main() { offset += (pos.x >> GOB_SIZE_X_SHIFT) << x_shift; offset += swizzle; - const ivec3 coord = ivec3(gl_GlobalInvocationID * uvec3(block_dims, 1)); + const ivec3 coord = ivec3(gl_GlobalInvocationID * uvec3(BLOCK_WIDTH, BLOCK_HEIGHT, 1)); if (any(greaterThanEqual(coord, imageSize(dest_image)))) { return; } diff --git a/src/video_core/host_shaders/astc_decoder_spv_includes.h b/src/video_core/host_shaders/astc_decoder_spv_includes.h new file mode 100644 index 000000000..44ee50c5f --- /dev/null +++ b/src/video_core/host_shaders/astc_decoder_spv_includes.h @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "video_core/host_shaders/astc_decoder_comp_10x10_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_10x5_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_10x6_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_10x8_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_12x10_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_12x12_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_4x4_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_5x4_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_5x5_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_6x5_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_6x6_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_8x5_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_8x6_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_8x8_spv.h" +#include "video_core/host_shaders/astc_decoder_comp_spv.h" diff --git a/src/video_core/renderer_vulkan/vk_compute_pass.cpp b/src/video_core/renderer_vulkan/vk_compute_pass.cpp index 54ee030ce..f22342252 100644 --- a/src/video_core/renderer_vulkan/vk_compute_pass.cpp +++ b/src/video_core/renderer_vulkan/vk_compute_pass.cpp @@ -11,7 +11,7 @@ #include "common/assert.h" #include "common/common_types.h" #include "common/div_ceil.h" -#include "video_core/host_shaders/astc_decoder_comp_spv.h" +#include "video_core/host_shaders/astc_decoder_spv_includes.h" #include "video_core/host_shaders/vulkan_quad_indexed_comp_spv.h" #include "video_core/host_shaders/vulkan_uint8_comp_spv.h" #include "video_core/renderer_vulkan/vk_compute_pass.h" @@ -124,13 +124,62 @@ constexpr std::array }}; struct AstcPushConstants { - std::array blocks_dims; u32 layer_stride; u32 block_size; u32 x_shift; u32 block_height; u32 block_height_mask; }; + +size_t AstcFormatIndex(VideoCore::Surface::PixelFormat format) { + switch (format) { + case VideoCore::Surface::PixelFormat::ASTC_2D_4X4_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_4X4_UNORM: + return 0; + case VideoCore::Surface::PixelFormat::ASTC_2D_5X4_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_5X4_UNORM: + return 1; + case VideoCore::Surface::PixelFormat::ASTC_2D_5X5_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_5X5_UNORM: + return 2; + case VideoCore::Surface::PixelFormat::ASTC_2D_6X5_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_6X5_UNORM: + return 3; + case VideoCore::Surface::PixelFormat::ASTC_2D_6X6_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_6X6_UNORM: + return 4; + case VideoCore::Surface::PixelFormat::ASTC_2D_8X5_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_8X5_UNORM: + return 5; + case VideoCore::Surface::PixelFormat::ASTC_2D_8X6_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_8X6_UNORM: + return 6; + case VideoCore::Surface::PixelFormat::ASTC_2D_8X8_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_8X8_UNORM: + return 7; + case VideoCore::Surface::PixelFormat::ASTC_2D_10X5_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_10X5_UNORM: + return 8; + case VideoCore::Surface::PixelFormat::ASTC_2D_10X6_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_10X6_UNORM: + return 9; + case VideoCore::Surface::PixelFormat::ASTC_2D_10X8_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_10X8_UNORM: + return 10; + case VideoCore::Surface::PixelFormat::ASTC_2D_10X10_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_10X10_UNORM: + return 11; + case VideoCore::Surface::PixelFormat::ASTC_2D_12X10_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_12X10_UNORM: + return 12; + case VideoCore::Surface::PixelFormat::ASTC_2D_12X12_SRGB: + case VideoCore::Surface::PixelFormat::ASTC_2D_12X12_UNORM: + return 13; + default: + UNREACHABLE(); + return 0; + } +} } // Anonymous namespace ComputePass::ComputePass(const Device& device_, DescriptorPool& descriptor_pool, @@ -312,19 +361,53 @@ ASTCDecoderPass::ASTCDecoderPass(const Device& device_, Scheduler& scheduler_, COMPUTE_PUSH_CONSTANT_RANGE, ASTC_DECODER_COMP_SPV), scheduler{scheduler_}, staging_buffer_pool{staging_buffer_pool_}, compute_pass_descriptor_queue{compute_pass_descriptor_queue_}, memory_allocator{ - memory_allocator_} {} + memory_allocator_} { + // These must match the order found in AstcFormatIndex + static constexpr std::array, 14> ASTC_SHADERS{ + ASTC_DECODER_COMP_4X4_SPV, ASTC_DECODER_COMP_5X4_SPV, ASTC_DECODER_COMP_5X5_SPV, + ASTC_DECODER_COMP_6X5_SPV, ASTC_DECODER_COMP_6X6_SPV, ASTC_DECODER_COMP_8X5_SPV, + ASTC_DECODER_COMP_8X6_SPV, ASTC_DECODER_COMP_8X8_SPV, ASTC_DECODER_COMP_10X5_SPV, + ASTC_DECODER_COMP_10X6_SPV, ASTC_DECODER_COMP_10X8_SPV, ASTC_DECODER_COMP_10X10_SPV, + ASTC_DECODER_COMP_12X10_SPV, ASTC_DECODER_COMP_12X12_SPV, + }; + for (size_t index = 0; index < ASTC_SHADERS.size(); ++index) { + const auto& code = ASTC_SHADERS[index]; + const auto module_ = device.GetLogical().CreateShaderModule({ + .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .codeSize = static_cast(code.size_bytes()), + .pCode = code.data(), + }); + device.SaveShader(code); + astc_pipelines[index] = device.GetLogical().CreateComputePipeline({ + .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .stage{ + .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .stage = VK_SHADER_STAGE_COMPUTE_BIT, + .module = *module_, + .pName = "main", + .pSpecializationInfo = nullptr, + }, + .layout = *layout, + .basePipelineHandle = nullptr, + .basePipelineIndex = 0, + }); + } +} ASTCDecoderPass::~ASTCDecoderPass() = default; void ASTCDecoderPass::Assemble(Image& image, const StagingBufferRef& map, std::span swizzles) { using namespace VideoCommon::Accelerated; - const std::array block_dims{ - VideoCore::Surface::DefaultBlockWidth(image.info.format), - VideoCore::Surface::DefaultBlockHeight(image.info.format), - }; + scheduler.RequestOutsideRenderPassOperationContext(); - const VkPipeline vk_pipeline = *pipeline; + const VkPipeline vk_pipeline = *astc_pipelines[AstcFormatIndex(image.info.format)]; const VkImageAspectFlags aspect_mask = image.AspectMask(); const VkImage vk_image = image.Handle(); const bool is_initialized = image.ExchangeInitialization(); @@ -371,10 +454,9 @@ void ASTCDecoderPass::Assemble(Image& image, const StagingBufferRef& map, ASSERT(params.origin == (std::array{0, 0, 0})); ASSERT(params.destination == (std::array{0, 0, 0})); ASSERT(params.bytes_per_block_log2 == 4); - scheduler.Record([this, num_dispatches_x, num_dispatches_y, num_dispatches_z, block_dims, - params, descriptor_data](vk::CommandBuffer cmdbuf) { + scheduler.Record([this, num_dispatches_x, num_dispatches_y, num_dispatches_z, params, + descriptor_data](vk::CommandBuffer cmdbuf) { const AstcPushConstants uniforms{ - .blocks_dims = block_dims, .layer_stride = params.layer_stride, .block_size = params.block_size, .x_shift = params.x_shift, diff --git a/src/video_core/renderer_vulkan/vk_compute_pass.h b/src/video_core/renderer_vulkan/vk_compute_pass.h index dd3927376..1f264bea6 100644 --- a/src/video_core/renderer_vulkan/vk_compute_pass.h +++ b/src/video_core/renderer_vulkan/vk_compute_pass.h @@ -95,6 +95,8 @@ public: std::span swizzles); private: + std::array astc_pipelines; + Scheduler& scheduler; StagingBufferPool& staging_buffer_pool; ComputePassDescriptorQueue& compute_pass_descriptor_queue; From 8ce158bce6912b2263f1724e6c09d8b517ef18c3 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Wed, 2 Aug 2023 18:15:59 -0400 Subject: [PATCH 24/33] Revert "vulkan dims specialization" This reverts commit e6243058f2269bd79ac8479d58e55feec2611e9d. --- src/video_core/host_shaders/CMakeLists.txt | 57 ---------- src/video_core/host_shaders/astc_decoder.comp | 42 +++---- .../host_shaders/astc_decoder_spv_includes.h | 20 ---- .../renderer_vulkan/vk_compute_pass.cpp | 104 ++---------------- .../renderer_vulkan/vk_compute_pass.h | 2 - 5 files changed, 27 insertions(+), 198 deletions(-) delete mode 100644 src/video_core/host_shaders/astc_decoder_spv_includes.h diff --git a/src/video_core/host_shaders/CMakeLists.txt b/src/video_core/host_shaders/CMakeLists.txt index 20e8388ee..e61d9af80 100644 --- a/src/video_core/host_shaders/CMakeLists.txt +++ b/src/video_core/host_shaders/CMakeLists.txt @@ -13,11 +13,6 @@ set(GLSL_INCLUDES ${FIDELITYFX_FILES} ) -set(ASTC_INCLUDES - # astc_decoder_glsl_includes.h - astc_decoder_spv_includes.h -) - set(SHADER_FILES astc_decoder.comp blit_color_float.frag @@ -100,60 +95,9 @@ if (NOT GLSLANG_ERROR STREQUAL "") set(QUIET_FLAG "") endif() -macro(ASTC_GEN) - # paired list of valid astc block dimensions - set(ASTC_WIDTHS 4 5 5 6 6 6 8 8 8 10 10 10 10 12 12) - set(ASTC_HEIGHTS 4 4 5 4 5 6 5 6 8 5 6 8 10 10 12) - list(LENGTH ASTC_WIDTHS NUM_ASTC_FORMATS) - math(EXPR NUM_ASTC_FORMATS "${NUM_ASTC_FORMATS}-1") - foreach(i RANGE ${NUM_ASTC_FORMATS}) - list(GET ASTC_WIDTHS ${i} ASTC_WIDTH) - list(GET ASTC_HEIGHTS ${i} ASTC_HEIGHT) - - # Vulkan SPIR-V Specialization - - string(TOUPPER ${SHADER_NAME}_${ASTC_WIDTH}x${ASTC_HEIGHT}_SPV SPIRV_VARIABLE_NAME) - set(SPIRV_HEADER_FILE ${SHADER_DIR}/${SHADER_NAME}_${ASTC_WIDTH}x${ASTC_HEIGHT}_spv.h) - add_custom_command( - OUTPUT - ${SPIRV_HEADER_FILE} - COMMAND - ${GLSLANGVALIDATOR} -V -DBLOCK_WIDTH=${ASTC_WIDTH} -DBLOCK_HEIGHT=${ASTC_HEIGHT} ${QUIET_FLAG} ${GLSL_FLAGS} --variable-name ${SPIRV_VARIABLE_NAME} -o ${SPIRV_HEADER_FILE} ${SOURCE_FILE} - MAIN_DEPENDENCY - ${SOURCE_FILE} - ) - set(SHADER_HEADERS ${SHADER_HEADERS} ${SPIRV_HEADER_FILE}) - - # GLSL Specialization - # Disabled as there was no noticeable performance uplift specializing the shaders for OGL - - # set(SOURCE_HEADER_FILE ${SHADER_DIR}/${SHADER_NAME}_${ASTC_WIDTH}x${ASTC_HEIGHT}.h) - # set(SHADER_DEFINES "#define BLOCK_WIDTH ${ASTC_WIDTH}" "#define BLOCK_HEIGHT ${ASTC_HEIGHT}") - # set(DEFINES_LINE_NUMBER 14) - # string(TOUPPER ${SHADER_NAME}_${ASTC_WIDTH}x${ASTC_HEIGHT} GLSL_VARIABLE_NAME) - # add_custom_command( - # OUTPUT - # ${SOURCE_HEADER_FILE} - # COMMAND - # ${CMAKE_COMMAND} -P ${HEADER_GENERATOR} ${SOURCE_FILE} ${SOURCE_HEADER_FILE} ${INPUT_FILE} "${SHADER_DEFINES}" ${DEFINES_LINE_NUMBER} ${GLSL_VARIABLE_NAME} - # MAIN_DEPENDENCY - # ${SOURCE_FILE} - # DEPENDS - # ${INPUT_FILE} - # ${SOURCE_FILE} - # ) - # set(SHADER_HEADERS ${SHADER_HEADERS} ${SOURCE_HEADER_FILE}) - endforeach() -endmacro() - foreach(FILENAME IN ITEMS ${SHADER_FILES}) string(REPLACE "." "_" SHADER_NAME ${FILENAME}) set(SOURCE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}) - - if (${FILENAME} MATCHES "astc_decoder.comp") - ASTC_GEN() - endif() - # Skip generating source headers on Vulkan exclusive files if (NOT ${FILENAME} MATCHES "vulkan.*") set(SOURCE_HEADER_FILE ${SHADER_DIR}/${SHADER_NAME}.h) @@ -207,7 +151,6 @@ endforeach() set(SHADER_SOURCES ${SHADER_FILES}) list(APPEND SHADER_SOURCES ${GLSL_INCLUDES}) -list(APPEND SHADER_SOURCES ${ASTC_INCLUDES}) add_custom_target(host_shaders DEPENDS diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index b4bb8299f..a33c916ac 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -24,9 +24,7 @@ layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; BEGIN_PUSH_CONSTANTS -#ifndef BLOCK_WIDTH UNIFORM(1) uvec2 block_dims; -#endif UNIFORM(2) uint layer_stride; UNIFORM(3) uint block_size; UNIFORM(4) uint x_shift; @@ -77,15 +75,7 @@ int color_bitsread = 0; // At most will require BLOCK_WIDTH x BLOCK_HEIGHT x 2 in dual plane mode // So the maximum would be 144 (12 x 12) elements, x 2 for two planes #define DIVCEIL(number, divisor) (number + divisor - 1) / divisor - -#ifndef BLOCK_WIDTH -#define BLOCK_WIDTH block_dims.x -#define BLOCK_HEIGHT block_dims.y #define ARRAY_NUM_ELEMENTS 144 -#else -#define ARRAY_NUM_ELEMENTS BLOCK_WIDTH * BLOCK_HEIGHT -#endif - #define VECTOR_ARRAY_SIZE DIVCEIL(ARRAY_NUM_ELEMENTS * 2, 4) uvec4 result_vector[VECTOR_ARRAY_SIZE]; @@ -275,7 +265,7 @@ uint Hash52(uint p) { } uint Select2DPartition(uint seed, uint x, uint y, uint partition_count) { - if ((BLOCK_WIDTH * BLOCK_HEIGHT) < 32) { + if ((block_dims.y * block_dims.x) < 32) { x <<= 1; y <<= 1; } @@ -888,8 +878,8 @@ uint UnquantizeTexelWeight(EncodingData val) { uvec4 unquantized_texel_weights[VECTOR_ARRAY_SIZE]; void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { - const uint Ds = uint((BLOCK_WIDTH * 0.5f + 1024) / (BLOCK_WIDTH - 1)); - const uint Dt = uint((BLOCK_HEIGHT * 0.5f + 1024) / (BLOCK_HEIGHT - 1)); + const uint Ds = uint((block_dims.x * 0.5f + 1024) / (block_dims.x - 1)); + const uint Dt = uint((block_dims.y * 0.5f + 1024) / (block_dims.y - 1)); const uint num_planes = is_dual_plane ? 2 : 1; const uint area = size.x * size.y; const uint loop_count = min(result_index, area * num_planes); @@ -900,8 +890,8 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { UnquantizeTexelWeight(GetEncodingFromVector(itr)); } for (uint plane = 0; plane < num_planes; ++plane) { - for (uint t = 0; t < BLOCK_HEIGHT; t++) { - for (uint s = 0; s < BLOCK_WIDTH; s++) { + for (uint t = 0; t < block_dims.y; t++) { + for (uint s = 0; s < block_dims.x; s++) { const uint cs = Ds * s; const uint ct = Dt * t; const uint gs = (cs * (size.x - 1) + 32) >> 6; @@ -944,7 +934,7 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { VectorIndicesFromBase(offset_base); p.w = result_vector[array_index][vector_index]; } - const uint offset = (t * BLOCK_WIDTH + s) + ARRAY_NUM_ELEMENTS * plane; + const uint offset = (t * block_dims.x + s) + ARRAY_NUM_ELEMENTS * plane; const uint array_index = offset / 4; const uint vector_index = offset % 4; unquantized_texel_weights[array_index][vector_index] = (uint(dot(p, w)) + 8) >> 4; @@ -986,8 +976,8 @@ int FindLayout(uint mode) { void FillError(ivec3 coord) { - for (uint j = 0; j < BLOCK_HEIGHT; j++) { - for (uint i = 0; i < BLOCK_WIDTH; i++) { + for (uint j = 0; j < block_dims.y; j++) { + for (uint i = 0; i < block_dims.x; i++) { imageStore(dest_image, coord + ivec3(i, j, 0), vec4(0.0, 0.0, 0.0, 0.0)); } } @@ -1003,8 +993,8 @@ void FillVoidExtentLDR(ivec3 coord) { const float r = float(r_u) / 65535.0f; const float g = float(g_u) / 65535.0f; const float b = float(b_u) / 65535.0f; - for (uint j = 0; j < BLOCK_HEIGHT; j++) { - for (uint i = 0; i < BLOCK_WIDTH; i++) { + for (uint j = 0; j < block_dims.y; j++) { + for (uint i = 0; i < block_dims.x; i++) { imageStore(dest_image, coord + ivec3(i, j, 0), vec4(r, g, b, a)); } } @@ -1099,7 +1089,7 @@ void DecompressBlock(ivec3 coord) { return; } const uvec2 size_params = DecodeBlockSize(mode); - if ((size_params.x > BLOCK_WIDTH) || (size_params.y > BLOCK_HEIGHT)) { + if ((size_params.x > block_dims.x) || (size_params.y > block_dims.y)) { FillError(coord); return; } @@ -1228,21 +1218,21 @@ void DecompressBlock(ivec3 coord) { DecodeIntegerSequence(max_weight, GetNumWeightValues(size_params, dual_plane)); UnquantizeTexelWeights(size_params, dual_plane); - for (uint j = 0; j < BLOCK_HEIGHT; j++) { - for (uint i = 0; i < BLOCK_WIDTH; i++) { + for (uint j = 0; j < block_dims.y; j++) { + for (uint i = 0; i < block_dims.x; i++) { uint local_partition = 0; if (num_partitions > 1) { local_partition = Select2DPartition(partition_index, i, j, num_partitions); } const uvec4 C0 = ReplicateByteTo16(endpoints0[local_partition]); const uvec4 C1 = ReplicateByteTo16(endpoints1[local_partition]); - const uint weight_offset = (j * BLOCK_WIDTH + i); + const uint weight_offset = (j * block_dims.x + i); const uint array_index = weight_offset / 4; const uint vector_index = weight_offset % 4; const uint primary_weight = unquantized_texel_weights[array_index][vector_index]; uvec4 weight_vec = uvec4(primary_weight); if (dual_plane) { - const uint secondary_weight_offset = (j * BLOCK_WIDTH + i) + ARRAY_NUM_ELEMENTS; + const uint secondary_weight_offset = (j * block_dims.x + i) + ARRAY_NUM_ELEMENTS; const uint secondary_array_index = secondary_weight_offset / 4; const uint secondary_vector_index = secondary_weight_offset % 4; const uint secondary_weight = @@ -1280,7 +1270,7 @@ void main() { offset += (pos.x >> GOB_SIZE_X_SHIFT) << x_shift; offset += swizzle; - const ivec3 coord = ivec3(gl_GlobalInvocationID * uvec3(BLOCK_WIDTH, BLOCK_HEIGHT, 1)); + const ivec3 coord = ivec3(gl_GlobalInvocationID * uvec3(block_dims, 1)); if (any(greaterThanEqual(coord, imageSize(dest_image)))) { return; } diff --git a/src/video_core/host_shaders/astc_decoder_spv_includes.h b/src/video_core/host_shaders/astc_decoder_spv_includes.h deleted file mode 100644 index 44ee50c5f..000000000 --- a/src/video_core/host_shaders/astc_decoder_spv_includes.h +++ /dev/null @@ -1,20 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#pragma once - -#include "video_core/host_shaders/astc_decoder_comp_10x10_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_10x5_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_10x6_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_10x8_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_12x10_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_12x12_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_4x4_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_5x4_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_5x5_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_6x5_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_6x6_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_8x5_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_8x6_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_8x8_spv.h" -#include "video_core/host_shaders/astc_decoder_comp_spv.h" diff --git a/src/video_core/renderer_vulkan/vk_compute_pass.cpp b/src/video_core/renderer_vulkan/vk_compute_pass.cpp index f22342252..54ee030ce 100644 --- a/src/video_core/renderer_vulkan/vk_compute_pass.cpp +++ b/src/video_core/renderer_vulkan/vk_compute_pass.cpp @@ -11,7 +11,7 @@ #include "common/assert.h" #include "common/common_types.h" #include "common/div_ceil.h" -#include "video_core/host_shaders/astc_decoder_spv_includes.h" +#include "video_core/host_shaders/astc_decoder_comp_spv.h" #include "video_core/host_shaders/vulkan_quad_indexed_comp_spv.h" #include "video_core/host_shaders/vulkan_uint8_comp_spv.h" #include "video_core/renderer_vulkan/vk_compute_pass.h" @@ -124,62 +124,13 @@ constexpr std::array }}; struct AstcPushConstants { + std::array blocks_dims; u32 layer_stride; u32 block_size; u32 x_shift; u32 block_height; u32 block_height_mask; }; - -size_t AstcFormatIndex(VideoCore::Surface::PixelFormat format) { - switch (format) { - case VideoCore::Surface::PixelFormat::ASTC_2D_4X4_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_4X4_UNORM: - return 0; - case VideoCore::Surface::PixelFormat::ASTC_2D_5X4_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_5X4_UNORM: - return 1; - case VideoCore::Surface::PixelFormat::ASTC_2D_5X5_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_5X5_UNORM: - return 2; - case VideoCore::Surface::PixelFormat::ASTC_2D_6X5_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_6X5_UNORM: - return 3; - case VideoCore::Surface::PixelFormat::ASTC_2D_6X6_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_6X6_UNORM: - return 4; - case VideoCore::Surface::PixelFormat::ASTC_2D_8X5_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_8X5_UNORM: - return 5; - case VideoCore::Surface::PixelFormat::ASTC_2D_8X6_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_8X6_UNORM: - return 6; - case VideoCore::Surface::PixelFormat::ASTC_2D_8X8_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_8X8_UNORM: - return 7; - case VideoCore::Surface::PixelFormat::ASTC_2D_10X5_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_10X5_UNORM: - return 8; - case VideoCore::Surface::PixelFormat::ASTC_2D_10X6_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_10X6_UNORM: - return 9; - case VideoCore::Surface::PixelFormat::ASTC_2D_10X8_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_10X8_UNORM: - return 10; - case VideoCore::Surface::PixelFormat::ASTC_2D_10X10_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_10X10_UNORM: - return 11; - case VideoCore::Surface::PixelFormat::ASTC_2D_12X10_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_12X10_UNORM: - return 12; - case VideoCore::Surface::PixelFormat::ASTC_2D_12X12_SRGB: - case VideoCore::Surface::PixelFormat::ASTC_2D_12X12_UNORM: - return 13; - default: - UNREACHABLE(); - return 0; - } -} } // Anonymous namespace ComputePass::ComputePass(const Device& device_, DescriptorPool& descriptor_pool, @@ -361,53 +312,19 @@ ASTCDecoderPass::ASTCDecoderPass(const Device& device_, Scheduler& scheduler_, COMPUTE_PUSH_CONSTANT_RANGE, ASTC_DECODER_COMP_SPV), scheduler{scheduler_}, staging_buffer_pool{staging_buffer_pool_}, compute_pass_descriptor_queue{compute_pass_descriptor_queue_}, memory_allocator{ - memory_allocator_} { - // These must match the order found in AstcFormatIndex - static constexpr std::array, 14> ASTC_SHADERS{ - ASTC_DECODER_COMP_4X4_SPV, ASTC_DECODER_COMP_5X4_SPV, ASTC_DECODER_COMP_5X5_SPV, - ASTC_DECODER_COMP_6X5_SPV, ASTC_DECODER_COMP_6X6_SPV, ASTC_DECODER_COMP_8X5_SPV, - ASTC_DECODER_COMP_8X6_SPV, ASTC_DECODER_COMP_8X8_SPV, ASTC_DECODER_COMP_10X5_SPV, - ASTC_DECODER_COMP_10X6_SPV, ASTC_DECODER_COMP_10X8_SPV, ASTC_DECODER_COMP_10X10_SPV, - ASTC_DECODER_COMP_12X10_SPV, ASTC_DECODER_COMP_12X12_SPV, - }; - for (size_t index = 0; index < ASTC_SHADERS.size(); ++index) { - const auto& code = ASTC_SHADERS[index]; - const auto module_ = device.GetLogical().CreateShaderModule({ - .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, - .pNext = nullptr, - .flags = 0, - .codeSize = static_cast(code.size_bytes()), - .pCode = code.data(), - }); - device.SaveShader(code); - astc_pipelines[index] = device.GetLogical().CreateComputePipeline({ - .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, - .pNext = nullptr, - .flags = 0, - .stage{ - .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, - .pNext = nullptr, - .flags = 0, - .stage = VK_SHADER_STAGE_COMPUTE_BIT, - .module = *module_, - .pName = "main", - .pSpecializationInfo = nullptr, - }, - .layout = *layout, - .basePipelineHandle = nullptr, - .basePipelineIndex = 0, - }); - } -} + memory_allocator_} {} ASTCDecoderPass::~ASTCDecoderPass() = default; void ASTCDecoderPass::Assemble(Image& image, const StagingBufferRef& map, std::span swizzles) { using namespace VideoCommon::Accelerated; - + const std::array block_dims{ + VideoCore::Surface::DefaultBlockWidth(image.info.format), + VideoCore::Surface::DefaultBlockHeight(image.info.format), + }; scheduler.RequestOutsideRenderPassOperationContext(); - const VkPipeline vk_pipeline = *astc_pipelines[AstcFormatIndex(image.info.format)]; + const VkPipeline vk_pipeline = *pipeline; const VkImageAspectFlags aspect_mask = image.AspectMask(); const VkImage vk_image = image.Handle(); const bool is_initialized = image.ExchangeInitialization(); @@ -454,9 +371,10 @@ void ASTCDecoderPass::Assemble(Image& image, const StagingBufferRef& map, ASSERT(params.origin == (std::array{0, 0, 0})); ASSERT(params.destination == (std::array{0, 0, 0})); ASSERT(params.bytes_per_block_log2 == 4); - scheduler.Record([this, num_dispatches_x, num_dispatches_y, num_dispatches_z, params, - descriptor_data](vk::CommandBuffer cmdbuf) { + scheduler.Record([this, num_dispatches_x, num_dispatches_y, num_dispatches_z, block_dims, + params, descriptor_data](vk::CommandBuffer cmdbuf) { const AstcPushConstants uniforms{ + .blocks_dims = block_dims, .layer_stride = params.layer_stride, .block_size = params.block_size, .x_shift = params.x_shift, diff --git a/src/video_core/renderer_vulkan/vk_compute_pass.h b/src/video_core/renderer_vulkan/vk_compute_pass.h index 1f264bea6..dd3927376 100644 --- a/src/video_core/renderer_vulkan/vk_compute_pass.h +++ b/src/video_core/renderer_vulkan/vk_compute_pass.h @@ -95,8 +95,6 @@ public: std::span swizzles); private: - std::array astc_pipelines; - Scheduler& scheduler; StagingBufferPool& staging_buffer_pool; ComputePassDescriptorQueue& compute_pass_descriptor_queue; From b36e645fee2104501be7e426132159dfc97eea05 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Wed, 2 Aug 2023 18:33:43 -0400 Subject: [PATCH 25/33] undo uint --- src/video_core/host_shaders/astc_decoder.comp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index a33c916ac..547cd881f 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -79,7 +79,7 @@ int color_bitsread = 0; #define VECTOR_ARRAY_SIZE DIVCEIL(ARRAY_NUM_ELEMENTS * 2, 4) uvec4 result_vector[VECTOR_ARRAY_SIZE]; -uint result_index = 0; +int result_index = 0; uint result_vector_max_index; bool result_limit_reached = false; @@ -122,8 +122,8 @@ void ResultEmplaceBack(EncodingData val) { result_limit_reached = true; return; } - const uint array_index = result_index / 4u; - const uint vector_index = result_index % 4u; + const uint array_index = result_index / 4; + const uint vector_index = result_index % 4; result_vector[array_index][vector_index] = val.data; ++result_index; } From 31a0cff0367bddb51785145fba1089b625e06b62 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 6 Aug 2023 12:18:55 -0400 Subject: [PATCH 26/33] minor --- src/video_core/host_shaders/astc_decoder.comp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 547cd881f..9d9532a98 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -809,18 +809,15 @@ uint UnquantizeTexelWeight(EncodingData val) { const uint A = ReplicateBitTo7((bitval & 1)); uint B = 0, C = 0, D = 0; uint result = 0; + const uint bitlen_0_results[5] = {0, 16, 32, 48, 64}; switch (encoding) { case JUST_BITS: - result = FastReplicateTo6(bitval, bitlen); - break; + return FastReplicateTo6(bitval, bitlen); case TRIT: { D = QuintTritValue(val); switch (bitlen) { - case 0: { - const uint results[3] = {0, 32, 63}; - result = results[D]; - break; - } + case 0: + return bitlen_0_results[D * 2]; case 1: { C = 50; break; @@ -845,11 +842,8 @@ uint UnquantizeTexelWeight(EncodingData val) { case QUINT: { D = QuintTritValue(val); switch (bitlen) { - case 0: { - const uint results[5] = {0, 16, 32, 47, 63}; - result = results[D]; - break; - } + case 0: + return bitlen_0_results[D]; case 1: { C = 28; break; From 913803bf653989d2d08ddd39caf2216b55275dfc Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 6 Aug 2023 13:32:35 -0400 Subject: [PATCH 27/33] Compute Replicate --- src/video_core/host_shaders/astc_decoder.comp | 105 ++++-------------- 1 file changed, 20 insertions(+), 85 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 9d9532a98..5e922d1fe 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -140,98 +140,33 @@ uint ReplicateBitTo9(uint value) { return value * 511; } -uint FastReplicateTo8(uint value, uint num_bits) { - if (value == 0) { +uint ReplicateBits(uint value, uint num_bits, uint to_bit) { + if (value == 0 || num_bits == 0) { return 0; } - const uint array_index = value / 4; - const uint vector_index = bitfieldExtract(value, 0, 2); - switch (num_bits) { - case 1: - return 255; - case 2: { - const uvec4 REPLICATE_2_BIT_TO_8_TABLE = (uvec4(0, 85, 170, 255)); - return REPLICATE_2_BIT_TO_8_TABLE[vector_index]; + if (num_bits >= to_bit) { + return value; } - case 3: { - const uvec4 REPLICATE_3_BIT_TO_8_TABLE[2] = - uvec4[](uvec4(0, 36, 73, 109), uvec4(146, 182, 219, 255)); - return REPLICATE_3_BIT_TO_8_TABLE[array_index][vector_index]; + const uint v = value & uint((1 << num_bits) - 1); + uint res = v; + uint reslen = num_bits; + while (reslen < to_bit) { + const uint num_dst_bits_to_shift_up = min(num_bits, to_bit - reslen); + const uint num_src_bits_to_shift_down = num_bits - num_dst_bits_to_shift_up; + + res <<= num_dst_bits_to_shift_up; + res |= (v >> num_src_bits_to_shift_down); + reslen += num_bits; } - case 4: { - const uvec4 REPLICATE_4_BIT_TO_8_TABLE[4] = - uvec4[](uvec4(0, 17, 34, 51), uvec4(68, 85, 102, 119), uvec4(136, 153, 170, 187), - uvec4(204, 221, 238, 255)); - return REPLICATE_4_BIT_TO_8_TABLE[array_index][vector_index]; - } - case 5: { - const uvec4 REPLICATE_5_BIT_TO_8_TABLE[8] = - uvec4[](uvec4(0, 8, 16, 24), uvec4(33, 41, 49, 57), uvec4(66, 74, 82, 90), - uvec4(99, 107, 115, 123), uvec4(132, 140, 148, 156), uvec4(165, 173, 181, 189), - uvec4(198, 206, 214, 222), uvec4(231, 239, 247, 255)); - return REPLICATE_5_BIT_TO_8_TABLE[array_index][vector_index]; - } - case 6: { - const uvec4 REPLICATE_6_BIT_TO_8_TABLE[16] = uvec4[]( - uvec4(0, 4, 8, 12), uvec4(16, 20, 24, 28), uvec4(32, 36, 40, 44), uvec4(48, 52, 56, 60), - uvec4(65, 69, 73, 77), uvec4(81, 85, 89, 93), uvec4(97, 101, 105, 109), - uvec4(113, 117, 121, 125), uvec4(130, 134, 138, 142), uvec4(146, 150, 154, 158), - uvec4(162, 166, 170, 174), uvec4(178, 182, 186, 190), uvec4(195, 199, 203, 207), - uvec4(211, 215, 219, 223), uvec4(227, 231, 235, 239), uvec4(243, 247, 251, 255)); - return REPLICATE_6_BIT_TO_8_TABLE[array_index][vector_index]; - } - case 7: { - const uvec4 REPLICATE_7_BIT_TO_8_TABLE[32] = - uvec4[](uvec4(0, 2, 4, 6), uvec4(8, 10, 12, 14), uvec4(16, 18, 20, 22), - uvec4(24, 26, 28, 30), uvec4(32, 34, 36, 38), uvec4(40, 42, 44, 46), - uvec4(48, 50, 52, 54), uvec4(56, 58, 60, 62), uvec4(64, 66, 68, 70), - uvec4(72, 74, 76, 78), uvec4(80, 82, 84, 86), uvec4(88, 90, 92, 94), - uvec4(96, 98, 100, 102), uvec4(104, 106, 108, 110), uvec4(112, 114, 116, 118), - uvec4(120, 122, 124, 126), uvec4(129, 131, 133, 135), uvec4(137, 139, 141, 143), - uvec4(145, 147, 149, 151), uvec4(153, 155, 157, 159), uvec4(161, 163, 165, 167), - uvec4(169, 171, 173, 175), uvec4(177, 179, 181, 183), uvec4(185, 187, 189, 191), - uvec4(193, 195, 197, 199), uvec4(201, 203, 205, 207), uvec4(209, 211, 213, 215), - uvec4(217, 219, 221, 223), uvec4(225, 227, 229, 231), uvec4(233, 235, 237, 239), - uvec4(241, 243, 245, 247), uvec4(249, 251, 253, 255)); - return REPLICATE_7_BIT_TO_8_TABLE[array_index][vector_index]; - } - } - return value; + return res; +} + +uint FastReplicateTo8(uint value, uint num_bits) { + return ReplicateBits(value, num_bits, 8); } uint FastReplicateTo6(uint value, uint num_bits) { - if (value == 0) { - return 0; - } - const uint array_index = value / 4; - const uint vector_index = bitfieldExtract(value, 0, 2); - switch (num_bits) { - case 1: - return 63; - case 2: { - const uvec4 REPLICATE_2_BIT_TO_6_TABLE = uvec4(0, 21, 42, 63); - return REPLICATE_2_BIT_TO_6_TABLE[vector_index]; - } - case 3: { - const uvec4 REPLICATE_3_BIT_TO_6_TABLE[2] = - uvec4[](uvec4(0, 9, 18, 27), uvec4(36, 45, 54, 63)); - return REPLICATE_3_BIT_TO_6_TABLE[array_index][vector_index]; - } - case 4: { - const uvec4 REPLICATE_4_BIT_TO_6_TABLE[4] = - uvec4[](uvec4(0, 4, 8, 12), uvec4(17, 21, 25, 29), uvec4(34, 38, 42, 46), - uvec4(51, 55, 59, 63)); - return REPLICATE_4_BIT_TO_6_TABLE[array_index][vector_index]; - } - case 5: { - const uvec4 REPLICATE_5_BIT_TO_6_TABLE[8] = - uvec4[](uvec4(0, 2, 4, 6), uvec4(8, 10, 12, 14), uvec4(16, 18, 20, 22), - uvec4(24, 26, 28, 30), uvec4(33, 35, 37, 39), uvec4(41, 43, 45, 47), - uvec4(49, 51, 53, 55), uvec4(57, 59, 61, 63)); - return REPLICATE_5_BIT_TO_6_TABLE[array_index][vector_index]; - } - } - return value; + return ReplicateBits(value, num_bits, 6); } uint Div3Floor(uint v) { From b18c1fb1bbbcc235e9a1cbbb2704fed2e5895e61 Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 6 Aug 2023 14:49:34 -0400 Subject: [PATCH 28/33] HACK: Avoid swizzling and reuploading ASTC image every frame --- .../renderer_opengl/gl_texture_cache.cpp | 9 ++++++++- .../renderer_vulkan/vk_staging_buffer_pool.cpp | 1 + .../renderer_vulkan/vk_texture_cache.cpp | 10 +++++++++- src/video_core/texture_cache/texture_cache.h | 14 ++++++++++++-- src/video_core/textures/decoders.cpp | 9 +++++++++ 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/video_core/renderer_opengl/gl_texture_cache.cpp b/src/video_core/renderer_opengl/gl_texture_cache.cpp index 9cafd2983..9ca544ec9 100644 --- a/src/video_core/renderer_opengl/gl_texture_cache.cpp +++ b/src/video_core/renderer_opengl/gl_texture_cache.cpp @@ -554,7 +554,14 @@ void TextureCacheRuntime::Finish() { } StagingBufferMap TextureCacheRuntime::UploadStagingBuffer(size_t size) { - return staging_buffer_pool.RequestUploadBuffer(size); + static StagingBufferMap result; + static size_t last_size = 0; + if (size == last_size) { + return result; + } + last_size = size; + result = staging_buffer_pool.RequestUploadBuffer(size); + return result; } StagingBufferMap TextureCacheRuntime::DownloadStagingBuffer(size_t size) { diff --git a/src/video_core/renderer_vulkan/vk_staging_buffer_pool.cpp b/src/video_core/renderer_vulkan/vk_staging_buffer_pool.cpp index ce92f66ab..27328dab2 100644 --- a/src/video_core/renderer_vulkan/vk_staging_buffer_pool.cpp +++ b/src/video_core/renderer_vulkan/vk_staging_buffer_pool.cpp @@ -214,6 +214,7 @@ StagingBufferPool::StagingBuffersCache& StagingBufferPool::GetCache(MemoryUsage } void StagingBufferPool::ReleaseCache(MemoryUsage usage) { + return; ReleaseLevel(GetCache(usage), current_delete_level); } diff --git a/src/video_core/renderer_vulkan/vk_texture_cache.cpp b/src/video_core/renderer_vulkan/vk_texture_cache.cpp index b3e17c332..51be57b19 100644 --- a/src/video_core/renderer_vulkan/vk_texture_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_texture_cache.cpp @@ -846,7 +846,15 @@ void TextureCacheRuntime::Finish() { } StagingBufferRef TextureCacheRuntime::UploadStagingBuffer(size_t size) { - return staging_buffer_pool.Request(size, MemoryUsage::Upload); + static StagingBufferRef result; + static size_t last_size = 0; + if (size == last_size) { + return result; + } + LOG_ERROR(Debug, "Called"); + last_size = size; + result = staging_buffer_pool.Request(size, MemoryUsage::Upload); + return result; } StagingBufferRef TextureCacheRuntime::DownloadStagingBuffer(size_t size, bool deferred) { diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.h index 4457b366f..6b87987de 100644 --- a/src/video_core/texture_cache/texture_cache.h +++ b/src/video_core/texture_cache/texture_cache.h @@ -1021,8 +1021,18 @@ void TextureCache

::UploadImageContents(Image& image, StagingBuffer& staging) const GPUVAddr gpu_addr = image.gpu_addr; if (True(image.flags & ImageFlagBits::AcceleratedUpload)) { - gpu_memory->ReadBlock(gpu_addr, mapped_span.data(), mapped_span.size_bytes(), - VideoCommon::CacheType::NoTextureCache); + static u64 last_size = 0; + bool has_run = false; + if (last_size == image.unswizzled_size_bytes) { + has_run = true; + } + last_size = image.unswizzled_size_bytes; + + if (!has_run) { + LOG_ERROR(Debug, "Called"); + gpu_memory->ReadBlock(gpu_addr, mapped_span.data(), mapped_span.size_bytes(), + VideoCommon::CacheType::NoTextureCache); + } const auto uploads = FullUploadSwizzles(image.info); runtime.AccelerateImageUpload(image, staging, uploads); return; diff --git a/src/video_core/textures/decoders.cpp b/src/video_core/textures/decoders.cpp index 95bcdd37b..dde4b0904 100644 --- a/src/video_core/textures/decoders.cpp +++ b/src/video_core/textures/decoders.cpp @@ -88,6 +88,7 @@ void SwizzleImpl(std::span output, std::span input, u32 width, u32 } } + template void SwizzleSubrectImpl(std::span output, std::span input, u32 width, u32 height, u32 depth, u32 origin_x, u32 origin_y, u32 extent_x, u32 num_lines, @@ -95,6 +96,14 @@ void SwizzleSubrectImpl(std::span output, std::span input, u32 wid // The origin of the transformation can be configured here, leave it as zero as the current API // doesn't expose it. static constexpr u32 origin_z = 0; + static u32 last_width = 0; + static u32 last_height = 0; + if (last_width == width && last_height == height) { + return; + } + LOG_ERROR(Debug, "Called"); + last_width = width; + last_height = height; // We can configure here a custom pitch // As it's not exposed 'width * BYTES_PER_PIXEL' will be the expected pitch. From 9058486b9b9833d0554d3e3f11a582859657b21f Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Sun, 6 Aug 2023 14:55:05 -0400 Subject: [PATCH 29/33] Revert "HACK: Avoid swizzling and reuploading ASTC image every frame" This reverts commit b18c1fb1bbbcc235e9a1cbbb2704fed2e5895e61. --- .../renderer_opengl/gl_texture_cache.cpp | 9 +-------- .../renderer_vulkan/vk_staging_buffer_pool.cpp | 1 - .../renderer_vulkan/vk_texture_cache.cpp | 10 +--------- src/video_core/texture_cache/texture_cache.h | 14 ++------------ src/video_core/textures/decoders.cpp | 9 --------- 5 files changed, 4 insertions(+), 39 deletions(-) diff --git a/src/video_core/renderer_opengl/gl_texture_cache.cpp b/src/video_core/renderer_opengl/gl_texture_cache.cpp index 9ca544ec9..9cafd2983 100644 --- a/src/video_core/renderer_opengl/gl_texture_cache.cpp +++ b/src/video_core/renderer_opengl/gl_texture_cache.cpp @@ -554,14 +554,7 @@ void TextureCacheRuntime::Finish() { } StagingBufferMap TextureCacheRuntime::UploadStagingBuffer(size_t size) { - static StagingBufferMap result; - static size_t last_size = 0; - if (size == last_size) { - return result; - } - last_size = size; - result = staging_buffer_pool.RequestUploadBuffer(size); - return result; + return staging_buffer_pool.RequestUploadBuffer(size); } StagingBufferMap TextureCacheRuntime::DownloadStagingBuffer(size_t size) { diff --git a/src/video_core/renderer_vulkan/vk_staging_buffer_pool.cpp b/src/video_core/renderer_vulkan/vk_staging_buffer_pool.cpp index 27328dab2..ce92f66ab 100644 --- a/src/video_core/renderer_vulkan/vk_staging_buffer_pool.cpp +++ b/src/video_core/renderer_vulkan/vk_staging_buffer_pool.cpp @@ -214,7 +214,6 @@ StagingBufferPool::StagingBuffersCache& StagingBufferPool::GetCache(MemoryUsage } void StagingBufferPool::ReleaseCache(MemoryUsage usage) { - return; ReleaseLevel(GetCache(usage), current_delete_level); } diff --git a/src/video_core/renderer_vulkan/vk_texture_cache.cpp b/src/video_core/renderer_vulkan/vk_texture_cache.cpp index 51be57b19..b3e17c332 100644 --- a/src/video_core/renderer_vulkan/vk_texture_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_texture_cache.cpp @@ -846,15 +846,7 @@ void TextureCacheRuntime::Finish() { } StagingBufferRef TextureCacheRuntime::UploadStagingBuffer(size_t size) { - static StagingBufferRef result; - static size_t last_size = 0; - if (size == last_size) { - return result; - } - LOG_ERROR(Debug, "Called"); - last_size = size; - result = staging_buffer_pool.Request(size, MemoryUsage::Upload); - return result; + return staging_buffer_pool.Request(size, MemoryUsage::Upload); } StagingBufferRef TextureCacheRuntime::DownloadStagingBuffer(size_t size, bool deferred) { diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.h index 6b87987de..4457b366f 100644 --- a/src/video_core/texture_cache/texture_cache.h +++ b/src/video_core/texture_cache/texture_cache.h @@ -1021,18 +1021,8 @@ void TextureCache

::UploadImageContents(Image& image, StagingBuffer& staging) const GPUVAddr gpu_addr = image.gpu_addr; if (True(image.flags & ImageFlagBits::AcceleratedUpload)) { - static u64 last_size = 0; - bool has_run = false; - if (last_size == image.unswizzled_size_bytes) { - has_run = true; - } - last_size = image.unswizzled_size_bytes; - - if (!has_run) { - LOG_ERROR(Debug, "Called"); - gpu_memory->ReadBlock(gpu_addr, mapped_span.data(), mapped_span.size_bytes(), - VideoCommon::CacheType::NoTextureCache); - } + gpu_memory->ReadBlock(gpu_addr, mapped_span.data(), mapped_span.size_bytes(), + VideoCommon::CacheType::NoTextureCache); const auto uploads = FullUploadSwizzles(image.info); runtime.AccelerateImageUpload(image, staging, uploads); return; diff --git a/src/video_core/textures/decoders.cpp b/src/video_core/textures/decoders.cpp index dde4b0904..95bcdd37b 100644 --- a/src/video_core/textures/decoders.cpp +++ b/src/video_core/textures/decoders.cpp @@ -88,7 +88,6 @@ void SwizzleImpl(std::span output, std::span input, u32 width, u32 } } - template void SwizzleSubrectImpl(std::span output, std::span input, u32 width, u32 height, u32 depth, u32 origin_x, u32 origin_y, u32 extent_x, u32 num_lines, @@ -96,14 +95,6 @@ void SwizzleSubrectImpl(std::span output, std::span input, u32 wid // The origin of the transformation can be configured here, leave it as zero as the current API // doesn't expose it. static constexpr u32 origin_z = 0; - static u32 last_width = 0; - static u32 last_height = 0; - if (last_width == width && last_height == height) { - return; - } - LOG_ERROR(Debug, "Called"); - last_width = width; - last_height = height; // We can configure here a custom pitch // As it's not exposed 'width * BYTES_PER_PIXEL' will be the expected pitch. From 70f8ffb787a8bf394dfdc33420da3e6632472e5f Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Wed, 9 Aug 2023 17:21:33 -0400 Subject: [PATCH 30/33] GetUnquantizedWeightVector --- src/video_core/host_shaders/astc_decoder.comp | 130 +++++++++--------- 1 file changed, 62 insertions(+), 68 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 5e922d1fe..4014d4bfe 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -804,11 +804,7 @@ uint UnquantizeTexelWeight(EncodingData val) { return result; } -uvec4 unquantized_texel_weights[VECTOR_ARRAY_SIZE]; - void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { - const uint Ds = uint((block_dims.x * 0.5f + 1024) / (block_dims.x - 1)); - const uint Dt = uint((block_dims.y * 0.5f + 1024) / (block_dims.y - 1)); const uint num_planes = is_dual_plane ? 2 : 1; const uint area = size.x * size.y; const uint loop_count = min(result_index, area * num_planes); @@ -818,58 +814,71 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { result_vector[array_index][vector_index] = UnquantizeTexelWeight(GetEncodingFromVector(itr)); } - for (uint plane = 0; plane < num_planes; ++plane) { - for (uint t = 0; t < block_dims.y; t++) { - for (uint s = 0; s < block_dims.x; s++) { - const uint cs = Ds * s; - const uint ct = Dt * t; - const uint gs = (cs * (size.x - 1) + 32) >> 6; - const uint gt = (ct * (size.y - 1) + 32) >> 6; - const uint js = gs >> 4; - const uint fs = gs & 0xF; - const uint jt = gt >> 4; - const uint ft = gt & 0x0F; - const uint w11 = (fs * ft + 8) >> 4; - const uint w10 = ft - w11; - const uint w01 = fs - w11; - const uint w00 = 16 - fs - ft + w11; - const uvec4 w = uvec4(w00, w01, w10, w11); - const uint v0 = jt * size.x + js; +} - uvec4 p = uvec4(0); - -#define VectorIndicesFromBase(offset_base) \ - const uint offset = is_dual_plane ? 2 * offset_base + plane : offset_base; \ - const uint array_index = offset / 4; \ +uint GetUnquantizedTexelWieght(uint offset_base, uint plane, bool is_dual_plane) { + const uint offset = is_dual_plane ? 2 * offset_base + plane : offset_base; + const uint array_index = offset / 4; const uint vector_index = offset % 4; + return result_vector[array_index][vector_index]; +} - if (v0 < area) { - const uint offset_base = v0; - VectorIndicesFromBase(offset_base); - p.x = result_vector[array_index][vector_index]; - } - if ((v0 + 1) < (area)) { - const uint offset_base = v0 + 1; - VectorIndicesFromBase(offset_base); - p.y = result_vector[array_index][vector_index]; - } - if ((v0 + size.x) < (area)) { - const uint offset_base = v0 + size.x; - VectorIndicesFromBase(offset_base); - p.z = result_vector[array_index][vector_index]; - } - if ((v0 + size.x + 1) < (area)) { - const uint offset_base = v0 + size.x + 1; - VectorIndicesFromBase(offset_base); - p.w = result_vector[array_index][vector_index]; - } - const uint offset = (t * block_dims.x + s) + ARRAY_NUM_ELEMENTS * plane; - const uint array_index = offset / 4; - const uint vector_index = offset % 4; - unquantized_texel_weights[array_index][vector_index] = (uint(dot(p, w)) + 8) >> 4; - } +uvec4 GetUnquantizedWeightVector(uint t, uint s, uvec2 size, uint plane_index, bool is_dual_plane) { + const uint Ds = uint((block_dims.x * 0.5f + 1024) / (block_dims.x - 1)); + const uint Dt = uint((block_dims.y * 0.5f + 1024) / (block_dims.y - 1)); + const uint area = size.x * size.y; + + const uint cs = Ds * s; + const uint ct = Dt * t; + const uint gs = (cs * (size.x - 1) + 32) >> 6; + const uint gt = (ct * (size.y - 1) + 32) >> 6; + const uint js = gs >> 4; + const uint fs = gs & 0xF; + const uint jt = gt >> 4; + const uint ft = gt & 0x0F; + const uint w11 = (fs * ft + 8) >> 4; + const uint w10 = ft - w11; + const uint w01 = fs - w11; + const uint w00 = 16 - fs - ft + w11; + const uvec4 w = uvec4(w00, w01, w10, w11); + const uint v0 = jt * size.x + js; + + uvec4 p0 = uvec4(0); + uvec4 p1 = uvec4(0); + + if (v0 < area) { + const uint offset_base = v0; + p0.x = GetUnquantizedTexelWieght(offset_base, 0, is_dual_plane); + p1.x = GetUnquantizedTexelWieght(offset_base, 1, is_dual_plane); + } + if ((v0 + 1) < (area)) { + const uint offset_base = v0 + 1; + p0.y = GetUnquantizedTexelWieght(offset_base, 0, is_dual_plane); + p1.y = GetUnquantizedTexelWieght(offset_base, 1, is_dual_plane); + } + if ((v0 + size.x) < (area)) { + const uint offset_base = v0 + size.x; + p0.z = GetUnquantizedTexelWieght(offset_base, 0, is_dual_plane); + p1.z = GetUnquantizedTexelWieght(offset_base, 1, is_dual_plane); + } + if ((v0 + size.x + 1) < (area)) { + const uint offset_base = v0 + size.x + 1; + p0.w = GetUnquantizedTexelWieght(offset_base, 0, is_dual_plane); + p1.w = GetUnquantizedTexelWieght(offset_base, 1, is_dual_plane); + } + + const uint primary_weight = (uint(dot(p0, w)) + 8) >> 4; + + uvec4 weight_vec = uvec4(primary_weight); + + if (is_dual_plane) { + const uint secondary_weight = (uint(dot(p1, w)) + 8) >> 4; + for (uint c = 0; c < 4; c++) { + const bool is_secondary = ((plane_index + 1u) & 3u) == c; + weight_vec[c] = is_secondary ? secondary_weight : primary_weight; } } + return weight_vec; } int FindLayout(uint mode) { @@ -1155,25 +1164,10 @@ void DecompressBlock(ivec3 coord) { } const uvec4 C0 = ReplicateByteTo16(endpoints0[local_partition]); const uvec4 C1 = ReplicateByteTo16(endpoints1[local_partition]); - const uint weight_offset = (j * block_dims.x + i); - const uint array_index = weight_offset / 4; - const uint vector_index = weight_offset % 4; - const uint primary_weight = unquantized_texel_weights[array_index][vector_index]; - uvec4 weight_vec = uvec4(primary_weight); - if (dual_plane) { - const uint secondary_weight_offset = (j * block_dims.x + i) + ARRAY_NUM_ELEMENTS; - const uint secondary_array_index = secondary_weight_offset / 4; - const uint secondary_vector_index = secondary_weight_offset % 4; - const uint secondary_weight = - unquantized_texel_weights[secondary_array_index][secondary_vector_index]; - for (uint c = 0; c < 4; c++) { - const bool is_secondary = ((plane_index + 1u) & 3u) == c; - weight_vec[c] = is_secondary ? secondary_weight : primary_weight; - } - } + const uvec4 weight_vec = GetUnquantizedWeightVector(j, i, size_params, plane_index, dual_plane); const vec4 Cf = vec4((C0 * (uvec4(64) - weight_vec) + C1 * weight_vec + uvec4(32)) / 64); - const vec4 p = (Cf / 65535.0); + const vec4 p = (Cf / 65535.0f); imageStore(dest_image, coord + ivec3(i, j, 0), p.gbar); } } From 71857e889e3403621d78a2fceaf8265b1113f89f Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:34:57 -0400 Subject: [PATCH 31/33] flatten result vector --- src/video_core/host_shaders/astc_decoder.comp | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 4014d4bfe..70f147d80 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -77,7 +77,7 @@ int color_bitsread = 0; #define DIVCEIL(number, divisor) (number + divisor - 1) / divisor #define ARRAY_NUM_ELEMENTS 144 #define VECTOR_ARRAY_SIZE DIVCEIL(ARRAY_NUM_ELEMENTS * 2, 4) -uvec4 result_vector[VECTOR_ARRAY_SIZE]; +uint result_vector[ARRAY_NUM_ELEMENTS * 2]; int result_index = 0; uint result_vector_max_index; @@ -122,9 +122,7 @@ void ResultEmplaceBack(EncodingData val) { result_limit_reached = true; return; } - const uint array_index = result_index / 4; - const uint vector_index = result_index % 4; - result_vector[array_index][vector_index] = val.data; + result_vector[result_index] = val.data; ++result_index; } @@ -309,10 +307,7 @@ uint StreamColorBits(uint num_bits) { } EncodingData GetEncodingFromVector(uint index) { - const uint array_index = index / 4; - const uint vector_index = index % 4; - - const uint data = result_vector[array_index][vector_index]; + const uint data = result_vector[index]; return EncodingData(data); } @@ -809,18 +804,14 @@ void UnquantizeTexelWeights(uvec2 size, bool is_dual_plane) { const uint area = size.x * size.y; const uint loop_count = min(result_index, area * num_planes); for (uint itr = 0; itr < loop_count; ++itr) { - const uint array_index = itr / 4; - const uint vector_index = itr % 4; - result_vector[array_index][vector_index] = + result_vector[itr] = UnquantizeTexelWeight(GetEncodingFromVector(itr)); } } uint GetUnquantizedTexelWieght(uint offset_base, uint plane, bool is_dual_plane) { const uint offset = is_dual_plane ? 2 * offset_base + plane : offset_base; - const uint array_index = offset / 4; - const uint vector_index = offset % 4; - return result_vector[array_index][vector_index]; + return result_vector[offset]; } uvec4 GetUnquantizedWeightVector(uint t, uint s, uvec2 size, uint plane_index, bool is_dual_plane) { From 0f7220c9c8073ad1d2ab5dbc713b8c7d7a62bd0c Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:38:37 -0400 Subject: [PATCH 32/33] flatten encoding_values --- src/video_core/host_shaders/astc_decoder.comp | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index 70f147d80..a5d9c97b4 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -54,13 +54,13 @@ const uint TRIT = 2u; // ASTC Encodings data, sorted in ascending order based on their BitLength value // (see GetBitLength() function) -const uvec4 encoding_values[6] = uvec4[]( - uvec4((JUST_BITS), (JUST_BITS | (1u << 8u)), (TRIT), (JUST_BITS | (2u << 8u))), - uvec4((QUINT), (TRIT | (1u << 8u)), (JUST_BITS | (3u << 8u)), (QUINT | (1u << 8u))), - uvec4((TRIT | (2u << 8u)), (JUST_BITS | (4u << 8u)), (QUINT | (2u << 8u)), (TRIT | (3u << 8u))), - uvec4((JUST_BITS | (5u << 8u)), (QUINT | (3u << 8u)), (TRIT | (4u << 8u)), (JUST_BITS | (6u << 8u))), - uvec4((QUINT | (4u << 8u)), (TRIT | (5u << 8u)), (JUST_BITS | (7u << 8u)), (QUINT | (5u << 8u))), - uvec4((TRIT | (6u << 8u)), (JUST_BITS | (8u << 8u)), 0u, 0u)); +const uint encoding_values[22] = uint[]( + (JUST_BITS), (JUST_BITS | (1u << 8u)), (TRIT), (JUST_BITS | (2u << 8u)), + (QUINT), (TRIT | (1u << 8u)), (JUST_BITS | (3u << 8u)), (QUINT | (1u << 8u)), + (TRIT | (2u << 8u)), (JUST_BITS | (4u << 8u)), (QUINT | (2u << 8u)), (TRIT | (3u << 8u)), + (JUST_BITS | (5u << 8u)), (QUINT | (3u << 8u)), (TRIT | (4u << 8u)), (JUST_BITS | (6u << 8u)), + (QUINT | (4u << 8u)), (TRIT | (5u << 8u)), (JUST_BITS | (7u << 8u)), (QUINT | (5u << 8u)), + (TRIT | (6u << 8u)), (JUST_BITS | (8u << 8u))); // Input ASTC texture globals int total_bitsread = 0; @@ -313,8 +313,7 @@ EncodingData GetEncodingFromVector(uint index) { // Returns the number of bits required to encode n_vals values. uint GetBitLength(uint n_vals, uint encoding_index) { - const EncodingData encoding_value = - EncodingData(encoding_values[encoding_index / 4][encoding_index % 4]); + const EncodingData encoding_value = EncodingData(encoding_values[encoding_index]); const uint encoding = Encoding(encoding_value); uint total_bits = NumBits(encoding_value) * n_vals; if (encoding == TRIT) { @@ -435,7 +434,7 @@ void DecodeTritBlock(uint num_bits) { } void DecodeIntegerSequence(uint max_range, uint num_values) { - EncodingData val = EncodingData(encoding_values[max_range / 4][max_range % 4]); + EncodingData val = EncodingData(encoding_values[max_range]); const uint encoding = Encoding(val); const uint num_bits = NumBits(val); uint vals_decoded = 0; @@ -467,7 +466,7 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { // Find the largest encoding that's within color_data_bits // TODO(ameerj): profile with binary search int range = 0; - while (++range < ((encoding_values.length() * 4) - 2)) { + while (++range < encoding_values.length()) { const uint bit_length = GetBitLength(num_values, range); if (bit_length > color_data_bits) { break; From 5c25712af9530dfd27960036141989a6c2f0c3bc Mon Sep 17 00:00:00 2001 From: Ameer J <52414509+ameerj@users.noreply.github.com> Date: Wed, 9 Aug 2023 18:45:52 -0400 Subject: [PATCH 33/33] flatten color_values --- src/video_core/host_shaders/astc_decoder.comp | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/video_core/host_shaders/astc_decoder.comp b/src/video_core/host_shaders/astc_decoder.comp index a5d9c97b4..5ff17cd0c 100644 --- a/src/video_core/host_shaders/astc_decoder.comp +++ b/src/video_core/host_shaders/astc_decoder.comp @@ -457,8 +457,7 @@ void DecodeIntegerSequence(uint max_range, uint num_values) { } } -uvec4 color_values[8]; -void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { +void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits, out uint color_values[32]) { uint num_values = 0; for (uint i = 0; i < num_partitions; i++) { num_values += ((modes[i] >> 2) + 1) << 1; @@ -486,8 +485,7 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { A = ReplicateBitTo9((bitval & 1)); switch (encoding) { case JUST_BITS: - color_values[out_index / 4][out_index % 4] = FastReplicateTo8(bitval, bitlen); - ++out_index; + color_values[++out_index] = FastReplicateTo8(bitval, bitlen); break; case TRIT: { D = QuintTritValue(val); @@ -566,8 +564,7 @@ void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) { uint T = (D * C) + B; T ^= A; T = (A & 0x80) | (T >> 2); - color_values[out_index / 4][out_index % 4] = T; - ++out_index; + color_values[++out_index] = T; } } } @@ -592,19 +589,17 @@ ivec4 BlueContract(int a, int r, int g, int b) { return ivec4(a, (r + b) >> 1, (g + b) >> 1, b); } -void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, +void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode, uint color_values[32], inout uint colvals_index) { #define READ_UINT_VALUES(N) \ uvec4 V[2]; \ for (uint i = 0; i < N; i++) { \ - V[i / 4][i % 4] = color_values[colvals_index / 4][colvals_index % 4]; \ - ++colvals_index; \ + V[i / 4][i % 4] = color_values[++colvals_index]; \ } #define READ_INT_VALUES(N) \ ivec4 V[2]; \ for (uint i = 0; i < N; i++) { \ - V[i / 4][i % 4] = int(color_values[colvals_index / 4][colvals_index % 4]); \ - ++colvals_index; \ + V[i / 4][i % 4] = int(color_values[++colvals_index]); \ } switch (color_endpoint_mode) { @@ -1111,11 +1106,11 @@ void DecompressBlock(ivec3 coord) { { // This decode phase should at most push 32 elements into the vector result_vector_max_index = 32; - + uint color_values[32]; uint colvals_index = 0; - DecodeColorValues(color_endpoint_mode, num_partitions, color_data_bits); + DecodeColorValues(color_endpoint_mode, num_partitions, color_data_bits, color_values); for (uint i = 0; i < num_partitions; i++) { - ComputeEndpoints(endpoints0[i], endpoints1[i], color_endpoint_mode[i], + ComputeEndpoints(endpoints0[i], endpoints1[i], color_endpoint_mode[i], color_values, colvals_index); } }