Frage

I want to create a texture from a pointer to a pixel buffer with the format BGRA and it constantly changes / updates its values. I want to draw this texture to the screen at the top left corner every frame.

Using some of my XNA and OpenGL knowledge, I figured out some of it but I'm stuck getting the texture to see the changes to the buffer. I also don't know how to draw the texture properly so I created a sprite (just like in XNA).

I have written the following..

DirectX 9:

void CreateSprite(IDirect3DDevice9* Device, ID3DXSprite* &Sprite)
{
    D3DXCreateSprite(Device, Sprite);
}

void DrawSprite(IDirect3DTexture9* Texture, ID3DXSprite* Sprite, D3DXVECTOR3 Position)
{
    Sprite->Begin(D3DXSPRITE_ALPHABLEND);
        sprite->Draw(Texture, nullptr, nullptr, &Position, 0xFFFFFFFF);
    Sprite->End();
    //SafeRelease(Sprite);
    //SafeRelease(Texture);
}

void BufferToTexture(IDirect3DDevice9* Device, BufferInfo* BuffPtr)
{
    if (BuffPtr != nullptr)
    {
        if (Texture == nullptr)
        {
            Device->CreateTexture(BuffPtr->width, BuffPtr->height, 1, 0, D3DFMT_X8B8G8R8, D3DPOOL_MANAGED, &Texture, 0);
        }
        else
        {
            D3DLOCKED_RECT rect;
            Texture->LockRect(&rect, 0, D3DLOCK_DISCARD);
            std::uint8_t* dest = static_cast<std::uint8_t*>(rect.pBits);
            memcpy(dest, BuffPtr->Pixels, BuffPtr->width * BuffPtr->height * 4);
            Texture->UnlockRect(0);
        }

        //I modify the Pixel Buffer. Not sure if the Texture will see these changes.
        //Would be better if I can make the texture's rect.pBits point to BuffPtr->Pixels.
        //So that I can avoid the above memcpy.
        std::uint8_t* Ptr = (std::uint8_t*)BuffPtr->Pixels;
        for (int I = 0; I < BuffPtr->height; ++I)
        {
            for (int J = 0; J < BuffPtr->width; ++J)
            {
                std::uint8_t B = *(Ptr++);
                std::uint8_t G = *(Ptr++);
                std::uint8_t R = *(Ptr++);
                *(Ptr++) = (B == 0 && G == 0 && R == 0) ? 0 : 0xFF;
            }
        }
    }
}

In OpenGL, the code uses the pixel buffer directly. Any changes to the buffer also happens to the texture and no copying is done. The code allows me to draw an array of pixels directly to the screen. I'm trying to accomplish the same thing in DirectX but I'm not sure how to make the texture see changes in the buffer. Also, I'm not sure how to draw my DirectX texture to the screen.

War es hilfreich?

Lösung

"Not sure if the Texture will see these changes."

No, it will not because you are making changes to the memory after you Unlock the rect. Locking and unlocking a resource in D3D is analogous to unmapping a buffer object in OpenGL.

As for why it works in OpenGL, I can only imagine you are mapping and unmapping your PBO somewhere outside of the function BufferToTexture (...). If you alter your D3D code to work this way as well, you should be part way toward solving this.

You should also be aware that D3D uses a different texture origin: (0,0) is top-left in D3D and bottom-left in OpenGL.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top