Page 1 of 1

Love2D 11.1 Multiple Canvases void effects

Posted: Thu Aug 30, 2018 12:39 am
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?

Re: Love2D 11.1 Multiple Canvases void effects

Posted: Thu Aug 30, 2018 6:05 am
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() {
...
}

Re: Love2D 11.1 Multiple Canvases void effects

Posted: Thu Aug 30, 2018 7:57 am
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.

Re: Love2D 11.1 Multiple Canvases void effects

Posted: Thu Aug 30, 2018 11:42 am
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.