Page 1 of 2

gpu particle simulation

Posted: Mon Feb 29, 2016 5:00 am
by jonbro
ink dr_p

I threw together a little GPU based particle system in love2d this weekend. All simulation and display happens in shaders, so it is fast as heck, and displaying 262k particles.

Image

There are a few strange implementation details, but hopefully the code is pretty readable. You can check it out on github, along with a few notes on how it all works.

Re: gpu particle simulation

Posted: Mon Feb 29, 2016 11:11 am
by skyHights
This looks amazing, I'm going to check it out and look through the code.

Re: gpu particle simulation

Posted: Mon Mar 07, 2016 6:15 pm
by alberto_lara
This looks pretty cool, is there any interaction with the window or is just a random behavior?

Re: gpu particle simulation

Posted: Tue Mar 08, 2016 2:02 am
by Skeiks
JonBro! How's it going? This is a really cool particle sim, I'm going to look through the code later. I can't wait to see what you make using Love2D, I really enjoy developing with it.

Re: gpu particle simulation

Posted: Wed Mar 09, 2016 6:28 am
by pgimeno
I only see a dot moving slowly with a black background and clean cut borders. Not sure why.

Image

Re: gpu particle simulation

Posted: Wed Mar 09, 2016 9:01 pm
by jonbro
pgimeno wrote:I only see a dot moving slowly with a black background and clean cut borders. Not sure why.
there is

Code: Select all

local debug = false
up at the top of the code... if you turn that on, what kind of output do you get? Also, are you on 0.10? Also, this requires both shader and canvas support, and I don't know how widespread that is.
alberto_lara wrote:This looks pretty cool, is there any interaction with the window or is just a random behavior?
In this version it is just using curl noise to move the particles around. I have attached a version that has the particles influenced by the mouse position at the beginning of their life, and then noise at the end. Give this a second to warm up, because the particles fade in at the beginning of their life.
particles_with_mouse.love
(168.82 KiB) Downloaded 226 times
This version also is doing sorting on the particles, so older ones are drawn closer to the camera.
particles_no_alpha.love
(172.01 KiB) Downloaded 198 times
this one has the alpha turned off, so you can see the sorting behavior better. I also swapped in a particle with a little dropshadow.
Screen Shot 2016-03-09 at 1.08.00 PM.png
Screen Shot 2016-03-09 at 1.08.00 PM.png (649.18 KiB) Viewed 6410 times

Re: gpu particle simulation

Posted: Thu Mar 10, 2016 2:39 am
by pgimeno
jonbro wrote:
pgimeno wrote:I only see a dot moving slowly with a black background and clean cut borders. Not sure why.
there is

Code: Select all

local debug = false
up at the top of the code... if you turn that on, what kind of output do you get? Also, are you on 0.10? Also, this requires both shader and canvas support, and I don't know how widespread that is.
I get this when enabling debug:
Image

It eventually fills the screen.

Yes, I'm on 0.10.1, and yes, my GC supports shaders and canvases. I've used both myself. OpenGL version supported is 3.3.0. My driver is admittedly old (nVidia 334.21, while the newest is 361.28), but it hasn't broken this bad so far.

EDIT: Just to ensure that the problem is not related to combining multicanvas and shaders, I've made a quick test. It worked as expected.

Code: Select all

local shader = love.graphics.newShader[[
    void effects( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
    {
        love_Canvases[0] = vec4(1.0, 0.5, 0.0, 1.0);
        love_Canvases[1] = vec4(0.0, 0.5, 1.0, 1.0);
    }
]]

local c1, c2 = love.graphics.newCanvas(), love.graphics.newCanvas()

function love.draw()
  love.graphics.setCanvas(c1, c2)
  love.graphics.setShader(shader)
  local sx, sy = love.graphics.getDimensions()
  love.graphics.circle("fill", sx/2, sy/2, sx/2)
  love.graphics.setCanvas()
  love.graphics.setShader()
  love.graphics.draw(c1, -sx/2, 0)
  love.graphics.draw(c2, sx/2, 0)
end
Image

Re: gpu particle simulation

Posted: Thu Mar 10, 2016 5:49 pm
by jonbro
huh, that is super strange. Like the data for the particles is there, but it seems it is failing to read the position texture in the render texture, or actually, it is reading only one position rather than getting the particle data lookup. I suspect I am doing some math in the glsl that is failing on your card. Specifically, the line that calculates texture coords in render_vert.glsl:

Code: Select all

vec4 lookup = Texel(posTexture, vec2(mod(SpriteCount,particleCountSQRT)*1.0/particleCountSQRT+1/512.0, floor(SpriteCount/particleCountSQRT)/particleCountSQRT+1/512.0));
The alternate issue is that something about the mesh creation code is jacked, and it isn't storing the correct data, but that feels less likely to me...

Re: gpu particle simulation

Posted: Thu Mar 10, 2016 7:02 pm
by Skeiks
I attempted to run this on my home PC and I received an error. I don't have the message handy but it mentioned that you have an unused variable in one of your shaders. It might just be a quirk of my specific video card but I've found that externs and variables that don't an impact the final pixel throw errors. When I get home I'll try it again and post the error if I still receive it. Maybe that's related to the weirdness pgimeno is getting?

On my work laptop though it runs fine.

Re: gpu particle simulation

Posted: Thu Mar 10, 2016 10:44 pm
by Ref
Windows 7, canvases and shaders are fine, Love 10.1 - get blank white screen.