Exemplo n.º 1
0
        public override IEnumerator LoadBytesRoutine(NativeSlice <byte> data, bool linear = false)
        {
            var orientation = TextureOrientation.UNITY_DEFAULT;

            var ktx = new KtxNativeInstance(data);

            if (ktx.valid)
            {
                orientation = ktx.orientation;
                if (ktx.ktxClass == KtxClassId.ktxTexture2_c)
                {
                    if (ktx.needsTranscoding)
                    {
                        yield return(Transcode(ktx, linear));
                    }
                    else
                    {
                        Debug.LogError("Only supercompressed KTX is supported");
                    }
                }
                else
                {
                    Debug.LogError("Only KTX 2.0 is supported");
                }
            }
            ktx.Unload();
            OnTextureLoaded(texture, orientation);
        }
Exemplo n.º 2
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);
        }