Should i be using LockRect?

Started by
3 comments, last by Jerax 16 years, 10 months ago
Heres the situation... Im reading a texture format, the format gives the height, width, num mips maps and all that kind of info and then it gives the raw image data. This data is dxt1 or dxt3. So up until now I have been creating a texture with the supplied info and then locking the rect reading the raw data into it and unlocking. Is this the best way or should I be doing it another? Thanks for any help, -Dave Heres the source if anyone needs a clearer idea of what im doing...

// create the texture
D3DXCreateTexture(*pDevice, textures[texturePos].width, textures[texturePos].height, textures[texturePos].mipmapCount, D3DUSAGE_DYNAMIC, format, D3DPOOL_DEFAULT, &textures[texturePos].texture);

for(mipmapPos = 0; mipmapPos < textures[texturePos].mipmapCount; mipmapPos++){
    readBinary(is, dataSize);

    imageData = new char[dataSize];
    is.read(imageData, dataSize);

    textures[texturePos].texture->LockRect(mipmapPos, &rect, NULL, D3DLOCK_DISCARD);
    memcpy(rect.pBits, imageData, dataSize);
    textures[texturePos].texture->UnlockRect(mipmapPos);

    delete [] imageData;
}

Advertisement
You need to take into consideration the pitch. The texture memory isn't guaranteed to be consecutive.
A better solution is to use D3DXLoadSurfaceFromMemory.
The pitch may not always be the same as width * bytesPerPixel and therefore you should loop through the height and make sure you fill each row or pixel instead. If it's possible to just copy any arbitrary format like that into the texture memory i'm not sure but at least it's possible to clear all the pixels to an arbitrary color.

for (int y = 0; y < height; ++y){  for (int x = 0; x < width; ++x)  {    void* curPixel = rect.pBits + y * rect.Pitch + x * bytesPerPixel;    *curPixel = MYCOLOR;  }}
Using LockRect is fine - you won't see much if any difference from using D3DXLoadSurfaceFromMemory or similar.

This topic is closed to new replies.

Advertisement