Can threads use shaders?

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
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Can threads use shaders?

Post 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?
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Can threads use shaders?

Post 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.
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Re: Can threads use shaders?

Post 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.
User avatar
farzher
Prole
Posts: 41
Joined: Fri Jul 31, 2015 5:38 pm

Re: Can threads use shaders?

Post 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
butts
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Re: Can threads use shaders?

Post 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:
Last edited by Luke100000 on Tue Mar 01, 2016 8:24 pm, edited 1 time in total.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Can threads use shaders?

Post 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!
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Can threads use shaders?

Post 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.
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Re: Can threads use shaders?

Post 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 162 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.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Can threads use shaders?

Post 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).
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 80 guests