Love2D 11.1 Multiple Canvases void effects

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
AxisAngles
Prole
Posts: 12
Joined: Mon Feb 22, 2016 9:02 am

Love2D 11.1 Multiple Canvases void effects

Post by AxisAngles »

I am trying to do some deferred rendering stuff and I need to write to multiple canvases at the same time.
I saw that you can do

Code: Select all

love.graphics.setCanvas(canvas1, canvas2)
and to test, I copied the example code from https://love2d.org/wiki/love.graphics.newShader

Code: Select all

local pixelcode = [[
void effects(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
{
    // love_Canvases is a writable array of vec4 colors. Each index corresponds to a Canvas.
    // IMPORTANT: If you don't assign a value to all active canvases, bad things will happen.
    love_Canvases[0] = color;
    love_Canvases[1] = color + vec4(0.5);
    // etc.
}
]]

local testshader = love.graphics.newShader(pixelcode)
local w, h = love.graphics.getDimensions()

local canvas0 = love.graphics.newCanvas(w, h)
local canvas1 = love.graphics.newCanvas(w, h)



function love.draw()
	love.graphics.setCanvas(canvas0, canvas1)
	love.graphics.setShader(testshader)
	love.graphics.setColor(255, 0, 0, 255)
	love.graphics.polygon("fill", 100, 100, 200, 100, 200, 200, 100, 200)


	love.graphics.setCanvas()
	love.graphics.setShader()
	love.graphics.draw(canvas0)
end
And it draws a red square just how it should in love-0.10.2-win64
But when I run it in love-11.1.0-win64 and up, it throws me this error:

Code: Select all

Error

main.lua:12: bad argument #1 to 'newShader' (missing 'position' or 'effect' function?)


Traceback

[C]: in function 'newShader'
main.lua:12: in main chunk
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
Am I doing something wrong?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Love2D 11.1 Multiple Canvases void effects

Post by grump »

The signature of the effects function has changed with LÖVE 11.
Removed the 'void effects(...)' pixel shader entry point. Use the new 'void effect()' instead.

Code: Select all

void effect() {
...
}
AxisAngles
Prole
Posts: 12
Joined: Mon Feb 22, 2016 9:02 am

Re: Love2D 11.1 Multiple Canvases void effects

Post by AxisAngles »

I see, thank you. I tried void effect, but not without the arguments. Is there a way to get the pixel position? I think I can pipe in and do everything else custom.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Love2D 11.1 Multiple Canvases void effects

Post by grump »

Documentation is lacking in this regard. It has not been updated for 11 yet.
You can use love_PixelCoord:

Code: Select all

local gfx = love.graphics
local tex = gfx.newImage('texture.png')

local cvs1, cvs2 = gfx.newCanvas(256, 256), gfx.newCanvas(256, 256)
local shader = gfx.newShader([[
	uniform Image MainTex;

	void effect() {
		love_Canvases[0] = VaryingColor * Texel(MainTex, VaryingTexCoord.xy);
		love_Canvases[1] = vec4(love_PixelCoord.xy * 1 / 256, 1, 1);
	}
]])

function love.draw()
	gfx.setCanvas(cvs1, cvs2)
	gfx.setShader(shader)
	gfx.draw(tex)
	gfx.setCanvas()
	gfx.setShader()
	gfx.draw(cvs1)
	gfx.draw(cvs2, 0, 256)
end
I'm not sure this is the correct and best way to do this, but it works.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 9 guests