I am trying to implement an HLSL Shader in Unity 5 which displaces the vertices of a sphere.
What I'm trying to work out is how do I effectively pass information from one frame to the next so that I can 'save' the calculated velocities of each vertex.
On an Nvidia dev site I found an interesting article discussing GPU fluid dynamics simulations. In that article, they state:
On the GPU, though, the output of fragment processors is always written to the frame buffer. Think of the frame buffer as a two-dimensional array that cannot be directly read. There are two ways to get the contents of the frame buffer into a texture that can be read:
- Copy to texture (CTT) copies from the frame buffer to a texture.
- Render to texture (RTT) uses a texture as the frame buffer so the GPU can write directly to it.
I remember reading (source lost) that in Unity, the RenderTexture
is an FBO, but after hours of searching, I still don't understand how to implement this (if this even is the correct way to achieve this). My current thought was that I would create 2 passes, one to calculate the new velocities from the existing velocities and then a second to manipulate the mesh.
I've considered writing a GPGPU Shader, but I desperately want all of this functionality contained within a single Shader.
I'm at a dead end, can someone please just point me in the right direction.
Clarification:
In a Unity Compute Shader
I can render / output whatever I want to a render texture(s):
Render Textures can also be written into from compute shaders, if they have “random access” flag set (“unordered access view” in DX11), see RenderTexture.enableRandomWrite. http://docs.unity3d.com/Manual/ComputeShaders.html
Also note:
DirectX10 introduced a new feature known as Stream Output that is very useful for implementing particle systems. OpenGL followed in version 3.0 with the same feature and named it Transform Feedback. http://ogldev.atspace.co.uk/www/tutorial28/tutorial28.html
Can I do this in a normal shader? Is there a stream output in Unity 5.1+. Is there a transform feedback option?