public static TranscodeFormatTuple?GetFormatsForImage(
            IMetaData meta,
            ILevelInfo li,
            bool linear = false
            )
        {
            TranscodeFormatTuple?formats;

            formats = TranscodeFormatHelper.GetPreferredFormat(
                meta.hasAlpha,
                li.isPowerOfTwo,
                li.isSquare,
                linear
                );

            if (!formats.HasValue && meta.hasAlpha)
            {
                // Fall back to transcode RGB-only to RGBA texture
                formats = TranscodeFormatHelper.GetPreferredFormat(
                    false,
                    li.isPowerOfTwo,
                    li.isSquare,
                    linear
                    );
            }
            return(formats);
        }
 static void InitInternal()
 {
     initialized = true;
     TranscodeFormatHelper.Init();
     ktx_basisu_basis_init();
     transcoderCountAvailable = UnityEngine.SystemInfo.processorCount;
 }
Exemple #3
0
 protected virtual TranscodeFormatTuple?GetFormat(IMetaData meta, ILevelInfo li)
 {
     return(TranscodeFormatHelper.GetFormatsForImage(meta, li));
 }
Exemple #4
0
        protected override IEnumerator LoadBytesRoutine(NativeArray <byte> data)
        {
            Texture2D texture = null;

            var ktx = new KtxNativeInstance(data);

            if (ktx.valid)
            {
                // TODO: Maybe do this somewhere more central
                TranscodeFormatHelper.Init();

                var formats = GetFormat(ktx, ktx);

                if (formats.HasValue)
                {
                    var gf = formats.Value.format;
#if KTX_VERBOSE
                    Debug.LogFormat("Transcode to GraphicsFormat {0} ({1})", gf, formats.Value.transcodeFormat);
#endif
                    Profiler.BeginSample("KtxTranscode");

                    var job = new KtxTranscodeJob();

                    var jobHandle = ktx.LoadBytesJob(
                        ref job,
                        formats.Value.transcodeFormat
                        );

                    Profiler.EndSample();

                    while (!jobHandle.IsCompleted)
                    {
                        yield return(null);
                    }
                    jobHandle.Complete();

                    if (job.result[0] == KtxErrorCode.KTX_SUCCESS)
                    {
                        Profiler.BeginSample("LoadBytesRoutineGPUupload");
                        uint width  = ktx.baseWidth;
                        uint height = ktx.baseHeight;

                        if (formats.Value.format == GraphicsFormat.RGBA_DXT5_SRGB && !ktx.hasAlpha)
                        {
                            // ktx library automatically decides to use the smaller DXT1 instead of DXT5 if no alpha
                            gf = GraphicsFormat.RGBA_DXT1_SRGB;
                        }
                        texture = new Texture2D((int)width, (int)height, gf, TextureCreationFlags.None);

                        try {
                            ktx.LoadRawTextureData(texture);
                        }
                        catch (UnityException) {
                            Debug.LogError(ERR_MSG_TRANSCODE_FAILED);
                            texture = null;
                        }
                        Profiler.EndSample();
                    }
                    else
                    {
                        Debug.LogError(ERR_MSG_TRANSCODE_FAILED);
                    }
                    job.result.Dispose();
                }
            }
            ktx.Unload();
            OnTextureLoaded(texture);
        }
        IEnumerator Transcode(KtxNativeInstance ktx, bool linear)
        {
            // TODO: Maybe do this somewhere more central
            TranscodeFormatHelper.Init();

            var formats = GetFormat(ktx, ktx, linear);

            if (formats.HasValue)
            {
                var gf = formats.Value.format;
#if KTX_VERBOSE
                Debug.LogFormat("Transcode to GraphicsFormat {0} ({1})", gf, formats.Value.transcodeFormat);
#endif
                Profiler.BeginSample("KtxTranscode");

                var job = new KtxTranscodeJob();

                var jobHandle = ktx.LoadBytesJob(
                    ref job,
                    formats.Value.transcodeFormat
                    );

                Profiler.EndSample();

                while (!jobHandle.IsCompleted)
                {
                    yield return(null);
                }
                jobHandle.Complete();

                if (job.result[0] == KtxErrorCode.KTX_SUCCESS)
                {
                    Profiler.BeginSample("LoadBytesRoutineGPUupload");
                    uint width  = ktx.baseWidth;
                    uint height = ktx.baseHeight;

                    if (formats.Value.format == GraphicsFormat.RGBA_DXT5_SRGB && !ktx.hasAlpha)
                    {
                        // ktx library automatically decides to use the smaller DXT1 instead of DXT5 if no alpha
#if UNITY_2018_3_OR_NEWER
                        gf = GraphicsFormat.RGBA_DXT1_SRGB;
#else
                        gf = GraphicsFormat.RGB_DXT1_SRGB;
#endif
                    }
                    try {
                        texture = ktx.LoadTextureData(gf);
                    }
                    catch (UnityException) {
                        Debug.LogError(ERR_MSG_TRANSCODE_FAILED);
                        texture = null;
                    }
                    Profiler.EndSample();
                }
                else
                {
                    Debug.LogError(ERR_MSG_TRANSCODE_FAILED);
                }
                job.result.Dispose();
            }
        }
Exemple #6
0
 protected virtual TranscodeFormatTuple?GetFormat(IMetaData meta, ILevelInfo li, bool linear = false)
 {
     return(TranscodeFormatHelper.GetFormatsForImage(meta, li, linear));
 }