BinomialLLC / basis_universal

Basis Universal GPU Texture Codec
Apache License 2.0
2.72k stars 267 forks source link

Code triggers new in clang 13.1 (Xcode 13.3) and gcc 11 unused-but-set warnings #293

Open MarkCallow opened 2 years ago

MarkCallow commented 2 years ago

Here is a patch. (GitHub does not allow uploading .patch files so I'm pasting the text)

diff --git a/lib/basisu/encoder/basisu_bc7enc.cpp b/lib/basisu/encoder/basisu_bc7enc.cpp
index 22fdfa60..7d2bbfa1 100644
--- a/lib/basisu/encoder/basisu_bc7enc.cpp
+++ b/lib/basisu/encoder/basisu_bc7enc.cpp
@@ -1281,6 +1281,15 @@ static uint64_t find_optimal_solution(uint32_t mode, bc7enc_vec4F xl, bc7enc_vec
    return pResults->m_best_overall_err;
 }

+#if (__cplusplus >= 201703L)
+#define MAYBE_UNUSED [[maybe_unused]]
+#elif __GNUC__ || __clang__
+  #define MAYBE_UNUSED __attribute__((unused))
+#else
+  // VC++ has no equivalent
+  #define MAYBE_UNUSED
+#endif
+
 void check_best_overall_error(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults)
 {
    const uint32_t n = pParams->m_num_selector_weights;
@@ -1301,12 +1310,12 @@ void check_best_overall_error(const color_cell_compressor_params *pParams, color
        for (uint32_t c = 0; c < 4; c++)
            colors[i].m_c[c] = (uint8_t)astc_interpolate_linear(colors[0].m_c[c], colors[n - 1].m_c[c], pParams->m_pSelector_weights[i]);

-   uint64_t total_err = 0;
+    MAYBE_UNUSED uint64_t total_err = 0;
    for (uint32_t p = 0; p < pParams->m_num_pixels; p++)
    {
        const color_quad_u8 &orig = pParams->m_pPixels[p];
        const color_quad_u8 &packed = colors[pResults->m_pSelectors[p]];
-               
+
        if (pParams->m_has_alpha)
            total_err += compute_color_distance_rgba(&orig, &packed, pParams->m_perceptual, pParams->m_weights);
        else
diff --git a/lib/basisu/encoder/basisu_comp.cpp b/lib/basisu/encoder/basisu_comp.cpp
index 166a1c4f..ba57ab05 100644
--- a/lib/basisu/encoder/basisu_comp.cpp
+++ b/lib/basisu/encoder/basisu_comp.cpp
@@ -1303,13 +1303,12 @@ namespace basisu

        m_output_basis_file = comp_data;

-       uint32_t total_orig_pixels = 0, total_texels = 0, total_orig_texels = 0;
+       uint32_t total_orig_pixels = 0, total_orig_texels = 0;
        for (uint32_t i = 0; i < m_slice_descs.size(); i++)
        {
            const basisu_backend_slice_desc& slice_desc = m_slice_descs[i];

            total_orig_pixels += slice_desc.m_orig_width * slice_desc.m_orig_height;
-           total_texels += slice_desc.m_width * slice_desc.m_height;
        }

        m_basis_file_size = (uint32_t)comp_data.size();
diff --git a/lib/basisu/encoder/basisu_frontend.cpp b/lib/basisu/encoder/basisu_frontend.cpp
index 1f30a33c..a5f268f8 100644
--- a/lib/basisu/encoder/basisu_frontend.cpp
+++ b/lib/basisu/encoder/basisu_frontend.cpp
@@ -2346,8 +2346,6 @@ namespace basisu
                    if (!cluster_block_indices.size())
                        continue;

-                   uint64_t overall_best_err = 0;
-
                    uint64_t total_err[4][4][4];
                    clear_obj(total_err);

@@ -2398,8 +2396,6 @@ namespace basisu
                            }

                            m_optimized_cluster_selectors[cluster_index].set_selector(x, y, best_sel);
-
-                           overall_best_err += best_err;
                        } // x
                    } // y

diff --git a/lib/basisu/encoder/basisu_gpu_texture.cpp b/lib/basisu/encoder/basisu_gpu_texture.cpp
index dec769d5..56c58849 100644
--- a/lib/basisu/encoder/basisu_gpu_texture.cpp
+++ b/lib/basisu/encoder/basisu_gpu_texture.cpp
@@ -1533,8 +1533,6 @@ namespace basisu
            packed_uint<4> packed_img_size(img_size);
            append_vector(ktx_data, (uint8_t *)&packed_img_size, sizeof(packed_img_size));

-           uint32_t bytes_written = 0;
-
            for (uint32_t array_index = 0; array_index < maximum<uint32_t>(1, header.m_numberOfArrayElements); array_index++)
            {
                for (uint32_t face_index = 0; face_index < header.m_numberOfFaces; face_index++)
@@ -1542,8 +1540,6 @@ namespace basisu
                    const gpu_image& img = gpu_images[cubemap_flag ? (array_index * 6 + face_index) : array_index][level_index];

                    append_vector(ktx_data, (uint8_t *)img.get_ptr(), img.get_size_in_bytes());
-                   
-                   bytes_written += img.get_size_in_bytes();
                }

            } // array_index
diff --git a/lib/basisu/encoder/basisu_uastc_enc.cpp b/lib/basisu/encoder/basisu_uastc_enc.cpp
index 271bbc6f..aadc2a90 100644
--- a/lib/basisu/encoder/basisu_uastc_enc.cpp
+++ b/lib/basisu/encoder/basisu_uastc_enc.cpp
@@ -383,13 +383,17 @@ namespace basisu
            }
        }

+#if UASTC_WRITE_MODE_DESCS
        uint32_t total_endpoint_bits = 0;
+#endif

        for (uint32_t i = 0; i < total_tq_values; i++)
        {
            const uint32_t num_bits = ep_trits ? 8 : 7;
            uastc_write_bits(buf, block_bit_offset, tq_values[i], num_bits, "ETQ");
+#if UASTC_WRITE_MODE_DESCS
            total_endpoint_bits += num_bits;
+#endif
        }

        if (tq_mul > 1)
@@ -414,20 +418,24 @@ namespace basisu
                    num_bits = 5;
            }
            uastc_write_bits(buf, block_bit_offset, tq_accum, num_bits, "ETQ");
+#if UASTC_WRITE_MODE_DESCS
            total_endpoint_bits += num_bits;
+#endif
        }

        for (uint32_t i = 0; i < total_values; i++)
        {
            uastc_write_bits(buf, block_bit_offset, bit_values[i], ep_bits, "EBITS");
+#if UASTC_WRITE_MODE_DESCS
            total_endpoint_bits += ep_bits;
+#endif
        }

 #if UASTC_WRITE_MODE_DESCS
        uint32_t weight_start = block_bit_offset;
+        uint32_t total_weight_bits = 0;
 #endif

-       uint32_t total_weight_bits = 0;
        const uint32_t plane_shift = (total_planes == 2) ? 1 : 0;
        for (uint32_t i = 0; i < 16 * total_planes; i++)
        {
@@ -443,7 +451,9 @@ namespace basisu

            uastc_write_bits(buf, block_bit_offset, weights[i], numbits, nullptr);

+#if UASTC_WRITE_MODE_DESCS
            total_weight_bits += numbits;
+#endif
        }

 #if UASTC_WRITE_MODE_DESCS

I created the MAYBE_UNUSED macro in encoder/basisu_bc7enc.cpp because one of the unused sets stores the return value of a function call that looks important and a different warning is likely if I remove the assignment thus ignoring the return value.

There is another of these warnings in zstd/zstd.c. Because any fix would have to be redone whenever the single file decoder is regenerated I supressed the warning for that file in my build.