BinomialLLC / basis_universal

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

Warning and other fixes for libktx to use v1.16.3. #292

Open MarkCallow opened 2 years ago

MarkCallow commented 2 years ago

Here is a patch with the changes I made to Basis v1.16.3 so I could use it in libktx. The warnings should be fixed for everyone's sake. The other things I leave to your judgement. GitHub doesn't support uploading .patch files to issues so I'm pasting it here.

diff --git a/lib/basisu/encoder/basisu_enc.cpp b/lib/basisu/encoder/basisu_enc.cpp
index b427215e..8cf77789 100644
--- a/lib/basisu/encoder/basisu_enc.cpp
+++ b/lib/basisu/encoder/basisu_enc.cpp
@@ -18,8 +18,10 @@
 #include "basisu_etc.h"
 #include "../transcoder/basisu_transcoder.h"
 #include "basisu_bc7enc.h"
+#if !defined(LIBKTX)
 #include "jpgd.h"
 #include "pvpngreader.h"
+#endif
 #include "basisu_opencl.h"
 #include <vector>

@@ -315,8 +317,8 @@ namespace basisu
                return ticks * g_timer_freq;
        }

+#if !defined(LIBKTX)
        const uint32_t MAX_32BIT_ALLOC_SIZE = 250000000;
-               
        bool load_tga(const char* pFilename, image& img)
        {
                int w = 0, h = 0, n_chans = 0;
@@ -417,7 +419,6 @@ namespace basisu

                return true;
        }
-
        bool load_image(const char* pFilename, image& img)
        {
                std::string ext(string_get_extension(std::string(pFilename)));
@@ -433,10 +434,9 @@ namespace basisu
                        return load_tga(pFilename, img);
                if ( (strcasecmp(pExt, "jpg") == 0) || (strcasecmp(pExt, "jfif") == 0) || (strcasecmp(pExt, "jpeg") == 0) )
                        return load_jpg(pFilename, img);
-
                return false;
        }
-       
+
        bool save_png(const char* pFilename, const image &img, uint32_t image_save_flags, uint32_t grayscale_comp)
        {
                if (!img.get_total_pixels())
@@ -503,6 +503,17 @@ namespace basisu

                return status;
        }
+#else
+       bool load_image(const char*, image&)
+       {
+               return false;
+    }
+
+       bool save_png(const char*, const image&, uint32_t, uint32_t)
+    {
+         return false;
+    }
+#endif

        bool read_file_to_vec(const char* pFilename, uint8_vec& data)
        {
@@ -1624,7 +1635,7 @@ namespace basisu

                //debug_printf("job_pool::job_thread: exiting\n");
        }
-
+#if !defined(LIBKTX)
        // .TGA image loading
        #pragma pack(push)
        #pragma pack(1)
@@ -2049,7 +2060,7 @@ namespace basisu

                return read_tga(&filedata[0], (uint32_t)filedata.size(), width, height, n_chans);
        }
-
+#endif
        void image::debug_text(uint32_t x_ofs, uint32_t y_ofs, uint32_t scale_x, uint32_t scale_y, const color_rgba& fg, const color_rgba* pBG, bool alpha_only, const char* pFmt, ...)
        {
                char buf[2048];
diff --git a/lib/basisu/encoder/basisu_frontend.cpp b/lib/basisu/encoder/basisu_frontend.cpp
index 00210e66..20cb883d 100644
--- a/lib/basisu/encoder/basisu_frontend.cpp
+++ b/lib/basisu/encoder/basisu_frontend.cpp
@@ -2338,7 +2338,7 @@ namespace basisu
                        const uint32_t last_index = minimum<uint32_t>((uint32_t)total_selector_clusters, cluster_index_iter + N);

 #ifndef __EMSCRIPTEN__                 
-                       m_params.m_pJob_pool->add_job([this, first_index, last_index, &total_clusters_processed, &total_selector_clusters] {
+                       m_params.m_pJob_pool->add_job([this, first_index, last_index] {
 #endif

                                for (uint32_t cluster_index = first_index; cluster_index < last_index; cluster_index++)
diff --git a/lib/basisu/encoder/basisu_opencl.cpp b/lib/basisu/encoder/basisu_opencl.cpp
index 81e3090a..5757633f 100644
--- a/lib/basisu/encoder/basisu_opencl.cpp
+++ b/lib/basisu/encoder/basisu_opencl.cpp
@@ -108,8 +108,9 @@ namespace basisu

                        if (ret == CL_DEVICE_NOT_FOUND)
                        {
+#if 0 // CI service VMs usually don't have GPUs so don't treat as an error.
                                ocl_error_printf("ocl::init: Couldn't get any GPU device ID's, trying CL_DEVICE_TYPE_CPU\n");
-
+#endif
                                ret = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_CPU, 1, &m_device_id, &num_devices);
                        }

@@ -135,14 +136,15 @@ namespace basisu
                        char plat_vers[256];
                        size_t rv = 0;
                        ret = clGetPlatformInfo(platforms[0], CL_PLATFORM_VERSION, sizeof(plat_vers), plat_vers, &rv);
+#if !defined(LIBKTX)
                        if (ret == CL_SUCCESS)
                                printf("OpenCL platform version: \"%s\"\n", plat_vers);
-
+#endif
                        // Serialize CL calls with the AMD driver to avoid lockups when multiple command queues per thread are used. This sucks, but what can we do?
                        m_use_mutex = (strstr(plat_vers, "AMD") != nullptr) || force_serialization;
-
+#if !defined(LIBKTX)
                        printf("Serializing OpenCL calls across threads: %u\n", (uint32_t)m_use_mutex);
-
+#endif
                        m_context = clCreateContext(nullptr, 1, &m_device_id, nullptr, nullptr, &ret);
                        if (ret != CL_SUCCESS)
                        {
@@ -161,9 +163,9 @@ namespace basisu
                                deinit();
                                return false;
                        }
-                                               
+#if !defined(LIBKTX)
                        printf("OpenCL init time: %3.3f secs\n", tm.get_elapsed_secs());
-
+#endif
                        return true;
                }

@@ -771,9 +773,9 @@ namespace basisu
                        g_ocl.deinit();
                        return false;
                }
-                                                               
+#if !defined(LIBKTX)
                printf("OpenCL support initialized successfully\n");
-
+#endif
                return true;
        }

diff --git a/lib/basisu/encoder/jpgd.cpp b/lib/basisu/encoder/jpgd.cpp
index fec8b714..92f80bee 100644
--- a/lib/basisu/encoder/jpgd.cpp
+++ b/lib/basisu/encoder/jpgd.cpp
@@ -3146,7 +3146,7 @@ namespace jpgd {

                for (int y = 0; y < image_height; y++)
                {
-                       const uint8* pScan_line;
+                       const uint8* pScan_line = 0; // init to quiet compile warning.
                        uint scan_line_len;
                        if (decoder.decode((const void**)&pScan_line, &scan_line_len) != JPGD_SUCCESS)
                        {
diff --git a/lib/basisu/transcoder/basisu_transcoder.cpp b/lib/basisu/transcoder/basisu_transcoder.cpp
index ce8c6613..690db043 100644
--- a/lib/basisu/transcoder/basisu_transcoder.cpp
+++ b/lib/basisu/transcoder/basisu_transcoder.cpp
@@ -17207,7 +17207,9 @@ namespace basist

        bool ktx2_transcoder::decompress_level_data(uint32_t level_index, basisu::uint8_vec& uncomp_data)
        {
+#if BASISD_SUPPORT_KTX2_ZSTD
                const uint8_t* pComp_data = m_levels[level_index].m_byte_offset + m_pData;
+#endif
                const uint64_t comp_size = m_levels[level_index].m_byte_length;

                const uint64_t uncomp_size = m_levels[level_index].m_uncompressed_byte_length;

The changes in basisu_enc.cpp remove the various file loaders because they are not used by libktx. These are all ifndef'ed LIBKTX.

The change in basisu_frontend.cpp fixes a warning that the items I've removed aren't used in the closure.

The changes in basisu_opencl.cpp remove all the printfs to stdout. Because many of libktx's clients can write their data to stdout, this is a no-no. These are ifndef'ed LIBKTX. A further change removes the error condition when a GPU device is not found. This is because CI VM's usually don't have GPUs so to run tests in CI you need to use a CPU device. I've put an #if 0 around the call to ocl_error_printf. Note that if this function is called in Debug configuration it throws an assert so this is not just a question of removing the message.

The changes in jpgd.cpp and basisu_transcoder.cpp are to quiet warnings.

For reasons related to how I incorporate code from the BasisU repo into KTX-Software, it is not easy for me to make a PR. Sorry.

richgel999 commented 2 years ago

Thanks a bunch! I will go through and merge what I can.