- Home /
GPU programming with Unity
How do I use the GPU in Unity? I'm using some extensive algorithms and want to know how I can reduce the time it takes to compute.
I read a page on here recently from a guy that did some GP GPU program$$anonymous$$g in unity and he used some kind of open source cuda equivalent maybe by $$anonymous$$icrosoft or something with some catchy name but I've forgotten, when I read the wiki article about it said that it was a 3rd type different from stream and cuda
Old ass post, but I'd just like to mention that what $$anonymous$$ountDoomTeam is talking about, is likely DirectCompute. It's basically $$anonymous$$icrosoft's Direct3D answer to CUDA and OpenCL.
Answer by Owen-Reynolds · Sep 01, 2011 at 03:56 PM
Just to bump with some so-so info:
o The physics system, Phys-X(sp?) uses the graphics card already (it was written by NVidea to show off the non-graphical uses of their chips.) I'm thinking their could be some conflicts if we throw more on the GPU.
o I believe GPU-as-CPU tricks use render-to-texture as communication back from the GPU, and that only Unity Pro allows R-to-T.
o GPU shaders work just fine, even on free. The docs cover an alternate method of setting lots of switches, instead of writing a shader. Easiest way to write real shaders is click Create->NewShader in your Assets, and also to download their package of the built-in shaders for samples. And read their "surface shaders" section.
" The physics system, Phys-X(sp?) uses the graphics card already (it was written by NVidea to show off the non-graphical uses of their chips.) "
This goes contrary to EVERYTHING I have read about PhysX in Unity. Citation needed, please.
You mean claims that Unity implements PhysX in software, on the CPU? That sounds reasonable, esp for mobile, lots of people assert it, but I've never seen a source. $$anonymous$$aybe you can citation-need those guys.
If you enable/disable hardware PhysX in the control panel, it makes zero difference to either framerate nor physics compute time within Unity. The logical conclusion is either that it's not any faster(lol), or it's software only.
Looks like the hardware/software switch is a recent addition. $$anonymous$$y version 3.5 Edit->ProjSettings->Physics doesn't one. So that may be a sign they are planning to add hardware PhysX for some targets.
You might get better answers by starting a new Question.
What ARE you going on about? The PhysX switch is part of the Nvidia control panel. Unity has no understanding of 'real' PhysX, only their particular implementation of it.
(Why would there be a hardware physics switch in Unity if it didn't support hardware? Derp!)
I'm not particularly after 'answers', here, I'm mostly attempting to stop you disse$$anonymous$$ating completely incorrect information.
Answer by Sigil · Sep 01, 2011 at 07:50 PM
Some more info. I don't think this requires Unity Pro either.
If you have an NVidia video card you can write your algorithms up using CUDA in a separate DLL and then call them from a Mono/Unity DLL (giving you access from scripts etc). Something like this (from the Mono/Unity DLL):
using System;
...
using System.Runtime.InteropServices;
namespace MyMonoDllPackage
{
public class MyCudaBehaviour : MonoBehaviour
{
[DllImport("MyCudaFunctions.dll")]
public static extern void DoSomethingInCuda(float[] aSomeData);
public void Update()
{
float[] tSomeData = { 0.0f, 0.1f, 0.2f, 0.4f };
DoSomethingInCuda(tSomeData);
}
}
}
Answer by MountDoomTeam · Feb 02, 2013 at 09:02 AM
DirectCompute dll
or
https://github.com/unity3d-jp/unity-doc/blob/master/en/ComputeShaders.txt
http://docs.unity3d.com/Documentation/Manual/ComputeShaders.html