Search code examples
renderingdirectxrenderdirectx-9

Copy data from Offscreen-plain to Render target


What am I doing wrong?

I want to create multisampled render target with bitmap from image.

My plan is:

  1. Create render target and offscreen-plain surfaces
  2. Fill offscreen surface with bitmap
  3. Use StretchRect to copy data from offscreen-plain to render target
IDirect3DSurface9* targetSurface;
IDirect3DSurface9* sourceSurface;

device->CreateRenderTarget(width, height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_4_SAMPLES, 0, false, &targetSurface, NULL);
device->CreateOffscreenPlainSurface(width, height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &sourceSurface, NULL);

D3DLOCKED_RECT lockedRect;
sourceSurface->LockRect(&lockedRect, NULL, 0);
// Writing data...
sourceSurface->UnlockRect();

// Copy image content to render target
device->StretchRect(sourceSurface, NULL, targetSurface, NULL, D3DTEXF_POINT);

But the rendered result is not what I expect.

Wrong rendered texture


Solution

  • The problem was the wrong bitmap filling.

    RenderTarget size is power of two.

    Also, offscreen-plain is redundant, so the code is following:

    IDirect3DSurface9* surface;
    
    device->CreateRenderTarget(width, height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_4_SAMPLES, 0, true, &surface, NULL);
    
    D3DLOCKED_RECT lockedRect;
    surface->LockRect(&lockedRect, NULL, 0);
    // Writing data...
    surface->UnlockRect();