4.3 Texture Memory and Storage

All textures must be stored somewhere, and several copies may be stored in several places (e.g. the host memory or memory onboard the graphics card). Sh abstracts this by providing an ShMemory class. Such a class only represents memory, it is connected to other objects (ShStorages) that take care of where the memory is stored. The system automatically keeps track of how up-to-date each storage is, and only transfers memory from one storage to another if necessary. Setting up a texture that shares memory with another texture is as simple as writing texture2.memory(texture1.memory());. Sh also provides an ShImage class that can be used to load PNG image files and then attach the image’s memory to a texture. For example:


ShImage image;                  // Declare an image
image.load_PNG("monalisa.png");  // Load a PNG and set up a new memory
// Now, declare a texture
ShTexture2D<ShColor3f> texture(image.width(), image.height());
texture.memory(image.memory()); // And share memory

Like most objects in Sh, memory is reference counted, and so it will stay around as long as the texture is used in an existing shader, even if the instance of the ShImage class used to load the image goes away.


Note: This manual is available as a bound book from AK Peters, including better formatting, in-depth examples, and about 200 pages not available on-line.