不覆盖文件时D3DXSaveSurfaceToFile速度下降

时间:2018-08-01 14:05:55

标签: c++ io directx screenshot direct3d

我正在尝试使用DirectX9记录我的桌面屏幕。 对于最初的120ish帧,它以60 FPS记录。 但是,一旦超过该值,它就会降至15 FPS。

但是,如果我将其设置为通过将我通过的文件名中的iTotalFrameCount更改为iCurrentFrameCount来替换相同的前60个文件/帧,则

帧速率保持在60 FPS。

如果每次运行都覆盖相同的文件,是什么导致文件写入速度大幅下降?

该文件夹中有700帧来自上一次运行,并且始终会覆盖它们。

是什么让前60个如此特别以至于它们写得很快?

bool TakeScreenShot(IDirect3DDevice9 *Device, char *FileName, int ScreenX, int ScreenY)
{
    static IDirect3DSurface9 *FrontBuffer = NULL; 

    HRESULT Result;

    IDirect3DSwapChain9* chain;



    if(FrontBuffer == NULL)
    {
        Result = Device->CreateOffscreenPlainSurface(ScreenX, ScreenY, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &FrontBuffer, NULL);

        if(Result == D3DERR_INVALIDCALL)
            printf("Invalid call\n");

        if (Result != D3D_OK)
        {
            printf("Last error %d\n", GetLastError());

            return false;
        }
    }

    Device->GetSwapChain(0, &chain);

    chain->GetFrontBufferData(FrontBuffer);

    Result = D3DXSaveSurfaceToFile(FileName, D3DXIFF_BMP, FrontBuffer, NULL, NULL);

    if (Result != D3D_OK)
        return false;

    return true;
}





static long long startTime;



// this is the function used to render a single frame
void render_frame(void)
{
    static DWORD dwTime = 0;

    static float fFrameRate;

    static int iCurrentFrameCount = 0,
                iTotalFrameCount = 0;


    if(startTime == 0)
        startTime = milliseconds_now();



    if(GetTickCount() - dwTime >= 1000)
    {
        stringstream sstream; 
        sstream << "Framerate: " << iCurrentFrameCount << endl;

        printf("%s\n", sstream.str().c_str());

        iCurrentFrameCount = 0;
        dwTime = GetTickCount();
    }


    stringstream sstream;

    sstream << "C:\\frames\\" << iTotalFrameCount << ".bmp";

    TakeScreenShot(d3ddev, (char*)sstream.str().c_str(), 1920, 1080);


    iCurrentFrameCount++;
    iTotalFrameCount++;


    if(GetAsyncKeyState(VK_ESCAPE) != 0)
    {
        long long recordTime = milliseconds_now() - startTime;


        float seconds = recordTime / 1000.0f;


        float frameRate = (float)iTotalFrameCount / seconds;


        printf("FPS: %f\n", frameRate);


        Sleep(10000);

        exit(0);
    }
}

0 个答案:

没有答案