使用图像的一部分创建directx9纹理

Create directx9 texture using a portion(s) of an image

本文关键字:创建 directx9 纹理 一部分 图像      更新时间:2023-10-16

我有一张图片(208x8),我想在不同的区域复制8x8的正方形,然后将所有的正方形连接起来创建一个IDirect3DTexture9*

取决于你到底想做什么IDirect3DDevice9:: updatessurface或IDirect3DDevice9::StretchRect可能会帮助你。

对于像你描述的非常小的纹理的简单操作,使用CPU(即使用IDirect3DTexture9::LockRect)操作它们可能是有利的。对于D3D9,这通常意味着纹理被重新上传到VRAM,所以它通常只对小的或不经常修改的纹理有用。但有时候,如果你是渲染受限的,并且你对在循环中更新纹理的位置很小心,那么就有可能隐藏像这样的操作成本,并"免费"获得它们。

为了避免VRAM上传,您可以使用POOL_MANAGED资源与适当的使用和锁定标志相结合,将资源定位在AGP孔径内,允许CPU和GPU的高速访问,参见:http://msdn.microsoft.com/en-us/library/windows/desktop/ee418784(v=vs.85).aspx

如果你在CPU上操作,要注意各种纹理格式的平铺和对齐限制。最好的信息在SDK附带的文档中(包括几个白皮书),在线文档是不完整的。

下面是一个基本的例子:

IDirect3DTexture9* m_tex = getYourTexture();
m_tex->LockRect(0, &outRect, d3dRect, D3DLOCK_DISCARD);
// Stride depends on your texture format - this is the number of bytes per texel. 
// Note that this may be less than 1 for DXT1 textures in which case you'll need 
// some bit swizzling logic. Can be inferred from Pitch and width.
int stride = 1; 
int rowPitch = outRect.Pitch;
// Choose a pointer type that suits your stride.
unsigned char* pixels = (unsigned char*)outRect.pBits;
// Clear to black.
for (int y=0; y < d3dRect.height; ++y)
{
    for (int x=0; x < d3dRect.width; ++x)
    {
        pixels[x + rowPitch * y] = 0x0;
    }
}
m_tex->UnlockRect(0);