problem on display yuv video with direct3d

Discussion in 'Rendering Technology and APIs' started by Ivy, Feb 3, 2010.

  1. Ivy

    Ivy
    Newcomer

    Joined:
    Feb 3, 2010
    Messages:
    1
    Likes Received:
    0
    Hi All,
    I am a beginner on direct3d and i need to display yuv video on a window. I've modified a sample code from internet and now my program can display yuv video. But i found the video will take up the whole client area of the window, while i want to assign a region for it. that is to say, i want it to display it on a certain rect on a window. Can i realize it?
    by the way, can i display multi-channel video on a window and each channel has it's own rect.
    thanks in advance!
    here is my sample code, i don't how to assign a rect for it.
    // initial
    D3DPRESENT_PARAMETERS d3dpp;
    m_pD3D = ::Direct3DCreate9(D3D_SDK_VERSION);

    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    d3dpp.BackBufferWidth = 352;
    d3dpp.BackBufferHeight = 240;

    if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
    D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED,
    &d3dpp, &m_pd3dDevice ) ) )
    return E_FAIL;

    // to the texture color at 0.0 or 1.0, respectively.
    m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
    m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );/
    m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
    m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );

    // set maximum ambient light
    m_pd3dDevice->SetRenderState(D3DRS_AMBIENT,RGB(255,255,255));
    m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
    m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE );
    m_pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
    m_pd3dDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);
    m_pd3dDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
    m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
    m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    m_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
    m_pd3dDevice->SetRenderState(D3DRS_ALPHAREF, 0x10);
    m_pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );

    // turn off alpha operation
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

    D3DSURFACE_DESC ddsd;
    HRESULT hr = m_pd3dDevice->CreateTexture( 352, 240, 1, 0, D3DFMT_UYVY,
    D3DPOOL_MANAGED, &m_pTexturesvideo, NULL);
    hr = m_pTexturesvideo->GetLevelDesc( 0, &ddsd );

    CUSTOMVERTEX* pVertices = NULL;
    RECT rect;
    rect.top = 0;
    rect.left = 0;
    rect.right = 352;
    rect.bottom = 240;

    CUSTOMVERTEX vertices[4] =
    {
    { (float)rect.left, (float)rect.top,
    0.0f, 1.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 0.0f },
    { (float)rect.right, (float)rect.top,
    0.0f, 1.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f },
    { (float)rect.left, (float)rect.bottom,
    0.0f, 1.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f },
    { (float)rect.right, (float)rect.bottom,
    0.0f, 1.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 1.0f }
    };

    m_pd3dDevice->CreateVertexBuffer(sizeof(vertices),
    0,
    D3DFVF_CUSTOMVERTEX,
    D3DPOOL_DEFAULT,
    &m_pVBvideo, NULL );

    m_pVBvideo->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 );
    memcpy( pVertices, vertices, sizeof(vertices));
    m_pVBvideo->Unlock();

    //capture vide in another thread
    D3DLOCKED_RECT d3dlr;
    m_pTexturesvideo->LockRect( 0, &d3dlr, 0, 0 );
    LONG lTexturePitch = d3dlr.Pitch;
    byte *pTextureBuffer = static_cast<byte *>(d3dlr.pBits);

    memcpy(pTextureBuffer, lpBuffer, nLength);
    m_pTexturesvideo->UnlockRect(0);

    HRESULT hr = m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0 );
    hr = m_pd3dDevice->BeginScene();

    hr = m_pd3dDevice->SetTexture( 0, m_pTexturesvideo );
    m_pd3dDevice->SetStreamSource( 0, m_pVBvideo, 0, sizeof(CUSTOMVERTEX));
    hr = m_pd3dDevice->SetVertexShader( NULL );
    m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
    m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
    m_pd3dDevice->SetTexture( 0, NULL );
    m_pd3dDevice->EndScene();
    m_pd3dDevice->Present( NULL, NULL, NULL, NULL );


     
Loading...

Share This Page

  • About Us

    Beyond3D has been around for over a decade and prides itself on being the best place on the web for in-depth, technically-driven discussion and analysis of 3D graphics hardware. If you love pixels and transistors, you've come to the right place!

    Beyond3D is proudly published by GPU Tools Ltd.
Loading...