Ejemplo n.º 1
0
        public bool Update()
        {
            bool result = false;
            //RenderTexture prev = RenderTexture.active;

            bool build = !_isBuilt;

#if AVPRODECKLINK_UNITYFEATURE_EXTERNALTEXTURES
            build |= _externalTexturePtr != DeckLinkPlugin.GetTexturePointer(_deviceHandle);
#endif

            if (build)
            {
                if (DeckLinkPlugin.GetLastCapturedFrameTime(_deviceHandle) > 0)
                {
                    if (!Build())
                    {
                        return(false);
                    }
                }
                return(false);
            }

            /*
             #if UNITY_5_3_OR_NEWER
             *                      Flip();
             #endif
             */
            if (_uvsDirty)
            {
                _requiresTextureCrop = (_usedTextureWidth != _rawTexture.width || _usedTextureHeight != _rawTexture.height);

                if (_requiresTextureCrop)
                {
                    CreateUVs(_flipX || false, !_flipY && true);
                }
                else
                {
                    Flip(_flipX || false, !_flipY && true);
                }

                _uvsDirty = false;
            }

            // Wait until next frame has been uploaded to the texture
            int lastFrameUploaded = (int)DeckLinkPlugin.GetLastFrameUploaded(_deviceHandle);
            if (_lastFrameUploaded != lastFrameUploaded)
            {
                RenderTexture prev = RenderTexture.active;

                if (!_deinterlace)
                {
                    // Format convert
                    result = DoFormatConversion(_finalTexture, _rawTexture);

                    if (_enable3D && _rightEyeRawTexture != null && _rightEyeFinalTexture != null)
                    {
                        result = result && DoFormatConversion(_rightEyeFinalTexture, _rightEyeRawTexture);
                    }
                }
                else
                {
                    // Format convert and Deinterlace
                    RenderTexture tempTarget = RenderTexture.GetTemporary(_finalTexture.width, _finalTexture.height, 0, _finalTexture.format, RenderTextureReadWrite.Default);
                    tempTarget.filterMode = FilterMode.Point;
                    tempTarget.wrapMode   = TextureWrapMode.Clamp;
                    if (DoFormatConversion(tempTarget, _rawTexture))
                    {
                        DoDeinterlace(tempTarget, _finalTexture);

                        result = true;
                    }

                    if (_enable3D && _rightEyeRawTexture != null && _rightEyeFinalTexture != null)
                    {
                        if (DoFormatConversion(tempTarget, _rightEyeRawTexture))
                        {
                            DoDeinterlace(tempTarget, _rightEyeFinalTexture);
                            result = result && true;
                        }
                    }

                    RenderTexture.ReleaseTemporary(tempTarget);
                }

                RenderTexture.active = prev;

                _lastFrameUploaded = lastFrameUploaded;
            }
            else if (!_finalTexture.IsCreated() || (_enable3D && _rightEyeFinalTexture != null && !_rightEyeFinalTexture.IsCreated()))
            {
                Debug.LogError("GPU Reset");
                // If the texture has been lost due to GPU reset(from full screen mode change or vsync change) we'll need fill the texture again
                Reset();
            }

            if (_conversionMaterial != null && _mode != null)
            {
                _conversionMaterial.SetFloat("_TextureWidth", _mode.Width);
            }

            return(ValidPicture && result);
        }
Ejemplo n.º 2
0
        private bool Build()
        {
            if (CreateMaterial())
            {
#if AVPRODECKLINK_UNITYFEATURE_EXTERNALTEXTURES
                System.IntPtr texPtr = DeckLinkPlugin.GetTexturePointer(_deviceHandle);
                if (texPtr != System.IntPtr.Zero)
                {
                    _usedTextureHeight = _mode.Height;

                    if (_mode.PixelFormat == DeckLinkPlugin.PixelFormat.YCbCr_10bpp_422)
                    {
                        _usedTextureWidth = ((_mode.Width * 16) / 6) / 4;
                    }
                    else
                    {
                        _usedTextureWidth = _mode.Pitch / 4;
                    }

                    int width, height;
                    CalcPOTResolutions(_usedTextureWidth, _usedTextureHeight, out width, out height);

                    _rawTexture         = Texture2D.CreateExternalTexture(width, height, TextureFormat.ARGB32, false, true, texPtr);
                    _externalTexturePtr = texPtr;

                    if (_enable3D)
                    {
                        texPtr = DeckLinkPlugin.GetRightTexturePointer(_deviceHandle);
                        if (texPtr != System.IntPtr.Zero)
                        {
                            _rightEyeRawTexture = Texture2D.CreateExternalTexture(width, height, TextureFormat.ARGB32, false, true, texPtr);
                        }
                    }
                }
#else
                CreateRawTexture();
#endif
                if (_rawTexture != null)
                {
                    CreateFinalTexture();

                    _requiresTextureCrop = (_usedTextureWidth != _rawTexture.width || _usedTextureHeight != _rawTexture.height);

                    if (_requiresTextureCrop)
                    {
                        CreateUVs(_flipX || false, !_flipY && true);
                    }
                    else
                    {
                        Flip(_flipX || false, !_flipY && true);
                    }

                    _conversionMaterial.SetFloat("_TextureWidth", _mode.Width);
                    //_conversionMaterial.mainTexture = _rawTexture;
#if !AVPRODECKLINK_UNITYFEATURE_EXTERNALTEXTURES
                    DeckLinkPlugin.SetTexturePointer(_deviceHandle, _rawTexture.GetNativeTexturePtr());
#endif
                }
            }

            _isBuilt = (_conversionMaterial != null && _rawTexture != null && _finalTexture != null);
            return(_isBuilt);
        }