Need to change image each frame

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Need to change image each frame

Post by parallax7d »

I need to generate an image, and render it each frame. This is for a particle simulation with lots of moving pixels.

I put love.graphics.newImage under love.update and while it did reload the image each frame, it was at an unusable framerate.

Would it be possible to load a new image once - then manipulate it in ram directly? That way I can redraw the image each loop without having to call newImage every frame? Or is there a simpler method I'm missing?
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Need to change image each frame

Post by slime »

You can do something like this:

Code: Select all

function love.load()
    imagedata = love.image.newImageData("myimage.png")
    image = love.graphics.newImage(imagedata)
end

function love.update(dt)
    imagedata:mapPixel(mymapfunction)

    -- Updates the image's contents in VRAM with the ImageData's latest changes.
    image:refresh()
end

function love.draw()
    love.graphics.draw(image)
end
But CPU-side pixel modifications will always be slow. I recommend drawing to a Canvas instead, if possible. It will be orders of magnitude faster.
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: Need to change image each frame

Post by parallax7d »

Unfortunately for my needs that method would be too slow like you said. The image:refresh() method looks promising though! It seems to be working at least 100x faster than a full graphics.newImage call. thanks!
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: Need to change image each frame

Post by parallax7d »

Is there any way for love to display an image from a memory region of my choosing? I'm assuming it's caching a copy on it's own? I would rather it just reference mine directly if possible.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Need to change image each frame

Post by slime »

Images live in video memory and are owned by OpenGL / the video driver. The driver will often arrange the contents of the image in VRAM such that its data is tiled rather than linear, for more efficient drawing.
Using a CPU-side linear memory directly as an OpenGL texture generally isn't possible.

As an aside, you can use this drop-in script to make ImageData methods more efficient without changing your own code: https://github.com/slime73/love-snippet ... ta-ffi.lua
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: Need to change image each frame

Post by parallax7d »

Just to clarify, so when we use love.graphics.newImage, is it not caching anything into system ram, it's uploading directly to the vram?
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Need to change image each frame

Post by slime »

Right - the love.graphics Image creates and owns a reference to a texture in VRAM. It also keeps a reference to the ImageData which was used to create the Image (ImageData objects reside purely in RAM.)

When you call Image:refresh, it re-uploads the ImageData's contents in RAM to the texture in VRAM.

Keep in mind OpenGL (and other graphics APIs) abstract away the notion of VRAM, since many GPUs don't even have dedicated VRAM. love.graphics.newImage just tells OpenGL to create a texture using the contents of the ImageData, and it doesn't really know where the OpenGL texture will be stored (but the graphics driver will put it in VRAM if possible.)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], slime and 210 guests