HRESULT CSurfaceQueueDeviceD3D9::CreateSharedSurface(
                                UINT Width, UINT Height, 
                                DXGI_FORMAT Format, 
                                IUnknown** ppTexture,
                                HANDLE* pHandle)
{
    ASSERT(m_pDevice);

    D3DFORMAT D3D9Format;

    if ((D3D9Format = DXGIToCrossAPID3D9Format(Format)) == D3DFMT_UNKNOWN)
    {
        return E_INVALIDARG;
    }

    HRESULT hr;

    *pHandle = NULL;

    hr = m_pDevice->CreateTexture(Width, Height, 1,
                                  D3DUSAGE_RENDERTARGET,
                                  D3D9Format,
                                  D3DPOOL_DEFAULT,
                                  (IDirect3DTexture9**)ppTexture,
                                  pHandle);
    return hr;
}
HRESULT CSurfaceQueueDeviceD3D9::CreateCopyResource(DXGI_FORMAT Format, UINT width, UINT height, IUnknown** ppRes)
{
      
    D3DFORMAT D3D9Format;

    if ((D3D9Format = DXGIToCrossAPID3D9Format(Format)) == D3DFMT_UNKNOWN)
    {
        return E_INVALIDARG;
    }

    return m_pDevice->CreateRenderTarget(
            width, height,
            D3D9Format,
            D3DMULTISAMPLE_NONE,
            0,
            TRUE,
            (IDirect3DSurface9**)ppRes,
            NULL
            );
}
HRESULT CSurfaceQueueDeviceD3D9::OpenSurface(
                                    HANDLE hSharedHandle, 
                                    void** ppUnknown, 
                                    UINT Width, 
                                    UINT Height, 
                                    DXGI_FORMAT Format)
{
    
    D3DFORMAT D3D9Format;

    // If the format is not cross api shareable the utility function will return
    // D3DFMT_UNKNOWN
    if ((D3D9Format = DXGIToCrossAPID3D9Format(Format)) == D3DFMT_UNKNOWN)
    {
        return E_INVALIDARG;
    }

    HRESULT hr = S_OK;
    IDirect3DTexture9** ppTexture = (IDirect3DTexture9**)ppUnknown;

    hr = m_pDevice->CreateTexture(Width, Height, 1,
                                  D3DUSAGE_RENDERTARGET,
                                  D3D9Format,
                                  D3DPOOL_DEFAULT,
                                  ppTexture,
                                  &hSharedHandle);
    if (SUCCEEDED(hr))
    {
        // Store the shared handle
        hr = (*ppTexture)->SetPrivateData(SharedHandleGuid, &hSharedHandle, sizeof(HANDLE), 0);

        if (FAILED(hr))
        {
            (*ppTexture)->Release();
            *ppTexture = NULL;
        }
    }

    return hr;
}
Example #4
0
HRESULT CD3DUtils::Sync(IUnknown *pUnknown)
{
    if(!pUnknown) return E_FAIL;

    HRESULT hr = E_FAIL;
    IDirect3DSurface9 *pSurface = NULL;
    ID3D10Texture2D *pTex10 = NULL;
    IDXSurfaceDevice *pDevice = NULL;
    D3DFORMAT DstFmt = D3DFMT_FORCE_DWORD;
    D3DFORMAT SynFmt = D3DFMT_FORCE_DWORD;
    IUnknown *pSyncSurf = NULL;

    hr = GetDeviceAndResource(pUnknown, &pDevice, &pSurface, &pTex10);
    CHECK_RETHR(hr);

    if (pSurface)
    {
        pSyncSurf = m_pSyncSurf9;

        D3DSURFACE_DESC desc;
        pSurface->GetDesc(&desc);
        DstFmt = desc.Format;

        if(pSyncSurf)
        {
            ((IDirect3DSurface9 *)pSyncSurf)->GetDesc(&desc);
            SynFmt = desc.Format;
        }

        SAFE_RELEASE(pSurface);
    }
    else if(pTex10)
    {
        pSyncSurf = m_pSyncSurf10;

        D3D10_TEXTURE2D_DESC  desc;
        pTex10->GetDesc(&desc);
        DstFmt = DXGIToCrossAPID3D9Format(desc.Format);

        if(pSyncSurf)
        {
            ((ID3D10Texture2D *)pSyncSurf)->GetDesc(&desc);
            SynFmt = DXGIToCrossAPID3D9Format(desc.Format);
        }

        SAFE_RELEASE(pTex10);
    }

    CHECK_RETHR(hr);

    if (SynFmt != DstFmt)
    {
        SAFE_RELEASE(pSyncSurf);
    }

    if (!pSyncSurf)
    {
        hr = pDevice->CreateSurface(SYNC_SURFACE_COPY_SIZE, SYNC_SURFACE_COPY_SIZE, DstFmt, 1, (IUnknown **)&pSyncSurf, NULL);
        CHECK_RETHR(hr);
    }

    hr = pDevice->CopySurface(pSyncSurf, pUnknown, SYNC_SURFACE_COPY_SIZE, SYNC_SURFACE_COPY_SIZE);
    if(S_OK == hr)
    {
        pDevice->LockSurface(pSyncSurf);
        pDevice->UnlockSurface(pSyncSurf);
    }

    if (pDevice == m_pSurfDev10)
        m_pSyncSurf10 = pSyncSurf;
    else if(pDevice == m_pSurfDev9)
        m_pSyncSurf9 = pSyncSurf;

    return hr;
}