Canvas, ImageData and separate Thread

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
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Canvas, ImageData and separate Thread

Post by Sasha264 »

Hello everyone! :crazy:

I have small canvas that filled up with few valuable pixels. It is not! updating every frame, instead in updates rarely. And i want to copy its content to the cpu memory, like Canvas:newImageData() does, but without blocking my main drawing thread! Is here a way to do this in a separate thread? Delay of this operation is ok, for example if data becames available in 10? 100? frames it will be ok.

Yes, I understand that Canvas:newImageData() does not designed to be called every frame, that it is causes main render loop to pause, etc...

I looked at this libs https://love2d.org/wiki/love-loader and https://love2d.org/wiki/Lily, they are able to load images in separate thread, but as far as I understand they only parallelize hdd -> cpu memory operation + decode operation, but not the cpu memory -> gpu memory operation. Is this correct?

I just looking for ANY way to copy SMALL amount of data from gpu memory to cpu memory WITHOUT huge increase of single frame time. Is here a way to do this? Looking for this for a few days and found just nothing :cry:
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Canvas, ImageData and separate Thread

Post by pgimeno »

As far as I know, all OpenGL calls must happen in the same thread. If I'm correct on that, it means that every data transfer needs to happen in the thread that performs the drawing. This StackOverflow answer suggests that you would gain nothing anyway, because the CPU threads have nothing to do with the GPU threads (or more precisely, with the way the GPU handles parallelism). You wouldn't have one GPU core, so to say, dedicated to rendering that canvas, which is what you seem to want.

That doesn't mean the performance must be bad. I use pixel-perfect collisions in T2R with that very technique, and the speed is quite acceptable even in a RPi. By the time I wrote it, I didn't pay attention to when I handle collisions, because I knew less about OGL parallelism than I know now, but I'm pretty convinced that if you perform the capture either at the very end or at the very start of the frame, without any drawing between the capture and the presentation, you'll get a negligible performance hit.
User avatar
Sasha264
Party member
Posts: 131
Joined: Mon Sep 08, 2014 7:57 am

Re: Canvas, ImageData and separate Thread

Post by Sasha264 »

@pgimeno, thanks a lot for the responce! :awesome:
Yes, I suspect that maybe it is not possible at all... But after playing modern 3d games noticed that the common feature is to draw game with low resolution textures and instantly change it to high resolution after they are loaded. What made me think that copying cpu memory <-> gpu memory in a background is somehow possible... :ehem:

About the capture either at the very end or at the very start of the frame: I've tried to move the capture code and/or the drawing code around the whole drawing sequence, start, end, middle, shuffling it, creating a queue, that contains N canvases with valuable pixels... With no visible result. Without Canvas:newImageData() it runs about ~700 fps, with it on every frame it drops to ~300 fps. If the copying performed on every 10 frame, then this frame time increases accordingly. :ultrashocked:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 88 guests