directxtexturesdirectx-9

Update directX texture


How can I solve following task: some app need to

  1. use dozens dx9 terxtures (render them with dx3d) and
  2. update some of them (whole or in part).

I.e. sometimes (once per frame/second/minute) i need to write bytes (void *) in different formats (argb, bgra, rgb, 888, 565) to some sub-rect of existing texture. In openGL solution is very simple - glTexImage2D. But here unfamiliar platform features completely confused me. Interested in solution for both dx9 and dx11.


Solution

  • To update a texture, make sure the texture is created in D3DPOOL_MANAGED memory pool.

    D3DXCreateTexture( device, size.x, size.y, numMipMaps,usage, textureFormat, D3DPOOL_MANAGED, &texture ); 
    

    Then call LockRect to update the data

    RECT rect  = {x,y,z,w};  // the dimensions you want to lock
    D3DLOCKED_RECT lockedRect = {0}; // "out" parameter from LockRect function below
    texture->LockRect(0, &lockedRect, &rect, 0);
    
    // copy the memory into lockedRect.pBits
    // make sure you increment each row by "Pitch"
    
    unsigned char* bits = ( unsigned char* )lockedRect.pBits; 
    for( int row = 0; row < numRows; row++ )
    {
        // copy one row of data into "bits", e.g. memcpy( bits, srcData, size )
        ...
    
        // move to the next row
        bits += lockedRect.Pitch;
    }
    
    // unlock when done
    texture->UnlockRect(0);