What's the difference between TextureResource object compared to Snapshot object?

I admit I don’t follow the blog discussion on new api additions and only just found out about the graphics.newTexture.

I’m a bit confused what’s actually the difference between rendering to a snapshot canvas, compared to rendering to a Texture resource obj? Both seem to be capable of achieving the same thing.

If I want a static background that I wish to render objects to in realtime, I would add an image to a snapshot canvas, then objects will be set so they are discarded to save memory, so theoretically you can render endlessly to the canvas without incurring additional memory usage.

Now creating a texture resource involves the same thing, adding an image to it then placing it into a rect (say the same size as the previously mentioned snapshot), then drawing objects into it. Not sure if the objects rendered into it are discarded or remain.

So, as mentioned, kinda confusing as to how texture resource differs to snapshot object. Or am i missing something?

Basically, the power of the texture resource canvas (or any object build on external bitmaps, for that matter) comes in its ability to impersonate a file, so you can use it to instantiate images (e.g. having multiple copies of your “snapshot”), sprites, etc. Furthermore, you can then update them dynamically (just update and invalidate the underlying texture resource). You can also put them in fills and thereby feed them to composite effects, something not available with snapshots.

On that last point, like any texture these will get fed to a shader, which can decide what to do with them. Since “colors” are really just numbers, you can sneak in all sorts of data this way. For instance, I emulated meshes using this approach, before official support landed: one image was the normal stuff you would see, the other (a texture resource canvas) had the current geometry and such describing how to lay it all out.

Cheers for shedding more light on this :wink:

Another good detail to know is that TextureResources are cached in RAM on the CPU side, while snapshots exists entirely in the GPU’s memory.  The reason why this is important is for the cases where the GPU’s memory gets cleared/dumped during a suspend on Android and WP8 (Windows Phone 8).  In such an event, all textures, shaders, snapshots, etc. are cleared from the GPU’s memory and Corona has to restore everything from RAM when your app gets resumed.  Since snapshots are not cached on the CPU side (they exist entirely in GPU’s memory for best performance), Corona will be unable to recover them.  But TextureResources *are* cached in RAM and can be restored.  I suppose the one downside to this is if your TextureResource is caching a large bitmap, then there are 2 copies of it on the device (GPU memory and CPU side RAM), which would be an issue for low-memory devices such as WP8.

So how do you restore a texture resource upon resuming an app? I tried using invalidating the texture but that doesn’t work.

simplified example of how i would use it below, perhaps I am using it incorrectly?:

obj=display.newGroup()

obj.tex=graphics.newTexture( { type=“canvas”, width=256, height=256} )

obj.bg=display.newImage(“imgBG.jpg”,true);obj.tex:draw(obj.bg)

obj.front=display.newImage(“imgFRONT.jpg”,true);obj.tex:draw(obj.front)

obj.tex:invalidate()

obj.img = display.newImageRect(obj, obj.tex.filename, obj.tex.baseDir, 256, 256)  

Then when I suspend and then resume, I call obj.tex:invalidate() but it doesn’t put back the texture.

@jacques1 You might very well have figured it out by now, but does an input of “cache” give you better results?

See the comments about params here.

Have yet to try using the ‘cache’ method, as i ended up simply reloading the page again upon resuming. Cheers for the help.

Basically, the power of the texture resource canvas (or any object build on external bitmaps, for that matter) comes in its ability to impersonate a file, so you can use it to instantiate images (e.g. having multiple copies of your “snapshot”), sprites, etc. Furthermore, you can then update them dynamically (just update and invalidate the underlying texture resource). You can also put them in fills and thereby feed them to composite effects, something not available with snapshots.

On that last point, like any texture these will get fed to a shader, which can decide what to do with them. Since “colors” are really just numbers, you can sneak in all sorts of data this way. For instance, I emulated meshes using this approach, before official support landed: one image was the normal stuff you would see, the other (a texture resource canvas) had the current geometry and such describing how to lay it all out.

Cheers for shedding more light on this :wink:

Another good detail to know is that TextureResources are cached in RAM on the CPU side, while snapshots exists entirely in the GPU’s memory.  The reason why this is important is for the cases where the GPU’s memory gets cleared/dumped during a suspend on Android and WP8 (Windows Phone 8).  In such an event, all textures, shaders, snapshots, etc. are cleared from the GPU’s memory and Corona has to restore everything from RAM when your app gets resumed.  Since snapshots are not cached on the CPU side (they exist entirely in GPU’s memory for best performance), Corona will be unable to recover them.  But TextureResources *are* cached in RAM and can be restored.  I suppose the one downside to this is if your TextureResource is caching a large bitmap, then there are 2 copies of it on the device (GPU memory and CPU side RAM), which would be an issue for low-memory devices such as WP8.

So how do you restore a texture resource upon resuming an app? I tried using invalidating the texture but that doesn’t work.

simplified example of how i would use it below, perhaps I am using it incorrectly?:

obj=display.newGroup()

obj.tex=graphics.newTexture( { type=“canvas”, width=256, height=256} )

obj.bg=display.newImage(“imgBG.jpg”,true);obj.tex:draw(obj.bg)

obj.front=display.newImage(“imgFRONT.jpg”,true);obj.tex:draw(obj.front)

obj.tex:invalidate()

obj.img = display.newImageRect(obj, obj.tex.filename, obj.tex.baseDir, 256, 256)  

Then when I suspend and then resume, I call obj.tex:invalidate() but it doesn’t put back the texture.

@jacques1 You might very well have figured it out by now, but does an input of “cache” give you better results?

See the comments about params here.

Have yet to try using the ‘cache’ method, as i ended up simply reloading the page again upon resuming. Cheers for the help.