Search code examples
graphicsmicrosoft-metrodirectxdirect2d

How to efficiently write pixels to the screen with Direct2D


I have a array of pixels (m_pixels) that I want to render to the screen using Direct2D. The array contains 10,000 elements (100 rows of 100 pixels). The code below loops over the pixels and draws them to the screen as 10x10 rectangles. Is there a more efficient way of performing this operation? How can I add a GaussianBlur effect to the pixels/image?

m_d2dContext->BeginDraw();
m_d2dContext->Clear(ColorF(0.0f, 0.0f, 0.0f));



// Render m_pixels
// m_pixels is updated by the solver directly before render
auto rect = D2D1_RECT_F();
ComPtr<ID2D1SolidColorBrush> pBlackBrush;
DX::ThrowIfFailed(m_d2dContext->CreateSolidColorBrush(ColorF(1.0f, 0.0f, 0.0f),&pBlackBrush));

for (int i=0; i<10000; i++){
    //Update the color
    pBlackBrush->SetColor(D2D1::ColorF(m_pixels[i]*3, m_pixels[i], m_pixels[i]));
    //Update the rectangle size
    rect.top = 10*floor(i/100);
    rect.left = 10*floor(i%100);
    rect.bottom = 10*floor(i/100) + 10;
    rect.right = 10*floor(i%100) + 10;
    //Set the rectangle color
    m_d2dContext->FillRectangle(rect, pBlackBrush.Get());
}

HRESULT hr = m_d2dContext->EndDraw();

Solution

  • Try using ID2D1RenderTarget::CreateBitmap() and ID2D1RenderTarget::DrawBitmap()