Love2D 11.1 Instance Arrays [SOLVED]

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
_DaCoolOne_
Prole
Posts: 16
Joined: Sat Mar 09, 2019 3:46 am

Love2D 11.1 Instance Arrays [SOLVED]

Post by _DaCoolOne_ »

Hello all!

I've been coding with Love2D ever since a friend recommended it to me, and I have to say I'm hooked! Lua + Love2D is a great combination!

I've started work on a 3D engine using some of the new features added in Love 11. I have a pretty good grasp of shaders, transform matrices, depth-buffers, etc, so I've gotten the basic engine down at this point. As a benchmark test, I rendered a large number of the same object successfully, but I wanted more.

Enter instance arrays.

https://love2d.org/wiki/love.graphics.drawInstanced

One of the features added to Love2D in version 11 according to the wiki is instance arrays. The basic idea is that instead of having a uniform value that you pass to the shader several times, you send all of your transforms once and then draw the mesh using one draw call. Sweet!

However, I found the documentation on this feature to be somewhat... well... lacking.

So, I'll cut to the chase. I have no idea how to get drawInstanced to draw objects in with different properties. The wiki says some vague things about using "love_InstanceID" in the vertex shader (which is not a listed vertex shader variable https://love2d.org/wiki/Shader_Variables)) and about attaching attributes via "a new Mesh:attachAttribute variant." (Which I have no idea what that means. Only one variant is listed, and it's been there since 0.10...)

What I've tried:
  • Creating a transform array in the vertex shader and referencing it with love_InstanceID (shader fails and says love_InstanceID is not declared, and it only indexes the first index of the array if I manually declare love_InstanceID)
  • Creating a mesh with the transforms and attaching it to the already existing mesh with Mesh:attachAttribute (Renders really weird shapes. I think that Mesh:attachAttribute doesn't work the way I think it does)
  • Reading the love2D wiki until my eyes started to bleed (Couldn't find anything, and now I'm half-blind)
  • Scouring the web for examples of people using instance arrays with Love2D (I couldn't find anything)
I haven't yet tried creating a second mesh and then creating a custom vertex map that references the transforms for each vertex (e.g. A vertex map of {1, 1, 1, 1, 2, 2, 2, 2} referencing transforms and attaching it to a four-vertex mesh) but this just sounds ridiculously inefficient and counter-intuitive. However, maybe I am desperate enough. :?

I have create a really simplified version of what I'm trying to accomplish. This demo is supposed to draw 1000 little 10x10 squares. There are two files besides main.lua and conf.lua (and the stone texture :P). One is called InstanceA.lua, the other is InstanceB.lua. InstanceA.lua uses love_InstanceID to attempt to draw the textures, InstanceB.lua uses Mesh:attachAttribute I have attached two .love files, however the only difference is that the first requires InstanceA.lua, and the second requires InstanceB.lua. You only need to download one if you are willing to unzip the file and change one character in main.lua.

Code: Select all

local Inst = require 'InstanceA' -- FileA
local Inst = require 'InstanceB' -- FileB
Also, a quick note about how I make object-oriented stuff in lua. "class" is a table that stores the static methods, "Instance" stores the non-static methods. "class()" is the constructor. The file returns "class"

Has anyone else gotten this to work? If so, how? Am I missing something obvious?

P.S. Please excuse any inefficiencies in the code. I literally just slapped together the bare minimum functions to show where I'm at.
Attachments
InstanceAttemptB.love
This is the version that uses Mesh:attachAttribute()
(3.08 KiB) Downloaded 75 times
InstanceAttemptA.love
This is the version that uses love_InstanceID
(3.08 KiB) Downloaded 78 times
Last edited by _DaCoolOne_ on Sun Mar 10, 2019 12:02 am, edited 1 time in total.

Code: Select all

function not(val)
	if val == true then
		return false
	else
		return true
	end
end
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Love2D 11.1 Instance Arrays

Post by zorg »

Hello and welcome to the forums!

Silly thing i noticed:
love.graphics.drawInstanced
... or the love_InstanceID GLSL 3 vertex shader variable is used,...
...Instancing is not supported by some older GPUs that are only capable of using OpenGL ES 2 or OpenGL 2...
GraphicsFeature
glsl3 and instancing are supported on OpenGL 3 and OpenGL ES 3-capable systems.

Code: Select all

local t = love.graphics.getSupported( ); if t.glsl3 and t.instancing then error('it should work! :3') end
That said, i'm not sure whether you explicitly need to do a pragma at the top of your shader code for löve to use that shader lang version or not; iirc it may just exist to force a lower version...

Code: Select all

#pragma language glsl3
//rest of shader code here
Reference: this thread: viewtopic.php?f=4&t=86439&p=226183&hilit=pragma#p226183
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
_DaCoolOne_
Prole
Posts: 16
Joined: Sat Mar 09, 2019 3:46 am

Re: Love2D 11.1 Instance Arrays

Post by _DaCoolOne_ »

Thank you for your welcome to the forums! :D

I did notice the part about needing to use openGL 3, but I just assumed because it was supported that Love2D would use it.

Turns out, I was wrong!

I added the "#pragma language glsl3" line to the beginning of the pixel and vertex shader code, and now it allows me to use love_instanceID inside of the shader. So that's all good now!

I'll leave the working code here as a reference in case anyone else has this problem. Thanks so much Zorg!
Attachments
InstancingExampleWorking.love
(3.09 KiB) Downloaded 121 times

Code: Select all

function not(val)
	if val == true then
		return false
	else
		return true
	end
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Google [Bot], lenlenlL6 and 48 guests