Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Floating point Textures in Compute Shaders

Discussion in 'Shaders' started by SkullBearer666, May 20, 2016.

  1. SkullBearer666

    SkullBearer666

    Joined:
    Oct 26, 2015
    Posts:
    3
    Hi,
    I am kind of new to Compute shaders, so I might be missing something basic, hence I apologize in advance if this sounds too noobish.

    I am trying to do fluid simulation in 3d. I have implemented the same in 2d using a pixel shader. I passed the x and y values of velocity as the r and g channel of a floating point render texture and the pressure as the b channel and used Graphics.Blit to run the simulation. It all worked fine.

    Now I am trying to do the same in 3d but using a ARGBF texture3D to store the x,y and z values of velocity in r,g and b respectively and pressure in a. This is how I initialize them in cs.

    Code (CSharp):
    1.             FieldT = new RenderTexture(dimension, dimension, 0, RenderTextureFormat.ARGBFloat);
    2.             FieldT.volumeDepth = dimension;
    3.             FieldT.isVolume = true;
    4.             FieldT.enableRandomWrite = true;
    5.             FieldT.Create();
    6.  
    7.             FieldCopyT = new RenderTexture(dimension, dimension, 0, RenderTextureFormat.ARGBFloat);
    8.             FieldCopyT.volumeDepth = dimension;
    9.             FieldCopyT.isVolume = true;
    10.             FieldCopyT.enableRandomWrite = true;
    11.             FieldCopyT.Create();
    12.  
    And in my Compute shader:

    Code (CSharp):
    1. Texture3D<float4> FieldT;
    2. RWTexture3D<float4> FieldCopyT;
    Then I use them, for example, as :

    Code (CSharp):
    1.     float4 FieldCentre = FieldT[id + int3( 0, 0, 0)];
    2.     float4 FieldRight  = FieldT[id + int3( 1, 0, 0)];
    3.     float4 FieldLeft   = FieldT[id + int3(-1, 0, 0)];
    4.     float4 FieldTop    = FieldT[id + int3( 0, 1, 0)];
    5.     float4 FieldDown   = FieldT[id + int3( 0,-1, 0)];
    6.     float4 FieldRoof   = FieldT[id + int3( 0, 0, 1)];
    7.     float4 FieldBase   = FieldT[id + int3( 0, 0,-1)];
    And also as :

    Code (CSharp):
    1.     float3 Was         = UV - dt*FieldStep*FieldCentre.xyz;      
    2.     FieldCentre.xyz = FieldT.SampleLevel(_LinearClamp,Was,0).xyz;    
    However, unlike in 2D, the Compute shader seems to be ignoring the fact that it is a Floating point texture, as when I run it, I only seem to be getting flow in the positive xyz directions (so I assume it is setting any negative value to zero). Is there a way to make the compute shader treat the texture as signed float or am I missing something very simple?
     
    Last edited: May 20, 2016
  2. SkullBearer666

    SkullBearer666

    Joined:
    Oct 26, 2015
    Posts:
    3