Page 1 of 1

Can threads use shaders?

Posted: Tue Mar 01, 2016 9:05 am
by Luke100000
Hello,

When I run my pixelshader in the main thread, it works fine, but when I run it in a separate thread created by love.thread.newThread() it crashes with this message: "Your GPU does not support shaders". (or something like that)
I enabled the love.graphics module.

Any ideas?

Re: Can threads use shaders?

Posted: Tue Mar 01, 2016 9:32 am
by slime
love.graphics can only be used in the main thread (the [wiki]love.thread[/wiki] wiki page mentions this as well).

"Using" a shader in a different thread wouldn't do much anyway, since shaders are actually bits of code that are run on your GPU, not your CPU.

Re: Can threads use shaders?

Posted: Tue Mar 01, 2016 9:52 am
by Luke100000
slime wrote:love.graphics can only be used in the main thread (the [wiki]love.thread[/wiki] wiki page mentions this as well).

"Using" a shader in a different thread wouldn't do much anyway, since shaders are actually bits of code that are run on your GPU, not your CPU.
Well, my plan was to render a texture. Since this would not draw the texture direct on the screen it should go (I think). About half of the time my code needs to load, prepare and save the texture. I wanted to short this time by using threads and in addition the main thread should not lag to much.

Re: Can threads use shaders?

Posted: Tue Mar 01, 2016 11:13 am
by farzher
I'm going to throw a related question in here.

Is it possible to just load a shader in another thread? I assume no.
It's annoying that making all my lg.newShader() and lg.newCanvas() freezes my game for a bit, and I can't put it into my load thread

Re: Can threads use shaders?

Posted: Tue Mar 01, 2016 12:56 pm
by Luke100000
farzher wrote:I'm going to throw a related question in here.

Is it possible to just load a shader in another thread? I assume no.
It's annoying that making all my lg.newShader() and lg.newCanvas() freezes my game for a bit, and I can't put it into my load thread
You can create Canvases, draw on them or do whatever you want, but shader wont work. That's what I found out. lg.present() does not work.
nope, I was totally wrong, switched it with love.image :death:

Re: Can threads use shaders?

Posted: Tue Mar 01, 2016 5:08 pm
by slime
You cannot do any love.graphics operation or use any love.graphics class in any thread other than the main one. love will hard-crash... or worse, if you try.

The big warning on the [wiki]love.thread[/wiki] wiki page is there for a reason!

Re: Can threads use shaders?

Posted: Tue Mar 01, 2016 5:47 pm
by T-Bone
Luke100000 wrote:
slime wrote:love.graphics can only be used in the main thread (the [wiki]love.thread[/wiki] wiki page mentions this as well).

"Using" a shader in a different thread wouldn't do much anyway, since shaders are actually bits of code that are run on your GPU, not your CPU.
Well, my plan was to render a texture. Since this would not draw the texture direct on the screen it should go (I think). About half of the time my code needs to load, prepare and save the texture. I wanted to short this time by using threads and in addition the main thread should not lag to much.
What do you mean by "about half the time"? If you mean "about every other frame", then it sounds like you need to really need to rethink your rendering structure, to make it faster. There isn't much point in rendering stuff to textures if their contents change often.

Re: Can threads use shaders?

Posted: Tue Mar 01, 2016 8:21 pm
by Luke100000
T-Bone wrote:
Luke100000 wrote:
slime wrote:love.graphics can only be used in the main thread (the [wiki]love.thread[/wiki] wiki page mentions this as well).

"Using" a shader in a different thread wouldn't do much anyway, since shaders are actually bits of code that are run on your GPU, not your CPU.
Well, my plan was to render a texture. Since this would not draw the texture direct on the screen it should go (I think). About half of the time my code needs to load, prepare and save the texture. I wanted to short this time by using threads and in addition the main thread should not lag to much.
What do you mean by "about half the time"? If you mean "about every other frame", then it sounds like you need to really need to rethink your rendering structure, to make it faster. There isn't much point in rendering stuff to textures if their contents change often.
I'm working on a library for my game which renders textures, you can download my program here:
pixel.love
(3.29 KiB) Downloaded 179 times
Simple click the button left bottom, put a random small image (64*64px) into the folder and press 'pixel'
Because this can run in the background, it should be threaded.
You cannot do any love.graphics operation or use any love.graphics class in any thread other than the main one. love will hard-crash... or worse, if you try.
AH I found my issues, I meant love.image, not the graphics module.
trAInsported uses this methode I think.

Re: Can threads use shaders?

Posted: Wed Mar 02, 2016 8:54 am
by T-Bone
I can't test your game right now (my computer is currently disconnected from the internet) and I'm having a hard time understand exactly what you're trying to achieve... But the only way I know of to make images in the background is by manually setting pixel values to ImageData. That allows you to implement shader-like code (in Lua) that runs on the background. It's typically very slow.

I also don't understand this sentence:
"Because this can run in the background, it should be threaded." Just because something can run in the background doesn't necessarily mean it should. It might be that you can make it fast enough by utilizing the GPU (which in turn does run things "in the background", in a way). So even though the GPU can only be accessed from the main thread, it could still be faster than running stuff from a background thread (or not, it totally depends on what you're trying to do).