VirtualBox

Changeset 94241 in vbox for trunk/src/VBox/Devices


Ignore:
Timestamp:
Mar 15, 2022 10:11:36 AM (3 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
150497
Message:

Devices/Graphics: cleaned up paInitialData initialization for textures; removed duplicated code: bugref:9830

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win-dx.cpp

    r94232 r94241  
    19391939     * Create D3D11 texture object.
    19401940     */
     1941    D3D11_SUBRESOURCE_DATA *paInitialData = NULL;
     1942    if (pSurface->paMipmapLevels[0].pSurfaceData)
     1943    {
     1944        /* Can happen for a non GBO surface or if GBO texture was updated prior to creation of the hardware resource. */
     1945        uint32_t const cSubresource = numMipLevels * pSurface->surfaceDesc.numArrayElements;
     1946        paInitialData = (D3D11_SUBRESOURCE_DATA *)RTMemAlloc(cSubresource * sizeof(D3D11_SUBRESOURCE_DATA));
     1947        AssertPtrReturn(paInitialData, VERR_NO_MEMORY);
     1948
     1949        for (uint32_t i = 0; i < cSubresource; ++i)
     1950        {
     1951            PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
     1952            D3D11_SUBRESOURCE_DATA *p = &paInitialData[i];
     1953            p->pSysMem          = pMipmapLevel->pSurfaceData;
     1954            p->SysMemPitch      = pMipmapLevel->cbSurfacePitch;
     1955            p->SysMemSlicePitch = pMipmapLevel->cbSurfacePlane;
     1956        }
     1957    }
     1958
    19411959    HRESULT hr = S_OK;
    19421960    if (pSurface->surfaceFlags & SVGA3D_SURFACE_SCREENTARGET)
     
    19451963         * Create the texture in backend device and open for the specified context.
    19461964         */
    1947         D3D11_SUBRESOURCE_DATA *paInitialData = NULL;
    1948         D3D11_SUBRESOURCE_DATA aInitialData[SVGA3D_MAX_MIP_LEVELS];
    1949         if (pSurface->paMipmapLevels[0].pSurfaceData)
    1950         {
    1951             /** @todo Can happen for a non GBO surface or if GBO texture was updated prior to creation if the hardware resource. Test this. */
    1952             for (uint32_t i = 0; i < numMipLevels; ++i)
    1953             {
    1954                 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
    1955                 D3D11_SUBRESOURCE_DATA *p = &aInitialData[i];
    1956                 p->pSysMem          = pMipmapLevel->pSurfaceData;
    1957                 p->SysMemPitch      = pMipmapLevel->cbSurfacePitch;
    1958                 p->SysMemSlicePitch = pMipmapLevel->cbSurfacePlane;
    1959             }
    1960             paInitialData = &aInitialData[0];
    1961         }
    1962 
    19631965        D3D11_TEXTURE2D_DESC td;
    19641966        RT_ZERO(td);
     
    20242026        Assert(cDepth == 1);
    20252027//DEBUG_BREAKPOINT_TEST();
    2026         D3D11_SUBRESOURCE_DATA *paInitialData = NULL;
    2027         D3D11_SUBRESOURCE_DATA aInitialData[6 * SVGA3D_MAX_MIP_LEVELS];
    2028         if (pSurface->paMipmapLevels[0].pSurfaceData)
    2029         {
    2030             /** @todo Can happen for a non GBO surface or if GBO texture was updated prior to creation if the hardware resource. Test this. */
    2031             /** @todo for (i = 0; i < pSurface->cFaces * numMipLevels; ++i) */
    2032             for (uint32_t iArray = 0; iArray < pSurface->surfaceDesc.numArrayElements; ++iArray)
    2033             {
    2034                 for (uint32_t i = 0; i < numMipLevels; ++i)
    2035                 {
    2036                     uint32_t const iSubresource = vmsvga3dCalcSubresource(i, iArray, numMipLevels);
    2037 
    2038                     PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[iSubresource];
    2039                     D3D11_SUBRESOURCE_DATA *p = &aInitialData[iSubresource];
    2040                     p->pSysMem          = pMipmapLevel->pSurfaceData;
    2041                     p->SysMemPitch      = pMipmapLevel->cbSurfacePitch;
    2042                     p->SysMemSlicePitch = pMipmapLevel->cbSurfacePlane;
    2043                 }
    2044             }
    2045             paInitialData = &aInitialData[0];
    2046         }
    20472028
    20482029        D3D11_TEXTURE2D_DESC td;
     
    21222103             */
    21232104            Assert(pSurface->cFaces == 1);
    2124 
    2125             D3D11_SUBRESOURCE_DATA *paInitialData = NULL;
    2126             D3D11_SUBRESOURCE_DATA aInitialData[SVGA3D_MAX_MIP_LEVELS];
    2127             if (pSurface->paMipmapLevels[0].pSurfaceData)
    2128             {
    2129                 /** @todo Can happen for a non GBO surface or if GBO texture was updated prior to creation if the hardware resource. Test this. */
    2130                 for (uint32_t i = 0; i < numMipLevels; ++i)
    2131                 {
    2132                     PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
    2133                     D3D11_SUBRESOURCE_DATA *p = &aInitialData[i];
    2134                     p->pSysMem          = pMipmapLevel->pSurfaceData;
    2135                     p->SysMemPitch      = pMipmapLevel->cbSurfacePitch;
    2136                     p->SysMemSlicePitch = pMipmapLevel->cbSurfacePlane;
    2137                 }
    2138                 paInitialData = &aInitialData[0];
    2139             }
     2105            Assert(pSurface->surfaceDesc.numArrayElements == 1);
    21402106
    21412107            D3D11_TEXTURE3D_DESC td;
     
    22052171             */
    22062172            Assert(pSurface->cFaces == 1);
    2207 
    2208             D3D11_SUBRESOURCE_DATA *paInitialData = NULL;
    2209             D3D11_SUBRESOURCE_DATA aInitialData[SVGA3D_MAX_MIP_LEVELS];
    2210             if (pSurface->paMipmapLevels[0].pSurfaceData)
    2211             {
    2212                 /** @todo Can happen for a non GBO surface or if GBO texture was updated prior to creation if the hardware resource. Test this. */
    2213                 for (uint32_t i = 0; i < numMipLevels; ++i)
    2214                 {
    2215                     PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
    2216                     D3D11_SUBRESOURCE_DATA *p = &aInitialData[i];
    2217                     p->pSysMem          = pMipmapLevel->pSurfaceData;
    2218                     p->SysMemPitch      = pMipmapLevel->cbSurfacePitch;
    2219                     p->SysMemSlicePitch = pMipmapLevel->cbSurfacePlane;
    2220                 }
    2221                 paInitialData = &aInitialData[0];
    2222             }
    22232173
    22242174            D3D11_TEXTURE2D_DESC td;
     
    22892239    Assert(hr == S_OK);
    22902240
     2241    RTMemFree(paInitialData);
     2242
    22912243    if (pSurface->autogenFilter != SVGA3D_TEX_FILTER_NONE)
    22922244    {
     
    23052257        else
    23062258            pSurface->idAssociatedContext = pDXContext->cid;
    2307         return VINF_SUCCESS;
    2308     }
    2309 
    2310     /** @todo different enmResType Failure. */
    2311     D3D_RELEASE(pBackendSurface->pStagingTexture);
    2312     D3D_RELEASE(pBackendSurface->pDynamicTexture);
    2313     D3D_RELEASE(pBackendSurface->u.pTexture2D);
    2314     RTMemFree(pBackendSurface);
    2315     return VERR_NO_MEMORY;
    2316 }
    2317 
    2318 
    2319 /** @todo This is practically the same code as vmsvga3dBackSurfaceCreateTexture */
    2320 static int vmsvga3dBackSurfaceCreateDepthStencilTexture(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, PVMSVGA3DSURFACE pSurface)
    2321 {
    2322     DXDEVICE *pDXDevice = dxDeviceFromContext(pThisCC->svga.p3dState, pDXContext);
    2323     AssertReturn(pDXDevice->pDevice, VERR_INVALID_STATE);
    2324 
    2325     if (pSurface->pBackendSurface != NULL)
    2326     {
    2327         AssertFailed(); /** @todo Should the function not be used like that? */
    2328         vmsvga3dBackSurfaceDestroy(pThisCC, pSurface);
    2329     }
    2330 
    2331     PVMSVGA3DBACKENDSURFACE pBackendSurface;
    2332     int rc = dxBackendSurfaceAlloc(&pBackendSurface);
    2333     AssertRCReturn(rc, rc);
    2334 
    2335     uint32_t const cWidth = pSurface->paMipmapLevels[0].mipmapSize.width;
    2336     uint32_t const cHeight = pSurface->paMipmapLevels[0].mipmapSize.height;
    2337     uint32_t const cDepth = pSurface->paMipmapLevels[0].mipmapSize.depth;
    2338     uint32_t const numMipLevels = pSurface->cLevels;
    2339 
    2340     DXGI_FORMAT dxgiFormat = vmsvgaDXSurfaceFormat2Dxgi(pSurface->format);
    2341     AssertReturn(dxgiFormat != DXGI_FORMAT_UNKNOWN, E_FAIL);
    2342 
    2343     /*
    2344      * Create D3D11 texture object.
    2345      */
    2346     HRESULT hr = S_OK;
    2347     if (pSurface->surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
    2348     {
    2349         /*
    2350          * CubeMap texture.
    2351          */
    2352         Assert(pSurface->cFaces == 6);
    2353         Assert(cWidth == cHeight);
    2354         Assert(cDepth == 1);
    2355         Assert(numMipLevels == 1);
    2356 //DEBUG_BREAKPOINT_TEST();
    2357         D3D11_SUBRESOURCE_DATA *paInitialData = NULL;
    2358         D3D11_SUBRESOURCE_DATA aInitialData[6 * SVGA3D_MAX_MIP_LEVELS];
    2359         if (pSurface->paMipmapLevels[0].pSurfaceData)
    2360         {
    2361             /** @todo Can happen for a non GBO surface or if GBO texture was updated prior to creation if the hardware resource. Test this. */
    2362             /** @todo for (i = 0; i < pSurface->cFaces * numMipLevels; ++i) */
    2363             for (uint32_t iArray = 0; iArray < pSurface->surfaceDesc.numArrayElements; ++iArray)
    2364             {
    2365                 for (uint32_t i = 0; i < numMipLevels; ++i)
    2366                 {
    2367                     uint32_t const iSubresource = vmsvga3dCalcSubresource(i, iArray, numMipLevels);
    2368 
    2369                     PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[iSubresource];
    2370                     D3D11_SUBRESOURCE_DATA *p = &aInitialData[iSubresource];
    2371                     p->pSysMem          = pMipmapLevel->pSurfaceData;
    2372                     p->SysMemPitch      = pMipmapLevel->cbSurfacePitch;
    2373                     p->SysMemSlicePitch = pMipmapLevel->cbSurfacePlane;
    2374                 }
    2375             }
    2376             paInitialData = &aInitialData[0];
    2377         }
    2378 
    2379         D3D11_TEXTURE2D_DESC td;
    2380         RT_ZERO(td);
    2381         td.Width              = cWidth;
    2382         td.Height             = cHeight;
    2383         td.MipLevels          = 1;
    2384         td.ArraySize          = pSurface->surfaceDesc.numArrayElements;
    2385         td.Format             = dxgiFormat;
    2386         td.SampleDesc.Count   = 1;
    2387         td.SampleDesc.Quality = 0;
    2388         td.Usage              = D3D11_USAGE_DEFAULT;
    2389         td.BindFlags          = dxBindFlags(pSurface->surfaceFlags);
    2390         td.CPUAccessFlags     = 0;
    2391         td.MiscFlags          = D3D11_RESOURCE_MISC_TEXTURECUBE;
    2392 
    2393         hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->u.pTexture2D);
    2394         Assert(SUCCEEDED(hr));
    2395         if (SUCCEEDED(hr))
    2396         {
    2397             /* Map-able texture. */
    2398             td.MipLevels      = 1; /* Must be for D3D11_USAGE_DYNAMIC. */
    2399             td.ArraySize      = 1; /* Must be for D3D11_USAGE_DYNAMIC. */
    2400             td.Usage          = D3D11_USAGE_DYNAMIC;
    2401             td.BindFlags      = D3D11_BIND_SHADER_RESOURCE; /* Have to specify a supported flag, otherwise E_INVALIDARG will be returned. */
    2402             td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    2403             td.MiscFlags      = 0;
    2404             hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->pDynamicTexture);
    2405             Assert(SUCCEEDED(hr));
    2406         }
    2407 
    2408         if (SUCCEEDED(hr))
    2409         {
    2410             /* Staging texture. */
    2411             td.Usage          = D3D11_USAGE_STAGING;
    2412             td.BindFlags      = 0; /* No flags allowed. */
    2413             td.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
    2414             td.MiscFlags      = 0;
    2415             hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->pStagingTexture);
    2416             Assert(SUCCEEDED(hr));
    2417         }
    2418 
    2419         if (SUCCEEDED(hr))
    2420         {
    2421             pBackendSurface->enmResType = VMSVGA3D_RESTYPE_TEXTURE_CUBE;
    2422         }
    2423     }
    2424     else if (pSurface->surfaceFlags & SVGA3D_SURFACE_1D)
    2425     {
    2426         AssertFailed(); /** @todo implement */
    2427         hr = E_FAIL;
    2428     }
    2429     else
    2430     {
    2431         if (cDepth > 1)
    2432         {
    2433             AssertFailed(); /** @todo implement */
    2434             hr = E_FAIL;
    2435         }
    2436         else
    2437         {
    2438             /*
    2439              * 2D texture.
    2440              */
    2441             Assert(pSurface->cFaces == 1);
    2442             Assert(numMipLevels == 1);
    2443 
    2444             D3D11_SUBRESOURCE_DATA *paInitialData = NULL;
    2445             D3D11_SUBRESOURCE_DATA aInitialData[SVGA3D_MAX_MIP_LEVELS];
    2446             if (pSurface->paMipmapLevels[0].pSurfaceData)
    2447             {
    2448                 /** @todo Can happen for a non GBO surface or if GBO texture was updated prior to creation if the hardware resource. Test this. */
    2449                 for (uint32_t i = 0; i < numMipLevels; ++i)
    2450                 {
    2451                     PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
    2452                     D3D11_SUBRESOURCE_DATA *p = &aInitialData[i];
    2453                     p->pSysMem          = pMipmapLevel->pSurfaceData;
    2454                     p->SysMemPitch      = pMipmapLevel->cbSurfacePitch;
    2455                     p->SysMemSlicePitch = pMipmapLevel->cbSurfacePlane;
    2456                 }
    2457                 paInitialData = &aInitialData[0];
    2458             }
    2459 
    2460             D3D11_TEXTURE2D_DESC td;
    2461             RT_ZERO(td);
    2462             td.Width              = cWidth;
    2463             td.Height             = cHeight;
    2464             td.MipLevels          = 1;
    2465             td.ArraySize          = pSurface->surfaceDesc.numArrayElements;
    2466             td.Format             = dxgiFormat;
    2467             td.SampleDesc.Count   = 1;
    2468             td.SampleDesc.Quality = 0;
    2469             td.Usage              = D3D11_USAGE_DEFAULT;
    2470             td.BindFlags          = dxBindFlags(pSurface->surfaceFlags);
    2471             td.CPUAccessFlags     = 0;
    2472             td.MiscFlags          = 0;
    2473 
    2474             hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->u.pTexture2D);
    2475             Assert(SUCCEEDED(hr));
    2476             if (SUCCEEDED(hr))
    2477             {
    2478                 /* Map-able texture. */
    2479                 td.MipLevels      = 1; /* Must be for D3D11_USAGE_DYNAMIC. */
    2480                 td.ArraySize      = 1; /* Must be for D3D11_USAGE_DYNAMIC. */
    2481                 td.Usage          = D3D11_USAGE_DYNAMIC;
    2482                 td.BindFlags      = D3D11_BIND_SHADER_RESOURCE; /* Have to specify a supported flag, otherwise E_INVALIDARG will be returned. */
    2483                 td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    2484                 td.MiscFlags      = 0;
    2485                 hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->pDynamicTexture);
    2486                 Assert(SUCCEEDED(hr));
    2487             }
    2488 
    2489             if (SUCCEEDED(hr))
    2490             {
    2491                 /* Staging texture. */
    2492                 td.Usage          = D3D11_USAGE_STAGING;
    2493                 td.BindFlags      = 0; /* No flags allowed. */
    2494                 td.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
    2495                 td.MiscFlags      = 0;
    2496                 hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->pStagingTexture);
    2497                 Assert(SUCCEEDED(hr));
    2498             }
    2499 
    2500             if (SUCCEEDED(hr))
    2501             {
    2502                 pBackendSurface->enmResType = VMSVGA3D_RESTYPE_TEXTURE_2D;
    2503             }
    2504         }
    2505     }
    2506 
    2507     if (SUCCEEDED(hr))
    2508     {
    2509         /*
    2510          * Success.
    2511          */
    2512         pBackendSurface->enmDxgiFormat = dxgiFormat;
    2513         pSurface->pBackendSurface = pBackendSurface;
    2514         pSurface->idAssociatedContext = pDXContext->cid;
    25152259        return VINF_SUCCESS;
    25162260    }
     
    67106454    {
    67116455        /* Create the actual texture. */
    6712         rc = vmsvga3dBackSurfaceCreateDepthStencilTexture(pThisCC, pDXContext, pSurface);
     6456        rc = vmsvga3dBackSurfaceCreateTexture(pThisCC, pDXContext, pSurface);
    67136457        AssertRCReturn(rc, rc);
    67146458    }
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette