third_party/virtualbox/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win.cpp
/**
 * Create a new 3d context
 *
 * @returns VBox status code.
 * @param   pThis           VGA device instance data.
 * @param   cid             Context id
 */
Create a new 3d context
@returns VBox status code.
@param   pThis           VGA device instance data.
@param   cid             Context id
Create a new 3d context @returns VBox status code . @param pThis VGA device instance data . @param cid Context id
int vmsvga3dContextDefine(PVGASTATE pThis, uint32_t cid)
{
    int                     rc;
    PVMSVGA3DCONTEXT        pContext;
    HRESULT                 hr;
    D3DPRESENT_PARAMETERS   PresParam;
    PVMSVGA3DSTATE          pState = pThis->svga.p3dState;
    Log(("vmsvga3dContextDefine id %x\n", cid));
    AssertReturn(pState, VERR_NO_MEMORY);
    AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
    if (cid >= pState->cContexts)
    {
        uint32_t cNew = RT_ALIGN(cid + 15, 16);
        void *pvNew = RTMemRealloc(pState->papContexts, sizeof(pState->papContexts[0]) * cNew);
        AssertReturn(pvNew, VERR_NO_MEMORY);
        pState->papContexts = (PVMSVGA3DCONTEXT *)pvNew;
        while (pState->cContexts < cNew)
        {
            pContext = (PVMSVGA3DCONTEXT)RTMemAllocZ(sizeof(*pContext));
            AssertReturn(pContext, VERR_NO_MEMORY);
            pContext->id = SVGA3D_INVALID_ID;
            pState->papContexts[pState->cContexts++] = pContext;
        }
    }
    if (pState->papContexts[cid]->id != SVGA3D_INVALID_ID)
        vmsvga3dContextDestroy(pThis, cid);
    pContext = pState->papContexts[cid];
    memset(pContext, 0, sizeof(*pContext));
    pContext->id               = cid;
    for (uint32_t i = 0; i< RT_ELEMENTS(pContext->aSidActiveTextures); i++)
        pContext->aSidActiveTextures[i] = SVGA3D_INVALID_ID;
    pContext->sidRenderTarget  = SVGA3D_INVALID_ID;
    pContext->state.shidVertex = SVGA3D_INVALID_ID;
    pContext->state.shidPixel  = SVGA3D_INVALID_ID;
    for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
        pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
    CREATESTRUCT cs;
    AssertReturn(pThis->svga.u64HostWindowId, VERR_INTERNAL_ERROR);
    cs.lpCreateParams   = NULL;
    cs.dwExStyle        = WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY | WS_EX_TRANSPARENT;
#ifdef DEBUG_GFX_WINDOW
    cs.lpszName         = (char *)RTMemAllocZ(256);
    RTStrPrintf((char *)cs.lpszName, 256, "Context %d OpenGL Window", cid);
#else
    cs.lpszName         = NULL;
#endif
    cs.lpszClass        = NULL;
#ifdef DEBUG_GFX_WINDOW
    cs.style            = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_CAPTION;
#else
    cs.style            = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED | WS_CHILD | WS_VISIBLE;
#endif
    cs.x                = 0;
    cs.y                = 0;
    cs.cx               = pThis->svga.uWidth;
    cs.cy               = pThis->svga.uHeight;
    cs.hwndParent       = (HWND)pThis->svga.u64HostWindowId;
    cs.hMenu            = NULL;
    cs.hInstance        = pState->hInstance;
    rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATEWINDOW, (WPARAM)&pContext->hwnd, (LPARAM)&cs);
    AssertRCReturn(rc, rc);
    PresParam.BackBufferWidth               = 0;
    PresParam.BackBufferHeight              = 0;
    PresParam.BackBufferFormat              = D3DFMT_UNKNOWN;
    PresParam.BackBufferCount               = 0;
    PresParam.MultiSampleType               = D3DMULTISAMPLE_NONE;
    PresParam.MultiSampleQuality            = 0;
    PresParam.SwapEffect                    = D3DSWAPEFFECT_FLIP;
    PresParam.hDeviceWindow                 = pContext->hwnd;
    PresParam.Windowed                      = TRUE;     
    PresParam.EnableAutoDepthStencil        = FALSE;
    PresParam.AutoDepthStencilFormat        = D3DFMT_UNKNOWN;   
    PresParam.Flags                         = 0;
    PresParam.FullScreen_RefreshRateInHz    = 0;        
    PresParam.PresentationInterval          = D3DPRESENT_INTERVAL_IMMEDIATE;
#ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL
    hr = pState->pD3D9->CreateDevice(D3DADAPTER_DEFAULT,
                                     D3DDEVTYPE_HAL,
                                     pContext->hwnd,
                                     D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, 
                                     &PresParam,
                                     &pContext->pDevice);
#else
    hr = pState->pD3D9->CreateDeviceEx(D3DADAPTER_DEFAULT,
                                       D3DDEVTYPE_HAL,
                                       pContext->hwnd,
                                       D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, 
                                       &PresParam,
                                       NULL,
                                       &pContext->pDevice);
#endif
    AssertMsgReturn(hr == D3D_OK, ("vmsvga3dContextDefine: CreateDevice failed with %x\n", hr), VERR_INTERNAL_ERROR);
    Log(("vmsvga3dContextDefine: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
    return VINF_SUCCESS;
}
int vmsvga3dContextDefine ( PVGASTATE pThis , uint32_t cid ) { int rc ; PVMSVGA3DCONTEXT pContext ; HRESULT hr ; D3DPRESENT_PARAMETERS PresParam ; PVMSVGA3DSTATE pState = pThis -> svga . p3dState ; Log ( ( " \n " , cid ) ) ; AssertReturn ( pState , VERR_NO_MEMORY ) ; AssertReturn ( cid < SVGA3D_MAX_CONTEXT_IDS , VERR_INVALID_PARAMETER ) ; if ( cid >= pState -> cContexts ) { uint32_t cNew = RT_ALIGN ( cid + 15 , 16 ) ; void * pvNew = RTMemRealloc ( pState -> papContexts , sizeof ( pState -> papContexts [ 0 ] ) * cNew ) ; AssertReturn ( pvNew , VERR_NO_MEMORY ) ; pState -> papContexts = ( PVMSVGA3DCONTEXT * ) pvNew ; while ( pState -> cContexts < cNew ) { pContext = ( PVMSVGA3DCONTEXT ) RTMemAllocZ ( sizeof ( * pContext ) ) ; AssertReturn ( pContext , VERR_NO_MEMORY ) ; pContext -> id = SVGA3D_INVALID_ID ; pState -> papContexts [ pState -> cContexts ++ ] = pContext ; } } if ( pState -> papContexts [ cid ] -> id != SVGA3D_INVALID_ID ) vmsvga3dContextDestroy ( pThis , cid ) ; pContext = pState -> papContexts [ cid ] ; memset ( pContext , 0 , sizeof ( * pContext ) ) ; pContext -> id = cid ; for ( uint32_t i = 0 ; i < RT_ELEMENTS ( pContext -> aSidActiveTextures ) ; i ++ ) pContext -> aSidActiveTextures [ i ] = SVGA3D_INVALID_ID ; pContext -> sidRenderTarget = SVGA3D_INVALID_ID ; pContext -> state . shidVertex = SVGA3D_INVALID_ID ; pContext -> state . shidPixel = SVGA3D_INVALID_ID ; for ( uint32_t i = 0 ; i < RT_ELEMENTS ( pContext -> state . aRenderTargets ) ; i ++ ) pContext -> state . aRenderTargets [ i ] = SVGA3D_INVALID_ID ; CREATESTRUCT cs ; AssertReturn ( pThis -> svga . u64HostWindowId , VERR_INTERNAL_ERROR ) ; cs . lpCreateParams = NULL ; cs . dwExStyle = WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY | WS_EX_TRANSPARENT ; #ifdef DEBUG_GFX_WINDOW cs . lpszName = ( char * ) RTMemAllocZ ( 256 ) ; RTStrPrintf ( ( char * ) cs . lpszName , 256 , " " , cid ) ; #else cs . lpszName = NULL ; #endif cs . lpszClass = NULL ; #ifdef DEBUG_GFX_WINDOW cs . style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_CAPTION ; #else cs . style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED | WS_CHILD | WS_VISIBLE ; #endif cs . x = 0 ; cs . y = 0 ; cs . cx = pThis -> svga . uWidth ; cs . cy = pThis -> svga . uHeight ; cs . hwndParent = ( HWND ) pThis -> svga . u64HostWindowId ; cs . hMenu = NULL ; cs . hInstance = pState -> hInstance ; rc = vmsvga3dSendThreadMessage ( pState -> pWindowThread , pState -> WndRequestSem , WM_VMSVGA3D_CREATEWINDOW , ( WPARAM ) & pContext -> hwnd , ( LPARAM ) & cs ) ; AssertRCReturn ( rc , rc ) ; PresParam . BackBufferWidth = 0 ; PresParam . BackBufferHeight = 0 ; PresParam . BackBufferFormat = D3DFMT_UNKNOWN ; PresParam . BackBufferCount = 0 ; PresParam . MultiSampleType = D3DMULTISAMPLE_NONE ; PresParam . MultiSampleQuality = 0 ; PresParam . SwapEffect = D3DSWAPEFFECT_FLIP ; PresParam . hDeviceWindow = pContext -> hwnd ; PresParam . Windowed = TRUE ; PresParam . EnableAutoDepthStencil = FALSE ; PresParam . AutoDepthStencilFormat = D3DFMT_UNKNOWN ; PresParam . Flags = 0 ; PresParam . FullScreen_RefreshRateInHz = 0 ; PresParam . PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE ; #ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL hr = pState -> pD3D9 -> CreateDevice ( D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , pContext -> hwnd , D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING , & PresParam , & pContext -> pDevice ) ; #else hr = pState -> pD3D9 -> CreateDeviceEx ( D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , pContext -> hwnd , D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING , & PresParam , NULL , & pContext -> pDevice ) ; #endif AssertMsgReturn ( hr == D3D_OK , ( " \n " , hr ) , VERR_INTERNAL_ERROR ) ; Log ( ( " \n " , PresParam . BackBufferWidth , PresParam . BackBufferHeight , PresParam . BackBufferCount , PresParam . BackBufferFormat ) ) ; return VINF_SUCCESS ; }
Create a new 3d context
 @returns VBox status code.
Create a new 3d context @returns VBox status code .
/* Grow the array. */ /* If one already exists with this id, then destroy it now. */ /* Create a context window. */ /* Changed when the function returns. */ /** @todo */ /* not relevant */ /* windowed -> 0 */ /** @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */ //D3DCREATE_HARDWARE_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,