Shaders and ints

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.
User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Shaders and ints

Post by RedHot »

I have been fiddling with ints and Love2d shaders (aka pixel effects)


The following code seems alright to me, but produces an error

Code: Select all

function love.load()
    effect = love.graphics.newPixelEffect [[
        extern int time;
        vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
        {
			return vec4(float(time)/2000.f , float(time)/100.f , 0.5f , 0.3f);
           // return vec4((1.0+sin(time))/2.0, abs(cos(time)), abs(sin(time)), 1.0);
        }
    ]]
end

function love.draw()
    -- boring white
    love.graphics.setPixelEffect()
    love.graphics.rectangle('fill', 10,10,790,285)

    -- LOOK AT THE PRETTY COLORS!
    love.graphics.setPixelEffect(effect)
    love.graphics.rectangle('fill', 10,305,790,285)
end

local t = 0
function love.update(dt)
    t = t + dt
	t= math.floor(t*100)
    effect:send("time", t)
end
And the error itself :
graphics.lua:1339 Incalid operation
- Trying to send the wrong value type to shader variable, or
- Trying to send array values with wrong dimension, or
- invalid variable name
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Shaders and ints

Post by slime »

LÖVE 0.8.0 doesn't support extern ints in shaders. Lua can't distinguish between an integral number that should be a float, and one that should be an int, so it always tries to send all Lua numbers as floats. You could just change the "extern int" to "extern float" and it should work the same, as long as you're sending integral numbers.

In LÖVE 0.9.0 you will be able to use the Shader:sendInt method if you need to.
User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: Shaders and ints

Post by RedHot »

FML...

Thank you kind sir for explaining it to me :) I have decided to use ints as I wanted to acces an array element.

I am also trying to use an internal array of vec2d (500 elements). Is love going to give me problems?
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Shaders and ints

Post by slime »

RedHot wrote:Thank you kind sir for explaining it to me :) I have decided to use ints as I wanted to acces an array element.
I think you should just be able to cast the extern float to an int inside the shader, like:

Code: Select all

extern float index;
[...]

vec4 effect(vec4 vcolor, Image texture, vec2 texcoord, vec2 pixcoord)
{
   [...]
   int arrayIndex = int(index);
   [...]
}
RedHot wrote:I am also trying to use an internal array of vec2d (500 elements). Is love going to give me problems?
Maybe. See here: viewtopic.php?f=4&t=33884#p104254

Also note that accessing large arrays in a shader might cause some performance issues, but that's probably not something to worry about until it becomes a real problem.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Shaders and ints

Post by T-Bone »

I guess it would be more correct/clear to use "extern number"? That way, you know that you're referring to a Lua number. It won't make any practical difference, but I find it easier to read.
User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: Shaders and ints

Post by RedHot »

The last thing I am struggling with is an error when compiling a shader. The following code produces an error

Code: Select all

function love.load()
	effect = love.graphics.newPixelEffect [[
		extern number maperx;
		//extern number blocksize;
        //extern vec2 mass[512];

        vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
        {
			number test = maperx *2.0f;
			return vec4(1.0,1.0,1.0,1.0);
        }
    ]]
end
It says that the variable maperx is either nonexistant or never used. I need to implicitly use the "maperx" in the return function for the code to work properly. I do realise that shaders tend to ommit unused variables but this is quite weird.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Shaders and ints

Post by T-Bone »

RedHot wrote:The last thing I am struggling with is an error when compiling a shader. The following code produces an error

Code: Select all

function love.load()
	effect = love.graphics.newPixelEffect [[
		extern number maperx;
		//extern number blocksize;
        //extern vec2 mass[512];

        vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
        {
			number test = maperx *2.0f;
			return vec4(1.0,1.0,1.0,1.0);
        }
    ]]
end
It says that the variable maperx is either nonexistant or never used. I need to implicitly use the "maperx" in the return function for the code to work properly. I do realise that shaders tend to ommit unused variables but this is quite weird.
Is there a point in calculating "test"? If not, can't you just comment it out? Or what kind of solution are you looking for?
User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: Shaders and ints

Post by RedHot »

I just want to make sure that once my shader code gets big I won't have to spend a couple of hours figuring what's wrong while one of my variables has been thrown away just because. I need to make sure all the things I introduce stay there.
User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: Shaders and ints

Post by RedHot »

Ok double posting and bumping :awesome:

I have managed to get some effects out of the cumbersome Love2d ;) (no srsly, the bugs are killing me when I want to do something more advanced).

I diagnosed an issue that I find hard to solve.

My draw function consists of a loop and with each cycle I send an array of data to the shader. The problem is a black blining square on the screen. The problem is visible whenever I send data in the loop function.

Here you can see the problem ~0:02 . http://www.youtube.com/watch?v=k4MDLMbtEZw

I tried to used canvas as I thought there might have been a problem with double/triple buffering. It changed nothing. Whenever I send data to the shader in a loop within the draw function I get the horrible results.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Shaders and ints

Post by slime »

Without seeing any of your code it's pretty impossible to say what the issue is.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 30 guests