硬件像素缓存是一种储存在显示卡硬件内存中特殊图形数据的类型,通常被用来作为纹理使用。像素缓存可以用来保存一维、二维或者三维图形。一个纹理可以有一些这种缓存来组成。和顶点缓存与索引缓存不同,像素缓存并不能直接创建。当你创建一个纹理的时候,所需的顶点缓存会带着相应的数据被自动创建出来。
更新像素缓存
这里提供了两种不同的更新像素缓存的方法;其中一种很简单,另外一种稍微有一些难度,不过在某些场合会表现的高效率。不过不管是选择了哪一种方法,都需要借助PixelBox对象来帽徽缓存中的图像数据(可以参照像素盒)。
从像素缓存中得到图像的简单方法是通过调用HardwarePixelBuffer::blitFromMemory方法。这里获得一个PixelBox对象并帮助你进行所有必要的定点格式转换与缩放。例如,建立一个手动纹理并把一个图片放入里面,你将要采用以下代码:
// Manually loads an image and puts the contents in a manually created texture
Image img;
// Create RGB texture with 5 mipmaps
另外一个高级的方法来从PixelBuffer(像素缓存)中得到图像数据的方法是对缓存加锁。通过对PixelBuffer加锁,你可以不管缓存的具体格式而直接的在GPU中存取其内容。
/// Lock the buffer so we can write to it
buffer->lock(HardwareBuffer::HBL_DISCARD);
/// Update the contents of pb here
/// Here we assume data.format is PF_X8R8G8B8 so we can address pixels as uint32.
size_t height = pb.getHeight();
size_t width = pb.getWidth();
// Skip between rows of image