Fast getPixel on canvas

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
Sirozz
Prole
Posts: 2
Joined: Mon Aug 06, 2018 12:35 pm

Fast getPixel on canvas

Post by Sirozz »

I'd like to get the color value of pixels on my canvas in real time.

I was able to get very fast results using a FFI pointer on ImageData but I really need to use a canvas because I don't want to reimplement all the graphical functions of Love: rectangle, circle etc.

I tried with a more standard solution: generating an imageData object from the canvas and using the getPixel method but it's impossible in my case as I need to get/set a lot of pixels per frame. I tried with a dirty flag to avoid generating the imageData on each frame and to speed up things a bit but it's just poor code optimisation.

Do you know of any methods to get a pointer on the video memory or a canvas directly?

Or more precisely, is it possible to get the color value of a pixel on the screen without generating an imageData object, which is so slow? Is there a way to keep the link between canvas/imageData and to refresh data after some drawings?

Thank you happy lovers!
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Fast getPixel on canvas

Post by pgimeno »

Sirozz wrote: Mon Aug 06, 2018 1:05 pm Do you know of any methods to get a pointer on the video memory or a canvas directly?
https://stackoverflow.com/questions/306 ... res-pixels
Sirozz wrote: Mon Aug 06, 2018 1:05 pm Or more precisely, is it possible to get the color value of a pixel on the screen without generating an imageData object, which is so slow? Is there a way to keep the link between canvas/imageData and to refresh data after some drawings?
I think that approach is not going to succeed, because of how modern GPU hardware works.

Think of the CPU and the GPU as two independent computers connected through a network. The GPU has its own RAM. The CPU can upload images to the GPU through the network, but that's not a very fast process; however, once they're in the GPU's RAM, the GPU can draw them very quickly. The CPU can also send drawing commands through the network so that the GPU interprets them, including commands to draw said images. The GPU enqueues these commands and processes them at its own pace, not necessarily as sent. However, when you request to capture the screen, first the command queue needs to be flushed by executing all pending commands, and then the whole image needs to be sent through the network. That's of course a slow process, and you can't rely on being able to do it several times in the same frame.

Given these limitations, in your case the best approach is probably to implement some drawing primitives that work on ImageData objects. Perhaps there are other alternatives, but without an idea of what you're trying to achieve, it's hard to give any better advice.

The drawing primitives approach has been used by at least one person, see viewtopic.php?t=85013
Sirozz
Prole
Posts: 2
Joined: Mon Aug 06, 2018 12:35 pm

Re: Fast getPixel on canvas

Post by Sirozz »

Thank your for this detailed response and the links :)

I've created a new fantasy console and it's almost done now. I was trying to reproduce some get/set pixel functionality with poke/peek in vram. My first implemetation was based on ffi pointers but I was stuck with color keys and map transparency so I decided to rewrite everything using canvas.

But it sounds like I will have to rewrite everything again...
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests