Login | Register 
FEATURES
APPLICATIONS
DOWNLOADS
EDUCATION
BLOG
WIKI
FORUM
STORE

2d fluid simulation on the GPU

Helpful techniques for working with TouchDesigner. Answers only, no questions.

2d fluid simulation on the GPU

Postby kurtoon » Wed Nov 02, 2016 12:21 am

Here is a simple 2d fluid solver I made as part of my masters thesis project. It was originally written in CUDA but has been moved to GLSL. The repo is here with some more information:

https://github.com/kamindustries/touchFluid

Let me know what you think!
Attachments
touchFluid.toe
(11.19 KiB) Downloaded 1116 times
kurtoon
 
Posts: 34
Joined: Fri Feb 26, 2010 11:47 pm
Location: Los Angeles

Re: 2d fluid simulation on the GPU

Postby DavidBraun » Wed Nov 02, 2016 11:27 am

This is excellent. Thanks for sharing. Here are some of my comments.

In the buoyancy shader, it seems like the direction of the buoyancy should be turned into a texture lookup rather than a constant.
Code: Select all
vec2 buoy = (dt * (densTemp.b - ambTemp) * sigma - densTemp.g * kappa ) * vec2(0.01) * vec2(0., 1.);

That vec2(0., 1.) could come from a texture. Then we could make generative buoyancy maps and see some really interesting fluids.

You have "res" as a uniform on a lot of shaders. This generally isn't necessary since you can refer to the input resolution and texel size with uTD2DInfos[0].res

Using expressions on GLSL TOPs can slow them down quite a bit, although this project runs at 60fps. Using a Parameter CHOP and exports would be more efficient at the risk of losing some exports once in a while.

Also, I'm curious since this project is packed full of features. Is there any fluid simulation concept that you haven't included?
DavidBraun
 
Posts: 343
Joined: Wed Feb 25, 2015 1:35 pm
Location: Chicago

Re: 2d fluid simulation on the GPU

Postby kurtoon » Wed Nov 02, 2016 2:32 pm

Hey David, thanks for the suggestions.

Glad to see you saw the buoyancy direction was hard coded. In making this, I had to make some choices that balanced simplicity and features, and for that one I chose simplicity. Actually, nearly every parameter can be spatialized with a texture: diffusion rate, curl amount, even delta time. You can go nuts. The nice thing with this version, as opposed to CUDA, is that it's easily hackable which I encourage everyone to do.

The resolution parameter is definitely something I want to change. There were a lot of people having some major issues with the CUDA version and I wanted to push this out ASAP. I'll move to using the built in uniforms. Just FYI, when changing the resolution, the Feedback TOPs need to be reset.

I didn't realize using expressions in GLSL TOPs can be slow. Is there a reason for this? Does it apply to expressions on the resolution/Custom tab parameters or just everything, including uniforms? Honestly I'm not a Touch ninja and don't really understand exports that well, so I use expressions all the time.

CFD is a huge topic. There are tons of things you can do. Some examples off the top of my head are free surface boundaries (non-solid boundaries, i.e. water vs air), combustion/chemical mixing, multiphase flow, and more accurate advection schemes such as the MacCormack method. You could also go in another direction entirely with SPH. There are many, many more phenomena out there. SIGGRAPH is always a big inspiration in that regard.
kurtoon
 
Posts: 34
Joined: Fri Feb 26, 2010 11:47 pm
Location: Los Angeles

Re: 2d fluid simulation on the GPU

Postby raganmd » Wed Nov 02, 2016 3:42 pm

This is great, and many thanks for sharing!!

Regarding exports:

Expressions essentially end up getting evaluated every frame that a given op cooks, which is what ends up being costly.

Exports end up being more like pointers and don't need to be evaluated.

You can see a whole breakdown of the various ways you can reference / export and their respective cooktimes here:

https://github.com/raganmd/td_fb_forum_examples/blob/master/example_explorer/tox_files/base_exploring_cooktimes.tox

Long story short, expressions tend to be about 5x as expensive, though that's always subject to their complexity.

Generally speaking, I tend to use exports on ops that cook frequently or every frame, and expressions when ops cook infrequently - though to be honest it's frequently project and resource specific.

Ideologically, exports are set at the source and push to the destination; expressions are set at the destination and pull from the source.
raganmd
 
Posts: 1380
Joined: Thu Mar 28, 2013 5:21 am
Location: San Francisco, CA

Re: 2d fluid simulation on the GPU

Postby DavidBraun » Mon Nov 07, 2016 3:36 pm

There's also a really good fluid sim project in Java here: https://github.com/diwi/PixelFlow/tree/master/src/com/thomasdiewald/pixelflow/glsl/Fluid (MIT license)

It has some major savings in its jacobi shader https://github.com/diwi/PixelFlow/blob/ ... acobi.frag
because it precomputes the obstacle in a single texture
https://github.com/diwi/PixelFlow/blob/ ... ounds.frag

When you get rid of the python expressions and improve the shader this way, it's a lot faster :)
DavidBraun
 
Posts: 343
Joined: Wed Feb 25, 2015 1:35 pm
Location: Chicago

Re: 2d fluid simulation on the GPU

Postby kurtoon » Fri Nov 18, 2016 10:05 pm

Thanks so much for the suggestions Matthew and David! I updated the repo with some of the changes you guys mentioned.

Matthew, that cooktimes tox is really cool and well put together, thanks for sharing. I removed all Python expressions on TOPs and switched to using exports. If I missed anything please let me know. I also removed the resolution parameter entirely- it's now just based on the input resolution of the velocity/obstacle texture. If that changes, you still need to hit the Reset pulse to clear the Feedback TOPs.

Thanks for the heads up about PixelFlow David. Precomputing the obstacle neighbors is a super smart idea. I rolled that into touchFluid and linked to PixelFlow in the obstacleBounds.frag shader.
kurtoon
 
Posts: 34
Joined: Fri Feb 26, 2010 11:47 pm
Location: Los Angeles

Re: 2d fluid simulation on the GPU

Postby sssslamin » Tue Aug 22, 2017 2:33 pm

hi, any tips on how to change the resolution of this
i tried just changing the values in the constant and resetting but the fluid dosent move well in the area beyond 1280x720 in this snip you can see the fluid values (idk velocity?) are concentrated in the lower left 1280x720 of the 1920x1080

nothing makes me feel stupid as glsl....

thanks
sssslamin
 
Posts: 27
Joined: Sat Jan 19, 2013 2:01 pm

Re: 2d fluid simulation on the GPU

Postby sssslamin » Tue Aug 22, 2017 2:35 pm

if anyone is reading this you can get the fluid to flow through a higher res by resetting the "feedback jacobi" op
sssslamin
 
Posts: 27
Joined: Sat Jan 19, 2013 2:01 pm

Re: 2d fluid simulation on the GPU

Postby LesAteliersNomad » Fri Dec 21, 2018 1:36 pm

Is there a fix for Jacobi shader to run smoothly on an AMD card on OSX? I experience a big fps drop on the AMD, but on nvidia it works at 60fps.

'Thanks
Attachments
Screenshot 2018-12-14 at 16.28.58.png
Screenshot 2018-12-14 at 16.28.58.png (660.56 KiB) Viewed 3169 times
LesAteliersNomad
 
Posts: 8
Joined: Tue Sep 15, 2015 5:09 am


Return to Techniques

Who is online

Users browsing this forum: No registered users and 4 guests