tags:

views:

20

answers:

1

I want to be able to set the individual pixels of the back buffer in my program in an efficient way. This is what I call in my rendering function:

void render_frame(void)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);

    d3ddev->BeginScene();

    d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &surface);
    D3DLOCKED_RECT locked;
    surface->LockRect(&locked, NULL, 0);
    *(BYTE*)locked.pBits=42;
    surface->UnlockRect();
    surface->Release();

    d3ddev->EndScene();

    d3ddev->Present(NULL, NULL, NULL, NULL);

}

However it always crashes. If I comment out the part where I assign the first byte to 42, it doesn't crash. So what am I doing wrong, shouldn't I be able to assign values to the buffer pointed by bPits since the surface is locked?

Just found out that locked.pBits is a null pointer. Why did LockRect fail, then?

+1  A: 

In order to lock the back buffer, you need to specify a flag at device creation:

D3DPRESENT_PARAMETERS d3dpp;
(...)
d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;

However, as specified in Direct3D specifications, this could seriously hurt the performances. You should rather draw textured triangles.

tibur
Thanks. However, do you know any reason why it would hurt the performance? With drawing textured triangles I would imagine it would have to do some processing followed by editing the back buffer, whereas what I'm trying to do is skip the first step and just edit it. Why would this be less efficient?
kaykun
It hurts the performances because the back buffer surface has to be downloaded from the GPU to the CPU, then re-uploaded to the GPU. It will also break the streaming: a graphic API is designed in a way it permits to issue rendering commands without executing them directly in order to increase performances.
tibur