void CCoherentUISystem::ChangeEntityDiffuseTextureForMaterial( CCoherentViewListener* pViewListener, const char* entityName, const char* materialName )
    {
        IMaterial* pMaterial = gEnv->p3DEngine->GetMaterialManager()->FindMaterial( materialName );
        IEntity* pEntity = gEnv->pEntitySystem->FindEntityByName( entityName );

        if ( pEntity && !pMaterial )
        {
            pMaterial = pEntity->GetMaterial();
        }

        if ( pMaterial )
        {
            STexSamplerRT& sampler = pMaterial->GetShaderItem().m_pShaderResources->GetTexture( EFTT_DIFFUSE )->m_Sampler;

            // Create a new texture and scrap the old one
            void* pD3DTextureDst = NULL;
            ITexture* pCryTex = gD3DSystem->CreateTexture(
                                    &pD3DTextureDst,
                                    sampler.m_pITex->GetWidth(),
                                    sampler.m_pITex->GetHeight(),
                                    1,
                                    eTF_A8R8G8B8,
                                    FT_USAGE_DYNAMIC
                                );

            int oldTextureID = sampler.m_pITex->GetTextureID();
            gEnv->pRenderer->RemoveTexture( oldTextureID );
            sampler.m_pITex = pCryTex;
            pCryTex->AddRef();

            pViewListener->SetTexture( pD3DTextureDst, pCryTex->GetTextureID() );
        }
    }
    void CCoherentUISystem::SetTexturesForListeners()
    {
        CCoherentViewListener* pListener = NULL;

        // Create HUD texture
        pListener = ( m_HudViewListener ? m_HudViewListener.get() : NULL );

        if ( pListener && pListener->GetTexture() == NULL )
        {
            void* pD3DTextureDst = NULL;
            ITexture* pCryTex = gD3DSystem->CreateTexture(
                                    &pD3DTextureDst,
                                    gEnv->pRenderer->GetWidth(),
                                    gEnv->pRenderer->GetHeight(),
                                    1,
                                    eTF_A8R8G8B8,
                                    FT_USAGE_DYNAMIC
                                );

            pListener->SetTexture( pD3DTextureDst, pCryTex->GetTextureID() );
        }

        // Create textures for entities
        for ( View::const_iterator iter = m_Views.begin(); iter != m_Views.end(); ++iter )
        {
            pListener = iter->first;

            if ( pListener && pListener->GetTexture() == NULL )
            {
                ChangeEntityDiffuseTextureForMaterial( pListener, pListener->GetEngineObjectName(), pListener->GetOverriddenMaterialName() );
            }
        }
    }
    Coherent::UI::CoherentHandle CCoherentUISystem::CreateSharedTextureDX9( const CreateSurfaceTask& task, TexturePair* outTexturePair )
    {
        IDirect3DTexture9* pD3DTex = nullptr;
        // Create a shared texture
        HANDLE result = 0;
        IDirect3DDevice9* pDevice = static_cast<IDirect3DDevice9*>( gD3DDevice );
        HRESULT hr = pDevice->CreateTexture( task.Width, task.Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pD3DTex, &result );

        if ( FAILED( hr ) )
        {
            CryLogAlways( "Unable to create shared texture with DirectX9 renderer!" );
        }

        ITexture* pCryTex = gD3DSystem->InjectTexture( pD3DTex, task.Width, task.Height, eTF_A8R8G8B8, 0 );
        // The native texture has one more reference after InjectTexture

        if ( outTexturePair )
        {
            outTexturePair->CryTextureID = pCryTex->GetTextureID();
            outTexturePair->NativeTexture.pTexDX9 = pD3DTex;
        }

        SAFE_RELEASE( pD3DTex );

        return Coherent::UI::CoherentHandle( result );
    }
int CUIDraw::CreateTexture(const char *strName, bool dontRelease)
{
	for(TTexturesMap::iterator iter=m_texturesMap.begin(); iter!=m_texturesMap.end(); ++iter)
	{
		if(0 == strcmpi((*iter).second->GetName(),strName))
		{
			return (*iter).first;
		}
	}
	uint32 flags = FT_NOMIPS|FT_DONT_RESIZE|FT_DONT_STREAM|FT_STATE_CLAMP;
	if (dontRelease)
	{
		GameWarning("Are you sure you want to permanently keep this UI texture '%s'?!", strName);
	}
	
	flags |= dontRelease ? FT_DONT_RELEASE : 0;
	ITexture *pTexture = m_pRenderer->EF_LoadTexture(strName,flags);
	pTexture->SetClamp(true);
	int iTextureID = pTexture->GetTextureID();
	m_texturesMap.insert(std::make_pair(iTextureID,pTexture));
	return iTextureID;
}
            virtual void ProcessEvent( EFlowEvent evt, SActivationInfo* pActInfo )
            {
                switch ( evt )
                {
                    case eFE_Suspend:
                        pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, false );
                        break;

                    case eFE_Resume:
                        pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, true );
                        break;

                    case eFE_Initialize:
                        break;

                    case eFE_Activate:

                        if ( IsPortActive( pActInfo, EIP_GET ) && !m_pVideo )
                        {
                            m_pVideo = gVideoplayerSystem->GetVideoplayerById( GetPortInt( pActInfo, EIP_VIDEOID ) );
                            pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, true );
                        }

                        else if ( m_pVideo )
                        {
                            // Set changed properties
                            if ( IsPortActive( pActInfo, EIP_VIDEOID ) )
                            {
                                m_pVideo = gVideoplayerSystem->GetVideoplayerById( GetPortInt( pActInfo, EIP_VIDEOID ) );
                            }
                        }

                        break;

                    case eFE_Update:
                        if ( m_pVideo )
                        {
                            ITexture* tex = m_pVideo->GetTexture();

                            if ( tex && m_nID != tex->GetTextureID() )
                            {
                                m_nID = tex->GetTextureID();
                                m_sName = tex->GetName();

                                ActivateOutput( pActInfo, EOP_TEXID, m_nID );
                                ActivateOutput( pActInfo, EOP_TEXNAME, m_sName );
                                ActivateOutput( pActInfo, EOP_CHANGED, true );
                            }
                        }

                        if ( !m_pVideo && m_nID != -1 )
                        {
                            m_nID = -1;
                            m_sName = "";

                            ActivateOutput( pActInfo, EOP_TEXID, m_nID );
                            ActivateOutput( pActInfo, EOP_TEXNAME, m_sName );
                            ActivateOutput( pActInfo, EOP_CHANGED, true );

                            pActInfo->pGraph->SetRegularlyUpdated( pActInfo->myID, false );
                        }

                        break;
                }
            }
    Coherent::UI::CoherentHandle CCoherentUISystem::CreateSharedTextureDX11( const CreateSurfaceTask& task, TexturePair* outTexturePair )
    {
        // The shared texture's format for DX11 must be DXGI_FORMAT_B8G8R8A8_UNORM.
        // There is no corresponding ETEX_Format and after injecting the created
        // texture, COM errors occur.
        // TODO: Find a way to fool CryEngine into accepting a DXGI_FORMAT_B8G8R8A8_UNORM texture.

        // Create shared texture
        D3D11_TEXTURE2D_DESC desc;
        memset( &desc, 0, sizeof( desc ) );
        desc.Width = task.Width;
        desc.Height = task.Height;
        desc.MipLevels = 1;
        desc.ArraySize = 1;
        desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
        desc.SampleDesc.Count = 1;
        desc.Usage = D3D11_USAGE_DEFAULT;
        desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
        desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;

        ID3D11Device* pDevice = static_cast<ID3D11Device*>( gD3DDevice );

        ID3D11Texture2D* pD3DTex = NULL;
        HRESULT hr = pDevice->CreateTexture2D( &desc, NULL, &pD3DTex );

        if ( FAILED( hr ) )
        {
            return Coherent::UI::CoherentHandle( 0 );
        }

        IDXGIResource* pTempResource = NULL;
        hr = pD3DTex->QueryInterface( __uuidof( IDXGIResource ), ( void** )&pTempResource );

        if ( FAILED( hr ) )
        {
            SAFE_RELEASE( pD3DTex );
            return Coherent::UI::CoherentHandle( 0 );
        }

        HANDLE result;
        hr = pTempResource->GetSharedHandle( &result );
        pTempResource->Release();

        if ( FAILED( hr ) )
        {
            SAFE_RELEASE( pD3DTex );
            return Coherent::UI::CoherentHandle( 0 );
        }

        ITexture* pCryTex = gD3DSystem->InjectTexture( pD3DTex, task.Width, task.Height, eTF_A8R8G8B8, 0 );
        // The native texture has one more reference after InjectTexture

        if ( outTexturePair )
        {
            outTexturePair->CryTextureID = pCryTex->GetTextureID();
            outTexturePair->NativeTexture.pTexDX11 = pD3DTex;
        }

        SAFE_RELEASE( pD3DTex );

        return Coherent::UI::CoherentHandle( result );
    }
    bool CVideoRendererDX9::CreateResources( unsigned nSourceWidth, unsigned nSourceHeight, unsigned nTargetWidth, unsigned nTargetHeight )
    {
        ReleaseResources();

        bool bMemSuccess = CVideoRenderer::CreateResources( nSourceWidth, nSourceHeight, nTargetWidth, nTargetHeight );

        IDirect3D9* pD3D = NULL;

        if ( m_pD3DDevice )
        {
            m_pD3DDevice->GetDirect3D( &pD3D );
        }

#if !defined(VP_DISABLE_RESOURCE)

        if ( pD3D )
        {
            HRESULT hr = m_pD3DDevice->CreateTexture( nTargetWidth, nTargetHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pTex, NULL );

            // Directly locking this was doesn't perform at all and brought some problems so use a staging texture
            //HRESULT hr = m_pD3DDevice->CreateTexture((nSourceWidth >> RESBASE) << RESBASE, (nSourceHeight >> RESBASE) << RESBASE, 1, D3DUSAGE_DYNAMIC, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &m_pTex, NULL);

            if ( FAILED( hr ) || !m_pTex )
            {
                // Could not create render target
                outputError( hr );
                m_pTex = NULL;
            }

            if ( m_pTex )
            {
#if !defined(USE_SEPERATEMEMORY)

                // YV12 format possible?
                if (    FAILED( pD3D->CheckDeviceFormatConversion( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_UNKNOWN, D3DFMT_X8R8G8B8 ) )
                        &&  SUCCEEDED( pD3D->CheckDeviceFormatConversion( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_YV12, D3DFMT_X8R8G8B8 ) ) )
                {
#if defined(_DEBUG)
                    gPlugin->LogAlways( "Creating YUV surface." );
#endif
                    hr = m_pD3DDevice->CreateOffscreenPlainSurface( nSourceWidth, nSourceHeight, D3DFMT_YV12, D3DPOOL_DEFAULT, &m_pSurfaceYUV, NULL );

                    if ( FAILED( hr ) || !m_pSurfaceYUV )
                    {
                        // Could not create YUV surface
                        outputError( hr );
                        m_pSurfaceYUV = NULL;
                    }
                }

#endif

                if ( !m_pSurfaceYUV )
                {
#if !defined(USE_SEPERATEMEMORY)
                    gPlugin->LogWarning( "Couldn't create YUV surface, switching to fallback." );
#endif

#if !defined(USE_UPDATE_SURFACE)
                    hr = m_pD3DDevice->CreateOffscreenPlainSurface( nSourceWidth, nSourceHeight, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pStagingSurface, NULL );
#else
                    hr = m_pD3DDevice->CreateOffscreenPlainSurface( nSourceWidth, nSourceHeight, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &m_pStagingSurface, NULL );
#endif

                    if ( FAILED( hr ) || !m_pStagingSurface )
                    {
                        // Could not create staging surface
                        outputError( hr );
                        m_pStagingSurface = NULL;
                    }
                }

                if ( m_pSurfaceYUV || m_pStagingSurface )
                {
                    ITexture* pTex = gD3DSystem->InjectTexture( m_pTex, nTargetWidth, nTargetHeight, eTF_A8R8G8B8, FT_USAGE_RENDERTARGET | VIDEO_TEXTURE_FLAGS );

                    if ( pTex )
                    {
                        m_iTex = pTex->GetTextureID();
                    }

                    else
                    {
                        gPlugin->LogError( "Couldn't inject texture" );
                    }
                }
            }
        }

#endif

        return bMemSuccess && m_pTex && m_iTex > 0 && ( m_pSurfaceYUV || m_pStagingSurface );
    }