Search code examples
c++directxawesomium

Create DirectX9Texture from RGBA (const char*) buffer


I have a RGBA format image buffer, and I need to convert it to a DirectX9Texture, I have searched the internet many times, but nothing solid comes up.

I'am trying to integrate Awesomium in my DirectX9 app. In other words, trying to display a webpage on a DirectX surface. And yes, I tried to create my own surface class, without sucess.

I know anwsers can't be too long, so if you have mercy, maybe you can link me to some correct places?


Solution

  • You cannot create a surface directly, you must create a texture, and then use its surface. Although, for your purposes, you shouldn't need to access the surface directly.

    IDirect3DDevice9* device = ...;
    // Create a texture: http://msdn.microsoft.com/en-us/library/windows/desktop/bb174363(v=vs.85).aspx
    // parameters should be fairly obvious from your input data.
    IDirect3DTexture9* tex;
    device->CreateTexture(w, h, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tex, 0);
    // Lock the texture for writing: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205913(v=vs.85).aspx
    D3DLOCKED_RECT rect;
    tex->LockRect(0, &rect, 0, D3DLOCK_DISCARD);
    // Write your image data to rect.pBits here. Note that each scanline of the locked surface 
    // may have padding, the rect.Pitch will tell you how many bytes each scanline expects. You
    // should know what the pitch of your input data is. Also, if your image data is in RGBA, you
    // will have to swizzle it to ARGB, as D3D9 does not have a RGBA format.
    
    // Unlock the texture so it can be used.
    tex->UnlockRect(0);
    

    This code also ignores any errors that could occur as a result of these function calls. In production code, you should be checking for any possible errors (eg. from CreateTexture, and LockRect).